blob: f8b07b2e2ab43e4a6b7bee1c2d1933f214e0a6e7 [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"
Nick Kledzik77595fc2008-02-26 20:26:43 +000017#include "llvm/Constants.h"
18#include "llvm/DerivedTypes.h"
Nick Lewycky8189d402009-06-17 06:52:10 +000019#include "llvm/Linker.h"
Owen Anderson0e7a5462009-07-02 00:31:14 +000020#include "llvm/LLVMContext.h"
Nick Lewycky8189d402009-06-17 06:52:10 +000021#include "llvm/Module.h"
Nick Lewycky8189d402009-06-17 06:52:10 +000022#include "llvm/PassManager.h"
Nick Lewycky8189d402009-06-17 06:52:10 +000023#include "llvm/Analysis/Passes.h"
Rafael Espindolac684e832011-08-02 21:50:27 +000024#include "llvm/Analysis/Verifier.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000025#include "llvm/Bitcode/ReaderWriter.h"
Bill Wendlingc94c5622012-03-31 11:15:43 +000026#include "llvm/Config/config.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"
Chris Lattner5ef31a02010-03-12 18:44:54 +000030#include "llvm/Target/Mangler.h"
Chris Lattner5ef31a02010-03-12 18:44:54 +000031#include "llvm/Target/TargetOptions.h"
32#include "llvm/Target/TargetData.h"
33#include "llvm/Target/TargetMachine.h"
Evan Cheng0e6a0522011-07-18 20:57:22 +000034#include "llvm/Target/TargetRegisterInfo.h"
Bill Wendlingc94c5622012-03-31 11:15:43 +000035#include "llvm/Transforms/IPO.h"
36#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Nick Kledzik920ae982008-07-08 21:14:10 +000037#include "llvm/Support/CommandLine.h"
David Greene71847812009-07-14 20:18:05 +000038#include "llvm/Support/FormattedStream.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000039#include "llvm/Support/MemoryBuffer.h"
Dan Gohman9f36c4e2010-10-07 20:48:46 +000040#include "llvm/Support/ToolOutputFile.h"
Michael J. Spencer3cc52ea2010-11-29 18:47:54 +000041#include "llvm/Support/Host.h"
Michael J. Spencer3cc52ea2010-11-29 18:47:54 +000042#include "llvm/Support/Signals.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000043#include "llvm/Support/TargetRegistry.h"
44#include "llvm/Support/TargetSelect.h"
Michael J. Spencerf2f516f2010-12-09 18:06:07 +000045#include "llvm/Support/system_error.h"
Bill Wendlingc94c5622012-03-31 11:15:43 +000046#include "llvm/ADT/StringExtras.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
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000221 std::string Triple = _linker.getModule()->getTargetTriple();
222 if (Triple.empty())
223 Triple = sys::getDefaultTargetTriple();
Bill Wendling604a8182008-06-18 06:35:30 +0000224
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000225 // create target machine from info for merged modules
226 const Target *march = TargetRegistry::lookupTarget(Triple, errMsg);
Bill Wendling0ca36af2012-08-08 22:03:50 +0000227 if (march == NULL)
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000228 return true;
Bill Wendlingc94c5622012-03-31 11:15:43 +0000229
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000230 // The relocation model is actually a static member of TargetMachine and
231 // needs to be set before the TargetMachine is instantiated.
232 Reloc::Model RelocModel = Reloc::Default;
Bill Wendling0ca36af2012-08-08 22:03:50 +0000233 switch (_codeModel) {
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000234 case LTO_CODEGEN_PIC_MODEL_STATIC:
235 RelocModel = Reloc::Static;
236 break;
237 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
238 RelocModel = Reloc::PIC_;
239 break;
240 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
241 RelocModel = Reloc::DynamicNoPIC;
242 break;
Bill Wendlingc94c5622012-03-31 11:15:43 +0000243 }
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000244
245 // construct LTOModule, hand over ownership of module and target
246 SubtargetFeatures Features;
247 Features.getDefaultSubtargetFeatures(llvm::Triple(Triple));
248 std::string FeatureStr = Features.getString();
249 TargetOptions Options;
250 LTOModule::getTargetOptions(Options);
251 _target = march->createTargetMachine(Triple, _mCpu, FeatureStr, Options,
252 RelocModel, CodeModel::Default,
253 CodeGenOpt::Aggressive);
Bill Wendlingc94c5622012-03-31 11:15:43 +0000254 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000255}
256
Bill Wendlingc94c5622012-03-31 11:15:43 +0000257void LTOCodeGenerator::
258applyRestriction(GlobalValue &GV,
259 std::vector<const char*> &mustPreserveList,
260 SmallPtrSet<GlobalValue*, 8> &asmUsed,
261 Mangler &mangler) {
Rafael Espindola38c4e532011-03-02 04:14:42 +0000262 SmallString<64> Buffer;
263 mangler.getNameWithPrefix(Buffer, &GV, false);
264
265 if (GV.isDeclaration())
266 return;
267 if (_mustPreserveSymbols.count(Buffer))
268 mustPreserveList.push_back(GV.getName().data());
269 if (_asmUndefinedRefs.count(Buffer))
270 asmUsed.insert(&GV);
271}
272
273static void findUsedValues(GlobalVariable *LLVMUsed,
274 SmallPtrSet<GlobalValue*, 8> &UsedValues) {
275 if (LLVMUsed == 0) return;
276
277 ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer());
278 if (Inits == 0) return;
279
280 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
Bill Wendling76b13ed2012-03-31 10:50:14 +0000281 if (GlobalValue *GV =
Bill Wendlingc94c5622012-03-31 11:15:43 +0000282 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
Rafael Espindola38c4e532011-03-02 04:14:42 +0000283 UsedValues.insert(GV);
284}
285
Chris Lattner5ef31a02010-03-12 18:44:54 +0000286void LTOCodeGenerator::applyScopeRestrictions() {
287 if (_scopeRestrictionsDone) return;
288 Module *mergedModule = _linker.getModule();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000289
Chris Lattner5ef31a02010-03-12 18:44:54 +0000290 // Start off with a verification pass.
291 PassManager passes;
292 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000293
Bill Wendling76b13ed2012-03-31 10:50:14 +0000294 // mark which symbols can not be internalized
Bill Wendlingc94c5622012-03-31 11:15:43 +0000295 MCContext Context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(),NULL);
Rafael Espindola38c4e532011-03-02 04:14:42 +0000296 Mangler mangler(Context, *_target->getTargetData());
297 std::vector<const char*> mustPreserveList;
298 SmallPtrSet<GlobalValue*, 8> asmUsed;
299
300 for (Module::iterator f = mergedModule->begin(),
301 e = mergedModule->end(); f != e; ++f)
302 applyRestriction(*f, mustPreserveList, asmUsed, mangler);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000303 for (Module::global_iterator v = mergedModule->global_begin(),
Rafael Espindola38c4e532011-03-02 04:14:42 +0000304 e = mergedModule->global_end(); v != e; ++v)
305 applyRestriction(*v, mustPreserveList, asmUsed, mangler);
306 for (Module::alias_iterator a = mergedModule->alias_begin(),
307 e = mergedModule->alias_end(); a != e; ++a)
308 applyRestriction(*a, mustPreserveList, asmUsed, mangler);
309
310 GlobalVariable *LLVMCompilerUsed =
311 mergedModule->getGlobalVariable("llvm.compiler.used");
312 findUsedValues(LLVMCompilerUsed, asmUsed);
313 if (LLVMCompilerUsed)
314 LLVMCompilerUsed->eraseFromParent();
315
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000316 llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(_context);
Rafael Espindola38c4e532011-03-02 04:14:42 +0000317 std::vector<Constant*> asmUsed2;
318 for (SmallPtrSet<GlobalValue*, 16>::const_iterator i = asmUsed.begin(),
319 e = asmUsed.end(); i !=e; ++i) {
320 GlobalValue *GV = *i;
321 Constant *c = ConstantExpr::getBitCast(GV, i8PTy);
322 asmUsed2.push_back(c);
Chris Lattner5ef31a02010-03-12 18:44:54 +0000323 }
Rafael Espindola38c4e532011-03-02 04:14:42 +0000324
325 llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size());
326 LLVMCompilerUsed =
327 new llvm::GlobalVariable(*mergedModule, ATy, false,
328 llvm::GlobalValue::AppendingLinkage,
329 llvm::ConstantArray::get(ATy, asmUsed2),
330 "llvm.compiler.used");
331
332 LLVMCompilerUsed->setSection("llvm.metadata");
333
334 passes.add(createInternalizePass(mustPreserveList));
335
Chris Lattner5ef31a02010-03-12 18:44:54 +0000336 // apply scope restrictions
337 passes.run(*mergedModule);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000338
Chris Lattner5ef31a02010-03-12 18:44:54 +0000339 _scopeRestrictionsDone = true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000340}
341
Nick Kledzik77595fc2008-02-26 20:26:43 +0000342/// Optimize merged modules using various IPO passes
Chris Lattner817a01f2011-05-22 00:20:07 +0000343bool LTOCodeGenerator::generateObjectFile(raw_ostream &out,
344 std::string &errMsg) {
Bill Wendling0ca36af2012-08-08 22:03:50 +0000345 if (this->determineTarget(errMsg))
Bill Wendlingc94c5622012-03-31 11:15:43 +0000346 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000347
Bill Wendlingc94c5622012-03-31 11:15:43 +0000348 Module* mergedModule = _linker.getModule();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000349
Bill Wendlingc94c5622012-03-31 11:15:43 +0000350 // if options were requested, set them
Bill Wendling0ca36af2012-08-08 22:03:50 +0000351 if (!_codegenOptions.empty())
Bill Wendlingc94c5622012-03-31 11:15:43 +0000352 cl::ParseCommandLineOptions(_codegenOptions.size(),
353 const_cast<char **>(&_codegenOptions[0]));
Devang Patela93ae712008-07-03 22:53:14 +0000354
Bill Wendling64d5b282012-04-09 22:18:01 +0000355 // mark which symbols can not be internalized
356 this->applyScopeRestrictions();
357
Bill Wendlingc94c5622012-03-31 11:15:43 +0000358 // Instantiate the pass manager to organize the passes.
359 PassManager passes;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000360
Bill Wendlingc94c5622012-03-31 11:15:43 +0000361 // Start off with a verification pass.
362 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000363
Bill Wendlingc94c5622012-03-31 11:15:43 +0000364 // Add an appropriate TargetData instance for this module...
365 passes.add(new TargetData(*_target->getTargetData()));
Bill Wendling76b13ed2012-03-31 10:50:14 +0000366
Rafael Espindola4d2e9d92012-04-16 10:58:38 +0000367 // Enabling internalize here would use its AllButMain variant. It
368 // keeps only main if it exists and does nothing for libraries. Instead
369 // we create the pass ourselves with the symbol list provided by the linker.
370 PassManagerBuilder().populateLTOPassManager(passes, /*Internalize=*/false,
Bill Wendling3197b442012-04-02 22:16:50 +0000371 !DisableInline,
372 DisableGVNLoadPRE);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000373
Bill Wendlingc94c5622012-03-31 11:15:43 +0000374 // Make sure everything is still good.
375 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000376
Bill Wendlingc94c5622012-03-31 11:15:43 +0000377 FunctionPassManager *codeGenPasses = new FunctionPassManager(mergedModule);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000378
Bill Wendlingc94c5622012-03-31 11:15:43 +0000379 codeGenPasses->add(new TargetData(*_target->getTargetData()));
Nick Kledzik77595fc2008-02-26 20:26:43 +0000380
Bill Wendlingc94c5622012-03-31 11:15:43 +0000381 formatted_raw_ostream Out(out);
Dan Gohmand4c45432010-09-01 14:20:41 +0000382
Bill Wendlingc94c5622012-03-31 11:15:43 +0000383 if (_target->addPassesToEmitFile(*codeGenPasses, Out,
David Blaikie4f56a302012-05-30 18:42:51 +0000384 TargetMachine::CGFT_ObjectFile)) {
Bill Wendlingc94c5622012-03-31 11:15:43 +0000385 errMsg = "target file type not supported";
386 return true;
387 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000388
Bill Wendlingc94c5622012-03-31 11:15:43 +0000389 // Run our queue of passes all at once now, efficiently.
390 passes.run(*mergedModule);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000391
Bill Wendlingc94c5622012-03-31 11:15:43 +0000392 // Run the code generator, and write assembly file
393 codeGenPasses->doInitialization();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000394
Bill Wendlingc94c5622012-03-31 11:15:43 +0000395 for (Module::iterator
396 it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it)
397 if (!it->isDeclaration())
398 codeGenPasses->run(*it);
Bill Wendling604a8182008-06-18 06:35:30 +0000399
Bill Wendlingc94c5622012-03-31 11:15:43 +0000400 codeGenPasses->doFinalization();
401 delete codeGenPasses;
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000402
Bill Wendlingc94c5622012-03-31 11:15:43 +0000403 return false; // success
Nick Kledzik77595fc2008-02-26 20:26:43 +0000404}
405
Bill Wendlingcaf71d42012-03-31 10:49:43 +0000406/// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
407/// LTO problems.
408void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) {
409 for (std::pair<StringRef, StringRef> o = getToken(options);
410 !o.first.empty(); o = getToken(o.second)) {
411 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add
412 // that.
Bill Wendling0ca36af2012-08-08 22:03:50 +0000413 if (_codegenOptions.empty())
Bill Wendlingcaf71d42012-03-31 10:49:43 +0000414 _codegenOptions.push_back(strdup("libLTO"));
415 _codegenOptions.push_back(strdup(o.first.str().c_str()));
416 }
Nick Kledzik920ae982008-07-08 21:14:10 +0000417}