blob: d440b04fa675529d689d95fc94db64bc16ef659a [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
18
Nick Kledzik77595fc2008-02-26 20:26:43 +000019#include "llvm/Constants.h"
20#include "llvm/DerivedTypes.h"
Nick Lewycky8189d402009-06-17 06:52:10 +000021#include "llvm/Linker.h"
Owen Anderson0e7a5462009-07-02 00:31:14 +000022#include "llvm/LLVMContext.h"
Nick Lewycky8189d402009-06-17 06:52:10 +000023#include "llvm/Module.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000024#include "llvm/ModuleProvider.h"
Nick Lewycky8189d402009-06-17 06:52:10 +000025#include "llvm/PassManager.h"
26#include "llvm/ADT/StringExtras.h"
27#include "llvm/Analysis/Passes.h"
28#include "llvm/Analysis/LoopPass.h"
29#include "llvm/Analysis/Verifier.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000030#include "llvm/Bitcode/ReaderWriter.h"
Nick Lewycky8189d402009-06-17 06:52:10 +000031#include "llvm/CodeGen/FileWriters.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"
Nick Kledzik77595fc2008-02-26 20:26:43 +000034#include "llvm/Support/Mangler.h"
35#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar006a0342009-06-03 21:06:14 +000036#include "llvm/Support/StandardPasses.h"
37#include "llvm/Support/SystemUtils.h"
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +000038#include "llvm/System/Host.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000039#include "llvm/System/Signals.h"
Bill Wendling604a8182008-06-18 06:35:30 +000040#include "llvm/Target/SubtargetFeature.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000041#include "llvm/Target/TargetOptions.h"
Chris Lattner2deb58f2009-06-17 16:42:19 +000042#include "llvm/Target/TargetAsmInfo.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000043#include "llvm/Target/TargetData.h"
44#include "llvm/Target/TargetMachine.h"
Daniel Dunbarff9834a2009-07-16 02:41:19 +000045#include "llvm/Target/TargetRegistry.h"
Chris Lattner2deb58f2009-06-17 16:42:19 +000046#include "llvm/Target/TargetSelect.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000047#include "llvm/Transforms/IPO.h"
48#include "llvm/Transforms/Scalar.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000049#include "llvm/Config/config.h"
50
Nick Kledzik77595fc2008-02-26 20:26:43 +000051
Nick Lewycky8189d402009-06-17 06:52:10 +000052#include <cstdlib>
Nick Kledzik77595fc2008-02-26 20:26:43 +000053#include <fstream>
54#include <unistd.h>
Nick Kledzik77595fc2008-02-26 20:26:43 +000055#include <fcntl.h>
56
57
58using namespace llvm;
59
Nick Kledzik920ae982008-07-08 21:14:10 +000060static cl::opt<bool> DisableInline("disable-inlining",
61 cl::desc("Do not run the inliner pass"));
Nick Kledzik77595fc2008-02-26 20:26:43 +000062
63
64const char* LTOCodeGenerator::getVersionString()
65{
66#ifdef LLVM_VERSION_INFO
67 return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
68#else
69 return PACKAGE_NAME " version " PACKAGE_VERSION;
70#endif
71}
72
73
Owen Anderson0e7a5462009-07-02 00:31:14 +000074LTOCodeGenerator::LTOCodeGenerator()
75 : _context(getGlobalContext()),
Owen Anderson8b477ed2009-07-01 16:58:40 +000076 _linker("LinkTimeOptimizer", "ld-temp.o", _context), _target(NULL),
Nick Kledzik77595fc2008-02-26 20:26:43 +000077 _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false),
Nick Kledzikef194ed2008-02-27 22:25:36 +000078 _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
Nick Lewycky3e4c41a2009-08-03 07:16:42 +000079 _nativeObjectFile(NULL), _assemblerPath(NULL)
Nick Kledzik77595fc2008-02-26 20:26:43 +000080{
Nick Lewyckyd42b58b2009-07-26 22:16:39 +000081 InitializeAllTargets();
82 InitializeAllAsmPrinters();
Nick Kledzik77595fc2008-02-26 20:26:43 +000083}
84
85LTOCodeGenerator::~LTOCodeGenerator()
86{
Nick Kledzikef194ed2008-02-27 22:25:36 +000087 delete _target;
88 delete _nativeObjectFile;
Nick Kledzik77595fc2008-02-26 20:26:43 +000089}
90
91
92
93bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg)
94{
95 return _linker.LinkInModule(mod->getLLVVMModule(), &errMsg);
96}
97
98
99bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug, std::string& errMsg)
100{
101 switch (debug) {
102 case LTO_DEBUG_MODEL_NONE:
103 _emitDwarfDebugInfo = false;
104 return false;
105
106 case LTO_DEBUG_MODEL_DWARF:
107 _emitDwarfDebugInfo = true;
108 return false;
109 }
110 errMsg = "unknown debug format";
111 return true;
112}
113
114
115bool LTOCodeGenerator::setCodePICModel(lto_codegen_model model,
Evan Cheng855a1682009-06-26 06:57:16 +0000116 std::string& errMsg)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000117{
118 switch (model) {
119 case LTO_CODEGEN_PIC_MODEL_STATIC:
120 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
121 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
122 _codeModel = model;
123 return false;
124 }
125 errMsg = "unknown pic model";
126 return true;
127}
128
Nick Kledzikcbad5862009-06-04 00:28:45 +0000129void LTOCodeGenerator::setAssemblerPath(const char* path)
130{
131 if ( _assemblerPath )
132 delete _assemblerPath;
133 _assemblerPath = new sys::Path(path);
134}
135
Nick Kledzik77595fc2008-02-26 20:26:43 +0000136void LTOCodeGenerator::addMustPreserveSymbol(const char* sym)
137{
138 _mustPreserveSymbols[sym] = 1;
139}
140
141
142bool LTOCodeGenerator::writeMergedModules(const char* path, std::string& errMsg)
143{
144 if ( this->determineTarget(errMsg) )
145 return true;
146
147 // mark which symbols can not be internalized
148 this->applyScopeRestrictions();
149
150 // create output file
151 std::ofstream out(path, std::ios_base::out|std::ios::trunc|std::ios::binary);
152 if ( out.fail() ) {
153 errMsg = "could not open bitcode file for writing: ";
154 errMsg += path;
155 return true;
156 }
157
158 // write bitcode to it
159 WriteBitcodeToFile(_linker.getModule(), out);
160 if ( out.fail() ) {
161 errMsg = "could not write bitcode file: ";
162 errMsg += path;
163 return true;
164 }
165
166 return false;
167}
168
169
Nick Kledzikef194ed2008-02-27 22:25:36 +0000170const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000171{
Nick Kledzikef194ed2008-02-27 22:25:36 +0000172 // make unique temp .s file to put generated assembly code
Nick Kledzik77595fc2008-02-26 20:26:43 +0000173 sys::Path uniqueAsmPath("lto-llvm.s");
174 if ( uniqueAsmPath.createTemporaryFileOnDisk(true, &errMsg) )
175 return NULL;
176 sys::RemoveFileOnSignal(uniqueAsmPath);
177
178 // generate assembly code
Owen Andersoncb371882008-08-21 00:14:44 +0000179 bool genResult = false;
180 {
David Greene71847812009-07-14 20:18:05 +0000181 raw_fd_ostream asmFD(raw_fd_ostream(uniqueAsmPath.c_str(),
Dan Gohmana1bdced2009-07-15 17:29:42 +0000182 /*Binary=*/false, /*Force=*/true,
183 errMsg));
David Greene71847812009-07-14 20:18:05 +0000184 formatted_raw_ostream asmFile(asmFD);
Dan Gohmaned3e8b42008-08-21 15:33:45 +0000185 if (!errMsg.empty())
186 return NULL;
Owen Andersoncb371882008-08-21 00:14:44 +0000187 genResult = this->generateAssemblyCode(asmFile, errMsg);
188 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000189 if ( genResult ) {
190 if ( uniqueAsmPath.exists() )
191 uniqueAsmPath.eraseFromDisk();
192 return NULL;
193 }
194
Nick Kledzikef194ed2008-02-27 22:25:36 +0000195 // make unique temp .o file to put generated object file
Nick Kledzik77595fc2008-02-26 20:26:43 +0000196 sys::PathWithStatus uniqueObjPath("lto-llvm.o");
197 if ( uniqueObjPath.createTemporaryFileOnDisk(true, &errMsg) ) {
198 if ( uniqueAsmPath.exists() )
199 uniqueAsmPath.eraseFromDisk();
200 return NULL;
201 }
202 sys::RemoveFileOnSignal(uniqueObjPath);
203
204 // assemble the assembly code
Nick Kledzikef194ed2008-02-27 22:25:36 +0000205 const std::string& uniqueObjStr = uniqueObjPath.toString();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000206 bool asmResult = this->assemble(uniqueAsmPath.toString(),
Nick Kledzikef194ed2008-02-27 22:25:36 +0000207 uniqueObjStr, errMsg);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000208 if ( !asmResult ) {
Nick Kledzikef194ed2008-02-27 22:25:36 +0000209 // remove old buffer if compile() called twice
210 delete _nativeObjectFile;
211
Nick Kledzik77595fc2008-02-26 20:26:43 +0000212 // read .o file into memory buffer
Chris Lattner038112a2008-04-01 18:04:03 +0000213 _nativeObjectFile = MemoryBuffer::getFile(uniqueObjStr.c_str(),&errMsg);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000214 }
Nick Kledzikef194ed2008-02-27 22:25:36 +0000215
216 // remove temp files
Nick Kledzik77595fc2008-02-26 20:26:43 +0000217 uniqueAsmPath.eraseFromDisk();
218 uniqueObjPath.eraseFromDisk();
Nick Kledzikef194ed2008-02-27 22:25:36 +0000219
220 // return buffer, unless error
221 if ( _nativeObjectFile == NULL )
222 return NULL;
223 *length = _nativeObjectFile->getBufferSize();
224 return _nativeObjectFile->getBufferStart();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000225}
226
227
228bool LTOCodeGenerator::assemble(const std::string& asmPath,
229 const std::string& objPath, std::string& errMsg)
230{
Nick Kledzikcbad5862009-06-04 00:28:45 +0000231 sys::Path tool;
232 bool needsCompilerOptions = true;
233 if ( _assemblerPath ) {
234 tool = *_assemblerPath;
235 needsCompilerOptions = false;
Nick Lewycky195bea32009-04-30 15:24:09 +0000236 } else {
237 // find compiler driver
Nick Kledzikcbad5862009-06-04 00:28:45 +0000238 tool = sys::Program::FindProgramByName("gcc");
239 if ( tool.isEmpty() ) {
Nick Lewycky195bea32009-04-30 15:24:09 +0000240 errMsg = "can't locate gcc";
241 return true;
242 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000243 }
244
245 // build argument list
246 std::vector<const char*> args;
247 std::string targetTriple = _linker.getModule()->getTargetTriple();
Nick Kledzikcbad5862009-06-04 00:28:45 +0000248 args.push_back(tool.c_str());
Rafael Espindolaf403cd72009-06-09 21:14:25 +0000249 if ( targetTriple.find("darwin") != std::string::npos ) {
Nick Kledzikd8b47112009-06-04 19:14:08 +0000250 // darwin specific command line options
Devang Patelf75e7892008-11-04 23:13:50 +0000251 if (strncmp(targetTriple.c_str(), "i386-apple-", 11) == 0) {
Nick Kledzik77595fc2008-02-26 20:26:43 +0000252 args.push_back("-arch");
253 args.push_back("i386");
254 }
255 else if (strncmp(targetTriple.c_str(), "x86_64-apple-", 13) == 0) {
256 args.push_back("-arch");
257 args.push_back("x86_64");
258 }
259 else if (strncmp(targetTriple.c_str(), "powerpc-apple-", 14) == 0) {
260 args.push_back("-arch");
261 args.push_back("ppc");
262 }
263 else if (strncmp(targetTriple.c_str(), "powerpc64-apple-", 16) == 0) {
264 args.push_back("-arch");
265 args.push_back("ppc64");
266 }
Evan Cheng9f777c62009-04-01 18:54:56 +0000267 else if (strncmp(targetTriple.c_str(), "arm-apple-", 10) == 0) {
268 args.push_back("-arch");
269 args.push_back("arm");
270 }
271 else if ((strncmp(targetTriple.c_str(), "armv4t-apple-", 13) == 0) ||
272 (strncmp(targetTriple.c_str(), "thumbv4t-apple-", 15) == 0)) {
273 args.push_back("-arch");
274 args.push_back("armv4t");
275 }
276 else if ((strncmp(targetTriple.c_str(), "armv5-apple-", 12) == 0) ||
277 (strncmp(targetTriple.c_str(), "armv5e-apple-", 13) == 0) ||
278 (strncmp(targetTriple.c_str(), "thumbv5-apple-", 14) == 0) ||
279 (strncmp(targetTriple.c_str(), "thumbv5e-apple-", 15) == 0)) {
280 args.push_back("-arch");
281 args.push_back("armv5");
282 }
283 else if ((strncmp(targetTriple.c_str(), "armv6-apple-", 12) == 0) ||
284 (strncmp(targetTriple.c_str(), "thumbv6-apple-", 14) == 0)) {
285 args.push_back("-arch");
286 args.push_back("armv6");
287 }
Bob Wilson75d6ffd2009-06-22 18:01:28 +0000288 else if ((strncmp(targetTriple.c_str(), "armv7-apple-", 12) == 0) ||
289 (strncmp(targetTriple.c_str(), "thumbv7-apple-", 14) == 0)) {
290 args.push_back("-arch");
291 args.push_back("armv7");
292 }
Nick Kledzikd8b47112009-06-04 19:14:08 +0000293 // add -static to assembler command line when code model requires
294 if ( (_assemblerPath != NULL) && (_codeModel == LTO_CODEGEN_PIC_MODEL_STATIC) )
295 args.push_back("-static");
Nick Kledzik77595fc2008-02-26 20:26:43 +0000296 }
Nick Kledzikcbad5862009-06-04 00:28:45 +0000297 if ( needsCompilerOptions ) {
298 args.push_back("-c");
299 args.push_back("-x");
300 args.push_back("assembler");
301 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000302 args.push_back("-o");
303 args.push_back(objPath.c_str());
304 args.push_back(asmPath.c_str());
305 args.push_back(0);
306
307 // invoke assembler
Nick Kledzikcbad5862009-06-04 00:28:45 +0000308 if ( sys::Program::ExecuteAndWait(tool, &args[0], 0, 0, 0, 0, &errMsg) ) {
Nick Kledzik77595fc2008-02-26 20:26:43 +0000309 errMsg = "error in assembly";
310 return true;
311 }
312 return false; // success
313}
314
315
316
317bool LTOCodeGenerator::determineTarget(std::string& errMsg)
318{
319 if ( _target == NULL ) {
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000320 std::string Triple = _linker.getModule()->getTargetTriple();
321 if (Triple.empty())
322 Triple = sys::getHostTriple();
323
Nick Kledzik77595fc2008-02-26 20:26:43 +0000324 // create target machine from info for merged modules
325 Module* mergedModule = _linker.getModule();
Daniel Dunbar4bd03ab2009-08-03 04:20:57 +0000326 const Target *march = TargetRegistry::lookupTarget(Triple, errMsg);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000327 if ( march == NULL )
328 return true;
Bill Wendling604a8182008-06-18 06:35:30 +0000329
Nick Kledzikf5a1c35f12009-06-03 22:52:12 +0000330 // The relocation model is actually a static member of TargetMachine
331 // and needs to be set before the TargetMachine is instantiated.
332 switch( _codeModel ) {
333 case LTO_CODEGEN_PIC_MODEL_STATIC:
334 TargetMachine::setRelocationModel(Reloc::Static);
335 break;
336 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
337 TargetMachine::setRelocationModel(Reloc::PIC_);
338 break;
339 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
340 TargetMachine::setRelocationModel(Reloc::DynamicNoPIC);
341 break;
342 }
343
Bill Wendling604a8182008-06-18 06:35:30 +0000344 // construct LTModule, hand over ownership of module and target
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000345 std::string FeatureStr = getFeatureString(Triple.c_str());
346 _target = march->createTargetMachine(*mergedModule, Triple, FeatureStr);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000347 }
348 return false;
349}
350
351void LTOCodeGenerator::applyScopeRestrictions()
352{
353 if ( !_scopeRestrictionsDone ) {
354 Module* mergedModule = _linker.getModule();
355
356 // Start off with a verification pass.
357 PassManager passes;
358 passes.add(createVerifierPass());
359
360 // mark which symbols can not be internalized
361 if ( !_mustPreserveSymbols.empty() ) {
362 Mangler mangler(*mergedModule,
363 _target->getTargetAsmInfo()->getGlobalPrefix());
364 std::vector<const char*> mustPreserveList;
365 for (Module::iterator f = mergedModule->begin(),
366 e = mergedModule->end(); f != e; ++f) {
367 if ( !f->isDeclaration()
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000368 && _mustPreserveSymbols.count(mangler.getMangledName(f)) )
Daniel Dunbar3d5126f2009-07-22 21:33:09 +0000369 mustPreserveList.push_back(::strdup(f->getNameStr().c_str()));
Nick Kledzik77595fc2008-02-26 20:26:43 +0000370 }
371 for (Module::global_iterator v = mergedModule->global_begin(),
372 e = mergedModule->global_end(); v != e; ++v) {
373 if ( !v->isDeclaration()
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000374 && _mustPreserveSymbols.count(mangler.getMangledName(v)) )
Daniel Dunbar3d5126f2009-07-22 21:33:09 +0000375 mustPreserveList.push_back(::strdup(v->getNameStr().c_str()));
Nick Kledzik77595fc2008-02-26 20:26:43 +0000376 }
377 passes.add(createInternalizePass(mustPreserveList));
378 }
379 // apply scope restrictions
380 passes.run(*mergedModule);
381
382 _scopeRestrictionsDone = true;
383 }
384}
385
Nick Kledzik77595fc2008-02-26 20:26:43 +0000386/// Optimize merged modules using various IPO passes
David Greene71847812009-07-14 20:18:05 +0000387bool LTOCodeGenerator::generateAssemblyCode(formatted_raw_ostream& out,
Owen Andersoncb371882008-08-21 00:14:44 +0000388 std::string& errMsg)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000389{
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000390 if ( this->determineTarget(errMsg) )
Nick Kledzik77595fc2008-02-26 20:26:43 +0000391 return true;
392
393 // mark which symbols can not be internalized
394 this->applyScopeRestrictions();
395
396 Module* mergedModule = _linker.getModule();
397
398 // If target supports exception handling then enable it now.
399 if ( _target->getTargetAsmInfo()->doesSupportExceptionHandling() )
400 llvm::ExceptionHandling = true;
401
Nick Kledzik920ae982008-07-08 21:14:10 +0000402 // if options were requested, set them
403 if ( !_codegenOptions.empty() )
404 cl::ParseCommandLineOptions(_codegenOptions.size(),
405 (char**)&_codegenOptions[0]);
Devang Patela93ae712008-07-03 22:53:14 +0000406
Nick Kledzik77595fc2008-02-26 20:26:43 +0000407 // Instantiate the pass manager to organize the passes.
408 PassManager passes;
409
410 // Start off with a verification pass.
411 passes.add(createVerifierPass());
412
413 // Add an appropriate TargetData instance for this module...
414 passes.add(new TargetData(*_target->getTargetData()));
415
Daniel Dunbar006a0342009-06-03 21:06:14 +0000416 createStandardLTOPasses(&passes, /*Internalize=*/ false, !DisableInline,
Daniel Dunbar006a0342009-06-03 21:06:14 +0000417 /*VerifyEach=*/ false);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000418
419 // Make sure everything is still good.
420 passes.add(createVerifierPass());
421
422 FunctionPassManager* codeGenPasses =
423 new FunctionPassManager(new ExistingModuleProvider(mergedModule));
424
425 codeGenPasses->add(new TargetData(*_target->getTargetData()));
426
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000427 ObjectCodeEmitter* oce = NULL;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000428
429 switch (_target->addPassesToEmitFile(*codeGenPasses, out,
Bill Wendling98a366d2009-04-29 23:29:43 +0000430 TargetMachine::AssemblyFile,
Bill Wendlingb8cb0bb2009-04-29 23:40:42 +0000431 CodeGenOpt::Aggressive)) {
Nick Kledzik77595fc2008-02-26 20:26:43 +0000432 case FileModel::MachOFile:
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000433 oce = AddMachOWriter(*codeGenPasses, out, *_target);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000434 break;
435 case FileModel::ElfFile:
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000436 oce = AddELFWriter(*codeGenPasses, out, *_target);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000437 break;
438 case FileModel::AsmFile:
439 break;
440 case FileModel::Error:
441 case FileModel::None:
442 errMsg = "target file type not supported";
443 return true;
444 }
445
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000446 if (_target->addPassesToEmitFileFinish(*codeGenPasses, oce,
Bill Wendlingb8cb0bb2009-04-29 23:40:42 +0000447 CodeGenOpt::Aggressive)) {
Nick Kledzik77595fc2008-02-26 20:26:43 +0000448 errMsg = "target does not support generation of this file type";
449 return true;
450 }
451
452 // Run our queue of passes all at once now, efficiently.
453 passes.run(*mergedModule);
454
455 // Run the code generator, and write assembly file
456 codeGenPasses->doInitialization();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000457
Bill Wendling604a8182008-06-18 06:35:30 +0000458 for (Module::iterator
459 it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it)
460 if (!it->isDeclaration())
461 codeGenPasses->run(*it);
462
463 codeGenPasses->doFinalization();
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000464
465 out.flush();
466
Nick Kledzik77595fc2008-02-26 20:26:43 +0000467 return false; // success
468}
469
470
Nick Kledzik920ae982008-07-08 21:14:10 +0000471/// Optimize merged modules using various IPO passes
472void LTOCodeGenerator::setCodeGenDebugOptions(const char* options)
473{
474 std::string ops(options);
475 for (std::string o = getToken(ops); !o.empty(); o = getToken(ops)) {
476 // ParseCommandLineOptions() expects argv[0] to be program name.
477 // Lazily add that.
478 if ( _codegenOptions.empty() )
479 _codegenOptions.push_back("libLTO");
480 _codegenOptions.push_back(strdup(o.c_str()));
481 }
482}