Reinstate -O3 for LTO.
[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 "LTOCodeGenerator.h"
16 #include "LTOModule.h"
17 #include "llvm/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Linker.h"
20 #include "llvm/LLVMContext.h"
21 #include "llvm/Module.h"
22 #include "llvm/PassManager.h"
23 #include "llvm/Analysis/Passes.h"
24 #include "llvm/Analysis/Verifier.h"
25 #include "llvm/Bitcode/ReaderWriter.h"
26 #include "llvm/Config/config.h"
27 #include "llvm/MC/MCAsmInfo.h"
28 #include "llvm/MC/MCContext.h"
29 #include "llvm/MC/SubtargetFeature.h"
30 #include "llvm/Target/Mangler.h"
31 #include "llvm/Target/TargetOptions.h"
32 #include "llvm/Target/TargetData.h"
33 #include "llvm/Target/TargetMachine.h"
34 #include "llvm/Target/TargetRegisterInfo.h"
35 #include "llvm/Transforms/IPO.h"
36 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
37 #include "llvm/Support/CommandLine.h"
38 #include "llvm/Support/FormattedStream.h"
39 #include "llvm/Support/MemoryBuffer.h"
40 #include "llvm/Support/ToolOutputFile.h"
41 #include "llvm/Support/Host.h"
42 #include "llvm/Support/Signals.h"
43 #include "llvm/Support/TargetRegistry.h"
44 #include "llvm/Support/TargetSelect.h"
45 #include "llvm/Support/system_error.h"
46 #include "llvm/ADT/StringExtras.h"
47 using namespace llvm;
48
49 static cl::opt<bool> DisableInline("disable-inlining", cl::init(false),
50   cl::desc("Do not run the inliner pass"));
51
52 static cl::opt<bool> DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
53   cl::desc("Do not run the GVN load PRE pass"));
54
55 const char* LTOCodeGenerator::getVersionString() {
56 #ifdef LLVM_VERSION_INFO
57   return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
58 #else
59   return PACKAGE_NAME " version " PACKAGE_VERSION;
60 #endif
61 }
62
63 LTOCodeGenerator::LTOCodeGenerator()
64   : _context(getGlobalContext()),
65     _linker("LinkTimeOptimizer", "ld-temp.o", _context), _target(NULL),
66     _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false),
67     _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
68     _nativeObjectFile(NULL) {
69   InitializeAllTargets();
70   InitializeAllTargetMCs();
71   InitializeAllAsmPrinters();
72 }
73
74 LTOCodeGenerator::~LTOCodeGenerator() {
75   delete _target;
76   delete _nativeObjectFile;
77
78   for (std::vector<char*>::iterator I = _codegenOptions.begin(),
79          E = _codegenOptions.end(); I != E; ++I)
80     free(*I);
81 }
82
83 bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) {
84   bool ret = _linker.LinkInModule(mod->getLLVVMModule(), &errMsg);
85
86   const std::vector<const char*> &undefs = mod->getAsmUndefinedRefs();
87   for (int i = 0, e = undefs.size(); i != e; ++i)
88     _asmUndefinedRefs[undefs[i]] = 1;
89
90   return ret;
91 }
92
93 bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug,
94                                     std::string& errMsg) {
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   llvm_unreachable("Unknown debug format!");
105 }
106
107 bool LTOCodeGenerator::setCodePICModel(lto_codegen_model model,
108                                        std::string& errMsg) {
109   switch (model) {
110   case LTO_CODEGEN_PIC_MODEL_STATIC:
111   case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
112   case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
113     _codeModel = model;
114     return false;
115   }
116   llvm_unreachable("Unknown PIC model!");
117 }
118
119 bool LTOCodeGenerator::writeMergedModules(const char *path,
120                                           std::string &errMsg) {
121   if (determineTarget(errMsg))
122     return true;
123
124   // mark which symbols can not be internalized
125   applyScopeRestrictions();
126
127   // create output file
128   std::string ErrInfo;
129   tool_output_file Out(path, ErrInfo,
130                        raw_fd_ostream::F_Binary);
131   if (!ErrInfo.empty()) {
132     errMsg = "could not open bitcode file for writing: ";
133     errMsg += path;
134     return true;
135   }
136
137   // write bitcode to it
138   WriteBitcodeToFile(_linker.getModule(), Out.os());
139   Out.os().close();
140
141   if (Out.os().has_error()) {
142     errMsg = "could not write bitcode file: ";
143     errMsg += path;
144     Out.os().clear_error();
145     return true;
146   }
147
148   Out.keep();
149   return false;
150 }
151
152 bool LTOCodeGenerator::compile_to_file(const char** name, std::string& errMsg) {
153   // make unique temp .o file to put generated object file
154   sys::PathWithStatus uniqueObjPath("lto-llvm.o");
155   if ( uniqueObjPath.createTemporaryFileOnDisk(false, &errMsg) ) {
156     uniqueObjPath.eraseFromDisk();
157     return true;
158   }
159   sys::RemoveFileOnSignal(uniqueObjPath);
160
161   // generate object file
162   bool genResult = false;
163   tool_output_file objFile(uniqueObjPath.c_str(), errMsg);
164   if (!errMsg.empty())
165     return true;
166
167   genResult = this->generateObjectFile(objFile.os(), errMsg);
168   objFile.os().close();
169   if (objFile.os().has_error()) {
170     objFile.os().clear_error();
171     return true;
172   }
173
174   objFile.keep();
175   if ( genResult ) {
176     uniqueObjPath.eraseFromDisk();
177     return true;
178   }
179
180   _nativeObjectPath = uniqueObjPath.str();
181   *name = _nativeObjectPath.c_str();
182   return false;
183 }
184
185 const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg) {
186   const char *name;
187   if (compile_to_file(&name, errMsg))
188     return NULL;
189
190   // remove old buffer if compile() called twice
191   delete _nativeObjectFile;
192
193   // read .o file into memory buffer
194   OwningPtr<MemoryBuffer> BuffPtr;
195   if (error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) {
196     errMsg = ec.message();
197     return NULL;
198   }
199   _nativeObjectFile = BuffPtr.take();
200
201   // remove temp files
202   sys::Path(_nativeObjectPath).eraseFromDisk();
203
204   // return buffer, unless error
205   if ( _nativeObjectFile == NULL )
206     return NULL;
207   *length = _nativeObjectFile->getBufferSize();
208   return _nativeObjectFile->getBufferStart();
209 }
210
211 bool LTOCodeGenerator::determineTarget(std::string& errMsg) {
212   if ( _target == NULL ) {
213     std::string Triple = _linker.getModule()->getTargetTriple();
214     if (Triple.empty())
215       Triple = sys::getDefaultTargetTriple();
216
217     // create target machine from info for merged modules
218     const Target *march = TargetRegistry::lookupTarget(Triple, errMsg);
219     if ( march == NULL )
220       return true;
221
222     // The relocation model is actually a static member of TargetMachine and
223     // needs to be set before the TargetMachine is instantiated.
224     Reloc::Model RelocModel = Reloc::Default;
225     switch( _codeModel ) {
226     case LTO_CODEGEN_PIC_MODEL_STATIC:
227       RelocModel = Reloc::Static;
228       break;
229     case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
230       RelocModel = Reloc::PIC_;
231       break;
232     case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
233       RelocModel = Reloc::DynamicNoPIC;
234       break;
235     }
236
237     // construct LTOModule, hand over ownership of module and target
238     SubtargetFeatures Features;
239     Features.getDefaultSubtargetFeatures(llvm::Triple(Triple));
240     std::string FeatureStr = Features.getString();
241     TargetOptions Options;
242     _target = march->createTargetMachine(Triple, _mCpu, FeatureStr, Options,
243                                          RelocModel, CodeModel::Default,
244                                          CodeGenOpt::Aggressive);
245   }
246   return false;
247 }
248
249 void LTOCodeGenerator::
250 applyRestriction(GlobalValue &GV,
251                  std::vector<const char*> &mustPreserveList,
252                  SmallPtrSet<GlobalValue*, 8> &asmUsed,
253                  Mangler &mangler) {
254   SmallString<64> Buffer;
255   mangler.getNameWithPrefix(Buffer, &GV, false);
256
257   if (GV.isDeclaration())
258     return;
259   if (_mustPreserveSymbols.count(Buffer))
260     mustPreserveList.push_back(GV.getName().data());
261   if (_asmUndefinedRefs.count(Buffer))
262     asmUsed.insert(&GV);
263 }
264
265 static void findUsedValues(GlobalVariable *LLVMUsed,
266                            SmallPtrSet<GlobalValue*, 8> &UsedValues) {
267   if (LLVMUsed == 0) return;
268
269   ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer());
270   if (Inits == 0) return;
271
272   for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
273     if (GlobalValue *GV =
274         dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
275       UsedValues.insert(GV);
276 }
277
278 void LTOCodeGenerator::applyScopeRestrictions() {
279   if (_scopeRestrictionsDone) return;
280   Module *mergedModule = _linker.getModule();
281
282   // Start off with a verification pass.
283   PassManager passes;
284   passes.add(createVerifierPass());
285
286   // mark which symbols can not be internalized
287   MCContext Context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(),NULL);
288   Mangler mangler(Context, *_target->getTargetData());
289   std::vector<const char*> mustPreserveList;
290   SmallPtrSet<GlobalValue*, 8> asmUsed;
291
292   for (Module::iterator f = mergedModule->begin(),
293          e = mergedModule->end(); f != e; ++f)
294     applyRestriction(*f, mustPreserveList, asmUsed, mangler);
295   for (Module::global_iterator v = mergedModule->global_begin(),
296          e = mergedModule->global_end(); v !=  e; ++v)
297     applyRestriction(*v, mustPreserveList, asmUsed, mangler);
298   for (Module::alias_iterator a = mergedModule->alias_begin(),
299          e = mergedModule->alias_end(); a != e; ++a)
300     applyRestriction(*a, mustPreserveList, asmUsed, mangler);
301
302   GlobalVariable *LLVMCompilerUsed =
303     mergedModule->getGlobalVariable("llvm.compiler.used");
304   findUsedValues(LLVMCompilerUsed, asmUsed);
305   if (LLVMCompilerUsed)
306     LLVMCompilerUsed->eraseFromParent();
307
308   llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(_context);
309   std::vector<Constant*> asmUsed2;
310   for (SmallPtrSet<GlobalValue*, 16>::const_iterator i = asmUsed.begin(),
311          e = asmUsed.end(); i !=e; ++i) {
312     GlobalValue *GV = *i;
313     Constant *c = ConstantExpr::getBitCast(GV, i8PTy);
314     asmUsed2.push_back(c);
315   }
316
317   llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size());
318   LLVMCompilerUsed =
319     new llvm::GlobalVariable(*mergedModule, ATy, false,
320                              llvm::GlobalValue::AppendingLinkage,
321                              llvm::ConstantArray::get(ATy, asmUsed2),
322                              "llvm.compiler.used");
323
324   LLVMCompilerUsed->setSection("llvm.metadata");
325
326   passes.add(createInternalizePass(mustPreserveList));
327
328   // apply scope restrictions
329   passes.run(*mergedModule);
330
331   _scopeRestrictionsDone = true;
332 }
333
334 /// Optimize merged modules using various IPO passes
335 bool LTOCodeGenerator::generateObjectFile(raw_ostream &out,
336                                           std::string &errMsg) {
337   if ( this->determineTarget(errMsg) )
338     return true;
339
340   Module* mergedModule = _linker.getModule();
341
342   // if options were requested, set them
343   if ( !_codegenOptions.empty() )
344     cl::ParseCommandLineOptions(_codegenOptions.size(),
345                                 const_cast<char **>(&_codegenOptions[0]));
346
347   // mark which symbols can not be internalized
348   this->applyScopeRestrictions();
349
350   // Instantiate the pass manager to organize the passes.
351   PassManager passes;
352
353   // Start off with a verification pass.
354   passes.add(createVerifierPass());
355
356   // Add an appropriate TargetData instance for this module...
357   passes.add(new TargetData(*_target->getTargetData()));
358
359   // Enabling internalize here would use its AllButMain variant. It
360   // keeps only main if it exists and does nothing for libraries. Instead
361   // we create the pass ourselves with the symbol list provided by the linker.
362   PassManagerBuilder().populateLTOPassManager(passes, /*Internalize=*/false,
363                                               !DisableInline,
364                                               DisableGVNLoadPRE);
365
366   // Make sure everything is still good.
367   passes.add(createVerifierPass());
368
369   FunctionPassManager *codeGenPasses = new FunctionPassManager(mergedModule);
370
371   codeGenPasses->add(new TargetData(*_target->getTargetData()));
372
373   formatted_raw_ostream Out(out);
374
375   if (_target->addPassesToEmitFile(*codeGenPasses, Out,
376                                    TargetMachine::CGFT_ObjectFile)) {
377     errMsg = "target file type not supported";
378     return true;
379   }
380
381   // Run our queue of passes all at once now, efficiently.
382   passes.run(*mergedModule);
383
384   // Run the code generator, and write assembly file
385   codeGenPasses->doInitialization();
386
387   for (Module::iterator
388          it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it)
389     if (!it->isDeclaration())
390       codeGenPasses->run(*it);
391
392   codeGenPasses->doFinalization();
393   delete codeGenPasses;
394
395   return false; // success
396 }
397
398 /// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
399 /// LTO problems.
400 void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) {
401   for (std::pair<StringRef, StringRef> o = getToken(options);
402        !o.first.empty(); o = getToken(o.second)) {
403     // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add
404     // that.
405     if ( _codegenOptions.empty() )
406       _codegenOptions.push_back(strdup("libLTO"));
407     _codegenOptions.push_back(strdup(o.first.str().c_str()));
408   }
409 }