blob: ac7af13cb8018a57e30a85b7cd524bc7005428fe [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"
Daniel Dunbar4baa5fe2009-08-03 04:03:51 +000038#include "llvm/System/Host.h"
Nick Kledzik6d886992008-02-26 20:26:43 +000039#include "llvm/System/Signals.h"
Bill Wendling568e37c2008-06-18 06:35:30 +000040#include "llvm/Target/SubtargetFeature.h"
Nick Kledzik6d886992008-02-26 20:26:43 +000041#include "llvm/Target/TargetOptions.h"
Chris Lattner41788082009-06-17 16:42:19 +000042#include "llvm/Target/TargetAsmInfo.h"
Nick Kledzik6d886992008-02-26 20:26:43 +000043#include "llvm/Target/TargetData.h"
44#include "llvm/Target/TargetMachine.h"
Daniel Dunbar3b2bc2c2009-07-16 02:41:19 +000045#include "llvm/Target/TargetRegistry.h"
Chris Lattner41788082009-06-17 16:42:19 +000046#include "llvm/Target/TargetSelect.h"
Nick Kledzik6d886992008-02-26 20:26:43 +000047#include "llvm/Transforms/IPO.h"
48#include "llvm/Transforms/Scalar.h"
Nick Kledzik6d886992008-02-26 20:26:43 +000049#include "llvm/Config/config.h"
50
Nick Kledzik6d886992008-02-26 20:26:43 +000051
Nick Lewycky3d1feeb2009-06-17 06:52:10 +000052#include <cstdlib>
Nick Kledzik6d886992008-02-26 20:26:43 +000053#include <fstream>
54#include <unistd.h>
Nick Kledzik6d886992008-02-26 20:26:43 +000055#include <fcntl.h>
56
57
58using namespace llvm;
59
Nick Kledzik4059fb12008-07-08 21:14:10 +000060static cl::opt<bool> DisableInline("disable-inlining",
61 cl::desc("Do not run the inliner pass"));
Nick Kledzik6d886992008-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 Anderson353dadb2009-07-02 00:31:14 +000074LTOCodeGenerator::LTOCodeGenerator()
75 : _context(getGlobalContext()),
Owen Anderson25209b42009-07-01 16:58:40 +000076 _linker("LinkTimeOptimizer", "ld-temp.o", _context), _target(NULL),
Nick Kledzik6d886992008-02-26 20:26:43 +000077 _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false),
Nick Kledzik79d0a052008-02-27 22:25:36 +000078 _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
Nick Kledzik39d9b412009-06-04 00:28:45 +000079 _nativeObjectFile(NULL), _gccPath(NULL), _assemblerPath(NULL)
Nick Kledzik6d886992008-02-26 20:26:43 +000080{
Nick Lewyckybbd1d0f2009-07-26 22:16:39 +000081 InitializeAllTargets();
82 InitializeAllAsmPrinters();
Nick Kledzik6d886992008-02-26 20:26:43 +000083}
84
85LTOCodeGenerator::~LTOCodeGenerator()
86{
Nick Kledzik79d0a052008-02-27 22:25:36 +000087 delete _target;
88 delete _nativeObjectFile;
Nick Kledzik6d886992008-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 Cheng521500b2009-06-26 06:57:16 +0000116 std::string& errMsg)
Nick Kledzik6d886992008-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 Lewyckyef973202009-04-30 15:24:09 +0000129void LTOCodeGenerator::setGccPath(const char* path)
130{
131 if ( _gccPath )
132 delete _gccPath;
133 _gccPath = new sys::Path(path);
134}
135
Nick Kledzik39d9b412009-06-04 00:28:45 +0000136void LTOCodeGenerator::setAssemblerPath(const char* path)
137{
138 if ( _assemblerPath )
139 delete _assemblerPath;
140 _assemblerPath = new sys::Path(path);
141}
142
Nick Kledzik6d886992008-02-26 20:26:43 +0000143void LTOCodeGenerator::addMustPreserveSymbol(const char* sym)
144{
145 _mustPreserveSymbols[sym] = 1;
146}
147
148
149bool LTOCodeGenerator::writeMergedModules(const char* path, std::string& errMsg)
150{
151 if ( this->determineTarget(errMsg) )
152 return true;
153
154 // mark which symbols can not be internalized
155 this->applyScopeRestrictions();
156
157 // create output file
158 std::ofstream out(path, std::ios_base::out|std::ios::trunc|std::ios::binary);
159 if ( out.fail() ) {
160 errMsg = "could not open bitcode file for writing: ";
161 errMsg += path;
162 return true;
163 }
164
165 // write bitcode to it
166 WriteBitcodeToFile(_linker.getModule(), out);
167 if ( out.fail() ) {
168 errMsg = "could not write bitcode file: ";
169 errMsg += path;
170 return true;
171 }
172
173 return false;
174}
175
176
Nick Kledzik79d0a052008-02-27 22:25:36 +0000177const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg)
Nick Kledzik6d886992008-02-26 20:26:43 +0000178{
Nick Kledzik79d0a052008-02-27 22:25:36 +0000179 // make unique temp .s file to put generated assembly code
Nick Kledzik6d886992008-02-26 20:26:43 +0000180 sys::Path uniqueAsmPath("lto-llvm.s");
181 if ( uniqueAsmPath.createTemporaryFileOnDisk(true, &errMsg) )
182 return NULL;
183 sys::RemoveFileOnSignal(uniqueAsmPath);
184
185 // generate assembly code
Owen Anderson847b99b2008-08-21 00:14:44 +0000186 bool genResult = false;
187 {
David Greene302008d2009-07-14 20:18:05 +0000188 raw_fd_ostream asmFD(raw_fd_ostream(uniqueAsmPath.c_str(),
Dan Gohmandd7ca8e2009-07-15 17:29:42 +0000189 /*Binary=*/false, /*Force=*/true,
190 errMsg));
David Greene302008d2009-07-14 20:18:05 +0000191 formatted_raw_ostream asmFile(asmFD);
Dan Gohmande20df82008-08-21 15:33:45 +0000192 if (!errMsg.empty())
193 return NULL;
Owen Anderson847b99b2008-08-21 00:14:44 +0000194 genResult = this->generateAssemblyCode(asmFile, errMsg);
195 }
Nick Kledzik6d886992008-02-26 20:26:43 +0000196 if ( genResult ) {
197 if ( uniqueAsmPath.exists() )
198 uniqueAsmPath.eraseFromDisk();
199 return NULL;
200 }
201
Nick Kledzik79d0a052008-02-27 22:25:36 +0000202 // make unique temp .o file to put generated object file
Nick Kledzik6d886992008-02-26 20:26:43 +0000203 sys::PathWithStatus uniqueObjPath("lto-llvm.o");
204 if ( uniqueObjPath.createTemporaryFileOnDisk(true, &errMsg) ) {
205 if ( uniqueAsmPath.exists() )
206 uniqueAsmPath.eraseFromDisk();
207 return NULL;
208 }
209 sys::RemoveFileOnSignal(uniqueObjPath);
210
211 // assemble the assembly code
Nick Kledzik79d0a052008-02-27 22:25:36 +0000212 const std::string& uniqueObjStr = uniqueObjPath.toString();
Nick Kledzik6d886992008-02-26 20:26:43 +0000213 bool asmResult = this->assemble(uniqueAsmPath.toString(),
Nick Kledzik79d0a052008-02-27 22:25:36 +0000214 uniqueObjStr, errMsg);
Nick Kledzik6d886992008-02-26 20:26:43 +0000215 if ( !asmResult ) {
Nick Kledzik79d0a052008-02-27 22:25:36 +0000216 // remove old buffer if compile() called twice
217 delete _nativeObjectFile;
218
Nick Kledzik6d886992008-02-26 20:26:43 +0000219 // read .o file into memory buffer
Chris Lattnerfc003612008-04-01 18:04:03 +0000220 _nativeObjectFile = MemoryBuffer::getFile(uniqueObjStr.c_str(),&errMsg);
Nick Kledzik6d886992008-02-26 20:26:43 +0000221 }
Nick Kledzik79d0a052008-02-27 22:25:36 +0000222
223 // remove temp files
Nick Kledzik6d886992008-02-26 20:26:43 +0000224 uniqueAsmPath.eraseFromDisk();
225 uniqueObjPath.eraseFromDisk();
Nick Kledzik79d0a052008-02-27 22:25:36 +0000226
227 // return buffer, unless error
228 if ( _nativeObjectFile == NULL )
229 return NULL;
230 *length = _nativeObjectFile->getBufferSize();
231 return _nativeObjectFile->getBufferStart();
Nick Kledzik6d886992008-02-26 20:26:43 +0000232}
233
234
235bool LTOCodeGenerator::assemble(const std::string& asmPath,
236 const std::string& objPath, std::string& errMsg)
237{
Nick Kledzik39d9b412009-06-04 00:28:45 +0000238 sys::Path tool;
239 bool needsCompilerOptions = true;
240 if ( _assemblerPath ) {
241 tool = *_assemblerPath;
242 needsCompilerOptions = false;
243 }
244 else if ( _gccPath ) {
245 tool = *_gccPath;
Nick Lewyckyef973202009-04-30 15:24:09 +0000246 } else {
247 // find compiler driver
Nick Kledzik39d9b412009-06-04 00:28:45 +0000248 tool = sys::Program::FindProgramByName("gcc");
249 if ( tool.isEmpty() ) {
Nick Lewyckyef973202009-04-30 15:24:09 +0000250 errMsg = "can't locate gcc";
251 return true;
252 }
Nick Kledzik6d886992008-02-26 20:26:43 +0000253 }
254
255 // build argument list
256 std::vector<const char*> args;
257 std::string targetTriple = _linker.getModule()->getTargetTriple();
Nick Kledzik39d9b412009-06-04 00:28:45 +0000258 args.push_back(tool.c_str());
Rafael Espindola6c279402009-06-09 21:14:25 +0000259 if ( targetTriple.find("darwin") != std::string::npos ) {
Nick Kledzikfe15a722009-06-04 19:14:08 +0000260 // darwin specific command line options
Devang Patelaa6cd1f2008-11-04 23:13:50 +0000261 if (strncmp(targetTriple.c_str(), "i386-apple-", 11) == 0) {
Nick Kledzik6d886992008-02-26 20:26:43 +0000262 args.push_back("-arch");
263 args.push_back("i386");
264 }
265 else if (strncmp(targetTriple.c_str(), "x86_64-apple-", 13) == 0) {
266 args.push_back("-arch");
267 args.push_back("x86_64");
268 }
269 else if (strncmp(targetTriple.c_str(), "powerpc-apple-", 14) == 0) {
270 args.push_back("-arch");
271 args.push_back("ppc");
272 }
273 else if (strncmp(targetTriple.c_str(), "powerpc64-apple-", 16) == 0) {
274 args.push_back("-arch");
275 args.push_back("ppc64");
276 }
Evan Chengdc5b6d62009-04-01 18:54:56 +0000277 else if (strncmp(targetTriple.c_str(), "arm-apple-", 10) == 0) {
278 args.push_back("-arch");
279 args.push_back("arm");
280 }
281 else if ((strncmp(targetTriple.c_str(), "armv4t-apple-", 13) == 0) ||
282 (strncmp(targetTriple.c_str(), "thumbv4t-apple-", 15) == 0)) {
283 args.push_back("-arch");
284 args.push_back("armv4t");
285 }
286 else if ((strncmp(targetTriple.c_str(), "armv5-apple-", 12) == 0) ||
287 (strncmp(targetTriple.c_str(), "armv5e-apple-", 13) == 0) ||
288 (strncmp(targetTriple.c_str(), "thumbv5-apple-", 14) == 0) ||
289 (strncmp(targetTriple.c_str(), "thumbv5e-apple-", 15) == 0)) {
290 args.push_back("-arch");
291 args.push_back("armv5");
292 }
293 else if ((strncmp(targetTriple.c_str(), "armv6-apple-", 12) == 0) ||
294 (strncmp(targetTriple.c_str(), "thumbv6-apple-", 14) == 0)) {
295 args.push_back("-arch");
296 args.push_back("armv6");
297 }
Bob Wilson5b50a242009-06-22 18:01:28 +0000298 else if ((strncmp(targetTriple.c_str(), "armv7-apple-", 12) == 0) ||
299 (strncmp(targetTriple.c_str(), "thumbv7-apple-", 14) == 0)) {
300 args.push_back("-arch");
301 args.push_back("armv7");
302 }
Nick Kledzikfe15a722009-06-04 19:14:08 +0000303 // add -static to assembler command line when code model requires
304 if ( (_assemblerPath != NULL) && (_codeModel == LTO_CODEGEN_PIC_MODEL_STATIC) )
305 args.push_back("-static");
Nick Kledzik6d886992008-02-26 20:26:43 +0000306 }
Nick Kledzik39d9b412009-06-04 00:28:45 +0000307 if ( needsCompilerOptions ) {
308 args.push_back("-c");
309 args.push_back("-x");
310 args.push_back("assembler");
311 }
Nick Kledzik6d886992008-02-26 20:26:43 +0000312 args.push_back("-o");
313 args.push_back(objPath.c_str());
314 args.push_back(asmPath.c_str());
315 args.push_back(0);
316
317 // invoke assembler
Nick Kledzik39d9b412009-06-04 00:28:45 +0000318 if ( sys::Program::ExecuteAndWait(tool, &args[0], 0, 0, 0, 0, &errMsg) ) {
Nick Kledzik6d886992008-02-26 20:26:43 +0000319 errMsg = "error in assembly";
320 return true;
321 }
322 return false; // success
323}
324
325
326
327bool LTOCodeGenerator::determineTarget(std::string& errMsg)
328{
329 if ( _target == NULL ) {
Daniel Dunbar4baa5fe2009-08-03 04:03:51 +0000330 std::string Triple = _linker.getModule()->getTargetTriple();
331 if (Triple.empty())
332 Triple = sys::getHostTriple();
333
Nick Kledzik6d886992008-02-26 20:26:43 +0000334 // create target machine from info for merged modules
335 Module* mergedModule = _linker.getModule();
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000336 const Target *march =
Daniel Dunbar4baa5fe2009-08-03 04:03:51 +0000337 TargetRegistry::lookupTarget(Triple,
338 /*FallbackToHost=*/false,
Daniel Dunbar48224ee2009-07-26 02:12:58 +0000339 /*RequireJIT=*/false,
340 errMsg);
Nick Kledzik6d886992008-02-26 20:26:43 +0000341 if ( march == NULL )
342 return true;
Bill Wendling568e37c2008-06-18 06:35:30 +0000343
Nick Kledzik3e843222009-06-03 22:52:12 +0000344 // The relocation model is actually a static member of TargetMachine
345 // and needs to be set before the TargetMachine is instantiated.
346 switch( _codeModel ) {
347 case LTO_CODEGEN_PIC_MODEL_STATIC:
348 TargetMachine::setRelocationModel(Reloc::Static);
349 break;
350 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
351 TargetMachine::setRelocationModel(Reloc::PIC_);
352 break;
353 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
354 TargetMachine::setRelocationModel(Reloc::DynamicNoPIC);
355 break;
356 }
357
Bill Wendling568e37c2008-06-18 06:35:30 +0000358 // construct LTModule, hand over ownership of module and target
Daniel Dunbar4baa5fe2009-08-03 04:03:51 +0000359 std::string FeatureStr = getFeatureString(Triple.c_str());
360 _target = march->createTargetMachine(*mergedModule, Triple, FeatureStr);
Nick Kledzik6d886992008-02-26 20:26:43 +0000361 }
362 return false;
363}
364
365void LTOCodeGenerator::applyScopeRestrictions()
366{
367 if ( !_scopeRestrictionsDone ) {
368 Module* mergedModule = _linker.getModule();
369
370 // Start off with a verification pass.
371 PassManager passes;
372 passes.add(createVerifierPass());
373
374 // mark which symbols can not be internalized
375 if ( !_mustPreserveSymbols.empty() ) {
376 Mangler mangler(*mergedModule,
377 _target->getTargetAsmInfo()->getGlobalPrefix());
378 std::vector<const char*> mustPreserveList;
379 for (Module::iterator f = mergedModule->begin(),
380 e = mergedModule->end(); f != e; ++f) {
381 if ( !f->isDeclaration()
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000382 && _mustPreserveSymbols.count(mangler.getMangledName(f)) )
Daniel Dunbare24a8e42009-07-22 21:33:09 +0000383 mustPreserveList.push_back(::strdup(f->getNameStr().c_str()));
Nick Kledzik6d886992008-02-26 20:26:43 +0000384 }
385 for (Module::global_iterator v = mergedModule->global_begin(),
386 e = mergedModule->global_end(); v != e; ++v) {
387 if ( !v->isDeclaration()
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000388 && _mustPreserveSymbols.count(mangler.getMangledName(v)) )
Daniel Dunbare24a8e42009-07-22 21:33:09 +0000389 mustPreserveList.push_back(::strdup(v->getNameStr().c_str()));
Nick Kledzik6d886992008-02-26 20:26:43 +0000390 }
391 passes.add(createInternalizePass(mustPreserveList));
392 }
393 // apply scope restrictions
394 passes.run(*mergedModule);
395
396 _scopeRestrictionsDone = true;
397 }
398}
399
Nick Kledzik6d886992008-02-26 20:26:43 +0000400/// Optimize merged modules using various IPO passes
David Greene302008d2009-07-14 20:18:05 +0000401bool LTOCodeGenerator::generateAssemblyCode(formatted_raw_ostream& out,
Owen Anderson847b99b2008-08-21 00:14:44 +0000402 std::string& errMsg)
Nick Kledzik6d886992008-02-26 20:26:43 +0000403{
Nick Lewyckybbd1d0f2009-07-26 22:16:39 +0000404 if ( this->determineTarget(errMsg) )
Nick Kledzik6d886992008-02-26 20:26:43 +0000405 return true;
406
407 // mark which symbols can not be internalized
408 this->applyScopeRestrictions();
409
410 Module* mergedModule = _linker.getModule();
411
412 // If target supports exception handling then enable it now.
413 if ( _target->getTargetAsmInfo()->doesSupportExceptionHandling() )
414 llvm::ExceptionHandling = true;
415
Nick Kledzik4059fb12008-07-08 21:14:10 +0000416 // if options were requested, set them
417 if ( !_codegenOptions.empty() )
418 cl::ParseCommandLineOptions(_codegenOptions.size(),
419 (char**)&_codegenOptions[0]);
Devang Patel6fb60262008-07-03 22:53:14 +0000420
Nick Kledzik6d886992008-02-26 20:26:43 +0000421 // Instantiate the pass manager to organize the passes.
422 PassManager passes;
423
424 // Start off with a verification pass.
425 passes.add(createVerifierPass());
426
427 // Add an appropriate TargetData instance for this module...
428 passes.add(new TargetData(*_target->getTargetData()));
429
Daniel Dunbar9068de52009-06-03 21:06:14 +0000430 createStandardLTOPasses(&passes, /*Internalize=*/ false, !DisableInline,
Daniel Dunbar9068de52009-06-03 21:06:14 +0000431 /*VerifyEach=*/ false);
Nick Kledzik6d886992008-02-26 20:26:43 +0000432
433 // Make sure everything is still good.
434 passes.add(createVerifierPass());
435
436 FunctionPassManager* codeGenPasses =
437 new FunctionPassManager(new ExistingModuleProvider(mergedModule));
438
439 codeGenPasses->add(new TargetData(*_target->getTargetData()));
440
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000441 ObjectCodeEmitter* oce = NULL;
Nick Kledzik6d886992008-02-26 20:26:43 +0000442
443 switch (_target->addPassesToEmitFile(*codeGenPasses, out,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000444 TargetMachine::AssemblyFile,
Bill Wendling2d1c1162009-04-29 23:40:42 +0000445 CodeGenOpt::Aggressive)) {
Nick Kledzik6d886992008-02-26 20:26:43 +0000446 case FileModel::MachOFile:
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000447 oce = AddMachOWriter(*codeGenPasses, out, *_target);
Nick Kledzik6d886992008-02-26 20:26:43 +0000448 break;
449 case FileModel::ElfFile:
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000450 oce = AddELFWriter(*codeGenPasses, out, *_target);
Nick Kledzik6d886992008-02-26 20:26:43 +0000451 break;
452 case FileModel::AsmFile:
453 break;
454 case FileModel::Error:
455 case FileModel::None:
456 errMsg = "target file type not supported";
457 return true;
458 }
459
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000460 if (_target->addPassesToEmitFileFinish(*codeGenPasses, oce,
Bill Wendling2d1c1162009-04-29 23:40:42 +0000461 CodeGenOpt::Aggressive)) {
Nick Kledzik6d886992008-02-26 20:26:43 +0000462 errMsg = "target does not support generation of this file type";
463 return true;
464 }
465
466 // Run our queue of passes all at once now, efficiently.
467 passes.run(*mergedModule);
468
469 // Run the code generator, and write assembly file
470 codeGenPasses->doInitialization();
Nick Kledzik6d886992008-02-26 20:26:43 +0000471
Bill Wendling568e37c2008-06-18 06:35:30 +0000472 for (Module::iterator
473 it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it)
474 if (!it->isDeclaration())
475 codeGenPasses->run(*it);
476
477 codeGenPasses->doFinalization();
Nick Lewyckybbd1d0f2009-07-26 22:16:39 +0000478
479 out.flush();
480
Nick Kledzik6d886992008-02-26 20:26:43 +0000481 return false; // success
482}
483
484
Nick Kledzik4059fb12008-07-08 21:14:10 +0000485/// Optimize merged modules using various IPO passes
486void LTOCodeGenerator::setCodeGenDebugOptions(const char* options)
487{
488 std::string ops(options);
489 for (std::string o = getToken(ops); !o.empty(); o = getToken(ops)) {
490 // ParseCommandLineOptions() expects argv[0] to be program name.
491 // Lazily add that.
492 if ( _codegenOptions.empty() )
493 _codegenOptions.push_back("libLTO");
494 _codegenOptions.push_back(strdup(o.c_str()));
495 }
496}