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" |
Rafael Espindola | 156f4ee | 2016-04-28 19:30:41 +0000 | [diff] [blame] | 12 | #include "Driver.h" |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 13 | #include "Error.h" |
| 14 | #include "InputFiles.h" |
| 15 | #include "Symbols.h" |
Davide Italiano | d26c4a1 | 2016-05-15 19:29:38 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/AliasAnalysis.h" |
| 17 | #include "llvm/Analysis/CGSCCPassManager.h" |
| 18 | #include "llvm/Analysis/LoopPassManager.h" |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 20 | #include "llvm/Analysis/TargetTransformInfo.h" |
| 21 | #include "llvm/Bitcode/ReaderWriter.h" |
Davide Italiano | 8eca282 | 2016-04-01 00:35:29 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/CommandFlags.h" |
Davide Italiano | bc17663 | 2016-04-15 22:38:10 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/ParallelCG.h" |
Davide Italiano | 334fce9 | 2016-05-11 01:07:22 +0000 | [diff] [blame] | 24 | #include "llvm/IR/AutoUpgrade.h" |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/LegacyPassManager.h" |
Davide Italiano | d26c4a1 | 2016-05-15 19:29:38 +0000 | [diff] [blame] | 26 | #include "llvm/IR/PassManager.h" |
Davide Italiano | 5020d2a | 2016-05-15 19:43:02 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Verifier.h" |
Davide Italiano | 595ee8c | 2016-06-22 18:09:23 +0000 | [diff] [blame] | 28 | #include "llvm/LTO/UpdateCompilerUsed.h" |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 29 | #include "llvm/Linker/IRMover.h" |
Davide Italiano | d26c4a1 | 2016-05-15 19:29:38 +0000 | [diff] [blame] | 30 | #include "llvm/Passes/PassBuilder.h" |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 31 | #include "llvm/Support/StringSaver.h" |
| 32 | #include "llvm/Support/TargetRegistry.h" |
| 33 | #include "llvm/Target/TargetMachine.h" |
| 34 | #include "llvm/Transforms/IPO.h" |
| 35 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
Rafael Espindola | 9fdd071 | 2016-04-27 23:54:04 +0000 | [diff] [blame] | 36 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 37 | |
| 38 | using namespace llvm; |
| 39 | using namespace llvm::object; |
| 40 | using namespace llvm::ELF; |
| 41 | |
| 42 | using namespace lld; |
| 43 | using namespace lld::elf; |
| 44 | |
| 45 | // This is for use when debugging LTO. |
Davide Italiano | bc17663 | 2016-04-15 22:38:10 +0000 | [diff] [blame] | 46 | static void saveLtoObjectFile(StringRef Buffer, unsigned I, bool Many) { |
| 47 | SmallString<128> Filename = Config->OutputFile; |
| 48 | if (Many) |
| 49 | Filename += utostr(I); |
| 50 | Filename += ".lto.o"; |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 51 | std::error_code EC; |
Davide Italiano | bc17663 | 2016-04-15 22:38:10 +0000 | [diff] [blame] | 52 | raw_fd_ostream OS(Filename, EC, sys::fs::OpenFlags::F_None); |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 53 | check(EC); |
| 54 | OS << Buffer; |
| 55 | } |
| 56 | |
| 57 | // This is for use when debugging LTO. |
| 58 | static void saveBCFile(Module &M, StringRef Suffix) { |
| 59 | std::error_code EC; |
| 60 | raw_fd_ostream OS(Config->OutputFile.str() + Suffix.str(), EC, |
| 61 | sys::fs::OpenFlags::F_None); |
| 62 | check(EC); |
| 63 | WriteBitcodeToFile(&M, OS, /* ShouldPreserveUseListOrder */ true); |
| 64 | } |
| 65 | |
Davide Italiano | d26c4a1 | 2016-05-15 19:29:38 +0000 | [diff] [blame] | 66 | static void runNewCustomLtoPasses(Module &M, TargetMachine &TM) { |
| 67 | PassBuilder PB(&TM); |
| 68 | |
| 69 | AAManager AA; |
Davide Italiano | df24d5b | 2016-06-02 22:58:11 +0000 | [diff] [blame] | 70 | |
| 71 | // Parse a custom AA pipeline if asked to. |
| 72 | if (!PB.parseAAPipeline(AA, Config->LtoAAPipeline)) { |
| 73 | error("Unable to parse AA pipeline description: " + Config->LtoAAPipeline); |
| 74 | return; |
| 75 | } |
| 76 | |
Davide Italiano | d26c4a1 | 2016-05-15 19:29:38 +0000 | [diff] [blame] | 77 | LoopAnalysisManager LAM; |
| 78 | FunctionAnalysisManager FAM; |
| 79 | CGSCCAnalysisManager CGAM; |
| 80 | ModuleAnalysisManager MAM; |
| 81 | |
| 82 | // Register the AA manager first so that our version is the one used. |
| 83 | FAM.registerPass([&] { return std::move(AA); }); |
| 84 | |
| 85 | // Register all the basic analyses with the managers. |
| 86 | PB.registerModuleAnalyses(MAM); |
| 87 | PB.registerCGSCCAnalyses(CGAM); |
| 88 | PB.registerFunctionAnalyses(FAM); |
| 89 | PB.registerLoopAnalyses(LAM); |
| 90 | PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); |
| 91 | |
| 92 | ModulePassManager MPM; |
| 93 | if (!Config->DisableVerify) |
| 94 | MPM.addPass(VerifierPass()); |
| 95 | |
| 96 | // Now, add all the passes we've been requested to. |
| 97 | if (!PB.parsePassPipeline(MPM, Config->LtoNewPmPasses)) { |
| 98 | error("unable to parse pass pipeline description: " + |
| 99 | Config->LtoNewPmPasses); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | if (!Config->DisableVerify) |
| 104 | MPM.addPass(VerifierPass()); |
| 105 | MPM.run(M, MAM); |
| 106 | } |
| 107 | |
| 108 | static void runOldLtoPasses(Module &M, TargetMachine &TM) { |
| 109 | // Note that the gold plugin has a similar piece of code, so |
| 110 | // it is probably better to move this code to a common place. |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 111 | legacy::PassManager LtoPasses; |
| 112 | LtoPasses.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis())); |
| 113 | PassManagerBuilder PMB; |
| 114 | PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM.getTargetTriple())); |
| 115 | PMB.Inliner = createFunctionInliningPass(); |
Davide Italiano | 842fa53 | 2016-04-03 03:39:09 +0000 | [diff] [blame] | 116 | PMB.VerifyInput = PMB.VerifyOutput = !Config->DisableVerify; |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 117 | PMB.LoopVectorize = true; |
| 118 | PMB.SLPVectorize = true; |
Peter Collingbourne | ed22f9b | 2016-03-31 21:00:27 +0000 | [diff] [blame] | 119 | PMB.OptLevel = Config->LtoO; |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 120 | PMB.populateLTOPassManager(LtoPasses); |
| 121 | LtoPasses.run(M); |
Davide Italiano | d26c4a1 | 2016-05-15 19:29:38 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static void runLTOPasses(Module &M, TargetMachine &TM) { |
| 125 | if (!Config->LtoNewPmPasses.empty()) { |
| 126 | // The user explicitly asked for a set of passes to be run. |
| 127 | // This needs the new PM to work as there's no clean way to |
| 128 | // pass a set of passes to run in the legacy PM. |
| 129 | runNewCustomLtoPasses(M, TM); |
| 130 | if (HasError) |
| 131 | return; |
| 132 | } else { |
| 133 | // Run the 'default' set of LTO passes. This code still uses |
| 134 | // the legacy PM as the new one is not the default. |
| 135 | runOldLtoPasses(M, TM); |
| 136 | } |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 137 | |
| 138 | if (Config->SaveTemps) |
| 139 | saveBCFile(M, ".lto.opt.bc"); |
| 140 | } |
| 141 | |
Rafael Espindola | ae605c1 | 2016-04-21 20:35:25 +0000 | [diff] [blame] | 142 | static bool shouldInternalize(const SmallPtrSet<GlobalValue *, 8> &Used, |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 143 | Symbol *S, GlobalValue *GV) { |
Davide Italiano | 3935690 | 2016-06-11 14:21:38 +0000 | [diff] [blame] | 144 | if (S->IsUsedInRegularObj || Used.count(GV)) |
Rafael Espindola | ae605c1 | 2016-04-21 20:35:25 +0000 | [diff] [blame] | 145 | return false; |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 146 | return !S->includeInDynsym(); |
Rafael Espindola | ae605c1 | 2016-04-21 20:35:25 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Rui Ueyama | 412c802 | 2016-04-22 21:16:18 +0000 | [diff] [blame] | 149 | BitcodeCompiler::BitcodeCompiler() |
Rafael Espindola | 156f4ee | 2016-04-28 19:30:41 +0000 | [diff] [blame] | 150 | : Combined(new llvm::Module("ld-temp.o", Driver->Context)), |
| 151 | Mover(*Combined) {} |
Rui Ueyama | 412c802 | 2016-04-22 21:16:18 +0000 | [diff] [blame] | 152 | |
Peter Collingbourne | 0ef3874 | 2016-05-12 19:46:14 +0000 | [diff] [blame] | 153 | static void undefine(Symbol *S) { |
Davide Italiano | 64ebf32 | 2016-06-01 16:38:13 +0000 | [diff] [blame] | 154 | replaceBody<Undefined>(S, S->body()->getName(), STV_DEFAULT, S->body()->Type); |
Peter Collingbourne | 0ef3874 | 2016-05-12 19:46:14 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Davide Italiano | 595ee8c | 2016-06-22 18:09:23 +0000 | [diff] [blame] | 157 | static void handleUndefinedAsmRefs(const BasicSymbolRef &Sym, GlobalValue *GV, |
| 158 | StringSet<> &AsmUndefinedRefs) { |
| 159 | // GV associated => not an assembly symbol, bail out. |
| 160 | if (GV) |
| 161 | return; |
| 162 | |
| 163 | // This is an undefined reference to a symbol in asm. We put that in |
| 164 | // compiler.used, so that we can preserve it from being dropped from |
| 165 | // the output, without necessarily preventing its internalization. |
| 166 | SmallString<64> Name; |
| 167 | raw_svector_ostream OS(Name); |
| 168 | Sym.printName(OS); |
| 169 | AsmUndefinedRefs.insert(Name.str()); |
| 170 | } |
| 171 | |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 172 | void BitcodeCompiler::add(BitcodeFile &F) { |
Rafael Espindola | 156f4ee | 2016-04-28 19:30:41 +0000 | [diff] [blame] | 173 | std::unique_ptr<IRObjectFile> Obj = std::move(F.Obj); |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 174 | std::vector<GlobalValue *> Keep; |
| 175 | unsigned BodyIndex = 0; |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 176 | ArrayRef<Symbol *> Syms = F.getSymbols(); |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 177 | |
Davide Italiano | 86f2bd5 | 2016-03-29 21:46:35 +0000 | [diff] [blame] | 178 | Module &M = Obj->getModule(); |
Davide Italiano | 493b683 | 2016-04-16 01:33:33 +0000 | [diff] [blame] | 179 | if (M.getDataLayoutStr().empty()) |
| 180 | fatal("invalid bitcode file: " + F.getName() + " has no datalayout"); |
Davide Italiano | 49fe4ed | 2016-03-29 23:57:22 +0000 | [diff] [blame] | 181 | |
Davide Italiano | 334fce9 | 2016-05-11 01:07:22 +0000 | [diff] [blame] | 182 | // Discard non-compatible debug infos if necessary. |
| 183 | M.materializeMetadata(); |
| 184 | UpgradeDebugInfo(M); |
| 185 | |
Davide Italiano | 49fe4ed | 2016-03-29 23:57:22 +0000 | [diff] [blame] | 186 | // If a symbol appears in @llvm.used, the linker is required |
| 187 | // to treat the symbol as there is a reference to the symbol |
| 188 | // that it cannot see. Therefore, we can't internalize. |
Davide Italiano | 86f2bd5 | 2016-03-29 21:46:35 +0000 | [diff] [blame] | 189 | SmallPtrSet<GlobalValue *, 8> Used; |
| 190 | collectUsedGlobalVariables(M, Used, /* CompilerUsed */ false); |
| 191 | |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 192 | for (const BasicSymbolRef &Sym : Obj->symbols()) { |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 193 | uint32_t Flags = Sym.getFlags(); |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 194 | GlobalValue *GV = Obj->getSymbolGV(Sym.getRawDataRefImpl()); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 195 | if (GV && GV->hasAppendingLinkage()) |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 196 | Keep.push_back(GV); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 197 | if (BitcodeFile::shouldSkip(Flags)) |
| 198 | continue; |
| 199 | Symbol *S = Syms[BodyIndex++]; |
Davide Italiano | 595ee8c | 2016-06-22 18:09:23 +0000 | [diff] [blame] | 200 | if (Flags & BasicSymbolRef::SF_Undefined) { |
| 201 | handleUndefinedAsmRefs(Sym, GV, AsmUndefinedRefs); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 202 | continue; |
Davide Italiano | 595ee8c | 2016-06-22 18:09:23 +0000 | [diff] [blame] | 203 | } |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 204 | auto *B = dyn_cast<DefinedBitcode>(S->body()); |
| 205 | if (!B || B->File != &F) |
Davide Italiano | 1460e9f | 2016-03-26 18:33:09 +0000 | [diff] [blame] | 206 | continue; |
Peter Collingbourne | 3ad1c1e | 2016-05-05 17:13:49 +0000 | [diff] [blame] | 207 | |
| 208 | // We collect the set of symbols we want to internalize here |
| 209 | // and change the linkage after the IRMover executed, i.e. after |
| 210 | // we imported the symbols and satisfied undefined references |
| 211 | // to it. We can't just change linkage here because otherwise |
| 212 | // the IRMover will just rename the symbol. |
| 213 | if (GV && shouldInternalize(Used, S, GV)) |
| 214 | InternalizedSyms.insert(GV->getName()); |
| 215 | |
| 216 | // At this point we know that either the combined LTO object will provide a |
| 217 | // definition of a symbol, or we will internalize it. In either case, we |
| 218 | // need to undefine the symbol. In the former case, the real definition |
| 219 | // needs to be able to replace the original definition without conflicting. |
| 220 | // In the latter case, we need to allow the combined LTO object to provide a |
| 221 | // definition with the same name, for example when doing parallel codegen. |
Peter Collingbourne | 0ef3874 | 2016-05-12 19:46:14 +0000 | [diff] [blame] | 222 | undefine(S); |
Peter Collingbourne | 3ad1c1e | 2016-05-05 17:13:49 +0000 | [diff] [blame] | 223 | |
| 224 | if (!GV) |
| 225 | // Module asm symbol. |
| 226 | continue; |
| 227 | |
Davide Italiano | 1460e9f | 2016-03-26 18:33:09 +0000 | [diff] [blame] | 228 | switch (GV->getLinkage()) { |
| 229 | default: |
| 230 | break; |
| 231 | case llvm::GlobalValue::LinkOnceAnyLinkage: |
| 232 | GV->setLinkage(GlobalValue::WeakAnyLinkage); |
| 233 | break; |
| 234 | case llvm::GlobalValue::LinkOnceODRLinkage: |
| 235 | GV->setLinkage(GlobalValue::WeakODRLinkage); |
| 236 | break; |
Davide Italiano | d4c2a03 | 2016-03-22 22:31:34 +0000 | [diff] [blame] | 237 | } |
Davide Italiano | 828ac541 | 2016-03-28 15:44:21 +0000 | [diff] [blame] | 238 | |
Davide Italiano | 1460e9f | 2016-03-26 18:33:09 +0000 | [diff] [blame] | 239 | Keep.push_back(GV); |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Peter Collingbourne | 5079f3b | 2016-05-27 05:21:45 +0000 | [diff] [blame] | 242 | if (Error E = Mover.move(Obj->takeModule(), Keep, |
| 243 | [](GlobalValue &, IRMover::ValueAdder) {})) { |
| 244 | handleAllErrors(std::move(E), [&](const llvm::ErrorInfoBase &EIB) { |
| 245 | fatal("failed to link module " + F.getName() + ": " + EIB.message()); |
| 246 | }); |
| 247 | } |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Davide Italiano | 828ac541 | 2016-03-28 15:44:21 +0000 | [diff] [blame] | 250 | static void internalize(GlobalValue &GV) { |
| 251 | assert(!GV.hasLocalLinkage() && |
Davide Italiano | 47c33f0 | 2016-03-29 21:48:25 +0000 | [diff] [blame] | 252 | "Trying to internalize a symbol with local linkage!"); |
Davide Italiano | 828ac541 | 2016-03-28 15:44:21 +0000 | [diff] [blame] | 253 | GV.setLinkage(GlobalValue::InternalLinkage); |
| 254 | } |
| 255 | |
Rafael Espindola | abf6c65 | 2016-04-17 23:20:08 +0000 | [diff] [blame] | 256 | std::vector<std::unique_ptr<InputFile>> BitcodeCompiler::runSplitCodegen( |
| 257 | const std::function<std::unique_ptr<TargetMachine>()> &TMFactory) { |
Davide Italiano | bc17663 | 2016-04-15 22:38:10 +0000 | [diff] [blame] | 258 | unsigned NumThreads = Config->LtoJobs; |
| 259 | OwningData.resize(NumThreads); |
| 260 | |
| 261 | std::list<raw_svector_ostream> OSs; |
| 262 | std::vector<raw_pwrite_stream *> OSPtrs; |
| 263 | for (SmallString<0> &Obj : OwningData) { |
| 264 | OSs.emplace_back(Obj); |
| 265 | OSPtrs.push_back(&OSs.back()); |
| 266 | } |
| 267 | |
Rafael Espindola | abf6c65 | 2016-04-17 23:20:08 +0000 | [diff] [blame] | 268 | splitCodeGen(std::move(Combined), OSPtrs, {}, TMFactory); |
Davide Italiano | bc17663 | 2016-04-15 22:38:10 +0000 | [diff] [blame] | 269 | |
| 270 | std::vector<std::unique_ptr<InputFile>> ObjFiles; |
| 271 | for (SmallString<0> &Obj : OwningData) |
| 272 | ObjFiles.push_back(createObjectFile( |
| 273 | MemoryBufferRef(Obj, "LLD-INTERNAL-combined-lto-object"))); |
| 274 | |
| 275 | if (Config->SaveTemps) |
| 276 | for (unsigned I = 0; I < NumThreads; ++I) |
| 277 | saveLtoObjectFile(OwningData[I], I, NumThreads > 1); |
| 278 | |
| 279 | return ObjFiles; |
| 280 | } |
| 281 | |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 282 | // Merge all the bitcode files we have seen, codegen the result |
| 283 | // and return the resulting ObjectFile. |
Davide Italiano | bc17663 | 2016-04-15 22:38:10 +0000 | [diff] [blame] | 284 | std::vector<std::unique_ptr<InputFile>> BitcodeCompiler::compile() { |
| 285 | TheTriple = Combined->getTargetTriple(); |
Davide Italiano | 828ac541 | 2016-03-28 15:44:21 +0000 | [diff] [blame] | 286 | for (const auto &Name : InternalizedSyms) { |
Davide Italiano | 15c41b2 | 2016-04-11 22:39:51 +0000 | [diff] [blame] | 287 | GlobalValue *GV = Combined->getNamedValue(Name.first()); |
Davide Italiano | 828ac541 | 2016-03-28 15:44:21 +0000 | [diff] [blame] | 288 | assert(GV); |
| 289 | internalize(*GV); |
| 290 | } |
| 291 | |
Rui Ueyama | 961f2ff | 2016-03-23 21:19:27 +0000 | [diff] [blame] | 292 | std::string Msg; |
Davide Italiano | bc17663 | 2016-04-15 22:38:10 +0000 | [diff] [blame] | 293 | const Target *T = TargetRegistry::lookupTarget(TheTriple, Msg); |
Rui Ueyama | 961f2ff | 2016-03-23 21:19:27 +0000 | [diff] [blame] | 294 | if (!T) |
| 295 | fatal("target not found: " + Msg); |
Davide Italiano | 8eca282 | 2016-04-01 00:35:29 +0000 | [diff] [blame] | 296 | TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Rafael Espindola | 254b58d | 2016-06-21 14:47:43 +0000 | [diff] [blame] | 297 | |
| 298 | // lld supports the new relocations. |
| 299 | Options.RelaxELFRelocations = true; |
| 300 | |
Rui Ueyama | 961f2ff | 2016-03-23 21:19:27 +0000 | [diff] [blame] | 301 | Reloc::Model R = Config->Pic ? Reloc::PIC_ : Reloc::Static; |
Rafael Espindola | abf6c65 | 2016-04-17 23:20:08 +0000 | [diff] [blame] | 302 | |
| 303 | auto CreateTargetMachine = [&]() { |
| 304 | return std::unique_ptr<TargetMachine>( |
| 305 | T->createTargetMachine(TheTriple, "", "", Options, R)); |
| 306 | }; |
| 307 | |
| 308 | std::unique_ptr<TargetMachine> TM = CreateTargetMachine(); |
Davide Italiano | 595ee8c | 2016-06-22 18:09:23 +0000 | [diff] [blame] | 309 | |
| 310 | // Update llvm.compiler.used so that optimizations won't strip |
| 311 | // off AsmUndefinedReferences. |
Davide Italiano | 30afae1 | 2016-06-22 19:51:05 +0000 | [diff] [blame^] | 312 | updateCompilerUsed(*Combined, *TM, AsmUndefinedRefs); |
Davide Italiano | 595ee8c | 2016-06-22 18:09:23 +0000 | [diff] [blame] | 313 | |
| 314 | if (Config->SaveTemps) |
| 315 | saveBCFile(*Combined, ".lto.bc"); |
| 316 | |
Rafael Espindola | abf6c65 | 2016-04-17 23:20:08 +0000 | [diff] [blame] | 317 | runLTOPasses(*Combined, *TM); |
Davide Italiano | d26c4a1 | 2016-05-15 19:29:38 +0000 | [diff] [blame] | 318 | if (HasError) |
| 319 | return {}; |
Rafael Espindola | abf6c65 | 2016-04-17 23:20:08 +0000 | [diff] [blame] | 320 | |
| 321 | return runSplitCodegen(CreateTargetMachine); |
Rui Ueyama | 961f2ff | 2016-03-23 21:19:27 +0000 | [diff] [blame] | 322 | } |