Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 1 | //===-- 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 Espindola | c13c9e5 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/CommandFlags.h" |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 16 | #include "llvm/LTO/LTOCodeGenerator.h" |
| 17 | #include "llvm/LTO/LTOModule.h" |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 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" |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 23 | #include "llvm/Support/TargetSelect.h" |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace llvm; |
| 26 | |
Rafael Espindola | c13c9e5 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 27 | static cl::opt<bool> |
| 28 | DisableOpt("disable-opt", cl::init(false), |
| 29 | cl::desc("Do not run any optimization passes")); |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 30 | |
Rafael Espindola | c13c9e5 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 31 | static cl::opt<bool> |
| 32 | DisableInline("disable-inlining", cl::init(false), |
| 33 | cl::desc("Do not run the inliner pass")); |
| 34 | |
| 35 | static cl::opt<bool> |
| 36 | DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false), |
| 37 | cl::desc("Do not run the GVN load PRE pass")); |
| 38 | |
| 39 | static cl::list<std::string> |
| 40 | InputFilenames(cl::Positional, cl::OneOrMore, |
| 41 | cl::desc("<input bitcode files>")); |
| 42 | |
| 43 | static cl::opt<std::string> |
| 44 | OutputFilename("o", cl::init(""), |
| 45 | cl::desc("Override output filename"), |
| 46 | cl::value_desc("filename")); |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 47 | |
Rafael Espindola | b964366 | 2013-10-02 14:12:56 +0000 | [diff] [blame] | 48 | static cl::list<std::string> |
| 49 | ExportedSymbols("exported-symbol", |
| 50 | cl::desc("Symbol to export from the resulting object file"), |
| 51 | cl::ZeroOrMore); |
| 52 | |
Rafael Espindola | 4389009 | 2013-10-03 18:29:09 +0000 | [diff] [blame] | 53 | static cl::list<std::string> |
| 54 | DSOSymbols("dso-symbol", |
| 55 | cl::desc("Symbol to put in the symtab in the resulting dso"), |
| 56 | cl::ZeroOrMore); |
Rafael Espindola | b964366 | 2013-10-02 14:12:56 +0000 | [diff] [blame] | 57 | |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 58 | int main(int argc, char **argv) { |
| 59 | // Print a stack trace if we signal out. |
| 60 | sys::PrintStackTraceOnErrorSignal(); |
| 61 | PrettyStackTraceProgram X(argc, argv); |
| 62 | |
| 63 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 64 | cl::ParseCommandLineOptions(argc, argv, "llvm LTO linker\n"); |
| 65 | |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 66 | // Initialize the configured targets. |
| 67 | InitializeAllTargets(); |
| 68 | InitializeAllTargetMCs(); |
| 69 | InitializeAllAsmPrinters(); |
| 70 | InitializeAllAsmParsers(); |
| 71 | |
Rafael Espindola | c13c9e5 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 72 | // set up the TargetOptions for the machine |
| 73 | TargetOptions Options; |
| 74 | Options.LessPreciseFPMADOption = EnableFPMAD; |
| 75 | Options.NoFramePointerElim = DisableFPElim; |
| 76 | Options.AllowFPOpFusion = FuseFPOps; |
| 77 | Options.UnsafeFPMath = EnableUnsafeFPMath; |
| 78 | Options.NoInfsFPMath = EnableNoInfsFPMath; |
| 79 | Options.NoNaNsFPMath = EnableNoNaNsFPMath; |
| 80 | Options.HonorSignDependentRoundingFPMathOption = |
| 81 | EnableHonorSignDependentRoundingFPMath; |
| 82 | Options.UseSoftFloat = GenerateSoftFloatCalls; |
| 83 | if (FloatABIForCalls != FloatABI::Default) |
| 84 | Options.FloatABIType = FloatABIForCalls; |
| 85 | Options.NoZerosInBSS = DontPlaceZerosInBSS; |
| 86 | Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt; |
| 87 | Options.DisableTailCalls = DisableTailCalls; |
| 88 | Options.StackAlignmentOverride = OverrideStackAlignment; |
| 89 | Options.TrapFuncName = TrapFuncName; |
| 90 | Options.PositionIndependentExecutable = EnablePIE; |
| 91 | Options.EnableSegmentedStacks = SegmentedStacks; |
| 92 | Options.UseInitArray = UseInitArray; |
| 93 | |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 94 | unsigned BaseArg = 0; |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 95 | |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 96 | LTOCodeGenerator CodeGen; |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 97 | |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 98 | CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC); |
| 99 | CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF); |
Rafael Espindola | c13c9e5 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 100 | CodeGen.setTargetOptions(Options); |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 101 | |
| 102 | for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) { |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 103 | std::string error; |
| 104 | OwningPtr<LTOModule> Module(LTOModule::makeLTOModule(InputFilenames[i].c_str(), |
Rafael Espindola | c13c9e5 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 105 | Options, error)); |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 106 | if (!error.empty()) { |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 107 | errs() << argv[0] << ": error loading file '" << InputFilenames[i] |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 108 | << "': " << error << "\n"; |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 109 | return 1; |
| 110 | } |
| 111 | |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 112 | |
| 113 | if (!CodeGen.addModule(Module.get(), error)) { |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 114 | errs() << argv[0] << ": error adding file '" << InputFilenames[i] |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 115 | << "': " << error << "\n"; |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 116 | return 1; |
| 117 | } |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Rafael Espindola | b964366 | 2013-10-02 14:12:56 +0000 | [diff] [blame] | 120 | // Add all the exported symbols to the table of symbols to preserve. |
| 121 | for (unsigned i = 0; i < ExportedSymbols.size(); ++i) |
| 122 | CodeGen.addMustPreserveSymbol(ExportedSymbols[i].c_str()); |
| 123 | |
Rafael Espindola | 4389009 | 2013-10-03 18:29:09 +0000 | [diff] [blame] | 124 | // Add all the dso symbols to the table of symbols to expose. |
| 125 | for (unsigned i = 0; i < DSOSymbols.size(); ++i) |
| 126 | CodeGen.addDSOSymbol(DSOSymbols[i].c_str()); |
| 127 | |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 128 | if (!OutputFilename.empty()) { |
| 129 | size_t len = 0; |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 130 | std::string ErrorInfo; |
Rafael Espindola | c13c9e5 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 131 | const void *Code = CodeGen.compile(&len, DisableOpt, DisableInline, |
| 132 | DisableGVNLoadPRE, ErrorInfo); |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 133 | if (Code == NULL) { |
| 134 | errs() << argv[0] |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 135 | << ": error compiling the code: " << ErrorInfo << "\n"; |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 136 | return 1; |
| 137 | } |
| 138 | |
Rafael Espindola | ec2ac89 | 2013-10-04 21:40:54 +0000 | [diff] [blame] | 139 | raw_fd_ostream FileStream(OutputFilename.c_str(), ErrorInfo, |
| 140 | sys::fs::F_Binary); |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 141 | if (!ErrorInfo.empty()) { |
| 142 | errs() << argv[0] << ": error opening the file '" << OutputFilename |
| 143 | << "': " << ErrorInfo << "\n"; |
| 144 | return 1; |
| 145 | } |
| 146 | |
| 147 | FileStream.write(reinterpret_cast<const char *>(Code), len); |
| 148 | } else { |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 149 | std::string ErrorInfo; |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 150 | const char *OutputName = NULL; |
Rafael Espindola | c13c9e5 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 151 | if (!CodeGen.compile_to_file(&OutputName, DisableOpt, DisableInline, |
| 152 | DisableGVNLoadPRE, ErrorInfo)) { |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 153 | errs() << argv[0] |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 154 | << ": error compiling the code: " << ErrorInfo |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 155 | << "\n"; |
| 156 | return 1; |
| 157 | } |
| 158 | |
| 159 | outs() << "Wrote native object file '" << OutputName << "'\n"; |
| 160 | } |
| 161 | |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 162 | return 0; |
| 163 | } |