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 implementes 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/SymbolTable.h"
21 #include "llvm/Bytecode/Reader.h"
22 #include "llvm/Bytecode/Writer.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/FileUtilities.h"
25 #include "llvm/Support/SystemUtils.h"
26 #include "llvm/Support/Mangler.h"
27 #include "llvm/System/Program.h"
28 #include "llvm/System/Signals.h"
29 #include "llvm/Analysis/Passes.h"
30 #include "llvm/Analysis/Verifier.h"
31 #include "llvm/Target/SubtargetFeature.h"
32 #include "llvm/Target/TargetData.h"
33 #include "llvm/Target/TargetMachine.h"
34 #include "llvm/Target/TargetMachineRegistry.h"
35 #include "llvm/Target/TargetAsmInfo.h"
36 #include "llvm/Transforms/IPO.h"
37 #include "llvm/Transforms/Scalar.h"
38 #include "llvm/Analysis/LoadValueNumbering.h"
39 #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
156 return LTO_READ_FAILURE;
158 // Use mangler to add GlobalPrefix to names to match linker names.
159 // FIXME : Instead of hard coding "-" use GlobalPrefix.
160 Mangler mangler(*m, Target->getTargetAsmInfo()->getGlobalPrefix());
161 modules.push_back(m);
163 for (Module::iterator f = m->begin(), e = m->end(); f != e; ++f) {
165 LTOLinkageTypes lt = getLTOLinkageType(f);
167 if (!f->isExternal() && lt != LTOInternalLinkage
168 && strncmp (f->getName().c_str(), "llvm.", 5)) {
169 LLVMSymbol *newSymbol = new LLVMSymbol(lt, f, f->getName(),
170 mangler.getValueName(f));
171 symbols[newSymbol->getMangledName()] = newSymbol;
172 allSymbols[newSymbol->getMangledName()] = newSymbol;
175 // Collect external symbols referenced by this function.
176 for (Function::iterator b = f->begin(), fe = f->end(); b != fe; ++b)
177 for (BasicBlock::iterator i = b->begin(), be = b->end();
179 for (unsigned count = 0, total = i->getNumOperands();
180 count != total; ++count)
181 findExternalRefs(i->getOperand(count), references, mangler);
184 for (Module::global_iterator v = m->global_begin(), e = m->global_end();
186 LTOLinkageTypes lt = getLTOLinkageType(v);
187 if (!v->isExternal() && lt != LTOInternalLinkage
188 && strncmp (v->getName().c_str(), "llvm.", 5)) {
189 LLVMSymbol *newSymbol = new LLVMSymbol(lt, v, v->getName(),
190 mangler.getValueName(v));
191 symbols[newSymbol->getMangledName()] = newSymbol;
192 allSymbols[newSymbol->getMangledName()] = newSymbol;
194 for (unsigned count = 0, total = v->getNumOperands();
195 count != total; ++count)
196 findExternalRefs(v->getOperand(count), references, mangler);
201 return LTO_READ_SUCCESS;
204 /// Get TargetMachine.
205 /// Use module M to find appropriate Target.
207 LTO::getTarget (Module *M) {
210 const TargetMachineRegistry::Entry* March =
211 TargetMachineRegistry::getClosestStaticTargetForModule(*M, Err);
217 std::string Features;
218 Target = March->CtorFn(*M, Features);
221 /// Optimize module M using various IPO passes. Use exportList to
222 /// internalize selected symbols. Target platform is selected
223 /// based on information available to module M. No new target
224 /// features are selected.
226 LTO::optimize(Module *M, std::ostream &Out,
227 std::vector<const char *> &exportList)
229 // Instantiate the pass manager to organize the passes.
232 // Collect Target info
237 return LTO_NO_TARGET;
239 // Start off with a verification pass.
240 Passes.add(createVerifierPass());
242 // Add an appropriate TargetData instance for this module...
243 Passes.add(new TargetData(*Target->getTargetData()));
245 // Often if the programmer does not specify proper prototypes for the
246 // functions they are calling, they end up calling a vararg version of the
247 // function that does not get a body filled in (the real function has typed
248 // arguments). This pass merges the two functions.
249 Passes.add(createFunctionResolvingPass());
251 // Internalize symbols if export list is nonemty
252 if (!exportList.empty())
253 Passes.add(createInternalizePass(exportList));
255 // Now that we internalized some globals, see if we can hack on them!
256 Passes.add(createGlobalOptimizerPass());
258 // Linking modules together can lead to duplicated global constants, only
259 // keep one copy of each constant...
260 Passes.add(createConstantMergePass());
262 // If the -s command line option was specified, strip the symbols out of the
263 // resulting program to make it smaller. -s is a GLD option that we are
265 Passes.add(createStripSymbolsPass());
267 // Propagate constants at call sites into the functions they call.
268 Passes.add(createIPConstantPropagationPass());
270 // Remove unused arguments from functions...
271 Passes.add(createDeadArgEliminationPass());
273 Passes.add(createFunctionInliningPass()); // Inline small functions
275 Passes.add(createPruneEHPass()); // Remove dead EH info
277 Passes.add(createGlobalDCEPass()); // Remove dead functions
279 // If we didn't decide to inline a function, check to see if we can
280 // transform it to pass arguments by value instead of by reference.
281 Passes.add(createArgumentPromotionPass());
283 // The IPO passes may leave cruft around. Clean up after them.
284 Passes.add(createInstructionCombiningPass());
286 Passes.add(createScalarReplAggregatesPass()); // Break up allocas
288 // Run a few AA driven optimizations here and now, to cleanup the code.
289 Passes.add(createGlobalsModRefPass()); // IP alias analysis
291 Passes.add(createLICMPass()); // Hoist loop invariants
292 Passes.add(createLoadValueNumberingPass()); // GVN for load instrs
293 Passes.add(createGCSEPass()); // Remove common subexprs
294 Passes.add(createDeadStoreEliminationPass()); // Nuke dead stores
296 // Cleanup and simplify the code after the scalar optimizations.
297 Passes.add(createInstructionCombiningPass());
299 // Delete basic blocks, which optimization passes may have killed...
300 Passes.add(createCFGSimplificationPass());
302 // Now that we have optimized the program, discard unreachable functions...
303 Passes.add(createGlobalDCEPass());
305 // Make sure everything is still good.
306 Passes.add(createVerifierPass());
308 FunctionPassManager *CodeGenPasses =
309 new FunctionPassManager(new ExistingModuleProvider(M));
311 CodeGenPasses->add(new TargetData(*Target->getTargetData()));
312 Target->addPassesToEmitFile(*CodeGenPasses, Out, TargetMachine::AssemblyFile,
315 // Run our queue of passes all at once now, efficiently.
318 // Run the code generator, if present.
319 CodeGenPasses->doInitialization();
320 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
321 if (!I->isExternal())
322 CodeGenPasses->run(*I);
324 CodeGenPasses->doFinalization();
326 return LTO_OPT_SUCCESS;
329 ///Link all modules together and optimize them using IPO. Generate
330 /// native object file using OutputFilename
331 /// Return appropriate LTOStatus.
333 LTO::optimizeModules(const std::string &OutputFilename,
334 std::vector<const char *> &exportList,
335 std::string &targetTriple)
340 std::ios::openmode io_mode =
341 std::ios::out | std::ios::trunc | std::ios::binary;
342 std::string *errMsg = NULL;
343 Module *bigOne = modules[0];
344 Linker theLinker("LinkTimeOptimizer", bigOne, false);
345 for (unsigned i = 1, e = modules.size(); i != e; ++i)
346 if (theLinker.LinkModules(bigOne, modules[i], errMsg))
347 return LTO_MODULE_MERGE_FAILURE;
350 // Enable this when -save-temps is used
351 std::ofstream Out("big.bc", io_mode);
352 WriteBytecodeToFile(bigOne, Out, true);
355 // Strip leading underscore because it was added to match names
357 for (unsigned i = 0, e = exportList.size(); i != e; ++i) {
358 const char *name = exportList[i];
359 NameToSymbolMap::iterator itr = allSymbols.find(name);
360 if (itr != allSymbols.end())
361 exportList[i] = allSymbols[name]->getName();
366 sys::Path TempDir = sys::Path::GetTemporaryDirectory(&ErrMsg);
367 if (TempDir.isEmpty()) {
368 std::cerr << "lto: " << ErrMsg << "\n";
369 return LTO_WRITE_FAILURE;
371 sys::Path tmpAsmFilePath(TempDir);
372 if (!tmpAsmFilePath.appendComponent("lto")) {
373 std::cerr << "lto: " << ErrMsg << "\n";
374 TempDir.eraseFromDisk(true);
375 return LTO_WRITE_FAILURE;
377 if (tmpAsmFilePath.createTemporaryFileOnDisk(&ErrMsg)) {
378 std::cerr << "lto: " << ErrMsg << "\n";
379 TempDir.eraseFromDisk(true);
380 return LTO_WRITE_FAILURE;
382 sys::RemoveFileOnSignal(tmpAsmFilePath);
384 std::ofstream asmFile(tmpAsmFilePath.c_str(), io_mode);
385 if (!asmFile.is_open() || asmFile.bad()) {
386 if (tmpAsmFilePath.exists()) {
387 tmpAsmFilePath.eraseFromDisk();
388 TempDir.eraseFromDisk(true);
390 return LTO_WRITE_FAILURE;
393 enum LTOStatus status = optimize(bigOne, asmFile, exportList);
395 if (status != LTO_OPT_SUCCESS) {
396 tmpAsmFilePath.eraseFromDisk();
397 TempDir.eraseFromDisk(true);
401 targetTriple = bigOne->getTargetTriple();
403 // Run GCC to assemble and link the program into native code.
406 // We can't just assemble and link the file with the system assembler
407 // and linker because we don't know where to put the _start symbol.
408 // GCC mysteriously knows how to do it.
409 const sys::Path gcc = sys::Program::FindProgramByName("gcc");
411 tmpAsmFilePath.eraseFromDisk();
412 TempDir.eraseFromDisk(true);
413 return LTO_ASM_FAILURE;
416 std::vector<const char*> args;
417 args.push_back(gcc.c_str());
418 args.push_back("-c");
419 args.push_back("-x");
420 args.push_back("assembler");
421 args.push_back("-o");
422 args.push_back(OutputFilename.c_str());
423 args.push_back(tmpAsmFilePath.c_str());
426 if (sys::Program::ExecuteAndWait(gcc, &args[0], 0, 0, 1, &ErrMsg)) {
427 std::cerr << "lto: " << ErrMsg << "\n";
428 return LTO_ASM_FAILURE;
431 tmpAsmFilePath.eraseFromDisk();
432 TempDir.eraseFromDisk(true);
434 return LTO_OPT_SUCCESS;
437 /// Destruct LTO. Delete all modules, symbols and target.
440 for (std::vector<Module *>::iterator itr = modules.begin(), e = modules.end();
446 for (NameToSymbolMap::iterator itr = allSymbols.begin(), e = allSymbols.end();