blob: 1d2631e07d1b33034579020249672db766bf79b7 [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
Nick Kledzik920ae982008-07-08 21:14:10 +000049static cl::opt<bool> DisableInline("disable-inlining",
50 cl::desc("Do not run the inliner pass"));
Nick Kledzik77595fc2008-02-26 20:26:43 +000051
Bill Wendlingcaf71d42012-03-31 10:49:43 +000052const char* LTOCodeGenerator::getVersionString() {
Nick Kledzik77595fc2008-02-26 20:26:43 +000053#ifdef LLVM_VERSION_INFO
Bill Wendlingcaf71d42012-03-31 10:49:43 +000054 return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
Nick Kledzik77595fc2008-02-26 20:26:43 +000055#else
Bill Wendlingcaf71d42012-03-31 10:49:43 +000056 return PACKAGE_NAME " version " PACKAGE_VERSION;
Nick Kledzik77595fc2008-02-26 20:26:43 +000057#endif
58}
59
Bill Wendling76b13ed2012-03-31 10:50:14 +000060LTOCodeGenerator::LTOCodeGenerator()
Bill Wendlingc94c5622012-03-31 11:15:43 +000061 : _context(getGlobalContext()),
62 _linker("LinkTimeOptimizer", "ld-temp.o", _context), _target(NULL),
63 _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false),
64 _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
65 _nativeObjectFile(NULL) {
66 InitializeAllTargets();
67 InitializeAllTargetMCs();
68 InitializeAllAsmPrinters();
Nick Kledzik77595fc2008-02-26 20:26:43 +000069}
70
Bill Wendlingcaf71d42012-03-31 10:49:43 +000071LTOCodeGenerator::~LTOCodeGenerator() {
72 delete _target;
73 delete _nativeObjectFile;
74
Bill Wendlingf2cc2ee2012-03-31 10:51:45 +000075 for (std::vector<char*>::iterator I = _codegenOptions.begin(),
Bill Wendlingcaf71d42012-03-31 10:49:43 +000076 E = _codegenOptions.end(); I != E; ++I)
77 free(*I);
Nick Kledzik77595fc2008-02-26 20:26:43 +000078}
79
Bill Wendlingc94c5622012-03-31 11:15:43 +000080bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) {
Rafael Espindola38c4e532011-03-02 04:14:42 +000081 bool ret = _linker.LinkInModule(mod->getLLVVMModule(), &errMsg);
82
83 const std::vector<const char*> &undefs = mod->getAsmUndefinedRefs();
84 for (int i = 0, e = undefs.size(); i != e; ++i)
85 _asmUndefinedRefs[undefs[i]] = 1;
86
87 return ret;
Nick Kledzik77595fc2008-02-26 20:26:43 +000088}
Bill Wendling76b13ed2012-03-31 10:50:14 +000089
Bill Wendlingc94c5622012-03-31 11:15:43 +000090bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug,
91 std::string& errMsg) {
92 switch (debug) {
93 case LTO_DEBUG_MODEL_NONE:
94 _emitDwarfDebugInfo = false;
95 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +000096
Bill Wendlingc94c5622012-03-31 11:15:43 +000097 case LTO_DEBUG_MODEL_DWARF:
98 _emitDwarfDebugInfo = true;
99 return false;
100 }
101 llvm_unreachable("Unknown debug format!");
Nick Kledzik77595fc2008-02-26 20:26:43 +0000102}
103
Bill Wendling76b13ed2012-03-31 10:50:14 +0000104bool LTOCodeGenerator::setCodePICModel(lto_codegen_model model,
Bill Wendlingc94c5622012-03-31 11:15:43 +0000105 std::string& errMsg) {
106 switch (model) {
107 case LTO_CODEGEN_PIC_MODEL_STATIC:
108 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
109 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
110 _codeModel = model;
111 return false;
112 }
113 llvm_unreachable("Unknown PIC model!");
Nick Kledzik77595fc2008-02-26 20:26:43 +0000114}
115
Chris Lattnerb515d752009-08-23 07:49:08 +0000116bool LTOCodeGenerator::writeMergedModules(const char *path,
117 std::string &errMsg) {
118 if (determineTarget(errMsg))
119 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000120
Bill Wendling76b13ed2012-03-31 10:50:14 +0000121 // mark which symbols can not be internalized
Chris Lattnerb515d752009-08-23 07:49:08 +0000122 applyScopeRestrictions();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000123
Chris Lattnerb515d752009-08-23 07:49:08 +0000124 // create output file
125 std::string ErrInfo;
Dan Gohmanf2914012010-08-20 16:59:15 +0000126 tool_output_file Out(path, ErrInfo,
127 raw_fd_ostream::F_Binary);
Chris Lattnerb515d752009-08-23 07:49:08 +0000128 if (!ErrInfo.empty()) {
129 errMsg = "could not open bitcode file for writing: ";
130 errMsg += path;
131 return true;
132 }
Bill Wendling76b13ed2012-03-31 10:50:14 +0000133
Chris Lattnerb515d752009-08-23 07:49:08 +0000134 // write bitcode to it
Dan Gohmand4c45432010-09-01 14:20:41 +0000135 WriteBitcodeToFile(_linker.getModule(), Out.os());
136 Out.os().close();
Dan Gohman4b7416b2010-05-27 20:19:47 +0000137
Dan Gohmand4c45432010-09-01 14:20:41 +0000138 if (Out.os().has_error()) {
Chris Lattnerb515d752009-08-23 07:49:08 +0000139 errMsg = "could not write bitcode file: ";
140 errMsg += path;
Dan Gohmand4c45432010-09-01 14:20:41 +0000141 Out.os().clear_error();
Chris Lattnerb515d752009-08-23 07:49:08 +0000142 return true;
143 }
Bill Wendling76b13ed2012-03-31 10:50:14 +0000144
Dan Gohmanf2914012010-08-20 16:59:15 +0000145 Out.keep();
Chris Lattnerb515d752009-08-23 07:49:08 +0000146 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000147}
148
Bill Wendlingc94c5622012-03-31 11:15:43 +0000149bool LTOCodeGenerator::compile_to_file(const char** name, std::string& errMsg) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000150 // make unique temp .o file to put generated object file
151 sys::PathWithStatus uniqueObjPath("lto-llvm.o");
152 if ( uniqueObjPath.createTemporaryFileOnDisk(false, &errMsg) ) {
153 uniqueObjPath.eraseFromDisk();
154 return true;
155 }
156 sys::RemoveFileOnSignal(uniqueObjPath);
157
158 // generate object file
159 bool genResult = false;
160 tool_output_file objFile(uniqueObjPath.c_str(), errMsg);
161 if (!errMsg.empty())
John Criswell3f0e2372011-08-18 01:19:05 +0000162 return true;
Bill Wendlingc94c5622012-03-31 11:15:43 +0000163
Rafael Espindola6421a882011-03-22 20:57:13 +0000164 genResult = this->generateObjectFile(objFile.os(), errMsg);
165 objFile.os().close();
166 if (objFile.os().has_error()) {
167 objFile.os().clear_error();
168 return true;
169 }
Bill Wendlingc94c5622012-03-31 11:15:43 +0000170
Rafael Espindola6421a882011-03-22 20:57:13 +0000171 objFile.keep();
172 if ( genResult ) {
173 uniqueObjPath.eraseFromDisk();
174 return true;
175 }
176
177 _nativeObjectPath = uniqueObjPath.str();
178 *name = _nativeObjectPath.c_str();
179 return false;
180}
181
Bill Wendlingc94c5622012-03-31 11:15:43 +0000182const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000183 const char *name;
184 if (compile_to_file(&name, errMsg))
185 return NULL;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000186
Rafael Espindola6421a882011-03-22 20:57:13 +0000187 // remove old buffer if compile() called twice
188 delete _nativeObjectFile;
Nick Kledzikef194ed2008-02-27 22:25:36 +0000189
Rafael Espindola6421a882011-03-22 20:57:13 +0000190 // read .o file into memory buffer
191 OwningPtr<MemoryBuffer> BuffPtr;
192 if (error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) {
193 errMsg = ec.message();
194 return NULL;
195 }
196 _nativeObjectFile = BuffPtr.take();
Rafael Espindolae9efea12011-02-24 21:04:06 +0000197
Rafael Espindola6421a882011-03-22 20:57:13 +0000198 // remove temp files
199 sys::Path(_nativeObjectPath).eraseFromDisk();
Rafael Espindolae9efea12011-02-24 21:04:06 +0000200
Rafael Espindola6421a882011-03-22 20:57:13 +0000201 // return buffer, unless error
202 if ( _nativeObjectFile == NULL )
203 return NULL;
204 *length = _nativeObjectFile->getBufferSize();
205 return _nativeObjectFile->getBufferStart();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000206}
207
Bill Wendlingc94c5622012-03-31 11:15:43 +0000208bool LTOCodeGenerator::determineTarget(std::string& errMsg) {
209 if ( _target == NULL ) {
210 std::string Triple = _linker.getModule()->getTargetTriple();
211 if (Triple.empty())
212 Triple = sys::getDefaultTargetTriple();
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000213
Bill Wendlingc94c5622012-03-31 11:15:43 +0000214 // create target machine from info for merged modules
215 const Target *march = TargetRegistry::lookupTarget(Triple, errMsg);
216 if ( march == NULL )
217 return true;
Bill Wendling604a8182008-06-18 06:35:30 +0000218
Bill Wendlingc94c5622012-03-31 11:15:43 +0000219 // The relocation model is actually a static member of TargetMachine and
220 // needs to be set before the TargetMachine is instantiated.
221 Reloc::Model RelocModel = Reloc::Default;
222 switch( _codeModel ) {
223 case LTO_CODEGEN_PIC_MODEL_STATIC:
224 RelocModel = Reloc::Static;
225 break;
226 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
227 RelocModel = Reloc::PIC_;
228 break;
229 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
230 RelocModel = Reloc::DynamicNoPIC;
231 break;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000232 }
Bill Wendlingc94c5622012-03-31 11:15:43 +0000233
234 // construct LTOModule, hand over ownership of module and target
235 SubtargetFeatures Features;
236 Features.getDefaultSubtargetFeatures(llvm::Triple(Triple));
237 std::string FeatureStr = Features.getString();
238 TargetOptions Options;
239 _target = march->createTargetMachine(Triple, _mCpu, FeatureStr, Options,
240 RelocModel);
241 }
242 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000243}
244
Bill Wendlingc94c5622012-03-31 11:15:43 +0000245void LTOCodeGenerator::
246applyRestriction(GlobalValue &GV,
247 std::vector<const char*> &mustPreserveList,
248 SmallPtrSet<GlobalValue*, 8> &asmUsed,
249 Mangler &mangler) {
Rafael Espindola38c4e532011-03-02 04:14:42 +0000250 SmallString<64> Buffer;
251 mangler.getNameWithPrefix(Buffer, &GV, false);
252
253 if (GV.isDeclaration())
254 return;
255 if (_mustPreserveSymbols.count(Buffer))
256 mustPreserveList.push_back(GV.getName().data());
257 if (_asmUndefinedRefs.count(Buffer))
258 asmUsed.insert(&GV);
259}
260
261static void findUsedValues(GlobalVariable *LLVMUsed,
262 SmallPtrSet<GlobalValue*, 8> &UsedValues) {
263 if (LLVMUsed == 0) return;
264
265 ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer());
266 if (Inits == 0) return;
267
268 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
Bill Wendling76b13ed2012-03-31 10:50:14 +0000269 if (GlobalValue *GV =
Bill Wendlingc94c5622012-03-31 11:15:43 +0000270 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
Rafael Espindola38c4e532011-03-02 04:14:42 +0000271 UsedValues.insert(GV);
272}
273
Chris Lattner5ef31a02010-03-12 18:44:54 +0000274void LTOCodeGenerator::applyScopeRestrictions() {
275 if (_scopeRestrictionsDone) return;
276 Module *mergedModule = _linker.getModule();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000277
Chris Lattner5ef31a02010-03-12 18:44:54 +0000278 // Start off with a verification pass.
279 PassManager passes;
280 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000281
Bill Wendling76b13ed2012-03-31 10:50:14 +0000282 // mark which symbols can not be internalized
Bill Wendlingc94c5622012-03-31 11:15:43 +0000283 MCContext Context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(),NULL);
Rafael Espindola38c4e532011-03-02 04:14:42 +0000284 Mangler mangler(Context, *_target->getTargetData());
285 std::vector<const char*> mustPreserveList;
286 SmallPtrSet<GlobalValue*, 8> asmUsed;
287
288 for (Module::iterator f = mergedModule->begin(),
289 e = mergedModule->end(); f != e; ++f)
290 applyRestriction(*f, mustPreserveList, asmUsed, mangler);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000291 for (Module::global_iterator v = mergedModule->global_begin(),
Rafael Espindola38c4e532011-03-02 04:14:42 +0000292 e = mergedModule->global_end(); v != e; ++v)
293 applyRestriction(*v, mustPreserveList, asmUsed, mangler);
294 for (Module::alias_iterator a = mergedModule->alias_begin(),
295 e = mergedModule->alias_end(); a != e; ++a)
296 applyRestriction(*a, mustPreserveList, asmUsed, mangler);
297
298 GlobalVariable *LLVMCompilerUsed =
299 mergedModule->getGlobalVariable("llvm.compiler.used");
300 findUsedValues(LLVMCompilerUsed, asmUsed);
301 if (LLVMCompilerUsed)
302 LLVMCompilerUsed->eraseFromParent();
303
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000304 llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(_context);
Rafael Espindola38c4e532011-03-02 04:14:42 +0000305 std::vector<Constant*> asmUsed2;
306 for (SmallPtrSet<GlobalValue*, 16>::const_iterator i = asmUsed.begin(),
307 e = asmUsed.end(); i !=e; ++i) {
308 GlobalValue *GV = *i;
309 Constant *c = ConstantExpr::getBitCast(GV, i8PTy);
310 asmUsed2.push_back(c);
Chris Lattner5ef31a02010-03-12 18:44:54 +0000311 }
Rafael Espindola38c4e532011-03-02 04:14:42 +0000312
313 llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size());
314 LLVMCompilerUsed =
315 new llvm::GlobalVariable(*mergedModule, ATy, false,
316 llvm::GlobalValue::AppendingLinkage,
317 llvm::ConstantArray::get(ATy, asmUsed2),
318 "llvm.compiler.used");
319
320 LLVMCompilerUsed->setSection("llvm.metadata");
321
322 passes.add(createInternalizePass(mustPreserveList));
323
Chris Lattner5ef31a02010-03-12 18:44:54 +0000324 // apply scope restrictions
325 passes.run(*mergedModule);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000326
Chris Lattner5ef31a02010-03-12 18:44:54 +0000327 _scopeRestrictionsDone = true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000328}
329
Nick Kledzik77595fc2008-02-26 20:26:43 +0000330/// Optimize merged modules using various IPO passes
Chris Lattner817a01f2011-05-22 00:20:07 +0000331bool LTOCodeGenerator::generateObjectFile(raw_ostream &out,
332 std::string &errMsg) {
Bill Wendlingc94c5622012-03-31 11:15:43 +0000333 if ( this->determineTarget(errMsg) )
334 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000335
Bill Wendlingc94c5622012-03-31 11:15:43 +0000336 // mark which symbols can not be internalized
337 this->applyScopeRestrictions();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000338
Bill Wendlingc94c5622012-03-31 11:15:43 +0000339 Module* mergedModule = _linker.getModule();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000340
Bill Wendlingc94c5622012-03-31 11:15:43 +0000341 // if options were requested, set them
342 if ( !_codegenOptions.empty() )
343 cl::ParseCommandLineOptions(_codegenOptions.size(),
344 const_cast<char **>(&_codegenOptions[0]));
Devang Patela93ae712008-07-03 22:53:14 +0000345
Bill Wendlingc94c5622012-03-31 11:15:43 +0000346 // Instantiate the pass manager to organize the passes.
347 PassManager passes;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000348
Bill Wendlingc94c5622012-03-31 11:15:43 +0000349 // Start off with a verification pass.
350 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000351
Bill Wendlingc94c5622012-03-31 11:15:43 +0000352 // Add an appropriate TargetData instance for this module...
353 passes.add(new TargetData(*_target->getTargetData()));
Bill Wendling76b13ed2012-03-31 10:50:14 +0000354
Bill Wendlingc94c5622012-03-31 11:15:43 +0000355 PassManagerBuilder().populateLTOPassManager(passes, /*Internalize=*/ false,
356 !DisableInline);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000357
Bill Wendlingc94c5622012-03-31 11:15:43 +0000358 // Make sure everything is still good.
359 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000360
Bill Wendlingc94c5622012-03-31 11:15:43 +0000361 FunctionPassManager *codeGenPasses = new FunctionPassManager(mergedModule);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000362
Bill Wendlingc94c5622012-03-31 11:15:43 +0000363 codeGenPasses->add(new TargetData(*_target->getTargetData()));
Nick Kledzik77595fc2008-02-26 20:26:43 +0000364
Bill Wendlingc94c5622012-03-31 11:15:43 +0000365 formatted_raw_ostream Out(out);
Dan Gohmand4c45432010-09-01 14:20:41 +0000366
Bill Wendlingc94c5622012-03-31 11:15:43 +0000367 if (_target->addPassesToEmitFile(*codeGenPasses, Out,
368 TargetMachine::CGFT_ObjectFile,
369 CodeGenOpt::Aggressive)) {
370 errMsg = "target file type not supported";
371 return true;
372 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000373
Bill Wendlingc94c5622012-03-31 11:15:43 +0000374 // Run our queue of passes all at once now, efficiently.
375 passes.run(*mergedModule);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000376
Bill Wendlingc94c5622012-03-31 11:15:43 +0000377 // Run the code generator, and write assembly file
378 codeGenPasses->doInitialization();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000379
Bill Wendlingc94c5622012-03-31 11:15:43 +0000380 for (Module::iterator
381 it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it)
382 if (!it->isDeclaration())
383 codeGenPasses->run(*it);
Bill Wendling604a8182008-06-18 06:35:30 +0000384
Bill Wendlingc94c5622012-03-31 11:15:43 +0000385 codeGenPasses->doFinalization();
386 delete codeGenPasses;
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000387
Bill Wendlingc94c5622012-03-31 11:15:43 +0000388 return false; // success
Nick Kledzik77595fc2008-02-26 20:26:43 +0000389}
390
Bill Wendlingcaf71d42012-03-31 10:49:43 +0000391/// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
392/// LTO problems.
393void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) {
394 for (std::pair<StringRef, StringRef> o = getToken(options);
395 !o.first.empty(); o = getToken(o.second)) {
396 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add
397 // that.
398 if ( _codegenOptions.empty() )
399 _codegenOptions.push_back(strdup("libLTO"));
400 _codegenOptions.push_back(strdup(o.first.str().c_str()));
401 }
Nick Kledzik920ae982008-07-08 21:14:10 +0000402}