blob: 75705154e45e6b06b7b7ee13960caf8b98d075ab [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 Carruth0b8c9a82013-01-02 11:36:10 +000022#include "llvm/IR/Constants.h"
23#include "llvm/IR/DataLayout.h"
24#include "llvm/IR/DerivedTypes.h"
25#include "llvm/IR/LLVMContext.h"
26#include "llvm/IR/Module.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000027#include "llvm/Linker.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"
Chandler Carruthf010c462012-12-04 10:44:52 +000031#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"
45#include "llvm/Transforms/IPO.h"
46#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000047using namespace llvm;
48
Bill Wendling9ac0aaa2012-08-06 21:34:54 +000049static cl::opt<bool>
Bill Wendling6303b662013-02-28 14:11:10 +000050DisableOpt("disable-opt", cl::init(false),
51 cl::desc("Do not run any optimization passes"));
52
53static cl::opt<bool>
Bill Wendling9ac0aaa2012-08-06 21:34:54 +000054DisableInline("disable-inlining", cl::init(false),
Nick Kledzik920ae982008-07-08 21:14:10 +000055 cl::desc("Do not run the inliner pass"));
Nick Kledzik77595fc2008-02-26 20:26:43 +000056
Bill Wendling9ac0aaa2012-08-06 21:34:54 +000057static cl::opt<bool>
58DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
Bill Wendling3197b442012-04-02 22:16:50 +000059 cl::desc("Do not run the GVN load PRE pass"));
60
Bill Wendlingcaf71d42012-03-31 10:49:43 +000061const char* LTOCodeGenerator::getVersionString() {
Nick Kledzik77595fc2008-02-26 20:26:43 +000062#ifdef LLVM_VERSION_INFO
Bill Wendlingcaf71d42012-03-31 10:49:43 +000063 return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
Nick Kledzik77595fc2008-02-26 20:26:43 +000064#else
Bill Wendlingcaf71d42012-03-31 10:49:43 +000065 return PACKAGE_NAME " version " PACKAGE_VERSION;
Nick Kledzik77595fc2008-02-26 20:26:43 +000066#endif
67}
68
Bill Wendling76b13ed2012-03-31 10:50:14 +000069LTOCodeGenerator::LTOCodeGenerator()
Bill Wendlingc94c5622012-03-31 11:15:43 +000070 : _context(getGlobalContext()),
71 _linker("LinkTimeOptimizer", "ld-temp.o", _context), _target(NULL),
72 _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false),
Bill Wendling50f31832012-12-10 21:33:45 +000073 _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
Bill Wendlingc94c5622012-03-31 11:15:43 +000074 _nativeObjectFile(NULL) {
75 InitializeAllTargets();
76 InitializeAllTargetMCs();
77 InitializeAllAsmPrinters();
Nick Kledzik77595fc2008-02-26 20:26:43 +000078}
79
Bill Wendlingcaf71d42012-03-31 10:49:43 +000080LTOCodeGenerator::~LTOCodeGenerator() {
81 delete _target;
82 delete _nativeObjectFile;
83
Bill Wendlingf2cc2ee2012-03-31 10:51:45 +000084 for (std::vector<char*>::iterator I = _codegenOptions.begin(),
Bill Wendlingcaf71d42012-03-31 10:49:43 +000085 E = _codegenOptions.end(); I != E; ++I)
86 free(*I);
Nick Kledzik77595fc2008-02-26 20:26:43 +000087}
88
Bill Wendlingc94c5622012-03-31 11:15:43 +000089bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) {
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
Bill Wendlingc94c5622012-03-31 11:15:43 +000099bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug,
100 std::string& errMsg) {
101 switch (debug) {
102 case LTO_DEBUG_MODEL_NONE:
103 _emitDwarfDebugInfo = false;
104 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000105
Bill Wendlingc94c5622012-03-31 11:15:43 +0000106 case LTO_DEBUG_MODEL_DWARF:
107 _emitDwarfDebugInfo = true;
108 return false;
109 }
110 llvm_unreachable("Unknown debug format!");
Nick Kledzik77595fc2008-02-26 20:26:43 +0000111}
112
Bill Wendling76b13ed2012-03-31 10:50:14 +0000113bool LTOCodeGenerator::setCodePICModel(lto_codegen_model model,
Bill Wendlingc94c5622012-03-31 11:15:43 +0000114 std::string& errMsg) {
115 switch (model) {
116 case LTO_CODEGEN_PIC_MODEL_STATIC:
117 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
118 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
119 _codeModel = model;
120 return false;
121 }
122 llvm_unreachable("Unknown PIC model!");
Nick Kledzik77595fc2008-02-26 20:26:43 +0000123}
124
Chris Lattnerb515d752009-08-23 07:49:08 +0000125bool LTOCodeGenerator::writeMergedModules(const char *path,
126 std::string &errMsg) {
127 if (determineTarget(errMsg))
128 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000129
Bill Wendling76b13ed2012-03-31 10:50:14 +0000130 // mark which symbols can not be internalized
Chris Lattnerb515d752009-08-23 07:49:08 +0000131 applyScopeRestrictions();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000132
Chris Lattnerb515d752009-08-23 07:49:08 +0000133 // create output file
134 std::string ErrInfo;
Dan Gohmanf2914012010-08-20 16:59:15 +0000135 tool_output_file Out(path, ErrInfo,
136 raw_fd_ostream::F_Binary);
Chris Lattnerb515d752009-08-23 07:49:08 +0000137 if (!ErrInfo.empty()) {
138 errMsg = "could not open bitcode file for writing: ";
139 errMsg += path;
140 return true;
141 }
Bill Wendling76b13ed2012-03-31 10:50:14 +0000142
Chris Lattnerb515d752009-08-23 07:49:08 +0000143 // write bitcode to it
Dan Gohmand4c45432010-09-01 14:20:41 +0000144 WriteBitcodeToFile(_linker.getModule(), Out.os());
145 Out.os().close();
Dan Gohman4b7416b2010-05-27 20:19:47 +0000146
Dan Gohmand4c45432010-09-01 14:20:41 +0000147 if (Out.os().has_error()) {
Chris Lattnerb515d752009-08-23 07:49:08 +0000148 errMsg = "could not write bitcode file: ";
149 errMsg += path;
Dan Gohmand4c45432010-09-01 14:20:41 +0000150 Out.os().clear_error();
Chris Lattnerb515d752009-08-23 07:49:08 +0000151 return true;
152 }
Bill Wendling76b13ed2012-03-31 10:50:14 +0000153
Dan Gohmanf2914012010-08-20 16:59:15 +0000154 Out.keep();
Chris Lattnerb515d752009-08-23 07:49:08 +0000155 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000156}
157
Bill Wendlingc94c5622012-03-31 11:15:43 +0000158bool LTOCodeGenerator::compile_to_file(const char** name, std::string& errMsg) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000159 // make unique temp .o file to put generated object file
160 sys::PathWithStatus uniqueObjPath("lto-llvm.o");
Bill Wendling0ca36af2012-08-08 22:03:50 +0000161 if (uniqueObjPath.createTemporaryFileOnDisk(false, &errMsg)) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000162 uniqueObjPath.eraseFromDisk();
163 return true;
164 }
165 sys::RemoveFileOnSignal(uniqueObjPath);
166
167 // generate object file
168 bool genResult = false;
169 tool_output_file objFile(uniqueObjPath.c_str(), errMsg);
Bill Wendling0b95a992012-09-06 21:07:57 +0000170 if (!errMsg.empty()) {
171 uniqueObjPath.eraseFromDisk();
John Criswell3f0e2372011-08-18 01:19:05 +0000172 return true;
Bill Wendling0b95a992012-09-06 21:07:57 +0000173 }
Bill Wendlingc94c5622012-03-31 11:15:43 +0000174
Rafael Espindola6421a882011-03-22 20:57:13 +0000175 genResult = this->generateObjectFile(objFile.os(), errMsg);
176 objFile.os().close();
177 if (objFile.os().has_error()) {
178 objFile.os().clear_error();
Bill Wendling0b95a992012-09-06 21:07:57 +0000179 uniqueObjPath.eraseFromDisk();
Rafael Espindola6421a882011-03-22 20:57:13 +0000180 return true;
181 }
Bill Wendlingc94c5622012-03-31 11:15:43 +0000182
Rafael Espindola6421a882011-03-22 20:57:13 +0000183 objFile.keep();
Bill Wendling0ca36af2012-08-08 22:03:50 +0000184 if (genResult) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000185 uniqueObjPath.eraseFromDisk();
186 return true;
187 }
188
189 _nativeObjectPath = uniqueObjPath.str();
190 *name = _nativeObjectPath.c_str();
191 return false;
192}
193
Bill Wendlingc94c5622012-03-31 11:15:43 +0000194const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000195 const char *name;
196 if (compile_to_file(&name, errMsg))
197 return NULL;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000198
Rafael Espindola6421a882011-03-22 20:57:13 +0000199 // remove old buffer if compile() called twice
200 delete _nativeObjectFile;
Nick Kledzikef194ed2008-02-27 22:25:36 +0000201
Rafael Espindola6421a882011-03-22 20:57:13 +0000202 // read .o file into memory buffer
203 OwningPtr<MemoryBuffer> BuffPtr;
204 if (error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) {
205 errMsg = ec.message();
Bill Wendling0b95a992012-09-06 21:07:57 +0000206 sys::Path(_nativeObjectPath).eraseFromDisk();
Rafael Espindola6421a882011-03-22 20:57:13 +0000207 return NULL;
208 }
209 _nativeObjectFile = BuffPtr.take();
Rafael Espindolae9efea12011-02-24 21:04:06 +0000210
Rafael Espindola6421a882011-03-22 20:57:13 +0000211 // remove temp files
212 sys::Path(_nativeObjectPath).eraseFromDisk();
Rafael Espindolae9efea12011-02-24 21:04:06 +0000213
Rafael Espindola6421a882011-03-22 20:57:13 +0000214 // return buffer, unless error
Bill Wendling0ca36af2012-08-08 22:03:50 +0000215 if (_nativeObjectFile == NULL)
Rafael Espindola6421a882011-03-22 20:57:13 +0000216 return NULL;
217 *length = _nativeObjectFile->getBufferSize();
218 return _nativeObjectFile->getBufferStart();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000219}
220
Bill Wendlingc94c5622012-03-31 11:15:43 +0000221bool LTOCodeGenerator::determineTarget(std::string& errMsg) {
Bill Wendling0ca36af2012-08-08 22:03:50 +0000222 if (_target != NULL)
223 return false;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000224
Bob Wilson47ed8a12012-10-12 17:39:25 +0000225 std::string TripleStr = _linker.getModule()->getTargetTriple();
226 if (TripleStr.empty())
227 TripleStr = sys::getDefaultTargetTriple();
228 llvm::Triple Triple(TripleStr);
Bill Wendling604a8182008-06-18 06:35:30 +0000229
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000230 // create target machine from info for merged modules
Bob Wilson47ed8a12012-10-12 17:39:25 +0000231 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
Bill Wendling0ca36af2012-08-08 22:03:50 +0000232 if (march == NULL)
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000233 return true;
Bill Wendlingc94c5622012-03-31 11:15:43 +0000234
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000235 // The relocation model is actually a static member of TargetMachine and
236 // needs to be set before the TargetMachine is instantiated.
237 Reloc::Model RelocModel = Reloc::Default;
Bill Wendling0ca36af2012-08-08 22:03:50 +0000238 switch (_codeModel) {
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000239 case LTO_CODEGEN_PIC_MODEL_STATIC:
240 RelocModel = Reloc::Static;
241 break;
242 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
243 RelocModel = Reloc::PIC_;
244 break;
245 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
246 RelocModel = Reloc::DynamicNoPIC;
247 break;
Bill Wendlingc94c5622012-03-31 11:15:43 +0000248 }
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000249
250 // construct LTOModule, hand over ownership of module and target
251 SubtargetFeatures Features;
Bob Wilson47ed8a12012-10-12 17:39:25 +0000252 Features.getDefaultSubtargetFeatures(Triple);
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000253 std::string FeatureStr = Features.getString();
Bob Wilson47ed8a12012-10-12 17:39:25 +0000254 // Set a default CPU for Darwin triples.
255 if (_mCpu.empty() && Triple.isOSDarwin()) {
256 if (Triple.getArch() == llvm::Triple::x86_64)
257 _mCpu = "core2";
258 else if (Triple.getArch() == llvm::Triple::x86)
259 _mCpu = "yonah";
260 }
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000261 TargetOptions Options;
262 LTOModule::getTargetOptions(Options);
Bob Wilson47ed8a12012-10-12 17:39:25 +0000263 _target = march->createTargetMachine(TripleStr, _mCpu, FeatureStr, Options,
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000264 RelocModel, CodeModel::Default,
265 CodeGenOpt::Aggressive);
Bill Wendlingc94c5622012-03-31 11:15:43 +0000266 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000267}
268
Bill Wendlingc94c5622012-03-31 11:15:43 +0000269void LTOCodeGenerator::
270applyRestriction(GlobalValue &GV,
271 std::vector<const char*> &mustPreserveList,
272 SmallPtrSet<GlobalValue*, 8> &asmUsed,
273 Mangler &mangler) {
Rafael Espindola38c4e532011-03-02 04:14:42 +0000274 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 =
Bill Wendlingc94c5622012-03-31 11:15:43 +0000294 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
Rafael Espindola38c4e532011-03-02 04:14:42 +0000295 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
Bill Wendlingc94c5622012-03-31 11:15:43 +0000307 MCContext Context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(),NULL);
Micah Villmow791cfc22012-10-08 16:39:34 +0000308 Mangler mangler(Context, *_target->getDataLayout());
Rafael Espindola38c4e532011-03-02 04:14:42 +0000309 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
Bill Wendling50f31832012-12-10 21:33:45 +0000346 passes.add(createInternalizePass(mustPreserveList));
Rafael Espindola38c4e532011-03-02 04:14:42 +0000347
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 Wendling0ca36af2012-08-08 22:03:50 +0000357 if (this->determineTarget(errMsg))
Bill Wendlingc94c5622012-03-31 11:15:43 +0000358 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000359
Bill Wendlingc94c5622012-03-31 11:15:43 +0000360 Module* mergedModule = _linker.getModule();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000361
Bill Wendlingc94c5622012-03-31 11:15:43 +0000362 // if options were requested, set them
Bill Wendling0ca36af2012-08-08 22:03:50 +0000363 if (!_codegenOptions.empty())
Bill Wendlingc94c5622012-03-31 11:15:43 +0000364 cl::ParseCommandLineOptions(_codegenOptions.size(),
365 const_cast<char **>(&_codegenOptions[0]));
Devang Patela93ae712008-07-03 22:53:14 +0000366
Bill Wendling64d5b282012-04-09 22:18:01 +0000367 // mark which symbols can not be internalized
368 this->applyScopeRestrictions();
369
Bill Wendlingc94c5622012-03-31 11:15:43 +0000370 // Instantiate the pass manager to organize the passes.
371 PassManager passes;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000372
Bill Wendlingc94c5622012-03-31 11:15:43 +0000373 // Start off with a verification pass.
374 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000375
Micah Villmow791cfc22012-10-08 16:39:34 +0000376 // Add an appropriate DataLayout instance for this module...
377 passes.add(new DataLayout(*_target->getDataLayout()));
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000378 _target->addAnalysisPasses(passes);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000379
Rafael Espindola4d2e9d92012-04-16 10:58:38 +0000380 // Enabling internalize here would use its AllButMain variant. It
381 // keeps only main if it exists and does nothing for libraries. Instead
382 // we create the pass ourselves with the symbol list provided by the linker.
Bill Wendling6303b662013-02-28 14:11:10 +0000383 if (!DisableOpt) {
384 PassManagerBuilder().populateLTOPassManager(passes,
Bill Wendling50f31832012-12-10 21:33:45 +0000385 /*Internalize=*/false,
Bill Wendling3197b442012-04-02 22:16:50 +0000386 !DisableInline,
387 DisableGVNLoadPRE);
Bill Wendling6303b662013-02-28 14:11:10 +0000388 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000389
Bill Wendlingc94c5622012-03-31 11:15:43 +0000390 // Make sure everything is still good.
391 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000392
Bill Wendlingc94c5622012-03-31 11:15:43 +0000393 FunctionPassManager *codeGenPasses = new FunctionPassManager(mergedModule);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000394
Micah Villmow791cfc22012-10-08 16:39:34 +0000395 codeGenPasses->add(new DataLayout(*_target->getDataLayout()));
Nadav Rotem1c1ff3b2013-01-15 01:53:57 +0000396 _target->addAnalysisPasses(*codeGenPasses);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000397
Bill Wendlingc94c5622012-03-31 11:15:43 +0000398 formatted_raw_ostream Out(out);
Dan Gohmand4c45432010-09-01 14:20:41 +0000399
Bill Wendlingc94c5622012-03-31 11:15:43 +0000400 if (_target->addPassesToEmitFile(*codeGenPasses, Out,
David Blaikie4f56a302012-05-30 18:42:51 +0000401 TargetMachine::CGFT_ObjectFile)) {
Bill Wendlingc94c5622012-03-31 11:15:43 +0000402 errMsg = "target file type not supported";
403 return true;
404 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000405
Bill Wendlingc94c5622012-03-31 11:15:43 +0000406 // Run our queue of passes all at once now, efficiently.
407 passes.run(*mergedModule);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000408
Bill Wendlingc94c5622012-03-31 11:15:43 +0000409 // Run the code generator, and write assembly file
410 codeGenPasses->doInitialization();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000411
Bill Wendlingc94c5622012-03-31 11:15:43 +0000412 for (Module::iterator
413 it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it)
414 if (!it->isDeclaration())
415 codeGenPasses->run(*it);
Bill Wendling604a8182008-06-18 06:35:30 +0000416
Bill Wendlingc94c5622012-03-31 11:15:43 +0000417 codeGenPasses->doFinalization();
418 delete codeGenPasses;
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000419
Bill Wendlingc94c5622012-03-31 11:15:43 +0000420 return false; // success
Nick Kledzik77595fc2008-02-26 20:26:43 +0000421}
422
Bill Wendlingcaf71d42012-03-31 10:49:43 +0000423/// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
424/// LTO problems.
425void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) {
426 for (std::pair<StringRef, StringRef> o = getToken(options);
427 !o.first.empty(); o = getToken(o.second)) {
428 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add
429 // that.
Bill Wendling0ca36af2012-08-08 22:03:50 +0000430 if (_codegenOptions.empty())
Bill Wendlingcaf71d42012-03-31 10:49:43 +0000431 _codegenOptions.push_back(strdup("libLTO"));
432 _codegenOptions.push_back(strdup(o.first.str().c_str()));
433 }
Nick Kledzik920ae982008-07-08 21:14:10 +0000434}