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/Support/MathExtras.h"
40 #include "llvm/Support/Streams.h"
41 #include "llvm/LinkTimeOptimizer.h"
47 llvm::LinkTimeOptimizer *createLLVMOptimizer()
49 llvm::LTO *l = new llvm::LTO();
55 /// If symbol is not used then make it internal and let optimizer takes
57 void LLVMSymbol::mayBeNotUsed() {
58 gv->setLinkage(GlobalValue::InternalLinkage);
61 // Map LLVM LinkageType to LTO LinakgeType
62 static LTOLinkageTypes
63 getLTOLinkageType(GlobalValue *v)
66 if (v->hasExternalLinkage())
67 lt = LTOExternalLinkage;
68 else if (v->hasLinkOnceLinkage())
69 lt = LTOLinkOnceLinkage;
70 else if (v->hasWeakLinkage())
73 // Otherwise it is internal linkage for link time optimizer
74 lt = LTOInternalLinkage;
78 // Find exeternal symbols referenced by VALUE. This is a recursive function.
80 findExternalRefs(Value *value, std::set<std::string> &references,
83 if (GlobalValue *gv = dyn_cast<GlobalValue>(value)) {
84 LTOLinkageTypes lt = getLTOLinkageType(gv);
85 if (lt != LTOInternalLinkage && strncmp (gv->getName().c_str(), "llvm.", 5))
86 references.insert(mangler.getValueName(gv));
89 // GlobalValue, even with InternalLinkage type, may have operands with
90 // ExternalLinkage type. Do not ignore these operands.
91 if (Constant *c = dyn_cast<Constant>(value))
92 // Handle ConstantExpr, ConstantStruct, ConstantArry etc..
93 for (unsigned i = 0, e = c->getNumOperands(); i != e; ++i)
94 findExternalRefs(c->getOperand(i), references, mangler);
97 /// If Module with InputFilename is available then remove it from allModules
98 /// and call delete on it.
100 LTO::removeModule (const std::string &InputFilename)
102 NameToModuleMap::iterator pos = allModules.find(InputFilename.c_str());
103 if (pos == allModules.end())
106 Module *m = pos->second;
107 allModules.erase(pos);
111 /// InputFilename is a LLVM bytecode file. If Module with InputFilename is
112 /// available then return it. Otherwise parseInputFilename.
114 LTO::getModule(const std::string &InputFilename)
118 NameToModuleMap::iterator pos = allModules.find(InputFilename.c_str());
119 if (pos != allModules.end())
120 m = allModules[InputFilename.c_str()];
122 m = ParseBytecodeFile(InputFilename);
123 allModules[InputFilename.c_str()] = m;
128 /// InputFilename is a LLVM bytecode file. Reade this bytecode file and
129 /// set corresponding target triplet string.
131 LTO::getTargetTriple(const std::string &InputFilename,
132 std::string &targetTriple)
134 Module *m = getModule(InputFilename);
136 targetTriple = m->getTargetTriple();
139 /// InputFilename is a LLVM bytecode file. Read it using bytecode reader.
140 /// Collect global functions and symbol names in symbols vector.
141 /// Collect external references in references vector.
142 /// Return LTO_READ_SUCCESS if there is no error.
144 LTO::readLLVMObjectFile(const std::string &InputFilename,
145 NameToSymbolMap &symbols,
146 std::set<std::string> &references)
148 Module *m = getModule(InputFilename);
150 return LTO_READ_FAILURE;
152 // 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 int alignment = ( 16 > f->getAlignment() ? 16 : f->getAlignment());
170 LLVMSymbol *newSymbol = new LLVMSymbol(lt, f, f->getName(),
171 mangler.getValueName(f),
173 symbols[newSymbol->getMangledName()] = newSymbol;
174 allSymbols[newSymbol->getMangledName()] = newSymbol;
177 // Collect external symbols referenced by this function.
178 for (Function::iterator b = f->begin(), fe = f->end(); b != fe; ++b)
179 for (BasicBlock::iterator i = b->begin(), be = b->end();
181 for (unsigned count = 0, total = i->getNumOperands();
182 count != total; ++count)
183 findExternalRefs(i->getOperand(count), references, mangler);
186 for (Module::global_iterator v = m->global_begin(), e = m->global_end();
188 LTOLinkageTypes lt = getLTOLinkageType(v);
189 if (!v->isExternal() && lt != LTOInternalLinkage
190 && strncmp (v->getName().c_str(), "llvm.", 5)) {
191 const TargetData *TD = Target->getTargetData();
192 LLVMSymbol *newSymbol = new LLVMSymbol(lt, v, v->getName(),
193 mangler.getValueName(v),
194 TD->getPreferredAlignmentLog(v));
195 symbols[newSymbol->getMangledName()] = newSymbol;
196 allSymbols[newSymbol->getMangledName()] = newSymbol;
198 for (unsigned count = 0, total = v->getNumOperands();
199 count != total; ++count)
200 findExternalRefs(v->getOperand(count), references, mangler);
205 return LTO_READ_SUCCESS;
208 /// Get TargetMachine.
209 /// Use module M to find appropriate Target.
211 LTO::getTarget (Module *M) {
217 const TargetMachineRegistry::Entry* March =
218 TargetMachineRegistry::getClosestStaticTargetForModule(*M, Err);
224 std::string Features;
225 Target = March->CtorFn(*M, Features);
228 /// Optimize module M using various IPO passes. Use exportList to
229 /// internalize selected symbols. Target platform is selected
230 /// based on information available to module M. No new target
231 /// features are selected.
233 LTO::optimize(Module *M, std::ostream &Out,
234 std::vector<const char *> &exportList)
236 // Instantiate the pass manager to organize the passes.
239 // Collect Target info
243 return LTO_NO_TARGET;
245 // Start off with a verification pass.
246 Passes.add(createVerifierPass());
248 // Add an appropriate TargetData instance for this module...
249 Passes.add(new TargetData(*Target->getTargetData()));
251 // Often if the programmer does not specify proper prototypes for the
252 // functions they are calling, they end up calling a vararg version of the
253 // function that does not get a body filled in (the real function has typed
254 // arguments). This pass merges the two functions.
255 Passes.add(createFunctionResolvingPass());
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()));
318 Target->addPassesToEmitFile(*CodeGenPasses, Out, TargetMachine::AssemblyFile,
321 // Run our queue of passes all at once now, efficiently.
324 // Run the code generator, if present.
325 CodeGenPasses->doInitialization();
326 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
327 if (!I->isExternal())
328 CodeGenPasses->run(*I);
330 CodeGenPasses->doFinalization();
332 return LTO_OPT_SUCCESS;
335 ///Link all modules together and optimize them using IPO. Generate
336 /// native object file using OutputFilename
337 /// Return appropriate LTOStatus.
339 LTO::optimizeModules(const std::string &OutputFilename,
340 std::vector<const char *> &exportList,
341 std::string &targetTriple,
343 const char *FinalOutputFilename)
348 std::ios::openmode io_mode =
349 std::ios::out | std::ios::trunc | std::ios::binary;
350 std::string *errMsg = NULL;
351 Module *bigOne = modules[0];
352 Linker theLinker("LinkTimeOptimizer", bigOne, false);
353 for (unsigned i = 1, e = modules.size(); i != e; ++i)
354 if (theLinker.LinkModules(bigOne, modules[i], errMsg))
355 return LTO_MODULE_MERGE_FAILURE;
357 sys::Path FinalOutputPath(FinalOutputFilename);
358 FinalOutputPath.eraseSuffix();
361 std::string tempFileName(FinalOutputPath.c_str());
362 tempFileName += "0.bc";
363 std::ofstream Out(tempFileName.c_str(), io_mode);
365 WriteBytecodeToFile(bigOne, L, true);
368 // Strip leading underscore because it was added to match names
370 for (unsigned i = 0, e = exportList.size(); i != e; ++i) {
371 const char *name = exportList[i];
372 NameToSymbolMap::iterator itr = allSymbols.find(name);
373 if (itr != allSymbols.end())
374 exportList[i] = allSymbols[name]->getName();
379 sys::Path TempDir = sys::Path::GetTemporaryDirectory(&ErrMsg);
380 if (TempDir.isEmpty()) {
381 cerr << "lto: " << ErrMsg << "\n";
382 return LTO_WRITE_FAILURE;
384 sys::Path tmpAsmFilePath(TempDir);
385 if (!tmpAsmFilePath.appendComponent("lto")) {
386 cerr << "lto: " << ErrMsg << "\n";
387 TempDir.eraseFromDisk(true);
388 return LTO_WRITE_FAILURE;
390 if (tmpAsmFilePath.createTemporaryFileOnDisk(&ErrMsg)) {
391 cerr << "lto: " << ErrMsg << "\n";
392 TempDir.eraseFromDisk(true);
393 return LTO_WRITE_FAILURE;
395 sys::RemoveFileOnSignal(tmpAsmFilePath);
397 std::ofstream asmFile(tmpAsmFilePath.c_str(), io_mode);
398 if (!asmFile.is_open() || asmFile.bad()) {
399 if (tmpAsmFilePath.exists()) {
400 tmpAsmFilePath.eraseFromDisk();
401 TempDir.eraseFromDisk(true);
403 return LTO_WRITE_FAILURE;
406 enum LTOStatus status = optimize(bigOne, asmFile, exportList);
408 if (status != LTO_OPT_SUCCESS) {
409 tmpAsmFilePath.eraseFromDisk();
410 TempDir.eraseFromDisk(true);
415 std::string tempFileName(FinalOutputPath.c_str());
416 tempFileName += "1.bc";
417 std::ofstream Out(tempFileName.c_str(), io_mode);
419 WriteBytecodeToFile(bigOne, L, true);
422 targetTriple = bigOne->getTargetTriple();
424 // Run GCC to assemble and link the program into native code.
427 // We can't just assemble and link the file with the system assembler
428 // and linker because we don't know where to put the _start symbol.
429 // GCC mysteriously knows how to do it.
430 const sys::Path gcc = sys::Program::FindProgramByName("gcc");
432 tmpAsmFilePath.eraseFromDisk();
433 TempDir.eraseFromDisk(true);
434 return LTO_ASM_FAILURE;
437 std::vector<const char*> args;
438 args.push_back(gcc.c_str());
439 args.push_back("-c");
440 args.push_back("-x");
441 args.push_back("assembler");
442 args.push_back("-o");
443 args.push_back(OutputFilename.c_str());
444 args.push_back(tmpAsmFilePath.c_str());
447 if (sys::Program::ExecuteAndWait(gcc, &args[0], 0, 0, 1, &ErrMsg)) {
448 cerr << "lto: " << ErrMsg << "\n";
449 return LTO_ASM_FAILURE;
452 tmpAsmFilePath.eraseFromDisk();
453 TempDir.eraseFromDisk(true);
455 return LTO_OPT_SUCCESS;
458 /// Destruct LTO. Delete all modules, symbols and target.
461 for (std::vector<Module *>::iterator itr = modules.begin(), e = modules.end();
467 for (NameToSymbolMap::iterator itr = allSymbols.begin(), e = allSymbols.end();