blob: a5023fb2f4fa6b2431cd14ebafeb21b1c1c3c028 [file] [log] [blame]
Nick Kledzik6d886992008-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 Kledzik79d0a052008-02-27 22:25:36 +000015#include "LTOModule.h"
16#include "LTOCodeGenerator.h"
17
18
Nick Kledzik6d886992008-02-26 20:26:43 +000019#include "llvm/Constants.h"
20#include "llvm/DerivedTypes.h"
Nick Lewycky3d1feeb2009-06-17 06:52:10 +000021#include "llvm/Linker.h"
Owen Anderson353dadb2009-07-02 00:31:14 +000022#include "llvm/LLVMContext.h"
Nick Lewycky3d1feeb2009-06-17 06:52:10 +000023#include "llvm/Module.h"
Nick Kledzik6d886992008-02-26 20:26:43 +000024#include "llvm/ModuleProvider.h"
Nick Lewycky3d1feeb2009-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 Kledzik6d886992008-02-26 20:26:43 +000030#include "llvm/Bitcode/ReaderWriter.h"
Nick Lewycky3d1feeb2009-06-17 06:52:10 +000031#include "llvm/CodeGen/FileWriters.h"
Nick Kledzik4059fb12008-07-08 21:14:10 +000032#include "llvm/Support/CommandLine.h"
David Greene302008d2009-07-14 20:18:05 +000033#include "llvm/Support/FormattedStream.h"
Nick Kledzik6d886992008-02-26 20:26:43 +000034#include "llvm/Support/Mangler.h"
35#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar9068de52009-06-03 21:06:14 +000036#include "llvm/Support/StandardPasses.h"
37#include "llvm/Support/SystemUtils.h"
Nick Kledzik6d886992008-02-26 20:26:43 +000038#include "llvm/System/Signals.h"
Bill Wendling568e37c2008-06-18 06:35:30 +000039#include "llvm/Target/SubtargetFeature.h"
Nick Kledzik6d886992008-02-26 20:26:43 +000040#include "llvm/Target/TargetOptions.h"
Chris Lattner41788082009-06-17 16:42:19 +000041#include "llvm/Target/TargetAsmInfo.h"
Nick Kledzik6d886992008-02-26 20:26:43 +000042#include "llvm/Target/TargetData.h"
43#include "llvm/Target/TargetMachine.h"
Daniel Dunbar3b2bc2c2009-07-16 02:41:19 +000044#include "llvm/Target/TargetRegistry.h"
Chris Lattner41788082009-06-17 16:42:19 +000045#include "llvm/Target/TargetSelect.h"
Nick Kledzik6d886992008-02-26 20:26:43 +000046#include "llvm/Transforms/IPO.h"
47#include "llvm/Transforms/Scalar.h"
Nick Kledzik6d886992008-02-26 20:26:43 +000048#include "llvm/Config/config.h"
49
Nick Kledzik6d886992008-02-26 20:26:43 +000050
Nick Lewycky3d1feeb2009-06-17 06:52:10 +000051#include <cstdlib>
Nick Kledzik6d886992008-02-26 20:26:43 +000052#include <fstream>
53#include <unistd.h>
Nick Kledzik6d886992008-02-26 20:26:43 +000054#include <fcntl.h>
55
56
57using namespace llvm;
58
Nick Kledzik4059fb12008-07-08 21:14:10 +000059static cl::opt<bool> DisableInline("disable-inlining",
60 cl::desc("Do not run the inliner pass"));
Nick Kledzik6d886992008-02-26 20:26:43 +000061
62
63const char* LTOCodeGenerator::getVersionString()
64{
65#ifdef LLVM_VERSION_INFO
66 return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
67#else
68 return PACKAGE_NAME " version " PACKAGE_VERSION;
69#endif
70}
71
72
Owen Anderson353dadb2009-07-02 00:31:14 +000073LTOCodeGenerator::LTOCodeGenerator()
74 : _context(getGlobalContext()),
Owen Anderson25209b42009-07-01 16:58:40 +000075 _linker("LinkTimeOptimizer", "ld-temp.o", _context), _target(NULL),
Nick Kledzik6d886992008-02-26 20:26:43 +000076 _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false),
Nick Kledzik79d0a052008-02-27 22:25:36 +000077 _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
Nick Kledzik39d9b412009-06-04 00:28:45 +000078 _nativeObjectFile(NULL), _gccPath(NULL), _assemblerPath(NULL)
Nick Kledzik6d886992008-02-26 20:26:43 +000079{
Nick Lewyckybbd1d0f2009-07-26 22:16:39 +000080 InitializeAllTargets();
81 InitializeAllAsmPrinters();
Nick Kledzik6d886992008-02-26 20:26:43 +000082}
83
84LTOCodeGenerator::~LTOCodeGenerator()
85{
Nick Kledzik79d0a052008-02-27 22:25:36 +000086 delete _target;
87 delete _nativeObjectFile;
Nick Kledzik6d886992008-02-26 20:26:43 +000088}
89
90
91
92bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg)
93{
94 return _linker.LinkInModule(mod->getLLVVMModule(), &errMsg);
95}
96
97
98bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug, std::string& errMsg)
99{
100 switch (debug) {
101 case LTO_DEBUG_MODEL_NONE:
102 _emitDwarfDebugInfo = false;
103 return false;
104
105 case LTO_DEBUG_MODEL_DWARF:
106 _emitDwarfDebugInfo = true;
107 return false;
108 }
109 errMsg = "unknown debug format";
110 return true;
111}
112
113
114bool LTOCodeGenerator::setCodePICModel(lto_codegen_model model,
Evan Cheng521500b2009-06-26 06:57:16 +0000115 std::string& errMsg)
Nick Kledzik6d886992008-02-26 20:26:43 +0000116{
117 switch (model) {
118 case LTO_CODEGEN_PIC_MODEL_STATIC:
119 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
120 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
121 _codeModel = model;
122 return false;
123 }
124 errMsg = "unknown pic model";
125 return true;
126}
127
Nick Lewyckyef973202009-04-30 15:24:09 +0000128void LTOCodeGenerator::setGccPath(const char* path)
129{
130 if ( _gccPath )
131 delete _gccPath;
132 _gccPath = new sys::Path(path);
133}
134
Nick Kledzik39d9b412009-06-04 00:28:45 +0000135void LTOCodeGenerator::setAssemblerPath(const char* path)
136{
137 if ( _assemblerPath )
138 delete _assemblerPath;
139 _assemblerPath = new sys::Path(path);
140}
141
Nick Kledzik6d886992008-02-26 20:26:43 +0000142void LTOCodeGenerator::addMustPreserveSymbol(const char* sym)
143{
144 _mustPreserveSymbols[sym] = 1;
145}
146
147
148bool LTOCodeGenerator::writeMergedModules(const char* path, std::string& errMsg)
149{
150 if ( this->determineTarget(errMsg) )
151 return true;
152
153 // mark which symbols can not be internalized
154 this->applyScopeRestrictions();
155
156 // create output file
157 std::ofstream out(path, std::ios_base::out|std::ios::trunc|std::ios::binary);
158 if ( out.fail() ) {
159 errMsg = "could not open bitcode file for writing: ";
160 errMsg += path;
161 return true;
162 }
163
164 // write bitcode to it
165 WriteBitcodeToFile(_linker.getModule(), out);
166 if ( out.fail() ) {
167 errMsg = "could not write bitcode file: ";
168 errMsg += path;
169 return true;
170 }
171
172 return false;
173}
174
175
Nick Kledzik79d0a052008-02-27 22:25:36 +0000176const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg)
Nick Kledzik6d886992008-02-26 20:26:43 +0000177{
Nick Kledzik79d0a052008-02-27 22:25:36 +0000178 // make unique temp .s file to put generated assembly code
Nick Kledzik6d886992008-02-26 20:26:43 +0000179 sys::Path uniqueAsmPath("lto-llvm.s");
180 if ( uniqueAsmPath.createTemporaryFileOnDisk(true, &errMsg) )
181 return NULL;
182 sys::RemoveFileOnSignal(uniqueAsmPath);
183
184 // generate assembly code
Owen Anderson847b99b2008-08-21 00:14:44 +0000185 bool genResult = false;
186 {
David Greene302008d2009-07-14 20:18:05 +0000187 raw_fd_ostream asmFD(raw_fd_ostream(uniqueAsmPath.c_str(),
Dan Gohmandd7ca8e2009-07-15 17:29:42 +0000188 /*Binary=*/false, /*Force=*/true,
189 errMsg));
David Greene302008d2009-07-14 20:18:05 +0000190 formatted_raw_ostream asmFile(asmFD);
Dan Gohmande20df82008-08-21 15:33:45 +0000191 if (!errMsg.empty())
192 return NULL;
Owen Anderson847b99b2008-08-21 00:14:44 +0000193 genResult = this->generateAssemblyCode(asmFile, errMsg);
194 }
Nick Kledzik6d886992008-02-26 20:26:43 +0000195 if ( genResult ) {
196 if ( uniqueAsmPath.exists() )
197 uniqueAsmPath.eraseFromDisk();
198 return NULL;
199 }
200
Nick Kledzik79d0a052008-02-27 22:25:36 +0000201 // make unique temp .o file to put generated object file
Nick Kledzik6d886992008-02-26 20:26:43 +0000202 sys::PathWithStatus uniqueObjPath("lto-llvm.o");
203 if ( uniqueObjPath.createTemporaryFileOnDisk(true, &errMsg) ) {
204 if ( uniqueAsmPath.exists() )
205 uniqueAsmPath.eraseFromDisk();
206 return NULL;
207 }
208 sys::RemoveFileOnSignal(uniqueObjPath);
209
210 // assemble the assembly code
Nick Kledzik79d0a052008-02-27 22:25:36 +0000211 const std::string& uniqueObjStr = uniqueObjPath.toString();
Nick Kledzik6d886992008-02-26 20:26:43 +0000212 bool asmResult = this->assemble(uniqueAsmPath.toString(),
Nick Kledzik79d0a052008-02-27 22:25:36 +0000213 uniqueObjStr, errMsg);
Nick Kledzik6d886992008-02-26 20:26:43 +0000214 if ( !asmResult ) {
Nick Kledzik79d0a052008-02-27 22:25:36 +0000215 // remove old buffer if compile() called twice
216 delete _nativeObjectFile;
217
Nick Kledzik6d886992008-02-26 20:26:43 +0000218 // read .o file into memory buffer
Chris Lattnerfc003612008-04-01 18:04:03 +0000219 _nativeObjectFile = MemoryBuffer::getFile(uniqueObjStr.c_str(),&errMsg);
Nick Kledzik6d886992008-02-26 20:26:43 +0000220 }
Nick Kledzik79d0a052008-02-27 22:25:36 +0000221
222 // remove temp files
Nick Kledzik6d886992008-02-26 20:26:43 +0000223 uniqueAsmPath.eraseFromDisk();
224 uniqueObjPath.eraseFromDisk();
Nick Kledzik79d0a052008-02-27 22:25:36 +0000225
226 // return buffer, unless error
227 if ( _nativeObjectFile == NULL )
228 return NULL;
229 *length = _nativeObjectFile->getBufferSize();
230 return _nativeObjectFile->getBufferStart();
Nick Kledzik6d886992008-02-26 20:26:43 +0000231}
232
233
234bool LTOCodeGenerator::assemble(const std::string& asmPath,
235 const std::string& objPath, std::string& errMsg)
236{
Nick Kledzik39d9b412009-06-04 00:28:45 +0000237 sys::Path tool;
238 bool needsCompilerOptions = true;
239 if ( _assemblerPath ) {
240 tool = *_assemblerPath;
241 needsCompilerOptions = false;
242 }
243 else if ( _gccPath ) {
244 tool = *_gccPath;
Nick Lewyckyef973202009-04-30 15:24:09 +0000245 } else {
246 // find compiler driver
Nick Kledzik39d9b412009-06-04 00:28:45 +0000247 tool = sys::Program::FindProgramByName("gcc");
248 if ( tool.isEmpty() ) {
Nick Lewyckyef973202009-04-30 15:24:09 +0000249 errMsg = "can't locate gcc";
250 return true;
251 }
Nick Kledzik6d886992008-02-26 20:26:43 +0000252 }
253
254 // build argument list
255 std::vector<const char*> args;
256 std::string targetTriple = _linker.getModule()->getTargetTriple();
Nick Kledzik39d9b412009-06-04 00:28:45 +0000257 args.push_back(tool.c_str());
Rafael Espindola6c279402009-06-09 21:14:25 +0000258 if ( targetTriple.find("darwin") != std::string::npos ) {
Nick Kledzikfe15a722009-06-04 19:14:08 +0000259 // darwin specific command line options
Devang Patelaa6cd1f2008-11-04 23:13:50 +0000260 if (strncmp(targetTriple.c_str(), "i386-apple-", 11) == 0) {
Nick Kledzik6d886992008-02-26 20:26:43 +0000261 args.push_back("-arch");
262 args.push_back("i386");
263 }
264 else if (strncmp(targetTriple.c_str(), "x86_64-apple-", 13) == 0) {
265 args.push_back("-arch");
266 args.push_back("x86_64");
267 }
268 else if (strncmp(targetTriple.c_str(), "powerpc-apple-", 14) == 0) {
269 args.push_back("-arch");
270 args.push_back("ppc");
271 }
272 else if (strncmp(targetTriple.c_str(), "powerpc64-apple-", 16) == 0) {
273 args.push_back("-arch");
274 args.push_back("ppc64");
275 }
Evan Chengdc5b6d62009-04-01 18:54:56 +0000276 else if (strncmp(targetTriple.c_str(), "arm-apple-", 10) == 0) {
277 args.push_back("-arch");
278 args.push_back("arm");
279 }
280 else if ((strncmp(targetTriple.c_str(), "armv4t-apple-", 13) == 0) ||
281 (strncmp(targetTriple.c_str(), "thumbv4t-apple-", 15) == 0)) {
282 args.push_back("-arch");
283 args.push_back("armv4t");
284 }
285 else if ((strncmp(targetTriple.c_str(), "armv5-apple-", 12) == 0) ||
286 (strncmp(targetTriple.c_str(), "armv5e-apple-", 13) == 0) ||
287 (strncmp(targetTriple.c_str(), "thumbv5-apple-", 14) == 0) ||
288 (strncmp(targetTriple.c_str(), "thumbv5e-apple-", 15) == 0)) {
289 args.push_back("-arch");
290 args.push_back("armv5");
291 }
292 else if ((strncmp(targetTriple.c_str(), "armv6-apple-", 12) == 0) ||
293 (strncmp(targetTriple.c_str(), "thumbv6-apple-", 14) == 0)) {
294 args.push_back("-arch");
295 args.push_back("armv6");
296 }
Bob Wilson5b50a242009-06-22 18:01:28 +0000297 else if ((strncmp(targetTriple.c_str(), "armv7-apple-", 12) == 0) ||
298 (strncmp(targetTriple.c_str(), "thumbv7-apple-", 14) == 0)) {
299 args.push_back("-arch");
300 args.push_back("armv7");
301 }
Nick Kledzikfe15a722009-06-04 19:14:08 +0000302 // add -static to assembler command line when code model requires
303 if ( (_assemblerPath != NULL) && (_codeModel == LTO_CODEGEN_PIC_MODEL_STATIC) )
304 args.push_back("-static");
Nick Kledzik6d886992008-02-26 20:26:43 +0000305 }
Nick Kledzik39d9b412009-06-04 00:28:45 +0000306 if ( needsCompilerOptions ) {
307 args.push_back("-c");
308 args.push_back("-x");
309 args.push_back("assembler");
310 }
Nick Kledzik6d886992008-02-26 20:26:43 +0000311 args.push_back("-o");
312 args.push_back(objPath.c_str());
313 args.push_back(asmPath.c_str());
314 args.push_back(0);
315
316 // invoke assembler
Nick Kledzik39d9b412009-06-04 00:28:45 +0000317 if ( sys::Program::ExecuteAndWait(tool, &args[0], 0, 0, 0, 0, &errMsg) ) {
Nick Kledzik6d886992008-02-26 20:26:43 +0000318 errMsg = "error in assembly";
319 return true;
320 }
321 return false; // success
322}
323
324
325
326bool LTOCodeGenerator::determineTarget(std::string& errMsg)
327{
328 if ( _target == NULL ) {
329 // create target machine from info for merged modules
330 Module* mergedModule = _linker.getModule();
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000331 const Target *march =
Daniel Dunbar48224ee2009-07-26 02:12:58 +0000332 TargetRegistry::lookupTarget(mergedModule->getTargetTriple(),
333 /*FallbackToHost=*/true,
334 /*RequireJIT=*/false,
335 errMsg);
Nick Kledzik6d886992008-02-26 20:26:43 +0000336 if ( march == NULL )
337 return true;
Bill Wendling568e37c2008-06-18 06:35:30 +0000338
Nick Kledzik3e843222009-06-03 22:52:12 +0000339 // The relocation model is actually a static member of TargetMachine
340 // and needs to be set before the TargetMachine is instantiated.
341 switch( _codeModel ) {
342 case LTO_CODEGEN_PIC_MODEL_STATIC:
343 TargetMachine::setRelocationModel(Reloc::Static);
344 break;
345 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
346 TargetMachine::setRelocationModel(Reloc::PIC_);
347 break;
348 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
349 TargetMachine::setRelocationModel(Reloc::DynamicNoPIC);
350 break;
351 }
352
Bill Wendling568e37c2008-06-18 06:35:30 +0000353 // construct LTModule, hand over ownership of module and target
Bill Wendling0478d1a2008-06-18 21:39:02 +0000354 std::string FeatureStr =
355 getFeatureString(_linker.getModule()->getTargetTriple().c_str());
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000356 _target = march->createTargetMachine(*mergedModule, FeatureStr.c_str());
Nick Kledzik6d886992008-02-26 20:26:43 +0000357 }
358 return false;
359}
360
361void LTOCodeGenerator::applyScopeRestrictions()
362{
363 if ( !_scopeRestrictionsDone ) {
364 Module* mergedModule = _linker.getModule();
365
366 // Start off with a verification pass.
367 PassManager passes;
368 passes.add(createVerifierPass());
369
370 // mark which symbols can not be internalized
371 if ( !_mustPreserveSymbols.empty() ) {
372 Mangler mangler(*mergedModule,
373 _target->getTargetAsmInfo()->getGlobalPrefix());
374 std::vector<const char*> mustPreserveList;
375 for (Module::iterator f = mergedModule->begin(),
376 e = mergedModule->end(); f != e; ++f) {
377 if ( !f->isDeclaration()
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000378 && _mustPreserveSymbols.count(mangler.getMangledName(f)) )
Daniel Dunbare24a8e42009-07-22 21:33:09 +0000379 mustPreserveList.push_back(::strdup(f->getNameStr().c_str()));
Nick Kledzik6d886992008-02-26 20:26:43 +0000380 }
381 for (Module::global_iterator v = mergedModule->global_begin(),
382 e = mergedModule->global_end(); v != e; ++v) {
383 if ( !v->isDeclaration()
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000384 && _mustPreserveSymbols.count(mangler.getMangledName(v)) )
Daniel Dunbare24a8e42009-07-22 21:33:09 +0000385 mustPreserveList.push_back(::strdup(v->getNameStr().c_str()));
Nick Kledzik6d886992008-02-26 20:26:43 +0000386 }
387 passes.add(createInternalizePass(mustPreserveList));
388 }
389 // apply scope restrictions
390 passes.run(*mergedModule);
391
392 _scopeRestrictionsDone = true;
393 }
394}
395
Nick Kledzik6d886992008-02-26 20:26:43 +0000396/// Optimize merged modules using various IPO passes
David Greene302008d2009-07-14 20:18:05 +0000397bool LTOCodeGenerator::generateAssemblyCode(formatted_raw_ostream& out,
Owen Anderson847b99b2008-08-21 00:14:44 +0000398 std::string& errMsg)
Nick Kledzik6d886992008-02-26 20:26:43 +0000399{
Nick Lewyckybbd1d0f2009-07-26 22:16:39 +0000400 if ( this->determineTarget(errMsg) )
Nick Kledzik6d886992008-02-26 20:26:43 +0000401 return true;
402
403 // mark which symbols can not be internalized
404 this->applyScopeRestrictions();
405
406 Module* mergedModule = _linker.getModule();
407
408 // If target supports exception handling then enable it now.
409 if ( _target->getTargetAsmInfo()->doesSupportExceptionHandling() )
410 llvm::ExceptionHandling = true;
411
Nick Kledzik4059fb12008-07-08 21:14:10 +0000412 // if options were requested, set them
413 if ( !_codegenOptions.empty() )
414 cl::ParseCommandLineOptions(_codegenOptions.size(),
415 (char**)&_codegenOptions[0]);
Devang Patel6fb60262008-07-03 22:53:14 +0000416
Nick Kledzik6d886992008-02-26 20:26:43 +0000417 // Instantiate the pass manager to organize the passes.
418 PassManager passes;
419
420 // Start off with a verification pass.
421 passes.add(createVerifierPass());
422
423 // Add an appropriate TargetData instance for this module...
424 passes.add(new TargetData(*_target->getTargetData()));
425
Daniel Dunbar9068de52009-06-03 21:06:14 +0000426 createStandardLTOPasses(&passes, /*Internalize=*/ false, !DisableInline,
Daniel Dunbar9068de52009-06-03 21:06:14 +0000427 /*VerifyEach=*/ false);
Nick Kledzik6d886992008-02-26 20:26:43 +0000428
429 // Make sure everything is still good.
430 passes.add(createVerifierPass());
431
432 FunctionPassManager* codeGenPasses =
433 new FunctionPassManager(new ExistingModuleProvider(mergedModule));
434
435 codeGenPasses->add(new TargetData(*_target->getTargetData()));
436
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000437 ObjectCodeEmitter* oce = NULL;
Nick Kledzik6d886992008-02-26 20:26:43 +0000438
439 switch (_target->addPassesToEmitFile(*codeGenPasses, out,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000440 TargetMachine::AssemblyFile,
Bill Wendling2d1c1162009-04-29 23:40:42 +0000441 CodeGenOpt::Aggressive)) {
Nick Kledzik6d886992008-02-26 20:26:43 +0000442 case FileModel::MachOFile:
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000443 oce = AddMachOWriter(*codeGenPasses, out, *_target);
Nick Kledzik6d886992008-02-26 20:26:43 +0000444 break;
445 case FileModel::ElfFile:
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000446 oce = AddELFWriter(*codeGenPasses, out, *_target);
Nick Kledzik6d886992008-02-26 20:26:43 +0000447 break;
448 case FileModel::AsmFile:
449 break;
450 case FileModel::Error:
451 case FileModel::None:
452 errMsg = "target file type not supported";
453 return true;
454 }
455
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000456 if (_target->addPassesToEmitFileFinish(*codeGenPasses, oce,
Bill Wendling2d1c1162009-04-29 23:40:42 +0000457 CodeGenOpt::Aggressive)) {
Nick Kledzik6d886992008-02-26 20:26:43 +0000458 errMsg = "target does not support generation of this file type";
459 return true;
460 }
461
462 // Run our queue of passes all at once now, efficiently.
463 passes.run(*mergedModule);
464
465 // Run the code generator, and write assembly file
466 codeGenPasses->doInitialization();
Nick Kledzik6d886992008-02-26 20:26:43 +0000467
Bill Wendling568e37c2008-06-18 06:35:30 +0000468 for (Module::iterator
469 it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it)
470 if (!it->isDeclaration())
471 codeGenPasses->run(*it);
472
473 codeGenPasses->doFinalization();
Nick Lewyckybbd1d0f2009-07-26 22:16:39 +0000474
475 out.flush();
476
Nick Kledzik6d886992008-02-26 20:26:43 +0000477 return false; // success
478}
479
480
Nick Kledzik4059fb12008-07-08 21:14:10 +0000481/// Optimize merged modules using various IPO passes
482void LTOCodeGenerator::setCodeGenDebugOptions(const char* options)
483{
484 std::string ops(options);
485 for (std::string o = getToken(ops); !o.empty(); o = getToken(ops)) {
486 // ParseCommandLineOptions() expects argv[0] to be program name.
487 // Lazily add that.
488 if ( _codegenOptions.empty() )
489 _codegenOptions.push_back("libLTO");
490 _codegenOptions.push_back(strdup(o.c_str()));
491 }
492}