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