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