blob: 91fcb2ee3d021e03bf73f0df740978d04c7af3c6 [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>
53DisableFPElimNonLeaf("disable-non-leaf-fp-elim",
54 cl::desc("Disable frame pointer elimination optimization for non-leaf funcs"),
55 cl::init(false));
56
57static cl::opt<bool>
58EnableUnsafeFPMath("enable-unsafe-fp-math",
59 cl::desc("Enable optimizations that may decrease FP precision"),
60 cl::init(false));
61
62static cl::opt<bool>
63EnableNoInfsFPMath("enable-no-infs-fp-math",
64 cl::desc("Enable FP math optimizations that assume no +-Infs"),
65 cl::init(false));
66
67static cl::opt<bool>
68EnableNoNaNsFPMath("enable-no-nans-fp-math",
69 cl::desc("Enable FP math optimizations that assume no NaNs"),
70 cl::init(false));
71
72static cl::opt<bool>
73EnableHonorSignDependentRoundingFPMath("enable-sign-dependent-rounding-fp-math",
74 cl::Hidden,
75 cl::desc("Force codegen to assume rounding mode can change dynamically"),
76 cl::init(false));
77
78static cl::opt<bool>
79GenerateSoftFloatCalls("soft-float",
80 cl::desc("Generate software floating point library calls"),
81 cl::init(false));
82
83static cl::opt<llvm::FloatABI::ABIType>
84FloatABIForCalls("float-abi",
85 cl::desc("Choose float ABI type"),
86 cl::init(FloatABI::Default),
87 cl::values(
88 clEnumValN(FloatABI::Default, "default",
89 "Target default float ABI type"),
90 clEnumValN(FloatABI::Soft, "soft",
91 "Soft float ABI (implied by -soft-float)"),
92 clEnumValN(FloatABI::Hard, "hard",
93 "Hard float ABI (uses FP registers)"),
94 clEnumValEnd));
95
96static cl::opt<llvm::FPOpFusion::FPOpFusionMode>
97FuseFPOps("fp-contract",
98 cl::desc("Enable aggresive formation of fused FP ops"),
99 cl::init(FPOpFusion::Standard),
100 cl::values(
101 clEnumValN(FPOpFusion::Fast, "fast",
102 "Fuse FP ops whenever profitable"),
103 clEnumValN(FPOpFusion::Standard, "on",
104 "Only fuse 'blessed' FP ops."),
105 clEnumValN(FPOpFusion::Strict, "off",
106 "Only fuse FP ops when the result won't be effected."),
107 clEnumValEnd));
108
109static cl::opt<bool>
110DontPlaceZerosInBSS("nozero-initialized-in-bss",
111 cl::desc("Don't place zero-initialized symbols into bss section"),
112 cl::init(false));
113
114static cl::opt<bool>
115EnableGuaranteedTailCallOpt("tailcallopt",
116 cl::desc("Turn fastcc calls into tail calls by (potentially) changing ABI."),
117 cl::init(false));
118
119static cl::opt<bool>
120DisableTailCalls("disable-tail-calls",
121 cl::desc("Never emit tail calls"),
122 cl::init(false));
123
124static cl::opt<unsigned>
125OverrideStackAlignment("stack-alignment",
126 cl::desc("Override default stack alignment"),
127 cl::init(0));
128
129static cl::opt<bool>
130EnableRealignStack("realign-stack",
131 cl::desc("Realign stack if needed"),
132 cl::init(true));
133
134static cl::opt<std::string>
135TrapFuncName("trap-func", cl::Hidden,
136 cl::desc("Emit a call to trap function rather than a trap instruction"),
137 cl::init(""));
138
139static cl::opt<bool>
140EnablePIE("enable-pie",
141 cl::desc("Assume the creation of a position independent executable."),
142 cl::init(false));
143
144static cl::opt<bool>
145SegmentedStacks("segmented-stacks",
146 cl::desc("Use segmented stacks if possible."),
147 cl::init(false));
148
149static cl::opt<bool>
150UseInitArray("use-init-array",
151 cl::desc("Use .init_array instead of .ctors."),
152 cl::init(false));
153
Chad Rosierd269bd82012-08-21 16:15:24 +0000154static cl::opt<unsigned>
155SSPBufferSize("stack-protector-buffer-size", cl::init(8),
156 cl::desc("Lower bound for a buffer to be considered for "
157 "stack protection"));
158
Bill Wendlingfb440502012-03-28 20:46:54 +0000159LTOModule::LTOModule(llvm::Module *m, llvm::TargetMachine *t)
160 : _module(m), _target(t),
161 _context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(), NULL),
Bill Wendling70b14002013-05-29 20:37:19 +0000162 _mangler(_context, t) {}
Bill Wendlingfb440502012-03-28 20:46:54 +0000163
164/// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM
165/// bitcode.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000166bool LTOModule::isBitcodeFile(const void *mem, size_t length) {
Rafael Espindola46ed3532013-06-11 18:05:26 +0000167 return sys::fs::identify_magic(StringRef((const char *)mem, length)) ==
168 sys::fs::file_magic::bitcode;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000169}
170
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000171bool LTOModule::isBitcodeFile(const char *path) {
172 return llvm::sys::Path(path).isBitcodeFile();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000173}
174
Bill Wendlingfb440502012-03-28 20:46:54 +0000175/// isBitcodeFileForTarget - Returns 'true' if the file (or memory contents) is
176/// LLVM bitcode for the specified triple.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000177bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length,
178 const char *triplePrefix) {
179 MemoryBuffer *buffer = makeBuffer(mem, length);
180 if (!buffer)
Nick Kledzikb481c202009-06-01 20:33:09 +0000181 return false;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000182 return isTargetMatch(buffer, triplePrefix);
Nick Kledzikb481c202009-06-01 20:33:09 +0000183}
184
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000185bool LTOModule::isBitcodeFileForTarget(const char *path,
186 const char *triplePrefix) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000187 OwningPtr<MemoryBuffer> buffer;
188 if (MemoryBuffer::getFile(path, buffer))
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000189 return false;
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000190 return isTargetMatch(buffer.take(), triplePrefix);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000191}
192
Bill Wendlingfb440502012-03-28 20:46:54 +0000193/// isTargetMatch - Returns 'true' if the memory buffer is for the specified
194/// target triple.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000195bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) {
Bill Wendling0198ce02010-10-06 01:22:42 +0000196 std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
197 delete buffer;
Bill Wendling5f689e72011-11-04 18:48:00 +0000198 return strncmp(Triple.c_str(), triplePrefix, strlen(triplePrefix)) == 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000199}
200
Bill Wendlingfb440502012-03-28 20:46:54 +0000201/// makeLTOModule - Create an LTOModule. N.B. These methods take ownership of
202/// the buffer.
203LTOModule *LTOModule::makeLTOModule(const char *path, std::string &errMsg) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000204 OwningPtr<MemoryBuffer> buffer;
205 if (error_code ec = MemoryBuffer::getFile(path, buffer)) {
Michael J. Spencerd4227232010-12-09 18:06:07 +0000206 errMsg = ec.message();
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000207 return NULL;
Michael J. Spencerd4227232010-12-09 18:06:07 +0000208 }
Rafael Espindola5b778b22011-03-18 19:51:00 +0000209 return makeLTOModule(buffer.take(), errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000210}
211
Rafael Espindola56e41f72011-02-08 22:40:47 +0000212LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
Bill Wendling5f689e72011-11-04 18:48:00 +0000213 size_t size, std::string &errMsg) {
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000214 return makeLTOModule(fd, path, size, size, 0, errMsg);
215}
216
217LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
218 size_t file_size,
219 size_t map_size,
220 off_t offset,
Rafael Espindola56e41f72011-02-08 22:40:47 +0000221 std::string &errMsg) {
222 OwningPtr<MemoryBuffer> buffer;
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000223 if (error_code ec = MemoryBuffer::getOpenFile(fd, path, buffer, file_size,
224 map_size, offset, false)) {
Rafael Espindola56e41f72011-02-08 22:40:47 +0000225 errMsg = ec.message();
226 return NULL;
227 }
Rafael Espindola5b778b22011-03-18 19:51:00 +0000228 return makeLTOModule(buffer.take(), errMsg);
Rafael Espindola56e41f72011-02-08 22:40:47 +0000229}
230
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000231LTOModule *LTOModule::makeLTOModule(const void *mem, size_t length,
232 std::string &errMsg) {
233 OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length));
234 if (!buffer)
235 return NULL;
Rafael Espindola5b778b22011-03-18 19:51:00 +0000236 return makeLTOModule(buffer.take(), errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000237}
238
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000239void LTOModule::getTargetOptions(TargetOptions &Options) {
240 Options.LessPreciseFPMADOption = EnableFPMAD;
241 Options.NoFramePointerElim = DisableFPElim;
242 Options.NoFramePointerElimNonLeaf = DisableFPElimNonLeaf;
243 Options.AllowFPOpFusion = FuseFPOps;
244 Options.UnsafeFPMath = EnableUnsafeFPMath;
245 Options.NoInfsFPMath = EnableNoInfsFPMath;
246 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
247 Options.HonorSignDependentRoundingFPMathOption =
248 EnableHonorSignDependentRoundingFPMath;
249 Options.UseSoftFloat = GenerateSoftFloatCalls;
250 if (FloatABIForCalls != FloatABI::Default)
251 Options.FloatABIType = FloatABIForCalls;
252 Options.NoZerosInBSS = DontPlaceZerosInBSS;
253 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
254 Options.DisableTailCalls = DisableTailCalls;
255 Options.StackAlignmentOverride = OverrideStackAlignment;
256 Options.RealignStack = EnableRealignStack;
257 Options.TrapFuncName = TrapFuncName;
258 Options.PositionIndependentExecutable = EnablePIE;
259 Options.EnableSegmentedStacks = SegmentedStacks;
260 Options.UseInitArray = UseInitArray;
Chad Rosierd269bd82012-08-21 16:15:24 +0000261 Options.SSPBufferSize = SSPBufferSize;
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000262}
263
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000264LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer,
265 std::string &errMsg) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000266 static bool Initialized = false;
267 if (!Initialized) {
268 InitializeAllTargets();
Evan Cheng8c886a42011-07-22 21:58:54 +0000269 InitializeAllTargetMCs();
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000270 InitializeAllAsmParsers();
271 Initialized = true;
272 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000273
274 // parse bitcode buffer
Rafael Espindola5b778b22011-03-18 19:51:00 +0000275 OwningPtr<Module> m(getLazyBitcodeModule(buffer, getGlobalContext(),
276 &errMsg));
277 if (!m) {
278 delete buffer;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000279 return NULL;
Rafael Espindola5b778b22011-03-18 19:51:00 +0000280 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000281
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000282 std::string TripleStr = m->getTargetTriple();
283 if (TripleStr.empty())
284 TripleStr = sys::getDefaultTargetTriple();
285 llvm::Triple Triple(TripleStr);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000286
287 // find machine architecture for this module
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000288 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000289 if (!march)
290 return NULL;
291
Nick Lewycky364c04a2011-04-21 01:54:08 +0000292 // construct LTOModule, hand over ownership of module and target
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000293 SubtargetFeatures Features;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000294 Features.getDefaultSubtargetFeatures(Triple);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000295 std::string FeatureStr = Features.getString();
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000296 // Set a default CPU for Darwin triples.
Evan Chengfe6e4052011-06-30 01:53:36 +0000297 std::string CPU;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000298 if (Triple.isOSDarwin()) {
299 if (Triple.getArch() == llvm::Triple::x86_64)
300 CPU = "core2";
301 else if (Triple.getArch() == llvm::Triple::x86)
302 CPU = "yonah";
303 }
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000304 TargetOptions Options;
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000305 getTargetOptions(Options);
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000306 TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000307 Options);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000308 LTOModule *Ret = new LTOModule(m.take(), target);
Bill Wendling7e58b382012-03-28 23:12:18 +0000309 if (Ret->parseSymbols(errMsg)) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000310 delete Ret;
311 return NULL;
312 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000313
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000314 return Ret;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000315}
316
Bill Wendlingfb440502012-03-28 20:46:54 +0000317/// makeBuffer - Create a MemoryBuffer from a memory range.
318MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length) {
Roman Divackyad06cee2012-09-05 22:26:57 +0000319 const char *startPtr = (const char*)mem;
Bill Wendlingfb440502012-03-28 20:46:54 +0000320 return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), "", false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000321}
322
Bill Wendlingfb440502012-03-28 20:46:54 +0000323/// objcClassNameFromExpression - Get string that the data pointer points to.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000324bool
325LTOModule::objcClassNameFromExpression(const Constant *c, std::string &name) {
326 if (const ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000327 Constant *op = ce->getOperand(0);
328 if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) {
329 Constant *cn = gvn->getInitializer();
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000330 if (ConstantDataArray *ca = dyn_cast<ConstantDataArray>(cn)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000331 if (ca->isCString()) {
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000332 name = ".objc_class_name_" + ca->getAsCString().str();
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000333 return true;
Nick Kledzikb481c202009-06-01 20:33:09 +0000334 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000335 }
Nick Kledzikb481c202009-06-01 20:33:09 +0000336 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000337 }
338 return false;
339}
340
Bill Wendlingfb440502012-03-28 20:46:54 +0000341/// addObjCClass - Parse i386/ppc ObjC class data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000342void LTOModule::addObjCClass(const GlobalVariable *clgv) {
343 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000344 if (!c) return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000345
Bill Wendling5f689e72011-11-04 18:48:00 +0000346 // second slot in __OBJC,__class is pointer to superclass name
347 std::string superclassName;
348 if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
349 NameAndAttributes info;
350 StringMap<NameAndAttributes>::value_type &entry =
351 _undefines.GetOrCreateValue(superclassName);
352 if (!entry.getValue().name) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000353 const char *symbolName = entry.getKey().data();
354 info.name = symbolName;
355 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000356 info.isFunction = false;
357 info.symbol = clgv;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000358 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000359 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000360 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000361
362 // third slot in __OBJC,__class is pointer to class name
363 std::string className;
364 if (objcClassNameFromExpression(c->getOperand(2), className)) {
365 StringSet::value_type &entry = _defines.GetOrCreateValue(className);
366 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000367
Bill Wendling5f689e72011-11-04 18:48:00 +0000368 NameAndAttributes info;
369 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000370 info.attributes = LTO_SYMBOL_PERMISSIONS_DATA |
371 LTO_SYMBOL_DEFINITION_REGULAR | LTO_SYMBOL_SCOPE_DEFAULT;
372 info.isFunction = false;
373 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000374 _symbols.push_back(info);
375 }
376}
377
Bill Wendlingfb440502012-03-28 20:46:54 +0000378/// addObjCCategory - Parse i386/ppc ObjC category data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000379void LTOModule::addObjCCategory(const GlobalVariable *clgv) {
380 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000381 if (!c) return;
382
383 // second slot in __OBJC,__category is pointer to target class name
384 std::string targetclassName;
385 if (!objcClassNameFromExpression(c->getOperand(1), targetclassName))
386 return;
387
388 NameAndAttributes info;
389 StringMap<NameAndAttributes>::value_type &entry =
390 _undefines.GetOrCreateValue(targetclassName);
391
392 if (entry.getValue().name)
393 return;
394
395 const char *symbolName = entry.getKey().data();
396 info.name = symbolName;
397 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000398 info.isFunction = false;
399 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000400 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000401}
402
Bill Wendlingfb440502012-03-28 20:46:54 +0000403/// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000404void LTOModule::addObjCClassRef(const GlobalVariable *clgv) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000405 std::string targetclassName;
Bill Wendling5f689e72011-11-04 18:48:00 +0000406 if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName))
407 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000408
Bill Wendling5f689e72011-11-04 18:48:00 +0000409 NameAndAttributes info;
410 StringMap<NameAndAttributes>::value_type &entry =
411 _undefines.GetOrCreateValue(targetclassName);
412 if (entry.getValue().name)
413 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000414
Bill Wendling5f689e72011-11-04 18:48:00 +0000415 const char *symbolName = entry.getKey().data();
416 info.name = symbolName;
417 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000418 info.isFunction = false;
419 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000420 entry.setValue(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000421}
422
Bill Wendlingfb440502012-03-28 20:46:54 +0000423/// addDefinedDataSymbol - Add a data symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000424void LTOModule::addDefinedDataSymbol(const GlobalValue *v) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000425 // Add to list of defined symbols.
Bill Wendlinga2af6742011-11-04 09:30:19 +0000426 addDefinedSymbol(v, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000427
Bill Wendling45f74e32012-08-06 22:52:45 +0000428 if (!v->hasSection() /* || !isTargetDarwin */)
429 return;
430
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000431 // Special case i386/ppc ObjC data structures in magic sections:
432 // The issue is that the old ObjC object format did some strange
433 // contortions to avoid real linker symbols. For instance, the
434 // ObjC class data structure is allocated statically in the executable
435 // that defines that class. That data structures contains a pointer to
436 // its superclass. But instead of just initializing that part of the
437 // struct to the address of its superclass, and letting the static and
438 // dynamic linkers do the rest, the runtime works by having that field
439 // instead point to a C-string that is the name of the superclass.
440 // At runtime the objc initialization updates that pointer and sets
441 // it to point to the actual super class. As far as the linker
442 // knows it is just a pointer to a string. But then someone wanted the
443 // linker to issue errors at build time if the superclass was not found.
444 // So they figured out a way in mach-o object format to use an absolute
445 // symbols (.objc_class_name_Foo = 0) and a floating reference
446 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
447 // a class was missing.
448 // The following synthesizes the implicit .objc_* symbols for the linker
449 // from the ObjC data structures generated by the front end.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000450
Bill Wendling45f74e32012-08-06 22:52:45 +0000451 // special case if this data blob is an ObjC class definition
452 if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000453 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000454 addObjCClass(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000455 }
Bill Wendling45f74e32012-08-06 22:52:45 +0000456 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000457
Bill Wendling45f74e32012-08-06 22:52:45 +0000458 // special case if this data blob is an ObjC category definition
459 else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000460 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000461 addObjCCategory(gv);
462 }
463 }
464
465 // special case if this data blob is the list of referenced classes
466 else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000467 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000468 addObjCClassRef(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000469 }
470 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000471}
472
Bill Wendlingfb440502012-03-28 20:46:54 +0000473/// addDefinedFunctionSymbol - Add a function symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000474void LTOModule::addDefinedFunctionSymbol(const Function *f) {
Bill Wendlingfb440502012-03-28 20:46:54 +0000475 // add to list of defined symbols
476 addDefinedSymbol(f, true);
477}
478
479/// addDefinedSymbol - Add a defined symbol to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000480void LTOModule::addDefinedSymbol(const GlobalValue *def, bool isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000481 // ignore all llvm.* symbols
482 if (def->getName().startswith("llvm."))
483 return;
484
485 // string is owned by _defines
Rafael Espindola34b59382011-02-11 05:23:09 +0000486 SmallString<64> Buffer;
Bill Wendlinga2af6742011-11-04 09:30:19 +0000487 _mangler.getNameWithPrefix(Buffer, def, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000488
489 // set alignment part log2() can have rounding errors
490 uint32_t align = def->getAlignment();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000491 uint32_t attr = align ? countTrailingZeros(def->getAlignment()) : 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000492
493 // set permissions part
Bill Wendling9ee2d332012-03-29 08:27:32 +0000494 if (isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000495 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000496 } else {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000497 const GlobalVariable *gv = dyn_cast<GlobalVariable>(def);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000498 if (gv && gv->isConstant())
499 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
500 else
501 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
502 }
503
504 // set definition part
Bill Wendling2776d462010-09-27 18:05:19 +0000505 if (def->hasWeakLinkage() || def->hasLinkOnceLinkage() ||
Bill Wendling34bc34e2012-08-17 18:33:14 +0000506 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000507 attr |= LTO_SYMBOL_DEFINITION_WEAK;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000508 else if (def->hasCommonLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000509 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000510 else
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000511 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000512
513 // set scope part
514 if (def->hasHiddenVisibility())
515 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
516 else if (def->hasProtectedVisibility())
517 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000518 else if (def->hasExternalLinkage() || def->hasWeakLinkage() ||
519 def->hasLinkOnceLinkage() || def->hasCommonLinkage() ||
520 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000521 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
Bill Wendling34bc34e2012-08-17 18:33:14 +0000522 else if (def->hasLinkOnceODRAutoHideLinkage())
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000523 attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000524 else
525 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
526
Chad Rosier772a91f2011-06-28 18:26:12 +0000527 StringSet::value_type &entry = _defines.GetOrCreateValue(Buffer);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000528 entry.setValue(1);
529
Bill Wendling9ee2d332012-03-29 08:27:32 +0000530 // fill information structure
531 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000532 StringRef Name = entry.getKey();
533 info.name = Name.data();
534 assert(info.name[Name.size()] == '\0');
Bill Wendling9ee2d332012-03-29 08:27:32 +0000535 info.attributes = attr;
536 info.isFunction = isFunction;
537 info.symbol = def;
538
539 // add to table of symbols
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000540 _symbols.push_back(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000541}
542
Bill Wendlingfb440502012-03-28 20:46:54 +0000543/// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
544/// defined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000545void LTOModule::addAsmGlobalSymbol(const char *name,
546 lto_symbol_attributes scope) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000547 StringSet::value_type &entry = _defines.GetOrCreateValue(name);
548
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000549 // only add new define if not already defined
Rafael Espindola477d11f2011-02-20 16:27:25 +0000550 if (entry.getValue())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000551 return;
552
Rafael Espindola477d11f2011-02-20 16:27:25 +0000553 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000554
555 NameAndAttributes &info = _undefines[entry.getKey().data()];
556
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000557 if (info.symbol == 0) {
Bill Wendling71b19bb2012-04-02 10:01:21 +0000558 // FIXME: This is trying to take care of module ASM like this:
559 //
560 // module asm ".zerofill __FOO, __foo, _bar_baz_qux, 0"
561 //
562 // but is gross and its mother dresses it funny. Have the ASM parser give us
563 // more details for this type of situation so that we're not guessing so
564 // much.
565
566 // fill information structure
Rafael Espindola5f4b32f2012-05-11 03:42:13 +0000567 info.name = entry.getKey().data();
Bill Wendling71b19bb2012-04-02 10:01:21 +0000568 info.attributes =
569 LTO_SYMBOL_PERMISSIONS_DATA | LTO_SYMBOL_DEFINITION_REGULAR | scope;
570 info.isFunction = false;
571 info.symbol = 0;
572
573 // add to table of symbols
574 _symbols.push_back(info);
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000575 return;
576 }
577
Bill Wendling9ee2d332012-03-29 08:27:32 +0000578 if (info.isFunction)
579 addDefinedFunctionSymbol(cast<Function>(info.symbol));
580 else
581 addDefinedDataSymbol(info.symbol);
Bill Wendling8f6c8a92012-03-30 23:26:06 +0000582
583 _symbols.back().attributes &= ~LTO_SYMBOL_SCOPE_MASK;
584 _symbols.back().attributes |= scope;
Devang Patela59fe952008-07-16 18:06:52 +0000585}
Nick Kledzik07b4a622008-02-26 20:26:43 +0000586
Bill Wendlingfb440502012-03-28 20:46:54 +0000587/// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the
588/// undefined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000589void LTOModule::addAsmGlobalSymbolUndef(const char *name) {
590 StringMap<NameAndAttributes>::value_type &entry =
591 _undefines.GetOrCreateValue(name);
592
593 _asm_undefines.push_back(entry.getKey().data());
594
595 // we already have the symbol
596 if (entry.getValue().name)
597 return;
598
599 uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;;
600 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
601 NameAndAttributes info;
602 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000603 info.attributes = attr;
604 info.isFunction = false;
605 info.symbol = 0;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000606
607 entry.setValue(info);
608}
609
Bill Wendlingfb440502012-03-28 20:46:54 +0000610/// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet to a
611/// list to be resolved later.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000612void
613LTOModule::addPotentialUndefinedSymbol(const GlobalValue *decl, bool isFunc) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000614 // ignore all llvm.* symbols
615 if (decl->getName().startswith("llvm."))
616 return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000617
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000618 // ignore all aliases
619 if (isa<GlobalAlias>(decl))
620 return;
Nick Lewycky0661b932009-07-09 06:03:04 +0000621
Rafael Espindola34b59382011-02-11 05:23:09 +0000622 SmallString<64> name;
Bill Wendlinga2af6742011-11-04 09:30:19 +0000623 _mangler.getNameWithPrefix(name, decl, false);
Rafael Espindola56548522009-04-24 16:55:21 +0000624
Rafael Espindola477d11f2011-02-20 16:27:25 +0000625 StringMap<NameAndAttributes>::value_type &entry =
Chad Rosier772a91f2011-06-28 18:26:12 +0000626 _undefines.GetOrCreateValue(name);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000627
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000628 // we already have the symbol
Rafael Espindola477d11f2011-02-20 16:27:25 +0000629 if (entry.getValue().name)
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000630 return;
Rafael Espindola56548522009-04-24 16:55:21 +0000631
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000632 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000633
634 info.name = entry.getKey().data();
Bill Wendlingfb440502012-03-28 20:46:54 +0000635
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000636 if (decl->hasExternalWeakLinkage())
637 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
638 else
639 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000640
Bill Wendling9ee2d332012-03-29 08:27:32 +0000641 info.isFunction = isFunc;
642 info.symbol = decl;
643
Rafael Espindola477d11f2011-02-20 16:27:25 +0000644 entry.setValue(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000645}
646
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000647namespace {
648 class RecordStreamer : public MCStreamer {
649 public:
Bill Wendling32867652012-04-03 03:56:52 +0000650 enum State { NeverSeen, Global, Defined, DefinedGlobal, Used };
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000651
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000652 private:
653 StringMap<State> Symbols;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000654
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000655 void markDefined(const MCSymbol &Symbol) {
656 State &S = Symbols[Symbol.getName()];
657 switch (S) {
658 case DefinedGlobal:
659 case Global:
660 S = DefinedGlobal;
661 break;
662 case NeverSeen:
663 case Defined:
664 case Used:
665 S = Defined;
666 break;
667 }
668 }
669 void markGlobal(const MCSymbol &Symbol) {
670 State &S = Symbols[Symbol.getName()];
671 switch (S) {
672 case DefinedGlobal:
673 case Defined:
674 S = DefinedGlobal;
675 break;
676
677 case NeverSeen:
678 case Global:
679 case Used:
680 S = Global;
681 break;
682 }
683 }
684 void markUsed(const MCSymbol &Symbol) {
685 State &S = Symbols[Symbol.getName()];
686 switch (S) {
687 case DefinedGlobal:
688 case Defined:
689 case Global:
690 break;
691
692 case NeverSeen:
693 case Used:
694 S = Used;
695 break;
696 }
697 }
698
699 // FIXME: mostly copied for the obj streamer.
700 void AddValueSymbols(const MCExpr *Value) {
701 switch (Value->getKind()) {
702 case MCExpr::Target:
703 // FIXME: What should we do in here?
704 break;
705
706 case MCExpr::Constant:
707 break;
708
709 case MCExpr::Binary: {
710 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
711 AddValueSymbols(BE->getLHS());
712 AddValueSymbols(BE->getRHS());
713 break;
714 }
715
716 case MCExpr::SymbolRef:
717 markUsed(cast<MCSymbolRefExpr>(Value)->getSymbol());
718 break;
719
720 case MCExpr::Unary:
721 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
722 break;
723 }
724 }
725
726 public:
727 typedef StringMap<State>::const_iterator const_iterator;
728
729 const_iterator begin() {
730 return Symbols.begin();
731 }
732
733 const_iterator end() {
734 return Symbols.end();
735 }
736
Chandler Carruthde093ef2013-01-31 23:29:57 +0000737 RecordStreamer(MCContext &Context)
738 : MCStreamer(SK_RecordStreamer, Context) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000739
Bill Wendling32867652012-04-03 03:56:52 +0000740 virtual void EmitInstruction(const MCInst &Inst) {
741 // Scan for values.
742 for (unsigned i = Inst.getNumOperands(); i--; )
743 if (Inst.getOperand(i).isExpr())
744 AddValueSymbols(Inst.getOperand(i).getExpr());
745 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000746 virtual void EmitLabel(MCSymbol *Symbol) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000747 Symbol->setSection(*getCurrentSection().first);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000748 markDefined(*Symbol);
749 }
Reed Kotleraee4d5d12012-12-16 04:00:45 +0000750 virtual void EmitDebugLabel(MCSymbol *Symbol) {
751 EmitLabel(Symbol);
752 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000753 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
754 // FIXME: should we handle aliases?
755 markDefined(*Symbol);
756 }
757 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
758 if (Attribute == MCSA_Global)
759 markGlobal(*Symbol);
760 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000761 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Cheng95847992012-06-22 20:30:39 +0000762 uint64_t Size , unsigned ByteAlignment) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000763 markDefined(*Symbol);
764 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000765 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
766 unsigned ByteAlignment) {
767 markDefined(*Symbol);
768 }
Bill Wendling32867652012-04-03 03:56:52 +0000769
Eli Benderskyf483ff92012-12-20 19:05:53 +0000770 virtual void EmitBundleAlignMode(unsigned AlignPow2) {}
Eli Bendersky802b6282013-01-07 21:51:08 +0000771 virtual void EmitBundleLock(bool AlignToEnd) {}
Eli Benderskyf483ff92012-12-20 19:05:53 +0000772 virtual void EmitBundleUnlock() {}
773
Bill Wendling32867652012-04-03 03:56:52 +0000774 // Noop calls.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000775 virtual void ChangeSection(const MCSection *Section,
776 const MCExpr *Subsection) {}
Eli Benderskycbb25142013-01-14 19:04:57 +0000777 virtual void InitToTextSection() {}
Bill Wendling32867652012-04-03 03:56:52 +0000778 virtual void InitSections() {}
779 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {}
780 virtual void EmitThumbFunc(MCSymbol *Func) {}
781 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {}
782 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {}
783 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {}
784 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {}
785 virtual void EmitCOFFSymbolType(int Type) {}
786 virtual void EndCOFFSymbolDef() {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000787 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {}
Benjamin Kramer63970512011-09-01 23:04:27 +0000788 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
789 unsigned ByteAlignment) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000790 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
791 uint64_t Size, unsigned ByteAlignment) {}
792 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) {}
793 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
Rafael Espindolafd057852011-05-01 03:50:49 +0000794 unsigned AddrSpace) {}
Rafael Espindola6aea5922011-04-21 23:39:26 +0000795 virtual void EmitULEB128Value(const MCExpr *Value) {}
796 virtual void EmitSLEB128Value(const MCExpr *Value) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000797 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
798 unsigned ValueSize,
799 unsigned MaxBytesToEmit) {}
800 virtual void EmitCodeAlignment(unsigned ByteAlignment,
801 unsigned MaxBytesToEmit) {}
Jim Grosbachb5912772012-01-27 00:37:08 +0000802 virtual bool EmitValueToOffset(const MCExpr *Offset,
803 unsigned char Value ) { return false; }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000804 virtual void EmitFileDirective(StringRef Filename) {}
805 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
806 const MCSymbol *LastLabel,
Evan Chengc7ac6902011-07-14 05:43:07 +0000807 const MCSymbol *Label,
808 unsigned PointerSize) {}
Rafael Espindola07082092012-01-07 03:13:18 +0000809 virtual void FinishImpl() {}
Chandler Carruthde093ef2013-01-31 23:29:57 +0000810
811 static bool classof(const MCStreamer *S) {
Chandler Carruth30cfaa22013-01-31 23:34:47 +0000812 return S->getKind() == SK_RecordStreamer;
Chandler Carruthde093ef2013-01-31 23:29:57 +0000813 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000814 };
Bill Wendling9ee2d332012-03-29 08:27:32 +0000815} // end anonymous namespace
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000816
Bill Wendlingfb440502012-03-28 20:46:54 +0000817/// addAsmGlobalSymbols - Add global symbols from module-level ASM to the
818/// defined or undefined lists.
Bill Wendlingac2abde2011-11-04 09:24:40 +0000819bool LTOModule::addAsmGlobalSymbols(std::string &errMsg) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000820 const std::string &inlineAsm = _module->getModuleInlineAsm();
Ivan Krasincc2a8012011-09-08 07:38:25 +0000821 if (inlineAsm.empty())
822 return false;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000823
Bill Wendlingac2abde2011-11-04 09:24:40 +0000824 OwningPtr<RecordStreamer> Streamer(new RecordStreamer(_context));
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000825 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(inlineAsm);
826 SourceMgr SrcMgr;
827 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach345768c2011-08-16 18:33:49 +0000828 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr,
Bill Wendlingac2abde2011-11-04 09:24:40 +0000829 _context, *Streamer,
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000830 *_target->getMCAsmInfo()));
Bill Wendling9351b3e2012-08-08 22:01:55 +0000831 const Target &T = _target->getTarget();
832 OwningPtr<MCSubtargetInfo>
833 STI(T.createMCSubtargetInfo(_target->getTargetTriple(),
834 _target->getTargetCPU(),
835 _target->getTargetFeatureString()));
836 OwningPtr<MCTargetAsmParser> TAP(T.createMCAsmParser(*STI, *Parser.get()));
Ivan Krasin8149dd62011-09-08 07:36:39 +0000837 if (!TAP) {
Bill Wendling9351b3e2012-08-08 22:01:55 +0000838 errMsg = "target " + std::string(T.getName()) +
839 " does not define AsmParser.";
Ivan Krasin8149dd62011-09-08 07:36:39 +0000840 return true;
841 }
842
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000843 Parser->setTargetParser(*TAP);
Bill Wendling9351b3e2012-08-08 22:01:55 +0000844 if (Parser->Run(false))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000845 return true;
846
847 for (RecordStreamer::const_iterator i = Streamer->begin(),
848 e = Streamer->end(); i != e; ++i) {
849 StringRef Key = i->first();
850 RecordStreamer::State Value = i->second;
851 if (Value == RecordStreamer::DefinedGlobal)
852 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_DEFAULT);
853 else if (Value == RecordStreamer::Defined)
854 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_INTERNAL);
855 else if (Value == RecordStreamer::Global ||
856 Value == RecordStreamer::Used)
857 addAsmGlobalSymbolUndef(Key.data());
858 }
Bill Wendling9351b3e2012-08-08 22:01:55 +0000859
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000860 return false;
861}
862
Bill Wendlingfb440502012-03-28 20:46:54 +0000863/// isDeclaration - Return 'true' if the global value is a declaration.
Rafael Espindola5b778b22011-03-18 19:51:00 +0000864static bool isDeclaration(const GlobalValue &V) {
865 if (V.hasAvailableExternallyLinkage())
866 return true;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000867
Rafael Espindola5b778b22011-03-18 19:51:00 +0000868 if (V.isMaterializable())
869 return false;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000870
Rafael Espindola5b778b22011-03-18 19:51:00 +0000871 return V.isDeclaration();
872}
873
Bill Wendling7e58b382012-03-28 23:12:18 +0000874/// parseSymbols - Parse the symbols from the module and model-level ASM and add
Bill Wendlingfb440502012-03-28 20:46:54 +0000875/// them to either the defined or undefined lists.
Bill Wendling7e58b382012-03-28 23:12:18 +0000876bool LTOModule::parseSymbols(std::string &errMsg) {
Daniel Dunbar919660b2010-08-10 23:46:46 +0000877 // add functions
Bill Wendling763acfc2012-03-29 03:34:57 +0000878 for (Module::iterator f = _module->begin(), e = _module->end(); f != e; ++f) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000879 if (isDeclaration(*f))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000880 addPotentialUndefinedSymbol(f, true);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000881 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000882 addDefinedFunctionSymbol(f);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000883 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000884
Daniel Dunbar919660b2010-08-10 23:46:46 +0000885 // add data
886 for (Module::global_iterator v = _module->global_begin(),
887 e = _module->global_end(); v != e; ++v) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000888 if (isDeclaration(*v))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000889 addPotentialUndefinedSymbol(v, false);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000890 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000891 addDefinedDataSymbol(v);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000892 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000893
Daniel Dunbar919660b2010-08-10 23:46:46 +0000894 // add asm globals
Bill Wendlingac2abde2011-11-04 09:24:40 +0000895 if (addAsmGlobalSymbols(errMsg))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000896 return true;
Daniel Dunbar919660b2010-08-10 23:46:46 +0000897
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000898 // add aliases
Bill Wendling9ee2d332012-03-29 08:27:32 +0000899 for (Module::alias_iterator a = _module->alias_begin(),
900 e = _module->alias_end(); a != e; ++a) {
901 if (isDeclaration(*a->getAliasedGlobal()))
Bill Wendlingd58ed732012-03-28 20:48:49 +0000902 // Is an alias to a declaration.
Bill Wendling9ee2d332012-03-29 08:27:32 +0000903 addPotentialUndefinedSymbol(a, false);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000904 else
Bill Wendling9ee2d332012-03-29 08:27:32 +0000905 addDefinedDataSymbol(a);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000906 }
907
Daniel Dunbar919660b2010-08-10 23:46:46 +0000908 // make symbols for all undefines
Bill Wendling9ee2d332012-03-29 08:27:32 +0000909 for (StringMap<NameAndAttributes>::iterator u =_undefines.begin(),
910 e = _undefines.end(); u != e; ++u) {
911 // If this symbol also has a definition, then don't make an undefine because
912 // it is a tentative definition.
913 if (_defines.count(u->getKey())) continue;
914 NameAndAttributes info = u->getValue();
915 _symbols.push_back(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000916 }
Bill Wendling9ee2d332012-03-29 08:27:32 +0000917
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000918 return false;
Nick Kledzik91a6dcf2008-02-27 22:25:36 +0000919}