1 //===-lto.cpp - LLVM Link Time Optimizer ----------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Devang Patel and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Link Time Optimization library. This library is
11 // intended to be used by linker to optimize code at link time.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Module.h"
16 #include "llvm/PassManager.h"
17 #include "llvm/Linker.h"
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/ModuleProvider.h"
21 #include "llvm/Bitcode/ReaderWriter.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/FileUtilities.h"
24 #include "llvm/Support/SystemUtils.h"
25 #include "llvm/Support/Mangler.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/System/Program.h"
28 #include "llvm/System/Signals.h"
29 #include "llvm/Analysis/Passes.h"
30 #include "llvm/Analysis/LoopPass.h"
31 #include "llvm/Analysis/Verifier.h"
32 #include "llvm/CodeGen/FileWriters.h"
33 #include "llvm/Target/SubtargetFeature.h"
34 #include "llvm/Target/TargetData.h"
35 #include "llvm/Target/TargetMachine.h"
36 #include "llvm/Target/TargetMachineRegistry.h"
37 #include "llvm/Target/TargetAsmInfo.h"
38 #include "llvm/Transforms/IPO.h"
39 #include "llvm/Transforms/Scalar.h"
40 #include "llvm/Analysis/LoadValueNumbering.h"
41 #include "llvm/Support/MathExtras.h"
42 #include "llvm/LinkTimeOptimizer.h"
48 llvm::LinkTimeOptimizer *createLLVMOptimizer(unsigned VERSION)
50 if (VERSION != LLVM_LTO_VERSION)
53 llvm::LTO *l = new llvm::LTO();
57 /// If symbol is not used then make it internal and let optimizer takes
59 void LLVMSymbol::mayBeNotUsed() {
60 gv->setLinkage(GlobalValue::InternalLinkage);
63 // Map LLVM LinkageType to LTO LinakgeType
64 static LTOLinkageTypes
65 getLTOLinkageType(GlobalValue *v)
68 if (v->hasExternalLinkage())
69 lt = LTOExternalLinkage;
70 else if (v->hasLinkOnceLinkage())
71 lt = LTOLinkOnceLinkage;
72 else if (v->hasWeakLinkage())
75 // Otherwise it is internal linkage for link time optimizer
76 lt = LTOInternalLinkage;
80 // Find exeternal symbols referenced by VALUE. This is a recursive function.
82 findExternalRefs(Value *value, std::set<std::string> &references,
85 if (GlobalValue *gv = dyn_cast<GlobalValue>(value)) {
86 LTOLinkageTypes lt = getLTOLinkageType(gv);
87 if (lt != LTOInternalLinkage && strncmp (gv->getName().c_str(), "llvm.", 5))
88 references.insert(mangler.getValueName(gv));
91 // GlobalValue, even with InternalLinkage type, may have operands with
92 // ExternalLinkage type. Do not ignore these operands.
93 if (Constant *c = dyn_cast<Constant>(value))
94 // Handle ConstantExpr, ConstantStruct, ConstantArry etc..
95 for (unsigned i = 0, e = c->getNumOperands(); i != e; ++i)
96 findExternalRefs(c->getOperand(i), references, mangler);
99 /// If Module with InputFilename is available then remove it from allModules
100 /// and call delete on it.
102 LTO::removeModule (const std::string &InputFilename)
104 NameToModuleMap::iterator pos = allModules.find(InputFilename.c_str());
105 if (pos == allModules.end())
108 Module *m = pos->second;
109 allModules.erase(pos);
113 /// InputFilename is a LLVM bitcode file. If Module with InputFilename is
114 /// available then return it. Otherwise parseInputFilename.
116 LTO::getModule(const std::string &InputFilename)
120 NameToModuleMap::iterator pos = allModules.find(InputFilename.c_str());
121 if (pos != allModules.end())
122 m = allModules[InputFilename.c_str()];
124 if (MemoryBuffer *Buffer
125 = MemoryBuffer::getFile(&InputFilename[0], InputFilename.size())) {
126 m = ParseBitcodeFile(Buffer);
129 allModules[InputFilename.c_str()] = m;
134 /// InputFilename is a LLVM bitcode file. Reade this bitcode file and
135 /// set corresponding target triplet string.
137 LTO::getTargetTriple(const std::string &InputFilename,
138 std::string &targetTriple)
140 Module *m = getModule(InputFilename);
142 targetTriple = m->getTargetTriple();
145 /// InputFilename is a LLVM bitcode file. Read it using bitcode reader.
146 /// Collect global functions and symbol names in symbols vector.
147 /// Collect external references in references vector.
148 /// Return LTO_READ_SUCCESS if there is no error.
150 LTO::readLLVMObjectFile(const std::string &InputFilename,
151 NameToSymbolMap &symbols,
152 std::set<std::string> &references)
154 Module *m = getModule(InputFilename);
156 return LTO_READ_FAILURE;
158 // Collect Target info
162 return LTO_READ_FAILURE;
164 // Use mangler to add GlobalPrefix to names to match linker names.
165 // FIXME : Instead of hard coding "-" use GlobalPrefix.
166 Mangler mangler(*m, Target->getTargetAsmInfo()->getGlobalPrefix());
167 modules.push_back(m);
169 for (Module::iterator f = m->begin(), e = m->end(); f != e; ++f) {
171 LTOLinkageTypes lt = getLTOLinkageType(f);
173 if (!f->isDeclaration() && lt != LTOInternalLinkage
174 && strncmp (f->getName().c_str(), "llvm.", 5)) {
175 int alignment = ( 16 > f->getAlignment() ? 16 : f->getAlignment());
176 LLVMSymbol *newSymbol = new LLVMSymbol(lt, f, f->getName(),
177 mangler.getValueName(f),
179 symbols[newSymbol->getMangledName()] = newSymbol;
180 allSymbols[newSymbol->getMangledName()] = newSymbol;
183 // Collect external symbols referenced by this function.
184 for (Function::iterator b = f->begin(), fe = f->end(); b != fe; ++b)
185 for (BasicBlock::iterator i = b->begin(), be = b->end();
187 for (unsigned count = 0, total = i->getNumOperands();
188 count != total; ++count)
189 findExternalRefs(i->getOperand(count), references, mangler);
192 for (Module::global_iterator v = m->global_begin(), e = m->global_end();
194 LTOLinkageTypes lt = getLTOLinkageType(v);
195 if (!v->isDeclaration() && lt != LTOInternalLinkage
196 && strncmp (v->getName().c_str(), "llvm.", 5)) {
197 const TargetData *TD = Target->getTargetData();
198 LLVMSymbol *newSymbol = new LLVMSymbol(lt, v, v->getName(),
199 mangler.getValueName(v),
200 TD->getPreferredAlignmentLog(v));
201 symbols[newSymbol->getMangledName()] = newSymbol;
202 allSymbols[newSymbol->getMangledName()] = newSymbol;
204 for (unsigned count = 0, total = v->getNumOperands();
205 count != total; ++count)
206 findExternalRefs(v->getOperand(count), references, mangler);
211 return LTO_READ_SUCCESS;
214 /// Get TargetMachine.
215 /// Use module M to find appropriate Target.
217 LTO::getTarget (Module *M) {
223 const TargetMachineRegistry::entry* March =
224 TargetMachineRegistry::getClosestStaticTargetForModule(*M, Err);
230 std::string Features;
231 Target = March->CtorFn(*M, Features);
234 /// Optimize module M using various IPO passes. Use exportList to
235 /// internalize selected symbols. Target platform is selected
236 /// based on information available to module M. No new target
237 /// features are selected.
239 LTO::optimize(Module *M, std::ostream &Out,
240 std::vector<const char *> &exportList)
242 // Instantiate the pass manager to organize the passes.
245 // Collect Target info
249 return LTO_NO_TARGET;
251 // Start off with a verification pass.
252 Passes.add(createVerifierPass());
254 // Add an appropriate TargetData instance for this module...
255 Passes.add(new TargetData(*Target->getTargetData()));
257 // Internalize symbols if export list is nonemty
258 if (!exportList.empty())
259 Passes.add(createInternalizePass(exportList));
261 // Now that we internalized some globals, see if we can hack on them!
262 Passes.add(createGlobalOptimizerPass());
264 // Linking modules together can lead to duplicated global constants, only
265 // keep one copy of each constant...
266 Passes.add(createConstantMergePass());
268 // If the -s command line option was specified, strip the symbols out of the
269 // resulting program to make it smaller. -s is a GLD option that we are
271 Passes.add(createStripSymbolsPass());
273 // Propagate constants at call sites into the functions they call.
274 Passes.add(createIPConstantPropagationPass());
276 // Remove unused arguments from functions...
277 Passes.add(createDeadArgEliminationPass());
279 Passes.add(createFunctionInliningPass()); // Inline small functions
281 Passes.add(createPruneEHPass()); // Remove dead EH info
283 Passes.add(createGlobalDCEPass()); // Remove dead functions
285 // If we didn't decide to inline a function, check to see if we can
286 // transform it to pass arguments by value instead of by reference.
287 Passes.add(createArgumentPromotionPass());
289 // The IPO passes may leave cruft around. Clean up after them.
290 Passes.add(createInstructionCombiningPass());
292 Passes.add(createScalarReplAggregatesPass()); // Break up allocas
294 // Run a few AA driven optimizations here and now, to cleanup the code.
295 Passes.add(createGlobalsModRefPass()); // IP alias analysis
297 Passes.add(createLICMPass()); // Hoist loop invariants
298 Passes.add(createLoadValueNumberingPass()); // GVN for load instrs
299 Passes.add(createGCSEPass()); // Remove common subexprs
300 Passes.add(createDeadStoreEliminationPass()); // Nuke dead stores
302 // Cleanup and simplify the code after the scalar optimizations.
303 Passes.add(createInstructionCombiningPass());
305 // Delete basic blocks, which optimization passes may have killed...
306 Passes.add(createCFGSimplificationPass());
308 // Now that we have optimized the program, discard unreachable functions...
309 Passes.add(createGlobalDCEPass());
311 // Make sure everything is still good.
312 Passes.add(createVerifierPass());
314 FunctionPassManager *CodeGenPasses =
315 new FunctionPassManager(new ExistingModuleProvider(M));
317 CodeGenPasses->add(new TargetData(*Target->getTargetData()));
319 MachineCodeEmitter *MCE = 0;
321 switch (Target->addPassesToEmitFile(*CodeGenPasses, Out,
322 TargetMachine::AssemblyFile, true)) {
324 case FileModel::Error:
325 return LTO_WRITE_FAILURE;
326 case FileModel::AsmFile:
328 case FileModel::MachOFile:
329 MCE = AddMachOWriter(*CodeGenPasses, Out, *Target);
331 case FileModel::ElfFile:
332 MCE = AddELFWriter(*CodeGenPasses, Out, *Target);
336 if (Target->addPassesToEmitFileFinish(*CodeGenPasses, MCE, true))
337 return LTO_WRITE_FAILURE;
339 // Run our queue of passes all at once now, efficiently.
342 // Run the code generator, if present.
343 CodeGenPasses->doInitialization();
344 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
345 if (!I->isDeclaration())
346 CodeGenPasses->run(*I);
348 CodeGenPasses->doFinalization();
350 return LTO_OPT_SUCCESS;
353 ///Link all modules together and optimize them using IPO. Generate
354 /// native object file using OutputFilename
355 /// Return appropriate LTOStatus.
357 LTO::optimizeModules(const std::string &OutputFilename,
358 std::vector<const char *> &exportList,
359 std::string &targetTriple,
361 const char *FinalOutputFilename)
366 std::ios::openmode io_mode =
367 std::ios::out | std::ios::trunc | std::ios::binary;
368 std::string *errMsg = NULL;
369 Module *bigOne = modules[0];
370 Linker theLinker("LinkTimeOptimizer", bigOne, false);
371 for (unsigned i = 1, e = modules.size(); i != e; ++i)
372 if (theLinker.LinkModules(bigOne, modules[i], errMsg))
373 return LTO_MODULE_MERGE_FAILURE;
374 // all modules have been handed off to the linker.
377 sys::Path FinalOutputPath(FinalOutputFilename);
378 FinalOutputPath.eraseSuffix();
381 std::string tempFileName(FinalOutputPath.c_str());
382 tempFileName += "0.bc";
383 std::ofstream Out(tempFileName.c_str(), io_mode);
384 WriteBitcodeToFile(bigOne, Out);
387 // Strip leading underscore because it was added to match names
389 for (unsigned i = 0, e = exportList.size(); i != e; ++i) {
390 const char *name = exportList[i];
391 NameToSymbolMap::iterator itr = allSymbols.find(name);
392 if (itr != allSymbols.end())
393 exportList[i] = allSymbols[name]->getName();
398 sys::Path TempDir = sys::Path::GetTemporaryDirectory(&ErrMsg);
399 if (TempDir.isEmpty()) {
400 cerr << "lto: " << ErrMsg << "\n";
401 return LTO_WRITE_FAILURE;
403 sys::Path tmpAsmFilePath(TempDir);
404 if (!tmpAsmFilePath.appendComponent("lto")) {
405 cerr << "lto: " << ErrMsg << "\n";
406 TempDir.eraseFromDisk(true);
407 return LTO_WRITE_FAILURE;
409 if (tmpAsmFilePath.createTemporaryFileOnDisk(true, &ErrMsg)) {
410 cerr << "lto: " << ErrMsg << "\n";
411 TempDir.eraseFromDisk(true);
412 return LTO_WRITE_FAILURE;
414 sys::RemoveFileOnSignal(tmpAsmFilePath);
416 std::ofstream asmFile(tmpAsmFilePath.c_str(), io_mode);
417 if (!asmFile.is_open() || asmFile.bad()) {
418 if (tmpAsmFilePath.exists()) {
419 tmpAsmFilePath.eraseFromDisk();
420 TempDir.eraseFromDisk(true);
422 return LTO_WRITE_FAILURE;
425 enum LTOStatus status = optimize(bigOne, asmFile, exportList);
427 if (status != LTO_OPT_SUCCESS) {
428 tmpAsmFilePath.eraseFromDisk();
429 TempDir.eraseFromDisk(true);
434 std::string tempFileName(FinalOutputPath.c_str());
435 tempFileName += "1.bc";
436 std::ofstream Out(tempFileName.c_str(), io_mode);
437 WriteBitcodeToFile(bigOne, Out);
440 targetTriple = bigOne->getTargetTriple();
442 // Run GCC to assemble and link the program into native code.
445 // We can't just assemble and link the file with the system assembler
446 // and linker because we don't know where to put the _start symbol.
447 // GCC mysteriously knows how to do it.
448 const sys::Path gcc = sys::Program::FindProgramByName("gcc");
450 tmpAsmFilePath.eraseFromDisk();
451 TempDir.eraseFromDisk(true);
452 return LTO_ASM_FAILURE;
455 std::vector<const char*> args;
456 args.push_back(gcc.c_str());
457 args.push_back("-c");
458 args.push_back("-x");
459 args.push_back("assembler");
460 args.push_back("-o");
461 args.push_back(OutputFilename.c_str());
462 args.push_back(tmpAsmFilePath.c_str());
465 if (sys::Program::ExecuteAndWait(gcc, &args[0], 0, 0, 1, 0, &ErrMsg)) {
466 cerr << "lto: " << ErrMsg << "\n";
467 return LTO_ASM_FAILURE;
470 tmpAsmFilePath.eraseFromDisk();
471 TempDir.eraseFromDisk(true);
473 return LTO_OPT_SUCCESS;
476 void LTO::printVersion() {
477 cl::PrintVersionMessage();
480 /// Unused pure-virtual destructor. Must remain empty.
481 LinkTimeOptimizer::~LinkTimeOptimizer() {}
483 /// Destruct LTO. Delete all modules, symbols and target.
486 for (std::vector<Module *>::iterator itr = modules.begin(), e = modules.end();
492 for (NameToSymbolMap::iterator itr = allSymbols.begin(), e = allSymbols.end();