blob: 3f82c529dc9c69e3cf0d00dd7461e8f8b02d06a0 [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.
Bill Wendling76b13ed2012-03-31 10:50:14 +00007//
Nick Kledzik77595fc2008-02-26 20:26:43 +00008//===----------------------------------------------------------------------===//
9//
Bill Wendling76b13ed2012-03-31 10:50:14 +000010// This file implements the Link Time Optimization library. This library is
Nick Kledzik77595fc2008-02-26 20:26:43 +000011// 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"
Nick Kledzik77595fc2008-02-26 20:26:43 +000017#include "llvm/Constants.h"
18#include "llvm/DerivedTypes.h"
Nick Lewycky8189d402009-06-17 06:52:10 +000019#include "llvm/Linker.h"
Owen Anderson0e7a5462009-07-02 00:31:14 +000020#include "llvm/LLVMContext.h"
Nick Lewycky8189d402009-06-17 06:52:10 +000021#include "llvm/Module.h"
Nick Lewycky8189d402009-06-17 06:52:10 +000022#include "llvm/PassManager.h"
23#include "llvm/ADT/StringExtras.h"
Viktor Kutuzov51cdac02009-11-17 18:48:27 +000024#include "llvm/ADT/Triple.h"
Nick Lewycky8189d402009-06-17 06:52:10 +000025#include "llvm/Analysis/Passes.h"
Rafael Espindolac684e832011-08-02 21:50:27 +000026#include "llvm/Analysis/Verifier.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"
Evan Chengab8be962011-06-29 01:14:12 +000030#include "llvm/MC/SubtargetFeature.h"
Chris Lattner5ef31a02010-03-12 18:44:54 +000031#include "llvm/Target/Mangler.h"
Chris Lattner5ef31a02010-03-12 18:44:54 +000032#include "llvm/Target/TargetOptions.h"
33#include "llvm/Target/TargetData.h"
34#include "llvm/Target/TargetMachine.h"
Evan Cheng0e6a0522011-07-18 20:57:22 +000035#include "llvm/Target/TargetRegisterInfo.h"
Nick Kledzik920ae982008-07-08 21:14:10 +000036#include "llvm/Support/CommandLine.h"
David Blaikie4d6ccb52012-01-20 21:51:11 +000037#include "llvm/Support/ErrorHandling.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/SystemUtils.h"
Dan Gohman9f36c4e2010-10-07 20:48:46 +000041#include "llvm/Support/ToolOutputFile.h"
Michael J. Spencer3cc52ea2010-11-29 18:47:54 +000042#include "llvm/Support/Host.h"
43#include "llvm/Support/Program.h"
44#include "llvm/Support/Signals.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000045#include "llvm/Support/TargetRegistry.h"
46#include "llvm/Support/TargetSelect.h"
Michael J. Spencerf2f516f2010-12-09 18:06:07 +000047#include "llvm/Support/system_error.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000048#include "llvm/Config/config.h"
Rafael Espindolac684e832011-08-02 21:50:27 +000049#include "llvm/Transforms/IPO.h"
Rafael Espindola3d453ac2011-08-02 21:50:24 +000050#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Nick Lewycky8189d402009-06-17 06:52:10 +000051#include <cstdlib>
Nick Kledzik77595fc2008-02-26 20:26:43 +000052#include <unistd.h>
Nick Kledzik77595fc2008-02-26 20:26:43 +000053#include <fcntl.h>
Nick Kledzik77595fc2008-02-26 20:26:43 +000054using namespace llvm;
55
Nick Kledzik920ae982008-07-08 21:14:10 +000056static cl::opt<bool> DisableInline("disable-inlining",
57 cl::desc("Do not run the inliner pass"));
Nick Kledzik77595fc2008-02-26 20:26:43 +000058
Bill Wendlingcaf71d42012-03-31 10:49:43 +000059const char* LTOCodeGenerator::getVersionString() {
Nick Kledzik77595fc2008-02-26 20:26:43 +000060#ifdef LLVM_VERSION_INFO
Bill Wendlingcaf71d42012-03-31 10:49:43 +000061 return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
Nick Kledzik77595fc2008-02-26 20:26:43 +000062#else
Bill Wendlingcaf71d42012-03-31 10:49:43 +000063 return PACKAGE_NAME " version " PACKAGE_VERSION;
Nick Kledzik77595fc2008-02-26 20:26:43 +000064#endif
65}
66
Bill Wendling76b13ed2012-03-31 10:50:14 +000067LTOCodeGenerator::LTOCodeGenerator()
Owen Anderson0e7a5462009-07-02 00:31:14 +000068 : _context(getGlobalContext()),
Owen Anderson8b477ed2009-07-01 16:58:40 +000069 _linker("LinkTimeOptimizer", "ld-temp.o", _context), _target(NULL),
Nick Kledzik77595fc2008-02-26 20:26:43 +000070 _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false),
Nick Kledzikef194ed2008-02-27 22:25:36 +000071 _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
Rafael Espindolae9efea12011-02-24 21:04:06 +000072 _nativeObjectFile(NULL)
Nick Kledzik77595fc2008-02-26 20:26:43 +000073{
Nick Lewyckyd42b58b2009-07-26 22:16:39 +000074 InitializeAllTargets();
Evan Chenge78085a2011-07-22 21:58:54 +000075 InitializeAllTargetMCs();
Nick Lewyckyd42b58b2009-07-26 22:16:39 +000076 InitializeAllAsmPrinters();
Nick Kledzik77595fc2008-02-26 20:26:43 +000077}
78
Bill Wendlingcaf71d42012-03-31 10:49:43 +000079LTOCodeGenerator::~LTOCodeGenerator() {
80 delete _target;
81 delete _nativeObjectFile;
82
Bill Wendlingf2cc2ee2012-03-31 10:51:45 +000083 for (std::vector<char*>::iterator I = _codegenOptions.begin(),
Bill Wendlingcaf71d42012-03-31 10:49:43 +000084 E = _codegenOptions.end(); I != E; ++I)
85 free(*I);
Nick Kledzik77595fc2008-02-26 20:26:43 +000086}
87
Nick Kledzik77595fc2008-02-26 20:26:43 +000088bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg)
89{
Rafael Espindola38c4e532011-03-02 04:14:42 +000090 bool ret = _linker.LinkInModule(mod->getLLVVMModule(), &errMsg);
91
92 const std::vector<const char*> &undefs = mod->getAsmUndefinedRefs();
93 for (int i = 0, e = undefs.size(); i != e; ++i)
94 _asmUndefinedRefs[undefs[i]] = 1;
95
96 return ret;
Nick Kledzik77595fc2008-02-26 20:26:43 +000097}
Bill Wendling76b13ed2012-03-31 10:50:14 +000098
Nick Kledzik77595fc2008-02-26 20:26:43 +000099
100bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug, std::string& errMsg)
101{
102 switch (debug) {
103 case LTO_DEBUG_MODEL_NONE:
104 _emitDwarfDebugInfo = false;
105 return false;
Bill Wendling76b13ed2012-03-31 10:50:14 +0000106
Nick Kledzik77595fc2008-02-26 20:26:43 +0000107 case LTO_DEBUG_MODEL_DWARF:
108 _emitDwarfDebugInfo = true;
109 return false;
110 }
David Blaikie4d6ccb52012-01-20 21:51:11 +0000111 llvm_unreachable("Unknown debug format!");
Nick Kledzik77595fc2008-02-26 20:26:43 +0000112}
113
114
Bill Wendling76b13ed2012-03-31 10:50:14 +0000115bool 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 }
David Blaikie4d6ccb52012-01-20 21:51:11 +0000125 llvm_unreachable("Unknown PIC model!");
Nick Kledzik77595fc2008-02-26 20:26:43 +0000126}
127
Rafael Espindola2d643ef2010-08-11 00:15:13 +0000128void LTOCodeGenerator::setCpu(const char* mCpu)
129{
130 _mCpu = mCpu;
131}
132
Nick Kledzik77595fc2008-02-26 20:26:43 +0000133void LTOCodeGenerator::addMustPreserveSymbol(const char* sym)
134{
135 _mustPreserveSymbols[sym] = 1;
136}
137
138
Chris Lattnerb515d752009-08-23 07:49:08 +0000139bool LTOCodeGenerator::writeMergedModules(const char *path,
140 std::string &errMsg) {
141 if (determineTarget(errMsg))
142 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000143
Bill Wendling76b13ed2012-03-31 10:50:14 +0000144 // mark which symbols can not be internalized
Chris Lattnerb515d752009-08-23 07:49:08 +0000145 applyScopeRestrictions();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000146
Chris Lattnerb515d752009-08-23 07:49:08 +0000147 // create output file
148 std::string ErrInfo;
Dan Gohmanf2914012010-08-20 16:59:15 +0000149 tool_output_file Out(path, ErrInfo,
150 raw_fd_ostream::F_Binary);
Chris Lattnerb515d752009-08-23 07:49:08 +0000151 if (!ErrInfo.empty()) {
152 errMsg = "could not open bitcode file for writing: ";
153 errMsg += path;
154 return true;
155 }
Bill Wendling76b13ed2012-03-31 10:50:14 +0000156
Chris Lattnerb515d752009-08-23 07:49:08 +0000157 // write bitcode to it
Dan Gohmand4c45432010-09-01 14:20:41 +0000158 WriteBitcodeToFile(_linker.getModule(), Out.os());
159 Out.os().close();
Dan Gohman4b7416b2010-05-27 20:19:47 +0000160
Dan Gohmand4c45432010-09-01 14:20:41 +0000161 if (Out.os().has_error()) {
Chris Lattnerb515d752009-08-23 07:49:08 +0000162 errMsg = "could not write bitcode file: ";
163 errMsg += path;
Dan Gohmand4c45432010-09-01 14:20:41 +0000164 Out.os().clear_error();
Chris Lattnerb515d752009-08-23 07:49:08 +0000165 return true;
166 }
Bill Wendling76b13ed2012-03-31 10:50:14 +0000167
Dan Gohmanf2914012010-08-20 16:59:15 +0000168 Out.keep();
Chris Lattnerb515d752009-08-23 07:49:08 +0000169 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000170}
171
172
Rafael Espindola6421a882011-03-22 20:57:13 +0000173bool LTOCodeGenerator::compile_to_file(const char** name, std::string& errMsg)
174{
175 // make unique temp .o file to put generated object file
176 sys::PathWithStatus uniqueObjPath("lto-llvm.o");
177 if ( uniqueObjPath.createTemporaryFileOnDisk(false, &errMsg) ) {
178 uniqueObjPath.eraseFromDisk();
179 return true;
180 }
181 sys::RemoveFileOnSignal(uniqueObjPath);
182
183 // generate object file
184 bool genResult = false;
185 tool_output_file objFile(uniqueObjPath.c_str(), errMsg);
186 if (!errMsg.empty())
John Criswell3f0e2372011-08-18 01:19:05 +0000187 return true;
Rafael Espindola6421a882011-03-22 20:57:13 +0000188 genResult = this->generateObjectFile(objFile.os(), errMsg);
189 objFile.os().close();
190 if (objFile.os().has_error()) {
191 objFile.os().clear_error();
192 return true;
193 }
194 objFile.keep();
195 if ( genResult ) {
196 uniqueObjPath.eraseFromDisk();
197 return true;
198 }
199
200 _nativeObjectPath = uniqueObjPath.str();
201 *name = _nativeObjectPath.c_str();
202 return false;
203}
204
Nick Kledzikef194ed2008-02-27 22:25:36 +0000205const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000206{
Rafael Espindola6421a882011-03-22 20:57:13 +0000207 const char *name;
208 if (compile_to_file(&name, errMsg))
209 return NULL;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000210
Rafael Espindola6421a882011-03-22 20:57:13 +0000211 // remove old buffer if compile() called twice
212 delete _nativeObjectFile;
Nick Kledzikef194ed2008-02-27 22:25:36 +0000213
Rafael Espindola6421a882011-03-22 20:57:13 +0000214 // read .o file into memory buffer
215 OwningPtr<MemoryBuffer> BuffPtr;
216 if (error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) {
217 errMsg = ec.message();
218 return NULL;
219 }
220 _nativeObjectFile = BuffPtr.take();
Rafael Espindolae9efea12011-02-24 21:04:06 +0000221
Rafael Espindola6421a882011-03-22 20:57:13 +0000222 // remove temp files
223 sys::Path(_nativeObjectPath).eraseFromDisk();
Rafael Espindolae9efea12011-02-24 21:04:06 +0000224
Rafael Espindola6421a882011-03-22 20:57:13 +0000225 // return buffer, unless error
226 if ( _nativeObjectFile == NULL )
227 return NULL;
228 *length = _nativeObjectFile->getBufferSize();
229 return _nativeObjectFile->getBufferStart();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000230}
231
Nick Kledzik77595fc2008-02-26 20:26:43 +0000232bool LTOCodeGenerator::determineTarget(std::string& errMsg)
233{
234 if ( _target == NULL ) {
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000235 std::string Triple = _linker.getModule()->getTargetTriple();
236 if (Triple.empty())
Sebastian Pop01738642011-11-01 21:32:20 +0000237 Triple = sys::getDefaultTargetTriple();
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000238
Nick Kledzik77595fc2008-02-26 20:26:43 +0000239 // create target machine from info for merged modules
Daniel Dunbar4bd03ab2009-08-03 04:20:57 +0000240 const Target *march = TargetRegistry::lookupTarget(Triple, errMsg);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000241 if ( march == NULL )
242 return true;
Bill Wendling604a8182008-06-18 06:35:30 +0000243
Nick Kledzikf5a1c35f12009-06-03 22:52:12 +0000244 // The relocation model is actually a static member of TargetMachine
245 // and needs to be set before the TargetMachine is instantiated.
Evan Cheng43966132011-07-19 06:37:02 +0000246 Reloc::Model RelocModel = Reloc::Default;
Nick Kledzikf5a1c35f12009-06-03 22:52:12 +0000247 switch( _codeModel ) {
248 case LTO_CODEGEN_PIC_MODEL_STATIC:
Evan Cheng43966132011-07-19 06:37:02 +0000249 RelocModel = Reloc::Static;
Nick Kledzikf5a1c35f12009-06-03 22:52:12 +0000250 break;
251 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
Evan Cheng43966132011-07-19 06:37:02 +0000252 RelocModel = Reloc::PIC_;
Nick Kledzikf5a1c35f12009-06-03 22:52:12 +0000253 break;
254 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
Evan Cheng43966132011-07-19 06:37:02 +0000255 RelocModel = Reloc::DynamicNoPIC;
Nick Kledzikf5a1c35f12009-06-03 22:52:12 +0000256 break;
257 }
258
Nick Lewyckyfce6b502011-07-25 21:12:44 +0000259 // construct LTOModule, hand over ownership of module and target
Bill Wendling81043ee2010-05-11 00:30:02 +0000260 SubtargetFeatures Features;
Evan Cheng276365d2011-06-30 01:53:36 +0000261 Features.getDefaultSubtargetFeatures(llvm::Triple(Triple));
Bill Wendling81043ee2010-05-11 00:30:02 +0000262 std::string FeatureStr = Features.getString();
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000263 TargetOptions Options;
264 _target = march->createTargetMachine(Triple, _mCpu, FeatureStr, Options,
Evan Cheng43966132011-07-19 06:37:02 +0000265 RelocModel);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000266 }
267 return false;
268}
269
Rafael Espindola38c4e532011-03-02 04:14:42 +0000270void LTOCodeGenerator::applyRestriction(GlobalValue &GV,
271 std::vector<const char*> &mustPreserveList,
272 SmallPtrSet<GlobalValue*, 8> &asmUsed,
273 Mangler &mangler) {
274 SmallString<64> Buffer;
275 mangler.getNameWithPrefix(Buffer, &GV, false);
276
277 if (GV.isDeclaration())
278 return;
279 if (_mustPreserveSymbols.count(Buffer))
280 mustPreserveList.push_back(GV.getName().data());
281 if (_asmUndefinedRefs.count(Buffer))
282 asmUsed.insert(&GV);
283}
284
285static void findUsedValues(GlobalVariable *LLVMUsed,
286 SmallPtrSet<GlobalValue*, 8> &UsedValues) {
287 if (LLVMUsed == 0) return;
288
289 ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer());
290 if (Inits == 0) return;
291
292 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
Bill Wendling76b13ed2012-03-31 10:50:14 +0000293 if (GlobalValue *GV =
Rafael Espindola38c4e532011-03-02 04:14:42 +0000294 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
295 UsedValues.insert(GV);
296}
297
Chris Lattner5ef31a02010-03-12 18:44:54 +0000298void LTOCodeGenerator::applyScopeRestrictions() {
299 if (_scopeRestrictionsDone) return;
300 Module *mergedModule = _linker.getModule();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000301
Chris Lattner5ef31a02010-03-12 18:44:54 +0000302 // Start off with a verification pass.
303 PassManager passes;
304 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000305
Bill Wendling76b13ed2012-03-31 10:50:14 +0000306 // mark which symbols can not be internalized
Evan Cheng203576a2011-07-20 19:50:42 +0000307 MCContext Context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(), NULL);
Rafael Espindola38c4e532011-03-02 04:14:42 +0000308 Mangler mangler(Context, *_target->getTargetData());
309 std::vector<const char*> mustPreserveList;
310 SmallPtrSet<GlobalValue*, 8> asmUsed;
311
312 for (Module::iterator f = mergedModule->begin(),
313 e = mergedModule->end(); f != e; ++f)
314 applyRestriction(*f, mustPreserveList, asmUsed, mangler);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000315 for (Module::global_iterator v = mergedModule->global_begin(),
Rafael Espindola38c4e532011-03-02 04:14:42 +0000316 e = mergedModule->global_end(); v != e; ++v)
317 applyRestriction(*v, mustPreserveList, asmUsed, mangler);
318 for (Module::alias_iterator a = mergedModule->alias_begin(),
319 e = mergedModule->alias_end(); a != e; ++a)
320 applyRestriction(*a, mustPreserveList, asmUsed, mangler);
321
322 GlobalVariable *LLVMCompilerUsed =
323 mergedModule->getGlobalVariable("llvm.compiler.used");
324 findUsedValues(LLVMCompilerUsed, asmUsed);
325 if (LLVMCompilerUsed)
326 LLVMCompilerUsed->eraseFromParent();
327
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000328 llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(_context);
Rafael Espindola38c4e532011-03-02 04:14:42 +0000329 std::vector<Constant*> asmUsed2;
330 for (SmallPtrSet<GlobalValue*, 16>::const_iterator i = asmUsed.begin(),
331 e = asmUsed.end(); i !=e; ++i) {
332 GlobalValue *GV = *i;
333 Constant *c = ConstantExpr::getBitCast(GV, i8PTy);
334 asmUsed2.push_back(c);
Chris Lattner5ef31a02010-03-12 18:44:54 +0000335 }
Rafael Espindola38c4e532011-03-02 04:14:42 +0000336
337 llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size());
338 LLVMCompilerUsed =
339 new llvm::GlobalVariable(*mergedModule, ATy, false,
340 llvm::GlobalValue::AppendingLinkage,
341 llvm::ConstantArray::get(ATy, asmUsed2),
342 "llvm.compiler.used");
343
344 LLVMCompilerUsed->setSection("llvm.metadata");
345
346 passes.add(createInternalizePass(mustPreserveList));
347
Chris Lattner5ef31a02010-03-12 18:44:54 +0000348 // apply scope restrictions
349 passes.run(*mergedModule);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000350
Chris Lattner5ef31a02010-03-12 18:44:54 +0000351 _scopeRestrictionsDone = true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000352}
353
Nick Kledzik77595fc2008-02-26 20:26:43 +0000354/// Optimize merged modules using various IPO passes
Chris Lattner817a01f2011-05-22 00:20:07 +0000355bool LTOCodeGenerator::generateObjectFile(raw_ostream &out,
356 std::string &errMsg) {
Bill Wendling76b13ed2012-03-31 10:50:14 +0000357 if ( this->determineTarget(errMsg) )
Nick Kledzik77595fc2008-02-26 20:26:43 +0000358 return true;
359
Bill Wendling76b13ed2012-03-31 10:50:14 +0000360 // mark which symbols can not be internalized
Nick Kledzik77595fc2008-02-26 20:26:43 +0000361 this->applyScopeRestrictions();
362
363 Module* mergedModule = _linker.getModule();
364
Nick Kledzik920ae982008-07-08 21:14:10 +0000365 // if options were requested, set them
366 if ( !_codegenOptions.empty() )
Bill Wendling76b13ed2012-03-31 10:50:14 +0000367 cl::ParseCommandLineOptions(_codegenOptions.size(),
Dan Gohman43bc70e2010-04-17 17:44:03 +0000368 const_cast<char **>(&_codegenOptions[0]));
Devang Patela93ae712008-07-03 22:53:14 +0000369
Nick Kledzik77595fc2008-02-26 20:26:43 +0000370 // Instantiate the pass manager to organize the passes.
371 PassManager passes;
372
373 // Start off with a verification pass.
374 passes.add(createVerifierPass());
375
376 // Add an appropriate TargetData instance for this module...
377 passes.add(new TargetData(*_target->getTargetData()));
Bill Wendling76b13ed2012-03-31 10:50:14 +0000378
Chris Lattner817a01f2011-05-22 00:20:07 +0000379 PassManagerBuilder().populateLTOPassManager(passes, /*Internalize=*/ false,
380 !DisableInline);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000381
382 // Make sure everything is still good.
383 passes.add(createVerifierPass());
384
Chris Lattner817a01f2011-05-22 00:20:07 +0000385 FunctionPassManager *codeGenPasses = new FunctionPassManager(mergedModule);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000386
387 codeGenPasses->add(new TargetData(*_target->getTargetData()));
388
Dan Gohmand4c45432010-09-01 14:20:41 +0000389 formatted_raw_ostream Out(out);
390
391 if (_target->addPassesToEmitFile(*codeGenPasses, Out,
Rafael Espindolae9efea12011-02-24 21:04:06 +0000392 TargetMachine::CGFT_ObjectFile,
Chris Lattner5669e302010-02-03 05:55:08 +0000393 CodeGenOpt::Aggressive)) {
394 errMsg = "target file type not supported";
395 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000396 }
397
Nick Kledzik77595fc2008-02-26 20:26:43 +0000398 // Run our queue of passes all at once now, efficiently.
399 passes.run(*mergedModule);
400
401 // Run the code generator, and write assembly file
402 codeGenPasses->doInitialization();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000403
Bill Wendling604a8182008-06-18 06:35:30 +0000404 for (Module::iterator
405 it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it)
406 if (!it->isDeclaration())
407 codeGenPasses->run(*it);
408
409 codeGenPasses->doFinalization();
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000410 delete codeGenPasses;
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000411
Nick Kledzik77595fc2008-02-26 20:26:43 +0000412 return false; // success
413}
414
Bill Wendlingcaf71d42012-03-31 10:49:43 +0000415/// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
416/// LTO problems.
417void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) {
418 for (std::pair<StringRef, StringRef> o = getToken(options);
419 !o.first.empty(); o = getToken(o.second)) {
420 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add
421 // that.
422 if ( _codegenOptions.empty() )
423 _codegenOptions.push_back(strdup("libLTO"));
424 _codegenOptions.push_back(strdup(o.first.str().c_str()));
425 }
Nick Kledzik920ae982008-07-08 21:14:10 +0000426}