blob: 2fdf94a6c5fabb59f18b7bcd7bf72556530af12f [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());
90 assert(GV);
91 if (GV->hasAppendingLinkage()) {
92 Keep.push_back(GV);
93 continue;
94 }
Davide Italiano1460e9f2016-03-26 18:33:09 +000095 if (BitcodeFile::shouldSkip(Sym))
96 continue;
97 SymbolBody *B = Bodies[BodyIndex++];
98 if (!B || &B->repl() != B || !isa<DefinedBitcode>(B))
99 continue;
100 switch (GV->getLinkage()) {
101 default:
102 break;
103 case llvm::GlobalValue::LinkOnceAnyLinkage:
104 GV->setLinkage(GlobalValue::WeakAnyLinkage);
105 break;
106 case llvm::GlobalValue::LinkOnceODRLinkage:
107 GV->setLinkage(GlobalValue::WeakODRLinkage);
108 break;
Davide Italianod4c2a032016-03-22 22:31:34 +0000109 }
Davide Italiano828ac5412016-03-28 15:44:21 +0000110
111 // We collect the set of symbols we want to internalize here
112 // and change the linkage after the IRMover executed, i.e. after
113 // we imported the symbols and satisfied undefined references
114 // to it. We can't just change linkage here because otherwise
115 // the IRMover will just rename the symbol.
116 // Shared libraries need to be handled slightly differently.
117 // For now, let's be conservative and just never internalize
118 // symbols when creating a shared library.
Rafael Espindola8caf33c2016-04-08 18:39:03 +0000119 if (!Config->Shared && !Config->ExportDynamic && !B->isUsedInRegularObj() &&
120 !B->MustBeInDynSym)
Davide Italiano86f2bd52016-03-29 21:46:35 +0000121 if (!Used.count(GV))
122 InternalizedSyms.insert(GV->getName());
Davide Italiano828ac5412016-03-28 15:44:21 +0000123
Davide Italiano1460e9f2016-03-26 18:33:09 +0000124 Keep.push_back(GV);
Rui Ueyama25992482016-03-22 20:52:10 +0000125 }
126
127 Mover.move(Obj->takeModule(), Keep,
128 [](GlobalValue &, IRMover::ValueAdder) {});
129}
130
Davide Italiano828ac5412016-03-28 15:44:21 +0000131static void internalize(GlobalValue &GV) {
132 assert(!GV.hasLocalLinkage() &&
Davide Italiano47c33f02016-03-29 21:48:25 +0000133 "Trying to internalize a symbol with local linkage!");
Davide Italiano828ac5412016-03-28 15:44:21 +0000134 GV.setLinkage(GlobalValue::InternalLinkage);
135}
136
Rui Ueyama25992482016-03-22 20:52:10 +0000137// Merge all the bitcode files we have seen, codegen the result
138// and return the resulting ObjectFile.
Rui Ueyama01ddc062016-03-29 19:08:46 +0000139std::unique_ptr<InputFile> BitcodeCompiler::compile() {
Davide Italiano828ac5412016-03-28 15:44:21 +0000140 for (const auto &Name : InternalizedSyms) {
141 GlobalValue *GV = Combined.getNamedValue(Name.first());
142 assert(GV);
143 internalize(*GV);
144 }
145
Rui Ueyama25992482016-03-22 20:52:10 +0000146 if (Config->SaveTemps)
147 saveBCFile(Combined, ".lto.bc");
148
Rui Ueyama961f2ff2016-03-23 21:19:27 +0000149 std::unique_ptr<TargetMachine> TM(getTargetMachine());
Rui Ueyama25992482016-03-22 20:52:10 +0000150 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 Ueyama01ddc062016-03-29 19:08:46 +0000162 return createObjectFile(*MB);
Rui Ueyama25992482016-03-22 20:52:10 +0000163}
164
Rui Ueyama961f2ff2016-03-23 21:19:27 +0000165TargetMachine *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 Italiano8eca2822016-04-01 00:35:29 +0000171 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rui Ueyama961f2ff2016-03-23 21:19:27 +0000172 Reloc::Model R = Config->Pic ? Reloc::PIC_ : Reloc::Static;
173 return T->createTargetMachine(TripleStr, "", "", Options, R);
174}