Move a call to getGlobalContext out of lib/LTO.
[oota-llvm.git] / tools / llvm-lto / llvm-lto.cpp
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
15 #include "llvm/ADT/StringSet.h"
16 #include "llvm/Bitcode/ReaderWriter.h"
17 #include "llvm/CodeGen/CommandFlags.h"
18 #include "llvm/IR/DiagnosticPrinter.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/LTO/LTOCodeGenerator.h"
21 #include "llvm/LTO/LTOModule.h"
22 #include "llvm/Object/FunctionIndexObjectFile.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/FileSystem.h"
25 #include "llvm/Support/ManagedStatic.h"
26 #include "llvm/Support/PrettyStackTrace.h"
27 #include "llvm/Support/Signals.h"
28 #include "llvm/Support/TargetSelect.h"
29 #include "llvm/Support/ToolOutputFile.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include <list>
32
33 using namespace llvm;
34
35 static cl::opt<char>
36 OptLevel("O",
37          cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
38                   "(default = '-O2')"),
39          cl::Prefix,
40          cl::ZeroOrMore,
41          cl::init('2'));
42
43 static cl::opt<bool> DisableVerify(
44     "disable-verify", cl::init(false),
45     cl::desc("Do not run the verifier during the optimization pipeline"));
46
47 static cl::opt<bool>
48 DisableInline("disable-inlining", cl::init(false),
49   cl::desc("Do not run the inliner pass"));
50
51 static cl::opt<bool>
52 DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
53   cl::desc("Do not run the GVN load PRE pass"));
54
55 static cl::opt<bool>
56 DisableLTOVectorization("disable-lto-vectorization", cl::init(false),
57   cl::desc("Do not run loop or slp vectorization during LTO"));
58
59 static cl::opt<bool>
60 UseDiagnosticHandler("use-diagnostic-handler", cl::init(false),
61   cl::desc("Use a diagnostic handler to test the handler interface"));
62
63 static cl::opt<bool>
64     ThinLTO("thinlto", cl::init(false),
65             cl::desc("Only write combined global index for ThinLTO backends"));
66
67 static cl::opt<bool>
68 SaveModuleFile("save-merged-module", cl::init(false),
69                cl::desc("Write merged LTO module to file before CodeGen"));
70
71 static cl::list<std::string>
72 InputFilenames(cl::Positional, cl::OneOrMore,
73   cl::desc("<input bitcode files>"));
74
75 static cl::opt<std::string>
76 OutputFilename("o", cl::init(""),
77   cl::desc("Override output filename"),
78   cl::value_desc("filename"));
79
80 static cl::list<std::string>
81 ExportedSymbols("exported-symbol",
82   cl::desc("Symbol to export from the resulting object file"),
83   cl::ZeroOrMore);
84
85 static cl::list<std::string>
86 DSOSymbols("dso-symbol",
87   cl::desc("Symbol to put in the symtab in the resulting dso"),
88   cl::ZeroOrMore);
89
90 static cl::opt<bool> ListSymbolsOnly(
91     "list-symbols-only", cl::init(false),
92     cl::desc("Instead of running LTO, list the symbols in each IR file"));
93
94 static cl::opt<bool> SetMergedModule(
95     "set-merged-module", cl::init(false),
96     cl::desc("Use the first input module as the merged module"));
97
98 static cl::opt<unsigned> Parallelism("j", cl::Prefix, cl::init(1),
99                                      cl::desc("Number of backend threads"));
100
101 namespace {
102 struct ModuleInfo {
103   std::vector<bool> CanBeHidden;
104 };
105 }
106
107 static void handleDiagnostics(lto_codegen_diagnostic_severity_t Severity,
108                               const char *Msg, void *) {
109   errs() << "llvm-lto: ";
110   switch (Severity) {
111   case LTO_DS_NOTE:
112     errs() << "note: ";
113     break;
114   case LTO_DS_REMARK:
115     errs() << "remark: ";
116     break;
117   case LTO_DS_ERROR:
118     errs() << "error: ";
119     break;
120   case LTO_DS_WARNING:
121     errs() << "warning: ";
122     break;
123   }
124   errs() << Msg << "\n";
125 }
126
127 static void diagnosticHandler(const DiagnosticInfo &DI) {
128   raw_ostream &OS = errs();
129   OS << "llvm-lto: ";
130   switch (DI.getSeverity()) {
131   case DS_Error:
132     OS << "error: ";
133     break;
134   case DS_Warning:
135     OS << "warning: ";
136     break;
137   case DS_Remark:
138     OS << "remark: ";
139     break;
140   case DS_Note:
141     OS << "note: ";
142     break;
143   }
144
145   DiagnosticPrinterRawOStream DP(OS);
146   DI.print(DP);
147   OS << '\n';
148
149   if (DI.getSeverity() == DS_Error)
150     exit(1);
151 }
152
153 static void error(const Twine &Msg) {
154   errs() << "llvm-lto: " << Msg << '\n';
155   exit(1);
156 }
157
158 static void error(std::error_code EC, const Twine &Prefix) {
159   if (EC)
160     error(Prefix + ": " + EC.message());
161 }
162
163 template <typename T>
164 static void error(const ErrorOr<T> &V, const Twine &Prefix) {
165   error(V.getError(), Prefix);
166 }
167
168 static std::unique_ptr<LTOModule>
169 getLocalLTOModule(StringRef Path, std::unique_ptr<MemoryBuffer> &Buffer,
170                   const TargetOptions &Options) {
171   ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
172       MemoryBuffer::getFile(Path);
173   error(BufferOrErr, "error loading file '" + Path + "'");
174   Buffer = std::move(BufferOrErr.get());
175   std::string Error;
176   std::unique_ptr<LTOModule> Ret(LTOModule::createInLocalContext(
177       Buffer->getBufferStart(), Buffer->getBufferSize(), Options, Error, Path));
178   if (!Error.empty())
179     error("error loading file '" + Path + "' " + Error);
180   return Ret;
181 }
182
183 /// \brief List symbols in each IR file.
184 ///
185 /// The main point here is to provide lit-testable coverage for the LTOModule
186 /// functionality that's exposed by the C API to list symbols.  Moreover, this
187 /// provides testing coverage for modules that have been created in their own
188 /// contexts.
189 static void listSymbols(const TargetOptions &Options) {
190   for (auto &Filename : InputFilenames) {
191     std::unique_ptr<MemoryBuffer> Buffer;
192     std::unique_ptr<LTOModule> Module =
193         getLocalLTOModule(Filename, Buffer, Options);
194
195     // List the symbols.
196     outs() << Filename << ":\n";
197     for (int I = 0, E = Module->getSymbolCount(); I != E; ++I)
198       outs() << Module->getSymbolName(I) << "\n";
199   }
200 }
201
202 /// Create a combined index file from the input IR files and write it.
203 ///
204 /// This is meant to enable testing of ThinLTO combined index generation,
205 /// currently available via the gold plugin via -thinlto.
206 static void createCombinedFunctionIndex() {
207   FunctionInfoIndex CombinedIndex;
208   uint64_t NextModuleId = 0;
209   for (auto &Filename : InputFilenames) {
210     ErrorOr<std::unique_ptr<FunctionInfoIndex>> IndexOrErr =
211         llvm::getFunctionIndexForFile(Filename, diagnosticHandler);
212     error(IndexOrErr, "error loading file '" + Filename + "'");
213     std::unique_ptr<FunctionInfoIndex> Index = std::move(IndexOrErr.get());
214     // Skip files without a function summary.
215     if (!Index)
216       continue;
217     CombinedIndex.mergeFrom(std::move(Index), ++NextModuleId);
218   }
219   std::error_code EC;
220   assert(!OutputFilename.empty());
221   raw_fd_ostream OS(OutputFilename + ".thinlto.bc", EC,
222                     sys::fs::OpenFlags::F_None);
223   error(EC, "error opening the file '" + OutputFilename + ".thinlto.bc'");
224   WriteFunctionSummaryToFile(CombinedIndex, OS);
225   OS.close();
226 }
227
228 int main(int argc, char **argv) {
229   // Print a stack trace if we signal out.
230   sys::PrintStackTraceOnErrorSignal();
231   PrettyStackTraceProgram X(argc, argv);
232
233   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
234   cl::ParseCommandLineOptions(argc, argv, "llvm LTO linker\n");
235
236   if (OptLevel < '0' || OptLevel > '3')
237     error("optimization level must be between 0 and 3");
238
239   // Initialize the configured targets.
240   InitializeAllTargets();
241   InitializeAllTargetMCs();
242   InitializeAllAsmPrinters();
243   InitializeAllAsmParsers();
244
245   // set up the TargetOptions for the machine
246   TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
247
248   if (ListSymbolsOnly) {
249     listSymbols(Options);
250     return 0;
251   }
252
253   if (ThinLTO) {
254     createCombinedFunctionIndex();
255     return 0;
256   }
257
258   unsigned BaseArg = 0;
259
260   LTOCodeGenerator CodeGen(getGlobalContext());
261
262   if (UseDiagnosticHandler)
263     CodeGen.setDiagnosticHandler(handleDiagnostics, nullptr);
264
265   CodeGen.setCodePICModel(RelocModel);
266
267   CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF);
268   CodeGen.setTargetOptions(Options);
269
270   llvm::StringSet<llvm::MallocAllocator> DSOSymbolsSet;
271   for (unsigned i = 0; i < DSOSymbols.size(); ++i)
272     DSOSymbolsSet.insert(DSOSymbols[i]);
273
274   std::vector<std::string> KeptDSOSyms;
275
276   for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) {
277     std::string error;
278     std::unique_ptr<LTOModule> Module(
279         LTOModule::createFromFile(InputFilenames[i].c_str(), Options, error));
280     if (!error.empty()) {
281       errs() << argv[0] << ": error loading file '" << InputFilenames[i]
282              << "': " << error << "\n";
283       return 1;
284     }
285
286     unsigned NumSyms = Module->getSymbolCount();
287     for (unsigned I = 0; I < NumSyms; ++I) {
288       StringRef Name = Module->getSymbolName(I);
289       if (!DSOSymbolsSet.count(Name))
290         continue;
291       lto_symbol_attributes Attrs = Module->getSymbolAttributes(I);
292       unsigned Scope = Attrs & LTO_SYMBOL_SCOPE_MASK;
293       if (Scope != LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN)
294         KeptDSOSyms.push_back(Name);
295     }
296
297     // We use the first input module as the destination module when
298     // SetMergedModule is true.
299     if (SetMergedModule && i == BaseArg) {
300       // Transfer ownership to the code generator.
301       CodeGen.setModule(std::move(Module));
302     } else if (!CodeGen.addModule(Module.get())) {
303       // Print a message here so that we know addModule() did not abort.
304       errs() << argv[0] << ": error adding file '" << InputFilenames[i] << "'\n";
305       return 1;
306     }
307   }
308
309   // Add all the exported symbols to the table of symbols to preserve.
310   for (unsigned i = 0; i < ExportedSymbols.size(); ++i)
311     CodeGen.addMustPreserveSymbol(ExportedSymbols[i].c_str());
312
313   // Add all the dso symbols to the table of symbols to expose.
314   for (unsigned i = 0; i < KeptDSOSyms.size(); ++i)
315     CodeGen.addMustPreserveSymbol(KeptDSOSyms[i].c_str());
316
317   // Set cpu and attrs strings for the default target/subtarget.
318   CodeGen.setCpu(MCPU.c_str());
319
320   CodeGen.setOptLevel(OptLevel - '0');
321
322   std::string attrs;
323   for (unsigned i = 0; i < MAttrs.size(); ++i) {
324     if (i > 0)
325       attrs.append(",");
326     attrs.append(MAttrs[i]);
327   }
328
329   if (!attrs.empty())
330     CodeGen.setAttr(attrs.c_str());
331
332   if (FileType.getNumOccurrences())
333     CodeGen.setFileType(FileType);
334
335   if (!OutputFilename.empty()) {
336     if (!CodeGen.optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
337                           DisableLTOVectorization)) {
338       // Diagnostic messages should have been printed by the handler.
339       errs() << argv[0] << ": error optimizing the code\n";
340       return 1;
341     }
342
343     if (SaveModuleFile) {
344       std::string ModuleFilename = OutputFilename;
345       ModuleFilename += ".merged.bc";
346       std::string ErrMsg;
347
348       if (!CodeGen.writeMergedModules(ModuleFilename.c_str())) {
349         errs() << argv[0] << ": writing merged module failed.\n";
350         return 1;
351       }
352     }
353
354     std::list<tool_output_file> OSs;
355     std::vector<raw_pwrite_stream *> OSPtrs;
356     for (unsigned I = 0; I != Parallelism; ++I) {
357       std::string PartFilename = OutputFilename;
358       if (Parallelism != 1)
359         PartFilename += "." + utostr(I);
360       std::error_code EC;
361       OSs.emplace_back(PartFilename, EC, sys::fs::F_None);
362       if (EC) {
363         errs() << argv[0] << ": error opening the file '" << PartFilename
364                << "': " << EC.message() << "\n";
365         return 1;
366       }
367       OSPtrs.push_back(&OSs.back().os());
368     }
369
370     if (!CodeGen.compileOptimized(OSPtrs)) {
371       // Diagnostic messages should have been printed by the handler.
372       errs() << argv[0] << ": error compiling the code\n";
373       return 1;
374     }
375
376     for (tool_output_file &OS : OSs)
377       OS.keep();
378   } else {
379     if (Parallelism != 1) {
380       errs() << argv[0] << ": -j must be specified together with -o\n";
381       return 1;
382     }
383
384     if (SaveModuleFile) {
385       errs() << argv[0] << ": -save-merged-module must be specified with -o\n";
386       return 1;
387     }
388
389     const char *OutputName = nullptr;
390     if (!CodeGen.compile_to_file(&OutputName, DisableVerify, DisableInline,
391                                  DisableGVNLoadPRE, DisableLTOVectorization)) {
392       // Diagnostic messages should have been printed by the handler.
393       errs() << argv[0] << ": error compiling the code\n";
394       return 1;
395     }
396
397     outs() << "Wrote native object file '" << OutputName << "'\n";
398   }
399
400   return 0;
401 }