37ee7e8c53cc8b334c4862d7abdaab72eb05a92a
[oota-llvm.git] / lib / LTO / LTOCodeGenerator.cpp
1 //===-LTOCodeGenerator.cpp - LLVM Link Time Optimizer ---------------------===//
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 file implements the Link Time Optimization library. This library is
11 // intended to be used by linker to optimize code at link time.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/LTO/LTOCodeGenerator.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/Analysis/Passes.h"
18 #include "llvm/Analysis/TargetLibraryInfo.h"
19 #include "llvm/Analysis/TargetTransformInfo.h"
20 #include "llvm/Bitcode/ReaderWriter.h"
21 #include "llvm/CodeGen/ParallelCG.h"
22 #include "llvm/CodeGen/RuntimeLibcalls.h"
23 #include "llvm/Config/config.h"
24 #include "llvm/IR/Constants.h"
25 #include "llvm/IR/DataLayout.h"
26 #include "llvm/IR/DerivedTypes.h"
27 #include "llvm/IR/DiagnosticInfo.h"
28 #include "llvm/IR/DiagnosticPrinter.h"
29 #include "llvm/IR/LLVMContext.h"
30 #include "llvm/IR/LegacyPassManager.h"
31 #include "llvm/IR/Mangler.h"
32 #include "llvm/IR/Module.h"
33 #include "llvm/IR/Verifier.h"
34 #include "llvm/InitializePasses.h"
35 #include "llvm/LTO/LTOModule.h"
36 #include "llvm/Linker/Linker.h"
37 #include "llvm/MC/MCAsmInfo.h"
38 #include "llvm/MC/MCContext.h"
39 #include "llvm/MC/SubtargetFeature.h"
40 #include "llvm/Support/CommandLine.h"
41 #include "llvm/Support/FileSystem.h"
42 #include "llvm/Support/Host.h"
43 #include "llvm/Support/MemoryBuffer.h"
44 #include "llvm/Support/Signals.h"
45 #include "llvm/Support/TargetRegistry.h"
46 #include "llvm/Support/TargetSelect.h"
47 #include "llvm/Support/ToolOutputFile.h"
48 #include "llvm/Support/raw_ostream.h"
49 #include "llvm/Target/TargetLowering.h"
50 #include "llvm/Target/TargetOptions.h"
51 #include "llvm/Target/TargetRegisterInfo.h"
52 #include "llvm/Target/TargetSubtargetInfo.h"
53 #include "llvm/Transforms/IPO.h"
54 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
55 #include "llvm/Transforms/ObjCARC.h"
56 #include <system_error>
57 using namespace llvm;
58
59 const char* LTOCodeGenerator::getVersionString() {
60 #ifdef LLVM_VERSION_INFO
61   return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
62 #else
63   return PACKAGE_NAME " version " PACKAGE_VERSION;
64 #endif
65 }
66
67 LTOCodeGenerator::LTOCodeGenerator()
68     : Context(getGlobalContext()),
69       MergedModule(new Module("ld-temp.o", Context)),
70       IRLinker(new Linker(MergedModule.get())) {
71   initializeLTOPasses();
72 }
73
74 LTOCodeGenerator::LTOCodeGenerator(std::unique_ptr<LLVMContext> Context)
75     : OwnedContext(std::move(Context)), Context(*OwnedContext),
76       MergedModule(new Module("ld-temp.o", *OwnedContext)),
77       IRLinker(new Linker(MergedModule.get())) {
78   initializeLTOPasses();
79 }
80
81 LTOCodeGenerator::~LTOCodeGenerator() {}
82
83 // Initialize LTO passes. Please keep this function in sync with
84 // PassManagerBuilder::populateLTOPassManager(), and make sure all LTO
85 // passes are initialized.
86 void LTOCodeGenerator::initializeLTOPasses() {
87   PassRegistry &R = *PassRegistry::getPassRegistry();
88
89   initializeInternalizePassPass(R);
90   initializeIPSCCPPass(R);
91   initializeGlobalOptPass(R);
92   initializeConstantMergePass(R);
93   initializeDAHPass(R);
94   initializeInstructionCombiningPassPass(R);
95   initializeSimpleInlinerPass(R);
96   initializePruneEHPass(R);
97   initializeGlobalDCEPass(R);
98   initializeArgPromotionPass(R);
99   initializeJumpThreadingPass(R);
100   initializeSROALegacyPassPass(R);
101   initializeSROA_DTPass(R);
102   initializeSROA_SSAUpPass(R);
103   initializeFunctionAttrsPass(R);
104   initializeGlobalsAAWrapperPassPass(R);
105   initializeLICMPass(R);
106   initializeMergedLoadStoreMotionPass(R);
107   initializeGVNPass(R);
108   initializeMemCpyOptPass(R);
109   initializeDCEPass(R);
110   initializeCFGSimplifyPassPass(R);
111 }
112
113 bool LTOCodeGenerator::addModule(LTOModule *Mod) {
114   assert(&Mod->getModule().getContext() == &Context &&
115          "Expected module in same context");
116
117   bool ret = IRLinker->linkInModule(&Mod->getModule());
118
119   const std::vector<const char *> &undefs = Mod->getAsmUndefinedRefs();
120   for (int i = 0, e = undefs.size(); i != e; ++i)
121     AsmUndefinedRefs[undefs[i]] = 1;
122
123   return !ret;
124 }
125
126 void LTOCodeGenerator::setModule(std::unique_ptr<LTOModule> Mod) {
127   assert(&Mod->getModule().getContext() == &Context &&
128          "Expected module in same context");
129
130   AsmUndefinedRefs.clear();
131
132   MergedModule = Mod->takeModule();
133   IRLinker = make_unique<Linker>(MergedModule.get());
134
135   const std::vector<const char*> &Undefs = Mod->getAsmUndefinedRefs();
136   for (int I = 0, E = Undefs.size(); I != E; ++I)
137     AsmUndefinedRefs[Undefs[I]] = 1;
138 }
139
140 void LTOCodeGenerator::setTargetOptions(TargetOptions Options) {
141   this->Options = Options;
142 }
143
144 void LTOCodeGenerator::setDebugInfo(lto_debug_model Debug) {
145   switch (Debug) {
146   case LTO_DEBUG_MODEL_NONE:
147     EmitDwarfDebugInfo = false;
148     return;
149
150   case LTO_DEBUG_MODEL_DWARF:
151     EmitDwarfDebugInfo = true;
152     return;
153   }
154   llvm_unreachable("Unknown debug format!");
155 }
156
157 void LTOCodeGenerator::setOptLevel(unsigned Level) {
158   OptLevel = Level;
159   switch (OptLevel) {
160   case 0:
161     CGOptLevel = CodeGenOpt::None;
162     break;
163   case 1:
164     CGOptLevel = CodeGenOpt::Less;
165     break;
166   case 2:
167     CGOptLevel = CodeGenOpt::Default;
168     break;
169   case 3:
170     CGOptLevel = CodeGenOpt::Aggressive;
171     break;
172   }
173 }
174
175 bool LTOCodeGenerator::writeMergedModules(const char *Path) {
176   if (!determineTarget())
177     return false;
178
179   // mark which symbols can not be internalized
180   applyScopeRestrictions();
181
182   // create output file
183   std::error_code EC;
184   tool_output_file Out(Path, EC, sys::fs::F_None);
185   if (EC) {
186     std::string ErrMsg = "could not open bitcode file for writing: ";
187     ErrMsg += Path;
188     emitError(ErrMsg);
189     return false;
190   }
191
192   // write bitcode to it
193   WriteBitcodeToFile(MergedModule.get(), Out.os(), ShouldEmbedUselists);
194   Out.os().close();
195
196   if (Out.os().has_error()) {
197     std::string ErrMsg = "could not write bitcode file: ";
198     ErrMsg += Path;
199     emitError(ErrMsg);
200     Out.os().clear_error();
201     return false;
202   }
203
204   Out.keep();
205   return true;
206 }
207
208 bool LTOCodeGenerator::compileOptimizedToFile(const char **Name) {
209   // make unique temp output file to put generated code
210   SmallString<128> Filename;
211   int FD;
212
213   const char *Extension =
214       (FileType == TargetMachine::CGFT_AssemblyFile ? "s" : "o");
215
216   std::error_code EC =
217       sys::fs::createTemporaryFile("lto-llvm", Extension, FD, Filename);
218   if (EC) {
219     emitError(EC.message());
220     return false;
221   }
222
223   // generate object file
224   tool_output_file objFile(Filename.c_str(), FD);
225
226   bool genResult = compileOptimized(&objFile.os());
227   objFile.os().close();
228   if (objFile.os().has_error()) {
229     objFile.os().clear_error();
230     sys::fs::remove(Twine(Filename));
231     return false;
232   }
233
234   objFile.keep();
235   if (!genResult) {
236     sys::fs::remove(Twine(Filename));
237     return false;
238   }
239
240   NativeObjectPath = Filename.c_str();
241   *Name = NativeObjectPath.c_str();
242   return true;
243 }
244
245 std::unique_ptr<MemoryBuffer>
246 LTOCodeGenerator::compileOptimized() {
247   const char *name;
248   if (!compileOptimizedToFile(&name))
249     return nullptr;
250
251   // read .o file into memory buffer
252   ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
253       MemoryBuffer::getFile(name, -1, false);
254   if (std::error_code EC = BufferOrErr.getError()) {
255     emitError(EC.message());
256     sys::fs::remove(NativeObjectPath);
257     return nullptr;
258   }
259
260   // remove temp files
261   sys::fs::remove(NativeObjectPath);
262
263   return std::move(*BufferOrErr);
264 }
265
266 bool LTOCodeGenerator::compile_to_file(const char **Name, bool DisableVerify,
267                                        bool DisableInline,
268                                        bool DisableGVNLoadPRE,
269                                        bool DisableVectorization) {
270   if (!optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
271                 DisableVectorization))
272     return false;
273
274   return compileOptimizedToFile(Name);
275 }
276
277 std::unique_ptr<MemoryBuffer>
278 LTOCodeGenerator::compile(bool DisableVerify, bool DisableInline,
279                           bool DisableGVNLoadPRE, bool DisableVectorization) {
280   if (!optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
281                 DisableVectorization))
282     return nullptr;
283
284   return compileOptimized();
285 }
286
287 bool LTOCodeGenerator::determineTarget() {
288   if (TargetMach)
289     return true;
290
291   std::string TripleStr = MergedModule->getTargetTriple();
292   if (TripleStr.empty()) {
293     TripleStr = sys::getDefaultTargetTriple();
294     MergedModule->setTargetTriple(TripleStr);
295   }
296   llvm::Triple Triple(TripleStr);
297
298   // create target machine from info for merged modules
299   std::string ErrMsg;
300   const Target *march = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
301   if (!march) {
302     emitError(ErrMsg);
303     return false;
304   }
305
306   // Construct LTOModule, hand over ownership of module and target. Use MAttr as
307   // the default set of features.
308   SubtargetFeatures Features(MAttr);
309   Features.getDefaultSubtargetFeatures(Triple);
310   FeatureStr = Features.getString();
311   // Set a default CPU for Darwin triples.
312   if (MCpu.empty() && Triple.isOSDarwin()) {
313     if (Triple.getArch() == llvm::Triple::x86_64)
314       MCpu = "core2";
315     else if (Triple.getArch() == llvm::Triple::x86)
316       MCpu = "yonah";
317     else if (Triple.getArch() == llvm::Triple::aarch64)
318       MCpu = "cyclone";
319   }
320
321   TargetMach.reset(march->createTargetMachine(TripleStr, MCpu, FeatureStr,
322                                               Options, RelocModel,
323                                               CodeModel::Default, CGOptLevel));
324   return true;
325 }
326
327 void LTOCodeGenerator::
328 applyRestriction(GlobalValue &GV,
329                  ArrayRef<StringRef> Libcalls,
330                  std::vector<const char*> &MustPreserveList,
331                  SmallPtrSetImpl<GlobalValue*> &AsmUsed,
332                  Mangler &Mangler) {
333   // There are no restrictions to apply to declarations.
334   if (GV.isDeclaration())
335     return;
336
337   // There is nothing more restrictive than private linkage.
338   if (GV.hasPrivateLinkage())
339     return;
340
341   SmallString<64> Buffer;
342   TargetMach->getNameWithPrefix(Buffer, &GV, Mangler);
343
344   if (MustPreserveSymbols.count(Buffer))
345     MustPreserveList.push_back(GV.getName().data());
346   if (AsmUndefinedRefs.count(Buffer))
347     AsmUsed.insert(&GV);
348
349   // Conservatively append user-supplied runtime library functions to
350   // llvm.compiler.used.  These could be internalized and deleted by
351   // optimizations like -globalopt, causing problems when later optimizations
352   // add new library calls (e.g., llvm.memset => memset and printf => puts).
353   // Leave it to the linker to remove any dead code (e.g. with -dead_strip).
354   if (isa<Function>(GV) &&
355       std::binary_search(Libcalls.begin(), Libcalls.end(), GV.getName()))
356     AsmUsed.insert(&GV);
357 }
358
359 static void findUsedValues(GlobalVariable *LLVMUsed,
360                            SmallPtrSetImpl<GlobalValue*> &UsedValues) {
361   if (!LLVMUsed) return;
362
363   ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
364   for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
365     if (GlobalValue *GV =
366         dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
367       UsedValues.insert(GV);
368 }
369
370 // Collect names of runtime library functions. User-defined functions with the
371 // same names are added to llvm.compiler.used to prevent them from being
372 // deleted by optimizations.
373 static void accumulateAndSortLibcalls(std::vector<StringRef> &Libcalls,
374                                       const TargetLibraryInfo& TLI,
375                                       const Module &Mod,
376                                       const TargetMachine &TM) {
377   // TargetLibraryInfo has info on C runtime library calls on the current
378   // target.
379   for (unsigned I = 0, E = static_cast<unsigned>(LibFunc::NumLibFuncs);
380        I != E; ++I) {
381     LibFunc::Func F = static_cast<LibFunc::Func>(I);
382     if (TLI.has(F))
383       Libcalls.push_back(TLI.getName(F));
384   }
385
386   SmallPtrSet<const TargetLowering *, 1> TLSet;
387
388   for (const Function &F : Mod) {
389     const TargetLowering *Lowering =
390         TM.getSubtargetImpl(F)->getTargetLowering();
391
392     if (Lowering && TLSet.insert(Lowering).second)
393       // TargetLowering has info on library calls that CodeGen expects to be
394       // available, both from the C runtime and compiler-rt.
395       for (unsigned I = 0, E = static_cast<unsigned>(RTLIB::UNKNOWN_LIBCALL);
396            I != E; ++I)
397         if (const char *Name =
398                 Lowering->getLibcallName(static_cast<RTLIB::Libcall>(I)))
399           Libcalls.push_back(Name);
400   }
401
402   array_pod_sort(Libcalls.begin(), Libcalls.end());
403   Libcalls.erase(std::unique(Libcalls.begin(), Libcalls.end()),
404                  Libcalls.end());
405 }
406
407 void LTOCodeGenerator::applyScopeRestrictions() {
408   if (ScopeRestrictionsDone || !ShouldInternalize)
409     return;
410
411   // Start off with a verification pass.
412   legacy::PassManager passes;
413   passes.add(createVerifierPass());
414
415   // mark which symbols can not be internalized
416   Mangler Mangler;
417   std::vector<const char*> MustPreserveList;
418   SmallPtrSet<GlobalValue*, 8> AsmUsed;
419   std::vector<StringRef> Libcalls;
420   TargetLibraryInfoImpl TLII(Triple(TargetMach->getTargetTriple()));
421   TargetLibraryInfo TLI(TLII);
422
423   accumulateAndSortLibcalls(Libcalls, TLI, *MergedModule, *TargetMach);
424
425   for (Function &f : *MergedModule)
426     applyRestriction(f, Libcalls, MustPreserveList, AsmUsed, Mangler);
427   for (GlobalVariable &v : MergedModule->globals())
428     applyRestriction(v, Libcalls, MustPreserveList, AsmUsed, Mangler);
429   for (GlobalAlias &a : MergedModule->aliases())
430     applyRestriction(a, Libcalls, MustPreserveList, AsmUsed, Mangler);
431
432   GlobalVariable *LLVMCompilerUsed =
433     MergedModule->getGlobalVariable("llvm.compiler.used");
434   findUsedValues(LLVMCompilerUsed, AsmUsed);
435   if (LLVMCompilerUsed)
436     LLVMCompilerUsed->eraseFromParent();
437
438   if (!AsmUsed.empty()) {
439     llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(Context);
440     std::vector<Constant*> asmUsed2;
441     for (auto *GV : AsmUsed) {
442       Constant *c = ConstantExpr::getBitCast(GV, i8PTy);
443       asmUsed2.push_back(c);
444     }
445
446     llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size());
447     LLVMCompilerUsed =
448       new llvm::GlobalVariable(*MergedModule, ATy, false,
449                                llvm::GlobalValue::AppendingLinkage,
450                                llvm::ConstantArray::get(ATy, asmUsed2),
451                                "llvm.compiler.used");
452
453     LLVMCompilerUsed->setSection("llvm.metadata");
454   }
455
456   passes.add(createInternalizePass(MustPreserveList));
457
458   // apply scope restrictions
459   passes.run(*MergedModule);
460
461   ScopeRestrictionsDone = true;
462 }
463
464 /// Optimize merged modules using various IPO passes
465 bool LTOCodeGenerator::optimize(bool DisableVerify, bool DisableInline,
466                                 bool DisableGVNLoadPRE,
467                                 bool DisableVectorization) {
468   if (!this->determineTarget())
469     return false;
470
471   // Mark which symbols can not be internalized
472   this->applyScopeRestrictions();
473
474   // Instantiate the pass manager to organize the passes.
475   legacy::PassManager passes;
476
477   // Add an appropriate DataLayout instance for this module...
478   MergedModule->setDataLayout(TargetMach->createDataLayout());
479
480   passes.add(
481       createTargetTransformInfoWrapperPass(TargetMach->getTargetIRAnalysis()));
482
483   Triple TargetTriple(TargetMach->getTargetTriple());
484   PassManagerBuilder PMB;
485   PMB.DisableGVNLoadPRE = DisableGVNLoadPRE;
486   PMB.LoopVectorize = !DisableVectorization;
487   PMB.SLPVectorize = !DisableVectorization;
488   if (!DisableInline)
489     PMB.Inliner = createFunctionInliningPass();
490   PMB.LibraryInfo = new TargetLibraryInfoImpl(TargetTriple);
491   PMB.OptLevel = OptLevel;
492   PMB.VerifyInput = !DisableVerify;
493   PMB.VerifyOutput = !DisableVerify;
494
495   PMB.populateLTOPassManager(passes);
496
497   // Run our queue of passes all at once now, efficiently.
498   passes.run(*MergedModule);
499
500   return true;
501 }
502
503 bool LTOCodeGenerator::compileOptimized(ArrayRef<raw_pwrite_stream *> Out) {
504   if (!this->determineTarget())
505     return false;
506
507   legacy::PassManager preCodeGenPasses;
508
509   // If the bitcode files contain ARC code and were compiled with optimization,
510   // the ObjCARCContractPass must be run, so do it unconditionally here.
511   preCodeGenPasses.add(createObjCARCContractPass());
512   preCodeGenPasses.run(*MergedModule);
513
514   // Do code generation. We need to preserve the module in case the client calls
515   // writeMergedModules() after compilation, but we only need to allow this at
516   // parallelism level 1. This is achieved by having splitCodeGen return the
517   // original module at parallelism level 1 which we then assign back to
518   // MergedModule.
519   MergedModule =
520       splitCodeGen(std::move(MergedModule), Out, MCpu, FeatureStr, Options,
521                    RelocModel, CodeModel::Default, CGOptLevel, FileType);
522
523   return true;
524 }
525
526 /// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
527 /// LTO problems.
528 void LTOCodeGenerator::setCodeGenDebugOptions(const char *Options) {
529   for (std::pair<StringRef, StringRef> o = getToken(Options); !o.first.empty();
530        o = getToken(o.second))
531     CodegenOptions.push_back(o.first);
532 }
533
534 void LTOCodeGenerator::parseCodeGenDebugOptions() {
535   // if options were requested, set them
536   if (!CodegenOptions.empty()) {
537     // ParseCommandLineOptions() expects argv[0] to be program name.
538     std::vector<const char *> CodegenArgv(1, "libLLVMLTO");
539     for (std::string &Arg : CodegenOptions)
540       CodegenArgv.push_back(Arg.c_str());
541     cl::ParseCommandLineOptions(CodegenArgv.size(), CodegenArgv.data());
542   }
543 }
544
545 void LTOCodeGenerator::DiagnosticHandler(const DiagnosticInfo &DI,
546                                          void *Context) {
547   ((LTOCodeGenerator *)Context)->DiagnosticHandler2(DI);
548 }
549
550 void LTOCodeGenerator::DiagnosticHandler2(const DiagnosticInfo &DI) {
551   // Map the LLVM internal diagnostic severity to the LTO diagnostic severity.
552   lto_codegen_diagnostic_severity_t Severity;
553   switch (DI.getSeverity()) {
554   case DS_Error:
555     Severity = LTO_DS_ERROR;
556     break;
557   case DS_Warning:
558     Severity = LTO_DS_WARNING;
559     break;
560   case DS_Remark:
561     Severity = LTO_DS_REMARK;
562     break;
563   case DS_Note:
564     Severity = LTO_DS_NOTE;
565     break;
566   }
567   // Create the string that will be reported to the external diagnostic handler.
568   std::string MsgStorage;
569   raw_string_ostream Stream(MsgStorage);
570   DiagnosticPrinterRawOStream DP(Stream);
571   DI.print(DP);
572   Stream.flush();
573
574   // If this method has been called it means someone has set up an external
575   // diagnostic handler. Assert on that.
576   assert(DiagHandler && "Invalid diagnostic handler");
577   (*DiagHandler)(Severity, MsgStorage.c_str(), DiagContext);
578 }
579
580 void
581 LTOCodeGenerator::setDiagnosticHandler(lto_diagnostic_handler_t DiagHandler,
582                                        void *Ctxt) {
583   this->DiagHandler = DiagHandler;
584   this->DiagContext = Ctxt;
585   if (!DiagHandler)
586     return Context.setDiagnosticHandler(nullptr, nullptr);
587   // Register the LTOCodeGenerator stub in the LLVMContext to forward the
588   // diagnostic to the external DiagHandler.
589   Context.setDiagnosticHandler(LTOCodeGenerator::DiagnosticHandler, this,
590                                /* RespectFilters */ true);
591 }
592
593 namespace {
594 class LTODiagnosticInfo : public DiagnosticInfo {
595   const Twine &Msg;
596 public:
597   LTODiagnosticInfo(const Twine &DiagMsg, DiagnosticSeverity Severity=DS_Error)
598       : DiagnosticInfo(DK_Linker, Severity), Msg(DiagMsg) {}
599   void print(DiagnosticPrinter &DP) const override { DP << Msg; }
600 };
601 }
602
603 void LTOCodeGenerator::emitError(const std::string &ErrMsg) {
604   if (DiagHandler)
605     (*DiagHandler)(LTO_DS_ERROR, ErrMsg.c_str(), DiagContext);
606   else
607     Context.diagnose(LTODiagnosticInfo(ErrMsg));
608 }