blob: 7b1c31ac693b9314827bae8abe7a3f873c99fdb7 [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)) {
Davide Italianobcd660a2016-03-23 18:41:48 +000088 switch (GV->getLinkage()) {
89 default:
90 break;
91 case llvm::GlobalValue::LinkOnceAnyLinkage:
92 GV->setLinkage(GlobalValue::WeakAnyLinkage);
93 break;
94 case llvm::GlobalValue::LinkOnceODRLinkage:
Davide Italianof20a55f2016-03-23 17:59:07 +000095 GV->setLinkage(GlobalValue::WeakODRLinkage);
Davide Italianobcd660a2016-03-23 18:41:48 +000096 break;
97 }
Rui Ueyama25992482016-03-22 20:52:10 +000098 Keep.push_back(GV);
Davide Italianof20a55f2016-03-23 17:59:07 +000099 }
Davide Italianod4c2a032016-03-22 22:31:34 +0000100 }
Rui Ueyama25992482016-03-22 20:52:10 +0000101 }
102
103 Mover.move(Obj->takeModule(), Keep,
104 [](GlobalValue &, IRMover::ValueAdder) {});
105}
106
107// Merge all the bitcode files we have seen, codegen the result
108// and return the resulting ObjectFile.
109template <class ELFT>
110std::unique_ptr<elf::ObjectFile<ELFT>> BitcodeCompiler::compile() {
111 if (Config->SaveTemps)
112 saveBCFile(Combined, ".lto.bc");
113
Rui Ueyama961f2ff2016-03-23 21:19:27 +0000114 std::unique_ptr<TargetMachine> TM(getTargetMachine());
Rui Ueyama25992482016-03-22 20:52:10 +0000115 runLTOPasses(Combined, *TM);
116
117 raw_svector_ostream OS(OwningData);
118 legacy::PassManager CodeGenPasses;
119 if (TM->addPassesToEmitFile(CodeGenPasses, OS,
120 TargetMachine::CGFT_ObjectFile))
121 fatal("failed to setup codegen");
122 CodeGenPasses.run(Combined);
123 MB = MemoryBuffer::getMemBuffer(OwningData,
124 "LLD-INTERNAL-combined-lto-object", false);
125 if (Config->SaveTemps)
126 saveLtoObjectFile(MB->getBuffer());
127
128 std::unique_ptr<InputFile> IF = createObjectFile(*MB);
129 auto *OF = cast<ObjectFile<ELFT>>(IF.release());
130 return std::unique_ptr<ObjectFile<ELFT>>(OF);
131}
132
Rui Ueyama961f2ff2016-03-23 21:19:27 +0000133TargetMachine *BitcodeCompiler::getTargetMachine() {
134 StringRef TripleStr = Combined.getTargetTriple();
135 std::string Msg;
136 const Target *T = TargetRegistry::lookupTarget(TripleStr, Msg);
137 if (!T)
138 fatal("target not found: " + Msg);
139 TargetOptions Options;
140 Reloc::Model R = Config->Pic ? Reloc::PIC_ : Reloc::Static;
141 return T->createTargetMachine(TripleStr, "", "", Options, R);
142}
143
Rui Ueyama25992482016-03-22 20:52:10 +0000144template std::unique_ptr<elf::ObjectFile<ELF32LE>> BitcodeCompiler::compile();
145template std::unique_ptr<elf::ObjectFile<ELF32BE>> BitcodeCompiler::compile();
146template std::unique_ptr<elf::ObjectFile<ELF64LE>> BitcodeCompiler::compile();
147template std::unique_ptr<elf::ObjectFile<ELF64BE>> BitcodeCompiler::compile();