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