blob: adb7102b3c76fc35720b3e0fa992f78ff3553254 [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
Nick Kledzik77595fc2008-02-26 20:26:43 +000018#include "llvm/Constants.h"
19#include "llvm/DerivedTypes.h"
Nick Lewycky8189d402009-06-17 06:52:10 +000020#include "llvm/Linker.h"
Owen Anderson0e7a5462009-07-02 00:31:14 +000021#include "llvm/LLVMContext.h"
Nick Lewycky8189d402009-06-17 06:52:10 +000022#include "llvm/Module.h"
Nick Lewycky8189d402009-06-17 06:52:10 +000023#include "llvm/PassManager.h"
24#include "llvm/ADT/StringExtras.h"
Viktor Kutuzov51cdac02009-11-17 18:48:27 +000025#include "llvm/ADT/Triple.h"
Nick Lewycky8189d402009-06-17 06:52:10 +000026#include "llvm/Analysis/Passes.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000027#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattner5ef31a02010-03-12 18:44:54 +000028#include "llvm/MC/MCAsmInfo.h"
29#include "llvm/MC/MCContext.h"
30#include "llvm/Target/Mangler.h"
31#include "llvm/Target/SubtargetFeature.h"
32#include "llvm/Target/TargetOptions.h"
33#include "llvm/Target/TargetData.h"
34#include "llvm/Target/TargetMachine.h"
35#include "llvm/Target/TargetRegistry.h"
36#include "llvm/Target/TargetSelect.h"
Nick Kledzik920ae982008-07-08 21:14:10 +000037#include "llvm/Support/CommandLine.h"
David Greene71847812009-07-14 20:18:05 +000038#include "llvm/Support/FormattedStream.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000039#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar006a0342009-06-03 21:06:14 +000040#include "llvm/Support/StandardPasses.h"
41#include "llvm/Support/SystemUtils.h"
Dan Gohman9f36c4e2010-10-07 20:48:46 +000042#include "llvm/Support/ToolOutputFile.h"
Michael J. Spencer3cc52ea2010-11-29 18:47:54 +000043#include "llvm/Support/Host.h"
44#include "llvm/Support/Program.h"
45#include "llvm/Support/Signals.h"
Michael J. Spencerf2f516f2010-12-09 18:06:07 +000046#include "llvm/Support/system_error.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000047#include "llvm/Config/config.h"
Nick Lewycky8189d402009-06-17 06:52:10 +000048#include <cstdlib>
Nick Kledzik77595fc2008-02-26 20:26:43 +000049#include <unistd.h>
Nick Kledzik77595fc2008-02-26 20:26:43 +000050#include <fcntl.h>
51
52
53using namespace llvm;
54
Nick Kledzik920ae982008-07-08 21:14:10 +000055static cl::opt<bool> DisableInline("disable-inlining",
56 cl::desc("Do not run the inliner pass"));
Nick Kledzik77595fc2008-02-26 20:26:43 +000057
58
59const char* LTOCodeGenerator::getVersionString()
60{
61#ifdef LLVM_VERSION_INFO
62 return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
63#else
64 return PACKAGE_NAME " version " PACKAGE_VERSION;
65#endif
66}
67
68
Owen Anderson0e7a5462009-07-02 00:31:14 +000069LTOCodeGenerator::LTOCodeGenerator()
70 : _context(getGlobalContext()),
Owen Anderson8b477ed2009-07-01 16:58:40 +000071 _linker("LinkTimeOptimizer", "ld-temp.o", _context), _target(NULL),
Nick Kledzik77595fc2008-02-26 20:26:43 +000072 _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false),
Nick Kledzikef194ed2008-02-27 22:25:36 +000073 _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
Nick Lewycky3e4c41a2009-08-03 07:16:42 +000074 _nativeObjectFile(NULL), _assemblerPath(NULL)
Nick Kledzik77595fc2008-02-26 20:26:43 +000075{
Nick Lewyckyd42b58b2009-07-26 22:16:39 +000076 InitializeAllTargets();
77 InitializeAllAsmPrinters();
Nick Kledzik77595fc2008-02-26 20:26:43 +000078}
79
80LTOCodeGenerator::~LTOCodeGenerator()
81{
Nick Kledzikef194ed2008-02-27 22:25:36 +000082 delete _target;
83 delete _nativeObjectFile;
Nick Kledzik77595fc2008-02-26 20:26:43 +000084}
85
86
87
88bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg)
89{
90 return _linker.LinkInModule(mod->getLLVVMModule(), &errMsg);
91}
92
93
94bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug, std::string& errMsg)
95{
96 switch (debug) {
97 case LTO_DEBUG_MODEL_NONE:
98 _emitDwarfDebugInfo = false;
99 return false;
100
101 case LTO_DEBUG_MODEL_DWARF:
102 _emitDwarfDebugInfo = true;
103 return false;
104 }
105 errMsg = "unknown debug format";
106 return true;
107}
108
109
110bool LTOCodeGenerator::setCodePICModel(lto_codegen_model model,
Evan Cheng855a1682009-06-26 06:57:16 +0000111 std::string& errMsg)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000112{
113 switch (model) {
114 case LTO_CODEGEN_PIC_MODEL_STATIC:
115 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
116 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
117 _codeModel = model;
118 return false;
119 }
120 errMsg = "unknown pic model";
121 return true;
122}
123
Rafael Espindola2d643ef2010-08-11 00:15:13 +0000124void LTOCodeGenerator::setCpu(const char* mCpu)
125{
126 _mCpu = mCpu;
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
Rafael Espindola98197e52010-08-10 18:55:09 +0000136void LTOCodeGenerator::setAssemblerArgs(const char** args, int nargs)
137{
138 for (int i = 0; i < nargs; ++i) {
139 const char *arg = args[i];
140 _assemblerArgs.push_back(arg);
141 }
142}
143
Nick Kledzik77595fc2008-02-26 20:26:43 +0000144void LTOCodeGenerator::addMustPreserveSymbol(const char* sym)
145{
146 _mustPreserveSymbols[sym] = 1;
147}
148
149
Chris Lattnerb515d752009-08-23 07:49:08 +0000150bool LTOCodeGenerator::writeMergedModules(const char *path,
151 std::string &errMsg) {
152 if (determineTarget(errMsg))
153 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000154
Chris Lattnerb515d752009-08-23 07:49:08 +0000155 // mark which symbols can not be internalized
156 applyScopeRestrictions();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000157
Chris Lattnerb515d752009-08-23 07:49:08 +0000158 // create output file
159 std::string ErrInfo;
Dan Gohmanf2914012010-08-20 16:59:15 +0000160 tool_output_file Out(path, ErrInfo,
161 raw_fd_ostream::F_Binary);
Chris Lattnerb515d752009-08-23 07:49:08 +0000162 if (!ErrInfo.empty()) {
163 errMsg = "could not open bitcode file for writing: ";
164 errMsg += path;
165 return true;
166 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000167
Chris Lattnerb515d752009-08-23 07:49:08 +0000168 // write bitcode to it
Dan Gohmand4c45432010-09-01 14:20:41 +0000169 WriteBitcodeToFile(_linker.getModule(), Out.os());
170 Out.os().close();
Dan Gohman4b7416b2010-05-27 20:19:47 +0000171
Dan Gohmand4c45432010-09-01 14:20:41 +0000172 if (Out.os().has_error()) {
Chris Lattnerb515d752009-08-23 07:49:08 +0000173 errMsg = "could not write bitcode file: ";
174 errMsg += path;
Dan Gohmand4c45432010-09-01 14:20:41 +0000175 Out.os().clear_error();
Chris Lattnerb515d752009-08-23 07:49:08 +0000176 return true;
177 }
178
Dan Gohmanf2914012010-08-20 16:59:15 +0000179 Out.keep();
Chris Lattnerb515d752009-08-23 07:49:08 +0000180 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000181}
182
183
Nick Kledzikef194ed2008-02-27 22:25:36 +0000184const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000185{
Nick Kledzikef194ed2008-02-27 22:25:36 +0000186 // make unique temp .s file to put generated assembly code
Nick Kledzik77595fc2008-02-26 20:26:43 +0000187 sys::Path uniqueAsmPath("lto-llvm.s");
Devang Patel9267d142010-12-06 18:04:39 +0000188 if ( uniqueAsmPath.createTemporaryFileOnDisk(false, &errMsg) )
Nick Kledzik77595fc2008-02-26 20:26:43 +0000189 return NULL;
190 sys::RemoveFileOnSignal(uniqueAsmPath);
191
192 // generate assembly code
Owen Andersoncb371882008-08-21 00:14:44 +0000193 bool genResult = false;
194 {
Dan Gohmand4c45432010-09-01 14:20:41 +0000195 tool_output_file asmFile(uniqueAsmPath.c_str(), errMsg);
Dan Gohmaned3e8b42008-08-21 15:33:45 +0000196 if (!errMsg.empty())
197 return NULL;
Dan Gohmand4c45432010-09-01 14:20:41 +0000198 genResult = this->generateAssemblyCode(asmFile.os(), errMsg);
199 asmFile.os().close();
200 if (asmFile.os().has_error()) {
201 asmFile.os().clear_error();
Dan Gohmanf2914012010-08-20 16:59:15 +0000202 return NULL;
203 }
204 asmFile.keep();
Owen Andersoncb371882008-08-21 00:14:44 +0000205 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000206 if ( genResult ) {
Dan Gohmand27047f2010-05-27 20:51:54 +0000207 uniqueAsmPath.eraseFromDisk();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000208 return NULL;
209 }
210
Nick Kledzikef194ed2008-02-27 22:25:36 +0000211 // make unique temp .o file to put generated object file
Nick Kledzik77595fc2008-02-26 20:26:43 +0000212 sys::PathWithStatus uniqueObjPath("lto-llvm.o");
Devang Patelb7bbd462010-12-03 23:58:31 +0000213 if ( uniqueObjPath.createTemporaryFileOnDisk(false, &errMsg) ) {
Dan Gohmand27047f2010-05-27 20:51:54 +0000214 uniqueAsmPath.eraseFromDisk();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000215 return NULL;
216 }
217 sys::RemoveFileOnSignal(uniqueObjPath);
218
219 // assemble the assembly code
Chris Lattner74382b72009-08-23 22:45:37 +0000220 const std::string& uniqueObjStr = uniqueObjPath.str();
221 bool asmResult = this->assemble(uniqueAsmPath.str(), uniqueObjStr, errMsg);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000222 if ( !asmResult ) {
Nick Kledzikef194ed2008-02-27 22:25:36 +0000223 // remove old buffer if compile() called twice
224 delete _nativeObjectFile;
Michael J. Spencerf2f516f2010-12-09 18:06:07 +0000225
Nick Kledzik77595fc2008-02-26 20:26:43 +0000226 // read .o file into memory buffer
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000227 OwningPtr<MemoryBuffer> BuffPtr;
228 if (error_code ec = MemoryBuffer::getFile(uniqueObjStr.c_str(),BuffPtr))
Michael J. Spencerf2f516f2010-12-09 18:06:07 +0000229 errMsg = ec.message();
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000230 _nativeObjectFile = BuffPtr.take();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000231 }
Nick Kledzikef194ed2008-02-27 22:25:36 +0000232
233 // remove temp files
Nick Kledzik77595fc2008-02-26 20:26:43 +0000234 uniqueAsmPath.eraseFromDisk();
235 uniqueObjPath.eraseFromDisk();
Nick Kledzikef194ed2008-02-27 22:25:36 +0000236
237 // return buffer, unless error
238 if ( _nativeObjectFile == NULL )
239 return NULL;
240 *length = _nativeObjectFile->getBufferSize();
241 return _nativeObjectFile->getBufferStart();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000242}
243
244
245bool LTOCodeGenerator::assemble(const std::string& asmPath,
246 const std::string& objPath, std::string& errMsg)
247{
Nick Kledzikcbad5862009-06-04 00:28:45 +0000248 sys::Path tool;
249 bool needsCompilerOptions = true;
250 if ( _assemblerPath ) {
251 tool = *_assemblerPath;
252 needsCompilerOptions = false;
Nick Lewycky195bea32009-04-30 15:24:09 +0000253 } else {
254 // find compiler driver
Nick Kledzikcbad5862009-06-04 00:28:45 +0000255 tool = sys::Program::FindProgramByName("gcc");
256 if ( tool.isEmpty() ) {
Nick Lewycky195bea32009-04-30 15:24:09 +0000257 errMsg = "can't locate gcc";
258 return true;
259 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000260 }
261
262 // build argument list
263 std::vector<const char*> args;
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000264 llvm::Triple targetTriple(_linker.getModule()->getTargetTriple());
265 const char *arch = targetTriple.getArchNameForAssembler();
266
Nick Kledzikcbad5862009-06-04 00:28:45 +0000267 args.push_back(tool.c_str());
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000268
269 if (targetTriple.getOS() == Triple::Darwin) {
Nick Kledzikd8b47112009-06-04 19:14:08 +0000270 // darwin specific command line options
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000271 if (arch != NULL) {
Nick Kledzik77595fc2008-02-26 20:26:43 +0000272 args.push_back("-arch");
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000273 args.push_back(arch);
Bob Wilson75d6ffd2009-06-22 18:01:28 +0000274 }
Nick Kledzikd8b47112009-06-04 19:14:08 +0000275 // add -static to assembler command line when code model requires
Chris Lattner5ef31a02010-03-12 18:44:54 +0000276 if ( (_assemblerPath != NULL) &&
277 (_codeModel == LTO_CODEGEN_PIC_MODEL_STATIC) )
Nick Kledzikd8b47112009-06-04 19:14:08 +0000278 args.push_back("-static");
Nick Kledzik77595fc2008-02-26 20:26:43 +0000279 }
Nick Kledzikcbad5862009-06-04 00:28:45 +0000280 if ( needsCompilerOptions ) {
281 args.push_back("-c");
282 args.push_back("-x");
283 args.push_back("assembler");
Rafael Espindola98197e52010-08-10 18:55:09 +0000284 } else {
285 for (std::vector<std::string>::iterator I = _assemblerArgs.begin(),
286 E = _assemblerArgs.end(); I != E; ++I) {
287 args.push_back(I->c_str());
288 }
Nick Kledzikcbad5862009-06-04 00:28:45 +0000289 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000290 args.push_back("-o");
291 args.push_back(objPath.c_str());
292 args.push_back(asmPath.c_str());
293 args.push_back(0);
294
295 // invoke assembler
Nick Kledzikcbad5862009-06-04 00:28:45 +0000296 if ( sys::Program::ExecuteAndWait(tool, &args[0], 0, 0, 0, 0, &errMsg) ) {
Nick Kledzik77595fc2008-02-26 20:26:43 +0000297 errMsg = "error in assembly";
298 return true;
299 }
300 return false; // success
301}
302
303
304
305bool LTOCodeGenerator::determineTarget(std::string& errMsg)
306{
307 if ( _target == NULL ) {
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000308 std::string Triple = _linker.getModule()->getTargetTriple();
309 if (Triple.empty())
310 Triple = sys::getHostTriple();
311
Nick Kledzik77595fc2008-02-26 20:26:43 +0000312 // create target machine from info for merged modules
Daniel Dunbar4bd03ab2009-08-03 04:20:57 +0000313 const Target *march = TargetRegistry::lookupTarget(Triple, errMsg);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000314 if ( march == NULL )
315 return true;
Bill Wendling604a8182008-06-18 06:35:30 +0000316
Nick Kledzikf5a1c35f12009-06-03 22:52:12 +0000317 // The relocation model is actually a static member of TargetMachine
318 // and needs to be set before the TargetMachine is instantiated.
319 switch( _codeModel ) {
320 case LTO_CODEGEN_PIC_MODEL_STATIC:
321 TargetMachine::setRelocationModel(Reloc::Static);
322 break;
323 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
324 TargetMachine::setRelocationModel(Reloc::PIC_);
325 break;
326 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
327 TargetMachine::setRelocationModel(Reloc::DynamicNoPIC);
328 break;
329 }
330
Bill Wendling604a8182008-06-18 06:35:30 +0000331 // construct LTModule, hand over ownership of module and target
Bill Wendling81043ee2010-05-11 00:30:02 +0000332 SubtargetFeatures Features;
Rafael Espindola2d643ef2010-08-11 00:15:13 +0000333 Features.getDefaultSubtargetFeatures(_mCpu, llvm::Triple(Triple));
Bill Wendling81043ee2010-05-11 00:30:02 +0000334 std::string FeatureStr = Features.getString();
Viktor Kutuzov308f6632009-11-25 22:44:18 +0000335 _target = march->createTargetMachine(Triple, FeatureStr);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000336 }
337 return false;
338}
339
Chris Lattner5ef31a02010-03-12 18:44:54 +0000340void LTOCodeGenerator::applyScopeRestrictions() {
341 if (_scopeRestrictionsDone) return;
342 Module *mergedModule = _linker.getModule();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000343
Chris Lattner5ef31a02010-03-12 18:44:54 +0000344 // Start off with a verification pass.
345 PassManager passes;
346 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000347
Chris Lattner5ef31a02010-03-12 18:44:54 +0000348 // mark which symbols can not be internalized
349 if (!_mustPreserveSymbols.empty()) {
Rafael Espindola89b93722010-12-10 07:39:47 +0000350 MCContext Context(*_target->getMCAsmInfo(), NULL);
Chris Lattnerb87c3052010-03-12 20:47:28 +0000351 Mangler mangler(Context, *_target->getTargetData());
Chris Lattner5ef31a02010-03-12 18:44:54 +0000352 std::vector<const char*> mustPreserveList;
Rafael Espindolaef1860a2011-02-11 05:23:09 +0000353 SmallString<64> Buffer;
Chris Lattner5ef31a02010-03-12 18:44:54 +0000354 for (Module::iterator f = mergedModule->begin(),
355 e = mergedModule->end(); f != e; ++f) {
Rafael Espindolad7401b32011-02-12 00:19:56 +0000356 Buffer.clear();
Rafael Espindolaef1860a2011-02-11 05:23:09 +0000357 mangler.getNameWithPrefix(Buffer, f, false);
Chris Lattner5ef31a02010-03-12 18:44:54 +0000358 if (!f->isDeclaration() &&
Rafael Espindolaef1860a2011-02-11 05:23:09 +0000359 _mustPreserveSymbols.count(Buffer))
Chris Lattner5ef31a02010-03-12 18:44:54 +0000360 mustPreserveList.push_back(::strdup(f->getNameStr().c_str()));
Nick Kledzik77595fc2008-02-26 20:26:43 +0000361 }
Chris Lattner5ef31a02010-03-12 18:44:54 +0000362 for (Module::global_iterator v = mergedModule->global_begin(),
363 e = mergedModule->global_end(); v != e; ++v) {
Rafael Espindolad7401b32011-02-12 00:19:56 +0000364 Buffer.clear();
Rafael Espindolaef1860a2011-02-11 05:23:09 +0000365 mangler.getNameWithPrefix(Buffer, v, false);
Bill Wendlingc3d0e0c2010-04-27 00:55:25 +0000366 if (!v->isDeclaration() &&
Rafael Espindolaef1860a2011-02-11 05:23:09 +0000367 _mustPreserveSymbols.count(Buffer))
Chris Lattner5ef31a02010-03-12 18:44:54 +0000368 mustPreserveList.push_back(::strdup(v->getNameStr().c_str()));
369 }
Rafael Espindola2e3066b2011-02-12 18:03:13 +0000370 for (Module::alias_iterator a = mergedModule->alias_begin(),
371 e = mergedModule->alias_end(); a != e; ++a) {
372 Buffer.clear();
373 mangler.getNameWithPrefix(Buffer, a, false);
374 if (!a->isDeclaration() &&
375 _mustPreserveSymbols.count(Buffer))
376 mustPreserveList.push_back(::strdup(a->getNameStr().c_str()));
377 }
Chris Lattner5ef31a02010-03-12 18:44:54 +0000378 passes.add(createInternalizePass(mustPreserveList));
379 }
380
381 // apply scope restrictions
382 passes.run(*mergedModule);
383
384 _scopeRestrictionsDone = true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000385}
386
Nick Kledzik77595fc2008-02-26 20:26:43 +0000387/// Optimize merged modules using various IPO passes
Dan Gohmand4c45432010-09-01 14:20:41 +0000388bool LTOCodeGenerator::generateAssemblyCode(raw_ostream& out,
Owen Andersoncb371882008-08-21 00:14:44 +0000389 std::string& errMsg)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000390{
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000391 if ( this->determineTarget(errMsg) )
Nick Kledzik77595fc2008-02-26 20:26:43 +0000392 return true;
393
394 // mark which symbols can not be internalized
395 this->applyScopeRestrictions();
396
397 Module* mergedModule = _linker.getModule();
398
Nick Kledzik920ae982008-07-08 21:14:10 +0000399 // if options were requested, set them
400 if ( !_codegenOptions.empty() )
401 cl::ParseCommandLineOptions(_codegenOptions.size(),
Dan Gohman43bc70e2010-04-17 17:44:03 +0000402 const_cast<char **>(&_codegenOptions[0]));
Devang Patela93ae712008-07-03 22:53:14 +0000403
Nick Kledzik77595fc2008-02-26 20:26:43 +0000404 // Instantiate the pass manager to organize the passes.
405 PassManager passes;
406
407 // Start off with a verification pass.
408 passes.add(createVerifierPass());
409
410 // Add an appropriate TargetData instance for this module...
411 passes.add(new TargetData(*_target->getTargetData()));
412
Daniel Dunbar006a0342009-06-03 21:06:14 +0000413 createStandardLTOPasses(&passes, /*Internalize=*/ false, !DisableInline,
Daniel Dunbar006a0342009-06-03 21:06:14 +0000414 /*VerifyEach=*/ false);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000415
416 // Make sure everything is still good.
417 passes.add(createVerifierPass());
418
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000419 FunctionPassManager* codeGenPasses = new FunctionPassManager(mergedModule);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000420
421 codeGenPasses->add(new TargetData(*_target->getTargetData()));
422
Dan Gohmand4c45432010-09-01 14:20:41 +0000423 formatted_raw_ostream Out(out);
424
425 if (_target->addPassesToEmitFile(*codeGenPasses, Out,
Chris Lattner5669e302010-02-03 05:55:08 +0000426 TargetMachine::CGFT_AssemblyFile,
427 CodeGenOpt::Aggressive)) {
428 errMsg = "target file type not supported";
429 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000430 }
431
Nick Kledzik77595fc2008-02-26 20:26:43 +0000432 // Run our queue of passes all at once now, efficiently.
433 passes.run(*mergedModule);
434
435 // Run the code generator, and write assembly file
436 codeGenPasses->doInitialization();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000437
Bill Wendling604a8182008-06-18 06:35:30 +0000438 for (Module::iterator
439 it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it)
440 if (!it->isDeclaration())
441 codeGenPasses->run(*it);
442
443 codeGenPasses->doFinalization();
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000444
Nick Kledzik77595fc2008-02-26 20:26:43 +0000445 return false; // success
446}
447
448
Nick Kledzik920ae982008-07-08 21:14:10 +0000449/// Optimize merged modules using various IPO passes
450void LTOCodeGenerator::setCodeGenDebugOptions(const char* options)
451{
Benjamin Kramerd4f19592010-01-11 18:03:24 +0000452 for (std::pair<StringRef, StringRef> o = getToken(options);
453 !o.first.empty(); o = getToken(o.second)) {
Nick Kledzik920ae982008-07-08 21:14:10 +0000454 // ParseCommandLineOptions() expects argv[0] to be program name.
455 // Lazily add that.
456 if ( _codegenOptions.empty() )
457 _codegenOptions.push_back("libLTO");
Benjamin Kramerd4f19592010-01-11 18:03:24 +0000458 _codegenOptions.push_back(strdup(o.first.str().c_str()));
Nick Kledzik920ae982008-07-08 21:14:10 +0000459 }
460}