blob: b80bc34a231da14ab7dc5fb049d7e9500de1d274 [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 Wendling9ac0aaa2012-08-06 21:34:54 +000049static cl::opt<bool>
50DisableInline("disable-inlining", cl::init(false),
Nick Kledzik920ae982008-07-08 21:14:10 +000051 cl::desc("Do not run the inliner pass"));
Nick Kledzik77595fc2008-02-26 20:26:43 +000052
Bill Wendling9ac0aaa2012-08-06 21:34:54 +000053static cl::opt<bool>
54DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
Bill Wendling3197b442012-04-02 22:16:50 +000055 cl::desc("Do not run the GVN load PRE pass"));
56
Bill Wendlingcaf71d42012-03-31 10:49:43 +000057const char* LTOCodeGenerator::getVersionString() {
Nick Kledzik77595fc2008-02-26 20:26:43 +000058#ifdef LLVM_VERSION_INFO
Bill Wendlingcaf71d42012-03-31 10:49:43 +000059 return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
Nick Kledzik77595fc2008-02-26 20:26:43 +000060#else
Bill Wendlingcaf71d42012-03-31 10:49:43 +000061 return PACKAGE_NAME " version " PACKAGE_VERSION;
Nick Kledzik77595fc2008-02-26 20:26:43 +000062#endif
63}
64
Bill Wendling76b13ed2012-03-31 10:50:14 +000065LTOCodeGenerator::LTOCodeGenerator()
Bill Wendlingc94c5622012-03-31 11:15:43 +000066 : _context(getGlobalContext()),
67 _linker("LinkTimeOptimizer", "ld-temp.o", _context), _target(NULL),
68 _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false),
Rafael Espindola4d2e9d92012-04-16 10:58:38 +000069 _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
Bill Wendlingc94c5622012-03-31 11:15:43 +000070 _nativeObjectFile(NULL) {
71 InitializeAllTargets();
72 InitializeAllTargetMCs();
73 InitializeAllAsmPrinters();
Nick Kledzik77595fc2008-02-26 20:26:43 +000074}
75
Bill Wendlingcaf71d42012-03-31 10:49:43 +000076LTOCodeGenerator::~LTOCodeGenerator() {
77 delete _target;
78 delete _nativeObjectFile;
79
Bill Wendlingf2cc2ee2012-03-31 10:51:45 +000080 for (std::vector<char*>::iterator I = _codegenOptions.begin(),
Bill Wendlingcaf71d42012-03-31 10:49:43 +000081 E = _codegenOptions.end(); I != E; ++I)
82 free(*I);
Nick Kledzik77595fc2008-02-26 20:26:43 +000083}
84
Bill Wendlingc94c5622012-03-31 11:15:43 +000085bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) {
Rafael Espindola38c4e532011-03-02 04:14:42 +000086 bool ret = _linker.LinkInModule(mod->getLLVVMModule(), &errMsg);
87
88 const std::vector<const char*> &undefs = mod->getAsmUndefinedRefs();
89 for (int i = 0, e = undefs.size(); i != e; ++i)
90 _asmUndefinedRefs[undefs[i]] = 1;
91
92 return ret;
Nick Kledzik77595fc2008-02-26 20:26:43 +000093}
Bill Wendling76b13ed2012-03-31 10:50:14 +000094
Bill Wendlingc94c5622012-03-31 11:15:43 +000095bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug,
96 std::string& errMsg) {
97 switch (debug) {
98 case LTO_DEBUG_MODEL_NONE:
99 _emitDwarfDebugInfo = false;
100 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000101
Bill Wendlingc94c5622012-03-31 11:15:43 +0000102 case LTO_DEBUG_MODEL_DWARF:
103 _emitDwarfDebugInfo = true;
104 return false;
105 }
106 llvm_unreachable("Unknown debug format!");
Nick Kledzik77595fc2008-02-26 20:26:43 +0000107}
108
Bill Wendling76b13ed2012-03-31 10:50:14 +0000109bool LTOCodeGenerator::setCodePICModel(lto_codegen_model model,
Bill Wendlingc94c5622012-03-31 11:15:43 +0000110 std::string& errMsg) {
111 switch (model) {
112 case LTO_CODEGEN_PIC_MODEL_STATIC:
113 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
114 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
115 _codeModel = model;
116 return false;
117 }
118 llvm_unreachable("Unknown PIC model!");
Nick Kledzik77595fc2008-02-26 20:26:43 +0000119}
120
Chris Lattnerb515d752009-08-23 07:49:08 +0000121bool LTOCodeGenerator::writeMergedModules(const char *path,
122 std::string &errMsg) {
123 if (determineTarget(errMsg))
124 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000125
Bill Wendling76b13ed2012-03-31 10:50:14 +0000126 // mark which symbols can not be internalized
Chris Lattnerb515d752009-08-23 07:49:08 +0000127 applyScopeRestrictions();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000128
Chris Lattnerb515d752009-08-23 07:49:08 +0000129 // create output file
130 std::string ErrInfo;
Dan Gohmanf2914012010-08-20 16:59:15 +0000131 tool_output_file Out(path, ErrInfo,
132 raw_fd_ostream::F_Binary);
Chris Lattnerb515d752009-08-23 07:49:08 +0000133 if (!ErrInfo.empty()) {
134 errMsg = "could not open bitcode file for writing: ";
135 errMsg += path;
136 return true;
137 }
Bill Wendling76b13ed2012-03-31 10:50:14 +0000138
Chris Lattnerb515d752009-08-23 07:49:08 +0000139 // write bitcode to it
Dan Gohmand4c45432010-09-01 14:20:41 +0000140 WriteBitcodeToFile(_linker.getModule(), Out.os());
141 Out.os().close();
Dan Gohman4b7416b2010-05-27 20:19:47 +0000142
Dan Gohmand4c45432010-09-01 14:20:41 +0000143 if (Out.os().has_error()) {
Chris Lattnerb515d752009-08-23 07:49:08 +0000144 errMsg = "could not write bitcode file: ";
145 errMsg += path;
Dan Gohmand4c45432010-09-01 14:20:41 +0000146 Out.os().clear_error();
Chris Lattnerb515d752009-08-23 07:49:08 +0000147 return true;
148 }
Bill Wendling76b13ed2012-03-31 10:50:14 +0000149
Dan Gohmanf2914012010-08-20 16:59:15 +0000150 Out.keep();
Chris Lattnerb515d752009-08-23 07:49:08 +0000151 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000152}
153
Bill Wendlingc94c5622012-03-31 11:15:43 +0000154bool LTOCodeGenerator::compile_to_file(const char** name, std::string& errMsg) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000155 // make unique temp .o file to put generated object file
156 sys::PathWithStatus uniqueObjPath("lto-llvm.o");
Bill Wendling0ca36af2012-08-08 22:03:50 +0000157 if (uniqueObjPath.createTemporaryFileOnDisk(false, &errMsg)) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000158 uniqueObjPath.eraseFromDisk();
159 return true;
160 }
161 sys::RemoveFileOnSignal(uniqueObjPath);
162
163 // generate object file
164 bool genResult = false;
165 tool_output_file objFile(uniqueObjPath.c_str(), errMsg);
166 if (!errMsg.empty())
John Criswell3f0e2372011-08-18 01:19:05 +0000167 return true;
Bill Wendlingc94c5622012-03-31 11:15:43 +0000168
Rafael Espindola6421a882011-03-22 20:57:13 +0000169 genResult = this->generateObjectFile(objFile.os(), errMsg);
170 objFile.os().close();
171 if (objFile.os().has_error()) {
172 objFile.os().clear_error();
173 return true;
174 }
Bill Wendlingc94c5622012-03-31 11:15:43 +0000175
Rafael Espindola6421a882011-03-22 20:57:13 +0000176 objFile.keep();
Bill Wendling0ca36af2012-08-08 22:03:50 +0000177 if (genResult) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000178 uniqueObjPath.eraseFromDisk();
179 return true;
180 }
181
182 _nativeObjectPath = uniqueObjPath.str();
183 *name = _nativeObjectPath.c_str();
184 return false;
185}
186
Bill Wendlingc94c5622012-03-31 11:15:43 +0000187const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000188 const char *name;
189 if (compile_to_file(&name, errMsg))
190 return NULL;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000191
Rafael Espindola6421a882011-03-22 20:57:13 +0000192 // remove old buffer if compile() called twice
193 delete _nativeObjectFile;
Nick Kledzikef194ed2008-02-27 22:25:36 +0000194
Rafael Espindola6421a882011-03-22 20:57:13 +0000195 // read .o file into memory buffer
196 OwningPtr<MemoryBuffer> BuffPtr;
197 if (error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) {
198 errMsg = ec.message();
199 return NULL;
200 }
201 _nativeObjectFile = BuffPtr.take();
Rafael Espindolae9efea12011-02-24 21:04:06 +0000202
Rafael Espindola6421a882011-03-22 20:57:13 +0000203 // remove temp files
204 sys::Path(_nativeObjectPath).eraseFromDisk();
Rafael Espindolae9efea12011-02-24 21:04:06 +0000205
Rafael Espindola6421a882011-03-22 20:57:13 +0000206 // return buffer, unless error
Bill Wendling0ca36af2012-08-08 22:03:50 +0000207 if (_nativeObjectFile == NULL)
Rafael Espindola6421a882011-03-22 20:57:13 +0000208 return NULL;
209 *length = _nativeObjectFile->getBufferSize();
210 return _nativeObjectFile->getBufferStart();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000211}
212
Bill Wendlingc94c5622012-03-31 11:15:43 +0000213bool LTOCodeGenerator::determineTarget(std::string& errMsg) {
Bill Wendling0ca36af2012-08-08 22:03:50 +0000214 if (_target != NULL)
215 return false;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000216
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000217 std::string Triple = _linker.getModule()->getTargetTriple();
218 if (Triple.empty())
219 Triple = sys::getDefaultTargetTriple();
Bill Wendling604a8182008-06-18 06:35:30 +0000220
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000221 // create target machine from info for merged modules
222 const Target *march = TargetRegistry::lookupTarget(Triple, errMsg);
Bill Wendling0ca36af2012-08-08 22:03:50 +0000223 if (march == NULL)
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000224 return true;
Bill Wendlingc94c5622012-03-31 11:15:43 +0000225
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000226 // The relocation model is actually a static member of TargetMachine and
227 // needs to be set before the TargetMachine is instantiated.
228 Reloc::Model RelocModel = Reloc::Default;
Bill Wendling0ca36af2012-08-08 22:03:50 +0000229 switch (_codeModel) {
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000230 case LTO_CODEGEN_PIC_MODEL_STATIC:
231 RelocModel = Reloc::Static;
232 break;
233 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
234 RelocModel = Reloc::PIC_;
235 break;
236 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
237 RelocModel = Reloc::DynamicNoPIC;
238 break;
Bill Wendlingc94c5622012-03-31 11:15:43 +0000239 }
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000240
241 // construct LTOModule, hand over ownership of module and target
242 SubtargetFeatures Features;
243 Features.getDefaultSubtargetFeatures(llvm::Triple(Triple));
244 std::string FeatureStr = Features.getString();
245 TargetOptions Options;
246 LTOModule::getTargetOptions(Options);
247 _target = march->createTargetMachine(Triple, _mCpu, FeatureStr, Options,
248 RelocModel, CodeModel::Default,
249 CodeGenOpt::Aggressive);
Bill Wendlingc94c5622012-03-31 11:15:43 +0000250 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000251}
252
Bill Wendlingc94c5622012-03-31 11:15:43 +0000253void LTOCodeGenerator::
254applyRestriction(GlobalValue &GV,
255 std::vector<const char*> &mustPreserveList,
256 SmallPtrSet<GlobalValue*, 8> &asmUsed,
257 Mangler &mangler) {
Rafael Espindola38c4e532011-03-02 04:14:42 +0000258 SmallString<64> Buffer;
259 mangler.getNameWithPrefix(Buffer, &GV, false);
260
261 if (GV.isDeclaration())
262 return;
263 if (_mustPreserveSymbols.count(Buffer))
264 mustPreserveList.push_back(GV.getName().data());
265 if (_asmUndefinedRefs.count(Buffer))
266 asmUsed.insert(&GV);
267}
268
269static void findUsedValues(GlobalVariable *LLVMUsed,
270 SmallPtrSet<GlobalValue*, 8> &UsedValues) {
271 if (LLVMUsed == 0) return;
272
273 ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer());
274 if (Inits == 0) return;
275
276 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
Bill Wendling76b13ed2012-03-31 10:50:14 +0000277 if (GlobalValue *GV =
Bill Wendlingc94c5622012-03-31 11:15:43 +0000278 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
Rafael Espindola38c4e532011-03-02 04:14:42 +0000279 UsedValues.insert(GV);
280}
281
Chris Lattner5ef31a02010-03-12 18:44:54 +0000282void LTOCodeGenerator::applyScopeRestrictions() {
283 if (_scopeRestrictionsDone) return;
284 Module *mergedModule = _linker.getModule();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000285
Chris Lattner5ef31a02010-03-12 18:44:54 +0000286 // Start off with a verification pass.
287 PassManager passes;
288 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000289
Bill Wendling76b13ed2012-03-31 10:50:14 +0000290 // mark which symbols can not be internalized
Bill Wendlingc94c5622012-03-31 11:15:43 +0000291 MCContext Context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(),NULL);
Rafael Espindola38c4e532011-03-02 04:14:42 +0000292 Mangler mangler(Context, *_target->getTargetData());
293 std::vector<const char*> mustPreserveList;
294 SmallPtrSet<GlobalValue*, 8> asmUsed;
295
296 for (Module::iterator f = mergedModule->begin(),
297 e = mergedModule->end(); f != e; ++f)
298 applyRestriction(*f, mustPreserveList, asmUsed, mangler);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000299 for (Module::global_iterator v = mergedModule->global_begin(),
Rafael Espindola38c4e532011-03-02 04:14:42 +0000300 e = mergedModule->global_end(); v != e; ++v)
301 applyRestriction(*v, mustPreserveList, asmUsed, mangler);
302 for (Module::alias_iterator a = mergedModule->alias_begin(),
303 e = mergedModule->alias_end(); a != e; ++a)
304 applyRestriction(*a, mustPreserveList, asmUsed, mangler);
305
306 GlobalVariable *LLVMCompilerUsed =
307 mergedModule->getGlobalVariable("llvm.compiler.used");
308 findUsedValues(LLVMCompilerUsed, asmUsed);
309 if (LLVMCompilerUsed)
310 LLVMCompilerUsed->eraseFromParent();
311
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000312 llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(_context);
Rafael Espindola38c4e532011-03-02 04:14:42 +0000313 std::vector<Constant*> asmUsed2;
314 for (SmallPtrSet<GlobalValue*, 16>::const_iterator i = asmUsed.begin(),
315 e = asmUsed.end(); i !=e; ++i) {
316 GlobalValue *GV = *i;
317 Constant *c = ConstantExpr::getBitCast(GV, i8PTy);
318 asmUsed2.push_back(c);
Chris Lattner5ef31a02010-03-12 18:44:54 +0000319 }
Rafael Espindola38c4e532011-03-02 04:14:42 +0000320
321 llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size());
322 LLVMCompilerUsed =
323 new llvm::GlobalVariable(*mergedModule, ATy, false,
324 llvm::GlobalValue::AppendingLinkage,
325 llvm::ConstantArray::get(ATy, asmUsed2),
326 "llvm.compiler.used");
327
328 LLVMCompilerUsed->setSection("llvm.metadata");
329
330 passes.add(createInternalizePass(mustPreserveList));
331
Chris Lattner5ef31a02010-03-12 18:44:54 +0000332 // apply scope restrictions
333 passes.run(*mergedModule);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000334
Chris Lattner5ef31a02010-03-12 18:44:54 +0000335 _scopeRestrictionsDone = true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000336}
337
Nick Kledzik77595fc2008-02-26 20:26:43 +0000338/// Optimize merged modules using various IPO passes
Chris Lattner817a01f2011-05-22 00:20:07 +0000339bool LTOCodeGenerator::generateObjectFile(raw_ostream &out,
340 std::string &errMsg) {
Bill Wendling0ca36af2012-08-08 22:03:50 +0000341 if (this->determineTarget(errMsg))
Bill Wendlingc94c5622012-03-31 11:15:43 +0000342 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000343
Bill Wendlingc94c5622012-03-31 11:15:43 +0000344 Module* mergedModule = _linker.getModule();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000345
Bill Wendlingc94c5622012-03-31 11:15:43 +0000346 // if options were requested, set them
Bill Wendling0ca36af2012-08-08 22:03:50 +0000347 if (!_codegenOptions.empty())
Bill Wendlingc94c5622012-03-31 11:15:43 +0000348 cl::ParseCommandLineOptions(_codegenOptions.size(),
349 const_cast<char **>(&_codegenOptions[0]));
Devang Patela93ae712008-07-03 22:53:14 +0000350
Bill Wendling64d5b282012-04-09 22:18:01 +0000351 // mark which symbols can not be internalized
352 this->applyScopeRestrictions();
353
Bill Wendlingc94c5622012-03-31 11:15:43 +0000354 // Instantiate the pass manager to organize the passes.
355 PassManager passes;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000356
Bill Wendlingc94c5622012-03-31 11:15:43 +0000357 // Start off with a verification pass.
358 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000359
Bill Wendlingc94c5622012-03-31 11:15:43 +0000360 // Add an appropriate TargetData instance for this module...
361 passes.add(new TargetData(*_target->getTargetData()));
Bill Wendling76b13ed2012-03-31 10:50:14 +0000362
Rafael Espindola4d2e9d92012-04-16 10:58:38 +0000363 // Enabling internalize here would use its AllButMain variant. It
364 // keeps only main if it exists and does nothing for libraries. Instead
365 // we create the pass ourselves with the symbol list provided by the linker.
366 PassManagerBuilder().populateLTOPassManager(passes, /*Internalize=*/false,
Bill Wendling3197b442012-04-02 22:16:50 +0000367 !DisableInline,
368 DisableGVNLoadPRE);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000369
Bill Wendlingc94c5622012-03-31 11:15:43 +0000370 // Make sure everything is still good.
371 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000372
Bill Wendlingc94c5622012-03-31 11:15:43 +0000373 FunctionPassManager *codeGenPasses = new FunctionPassManager(mergedModule);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000374
Bill Wendlingc94c5622012-03-31 11:15:43 +0000375 codeGenPasses->add(new TargetData(*_target->getTargetData()));
Nick Kledzik77595fc2008-02-26 20:26:43 +0000376
Bill Wendlingc94c5622012-03-31 11:15:43 +0000377 formatted_raw_ostream Out(out);
Dan Gohmand4c45432010-09-01 14:20:41 +0000378
Bill Wendlingc94c5622012-03-31 11:15:43 +0000379 if (_target->addPassesToEmitFile(*codeGenPasses, Out,
David Blaikie4f56a302012-05-30 18:42:51 +0000380 TargetMachine::CGFT_ObjectFile)) {
Bill Wendlingc94c5622012-03-31 11:15:43 +0000381 errMsg = "target file type not supported";
382 return true;
383 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000384
Bill Wendlingc94c5622012-03-31 11:15:43 +0000385 // Run our queue of passes all at once now, efficiently.
386 passes.run(*mergedModule);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000387
Bill Wendlingc94c5622012-03-31 11:15:43 +0000388 // Run the code generator, and write assembly file
389 codeGenPasses->doInitialization();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000390
Bill Wendlingc94c5622012-03-31 11:15:43 +0000391 for (Module::iterator
392 it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it)
393 if (!it->isDeclaration())
394 codeGenPasses->run(*it);
Bill Wendling604a8182008-06-18 06:35:30 +0000395
Bill Wendlingc94c5622012-03-31 11:15:43 +0000396 codeGenPasses->doFinalization();
397 delete codeGenPasses;
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000398
Bill Wendlingc94c5622012-03-31 11:15:43 +0000399 return false; // success
Nick Kledzik77595fc2008-02-26 20:26:43 +0000400}
401
Bill Wendlingcaf71d42012-03-31 10:49:43 +0000402/// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
403/// LTO problems.
404void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) {
405 for (std::pair<StringRef, StringRef> o = getToken(options);
406 !o.first.empty(); o = getToken(o.second)) {
407 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add
408 // that.
Bill Wendling0ca36af2012-08-08 22:03:50 +0000409 if (_codegenOptions.empty())
Bill Wendlingcaf71d42012-03-31 10:49:43 +0000410 _codegenOptions.push_back(strdup("libLTO"));
411 _codegenOptions.push_back(strdup(o.first.str().c_str()));
412 }
Nick Kledzik920ae982008-07-08 21:14:10 +0000413}