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