blob: 807d860b79cfaab2541f5819e280675de93ea3df [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"
Rafael Espindolad1fcac92013-06-11 20:00:56 +000035#include "llvm/Support/PathV1.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000036#include "llvm/Support/SourceMgr.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000037#include "llvm/Support/TargetRegistry.h"
38#include "llvm/Support/TargetSelect.h"
39#include "llvm/Support/system_error.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000040#include "llvm/Target/TargetRegisterInfo.h"
Nick Kledzik07b4a622008-02-26 20:26:43 +000041using namespace llvm;
42
Bill Wendlingb8dcda72012-08-06 21:34:54 +000043static cl::opt<bool>
44EnableFPMAD("enable-fp-mad",
45 cl::desc("Enable less precise MAD instructions to be generated"),
46 cl::init(false));
47
48static cl::opt<bool>
49DisableFPElim("disable-fp-elim",
50 cl::desc("Disable frame pointer elimination optimization"),
51 cl::init(false));
52
53static cl::opt<bool>
54DisableFPElimNonLeaf("disable-non-leaf-fp-elim",
55 cl::desc("Disable frame pointer elimination optimization for non-leaf funcs"),
56 cl::init(false));
57
58static cl::opt<bool>
59EnableUnsafeFPMath("enable-unsafe-fp-math",
60 cl::desc("Enable optimizations that may decrease FP precision"),
61 cl::init(false));
62
63static cl::opt<bool>
64EnableNoInfsFPMath("enable-no-infs-fp-math",
65 cl::desc("Enable FP math optimizations that assume no +-Infs"),
66 cl::init(false));
67
68static cl::opt<bool>
69EnableNoNaNsFPMath("enable-no-nans-fp-math",
70 cl::desc("Enable FP math optimizations that assume no NaNs"),
71 cl::init(false));
72
73static cl::opt<bool>
74EnableHonorSignDependentRoundingFPMath("enable-sign-dependent-rounding-fp-math",
75 cl::Hidden,
76 cl::desc("Force codegen to assume rounding mode can change dynamically"),
77 cl::init(false));
78
79static cl::opt<bool>
80GenerateSoftFloatCalls("soft-float",
81 cl::desc("Generate software floating point library calls"),
82 cl::init(false));
83
84static cl::opt<llvm::FloatABI::ABIType>
85FloatABIForCalls("float-abi",
86 cl::desc("Choose float ABI type"),
87 cl::init(FloatABI::Default),
88 cl::values(
89 clEnumValN(FloatABI::Default, "default",
90 "Target default float ABI type"),
91 clEnumValN(FloatABI::Soft, "soft",
92 "Soft float ABI (implied by -soft-float)"),
93 clEnumValN(FloatABI::Hard, "hard",
94 "Hard float ABI (uses FP registers)"),
95 clEnumValEnd));
96
97static cl::opt<llvm::FPOpFusion::FPOpFusionMode>
98FuseFPOps("fp-contract",
99 cl::desc("Enable aggresive formation of fused FP ops"),
100 cl::init(FPOpFusion::Standard),
101 cl::values(
102 clEnumValN(FPOpFusion::Fast, "fast",
103 "Fuse FP ops whenever profitable"),
104 clEnumValN(FPOpFusion::Standard, "on",
105 "Only fuse 'blessed' FP ops."),
106 clEnumValN(FPOpFusion::Strict, "off",
107 "Only fuse FP ops when the result won't be effected."),
108 clEnumValEnd));
109
110static cl::opt<bool>
111DontPlaceZerosInBSS("nozero-initialized-in-bss",
112 cl::desc("Don't place zero-initialized symbols into bss section"),
113 cl::init(false));
114
115static cl::opt<bool>
116EnableGuaranteedTailCallOpt("tailcallopt",
117 cl::desc("Turn fastcc calls into tail calls by (potentially) changing ABI."),
118 cl::init(false));
119
120static cl::opt<bool>
121DisableTailCalls("disable-tail-calls",
122 cl::desc("Never emit tail calls"),
123 cl::init(false));
124
125static cl::opt<unsigned>
126OverrideStackAlignment("stack-alignment",
127 cl::desc("Override default stack alignment"),
128 cl::init(0));
129
130static cl::opt<bool>
131EnableRealignStack("realign-stack",
132 cl::desc("Realign stack if needed"),
133 cl::init(true));
134
135static cl::opt<std::string>
136TrapFuncName("trap-func", cl::Hidden,
137 cl::desc("Emit a call to trap function rather than a trap instruction"),
138 cl::init(""));
139
140static cl::opt<bool>
141EnablePIE("enable-pie",
142 cl::desc("Assume the creation of a position independent executable."),
143 cl::init(false));
144
145static cl::opt<bool>
146SegmentedStacks("segmented-stacks",
147 cl::desc("Use segmented stacks if possible."),
148 cl::init(false));
149
150static cl::opt<bool>
151UseInitArray("use-init-array",
152 cl::desc("Use .init_array instead of .ctors."),
153 cl::init(false));
154
Chad Rosierd269bd82012-08-21 16:15:24 +0000155static cl::opt<unsigned>
156SSPBufferSize("stack-protector-buffer-size", cl::init(8),
157 cl::desc("Lower bound for a buffer to be considered for "
158 "stack protection"));
159
Bill Wendlingfb440502012-03-28 20:46:54 +0000160LTOModule::LTOModule(llvm::Module *m, llvm::TargetMachine *t)
161 : _module(m), _target(t),
162 _context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(), NULL),
Bill Wendling70b14002013-05-29 20:37:19 +0000163 _mangler(_context, t) {}
Bill Wendlingfb440502012-03-28 20:46:54 +0000164
165/// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM
166/// bitcode.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000167bool LTOModule::isBitcodeFile(const void *mem, size_t length) {
Rafael Espindola46ed3532013-06-11 18:05:26 +0000168 return sys::fs::identify_magic(StringRef((const char *)mem, length)) ==
169 sys::fs::file_magic::bitcode;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000170}
171
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000172bool LTOModule::isBitcodeFile(const char *path) {
173 return llvm::sys::Path(path).isBitcodeFile();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000174}
175
Bill Wendlingfb440502012-03-28 20:46:54 +0000176/// isBitcodeFileForTarget - Returns 'true' if the file (or memory contents) is
177/// LLVM bitcode for the specified triple.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000178bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length,
179 const char *triplePrefix) {
180 MemoryBuffer *buffer = makeBuffer(mem, length);
181 if (!buffer)
Nick Kledzikb481c202009-06-01 20:33:09 +0000182 return false;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000183 return isTargetMatch(buffer, triplePrefix);
Nick Kledzikb481c202009-06-01 20:33:09 +0000184}
185
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000186bool LTOModule::isBitcodeFileForTarget(const char *path,
187 const char *triplePrefix) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000188 OwningPtr<MemoryBuffer> buffer;
189 if (MemoryBuffer::getFile(path, buffer))
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000190 return false;
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000191 return isTargetMatch(buffer.take(), triplePrefix);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000192}
193
Bill Wendlingfb440502012-03-28 20:46:54 +0000194/// isTargetMatch - Returns 'true' if the memory buffer is for the specified
195/// target triple.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000196bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) {
Bill Wendling0198ce02010-10-06 01:22:42 +0000197 std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
198 delete buffer;
Bill Wendling5f689e72011-11-04 18:48:00 +0000199 return strncmp(Triple.c_str(), triplePrefix, strlen(triplePrefix)) == 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000200}
201
Bill Wendlingfb440502012-03-28 20:46:54 +0000202/// makeLTOModule - Create an LTOModule. N.B. These methods take ownership of
203/// the buffer.
204LTOModule *LTOModule::makeLTOModule(const char *path, std::string &errMsg) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000205 OwningPtr<MemoryBuffer> buffer;
206 if (error_code ec = MemoryBuffer::getFile(path, buffer)) {
Michael J. Spencerd4227232010-12-09 18:06:07 +0000207 errMsg = ec.message();
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000208 return NULL;
Michael J. Spencerd4227232010-12-09 18:06:07 +0000209 }
Rafael Espindola5b778b22011-03-18 19:51:00 +0000210 return makeLTOModule(buffer.take(), errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000211}
212
Rafael Espindola56e41f72011-02-08 22:40:47 +0000213LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
Bill Wendling5f689e72011-11-04 18:48:00 +0000214 size_t size, std::string &errMsg) {
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000215 return makeLTOModule(fd, path, size, size, 0, errMsg);
216}
217
218LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
219 size_t file_size,
220 size_t map_size,
221 off_t offset,
Rafael Espindola56e41f72011-02-08 22:40:47 +0000222 std::string &errMsg) {
223 OwningPtr<MemoryBuffer> buffer;
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000224 if (error_code ec = MemoryBuffer::getOpenFile(fd, path, buffer, file_size,
225 map_size, offset, false)) {
Rafael Espindola56e41f72011-02-08 22:40:47 +0000226 errMsg = ec.message();
227 return NULL;
228 }
Rafael Espindola5b778b22011-03-18 19:51:00 +0000229 return makeLTOModule(buffer.take(), errMsg);
Rafael Espindola56e41f72011-02-08 22:40:47 +0000230}
231
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000232LTOModule *LTOModule::makeLTOModule(const void *mem, size_t length,
233 std::string &errMsg) {
234 OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length));
235 if (!buffer)
236 return NULL;
Rafael Espindola5b778b22011-03-18 19:51:00 +0000237 return makeLTOModule(buffer.take(), errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000238}
239
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000240void LTOModule::getTargetOptions(TargetOptions &Options) {
241 Options.LessPreciseFPMADOption = EnableFPMAD;
242 Options.NoFramePointerElim = DisableFPElim;
243 Options.NoFramePointerElimNonLeaf = DisableFPElimNonLeaf;
244 Options.AllowFPOpFusion = FuseFPOps;
245 Options.UnsafeFPMath = EnableUnsafeFPMath;
246 Options.NoInfsFPMath = EnableNoInfsFPMath;
247 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
248 Options.HonorSignDependentRoundingFPMathOption =
249 EnableHonorSignDependentRoundingFPMath;
250 Options.UseSoftFloat = GenerateSoftFloatCalls;
251 if (FloatABIForCalls != FloatABI::Default)
252 Options.FloatABIType = FloatABIForCalls;
253 Options.NoZerosInBSS = DontPlaceZerosInBSS;
254 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
255 Options.DisableTailCalls = DisableTailCalls;
256 Options.StackAlignmentOverride = OverrideStackAlignment;
257 Options.RealignStack = EnableRealignStack;
258 Options.TrapFuncName = TrapFuncName;
259 Options.PositionIndependentExecutable = EnablePIE;
260 Options.EnableSegmentedStacks = SegmentedStacks;
261 Options.UseInitArray = UseInitArray;
Chad Rosierd269bd82012-08-21 16:15:24 +0000262 Options.SSPBufferSize = SSPBufferSize;
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000263}
264
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000265LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer,
266 std::string &errMsg) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000267 static bool Initialized = false;
268 if (!Initialized) {
269 InitializeAllTargets();
Evan Cheng8c886a42011-07-22 21:58:54 +0000270 InitializeAllTargetMCs();
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000271 InitializeAllAsmParsers();
272 Initialized = true;
273 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000274
275 // parse bitcode buffer
Rafael Espindola5b778b22011-03-18 19:51:00 +0000276 OwningPtr<Module> m(getLazyBitcodeModule(buffer, getGlobalContext(),
277 &errMsg));
278 if (!m) {
279 delete buffer;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000280 return NULL;
Rafael Espindola5b778b22011-03-18 19:51:00 +0000281 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000282
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000283 std::string TripleStr = m->getTargetTriple();
284 if (TripleStr.empty())
285 TripleStr = sys::getDefaultTargetTriple();
286 llvm::Triple Triple(TripleStr);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000287
288 // find machine architecture for this module
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000289 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000290 if (!march)
291 return NULL;
292
Nick Lewycky364c04a2011-04-21 01:54:08 +0000293 // construct LTOModule, hand over ownership of module and target
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000294 SubtargetFeatures Features;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000295 Features.getDefaultSubtargetFeatures(Triple);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000296 std::string FeatureStr = Features.getString();
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000297 // Set a default CPU for Darwin triples.
Evan Chengfe6e4052011-06-30 01:53:36 +0000298 std::string CPU;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000299 if (Triple.isOSDarwin()) {
300 if (Triple.getArch() == llvm::Triple::x86_64)
301 CPU = "core2";
302 else if (Triple.getArch() == llvm::Triple::x86)
303 CPU = "yonah";
304 }
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000305 TargetOptions Options;
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000306 getTargetOptions(Options);
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000307 TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000308 Options);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000309 LTOModule *Ret = new LTOModule(m.take(), target);
Bill Wendling7e58b382012-03-28 23:12:18 +0000310 if (Ret->parseSymbols(errMsg)) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000311 delete Ret;
312 return NULL;
313 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000314
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000315 return Ret;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000316}
317
Bill Wendlingfb440502012-03-28 20:46:54 +0000318/// makeBuffer - Create a MemoryBuffer from a memory range.
319MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length) {
Roman Divackyad06cee2012-09-05 22:26:57 +0000320 const char *startPtr = (const char*)mem;
Bill Wendlingfb440502012-03-28 20:46:54 +0000321 return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), "", false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000322}
323
Bill Wendlingfb440502012-03-28 20:46:54 +0000324/// objcClassNameFromExpression - Get string that the data pointer points to.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000325bool
326LTOModule::objcClassNameFromExpression(const Constant *c, std::string &name) {
327 if (const ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000328 Constant *op = ce->getOperand(0);
329 if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) {
330 Constant *cn = gvn->getInitializer();
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000331 if (ConstantDataArray *ca = dyn_cast<ConstantDataArray>(cn)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000332 if (ca->isCString()) {
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000333 name = ".objc_class_name_" + ca->getAsCString().str();
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000334 return true;
Nick Kledzikb481c202009-06-01 20:33:09 +0000335 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000336 }
Nick Kledzikb481c202009-06-01 20:33:09 +0000337 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000338 }
339 return false;
340}
341
Bill Wendlingfb440502012-03-28 20:46:54 +0000342/// addObjCClass - Parse i386/ppc ObjC class data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000343void LTOModule::addObjCClass(const GlobalVariable *clgv) {
344 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000345 if (!c) return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000346
Bill Wendling5f689e72011-11-04 18:48:00 +0000347 // second slot in __OBJC,__class is pointer to superclass name
348 std::string superclassName;
349 if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
350 NameAndAttributes info;
351 StringMap<NameAndAttributes>::value_type &entry =
352 _undefines.GetOrCreateValue(superclassName);
353 if (!entry.getValue().name) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000354 const char *symbolName = entry.getKey().data();
355 info.name = symbolName;
356 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000357 info.isFunction = false;
358 info.symbol = clgv;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000359 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000360 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000361 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000362
363 // third slot in __OBJC,__class is pointer to class name
364 std::string className;
365 if (objcClassNameFromExpression(c->getOperand(2), className)) {
366 StringSet::value_type &entry = _defines.GetOrCreateValue(className);
367 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000368
Bill Wendling5f689e72011-11-04 18:48:00 +0000369 NameAndAttributes info;
370 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000371 info.attributes = LTO_SYMBOL_PERMISSIONS_DATA |
372 LTO_SYMBOL_DEFINITION_REGULAR | LTO_SYMBOL_SCOPE_DEFAULT;
373 info.isFunction = false;
374 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000375 _symbols.push_back(info);
376 }
377}
378
Bill Wendlingfb440502012-03-28 20:46:54 +0000379/// addObjCCategory - Parse i386/ppc ObjC category data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000380void LTOModule::addObjCCategory(const GlobalVariable *clgv) {
381 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000382 if (!c) return;
383
384 // second slot in __OBJC,__category is pointer to target class name
385 std::string targetclassName;
386 if (!objcClassNameFromExpression(c->getOperand(1), targetclassName))
387 return;
388
389 NameAndAttributes info;
390 StringMap<NameAndAttributes>::value_type &entry =
391 _undefines.GetOrCreateValue(targetclassName);
392
393 if (entry.getValue().name)
394 return;
395
396 const char *symbolName = entry.getKey().data();
397 info.name = symbolName;
398 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000399 info.isFunction = false;
400 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000401 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000402}
403
Bill Wendlingfb440502012-03-28 20:46:54 +0000404/// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000405void LTOModule::addObjCClassRef(const GlobalVariable *clgv) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000406 std::string targetclassName;
Bill Wendling5f689e72011-11-04 18:48:00 +0000407 if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName))
408 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000409
Bill Wendling5f689e72011-11-04 18:48:00 +0000410 NameAndAttributes info;
411 StringMap<NameAndAttributes>::value_type &entry =
412 _undefines.GetOrCreateValue(targetclassName);
413 if (entry.getValue().name)
414 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000415
Bill Wendling5f689e72011-11-04 18:48:00 +0000416 const char *symbolName = entry.getKey().data();
417 info.name = symbolName;
418 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000419 info.isFunction = false;
420 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000421 entry.setValue(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000422}
423
Bill Wendlingfb440502012-03-28 20:46:54 +0000424/// addDefinedDataSymbol - Add a data symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000425void LTOModule::addDefinedDataSymbol(const GlobalValue *v) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000426 // Add to list of defined symbols.
Bill Wendlinga2af6742011-11-04 09:30:19 +0000427 addDefinedSymbol(v, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000428
Bill Wendling45f74e32012-08-06 22:52:45 +0000429 if (!v->hasSection() /* || !isTargetDarwin */)
430 return;
431
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000432 // Special case i386/ppc ObjC data structures in magic sections:
433 // The issue is that the old ObjC object format did some strange
434 // contortions to avoid real linker symbols. For instance, the
435 // ObjC class data structure is allocated statically in the executable
436 // that defines that class. That data structures contains a pointer to
437 // its superclass. But instead of just initializing that part of the
438 // struct to the address of its superclass, and letting the static and
439 // dynamic linkers do the rest, the runtime works by having that field
440 // instead point to a C-string that is the name of the superclass.
441 // At runtime the objc initialization updates that pointer and sets
442 // it to point to the actual super class. As far as the linker
443 // knows it is just a pointer to a string. But then someone wanted the
444 // linker to issue errors at build time if the superclass was not found.
445 // So they figured out a way in mach-o object format to use an absolute
446 // symbols (.objc_class_name_Foo = 0) and a floating reference
447 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
448 // a class was missing.
449 // The following synthesizes the implicit .objc_* symbols for the linker
450 // from the ObjC data structures generated by the front end.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000451
Bill Wendling45f74e32012-08-06 22:52:45 +0000452 // special case if this data blob is an ObjC class definition
453 if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000454 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000455 addObjCClass(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000456 }
Bill Wendling45f74e32012-08-06 22:52:45 +0000457 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000458
Bill Wendling45f74e32012-08-06 22:52:45 +0000459 // special case if this data blob is an ObjC category definition
460 else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000461 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000462 addObjCCategory(gv);
463 }
464 }
465
466 // special case if this data blob is the list of referenced classes
467 else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000468 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000469 addObjCClassRef(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000470 }
471 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000472}
473
Bill Wendlingfb440502012-03-28 20:46:54 +0000474/// addDefinedFunctionSymbol - Add a function symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000475void LTOModule::addDefinedFunctionSymbol(const Function *f) {
Bill Wendlingfb440502012-03-28 20:46:54 +0000476 // add to list of defined symbols
477 addDefinedSymbol(f, true);
478}
479
480/// addDefinedSymbol - Add a defined symbol to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000481void LTOModule::addDefinedSymbol(const GlobalValue *def, bool isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000482 // ignore all llvm.* symbols
483 if (def->getName().startswith("llvm."))
484 return;
485
486 // string is owned by _defines
Rafael Espindola34b59382011-02-11 05:23:09 +0000487 SmallString<64> Buffer;
Bill Wendlinga2af6742011-11-04 09:30:19 +0000488 _mangler.getNameWithPrefix(Buffer, def, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000489
490 // set alignment part log2() can have rounding errors
491 uint32_t align = def->getAlignment();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000492 uint32_t attr = align ? countTrailingZeros(def->getAlignment()) : 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000493
494 // set permissions part
Bill Wendling9ee2d332012-03-29 08:27:32 +0000495 if (isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000496 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000497 } else {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000498 const GlobalVariable *gv = dyn_cast<GlobalVariable>(def);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000499 if (gv && gv->isConstant())
500 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
501 else
502 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
503 }
504
505 // set definition part
Bill Wendling2776d462010-09-27 18:05:19 +0000506 if (def->hasWeakLinkage() || def->hasLinkOnceLinkage() ||
Bill Wendling34bc34e2012-08-17 18:33:14 +0000507 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000508 attr |= LTO_SYMBOL_DEFINITION_WEAK;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000509 else if (def->hasCommonLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000510 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000511 else
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000512 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000513
514 // set scope part
515 if (def->hasHiddenVisibility())
516 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
517 else if (def->hasProtectedVisibility())
518 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000519 else if (def->hasExternalLinkage() || def->hasWeakLinkage() ||
520 def->hasLinkOnceLinkage() || def->hasCommonLinkage() ||
521 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000522 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
Bill Wendling34bc34e2012-08-17 18:33:14 +0000523 else if (def->hasLinkOnceODRAutoHideLinkage())
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000524 attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000525 else
526 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
527
Chad Rosier772a91f2011-06-28 18:26:12 +0000528 StringSet::value_type &entry = _defines.GetOrCreateValue(Buffer);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000529 entry.setValue(1);
530
Bill Wendling9ee2d332012-03-29 08:27:32 +0000531 // fill information structure
532 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000533 StringRef Name = entry.getKey();
534 info.name = Name.data();
535 assert(info.name[Name.size()] == '\0');
Bill Wendling9ee2d332012-03-29 08:27:32 +0000536 info.attributes = attr;
537 info.isFunction = isFunction;
538 info.symbol = def;
539
540 // add to table of symbols
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000541 _symbols.push_back(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000542}
543
Bill Wendlingfb440502012-03-28 20:46:54 +0000544/// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
545/// defined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000546void LTOModule::addAsmGlobalSymbol(const char *name,
547 lto_symbol_attributes scope) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000548 StringSet::value_type &entry = _defines.GetOrCreateValue(name);
549
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000550 // only add new define if not already defined
Rafael Espindola477d11f2011-02-20 16:27:25 +0000551 if (entry.getValue())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000552 return;
553
Rafael Espindola477d11f2011-02-20 16:27:25 +0000554 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000555
556 NameAndAttributes &info = _undefines[entry.getKey().data()];
557
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000558 if (info.symbol == 0) {
Bill Wendling71b19bb2012-04-02 10:01:21 +0000559 // FIXME: This is trying to take care of module ASM like this:
560 //
561 // module asm ".zerofill __FOO, __foo, _bar_baz_qux, 0"
562 //
563 // but is gross and its mother dresses it funny. Have the ASM parser give us
564 // more details for this type of situation so that we're not guessing so
565 // much.
566
567 // fill information structure
Rafael Espindola5f4b32f2012-05-11 03:42:13 +0000568 info.name = entry.getKey().data();
Bill Wendling71b19bb2012-04-02 10:01:21 +0000569 info.attributes =
570 LTO_SYMBOL_PERMISSIONS_DATA | LTO_SYMBOL_DEFINITION_REGULAR | scope;
571 info.isFunction = false;
572 info.symbol = 0;
573
574 // add to table of symbols
575 _symbols.push_back(info);
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000576 return;
577 }
578
Bill Wendling9ee2d332012-03-29 08:27:32 +0000579 if (info.isFunction)
580 addDefinedFunctionSymbol(cast<Function>(info.symbol));
581 else
582 addDefinedDataSymbol(info.symbol);
Bill Wendling8f6c8a92012-03-30 23:26:06 +0000583
584 _symbols.back().attributes &= ~LTO_SYMBOL_SCOPE_MASK;
585 _symbols.back().attributes |= scope;
Devang Patela59fe952008-07-16 18:06:52 +0000586}
Nick Kledzik07b4a622008-02-26 20:26:43 +0000587
Bill Wendlingfb440502012-03-28 20:46:54 +0000588/// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the
589/// undefined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000590void LTOModule::addAsmGlobalSymbolUndef(const char *name) {
591 StringMap<NameAndAttributes>::value_type &entry =
592 _undefines.GetOrCreateValue(name);
593
594 _asm_undefines.push_back(entry.getKey().data());
595
596 // we already have the symbol
597 if (entry.getValue().name)
598 return;
599
600 uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;;
601 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
602 NameAndAttributes info;
603 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000604 info.attributes = attr;
605 info.isFunction = false;
606 info.symbol = 0;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000607
608 entry.setValue(info);
609}
610
Bill Wendlingfb440502012-03-28 20:46:54 +0000611/// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet to a
612/// list to be resolved later.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000613void
614LTOModule::addPotentialUndefinedSymbol(const GlobalValue *decl, bool isFunc) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000615 // ignore all llvm.* symbols
616 if (decl->getName().startswith("llvm."))
617 return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000618
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000619 // ignore all aliases
620 if (isa<GlobalAlias>(decl))
621 return;
Nick Lewycky0661b932009-07-09 06:03:04 +0000622
Rafael Espindola34b59382011-02-11 05:23:09 +0000623 SmallString<64> name;
Bill Wendlinga2af6742011-11-04 09:30:19 +0000624 _mangler.getNameWithPrefix(name, decl, false);
Rafael Espindola56548522009-04-24 16:55:21 +0000625
Rafael Espindola477d11f2011-02-20 16:27:25 +0000626 StringMap<NameAndAttributes>::value_type &entry =
Chad Rosier772a91f2011-06-28 18:26:12 +0000627 _undefines.GetOrCreateValue(name);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000628
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000629 // we already have the symbol
Rafael Espindola477d11f2011-02-20 16:27:25 +0000630 if (entry.getValue().name)
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000631 return;
Rafael Espindola56548522009-04-24 16:55:21 +0000632
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000633 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000634
635 info.name = entry.getKey().data();
Bill Wendlingfb440502012-03-28 20:46:54 +0000636
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000637 if (decl->hasExternalWeakLinkage())
638 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
639 else
640 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000641
Bill Wendling9ee2d332012-03-29 08:27:32 +0000642 info.isFunction = isFunc;
643 info.symbol = decl;
644
Rafael Espindola477d11f2011-02-20 16:27:25 +0000645 entry.setValue(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000646}
647
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000648namespace {
649 class RecordStreamer : public MCStreamer {
650 public:
Bill Wendling32867652012-04-03 03:56:52 +0000651 enum State { NeverSeen, Global, Defined, DefinedGlobal, Used };
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000652
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000653 private:
654 StringMap<State> Symbols;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000655
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000656 void markDefined(const MCSymbol &Symbol) {
657 State &S = Symbols[Symbol.getName()];
658 switch (S) {
659 case DefinedGlobal:
660 case Global:
661 S = DefinedGlobal;
662 break;
663 case NeverSeen:
664 case Defined:
665 case Used:
666 S = Defined;
667 break;
668 }
669 }
670 void markGlobal(const MCSymbol &Symbol) {
671 State &S = Symbols[Symbol.getName()];
672 switch (S) {
673 case DefinedGlobal:
674 case Defined:
675 S = DefinedGlobal;
676 break;
677
678 case NeverSeen:
679 case Global:
680 case Used:
681 S = Global;
682 break;
683 }
684 }
685 void markUsed(const MCSymbol &Symbol) {
686 State &S = Symbols[Symbol.getName()];
687 switch (S) {
688 case DefinedGlobal:
689 case Defined:
690 case Global:
691 break;
692
693 case NeverSeen:
694 case Used:
695 S = Used;
696 break;
697 }
698 }
699
700 // FIXME: mostly copied for the obj streamer.
701 void AddValueSymbols(const MCExpr *Value) {
702 switch (Value->getKind()) {
703 case MCExpr::Target:
704 // FIXME: What should we do in here?
705 break;
706
707 case MCExpr::Constant:
708 break;
709
710 case MCExpr::Binary: {
711 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
712 AddValueSymbols(BE->getLHS());
713 AddValueSymbols(BE->getRHS());
714 break;
715 }
716
717 case MCExpr::SymbolRef:
718 markUsed(cast<MCSymbolRefExpr>(Value)->getSymbol());
719 break;
720
721 case MCExpr::Unary:
722 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
723 break;
724 }
725 }
726
727 public:
728 typedef StringMap<State>::const_iterator const_iterator;
729
730 const_iterator begin() {
731 return Symbols.begin();
732 }
733
734 const_iterator end() {
735 return Symbols.end();
736 }
737
Chandler Carruthde093ef2013-01-31 23:29:57 +0000738 RecordStreamer(MCContext &Context)
739 : MCStreamer(SK_RecordStreamer, Context) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000740
Bill Wendling32867652012-04-03 03:56:52 +0000741 virtual void EmitInstruction(const MCInst &Inst) {
742 // Scan for values.
743 for (unsigned i = Inst.getNumOperands(); i--; )
744 if (Inst.getOperand(i).isExpr())
745 AddValueSymbols(Inst.getOperand(i).getExpr());
746 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000747 virtual void EmitLabel(MCSymbol *Symbol) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000748 Symbol->setSection(*getCurrentSection().first);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000749 markDefined(*Symbol);
750 }
Reed Kotleraee4d5d12012-12-16 04:00:45 +0000751 virtual void EmitDebugLabel(MCSymbol *Symbol) {
752 EmitLabel(Symbol);
753 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000754 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
755 // FIXME: should we handle aliases?
756 markDefined(*Symbol);
757 }
758 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
759 if (Attribute == MCSA_Global)
760 markGlobal(*Symbol);
761 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000762 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Cheng95847992012-06-22 20:30:39 +0000763 uint64_t Size , unsigned ByteAlignment) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000764 markDefined(*Symbol);
765 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000766 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
767 unsigned ByteAlignment) {
768 markDefined(*Symbol);
769 }
Bill Wendling32867652012-04-03 03:56:52 +0000770
Eli Benderskyf483ff92012-12-20 19:05:53 +0000771 virtual void EmitBundleAlignMode(unsigned AlignPow2) {}
Eli Bendersky802b6282013-01-07 21:51:08 +0000772 virtual void EmitBundleLock(bool AlignToEnd) {}
Eli Benderskyf483ff92012-12-20 19:05:53 +0000773 virtual void EmitBundleUnlock() {}
774
Bill Wendling32867652012-04-03 03:56:52 +0000775 // Noop calls.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000776 virtual void ChangeSection(const MCSection *Section,
777 const MCExpr *Subsection) {}
Eli Benderskycbb25142013-01-14 19:04:57 +0000778 virtual void InitToTextSection() {}
Bill Wendling32867652012-04-03 03:56:52 +0000779 virtual void InitSections() {}
780 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {}
781 virtual void EmitThumbFunc(MCSymbol *Func) {}
782 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {}
783 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {}
784 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {}
785 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {}
786 virtual void EmitCOFFSymbolType(int Type) {}
787 virtual void EndCOFFSymbolDef() {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000788 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {}
Benjamin Kramer63970512011-09-01 23:04:27 +0000789 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
790 unsigned ByteAlignment) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000791 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
792 uint64_t Size, unsigned ByteAlignment) {}
793 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) {}
794 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
Rafael Espindolafd057852011-05-01 03:50:49 +0000795 unsigned AddrSpace) {}
Rafael Espindola6aea5922011-04-21 23:39:26 +0000796 virtual void EmitULEB128Value(const MCExpr *Value) {}
797 virtual void EmitSLEB128Value(const MCExpr *Value) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000798 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
799 unsigned ValueSize,
800 unsigned MaxBytesToEmit) {}
801 virtual void EmitCodeAlignment(unsigned ByteAlignment,
802 unsigned MaxBytesToEmit) {}
Jim Grosbachb5912772012-01-27 00:37:08 +0000803 virtual bool EmitValueToOffset(const MCExpr *Offset,
804 unsigned char Value ) { return false; }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000805 virtual void EmitFileDirective(StringRef Filename) {}
806 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
807 const MCSymbol *LastLabel,
Evan Chengc7ac6902011-07-14 05:43:07 +0000808 const MCSymbol *Label,
809 unsigned PointerSize) {}
Rafael Espindola07082092012-01-07 03:13:18 +0000810 virtual void FinishImpl() {}
Chandler Carruthde093ef2013-01-31 23:29:57 +0000811
812 static bool classof(const MCStreamer *S) {
Chandler Carruth30cfaa22013-01-31 23:34:47 +0000813 return S->getKind() == SK_RecordStreamer;
Chandler Carruthde093ef2013-01-31 23:29:57 +0000814 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000815 };
Bill Wendling9ee2d332012-03-29 08:27:32 +0000816} // end anonymous namespace
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000817
Bill Wendlingfb440502012-03-28 20:46:54 +0000818/// addAsmGlobalSymbols - Add global symbols from module-level ASM to the
819/// defined or undefined lists.
Bill Wendlingac2abde2011-11-04 09:24:40 +0000820bool LTOModule::addAsmGlobalSymbols(std::string &errMsg) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000821 const std::string &inlineAsm = _module->getModuleInlineAsm();
Ivan Krasincc2a8012011-09-08 07:38:25 +0000822 if (inlineAsm.empty())
823 return false;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000824
Bill Wendlingac2abde2011-11-04 09:24:40 +0000825 OwningPtr<RecordStreamer> Streamer(new RecordStreamer(_context));
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000826 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(inlineAsm);
827 SourceMgr SrcMgr;
828 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach345768c2011-08-16 18:33:49 +0000829 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr,
Bill Wendlingac2abde2011-11-04 09:24:40 +0000830 _context, *Streamer,
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000831 *_target->getMCAsmInfo()));
Bill Wendling9351b3e2012-08-08 22:01:55 +0000832 const Target &T = _target->getTarget();
833 OwningPtr<MCSubtargetInfo>
834 STI(T.createMCSubtargetInfo(_target->getTargetTriple(),
835 _target->getTargetCPU(),
836 _target->getTargetFeatureString()));
837 OwningPtr<MCTargetAsmParser> TAP(T.createMCAsmParser(*STI, *Parser.get()));
Ivan Krasin8149dd62011-09-08 07:36:39 +0000838 if (!TAP) {
Bill Wendling9351b3e2012-08-08 22:01:55 +0000839 errMsg = "target " + std::string(T.getName()) +
840 " does not define AsmParser.";
Ivan Krasin8149dd62011-09-08 07:36:39 +0000841 return true;
842 }
843
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000844 Parser->setTargetParser(*TAP);
Bill Wendling9351b3e2012-08-08 22:01:55 +0000845 if (Parser->Run(false))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000846 return true;
847
848 for (RecordStreamer::const_iterator i = Streamer->begin(),
849 e = Streamer->end(); i != e; ++i) {
850 StringRef Key = i->first();
851 RecordStreamer::State Value = i->second;
852 if (Value == RecordStreamer::DefinedGlobal)
853 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_DEFAULT);
854 else if (Value == RecordStreamer::Defined)
855 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_INTERNAL);
856 else if (Value == RecordStreamer::Global ||
857 Value == RecordStreamer::Used)
858 addAsmGlobalSymbolUndef(Key.data());
859 }
Bill Wendling9351b3e2012-08-08 22:01:55 +0000860
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000861 return false;
862}
863
Bill Wendlingfb440502012-03-28 20:46:54 +0000864/// isDeclaration - Return 'true' if the global value is a declaration.
Rafael Espindola5b778b22011-03-18 19:51:00 +0000865static bool isDeclaration(const GlobalValue &V) {
866 if (V.hasAvailableExternallyLinkage())
867 return true;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000868
Rafael Espindola5b778b22011-03-18 19:51:00 +0000869 if (V.isMaterializable())
870 return false;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000871
Rafael Espindola5b778b22011-03-18 19:51:00 +0000872 return V.isDeclaration();
873}
874
Bill Wendling7e58b382012-03-28 23:12:18 +0000875/// parseSymbols - Parse the symbols from the module and model-level ASM and add
Bill Wendlingfb440502012-03-28 20:46:54 +0000876/// them to either the defined or undefined lists.
Bill Wendling7e58b382012-03-28 23:12:18 +0000877bool LTOModule::parseSymbols(std::string &errMsg) {
Daniel Dunbar919660b2010-08-10 23:46:46 +0000878 // add functions
Bill Wendling763acfc2012-03-29 03:34:57 +0000879 for (Module::iterator f = _module->begin(), e = _module->end(); f != e; ++f) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000880 if (isDeclaration(*f))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000881 addPotentialUndefinedSymbol(f, true);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000882 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000883 addDefinedFunctionSymbol(f);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000884 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000885
Daniel Dunbar919660b2010-08-10 23:46:46 +0000886 // add data
887 for (Module::global_iterator v = _module->global_begin(),
888 e = _module->global_end(); v != e; ++v) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000889 if (isDeclaration(*v))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000890 addPotentialUndefinedSymbol(v, false);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000891 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000892 addDefinedDataSymbol(v);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000893 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000894
Daniel Dunbar919660b2010-08-10 23:46:46 +0000895 // add asm globals
Bill Wendlingac2abde2011-11-04 09:24:40 +0000896 if (addAsmGlobalSymbols(errMsg))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000897 return true;
Daniel Dunbar919660b2010-08-10 23:46:46 +0000898
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000899 // add aliases
Bill Wendling9ee2d332012-03-29 08:27:32 +0000900 for (Module::alias_iterator a = _module->alias_begin(),
901 e = _module->alias_end(); a != e; ++a) {
902 if (isDeclaration(*a->getAliasedGlobal()))
Bill Wendlingd58ed732012-03-28 20:48:49 +0000903 // Is an alias to a declaration.
Bill Wendling9ee2d332012-03-29 08:27:32 +0000904 addPotentialUndefinedSymbol(a, false);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000905 else
Bill Wendling9ee2d332012-03-29 08:27:32 +0000906 addDefinedDataSymbol(a);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000907 }
908
Daniel Dunbar919660b2010-08-10 23:46:46 +0000909 // make symbols for all undefines
Bill Wendling9ee2d332012-03-29 08:27:32 +0000910 for (StringMap<NameAndAttributes>::iterator u =_undefines.begin(),
911 e = _undefines.end(); u != e; ++u) {
912 // If this symbol also has a definition, then don't make an undefine because
913 // it is a tentative definition.
914 if (_defines.count(u->getKey())) continue;
915 NameAndAttributes info = u->getValue();
916 _symbols.push_back(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000917 }
Bill Wendling9ee2d332012-03-29 08:27:32 +0000918
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000919 return false;
Nick Kledzik91a6dcf2008-02-27 22:25:36 +0000920}