blob: 08139470be4ccaa522e7854cdc8ff9dbe9603aa7 [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 "LTOCodeGenerator.h"
Bill Wendlingab53bc72012-03-31 11:10:35 +000016#include "LTOModule.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"
Nick Lewycky8189d402009-06-17 06:52:10 +000023#include "llvm/Analysis/Passes.h"
Rafael Espindolac684e832011-08-02 21:50:27 +000024#include "llvm/Analysis/Verifier.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000025#include "llvm/Bitcode/ReaderWriter.h"
Bill Wendlingc94c5622012-03-31 11:15:43 +000026#include "llvm/Config/config.h"
Chris Lattner5ef31a02010-03-12 18:44:54 +000027#include "llvm/MC/MCAsmInfo.h"
28#include "llvm/MC/MCContext.h"
Evan Chengab8be962011-06-29 01:14:12 +000029#include "llvm/MC/SubtargetFeature.h"
Chris Lattner5ef31a02010-03-12 18:44:54 +000030#include "llvm/Target/Mangler.h"
Chris Lattner5ef31a02010-03-12 18:44:54 +000031#include "llvm/Target/TargetOptions.h"
32#include "llvm/Target/TargetData.h"
33#include "llvm/Target/TargetMachine.h"
Evan Cheng0e6a0522011-07-18 20:57:22 +000034#include "llvm/Target/TargetRegisterInfo.h"
Bill Wendlingc94c5622012-03-31 11:15:43 +000035#include "llvm/Transforms/IPO.h"
36#include "llvm/Transforms/IPO/PassManagerBuilder.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"
Dan Gohman9f36c4e2010-10-07 20:48:46 +000040#include "llvm/Support/ToolOutputFile.h"
Michael J. Spencer3cc52ea2010-11-29 18:47:54 +000041#include "llvm/Support/Host.h"
Michael J. Spencer3cc52ea2010-11-29 18:47:54 +000042#include "llvm/Support/Signals.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000043#include "llvm/Support/TargetRegistry.h"
44#include "llvm/Support/TargetSelect.h"
Michael J. Spencerf2f516f2010-12-09 18:06:07 +000045#include "llvm/Support/system_error.h"
Bill Wendlingc94c5622012-03-31 11:15:43 +000046#include "llvm/ADT/StringExtras.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000047using namespace llvm;
48
Bill Wendling97d99032012-04-05 21:26:44 +000049static cl::opt<bool> DisableInline("disable-inlining", cl::init(false),
Nick Kledzik920ae982008-07-08 21:14:10 +000050 cl::desc("Do not run the inliner pass"));
Nick Kledzik77595fc2008-02-26 20:26:43 +000051
Bill Wendling97d99032012-04-05 21:26:44 +000052static cl::opt<bool> DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
Bill Wendling3197b442012-04-02 22:16:50 +000053 cl::desc("Do not run the GVN load PRE pass"));
54
Bill Wendlingcaf71d42012-03-31 10:49:43 +000055const char* LTOCodeGenerator::getVersionString() {
Nick Kledzik77595fc2008-02-26 20:26:43 +000056#ifdef LLVM_VERSION_INFO
Bill Wendlingcaf71d42012-03-31 10:49:43 +000057 return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
Nick Kledzik77595fc2008-02-26 20:26:43 +000058#else
Bill Wendlingcaf71d42012-03-31 10:49:43 +000059 return PACKAGE_NAME " version " PACKAGE_VERSION;
Nick Kledzik77595fc2008-02-26 20:26:43 +000060#endif
61}
62
Bill Wendling76b13ed2012-03-31 10:50:14 +000063LTOCodeGenerator::LTOCodeGenerator()
Bill Wendlingc94c5622012-03-31 11:15:43 +000064 : _context(getGlobalContext()),
65 _linker("LinkTimeOptimizer", "ld-temp.o", _context), _target(NULL),
66 _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false),
Rafael Espindola4d2e9d92012-04-16 10:58:38 +000067 _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
Bill Wendlingc94c5622012-03-31 11:15:43 +000068 _nativeObjectFile(NULL) {
69 InitializeAllTargets();
70 InitializeAllTargetMCs();
71 InitializeAllAsmPrinters();
Nick Kledzik77595fc2008-02-26 20:26:43 +000072}
73
Bill Wendlingcaf71d42012-03-31 10:49:43 +000074LTOCodeGenerator::~LTOCodeGenerator() {
75 delete _target;
76 delete _nativeObjectFile;
77
Bill Wendlingf2cc2ee2012-03-31 10:51:45 +000078 for (std::vector<char*>::iterator I = _codegenOptions.begin(),
Bill Wendlingcaf71d42012-03-31 10:49:43 +000079 E = _codegenOptions.end(); I != E; ++I)
80 free(*I);
Nick Kledzik77595fc2008-02-26 20:26:43 +000081}
82
Bill Wendlingc94c5622012-03-31 11:15:43 +000083bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) {
Rafael Espindola38c4e532011-03-02 04:14:42 +000084 bool ret = _linker.LinkInModule(mod->getLLVVMModule(), &errMsg);
85
86 const std::vector<const char*> &undefs = mod->getAsmUndefinedRefs();
87 for (int i = 0, e = undefs.size(); i != e; ++i)
88 _asmUndefinedRefs[undefs[i]] = 1;
89
90 return ret;
Nick Kledzik77595fc2008-02-26 20:26:43 +000091}
Bill Wendling76b13ed2012-03-31 10:50:14 +000092
Bill Wendlingc94c5622012-03-31 11:15:43 +000093bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug,
94 std::string& errMsg) {
95 switch (debug) {
96 case LTO_DEBUG_MODEL_NONE:
97 _emitDwarfDebugInfo = false;
98 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +000099
Bill Wendlingc94c5622012-03-31 11:15:43 +0000100 case LTO_DEBUG_MODEL_DWARF:
101 _emitDwarfDebugInfo = true;
102 return false;
103 }
104 llvm_unreachable("Unknown debug format!");
Nick Kledzik77595fc2008-02-26 20:26:43 +0000105}
106
Bill Wendling76b13ed2012-03-31 10:50:14 +0000107bool LTOCodeGenerator::setCodePICModel(lto_codegen_model model,
Bill Wendlingc94c5622012-03-31 11:15:43 +0000108 std::string& errMsg) {
109 switch (model) {
110 case LTO_CODEGEN_PIC_MODEL_STATIC:
111 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
112 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
113 _codeModel = model;
114 return false;
115 }
116 llvm_unreachable("Unknown PIC model!");
Nick Kledzik77595fc2008-02-26 20:26:43 +0000117}
118
Chris Lattnerb515d752009-08-23 07:49:08 +0000119bool LTOCodeGenerator::writeMergedModules(const char *path,
120 std::string &errMsg) {
121 if (determineTarget(errMsg))
122 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000123
Bill Wendling76b13ed2012-03-31 10:50:14 +0000124 // mark which symbols can not be internalized
Chris Lattnerb515d752009-08-23 07:49:08 +0000125 applyScopeRestrictions();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000126
Chris Lattnerb515d752009-08-23 07:49:08 +0000127 // create output file
128 std::string ErrInfo;
Dan Gohmanf2914012010-08-20 16:59:15 +0000129 tool_output_file Out(path, ErrInfo,
130 raw_fd_ostream::F_Binary);
Chris Lattnerb515d752009-08-23 07:49:08 +0000131 if (!ErrInfo.empty()) {
132 errMsg = "could not open bitcode file for writing: ";
133 errMsg += path;
134 return true;
135 }
Bill Wendling76b13ed2012-03-31 10:50:14 +0000136
Chris Lattnerb515d752009-08-23 07:49:08 +0000137 // write bitcode to it
Dan Gohmand4c45432010-09-01 14:20:41 +0000138 WriteBitcodeToFile(_linker.getModule(), Out.os());
139 Out.os().close();
Dan Gohman4b7416b2010-05-27 20:19:47 +0000140
Dan Gohmand4c45432010-09-01 14:20:41 +0000141 if (Out.os().has_error()) {
Chris Lattnerb515d752009-08-23 07:49:08 +0000142 errMsg = "could not write bitcode file: ";
143 errMsg += path;
Dan Gohmand4c45432010-09-01 14:20:41 +0000144 Out.os().clear_error();
Chris Lattnerb515d752009-08-23 07:49:08 +0000145 return true;
146 }
Bill Wendling76b13ed2012-03-31 10:50:14 +0000147
Dan Gohmanf2914012010-08-20 16:59:15 +0000148 Out.keep();
Chris Lattnerb515d752009-08-23 07:49:08 +0000149 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000150}
151
Bill Wendlingc94c5622012-03-31 11:15:43 +0000152bool LTOCodeGenerator::compile_to_file(const char** name, std::string& errMsg) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000153 // make unique temp .o file to put generated object file
154 sys::PathWithStatus uniqueObjPath("lto-llvm.o");
155 if ( uniqueObjPath.createTemporaryFileOnDisk(false, &errMsg) ) {
156 uniqueObjPath.eraseFromDisk();
157 return true;
158 }
159 sys::RemoveFileOnSignal(uniqueObjPath);
160
161 // generate object file
162 bool genResult = false;
163 tool_output_file objFile(uniqueObjPath.c_str(), errMsg);
164 if (!errMsg.empty())
John Criswell3f0e2372011-08-18 01:19:05 +0000165 return true;
Bill Wendlingc94c5622012-03-31 11:15:43 +0000166
Rafael Espindola6421a882011-03-22 20:57:13 +0000167 genResult = this->generateObjectFile(objFile.os(), errMsg);
168 objFile.os().close();
169 if (objFile.os().has_error()) {
170 objFile.os().clear_error();
171 return true;
172 }
Bill Wendlingc94c5622012-03-31 11:15:43 +0000173
Rafael Espindola6421a882011-03-22 20:57:13 +0000174 objFile.keep();
175 if ( genResult ) {
176 uniqueObjPath.eraseFromDisk();
177 return true;
178 }
179
180 _nativeObjectPath = uniqueObjPath.str();
181 *name = _nativeObjectPath.c_str();
182 return false;
183}
184
Bill Wendlingc94c5622012-03-31 11:15:43 +0000185const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000186 const char *name;
187 if (compile_to_file(&name, errMsg))
188 return NULL;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000189
Rafael Espindola6421a882011-03-22 20:57:13 +0000190 // remove old buffer if compile() called twice
191 delete _nativeObjectFile;
Nick Kledzikef194ed2008-02-27 22:25:36 +0000192
Rafael Espindola6421a882011-03-22 20:57:13 +0000193 // read .o file into memory buffer
194 OwningPtr<MemoryBuffer> BuffPtr;
195 if (error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) {
196 errMsg = ec.message();
197 return NULL;
198 }
199 _nativeObjectFile = BuffPtr.take();
Rafael Espindolae9efea12011-02-24 21:04:06 +0000200
Rafael Espindola6421a882011-03-22 20:57:13 +0000201 // remove temp files
202 sys::Path(_nativeObjectPath).eraseFromDisk();
Rafael Espindolae9efea12011-02-24 21:04:06 +0000203
Rafael Espindola6421a882011-03-22 20:57:13 +0000204 // return buffer, unless error
205 if ( _nativeObjectFile == NULL )
206 return NULL;
207 *length = _nativeObjectFile->getBufferSize();
208 return _nativeObjectFile->getBufferStart();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000209}
210
Bill Wendlingc94c5622012-03-31 11:15:43 +0000211bool LTOCodeGenerator::determineTarget(std::string& errMsg) {
212 if ( _target == NULL ) {
213 std::string Triple = _linker.getModule()->getTargetTriple();
214 if (Triple.empty())
215 Triple = sys::getDefaultTargetTriple();
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000216
Bill Wendlingc94c5622012-03-31 11:15:43 +0000217 // create target machine from info for merged modules
218 const Target *march = TargetRegistry::lookupTarget(Triple, errMsg);
219 if ( march == NULL )
220 return true;
Bill Wendling604a8182008-06-18 06:35:30 +0000221
Bill Wendlingc94c5622012-03-31 11:15:43 +0000222 // The relocation model is actually a static member of TargetMachine and
223 // needs to be set before the TargetMachine is instantiated.
224 Reloc::Model RelocModel = Reloc::Default;
225 switch( _codeModel ) {
226 case LTO_CODEGEN_PIC_MODEL_STATIC:
227 RelocModel = Reloc::Static;
228 break;
229 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
230 RelocModel = Reloc::PIC_;
231 break;
232 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
233 RelocModel = Reloc::DynamicNoPIC;
234 break;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000235 }
Bill Wendlingc94c5622012-03-31 11:15:43 +0000236
237 // construct LTOModule, hand over ownership of module and target
238 SubtargetFeatures Features;
239 Features.getDefaultSubtargetFeatures(llvm::Triple(Triple));
240 std::string FeatureStr = Features.getString();
241 TargetOptions Options;
242 _target = march->createTargetMachine(Triple, _mCpu, FeatureStr, Options,
David Blaikie4f56a302012-05-30 18:42:51 +0000243 RelocModel, CodeModel::Default,
244 CodeGenOpt::Aggressive);
Bill Wendlingc94c5622012-03-31 11:15:43 +0000245 }
246 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000247}
248
Bill Wendlingc94c5622012-03-31 11:15:43 +0000249void LTOCodeGenerator::
250applyRestriction(GlobalValue &GV,
251 std::vector<const char*> &mustPreserveList,
252 SmallPtrSet<GlobalValue*, 8> &asmUsed,
253 Mangler &mangler) {
Rafael Espindola38c4e532011-03-02 04:14:42 +0000254 SmallString<64> Buffer;
255 mangler.getNameWithPrefix(Buffer, &GV, false);
256
257 if (GV.isDeclaration())
258 return;
259 if (_mustPreserveSymbols.count(Buffer))
260 mustPreserveList.push_back(GV.getName().data());
261 if (_asmUndefinedRefs.count(Buffer))
262 asmUsed.insert(&GV);
263}
264
265static void findUsedValues(GlobalVariable *LLVMUsed,
266 SmallPtrSet<GlobalValue*, 8> &UsedValues) {
267 if (LLVMUsed == 0) return;
268
269 ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer());
270 if (Inits == 0) return;
271
272 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
Bill Wendling76b13ed2012-03-31 10:50:14 +0000273 if (GlobalValue *GV =
Bill Wendlingc94c5622012-03-31 11:15:43 +0000274 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
Rafael Espindola38c4e532011-03-02 04:14:42 +0000275 UsedValues.insert(GV);
276}
277
Chris Lattner5ef31a02010-03-12 18:44:54 +0000278void LTOCodeGenerator::applyScopeRestrictions() {
279 if (_scopeRestrictionsDone) return;
280 Module *mergedModule = _linker.getModule();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000281
Chris Lattner5ef31a02010-03-12 18:44:54 +0000282 // Start off with a verification pass.
283 PassManager passes;
284 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000285
Bill Wendling76b13ed2012-03-31 10:50:14 +0000286 // mark which symbols can not be internalized
Bill Wendlingc94c5622012-03-31 11:15:43 +0000287 MCContext Context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(),NULL);
Rafael Espindola38c4e532011-03-02 04:14:42 +0000288 Mangler mangler(Context, *_target->getTargetData());
289 std::vector<const char*> mustPreserveList;
290 SmallPtrSet<GlobalValue*, 8> asmUsed;
291
292 for (Module::iterator f = mergedModule->begin(),
293 e = mergedModule->end(); f != e; ++f)
294 applyRestriction(*f, mustPreserveList, asmUsed, mangler);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000295 for (Module::global_iterator v = mergedModule->global_begin(),
Rafael Espindola38c4e532011-03-02 04:14:42 +0000296 e = mergedModule->global_end(); v != e; ++v)
297 applyRestriction(*v, mustPreserveList, asmUsed, mangler);
298 for (Module::alias_iterator a = mergedModule->alias_begin(),
299 e = mergedModule->alias_end(); a != e; ++a)
300 applyRestriction(*a, mustPreserveList, asmUsed, mangler);
301
302 GlobalVariable *LLVMCompilerUsed =
303 mergedModule->getGlobalVariable("llvm.compiler.used");
304 findUsedValues(LLVMCompilerUsed, asmUsed);
305 if (LLVMCompilerUsed)
306 LLVMCompilerUsed->eraseFromParent();
307
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000308 llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(_context);
Rafael Espindola38c4e532011-03-02 04:14:42 +0000309 std::vector<Constant*> asmUsed2;
310 for (SmallPtrSet<GlobalValue*, 16>::const_iterator i = asmUsed.begin(),
311 e = asmUsed.end(); i !=e; ++i) {
312 GlobalValue *GV = *i;
313 Constant *c = ConstantExpr::getBitCast(GV, i8PTy);
314 asmUsed2.push_back(c);
Chris Lattner5ef31a02010-03-12 18:44:54 +0000315 }
Rafael Espindola38c4e532011-03-02 04:14:42 +0000316
317 llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size());
318 LLVMCompilerUsed =
319 new llvm::GlobalVariable(*mergedModule, ATy, false,
320 llvm::GlobalValue::AppendingLinkage,
321 llvm::ConstantArray::get(ATy, asmUsed2),
322 "llvm.compiler.used");
323
324 LLVMCompilerUsed->setSection("llvm.metadata");
325
326 passes.add(createInternalizePass(mustPreserveList));
327
Chris Lattner5ef31a02010-03-12 18:44:54 +0000328 // apply scope restrictions
329 passes.run(*mergedModule);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000330
Chris Lattner5ef31a02010-03-12 18:44:54 +0000331 _scopeRestrictionsDone = true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000332}
333
Nick Kledzik77595fc2008-02-26 20:26:43 +0000334/// Optimize merged modules using various IPO passes
Chris Lattner817a01f2011-05-22 00:20:07 +0000335bool LTOCodeGenerator::generateObjectFile(raw_ostream &out,
336 std::string &errMsg) {
Bill Wendlingc94c5622012-03-31 11:15:43 +0000337 if ( this->determineTarget(errMsg) )
338 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000339
Bill Wendlingc94c5622012-03-31 11:15:43 +0000340 Module* mergedModule = _linker.getModule();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000341
Bill Wendlingc94c5622012-03-31 11:15:43 +0000342 // if options were requested, set them
343 if ( !_codegenOptions.empty() )
344 cl::ParseCommandLineOptions(_codegenOptions.size(),
345 const_cast<char **>(&_codegenOptions[0]));
Devang Patela93ae712008-07-03 22:53:14 +0000346
Bill Wendling64d5b282012-04-09 22:18:01 +0000347 // mark which symbols can not be internalized
348 this->applyScopeRestrictions();
349
Bill Wendlingc94c5622012-03-31 11:15:43 +0000350 // Instantiate the pass manager to organize the passes.
351 PassManager passes;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000352
Bill Wendlingc94c5622012-03-31 11:15:43 +0000353 // Start off with a verification pass.
354 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000355
Bill Wendlingc94c5622012-03-31 11:15:43 +0000356 // Add an appropriate TargetData instance for this module...
357 passes.add(new TargetData(*_target->getTargetData()));
Bill Wendling76b13ed2012-03-31 10:50:14 +0000358
Rafael Espindola4d2e9d92012-04-16 10:58:38 +0000359 // Enabling internalize here would use its AllButMain variant. It
360 // keeps only main if it exists and does nothing for libraries. Instead
361 // we create the pass ourselves with the symbol list provided by the linker.
362 PassManagerBuilder().populateLTOPassManager(passes, /*Internalize=*/false,
Bill Wendling3197b442012-04-02 22:16:50 +0000363 !DisableInline,
364 DisableGVNLoadPRE);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000365
Bill Wendlingc94c5622012-03-31 11:15:43 +0000366 // Make sure everything is still good.
367 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000368
Bill Wendlingc94c5622012-03-31 11:15:43 +0000369 FunctionPassManager *codeGenPasses = new FunctionPassManager(mergedModule);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000370
Bill Wendlingc94c5622012-03-31 11:15:43 +0000371 codeGenPasses->add(new TargetData(*_target->getTargetData()));
Nick Kledzik77595fc2008-02-26 20:26:43 +0000372
Bill Wendlingc94c5622012-03-31 11:15:43 +0000373 formatted_raw_ostream Out(out);
Dan Gohmand4c45432010-09-01 14:20:41 +0000374
Bill Wendlingc94c5622012-03-31 11:15:43 +0000375 if (_target->addPassesToEmitFile(*codeGenPasses, Out,
David Blaikie4f56a302012-05-30 18:42:51 +0000376 TargetMachine::CGFT_ObjectFile)) {
Bill Wendlingc94c5622012-03-31 11:15:43 +0000377 errMsg = "target file type not supported";
378 return true;
379 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000380
Bill Wendlingc94c5622012-03-31 11:15:43 +0000381 // Run our queue of passes all at once now, efficiently.
382 passes.run(*mergedModule);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000383
Bill Wendlingc94c5622012-03-31 11:15:43 +0000384 // Run the code generator, and write assembly file
385 codeGenPasses->doInitialization();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000386
Bill Wendlingc94c5622012-03-31 11:15:43 +0000387 for (Module::iterator
388 it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it)
389 if (!it->isDeclaration())
390 codeGenPasses->run(*it);
Bill Wendling604a8182008-06-18 06:35:30 +0000391
Bill Wendlingc94c5622012-03-31 11:15:43 +0000392 codeGenPasses->doFinalization();
393 delete codeGenPasses;
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000394
Bill Wendlingc94c5622012-03-31 11:15:43 +0000395 return false; // success
Nick Kledzik77595fc2008-02-26 20:26:43 +0000396}
397
Bill Wendlingcaf71d42012-03-31 10:49:43 +0000398/// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
399/// LTO problems.
400void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) {
401 for (std::pair<StringRef, StringRef> o = getToken(options);
402 !o.first.empty(); o = getToken(o.second)) {
403 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add
404 // that.
405 if ( _codegenOptions.empty() )
406 _codegenOptions.push_back(strdup("libLTO"));
407 _codegenOptions.push_back(strdup(o.first.str().c_str()));
408 }
Nick Kledzik920ae982008-07-08 21:14:10 +0000409}