Move MemCpyOpt after GVN.
[oota-llvm.git] / tools / lto2 / 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/SystemUtils.h"
27 #include "llvm/Support/Mangler.h"
28 #include "llvm/Support/MemoryBuffer.h"
29 #include "llvm/System/Signals.h"
30 #include "llvm/Analysis/Passes.h"
31 #include "llvm/Analysis/LoopPass.h"
32 #include "llvm/Analysis/Verifier.h"
33 #include "llvm/Analysis/LoadValueNumbering.h"
34 #include "llvm/CodeGen/FileWriters.h"
35 #include "llvm/Target/TargetOptions.h"
36 #include "llvm/Target/TargetData.h"
37 #include "llvm/Target/TargetMachine.h"
38 #include "llvm/Target/TargetMachineRegistry.h"
39 #include "llvm/Target/TargetAsmInfo.h"
40 #include "llvm/Transforms/IPO.h"
41 #include "llvm/Transforms/Scalar.h"
42 #include "llvm/Config/config.h"
43
44
45 #include <fstream>
46 #include <unistd.h>
47 #include <stdlib.h>
48 #include <fcntl.h>
49
50
51 using namespace llvm;
52
53
54
55 const char* LTOCodeGenerator::getVersionString()
56 {
57 #ifdef LLVM_VERSION_INFO
58     return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
59 #else
60     return PACKAGE_NAME " version " PACKAGE_VERSION;
61 #endif
62 }
63
64
65 LTOCodeGenerator::LTOCodeGenerator() 
66     : _linker("LinkTimeOptimizer", "ld-temp.o"), _target(NULL),
67       _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false),
68       _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
69       _nativeObjectFile(NULL)
70 {
71
72 }
73
74 LTOCodeGenerator::~LTOCodeGenerator()
75 {
76     delete _target;
77     delete _nativeObjectFile;
78 }
79
80
81
82 bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg)
83 {
84     return _linker.LinkInModule(mod->getLLVVMModule(), &errMsg);
85 }
86     
87
88 bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug, std::string& errMsg)
89 {
90     switch (debug) {
91         case LTO_DEBUG_MODEL_NONE:
92             _emitDwarfDebugInfo = false;
93             return false;
94             
95         case LTO_DEBUG_MODEL_DWARF:
96             _emitDwarfDebugInfo = true;
97             return false;
98     }
99     errMsg = "unknown debug format";
100     return true;
101 }
102
103
104 bool LTOCodeGenerator::setCodePICModel(lto_codegen_model model, 
105                                                         std::string& errMsg)
106 {
107     switch (model) {
108         case LTO_CODEGEN_PIC_MODEL_STATIC:
109         case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
110         case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
111             _codeModel = model;
112             return false;
113     }
114     errMsg = "unknown pic model";
115     return true;
116 }
117
118
119 void LTOCodeGenerator::addMustPreserveSymbol(const char* sym)
120 {
121     _mustPreserveSymbols[sym] = 1;
122 }
123
124
125 bool LTOCodeGenerator::writeMergedModules(const char* path, std::string& errMsg)
126 {
127     if ( this->determineTarget(errMsg) ) 
128         return true;
129
130     // mark which symbols can not be internalized 
131     this->applyScopeRestrictions();
132
133     // create output file
134     std::ofstream out(path, std::ios_base::out|std::ios::trunc|std::ios::binary);
135     if ( out.fail() ) {
136         errMsg = "could not open bitcode file for writing: ";
137         errMsg += path;
138         return true;
139     }
140     
141     // write bitcode to it
142     WriteBitcodeToFile(_linker.getModule(), out);
143     if ( out.fail() ) {
144         errMsg = "could not write bitcode file: ";
145         errMsg += path;
146         return true;
147     }
148     
149     return false;
150 }
151
152
153 const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg)
154 {
155     // make unique temp .s file to put generated assembly code
156     sys::Path uniqueAsmPath("lto-llvm.s");
157     if ( uniqueAsmPath.createTemporaryFileOnDisk(true, &errMsg) )
158         return NULL;
159     sys::RemoveFileOnSignal(uniqueAsmPath);
160        
161     // generate assembly code
162     std::ofstream asmFile(uniqueAsmPath.c_str());
163     bool genResult = this->generateAssemblyCode(asmFile, errMsg);
164     asmFile.close();
165     if ( genResult ) {
166         if ( uniqueAsmPath.exists() )
167             uniqueAsmPath.eraseFromDisk();
168         return NULL;
169     }
170     
171     // make unique temp .o file to put generated object file
172     sys::PathWithStatus uniqueObjPath("lto-llvm.o");
173     if ( uniqueObjPath.createTemporaryFileOnDisk(true, &errMsg) ) {
174         if ( uniqueAsmPath.exists() )
175             uniqueAsmPath.eraseFromDisk();
176         return NULL;
177     }
178     sys::RemoveFileOnSignal(uniqueObjPath);
179
180     // assemble the assembly code
181     const std::string& uniqueObjStr = uniqueObjPath.toString();
182     bool asmResult = this->assemble(uniqueAsmPath.toString(), 
183                                                         uniqueObjStr, errMsg);
184     if ( !asmResult ) {
185         // remove old buffer if compile() called twice
186         delete _nativeObjectFile;
187         
188         // read .o file into memory buffer
189         _nativeObjectFile = MemoryBuffer::getFile(uniqueObjStr.c_str(),&errMsg);
190     }
191
192     // remove temp files
193     uniqueAsmPath.eraseFromDisk();
194     uniqueObjPath.eraseFromDisk();
195
196     // return buffer, unless error
197     if ( _nativeObjectFile == NULL )
198         return NULL;
199     *length = _nativeObjectFile->getBufferSize();
200     return _nativeObjectFile->getBufferStart();
201 }
202
203
204 bool LTOCodeGenerator::assemble(const std::string& asmPath, 
205                                 const std::string& objPath, std::string& errMsg)
206 {
207     // find compiler driver
208     const sys::Path gcc = sys::Program::FindProgramByName("gcc");
209     if ( gcc.isEmpty() ) {
210         errMsg = "can't locate gcc";
211         return true;
212     }
213
214     // build argument list
215     std::vector<const char*> args;
216     std::string targetTriple = _linker.getModule()->getTargetTriple();
217     args.push_back(gcc.c_str());
218     if ( targetTriple.find("darwin") != targetTriple.size() ) {
219         if (strncmp(targetTriple.c_str(), "i686-apple-", 11) == 0) {
220             args.push_back("-arch");
221             args.push_back("i386");
222         }
223         else if (strncmp(targetTriple.c_str(), "x86_64-apple-", 13) == 0) {
224             args.push_back("-arch");
225             args.push_back("x86_64");
226         }
227         else if (strncmp(targetTriple.c_str(), "powerpc-apple-", 14) == 0) {
228             args.push_back("-arch");
229             args.push_back("ppc");
230         }
231         else if (strncmp(targetTriple.c_str(), "powerpc64-apple-", 16) == 0) {
232             args.push_back("-arch");
233             args.push_back("ppc64");
234         }
235     }
236     args.push_back("-c");
237     args.push_back("-x");
238     args.push_back("assembler");
239     args.push_back("-o");
240     args.push_back(objPath.c_str());
241     args.push_back(asmPath.c_str());
242     args.push_back(0);
243
244     // invoke assembler
245     if ( sys::Program::ExecuteAndWait(gcc, &args[0], 0, 0, 0, 0, &errMsg) ) {
246         errMsg = "error in assembly";    
247         return true;
248     }
249     return false; // success
250 }
251
252
253
254 bool LTOCodeGenerator::determineTarget(std::string& errMsg)
255 {
256     if ( _target == NULL ) {
257         // create target machine from info for merged modules
258         Module* mergedModule = _linker.getModule();
259         const TargetMachineRegistry::entry* march = 
260           TargetMachineRegistry::getClosestStaticTargetForModule(
261                                                        *mergedModule, errMsg);
262         if ( march == NULL )
263             return true;
264         std::string features;
265         _target = march->CtorFn(*mergedModule, features);
266     }
267     return false;
268 }
269
270 void LTOCodeGenerator::applyScopeRestrictions()
271 {
272     if ( !_scopeRestrictionsDone ) {
273         Module* mergedModule = _linker.getModule();
274
275         // Start off with a verification pass.
276         PassManager passes;
277         passes.add(createVerifierPass());
278
279         // mark which symbols can not be internalized 
280         if ( !_mustPreserveSymbols.empty() ) {
281             Mangler mangler(*mergedModule, 
282                                 _target->getTargetAsmInfo()->getGlobalPrefix());
283             std::vector<const char*> mustPreserveList;
284             for (Module::iterator f = mergedModule->begin(), 
285                                         e = mergedModule->end(); f != e; ++f) {
286                 if ( !f->isDeclaration() 
287                   && _mustPreserveSymbols.count(mangler.getValueName(f)) )
288                     mustPreserveList.push_back(::strdup(f->getName().c_str()));
289             }
290             for (Module::global_iterator v = mergedModule->global_begin(), 
291                                  e = mergedModule->global_end(); v !=  e; ++v) {
292                 if ( !v->isDeclaration()
293                   && _mustPreserveSymbols.count(mangler.getValueName(v)) )
294                     mustPreserveList.push_back(::strdup(v->getName().c_str()));
295             }
296             passes.add(createInternalizePass(mustPreserveList));
297         }
298         // apply scope restrictions
299         passes.run(*mergedModule);
300         
301         _scopeRestrictionsDone = true;
302     }
303 }
304
305
306 /// Optimize merged modules using various IPO passes
307 bool LTOCodeGenerator::generateAssemblyCode(std::ostream& out, std::string& errMsg)
308 {
309     if (  this->determineTarget(errMsg) ) 
310         return true;
311
312     // mark which symbols can not be internalized 
313     this->applyScopeRestrictions();
314
315     Module* mergedModule = _linker.getModule();
316
317      // If target supports exception handling then enable it now.
318     if ( _target->getTargetAsmInfo()->doesSupportExceptionHandling() )
319         llvm::ExceptionHandling = true;
320
321     // set codegen model
322     switch( _codeModel ) {
323         case LTO_CODEGEN_PIC_MODEL_STATIC:
324             _target->setRelocationModel(Reloc::Static);
325             break;
326         case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
327             _target->setRelocationModel(Reloc::PIC_);
328             break;
329         case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
330             _target->setRelocationModel(Reloc::DynamicNoPIC);
331             break;
332     }
333
334     // Instantiate the pass manager to organize the passes.
335     PassManager passes;
336
337     // Start off with a verification pass.
338     passes.add(createVerifierPass());
339
340     // Add an appropriate TargetData instance for this module...
341     passes.add(new TargetData(*_target->getTargetData()));
342     
343     // Now that we internalized some globals, see if we can hack on them!
344     passes.add(createGlobalOptimizerPass());
345
346     // Linking modules together can lead to duplicated global constants, only
347     // keep one copy of each constant...
348     passes.add(createConstantMergePass());
349
350     // If the -s command line option was specified, strip the symbols out of the
351     // resulting program to make it smaller.  -s is a GLD option that we are
352     // supporting.
353     passes.add(createStripSymbolsPass());
354     
355     // Propagate constants at call sites into the functions they call.
356     passes.add(createIPConstantPropagationPass());
357
358     // Remove unused arguments from functions...
359     passes.add(createDeadArgEliminationPass());
360
361     passes.add(createFunctionInliningPass()); // Inline small functions
362
363     passes.add(createPruneEHPass());            // Remove dead EH info
364
365     passes.add(createGlobalDCEPass());          // Remove dead functions
366
367     // If we didn't decide to inline a function, check to see if we can
368     // transform it to pass arguments by value instead of by reference.
369     passes.add(createArgumentPromotionPass());
370
371     // The IPO passes may leave cruft around.  Clean up after them.
372     passes.add(createInstructionCombiningPass());
373     passes.add(createJumpThreadingPass());        // Thread jumps.
374     passes.add(createScalarReplAggregatesPass()); // Break up allocas
375
376     // Run a few AA driven optimizations here and now, to cleanup the code.
377     passes.add(createGlobalsModRefPass());      // IP alias analysis
378
379     passes.add(createLICMPass());               // Hoist loop invariants
380     passes.add(createGVNPass());               // Remove common subexprs
381     passes.add(createMemCpyOptPass());  // Remove dead memcpy's
382     passes.add(createDeadStoreEliminationPass()); // Nuke dead stores
383
384     // Cleanup and simplify the code after the scalar optimizations.
385     passes.add(createInstructionCombiningPass());
386
387     passes.add(createJumpThreadingPass());        // Thread jumps.
388
389     // Delete basic blocks, which optimization passes may have killed...
390     passes.add(createCFGSimplificationPass());
391
392     // Now that we have optimized the program, discard unreachable functions...
393     passes.add(createGlobalDCEPass());
394
395     // Make sure everything is still good.
396     passes.add(createVerifierPass());
397
398     FunctionPassManager* codeGenPasses =
399             new FunctionPassManager(new ExistingModuleProvider(mergedModule));
400
401     codeGenPasses->add(new TargetData(*_target->getTargetData()));
402
403     MachineCodeEmitter* mce = NULL;
404
405     switch (_target->addPassesToEmitFile(*codeGenPasses, out,
406                                       TargetMachine::AssemblyFile, true)) {
407         case FileModel::MachOFile:
408             mce = AddMachOWriter(*codeGenPasses, out, *_target);
409             break;
410         case FileModel::ElfFile:
411             mce = AddELFWriter(*codeGenPasses, out, *_target);
412             break;
413         case FileModel::AsmFile:
414             break;
415         case FileModel::Error:
416         case FileModel::None:
417             errMsg = "target file type not supported";
418             return true;
419     }
420
421     if (_target->addPassesToEmitFileFinish(*codeGenPasses, mce, true)) {
422         errMsg = "target does not support generation of this file type";
423         return true;
424     }
425
426     // Run our queue of passes all at once now, efficiently.
427     passes.run(*mergedModule);
428
429     // Run the code generator, and write assembly file
430     codeGenPasses->doInitialization();
431     for (Module::iterator it = mergedModule->begin(),
432                 e = mergedModule->end(); it != e; ++it) {
433         if (!it->isDeclaration())
434             codeGenPasses->run(*it);
435     }
436     codeGenPasses->doFinalization();
437
438     return false; // success
439 }
440
441
442