blob: 22e9754bcfa80460cf44a91d4208798a44593928 [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
Peter Collingbournecc488542013-09-24 23:52:22 +000015#include "llvm/LTO/LTOCodeGenerator.h"
16#include "llvm/LTO/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/TargetRegisterInfo.h"
Peter Collingbournecc488542013-09-24 23:52:22 +000044#include "llvm/Target/Mangler.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000045#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"
Shuxin Yang67d135a2013-08-12 18:29:43 +000048using namespace llvm;
Shuxin Yang67d135a2013-08-12 18:29:43 +000049
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()
Rafael Espindola66efc632013-09-04 17:44:24 +000071 : Context(getGlobalContext()), Linker(new Module("ld-temp.o", Context)),
72 TargetMach(NULL), EmitDwarfDebugInfo(false), ScopeRestrictionsDone(false),
73 CodeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC), NativeObjectFile(NULL) {
Shuxin Yang8945f752013-07-22 18:40:34 +000074 initializeLTOPasses();
Nick Kledzik77595fc2008-02-26 20:26:43 +000075}
76
Bill Wendlingcaf71d42012-03-31 10:49:43 +000077LTOCodeGenerator::~LTOCodeGenerator() {
Rafael Espindola66efc632013-09-04 17:44:24 +000078 delete TargetMach;
79 delete NativeObjectFile;
80 delete Linker.getModule();
Bill Wendlingcaf71d42012-03-31 10:49:43 +000081
Rafael Espindola66efc632013-09-04 17:44:24 +000082 for (std::vector<char *>::iterator I = CodegenOptions.begin(),
83 E = CodegenOptions.end();
84 I != E; ++I)
Bill Wendlingcaf71d42012-03-31 10:49:43 +000085 free(*I);
Nick Kledzik77595fc2008-02-26 20:26:43 +000086}
87
Shuxin Yang8945f752013-07-22 18:40:34 +000088// Initialize LTO passes. Please keep this funciton in sync with
Shuxin Yangc679d6b2013-07-23 06:44:34 +000089// PassManagerBuilder::populateLTOPassManager(), and make sure all LTO
Shuxin Yang8945f752013-07-22 18:40:34 +000090// passes are initialized.
91//
92void LTOCodeGenerator::initializeLTOPasses() {
93 PassRegistry &R = *PassRegistry::getPassRegistry();
94
95 initializeInternalizePassPass(R);
96 initializeIPSCCPPass(R);
97 initializeGlobalOptPass(R);
98 initializeConstantMergePass(R);
99 initializeDAHPass(R);
100 initializeInstCombinerPass(R);
101 initializeSimpleInlinerPass(R);
102 initializePruneEHPass(R);
103 initializeGlobalDCEPass(R);
104 initializeArgPromotionPass(R);
105 initializeJumpThreadingPass(R);
106 initializeSROAPass(R);
107 initializeSROA_DTPass(R);
108 initializeSROA_SSAUpPass(R);
109 initializeFunctionAttrsPass(R);
110 initializeGlobalsModRefPass(R);
111 initializeLICMPass(R);
112 initializeGVNPass(R);
113 initializeMemCpyOptPass(R);
114 initializeDCEPass(R);
Tom Stellard01d72032013-08-06 02:43:45 +0000115 initializeCFGSimplifyPassPass(R);
Shuxin Yang8945f752013-07-22 18:40:34 +0000116}
117
Bill Wendlingc94c5622012-03-31 11:15:43 +0000118bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) {
Rafael Espindola66efc632013-09-04 17:44:24 +0000119 bool ret = Linker.linkInModule(mod->getLLVVMModule(), &errMsg);
Rafael Espindola38c4e532011-03-02 04:14:42 +0000120
121 const std::vector<const char*> &undefs = mod->getAsmUndefinedRefs();
122 for (int i = 0, e = undefs.size(); i != e; ++i)
Rafael Espindola66efc632013-09-04 17:44:24 +0000123 AsmUndefinedRefs[undefs[i]] = 1;
Rafael Espindola38c4e532011-03-02 04:14:42 +0000124
Shuxin Yang235089b2013-08-07 05:19:23 +0000125 return !ret;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000126}
Bill Wendling76b13ed2012-03-31 10:50:14 +0000127
Shuxin Yang235089b2013-08-07 05:19:23 +0000128void LTOCodeGenerator::setDebugInfo(lto_debug_model debug) {
Bill Wendlingc94c5622012-03-31 11:15:43 +0000129 switch (debug) {
130 case LTO_DEBUG_MODEL_NONE:
Rafael Espindola66efc632013-09-04 17:44:24 +0000131 EmitDwarfDebugInfo = false;
Shuxin Yang235089b2013-08-07 05:19:23 +0000132 return;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000133
Bill Wendlingc94c5622012-03-31 11:15:43 +0000134 case LTO_DEBUG_MODEL_DWARF:
Rafael Espindola66efc632013-09-04 17:44:24 +0000135 EmitDwarfDebugInfo = true;
Shuxin Yang235089b2013-08-07 05:19:23 +0000136 return;
Bill Wendlingc94c5622012-03-31 11:15:43 +0000137 }
138 llvm_unreachable("Unknown debug format!");
Nick Kledzik77595fc2008-02-26 20:26:43 +0000139}
140
Shuxin Yang235089b2013-08-07 05:19:23 +0000141void LTOCodeGenerator::setCodePICModel(lto_codegen_model model) {
Bill Wendlingc94c5622012-03-31 11:15:43 +0000142 switch (model) {
143 case LTO_CODEGEN_PIC_MODEL_STATIC:
144 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
145 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
Rafael Espindola66efc632013-09-04 17:44:24 +0000146 CodeModel = model;
Shuxin Yang235089b2013-08-07 05:19:23 +0000147 return;
Bill Wendlingc94c5622012-03-31 11:15:43 +0000148 }
149 llvm_unreachable("Unknown PIC model!");
Nick Kledzik77595fc2008-02-26 20:26:43 +0000150}
151
Chris Lattnerb515d752009-08-23 07:49:08 +0000152bool LTOCodeGenerator::writeMergedModules(const char *path,
153 std::string &errMsg) {
Shuxin Yang08809392013-08-06 21:51:21 +0000154 if (!determineTarget(errMsg))
Shuxin Yang235089b2013-08-07 05:19:23 +0000155 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000156
Bill Wendling6ba2ed52013-08-08 23:51:04 +0000157 // mark which symbols can not be internalized
158 applyScopeRestrictions();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000159
Chris Lattnerb515d752009-08-23 07:49:08 +0000160 // create output file
161 std::string ErrInfo;
Rafael Espindolac1b49b52013-07-16 19:44:17 +0000162 tool_output_file Out(path, ErrInfo, sys::fs::F_Binary);
Chris Lattnerb515d752009-08-23 07:49:08 +0000163 if (!ErrInfo.empty()) {
164 errMsg = "could not open bitcode file for writing: ";
165 errMsg += path;
Shuxin Yang235089b2013-08-07 05:19:23 +0000166 return false;
Chris Lattnerb515d752009-08-23 07:49:08 +0000167 }
Bill Wendling76b13ed2012-03-31 10:50:14 +0000168
Chris Lattnerb515d752009-08-23 07:49:08 +0000169 // write bitcode to it
Rafael Espindola66efc632013-09-04 17:44:24 +0000170 WriteBitcodeToFile(Linker.getModule(), Out.os());
Dan Gohmand4c45432010-09-01 14:20:41 +0000171 Out.os().close();
Dan Gohman4b7416b2010-05-27 20:19:47 +0000172
Dan Gohmand4c45432010-09-01 14:20:41 +0000173 if (Out.os().has_error()) {
Chris Lattnerb515d752009-08-23 07:49:08 +0000174 errMsg = "could not write bitcode file: ";
175 errMsg += path;
Dan Gohmand4c45432010-09-01 14:20:41 +0000176 Out.os().clear_error();
Shuxin Yang235089b2013-08-07 05:19:23 +0000177 return false;
Chris Lattnerb515d752009-08-23 07:49:08 +0000178 }
Bill Wendling76b13ed2012-03-31 10:50:14 +0000179
Dan Gohmanf2914012010-08-20 16:59:15 +0000180 Out.keep();
Shuxin Yang235089b2013-08-07 05:19:23 +0000181 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000182}
183
Shuxin Yangcfaa6362013-08-12 21:07:31 +0000184bool LTOCodeGenerator::compile_to_file(const char** name, std::string& errMsg) {
185 // make unique temp .o file to put generated object file
186 SmallString<128> Filename;
187 int FD;
188 error_code EC = sys::fs::createTemporaryFile("lto-llvm", "o", FD, Filename);
189 if (EC) {
190 errMsg = EC.message();
Shuxin Yang235089b2013-08-07 05:19:23 +0000191 return false;
Shuxin Yangcfaa6362013-08-12 21:07:31 +0000192 }
Rafael Espindola6421a882011-03-22 20:57:13 +0000193
Shuxin Yangcfaa6362013-08-12 21:07:31 +0000194 // generate object file
195 tool_output_file objFile(Filename.c_str(), FD);
Bill Wendlingc94c5622012-03-31 11:15:43 +0000196
Shuxin Yangcfaa6362013-08-12 21:07:31 +0000197 bool genResult = generateObjectFile(objFile.os(), errMsg);
198 objFile.os().close();
199 if (objFile.os().has_error()) {
200 objFile.os().clear_error();
201 sys::fs::remove(Twine(Filename));
Shuxin Yang235089b2013-08-07 05:19:23 +0000202 return false;
Shuxin Yangcfaa6362013-08-12 21:07:31 +0000203 }
Bill Wendlingc94c5622012-03-31 11:15:43 +0000204
Shuxin Yangcfaa6362013-08-12 21:07:31 +0000205 objFile.keep();
206 if (!genResult) {
207 sys::fs::remove(Twine(Filename));
Shuxin Yang235089b2013-08-07 05:19:23 +0000208 return false;
Shuxin Yangcfaa6362013-08-12 21:07:31 +0000209 }
Rafael Espindola6421a882011-03-22 20:57:13 +0000210
Rafael Espindola66efc632013-09-04 17:44:24 +0000211 NativeObjectPath = Filename.c_str();
212 *name = NativeObjectPath.c_str();
Shuxin Yang235089b2013-08-07 05:19:23 +0000213 return true;
Rafael Espindola6421a882011-03-22 20:57:13 +0000214}
215
Bill Wendlingc94c5622012-03-31 11:15:43 +0000216const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000217 const char *name;
Shuxin Yang235089b2013-08-07 05:19:23 +0000218 if (!compile_to_file(&name, errMsg))
Rafael Espindola6421a882011-03-22 20:57:13 +0000219 return NULL;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000220
Rafael Espindola6421a882011-03-22 20:57:13 +0000221 // remove old buffer if compile() called twice
Rafael Espindola66efc632013-09-04 17:44:24 +0000222 delete NativeObjectFile;
Nick Kledzikef194ed2008-02-27 22:25:36 +0000223
Rafael Espindola6421a882011-03-22 20:57:13 +0000224 // read .o file into memory buffer
225 OwningPtr<MemoryBuffer> BuffPtr;
226 if (error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) {
227 errMsg = ec.message();
Rafael Espindola66efc632013-09-04 17:44:24 +0000228 sys::fs::remove(NativeObjectPath);
Shuxin Yangcfaa6362013-08-12 21:07:31 +0000229 return NULL;
Rafael Espindola6421a882011-03-22 20:57:13 +0000230 }
Rafael Espindola66efc632013-09-04 17:44:24 +0000231 NativeObjectFile = BuffPtr.take();
Rafael Espindolae9efea12011-02-24 21:04:06 +0000232
Shuxin Yangcfaa6362013-08-12 21:07:31 +0000233 // remove temp files
Rafael Espindola66efc632013-09-04 17:44:24 +0000234 sys::fs::remove(NativeObjectPath);
Rafael Espindolae9efea12011-02-24 21:04:06 +0000235
Shuxin Yangcfaa6362013-08-12 21:07:31 +0000236 // return buffer, unless error
Rafael Espindola66efc632013-09-04 17:44:24 +0000237 if (NativeObjectFile == NULL)
Shuxin Yangcfaa6362013-08-12 21:07:31 +0000238 return NULL;
Rafael Espindola66efc632013-09-04 17:44:24 +0000239 *length = NativeObjectFile->getBufferSize();
240 return NativeObjectFile->getBufferStart();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000241}
242
Bill Wendling8c18a6f2013-05-23 21:21:50 +0000243bool LTOCodeGenerator::determineTarget(std::string &errMsg) {
Rafael Espindola66efc632013-09-04 17:44:24 +0000244 if (TargetMach != NULL)
Shuxin Yang08809392013-08-06 21:51:21 +0000245 return true;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000246
Bill Wendling8c18a6f2013-05-23 21:21:50 +0000247 // if options were requested, set them
Rafael Espindola66efc632013-09-04 17:44:24 +0000248 if (!CodegenOptions.empty())
249 cl::ParseCommandLineOptions(CodegenOptions.size(),
250 const_cast<char **>(&CodegenOptions[0]));
Bill Wendling8c18a6f2013-05-23 21:21:50 +0000251
Rafael Espindola66efc632013-09-04 17:44:24 +0000252 std::string TripleStr = Linker.getModule()->getTargetTriple();
Bob Wilson47ed8a12012-10-12 17:39:25 +0000253 if (TripleStr.empty())
254 TripleStr = sys::getDefaultTargetTriple();
255 llvm::Triple Triple(TripleStr);
Bill Wendling604a8182008-06-18 06:35:30 +0000256
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000257 // create target machine from info for merged modules
Bob Wilson47ed8a12012-10-12 17:39:25 +0000258 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
Bill Wendling0ca36af2012-08-08 22:03:50 +0000259 if (march == NULL)
Shuxin Yang08809392013-08-06 21:51:21 +0000260 return false;
Bill Wendlingc94c5622012-03-31 11:15:43 +0000261
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000262 // The relocation model is actually a static member of TargetMachine and
263 // needs to be set before the TargetMachine is instantiated.
264 Reloc::Model RelocModel = Reloc::Default;
Rafael Espindola66efc632013-09-04 17:44:24 +0000265 switch (CodeModel) {
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000266 case LTO_CODEGEN_PIC_MODEL_STATIC:
267 RelocModel = Reloc::Static;
268 break;
269 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
270 RelocModel = Reloc::PIC_;
271 break;
272 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
273 RelocModel = Reloc::DynamicNoPIC;
274 break;
Bill Wendlingc94c5622012-03-31 11:15:43 +0000275 }
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000276
277 // construct LTOModule, hand over ownership of module and target
278 SubtargetFeatures Features;
Bob Wilson47ed8a12012-10-12 17:39:25 +0000279 Features.getDefaultSubtargetFeatures(Triple);
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000280 std::string FeatureStr = Features.getString();
Bob Wilson47ed8a12012-10-12 17:39:25 +0000281 // Set a default CPU for Darwin triples.
Rafael Espindola66efc632013-09-04 17:44:24 +0000282 if (MCpu.empty() && Triple.isOSDarwin()) {
Bob Wilson47ed8a12012-10-12 17:39:25 +0000283 if (Triple.getArch() == llvm::Triple::x86_64)
Rafael Espindola66efc632013-09-04 17:44:24 +0000284 MCpu = "core2";
Bob Wilson47ed8a12012-10-12 17:39:25 +0000285 else if (Triple.getArch() == llvm::Triple::x86)
Rafael Espindola66efc632013-09-04 17:44:24 +0000286 MCpu = "yonah";
Bob Wilson47ed8a12012-10-12 17:39:25 +0000287 }
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000288 TargetOptions Options;
289 LTOModule::getTargetOptions(Options);
Rafael Espindola66efc632013-09-04 17:44:24 +0000290 TargetMach = march->createTargetMachine(TripleStr, MCpu, FeatureStr, Options,
291 RelocModel, CodeModel::Default,
292 CodeGenOpt::Aggressive);
Shuxin Yang08809392013-08-06 21:51:21 +0000293 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000294}
295
Bill Wendlingc94c5622012-03-31 11:15:43 +0000296void LTOCodeGenerator::
297applyRestriction(GlobalValue &GV,
Rafael Espindola775079c2013-09-04 20:08:46 +0000298 std::vector<const char*> &MustPreserveList,
299 SmallPtrSet<GlobalValue*, 8> &AsmUsed,
300 Mangler &Mangler) {
Rafael Espindola38c4e532011-03-02 04:14:42 +0000301 SmallString<64> Buffer;
Rafael Espindola775079c2013-09-04 20:08:46 +0000302 Mangler.getNameWithPrefix(Buffer, &GV, false);
Rafael Espindola38c4e532011-03-02 04:14:42 +0000303
304 if (GV.isDeclaration())
305 return;
Rafael Espindola66efc632013-09-04 17:44:24 +0000306 if (MustPreserveSymbols.count(Buffer))
Rafael Espindola775079c2013-09-04 20:08:46 +0000307 MustPreserveList.push_back(GV.getName().data());
Rafael Espindola66efc632013-09-04 17:44:24 +0000308 if (AsmUndefinedRefs.count(Buffer))
Rafael Espindola775079c2013-09-04 20:08:46 +0000309 AsmUsed.insert(&GV);
Rafael Espindola38c4e532011-03-02 04:14:42 +0000310}
311
312static void findUsedValues(GlobalVariable *LLVMUsed,
313 SmallPtrSet<GlobalValue*, 8> &UsedValues) {
314 if (LLVMUsed == 0) return;
315
Rafael Espindolad4ee3922013-04-24 17:54:35 +0000316 ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
Rafael Espindola38c4e532011-03-02 04:14:42 +0000317 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
Bill Wendling76b13ed2012-03-31 10:50:14 +0000318 if (GlobalValue *GV =
Bill Wendlingc94c5622012-03-31 11:15:43 +0000319 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
Rafael Espindola38c4e532011-03-02 04:14:42 +0000320 UsedValues.insert(GV);
321}
322
Chris Lattner5ef31a02010-03-12 18:44:54 +0000323void LTOCodeGenerator::applyScopeRestrictions() {
Rafael Espindola66efc632013-09-04 17:44:24 +0000324 if (ScopeRestrictionsDone)
325 return;
326 Module *mergedModule = Linker.getModule();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000327
Chris Lattner5ef31a02010-03-12 18:44:54 +0000328 // Start off with a verification pass.
329 PassManager passes;
330 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000331
Bill Wendling76b13ed2012-03-31 10:50:14 +0000332 // mark which symbols can not be internalized
Rafael Espindola66efc632013-09-04 17:44:24 +0000333 MCContext MContext(TargetMach->getMCAsmInfo(), TargetMach->getRegisterInfo(),
334 NULL);
Rafael Espindola775079c2013-09-04 20:08:46 +0000335 Mangler Mangler(MContext, TargetMach);
336 std::vector<const char*> MustPreserveList;
337 SmallPtrSet<GlobalValue*, 8> AsmUsed;
Rafael Espindola38c4e532011-03-02 04:14:42 +0000338
339 for (Module::iterator f = mergedModule->begin(),
340 e = mergedModule->end(); f != e; ++f)
Rafael Espindola775079c2013-09-04 20:08:46 +0000341 applyRestriction(*f, MustPreserveList, AsmUsed, Mangler);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000342 for (Module::global_iterator v = mergedModule->global_begin(),
Rafael Espindola38c4e532011-03-02 04:14:42 +0000343 e = mergedModule->global_end(); v != e; ++v)
Rafael Espindola775079c2013-09-04 20:08:46 +0000344 applyRestriction(*v, MustPreserveList, AsmUsed, Mangler);
Rafael Espindola38c4e532011-03-02 04:14:42 +0000345 for (Module::alias_iterator a = mergedModule->alias_begin(),
346 e = mergedModule->alias_end(); a != e; ++a)
Rafael Espindola775079c2013-09-04 20:08:46 +0000347 applyRestriction(*a, MustPreserveList, AsmUsed, Mangler);
Rafael Espindola38c4e532011-03-02 04:14:42 +0000348
349 GlobalVariable *LLVMCompilerUsed =
350 mergedModule->getGlobalVariable("llvm.compiler.used");
Rafael Espindola775079c2013-09-04 20:08:46 +0000351 findUsedValues(LLVMCompilerUsed, AsmUsed);
Rafael Espindola38c4e532011-03-02 04:14:42 +0000352 if (LLVMCompilerUsed)
353 LLVMCompilerUsed->eraseFromParent();
354
Rafael Espindola775079c2013-09-04 20:08:46 +0000355 if (!AsmUsed.empty()) {
Rafael Espindola66efc632013-09-04 17:44:24 +0000356 llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(Context);
Rafael Espindolad4ee3922013-04-24 17:54:35 +0000357 std::vector<Constant*> asmUsed2;
Rafael Espindola775079c2013-09-04 20:08:46 +0000358 for (SmallPtrSet<GlobalValue*, 16>::const_iterator i = AsmUsed.begin(),
359 e = AsmUsed.end(); i !=e; ++i) {
Rafael Espindolad4ee3922013-04-24 17:54:35 +0000360 GlobalValue *GV = *i;
361 Constant *c = ConstantExpr::getBitCast(GV, i8PTy);
362 asmUsed2.push_back(c);
363 }
364
365 llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size());
366 LLVMCompilerUsed =
367 new llvm::GlobalVariable(*mergedModule, ATy, false,
368 llvm::GlobalValue::AppendingLinkage,
369 llvm::ConstantArray::get(ATy, asmUsed2),
370 "llvm.compiler.used");
371
372 LLVMCompilerUsed->setSection("llvm.metadata");
Chris Lattner5ef31a02010-03-12 18:44:54 +0000373 }
Rafael Espindola38c4e532011-03-02 04:14:42 +0000374
Rafael Espindola775079c2013-09-04 20:08:46 +0000375 passes.add(createInternalizePass(MustPreserveList));
Rafael Espindola38c4e532011-03-02 04:14:42 +0000376
Chris Lattner5ef31a02010-03-12 18:44:54 +0000377 // apply scope restrictions
378 passes.run(*mergedModule);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000379
Rafael Espindola66efc632013-09-04 17:44:24 +0000380 ScopeRestrictionsDone = true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000381}
382
Nick Kledzik77595fc2008-02-26 20:26:43 +0000383/// Optimize merged modules using various IPO passes
Chris Lattner817a01f2011-05-22 00:20:07 +0000384bool LTOCodeGenerator::generateObjectFile(raw_ostream &out,
385 std::string &errMsg) {
Shuxin Yang08809392013-08-06 21:51:21 +0000386 if (!this->determineTarget(errMsg))
387 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000388
Rafael Espindola66efc632013-09-04 17:44:24 +0000389 Module *mergedModule = Linker.getModule();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000390
Bill Wendling8c18a6f2013-05-23 21:21:50 +0000391 // Mark which symbols can not be internalized
Bill Wendling64d5b282012-04-09 22:18:01 +0000392 this->applyScopeRestrictions();
393
Bill Wendlingc94c5622012-03-31 11:15:43 +0000394 // Instantiate the pass manager to organize the passes.
395 PassManager passes;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000396
Bill Wendlingc94c5622012-03-31 11:15:43 +0000397 // Start off with a verification pass.
398 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000399
Micah Villmow791cfc22012-10-08 16:39:34 +0000400 // Add an appropriate DataLayout instance for this module...
Rafael Espindola66efc632013-09-04 17:44:24 +0000401 passes.add(new DataLayout(*TargetMach->getDataLayout()));
402 TargetMach->addAnalysisPasses(passes);
Bill Wendling76b13ed2012-03-31 10:50:14 +0000403
Rafael Espindola4d2e9d92012-04-16 10:58:38 +0000404 // Enabling internalize here would use its AllButMain variant. It
405 // keeps only main if it exists and does nothing for libraries. Instead
406 // we create the pass ourselves with the symbol list provided by the linker.
Bill Wendling8c18a6f2013-05-23 21:21:50 +0000407 if (!DisableOpt)
Bill Wendling6303b662013-02-28 14:11:10 +0000408 PassManagerBuilder().populateLTOPassManager(passes,
Bill Wendling50f31832012-12-10 21:33:45 +0000409 /*Internalize=*/false,
Bill Wendling3197b442012-04-02 22:16:50 +0000410 !DisableInline,
411 DisableGVNLoadPRE);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000412
Bill Wendlingc94c5622012-03-31 11:15:43 +0000413 // Make sure everything is still good.
414 passes.add(createVerifierPass());
Nick Kledzik77595fc2008-02-26 20:26:43 +0000415
Lang Hamesa991b252013-03-13 21:18:46 +0000416 PassManager codeGenPasses;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000417
Rafael Espindola66efc632013-09-04 17:44:24 +0000418 codeGenPasses.add(new DataLayout(*TargetMach->getDataLayout()));
419 TargetMach->addAnalysisPasses(codeGenPasses);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000420
Bill Wendlingc94c5622012-03-31 11:15:43 +0000421 formatted_raw_ostream Out(out);
Dan Gohmand4c45432010-09-01 14:20:41 +0000422
Bob Wilsond6965442013-03-29 23:28:55 +0000423 // If the bitcode files contain ARC code and were compiled with optimization,
424 // the ObjCARCContractPass must be run, so do it unconditionally here.
425 codeGenPasses.add(createObjCARCContractPass());
426
Rafael Espindola66efc632013-09-04 17:44:24 +0000427 if (TargetMach->addPassesToEmitFile(codeGenPasses, Out,
428 TargetMachine::CGFT_ObjectFile)) {
Bill Wendlingc94c5622012-03-31 11:15:43 +0000429 errMsg = "target file type not supported";
Shuxin Yang08809392013-08-06 21:51:21 +0000430 return false;
Bill Wendlingc94c5622012-03-31 11:15:43 +0000431 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000432
Bill Wendlingc94c5622012-03-31 11:15:43 +0000433 // Run our queue of passes all at once now, efficiently.
434 passes.run(*mergedModule);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000435
Bill Wendlingc94c5622012-03-31 11:15:43 +0000436 // Run the code generator, and write assembly file
Lang Hamesa991b252013-03-13 21:18:46 +0000437 codeGenPasses.run(*mergedModule);
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000438
Shuxin Yang08809392013-08-06 21:51:21 +0000439 return true;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000440}
441
Bill Wendlingcaf71d42012-03-31 10:49:43 +0000442/// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
443/// LTO problems.
444void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) {
445 for (std::pair<StringRef, StringRef> o = getToken(options);
446 !o.first.empty(); o = getToken(o.second)) {
447 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add
448 // that.
Rafael Espindola66efc632013-09-04 17:44:24 +0000449 if (CodegenOptions.empty())
Peter Collingbournecc488542013-09-24 23:52:22 +0000450 CodegenOptions.push_back(strdup("libLLVMLTO"));
Rafael Espindola66efc632013-09-04 17:44:24 +0000451 CodegenOptions.push_back(strdup(o.first.str().c_str()));
Bill Wendlingcaf71d42012-03-31 10:49:43 +0000452 }
Nick Kledzik920ae982008-07-08 21:14:10 +0000453}