blob: 76a5b06ad11d0ca7513ac6b79e916b18520ce7e0 [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"
18#include "llvm/IR/LegacyPassManager.h"
19#include "llvm/Linker/IRMover.h"
20#include "llvm/Support/StringSaver.h"
21#include "llvm/Support/TargetRegistry.h"
22#include "llvm/Target/TargetMachine.h"
23#include "llvm/Transforms/IPO.h"
24#include "llvm/Transforms/IPO/PassManagerBuilder.h"
25
26using namespace llvm;
27using namespace llvm::object;
28using namespace llvm::ELF;
29
30using namespace lld;
31using namespace lld::elf;
32
33// This is for use when debugging LTO.
34static void saveLtoObjectFile(StringRef Buffer) {
35 std::error_code EC;
36 raw_fd_ostream OS(Config->OutputFile.str() + ".lto.o", EC,
37 sys::fs::OpenFlags::F_None);
38 check(EC);
39 OS << Buffer;
40}
41
42// This is for use when debugging LTO.
43static void saveBCFile(Module &M, StringRef Suffix) {
44 std::error_code EC;
45 raw_fd_ostream OS(Config->OutputFile.str() + Suffix.str(), EC,
46 sys::fs::OpenFlags::F_None);
47 check(EC);
48 WriteBitcodeToFile(&M, OS, /* ShouldPreserveUseListOrder */ true);
49}
50
51// Run LTO passes.
52// FIXME: Reduce code duplication by sharing this code with the gold plugin.
53static void runLTOPasses(Module &M, TargetMachine &TM) {
54 legacy::PassManager LtoPasses;
55 LtoPasses.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis()));
56 PassManagerBuilder PMB;
57 PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM.getTargetTriple()));
58 PMB.Inliner = createFunctionInliningPass();
59 PMB.VerifyInput = true;
60 PMB.VerifyOutput = true;
61 PMB.LoopVectorize = true;
62 PMB.SLPVectorize = true;
63 PMB.OptLevel = 2; // FIXME: This should be an option.
64 PMB.populateLTOPassManager(LtoPasses);
65 LtoPasses.run(M);
66
67 if (Config->SaveTemps)
68 saveBCFile(M, ".lto.opt.bc");
69}
70
71void BitcodeCompiler::add(BitcodeFile &F) {
72 std::unique_ptr<IRObjectFile> Obj =
73 check(IRObjectFile::create(F.MB, Context));
74 std::vector<GlobalValue *> Keep;
75 unsigned BodyIndex = 0;
76 ArrayRef<SymbolBody *> Bodies = F.getSymbols();
77
78 for (const BasicSymbolRef &Sym : Obj->symbols()) {
79 GlobalValue *GV = Obj->getSymbolGV(Sym.getRawDataRefImpl());
80 assert(GV);
81 if (GV->hasAppendingLinkage()) {
82 Keep.push_back(GV);
83 continue;
84 }
Davide Italiano1460e9f2016-03-26 18:33:09 +000085 if (BitcodeFile::shouldSkip(Sym))
86 continue;
87 SymbolBody *B = Bodies[BodyIndex++];
88 if (!B || &B->repl() != B || !isa<DefinedBitcode>(B))
89 continue;
90 switch (GV->getLinkage()) {
91 default:
92 break;
93 case llvm::GlobalValue::LinkOnceAnyLinkage:
94 GV->setLinkage(GlobalValue::WeakAnyLinkage);
95 break;
96 case llvm::GlobalValue::LinkOnceODRLinkage:
97 GV->setLinkage(GlobalValue::WeakODRLinkage);
98 break;
Davide Italianod4c2a032016-03-22 22:31:34 +000099 }
Davide Italiano828ac5412016-03-28 15:44:21 +0000100
101 // We collect the set of symbols we want to internalize here
102 // and change the linkage after the IRMover executed, i.e. after
103 // we imported the symbols and satisfied undefined references
104 // to it. We can't just change linkage here because otherwise
105 // the IRMover will just rename the symbol.
106 // Shared libraries need to be handled slightly differently.
107 // For now, let's be conservative and just never internalize
108 // symbols when creating a shared library.
Davide Italiano3acdfee2016-03-29 04:34:09 +0000109 if (!Config->Shared && !Config->ExportDynamic && !B->isUsedInRegularObj())
Davide Italiano828ac5412016-03-28 15:44:21 +0000110 InternalizedSyms.insert(GV->getName());
111
Davide Italiano1460e9f2016-03-26 18:33:09 +0000112 Keep.push_back(GV);
Rui Ueyama25992482016-03-22 20:52:10 +0000113 }
114
115 Mover.move(Obj->takeModule(), Keep,
116 [](GlobalValue &, IRMover::ValueAdder) {});
117}
118
Davide Italiano828ac5412016-03-28 15:44:21 +0000119static void internalize(GlobalValue &GV) {
120 assert(!GV.hasLocalLinkage() &&
121 "Trying to internalize a symbol with local linkage!") ;
122 GV.setLinkage(GlobalValue::InternalLinkage);
123}
124
Rui Ueyama25992482016-03-22 20:52:10 +0000125// Merge all the bitcode files we have seen, codegen the result
126// and return the resulting ObjectFile.
127template <class ELFT>
128std::unique_ptr<elf::ObjectFile<ELFT>> BitcodeCompiler::compile() {
Davide Italiano828ac5412016-03-28 15:44:21 +0000129 for (const auto &Name : InternalizedSyms) {
130 GlobalValue *GV = Combined.getNamedValue(Name.first());
131 assert(GV);
132 internalize(*GV);
133 }
134
Rui Ueyama25992482016-03-22 20:52:10 +0000135 if (Config->SaveTemps)
136 saveBCFile(Combined, ".lto.bc");
137
Rui Ueyama961f2ff2016-03-23 21:19:27 +0000138 std::unique_ptr<TargetMachine> TM(getTargetMachine());
Rui Ueyama25992482016-03-22 20:52:10 +0000139 runLTOPasses(Combined, *TM);
140
141 raw_svector_ostream OS(OwningData);
142 legacy::PassManager CodeGenPasses;
143 if (TM->addPassesToEmitFile(CodeGenPasses, OS,
144 TargetMachine::CGFT_ObjectFile))
145 fatal("failed to setup codegen");
146 CodeGenPasses.run(Combined);
147 MB = MemoryBuffer::getMemBuffer(OwningData,
148 "LLD-INTERNAL-combined-lto-object", false);
149 if (Config->SaveTemps)
150 saveLtoObjectFile(MB->getBuffer());
151
152 std::unique_ptr<InputFile> IF = createObjectFile(*MB);
153 auto *OF = cast<ObjectFile<ELFT>>(IF.release());
154 return std::unique_ptr<ObjectFile<ELFT>>(OF);
155}
156
Rui Ueyama961f2ff2016-03-23 21:19:27 +0000157TargetMachine *BitcodeCompiler::getTargetMachine() {
158 StringRef TripleStr = Combined.getTargetTriple();
159 std::string Msg;
160 const Target *T = TargetRegistry::lookupTarget(TripleStr, Msg);
161 if (!T)
162 fatal("target not found: " + Msg);
163 TargetOptions Options;
164 Reloc::Model R = Config->Pic ? Reloc::PIC_ : Reloc::Static;
165 return T->createTargetMachine(TripleStr, "", "", Options, R);
166}
167
Rui Ueyama25992482016-03-22 20:52:10 +0000168template std::unique_ptr<elf::ObjectFile<ELF32LE>> BitcodeCompiler::compile();
169template std::unique_ptr<elf::ObjectFile<ELF32BE>> BitcodeCompiler::compile();
170template std::unique_ptr<elf::ObjectFile<ELF64LE>> BitcodeCompiler::compile();
171template std::unique_ptr<elf::ObjectFile<ELF64BE>> BitcodeCompiler::compile();