Allow a user of libLTO to specify the full pathname of the gcc executable to
[oota-llvm.git] / tools / lto / LTOCodeGenerator.cpp
1 //===-LTOCodeGenerator.cpp - LLVM Link Time Optimizer ---------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
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.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "LTOModule.h"
16 #include "LTOCodeGenerator.h"
17
18
19 #include "llvm/Module.h"
20 #include "llvm/PassManager.h"
21 #include "llvm/Linker.h"
22 #include "llvm/Constants.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/ModuleProvider.h"
25 #include "llvm/Bitcode/ReaderWriter.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/SystemUtils.h"
28 #include "llvm/Support/Mangler.h"
29 #include "llvm/Support/MemoryBuffer.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/System/Signals.h"
32 #include "llvm/Analysis/Passes.h"
33 #include "llvm/Analysis/LoopPass.h"
34 #include "llvm/Analysis/Verifier.h"
35 #include "llvm/CodeGen/FileWriters.h"
36 #include "llvm/Target/SubtargetFeature.h"
37 #include "llvm/Target/TargetOptions.h"
38 #include "llvm/Target/TargetData.h"
39 #include "llvm/Target/TargetMachine.h"
40 #include "llvm/Target/TargetMachineRegistry.h"
41 #include "llvm/Target/TargetAsmInfo.h"
42 #include "llvm/Transforms/IPO.h"
43 #include "llvm/Transforms/Scalar.h"
44 #include "llvm/ADT/StringExtras.h"
45 #include "llvm/Config/config.h"
46
47
48 #include <fstream>
49 #include <unistd.h>
50 #include <stdlib.h>
51 #include <fcntl.h>
52
53
54 using namespace llvm;
55
56 static cl::opt<bool> DisableInline("disable-inlining",
57   cl::desc("Do not run the inliner pass"));
58
59
60 const char* LTOCodeGenerator::getVersionString()
61 {
62 #ifdef LLVM_VERSION_INFO
63     return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
64 #else
65     return PACKAGE_NAME " version " PACKAGE_VERSION;
66 #endif
67 }
68
69
70 LTOCodeGenerator::LTOCodeGenerator() 
71     : _linker("LinkTimeOptimizer", "ld-temp.o"), _target(NULL),
72       _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false),
73       _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
74       _nativeObjectFile(NULL), _gccPath(NULL)
75 {
76
77 }
78
79 LTOCodeGenerator::~LTOCodeGenerator()
80 {
81     delete _target;
82     delete _nativeObjectFile;
83 }
84
85
86
87 bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg)
88 {
89     return _linker.LinkInModule(mod->getLLVVMModule(), &errMsg);
90 }
91     
92
93 bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug, std::string& errMsg)
94 {
95     switch (debug) {
96         case LTO_DEBUG_MODEL_NONE:
97             _emitDwarfDebugInfo = false;
98             return false;
99             
100         case LTO_DEBUG_MODEL_DWARF:
101             _emitDwarfDebugInfo = true;
102             return false;
103     }
104     errMsg = "unknown debug format";
105     return true;
106 }
107
108
109 bool LTOCodeGenerator::setCodePICModel(lto_codegen_model model, 
110                                                         std::string& errMsg)
111 {
112     switch (model) {
113         case LTO_CODEGEN_PIC_MODEL_STATIC:
114         case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
115         case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
116             _codeModel = model;
117             return false;
118     }
119     errMsg = "unknown pic model";
120     return true;
121 }
122
123 void LTOCodeGenerator::setGccPath(const char* path)
124 {
125     if ( _gccPath )
126         delete _gccPath;
127     _gccPath = new sys::Path(path);
128 }
129
130 void LTOCodeGenerator::addMustPreserveSymbol(const char* sym)
131 {
132     _mustPreserveSymbols[sym] = 1;
133 }
134
135
136 bool LTOCodeGenerator::writeMergedModules(const char* path, std::string& errMsg)
137 {
138     if ( this->determineTarget(errMsg) ) 
139         return true;
140
141     // mark which symbols can not be internalized 
142     this->applyScopeRestrictions();
143
144     // create output file
145     std::ofstream out(path, std::ios_base::out|std::ios::trunc|std::ios::binary);
146     if ( out.fail() ) {
147         errMsg = "could not open bitcode file for writing: ";
148         errMsg += path;
149         return true;
150     }
151     
152     // write bitcode to it
153     WriteBitcodeToFile(_linker.getModule(), out);
154     if ( out.fail() ) {
155         errMsg = "could not write bitcode file: ";
156         errMsg += path;
157         return true;
158     }
159     
160     return false;
161 }
162
163
164 const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg)
165 {
166     // make unique temp .s file to put generated assembly code
167     sys::Path uniqueAsmPath("lto-llvm.s");
168     if ( uniqueAsmPath.createTemporaryFileOnDisk(true, &errMsg) )
169         return NULL;
170     sys::RemoveFileOnSignal(uniqueAsmPath);
171        
172     // generate assembly code
173     bool genResult = false;
174     {
175       raw_fd_ostream asmFile(uniqueAsmPath.c_str(), false, errMsg);
176       if (!errMsg.empty())
177         return NULL;
178       genResult = this->generateAssemblyCode(asmFile, errMsg);
179     }
180     if ( genResult ) {
181         if ( uniqueAsmPath.exists() )
182             uniqueAsmPath.eraseFromDisk();
183         return NULL;
184     }
185     
186     // make unique temp .o file to put generated object file
187     sys::PathWithStatus uniqueObjPath("lto-llvm.o");
188     if ( uniqueObjPath.createTemporaryFileOnDisk(true, &errMsg) ) {
189         if ( uniqueAsmPath.exists() )
190             uniqueAsmPath.eraseFromDisk();
191         return NULL;
192     }
193     sys::RemoveFileOnSignal(uniqueObjPath);
194
195     // assemble the assembly code
196     const std::string& uniqueObjStr = uniqueObjPath.toString();
197     bool asmResult = this->assemble(uniqueAsmPath.toString(), 
198                                                         uniqueObjStr, errMsg);
199     if ( !asmResult ) {
200         // remove old buffer if compile() called twice
201         delete _nativeObjectFile;
202         
203         // read .o file into memory buffer
204         _nativeObjectFile = MemoryBuffer::getFile(uniqueObjStr.c_str(),&errMsg);
205     }
206
207     // remove temp files
208     uniqueAsmPath.eraseFromDisk();
209     uniqueObjPath.eraseFromDisk();
210
211     // return buffer, unless error
212     if ( _nativeObjectFile == NULL )
213         return NULL;
214     *length = _nativeObjectFile->getBufferSize();
215     return _nativeObjectFile->getBufferStart();
216 }
217
218
219 bool LTOCodeGenerator::assemble(const std::string& asmPath, 
220                                 const std::string& objPath, std::string& errMsg)
221 {
222     sys::Path gcc;
223     if ( _gccPath ) {
224         gcc = *_gccPath;
225     } else {
226         // find compiler driver
227         gcc = sys::Program::FindProgramByName("gcc");
228         if ( gcc.isEmpty() ) {
229             errMsg = "can't locate gcc";
230             return true;
231         }
232     }
233
234     // build argument list
235     std::vector<const char*> args;
236     std::string targetTriple = _linker.getModule()->getTargetTriple();
237     args.push_back(gcc.c_str());
238     if ( targetTriple.find("darwin") != targetTriple.size() ) {
239         if (strncmp(targetTriple.c_str(), "i386-apple-", 11) == 0) {
240             args.push_back("-arch");
241             args.push_back("i386");
242         }
243         else if (strncmp(targetTriple.c_str(), "x86_64-apple-", 13) == 0) {
244             args.push_back("-arch");
245             args.push_back("x86_64");
246         }
247         else if (strncmp(targetTriple.c_str(), "powerpc-apple-", 14) == 0) {
248             args.push_back("-arch");
249             args.push_back("ppc");
250         }
251         else if (strncmp(targetTriple.c_str(), "powerpc64-apple-", 16) == 0) {
252             args.push_back("-arch");
253             args.push_back("ppc64");
254         }
255         else if (strncmp(targetTriple.c_str(), "arm-apple-", 10) == 0) {
256             args.push_back("-arch");
257             args.push_back("arm");
258         }
259         else if ((strncmp(targetTriple.c_str(), "armv4t-apple-", 13) == 0) ||
260                  (strncmp(targetTriple.c_str(), "thumbv4t-apple-", 15) == 0)) {
261             args.push_back("-arch");
262             args.push_back("armv4t");
263         }
264         else if ((strncmp(targetTriple.c_str(), "armv5-apple-", 12) == 0) ||
265                  (strncmp(targetTriple.c_str(), "armv5e-apple-", 13) == 0) ||
266                  (strncmp(targetTriple.c_str(), "thumbv5-apple-", 14) == 0) ||
267                  (strncmp(targetTriple.c_str(), "thumbv5e-apple-", 15) == 0)) {
268             args.push_back("-arch");
269             args.push_back("armv5");
270         }
271         else if ((strncmp(targetTriple.c_str(), "armv6-apple-", 12) == 0) ||
272                  (strncmp(targetTriple.c_str(), "thumbv6-apple-", 14) == 0)) {
273             args.push_back("-arch");
274             args.push_back("armv6");
275         }
276     }
277     args.push_back("-c");
278     args.push_back("-x");
279     args.push_back("assembler");
280     args.push_back("-o");
281     args.push_back(objPath.c_str());
282     args.push_back(asmPath.c_str());
283     args.push_back(0);
284
285     // invoke assembler
286     if ( sys::Program::ExecuteAndWait(gcc, &args[0], 0, 0, 0, 0, &errMsg) ) {
287         errMsg = "error in assembly";    
288         return true;
289     }
290     return false; // success
291 }
292
293
294
295 bool LTOCodeGenerator::determineTarget(std::string& errMsg)
296 {
297     if ( _target == NULL ) {
298         // create target machine from info for merged modules
299         Module* mergedModule = _linker.getModule();
300         const TargetMachineRegistry::entry* march = 
301           TargetMachineRegistry::getClosestStaticTargetForModule(
302                                                        *mergedModule, errMsg);
303         if ( march == NULL )
304             return true;
305
306         // construct LTModule, hand over ownership of module and target
307         std::string FeatureStr =
308           getFeatureString(_linker.getModule()->getTargetTriple().c_str());
309         _target = march->CtorFn(*mergedModule, FeatureStr.c_str());
310     }
311     return false;
312 }
313
314 void LTOCodeGenerator::applyScopeRestrictions()
315 {
316     if ( !_scopeRestrictionsDone ) {
317         Module* mergedModule = _linker.getModule();
318
319         // Start off with a verification pass.
320         PassManager passes;
321         passes.add(createVerifierPass());
322
323         // mark which symbols can not be internalized 
324         if ( !_mustPreserveSymbols.empty() ) {
325             Mangler mangler(*mergedModule, 
326                                 _target->getTargetAsmInfo()->getGlobalPrefix());
327             std::vector<const char*> mustPreserveList;
328             for (Module::iterator f = mergedModule->begin(), 
329                                         e = mergedModule->end(); f != e; ++f) {
330                 if ( !f->isDeclaration() 
331                   && _mustPreserveSymbols.count(mangler.getValueName(f)) )
332                     mustPreserveList.push_back(::strdup(f->getName().c_str()));
333             }
334             for (Module::global_iterator v = mergedModule->global_begin(), 
335                                  e = mergedModule->global_end(); v !=  e; ++v) {
336                 if ( !v->isDeclaration()
337                   && _mustPreserveSymbols.count(mangler.getValueName(v)) )
338                     mustPreserveList.push_back(::strdup(v->getName().c_str()));
339             }
340             passes.add(createInternalizePass(mustPreserveList));
341         }
342         // apply scope restrictions
343         passes.run(*mergedModule);
344         
345         _scopeRestrictionsDone = true;
346     }
347 }
348
349 /// Optimize merged modules using various IPO passes
350 bool LTOCodeGenerator::generateAssemblyCode(raw_ostream& out,
351                                             std::string& errMsg)
352 {
353     if (  this->determineTarget(errMsg) ) 
354         return true;
355
356     // mark which symbols can not be internalized 
357     this->applyScopeRestrictions();
358
359     Module* mergedModule = _linker.getModule();
360
361      // If target supports exception handling then enable it now.
362     if ( _target->getTargetAsmInfo()->doesSupportExceptionHandling() )
363         llvm::ExceptionHandling = true;
364
365     // set codegen model
366     switch( _codeModel ) {
367         case LTO_CODEGEN_PIC_MODEL_STATIC:
368             _target->setRelocationModel(Reloc::Static);
369             break;
370         case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
371             _target->setRelocationModel(Reloc::PIC_);
372             break;
373         case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
374             _target->setRelocationModel(Reloc::DynamicNoPIC);
375             break;
376     }
377
378     // if options were requested, set them
379     if ( !_codegenOptions.empty() )
380         cl::ParseCommandLineOptions(_codegenOptions.size(), 
381                                                 (char**)&_codegenOptions[0]);
382
383     // Instantiate the pass manager to organize the passes.
384     PassManager passes;
385
386     // Start off with a verification pass.
387     passes.add(createVerifierPass());
388
389     // Add an appropriate TargetData instance for this module...
390     passes.add(new TargetData(*_target->getTargetData()));
391     
392     // Propagate constants at call sites into the functions they call.  This
393     // opens opportunities for globalopt (and inlining) by substituting function
394     // pointers passed as arguments to direct uses of functions.  
395     passes.add(createIPSCCPPass());
396
397     // Now that we internalized some globals, see if we can hack on them!
398     passes.add(createGlobalOptimizerPass());
399
400     // Linking modules together can lead to duplicated global constants, only
401     // keep one copy of each constant...
402     passes.add(createConstantMergePass());
403
404     // Remove unused arguments from functions...
405     passes.add(createDeadArgEliminationPass());
406
407     // Reduce the code after globalopt and ipsccp.  Both can open up significant
408     // simplification opportunities, and both can propagate functions through
409     // function pointers.  When this happens, we often have to resolve varargs
410     // calls, etc, so let instcombine do this.
411     passes.add(createInstructionCombiningPass());
412     if (!DisableInline)
413         passes.add(createFunctionInliningPass()); // Inline small functions
414     passes.add(createPruneEHPass());              // Remove dead EH info
415     passes.add(createGlobalDCEPass());            // Remove dead functions
416
417     // If we didn't decide to inline a function, check to see if we can
418     // transform it to pass arguments by value instead of by reference.
419     passes.add(createArgumentPromotionPass());
420
421     // The IPO passes may leave cruft around.  Clean up after them.
422     passes.add(createInstructionCombiningPass());
423     passes.add(createJumpThreadingPass());        // Thread jumps.
424     passes.add(createScalarReplAggregatesPass()); // Break up allocas
425
426     // Run a few AA driven optimizations here and now, to cleanup the code.
427     passes.add(createFunctionAttrsPass());        // Add nocapture
428     passes.add(createGlobalsModRefPass());        // IP alias analysis
429     passes.add(createLICMPass());                 // Hoist loop invariants
430     passes.add(createGVNPass());                  // Remove common subexprs
431     passes.add(createMemCpyOptPass());            // Remove dead memcpy's
432     passes.add(createDeadStoreEliminationPass()); // Nuke dead stores
433
434     // Cleanup and simplify the code after the scalar optimizations.
435     passes.add(createInstructionCombiningPass());
436     passes.add(createJumpThreadingPass());        // Thread jumps.
437     passes.add(createPromoteMemoryToRegisterPass()); // Cleanup after threading.
438
439
440     // Delete basic blocks, which optimization passes may have killed...
441     passes.add(createCFGSimplificationPass());
442
443     // Now that we have optimized the program, discard unreachable functions...
444     passes.add(createGlobalDCEPass());
445
446     // Make sure everything is still good.
447     passes.add(createVerifierPass());
448
449     FunctionPassManager* codeGenPasses =
450             new FunctionPassManager(new ExistingModuleProvider(mergedModule));
451
452     codeGenPasses->add(new TargetData(*_target->getTargetData()));
453
454     MachineCodeEmitter* mce = NULL;
455
456     switch (_target->addPassesToEmitFile(*codeGenPasses, out,
457                                          TargetMachine::AssemblyFile,
458                                          CodeGenOpt::Aggressive)) {
459         case FileModel::MachOFile:
460             mce = AddMachOWriter(*codeGenPasses, out, *_target);
461             break;
462         case FileModel::ElfFile:
463             mce = AddELFWriter(*codeGenPasses, out, *_target);
464             break;
465         case FileModel::AsmFile:
466             break;
467         case FileModel::Error:
468         case FileModel::None:
469             errMsg = "target file type not supported";
470             return true;
471     }
472
473     if (_target->addPassesToEmitFileFinish(*codeGenPasses, mce,
474                                            CodeGenOpt::Aggressive)) {
475         errMsg = "target does not support generation of this file type";
476         return true;
477     }
478
479     // Run our queue of passes all at once now, efficiently.
480     passes.run(*mergedModule);
481
482     // Run the code generator, and write assembly file
483     codeGenPasses->doInitialization();
484
485     for (Module::iterator
486            it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it)
487       if (!it->isDeclaration())
488         codeGenPasses->run(*it);
489
490     codeGenPasses->doFinalization();
491     return false; // success
492 }
493
494
495 /// Optimize merged modules using various IPO passes
496 void LTOCodeGenerator::setCodeGenDebugOptions(const char* options)
497 {
498     std::string ops(options);
499     for (std::string o = getToken(ops); !o.empty(); o = getToken(ops)) {
500         // ParseCommandLineOptions() expects argv[0] to be program name.
501         // Lazily add that.
502         if ( _codegenOptions.empty() ) 
503             _codegenOptions.push_back("libLTO");
504         _codegenOptions.push_back(strdup(o.c_str()));
505     }
506 }