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 int main(int argc, char **argv) {
49 // Print a stack trace if we signal out.
50 sys::PrintStackTraceOnErrorSignal();
51 PrettyStackTraceProgram X(argc, argv);
53 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
54 cl::ParseCommandLineOptions(argc, argv, "llvm LTO linker\n");
56 // Initialize the configured targets.
57 InitializeAllTargets();
58 InitializeAllTargetMCs();
59 InitializeAllAsmPrinters();
60 InitializeAllAsmParsers();
62 // set up the TargetOptions for the machine
63 TargetOptions Options;
64 Options.LessPreciseFPMADOption = EnableFPMAD;
65 Options.NoFramePointerElim = DisableFPElim;
66 Options.AllowFPOpFusion = FuseFPOps;
67 Options.UnsafeFPMath = EnableUnsafeFPMath;
68 Options.NoInfsFPMath = EnableNoInfsFPMath;
69 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
70 Options.HonorSignDependentRoundingFPMathOption =
71 EnableHonorSignDependentRoundingFPMath;
72 Options.UseSoftFloat = GenerateSoftFloatCalls;
73 if (FloatABIForCalls != FloatABI::Default)
74 Options.FloatABIType = FloatABIForCalls;
75 Options.NoZerosInBSS = DontPlaceZerosInBSS;
76 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
77 Options.DisableTailCalls = DisableTailCalls;
78 Options.StackAlignmentOverride = OverrideStackAlignment;
79 Options.TrapFuncName = TrapFuncName;
80 Options.PositionIndependentExecutable = EnablePIE;
81 Options.EnableSegmentedStacks = SegmentedStacks;
82 Options.UseInitArray = UseInitArray;
85 std::string ErrorMessage;
87 LTOCodeGenerator CodeGen;
89 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC);
90 CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF);
91 CodeGen.setTargetOptions(Options);
93 for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) {
95 OwningPtr<LTOModule> Module(LTOModule::makeLTOModule(InputFilenames[i].c_str(),
98 errs() << argv[0] << ": error loading file '" << InputFilenames[i]
99 << "': " << error << "\n";
104 if (!CodeGen.addModule(Module.get(), error)) {
105 errs() << argv[0] << ": error adding file '" << InputFilenames[i]
106 << "': " << error << "\n";
111 if (!OutputFilename.empty()) {
113 std::string ErrorInfo;
114 const void *Code = CodeGen.compile(&len, DisableOpt, DisableInline,
115 DisableGVNLoadPRE, ErrorInfo);
118 << ": error compiling the code: " << ErrorInfo << "\n";
122 raw_fd_ostream FileStream(OutputFilename.c_str(), ErrorInfo);
123 if (!ErrorInfo.empty()) {
124 errs() << argv[0] << ": error opening the file '" << OutputFilename
125 << "': " << ErrorInfo << "\n";
129 FileStream.write(reinterpret_cast<const char *>(Code), len);
131 std::string ErrorInfo;
132 const char *OutputName = NULL;
133 if (!CodeGen.compile_to_file(&OutputName, DisableOpt, DisableInline,
134 DisableGVNLoadPRE, ErrorInfo)) {
136 << ": error compiling the code: " << ErrorInfo
141 outs() << "Wrote native object file '" << OutputName << "'\n";