blob: bd3c215e39d2c2a02581f12e3c14a55075190dca [file] [log] [blame]
Rui Ueyama25992482016-03-22 20:52:10 +00001//===- 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 Italiano8eca2822016-04-01 00:35:29 +000018#include "llvm/CodeGen/CommandFlags.h"
Rui Ueyama25992482016-03-22 20:52:10 +000019#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 Italiano86f2bd52016-03-29 21:46:35 +000025#include "llvm/Transforms/Utils/ModuleUtils.h"
Rui Ueyama25992482016-03-22 20:52:10 +000026#include "llvm/Transforms/IPO/PassManagerBuilder.h"
27
28using namespace llvm;
29using namespace llvm::object;
30using namespace llvm::ELF;
31
32using namespace lld;
33using namespace lld::elf;
34
35// This is for use when debugging LTO.
36static 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.
45static 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 Ueyama4e62db42016-03-29 19:19:03 +000054// 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 Ueyama25992482016-03-22 20:52:10 +000056static 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 Italiano842fa532016-04-03 03:39:09 +000062 PMB.VerifyInput = PMB.VerifyOutput = !Config->DisableVerify;
Rui Ueyama25992482016-03-22 20:52:10 +000063 PMB.LoopVectorize = true;
64 PMB.SLPVectorize = true;
Peter Collingbourneed22f9b2016-03-31 21:00:27 +000065 PMB.OptLevel = Config->LtoO;
Rui Ueyama25992482016-03-22 20:52:10 +000066 PMB.populateLTOPassManager(LtoPasses);
67 LtoPasses.run(M);
68
69 if (Config->SaveTemps)
70 saveBCFile(M, ".lto.opt.bc");
71}
72
73void 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 Italiano86f2bd52016-03-29 21:46:35 +000080 Module &M = Obj->getModule();
Davide Italiano49fe4ed2016-03-29 23:57:22 +000081
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 Italiano86f2bd52016-03-29 21:46:35 +000085 SmallPtrSet<GlobalValue *, 8> Used;
86 collectUsedGlobalVariables(M, Used, /* CompilerUsed */ false);
87
Rui Ueyama25992482016-03-22 20:52:10 +000088 for (const BasicSymbolRef &Sym : Obj->symbols()) {
89 GlobalValue *GV = Obj->getSymbolGV(Sym.getRawDataRefImpl());
Peter Collingbourne7cf73ec2016-04-11 16:39:43 +000090 // Ignore module asm symbols.
91 if (!GV)
92 continue;
Rui Ueyama25992482016-03-22 20:52:10 +000093 if (GV->hasAppendingLinkage()) {
94 Keep.push_back(GV);
95 continue;
96 }
Davide Italiano1460e9f2016-03-26 18:33:09 +000097 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 Italianod4c2a032016-03-22 22:31:34 +0000111 }
Davide Italiano828ac5412016-03-28 15:44:21 +0000112
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 Espindola8caf33c2016-04-08 18:39:03 +0000121 if (!Config->Shared && !Config->ExportDynamic && !B->isUsedInRegularObj() &&
122 !B->MustBeInDynSym)
Davide Italiano86f2bd52016-03-29 21:46:35 +0000123 if (!Used.count(GV))
124 InternalizedSyms.insert(GV->getName());
Davide Italiano828ac5412016-03-28 15:44:21 +0000125
Davide Italiano1460e9f2016-03-26 18:33:09 +0000126 Keep.push_back(GV);
Rui Ueyama25992482016-03-22 20:52:10 +0000127 }
128
129 Mover.move(Obj->takeModule(), Keep,
130 [](GlobalValue &, IRMover::ValueAdder) {});
131}
132
Davide Italiano828ac5412016-03-28 15:44:21 +0000133static void internalize(GlobalValue &GV) {
134 assert(!GV.hasLocalLinkage() &&
Davide Italiano47c33f02016-03-29 21:48:25 +0000135 "Trying to internalize a symbol with local linkage!");
Davide Italiano828ac5412016-03-28 15:44:21 +0000136 GV.setLinkage(GlobalValue::InternalLinkage);
137}
138
Rui Ueyama25992482016-03-22 20:52:10 +0000139// Merge all the bitcode files we have seen, codegen the result
140// and return the resulting ObjectFile.
Rui Ueyama01ddc062016-03-29 19:08:46 +0000141std::unique_ptr<InputFile> BitcodeCompiler::compile() {
Davide Italiano828ac5412016-03-28 15:44:21 +0000142 for (const auto &Name : InternalizedSyms) {
143 GlobalValue *GV = Combined.getNamedValue(Name.first());
144 assert(GV);
145 internalize(*GV);
146 }
147
Rui Ueyama25992482016-03-22 20:52:10 +0000148 if (Config->SaveTemps)
149 saveBCFile(Combined, ".lto.bc");
150
Rui Ueyama961f2ff2016-03-23 21:19:27 +0000151 std::unique_ptr<TargetMachine> TM(getTargetMachine());
Rui Ueyama25992482016-03-22 20:52:10 +0000152 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 Ueyama01ddc062016-03-29 19:08:46 +0000164 return createObjectFile(*MB);
Rui Ueyama25992482016-03-22 20:52:10 +0000165}
166
Rui Ueyama961f2ff2016-03-23 21:19:27 +0000167TargetMachine *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 Italiano8eca2822016-04-01 00:35:29 +0000173 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rui Ueyama961f2ff2016-03-23 21:19:27 +0000174 Reloc::Model R = Config->Pic ? Reloc::PIC_ : Reloc::Static;
175 return T->createTargetMachine(TripleStr, "", "", Options, R);
176}