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