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/Bytecode/Reader.h"
21 #include "llvm/Bytecode/Writer.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/System/Program.h"
27 #include "llvm/System/Signals.h"
28 #include "llvm/Analysis/Passes.h"
29 #include "llvm/Analysis/Verifier.h"
30 #include "llvm/Target/SubtargetFeature.h"
31 #include "llvm/Target/TargetData.h"
32 #include "llvm/Target/TargetMachine.h"
33 #include "llvm/Target/TargetMachineRegistry.h"
34 #include "llvm/Target/TargetAsmInfo.h"
35 #include "llvm/Transforms/IPO.h"
36 #include "llvm/Transforms/Scalar.h"
37 #include "llvm/Analysis/LoadValueNumbering.h"
38 #include "llvm/Support/MathExtras.h"
39 #include "llvm/Support/Streams.h"
40 #include "llvm/LinkTimeOptimizer.h"
46 llvm::LinkTimeOptimizer *createLLVMOptimizer()
48 llvm::LTO *l = new llvm::LTO();
54 /// If symbol is not used then make it internal and let optimizer takes
56 void LLVMSymbol::mayBeNotUsed() {
57 gv->setLinkage(GlobalValue::InternalLinkage);
60 // Map LLVM LinkageType to LTO LinakgeType
61 static LTOLinkageTypes
62 getLTOLinkageType(GlobalValue *v)
65 if (v->hasExternalLinkage())
66 lt = LTOExternalLinkage;
67 else if (v->hasLinkOnceLinkage())
68 lt = LTOLinkOnceLinkage;
69 else if (v->hasWeakLinkage())
72 // Otherwise it is internal linkage for link time optimizer
73 lt = LTOInternalLinkage;
77 // Find exeternal symbols referenced by VALUE. This is a recursive function.
79 findExternalRefs(Value *value, std::set<std::string> &references,
82 if (GlobalValue *gv = dyn_cast<GlobalValue>(value)) {
83 LTOLinkageTypes lt = getLTOLinkageType(gv);
84 if (lt != LTOInternalLinkage && strncmp (gv->getName().c_str(), "llvm.", 5))
85 references.insert(mangler.getValueName(gv));
88 // GlobalValue, even with InternalLinkage type, may have operands with
89 // ExternalLinkage type. Do not ignore these operands.
90 if (Constant *c = dyn_cast<Constant>(value))
91 // Handle ConstantExpr, ConstantStruct, ConstantArry etc..
92 for (unsigned i = 0, e = c->getNumOperands(); i != e; ++i)
93 findExternalRefs(c->getOperand(i), references, mangler);
96 /// If Module with InputFilename is available then remove it from allModules
97 /// and call delete on it.
99 LTO::removeModule (const std::string &InputFilename)
101 NameToModuleMap::iterator pos = allModules.find(InputFilename.c_str());
102 if (pos == allModules.end())
105 Module *m = pos->second;
106 allModules.erase(pos);
110 /// InputFilename is a LLVM bytecode file. If Module with InputFilename is
111 /// available then return it. Otherwise parseInputFilename.
113 LTO::getModule(const std::string &InputFilename)
117 NameToModuleMap::iterator pos = allModules.find(InputFilename.c_str());
118 if (pos != allModules.end())
119 m = allModules[InputFilename.c_str()];
121 m = ParseBytecodeFile(InputFilename);
122 allModules[InputFilename.c_str()] = m;
127 /// InputFilename is a LLVM bytecode file. Reade this bytecode file and
128 /// set corresponding target triplet string.
130 LTO::getTargetTriple(const std::string &InputFilename,
131 std::string &targetTriple)
133 Module *m = getModule(InputFilename);
135 targetTriple = m->getTargetTriple();
138 /// InputFilename is a LLVM bytecode file. Read it using bytecode reader.
139 /// Collect global functions and symbol names in symbols vector.
140 /// Collect external references in references vector.
141 /// Return LTO_READ_SUCCESS if there is no error.
143 LTO::readLLVMObjectFile(const std::string &InputFilename,
144 NameToSymbolMap &symbols,
145 std::set<std::string> &references)
147 Module *m = getModule(InputFilename);
149 return LTO_READ_FAILURE;
151 // Collect Target info
155 return LTO_READ_FAILURE;
157 // Use mangler to add GlobalPrefix to names to match linker names.
158 // FIXME : Instead of hard coding "-" use GlobalPrefix.
159 Mangler mangler(*m, Target->getTargetAsmInfo()->getGlobalPrefix());
160 modules.push_back(m);
162 for (Module::iterator f = m->begin(), e = m->end(); f != e; ++f) {
164 LTOLinkageTypes lt = getLTOLinkageType(f);
166 if (!f->isDeclaration() && lt != LTOInternalLinkage
167 && strncmp (f->getName().c_str(), "llvm.", 5)) {
168 int alignment = ( 16 > f->getAlignment() ? 16 : f->getAlignment());
169 LLVMSymbol *newSymbol = new LLVMSymbol(lt, f, f->getName(),
170 mangler.getValueName(f),
172 symbols[newSymbol->getMangledName()] = newSymbol;
173 allSymbols[newSymbol->getMangledName()] = newSymbol;
176 // Collect external symbols referenced by this function.
177 for (Function::iterator b = f->begin(), fe = f->end(); b != fe; ++b)
178 for (BasicBlock::iterator i = b->begin(), be = b->end();
180 for (unsigned count = 0, total = i->getNumOperands();
181 count != total; ++count)
182 findExternalRefs(i->getOperand(count), references, mangler);
185 for (Module::global_iterator v = m->global_begin(), e = m->global_end();
187 LTOLinkageTypes lt = getLTOLinkageType(v);
188 if (!v->isDeclaration() && lt != LTOInternalLinkage
189 && strncmp (v->getName().c_str(), "llvm.", 5)) {
190 const TargetData *TD = Target->getTargetData();
191 LLVMSymbol *newSymbol = new LLVMSymbol(lt, v, v->getName(),
192 mangler.getValueName(v),
193 TD->getPreferredAlignmentLog(v));
194 symbols[newSymbol->getMangledName()] = newSymbol;
195 allSymbols[newSymbol->getMangledName()] = newSymbol;
197 for (unsigned count = 0, total = v->getNumOperands();
198 count != total; ++count)
199 findExternalRefs(v->getOperand(count), references, mangler);
204 return LTO_READ_SUCCESS;
207 /// Get TargetMachine.
208 /// Use module M to find appropriate Target.
210 LTO::getTarget (Module *M) {
216 const TargetMachineRegistry::Entry* March =
217 TargetMachineRegistry::getClosestStaticTargetForModule(*M, Err);
223 std::string Features;
224 Target = March->CtorFn(*M, Features);
227 /// Optimize module M using various IPO passes. Use exportList to
228 /// internalize selected symbols. Target platform is selected
229 /// based on information available to module M. No new target
230 /// features are selected.
232 LTO::optimize(Module *M, std::ostream &Out,
233 std::vector<const char *> &exportList)
235 // Instantiate the pass manager to organize the passes.
238 // Collect Target info
242 return LTO_NO_TARGET;
244 // Start off with a verification pass.
245 Passes.add(createVerifierPass());
247 // Add an appropriate TargetData instance for this module...
248 Passes.add(new TargetData(*Target->getTargetData()));
250 // Internalize symbols if export list is nonemty
251 if (!exportList.empty())
252 Passes.add(createInternalizePass(exportList));
254 // Now that we internalized some globals, see if we can hack on them!
255 Passes.add(createGlobalOptimizerPass());
257 // Linking modules together can lead to duplicated global constants, only
258 // keep one copy of each constant...
259 Passes.add(createConstantMergePass());
261 // If the -s command line option was specified, strip the symbols out of the
262 // resulting program to make it smaller. -s is a GLD option that we are
264 Passes.add(createStripSymbolsPass());
266 // Propagate constants at call sites into the functions they call.
267 Passes.add(createIPConstantPropagationPass());
269 // Remove unused arguments from functions...
270 Passes.add(createDeadArgEliminationPass());
272 Passes.add(createFunctionInliningPass()); // Inline small functions
274 Passes.add(createPruneEHPass()); // Remove dead EH info
276 Passes.add(createGlobalDCEPass()); // Remove dead functions
278 // If we didn't decide to inline a function, check to see if we can
279 // transform it to pass arguments by value instead of by reference.
280 Passes.add(createArgumentPromotionPass());
282 // The IPO passes may leave cruft around. Clean up after them.
283 Passes.add(createInstructionCombiningPass());
285 Passes.add(createScalarReplAggregatesPass()); // Break up allocas
287 // Run a few AA driven optimizations here and now, to cleanup the code.
288 Passes.add(createGlobalsModRefPass()); // IP alias analysis
290 Passes.add(createLICMPass()); // Hoist loop invariants
291 Passes.add(createLoadValueNumberingPass()); // GVN for load instrs
292 Passes.add(createGCSEPass()); // Remove common subexprs
293 Passes.add(createDeadStoreEliminationPass()); // Nuke dead stores
295 // Cleanup and simplify the code after the scalar optimizations.
296 Passes.add(createInstructionCombiningPass());
298 // Delete basic blocks, which optimization passes may have killed...
299 Passes.add(createCFGSimplificationPass());
301 // Now that we have optimized the program, discard unreachable functions...
302 Passes.add(createGlobalDCEPass());
304 // Make sure everything is still good.
305 Passes.add(createVerifierPass());
307 FunctionPassManager *CodeGenPasses =
308 new FunctionPassManager(new ExistingModuleProvider(M));
310 CodeGenPasses->add(new TargetData(*Target->getTargetData()));
311 Target->addPassesToEmitFile(*CodeGenPasses, Out, TargetMachine::AssemblyFile,
314 // Run our queue of passes all at once now, efficiently.
317 // Run the code generator, if present.
318 CodeGenPasses->doInitialization();
319 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
320 if (!I->isDeclaration())
321 CodeGenPasses->run(*I);
323 CodeGenPasses->doFinalization();
325 return LTO_OPT_SUCCESS;
328 ///Link all modules together and optimize them using IPO. Generate
329 /// native object file using OutputFilename
330 /// Return appropriate LTOStatus.
332 LTO::optimizeModules(const std::string &OutputFilename,
333 std::vector<const char *> &exportList,
334 std::string &targetTriple,
336 const char *FinalOutputFilename)
341 std::ios::openmode io_mode =
342 std::ios::out | std::ios::trunc | std::ios::binary;
343 std::string *errMsg = NULL;
344 Module *bigOne = modules[0];
345 Linker theLinker("LinkTimeOptimizer", bigOne, false);
346 for (unsigned i = 1, e = modules.size(); i != e; ++i)
347 if (theLinker.LinkModules(bigOne, modules[i], errMsg))
348 return LTO_MODULE_MERGE_FAILURE;
349 // all modules have been handed off to the linker.
352 sys::Path FinalOutputPath(FinalOutputFilename);
353 FinalOutputPath.eraseSuffix();
356 std::string tempFileName(FinalOutputPath.c_str());
357 tempFileName += "0.bc";
358 std::ofstream Out(tempFileName.c_str(), io_mode);
360 WriteBytecodeToFile(bigOne, L);
363 // Strip leading underscore because it was added to match names
365 for (unsigned i = 0, e = exportList.size(); i != e; ++i) {
366 const char *name = exportList[i];
367 NameToSymbolMap::iterator itr = allSymbols.find(name);
368 if (itr != allSymbols.end())
369 exportList[i] = allSymbols[name]->getName();
374 sys::Path TempDir = sys::Path::GetTemporaryDirectory(&ErrMsg);
375 if (TempDir.isEmpty()) {
376 cerr << "lto: " << ErrMsg << "\n";
377 return LTO_WRITE_FAILURE;
379 sys::Path tmpAsmFilePath(TempDir);
380 if (!tmpAsmFilePath.appendComponent("lto")) {
381 cerr << "lto: " << ErrMsg << "\n";
382 TempDir.eraseFromDisk(true);
383 return LTO_WRITE_FAILURE;
385 if (tmpAsmFilePath.createTemporaryFileOnDisk(&ErrMsg)) {
386 cerr << "lto: " << ErrMsg << "\n";
387 TempDir.eraseFromDisk(true);
388 return LTO_WRITE_FAILURE;
390 sys::RemoveFileOnSignal(tmpAsmFilePath);
392 std::ofstream asmFile(tmpAsmFilePath.c_str(), io_mode);
393 if (!asmFile.is_open() || asmFile.bad()) {
394 if (tmpAsmFilePath.exists()) {
395 tmpAsmFilePath.eraseFromDisk();
396 TempDir.eraseFromDisk(true);
398 return LTO_WRITE_FAILURE;
401 enum LTOStatus status = optimize(bigOne, asmFile, exportList);
403 if (status != LTO_OPT_SUCCESS) {
404 tmpAsmFilePath.eraseFromDisk();
405 TempDir.eraseFromDisk(true);
410 std::string tempFileName(FinalOutputPath.c_str());
411 tempFileName += "1.bc";
412 std::ofstream Out(tempFileName.c_str(), io_mode);
414 WriteBytecodeToFile(bigOne, L);
417 targetTriple = bigOne->getTargetTriple();
419 // Run GCC to assemble and link the program into native code.
422 // We can't just assemble and link the file with the system assembler
423 // and linker because we don't know where to put the _start symbol.
424 // GCC mysteriously knows how to do it.
425 const sys::Path gcc = sys::Program::FindProgramByName("gcc");
427 tmpAsmFilePath.eraseFromDisk();
428 TempDir.eraseFromDisk(true);
429 return LTO_ASM_FAILURE;
432 std::vector<const char*> args;
433 args.push_back(gcc.c_str());
434 args.push_back("-c");
435 args.push_back("-x");
436 args.push_back("assembler");
437 args.push_back("-o");
438 args.push_back(OutputFilename.c_str());
439 args.push_back(tmpAsmFilePath.c_str());
442 if (sys::Program::ExecuteAndWait(gcc, &args[0], 0, 0, 1, &ErrMsg)) {
443 cerr << "lto: " << ErrMsg << "\n";
444 return LTO_ASM_FAILURE;
447 tmpAsmFilePath.eraseFromDisk();
448 TempDir.eraseFromDisk(true);
450 return LTO_OPT_SUCCESS;
453 void LTO::printVersion() {
454 cl::PrintVersionMessage();
457 /// Unused pure-virtual destructor. Must remain empty.
458 LinkTimeOptimizer::~LinkTimeOptimizer() {}
460 /// Destruct LTO. Delete all modules, symbols and target.
463 for (std::vector<Module *>::iterator itr = modules.begin(), e = modules.end();
469 for (NameToSymbolMap::iterator itr = allSymbols.begin(), e = allSymbols.end();