blob: 465ccb4f2cb57b34bfe5e3497eb8935ac6b68c28 [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"
Bob Wilsond6965442013-03-29 23:28:55 +000047#include "llvm/Transforms/ObjCARC.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000048using namespace llvm;
49
Bill Wendling9ac0aaa2012-08-06 21:34:54 +000050static cl::opt<bool>
Bill Wendling6303b662013-02-28 14:11:10 +000051DisableOpt("disable-opt", cl::init(false),
52 cl::desc("Do not run any optimization passes"));
53
54static cl::opt<bool>
Bill Wendling9ac0aaa2012-08-06 21:34:54 +000055DisableInline("disable-inlining", cl::init(false),
Nick Kledzik920ae982008-07-08 21:14:10 +000056 cl::desc("Do not run the inliner pass"));
Nick Kledzik77595fc2008-02-26 20:26:43 +000057
Bill Wendling9ac0aaa2012-08-06 21:34:54 +000058static cl::opt<bool>
59DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
Bill Wendling3197b442012-04-02 22:16:50 +000060 cl::desc("Do not run the GVN load PRE pass"));
61
Bill Wendlingcaf71d42012-03-31 10:49:43 +000062const char* LTOCodeGenerator::getVersionString() {
Nick Kledzik77595fc2008-02-26 20:26:43 +000063#ifdef LLVM_VERSION_INFO
Bill Wendlingcaf71d42012-03-31 10:49:43 +000064 return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
Nick Kledzik77595fc2008-02-26 20:26:43 +000065#else
Bill Wendlingcaf71d42012-03-31 10:49:43 +000066 return PACKAGE_NAME " version " PACKAGE_VERSION;
Nick Kledzik77595fc2008-02-26 20:26:43 +000067#endif
68}
69
Bill Wendling76b13ed2012-03-31 10:50:14 +000070LTOCodeGenerator::LTOCodeGenerator()
Bill Wendlingc94c5622012-03-31 11:15:43 +000071 : _context(getGlobalContext()),
Rafael Espindola10519372013-05-04 02:43:00 +000072 _linker(new Module("ld-temp.o", _context)), _target(NULL),
Bill Wendlingc94c5622012-03-31 11:15:43 +000073 _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false),
Bill Wendling50f31832012-12-10 21:33:45 +000074 _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
Bill Wendlingc94c5622012-03-31 11:15:43 +000075 _nativeObjectFile(NULL) {
76 InitializeAllTargets();
77 InitializeAllTargetMCs();
78 InitializeAllAsmPrinters();
Nick Kledzik77595fc2008-02-26 20:26:43 +000079}
80
Bill Wendlingcaf71d42012-03-31 10:49:43 +000081LTOCodeGenerator::~LTOCodeGenerator() {
82 delete _target;
83 delete _nativeObjectFile;
Rafael Espindola10519372013-05-04 02:43:00 +000084 delete _linker.getModule();
Bill Wendlingcaf71d42012-03-31 10:49:43 +000085
Bill Wendlingf2cc2ee2012-03-31 10:51:45 +000086 for (std::vector<char*>::iterator I = _codegenOptions.begin(),
Bill Wendlingcaf71d42012-03-31 10:49:43 +000087 E = _codegenOptions.end(); I != E; ++I)
88 free(*I);
Nick Kledzik77595fc2008-02-26 20:26:43 +000089}
90
Bill Wendlingc94c5622012-03-31 11:15:43 +000091bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) {
Rafael Espindolafca88632013-05-04 03:06:50 +000092 bool ret = _linker.linkInModule(mod->getLLVVMModule(), &errMsg);
Rafael Espindola38c4e532011-03-02 04:14:42 +000093
94 const std::vector<const char*> &undefs = mod->getAsmUndefinedRefs();
95 for (int i = 0, e = undefs.size(); i != e; ++i)
96 _asmUndefinedRefs[undefs[i]] = 1;
97
98 return ret;
Nick Kledzik77595fc2008-02-26 20:26:43 +000099}
Bill Wendling76b13ed2012-03-31 10:50:14 +0000100
Bill Wendlingc94c5622012-03-31 11:15:43 +0000101bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug,
102 std::string& errMsg) {
103 switch (debug) {
104 case LTO_DEBUG_MODEL_NONE:
105 _emitDwarfDebugInfo = false;
106 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000107
Bill Wendlingc94c5622012-03-31 11:15:43 +0000108 case LTO_DEBUG_MODEL_DWARF:
109 _emitDwarfDebugInfo = true;
110 return false;
111 }
112 llvm_unreachable("Unknown debug format!");
Nick Kledzik77595fc2008-02-26 20:26:43 +0000113}
114
Bill Wendling76b13ed2012-03-31 10:50:14 +0000115bool LTOCodeGenerator::setCodePICModel(lto_codegen_model model,
Bill Wendlingc94c5622012-03-31 11:15:43 +0000116 std::string& errMsg) {
117 switch (model) {
118 case LTO_CODEGEN_PIC_MODEL_STATIC:
119 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
120 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
121 _codeModel = model;
122 return false;
123 }
124 llvm_unreachable("Unknown PIC model!");
Nick Kledzik77595fc2008-02-26 20:26:43 +0000125}
126
Chris Lattnerb515d752009-08-23 07:49:08 +0000127bool LTOCodeGenerator::writeMergedModules(const char *path,
128 std::string &errMsg) {
129 if (determineTarget(errMsg))
130 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000131
Bill Wendling76b13ed2012-03-31 10:50:14 +0000132 // mark which symbols can not be internalized
Chris Lattnerb515d752009-08-23 07:49:08 +0000133 applyScopeRestrictions();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000134
Chris Lattnerb515d752009-08-23 07:49:08 +0000135 // create output file
136 std::string ErrInfo;
Dan Gohmanf2914012010-08-20 16:59:15 +0000137 tool_output_file Out(path, ErrInfo,
138 raw_fd_ostream::F_Binary);
Chris Lattnerb515d752009-08-23 07:49:08 +0000139 if (!ErrInfo.empty()) {
140 errMsg = "could not open bitcode file for writing: ";
141 errMsg += path;
142 return true;
143 }
Bill Wendling76b13ed2012-03-31 10:50:14 +0000144
Chris Lattnerb515d752009-08-23 07:49:08 +0000145 // write bitcode to it
Dan Gohmand4c45432010-09-01 14:20:41 +0000146 WriteBitcodeToFile(_linker.getModule(), Out.os());
147 Out.os().close();
Dan Gohman4b7416b2010-05-27 20:19:47 +0000148
Dan Gohmand4c45432010-09-01 14:20:41 +0000149 if (Out.os().has_error()) {
Chris Lattnerb515d752009-08-23 07:49:08 +0000150 errMsg = "could not write bitcode file: ";
151 errMsg += path;
Dan Gohmand4c45432010-09-01 14:20:41 +0000152 Out.os().clear_error();
Chris Lattnerb515d752009-08-23 07:49:08 +0000153 return true;
154 }
Bill Wendling76b13ed2012-03-31 10:50:14 +0000155
Dan Gohmanf2914012010-08-20 16:59:15 +0000156 Out.keep();
Chris Lattnerb515d752009-08-23 07:49:08 +0000157 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000158}
159
Bill Wendlingc94c5622012-03-31 11:15:43 +0000160bool LTOCodeGenerator::compile_to_file(const char** name, std::string& errMsg) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000161 // make unique temp .o file to put generated object file
162 sys::PathWithStatus uniqueObjPath("lto-llvm.o");
Bill Wendling0ca36af2012-08-08 22:03:50 +0000163 if (uniqueObjPath.createTemporaryFileOnDisk(false, &errMsg)) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000164 uniqueObjPath.eraseFromDisk();
165 return true;
166 }
167 sys::RemoveFileOnSignal(uniqueObjPath);
168
169 // generate object file
170 bool genResult = false;
171 tool_output_file objFile(uniqueObjPath.c_str(), errMsg);
Bill Wendling0b95a992012-09-06 21:07:57 +0000172 if (!errMsg.empty()) {
173 uniqueObjPath.eraseFromDisk();
John Criswell3f0e2372011-08-18 01:19:05 +0000174 return true;
Bill Wendling0b95a992012-09-06 21:07:57 +0000175 }
Bill Wendlingc94c5622012-03-31 11:15:43 +0000176
Rafael Espindola6421a882011-03-22 20:57:13 +0000177 genResult = this->generateObjectFile(objFile.os(), errMsg);
178 objFile.os().close();
179 if (objFile.os().has_error()) {
180 objFile.os().clear_error();
Bill Wendling0b95a992012-09-06 21:07:57 +0000181 uniqueObjPath.eraseFromDisk();
Rafael Espindola6421a882011-03-22 20:57:13 +0000182 return true;
183 }
Bill Wendlingc94c5622012-03-31 11:15:43 +0000184
Rafael Espindola6421a882011-03-22 20:57:13 +0000185 objFile.keep();
Bill Wendling0ca36af2012-08-08 22:03:50 +0000186 if (genResult) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000187 uniqueObjPath.eraseFromDisk();
188 return true;
189 }
190
191 _nativeObjectPath = uniqueObjPath.str();
192 *name = _nativeObjectPath.c_str();
193 return false;
194}
195
Bill Wendlingc94c5622012-03-31 11:15:43 +0000196const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000197 const char *name;
198 if (compile_to_file(&name, errMsg))
199 return NULL;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000200
Rafael Espindola6421a882011-03-22 20:57:13 +0000201 // remove old buffer if compile() called twice
202 delete _nativeObjectFile;
Nick Kledzikef194ed2008-02-27 22:25:36 +0000203
Rafael Espindola6421a882011-03-22 20:57:13 +0000204 // read .o file into memory buffer
205 OwningPtr<MemoryBuffer> BuffPtr;
206 if (error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) {
207 errMsg = ec.message();
Bill Wendling0b95a992012-09-06 21:07:57 +0000208 sys::Path(_nativeObjectPath).eraseFromDisk();
Rafael Espindola6421a882011-03-22 20:57:13 +0000209 return NULL;
210 }
211 _nativeObjectFile = BuffPtr.take();
Rafael Espindolae9efea12011-02-24 21:04:06 +0000212
Rafael Espindola6421a882011-03-22 20:57:13 +0000213 // remove temp files
214 sys::Path(_nativeObjectPath).eraseFromDisk();
Rafael Espindolae9efea12011-02-24 21:04:06 +0000215
Rafael Espindola6421a882011-03-22 20:57:13 +0000216 // return buffer, unless error
Bill Wendling0ca36af2012-08-08 22:03:50 +0000217 if (_nativeObjectFile == NULL)
Rafael Espindola6421a882011-03-22 20:57:13 +0000218 return NULL;
219 *length = _nativeObjectFile->getBufferSize();
220 return _nativeObjectFile->getBufferStart();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000221}
222
Bill Wendling8c18a6f2013-05-23 21:21:50 +0000223bool LTOCodeGenerator::determineTarget(std::string &errMsg) {
Bill Wendling0ca36af2012-08-08 22:03:50 +0000224 if (_target != NULL)
225 return false;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000226
Bill Wendling8c18a6f2013-05-23 21:21:50 +0000227 // if options were requested, set them
228 if (!_codegenOptions.empty())
229 cl::ParseCommandLineOptions(_codegenOptions.size(),
230 const_cast<char **>(&_codegenOptions[0]));
231
Bob Wilson47ed8a12012-10-12 17:39:25 +0000232 std::string TripleStr = _linker.getModule()->getTargetTriple();
233 if (TripleStr.empty())
234 TripleStr = sys::getDefaultTargetTriple();
235 llvm::Triple Triple(TripleStr);
Bill Wendling604a8182008-06-18 06:35:30 +0000236
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000237 // create target machine from info for merged modules
Bob Wilson47ed8a12012-10-12 17:39:25 +0000238 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
Bill Wendling0ca36af2012-08-08 22:03:50 +0000239 if (march == NULL)
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000240 return true;
Bill Wendlingc94c5622012-03-31 11:15:43 +0000241
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000242 // The relocation model is actually a static member of TargetMachine and
243 // needs to be set before the TargetMachine is instantiated.
244 Reloc::Model RelocModel = Reloc::Default;
Bill Wendling0ca36af2012-08-08 22:03:50 +0000245 switch (_codeModel) {
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000246 case LTO_CODEGEN_PIC_MODEL_STATIC:
247 RelocModel = Reloc::Static;
248 break;
249 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
250 RelocModel = Reloc::PIC_;
251 break;
252 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
253 RelocModel = Reloc::DynamicNoPIC;
254 break;
Bill Wendlingc94c5622012-03-31 11:15:43 +0000255 }
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000256
257 // construct LTOModule, hand over ownership of module and target
258 SubtargetFeatures Features;
Bob Wilson47ed8a12012-10-12 17:39:25 +0000259 Features.getDefaultSubtargetFeatures(Triple);
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000260 std::string FeatureStr = Features.getString();
Bob Wilson47ed8a12012-10-12 17:39:25 +0000261 // Set a default CPU for Darwin triples.
262 if (_mCpu.empty() && Triple.isOSDarwin()) {
263 if (Triple.getArch() == llvm::Triple::x86_64)
264 _mCpu = "core2";
265 else if (Triple.getArch() == llvm::Triple::x86)
266 _mCpu = "yonah";
267 }
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000268 TargetOptions Options;
269 LTOModule::getTargetOptions(Options);
Bob Wilson47ed8a12012-10-12 17:39:25 +0000270 _target = march->createTargetMachine(TripleStr, _mCpu, FeatureStr, Options,
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000271 RelocModel, CodeModel::Default,
272 CodeGenOpt::Aggressive);
Bill Wendlingc94c5622012-03-31 11:15:43 +0000273 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000274}
275
Bill Wendlingc94c5622012-03-31 11:15:43 +0000276void LTOCodeGenerator::
277applyRestriction(GlobalValue &GV,
278 std::vector<const char*> &mustPreserveList,
279 SmallPtrSet<GlobalValue*, 8> &asmUsed,
280 Mangler &mangler) {
Rafael Espindola38c4e532011-03-02 04:14:42 +0000281 SmallString<64> Buffer;
282 mangler.getNameWithPrefix(Buffer, &GV, false);
283
284 if (GV.isDeclaration())
285 return;
286 if (_mustPreserveSymbols.count(Buffer))
287 mustPreserveList.push_back(GV.getName().data());
288 if (_asmUndefinedRefs.count(Buffer))
289 asmUsed.insert(&GV);
290}
291
292static void findUsedValues(GlobalVariable *LLVMUsed,
293 SmallPtrSet<GlobalValue*, 8> &UsedValues) {
294 if (LLVMUsed == 0) return;
295
Rafael Espindolad4ee3922013-04-24 17:54:35 +0000296 ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
Rafael Espindola38c4e532011-03-02 04:14:42 +0000297 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
Bill Wendling76b13ed2012-03-31 10:50:14 +0000298 if (GlobalValue *GV =
Bill Wendlingc94c5622012-03-31 11:15:43 +0000299 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
Rafael Espindola38c4e532011-03-02 04:14:42 +0000300 UsedValues.insert(GV);
301}
302
Chris Lattner5ef31a02010-03-12 18:44:54 +0000303void LTOCodeGenerator::applyScopeRestrictions() {
304 if (_scopeRestrictionsDone) return;
305 Module *mergedModule = _linker.getModule();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000306
Chris Lattner5ef31a02010-03-12 18:44:54 +0000307 // Start off with a verification pass.
308 PassManager passes;
309 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000310
Bill Wendling76b13ed2012-03-31 10:50:14 +0000311 // mark which symbols can not be internalized
Bill Wendlingc94c5622012-03-31 11:15:43 +0000312 MCContext Context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(),NULL);
Bill Wendlingcc5a8822013-05-29 20:37:19 +0000313 Mangler mangler(Context, _target);
Rafael Espindola38c4e532011-03-02 04:14:42 +0000314 std::vector<const char*> mustPreserveList;
315 SmallPtrSet<GlobalValue*, 8> asmUsed;
316
317 for (Module::iterator f = mergedModule->begin(),
318 e = mergedModule->end(); f != e; ++f)
319 applyRestriction(*f, mustPreserveList, asmUsed, mangler);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000320 for (Module::global_iterator v = mergedModule->global_begin(),
Rafael Espindola38c4e532011-03-02 04:14:42 +0000321 e = mergedModule->global_end(); v != e; ++v)
322 applyRestriction(*v, mustPreserveList, asmUsed, mangler);
323 for (Module::alias_iterator a = mergedModule->alias_begin(),
324 e = mergedModule->alias_end(); a != e; ++a)
325 applyRestriction(*a, mustPreserveList, asmUsed, mangler);
326
327 GlobalVariable *LLVMCompilerUsed =
328 mergedModule->getGlobalVariable("llvm.compiler.used");
329 findUsedValues(LLVMCompilerUsed, asmUsed);
330 if (LLVMCompilerUsed)
331 LLVMCompilerUsed->eraseFromParent();
332
Rafael Espindolad4ee3922013-04-24 17:54:35 +0000333 if (!asmUsed.empty()) {
334 llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(_context);
335 std::vector<Constant*> asmUsed2;
336 for (SmallPtrSet<GlobalValue*, 16>::const_iterator i = asmUsed.begin(),
337 e = asmUsed.end(); i !=e; ++i) {
338 GlobalValue *GV = *i;
339 Constant *c = ConstantExpr::getBitCast(GV, i8PTy);
340 asmUsed2.push_back(c);
341 }
342
343 llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size());
344 LLVMCompilerUsed =
345 new llvm::GlobalVariable(*mergedModule, ATy, false,
346 llvm::GlobalValue::AppendingLinkage,
347 llvm::ConstantArray::get(ATy, asmUsed2),
348 "llvm.compiler.used");
349
350 LLVMCompilerUsed->setSection("llvm.metadata");
Chris Lattner5ef31a02010-03-12 18:44:54 +0000351 }
Rafael Espindola38c4e532011-03-02 04:14:42 +0000352
Bill Wendling50f31832012-12-10 21:33:45 +0000353 passes.add(createInternalizePass(mustPreserveList));
Rafael Espindola38c4e532011-03-02 04:14:42 +0000354
Chris Lattner5ef31a02010-03-12 18:44:54 +0000355 // apply scope restrictions
356 passes.run(*mergedModule);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000357
Chris Lattner5ef31a02010-03-12 18:44:54 +0000358 _scopeRestrictionsDone = true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000359}
360
Nick Kledzik77595fc2008-02-26 20:26:43 +0000361/// Optimize merged modules using various IPO passes
Chris Lattner817a01f2011-05-22 00:20:07 +0000362bool LTOCodeGenerator::generateObjectFile(raw_ostream &out,
363 std::string &errMsg) {
Bill Wendling0ca36af2012-08-08 22:03:50 +0000364 if (this->determineTarget(errMsg))
Bill Wendlingc94c5622012-03-31 11:15:43 +0000365 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000366
Bill Wendlingc94c5622012-03-31 11:15:43 +0000367 Module* mergedModule = _linker.getModule();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000368
Bill Wendling8c18a6f2013-05-23 21:21:50 +0000369 // Mark which symbols can not be internalized
Bill Wendling64d5b282012-04-09 22:18:01 +0000370 this->applyScopeRestrictions();
371
Bill Wendlingc94c5622012-03-31 11:15:43 +0000372 // Instantiate the pass manager to organize the passes.
373 PassManager passes;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000374
Bill Wendlingc94c5622012-03-31 11:15:43 +0000375 // Start off with a verification pass.
376 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000377
Micah Villmow791cfc22012-10-08 16:39:34 +0000378 // Add an appropriate DataLayout instance for this module...
379 passes.add(new DataLayout(*_target->getDataLayout()));
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000380 _target->addAnalysisPasses(passes);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000381
Rafael Espindola4d2e9d92012-04-16 10:58:38 +0000382 // Enabling internalize here would use its AllButMain variant. It
383 // keeps only main if it exists and does nothing for libraries. Instead
384 // we create the pass ourselves with the symbol list provided by the linker.
Bill Wendling8c18a6f2013-05-23 21:21:50 +0000385 if (!DisableOpt)
Bill Wendling6303b662013-02-28 14:11:10 +0000386 PassManagerBuilder().populateLTOPassManager(passes,
Bill Wendling50f31832012-12-10 21:33:45 +0000387 /*Internalize=*/false,
Bill Wendling3197b442012-04-02 22:16:50 +0000388 !DisableInline,
389 DisableGVNLoadPRE);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000390
Bill Wendlingc94c5622012-03-31 11:15:43 +0000391 // Make sure everything is still good.
392 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000393
Lang Hamesa991b252013-03-13 21:18:46 +0000394 PassManager codeGenPasses;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000395
Lang Hamesa991b252013-03-13 21:18:46 +0000396 codeGenPasses.add(new DataLayout(*_target->getDataLayout()));
397 _target->addAnalysisPasses(codeGenPasses);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000398
Bill Wendlingc94c5622012-03-31 11:15:43 +0000399 formatted_raw_ostream Out(out);
Dan Gohmand4c45432010-09-01 14:20:41 +0000400
Bob Wilsond6965442013-03-29 23:28:55 +0000401 // If the bitcode files contain ARC code and were compiled with optimization,
402 // the ObjCARCContractPass must be run, so do it unconditionally here.
403 codeGenPasses.add(createObjCARCContractPass());
404
Lang Hamesa991b252013-03-13 21:18:46 +0000405 if (_target->addPassesToEmitFile(codeGenPasses, Out,
David Blaikie4f56a302012-05-30 18:42:51 +0000406 TargetMachine::CGFT_ObjectFile)) {
Bill Wendlingc94c5622012-03-31 11:15:43 +0000407 errMsg = "target file type not supported";
408 return true;
409 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000410
Bill Wendlingc94c5622012-03-31 11:15:43 +0000411 // Run our queue of passes all at once now, efficiently.
412 passes.run(*mergedModule);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000413
Bill Wendlingc94c5622012-03-31 11:15:43 +0000414 // Run the code generator, and write assembly file
Lang Hamesa991b252013-03-13 21:18:46 +0000415 codeGenPasses.run(*mergedModule);
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000416
Bill Wendlingc94c5622012-03-31 11:15:43 +0000417 return false; // success
Nick Kledzik77595fc2008-02-26 20:26:43 +0000418}
419
Bill Wendlingcaf71d42012-03-31 10:49:43 +0000420/// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
421/// LTO problems.
422void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) {
423 for (std::pair<StringRef, StringRef> o = getToken(options);
424 !o.first.empty(); o = getToken(o.second)) {
425 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add
426 // that.
Bill Wendling0ca36af2012-08-08 22:03:50 +0000427 if (_codegenOptions.empty())
Bill Wendlingcaf71d42012-03-31 10:49:43 +0000428 _codegenOptions.push_back(strdup("libLTO"));
429 _codegenOptions.push_back(strdup(o.first.str().c_str()));
430 }
Nick Kledzik920ae982008-07-08 21:14:10 +0000431}