blob: 3c950ba050211ed5031a705014db2de4ac89c8dc [file] [log] [blame]
Peter Collingbourne4e380b02013-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 Espindola282a4702013-10-31 20:51:58 +000015#include "llvm/ADT/StringSet.h"
Rafael Espindola0b385c72013-09-30 16:39:19 +000016#include "llvm/CodeGen/CommandFlags.h"
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000017#include "llvm/LTO/LTOCodeGenerator.h"
18#include "llvm/LTO/LTOModule.h"
Peter Collingbourne4e380b02013-09-19 22:15:52 +000019#include "llvm/Support/CommandLine.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000020#include "llvm/Support/FileSystem.h"
Peter Collingbourne4e380b02013-09-19 22:15:52 +000021#include "llvm/Support/ManagedStatic.h"
22#include "llvm/Support/PrettyStackTrace.h"
23#include "llvm/Support/Signals.h"
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000024#include "llvm/Support/TargetSelect.h"
Chandler Carruth07baed52014-01-13 08:04:33 +000025#include "llvm/Support/raw_ostream.h"
Peter Collingbourne4e380b02013-09-19 22:15:52 +000026
27using namespace llvm;
28
Rafael Espindola0b385c72013-09-30 16:39:19 +000029static cl::opt<bool>
30DisableOpt("disable-opt", cl::init(false),
31 cl::desc("Do not run any optimization passes"));
Peter Collingbourne4e380b02013-09-19 22:15:52 +000032
Rafael Espindola0b385c72013-09-30 16:39:19 +000033static cl::opt<bool>
34DisableInline("disable-inlining", cl::init(false),
35 cl::desc("Do not run the inliner pass"));
36
37static cl::opt<bool>
38DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
39 cl::desc("Do not run the GVN load PRE pass"));
40
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +000041static cl::opt<bool>
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +000042DisableLTOVectorization("disable-lto-vectorization", cl::init(false),
43 cl::desc("Do not run loop or slp vectorization during LTO"));
44
45static cl::opt<bool>
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +000046UseDiagnosticHandler("use-diagnostic-handler", cl::init(false),
47 cl::desc("Use a diagnostic handler to test the handler interface"));
48
Rafael Espindola0b385c72013-09-30 16:39:19 +000049static cl::list<std::string>
50InputFilenames(cl::Positional, cl::OneOrMore,
51 cl::desc("<input bitcode files>"));
52
53static cl::opt<std::string>
54OutputFilename("o", cl::init(""),
55 cl::desc("Override output filename"),
56 cl::value_desc("filename"));
Peter Collingbourne4e380b02013-09-19 22:15:52 +000057
Rafael Espindoladafc53d2013-10-02 14:12:56 +000058static cl::list<std::string>
59ExportedSymbols("exported-symbol",
60 cl::desc("Symbol to export from the resulting object file"),
61 cl::ZeroOrMore);
62
Rafael Espindolacda29112013-10-03 18:29:09 +000063static cl::list<std::string>
64DSOSymbols("dso-symbol",
65 cl::desc("Symbol to put in the symtab in the resulting dso"),
66 cl::ZeroOrMore);
Rafael Espindoladafc53d2013-10-02 14:12:56 +000067
Rafael Espindola282a4702013-10-31 20:51:58 +000068namespace {
69struct ModuleInfo {
70 std::vector<bool> CanBeHidden;
71};
72}
73
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +000074void handleDiagnostics(lto_codegen_diagnostic_severity_t Severity,
75 const char *Msg, void *) {
76 switch (Severity) {
77 case LTO_DS_NOTE:
78 errs() << "note: ";
79 break;
80 case LTO_DS_REMARK:
81 errs() << "remark: ";
82 break;
83 case LTO_DS_ERROR:
84 errs() << "error: ";
85 break;
86 case LTO_DS_WARNING:
87 errs() << "warning: ";
88 break;
89 }
90 errs() << Msg << "\n";
91}
92
Peter Collingbourne4e380b02013-09-19 22:15:52 +000093int main(int argc, char **argv) {
94 // Print a stack trace if we signal out.
95 sys::PrintStackTraceOnErrorSignal();
96 PrettyStackTraceProgram X(argc, argv);
97
98 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
99 cl::ParseCommandLineOptions(argc, argv, "llvm LTO linker\n");
100
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000101 // Initialize the configured targets.
102 InitializeAllTargets();
103 InitializeAllTargetMCs();
104 InitializeAllAsmPrinters();
105 InitializeAllAsmParsers();
106
Rafael Espindola0b385c72013-09-30 16:39:19 +0000107 // set up the TargetOptions for the machine
Eli Benderskyf0f21002014-02-19 17:09:35 +0000108 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000109
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000110 unsigned BaseArg = 0;
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000111
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000112 LTOCodeGenerator CodeGen;
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000113
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000114 if (UseDiagnosticHandler)
115 CodeGen.setDiagnosticHandler(handleDiagnostics, nullptr);
116
James Molloy951e5292014-04-14 13:54:16 +0000117 switch (RelocModel) {
118 case Reloc::Static:
119 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_STATIC);
120 break;
121 case Reloc::PIC_:
122 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC);
123 break;
124 case Reloc::DynamicNoPIC:
125 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC);
126 break;
127 default:
128 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DEFAULT);
129 }
130
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000131 CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF);
Rafael Espindola0b385c72013-09-30 16:39:19 +0000132 CodeGen.setTargetOptions(Options);
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000133
Rafael Espindola282a4702013-10-31 20:51:58 +0000134 llvm::StringSet<llvm::MallocAllocator> DSOSymbolsSet;
135 for (unsigned i = 0; i < DSOSymbols.size(); ++i)
136 DSOSymbolsSet.insert(DSOSymbols[i]);
137
138 std::vector<std::string> KeptDSOSyms;
139
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000140 for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000141 std::string error;
Ahmed Charles56440fd2014-03-06 05:51:42 +0000142 std::unique_ptr<LTOModule> Module(
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000143 LTOModule::createFromFile(InputFilenames[i].c_str(), Options, error));
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000144 if (!error.empty()) {
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000145 errs() << argv[0] << ": error loading file '" << InputFilenames[i]
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000146 << "': " << error << "\n";
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000147 return 1;
148 }
149
Rafael Espindolad12b4a32014-10-25 04:06:10 +0000150 if (!CodeGen.addModule(Module.get()))
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000151 return 1;
Rafael Espindola282a4702013-10-31 20:51:58 +0000152
153 unsigned NumSyms = Module->getSymbolCount();
154 for (unsigned I = 0; I < NumSyms; ++I) {
155 StringRef Name = Module->getSymbolName(I);
156 if (!DSOSymbolsSet.count(Name))
157 continue;
158 lto_symbol_attributes Attrs = Module->getSymbolAttributes(I);
159 unsigned Scope = Attrs & LTO_SYMBOL_SCOPE_MASK;
160 if (Scope != LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN)
161 KeptDSOSyms.push_back(Name);
162 }
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000163 }
164
Rafael Espindoladafc53d2013-10-02 14:12:56 +0000165 // Add all the exported symbols to the table of symbols to preserve.
166 for (unsigned i = 0; i < ExportedSymbols.size(); ++i)
167 CodeGen.addMustPreserveSymbol(ExportedSymbols[i].c_str());
168
Rafael Espindolacda29112013-10-03 18:29:09 +0000169 // Add all the dso symbols to the table of symbols to expose.
Rafael Espindola282a4702013-10-31 20:51:58 +0000170 for (unsigned i = 0; i < KeptDSOSyms.size(); ++i)
171 CodeGen.addMustPreserveSymbol(KeptDSOSyms[i].c_str());
Rafael Espindolacda29112013-10-03 18:29:09 +0000172
Tom Roederfd1bc602014-04-25 21:46:51 +0000173 std::string attrs;
174 for (unsigned i = 0; i < MAttrs.size(); ++i) {
175 if (i > 0)
176 attrs.append(",");
177 attrs.append(MAttrs[i]);
178 }
179
180 if (!attrs.empty())
181 CodeGen.setAttr(attrs.c_str());
182
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000183 if (!OutputFilename.empty()) {
184 size_t len = 0;
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000185 std::string ErrorInfo;
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000186 const void *Code =
187 CodeGen.compile(&len, DisableOpt, DisableInline, DisableGVNLoadPRE,
188 DisableLTOVectorization, ErrorInfo);
Craig Toppere6cb63e2014-04-25 04:24:47 +0000189 if (!Code) {
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000190 errs() << argv[0]
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000191 << ": error compiling the code: " << ErrorInfo << "\n";
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000192 return 1;
193 }
194
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000195 std::error_code EC;
196 raw_fd_ostream FileStream(OutputFilename, EC, sys::fs::F_None);
197 if (EC) {
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000198 errs() << argv[0] << ": error opening the file '" << OutputFilename
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000199 << "': " << EC.message() << "\n";
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000200 return 1;
201 }
202
203 FileStream.write(reinterpret_cast<const char *>(Code), len);
204 } else {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000205 std::string ErrorInfo;
Craig Toppere6cb63e2014-04-25 04:24:47 +0000206 const char *OutputName = nullptr;
Rafael Espindola0b385c72013-09-30 16:39:19 +0000207 if (!CodeGen.compile_to_file(&OutputName, DisableOpt, DisableInline,
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000208 DisableGVNLoadPRE, DisableLTOVectorization,
209 ErrorInfo)) {
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000210 errs() << argv[0]
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000211 << ": error compiling the code: " << ErrorInfo
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000212 << "\n";
213 return 1;
214 }
215
216 outs() << "Wrote native object file '" << OutputName << "'\n";
217 }
218
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000219 return 0;
220}