blob: e7d8b6e26dc6685ea1da9f0991556c7e1dbd4ad6 [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");
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())
John Criswell3f0e2372011-08-18 01:19:05 +0000167 return true;
Bill Wendlingc94c5622012-03-31 11:15:43 +0000168
Rafael Espindola6421a882011-03-22 20:57:13 +0000169 genResult = this->generateObjectFile(objFile.os(), errMsg);
170 objFile.os().close();
171 if (objFile.os().has_error()) {
172 objFile.os().clear_error();
173 return true;
174 }
Bill Wendlingc94c5622012-03-31 11:15:43 +0000175
Rafael Espindola6421a882011-03-22 20:57:13 +0000176 objFile.keep();
177 if ( genResult ) {
178 uniqueObjPath.eraseFromDisk();
179 return true;
180 }
181
182 _nativeObjectPath = uniqueObjPath.str();
183 *name = _nativeObjectPath.c_str();
184 return false;
185}
186
Bill Wendlingc94c5622012-03-31 11:15:43 +0000187const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000188 const char *name;
189 if (compile_to_file(&name, errMsg))
190 return NULL;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000191
Rafael Espindola6421a882011-03-22 20:57:13 +0000192 // remove old buffer if compile() called twice
193 delete _nativeObjectFile;
Nick Kledzikef194ed2008-02-27 22:25:36 +0000194
Rafael Espindola6421a882011-03-22 20:57:13 +0000195 // read .o file into memory buffer
196 OwningPtr<MemoryBuffer> BuffPtr;
197 if (error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) {
198 errMsg = ec.message();
199 return NULL;
200 }
201 _nativeObjectFile = BuffPtr.take();
Rafael Espindolae9efea12011-02-24 21:04:06 +0000202
Rafael Espindola6421a882011-03-22 20:57:13 +0000203 // remove temp files
204 sys::Path(_nativeObjectPath).eraseFromDisk();
Rafael Espindolae9efea12011-02-24 21:04:06 +0000205
Rafael Espindola6421a882011-03-22 20:57:13 +0000206 // return buffer, unless error
207 if ( _nativeObjectFile == NULL )
208 return NULL;
209 *length = _nativeObjectFile->getBufferSize();
210 return _nativeObjectFile->getBufferStart();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000211}
212
Bill Wendlingc94c5622012-03-31 11:15:43 +0000213bool LTOCodeGenerator::determineTarget(std::string& errMsg) {
214 if ( _target == NULL ) {
215 std::string Triple = _linker.getModule()->getTargetTriple();
216 if (Triple.empty())
217 Triple = sys::getDefaultTargetTriple();
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000218
Bill Wendlingc94c5622012-03-31 11:15:43 +0000219 // create target machine from info for merged modules
220 const Target *march = TargetRegistry::lookupTarget(Triple, errMsg);
221 if ( march == NULL )
222 return true;
Bill Wendling604a8182008-06-18 06:35:30 +0000223
Bill Wendlingc94c5622012-03-31 11:15:43 +0000224 // The relocation model is actually a static member of TargetMachine and
225 // needs to be set before the TargetMachine is instantiated.
226 Reloc::Model RelocModel = Reloc::Default;
227 switch( _codeModel ) {
228 case LTO_CODEGEN_PIC_MODEL_STATIC:
229 RelocModel = Reloc::Static;
230 break;
231 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
232 RelocModel = Reloc::PIC_;
233 break;
234 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
235 RelocModel = Reloc::DynamicNoPIC;
236 break;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000237 }
Bill Wendlingc94c5622012-03-31 11:15:43 +0000238
239 // construct LTOModule, hand over ownership of module and target
240 SubtargetFeatures Features;
241 Features.getDefaultSubtargetFeatures(llvm::Triple(Triple));
242 std::string FeatureStr = Features.getString();
243 TargetOptions Options;
Bill Wendling9ac0aaa2012-08-06 21:34:54 +0000244 LTOModule::getTargetOptions(Options);
Bill Wendlingc94c5622012-03-31 11:15:43 +0000245 _target = march->createTargetMachine(Triple, _mCpu, FeatureStr, Options,
David Blaikie4f56a302012-05-30 18:42:51 +0000246 RelocModel, CodeModel::Default,
247 CodeGenOpt::Aggressive);
Bill Wendlingc94c5622012-03-31 11:15:43 +0000248 }
249 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000250}
251
Bill Wendlingc94c5622012-03-31 11:15:43 +0000252void LTOCodeGenerator::
253applyRestriction(GlobalValue &GV,
254 std::vector<const char*> &mustPreserveList,
255 SmallPtrSet<GlobalValue*, 8> &asmUsed,
256 Mangler &mangler) {
Rafael Espindola38c4e532011-03-02 04:14:42 +0000257 SmallString<64> Buffer;
258 mangler.getNameWithPrefix(Buffer, &GV, false);
259
260 if (GV.isDeclaration())
261 return;
262 if (_mustPreserveSymbols.count(Buffer))
263 mustPreserveList.push_back(GV.getName().data());
264 if (_asmUndefinedRefs.count(Buffer))
265 asmUsed.insert(&GV);
266}
267
268static void findUsedValues(GlobalVariable *LLVMUsed,
269 SmallPtrSet<GlobalValue*, 8> &UsedValues) {
270 if (LLVMUsed == 0) return;
271
272 ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer());
273 if (Inits == 0) return;
274
275 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
Bill Wendling76b13ed2012-03-31 10:50:14 +0000276 if (GlobalValue *GV =
Bill Wendlingc94c5622012-03-31 11:15:43 +0000277 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
Rafael Espindola38c4e532011-03-02 04:14:42 +0000278 UsedValues.insert(GV);
279}
280
Chris Lattner5ef31a02010-03-12 18:44:54 +0000281void LTOCodeGenerator::applyScopeRestrictions() {
282 if (_scopeRestrictionsDone) return;
283 Module *mergedModule = _linker.getModule();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000284
Chris Lattner5ef31a02010-03-12 18:44:54 +0000285 // Start off with a verification pass.
286 PassManager passes;
287 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000288
Bill Wendling76b13ed2012-03-31 10:50:14 +0000289 // mark which symbols can not be internalized
Bill Wendlingc94c5622012-03-31 11:15:43 +0000290 MCContext Context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(),NULL);
Rafael Espindola38c4e532011-03-02 04:14:42 +0000291 Mangler mangler(Context, *_target->getTargetData());
292 std::vector<const char*> mustPreserveList;
293 SmallPtrSet<GlobalValue*, 8> asmUsed;
294
295 for (Module::iterator f = mergedModule->begin(),
296 e = mergedModule->end(); f != e; ++f)
297 applyRestriction(*f, mustPreserveList, asmUsed, mangler);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000298 for (Module::global_iterator v = mergedModule->global_begin(),
Rafael Espindola38c4e532011-03-02 04:14:42 +0000299 e = mergedModule->global_end(); v != e; ++v)
300 applyRestriction(*v, mustPreserveList, asmUsed, mangler);
301 for (Module::alias_iterator a = mergedModule->alias_begin(),
302 e = mergedModule->alias_end(); a != e; ++a)
303 applyRestriction(*a, mustPreserveList, asmUsed, mangler);
304
305 GlobalVariable *LLVMCompilerUsed =
306 mergedModule->getGlobalVariable("llvm.compiler.used");
307 findUsedValues(LLVMCompilerUsed, asmUsed);
308 if (LLVMCompilerUsed)
309 LLVMCompilerUsed->eraseFromParent();
310
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000311 llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(_context);
Rafael Espindola38c4e532011-03-02 04:14:42 +0000312 std::vector<Constant*> asmUsed2;
313 for (SmallPtrSet<GlobalValue*, 16>::const_iterator i = asmUsed.begin(),
314 e = asmUsed.end(); i !=e; ++i) {
315 GlobalValue *GV = *i;
316 Constant *c = ConstantExpr::getBitCast(GV, i8PTy);
317 asmUsed2.push_back(c);
Chris Lattner5ef31a02010-03-12 18:44:54 +0000318 }
Rafael Espindola38c4e532011-03-02 04:14:42 +0000319
320 llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size());
321 LLVMCompilerUsed =
322 new llvm::GlobalVariable(*mergedModule, ATy, false,
323 llvm::GlobalValue::AppendingLinkage,
324 llvm::ConstantArray::get(ATy, asmUsed2),
325 "llvm.compiler.used");
326
327 LLVMCompilerUsed->setSection("llvm.metadata");
328
329 passes.add(createInternalizePass(mustPreserveList));
330
Chris Lattner5ef31a02010-03-12 18:44:54 +0000331 // apply scope restrictions
332 passes.run(*mergedModule);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000333
Chris Lattner5ef31a02010-03-12 18:44:54 +0000334 _scopeRestrictionsDone = true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000335}
336
Nick Kledzik77595fc2008-02-26 20:26:43 +0000337/// Optimize merged modules using various IPO passes
Chris Lattner817a01f2011-05-22 00:20:07 +0000338bool LTOCodeGenerator::generateObjectFile(raw_ostream &out,
339 std::string &errMsg) {
Bill Wendlingc94c5622012-03-31 11:15:43 +0000340 if ( this->determineTarget(errMsg) )
341 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000342
Bill Wendlingc94c5622012-03-31 11:15:43 +0000343 Module* mergedModule = _linker.getModule();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000344
Bill Wendlingc94c5622012-03-31 11:15:43 +0000345 // if options were requested, set them
346 if ( !_codegenOptions.empty() )
347 cl::ParseCommandLineOptions(_codegenOptions.size(),
348 const_cast<char **>(&_codegenOptions[0]));
Devang Patela93ae712008-07-03 22:53:14 +0000349
Bill Wendling64d5b282012-04-09 22:18:01 +0000350 // mark which symbols can not be internalized
351 this->applyScopeRestrictions();
352
Bill Wendlingc94c5622012-03-31 11:15:43 +0000353 // Instantiate the pass manager to organize the passes.
354 PassManager passes;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000355
Bill Wendlingc94c5622012-03-31 11:15:43 +0000356 // Start off with a verification pass.
357 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000358
Bill Wendlingc94c5622012-03-31 11:15:43 +0000359 // Add an appropriate TargetData instance for this module...
360 passes.add(new TargetData(*_target->getTargetData()));
Bill Wendling76b13ed2012-03-31 10:50:14 +0000361
Rafael Espindola4d2e9d92012-04-16 10:58:38 +0000362 // Enabling internalize here would use its AllButMain variant. It
363 // keeps only main if it exists and does nothing for libraries. Instead
364 // we create the pass ourselves with the symbol list provided by the linker.
365 PassManagerBuilder().populateLTOPassManager(passes, /*Internalize=*/false,
Bill Wendling3197b442012-04-02 22:16:50 +0000366 !DisableInline,
367 DisableGVNLoadPRE);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000368
Bill Wendlingc94c5622012-03-31 11:15:43 +0000369 // Make sure everything is still good.
370 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000371
Bill Wendlingc94c5622012-03-31 11:15:43 +0000372 FunctionPassManager *codeGenPasses = new FunctionPassManager(mergedModule);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000373
Bill Wendlingc94c5622012-03-31 11:15:43 +0000374 codeGenPasses->add(new TargetData(*_target->getTargetData()));
Nick Kledzik77595fc2008-02-26 20:26:43 +0000375
Bill Wendlingc94c5622012-03-31 11:15:43 +0000376 formatted_raw_ostream Out(out);
Dan Gohmand4c45432010-09-01 14:20:41 +0000377
Bill Wendlingc94c5622012-03-31 11:15:43 +0000378 if (_target->addPassesToEmitFile(*codeGenPasses, Out,
David Blaikie4f56a302012-05-30 18:42:51 +0000379 TargetMachine::CGFT_ObjectFile)) {
Bill Wendlingc94c5622012-03-31 11:15:43 +0000380 errMsg = "target file type not supported";
381 return true;
382 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000383
Bill Wendlingc94c5622012-03-31 11:15:43 +0000384 // Run our queue of passes all at once now, efficiently.
385 passes.run(*mergedModule);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000386
Bill Wendlingc94c5622012-03-31 11:15:43 +0000387 // Run the code generator, and write assembly file
388 codeGenPasses->doInitialization();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000389
Bill Wendlingc94c5622012-03-31 11:15:43 +0000390 for (Module::iterator
391 it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it)
392 if (!it->isDeclaration())
393 codeGenPasses->run(*it);
Bill Wendling604a8182008-06-18 06:35:30 +0000394
Bill Wendlingc94c5622012-03-31 11:15:43 +0000395 codeGenPasses->doFinalization();
396 delete codeGenPasses;
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000397
Bill Wendlingc94c5622012-03-31 11:15:43 +0000398 return false; // success
Nick Kledzik77595fc2008-02-26 20:26:43 +0000399}
400
Bill Wendlingcaf71d42012-03-31 10:49:43 +0000401/// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
402/// LTO problems.
403void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) {
404 for (std::pair<StringRef, StringRef> o = getToken(options);
405 !o.first.empty(); o = getToken(o.second)) {
406 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add
407 // that.
408 if ( _codegenOptions.empty() )
409 _codegenOptions.push_back(strdup("libLTO"));
410 _codegenOptions.push_back(strdup(o.first.str().c_str()));
411 }
Nick Kledzik920ae982008-07-08 21:14:10 +0000412}