blob: 40983966a99fa05fb2b51c029e9072c35b9af962 [file] [log] [blame]
Nick Kledzik77595fc2008-02-26 20:26:43 +00001//===-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.
Bill Wendling76b13ed2012-03-31 10:50:14 +00007//
Nick Kledzik77595fc2008-02-26 20:26:43 +00008//===----------------------------------------------------------------------===//
9//
Bill Wendling76b13ed2012-03-31 10:50:14 +000010// This file implements the Link Time Optimization library. This library is
Nick Kledzik77595fc2008-02-26 20:26:43 +000011// intended to be used by linker to optimize code at link time.
12//
13//===----------------------------------------------------------------------===//
14
Nick Kledzikef194ed2008-02-27 22:25:36 +000015#include "LTOCodeGenerator.h"
Bill Wendlingab53bc72012-03-31 11:10:35 +000016#include "LTOModule.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000017#include "llvm/ADT/StringExtras.h"
Nick Lewycky8189d402009-06-17 06:52:10 +000018#include "llvm/Analysis/Passes.h"
Rafael Espindolac684e832011-08-02 21:50:27 +000019#include "llvm/Analysis/Verifier.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000020#include "llvm/Bitcode/ReaderWriter.h"
Bill Wendlingc94c5622012-03-31 11:15:43 +000021#include "llvm/Config/config.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000022#include "llvm/Constants.h"
23#include "llvm/DataLayout.h"
24#include "llvm/DerivedTypes.h"
25#include "llvm/LLVMContext.h"
26#include "llvm/Linker.h"
Chris Lattner5ef31a02010-03-12 18:44:54 +000027#include "llvm/MC/MCAsmInfo.h"
28#include "llvm/MC/MCContext.h"
Evan Chengab8be962011-06-29 01:14:12 +000029#include "llvm/MC/SubtargetFeature.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000030#include "llvm/Module.h"
31#include "llvm/PassManager.h"
Nick Kledzik920ae982008-07-08 21:14:10 +000032#include "llvm/Support/CommandLine.h"
David Greene71847812009-07-14 20:18:05 +000033#include "llvm/Support/FormattedStream.h"
Michael J. Spencer3cc52ea2010-11-29 18:47:54 +000034#include "llvm/Support/Host.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000035#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer3cc52ea2010-11-29 18:47:54 +000036#include "llvm/Support/Signals.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000037#include "llvm/Support/TargetRegistry.h"
38#include "llvm/Support/TargetSelect.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000039#include "llvm/Support/ToolOutputFile.h"
Michael J. Spencerf2f516f2010-12-09 18:06:07 +000040#include "llvm/Support/system_error.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000041#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"
Nick Kledzik77595fc2008-02-26 20:26:43 +000047using namespace llvm;
48
Bill Wendling9ac0aaa2012-08-06 21:34:54 +000049static cl::opt<bool>
50DisableInline("disable-inlining", cl::init(false),
Nick Kledzik920ae982008-07-08 21:14:10 +000051 cl::desc("Do not run the inliner pass"));
Nick Kledzik77595fc2008-02-26 20:26:43 +000052
Bill Wendling9ac0aaa2012-08-06 21:34:54 +000053static cl::opt<bool>
54DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
Bill Wendling3197b442012-04-02 22:16:50 +000055 cl::desc("Do not run the GVN load PRE pass"));
56
Bill Wendlingcaf71d42012-03-31 10:49:43 +000057const char* LTOCodeGenerator::getVersionString() {
Nick Kledzik77595fc2008-02-26 20:26:43 +000058#ifdef LLVM_VERSION_INFO
Bill Wendlingcaf71d42012-03-31 10:49:43 +000059 return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
Nick Kledzik77595fc2008-02-26 20:26:43 +000060#else
Bill Wendlingcaf71d42012-03-31 10:49:43 +000061 return PACKAGE_NAME " version " PACKAGE_VERSION;
Nick Kledzik77595fc2008-02-26 20:26:43 +000062#endif
63}
64
Bill Wendling76b13ed2012-03-31 10:50:14 +000065LTOCodeGenerator::LTOCodeGenerator()
Bill Wendlingc94c5622012-03-31 11:15:43 +000066 : _context(getGlobalContext()),
67 _linker("LinkTimeOptimizer", "ld-temp.o", _context), _target(NULL),
68 _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false),
Rafael Espindola4d2e9d92012-04-16 10:58:38 +000069 _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
Bill Wendlingc94c5622012-03-31 11:15:43 +000070 _nativeObjectFile(NULL) {
71 InitializeAllTargets();
72 InitializeAllTargetMCs();
73 InitializeAllAsmPrinters();
Nick Kledzik77595fc2008-02-26 20:26:43 +000074}
75
Bill Wendlingcaf71d42012-03-31 10:49:43 +000076LTOCodeGenerator::~LTOCodeGenerator() {
77 delete _target;
78 delete _nativeObjectFile;
79
Bill Wendlingf2cc2ee2012-03-31 10:51:45 +000080 for (std::vector<char*>::iterator I = _codegenOptions.begin(),
Bill Wendlingcaf71d42012-03-31 10:49:43 +000081 E = _codegenOptions.end(); I != E; ++I)
82 free(*I);
Nick Kledzik77595fc2008-02-26 20:26:43 +000083}
84
Bill Wendlingc94c5622012-03-31 11:15:43 +000085bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) {
Rafael Espindola38c4e532011-03-02 04:14:42 +000086 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;
Nick Kledzik77595fc2008-02-26 20:26:43 +000093}
Bill Wendling76b13ed2012-03-31 10:50:14 +000094
Bill Wendlingc94c5622012-03-31 11:15:43 +000095bool 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;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000101
Bill Wendlingc94c5622012-03-31 11:15:43 +0000102 case LTO_DEBUG_MODEL_DWARF:
103 _emitDwarfDebugInfo = true;
104 return false;
105 }
106 llvm_unreachable("Unknown debug format!");
Nick Kledzik77595fc2008-02-26 20:26:43 +0000107}
108
Bill Wendling76b13ed2012-03-31 10:50:14 +0000109bool LTOCodeGenerator::setCodePICModel(lto_codegen_model model,
Bill Wendlingc94c5622012-03-31 11:15:43 +0000110 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!");
Nick Kledzik77595fc2008-02-26 20:26:43 +0000119}
120
Chris Lattnerb515d752009-08-23 07:49:08 +0000121bool LTOCodeGenerator::writeMergedModules(const char *path,
122 std::string &errMsg) {
123 if (determineTarget(errMsg))
124 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000125
Bill Wendling76b13ed2012-03-31 10:50:14 +0000126 // mark which symbols can not be internalized
Chris Lattnerb515d752009-08-23 07:49:08 +0000127 applyScopeRestrictions();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000128
Chris Lattnerb515d752009-08-23 07:49:08 +0000129 // create output file
130 std::string ErrInfo;
Dan Gohmanf2914012010-08-20 16:59:15 +0000131 tool_output_file Out(path, ErrInfo,
132 raw_fd_ostream::F_Binary);
Chris Lattnerb515d752009-08-23 07:49:08 +0000133 if (!ErrInfo.empty()) {
134 errMsg = "could not open bitcode file for writing: ";
135 errMsg += path;
136 return true;
137 }
Bill Wendling76b13ed2012-03-31 10:50:14 +0000138
Chris Lattnerb515d752009-08-23 07:49:08 +0000139 // write bitcode to it
Dan Gohmand4c45432010-09-01 14:20:41 +0000140 WriteBitcodeToFile(_linker.getModule(), Out.os());
141 Out.os().close();
Dan Gohman4b7416b2010-05-27 20:19:47 +0000142
Dan Gohmand4c45432010-09-01 14:20:41 +0000143 if (Out.os().has_error()) {
Chris Lattnerb515d752009-08-23 07:49:08 +0000144 errMsg = "could not write bitcode file: ";
145 errMsg += path;
Dan Gohmand4c45432010-09-01 14:20:41 +0000146 Out.os().clear_error();
Chris Lattnerb515d752009-08-23 07:49:08 +0000147 return true;
148 }
Bill Wendling76b13ed2012-03-31 10:50:14 +0000149
Dan Gohmanf2914012010-08-20 16:59:15 +0000150 Out.keep();
Chris Lattnerb515d752009-08-23 07:49:08 +0000151 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000152}
153
Bill Wendlingc94c5622012-03-31 11:15:43 +0000154bool LTOCodeGenerator::compile_to_file(const char** name, std::string& errMsg) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000155 // make unique temp .o file to put generated object file
156 sys::PathWithStatus uniqueObjPath("lto-llvm.o");
Bill Wendling0ca36af2012-08-08 22:03:50 +0000157 if (uniqueObjPath.createTemporaryFileOnDisk(false, &errMsg)) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000158 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);
Bill Wendling0b95a992012-09-06 21:07:57 +0000166 if (!errMsg.empty()) {
167 uniqueObjPath.eraseFromDisk();
John Criswell3f0e2372011-08-18 01:19:05 +0000168 return true;
Bill Wendling0b95a992012-09-06 21:07:57 +0000169 }
Bill Wendlingc94c5622012-03-31 11:15:43 +0000170
Rafael Espindola6421a882011-03-22 20:57:13 +0000171 genResult = this->generateObjectFile(objFile.os(), errMsg);
172 objFile.os().close();
173 if (objFile.os().has_error()) {
174 objFile.os().clear_error();
Bill Wendling0b95a992012-09-06 21:07:57 +0000175 uniqueObjPath.eraseFromDisk();
Rafael Espindola6421a882011-03-22 20:57:13 +0000176 return true;
177 }
Bill Wendlingc94c5622012-03-31 11:15:43 +0000178
Rafael Espindola6421a882011-03-22 20:57:13 +0000179 objFile.keep();
Bill Wendling0ca36af2012-08-08 22:03:50 +0000180 if (genResult) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000181 uniqueObjPath.eraseFromDisk();
182 return true;
183 }
184
185 _nativeObjectPath = uniqueObjPath.str();
186 *name = _nativeObjectPath.c_str();
187 return false;
188}
189
Bill Wendlingc94c5622012-03-31 11:15:43 +0000190const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000191 const char *name;
192 if (compile_to_file(&name, errMsg))
193 return NULL;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000194
Rafael Espindola6421a882011-03-22 20:57:13 +0000195 // remove old buffer if compile() called twice
196 delete _nativeObjectFile;
Nick Kledzikef194ed2008-02-27 22:25:36 +0000197
Rafael Espindola6421a882011-03-22 20:57:13 +0000198 // 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();
Bill Wendling0b95a992012-09-06 21:07:57 +0000202 sys::Path(_nativeObjectPath).eraseFromDisk();
Rafael Espindola6421a882011-03-22 20:57:13 +0000203 return NULL;
204 }
205 _nativeObjectFile = BuffPtr.take();
Rafael Espindolae9efea12011-02-24 21:04:06 +0000206
Rafael Espindola6421a882011-03-22 20:57:13 +0000207 // remove temp files
208 sys::Path(_nativeObjectPath).eraseFromDisk();
Rafael Espindolae9efea12011-02-24 21:04:06 +0000209
Rafael Espindola6421a882011-03-22 20:57:13 +0000210 // return buffer, unless error
Bill Wendling0ca36af2012-08-08 22:03:50 +0000211 if (_nativeObjectFile == NULL)
Rafael Espindola6421a882011-03-22 20:57:13 +0000212 return NULL;
213 *length = _nativeObjectFile->getBufferSize();
214 return _nativeObjectFile->getBufferStart();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000215}
216
Bill Wendlingc94c5622012-03-31 11:15:43 +0000217bool LTOCodeGenerator::determineTarget(std::string& errMsg) {
Bill Wendling0ca36af2012-08-08 22:03:50 +0000218 if (_target != NULL)
219 return false;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000220
Bob Wilson47ed8a12012-10-12 17:39:25 +0000221 std::string TripleStr = _linker.getModule()->getTargetTriple();
222 if (TripleStr.empty())
223 TripleStr = sys::getDefaultTargetTriple();
224 llvm::Triple Triple(TripleStr);
Bill Wendling604a8182008-06-18 06:35:30 +0000225
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000226 // create target machine from info for merged modules
Bob Wilson47ed8a12012-10-12 17:39:25 +0000227 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
Bill Wendling0ca36af2012-08-08 22:03:50 +0000228 if (march == NULL)
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000229 return true;
Bill Wendlingc94c5622012-03-31 11:15:43 +0000230
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000231 // 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;
Bill Wendling0ca36af2012-08-08 22:03:50 +0000234 switch (_codeModel) {
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000235 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;
Bill Wendlingc94c5622012-03-31 11:15:43 +0000244 }
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000245
246 // construct LTOModule, hand over ownership of module and target
247 SubtargetFeatures Features;
Bob Wilson47ed8a12012-10-12 17:39:25 +0000248 Features.getDefaultSubtargetFeatures(Triple);
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000249 std::string FeatureStr = Features.getString();
Bob Wilson47ed8a12012-10-12 17:39:25 +0000250 // 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 }
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000257 TargetOptions Options;
258 LTOModule::getTargetOptions(Options);
Bob Wilson47ed8a12012-10-12 17:39:25 +0000259 _target = march->createTargetMachine(TripleStr, _mCpu, FeatureStr, Options,
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000260 RelocModel, CodeModel::Default,
261 CodeGenOpt::Aggressive);
Bill Wendlingc94c5622012-03-31 11:15:43 +0000262 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000263}
264
Bill Wendlingc94c5622012-03-31 11:15:43 +0000265void LTOCodeGenerator::
266applyRestriction(GlobalValue &GV,
267 std::vector<const char*> &mustPreserveList,
268 SmallPtrSet<GlobalValue*, 8> &asmUsed,
269 Mangler &mangler) {
Rafael Espindola38c4e532011-03-02 04:14:42 +0000270 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
281static 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)
Bill Wendling76b13ed2012-03-31 10:50:14 +0000289 if (GlobalValue *GV =
Bill Wendlingc94c5622012-03-31 11:15:43 +0000290 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
Rafael Espindola38c4e532011-03-02 04:14:42 +0000291 UsedValues.insert(GV);
292}
293
Chris Lattner5ef31a02010-03-12 18:44:54 +0000294void LTOCodeGenerator::applyScopeRestrictions() {
295 if (_scopeRestrictionsDone) return;
296 Module *mergedModule = _linker.getModule();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000297
Chris Lattner5ef31a02010-03-12 18:44:54 +0000298 // Start off with a verification pass.
299 PassManager passes;
300 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000301
Bill Wendling76b13ed2012-03-31 10:50:14 +0000302 // mark which symbols can not be internalized
Bill Wendlingc94c5622012-03-31 11:15:43 +0000303 MCContext Context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(),NULL);
Micah Villmow791cfc22012-10-08 16:39:34 +0000304 Mangler mangler(Context, *_target->getDataLayout());
Rafael Espindola38c4e532011-03-02 04:14:42 +0000305 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);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000311 for (Module::global_iterator v = mergedModule->global_begin(),
Rafael Espindola38c4e532011-03-02 04:14:42 +0000312 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
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000324 llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(_context);
Rafael Espindola38c4e532011-03-02 04:14:42 +0000325 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);
Chris Lattner5ef31a02010-03-12 18:44:54 +0000331 }
Rafael Espindola38c4e532011-03-02 04:14:42 +0000332
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
Chris Lattner5ef31a02010-03-12 18:44:54 +0000344 // apply scope restrictions
345 passes.run(*mergedModule);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000346
Chris Lattner5ef31a02010-03-12 18:44:54 +0000347 _scopeRestrictionsDone = true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000348}
349
Nick Kledzik77595fc2008-02-26 20:26:43 +0000350/// Optimize merged modules using various IPO passes
Chris Lattner817a01f2011-05-22 00:20:07 +0000351bool LTOCodeGenerator::generateObjectFile(raw_ostream &out,
352 std::string &errMsg) {
Bill Wendling0ca36af2012-08-08 22:03:50 +0000353 if (this->determineTarget(errMsg))
Bill Wendlingc94c5622012-03-31 11:15:43 +0000354 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000355
Bill Wendlingc94c5622012-03-31 11:15:43 +0000356 Module* mergedModule = _linker.getModule();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000357
Bill Wendlingc94c5622012-03-31 11:15:43 +0000358 // if options were requested, set them
Bill Wendling0ca36af2012-08-08 22:03:50 +0000359 if (!_codegenOptions.empty())
Bill Wendlingc94c5622012-03-31 11:15:43 +0000360 cl::ParseCommandLineOptions(_codegenOptions.size(),
361 const_cast<char **>(&_codegenOptions[0]));
Devang Patela93ae712008-07-03 22:53:14 +0000362
Bill Wendling64d5b282012-04-09 22:18:01 +0000363 // mark which symbols can not be internalized
364 this->applyScopeRestrictions();
365
Bill Wendlingc94c5622012-03-31 11:15:43 +0000366 // Instantiate the pass manager to organize the passes.
367 PassManager passes;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000368
Bill Wendlingc94c5622012-03-31 11:15:43 +0000369 // Start off with a verification pass.
370 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000371
Micah Villmow791cfc22012-10-08 16:39:34 +0000372 // Add an appropriate DataLayout instance for this module...
373 passes.add(new DataLayout(*_target->getDataLayout()));
Nadav Rotemcbd9a192012-10-18 23:22:48 +0000374 passes.add(new TargetTransformInfo(_target->getScalarTargetTransformInfo(),
375 _target->getVectorTargetTransformInfo()));
Bill Wendling76b13ed2012-03-31 10:50:14 +0000376
Rafael Espindola4d2e9d92012-04-16 10:58:38 +0000377 // Enabling internalize here would use its AllButMain variant. It
378 // keeps only main if it exists and does nothing for libraries. Instead
379 // we create the pass ourselves with the symbol list provided by the linker.
380 PassManagerBuilder().populateLTOPassManager(passes, /*Internalize=*/false,
Bill Wendling3197b442012-04-02 22:16:50 +0000381 !DisableInline,
382 DisableGVNLoadPRE);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000383
Bill Wendlingc94c5622012-03-31 11:15:43 +0000384 // Make sure everything is still good.
385 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000386
Bill Wendlingc94c5622012-03-31 11:15:43 +0000387 FunctionPassManager *codeGenPasses = new FunctionPassManager(mergedModule);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000388
Micah Villmow791cfc22012-10-08 16:39:34 +0000389 codeGenPasses->add(new DataLayout(*_target->getDataLayout()));
Nick Kledzik77595fc2008-02-26 20:26:43 +0000390
Bill Wendlingc94c5622012-03-31 11:15:43 +0000391 formatted_raw_ostream Out(out);
Dan Gohmand4c45432010-09-01 14:20:41 +0000392
Bill Wendlingc94c5622012-03-31 11:15:43 +0000393 if (_target->addPassesToEmitFile(*codeGenPasses, Out,
David Blaikie4f56a302012-05-30 18:42:51 +0000394 TargetMachine::CGFT_ObjectFile)) {
Bill Wendlingc94c5622012-03-31 11:15:43 +0000395 errMsg = "target file type not supported";
396 return true;
397 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000398
Bill Wendlingc94c5622012-03-31 11:15:43 +0000399 // Run our queue of passes all at once now, efficiently.
400 passes.run(*mergedModule);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000401
Bill Wendlingc94c5622012-03-31 11:15:43 +0000402 // Run the code generator, and write assembly file
403 codeGenPasses->doInitialization();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000404
Bill Wendlingc94c5622012-03-31 11:15:43 +0000405 for (Module::iterator
406 it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it)
407 if (!it->isDeclaration())
408 codeGenPasses->run(*it);
Bill Wendling604a8182008-06-18 06:35:30 +0000409
Bill Wendlingc94c5622012-03-31 11:15:43 +0000410 codeGenPasses->doFinalization();
411 delete codeGenPasses;
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000412
Bill Wendlingc94c5622012-03-31 11:15:43 +0000413 return false; // success
Nick Kledzik77595fc2008-02-26 20:26:43 +0000414}
415
Bill Wendlingcaf71d42012-03-31 10:49:43 +0000416/// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
417/// LTO problems.
418void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) {
419 for (std::pair<StringRef, StringRef> o = getToken(options);
420 !o.first.empty(); o = getToken(o.second)) {
421 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add
422 // that.
Bill Wendling0ca36af2012-08-08 22:03:50 +0000423 if (_codegenOptions.empty())
Bill Wendlingcaf71d42012-03-31 10:49:43 +0000424 _codegenOptions.push_back(strdup("libLTO"));
425 _codegenOptions.push_back(strdup(o.first.str().c_str()));
426 }
Nick Kledzik920ae982008-07-08 21:14:10 +0000427}