blob: 7620bcb1c1802a179d849e465b7b8954a18c3946 [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> EnableInternalizing("enable-internalizing", cl::init(false),
50 cl::desc("Internalize functions during LTO"));
51
52static cl::opt<bool> DisableInline("disable-inlining", cl::init(false),
Nick Kledzik920ae982008-07-08 21:14:10 +000053 cl::desc("Do not run the inliner pass"));
Nick Kledzik77595fc2008-02-26 20:26:43 +000054
Bill Wendling97d99032012-04-05 21:26:44 +000055static cl::opt<bool> DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
Bill Wendling3197b442012-04-02 22:16:50 +000056 cl::desc("Do not run the GVN load PRE pass"));
57
Bill Wendlingcaf71d42012-03-31 10:49:43 +000058const char* LTOCodeGenerator::getVersionString() {
Nick Kledzik77595fc2008-02-26 20:26:43 +000059#ifdef LLVM_VERSION_INFO
Bill Wendlingcaf71d42012-03-31 10:49:43 +000060 return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
Nick Kledzik77595fc2008-02-26 20:26:43 +000061#else
Bill Wendlingcaf71d42012-03-31 10:49:43 +000062 return PACKAGE_NAME " version " PACKAGE_VERSION;
Nick Kledzik77595fc2008-02-26 20:26:43 +000063#endif
64}
65
Bill Wendling76b13ed2012-03-31 10:50:14 +000066LTOCodeGenerator::LTOCodeGenerator()
Bill Wendlingc94c5622012-03-31 11:15:43 +000067 : _context(getGlobalContext()),
68 _linker("LinkTimeOptimizer", "ld-temp.o", _context), _target(NULL),
69 _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false),
Bill Wendling3029a0c2012-04-09 05:26:48 +000070 _runInternalizePass(false), _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
Bill Wendlingc94c5622012-03-31 11:15:43 +000071 _nativeObjectFile(NULL) {
72 InitializeAllTargets();
73 InitializeAllTargetMCs();
74 InitializeAllAsmPrinters();
Nick Kledzik77595fc2008-02-26 20:26:43 +000075}
76
Bill Wendlingcaf71d42012-03-31 10:49:43 +000077LTOCodeGenerator::~LTOCodeGenerator() {
78 delete _target;
79 delete _nativeObjectFile;
80
Bill Wendlingf2cc2ee2012-03-31 10:51:45 +000081 for (std::vector<char*>::iterator I = _codegenOptions.begin(),
Bill Wendlingcaf71d42012-03-31 10:49:43 +000082 E = _codegenOptions.end(); I != E; ++I)
83 free(*I);
Nick Kledzik77595fc2008-02-26 20:26:43 +000084}
85
Bill Wendlingc94c5622012-03-31 11:15:43 +000086bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) {
Rafael Espindola38c4e532011-03-02 04:14:42 +000087 bool ret = _linker.LinkInModule(mod->getLLVVMModule(), &errMsg);
88
89 const std::vector<const char*> &undefs = mod->getAsmUndefinedRefs();
90 for (int i = 0, e = undefs.size(); i != e; ++i)
91 _asmUndefinedRefs[undefs[i]] = 1;
92
93 return ret;
Nick Kledzik77595fc2008-02-26 20:26:43 +000094}
Bill Wendling76b13ed2012-03-31 10:50:14 +000095
Bill Wendlingc94c5622012-03-31 11:15:43 +000096bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug,
97 std::string& errMsg) {
98 switch (debug) {
99 case LTO_DEBUG_MODEL_NONE:
100 _emitDwarfDebugInfo = false;
101 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000102
Bill Wendlingc94c5622012-03-31 11:15:43 +0000103 case LTO_DEBUG_MODEL_DWARF:
104 _emitDwarfDebugInfo = true;
105 return false;
106 }
107 llvm_unreachable("Unknown debug format!");
Nick Kledzik77595fc2008-02-26 20:26:43 +0000108}
109
Bill Wendling76b13ed2012-03-31 10:50:14 +0000110bool LTOCodeGenerator::setCodePICModel(lto_codegen_model model,
Bill Wendlingc94c5622012-03-31 11:15:43 +0000111 std::string& errMsg) {
112 switch (model) {
113 case LTO_CODEGEN_PIC_MODEL_STATIC:
114 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
115 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
116 _codeModel = model;
117 return false;
118 }
119 llvm_unreachable("Unknown PIC model!");
Nick Kledzik77595fc2008-02-26 20:26:43 +0000120}
121
Chris Lattnerb515d752009-08-23 07:49:08 +0000122bool LTOCodeGenerator::writeMergedModules(const char *path,
123 std::string &errMsg) {
124 if (determineTarget(errMsg))
125 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000126
Bill Wendling76b13ed2012-03-31 10:50:14 +0000127 // mark which symbols can not be internalized
Chris Lattnerb515d752009-08-23 07:49:08 +0000128 applyScopeRestrictions();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000129
Chris Lattnerb515d752009-08-23 07:49:08 +0000130 // create output file
131 std::string ErrInfo;
Dan Gohmanf2914012010-08-20 16:59:15 +0000132 tool_output_file Out(path, ErrInfo,
133 raw_fd_ostream::F_Binary);
Chris Lattnerb515d752009-08-23 07:49:08 +0000134 if (!ErrInfo.empty()) {
135 errMsg = "could not open bitcode file for writing: ";
136 errMsg += path;
137 return true;
138 }
Bill Wendling76b13ed2012-03-31 10:50:14 +0000139
Chris Lattnerb515d752009-08-23 07:49:08 +0000140 // write bitcode to it
Dan Gohmand4c45432010-09-01 14:20:41 +0000141 WriteBitcodeToFile(_linker.getModule(), Out.os());
142 Out.os().close();
Dan Gohman4b7416b2010-05-27 20:19:47 +0000143
Dan Gohmand4c45432010-09-01 14:20:41 +0000144 if (Out.os().has_error()) {
Chris Lattnerb515d752009-08-23 07:49:08 +0000145 errMsg = "could not write bitcode file: ";
146 errMsg += path;
Dan Gohmand4c45432010-09-01 14:20:41 +0000147 Out.os().clear_error();
Chris Lattnerb515d752009-08-23 07:49:08 +0000148 return true;
149 }
Bill Wendling76b13ed2012-03-31 10:50:14 +0000150
Dan Gohmanf2914012010-08-20 16:59:15 +0000151 Out.keep();
Chris Lattnerb515d752009-08-23 07:49:08 +0000152 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000153}
154
Bill Wendlingc94c5622012-03-31 11:15:43 +0000155bool LTOCodeGenerator::compile_to_file(const char** name, std::string& errMsg) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000156 // make unique temp .o file to put generated object file
157 sys::PathWithStatus uniqueObjPath("lto-llvm.o");
158 if ( uniqueObjPath.createTemporaryFileOnDisk(false, &errMsg) ) {
159 uniqueObjPath.eraseFromDisk();
160 return true;
161 }
162 sys::RemoveFileOnSignal(uniqueObjPath);
163
164 // generate object file
165 bool genResult = false;
166 tool_output_file objFile(uniqueObjPath.c_str(), errMsg);
167 if (!errMsg.empty())
John Criswell3f0e2372011-08-18 01:19:05 +0000168 return true;
Bill Wendlingc94c5622012-03-31 11:15:43 +0000169
Rafael Espindola6421a882011-03-22 20:57:13 +0000170 genResult = this->generateObjectFile(objFile.os(), errMsg);
171 objFile.os().close();
172 if (objFile.os().has_error()) {
173 objFile.os().clear_error();
174 return true;
175 }
Bill Wendlingc94c5622012-03-31 11:15:43 +0000176
Rafael Espindola6421a882011-03-22 20:57:13 +0000177 objFile.keep();
178 if ( genResult ) {
179 uniqueObjPath.eraseFromDisk();
180 return true;
181 }
182
183 _nativeObjectPath = uniqueObjPath.str();
184 *name = _nativeObjectPath.c_str();
185 return false;
186}
187
Bill Wendlingc94c5622012-03-31 11:15:43 +0000188const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000189 const char *name;
190 if (compile_to_file(&name, errMsg))
191 return NULL;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000192
Rafael Espindola6421a882011-03-22 20:57:13 +0000193 // remove old buffer if compile() called twice
194 delete _nativeObjectFile;
Nick Kledzikef194ed2008-02-27 22:25:36 +0000195
Rafael Espindola6421a882011-03-22 20:57:13 +0000196 // read .o file into memory buffer
197 OwningPtr<MemoryBuffer> BuffPtr;
198 if (error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) {
199 errMsg = ec.message();
200 return NULL;
201 }
202 _nativeObjectFile = BuffPtr.take();
Rafael Espindolae9efea12011-02-24 21:04:06 +0000203
Rafael Espindola6421a882011-03-22 20:57:13 +0000204 // remove temp files
205 sys::Path(_nativeObjectPath).eraseFromDisk();
Rafael Espindolae9efea12011-02-24 21:04:06 +0000206
Rafael Espindola6421a882011-03-22 20:57:13 +0000207 // return buffer, unless error
208 if ( _nativeObjectFile == NULL )
209 return NULL;
210 *length = _nativeObjectFile->getBufferSize();
211 return _nativeObjectFile->getBufferStart();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000212}
213
Bill Wendlingc94c5622012-03-31 11:15:43 +0000214bool LTOCodeGenerator::determineTarget(std::string& errMsg) {
215 if ( _target == NULL ) {
216 std::string Triple = _linker.getModule()->getTargetTriple();
217 if (Triple.empty())
218 Triple = sys::getDefaultTargetTriple();
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000219
Bill Wendlingc94c5622012-03-31 11:15:43 +0000220 // create target machine from info for merged modules
221 const Target *march = TargetRegistry::lookupTarget(Triple, errMsg);
222 if ( march == NULL )
223 return true;
Bill Wendling604a8182008-06-18 06:35:30 +0000224
Bill Wendlingc94c5622012-03-31 11:15:43 +0000225 // The relocation model is actually a static member of TargetMachine and
226 // needs to be set before the TargetMachine is instantiated.
227 Reloc::Model RelocModel = Reloc::Default;
228 switch( _codeModel ) {
229 case LTO_CODEGEN_PIC_MODEL_STATIC:
230 RelocModel = Reloc::Static;
231 break;
232 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
233 RelocModel = Reloc::PIC_;
234 break;
235 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
236 RelocModel = Reloc::DynamicNoPIC;
237 break;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000238 }
Bill Wendlingc94c5622012-03-31 11:15:43 +0000239
240 // construct LTOModule, hand over ownership of module and target
241 SubtargetFeatures Features;
242 Features.getDefaultSubtargetFeatures(llvm::Triple(Triple));
243 std::string FeatureStr = Features.getString();
244 TargetOptions Options;
245 _target = march->createTargetMachine(Triple, _mCpu, FeatureStr, Options,
246 RelocModel);
247 }
248 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000249}
250
Bill Wendlingc94c5622012-03-31 11:15:43 +0000251void LTOCodeGenerator::
252applyRestriction(GlobalValue &GV,
253 std::vector<const char*> &mustPreserveList,
254 SmallPtrSet<GlobalValue*, 8> &asmUsed,
255 Mangler &mangler) {
Rafael Espindola38c4e532011-03-02 04:14:42 +0000256 SmallString<64> Buffer;
257 mangler.getNameWithPrefix(Buffer, &GV, false);
258
259 if (GV.isDeclaration())
260 return;
261 if (_mustPreserveSymbols.count(Buffer))
262 mustPreserveList.push_back(GV.getName().data());
263 if (_asmUndefinedRefs.count(Buffer))
264 asmUsed.insert(&GV);
265}
266
267static void findUsedValues(GlobalVariable *LLVMUsed,
268 SmallPtrSet<GlobalValue*, 8> &UsedValues) {
269 if (LLVMUsed == 0) return;
270
271 ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer());
272 if (Inits == 0) return;
273
274 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
Bill Wendling76b13ed2012-03-31 10:50:14 +0000275 if (GlobalValue *GV =
Bill Wendlingc94c5622012-03-31 11:15:43 +0000276 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
Rafael Espindola38c4e532011-03-02 04:14:42 +0000277 UsedValues.insert(GV);
278}
279
Chris Lattner5ef31a02010-03-12 18:44:54 +0000280void LTOCodeGenerator::applyScopeRestrictions() {
Bill Wendling97d99032012-04-05 21:26:44 +0000281 // Internalize only if specifically asked for. Otherwise, global symbols which
282 // exist in the final image, but which are used outside of that image
283 // (e.g. bundling) may be removed. This also happens when a function is used
284 // only in inline asm. LLVM doesn't recognize that as a "use", so it could be
285 // stripped.
286 if (!EnableInternalizing)
287 return;
288
Chris Lattner5ef31a02010-03-12 18:44:54 +0000289 if (_scopeRestrictionsDone) return;
290 Module *mergedModule = _linker.getModule();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000291
Chris Lattner5ef31a02010-03-12 18:44:54 +0000292 // Start off with a verification pass.
293 PassManager passes;
294 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000295
Bill Wendling76b13ed2012-03-31 10:50:14 +0000296 // mark which symbols can not be internalized
Bill Wendlingc94c5622012-03-31 11:15:43 +0000297 MCContext Context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(),NULL);
Rafael Espindola38c4e532011-03-02 04:14:42 +0000298 Mangler mangler(Context, *_target->getTargetData());
299 std::vector<const char*> mustPreserveList;
300 SmallPtrSet<GlobalValue*, 8> asmUsed;
301
302 for (Module::iterator f = mergedModule->begin(),
303 e = mergedModule->end(); f != e; ++f)
304 applyRestriction(*f, mustPreserveList, asmUsed, mangler);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000305 for (Module::global_iterator v = mergedModule->global_begin(),
Rafael Espindola38c4e532011-03-02 04:14:42 +0000306 e = mergedModule->global_end(); v != e; ++v)
307 applyRestriction(*v, mustPreserveList, asmUsed, mangler);
308 for (Module::alias_iterator a = mergedModule->alias_begin(),
309 e = mergedModule->alias_end(); a != e; ++a)
310 applyRestriction(*a, mustPreserveList, asmUsed, mangler);
311
312 GlobalVariable *LLVMCompilerUsed =
313 mergedModule->getGlobalVariable("llvm.compiler.used");
314 findUsedValues(LLVMCompilerUsed, asmUsed);
315 if (LLVMCompilerUsed)
316 LLVMCompilerUsed->eraseFromParent();
317
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000318 llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(_context);
Rafael Espindola38c4e532011-03-02 04:14:42 +0000319 std::vector<Constant*> asmUsed2;
320 for (SmallPtrSet<GlobalValue*, 16>::const_iterator i = asmUsed.begin(),
321 e = asmUsed.end(); i !=e; ++i) {
322 GlobalValue *GV = *i;
323 Constant *c = ConstantExpr::getBitCast(GV, i8PTy);
324 asmUsed2.push_back(c);
Chris Lattner5ef31a02010-03-12 18:44:54 +0000325 }
Rafael Espindola38c4e532011-03-02 04:14:42 +0000326
327 llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size());
328 LLVMCompilerUsed =
329 new llvm::GlobalVariable(*mergedModule, ATy, false,
330 llvm::GlobalValue::AppendingLinkage,
331 llvm::ConstantArray::get(ATy, asmUsed2),
332 "llvm.compiler.used");
333
334 LLVMCompilerUsed->setSection("llvm.metadata");
335
336 passes.add(createInternalizePass(mustPreserveList));
337
Chris Lattner5ef31a02010-03-12 18:44:54 +0000338 // apply scope restrictions
339 passes.run(*mergedModule);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000340
Chris Lattner5ef31a02010-03-12 18:44:54 +0000341 _scopeRestrictionsDone = true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000342}
343
Nick Kledzik77595fc2008-02-26 20:26:43 +0000344/// Optimize merged modules using various IPO passes
Chris Lattner817a01f2011-05-22 00:20:07 +0000345bool LTOCodeGenerator::generateObjectFile(raw_ostream &out,
346 std::string &errMsg) {
Bill Wendlingc94c5622012-03-31 11:15:43 +0000347 if ( this->determineTarget(errMsg) )
348 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000349
Bill Wendlingc94c5622012-03-31 11:15:43 +0000350 // mark which symbols can not be internalized
351 this->applyScopeRestrictions();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000352
Bill Wendlingc94c5622012-03-31 11:15:43 +0000353 Module* mergedModule = _linker.getModule();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000354
Bill Wendlingc94c5622012-03-31 11:15:43 +0000355 // if options were requested, set them
356 if ( !_codegenOptions.empty() )
357 cl::ParseCommandLineOptions(_codegenOptions.size(),
358 const_cast<char **>(&_codegenOptions[0]));
Devang Patela93ae712008-07-03 22:53:14 +0000359
Bill Wendlingc94c5622012-03-31 11:15:43 +0000360 // Instantiate the pass manager to organize the passes.
361 PassManager passes;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000362
Bill Wendlingc94c5622012-03-31 11:15:43 +0000363 // Start off with a verification pass.
364 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000365
Bill Wendlingc94c5622012-03-31 11:15:43 +0000366 // Add an appropriate TargetData instance for this module...
367 passes.add(new TargetData(*_target->getTargetData()));
Bill Wendling76b13ed2012-03-31 10:50:14 +0000368
Bill Wendling3029a0c2012-04-09 05:26:48 +0000369 PassManagerBuilder().populateLTOPassManager(passes,
370 _runInternalizePass,
Bill Wendling3197b442012-04-02 22:16:50 +0000371 !DisableInline,
372 DisableGVNLoadPRE);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000373
Bill Wendlingc94c5622012-03-31 11:15:43 +0000374 // Make sure everything is still good.
375 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000376
Bill Wendlingc94c5622012-03-31 11:15:43 +0000377 FunctionPassManager *codeGenPasses = new FunctionPassManager(mergedModule);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000378
Bill Wendlingc94c5622012-03-31 11:15:43 +0000379 codeGenPasses->add(new TargetData(*_target->getTargetData()));
Nick Kledzik77595fc2008-02-26 20:26:43 +0000380
Bill Wendlingc94c5622012-03-31 11:15:43 +0000381 formatted_raw_ostream Out(out);
Dan Gohmand4c45432010-09-01 14:20:41 +0000382
Bill Wendlingc94c5622012-03-31 11:15:43 +0000383 if (_target->addPassesToEmitFile(*codeGenPasses, Out,
384 TargetMachine::CGFT_ObjectFile,
385 CodeGenOpt::Aggressive)) {
386 errMsg = "target file type not supported";
387 return true;
388 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000389
Bill Wendlingc94c5622012-03-31 11:15:43 +0000390 // Run our queue of passes all at once now, efficiently.
391 passes.run(*mergedModule);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000392
Bill Wendlingc94c5622012-03-31 11:15:43 +0000393 // Run the code generator, and write assembly file
394 codeGenPasses->doInitialization();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000395
Bill Wendlingc94c5622012-03-31 11:15:43 +0000396 for (Module::iterator
397 it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it)
398 if (!it->isDeclaration())
399 codeGenPasses->run(*it);
Bill Wendling604a8182008-06-18 06:35:30 +0000400
Bill Wendlingc94c5622012-03-31 11:15:43 +0000401 codeGenPasses->doFinalization();
402 delete codeGenPasses;
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000403
Bill Wendlingc94c5622012-03-31 11:15:43 +0000404 return false; // success
Nick Kledzik77595fc2008-02-26 20:26:43 +0000405}
406
Bill Wendlingcaf71d42012-03-31 10:49:43 +0000407/// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
408/// LTO problems.
409void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) {
410 for (std::pair<StringRef, StringRef> o = getToken(options);
411 !o.first.empty(); o = getToken(o.second)) {
412 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add
413 // that.
414 if ( _codegenOptions.empty() )
415 _codegenOptions.push_back(strdup("libLTO"));
416 _codegenOptions.push_back(strdup(o.first.str().c_str()));
417 }
Nick Kledzik920ae982008-07-08 21:14:10 +0000418}