blob: 3ecd13f5c7153b5ae019672a4878186e0a5cb41c [file] [log] [blame]
Peter Collingbourne88fae0e2013-09-19 22:15:52 +00001//===-- llvm-lto: a simple command-line program to link modules with LTO --===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This program takes in a list of bitcode files, links them, performs link-time
11// optimization, and outputs an object file.
12//
13//===----------------------------------------------------------------------===//
14
Rafael Espindolac13c9e52013-09-30 16:39:19 +000015#include "llvm/CodeGen/CommandFlags.h"
Peter Collingbournecc488542013-09-24 23:52:22 +000016#include "llvm/LTO/LTOCodeGenerator.h"
17#include "llvm/LTO/LTOModule.h"
Peter Collingbourne88fae0e2013-09-19 22:15:52 +000018#include "llvm/Support/CommandLine.h"
19#include "llvm/Support/ManagedStatic.h"
20#include "llvm/Support/PrettyStackTrace.h"
21#include "llvm/Support/Signals.h"
22#include "llvm/Support/raw_ostream.h"
Peter Collingbournecc488542013-09-24 23:52:22 +000023#include "llvm/Support/TargetSelect.h"
Peter Collingbourne88fae0e2013-09-19 22:15:52 +000024
25using namespace llvm;
26
Rafael Espindolac13c9e52013-09-30 16:39:19 +000027static cl::opt<bool>
28DisableOpt("disable-opt", cl::init(false),
29 cl::desc("Do not run any optimization passes"));
Peter Collingbourne88fae0e2013-09-19 22:15:52 +000030
Rafael Espindolac13c9e52013-09-30 16:39:19 +000031static cl::opt<bool>
32DisableInline("disable-inlining", cl::init(false),
33 cl::desc("Do not run the inliner pass"));
34
35static cl::opt<bool>
36DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
37 cl::desc("Do not run the GVN load PRE pass"));
38
39static cl::list<std::string>
40InputFilenames(cl::Positional, cl::OneOrMore,
41 cl::desc("<input bitcode files>"));
42
43static cl::opt<std::string>
44OutputFilename("o", cl::init(""),
45 cl::desc("Override output filename"),
46 cl::value_desc("filename"));
Peter Collingbourne88fae0e2013-09-19 22:15:52 +000047
Rafael Espindolab9643662013-10-02 14:12:56 +000048static cl::list<std::string>
49ExportedSymbols("exported-symbol",
50 cl::desc("Symbol to export from the resulting object file"),
51 cl::ZeroOrMore);
52
53
Peter Collingbourne88fae0e2013-09-19 22:15:52 +000054int main(int argc, char **argv) {
55 // Print a stack trace if we signal out.
56 sys::PrintStackTraceOnErrorSignal();
57 PrettyStackTraceProgram X(argc, argv);
58
59 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
60 cl::ParseCommandLineOptions(argc, argv, "llvm LTO linker\n");
61
Peter Collingbournecc488542013-09-24 23:52:22 +000062 // Initialize the configured targets.
63 InitializeAllTargets();
64 InitializeAllTargetMCs();
65 InitializeAllAsmPrinters();
66 InitializeAllAsmParsers();
67
Rafael Espindolac13c9e52013-09-30 16:39:19 +000068 // set up the TargetOptions for the machine
69 TargetOptions Options;
70 Options.LessPreciseFPMADOption = EnableFPMAD;
71 Options.NoFramePointerElim = DisableFPElim;
72 Options.AllowFPOpFusion = FuseFPOps;
73 Options.UnsafeFPMath = EnableUnsafeFPMath;
74 Options.NoInfsFPMath = EnableNoInfsFPMath;
75 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
76 Options.HonorSignDependentRoundingFPMathOption =
77 EnableHonorSignDependentRoundingFPMath;
78 Options.UseSoftFloat = GenerateSoftFloatCalls;
79 if (FloatABIForCalls != FloatABI::Default)
80 Options.FloatABIType = FloatABIForCalls;
81 Options.NoZerosInBSS = DontPlaceZerosInBSS;
82 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
83 Options.DisableTailCalls = DisableTailCalls;
84 Options.StackAlignmentOverride = OverrideStackAlignment;
85 Options.TrapFuncName = TrapFuncName;
86 Options.PositionIndependentExecutable = EnablePIE;
87 Options.EnableSegmentedStacks = SegmentedStacks;
88 Options.UseInitArray = UseInitArray;
89
Peter Collingbourne88fae0e2013-09-19 22:15:52 +000090 unsigned BaseArg = 0;
Peter Collingbourne88fae0e2013-09-19 22:15:52 +000091
Peter Collingbournecc488542013-09-24 23:52:22 +000092 LTOCodeGenerator CodeGen;
Peter Collingbourne88fae0e2013-09-19 22:15:52 +000093
Peter Collingbournecc488542013-09-24 23:52:22 +000094 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC);
95 CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF);
Rafael Espindolac13c9e52013-09-30 16:39:19 +000096 CodeGen.setTargetOptions(Options);
Peter Collingbourne88fae0e2013-09-19 22:15:52 +000097
98 for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) {
Peter Collingbournecc488542013-09-24 23:52:22 +000099 std::string error;
100 OwningPtr<LTOModule> Module(LTOModule::makeLTOModule(InputFilenames[i].c_str(),
Rafael Espindolac13c9e52013-09-30 16:39:19 +0000101 Options, error));
Peter Collingbournecc488542013-09-24 23:52:22 +0000102 if (!error.empty()) {
Peter Collingbourne88fae0e2013-09-19 22:15:52 +0000103 errs() << argv[0] << ": error loading file '" << InputFilenames[i]
Peter Collingbournecc488542013-09-24 23:52:22 +0000104 << "': " << error << "\n";
Peter Collingbourne88fae0e2013-09-19 22:15:52 +0000105 return 1;
106 }
107
Peter Collingbournecc488542013-09-24 23:52:22 +0000108
109 if (!CodeGen.addModule(Module.get(), error)) {
Peter Collingbourne88fae0e2013-09-19 22:15:52 +0000110 errs() << argv[0] << ": error adding file '" << InputFilenames[i]
Peter Collingbournecc488542013-09-24 23:52:22 +0000111 << "': " << error << "\n";
Peter Collingbourne88fae0e2013-09-19 22:15:52 +0000112 return 1;
113 }
Peter Collingbourne88fae0e2013-09-19 22:15:52 +0000114 }
115
Rafael Espindolab9643662013-10-02 14:12:56 +0000116 // Add all the exported symbols to the table of symbols to preserve.
117 for (unsigned i = 0; i < ExportedSymbols.size(); ++i)
118 CodeGen.addMustPreserveSymbol(ExportedSymbols[i].c_str());
119
Peter Collingbourne88fae0e2013-09-19 22:15:52 +0000120 if (!OutputFilename.empty()) {
121 size_t len = 0;
Peter Collingbournecc488542013-09-24 23:52:22 +0000122 std::string ErrorInfo;
Rafael Espindolac13c9e52013-09-30 16:39:19 +0000123 const void *Code = CodeGen.compile(&len, DisableOpt, DisableInline,
124 DisableGVNLoadPRE, ErrorInfo);
Peter Collingbourne88fae0e2013-09-19 22:15:52 +0000125 if (Code == NULL) {
126 errs() << argv[0]
Peter Collingbournecc488542013-09-24 23:52:22 +0000127 << ": error compiling the code: " << ErrorInfo << "\n";
Peter Collingbourne88fae0e2013-09-19 22:15:52 +0000128 return 1;
129 }
130
Peter Collingbourne88fae0e2013-09-19 22:15:52 +0000131 raw_fd_ostream FileStream(OutputFilename.c_str(), ErrorInfo);
132 if (!ErrorInfo.empty()) {
133 errs() << argv[0] << ": error opening the file '" << OutputFilename
134 << "': " << ErrorInfo << "\n";
135 return 1;
136 }
137
138 FileStream.write(reinterpret_cast<const char *>(Code), len);
139 } else {
Peter Collingbournecc488542013-09-24 23:52:22 +0000140 std::string ErrorInfo;
Peter Collingbourne88fae0e2013-09-19 22:15:52 +0000141 const char *OutputName = NULL;
Rafael Espindolac13c9e52013-09-30 16:39:19 +0000142 if (!CodeGen.compile_to_file(&OutputName, DisableOpt, DisableInline,
143 DisableGVNLoadPRE, ErrorInfo)) {
Peter Collingbourne88fae0e2013-09-19 22:15:52 +0000144 errs() << argv[0]
Peter Collingbournecc488542013-09-24 23:52:22 +0000145 << ": error compiling the code: " << ErrorInfo
Peter Collingbourne88fae0e2013-09-19 22:15:52 +0000146 << "\n";
147 return 1;
148 }
149
150 outs() << "Wrote native object file '" << OutputName << "'\n";
151 }
152
Peter Collingbourne88fae0e2013-09-19 22:15:52 +0000153 return 0;
154}