1 //===-- llvm-lto: a simple command-line program to link modules with LTO --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This program takes in a list of bitcode files, links them, performs link-time
11 // optimization, and outputs an object file.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/CodeGen/CommandFlags.h"
16 #include "llvm/LTO/LTOCodeGenerator.h"
17 #include "llvm/LTO/LTOModule.h"
18 #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"
23 #include "llvm/Support/TargetSelect.h"
28 DisableOpt("disable-opt", cl::init(false),
29 cl::desc("Do not run any optimization passes"));
32 DisableInline("disable-inlining", cl::init(false),
33 cl::desc("Do not run the inliner pass"));
36 DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
37 cl::desc("Do not run the GVN load PRE pass"));
39 static cl::list<std::string>
40 InputFilenames(cl::Positional, cl::OneOrMore,
41 cl::desc("<input bitcode files>"));
43 static cl::opt<std::string>
44 OutputFilename("o", cl::init(""),
45 cl::desc("Override output filename"),
46 cl::value_desc("filename"));
48 static cl::list<std::string>
49 ExportedSymbols("exported-symbol",
50 cl::desc("Symbol to export from the resulting object file"),
54 int main(int argc, char **argv) {
55 // Print a stack trace if we signal out.
56 sys::PrintStackTraceOnErrorSignal();
57 PrettyStackTraceProgram X(argc, argv);
59 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
60 cl::ParseCommandLineOptions(argc, argv, "llvm LTO linker\n");
62 // Initialize the configured targets.
63 InitializeAllTargets();
64 InitializeAllTargetMCs();
65 InitializeAllAsmPrinters();
66 InitializeAllAsmParsers();
68 // 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;
92 LTOCodeGenerator CodeGen;
94 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC);
95 CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF);
96 CodeGen.setTargetOptions(Options);
98 for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) {
100 OwningPtr<LTOModule> Module(LTOModule::makeLTOModule(InputFilenames[i].c_str(),
102 if (!error.empty()) {
103 errs() << argv[0] << ": error loading file '" << InputFilenames[i]
104 << "': " << error << "\n";
109 if (!CodeGen.addModule(Module.get(), error)) {
110 errs() << argv[0] << ": error adding file '" << InputFilenames[i]
111 << "': " << error << "\n";
116 // 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());
120 if (!OutputFilename.empty()) {
122 std::string ErrorInfo;
123 const void *Code = CodeGen.compile(&len, DisableOpt, DisableInline,
124 DisableGVNLoadPRE, ErrorInfo);
127 << ": error compiling the code: " << ErrorInfo << "\n";
131 raw_fd_ostream FileStream(OutputFilename.c_str(), ErrorInfo);
132 if (!ErrorInfo.empty()) {
133 errs() << argv[0] << ": error opening the file '" << OutputFilename
134 << "': " << ErrorInfo << "\n";
138 FileStream.write(reinterpret_cast<const char *>(Code), len);
140 std::string ErrorInfo;
141 const char *OutputName = NULL;
142 if (!CodeGen.compile_to_file(&OutputName, DisableOpt, DisableInline,
143 DisableGVNLoadPRE, ErrorInfo)) {
145 << ": error compiling the code: " << ErrorInfo
150 outs() << "Wrote native object file '" << OutputName << "'\n";