Instead of hard coding global prefix, use TargetAsmInfo.
[oota-llvm.git] / tools / lto / lto.cpp
1 //===-lto.cpp - LLVM Link Time Optimizer ----------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implementes link time optimization library. This library is 
11 // intended to be used by linker to optimize code at link time.
12 //
13 //===----------------------------------------------------------------------===//
14
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"
40 #include <fstream>
41 #include <iostream>
42
43 using namespace llvm;
44
45 extern "C"
46 llvm::LinkTimeOptimizer *createLLVMOptimizer()
47 {
48   llvm::LTO *l = new llvm::LTO();
49   return l;
50 }
51
52
53
54 /// If symbol is not used then make it internal and let optimizer takes 
55 /// care of it.
56 void LLVMSymbol::mayBeNotUsed() { 
57   gv->setLinkage(GlobalValue::InternalLinkage); 
58 }
59
60 // Map LLVM LinkageType to LTO LinakgeType
61 static LTOLinkageTypes
62 getLTOLinkageType(GlobalValue *v)
63 {
64   LTOLinkageTypes lt;
65   if (v->hasExternalLinkage())
66     lt = LTOExternalLinkage;
67   else if (v->hasLinkOnceLinkage())
68     lt = LTOLinkOnceLinkage;
69   else if (v->hasWeakLinkage())
70     lt = LTOWeakLinkage;
71   else
72     // Otherwise it is internal linkage for link time optimizer
73     lt = LTOInternalLinkage;
74   return lt;
75 }
76
77 // Find exeternal symbols referenced by VALUE. This is a recursive function.
78 static void
79 findExternalRefs(Value *value, std::set<std::string> &references, 
80                  Mangler &mangler) {
81
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));
86   }
87
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);
94 }
95
96 /// If Module with InputFilename is available then remove it from allModules
97 /// and call delete on it.
98 void
99 LTO::removeModule (const std::string &InputFilename)
100 {
101   NameToModuleMap::iterator pos = allModules.find(InputFilename.c_str());
102   if (pos == allModules.end()) 
103     return;
104
105   Module *m = pos->second;
106   allModules.erase(pos);
107   delete m;
108 }
109
110 /// InputFilename is a LLVM bytecode file. If Module with InputFilename is
111 /// available then return it. Otherwise parseInputFilename.
112 Module *
113 LTO::getModule(const std::string &InputFilename)
114 {
115   Module *m = NULL;
116
117   NameToModuleMap::iterator pos = allModules.find(InputFilename.c_str());
118   if (pos != allModules.end())
119     m = allModules[InputFilename.c_str()];
120   else {
121     m = ParseBytecodeFile(InputFilename);
122     allModules[InputFilename.c_str()] = m;
123   }
124   return m;
125 }
126
127 /// InputFilename is a LLVM bytecode file. Reade this bytecode file and 
128 /// set corresponding target triplet string.
129 void
130 LTO::getTargetTriple(const std::string &InputFilename, 
131                                    std::string &targetTriple)
132 {
133   Module *m = getModule(InputFilename);
134   if (m)
135     targetTriple = m->getTargetTriple();
136 }
137
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.
142 enum LTOStatus
143 LTO::readLLVMObjectFile(const std::string &InputFilename,
144                                       NameToSymbolMap &symbols,
145                                       std::set<std::string> &references)
146 {
147   Module *m = getModule(InputFilename);
148   if (!m)
149     return LTO_READ_FAILURE;
150
151   // Collect Target info
152   if (!Target) 
153     getTarget(m);
154
155   if (!Target)
156     return LTO_READ_FAILURE;
157   
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);
162   
163   for (Module::iterator f = m->begin(), e = m->end(); f != e; ++f) {
164
165     LTOLinkageTypes lt = getLTOLinkageType(f);
166
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;
173     }
174
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(); 
178            i != be; ++i)
179         for (unsigned count = 0, total = i->getNumOperands(); 
180              count != total; ++count)
181           findExternalRefs(i->getOperand(count), references, mangler);
182   }
183     
184   for (Module::global_iterator v = m->global_begin(), e = m->global_end();
185        v !=  e; ++v) {
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;
193
194       for (unsigned count = 0, total = v->getNumOperands(); 
195            count != total; ++count)
196         findExternalRefs(v->getOperand(count), references, mangler);
197
198     }
199   }
200   
201   return LTO_READ_SUCCESS;
202 }
203
204 /// Get TargetMachine.
205 /// Use module M to find appropriate Target.
206 void
207 LTO::getTarget (Module *M) {
208
209   std::string Err;
210   const TargetMachineRegistry::Entry* March = 
211     TargetMachineRegistry::getClosestStaticTargetForModule(*M, Err);
212   
213   if (March == 0)
214     return;
215   
216   // Create target
217   std::string Features;
218   Target = March->CtorFn(*M, Features);
219 }
220
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. 
225 enum LTOStatus 
226 LTO::optimize(Module *M, std::ostream &Out,
227               std::vector<const char *> &exportList)
228 {
229   // Instantiate the pass manager to organize the passes.
230   PassManager Passes;
231   
232   // Collect Target info
233   if (!Target) 
234     getTarget(M);
235
236   if (!Target)
237     return LTO_NO_TARGET;
238   
239   // Start off with a verification pass.
240   Passes.add(createVerifierPass());
241   
242   // Add an appropriate TargetData instance for this module...
243   Passes.add(new TargetData(*Target->getTargetData()));
244   
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());
250   
251   // Internalize symbols if export list is nonemty
252   if (!exportList.empty())
253     Passes.add(createInternalizePass(exportList));
254
255   // Now that we internalized some globals, see if we can hack on them!
256   Passes.add(createGlobalOptimizerPass());
257   
258   // Linking modules together can lead to duplicated global constants, only
259   // keep one copy of each constant...
260   Passes.add(createConstantMergePass());
261   
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
264   // supporting.
265   Passes.add(createStripSymbolsPass());
266   
267   // Propagate constants at call sites into the functions they call.
268   Passes.add(createIPConstantPropagationPass());
269   
270   // Remove unused arguments from functions...
271   Passes.add(createDeadArgEliminationPass());
272   
273   Passes.add(createFunctionInliningPass()); // Inline small functions
274   
275   Passes.add(createPruneEHPass());            // Remove dead EH info
276
277   Passes.add(createGlobalDCEPass());          // Remove dead functions
278
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());
282
283   // The IPO passes may leave cruft around.  Clean up after them.
284   Passes.add(createInstructionCombiningPass());
285   
286   Passes.add(createScalarReplAggregatesPass()); // Break up allocas
287   
288   // Run a few AA driven optimizations here and now, to cleanup the code.
289   Passes.add(createGlobalsModRefPass());      // IP alias analysis
290   
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
295
296   // Cleanup and simplify the code after the scalar optimizations.
297   Passes.add(createInstructionCombiningPass());
298  
299   // Delete basic blocks, which optimization passes may have killed...
300   Passes.add(createCFGSimplificationPass());
301   
302   // Now that we have optimized the program, discard unreachable functions...
303   Passes.add(createGlobalDCEPass());
304   
305   // Make sure everything is still good.
306   Passes.add(createVerifierPass());
307
308   FunctionPassManager *CodeGenPasses =
309     new FunctionPassManager(new ExistingModuleProvider(M));
310
311   CodeGenPasses->add(new TargetData(*Target->getTargetData()));
312   Target->addPassesToEmitFile(*CodeGenPasses, Out, TargetMachine::AssemblyFile, 
313                              true);
314
315   // Run our queue of passes all at once now, efficiently.
316   Passes.run(*M);
317
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);
323   }
324   CodeGenPasses->doFinalization();
325
326   return LTO_OPT_SUCCESS;
327 }
328
329 ///Link all modules together and optimize them using IPO. Generate
330 /// native object file using OutputFilename
331 /// Return appropriate LTOStatus.
332 enum LTOStatus
333 LTO::optimizeModules(const std::string &OutputFilename,
334                                    std::vector<const char *> &exportList,
335                                    std::string &targetTriple)
336 {
337   if (modules.empty())
338     return LTO_NO_WORK;
339
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;
348
349 #if 0
350   // Enable this when -save-temps is used
351   std::ofstream Out("big.bc", io_mode);
352   WriteBytecodeToFile(bigOne, Out, true);
353 #endif
354
355   // Strip leading underscore because it was added to match names
356   // seen by linker.
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();
362   }
363
364
365   std::string ErrMsg;
366   sys::Path TempDir = sys::Path::GetTemporaryDirectory(&ErrMsg);
367   if (TempDir.isEmpty()) {
368     std::cerr << "lto: " << ErrMsg << "\n";
369     return LTO_WRITE_FAILURE;
370   }
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;
376   }
377   if (tmpAsmFilePath.createTemporaryFileOnDisk(&ErrMsg)) {
378     std::cerr << "lto: " << ErrMsg << "\n";
379     TempDir.eraseFromDisk(true);
380     return LTO_WRITE_FAILURE;
381   }
382   sys::RemoveFileOnSignal(tmpAsmFilePath);
383
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);
389     }
390     return LTO_WRITE_FAILURE;
391   }
392
393   enum LTOStatus status = optimize(bigOne, asmFile, exportList);
394   asmFile.close();
395   if (status != LTO_OPT_SUCCESS) {
396     tmpAsmFilePath.eraseFromDisk();
397     TempDir.eraseFromDisk(true);
398     return status;
399   }
400
401   targetTriple = bigOne->getTargetTriple();
402
403   // Run GCC to assemble and link the program into native code.
404   //
405   // Note:
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");
410   if (gcc.isEmpty()) {
411     tmpAsmFilePath.eraseFromDisk();
412     TempDir.eraseFromDisk(true);
413     return LTO_ASM_FAILURE;
414   }
415
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());
424   args.push_back(0);
425
426   if (sys::Program::ExecuteAndWait(gcc, &args[0], 0, 0, 1, &ErrMsg)) {
427     std::cerr << "lto: " << ErrMsg << "\n";
428     return LTO_ASM_FAILURE;
429   }
430
431   tmpAsmFilePath.eraseFromDisk();
432   TempDir.eraseFromDisk(true);
433
434   return LTO_OPT_SUCCESS;
435 }
436
437 /// Destruct LTO. Delete all modules, symbols and target.
438 LTO::~LTO() {
439   
440   for (std::vector<Module *>::iterator itr = modules.begin(), e = modules.end();
441        itr != e; ++itr)
442     delete *itr;
443
444   modules.clear();
445
446   for (NameToSymbolMap::iterator itr = allSymbols.begin(), e = allSymbols.end(); 
447        itr != e; ++itr)
448     delete itr->second;
449
450   allSymbols.clear();
451
452   delete Target;
453 }