blob: 91f5cc0b773e32e8e11f12d9e4c97a052773a255 [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"
Chandler Carruthf010c462012-12-04 10:44:52 +000017#include "llvm/ADT/StringExtras.h"
Nick Lewycky8189d402009-06-17 06:52:10 +000018#include "llvm/Analysis/Passes.h"
Rafael Espindolac684e832011-08-02 21:50:27 +000019#include "llvm/Analysis/Verifier.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000020#include "llvm/Bitcode/ReaderWriter.h"
Bill Wendlingc94c5622012-03-31 11:15:43 +000021#include "llvm/Config/config.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000022#include "llvm/Constants.h"
23#include "llvm/DataLayout.h"
24#include "llvm/DerivedTypes.h"
25#include "llvm/LLVMContext.h"
26#include "llvm/Linker.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"
Chandler Carruthf010c462012-12-04 10:44:52 +000030#include "llvm/Module.h"
31#include "llvm/PassManager.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"
Michael J. Spencer3cc52ea2010-11-29 18:47:54 +000034#include "llvm/Support/Host.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000035#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer3cc52ea2010-11-29 18:47:54 +000036#include "llvm/Support/Signals.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000037#include "llvm/Support/TargetRegistry.h"
38#include "llvm/Support/TargetSelect.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000039#include "llvm/Support/ToolOutputFile.h"
Michael J. Spencerf2f516f2010-12-09 18:06:07 +000040#include "llvm/Support/system_error.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000041#include "llvm/Target/Mangler.h"
42#include "llvm/Target/TargetMachine.h"
43#include "llvm/Target/TargetOptions.h"
44#include "llvm/Target/TargetRegisterInfo.h"
Chandler Carruth7f00f872013-01-02 10:26:28 +000045#include "llvm/TargetTransformInfo.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000046#include "llvm/Transforms/IPO.h"
47#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000048using namespace llvm;
49
Bill Wendling9ac0aaa2012-08-06 21:34:54 +000050static cl::opt<bool>
51DisableInline("disable-inlining", cl::init(false),
Nick Kledzik920ae982008-07-08 21:14:10 +000052 cl::desc("Do not run the inliner pass"));
Nick Kledzik77595fc2008-02-26 20:26:43 +000053
Bill Wendling9ac0aaa2012-08-06 21:34:54 +000054static cl::opt<bool>
55DisableGVNLoadPRE("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 Wendling50f31832012-12-10 21:33:45 +000070 _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");
Bill Wendling0ca36af2012-08-08 22:03:50 +0000158 if (uniqueObjPath.createTemporaryFileOnDisk(false, &errMsg)) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000159 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);
Bill Wendling0b95a992012-09-06 21:07:57 +0000167 if (!errMsg.empty()) {
168 uniqueObjPath.eraseFromDisk();
John Criswell3f0e2372011-08-18 01:19:05 +0000169 return true;
Bill Wendling0b95a992012-09-06 21:07:57 +0000170 }
Bill Wendlingc94c5622012-03-31 11:15:43 +0000171
Rafael Espindola6421a882011-03-22 20:57:13 +0000172 genResult = this->generateObjectFile(objFile.os(), errMsg);
173 objFile.os().close();
174 if (objFile.os().has_error()) {
175 objFile.os().clear_error();
Bill Wendling0b95a992012-09-06 21:07:57 +0000176 uniqueObjPath.eraseFromDisk();
Rafael Espindola6421a882011-03-22 20:57:13 +0000177 return true;
178 }
Bill Wendlingc94c5622012-03-31 11:15:43 +0000179
Rafael Espindola6421a882011-03-22 20:57:13 +0000180 objFile.keep();
Bill Wendling0ca36af2012-08-08 22:03:50 +0000181 if (genResult) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000182 uniqueObjPath.eraseFromDisk();
183 return true;
184 }
185
186 _nativeObjectPath = uniqueObjPath.str();
187 *name = _nativeObjectPath.c_str();
188 return false;
189}
190
Bill Wendlingc94c5622012-03-31 11:15:43 +0000191const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000192 const char *name;
193 if (compile_to_file(&name, errMsg))
194 return NULL;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000195
Rafael Espindola6421a882011-03-22 20:57:13 +0000196 // remove old buffer if compile() called twice
197 delete _nativeObjectFile;
Nick Kledzikef194ed2008-02-27 22:25:36 +0000198
Rafael Espindola6421a882011-03-22 20:57:13 +0000199 // read .o file into memory buffer
200 OwningPtr<MemoryBuffer> BuffPtr;
201 if (error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) {
202 errMsg = ec.message();
Bill Wendling0b95a992012-09-06 21:07:57 +0000203 sys::Path(_nativeObjectPath).eraseFromDisk();
Rafael Espindola6421a882011-03-22 20:57:13 +0000204 return NULL;
205 }
206 _nativeObjectFile = BuffPtr.take();
Rafael Espindolae9efea12011-02-24 21:04:06 +0000207
Rafael Espindola6421a882011-03-22 20:57:13 +0000208 // remove temp files
209 sys::Path(_nativeObjectPath).eraseFromDisk();
Rafael Espindolae9efea12011-02-24 21:04:06 +0000210
Rafael Espindola6421a882011-03-22 20:57:13 +0000211 // return buffer, unless error
Bill Wendling0ca36af2012-08-08 22:03:50 +0000212 if (_nativeObjectFile == NULL)
Rafael Espindola6421a882011-03-22 20:57:13 +0000213 return NULL;
214 *length = _nativeObjectFile->getBufferSize();
215 return _nativeObjectFile->getBufferStart();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000216}
217
Bill Wendlingc94c5622012-03-31 11:15:43 +0000218bool LTOCodeGenerator::determineTarget(std::string& errMsg) {
Bill Wendling0ca36af2012-08-08 22:03:50 +0000219 if (_target != NULL)
220 return false;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000221
Bob Wilson47ed8a12012-10-12 17:39:25 +0000222 std::string TripleStr = _linker.getModule()->getTargetTriple();
223 if (TripleStr.empty())
224 TripleStr = sys::getDefaultTargetTriple();
225 llvm::Triple Triple(TripleStr);
Bill Wendling604a8182008-06-18 06:35:30 +0000226
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000227 // create target machine from info for merged modules
Bob Wilson47ed8a12012-10-12 17:39:25 +0000228 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
Bill Wendling0ca36af2012-08-08 22:03:50 +0000229 if (march == NULL)
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000230 return true;
Bill Wendlingc94c5622012-03-31 11:15:43 +0000231
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000232 // The relocation model is actually a static member of TargetMachine and
233 // needs to be set before the TargetMachine is instantiated.
234 Reloc::Model RelocModel = Reloc::Default;
Bill Wendling0ca36af2012-08-08 22:03:50 +0000235 switch (_codeModel) {
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000236 case LTO_CODEGEN_PIC_MODEL_STATIC:
237 RelocModel = Reloc::Static;
238 break;
239 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
240 RelocModel = Reloc::PIC_;
241 break;
242 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
243 RelocModel = Reloc::DynamicNoPIC;
244 break;
Bill Wendlingc94c5622012-03-31 11:15:43 +0000245 }
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000246
247 // construct LTOModule, hand over ownership of module and target
248 SubtargetFeatures Features;
Bob Wilson47ed8a12012-10-12 17:39:25 +0000249 Features.getDefaultSubtargetFeatures(Triple);
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000250 std::string FeatureStr = Features.getString();
Bob Wilson47ed8a12012-10-12 17:39:25 +0000251 // Set a default CPU for Darwin triples.
252 if (_mCpu.empty() && Triple.isOSDarwin()) {
253 if (Triple.getArch() == llvm::Triple::x86_64)
254 _mCpu = "core2";
255 else if (Triple.getArch() == llvm::Triple::x86)
256 _mCpu = "yonah";
257 }
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000258 TargetOptions Options;
259 LTOModule::getTargetOptions(Options);
Bob Wilson47ed8a12012-10-12 17:39:25 +0000260 _target = march->createTargetMachine(TripleStr, _mCpu, FeatureStr, Options,
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000261 RelocModel, CodeModel::Default,
262 CodeGenOpt::Aggressive);
Bill Wendlingc94c5622012-03-31 11:15:43 +0000263 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000264}
265
Bill Wendlingc94c5622012-03-31 11:15:43 +0000266void LTOCodeGenerator::
267applyRestriction(GlobalValue &GV,
268 std::vector<const char*> &mustPreserveList,
269 SmallPtrSet<GlobalValue*, 8> &asmUsed,
270 Mangler &mangler) {
Rafael Espindola38c4e532011-03-02 04:14:42 +0000271 SmallString<64> Buffer;
272 mangler.getNameWithPrefix(Buffer, &GV, false);
273
274 if (GV.isDeclaration())
275 return;
276 if (_mustPreserveSymbols.count(Buffer))
277 mustPreserveList.push_back(GV.getName().data());
278 if (_asmUndefinedRefs.count(Buffer))
279 asmUsed.insert(&GV);
280}
281
282static void findUsedValues(GlobalVariable *LLVMUsed,
283 SmallPtrSet<GlobalValue*, 8> &UsedValues) {
284 if (LLVMUsed == 0) return;
285
286 ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer());
287 if (Inits == 0) return;
288
289 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
Bill Wendling76b13ed2012-03-31 10:50:14 +0000290 if (GlobalValue *GV =
Bill Wendlingc94c5622012-03-31 11:15:43 +0000291 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
Rafael Espindola38c4e532011-03-02 04:14:42 +0000292 UsedValues.insert(GV);
293}
294
Chris Lattner5ef31a02010-03-12 18:44:54 +0000295void LTOCodeGenerator::applyScopeRestrictions() {
296 if (_scopeRestrictionsDone) return;
297 Module *mergedModule = _linker.getModule();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000298
Chris Lattner5ef31a02010-03-12 18:44:54 +0000299 // Start off with a verification pass.
300 PassManager passes;
301 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000302
Bill Wendling76b13ed2012-03-31 10:50:14 +0000303 // mark which symbols can not be internalized
Bill Wendlingc94c5622012-03-31 11:15:43 +0000304 MCContext Context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(),NULL);
Micah Villmow791cfc22012-10-08 16:39:34 +0000305 Mangler mangler(Context, *_target->getDataLayout());
Rafael Espindola38c4e532011-03-02 04:14:42 +0000306 std::vector<const char*> mustPreserveList;
307 SmallPtrSet<GlobalValue*, 8> asmUsed;
308
309 for (Module::iterator f = mergedModule->begin(),
310 e = mergedModule->end(); f != e; ++f)
311 applyRestriction(*f, mustPreserveList, asmUsed, mangler);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000312 for (Module::global_iterator v = mergedModule->global_begin(),
Rafael Espindola38c4e532011-03-02 04:14:42 +0000313 e = mergedModule->global_end(); v != e; ++v)
314 applyRestriction(*v, mustPreserveList, asmUsed, mangler);
315 for (Module::alias_iterator a = mergedModule->alias_begin(),
316 e = mergedModule->alias_end(); a != e; ++a)
317 applyRestriction(*a, mustPreserveList, asmUsed, mangler);
318
319 GlobalVariable *LLVMCompilerUsed =
320 mergedModule->getGlobalVariable("llvm.compiler.used");
321 findUsedValues(LLVMCompilerUsed, asmUsed);
322 if (LLVMCompilerUsed)
323 LLVMCompilerUsed->eraseFromParent();
324
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000325 llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(_context);
Rafael Espindola38c4e532011-03-02 04:14:42 +0000326 std::vector<Constant*> asmUsed2;
327 for (SmallPtrSet<GlobalValue*, 16>::const_iterator i = asmUsed.begin(),
328 e = asmUsed.end(); i !=e; ++i) {
329 GlobalValue *GV = *i;
330 Constant *c = ConstantExpr::getBitCast(GV, i8PTy);
331 asmUsed2.push_back(c);
Chris Lattner5ef31a02010-03-12 18:44:54 +0000332 }
Rafael Espindola38c4e532011-03-02 04:14:42 +0000333
334 llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size());
335 LLVMCompilerUsed =
336 new llvm::GlobalVariable(*mergedModule, ATy, false,
337 llvm::GlobalValue::AppendingLinkage,
338 llvm::ConstantArray::get(ATy, asmUsed2),
339 "llvm.compiler.used");
340
341 LLVMCompilerUsed->setSection("llvm.metadata");
342
Bill Wendling50f31832012-12-10 21:33:45 +0000343 passes.add(createInternalizePass(mustPreserveList));
Rafael Espindola38c4e532011-03-02 04:14:42 +0000344
Chris Lattner5ef31a02010-03-12 18:44:54 +0000345 // apply scope restrictions
346 passes.run(*mergedModule);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000347
Chris Lattner5ef31a02010-03-12 18:44:54 +0000348 _scopeRestrictionsDone = true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000349}
350
Nick Kledzik77595fc2008-02-26 20:26:43 +0000351/// Optimize merged modules using various IPO passes
Chris Lattner817a01f2011-05-22 00:20:07 +0000352bool LTOCodeGenerator::generateObjectFile(raw_ostream &out,
353 std::string &errMsg) {
Bill Wendling0ca36af2012-08-08 22:03:50 +0000354 if (this->determineTarget(errMsg))
Bill Wendlingc94c5622012-03-31 11:15:43 +0000355 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000356
Bill Wendlingc94c5622012-03-31 11:15:43 +0000357 Module* mergedModule = _linker.getModule();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000358
Bill Wendlingc94c5622012-03-31 11:15:43 +0000359 // if options were requested, set them
Bill Wendling0ca36af2012-08-08 22:03:50 +0000360 if (!_codegenOptions.empty())
Bill Wendlingc94c5622012-03-31 11:15:43 +0000361 cl::ParseCommandLineOptions(_codegenOptions.size(),
362 const_cast<char **>(&_codegenOptions[0]));
Devang Patela93ae712008-07-03 22:53:14 +0000363
Bill Wendling64d5b282012-04-09 22:18:01 +0000364 // mark which symbols can not be internalized
365 this->applyScopeRestrictions();
366
Bill Wendlingc94c5622012-03-31 11:15:43 +0000367 // Instantiate the pass manager to organize the passes.
368 PassManager passes;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000369
Bill Wendlingc94c5622012-03-31 11:15:43 +0000370 // Start off with a verification pass.
371 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000372
Micah Villmow791cfc22012-10-08 16:39:34 +0000373 // Add an appropriate DataLayout instance for this module...
374 passes.add(new DataLayout(*_target->getDataLayout()));
Nadav Rotemcbd9a192012-10-18 23:22:48 +0000375 passes.add(new TargetTransformInfo(_target->getScalarTargetTransformInfo(),
376 _target->getVectorTargetTransformInfo()));
Bill Wendling76b13ed2012-03-31 10:50:14 +0000377
Rafael Espindola4d2e9d92012-04-16 10:58:38 +0000378 // Enabling internalize here would use its AllButMain variant. It
379 // keeps only main if it exists and does nothing for libraries. Instead
380 // we create the pass ourselves with the symbol list provided by the linker.
Bill Wendlingf62b9cd2012-12-08 00:18:16 +0000381 PassManagerBuilder().populateLTOPassManager(passes,
Bill Wendling50f31832012-12-10 21:33:45 +0000382 /*Internalize=*/false,
Bill Wendling3197b442012-04-02 22:16:50 +0000383 !DisableInline,
384 DisableGVNLoadPRE);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000385
Bill Wendlingc94c5622012-03-31 11:15:43 +0000386 // Make sure everything is still good.
387 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000388
Bill Wendlingc94c5622012-03-31 11:15:43 +0000389 FunctionPassManager *codeGenPasses = new FunctionPassManager(mergedModule);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000390
Micah Villmow791cfc22012-10-08 16:39:34 +0000391 codeGenPasses->add(new DataLayout(*_target->getDataLayout()));
Nick Kledzik77595fc2008-02-26 20:26:43 +0000392
Bill Wendlingc94c5622012-03-31 11:15:43 +0000393 formatted_raw_ostream Out(out);
Dan Gohmand4c45432010-09-01 14:20:41 +0000394
Bill Wendlingc94c5622012-03-31 11:15:43 +0000395 if (_target->addPassesToEmitFile(*codeGenPasses, Out,
David Blaikie4f56a302012-05-30 18:42:51 +0000396 TargetMachine::CGFT_ObjectFile)) {
Bill Wendlingc94c5622012-03-31 11:15:43 +0000397 errMsg = "target file type not supported";
398 return true;
399 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000400
Bill Wendlingc94c5622012-03-31 11:15:43 +0000401 // Run our queue of passes all at once now, efficiently.
402 passes.run(*mergedModule);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000403
Bill Wendlingc94c5622012-03-31 11:15:43 +0000404 // Run the code generator, and write assembly file
405 codeGenPasses->doInitialization();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000406
Bill Wendlingc94c5622012-03-31 11:15:43 +0000407 for (Module::iterator
408 it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it)
409 if (!it->isDeclaration())
410 codeGenPasses->run(*it);
Bill Wendling604a8182008-06-18 06:35:30 +0000411
Bill Wendlingc94c5622012-03-31 11:15:43 +0000412 codeGenPasses->doFinalization();
413 delete codeGenPasses;
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000414
Bill Wendlingc94c5622012-03-31 11:15:43 +0000415 return false; // success
Nick Kledzik77595fc2008-02-26 20:26:43 +0000416}
417
Bill Wendlingcaf71d42012-03-31 10:49:43 +0000418/// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
419/// LTO problems.
420void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) {
421 for (std::pair<StringRef, StringRef> o = getToken(options);
422 !o.first.empty(); o = getToken(o.second)) {
423 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add
424 // that.
Bill Wendling0ca36af2012-08-08 22:03:50 +0000425 if (_codegenOptions.empty())
Bill Wendlingcaf71d42012-03-31 10:49:43 +0000426 _codegenOptions.push_back(strdup("libLTO"));
427 _codegenOptions.push_back(strdup(o.first.str().c_str()));
428 }
Nick Kledzik920ae982008-07-08 21:14:10 +0000429}