blob: 18123460774157d259e4e9eb9b9766360d5ebcb4 [file] [log] [blame]
Chris Lattner2eff5052010-03-12 18:44:54 +00001//===-- LTOModule.cpp - LLVM Link Time Optimizer --------------------------===//
Nick Kledzik07b4a622008-02-26 20:26:43 +00002//
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.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +00007//
Nick Kledzik07b4a622008-02-26 20:26:43 +00008//===----------------------------------------------------------------------===//
9//
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000010// This file implements the Link Time Optimization library. This library is
Nick Kledzik07b4a622008-02-26 20:26:43 +000011// intended to be used by linker to optimize code at link time.
12//
13//===----------------------------------------------------------------------===//
14
Nick Kledzik91a6dcf2008-02-27 22:25:36 +000015#include "LTOModule.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000016#include "llvm/ADT/OwningPtr.h"
17#include "llvm/ADT/Triple.h"
18#include "llvm/Bitcode/ReaderWriter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Constants.h"
20#include "llvm/IR/LLVMContext.h"
21#include "llvm/IR/Module.h"
Rafael Espindola1e49a6d2011-03-02 04:14:42 +000022#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCInst.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000024#include "llvm/MC/MCParser/MCAsmParser.h"
Rafael Espindola1e49a6d2011-03-02 04:14:42 +000025#include "llvm/MC/MCStreamer.h"
Evan Cheng91111d22011-07-09 05:47:46 +000026#include "llvm/MC/MCSubtargetInfo.h"
Rafael Espindola1e49a6d2011-03-02 04:14:42 +000027#include "llvm/MC/MCSymbol.h"
Evan Cheng11424442011-07-26 00:24:13 +000028#include "llvm/MC/MCTargetAsmParser.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000029#include "llvm/MC/SubtargetFeature.h"
Bill Wendlingb8dcda72012-08-06 21:34:54 +000030#include "llvm/Support/CommandLine.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000031#include "llvm/Support/Host.h"
Rafael Espindola46ed3532013-06-11 18:05:26 +000032#include "llvm/Support/FileSystem.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000033#include "llvm/Support/MemoryBuffer.h"
34#include "llvm/Support/Path.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000035#include "llvm/Support/SourceMgr.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000036#include "llvm/Support/TargetRegistry.h"
37#include "llvm/Support/TargetSelect.h"
38#include "llvm/Support/system_error.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000039#include "llvm/Target/TargetRegisterInfo.h"
Nick Kledzik07b4a622008-02-26 20:26:43 +000040using namespace llvm;
41
Bill Wendlingb8dcda72012-08-06 21:34:54 +000042static cl::opt<bool>
43EnableFPMAD("enable-fp-mad",
44 cl::desc("Enable less precise MAD instructions to be generated"),
45 cl::init(false));
46
47static cl::opt<bool>
48DisableFPElim("disable-fp-elim",
49 cl::desc("Disable frame pointer elimination optimization"),
50 cl::init(false));
51
52static cl::opt<bool>
Bill Wendlingb8dcda72012-08-06 21:34:54 +000053EnableUnsafeFPMath("enable-unsafe-fp-math",
54 cl::desc("Enable optimizations that may decrease FP precision"),
55 cl::init(false));
56
57static cl::opt<bool>
58EnableNoInfsFPMath("enable-no-infs-fp-math",
59 cl::desc("Enable FP math optimizations that assume no +-Infs"),
60 cl::init(false));
61
62static cl::opt<bool>
63EnableNoNaNsFPMath("enable-no-nans-fp-math",
64 cl::desc("Enable FP math optimizations that assume no NaNs"),
65 cl::init(false));
66
67static cl::opt<bool>
68EnableHonorSignDependentRoundingFPMath("enable-sign-dependent-rounding-fp-math",
69 cl::Hidden,
70 cl::desc("Force codegen to assume rounding mode can change dynamically"),
71 cl::init(false));
72
73static cl::opt<bool>
74GenerateSoftFloatCalls("soft-float",
75 cl::desc("Generate software floating point library calls"),
76 cl::init(false));
77
78static cl::opt<llvm::FloatABI::ABIType>
79FloatABIForCalls("float-abi",
80 cl::desc("Choose float ABI type"),
81 cl::init(FloatABI::Default),
82 cl::values(
83 clEnumValN(FloatABI::Default, "default",
84 "Target default float ABI type"),
85 clEnumValN(FloatABI::Soft, "soft",
86 "Soft float ABI (implied by -soft-float)"),
87 clEnumValN(FloatABI::Hard, "hard",
88 "Hard float ABI (uses FP registers)"),
89 clEnumValEnd));
90
91static cl::opt<llvm::FPOpFusion::FPOpFusionMode>
92FuseFPOps("fp-contract",
93 cl::desc("Enable aggresive formation of fused FP ops"),
94 cl::init(FPOpFusion::Standard),
95 cl::values(
96 clEnumValN(FPOpFusion::Fast, "fast",
97 "Fuse FP ops whenever profitable"),
98 clEnumValN(FPOpFusion::Standard, "on",
99 "Only fuse 'blessed' FP ops."),
100 clEnumValN(FPOpFusion::Strict, "off",
101 "Only fuse FP ops when the result won't be effected."),
102 clEnumValEnd));
103
104static cl::opt<bool>
105DontPlaceZerosInBSS("nozero-initialized-in-bss",
106 cl::desc("Don't place zero-initialized symbols into bss section"),
107 cl::init(false));
108
109static cl::opt<bool>
110EnableGuaranteedTailCallOpt("tailcallopt",
111 cl::desc("Turn fastcc calls into tail calls by (potentially) changing ABI."),
112 cl::init(false));
113
114static cl::opt<bool>
115DisableTailCalls("disable-tail-calls",
116 cl::desc("Never emit tail calls"),
117 cl::init(false));
118
119static cl::opt<unsigned>
120OverrideStackAlignment("stack-alignment",
121 cl::desc("Override default stack alignment"),
122 cl::init(0));
123
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000124static cl::opt<std::string>
125TrapFuncName("trap-func", cl::Hidden,
126 cl::desc("Emit a call to trap function rather than a trap instruction"),
127 cl::init(""));
128
129static cl::opt<bool>
130EnablePIE("enable-pie",
131 cl::desc("Assume the creation of a position independent executable."),
132 cl::init(false));
133
134static cl::opt<bool>
135SegmentedStacks("segmented-stacks",
136 cl::desc("Use segmented stacks if possible."),
137 cl::init(false));
138
139static cl::opt<bool>
140UseInitArray("use-init-array",
141 cl::desc("Use .init_array instead of .ctors."),
142 cl::init(false));
143
Bill Wendlingfb440502012-03-28 20:46:54 +0000144LTOModule::LTOModule(llvm::Module *m, llvm::TargetMachine *t)
145 : _module(m), _target(t),
Bill Wendlingbc07a892013-06-18 07:20:20 +0000146 _context(_target->getMCAsmInfo(), _target->getRegisterInfo(), NULL),
Bill Wendling70b14002013-05-29 20:37:19 +0000147 _mangler(_context, t) {}
Bill Wendlingfb440502012-03-28 20:46:54 +0000148
149/// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM
150/// bitcode.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000151bool LTOModule::isBitcodeFile(const void *mem, size_t length) {
Rafael Espindola46ed3532013-06-11 18:05:26 +0000152 return sys::fs::identify_magic(StringRef((const char *)mem, length)) ==
153 sys::fs::file_magic::bitcode;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000154}
155
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000156bool LTOModule::isBitcodeFile(const char *path) {
Rafael Espindola71affba2013-06-12 15:13:57 +0000157 sys::fs::file_magic type;
158 if (sys::fs::identify_magic(path, type))
159 return false;
160 return type == sys::fs::file_magic::bitcode;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000161}
162
Bill Wendlingfb440502012-03-28 20:46:54 +0000163/// isBitcodeFileForTarget - Returns 'true' if the file (or memory contents) is
164/// LLVM bitcode for the specified triple.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000165bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length,
166 const char *triplePrefix) {
167 MemoryBuffer *buffer = makeBuffer(mem, length);
168 if (!buffer)
Nick Kledzikb481c202009-06-01 20:33:09 +0000169 return false;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000170 return isTargetMatch(buffer, triplePrefix);
Nick Kledzikb481c202009-06-01 20:33:09 +0000171}
172
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000173bool LTOModule::isBitcodeFileForTarget(const char *path,
174 const char *triplePrefix) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000175 OwningPtr<MemoryBuffer> buffer;
176 if (MemoryBuffer::getFile(path, buffer))
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000177 return false;
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000178 return isTargetMatch(buffer.take(), triplePrefix);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000179}
180
Bill Wendlingfb440502012-03-28 20:46:54 +0000181/// isTargetMatch - Returns 'true' if the memory buffer is for the specified
182/// target triple.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000183bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) {
Bill Wendling0198ce02010-10-06 01:22:42 +0000184 std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
185 delete buffer;
Bill Wendling5f689e72011-11-04 18:48:00 +0000186 return strncmp(Triple.c_str(), triplePrefix, strlen(triplePrefix)) == 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000187}
188
Bill Wendlingfb440502012-03-28 20:46:54 +0000189/// makeLTOModule - Create an LTOModule. N.B. These methods take ownership of
190/// the buffer.
191LTOModule *LTOModule::makeLTOModule(const char *path, std::string &errMsg) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000192 OwningPtr<MemoryBuffer> buffer;
193 if (error_code ec = MemoryBuffer::getFile(path, buffer)) {
Michael J. Spencerd4227232010-12-09 18:06:07 +0000194 errMsg = ec.message();
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000195 return NULL;
Michael J. Spencerd4227232010-12-09 18:06:07 +0000196 }
Rafael Espindola5b778b22011-03-18 19:51:00 +0000197 return makeLTOModule(buffer.take(), errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000198}
199
Rafael Espindola56e41f72011-02-08 22:40:47 +0000200LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
Bill Wendling5f689e72011-11-04 18:48:00 +0000201 size_t size, std::string &errMsg) {
Rafael Espindola3d2ac2e2013-07-23 20:25:01 +0000202 return makeLTOModule(fd, path, size, 0, errMsg);
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000203}
204
205LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000206 size_t map_size,
207 off_t offset,
Rafael Espindola56e41f72011-02-08 22:40:47 +0000208 std::string &errMsg) {
209 OwningPtr<MemoryBuffer> buffer;
Rafael Espindola3d2ac2e2013-07-23 20:25:01 +0000210 if (error_code ec =
211 MemoryBuffer::getOpenFileSlice(fd, path, buffer, map_size, offset)) {
Rafael Espindola56e41f72011-02-08 22:40:47 +0000212 errMsg = ec.message();
213 return NULL;
214 }
Rafael Espindola5b778b22011-03-18 19:51:00 +0000215 return makeLTOModule(buffer.take(), errMsg);
Rafael Espindola56e41f72011-02-08 22:40:47 +0000216}
217
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000218LTOModule *LTOModule::makeLTOModule(const void *mem, size_t length,
219 std::string &errMsg) {
220 OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length));
221 if (!buffer)
222 return NULL;
Rafael Espindola5b778b22011-03-18 19:51:00 +0000223 return makeLTOModule(buffer.take(), errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000224}
225
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000226void LTOModule::getTargetOptions(TargetOptions &Options) {
227 Options.LessPreciseFPMADOption = EnableFPMAD;
228 Options.NoFramePointerElim = DisableFPElim;
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000229 Options.AllowFPOpFusion = FuseFPOps;
230 Options.UnsafeFPMath = EnableUnsafeFPMath;
231 Options.NoInfsFPMath = EnableNoInfsFPMath;
232 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
233 Options.HonorSignDependentRoundingFPMathOption =
234 EnableHonorSignDependentRoundingFPMath;
235 Options.UseSoftFloat = GenerateSoftFloatCalls;
236 if (FloatABIForCalls != FloatABI::Default)
237 Options.FloatABIType = FloatABIForCalls;
238 Options.NoZerosInBSS = DontPlaceZerosInBSS;
239 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
240 Options.DisableTailCalls = DisableTailCalls;
241 Options.StackAlignmentOverride = OverrideStackAlignment;
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000242 Options.TrapFuncName = TrapFuncName;
243 Options.PositionIndependentExecutable = EnablePIE;
244 Options.EnableSegmentedStacks = SegmentedStacks;
245 Options.UseInitArray = UseInitArray;
246}
247
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000248LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer,
249 std::string &errMsg) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000250 static bool Initialized = false;
251 if (!Initialized) {
252 InitializeAllTargets();
Evan Cheng8c886a42011-07-22 21:58:54 +0000253 InitializeAllTargetMCs();
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000254 InitializeAllAsmParsers();
255 Initialized = true;
256 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000257
258 // parse bitcode buffer
Rafael Espindola5b778b22011-03-18 19:51:00 +0000259 OwningPtr<Module> m(getLazyBitcodeModule(buffer, getGlobalContext(),
260 &errMsg));
261 if (!m) {
262 delete buffer;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000263 return NULL;
Rafael Espindola5b778b22011-03-18 19:51:00 +0000264 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000265
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000266 std::string TripleStr = m->getTargetTriple();
267 if (TripleStr.empty())
268 TripleStr = sys::getDefaultTargetTriple();
269 llvm::Triple Triple(TripleStr);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000270
271 // find machine architecture for this module
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000272 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000273 if (!march)
274 return NULL;
275
Nick Lewycky364c04a2011-04-21 01:54:08 +0000276 // construct LTOModule, hand over ownership of module and target
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000277 SubtargetFeatures Features;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000278 Features.getDefaultSubtargetFeatures(Triple);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000279 std::string FeatureStr = Features.getString();
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000280 // Set a default CPU for Darwin triples.
Evan Chengfe6e4052011-06-30 01:53:36 +0000281 std::string CPU;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000282 if (Triple.isOSDarwin()) {
283 if (Triple.getArch() == llvm::Triple::x86_64)
284 CPU = "core2";
285 else if (Triple.getArch() == llvm::Triple::x86)
286 CPU = "yonah";
287 }
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000288 TargetOptions Options;
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000289 getTargetOptions(Options);
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000290 TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000291 Options);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000292 LTOModule *Ret = new LTOModule(m.take(), target);
Bill Wendling7e58b382012-03-28 23:12:18 +0000293 if (Ret->parseSymbols(errMsg)) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000294 delete Ret;
295 return NULL;
296 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000297
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000298 return Ret;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000299}
300
Bill Wendlingfb440502012-03-28 20:46:54 +0000301/// makeBuffer - Create a MemoryBuffer from a memory range.
302MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length) {
Roman Divackyad06cee2012-09-05 22:26:57 +0000303 const char *startPtr = (const char*)mem;
Bill Wendlingfb440502012-03-28 20:46:54 +0000304 return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), "", false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000305}
306
Bill Wendlingfb440502012-03-28 20:46:54 +0000307/// objcClassNameFromExpression - Get string that the data pointer points to.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000308bool
309LTOModule::objcClassNameFromExpression(const Constant *c, std::string &name) {
310 if (const ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000311 Constant *op = ce->getOperand(0);
312 if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) {
313 Constant *cn = gvn->getInitializer();
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000314 if (ConstantDataArray *ca = dyn_cast<ConstantDataArray>(cn)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000315 if (ca->isCString()) {
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000316 name = ".objc_class_name_" + ca->getAsCString().str();
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000317 return true;
Nick Kledzikb481c202009-06-01 20:33:09 +0000318 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000319 }
Nick Kledzikb481c202009-06-01 20:33:09 +0000320 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000321 }
322 return false;
323}
324
Bill Wendlingfb440502012-03-28 20:46:54 +0000325/// addObjCClass - Parse i386/ppc ObjC class data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000326void LTOModule::addObjCClass(const GlobalVariable *clgv) {
327 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000328 if (!c) return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000329
Bill Wendling5f689e72011-11-04 18:48:00 +0000330 // second slot in __OBJC,__class is pointer to superclass name
331 std::string superclassName;
332 if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
333 NameAndAttributes info;
334 StringMap<NameAndAttributes>::value_type &entry =
335 _undefines.GetOrCreateValue(superclassName);
336 if (!entry.getValue().name) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000337 const char *symbolName = entry.getKey().data();
338 info.name = symbolName;
339 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000340 info.isFunction = false;
341 info.symbol = clgv;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000342 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000343 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000344 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000345
346 // third slot in __OBJC,__class is pointer to class name
347 std::string className;
348 if (objcClassNameFromExpression(c->getOperand(2), className)) {
349 StringSet::value_type &entry = _defines.GetOrCreateValue(className);
350 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000351
Bill Wendling5f689e72011-11-04 18:48:00 +0000352 NameAndAttributes info;
353 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000354 info.attributes = LTO_SYMBOL_PERMISSIONS_DATA |
355 LTO_SYMBOL_DEFINITION_REGULAR | LTO_SYMBOL_SCOPE_DEFAULT;
356 info.isFunction = false;
357 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000358 _symbols.push_back(info);
359 }
360}
361
Bill Wendlingfb440502012-03-28 20:46:54 +0000362/// addObjCCategory - Parse i386/ppc ObjC category data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000363void LTOModule::addObjCCategory(const GlobalVariable *clgv) {
364 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000365 if (!c) return;
366
367 // second slot in __OBJC,__category is pointer to target class name
368 std::string targetclassName;
369 if (!objcClassNameFromExpression(c->getOperand(1), targetclassName))
370 return;
371
372 NameAndAttributes info;
373 StringMap<NameAndAttributes>::value_type &entry =
374 _undefines.GetOrCreateValue(targetclassName);
375
376 if (entry.getValue().name)
377 return;
378
379 const char *symbolName = entry.getKey().data();
380 info.name = symbolName;
381 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000382 info.isFunction = false;
383 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000384 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000385}
386
Bill Wendlingfb440502012-03-28 20:46:54 +0000387/// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000388void LTOModule::addObjCClassRef(const GlobalVariable *clgv) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000389 std::string targetclassName;
Bill Wendling5f689e72011-11-04 18:48:00 +0000390 if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName))
391 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000392
Bill Wendling5f689e72011-11-04 18:48:00 +0000393 NameAndAttributes info;
394 StringMap<NameAndAttributes>::value_type &entry =
395 _undefines.GetOrCreateValue(targetclassName);
396 if (entry.getValue().name)
397 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000398
Bill Wendling5f689e72011-11-04 18:48:00 +0000399 const char *symbolName = entry.getKey().data();
400 info.name = symbolName;
401 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000402 info.isFunction = false;
403 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000404 entry.setValue(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000405}
406
Bill Wendlingfb440502012-03-28 20:46:54 +0000407/// addDefinedDataSymbol - Add a data symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000408void LTOModule::addDefinedDataSymbol(const GlobalValue *v) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000409 // Add to list of defined symbols.
Bill Wendlinga2af6742011-11-04 09:30:19 +0000410 addDefinedSymbol(v, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000411
Bill Wendling45f74e32012-08-06 22:52:45 +0000412 if (!v->hasSection() /* || !isTargetDarwin */)
413 return;
414
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000415 // Special case i386/ppc ObjC data structures in magic sections:
416 // The issue is that the old ObjC object format did some strange
417 // contortions to avoid real linker symbols. For instance, the
418 // ObjC class data structure is allocated statically in the executable
419 // that defines that class. That data structures contains a pointer to
420 // its superclass. But instead of just initializing that part of the
421 // struct to the address of its superclass, and letting the static and
422 // dynamic linkers do the rest, the runtime works by having that field
423 // instead point to a C-string that is the name of the superclass.
424 // At runtime the objc initialization updates that pointer and sets
425 // it to point to the actual super class. As far as the linker
426 // knows it is just a pointer to a string. But then someone wanted the
427 // linker to issue errors at build time if the superclass was not found.
428 // So they figured out a way in mach-o object format to use an absolute
429 // symbols (.objc_class_name_Foo = 0) and a floating reference
430 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
431 // a class was missing.
432 // The following synthesizes the implicit .objc_* symbols for the linker
433 // from the ObjC data structures generated by the front end.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000434
Bill Wendling45f74e32012-08-06 22:52:45 +0000435 // special case if this data blob is an ObjC class definition
436 if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000437 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000438 addObjCClass(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000439 }
Bill Wendling45f74e32012-08-06 22:52:45 +0000440 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000441
Bill Wendling45f74e32012-08-06 22:52:45 +0000442 // special case if this data blob is an ObjC category definition
443 else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000444 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000445 addObjCCategory(gv);
446 }
447 }
448
449 // special case if this data blob is the list of referenced classes
450 else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000451 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000452 addObjCClassRef(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000453 }
454 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000455}
456
Bill Wendlingfb440502012-03-28 20:46:54 +0000457/// addDefinedFunctionSymbol - Add a function symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000458void LTOModule::addDefinedFunctionSymbol(const Function *f) {
Bill Wendlingfb440502012-03-28 20:46:54 +0000459 // add to list of defined symbols
460 addDefinedSymbol(f, true);
461}
462
463/// addDefinedSymbol - Add a defined symbol to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000464void LTOModule::addDefinedSymbol(const GlobalValue *def, bool isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000465 // ignore all llvm.* symbols
466 if (def->getName().startswith("llvm."))
467 return;
468
469 // string is owned by _defines
Rafael Espindola34b59382011-02-11 05:23:09 +0000470 SmallString<64> Buffer;
Bill Wendlinga2af6742011-11-04 09:30:19 +0000471 _mangler.getNameWithPrefix(Buffer, def, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000472
473 // set alignment part log2() can have rounding errors
474 uint32_t align = def->getAlignment();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000475 uint32_t attr = align ? countTrailingZeros(def->getAlignment()) : 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000476
477 // set permissions part
Bill Wendling9ee2d332012-03-29 08:27:32 +0000478 if (isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000479 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000480 } else {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000481 const GlobalVariable *gv = dyn_cast<GlobalVariable>(def);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000482 if (gv && gv->isConstant())
483 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
484 else
485 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
486 }
487
488 // set definition part
Bill Wendling2776d462010-09-27 18:05:19 +0000489 if (def->hasWeakLinkage() || def->hasLinkOnceLinkage() ||
Bill Wendling34bc34e2012-08-17 18:33:14 +0000490 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000491 attr |= LTO_SYMBOL_DEFINITION_WEAK;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000492 else if (def->hasCommonLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000493 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000494 else
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000495 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000496
497 // set scope part
498 if (def->hasHiddenVisibility())
499 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
500 else if (def->hasProtectedVisibility())
501 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000502 else if (def->hasExternalLinkage() || def->hasWeakLinkage() ||
503 def->hasLinkOnceLinkage() || def->hasCommonLinkage() ||
504 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000505 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
Bill Wendling34bc34e2012-08-17 18:33:14 +0000506 else if (def->hasLinkOnceODRAutoHideLinkage())
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000507 attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000508 else
509 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
510
Chad Rosier772a91f2011-06-28 18:26:12 +0000511 StringSet::value_type &entry = _defines.GetOrCreateValue(Buffer);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000512 entry.setValue(1);
513
Bill Wendling9ee2d332012-03-29 08:27:32 +0000514 // fill information structure
515 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000516 StringRef Name = entry.getKey();
517 info.name = Name.data();
518 assert(info.name[Name.size()] == '\0');
Bill Wendling9ee2d332012-03-29 08:27:32 +0000519 info.attributes = attr;
520 info.isFunction = isFunction;
521 info.symbol = def;
522
523 // add to table of symbols
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000524 _symbols.push_back(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000525}
526
Bill Wendlingfb440502012-03-28 20:46:54 +0000527/// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
528/// defined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000529void LTOModule::addAsmGlobalSymbol(const char *name,
530 lto_symbol_attributes scope) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000531 StringSet::value_type &entry = _defines.GetOrCreateValue(name);
532
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000533 // only add new define if not already defined
Rafael Espindola477d11f2011-02-20 16:27:25 +0000534 if (entry.getValue())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000535 return;
536
Rafael Espindola477d11f2011-02-20 16:27:25 +0000537 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000538
539 NameAndAttributes &info = _undefines[entry.getKey().data()];
540
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000541 if (info.symbol == 0) {
Bill Wendling71b19bb2012-04-02 10:01:21 +0000542 // FIXME: This is trying to take care of module ASM like this:
543 //
544 // module asm ".zerofill __FOO, __foo, _bar_baz_qux, 0"
545 //
546 // but is gross and its mother dresses it funny. Have the ASM parser give us
547 // more details for this type of situation so that we're not guessing so
548 // much.
549
550 // fill information structure
Rafael Espindola5f4b32f2012-05-11 03:42:13 +0000551 info.name = entry.getKey().data();
Bill Wendling71b19bb2012-04-02 10:01:21 +0000552 info.attributes =
553 LTO_SYMBOL_PERMISSIONS_DATA | LTO_SYMBOL_DEFINITION_REGULAR | scope;
554 info.isFunction = false;
555 info.symbol = 0;
556
557 // add to table of symbols
558 _symbols.push_back(info);
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000559 return;
560 }
561
Bill Wendling9ee2d332012-03-29 08:27:32 +0000562 if (info.isFunction)
563 addDefinedFunctionSymbol(cast<Function>(info.symbol));
564 else
565 addDefinedDataSymbol(info.symbol);
Bill Wendling8f6c8a92012-03-30 23:26:06 +0000566
567 _symbols.back().attributes &= ~LTO_SYMBOL_SCOPE_MASK;
568 _symbols.back().attributes |= scope;
Devang Patela59fe952008-07-16 18:06:52 +0000569}
Nick Kledzik07b4a622008-02-26 20:26:43 +0000570
Bill Wendlingfb440502012-03-28 20:46:54 +0000571/// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the
572/// undefined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000573void LTOModule::addAsmGlobalSymbolUndef(const char *name) {
574 StringMap<NameAndAttributes>::value_type &entry =
575 _undefines.GetOrCreateValue(name);
576
577 _asm_undefines.push_back(entry.getKey().data());
578
579 // we already have the symbol
580 if (entry.getValue().name)
581 return;
582
583 uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;;
584 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
585 NameAndAttributes info;
586 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000587 info.attributes = attr;
588 info.isFunction = false;
589 info.symbol = 0;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000590
591 entry.setValue(info);
592}
593
Bill Wendlingfb440502012-03-28 20:46:54 +0000594/// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet to a
595/// list to be resolved later.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000596void
597LTOModule::addPotentialUndefinedSymbol(const GlobalValue *decl, bool isFunc) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000598 // ignore all llvm.* symbols
599 if (decl->getName().startswith("llvm."))
600 return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000601
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000602 // ignore all aliases
603 if (isa<GlobalAlias>(decl))
604 return;
Nick Lewycky0661b932009-07-09 06:03:04 +0000605
Rafael Espindola34b59382011-02-11 05:23:09 +0000606 SmallString<64> name;
Bill Wendlinga2af6742011-11-04 09:30:19 +0000607 _mangler.getNameWithPrefix(name, decl, false);
Rafael Espindola56548522009-04-24 16:55:21 +0000608
Rafael Espindola477d11f2011-02-20 16:27:25 +0000609 StringMap<NameAndAttributes>::value_type &entry =
Chad Rosier772a91f2011-06-28 18:26:12 +0000610 _undefines.GetOrCreateValue(name);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000611
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000612 // we already have the symbol
Rafael Espindola477d11f2011-02-20 16:27:25 +0000613 if (entry.getValue().name)
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000614 return;
Rafael Espindola56548522009-04-24 16:55:21 +0000615
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000616 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000617
618 info.name = entry.getKey().data();
Bill Wendlingfb440502012-03-28 20:46:54 +0000619
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000620 if (decl->hasExternalWeakLinkage())
621 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
622 else
623 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000624
Bill Wendling9ee2d332012-03-29 08:27:32 +0000625 info.isFunction = isFunc;
626 info.symbol = decl;
627
Rafael Espindola477d11f2011-02-20 16:27:25 +0000628 entry.setValue(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000629}
630
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000631namespace {
632 class RecordStreamer : public MCStreamer {
633 public:
Bill Wendling32867652012-04-03 03:56:52 +0000634 enum State { NeverSeen, Global, Defined, DefinedGlobal, Used };
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000635
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000636 private:
637 StringMap<State> Symbols;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000638
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000639 void markDefined(const MCSymbol &Symbol) {
640 State &S = Symbols[Symbol.getName()];
641 switch (S) {
642 case DefinedGlobal:
643 case Global:
644 S = DefinedGlobal;
645 break;
646 case NeverSeen:
647 case Defined:
648 case Used:
649 S = Defined;
650 break;
651 }
652 }
653 void markGlobal(const MCSymbol &Symbol) {
654 State &S = Symbols[Symbol.getName()];
655 switch (S) {
656 case DefinedGlobal:
657 case Defined:
658 S = DefinedGlobal;
659 break;
660
661 case NeverSeen:
662 case Global:
663 case Used:
664 S = Global;
665 break;
666 }
667 }
668 void markUsed(const MCSymbol &Symbol) {
669 State &S = Symbols[Symbol.getName()];
670 switch (S) {
671 case DefinedGlobal:
672 case Defined:
673 case Global:
674 break;
675
676 case NeverSeen:
677 case Used:
678 S = Used;
679 break;
680 }
681 }
682
683 // FIXME: mostly copied for the obj streamer.
684 void AddValueSymbols(const MCExpr *Value) {
685 switch (Value->getKind()) {
686 case MCExpr::Target:
687 // FIXME: What should we do in here?
688 break;
689
690 case MCExpr::Constant:
691 break;
692
693 case MCExpr::Binary: {
694 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
695 AddValueSymbols(BE->getLHS());
696 AddValueSymbols(BE->getRHS());
697 break;
698 }
699
700 case MCExpr::SymbolRef:
701 markUsed(cast<MCSymbolRefExpr>(Value)->getSymbol());
702 break;
703
704 case MCExpr::Unary:
705 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
706 break;
707 }
708 }
709
710 public:
711 typedef StringMap<State>::const_iterator const_iterator;
712
713 const_iterator begin() {
714 return Symbols.begin();
715 }
716
717 const_iterator end() {
718 return Symbols.end();
719 }
720
Chandler Carruthde093ef2013-01-31 23:29:57 +0000721 RecordStreamer(MCContext &Context)
722 : MCStreamer(SK_RecordStreamer, Context) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000723
Bill Wendling32867652012-04-03 03:56:52 +0000724 virtual void EmitInstruction(const MCInst &Inst) {
725 // Scan for values.
726 for (unsigned i = Inst.getNumOperands(); i--; )
727 if (Inst.getOperand(i).isExpr())
728 AddValueSymbols(Inst.getOperand(i).getExpr());
729 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000730 virtual void EmitLabel(MCSymbol *Symbol) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000731 Symbol->setSection(*getCurrentSection().first);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000732 markDefined(*Symbol);
733 }
Reed Kotleraee4d5d12012-12-16 04:00:45 +0000734 virtual void EmitDebugLabel(MCSymbol *Symbol) {
735 EmitLabel(Symbol);
736 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000737 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
738 // FIXME: should we handle aliases?
739 markDefined(*Symbol);
740 }
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000741 virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000742 if (Attribute == MCSA_Global)
743 markGlobal(*Symbol);
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000744 return true;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000745 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000746 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Cheng95847992012-06-22 20:30:39 +0000747 uint64_t Size , unsigned ByteAlignment) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000748 markDefined(*Symbol);
749 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000750 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
751 unsigned ByteAlignment) {
752 markDefined(*Symbol);
753 }
Bill Wendling32867652012-04-03 03:56:52 +0000754
Eli Benderskyf483ff92012-12-20 19:05:53 +0000755 virtual void EmitBundleAlignMode(unsigned AlignPow2) {}
Eli Bendersky802b6282013-01-07 21:51:08 +0000756 virtual void EmitBundleLock(bool AlignToEnd) {}
Eli Benderskyf483ff92012-12-20 19:05:53 +0000757 virtual void EmitBundleUnlock() {}
758
Bill Wendling32867652012-04-03 03:56:52 +0000759 // Noop calls.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000760 virtual void ChangeSection(const MCSection *Section,
761 const MCExpr *Subsection) {}
Eli Benderskycbb25142013-01-14 19:04:57 +0000762 virtual void InitToTextSection() {}
Bill Wendling32867652012-04-03 03:56:52 +0000763 virtual void InitSections() {}
764 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {}
765 virtual void EmitThumbFunc(MCSymbol *Func) {}
766 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {}
767 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {}
768 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {}
769 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {}
770 virtual void EmitCOFFSymbolType(int Type) {}
771 virtual void EndCOFFSymbolDef() {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000772 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {}
Benjamin Kramer63970512011-09-01 23:04:27 +0000773 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
774 unsigned ByteAlignment) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000775 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
776 uint64_t Size, unsigned ByteAlignment) {}
Rafael Espindola64e1af82013-07-02 15:49:13 +0000777 virtual void EmitBytes(StringRef Data) {}
778 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {}
Rafael Espindola6aea5922011-04-21 23:39:26 +0000779 virtual void EmitULEB128Value(const MCExpr *Value) {}
780 virtual void EmitSLEB128Value(const MCExpr *Value) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000781 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
782 unsigned ValueSize,
783 unsigned MaxBytesToEmit) {}
784 virtual void EmitCodeAlignment(unsigned ByteAlignment,
785 unsigned MaxBytesToEmit) {}
Jim Grosbachb5912772012-01-27 00:37:08 +0000786 virtual bool EmitValueToOffset(const MCExpr *Offset,
787 unsigned char Value ) { return false; }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000788 virtual void EmitFileDirective(StringRef Filename) {}
789 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
790 const MCSymbol *LastLabel,
Evan Chengc7ac6902011-07-14 05:43:07 +0000791 const MCSymbol *Label,
792 unsigned PointerSize) {}
Rafael Espindola07082092012-01-07 03:13:18 +0000793 virtual void FinishImpl() {}
Chandler Carruthde093ef2013-01-31 23:29:57 +0000794
795 static bool classof(const MCStreamer *S) {
Chandler Carruth30cfaa22013-01-31 23:34:47 +0000796 return S->getKind() == SK_RecordStreamer;
Chandler Carruthde093ef2013-01-31 23:29:57 +0000797 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000798 };
Bill Wendling9ee2d332012-03-29 08:27:32 +0000799} // end anonymous namespace
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000800
Bill Wendlingfb440502012-03-28 20:46:54 +0000801/// addAsmGlobalSymbols - Add global symbols from module-level ASM to the
802/// defined or undefined lists.
Bill Wendlingac2abde2011-11-04 09:24:40 +0000803bool LTOModule::addAsmGlobalSymbols(std::string &errMsg) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000804 const std::string &inlineAsm = _module->getModuleInlineAsm();
Ivan Krasincc2a8012011-09-08 07:38:25 +0000805 if (inlineAsm.empty())
806 return false;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000807
Bill Wendlingac2abde2011-11-04 09:24:40 +0000808 OwningPtr<RecordStreamer> Streamer(new RecordStreamer(_context));
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000809 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(inlineAsm);
810 SourceMgr SrcMgr;
811 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach345768c2011-08-16 18:33:49 +0000812 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr,
Bill Wendlingac2abde2011-11-04 09:24:40 +0000813 _context, *Streamer,
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000814 *_target->getMCAsmInfo()));
Bill Wendling9351b3e2012-08-08 22:01:55 +0000815 const Target &T = _target->getTarget();
816 OwningPtr<MCSubtargetInfo>
817 STI(T.createMCSubtargetInfo(_target->getTargetTriple(),
818 _target->getTargetCPU(),
819 _target->getTargetFeatureString()));
820 OwningPtr<MCTargetAsmParser> TAP(T.createMCAsmParser(*STI, *Parser.get()));
Ivan Krasin8149dd62011-09-08 07:36:39 +0000821 if (!TAP) {
Bill Wendling9351b3e2012-08-08 22:01:55 +0000822 errMsg = "target " + std::string(T.getName()) +
823 " does not define AsmParser.";
Ivan Krasin8149dd62011-09-08 07:36:39 +0000824 return true;
825 }
826
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000827 Parser->setTargetParser(*TAP);
Bill Wendling9351b3e2012-08-08 22:01:55 +0000828 if (Parser->Run(false))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000829 return true;
830
831 for (RecordStreamer::const_iterator i = Streamer->begin(),
832 e = Streamer->end(); i != e; ++i) {
833 StringRef Key = i->first();
834 RecordStreamer::State Value = i->second;
835 if (Value == RecordStreamer::DefinedGlobal)
836 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_DEFAULT);
837 else if (Value == RecordStreamer::Defined)
838 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_INTERNAL);
839 else if (Value == RecordStreamer::Global ||
840 Value == RecordStreamer::Used)
841 addAsmGlobalSymbolUndef(Key.data());
842 }
Bill Wendling9351b3e2012-08-08 22:01:55 +0000843
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000844 return false;
845}
846
Bill Wendlingfb440502012-03-28 20:46:54 +0000847/// isDeclaration - Return 'true' if the global value is a declaration.
Rafael Espindola5b778b22011-03-18 19:51:00 +0000848static bool isDeclaration(const GlobalValue &V) {
849 if (V.hasAvailableExternallyLinkage())
850 return true;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000851
Rafael Espindola5b778b22011-03-18 19:51:00 +0000852 if (V.isMaterializable())
853 return false;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000854
Rafael Espindola5b778b22011-03-18 19:51:00 +0000855 return V.isDeclaration();
856}
857
Bill Wendling7e58b382012-03-28 23:12:18 +0000858/// parseSymbols - Parse the symbols from the module and model-level ASM and add
Bill Wendlingfb440502012-03-28 20:46:54 +0000859/// them to either the defined or undefined lists.
Bill Wendling7e58b382012-03-28 23:12:18 +0000860bool LTOModule::parseSymbols(std::string &errMsg) {
Daniel Dunbar919660b2010-08-10 23:46:46 +0000861 // add functions
Bill Wendling763acfc2012-03-29 03:34:57 +0000862 for (Module::iterator f = _module->begin(), e = _module->end(); f != e; ++f) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000863 if (isDeclaration(*f))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000864 addPotentialUndefinedSymbol(f, true);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000865 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000866 addDefinedFunctionSymbol(f);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000867 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000868
Daniel Dunbar919660b2010-08-10 23:46:46 +0000869 // add data
870 for (Module::global_iterator v = _module->global_begin(),
871 e = _module->global_end(); v != e; ++v) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000872 if (isDeclaration(*v))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000873 addPotentialUndefinedSymbol(v, false);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000874 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000875 addDefinedDataSymbol(v);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000876 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000877
Daniel Dunbar919660b2010-08-10 23:46:46 +0000878 // add asm globals
Bill Wendlingac2abde2011-11-04 09:24:40 +0000879 if (addAsmGlobalSymbols(errMsg))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000880 return true;
Daniel Dunbar919660b2010-08-10 23:46:46 +0000881
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000882 // add aliases
Bill Wendling9ee2d332012-03-29 08:27:32 +0000883 for (Module::alias_iterator a = _module->alias_begin(),
884 e = _module->alias_end(); a != e; ++a) {
885 if (isDeclaration(*a->getAliasedGlobal()))
Bill Wendlingd58ed732012-03-28 20:48:49 +0000886 // Is an alias to a declaration.
Bill Wendling9ee2d332012-03-29 08:27:32 +0000887 addPotentialUndefinedSymbol(a, false);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000888 else
Bill Wendling9ee2d332012-03-29 08:27:32 +0000889 addDefinedDataSymbol(a);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000890 }
891
Daniel Dunbar919660b2010-08-10 23:46:46 +0000892 // make symbols for all undefines
Bill Wendling9ee2d332012-03-29 08:27:32 +0000893 for (StringMap<NameAndAttributes>::iterator u =_undefines.begin(),
894 e = _undefines.end(); u != e; ++u) {
895 // If this symbol also has a definition, then don't make an undefine because
896 // it is a tentative definition.
897 if (_defines.count(u->getKey())) continue;
898 NameAndAttributes info = u->getValue();
899 _symbols.push_back(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000900 }
Bill Wendling9ee2d332012-03-29 08:27:32 +0000901
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000902 return false;
Nick Kledzik91a6dcf2008-02-27 22:25:36 +0000903}