blob: 180236c475f9749fa5185d5295af13f0ac233c6f [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();
62 PMB.VerifyInput = true;
63 PMB.VerifyOutput = true;
64 PMB.LoopVectorize = true;
65 PMB.SLPVectorize = true;
Peter Collingbourneed22f9b2016-03-31 21:00:27 +000066 PMB.OptLevel = Config->LtoO;
Rui Ueyama25992482016-03-22 20:52:10 +000067 PMB.populateLTOPassManager(LtoPasses);
68 LtoPasses.run(M);
69
70 if (Config->SaveTemps)
71 saveBCFile(M, ".lto.opt.bc");
72}
73
74void BitcodeCompiler::add(BitcodeFile &F) {
75 std::unique_ptr<IRObjectFile> Obj =
76 check(IRObjectFile::create(F.MB, Context));
77 std::vector<GlobalValue *> Keep;
78 unsigned BodyIndex = 0;
79 ArrayRef<SymbolBody *> Bodies = F.getSymbols();
80
Davide Italiano86f2bd52016-03-29 21:46:35 +000081 Module &M = Obj->getModule();
Davide Italiano49fe4ed2016-03-29 23:57:22 +000082
83 // If a symbol appears in @llvm.used, the linker is required
84 // to treat the symbol as there is a reference to the symbol
85 // that it cannot see. Therefore, we can't internalize.
Davide Italiano86f2bd52016-03-29 21:46:35 +000086 SmallPtrSet<GlobalValue *, 8> Used;
87 collectUsedGlobalVariables(M, Used, /* CompilerUsed */ false);
88
Rui Ueyama25992482016-03-22 20:52:10 +000089 for (const BasicSymbolRef &Sym : Obj->symbols()) {
90 GlobalValue *GV = Obj->getSymbolGV(Sym.getRawDataRefImpl());
91 assert(GV);
92 if (GV->hasAppendingLinkage()) {
93 Keep.push_back(GV);
94 continue;
95 }
Davide Italiano1460e9f2016-03-26 18:33:09 +000096 if (BitcodeFile::shouldSkip(Sym))
97 continue;
98 SymbolBody *B = Bodies[BodyIndex++];
99 if (!B || &B->repl() != B || !isa<DefinedBitcode>(B))
100 continue;
101 switch (GV->getLinkage()) {
102 default:
103 break;
104 case llvm::GlobalValue::LinkOnceAnyLinkage:
105 GV->setLinkage(GlobalValue::WeakAnyLinkage);
106 break;
107 case llvm::GlobalValue::LinkOnceODRLinkage:
108 GV->setLinkage(GlobalValue::WeakODRLinkage);
109 break;
Davide Italianod4c2a032016-03-22 22:31:34 +0000110 }
Davide Italiano828ac5412016-03-28 15:44:21 +0000111
112 // We collect the set of symbols we want to internalize here
113 // and change the linkage after the IRMover executed, i.e. after
114 // we imported the symbols and satisfied undefined references
115 // to it. We can't just change linkage here because otherwise
116 // the IRMover will just rename the symbol.
117 // Shared libraries need to be handled slightly differently.
118 // For now, let's be conservative and just never internalize
119 // symbols when creating a shared library.
Davide Italiano3acdfee2016-03-29 04:34:09 +0000120 if (!Config->Shared && !Config->ExportDynamic && !B->isUsedInRegularObj())
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}