blob: 022a04c178e2677df11ee032e5ddb02a52ca67a3 [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"
Rafael Espindola68c0efa2013-06-17 18:05:35 +000033#include "llvm/Support/FileSystem.h"
David Greene71847812009-07-14 20:18:05 +000034#include "llvm/Support/FormattedStream.h"
Michael J. Spencer3cc52ea2010-11-29 18:47:54 +000035#include "llvm/Support/Host.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000036#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer3cc52ea2010-11-29 18:47:54 +000037#include "llvm/Support/Signals.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000038#include "llvm/Support/TargetRegistry.h"
39#include "llvm/Support/TargetSelect.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000040#include "llvm/Support/ToolOutputFile.h"
Michael J. Spencerf2f516f2010-12-09 18:06:07 +000041#include "llvm/Support/system_error.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000042#include "llvm/Target/Mangler.h"
43#include "llvm/Target/TargetMachine.h"
44#include "llvm/Target/TargetOptions.h"
45#include "llvm/Target/TargetRegisterInfo.h"
46#include "llvm/Transforms/IPO.h"
47#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Bob Wilsond6965442013-03-29 23:28:55 +000048#include "llvm/Transforms/ObjCARC.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000049using namespace llvm;
50
Bill Wendling9ac0aaa2012-08-06 21:34:54 +000051static cl::opt<bool>
Bill Wendling6303b662013-02-28 14:11:10 +000052DisableOpt("disable-opt", cl::init(false),
53 cl::desc("Do not run any optimization passes"));
54
55static cl::opt<bool>
Bill Wendling9ac0aaa2012-08-06 21:34:54 +000056DisableInline("disable-inlining", cl::init(false),
Nick Kledzik920ae982008-07-08 21:14:10 +000057 cl::desc("Do not run the inliner pass"));
Nick Kledzik77595fc2008-02-26 20:26:43 +000058
Bill Wendling9ac0aaa2012-08-06 21:34:54 +000059static cl::opt<bool>
60DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
Bill Wendling3197b442012-04-02 22:16:50 +000061 cl::desc("Do not run the GVN load PRE pass"));
62
Bill Wendlingcaf71d42012-03-31 10:49:43 +000063const char* LTOCodeGenerator::getVersionString() {
Nick Kledzik77595fc2008-02-26 20:26:43 +000064#ifdef LLVM_VERSION_INFO
Bill Wendlingcaf71d42012-03-31 10:49:43 +000065 return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
Nick Kledzik77595fc2008-02-26 20:26:43 +000066#else
Bill Wendlingcaf71d42012-03-31 10:49:43 +000067 return PACKAGE_NAME " version " PACKAGE_VERSION;
Nick Kledzik77595fc2008-02-26 20:26:43 +000068#endif
69}
70
Bill Wendling76b13ed2012-03-31 10:50:14 +000071LTOCodeGenerator::LTOCodeGenerator()
Bill Wendlingc94c5622012-03-31 11:15:43 +000072 : _context(getGlobalContext()),
Rafael Espindola10519372013-05-04 02:43:00 +000073 _linker(new Module("ld-temp.o", _context)), _target(NULL),
Bill Wendlingc94c5622012-03-31 11:15:43 +000074 _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false),
Bill Wendling50f31832012-12-10 21:33:45 +000075 _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
Bill Wendlingc94c5622012-03-31 11:15:43 +000076 _nativeObjectFile(NULL) {
77 InitializeAllTargets();
78 InitializeAllTargetMCs();
79 InitializeAllAsmPrinters();
Nick Kledzik77595fc2008-02-26 20:26:43 +000080}
81
Bill Wendlingcaf71d42012-03-31 10:49:43 +000082LTOCodeGenerator::~LTOCodeGenerator() {
83 delete _target;
84 delete _nativeObjectFile;
Rafael Espindola10519372013-05-04 02:43:00 +000085 delete _linker.getModule();
Bill Wendlingcaf71d42012-03-31 10:49:43 +000086
Bill Wendlingf2cc2ee2012-03-31 10:51:45 +000087 for (std::vector<char*>::iterator I = _codegenOptions.begin(),
Bill Wendlingcaf71d42012-03-31 10:49:43 +000088 E = _codegenOptions.end(); I != E; ++I)
89 free(*I);
Nick Kledzik77595fc2008-02-26 20:26:43 +000090}
91
Bill Wendlingc94c5622012-03-31 11:15:43 +000092bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) {
Rafael Espindolafca88632013-05-04 03:06:50 +000093 bool ret = _linker.linkInModule(mod->getLLVVMModule(), &errMsg);
Rafael Espindola38c4e532011-03-02 04:14:42 +000094
95 const std::vector<const char*> &undefs = mod->getAsmUndefinedRefs();
96 for (int i = 0, e = undefs.size(); i != e; ++i)
97 _asmUndefinedRefs[undefs[i]] = 1;
98
99 return ret;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000100}
Bill Wendling76b13ed2012-03-31 10:50:14 +0000101
Bill Wendlingc94c5622012-03-31 11:15:43 +0000102bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug,
103 std::string& errMsg) {
104 switch (debug) {
105 case LTO_DEBUG_MODEL_NONE:
106 _emitDwarfDebugInfo = false;
107 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000108
Bill Wendlingc94c5622012-03-31 11:15:43 +0000109 case LTO_DEBUG_MODEL_DWARF:
110 _emitDwarfDebugInfo = true;
111 return false;
112 }
113 llvm_unreachable("Unknown debug format!");
Nick Kledzik77595fc2008-02-26 20:26:43 +0000114}
115
Bill Wendling76b13ed2012-03-31 10:50:14 +0000116bool LTOCodeGenerator::setCodePICModel(lto_codegen_model model,
Bill Wendlingc94c5622012-03-31 11:15:43 +0000117 std::string& errMsg) {
118 switch (model) {
119 case LTO_CODEGEN_PIC_MODEL_STATIC:
120 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
121 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
122 _codeModel = model;
123 return false;
124 }
125 llvm_unreachable("Unknown PIC model!");
Nick Kledzik77595fc2008-02-26 20:26:43 +0000126}
127
Chris Lattnerb515d752009-08-23 07:49:08 +0000128bool LTOCodeGenerator::writeMergedModules(const char *path,
129 std::string &errMsg) {
130 if (determineTarget(errMsg))
131 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000132
Bill Wendling76b13ed2012-03-31 10:50:14 +0000133 // mark which symbols can not be internalized
Chris Lattnerb515d752009-08-23 07:49:08 +0000134 applyScopeRestrictions();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000135
Chris Lattnerb515d752009-08-23 07:49:08 +0000136 // create output file
137 std::string ErrInfo;
Dan Gohmanf2914012010-08-20 16:59:15 +0000138 tool_output_file Out(path, ErrInfo,
139 raw_fd_ostream::F_Binary);
Chris Lattnerb515d752009-08-23 07:49:08 +0000140 if (!ErrInfo.empty()) {
141 errMsg = "could not open bitcode file for writing: ";
142 errMsg += path;
143 return true;
144 }
Bill Wendling76b13ed2012-03-31 10:50:14 +0000145
Chris Lattnerb515d752009-08-23 07:49:08 +0000146 // write bitcode to it
Dan Gohmand4c45432010-09-01 14:20:41 +0000147 WriteBitcodeToFile(_linker.getModule(), Out.os());
148 Out.os().close();
Dan Gohman4b7416b2010-05-27 20:19:47 +0000149
Dan Gohmand4c45432010-09-01 14:20:41 +0000150 if (Out.os().has_error()) {
Chris Lattnerb515d752009-08-23 07:49:08 +0000151 errMsg = "could not write bitcode file: ";
152 errMsg += path;
Dan Gohmand4c45432010-09-01 14:20:41 +0000153 Out.os().clear_error();
Chris Lattnerb515d752009-08-23 07:49:08 +0000154 return true;
155 }
Bill Wendling76b13ed2012-03-31 10:50:14 +0000156
Dan Gohmanf2914012010-08-20 16:59:15 +0000157 Out.keep();
Chris Lattnerb515d752009-08-23 07:49:08 +0000158 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000159}
160
Bill Wendlingc94c5622012-03-31 11:15:43 +0000161bool LTOCodeGenerator::compile_to_file(const char** name, std::string& errMsg) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000162 // make unique temp .o file to put generated object file
Rafael Espindola68c0efa2013-06-17 18:05:35 +0000163 SmallString<128> Filename;
164 int FD;
Rafael Espindola1276b392013-07-05 20:14:52 +0000165 error_code EC = sys::fs::createTemporaryFile("lto-llvm", "o", FD, Filename);
Rafael Espindola68c0efa2013-06-17 18:05:35 +0000166 if (EC) {
167 errMsg = EC.message();
Rafael Espindola6421a882011-03-22 20:57:13 +0000168 return true;
169 }
Rafael Espindola6421a882011-03-22 20:57:13 +0000170
171 // generate object file
Rafael Espindola68c0efa2013-06-17 18:05:35 +0000172 tool_output_file objFile(Filename.c_str(), FD);
Bill Wendlingc94c5622012-03-31 11:15:43 +0000173
Rafael Espindola68c0efa2013-06-17 18:05:35 +0000174 bool genResult = generateObjectFile(objFile.os(), errMsg);
Rafael Espindola6421a882011-03-22 20:57:13 +0000175 objFile.os().close();
176 if (objFile.os().has_error()) {
177 objFile.os().clear_error();
Rafael Espindola68c0efa2013-06-17 18:05:35 +0000178 sys::fs::remove(Twine(Filename));
Rafael Espindola6421a882011-03-22 20:57:13 +0000179 return true;
180 }
Bill Wendlingc94c5622012-03-31 11:15:43 +0000181
Rafael Espindola6421a882011-03-22 20:57:13 +0000182 objFile.keep();
Bill Wendling0ca36af2012-08-08 22:03:50 +0000183 if (genResult) {
Rafael Espindola68c0efa2013-06-17 18:05:35 +0000184 sys::fs::remove(Twine(Filename));
Rafael Espindola6421a882011-03-22 20:57:13 +0000185 return true;
186 }
187
Rafael Espindola68c0efa2013-06-17 18:05:35 +0000188 _nativeObjectPath = Filename.c_str();
Rafael Espindola6421a882011-03-22 20:57:13 +0000189 *name = _nativeObjectPath.c_str();
190 return false;
191}
192
Bill Wendlingc94c5622012-03-31 11:15:43 +0000193const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000194 const char *name;
195 if (compile_to_file(&name, errMsg))
196 return NULL;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000197
Rafael Espindola6421a882011-03-22 20:57:13 +0000198 // remove old buffer if compile() called twice
199 delete _nativeObjectFile;
Nick Kledzikef194ed2008-02-27 22:25:36 +0000200
Rafael Espindola6421a882011-03-22 20:57:13 +0000201 // read .o file into memory buffer
202 OwningPtr<MemoryBuffer> BuffPtr;
203 if (error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) {
204 errMsg = ec.message();
Rafael Espindola68c0efa2013-06-17 18:05:35 +0000205 sys::fs::remove(_nativeObjectPath);
Rafael Espindola6421a882011-03-22 20:57:13 +0000206 return NULL;
207 }
208 _nativeObjectFile = BuffPtr.take();
Rafael Espindolae9efea12011-02-24 21:04:06 +0000209
Rafael Espindola6421a882011-03-22 20:57:13 +0000210 // remove temp files
Rafael Espindola68c0efa2013-06-17 18:05:35 +0000211 sys::fs::remove(_nativeObjectPath);
Rafael Espindolae9efea12011-02-24 21:04:06 +0000212
Rafael Espindola6421a882011-03-22 20:57:13 +0000213 // return buffer, unless error
Bill Wendling0ca36af2012-08-08 22:03:50 +0000214 if (_nativeObjectFile == NULL)
Rafael Espindola6421a882011-03-22 20:57:13 +0000215 return NULL;
216 *length = _nativeObjectFile->getBufferSize();
217 return _nativeObjectFile->getBufferStart();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000218}
219
Bill Wendling8c18a6f2013-05-23 21:21:50 +0000220bool LTOCodeGenerator::determineTarget(std::string &errMsg) {
Bill Wendling0ca36af2012-08-08 22:03:50 +0000221 if (_target != NULL)
222 return false;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000223
Bill Wendling8c18a6f2013-05-23 21:21:50 +0000224 // if options were requested, set them
225 if (!_codegenOptions.empty())
226 cl::ParseCommandLineOptions(_codegenOptions.size(),
227 const_cast<char **>(&_codegenOptions[0]));
228
Bob Wilson47ed8a12012-10-12 17:39:25 +0000229 std::string TripleStr = _linker.getModule()->getTargetTriple();
230 if (TripleStr.empty())
231 TripleStr = sys::getDefaultTargetTriple();
232 llvm::Triple Triple(TripleStr);
Bill Wendling604a8182008-06-18 06:35:30 +0000233
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000234 // create target machine from info for merged modules
Bob Wilson47ed8a12012-10-12 17:39:25 +0000235 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
Bill Wendling0ca36af2012-08-08 22:03:50 +0000236 if (march == NULL)
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000237 return true;
Bill Wendlingc94c5622012-03-31 11:15:43 +0000238
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000239 // The relocation model is actually a static member of TargetMachine and
240 // needs to be set before the TargetMachine is instantiated.
241 Reloc::Model RelocModel = Reloc::Default;
Bill Wendling0ca36af2012-08-08 22:03:50 +0000242 switch (_codeModel) {
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000243 case LTO_CODEGEN_PIC_MODEL_STATIC:
244 RelocModel = Reloc::Static;
245 break;
246 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
247 RelocModel = Reloc::PIC_;
248 break;
249 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
250 RelocModel = Reloc::DynamicNoPIC;
251 break;
Bill Wendlingc94c5622012-03-31 11:15:43 +0000252 }
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000253
254 // construct LTOModule, hand over ownership of module and target
255 SubtargetFeatures Features;
Bob Wilson47ed8a12012-10-12 17:39:25 +0000256 Features.getDefaultSubtargetFeatures(Triple);
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000257 std::string FeatureStr = Features.getString();
Bob Wilson47ed8a12012-10-12 17:39:25 +0000258 // Set a default CPU for Darwin triples.
259 if (_mCpu.empty() && Triple.isOSDarwin()) {
260 if (Triple.getArch() == llvm::Triple::x86_64)
261 _mCpu = "core2";
262 else if (Triple.getArch() == llvm::Triple::x86)
263 _mCpu = "yonah";
264 }
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000265 TargetOptions Options;
266 LTOModule::getTargetOptions(Options);
Bob Wilson47ed8a12012-10-12 17:39:25 +0000267 _target = march->createTargetMachine(TripleStr, _mCpu, FeatureStr, Options,
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000268 RelocModel, CodeModel::Default,
269 CodeGenOpt::Aggressive);
Bill Wendlingc94c5622012-03-31 11:15:43 +0000270 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000271}
272
Bill Wendlingc94c5622012-03-31 11:15:43 +0000273void LTOCodeGenerator::
274applyRestriction(GlobalValue &GV,
275 std::vector<const char*> &mustPreserveList,
276 SmallPtrSet<GlobalValue*, 8> &asmUsed,
277 Mangler &mangler) {
Rafael Espindola38c4e532011-03-02 04:14:42 +0000278 SmallString<64> Buffer;
279 mangler.getNameWithPrefix(Buffer, &GV, false);
280
281 if (GV.isDeclaration())
282 return;
283 if (_mustPreserveSymbols.count(Buffer))
284 mustPreserveList.push_back(GV.getName().data());
285 if (_asmUndefinedRefs.count(Buffer))
286 asmUsed.insert(&GV);
287}
288
289static void findUsedValues(GlobalVariable *LLVMUsed,
290 SmallPtrSet<GlobalValue*, 8> &UsedValues) {
291 if (LLVMUsed == 0) return;
292
Rafael Espindolad4ee3922013-04-24 17:54:35 +0000293 ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
Rafael Espindola38c4e532011-03-02 04:14:42 +0000294 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
Bill Wendling76b13ed2012-03-31 10:50:14 +0000295 if (GlobalValue *GV =
Bill Wendlingc94c5622012-03-31 11:15:43 +0000296 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
Rafael Espindola38c4e532011-03-02 04:14:42 +0000297 UsedValues.insert(GV);
298}
299
Chris Lattner5ef31a02010-03-12 18:44:54 +0000300void LTOCodeGenerator::applyScopeRestrictions() {
301 if (_scopeRestrictionsDone) return;
302 Module *mergedModule = _linker.getModule();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000303
Chris Lattner5ef31a02010-03-12 18:44:54 +0000304 // Start off with a verification pass.
305 PassManager passes;
306 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000307
Bill Wendling76b13ed2012-03-31 10:50:14 +0000308 // mark which symbols can not be internalized
Bill Wendling99cb6222013-06-18 07:20:20 +0000309 MCContext Context(_target->getMCAsmInfo(), _target->getRegisterInfo(), NULL);
Bill Wendlingcc5a8822013-05-29 20:37:19 +0000310 Mangler mangler(Context, _target);
Rafael Espindola38c4e532011-03-02 04:14:42 +0000311 std::vector<const char*> mustPreserveList;
312 SmallPtrSet<GlobalValue*, 8> asmUsed;
313
314 for (Module::iterator f = mergedModule->begin(),
315 e = mergedModule->end(); f != e; ++f)
316 applyRestriction(*f, mustPreserveList, asmUsed, mangler);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000317 for (Module::global_iterator v = mergedModule->global_begin(),
Rafael Espindola38c4e532011-03-02 04:14:42 +0000318 e = mergedModule->global_end(); v != e; ++v)
319 applyRestriction(*v, mustPreserveList, asmUsed, mangler);
320 for (Module::alias_iterator a = mergedModule->alias_begin(),
321 e = mergedModule->alias_end(); a != e; ++a)
322 applyRestriction(*a, mustPreserveList, asmUsed, mangler);
323
324 GlobalVariable *LLVMCompilerUsed =
325 mergedModule->getGlobalVariable("llvm.compiler.used");
326 findUsedValues(LLVMCompilerUsed, asmUsed);
327 if (LLVMCompilerUsed)
328 LLVMCompilerUsed->eraseFromParent();
329
Rafael Espindolad4ee3922013-04-24 17:54:35 +0000330 if (!asmUsed.empty()) {
331 llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(_context);
332 std::vector<Constant*> asmUsed2;
333 for (SmallPtrSet<GlobalValue*, 16>::const_iterator i = asmUsed.begin(),
334 e = asmUsed.end(); i !=e; ++i) {
335 GlobalValue *GV = *i;
336 Constant *c = ConstantExpr::getBitCast(GV, i8PTy);
337 asmUsed2.push_back(c);
338 }
339
340 llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size());
341 LLVMCompilerUsed =
342 new llvm::GlobalVariable(*mergedModule, ATy, false,
343 llvm::GlobalValue::AppendingLinkage,
344 llvm::ConstantArray::get(ATy, asmUsed2),
345 "llvm.compiler.used");
346
347 LLVMCompilerUsed->setSection("llvm.metadata");
Chris Lattner5ef31a02010-03-12 18:44:54 +0000348 }
Rafael Espindola38c4e532011-03-02 04:14:42 +0000349
Bill Wendling50f31832012-12-10 21:33:45 +0000350 passes.add(createInternalizePass(mustPreserveList));
Rafael Espindola38c4e532011-03-02 04:14:42 +0000351
Chris Lattner5ef31a02010-03-12 18:44:54 +0000352 // apply scope restrictions
353 passes.run(*mergedModule);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000354
Chris Lattner5ef31a02010-03-12 18:44:54 +0000355 _scopeRestrictionsDone = true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000356}
357
Nick Kledzik77595fc2008-02-26 20:26:43 +0000358/// Optimize merged modules using various IPO passes
Chris Lattner817a01f2011-05-22 00:20:07 +0000359bool LTOCodeGenerator::generateObjectFile(raw_ostream &out,
360 std::string &errMsg) {
Bill Wendling0ca36af2012-08-08 22:03:50 +0000361 if (this->determineTarget(errMsg))
Bill Wendlingc94c5622012-03-31 11:15:43 +0000362 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000363
Bill Wendlingc94c5622012-03-31 11:15:43 +0000364 Module* mergedModule = _linker.getModule();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000365
Bill Wendling8c18a6f2013-05-23 21:21:50 +0000366 // Mark which symbols can not be internalized
Bill Wendling64d5b282012-04-09 22:18:01 +0000367 this->applyScopeRestrictions();
368
Bill Wendlingc94c5622012-03-31 11:15:43 +0000369 // Instantiate the pass manager to organize the passes.
370 PassManager passes;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000371
Bill Wendlingc94c5622012-03-31 11:15:43 +0000372 // Start off with a verification pass.
373 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000374
Micah Villmow791cfc22012-10-08 16:39:34 +0000375 // Add an appropriate DataLayout instance for this module...
376 passes.add(new DataLayout(*_target->getDataLayout()));
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000377 _target->addAnalysisPasses(passes);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000378
Rafael Espindola4d2e9d92012-04-16 10:58:38 +0000379 // Enabling internalize here would use its AllButMain variant. It
380 // keeps only main if it exists and does nothing for libraries. Instead
381 // we create the pass ourselves with the symbol list provided by the linker.
Bill Wendling8c18a6f2013-05-23 21:21:50 +0000382 if (!DisableOpt)
Bill Wendling6303b662013-02-28 14:11:10 +0000383 PassManagerBuilder().populateLTOPassManager(passes,
Bill Wendling50f31832012-12-10 21:33:45 +0000384 /*Internalize=*/false,
Bill Wendling3197b442012-04-02 22:16:50 +0000385 !DisableInline,
386 DisableGVNLoadPRE);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000387
Bill Wendlingc94c5622012-03-31 11:15:43 +0000388 // Make sure everything is still good.
389 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000390
Lang Hamesa991b252013-03-13 21:18:46 +0000391 PassManager codeGenPasses;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000392
Lang Hamesa991b252013-03-13 21:18:46 +0000393 codeGenPasses.add(new DataLayout(*_target->getDataLayout()));
394 _target->addAnalysisPasses(codeGenPasses);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000395
Bill Wendlingc94c5622012-03-31 11:15:43 +0000396 formatted_raw_ostream Out(out);
Dan Gohmand4c45432010-09-01 14:20:41 +0000397
Bob Wilsond6965442013-03-29 23:28:55 +0000398 // If the bitcode files contain ARC code and were compiled with optimization,
399 // the ObjCARCContractPass must be run, so do it unconditionally here.
400 codeGenPasses.add(createObjCARCContractPass());
401
Lang Hamesa991b252013-03-13 21:18:46 +0000402 if (_target->addPassesToEmitFile(codeGenPasses, Out,
David Blaikie4f56a302012-05-30 18:42:51 +0000403 TargetMachine::CGFT_ObjectFile)) {
Bill Wendlingc94c5622012-03-31 11:15:43 +0000404 errMsg = "target file type not supported";
405 return true;
406 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000407
Bill Wendlingc94c5622012-03-31 11:15:43 +0000408 // Run our queue of passes all at once now, efficiently.
409 passes.run(*mergedModule);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000410
Bill Wendlingc94c5622012-03-31 11:15:43 +0000411 // Run the code generator, and write assembly file
Lang Hamesa991b252013-03-13 21:18:46 +0000412 codeGenPasses.run(*mergedModule);
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000413
Bill Wendlingc94c5622012-03-31 11:15:43 +0000414 return false; // success
Nick Kledzik77595fc2008-02-26 20:26:43 +0000415}
416
Bill Wendlingcaf71d42012-03-31 10:49:43 +0000417/// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
418/// LTO problems.
419void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) {
420 for (std::pair<StringRef, StringRef> o = getToken(options);
421 !o.first.empty(); o = getToken(o.second)) {
422 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add
423 // that.
Bill Wendling0ca36af2012-08-08 22:03:50 +0000424 if (_codegenOptions.empty())
Bill Wendlingcaf71d42012-03-31 10:49:43 +0000425 _codegenOptions.push_back(strdup("libLTO"));
426 _codegenOptions.push_back(strdup(o.first.str().c_str()));
427 }
Nick Kledzik920ae982008-07-08 21:14:10 +0000428}