blob: 758227d61e020fab5288e6e450e4ccaf563c1445 [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"
Shuxin Yang8945f752013-07-22 18:40:34 +000027#include "llvm/InitializePasses.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000028#include "llvm/Linker.h"
Chris Lattner5ef31a02010-03-12 18:44:54 +000029#include "llvm/MC/MCAsmInfo.h"
30#include "llvm/MC/MCContext.h"
Evan Chengab8be962011-06-29 01:14:12 +000031#include "llvm/MC/SubtargetFeature.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000032#include "llvm/PassManager.h"
Nick Kledzik920ae982008-07-08 21:14:10 +000033#include "llvm/Support/CommandLine.h"
Rafael Espindola68c0efa2013-06-17 18:05:35 +000034#include "llvm/Support/FileSystem.h"
David Greene71847812009-07-14 20:18:05 +000035#include "llvm/Support/FormattedStream.h"
Michael J. Spencer3cc52ea2010-11-29 18:47:54 +000036#include "llvm/Support/Host.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000037#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer3cc52ea2010-11-29 18:47:54 +000038#include "llvm/Support/Signals.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000039#include "llvm/Support/TargetRegistry.h"
40#include "llvm/Support/TargetSelect.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000041#include "llvm/Support/ToolOutputFile.h"
Michael J. Spencerf2f516f2010-12-09 18:06:07 +000042#include "llvm/Support/system_error.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000043#include "llvm/Target/Mangler.h"
44#include "llvm/Target/TargetMachine.h"
45#include "llvm/Target/TargetOptions.h"
46#include "llvm/Target/TargetRegisterInfo.h"
47#include "llvm/Transforms/IPO.h"
48#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Bob Wilsond6965442013-03-29 23:28:55 +000049#include "llvm/Transforms/ObjCARC.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000050using namespace llvm;
51
Bill Wendling9ac0aaa2012-08-06 21:34:54 +000052static cl::opt<bool>
Bill Wendling6303b662013-02-28 14:11:10 +000053DisableOpt("disable-opt", cl::init(false),
54 cl::desc("Do not run any optimization passes"));
55
56static cl::opt<bool>
Bill Wendling9ac0aaa2012-08-06 21:34:54 +000057DisableInline("disable-inlining", cl::init(false),
Nick Kledzik920ae982008-07-08 21:14:10 +000058 cl::desc("Do not run the inliner pass"));
Nick Kledzik77595fc2008-02-26 20:26:43 +000059
Bill Wendling9ac0aaa2012-08-06 21:34:54 +000060static cl::opt<bool>
61DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
Bill Wendling3197b442012-04-02 22:16:50 +000062 cl::desc("Do not run the GVN load PRE pass"));
63
Bill Wendlingcaf71d42012-03-31 10:49:43 +000064const char* LTOCodeGenerator::getVersionString() {
Nick Kledzik77595fc2008-02-26 20:26:43 +000065#ifdef LLVM_VERSION_INFO
Bill Wendlingcaf71d42012-03-31 10:49:43 +000066 return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
Nick Kledzik77595fc2008-02-26 20:26:43 +000067#else
Bill Wendlingcaf71d42012-03-31 10:49:43 +000068 return PACKAGE_NAME " version " PACKAGE_VERSION;
Nick Kledzik77595fc2008-02-26 20:26:43 +000069#endif
70}
71
Bill Wendling76b13ed2012-03-31 10:50:14 +000072LTOCodeGenerator::LTOCodeGenerator()
Bill Wendlingc94c5622012-03-31 11:15:43 +000073 : _context(getGlobalContext()),
Rafael Espindola10519372013-05-04 02:43:00 +000074 _linker(new Module("ld-temp.o", _context)), _target(NULL),
Bill Wendlingc94c5622012-03-31 11:15:43 +000075 _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false),
Bill Wendling50f31832012-12-10 21:33:45 +000076 _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
Bill Wendlingc94c5622012-03-31 11:15:43 +000077 _nativeObjectFile(NULL) {
78 InitializeAllTargets();
79 InitializeAllTargetMCs();
80 InitializeAllAsmPrinters();
Shuxin Yang8945f752013-07-22 18:40:34 +000081 initializeLTOPasses();
Nick Kledzik77595fc2008-02-26 20:26:43 +000082}
83
Bill Wendlingcaf71d42012-03-31 10:49:43 +000084LTOCodeGenerator::~LTOCodeGenerator() {
85 delete _target;
86 delete _nativeObjectFile;
Rafael Espindola10519372013-05-04 02:43:00 +000087 delete _linker.getModule();
Bill Wendlingcaf71d42012-03-31 10:49:43 +000088
Bill Wendlingf2cc2ee2012-03-31 10:51:45 +000089 for (std::vector<char*>::iterator I = _codegenOptions.begin(),
Bill Wendlingcaf71d42012-03-31 10:49:43 +000090 E = _codegenOptions.end(); I != E; ++I)
91 free(*I);
Nick Kledzik77595fc2008-02-26 20:26:43 +000092}
93
Shuxin Yang8945f752013-07-22 18:40:34 +000094// Initialize LTO passes. Please keep this funciton in sync with
Shuxin Yangc679d6b2013-07-23 06:44:34 +000095// PassManagerBuilder::populateLTOPassManager(), and make sure all LTO
Shuxin Yang8945f752013-07-22 18:40:34 +000096// passes are initialized.
97//
98void LTOCodeGenerator::initializeLTOPasses() {
99 PassRegistry &R = *PassRegistry::getPassRegistry();
100
101 initializeInternalizePassPass(R);
102 initializeIPSCCPPass(R);
103 initializeGlobalOptPass(R);
104 initializeConstantMergePass(R);
105 initializeDAHPass(R);
106 initializeInstCombinerPass(R);
107 initializeSimpleInlinerPass(R);
108 initializePruneEHPass(R);
109 initializeGlobalDCEPass(R);
110 initializeArgPromotionPass(R);
111 initializeJumpThreadingPass(R);
112 initializeSROAPass(R);
113 initializeSROA_DTPass(R);
114 initializeSROA_SSAUpPass(R);
115 initializeFunctionAttrsPass(R);
116 initializeGlobalsModRefPass(R);
117 initializeLICMPass(R);
118 initializeGVNPass(R);
119 initializeMemCpyOptPass(R);
120 initializeDCEPass(R);
Tom Stellard01d72032013-08-06 02:43:45 +0000121 initializeCFGSimplifyPassPass(R);
Shuxin Yang8945f752013-07-22 18:40:34 +0000122}
123
Bill Wendlingc94c5622012-03-31 11:15:43 +0000124bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) {
Rafael Espindolafca88632013-05-04 03:06:50 +0000125 bool ret = _linker.linkInModule(mod->getLLVVMModule(), &errMsg);
Rafael Espindola38c4e532011-03-02 04:14:42 +0000126
127 const std::vector<const char*> &undefs = mod->getAsmUndefinedRefs();
128 for (int i = 0, e = undefs.size(); i != e; ++i)
129 _asmUndefinedRefs[undefs[i]] = 1;
130
Shuxin Yang235089b2013-08-07 05:19:23 +0000131 return !ret;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000132}
Bill Wendling76b13ed2012-03-31 10:50:14 +0000133
Shuxin Yang235089b2013-08-07 05:19:23 +0000134void LTOCodeGenerator::setDebugInfo(lto_debug_model debug) {
Bill Wendlingc94c5622012-03-31 11:15:43 +0000135 switch (debug) {
136 case LTO_DEBUG_MODEL_NONE:
137 _emitDwarfDebugInfo = false;
Shuxin Yang235089b2013-08-07 05:19:23 +0000138 return;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000139
Bill Wendlingc94c5622012-03-31 11:15:43 +0000140 case LTO_DEBUG_MODEL_DWARF:
141 _emitDwarfDebugInfo = true;
Shuxin Yang235089b2013-08-07 05:19:23 +0000142 return;
Bill Wendlingc94c5622012-03-31 11:15:43 +0000143 }
144 llvm_unreachable("Unknown debug format!");
Nick Kledzik77595fc2008-02-26 20:26:43 +0000145}
146
Shuxin Yang235089b2013-08-07 05:19:23 +0000147void LTOCodeGenerator::setCodePICModel(lto_codegen_model model) {
Bill Wendlingc94c5622012-03-31 11:15:43 +0000148 switch (model) {
149 case LTO_CODEGEN_PIC_MODEL_STATIC:
150 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
151 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
152 _codeModel = model;
Shuxin Yang235089b2013-08-07 05:19:23 +0000153 return;
Bill Wendlingc94c5622012-03-31 11:15:43 +0000154 }
155 llvm_unreachable("Unknown PIC model!");
Nick Kledzik77595fc2008-02-26 20:26:43 +0000156}
157
Chris Lattnerb515d752009-08-23 07:49:08 +0000158bool LTOCodeGenerator::writeMergedModules(const char *path,
159 std::string &errMsg) {
Shuxin Yang08809392013-08-06 21:51:21 +0000160 if (!determineTarget(errMsg))
Shuxin Yang235089b2013-08-07 05:19:23 +0000161 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000162
Bill Wendlingc6fbaa92013-07-08 23:23:03 +0000163 // Run the verifier on the merged modules.
164 PassManager passes;
165 passes.add(createVerifierPass());
166 passes.run(*_linker.getModule());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000167
Chris Lattnerb515d752009-08-23 07:49:08 +0000168 // create output file
169 std::string ErrInfo;
Rafael Espindolac1b49b52013-07-16 19:44:17 +0000170 tool_output_file Out(path, ErrInfo, sys::fs::F_Binary);
Chris Lattnerb515d752009-08-23 07:49:08 +0000171 if (!ErrInfo.empty()) {
172 errMsg = "could not open bitcode file for writing: ";
173 errMsg += path;
Shuxin Yang235089b2013-08-07 05:19:23 +0000174 return false;
Chris Lattnerb515d752009-08-23 07:49:08 +0000175 }
Bill Wendling76b13ed2012-03-31 10:50:14 +0000176
Chris Lattnerb515d752009-08-23 07:49:08 +0000177 // write bitcode to it
Dan Gohmand4c45432010-09-01 14:20:41 +0000178 WriteBitcodeToFile(_linker.getModule(), Out.os());
179 Out.os().close();
Dan Gohman4b7416b2010-05-27 20:19:47 +0000180
Dan Gohmand4c45432010-09-01 14:20:41 +0000181 if (Out.os().has_error()) {
Chris Lattnerb515d752009-08-23 07:49:08 +0000182 errMsg = "could not write bitcode file: ";
183 errMsg += path;
Dan Gohmand4c45432010-09-01 14:20:41 +0000184 Out.os().clear_error();
Shuxin Yang235089b2013-08-07 05:19:23 +0000185 return false;
Chris Lattnerb515d752009-08-23 07:49:08 +0000186 }
Bill Wendling76b13ed2012-03-31 10:50:14 +0000187
Dan Gohmanf2914012010-08-20 16:59:15 +0000188 Out.keep();
Shuxin Yang235089b2013-08-07 05:19:23 +0000189 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000190}
191
Bill Wendlingc94c5622012-03-31 11:15:43 +0000192bool LTOCodeGenerator::compile_to_file(const char** name, std::string& errMsg) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000193 // make unique temp .o file to put generated object file
Rafael Espindola68c0efa2013-06-17 18:05:35 +0000194 SmallString<128> Filename;
195 int FD;
Rafael Espindola1276b392013-07-05 20:14:52 +0000196 error_code EC = sys::fs::createTemporaryFile("lto-llvm", "o", FD, Filename);
Rafael Espindola68c0efa2013-06-17 18:05:35 +0000197 if (EC) {
198 errMsg = EC.message();
Shuxin Yang235089b2013-08-07 05:19:23 +0000199 return false;
Rafael Espindola6421a882011-03-22 20:57:13 +0000200 }
Rafael Espindola6421a882011-03-22 20:57:13 +0000201
202 // generate object file
Rafael Espindola68c0efa2013-06-17 18:05:35 +0000203 tool_output_file objFile(Filename.c_str(), FD);
Bill Wendlingc94c5622012-03-31 11:15:43 +0000204
Rafael Espindola68c0efa2013-06-17 18:05:35 +0000205 bool genResult = generateObjectFile(objFile.os(), errMsg);
Rafael Espindola6421a882011-03-22 20:57:13 +0000206 objFile.os().close();
207 if (objFile.os().has_error()) {
208 objFile.os().clear_error();
Rafael Espindola68c0efa2013-06-17 18:05:35 +0000209 sys::fs::remove(Twine(Filename));
Shuxin Yang235089b2013-08-07 05:19:23 +0000210 return false;
Rafael Espindola6421a882011-03-22 20:57:13 +0000211 }
Bill Wendlingc94c5622012-03-31 11:15:43 +0000212
Rafael Espindola6421a882011-03-22 20:57:13 +0000213 objFile.keep();
Shuxin Yang08809392013-08-06 21:51:21 +0000214 if (!genResult) {
Rafael Espindola68c0efa2013-06-17 18:05:35 +0000215 sys::fs::remove(Twine(Filename));
Shuxin Yang235089b2013-08-07 05:19:23 +0000216 return false;
Rafael Espindola6421a882011-03-22 20:57:13 +0000217 }
218
Rafael Espindola68c0efa2013-06-17 18:05:35 +0000219 _nativeObjectPath = Filename.c_str();
Rafael Espindola6421a882011-03-22 20:57:13 +0000220 *name = _nativeObjectPath.c_str();
Shuxin Yang235089b2013-08-07 05:19:23 +0000221 return true;
Rafael Espindola6421a882011-03-22 20:57:13 +0000222}
223
Bill Wendlingc94c5622012-03-31 11:15:43 +0000224const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000225 const char *name;
Shuxin Yang235089b2013-08-07 05:19:23 +0000226 if (!compile_to_file(&name, errMsg))
Rafael Espindola6421a882011-03-22 20:57:13 +0000227 return NULL;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000228
Rafael Espindola6421a882011-03-22 20:57:13 +0000229 // remove old buffer if compile() called twice
230 delete _nativeObjectFile;
Nick Kledzikef194ed2008-02-27 22:25:36 +0000231
Rafael Espindola6421a882011-03-22 20:57:13 +0000232 // read .o file into memory buffer
233 OwningPtr<MemoryBuffer> BuffPtr;
234 if (error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) {
235 errMsg = ec.message();
Rafael Espindola68c0efa2013-06-17 18:05:35 +0000236 sys::fs::remove(_nativeObjectPath);
Rafael Espindola6421a882011-03-22 20:57:13 +0000237 return NULL;
238 }
239 _nativeObjectFile = BuffPtr.take();
Rafael Espindolae9efea12011-02-24 21:04:06 +0000240
Rafael Espindola6421a882011-03-22 20:57:13 +0000241 // remove temp files
Rafael Espindola68c0efa2013-06-17 18:05:35 +0000242 sys::fs::remove(_nativeObjectPath);
Rafael Espindolae9efea12011-02-24 21:04:06 +0000243
Rafael Espindola6421a882011-03-22 20:57:13 +0000244 // return buffer, unless error
Bill Wendling0ca36af2012-08-08 22:03:50 +0000245 if (_nativeObjectFile == NULL)
Rafael Espindola6421a882011-03-22 20:57:13 +0000246 return NULL;
247 *length = _nativeObjectFile->getBufferSize();
248 return _nativeObjectFile->getBufferStart();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000249}
250
Bill Wendling8c18a6f2013-05-23 21:21:50 +0000251bool LTOCodeGenerator::determineTarget(std::string &errMsg) {
Bill Wendling0ca36af2012-08-08 22:03:50 +0000252 if (_target != NULL)
Shuxin Yang08809392013-08-06 21:51:21 +0000253 return true;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000254
Bill Wendling8c18a6f2013-05-23 21:21:50 +0000255 // if options were requested, set them
256 if (!_codegenOptions.empty())
257 cl::ParseCommandLineOptions(_codegenOptions.size(),
258 const_cast<char **>(&_codegenOptions[0]));
259
Bob Wilson47ed8a12012-10-12 17:39:25 +0000260 std::string TripleStr = _linker.getModule()->getTargetTriple();
261 if (TripleStr.empty())
262 TripleStr = sys::getDefaultTargetTriple();
263 llvm::Triple Triple(TripleStr);
Bill Wendling604a8182008-06-18 06:35:30 +0000264
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000265 // create target machine from info for merged modules
Bob Wilson47ed8a12012-10-12 17:39:25 +0000266 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
Bill Wendling0ca36af2012-08-08 22:03:50 +0000267 if (march == NULL)
Shuxin Yang08809392013-08-06 21:51:21 +0000268 return false;
Bill Wendlingc94c5622012-03-31 11:15:43 +0000269
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000270 // The relocation model is actually a static member of TargetMachine and
271 // needs to be set before the TargetMachine is instantiated.
272 Reloc::Model RelocModel = Reloc::Default;
Bill Wendling0ca36af2012-08-08 22:03:50 +0000273 switch (_codeModel) {
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000274 case LTO_CODEGEN_PIC_MODEL_STATIC:
275 RelocModel = Reloc::Static;
276 break;
277 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
278 RelocModel = Reloc::PIC_;
279 break;
280 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
281 RelocModel = Reloc::DynamicNoPIC;
282 break;
Bill Wendlingc94c5622012-03-31 11:15:43 +0000283 }
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000284
285 // construct LTOModule, hand over ownership of module and target
286 SubtargetFeatures Features;
Bob Wilson47ed8a12012-10-12 17:39:25 +0000287 Features.getDefaultSubtargetFeatures(Triple);
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000288 std::string FeatureStr = Features.getString();
Bob Wilson47ed8a12012-10-12 17:39:25 +0000289 // Set a default CPU for Darwin triples.
290 if (_mCpu.empty() && Triple.isOSDarwin()) {
291 if (Triple.getArch() == llvm::Triple::x86_64)
292 _mCpu = "core2";
293 else if (Triple.getArch() == llvm::Triple::x86)
294 _mCpu = "yonah";
295 }
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000296 TargetOptions Options;
297 LTOModule::getTargetOptions(Options);
Bob Wilson47ed8a12012-10-12 17:39:25 +0000298 _target = march->createTargetMachine(TripleStr, _mCpu, FeatureStr, Options,
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000299 RelocModel, CodeModel::Default,
300 CodeGenOpt::Aggressive);
Shuxin Yang08809392013-08-06 21:51:21 +0000301 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000302}
303
Bill Wendlingc94c5622012-03-31 11:15:43 +0000304void LTOCodeGenerator::
305applyRestriction(GlobalValue &GV,
306 std::vector<const char*> &mustPreserveList,
307 SmallPtrSet<GlobalValue*, 8> &asmUsed,
308 Mangler &mangler) {
Rafael Espindola38c4e532011-03-02 04:14:42 +0000309 SmallString<64> Buffer;
310 mangler.getNameWithPrefix(Buffer, &GV, false);
311
312 if (GV.isDeclaration())
313 return;
314 if (_mustPreserveSymbols.count(Buffer))
315 mustPreserveList.push_back(GV.getName().data());
316 if (_asmUndefinedRefs.count(Buffer))
317 asmUsed.insert(&GV);
318}
319
320static void findUsedValues(GlobalVariable *LLVMUsed,
321 SmallPtrSet<GlobalValue*, 8> &UsedValues) {
322 if (LLVMUsed == 0) return;
323
Rafael Espindolad4ee3922013-04-24 17:54:35 +0000324 ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
Rafael Espindola38c4e532011-03-02 04:14:42 +0000325 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
Bill Wendling76b13ed2012-03-31 10:50:14 +0000326 if (GlobalValue *GV =
Bill Wendlingc94c5622012-03-31 11:15:43 +0000327 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
Rafael Espindola38c4e532011-03-02 04:14:42 +0000328 UsedValues.insert(GV);
329}
330
Chris Lattner5ef31a02010-03-12 18:44:54 +0000331void LTOCodeGenerator::applyScopeRestrictions() {
332 if (_scopeRestrictionsDone) return;
333 Module *mergedModule = _linker.getModule();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000334
Chris Lattner5ef31a02010-03-12 18:44:54 +0000335 // Start off with a verification pass.
336 PassManager passes;
337 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000338
Bill Wendling76b13ed2012-03-31 10:50:14 +0000339 // mark which symbols can not be internalized
Bill Wendling99cb6222013-06-18 07:20:20 +0000340 MCContext Context(_target->getMCAsmInfo(), _target->getRegisterInfo(), NULL);
Bill Wendlingcc5a8822013-05-29 20:37:19 +0000341 Mangler mangler(Context, _target);
Rafael Espindola38c4e532011-03-02 04:14:42 +0000342 std::vector<const char*> mustPreserveList;
343 SmallPtrSet<GlobalValue*, 8> asmUsed;
344
345 for (Module::iterator f = mergedModule->begin(),
346 e = mergedModule->end(); f != e; ++f)
347 applyRestriction(*f, mustPreserveList, asmUsed, mangler);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000348 for (Module::global_iterator v = mergedModule->global_begin(),
Rafael Espindola38c4e532011-03-02 04:14:42 +0000349 e = mergedModule->global_end(); v != e; ++v)
350 applyRestriction(*v, mustPreserveList, asmUsed, mangler);
351 for (Module::alias_iterator a = mergedModule->alias_begin(),
352 e = mergedModule->alias_end(); a != e; ++a)
353 applyRestriction(*a, mustPreserveList, asmUsed, mangler);
354
355 GlobalVariable *LLVMCompilerUsed =
356 mergedModule->getGlobalVariable("llvm.compiler.used");
357 findUsedValues(LLVMCompilerUsed, asmUsed);
358 if (LLVMCompilerUsed)
359 LLVMCompilerUsed->eraseFromParent();
360
Rafael Espindolad4ee3922013-04-24 17:54:35 +0000361 if (!asmUsed.empty()) {
362 llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(_context);
363 std::vector<Constant*> asmUsed2;
364 for (SmallPtrSet<GlobalValue*, 16>::const_iterator i = asmUsed.begin(),
365 e = asmUsed.end(); i !=e; ++i) {
366 GlobalValue *GV = *i;
367 Constant *c = ConstantExpr::getBitCast(GV, i8PTy);
368 asmUsed2.push_back(c);
369 }
370
371 llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size());
372 LLVMCompilerUsed =
373 new llvm::GlobalVariable(*mergedModule, ATy, false,
374 llvm::GlobalValue::AppendingLinkage,
375 llvm::ConstantArray::get(ATy, asmUsed2),
376 "llvm.compiler.used");
377
378 LLVMCompilerUsed->setSection("llvm.metadata");
Chris Lattner5ef31a02010-03-12 18:44:54 +0000379 }
Rafael Espindola38c4e532011-03-02 04:14:42 +0000380
Bill Wendling50f31832012-12-10 21:33:45 +0000381 passes.add(createInternalizePass(mustPreserveList));
Rafael Espindola38c4e532011-03-02 04:14:42 +0000382
Chris Lattner5ef31a02010-03-12 18:44:54 +0000383 // apply scope restrictions
384 passes.run(*mergedModule);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000385
Chris Lattner5ef31a02010-03-12 18:44:54 +0000386 _scopeRestrictionsDone = true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000387}
388
Nick Kledzik77595fc2008-02-26 20:26:43 +0000389/// Optimize merged modules using various IPO passes
Chris Lattner817a01f2011-05-22 00:20:07 +0000390bool LTOCodeGenerator::generateObjectFile(raw_ostream &out,
391 std::string &errMsg) {
Shuxin Yang08809392013-08-06 21:51:21 +0000392 if (!this->determineTarget(errMsg))
393 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000394
Bill Wendlingc94c5622012-03-31 11:15:43 +0000395 Module* mergedModule = _linker.getModule();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000396
Bill Wendling8c18a6f2013-05-23 21:21:50 +0000397 // Mark which symbols can not be internalized
Bill Wendling64d5b282012-04-09 22:18:01 +0000398 this->applyScopeRestrictions();
399
Bill Wendlingc94c5622012-03-31 11:15:43 +0000400 // Instantiate the pass manager to organize the passes.
401 PassManager passes;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000402
Bill Wendlingc94c5622012-03-31 11:15:43 +0000403 // Start off with a verification pass.
404 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000405
Micah Villmow791cfc22012-10-08 16:39:34 +0000406 // Add an appropriate DataLayout instance for this module...
407 passes.add(new DataLayout(*_target->getDataLayout()));
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000408 _target->addAnalysisPasses(passes);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000409
Rafael Espindola4d2e9d92012-04-16 10:58:38 +0000410 // Enabling internalize here would use its AllButMain variant. It
411 // keeps only main if it exists and does nothing for libraries. Instead
412 // we create the pass ourselves with the symbol list provided by the linker.
Bill Wendling8c18a6f2013-05-23 21:21:50 +0000413 if (!DisableOpt)
Bill Wendling6303b662013-02-28 14:11:10 +0000414 PassManagerBuilder().populateLTOPassManager(passes,
Bill Wendling50f31832012-12-10 21:33:45 +0000415 /*Internalize=*/false,
Bill Wendling3197b442012-04-02 22:16:50 +0000416 !DisableInline,
417 DisableGVNLoadPRE);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000418
Bill Wendlingc94c5622012-03-31 11:15:43 +0000419 // Make sure everything is still good.
420 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000421
Lang Hamesa991b252013-03-13 21:18:46 +0000422 PassManager codeGenPasses;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000423
Lang Hamesa991b252013-03-13 21:18:46 +0000424 codeGenPasses.add(new DataLayout(*_target->getDataLayout()));
425 _target->addAnalysisPasses(codeGenPasses);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000426
Bill Wendlingc94c5622012-03-31 11:15:43 +0000427 formatted_raw_ostream Out(out);
Dan Gohmand4c45432010-09-01 14:20:41 +0000428
Bob Wilsond6965442013-03-29 23:28:55 +0000429 // If the bitcode files contain ARC code and were compiled with optimization,
430 // the ObjCARCContractPass must be run, so do it unconditionally here.
431 codeGenPasses.add(createObjCARCContractPass());
432
Lang Hamesa991b252013-03-13 21:18:46 +0000433 if (_target->addPassesToEmitFile(codeGenPasses, Out,
David Blaikie4f56a302012-05-30 18:42:51 +0000434 TargetMachine::CGFT_ObjectFile)) {
Bill Wendlingc94c5622012-03-31 11:15:43 +0000435 errMsg = "target file type not supported";
Shuxin Yang08809392013-08-06 21:51:21 +0000436 return false;
Bill Wendlingc94c5622012-03-31 11:15:43 +0000437 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000438
Bill Wendlingc94c5622012-03-31 11:15:43 +0000439 // Run our queue of passes all at once now, efficiently.
440 passes.run(*mergedModule);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000441
Bill Wendlingc94c5622012-03-31 11:15:43 +0000442 // Run the code generator, and write assembly file
Lang Hamesa991b252013-03-13 21:18:46 +0000443 codeGenPasses.run(*mergedModule);
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000444
Shuxin Yang08809392013-08-06 21:51:21 +0000445 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000446}
447
Bill Wendlingcaf71d42012-03-31 10:49:43 +0000448/// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
449/// LTO problems.
450void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) {
451 for (std::pair<StringRef, StringRef> o = getToken(options);
452 !o.first.empty(); o = getToken(o.second)) {
453 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add
454 // that.
Bill Wendling0ca36af2012-08-08 22:03:50 +0000455 if (_codegenOptions.empty())
Bill Wendlingcaf71d42012-03-31 10:49:43 +0000456 _codegenOptions.push_back(strdup("libLTO"));
457 _codegenOptions.push_back(strdup(o.first.str().c_str()));
458 }
Nick Kledzik920ae982008-07-08 21:14:10 +0000459}