Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 1 | //===- LTO.cpp ------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Linker |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "LTO.h" |
| 11 | #include "Config.h" |
| 12 | #include "Error.h" |
| 13 | #include "InputFiles.h" |
| 14 | #include "Symbols.h" |
| 15 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 16 | #include "llvm/Analysis/TargetTransformInfo.h" |
| 17 | #include "llvm/Bitcode/ReaderWriter.h" |
| 18 | #include "llvm/IR/LegacyPassManager.h" |
| 19 | #include "llvm/Linker/IRMover.h" |
| 20 | #include "llvm/Support/StringSaver.h" |
| 21 | #include "llvm/Support/TargetRegistry.h" |
| 22 | #include "llvm/Target/TargetMachine.h" |
| 23 | #include "llvm/Transforms/IPO.h" |
| 24 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
| 25 | |
| 26 | using namespace llvm; |
| 27 | using namespace llvm::object; |
| 28 | using namespace llvm::ELF; |
| 29 | |
| 30 | using namespace lld; |
| 31 | using namespace lld::elf; |
| 32 | |
| 33 | // This is for use when debugging LTO. |
| 34 | static void saveLtoObjectFile(StringRef Buffer) { |
| 35 | std::error_code EC; |
| 36 | raw_fd_ostream OS(Config->OutputFile.str() + ".lto.o", EC, |
| 37 | sys::fs::OpenFlags::F_None); |
| 38 | check(EC); |
| 39 | OS << Buffer; |
| 40 | } |
| 41 | |
| 42 | // This is for use when debugging LTO. |
| 43 | static void saveBCFile(Module &M, StringRef Suffix) { |
| 44 | std::error_code EC; |
| 45 | raw_fd_ostream OS(Config->OutputFile.str() + Suffix.str(), EC, |
| 46 | sys::fs::OpenFlags::F_None); |
| 47 | check(EC); |
| 48 | WriteBitcodeToFile(&M, OS, /* ShouldPreserveUseListOrder */ true); |
| 49 | } |
| 50 | |
| 51 | // Run LTO passes. |
| 52 | // FIXME: Reduce code duplication by sharing this code with the gold plugin. |
| 53 | static void runLTOPasses(Module &M, TargetMachine &TM) { |
| 54 | legacy::PassManager LtoPasses; |
| 55 | LtoPasses.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis())); |
| 56 | PassManagerBuilder PMB; |
| 57 | PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM.getTargetTriple())); |
| 58 | PMB.Inliner = createFunctionInliningPass(); |
| 59 | PMB.VerifyInput = true; |
| 60 | PMB.VerifyOutput = true; |
| 61 | PMB.LoopVectorize = true; |
| 62 | PMB.SLPVectorize = true; |
| 63 | PMB.OptLevel = 2; // FIXME: This should be an option. |
| 64 | PMB.populateLTOPassManager(LtoPasses); |
| 65 | LtoPasses.run(M); |
| 66 | |
| 67 | if (Config->SaveTemps) |
| 68 | saveBCFile(M, ".lto.opt.bc"); |
| 69 | } |
| 70 | |
| 71 | void BitcodeCompiler::add(BitcodeFile &F) { |
| 72 | std::unique_ptr<IRObjectFile> Obj = |
| 73 | check(IRObjectFile::create(F.MB, Context)); |
| 74 | std::vector<GlobalValue *> Keep; |
| 75 | unsigned BodyIndex = 0; |
| 76 | ArrayRef<SymbolBody *> Bodies = F.getSymbols(); |
| 77 | |
| 78 | for (const BasicSymbolRef &Sym : Obj->symbols()) { |
| 79 | GlobalValue *GV = Obj->getSymbolGV(Sym.getRawDataRefImpl()); |
| 80 | assert(GV); |
| 81 | if (GV->hasAppendingLinkage()) { |
| 82 | Keep.push_back(GV); |
| 83 | continue; |
| 84 | } |
Davide Italiano | 1460e9f | 2016-03-26 18:33:09 +0000 | [diff] [blame] | 85 | if (BitcodeFile::shouldSkip(Sym)) |
| 86 | continue; |
| 87 | SymbolBody *B = Bodies[BodyIndex++]; |
| 88 | if (!B || &B->repl() != B || !isa<DefinedBitcode>(B)) |
| 89 | continue; |
| 90 | switch (GV->getLinkage()) { |
| 91 | default: |
| 92 | break; |
| 93 | case llvm::GlobalValue::LinkOnceAnyLinkage: |
| 94 | GV->setLinkage(GlobalValue::WeakAnyLinkage); |
| 95 | break; |
| 96 | case llvm::GlobalValue::LinkOnceODRLinkage: |
| 97 | GV->setLinkage(GlobalValue::WeakODRLinkage); |
| 98 | break; |
Davide Italiano | d4c2a03 | 2016-03-22 22:31:34 +0000 | [diff] [blame] | 99 | } |
Davide Italiano | 828ac541 | 2016-03-28 15:44:21 +0000 | [diff] [blame] | 100 | |
| 101 | // We collect the set of symbols we want to internalize here |
| 102 | // and change the linkage after the IRMover executed, i.e. after |
| 103 | // we imported the symbols and satisfied undefined references |
| 104 | // to it. We can't just change linkage here because otherwise |
| 105 | // the IRMover will just rename the symbol. |
| 106 | // Shared libraries need to be handled slightly differently. |
| 107 | // For now, let's be conservative and just never internalize |
| 108 | // symbols when creating a shared library. |
Davide Italiano | 3acdfee | 2016-03-29 04:34:09 +0000 | [diff] [blame] | 109 | if (!Config->Shared && !Config->ExportDynamic && !B->isUsedInRegularObj()) |
Davide Italiano | 828ac541 | 2016-03-28 15:44:21 +0000 | [diff] [blame] | 110 | InternalizedSyms.insert(GV->getName()); |
| 111 | |
Davide Italiano | 1460e9f | 2016-03-26 18:33:09 +0000 | [diff] [blame] | 112 | Keep.push_back(GV); |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | Mover.move(Obj->takeModule(), Keep, |
| 116 | [](GlobalValue &, IRMover::ValueAdder) {}); |
| 117 | } |
| 118 | |
Davide Italiano | 828ac541 | 2016-03-28 15:44:21 +0000 | [diff] [blame] | 119 | static void internalize(GlobalValue &GV) { |
| 120 | assert(!GV.hasLocalLinkage() && |
| 121 | "Trying to internalize a symbol with local linkage!") ; |
| 122 | GV.setLinkage(GlobalValue::InternalLinkage); |
| 123 | } |
| 124 | |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 125 | // Merge all the bitcode files we have seen, codegen the result |
| 126 | // and return the resulting ObjectFile. |
Rui Ueyama | 01ddc06 | 2016-03-29 19:08:46 +0000 | [diff] [blame^] | 127 | std::unique_ptr<InputFile> BitcodeCompiler::compile() { |
Davide Italiano | 828ac541 | 2016-03-28 15:44:21 +0000 | [diff] [blame] | 128 | for (const auto &Name : InternalizedSyms) { |
| 129 | GlobalValue *GV = Combined.getNamedValue(Name.first()); |
| 130 | assert(GV); |
| 131 | internalize(*GV); |
| 132 | } |
| 133 | |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 134 | if (Config->SaveTemps) |
| 135 | saveBCFile(Combined, ".lto.bc"); |
| 136 | |
Rui Ueyama | 961f2ff | 2016-03-23 21:19:27 +0000 | [diff] [blame] | 137 | std::unique_ptr<TargetMachine> TM(getTargetMachine()); |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 138 | runLTOPasses(Combined, *TM); |
| 139 | |
| 140 | raw_svector_ostream OS(OwningData); |
| 141 | legacy::PassManager CodeGenPasses; |
| 142 | if (TM->addPassesToEmitFile(CodeGenPasses, OS, |
| 143 | TargetMachine::CGFT_ObjectFile)) |
| 144 | fatal("failed to setup codegen"); |
| 145 | CodeGenPasses.run(Combined); |
| 146 | MB = MemoryBuffer::getMemBuffer(OwningData, |
| 147 | "LLD-INTERNAL-combined-lto-object", false); |
| 148 | if (Config->SaveTemps) |
| 149 | saveLtoObjectFile(MB->getBuffer()); |
Rui Ueyama | 01ddc06 | 2016-03-29 19:08:46 +0000 | [diff] [blame^] | 150 | return createObjectFile(*MB); |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Rui Ueyama | 961f2ff | 2016-03-23 21:19:27 +0000 | [diff] [blame] | 153 | TargetMachine *BitcodeCompiler::getTargetMachine() { |
| 154 | StringRef TripleStr = Combined.getTargetTriple(); |
| 155 | std::string Msg; |
| 156 | const Target *T = TargetRegistry::lookupTarget(TripleStr, Msg); |
| 157 | if (!T) |
| 158 | fatal("target not found: " + Msg); |
| 159 | TargetOptions Options; |
| 160 | Reloc::Model R = Config->Pic ? Reloc::PIC_ : Reloc::Static; |
| 161 | return T->createTargetMachine(TripleStr, "", "", Options, R); |
| 162 | } |