blob: bce268ec4f6236a2f6ceb302d63a59305f2802d6 [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.
Rui Ueyama01ddc062016-03-29 19:08:46 +0000127std::unique_ptr<InputFile> BitcodeCompiler::compile() {
Davide Italiano828ac5412016-03-28 15:44:21 +0000128 for (const auto &Name : InternalizedSyms) {
129 GlobalValue *GV = Combined.getNamedValue(Name.first());
130 assert(GV);
131 internalize(*GV);
132 }
133
Rui Ueyama25992482016-03-22 20:52:10 +0000134 if (Config->SaveTemps)
135 saveBCFile(Combined, ".lto.bc");
136
Rui Ueyama961f2ff2016-03-23 21:19:27 +0000137 std::unique_ptr<TargetMachine> TM(getTargetMachine());
Rui Ueyama25992482016-03-22 20:52:10 +0000138 runLTOPasses(Combined, *TM);
139
140 raw_svector_ostream OS(OwningData);
141 legacy::PassManager CodeGenPasses;
142 if (TM->addPassesToEmitFile(CodeGenPasses, OS,
143 TargetMachine::CGFT_ObjectFile))
144 fatal("failed to setup codegen");
145 CodeGenPasses.run(Combined);
146 MB = MemoryBuffer::getMemBuffer(OwningData,
147 "LLD-INTERNAL-combined-lto-object", false);
148 if (Config->SaveTemps)
149 saveLtoObjectFile(MB->getBuffer());
Rui Ueyama01ddc062016-03-29 19:08:46 +0000150 return createObjectFile(*MB);
Rui Ueyama25992482016-03-22 20:52:10 +0000151}
152
Rui Ueyama961f2ff2016-03-23 21:19:27 +0000153TargetMachine *BitcodeCompiler::getTargetMachine() {
154 StringRef TripleStr = Combined.getTargetTriple();
155 std::string Msg;
156 const Target *T = TargetRegistry::lookupTarget(TripleStr, Msg);
157 if (!T)
158 fatal("target not found: " + Msg);
159 TargetOptions Options;
160 Reloc::Model R = Config->Pic ? Reloc::PIC_ : Reloc::Static;
161 return T->createTargetMachine(TripleStr, "", "", Options, R);
162}