blob: 4bd754cfbad34731085f8a81a904d5879a127561 [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"
Chris Lattner3a71a1a2009-08-23 21:36:09 +000039#include "llvm/System/Program.h"
Nick Kledzik6d886992008-02-26 20:26:43 +000040#include "llvm/System/Signals.h"
Bill Wendling568e37c2008-06-18 06:35:30 +000041#include "llvm/Target/SubtargetFeature.h"
Nick Kledzik6d886992008-02-26 20:26:43 +000042#include "llvm/Target/TargetOptions.h"
Chris Lattner621c44d2009-08-22 20:48:53 +000043#include "llvm/MC/MCAsmInfo.h"
Nick Kledzik6d886992008-02-26 20:26:43 +000044#include "llvm/Target/TargetData.h"
45#include "llvm/Target/TargetMachine.h"
Daniel Dunbar3b2bc2c2009-07-16 02:41:19 +000046#include "llvm/Target/TargetRegistry.h"
Chris Lattner41788082009-06-17 16:42:19 +000047#include "llvm/Target/TargetSelect.h"
Nick Kledzik6d886992008-02-26 20:26:43 +000048#include "llvm/Transforms/IPO.h"
49#include "llvm/Transforms/Scalar.h"
Nick Kledzik6d886992008-02-26 20:26:43 +000050#include "llvm/Config/config.h"
Nick Lewycky3d1feeb2009-06-17 06:52:10 +000051#include <cstdlib>
Nick Kledzik6d886992008-02-26 20:26:43 +000052#include <unistd.h>
Nick Kledzik6d886992008-02-26 20:26:43 +000053#include <fcntl.h>
54
55
56using namespace llvm;
57
Nick Kledzik4059fb12008-07-08 21:14:10 +000058static cl::opt<bool> DisableInline("disable-inlining",
59 cl::desc("Do not run the inliner pass"));
Nick Kledzik6d886992008-02-26 20:26:43 +000060
61
62const char* LTOCodeGenerator::getVersionString()
63{
64#ifdef LLVM_VERSION_INFO
65 return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
66#else
67 return PACKAGE_NAME " version " PACKAGE_VERSION;
68#endif
69}
70
71
Owen Anderson353dadb2009-07-02 00:31:14 +000072LTOCodeGenerator::LTOCodeGenerator()
73 : _context(getGlobalContext()),
Owen Anderson25209b42009-07-01 16:58:40 +000074 _linker("LinkTimeOptimizer", "ld-temp.o", _context), _target(NULL),
Nick Kledzik6d886992008-02-26 20:26:43 +000075 _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false),
Nick Kledzik79d0a052008-02-27 22:25:36 +000076 _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
Nick Lewycky17b2a952009-08-03 07:16:42 +000077 _nativeObjectFile(NULL), _assemblerPath(NULL)
Nick Kledzik6d886992008-02-26 20:26:43 +000078{
Nick Lewyckybbd1d0f2009-07-26 22:16:39 +000079 InitializeAllTargets();
80 InitializeAllAsmPrinters();
Nick Kledzik6d886992008-02-26 20:26:43 +000081}
82
83LTOCodeGenerator::~LTOCodeGenerator()
84{
Nick Kledzik79d0a052008-02-27 22:25:36 +000085 delete _target;
86 delete _nativeObjectFile;
Nick Kledzik6d886992008-02-26 20:26:43 +000087}
88
89
90
91bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg)
92{
93 return _linker.LinkInModule(mod->getLLVVMModule(), &errMsg);
94}
95
96
97bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug, std::string& errMsg)
98{
99 switch (debug) {
100 case LTO_DEBUG_MODEL_NONE:
101 _emitDwarfDebugInfo = false;
102 return false;
103
104 case LTO_DEBUG_MODEL_DWARF:
105 _emitDwarfDebugInfo = true;
106 return false;
107 }
108 errMsg = "unknown debug format";
109 return true;
110}
111
112
113bool LTOCodeGenerator::setCodePICModel(lto_codegen_model model,
Evan Cheng521500b2009-06-26 06:57:16 +0000114 std::string& errMsg)
Nick Kledzik6d886992008-02-26 20:26:43 +0000115{
116 switch (model) {
117 case LTO_CODEGEN_PIC_MODEL_STATIC:
118 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
119 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
120 _codeModel = model;
121 return false;
122 }
123 errMsg = "unknown pic model";
124 return true;
125}
126
Nick Kledzik39d9b412009-06-04 00:28:45 +0000127void LTOCodeGenerator::setAssemblerPath(const char* path)
128{
129 if ( _assemblerPath )
130 delete _assemblerPath;
131 _assemblerPath = new sys::Path(path);
132}
133
Nick Kledzik6d886992008-02-26 20:26:43 +0000134void LTOCodeGenerator::addMustPreserveSymbol(const char* sym)
135{
136 _mustPreserveSymbols[sym] = 1;
137}
138
139
Chris Lattner371c1ef2009-08-23 07:49:08 +0000140bool LTOCodeGenerator::writeMergedModules(const char *path,
141 std::string &errMsg) {
142 if (determineTarget(errMsg))
143 return true;
Nick Kledzik6d886992008-02-26 20:26:43 +0000144
Chris Lattner371c1ef2009-08-23 07:49:08 +0000145 // mark which symbols can not be internalized
146 applyScopeRestrictions();
Nick Kledzik6d886992008-02-26 20:26:43 +0000147
Chris Lattner371c1ef2009-08-23 07:49:08 +0000148 // create output file
149 std::string ErrInfo;
150 raw_fd_ostream Out(path, ErrInfo,
151 raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
152 if (!ErrInfo.empty()) {
153 errMsg = "could not open bitcode file for writing: ";
154 errMsg += path;
155 return true;
156 }
Nick Kledzik6d886992008-02-26 20:26:43 +0000157
Chris Lattner371c1ef2009-08-23 07:49:08 +0000158 // write bitcode to it
159 WriteBitcodeToFile(_linker.getModule(), Out);
160
161 if (Out.has_error()) {
162 errMsg = "could not write bitcode file: ";
163 errMsg += path;
164 return true;
165 }
166
167 return false;
Nick Kledzik6d886992008-02-26 20:26:43 +0000168}
169
170
Nick Kledzik79d0a052008-02-27 22:25:36 +0000171const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg)
Nick Kledzik6d886992008-02-26 20:26:43 +0000172{
Nick Kledzik79d0a052008-02-27 22:25:36 +0000173 // make unique temp .s file to put generated assembly code
Nick Kledzik6d886992008-02-26 20:26:43 +0000174 sys::Path uniqueAsmPath("lto-llvm.s");
175 if ( uniqueAsmPath.createTemporaryFileOnDisk(true, &errMsg) )
176 return NULL;
177 sys::RemoveFileOnSignal(uniqueAsmPath);
178
179 // generate assembly code
Owen Anderson847b99b2008-08-21 00:14:44 +0000180 bool genResult = false;
181 {
Chris Lattnerfdcd46e2009-08-23 02:51:22 +0000182 raw_fd_ostream asmFD(uniqueAsmPath.c_str(), errMsg,
183 raw_fd_ostream::F_Force);
David Greene302008d2009-07-14 20:18:05 +0000184 formatted_raw_ostream asmFile(asmFD);
Dan Gohmande20df82008-08-21 15:33:45 +0000185 if (!errMsg.empty())
186 return NULL;
Owen Anderson847b99b2008-08-21 00:14:44 +0000187 genResult = this->generateAssemblyCode(asmFile, errMsg);
188 }
Nick Kledzik6d886992008-02-26 20:26:43 +0000189 if ( genResult ) {
190 if ( uniqueAsmPath.exists() )
191 uniqueAsmPath.eraseFromDisk();
192 return NULL;
193 }
194
Nick Kledzik79d0a052008-02-27 22:25:36 +0000195 // make unique temp .o file to put generated object file
Nick Kledzik6d886992008-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
Chris Lattnerb1aa85b2009-08-23 22:45:37 +0000205 const std::string& uniqueObjStr = uniqueObjPath.str();
206 bool asmResult = this->assemble(uniqueAsmPath.str(), uniqueObjStr, errMsg);
Nick Kledzik6d886992008-02-26 20:26:43 +0000207 if ( !asmResult ) {
Nick Kledzik79d0a052008-02-27 22:25:36 +0000208 // remove old buffer if compile() called twice
209 delete _nativeObjectFile;
210
Nick Kledzik6d886992008-02-26 20:26:43 +0000211 // read .o file into memory buffer
Chris Lattnerfc003612008-04-01 18:04:03 +0000212 _nativeObjectFile = MemoryBuffer::getFile(uniqueObjStr.c_str(),&errMsg);
Nick Kledzik6d886992008-02-26 20:26:43 +0000213 }
Nick Kledzik79d0a052008-02-27 22:25:36 +0000214
215 // remove temp files
Nick Kledzik6d886992008-02-26 20:26:43 +0000216 uniqueAsmPath.eraseFromDisk();
217 uniqueObjPath.eraseFromDisk();
Nick Kledzik79d0a052008-02-27 22:25:36 +0000218
219 // return buffer, unless error
220 if ( _nativeObjectFile == NULL )
221 return NULL;
222 *length = _nativeObjectFile->getBufferSize();
223 return _nativeObjectFile->getBufferStart();
Nick Kledzik6d886992008-02-26 20:26:43 +0000224}
225
226
227bool LTOCodeGenerator::assemble(const std::string& asmPath,
228 const std::string& objPath, std::string& errMsg)
229{
Nick Kledzik39d9b412009-06-04 00:28:45 +0000230 sys::Path tool;
231 bool needsCompilerOptions = true;
232 if ( _assemblerPath ) {
233 tool = *_assemblerPath;
234 needsCompilerOptions = false;
Nick Lewyckyef973202009-04-30 15:24:09 +0000235 } else {
236 // find compiler driver
Nick Kledzik39d9b412009-06-04 00:28:45 +0000237 tool = sys::Program::FindProgramByName("gcc");
238 if ( tool.isEmpty() ) {
Nick Lewyckyef973202009-04-30 15:24:09 +0000239 errMsg = "can't locate gcc";
240 return true;
241 }
Nick Kledzik6d886992008-02-26 20:26:43 +0000242 }
243
244 // build argument list
245 std::vector<const char*> args;
246 std::string targetTriple = _linker.getModule()->getTargetTriple();
Nick Kledzik39d9b412009-06-04 00:28:45 +0000247 args.push_back(tool.c_str());
Rafael Espindola6c279402009-06-09 21:14:25 +0000248 if ( targetTriple.find("darwin") != std::string::npos ) {
Nick Kledzikfe15a722009-06-04 19:14:08 +0000249 // darwin specific command line options
Devang Patelaa6cd1f2008-11-04 23:13:50 +0000250 if (strncmp(targetTriple.c_str(), "i386-apple-", 11) == 0) {
Nick Kledzik6d886992008-02-26 20:26:43 +0000251 args.push_back("-arch");
252 args.push_back("i386");
253 }
254 else if (strncmp(targetTriple.c_str(), "x86_64-apple-", 13) == 0) {
255 args.push_back("-arch");
256 args.push_back("x86_64");
257 }
258 else if (strncmp(targetTriple.c_str(), "powerpc-apple-", 14) == 0) {
259 args.push_back("-arch");
260 args.push_back("ppc");
261 }
262 else if (strncmp(targetTriple.c_str(), "powerpc64-apple-", 16) == 0) {
263 args.push_back("-arch");
264 args.push_back("ppc64");
265 }
Evan Chengdc5b6d62009-04-01 18:54:56 +0000266 else if (strncmp(targetTriple.c_str(), "arm-apple-", 10) == 0) {
267 args.push_back("-arch");
268 args.push_back("arm");
269 }
270 else if ((strncmp(targetTriple.c_str(), "armv4t-apple-", 13) == 0) ||
271 (strncmp(targetTriple.c_str(), "thumbv4t-apple-", 15) == 0)) {
272 args.push_back("-arch");
273 args.push_back("armv4t");
274 }
275 else if ((strncmp(targetTriple.c_str(), "armv5-apple-", 12) == 0) ||
276 (strncmp(targetTriple.c_str(), "armv5e-apple-", 13) == 0) ||
277 (strncmp(targetTriple.c_str(), "thumbv5-apple-", 14) == 0) ||
278 (strncmp(targetTriple.c_str(), "thumbv5e-apple-", 15) == 0)) {
279 args.push_back("-arch");
280 args.push_back("armv5");
281 }
282 else if ((strncmp(targetTriple.c_str(), "armv6-apple-", 12) == 0) ||
283 (strncmp(targetTriple.c_str(), "thumbv6-apple-", 14) == 0)) {
284 args.push_back("-arch");
285 args.push_back("armv6");
286 }
Bob Wilson5b50a242009-06-22 18:01:28 +0000287 else if ((strncmp(targetTriple.c_str(), "armv7-apple-", 12) == 0) ||
288 (strncmp(targetTriple.c_str(), "thumbv7-apple-", 14) == 0)) {
289 args.push_back("-arch");
290 args.push_back("armv7");
291 }
Nick Kledzikfe15a722009-06-04 19:14:08 +0000292 // add -static to assembler command line when code model requires
293 if ( (_assemblerPath != NULL) && (_codeModel == LTO_CODEGEN_PIC_MODEL_STATIC) )
294 args.push_back("-static");
Nick Kledzik6d886992008-02-26 20:26:43 +0000295 }
Nick Kledzik39d9b412009-06-04 00:28:45 +0000296 if ( needsCompilerOptions ) {
297 args.push_back("-c");
298 args.push_back("-x");
299 args.push_back("assembler");
300 }
Nick Kledzik6d886992008-02-26 20:26:43 +0000301 args.push_back("-o");
302 args.push_back(objPath.c_str());
303 args.push_back(asmPath.c_str());
304 args.push_back(0);
305
306 // invoke assembler
Nick Kledzik39d9b412009-06-04 00:28:45 +0000307 if ( sys::Program::ExecuteAndWait(tool, &args[0], 0, 0, 0, 0, &errMsg) ) {
Nick Kledzik6d886992008-02-26 20:26:43 +0000308 errMsg = "error in assembly";
309 return true;
310 }
311 return false; // success
312}
313
314
315
316bool LTOCodeGenerator::determineTarget(std::string& errMsg)
317{
318 if ( _target == NULL ) {
Daniel Dunbar4baa5fe2009-08-03 04:03:51 +0000319 std::string Triple = _linker.getModule()->getTargetTriple();
320 if (Triple.empty())
321 Triple = sys::getHostTriple();
322
Nick Kledzik6d886992008-02-26 20:26:43 +0000323 // create target machine from info for merged modules
Daniel Dunbar9aa08272009-08-03 04:20:57 +0000324 const Target *march = TargetRegistry::lookupTarget(Triple, errMsg);
Nick Kledzik6d886992008-02-26 20:26:43 +0000325 if ( march == NULL )
326 return true;
Bill Wendling568e37c2008-06-18 06:35:30 +0000327
Nick Kledzik3e843222009-06-03 22:52:12 +0000328 // The relocation model is actually a static member of TargetMachine
329 // and needs to be set before the TargetMachine is instantiated.
330 switch( _codeModel ) {
331 case LTO_CODEGEN_PIC_MODEL_STATIC:
332 TargetMachine::setRelocationModel(Reloc::Static);
333 break;
334 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
335 TargetMachine::setRelocationModel(Reloc::PIC_);
336 break;
337 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
338 TargetMachine::setRelocationModel(Reloc::DynamicNoPIC);
339 break;
340 }
341
Bill Wendling568e37c2008-06-18 06:35:30 +0000342 // construct LTModule, hand over ownership of module and target
Daniel Dunbar4baa5fe2009-08-03 04:03:51 +0000343 std::string FeatureStr = getFeatureString(Triple.c_str());
Daniel Dunbar09a25f32009-08-04 04:08:40 +0000344 _target = march->createTargetMachine(Triple, FeatureStr);
Nick Kledzik6d886992008-02-26 20:26:43 +0000345 }
346 return false;
347}
348
349void LTOCodeGenerator::applyScopeRestrictions()
350{
351 if ( !_scopeRestrictionsDone ) {
352 Module* mergedModule = _linker.getModule();
353
354 // Start off with a verification pass.
355 PassManager passes;
356 passes.add(createVerifierPass());
357
358 // mark which symbols can not be internalized
359 if ( !_mustPreserveSymbols.empty() ) {
360 Mangler mangler(*mergedModule,
Chris Lattner621c44d2009-08-22 20:48:53 +0000361 _target->getMCAsmInfo()->getGlobalPrefix());
Nick Kledzik6d886992008-02-26 20:26:43 +0000362 std::vector<const char*> mustPreserveList;
363 for (Module::iterator f = mergedModule->begin(),
364 e = mergedModule->end(); f != e; ++f) {
365 if ( !f->isDeclaration()
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000366 && _mustPreserveSymbols.count(mangler.getMangledName(f)) )
Daniel Dunbare24a8e42009-07-22 21:33:09 +0000367 mustPreserveList.push_back(::strdup(f->getNameStr().c_str()));
Nick Kledzik6d886992008-02-26 20:26:43 +0000368 }
369 for (Module::global_iterator v = mergedModule->global_begin(),
370 e = mergedModule->global_end(); v != e; ++v) {
371 if ( !v->isDeclaration()
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000372 && _mustPreserveSymbols.count(mangler.getMangledName(v)) )
Daniel Dunbare24a8e42009-07-22 21:33:09 +0000373 mustPreserveList.push_back(::strdup(v->getNameStr().c_str()));
Nick Kledzik6d886992008-02-26 20:26:43 +0000374 }
375 passes.add(createInternalizePass(mustPreserveList));
376 }
377 // apply scope restrictions
378 passes.run(*mergedModule);
379
380 _scopeRestrictionsDone = true;
381 }
382}
383
Nick Kledzik6d886992008-02-26 20:26:43 +0000384/// Optimize merged modules using various IPO passes
David Greene302008d2009-07-14 20:18:05 +0000385bool LTOCodeGenerator::generateAssemblyCode(formatted_raw_ostream& out,
Owen Anderson847b99b2008-08-21 00:14:44 +0000386 std::string& errMsg)
Nick Kledzik6d886992008-02-26 20:26:43 +0000387{
Nick Lewyckybbd1d0f2009-07-26 22:16:39 +0000388 if ( this->determineTarget(errMsg) )
Nick Kledzik6d886992008-02-26 20:26:43 +0000389 return true;
390
391 // mark which symbols can not be internalized
392 this->applyScopeRestrictions();
393
394 Module* mergedModule = _linker.getModule();
395
Jim Grosbach29feb6a2009-08-11 00:09:57 +0000396 // If target supports exception handling then enable it now.
Chris Lattner621c44d2009-08-22 20:48:53 +0000397 switch (_target->getMCAsmInfo()->getExceptionHandlingType()) {
Jim Grosbach29feb6a2009-08-11 00:09:57 +0000398 case ExceptionHandling::Dwarf:
399 llvm::DwarfExceptionHandling = true;
400 break;
401 case ExceptionHandling::SjLj:
402 llvm::SjLjExceptionHandling = true;
403 break;
404 case ExceptionHandling::None:
405 break;
406 default:
407 assert (0 && "Unknown exception handling model!");
408 }
Nick Kledzik6d886992008-02-26 20:26:43 +0000409
Nick Kledzik4059fb12008-07-08 21:14:10 +0000410 // if options were requested, set them
411 if ( !_codegenOptions.empty() )
412 cl::ParseCommandLineOptions(_codegenOptions.size(),
413 (char**)&_codegenOptions[0]);
Devang Patel6fb60262008-07-03 22:53:14 +0000414
Nick Kledzik6d886992008-02-26 20:26:43 +0000415 // Instantiate the pass manager to organize the passes.
416 PassManager passes;
417
418 // Start off with a verification pass.
419 passes.add(createVerifierPass());
420
421 // Add an appropriate TargetData instance for this module...
422 passes.add(new TargetData(*_target->getTargetData()));
423
Daniel Dunbar9068de52009-06-03 21:06:14 +0000424 createStandardLTOPasses(&passes, /*Internalize=*/ false, !DisableInline,
Daniel Dunbar9068de52009-06-03 21:06:14 +0000425 /*VerifyEach=*/ false);
Nick Kledzik6d886992008-02-26 20:26:43 +0000426
427 // Make sure everything is still good.
428 passes.add(createVerifierPass());
429
430 FunctionPassManager* codeGenPasses =
431 new FunctionPassManager(new ExistingModuleProvider(mergedModule));
432
433 codeGenPasses->add(new TargetData(*_target->getTargetData()));
434
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000435 ObjectCodeEmitter* oce = NULL;
Nick Kledzik6d886992008-02-26 20:26:43 +0000436
437 switch (_target->addPassesToEmitFile(*codeGenPasses, out,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000438 TargetMachine::AssemblyFile,
Bill Wendling2d1c1162009-04-29 23:40:42 +0000439 CodeGenOpt::Aggressive)) {
Nick Kledzik6d886992008-02-26 20:26:43 +0000440 case FileModel::MachOFile:
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000441 oce = AddMachOWriter(*codeGenPasses, out, *_target);
Nick Kledzik6d886992008-02-26 20:26:43 +0000442 break;
443 case FileModel::ElfFile:
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000444 oce = AddELFWriter(*codeGenPasses, out, *_target);
Nick Kledzik6d886992008-02-26 20:26:43 +0000445 break;
446 case FileModel::AsmFile:
447 break;
448 case FileModel::Error:
449 case FileModel::None:
450 errMsg = "target file type not supported";
451 return true;
452 }
453
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000454 if (_target->addPassesToEmitFileFinish(*codeGenPasses, oce,
Bill Wendling2d1c1162009-04-29 23:40:42 +0000455 CodeGenOpt::Aggressive)) {
Nick Kledzik6d886992008-02-26 20:26:43 +0000456 errMsg = "target does not support generation of this file type";
457 return true;
458 }
459
460 // Run our queue of passes all at once now, efficiently.
461 passes.run(*mergedModule);
462
463 // Run the code generator, and write assembly file
464 codeGenPasses->doInitialization();
Nick Kledzik6d886992008-02-26 20:26:43 +0000465
Bill Wendling568e37c2008-06-18 06:35:30 +0000466 for (Module::iterator
467 it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it)
468 if (!it->isDeclaration())
469 codeGenPasses->run(*it);
470
471 codeGenPasses->doFinalization();
Nick Lewyckybbd1d0f2009-07-26 22:16:39 +0000472
473 out.flush();
474
Nick Kledzik6d886992008-02-26 20:26:43 +0000475 return false; // success
476}
477
478
Nick Kledzik4059fb12008-07-08 21:14:10 +0000479/// Optimize merged modules using various IPO passes
480void LTOCodeGenerator::setCodeGenDebugOptions(const char* options)
481{
482 std::string ops(options);
483 for (std::string o = getToken(ops); !o.empty(); o = getToken(ops)) {
484 // ParseCommandLineOptions() expects argv[0] to be program name.
485 // Lazily add that.
486 if ( _codegenOptions.empty() )
487 _codegenOptions.push_back("libLTO");
488 _codegenOptions.push_back(strdup(o.c_str()));
489 }
490}