blob: ab97079bbd823ef3ec3a93f4f4c507a9fdf25571 [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 Italianod4c2a032016-03-22 22:31:34 +000085 if (!BitcodeFile::shouldSkip(Sym)) {
Rui Ueyama25992482016-03-22 20:52:10 +000086 if (SymbolBody *B = Bodies[BodyIndex++])
Davide Italianof20a55f2016-03-23 17:59:07 +000087 if (&B->repl() == B && isa<DefinedBitcode>(B)) {
88 if (GV->getLinkage() == llvm::GlobalValue::LinkOnceODRLinkage)
89 GV->setLinkage(GlobalValue::WeakODRLinkage);
Rui Ueyama25992482016-03-22 20:52:10 +000090 Keep.push_back(GV);
Davide Italianof20a55f2016-03-23 17:59:07 +000091 }
Davide Italianod4c2a032016-03-22 22:31:34 +000092 }
Rui Ueyama25992482016-03-22 20:52:10 +000093 }
94
95 Mover.move(Obj->takeModule(), Keep,
96 [](GlobalValue &, IRMover::ValueAdder) {});
97}
98
99// Merge all the bitcode files we have seen, codegen the result
100// and return the resulting ObjectFile.
101template <class ELFT>
102std::unique_ptr<elf::ObjectFile<ELFT>> BitcodeCompiler::compile() {
103 if (Config->SaveTemps)
104 saveBCFile(Combined, ".lto.bc");
105
106 StringRef TripleStr = Combined.getTargetTriple();
107 Triple TheTriple(TripleStr);
108
109 // FIXME: Should we have a default triple? The gold plugin uses
110 // sys::getDefaultTargetTriple(), but that is probably wrong given that this
111 // might be a cross linker.
112
113 std::string ErrMsg;
114 const Target *TheTarget = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
115 if (!TheTarget)
116 fatal("target not found: " + ErrMsg);
117
118 TargetOptions Options;
119 Reloc::Model R = Config->Pic ? Reloc::PIC_ : Reloc::Static;
120 std::unique_ptr<TargetMachine> TM(
121 TheTarget->createTargetMachine(TripleStr, "", "", Options, R));
122
123 runLTOPasses(Combined, *TM);
124
125 raw_svector_ostream OS(OwningData);
126 legacy::PassManager CodeGenPasses;
127 if (TM->addPassesToEmitFile(CodeGenPasses, OS,
128 TargetMachine::CGFT_ObjectFile))
129 fatal("failed to setup codegen");
130 CodeGenPasses.run(Combined);
131 MB = MemoryBuffer::getMemBuffer(OwningData,
132 "LLD-INTERNAL-combined-lto-object", false);
133 if (Config->SaveTemps)
134 saveLtoObjectFile(MB->getBuffer());
135
136 std::unique_ptr<InputFile> IF = createObjectFile(*MB);
137 auto *OF = cast<ObjectFile<ELFT>>(IF.release());
138 return std::unique_ptr<ObjectFile<ELFT>>(OF);
139}
140
141template std::unique_ptr<elf::ObjectFile<ELF32LE>> BitcodeCompiler::compile();
142template std::unique_ptr<elf::ObjectFile<ELF32BE>> BitcodeCompiler::compile();
143template std::unique_ptr<elf::ObjectFile<ELF64LE>> BitcodeCompiler::compile();
144template std::unique_ptr<elf::ObjectFile<ELF64BE>> BitcodeCompiler::compile();