blob: 2f51c0c3d05769facffdd986990aa04bfe2ed1a8 [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
48int main(int argc, char **argv) {
49 // Print a stack trace if we signal out.
50 sys::PrintStackTraceOnErrorSignal();
51 PrettyStackTraceProgram X(argc, argv);
52
53 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
54 cl::ParseCommandLineOptions(argc, argv, "llvm LTO linker\n");
55
Peter Collingbournecc488542013-09-24 23:52:22 +000056 // Initialize the configured targets.
57 InitializeAllTargets();
58 InitializeAllTargetMCs();
59 InitializeAllAsmPrinters();
60 InitializeAllAsmParsers();
61
Rafael Espindolac13c9e52013-09-30 16:39:19 +000062 // 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;
83
Peter Collingbourne88fae0e2013-09-19 22:15:52 +000084 unsigned BaseArg = 0;
85 std::string ErrorMessage;
86
Peter Collingbournecc488542013-09-24 23:52:22 +000087 LTOCodeGenerator CodeGen;
Peter Collingbourne88fae0e2013-09-19 22:15:52 +000088
Peter Collingbournecc488542013-09-24 23:52:22 +000089 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC);
90 CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF);
Rafael Espindolac13c9e52013-09-30 16:39:19 +000091 CodeGen.setTargetOptions(Options);
Peter Collingbourne88fae0e2013-09-19 22:15:52 +000092
93 for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) {
Peter Collingbournecc488542013-09-24 23:52:22 +000094 std::string error;
95 OwningPtr<LTOModule> Module(LTOModule::makeLTOModule(InputFilenames[i].c_str(),
Rafael Espindolac13c9e52013-09-30 16:39:19 +000096 Options, error));
Peter Collingbournecc488542013-09-24 23:52:22 +000097 if (!error.empty()) {
Peter Collingbourne88fae0e2013-09-19 22:15:52 +000098 errs() << argv[0] << ": error loading file '" << InputFilenames[i]
Peter Collingbournecc488542013-09-24 23:52:22 +000099 << "': " << error << "\n";
Peter Collingbourne88fae0e2013-09-19 22:15:52 +0000100 return 1;
101 }
102
Peter Collingbournecc488542013-09-24 23:52:22 +0000103
104 if (!CodeGen.addModule(Module.get(), error)) {
Peter Collingbourne88fae0e2013-09-19 22:15:52 +0000105 errs() << argv[0] << ": error adding file '" << InputFilenames[i]
Peter Collingbournecc488542013-09-24 23:52:22 +0000106 << "': " << error << "\n";
Peter Collingbourne88fae0e2013-09-19 22:15:52 +0000107 return 1;
108 }
Peter Collingbourne88fae0e2013-09-19 22:15:52 +0000109 }
110
111 if (!OutputFilename.empty()) {
112 size_t len = 0;
Peter Collingbournecc488542013-09-24 23:52:22 +0000113 std::string ErrorInfo;
Rafael Espindolac13c9e52013-09-30 16:39:19 +0000114 const void *Code = CodeGen.compile(&len, DisableOpt, DisableInline,
115 DisableGVNLoadPRE, ErrorInfo);
Peter Collingbourne88fae0e2013-09-19 22:15:52 +0000116 if (Code == NULL) {
117 errs() << argv[0]
Peter Collingbournecc488542013-09-24 23:52:22 +0000118 << ": error compiling the code: " << ErrorInfo << "\n";
Peter Collingbourne88fae0e2013-09-19 22:15:52 +0000119 return 1;
120 }
121
Peter Collingbourne88fae0e2013-09-19 22:15:52 +0000122 raw_fd_ostream FileStream(OutputFilename.c_str(), ErrorInfo);
123 if (!ErrorInfo.empty()) {
124 errs() << argv[0] << ": error opening the file '" << OutputFilename
125 << "': " << ErrorInfo << "\n";
126 return 1;
127 }
128
129 FileStream.write(reinterpret_cast<const char *>(Code), len);
130 } else {
Peter Collingbournecc488542013-09-24 23:52:22 +0000131 std::string ErrorInfo;
Peter Collingbourne88fae0e2013-09-19 22:15:52 +0000132 const char *OutputName = NULL;
Rafael Espindolac13c9e52013-09-30 16:39:19 +0000133 if (!CodeGen.compile_to_file(&OutputName, DisableOpt, DisableInline,
134 DisableGVNLoadPRE, ErrorInfo)) {
Peter Collingbourne88fae0e2013-09-19 22:15:52 +0000135 errs() << argv[0]
Peter Collingbournecc488542013-09-24 23:52:22 +0000136 << ": error compiling the code: " << ErrorInfo
Peter Collingbourne88fae0e2013-09-19 22:15:52 +0000137 << "\n";
138 return 1;
139 }
140
141 outs() << "Wrote native object file '" << OutputName << "'\n";
142 }
143
Peter Collingbourne88fae0e2013-09-19 22:15:52 +0000144 return 0;
145}