1 //===-LTOCodeGenerator.cpp - LLVM Link Time Optimizer ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
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>
59 const char* LTOCodeGenerator::getVersionString() {
60 #ifdef LLVM_VERSION_INFO
61 return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
63 return PACKAGE_NAME " version " PACKAGE_VERSION;
67 LTOCodeGenerator::LTOCodeGenerator(LLVMContext &Context)
68 : Context(Context), MergedModule(new Module("ld-temp.o", Context)),
69 TheLinker(new Linker(*MergedModule)) {
70 initializeLTOPasses();
73 LTOCodeGenerator::~LTOCodeGenerator() {}
75 // Initialize LTO passes. Please keep this function in sync with
76 // PassManagerBuilder::populateLTOPassManager(), and make sure all LTO
77 // passes are initialized.
78 void LTOCodeGenerator::initializeLTOPasses() {
79 PassRegistry &R = *PassRegistry::getPassRegistry();
81 initializeInternalizePassPass(R);
82 initializeIPSCCPPass(R);
83 initializeGlobalOptPass(R);
84 initializeConstantMergePass(R);
86 initializeInstructionCombiningPassPass(R);
87 initializeSimpleInlinerPass(R);
88 initializePruneEHPass(R);
89 initializeGlobalDCEPass(R);
90 initializeArgPromotionPass(R);
91 initializeJumpThreadingPass(R);
92 initializeSROALegacyPassPass(R);
93 initializeSROA_DTPass(R);
94 initializeSROA_SSAUpPass(R);
95 initializeFunctionAttrsPass(R);
96 initializeGlobalsAAWrapperPassPass(R);
97 initializeLICMPass(R);
98 initializeMergedLoadStoreMotionPass(R);
100 initializeMemCpyOptPass(R);
101 initializeDCEPass(R);
102 initializeCFGSimplifyPassPass(R);
105 bool LTOCodeGenerator::addModule(LTOModule *Mod) {
106 assert(&Mod->getModule().getContext() == &Context &&
107 "Expected module in same context");
109 bool ret = TheLinker->linkInModule(Mod->takeModule());
111 const std::vector<const char *> &undefs = Mod->getAsmUndefinedRefs();
112 for (int i = 0, e = undefs.size(); i != e; ++i)
113 AsmUndefinedRefs[undefs[i]] = 1;
118 void LTOCodeGenerator::setModule(std::unique_ptr<LTOModule> Mod) {
119 assert(&Mod->getModule().getContext() == &Context &&
120 "Expected module in same context");
122 AsmUndefinedRefs.clear();
124 MergedModule = Mod->takeModule();
125 TheLinker = make_unique<Linker>(*MergedModule);
127 const std::vector<const char*> &Undefs = Mod->getAsmUndefinedRefs();
128 for (int I = 0, E = Undefs.size(); I != E; ++I)
129 AsmUndefinedRefs[Undefs[I]] = 1;
132 void LTOCodeGenerator::setTargetOptions(TargetOptions Options) {
133 this->Options = Options;
136 void LTOCodeGenerator::setDebugInfo(lto_debug_model Debug) {
138 case LTO_DEBUG_MODEL_NONE:
139 EmitDwarfDebugInfo = false;
142 case LTO_DEBUG_MODEL_DWARF:
143 EmitDwarfDebugInfo = true;
146 llvm_unreachable("Unknown debug format!");
149 void LTOCodeGenerator::setOptLevel(unsigned Level) {
153 CGOptLevel = CodeGenOpt::None;
156 CGOptLevel = CodeGenOpt::Less;
159 CGOptLevel = CodeGenOpt::Default;
162 CGOptLevel = CodeGenOpt::Aggressive;
167 bool LTOCodeGenerator::writeMergedModules(const char *Path) {
168 if (!determineTarget())
171 // mark which symbols can not be internalized
172 applyScopeRestrictions();
174 // create output file
176 tool_output_file Out(Path, EC, sys::fs::F_None);
178 std::string ErrMsg = "could not open bitcode file for writing: ";
184 // write bitcode to it
185 WriteBitcodeToFile(MergedModule.get(), Out.os(), ShouldEmbedUselists);
188 if (Out.os().has_error()) {
189 std::string ErrMsg = "could not write bitcode file: ";
192 Out.os().clear_error();
200 bool LTOCodeGenerator::compileOptimizedToFile(const char **Name) {
201 // make unique temp output file to put generated code
202 SmallString<128> Filename;
205 const char *Extension =
206 (FileType == TargetMachine::CGFT_AssemblyFile ? "s" : "o");
209 sys::fs::createTemporaryFile("lto-llvm", Extension, FD, Filename);
211 emitError(EC.message());
215 // generate object file
216 tool_output_file objFile(Filename.c_str(), FD);
218 bool genResult = compileOptimized(&objFile.os());
219 objFile.os().close();
220 if (objFile.os().has_error()) {
221 objFile.os().clear_error();
222 sys::fs::remove(Twine(Filename));
228 sys::fs::remove(Twine(Filename));
232 NativeObjectPath = Filename.c_str();
233 *Name = NativeObjectPath.c_str();
237 std::unique_ptr<MemoryBuffer>
238 LTOCodeGenerator::compileOptimized() {
240 if (!compileOptimizedToFile(&name))
243 // read .o file into memory buffer
244 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
245 MemoryBuffer::getFile(name, -1, false);
246 if (std::error_code EC = BufferOrErr.getError()) {
247 emitError(EC.message());
248 sys::fs::remove(NativeObjectPath);
253 sys::fs::remove(NativeObjectPath);
255 return std::move(*BufferOrErr);
258 bool LTOCodeGenerator::compile_to_file(const char **Name, bool DisableVerify,
260 bool DisableGVNLoadPRE,
261 bool DisableVectorization) {
262 if (!optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
263 DisableVectorization))
266 return compileOptimizedToFile(Name);
269 std::unique_ptr<MemoryBuffer>
270 LTOCodeGenerator::compile(bool DisableVerify, bool DisableInline,
271 bool DisableGVNLoadPRE, bool DisableVectorization) {
272 if (!optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
273 DisableVectorization))
276 return compileOptimized();
279 bool LTOCodeGenerator::determineTarget() {
283 std::string TripleStr = MergedModule->getTargetTriple();
284 if (TripleStr.empty()) {
285 TripleStr = sys::getDefaultTargetTriple();
286 MergedModule->setTargetTriple(TripleStr);
288 llvm::Triple Triple(TripleStr);
290 // create target machine from info for merged modules
292 const Target *march = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
298 // Construct LTOModule, hand over ownership of module and target. Use MAttr as
299 // the default set of features.
300 SubtargetFeatures Features(MAttr);
301 Features.getDefaultSubtargetFeatures(Triple);
302 FeatureStr = Features.getString();
303 // Set a default CPU for Darwin triples.
304 if (MCpu.empty() && Triple.isOSDarwin()) {
305 if (Triple.getArch() == llvm::Triple::x86_64)
307 else if (Triple.getArch() == llvm::Triple::x86)
309 else if (Triple.getArch() == llvm::Triple::aarch64)
313 TargetMach.reset(march->createTargetMachine(TripleStr, MCpu, FeatureStr,
315 CodeModel::Default, CGOptLevel));
319 void LTOCodeGenerator::
320 applyRestriction(GlobalValue &GV,
321 ArrayRef<StringRef> Libcalls,
322 std::vector<const char*> &MustPreserveList,
323 SmallPtrSetImpl<GlobalValue*> &AsmUsed,
325 // There are no restrictions to apply to declarations.
326 if (GV.isDeclaration())
329 // There is nothing more restrictive than private linkage.
330 if (GV.hasPrivateLinkage())
333 SmallString<64> Buffer;
334 TargetMach->getNameWithPrefix(Buffer, &GV, Mangler);
336 if (MustPreserveSymbols.count(Buffer))
337 MustPreserveList.push_back(GV.getName().data());
338 if (AsmUndefinedRefs.count(Buffer))
341 // Conservatively append user-supplied runtime library functions to
342 // llvm.compiler.used. These could be internalized and deleted by
343 // optimizations like -globalopt, causing problems when later optimizations
344 // add new library calls (e.g., llvm.memset => memset and printf => puts).
345 // Leave it to the linker to remove any dead code (e.g. with -dead_strip).
346 if (isa<Function>(GV) &&
347 std::binary_search(Libcalls.begin(), Libcalls.end(), GV.getName()))
351 static void findUsedValues(GlobalVariable *LLVMUsed,
352 SmallPtrSetImpl<GlobalValue*> &UsedValues) {
353 if (!LLVMUsed) return;
355 ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
356 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
357 if (GlobalValue *GV =
358 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
359 UsedValues.insert(GV);
362 // Collect names of runtime library functions. User-defined functions with the
363 // same names are added to llvm.compiler.used to prevent them from being
364 // deleted by optimizations.
365 static void accumulateAndSortLibcalls(std::vector<StringRef> &Libcalls,
366 const TargetLibraryInfo& TLI,
368 const TargetMachine &TM) {
369 // TargetLibraryInfo has info on C runtime library calls on the current
371 for (unsigned I = 0, E = static_cast<unsigned>(LibFunc::NumLibFuncs);
373 LibFunc::Func F = static_cast<LibFunc::Func>(I);
375 Libcalls.push_back(TLI.getName(F));
378 SmallPtrSet<const TargetLowering *, 1> TLSet;
380 for (const Function &F : Mod) {
381 const TargetLowering *Lowering =
382 TM.getSubtargetImpl(F)->getTargetLowering();
384 if (Lowering && TLSet.insert(Lowering).second)
385 // TargetLowering has info on library calls that CodeGen expects to be
386 // available, both from the C runtime and compiler-rt.
387 for (unsigned I = 0, E = static_cast<unsigned>(RTLIB::UNKNOWN_LIBCALL);
389 if (const char *Name =
390 Lowering->getLibcallName(static_cast<RTLIB::Libcall>(I)))
391 Libcalls.push_back(Name);
394 array_pod_sort(Libcalls.begin(), Libcalls.end());
395 Libcalls.erase(std::unique(Libcalls.begin(), Libcalls.end()),
399 void LTOCodeGenerator::applyScopeRestrictions() {
400 if (ScopeRestrictionsDone || !ShouldInternalize)
403 // Start off with a verification pass.
404 legacy::PassManager passes;
405 passes.add(createVerifierPass());
407 // mark which symbols can not be internalized
409 std::vector<const char*> MustPreserveList;
410 SmallPtrSet<GlobalValue*, 8> AsmUsed;
411 std::vector<StringRef> Libcalls;
412 TargetLibraryInfoImpl TLII(Triple(TargetMach->getTargetTriple()));
413 TargetLibraryInfo TLI(TLII);
415 accumulateAndSortLibcalls(Libcalls, TLI, *MergedModule, *TargetMach);
417 for (Function &f : *MergedModule)
418 applyRestriction(f, Libcalls, MustPreserveList, AsmUsed, Mangler);
419 for (GlobalVariable &v : MergedModule->globals())
420 applyRestriction(v, Libcalls, MustPreserveList, AsmUsed, Mangler);
421 for (GlobalAlias &a : MergedModule->aliases())
422 applyRestriction(a, Libcalls, MustPreserveList, AsmUsed, Mangler);
424 GlobalVariable *LLVMCompilerUsed =
425 MergedModule->getGlobalVariable("llvm.compiler.used");
426 findUsedValues(LLVMCompilerUsed, AsmUsed);
427 if (LLVMCompilerUsed)
428 LLVMCompilerUsed->eraseFromParent();
430 if (!AsmUsed.empty()) {
431 llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(Context);
432 std::vector<Constant*> asmUsed2;
433 for (auto *GV : AsmUsed) {
434 Constant *c = ConstantExpr::getBitCast(GV, i8PTy);
435 asmUsed2.push_back(c);
438 llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size());
440 new llvm::GlobalVariable(*MergedModule, ATy, false,
441 llvm::GlobalValue::AppendingLinkage,
442 llvm::ConstantArray::get(ATy, asmUsed2),
443 "llvm.compiler.used");
445 LLVMCompilerUsed->setSection("llvm.metadata");
448 passes.add(createInternalizePass(MustPreserveList));
450 // apply scope restrictions
451 passes.run(*MergedModule);
453 ScopeRestrictionsDone = true;
456 /// Optimize merged modules using various IPO passes
457 bool LTOCodeGenerator::optimize(bool DisableVerify, bool DisableInline,
458 bool DisableGVNLoadPRE,
459 bool DisableVectorization) {
460 if (!this->determineTarget())
463 // Mark which symbols can not be internalized
464 this->applyScopeRestrictions();
466 // Instantiate the pass manager to organize the passes.
467 legacy::PassManager passes;
469 // Add an appropriate DataLayout instance for this module...
470 MergedModule->setDataLayout(TargetMach->createDataLayout());
473 createTargetTransformInfoWrapperPass(TargetMach->getTargetIRAnalysis()));
475 Triple TargetTriple(TargetMach->getTargetTriple());
476 PassManagerBuilder PMB;
477 PMB.DisableGVNLoadPRE = DisableGVNLoadPRE;
478 PMB.LoopVectorize = !DisableVectorization;
479 PMB.SLPVectorize = !DisableVectorization;
481 PMB.Inliner = createFunctionInliningPass();
482 PMB.LibraryInfo = new TargetLibraryInfoImpl(TargetTriple);
483 PMB.OptLevel = OptLevel;
484 PMB.VerifyInput = !DisableVerify;
485 PMB.VerifyOutput = !DisableVerify;
487 PMB.populateLTOPassManager(passes);
489 // Run our queue of passes all at once now, efficiently.
490 passes.run(*MergedModule);
495 bool LTOCodeGenerator::compileOptimized(ArrayRef<raw_pwrite_stream *> Out) {
496 if (!this->determineTarget())
499 legacy::PassManager preCodeGenPasses;
501 // If the bitcode files contain ARC code and were compiled with optimization,
502 // the ObjCARCContractPass must be run, so do it unconditionally here.
503 preCodeGenPasses.add(createObjCARCContractPass());
504 preCodeGenPasses.run(*MergedModule);
506 // Do code generation. We need to preserve the module in case the client calls
507 // writeMergedModules() after compilation, but we only need to allow this at
508 // parallelism level 1. This is achieved by having splitCodeGen return the
509 // original module at parallelism level 1 which we then assign back to
512 splitCodeGen(std::move(MergedModule), Out, MCpu, FeatureStr, Options,
513 RelocModel, CodeModel::Default, CGOptLevel, FileType);
518 /// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
520 void LTOCodeGenerator::setCodeGenDebugOptions(const char *Options) {
521 for (std::pair<StringRef, StringRef> o = getToken(Options); !o.first.empty();
522 o = getToken(o.second))
523 CodegenOptions.push_back(o.first);
526 void LTOCodeGenerator::parseCodeGenDebugOptions() {
527 // if options were requested, set them
528 if (!CodegenOptions.empty()) {
529 // ParseCommandLineOptions() expects argv[0] to be program name.
530 std::vector<const char *> CodegenArgv(1, "libLLVMLTO");
531 for (std::string &Arg : CodegenOptions)
532 CodegenArgv.push_back(Arg.c_str());
533 cl::ParseCommandLineOptions(CodegenArgv.size(), CodegenArgv.data());
537 void LTOCodeGenerator::DiagnosticHandler(const DiagnosticInfo &DI,
539 ((LTOCodeGenerator *)Context)->DiagnosticHandler2(DI);
542 void LTOCodeGenerator::DiagnosticHandler2(const DiagnosticInfo &DI) {
543 // Map the LLVM internal diagnostic severity to the LTO diagnostic severity.
544 lto_codegen_diagnostic_severity_t Severity;
545 switch (DI.getSeverity()) {
547 Severity = LTO_DS_ERROR;
550 Severity = LTO_DS_WARNING;
553 Severity = LTO_DS_REMARK;
556 Severity = LTO_DS_NOTE;
559 // Create the string that will be reported to the external diagnostic handler.
560 std::string MsgStorage;
561 raw_string_ostream Stream(MsgStorage);
562 DiagnosticPrinterRawOStream DP(Stream);
566 // If this method has been called it means someone has set up an external
567 // diagnostic handler. Assert on that.
568 assert(DiagHandler && "Invalid diagnostic handler");
569 (*DiagHandler)(Severity, MsgStorage.c_str(), DiagContext);
573 LTOCodeGenerator::setDiagnosticHandler(lto_diagnostic_handler_t DiagHandler,
575 this->DiagHandler = DiagHandler;
576 this->DiagContext = Ctxt;
578 return Context.setDiagnosticHandler(nullptr, nullptr);
579 // Register the LTOCodeGenerator stub in the LLVMContext to forward the
580 // diagnostic to the external DiagHandler.
581 Context.setDiagnosticHandler(LTOCodeGenerator::DiagnosticHandler, this,
582 /* RespectFilters */ true);
586 class LTODiagnosticInfo : public DiagnosticInfo {
589 LTODiagnosticInfo(const Twine &DiagMsg, DiagnosticSeverity Severity=DS_Error)
590 : DiagnosticInfo(DK_Linker, Severity), Msg(DiagMsg) {}
591 void print(DiagnosticPrinter &DP) const override { DP << Msg; }
595 void LTOCodeGenerator::emitError(const std::string &ErrMsg) {
597 (*DiagHandler)(LTO_DS_ERROR, ErrMsg.c_str(), DiagContext);
599 Context.diagnose(LTODiagnosticInfo(ErrMsg));