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