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