blob: 97827643e8573ef0446726e40656a5a49da1bcda [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.
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
Nick Kledzikef194ed2008-02-27 22:25:36 +000015#include "LTOModule.h"
16#include "LTOCodeGenerator.h"
17
Nick Kledzik77595fc2008-02-26 20:26:43 +000018#include "llvm/Constants.h"
19#include "llvm/DerivedTypes.h"
Nick Lewycky8189d402009-06-17 06:52:10 +000020#include "llvm/Linker.h"
Owen Anderson0e7a5462009-07-02 00:31:14 +000021#include "llvm/LLVMContext.h"
Nick Lewycky8189d402009-06-17 06:52:10 +000022#include "llvm/Module.h"
Nick Lewycky8189d402009-06-17 06:52:10 +000023#include "llvm/PassManager.h"
24#include "llvm/ADT/StringExtras.h"
Viktor Kutuzov51cdac02009-11-17 18:48:27 +000025#include "llvm/ADT/Triple.h"
Nick Lewycky8189d402009-06-17 06:52:10 +000026#include "llvm/Analysis/Passes.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000027#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattner5ef31a02010-03-12 18:44:54 +000028#include "llvm/MC/MCAsmInfo.h"
29#include "llvm/MC/MCContext.h"
30#include "llvm/Target/Mangler.h"
31#include "llvm/Target/SubtargetFeature.h"
32#include "llvm/Target/TargetOptions.h"
33#include "llvm/Target/TargetData.h"
34#include "llvm/Target/TargetMachine.h"
35#include "llvm/Target/TargetRegistry.h"
36#include "llvm/Target/TargetSelect.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"
Daniel Dunbar006a0342009-06-03 21:06:14 +000040#include "llvm/Support/StandardPasses.h"
41#include "llvm/Support/SystemUtils.h"
Dan Gohman9f36c4e2010-10-07 20:48:46 +000042#include "llvm/Support/ToolOutputFile.h"
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +000043#include "llvm/System/Host.h"
Chris Lattnerb683ea42009-08-23 21:36:09 +000044#include "llvm/System/Program.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000045#include "llvm/System/Signals.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000046#include "llvm/Config/config.h"
Nick Lewycky8189d402009-06-17 06:52:10 +000047#include <cstdlib>
Nick Kledzik77595fc2008-02-26 20:26:43 +000048#include <unistd.h>
Nick Kledzik77595fc2008-02-26 20:26:43 +000049#include <fcntl.h>
50
51
52using namespace llvm;
53
Nick Kledzik920ae982008-07-08 21:14:10 +000054static cl::opt<bool> DisableInline("disable-inlining",
55 cl::desc("Do not run the inliner pass"));
Nick Kledzik77595fc2008-02-26 20:26:43 +000056
57
58const char* LTOCodeGenerator::getVersionString()
59{
60#ifdef LLVM_VERSION_INFO
61 return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
62#else
63 return PACKAGE_NAME " version " PACKAGE_VERSION;
64#endif
65}
66
67
Owen Anderson0e7a5462009-07-02 00:31:14 +000068LTOCodeGenerator::LTOCodeGenerator()
69 : _context(getGlobalContext()),
Owen Anderson8b477ed2009-07-01 16:58:40 +000070 _linker("LinkTimeOptimizer", "ld-temp.o", _context), _target(NULL),
Nick Kledzik77595fc2008-02-26 20:26:43 +000071 _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false),
Nick Kledzikef194ed2008-02-27 22:25:36 +000072 _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
Nick Lewycky3e4c41a2009-08-03 07:16:42 +000073 _nativeObjectFile(NULL), _assemblerPath(NULL)
Nick Kledzik77595fc2008-02-26 20:26:43 +000074{
Nick Lewyckyd42b58b2009-07-26 22:16:39 +000075 InitializeAllTargets();
76 InitializeAllAsmPrinters();
Nick Kledzik77595fc2008-02-26 20:26:43 +000077}
78
79LTOCodeGenerator::~LTOCodeGenerator()
80{
Nick Kledzikef194ed2008-02-27 22:25:36 +000081 delete _target;
82 delete _nativeObjectFile;
Nick Kledzik77595fc2008-02-26 20:26:43 +000083}
84
85
86
87bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg)
88{
89 return _linker.LinkInModule(mod->getLLVVMModule(), &errMsg);
90}
91
92
93bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug, std::string& errMsg)
94{
95 switch (debug) {
96 case LTO_DEBUG_MODEL_NONE:
97 _emitDwarfDebugInfo = false;
98 return false;
99
100 case LTO_DEBUG_MODEL_DWARF:
101 _emitDwarfDebugInfo = true;
102 return false;
103 }
104 errMsg = "unknown debug format";
105 return true;
106}
107
108
109bool LTOCodeGenerator::setCodePICModel(lto_codegen_model model,
Evan Cheng855a1682009-06-26 06:57:16 +0000110 std::string& errMsg)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000111{
112 switch (model) {
113 case LTO_CODEGEN_PIC_MODEL_STATIC:
114 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
115 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
116 _codeModel = model;
117 return false;
118 }
119 errMsg = "unknown pic model";
120 return true;
121}
122
Rafael Espindola2d643ef2010-08-11 00:15:13 +0000123void LTOCodeGenerator::setCpu(const char* mCpu)
124{
125 _mCpu = mCpu;
126}
127
Nick Kledzikcbad5862009-06-04 00:28:45 +0000128void LTOCodeGenerator::setAssemblerPath(const char* path)
129{
130 if ( _assemblerPath )
131 delete _assemblerPath;
132 _assemblerPath = new sys::Path(path);
133}
134
Rafael Espindola98197e52010-08-10 18:55:09 +0000135void LTOCodeGenerator::setAssemblerArgs(const char** args, int nargs)
136{
137 for (int i = 0; i < nargs; ++i) {
138 const char *arg = args[i];
139 _assemblerArgs.push_back(arg);
140 }
141}
142
Nick Kledzik77595fc2008-02-26 20:26:43 +0000143void LTOCodeGenerator::addMustPreserveSymbol(const char* sym)
144{
145 _mustPreserveSymbols[sym] = 1;
146}
147
148
Chris Lattnerb515d752009-08-23 07:49:08 +0000149bool LTOCodeGenerator::writeMergedModules(const char *path,
150 std::string &errMsg) {
151 if (determineTarget(errMsg))
152 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000153
Chris Lattnerb515d752009-08-23 07:49:08 +0000154 // mark which symbols can not be internalized
155 applyScopeRestrictions();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000156
Chris Lattnerb515d752009-08-23 07:49:08 +0000157 // create output file
158 std::string ErrInfo;
Dan Gohmanf2914012010-08-20 16:59:15 +0000159 tool_output_file Out(path, ErrInfo,
160 raw_fd_ostream::F_Binary);
Chris Lattnerb515d752009-08-23 07:49:08 +0000161 if (!ErrInfo.empty()) {
162 errMsg = "could not open bitcode file for writing: ";
163 errMsg += path;
164 return true;
165 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000166
Chris Lattnerb515d752009-08-23 07:49:08 +0000167 // write bitcode to it
Dan Gohmand4c45432010-09-01 14:20:41 +0000168 WriteBitcodeToFile(_linker.getModule(), Out.os());
169 Out.os().close();
Dan Gohman4b7416b2010-05-27 20:19:47 +0000170
Dan Gohmand4c45432010-09-01 14:20:41 +0000171 if (Out.os().has_error()) {
Chris Lattnerb515d752009-08-23 07:49:08 +0000172 errMsg = "could not write bitcode file: ";
173 errMsg += path;
Dan Gohmand4c45432010-09-01 14:20:41 +0000174 Out.os().clear_error();
Chris Lattnerb515d752009-08-23 07:49:08 +0000175 return true;
176 }
177
Dan Gohmanf2914012010-08-20 16:59:15 +0000178 Out.keep();
Chris Lattnerb515d752009-08-23 07:49:08 +0000179 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000180}
181
182
Nick Kledzikef194ed2008-02-27 22:25:36 +0000183const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000184{
Nick Kledzikef194ed2008-02-27 22:25:36 +0000185 // make unique temp .s file to put generated assembly code
Nick Kledzik77595fc2008-02-26 20:26:43 +0000186 sys::Path uniqueAsmPath("lto-llvm.s");
187 if ( uniqueAsmPath.createTemporaryFileOnDisk(true, &errMsg) )
188 return NULL;
189 sys::RemoveFileOnSignal(uniqueAsmPath);
190
191 // generate assembly code
Owen Andersoncb371882008-08-21 00:14:44 +0000192 bool genResult = false;
193 {
Dan Gohmand4c45432010-09-01 14:20:41 +0000194 tool_output_file asmFile(uniqueAsmPath.c_str(), errMsg);
Dan Gohmaned3e8b42008-08-21 15:33:45 +0000195 if (!errMsg.empty())
196 return NULL;
Dan Gohmand4c45432010-09-01 14:20:41 +0000197 genResult = this->generateAssemblyCode(asmFile.os(), errMsg);
198 asmFile.os().close();
199 if (asmFile.os().has_error()) {
200 asmFile.os().clear_error();
Dan Gohmanf2914012010-08-20 16:59:15 +0000201 return NULL;
202 }
203 asmFile.keep();
Owen Andersoncb371882008-08-21 00:14:44 +0000204 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000205 if ( genResult ) {
Dan Gohmand27047f2010-05-27 20:51:54 +0000206 uniqueAsmPath.eraseFromDisk();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000207 return NULL;
208 }
209
Nick Kledzikef194ed2008-02-27 22:25:36 +0000210 // make unique temp .o file to put generated object file
Nick Kledzik77595fc2008-02-26 20:26:43 +0000211 sys::PathWithStatus uniqueObjPath("lto-llvm.o");
212 if ( uniqueObjPath.createTemporaryFileOnDisk(true, &errMsg) ) {
Dan Gohmand27047f2010-05-27 20:51:54 +0000213 uniqueAsmPath.eraseFromDisk();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000214 return NULL;
215 }
216 sys::RemoveFileOnSignal(uniqueObjPath);
217
218 // assemble the assembly code
Chris Lattner74382b72009-08-23 22:45:37 +0000219 const std::string& uniqueObjStr = uniqueObjPath.str();
220 bool asmResult = this->assemble(uniqueAsmPath.str(), uniqueObjStr, errMsg);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000221 if ( !asmResult ) {
Nick Kledzikef194ed2008-02-27 22:25:36 +0000222 // remove old buffer if compile() called twice
223 delete _nativeObjectFile;
224
Nick Kledzik77595fc2008-02-26 20:26:43 +0000225 // read .o file into memory buffer
Chris Lattner038112a2008-04-01 18:04:03 +0000226 _nativeObjectFile = MemoryBuffer::getFile(uniqueObjStr.c_str(),&errMsg);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000227 }
Nick Kledzikef194ed2008-02-27 22:25:36 +0000228
229 // remove temp files
Nick Kledzik77595fc2008-02-26 20:26:43 +0000230 uniqueAsmPath.eraseFromDisk();
231 uniqueObjPath.eraseFromDisk();
Nick Kledzikef194ed2008-02-27 22:25:36 +0000232
233 // return buffer, unless error
234 if ( _nativeObjectFile == NULL )
235 return NULL;
236 *length = _nativeObjectFile->getBufferSize();
237 return _nativeObjectFile->getBufferStart();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000238}
239
240
241bool LTOCodeGenerator::assemble(const std::string& asmPath,
242 const std::string& objPath, std::string& errMsg)
243{
Nick Kledzikcbad5862009-06-04 00:28:45 +0000244 sys::Path tool;
245 bool needsCompilerOptions = true;
246 if ( _assemblerPath ) {
247 tool = *_assemblerPath;
248 needsCompilerOptions = false;
Nick Lewycky195bea32009-04-30 15:24:09 +0000249 } else {
250 // find compiler driver
Nick Kledzikcbad5862009-06-04 00:28:45 +0000251 tool = sys::Program::FindProgramByName("gcc");
252 if ( tool.isEmpty() ) {
Nick Lewycky195bea32009-04-30 15:24:09 +0000253 errMsg = "can't locate gcc";
254 return true;
255 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000256 }
257
258 // build argument list
259 std::vector<const char*> args;
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000260 llvm::Triple targetTriple(_linker.getModule()->getTargetTriple());
261 const char *arch = targetTriple.getArchNameForAssembler();
262
Nick Kledzikcbad5862009-06-04 00:28:45 +0000263 args.push_back(tool.c_str());
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000264
265 if (targetTriple.getOS() == Triple::Darwin) {
Nick Kledzikd8b47112009-06-04 19:14:08 +0000266 // darwin specific command line options
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000267 if (arch != NULL) {
Nick Kledzik77595fc2008-02-26 20:26:43 +0000268 args.push_back("-arch");
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000269 args.push_back(arch);
Bob Wilson75d6ffd2009-06-22 18:01:28 +0000270 }
Nick Kledzikd8b47112009-06-04 19:14:08 +0000271 // add -static to assembler command line when code model requires
Chris Lattner5ef31a02010-03-12 18:44:54 +0000272 if ( (_assemblerPath != NULL) &&
273 (_codeModel == LTO_CODEGEN_PIC_MODEL_STATIC) )
Nick Kledzikd8b47112009-06-04 19:14:08 +0000274 args.push_back("-static");
Nick Kledzik77595fc2008-02-26 20:26:43 +0000275 }
Nick Kledzikcbad5862009-06-04 00:28:45 +0000276 if ( needsCompilerOptions ) {
277 args.push_back("-c");
278 args.push_back("-x");
279 args.push_back("assembler");
Rafael Espindola98197e52010-08-10 18:55:09 +0000280 } else {
281 for (std::vector<std::string>::iterator I = _assemblerArgs.begin(),
282 E = _assemblerArgs.end(); I != E; ++I) {
283 args.push_back(I->c_str());
284 }
Nick Kledzikcbad5862009-06-04 00:28:45 +0000285 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000286 args.push_back("-o");
287 args.push_back(objPath.c_str());
288 args.push_back(asmPath.c_str());
289 args.push_back(0);
290
291 // invoke assembler
Nick Kledzikcbad5862009-06-04 00:28:45 +0000292 if ( sys::Program::ExecuteAndWait(tool, &args[0], 0, 0, 0, 0, &errMsg) ) {
Nick Kledzik77595fc2008-02-26 20:26:43 +0000293 errMsg = "error in assembly";
294 return true;
295 }
296 return false; // success
297}
298
299
300
301bool LTOCodeGenerator::determineTarget(std::string& errMsg)
302{
303 if ( _target == NULL ) {
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000304 std::string Triple = _linker.getModule()->getTargetTriple();
305 if (Triple.empty())
306 Triple = sys::getHostTriple();
307
Nick Kledzik77595fc2008-02-26 20:26:43 +0000308 // create target machine from info for merged modules
Daniel Dunbar4bd03ab2009-08-03 04:20:57 +0000309 const Target *march = TargetRegistry::lookupTarget(Triple, errMsg);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000310 if ( march == NULL )
311 return true;
Bill Wendling604a8182008-06-18 06:35:30 +0000312
Nick Kledzikf5a1c35f12009-06-03 22:52:12 +0000313 // The relocation model is actually a static member of TargetMachine
314 // and needs to be set before the TargetMachine is instantiated.
315 switch( _codeModel ) {
316 case LTO_CODEGEN_PIC_MODEL_STATIC:
317 TargetMachine::setRelocationModel(Reloc::Static);
318 break;
319 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
320 TargetMachine::setRelocationModel(Reloc::PIC_);
321 break;
322 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
323 TargetMachine::setRelocationModel(Reloc::DynamicNoPIC);
324 break;
325 }
326
Bill Wendling604a8182008-06-18 06:35:30 +0000327 // construct LTModule, hand over ownership of module and target
Bill Wendling81043ee2010-05-11 00:30:02 +0000328 SubtargetFeatures Features;
Rafael Espindola2d643ef2010-08-11 00:15:13 +0000329 Features.getDefaultSubtargetFeatures(_mCpu, llvm::Triple(Triple));
Bill Wendling81043ee2010-05-11 00:30:02 +0000330 std::string FeatureStr = Features.getString();
Viktor Kutuzov308f6632009-11-25 22:44:18 +0000331 _target = march->createTargetMachine(Triple, FeatureStr);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000332 }
333 return false;
334}
335
Chris Lattner5ef31a02010-03-12 18:44:54 +0000336void LTOCodeGenerator::applyScopeRestrictions() {
337 if (_scopeRestrictionsDone) return;
338 Module *mergedModule = _linker.getModule();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000339
Chris Lattner5ef31a02010-03-12 18:44:54 +0000340 // Start off with a verification pass.
341 PassManager passes;
342 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000343
Chris Lattner5ef31a02010-03-12 18:44:54 +0000344 // mark which symbols can not be internalized
345 if (!_mustPreserveSymbols.empty()) {
346 MCContext Context(*_target->getMCAsmInfo());
Chris Lattnerb87c3052010-03-12 20:47:28 +0000347 Mangler mangler(Context, *_target->getTargetData());
Chris Lattner5ef31a02010-03-12 18:44:54 +0000348 std::vector<const char*> mustPreserveList;
349 for (Module::iterator f = mergedModule->begin(),
350 e = mergedModule->end(); f != e; ++f) {
351 if (!f->isDeclaration() &&
352 _mustPreserveSymbols.count(mangler.getNameWithPrefix(f)))
353 mustPreserveList.push_back(::strdup(f->getNameStr().c_str()));
Nick Kledzik77595fc2008-02-26 20:26:43 +0000354 }
Chris Lattner5ef31a02010-03-12 18:44:54 +0000355 for (Module::global_iterator v = mergedModule->global_begin(),
356 e = mergedModule->global_end(); v != e; ++v) {
Bill Wendlingc3d0e0c2010-04-27 00:55:25 +0000357 if (!v->isDeclaration() &&
Chris Lattner5ef31a02010-03-12 18:44:54 +0000358 _mustPreserveSymbols.count(mangler.getNameWithPrefix(v)))
359 mustPreserveList.push_back(::strdup(v->getNameStr().c_str()));
360 }
361 passes.add(createInternalizePass(mustPreserveList));
362 }
363
364 // apply scope restrictions
365 passes.run(*mergedModule);
366
367 _scopeRestrictionsDone = true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000368}
369
Nick Kledzik77595fc2008-02-26 20:26:43 +0000370/// Optimize merged modules using various IPO passes
Dan Gohmand4c45432010-09-01 14:20:41 +0000371bool LTOCodeGenerator::generateAssemblyCode(raw_ostream& out,
Owen Andersoncb371882008-08-21 00:14:44 +0000372 std::string& errMsg)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000373{
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000374 if ( this->determineTarget(errMsg) )
Nick Kledzik77595fc2008-02-26 20:26:43 +0000375 return true;
376
377 // mark which symbols can not be internalized
378 this->applyScopeRestrictions();
379
380 Module* mergedModule = _linker.getModule();
381
Nick Kledzik920ae982008-07-08 21:14:10 +0000382 // if options were requested, set them
383 if ( !_codegenOptions.empty() )
384 cl::ParseCommandLineOptions(_codegenOptions.size(),
Dan Gohman43bc70e2010-04-17 17:44:03 +0000385 const_cast<char **>(&_codegenOptions[0]));
Devang Patela93ae712008-07-03 22:53:14 +0000386
Nick Kledzik77595fc2008-02-26 20:26:43 +0000387 // Instantiate the pass manager to organize the passes.
388 PassManager passes;
389
390 // Start off with a verification pass.
391 passes.add(createVerifierPass());
392
393 // Add an appropriate TargetData instance for this module...
394 passes.add(new TargetData(*_target->getTargetData()));
395
Daniel Dunbar006a0342009-06-03 21:06:14 +0000396 createStandardLTOPasses(&passes, /*Internalize=*/ false, !DisableInline,
Daniel Dunbar006a0342009-06-03 21:06:14 +0000397 /*VerifyEach=*/ false);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000398
399 // Make sure everything is still good.
400 passes.add(createVerifierPass());
401
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000402 FunctionPassManager* codeGenPasses = new FunctionPassManager(mergedModule);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000403
404 codeGenPasses->add(new TargetData(*_target->getTargetData()));
405
Dan Gohmand4c45432010-09-01 14:20:41 +0000406 formatted_raw_ostream Out(out);
407
408 if (_target->addPassesToEmitFile(*codeGenPasses, Out,
Chris Lattner5669e302010-02-03 05:55:08 +0000409 TargetMachine::CGFT_AssemblyFile,
410 CodeGenOpt::Aggressive)) {
411 errMsg = "target file type not supported";
412 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000413 }
414
Nick Kledzik77595fc2008-02-26 20:26:43 +0000415 // Run our queue of passes all at once now, efficiently.
416 passes.run(*mergedModule);
417
418 // Run the code generator, and write assembly file
419 codeGenPasses->doInitialization();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000420
Bill Wendling604a8182008-06-18 06:35:30 +0000421 for (Module::iterator
422 it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it)
423 if (!it->isDeclaration())
424 codeGenPasses->run(*it);
425
426 codeGenPasses->doFinalization();
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000427
Nick Kledzik77595fc2008-02-26 20:26:43 +0000428 return false; // success
429}
430
431
Nick Kledzik920ae982008-07-08 21:14:10 +0000432/// Optimize merged modules using various IPO passes
433void LTOCodeGenerator::setCodeGenDebugOptions(const char* options)
434{
Benjamin Kramerd4f19592010-01-11 18:03:24 +0000435 for (std::pair<StringRef, StringRef> o = getToken(options);
436 !o.first.empty(); o = getToken(o.second)) {
Nick Kledzik920ae982008-07-08 21:14:10 +0000437 // ParseCommandLineOptions() expects argv[0] to be program name.
438 // Lazily add that.
439 if ( _codegenOptions.empty() )
440 _codegenOptions.push_back("libLTO");
Benjamin Kramerd4f19592010-01-11 18:03:24 +0000441 _codegenOptions.push_back(strdup(o.first.str().c_str()));
Nick Kledzik920ae982008-07-08 21:14:10 +0000442 }
443}