blob: 190578e2630c484d40d41d897a1b211d257e8684 [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 {
Dan Gohman815944d2009-08-13 16:51:51 +0000181 raw_fd_ostream asmFD(uniqueAsmPath.c_str(),
182 /*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
Daniel Dunbar4bd03ab2009-08-03 04:20:57 +0000325 const Target *march = TargetRegistry::lookupTarget(Triple, errMsg);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000326 if ( march == NULL )
327 return true;
Bill Wendling604a8182008-06-18 06:35:30 +0000328
Nick Kledzikf5a1c35f12009-06-03 22:52:12 +0000329 // The relocation model is actually a static member of TargetMachine
330 // and needs to be set before the TargetMachine is instantiated.
331 switch( _codeModel ) {
332 case LTO_CODEGEN_PIC_MODEL_STATIC:
333 TargetMachine::setRelocationModel(Reloc::Static);
334 break;
335 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
336 TargetMachine::setRelocationModel(Reloc::PIC_);
337 break;
338 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
339 TargetMachine::setRelocationModel(Reloc::DynamicNoPIC);
340 break;
341 }
342
Bill Wendling604a8182008-06-18 06:35:30 +0000343 // construct LTModule, hand over ownership of module and target
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000344 std::string FeatureStr = getFeatureString(Triple.c_str());
Daniel Dunbar4b3d5722009-08-04 04:08:40 +0000345 _target = march->createTargetMachine(Triple, FeatureStr);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000346 }
347 return false;
348}
349
350void LTOCodeGenerator::applyScopeRestrictions()
351{
352 if ( !_scopeRestrictionsDone ) {
353 Module* mergedModule = _linker.getModule();
354
355 // Start off with a verification pass.
356 PassManager passes;
357 passes.add(createVerifierPass());
358
359 // mark which symbols can not be internalized
360 if ( !_mustPreserveSymbols.empty() ) {
361 Mangler mangler(*mergedModule,
362 _target->getTargetAsmInfo()->getGlobalPrefix());
363 std::vector<const char*> mustPreserveList;
364 for (Module::iterator f = mergedModule->begin(),
365 e = mergedModule->end(); f != e; ++f) {
366 if ( !f->isDeclaration()
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000367 && _mustPreserveSymbols.count(mangler.getMangledName(f)) )
Daniel Dunbar3d5126f2009-07-22 21:33:09 +0000368 mustPreserveList.push_back(::strdup(f->getNameStr().c_str()));
Nick Kledzik77595fc2008-02-26 20:26:43 +0000369 }
370 for (Module::global_iterator v = mergedModule->global_begin(),
371 e = mergedModule->global_end(); v != e; ++v) {
372 if ( !v->isDeclaration()
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000373 && _mustPreserveSymbols.count(mangler.getMangledName(v)) )
Daniel Dunbar3d5126f2009-07-22 21:33:09 +0000374 mustPreserveList.push_back(::strdup(v->getNameStr().c_str()));
Nick Kledzik77595fc2008-02-26 20:26:43 +0000375 }
376 passes.add(createInternalizePass(mustPreserveList));
377 }
378 // apply scope restrictions
379 passes.run(*mergedModule);
380
381 _scopeRestrictionsDone = true;
382 }
383}
384
Nick Kledzik77595fc2008-02-26 20:26:43 +0000385/// Optimize merged modules using various IPO passes
David Greene71847812009-07-14 20:18:05 +0000386bool LTOCodeGenerator::generateAssemblyCode(formatted_raw_ostream& out,
Owen Andersoncb371882008-08-21 00:14:44 +0000387 std::string& errMsg)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000388{
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000389 if ( this->determineTarget(errMsg) )
Nick Kledzik77595fc2008-02-26 20:26:43 +0000390 return true;
391
392 // mark which symbols can not be internalized
393 this->applyScopeRestrictions();
394
395 Module* mergedModule = _linker.getModule();
396
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000397 // If target supports exception handling then enable it now.
398 switch (_target->getTargetAsmInfo()->getExceptionHandlingType()) {
399 case ExceptionHandling::Dwarf:
400 llvm::DwarfExceptionHandling = true;
401 break;
402 case ExceptionHandling::SjLj:
403 llvm::SjLjExceptionHandling = true;
404 break;
405 case ExceptionHandling::None:
406 break;
407 default:
408 assert (0 && "Unknown exception handling model!");
409 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000410
Nick Kledzik920ae982008-07-08 21:14:10 +0000411 // if options were requested, set them
412 if ( !_codegenOptions.empty() )
413 cl::ParseCommandLineOptions(_codegenOptions.size(),
414 (char**)&_codegenOptions[0]);
Devang Patela93ae712008-07-03 22:53:14 +0000415
Nick Kledzik77595fc2008-02-26 20:26:43 +0000416 // Instantiate the pass manager to organize the passes.
417 PassManager passes;
418
419 // Start off with a verification pass.
420 passes.add(createVerifierPass());
421
422 // Add an appropriate TargetData instance for this module...
423 passes.add(new TargetData(*_target->getTargetData()));
424
Daniel Dunbar006a0342009-06-03 21:06:14 +0000425 createStandardLTOPasses(&passes, /*Internalize=*/ false, !DisableInline,
Daniel Dunbar006a0342009-06-03 21:06:14 +0000426 /*VerifyEach=*/ false);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000427
428 // Make sure everything is still good.
429 passes.add(createVerifierPass());
430
431 FunctionPassManager* codeGenPasses =
432 new FunctionPassManager(new ExistingModuleProvider(mergedModule));
433
434 codeGenPasses->add(new TargetData(*_target->getTargetData()));
435
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000436 ObjectCodeEmitter* oce = NULL;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000437
438 switch (_target->addPassesToEmitFile(*codeGenPasses, out,
Bill Wendling98a366d2009-04-29 23:29:43 +0000439 TargetMachine::AssemblyFile,
Bill Wendlingb8cb0bb2009-04-29 23:40:42 +0000440 CodeGenOpt::Aggressive)) {
Nick Kledzik77595fc2008-02-26 20:26:43 +0000441 case FileModel::MachOFile:
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000442 oce = AddMachOWriter(*codeGenPasses, out, *_target);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000443 break;
444 case FileModel::ElfFile:
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000445 oce = AddELFWriter(*codeGenPasses, out, *_target);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000446 break;
447 case FileModel::AsmFile:
448 break;
449 case FileModel::Error:
450 case FileModel::None:
451 errMsg = "target file type not supported";
452 return true;
453 }
454
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000455 if (_target->addPassesToEmitFileFinish(*codeGenPasses, oce,
Bill Wendlingb8cb0bb2009-04-29 23:40:42 +0000456 CodeGenOpt::Aggressive)) {
Nick Kledzik77595fc2008-02-26 20:26:43 +0000457 errMsg = "target does not support generation of this file type";
458 return true;
459 }
460
461 // Run our queue of passes all at once now, efficiently.
462 passes.run(*mergedModule);
463
464 // Run the code generator, and write assembly file
465 codeGenPasses->doInitialization();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000466
Bill Wendling604a8182008-06-18 06:35:30 +0000467 for (Module::iterator
468 it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it)
469 if (!it->isDeclaration())
470 codeGenPasses->run(*it);
471
472 codeGenPasses->doFinalization();
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000473
474 out.flush();
475
Nick Kledzik77595fc2008-02-26 20:26:43 +0000476 return false; // success
477}
478
479
Nick Kledzik920ae982008-07-08 21:14:10 +0000480/// Optimize merged modules using various IPO passes
481void LTOCodeGenerator::setCodeGenDebugOptions(const char* options)
482{
483 std::string ops(options);
484 for (std::string o = getToken(ops); !o.empty(); o = getToken(ops)) {
485 // ParseCommandLineOptions() expects argv[0] to be program name.
486 // Lazily add that.
487 if ( _codegenOptions.empty() )
488 _codegenOptions.push_back("libLTO");
489 _codegenOptions.push_back(strdup(o.c_str()));
490 }
491}