blob: 5a388cbad6f777e0282b9bdc845aa3e4eb34a64d [file] [log] [blame]
Chris Lattner5ef31a02010-03-12 18:44:54 +00001//===-- LTOModule.cpp - LLVM Link Time Optimizer --------------------------===//
Nick Kledzik77595fc2008-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 Dunbarb06913d2010-08-10 23:46:39 +00007//
Nick Kledzik77595fc2008-02-26 20:26:43 +00008//===----------------------------------------------------------------------===//
9//
Daniel Dunbarb06913d2010-08-10 23:46:39 +000010// This file implements the Link Time Optimization library. This library is
Nick Kledzik77595fc2008-02-26 20:26:43 +000011// intended to be used by linker to optimize code at link time.
12//
13//===----------------------------------------------------------------------===//
14
Nick Kledzikef194ed2008-02-27 22:25:36 +000015#include "LTOModule.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000016#include "llvm/ADT/OwningPtr.h"
17#include "llvm/ADT/Triple.h"
18#include "llvm/Bitcode/ReaderWriter.h"
Nick Kledzik3eb445f2009-06-01 20:33:09 +000019#include "llvm/Constants.h"
Owen Anderson8b477ed2009-07-01 16:58:40 +000020#include "llvm/LLVMContext.h"
Rafael Espindola38c4e532011-03-02 04:14:42 +000021#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCInst.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000023#include "llvm/MC/MCParser/MCAsmParser.h"
Rafael Espindola38c4e532011-03-02 04:14:42 +000024#include "llvm/MC/MCStreamer.h"
Evan Chengffc0e732011-07-09 05:47:46 +000025#include "llvm/MC/MCSubtargetInfo.h"
Rafael Espindola38c4e532011-03-02 04:14:42 +000026#include "llvm/MC/MCSymbol.h"
Evan Cheng94b95502011-07-26 00:24:13 +000027#include "llvm/MC/MCTargetAsmParser.h"
Bill Wendling5ff4bc22012-03-30 23:26:06 +000028#include "llvm/MC/SubtargetFeature.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000029#include "llvm/Module.h"
Bill Wendling9ac0aaa2012-08-06 21:34:54 +000030#include "llvm/Support/CommandLine.h"
Bill Wendling5ff4bc22012-03-30 23:26:06 +000031#include "llvm/Support/Host.h"
Bill Wendling5ff4bc22012-03-30 23:26:06 +000032#include "llvm/Support/MemoryBuffer.h"
33#include "llvm/Support/Path.h"
Bill Wendling5ff4bc22012-03-30 23:26:06 +000034#include "llvm/Support/SourceMgr.h"
Bill Wendling5ff4bc22012-03-30 23:26:06 +000035#include "llvm/Support/TargetRegistry.h"
36#include "llvm/Support/TargetSelect.h"
37#include "llvm/Support/system_error.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000038#include "llvm/Target/TargetRegisterInfo.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000039using namespace llvm;
40
Bill Wendling9ac0aaa2012-08-06 21:34:54 +000041static cl::opt<bool>
42EnableFPMAD("enable-fp-mad",
43 cl::desc("Enable less precise MAD instructions to be generated"),
44 cl::init(false));
45
46static cl::opt<bool>
47DisableFPElim("disable-fp-elim",
48 cl::desc("Disable frame pointer elimination optimization"),
49 cl::init(false));
50
51static cl::opt<bool>
52DisableFPElimNonLeaf("disable-non-leaf-fp-elim",
53 cl::desc("Disable frame pointer elimination optimization for non-leaf funcs"),
54 cl::init(false));
55
56static cl::opt<bool>
57EnableUnsafeFPMath("enable-unsafe-fp-math",
58 cl::desc("Enable optimizations that may decrease FP precision"),
59 cl::init(false));
60
61static cl::opt<bool>
62EnableNoInfsFPMath("enable-no-infs-fp-math",
63 cl::desc("Enable FP math optimizations that assume no +-Infs"),
64 cl::init(false));
65
66static cl::opt<bool>
67EnableNoNaNsFPMath("enable-no-nans-fp-math",
68 cl::desc("Enable FP math optimizations that assume no NaNs"),
69 cl::init(false));
70
71static cl::opt<bool>
72EnableHonorSignDependentRoundingFPMath("enable-sign-dependent-rounding-fp-math",
73 cl::Hidden,
74 cl::desc("Force codegen to assume rounding mode can change dynamically"),
75 cl::init(false));
76
77static cl::opt<bool>
78GenerateSoftFloatCalls("soft-float",
79 cl::desc("Generate software floating point library calls"),
80 cl::init(false));
81
82static cl::opt<llvm::FloatABI::ABIType>
83FloatABIForCalls("float-abi",
84 cl::desc("Choose float ABI type"),
85 cl::init(FloatABI::Default),
86 cl::values(
87 clEnumValN(FloatABI::Default, "default",
88 "Target default float ABI type"),
89 clEnumValN(FloatABI::Soft, "soft",
90 "Soft float ABI (implied by -soft-float)"),
91 clEnumValN(FloatABI::Hard, "hard",
92 "Hard float ABI (uses FP registers)"),
93 clEnumValEnd));
94
95static cl::opt<llvm::FPOpFusion::FPOpFusionMode>
96FuseFPOps("fp-contract",
97 cl::desc("Enable aggresive formation of fused FP ops"),
98 cl::init(FPOpFusion::Standard),
99 cl::values(
100 clEnumValN(FPOpFusion::Fast, "fast",
101 "Fuse FP ops whenever profitable"),
102 clEnumValN(FPOpFusion::Standard, "on",
103 "Only fuse 'blessed' FP ops."),
104 clEnumValN(FPOpFusion::Strict, "off",
105 "Only fuse FP ops when the result won't be effected."),
106 clEnumValEnd));
107
108static cl::opt<bool>
109DontPlaceZerosInBSS("nozero-initialized-in-bss",
110 cl::desc("Don't place zero-initialized symbols into bss section"),
111 cl::init(false));
112
113static cl::opt<bool>
114EnableGuaranteedTailCallOpt("tailcallopt",
115 cl::desc("Turn fastcc calls into tail calls by (potentially) changing ABI."),
116 cl::init(false));
117
118static cl::opt<bool>
119DisableTailCalls("disable-tail-calls",
120 cl::desc("Never emit tail calls"),
121 cl::init(false));
122
123static cl::opt<unsigned>
124OverrideStackAlignment("stack-alignment",
125 cl::desc("Override default stack alignment"),
126 cl::init(0));
127
128static cl::opt<bool>
129EnableRealignStack("realign-stack",
130 cl::desc("Realign stack if needed"),
131 cl::init(true));
132
133static cl::opt<std::string>
134TrapFuncName("trap-func", cl::Hidden,
135 cl::desc("Emit a call to trap function rather than a trap instruction"),
136 cl::init(""));
137
138static cl::opt<bool>
139EnablePIE("enable-pie",
140 cl::desc("Assume the creation of a position independent executable."),
141 cl::init(false));
142
143static cl::opt<bool>
144SegmentedStacks("segmented-stacks",
145 cl::desc("Use segmented stacks if possible."),
146 cl::init(false));
147
148static cl::opt<bool>
149UseInitArray("use-init-array",
150 cl::desc("Use .init_array instead of .ctors."),
151 cl::init(false));
152
Chad Rosier35907e92012-08-21 16:15:24 +0000153static cl::opt<unsigned>
154SSPBufferSize("stack-protector-buffer-size", cl::init(8),
155 cl::desc("Lower bound for a buffer to be considered for "
156 "stack protection"));
157
Bill Wendling62cf01e2012-03-28 20:46:54 +0000158LTOModule::LTOModule(llvm::Module *m, llvm::TargetMachine *t)
159 : _module(m), _target(t),
160 _context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(), NULL),
Micah Villmow791cfc22012-10-08 16:39:34 +0000161 _mangler(_context, *_target->getDataLayout()) {}
Bill Wendling62cf01e2012-03-28 20:46:54 +0000162
163/// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM
164/// bitcode.
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000165bool LTOModule::isBitcodeFile(const void *mem, size_t length) {
Roman Divacky59324292012-09-05 22:26:57 +0000166 return llvm::sys::IdentifyFileType((const char*)mem, length)
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000167 == llvm::sys::Bitcode_FileType;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000168}
169
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000170bool LTOModule::isBitcodeFile(const char *path) {
171 return llvm::sys::Path(path).isBitcodeFile();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000172}
173
Bill Wendling62cf01e2012-03-28 20:46:54 +0000174/// isBitcodeFileForTarget - Returns 'true' if the file (or memory contents) is
175/// LLVM bitcode for the specified triple.
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000176bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length,
177 const char *triplePrefix) {
178 MemoryBuffer *buffer = makeBuffer(mem, length);
179 if (!buffer)
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000180 return false;
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000181 return isTargetMatch(buffer, triplePrefix);
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000182}
183
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000184bool LTOModule::isBitcodeFileForTarget(const char *path,
185 const char *triplePrefix) {
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000186 OwningPtr<MemoryBuffer> buffer;
187 if (MemoryBuffer::getFile(path, buffer))
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000188 return false;
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000189 return isTargetMatch(buffer.take(), triplePrefix);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000190}
191
Bill Wendling62cf01e2012-03-28 20:46:54 +0000192/// isTargetMatch - Returns 'true' if the memory buffer is for the specified
193/// target triple.
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000194bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) {
Bill Wendling34711742010-10-06 01:22:42 +0000195 std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
196 delete buffer;
Bill Wendling931d4c22011-11-04 18:48:00 +0000197 return strncmp(Triple.c_str(), triplePrefix, strlen(triplePrefix)) == 0;
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000198}
199
Bill Wendling62cf01e2012-03-28 20:46:54 +0000200/// makeLTOModule - Create an LTOModule. N.B. These methods take ownership of
201/// the buffer.
202LTOModule *LTOModule::makeLTOModule(const char *path, std::string &errMsg) {
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000203 OwningPtr<MemoryBuffer> buffer;
204 if (error_code ec = MemoryBuffer::getFile(path, buffer)) {
Michael J. Spencerf2f516f2010-12-09 18:06:07 +0000205 errMsg = ec.message();
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000206 return NULL;
Michael J. Spencerf2f516f2010-12-09 18:06:07 +0000207 }
Rafael Espindolaf19d7a72011-03-18 19:51:00 +0000208 return makeLTOModule(buffer.take(), errMsg);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000209}
210
Rafael Espindolab4cc0312011-02-08 22:40:47 +0000211LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
Bill Wendling931d4c22011-11-04 18:48:00 +0000212 size_t size, std::string &errMsg) {
Rafael Espindolaf21b1052011-03-17 00:36:11 +0000213 return makeLTOModule(fd, path, size, size, 0, errMsg);
214}
215
216LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
217 size_t file_size,
218 size_t map_size,
219 off_t offset,
Rafael Espindolab4cc0312011-02-08 22:40:47 +0000220 std::string &errMsg) {
221 OwningPtr<MemoryBuffer> buffer;
Rafael Espindolaf21b1052011-03-17 00:36:11 +0000222 if (error_code ec = MemoryBuffer::getOpenFile(fd, path, buffer, file_size,
223 map_size, offset, false)) {
Rafael Espindolab4cc0312011-02-08 22:40:47 +0000224 errMsg = ec.message();
225 return NULL;
226 }
Rafael Espindolaf19d7a72011-03-18 19:51:00 +0000227 return makeLTOModule(buffer.take(), errMsg);
Rafael Espindolab4cc0312011-02-08 22:40:47 +0000228}
229
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000230LTOModule *LTOModule::makeLTOModule(const void *mem, size_t length,
231 std::string &errMsg) {
232 OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length));
233 if (!buffer)
234 return NULL;
Rafael Espindolaf19d7a72011-03-18 19:51:00 +0000235 return makeLTOModule(buffer.take(), errMsg);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000236}
237
Bill Wendling9ac0aaa2012-08-06 21:34:54 +0000238void LTOModule::getTargetOptions(TargetOptions &Options) {
239 Options.LessPreciseFPMADOption = EnableFPMAD;
240 Options.NoFramePointerElim = DisableFPElim;
241 Options.NoFramePointerElimNonLeaf = DisableFPElimNonLeaf;
242 Options.AllowFPOpFusion = FuseFPOps;
243 Options.UnsafeFPMath = EnableUnsafeFPMath;
244 Options.NoInfsFPMath = EnableNoInfsFPMath;
245 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
246 Options.HonorSignDependentRoundingFPMathOption =
247 EnableHonorSignDependentRoundingFPMath;
248 Options.UseSoftFloat = GenerateSoftFloatCalls;
249 if (FloatABIForCalls != FloatABI::Default)
250 Options.FloatABIType = FloatABIForCalls;
251 Options.NoZerosInBSS = DontPlaceZerosInBSS;
252 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
253 Options.DisableTailCalls = DisableTailCalls;
254 Options.StackAlignmentOverride = OverrideStackAlignment;
255 Options.RealignStack = EnableRealignStack;
256 Options.TrapFuncName = TrapFuncName;
257 Options.PositionIndependentExecutable = EnablePIE;
258 Options.EnableSegmentedStacks = SegmentedStacks;
259 Options.UseInitArray = UseInitArray;
Chad Rosier35907e92012-08-21 16:15:24 +0000260 Options.SSPBufferSize = SSPBufferSize;
Bill Wendling9ac0aaa2012-08-06 21:34:54 +0000261}
262
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000263LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer,
264 std::string &errMsg) {
Rafael Espindola38c4e532011-03-02 04:14:42 +0000265 static bool Initialized = false;
266 if (!Initialized) {
267 InitializeAllTargets();
Evan Chenge78085a2011-07-22 21:58:54 +0000268 InitializeAllTargetMCs();
Rafael Espindola38c4e532011-03-02 04:14:42 +0000269 InitializeAllAsmParsers();
270 Initialized = true;
271 }
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000272
273 // parse bitcode buffer
Rafael Espindolaf19d7a72011-03-18 19:51:00 +0000274 OwningPtr<Module> m(getLazyBitcodeModule(buffer, getGlobalContext(),
275 &errMsg));
276 if (!m) {
277 delete buffer;
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000278 return NULL;
Rafael Espindolaf19d7a72011-03-18 19:51:00 +0000279 }
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000280
Bob Wilson47ed8a12012-10-12 17:39:25 +0000281 std::string TripleStr = m->getTargetTriple();
282 if (TripleStr.empty())
283 TripleStr = sys::getDefaultTargetTriple();
284 llvm::Triple Triple(TripleStr);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000285
286 // find machine architecture for this module
Bob Wilson47ed8a12012-10-12 17:39:25 +0000287 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000288 if (!march)
289 return NULL;
290
Nick Lewycky333ed452011-04-21 01:54:08 +0000291 // construct LTOModule, hand over ownership of module and target
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000292 SubtargetFeatures Features;
Bob Wilson47ed8a12012-10-12 17:39:25 +0000293 Features.getDefaultSubtargetFeatures(Triple);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000294 std::string FeatureStr = Features.getString();
Bob Wilson47ed8a12012-10-12 17:39:25 +0000295 // Set a default CPU for Darwin triples.
Evan Cheng276365d2011-06-30 01:53:36 +0000296 std::string CPU;
Bob Wilson47ed8a12012-10-12 17:39:25 +0000297 if (Triple.isOSDarwin()) {
298 if (Triple.getArch() == llvm::Triple::x86_64)
299 CPU = "core2";
300 else if (Triple.getArch() == llvm::Triple::x86)
301 CPU = "yonah";
302 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000303 TargetOptions Options;
Bill Wendling9ac0aaa2012-08-06 21:34:54 +0000304 getTargetOptions(Options);
Bob Wilson47ed8a12012-10-12 17:39:25 +0000305 TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000306 Options);
Rafael Espindola38c4e532011-03-02 04:14:42 +0000307 LTOModule *Ret = new LTOModule(m.take(), target);
Bill Wendling3bb17382012-03-28 23:12:18 +0000308 if (Ret->parseSymbols(errMsg)) {
Rafael Espindola38c4e532011-03-02 04:14:42 +0000309 delete Ret;
310 return NULL;
311 }
Bill Wendling931d4c22011-11-04 18:48:00 +0000312
Rafael Espindola38c4e532011-03-02 04:14:42 +0000313 return Ret;
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000314}
315
Bill Wendling62cf01e2012-03-28 20:46:54 +0000316/// makeBuffer - Create a MemoryBuffer from a memory range.
317MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length) {
Roman Divacky59324292012-09-05 22:26:57 +0000318 const char *startPtr = (const char*)mem;
Bill Wendling62cf01e2012-03-28 20:46:54 +0000319 return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), "", false);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000320}
321
Bill Wendling62cf01e2012-03-28 20:46:54 +0000322/// objcClassNameFromExpression - Get string that the data pointer points to.
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000323bool LTOModule::objcClassNameFromExpression(Constant *c, std::string &name) {
324 if (ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
325 Constant *op = ce->getOperand(0);
326 if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) {
327 Constant *cn = gvn->getInitializer();
Chris Lattner18c7f802012-02-05 02:29:43 +0000328 if (ConstantDataArray *ca = dyn_cast<ConstantDataArray>(cn)) {
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000329 if (ca->isCString()) {
Chris Lattner18c7f802012-02-05 02:29:43 +0000330 name = ".objc_class_name_" + ca->getAsCString().str();
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000331 return true;
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000332 }
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000333 }
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000334 }
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000335 }
336 return false;
337}
338
Bill Wendling62cf01e2012-03-28 20:46:54 +0000339/// addObjCClass - Parse i386/ppc ObjC class data structure.
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000340void LTOModule::addObjCClass(GlobalVariable *clgv) {
Bill Wendling931d4c22011-11-04 18:48:00 +0000341 ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
342 if (!c) return;
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000343
Bill Wendling931d4c22011-11-04 18:48:00 +0000344 // second slot in __OBJC,__class is pointer to superclass name
345 std::string superclassName;
346 if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
347 NameAndAttributes info;
348 StringMap<NameAndAttributes>::value_type &entry =
349 _undefines.GetOrCreateValue(superclassName);
350 if (!entry.getValue().name) {
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000351 const char *symbolName = entry.getKey().data();
352 info.name = symbolName;
353 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling24b87802012-03-29 08:27:32 +0000354 info.isFunction = false;
355 info.symbol = clgv;
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000356 entry.setValue(info);
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000357 }
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000358 }
Bill Wendling931d4c22011-11-04 18:48:00 +0000359
360 // third slot in __OBJC,__class is pointer to class name
361 std::string className;
362 if (objcClassNameFromExpression(c->getOperand(2), className)) {
363 StringSet::value_type &entry = _defines.GetOrCreateValue(className);
364 entry.setValue(1);
Bill Wendling24b87802012-03-29 08:27:32 +0000365
Bill Wendling931d4c22011-11-04 18:48:00 +0000366 NameAndAttributes info;
367 info.name = entry.getKey().data();
Bill Wendling24b87802012-03-29 08:27:32 +0000368 info.attributes = LTO_SYMBOL_PERMISSIONS_DATA |
369 LTO_SYMBOL_DEFINITION_REGULAR | LTO_SYMBOL_SCOPE_DEFAULT;
370 info.isFunction = false;
371 info.symbol = clgv;
Bill Wendling931d4c22011-11-04 18:48:00 +0000372 _symbols.push_back(info);
373 }
374}
375
Bill Wendling62cf01e2012-03-28 20:46:54 +0000376/// addObjCCategory - Parse i386/ppc ObjC category data structure.
Bill Wendling931d4c22011-11-04 18:48:00 +0000377void LTOModule::addObjCCategory(GlobalVariable *clgv) {
378 ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
379 if (!c) return;
380
381 // second slot in __OBJC,__category is pointer to target class name
382 std::string targetclassName;
383 if (!objcClassNameFromExpression(c->getOperand(1), targetclassName))
384 return;
385
386 NameAndAttributes info;
387 StringMap<NameAndAttributes>::value_type &entry =
388 _undefines.GetOrCreateValue(targetclassName);
389
390 if (entry.getValue().name)
391 return;
392
393 const char *symbolName = entry.getKey().data();
394 info.name = symbolName;
395 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling24b87802012-03-29 08:27:32 +0000396 info.isFunction = false;
397 info.symbol = clgv;
Bill Wendling931d4c22011-11-04 18:48:00 +0000398 entry.setValue(info);
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000399}
400
Bill Wendling62cf01e2012-03-28 20:46:54 +0000401/// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000402void LTOModule::addObjCClassRef(GlobalVariable *clgv) {
403 std::string targetclassName;
Bill Wendling931d4c22011-11-04 18:48:00 +0000404 if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName))
405 return;
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000406
Bill Wendling931d4c22011-11-04 18:48:00 +0000407 NameAndAttributes info;
408 StringMap<NameAndAttributes>::value_type &entry =
409 _undefines.GetOrCreateValue(targetclassName);
410 if (entry.getValue().name)
411 return;
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000412
Bill Wendling931d4c22011-11-04 18:48:00 +0000413 const char *symbolName = entry.getKey().data();
414 info.name = symbolName;
415 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling24b87802012-03-29 08:27:32 +0000416 info.isFunction = false;
417 info.symbol = clgv;
Bill Wendling931d4c22011-11-04 18:48:00 +0000418 entry.setValue(info);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000419}
420
Bill Wendling62cf01e2012-03-28 20:46:54 +0000421/// addDefinedDataSymbol - Add a data symbol as defined to the list.
Bill Wendlinga7280fd2011-11-04 09:30:19 +0000422void LTOModule::addDefinedDataSymbol(GlobalValue *v) {
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000423 // Add to list of defined symbols.
Bill Wendlinga7280fd2011-11-04 09:30:19 +0000424 addDefinedSymbol(v, false);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000425
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000426 if (!v->hasSection() /* || !isTargetDarwin */)
427 return;
428
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000429 // Special case i386/ppc ObjC data structures in magic sections:
430 // The issue is that the old ObjC object format did some strange
431 // contortions to avoid real linker symbols. For instance, the
432 // ObjC class data structure is allocated statically in the executable
433 // that defines that class. That data structures contains a pointer to
434 // its superclass. But instead of just initializing that part of the
435 // struct to the address of its superclass, and letting the static and
436 // dynamic linkers do the rest, the runtime works by having that field
437 // instead point to a C-string that is the name of the superclass.
438 // At runtime the objc initialization updates that pointer and sets
439 // it to point to the actual super class. As far as the linker
440 // knows it is just a pointer to a string. But then someone wanted the
441 // linker to issue errors at build time if the superclass was not found.
442 // So they figured out a way in mach-o object format to use an absolute
443 // symbols (.objc_class_name_Foo = 0) and a floating reference
444 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
445 // a class was missing.
446 // The following synthesizes the implicit .objc_* symbols for the linker
447 // from the ObjC data structures generated by the front end.
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000448
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000449 // special case if this data blob is an ObjC class definition
450 if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) {
451 if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
452 addObjCClass(gv);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000453 }
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000454 }
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000455
Bill Wendlingeda3fc62012-08-06 22:52:45 +0000456 // special case if this data blob is an ObjC category definition
457 else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) {
458 if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
459 addObjCCategory(gv);
460 }
461 }
462
463 // special case if this data blob is the list of referenced classes
464 else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) {
465 if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
466 addObjCClassRef(gv);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000467 }
468 }
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000469}
470
Bill Wendling62cf01e2012-03-28 20:46:54 +0000471/// addDefinedFunctionSymbol - Add a function symbol as defined to the list.
472void LTOModule::addDefinedFunctionSymbol(Function *f) {
473 // add to list of defined symbols
474 addDefinedSymbol(f, true);
475}
476
477/// addDefinedSymbol - Add a defined symbol to the list.
Bill Wendlinga7280fd2011-11-04 09:30:19 +0000478void LTOModule::addDefinedSymbol(GlobalValue *def, bool isFunction) {
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000479 // ignore all llvm.* symbols
480 if (def->getName().startswith("llvm."))
481 return;
482
483 // string is owned by _defines
Rafael Espindolaef1860a2011-02-11 05:23:09 +0000484 SmallString<64> Buffer;
Bill Wendlinga7280fd2011-11-04 09:30:19 +0000485 _mangler.getNameWithPrefix(Buffer, def, false);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000486
487 // set alignment part log2() can have rounding errors
488 uint32_t align = def->getAlignment();
489 uint32_t attr = align ? CountTrailingZeros_32(def->getAlignment()) : 0;
490
491 // set permissions part
Bill Wendling24b87802012-03-29 08:27:32 +0000492 if (isFunction) {
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000493 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
Bill Wendling24b87802012-03-29 08:27:32 +0000494 } else {
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000495 GlobalVariable *gv = dyn_cast<GlobalVariable>(def);
496 if (gv && gv->isConstant())
497 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
498 else
499 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
500 }
501
502 // set definition part
Bill Wendling563ef5e2010-09-27 18:05:19 +0000503 if (def->hasWeakLinkage() || def->hasLinkOnceLinkage() ||
Bill Wendling32811be2012-08-17 18:33:14 +0000504 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000505 attr |= LTO_SYMBOL_DEFINITION_WEAK;
Bill Wendling7afea0c2010-09-27 20:17:45 +0000506 else if (def->hasCommonLinkage())
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000507 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
Bill Wendling7afea0c2010-09-27 20:17:45 +0000508 else
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000509 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000510
511 // set scope part
512 if (def->hasHiddenVisibility())
513 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
514 else if (def->hasProtectedVisibility())
515 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
Bill Wendling7afea0c2010-09-27 20:17:45 +0000516 else if (def->hasExternalLinkage() || def->hasWeakLinkage() ||
517 def->hasLinkOnceLinkage() || def->hasCommonLinkage() ||
518 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000519 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
Bill Wendling32811be2012-08-17 18:33:14 +0000520 else if (def->hasLinkOnceODRAutoHideLinkage())
Bill Wendling7afea0c2010-09-27 20:17:45 +0000521 attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000522 else
523 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
524
Chad Rosierbd35f272011-06-28 18:26:12 +0000525 StringSet::value_type &entry = _defines.GetOrCreateValue(Buffer);
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000526 entry.setValue(1);
527
Bill Wendling24b87802012-03-29 08:27:32 +0000528 // fill information structure
529 NameAndAttributes info;
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000530 StringRef Name = entry.getKey();
531 info.name = Name.data();
532 assert(info.name[Name.size()] == '\0');
Bill Wendling24b87802012-03-29 08:27:32 +0000533 info.attributes = attr;
534 info.isFunction = isFunction;
535 info.symbol = def;
536
537 // add to table of symbols
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000538 _symbols.push_back(info);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000539}
540
Bill Wendling62cf01e2012-03-28 20:46:54 +0000541/// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
542/// defined list.
Rafael Espindola38c4e532011-03-02 04:14:42 +0000543void LTOModule::addAsmGlobalSymbol(const char *name,
544 lto_symbol_attributes scope) {
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000545 StringSet::value_type &entry = _defines.GetOrCreateValue(name);
546
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000547 // only add new define if not already defined
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000548 if (entry.getValue())
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000549 return;
550
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000551 entry.setValue(1);
Bill Wendling24b87802012-03-29 08:27:32 +0000552
553 NameAndAttributes &info = _undefines[entry.getKey().data()];
554
Bill Wendling1fcbca02012-04-02 03:33:31 +0000555 if (info.symbol == 0) {
Bill Wendling8ba94052012-04-02 10:01:21 +0000556 // FIXME: This is trying to take care of module ASM like this:
557 //
558 // module asm ".zerofill __FOO, __foo, _bar_baz_qux, 0"
559 //
560 // but is gross and its mother dresses it funny. Have the ASM parser give us
561 // more details for this type of situation so that we're not guessing so
562 // much.
563
564 // fill information structure
Rafael Espindola383fd7a2012-05-11 03:42:13 +0000565 info.name = entry.getKey().data();
Bill Wendling8ba94052012-04-02 10:01:21 +0000566 info.attributes =
567 LTO_SYMBOL_PERMISSIONS_DATA | LTO_SYMBOL_DEFINITION_REGULAR | scope;
568 info.isFunction = false;
569 info.symbol = 0;
570
571 // add to table of symbols
572 _symbols.push_back(info);
Bill Wendling1fcbca02012-04-02 03:33:31 +0000573 return;
574 }
575
Bill Wendling24b87802012-03-29 08:27:32 +0000576 if (info.isFunction)
577 addDefinedFunctionSymbol(cast<Function>(info.symbol));
578 else
579 addDefinedDataSymbol(info.symbol);
Bill Wendling5ff4bc22012-03-30 23:26:06 +0000580
581 _symbols.back().attributes &= ~LTO_SYMBOL_SCOPE_MASK;
582 _symbols.back().attributes |= scope;
Devang Patelc2aec572008-07-16 18:06:52 +0000583}
Nick Kledzik77595fc2008-02-26 20:26:43 +0000584
Bill Wendling62cf01e2012-03-28 20:46:54 +0000585/// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the
586/// undefined list.
Rafael Espindola38c4e532011-03-02 04:14:42 +0000587void LTOModule::addAsmGlobalSymbolUndef(const char *name) {
588 StringMap<NameAndAttributes>::value_type &entry =
589 _undefines.GetOrCreateValue(name);
590
591 _asm_undefines.push_back(entry.getKey().data());
592
593 // we already have the symbol
594 if (entry.getValue().name)
595 return;
596
597 uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;;
598 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
599 NameAndAttributes info;
600 info.name = entry.getKey().data();
Bill Wendling24b87802012-03-29 08:27:32 +0000601 info.attributes = attr;
602 info.isFunction = false;
603 info.symbol = 0;
Rafael Espindola38c4e532011-03-02 04:14:42 +0000604
605 entry.setValue(info);
606}
607
Bill Wendling62cf01e2012-03-28 20:46:54 +0000608/// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet to a
609/// list to be resolved later.
Bill Wendling24b87802012-03-29 08:27:32 +0000610void LTOModule::addPotentialUndefinedSymbol(GlobalValue *decl, bool isFunc) {
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000611 // ignore all llvm.* symbols
612 if (decl->getName().startswith("llvm."))
613 return;
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000614
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000615 // ignore all aliases
616 if (isa<GlobalAlias>(decl))
617 return;
Nick Lewycky485ded02009-07-09 06:03:04 +0000618
Rafael Espindolaef1860a2011-02-11 05:23:09 +0000619 SmallString<64> name;
Bill Wendlinga7280fd2011-11-04 09:30:19 +0000620 _mangler.getNameWithPrefix(name, decl, false);
Rafael Espindola7431af02009-04-24 16:55:21 +0000621
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000622 StringMap<NameAndAttributes>::value_type &entry =
Chad Rosierbd35f272011-06-28 18:26:12 +0000623 _undefines.GetOrCreateValue(name);
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000624
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000625 // we already have the symbol
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000626 if (entry.getValue().name)
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000627 return;
Rafael Espindola7431af02009-04-24 16:55:21 +0000628
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000629 NameAndAttributes info;
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000630
631 info.name = entry.getKey().data();
Bill Wendling62cf01e2012-03-28 20:46:54 +0000632
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000633 if (decl->hasExternalWeakLinkage())
634 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
635 else
636 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000637
Bill Wendling24b87802012-03-29 08:27:32 +0000638 info.isFunction = isFunc;
639 info.symbol = decl;
640
Rafael Espindolacd6c93e2011-02-20 16:27:25 +0000641 entry.setValue(info);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000642}
643
Rafael Espindola38c4e532011-03-02 04:14:42 +0000644namespace {
645 class RecordStreamer : public MCStreamer {
646 public:
Bill Wendling90e7d4f2012-04-03 03:56:52 +0000647 enum State { NeverSeen, Global, Defined, DefinedGlobal, Used };
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000648
Rafael Espindola38c4e532011-03-02 04:14:42 +0000649 private:
650 StringMap<State> Symbols;
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000651
Rafael Espindola38c4e532011-03-02 04:14:42 +0000652 void markDefined(const MCSymbol &Symbol) {
653 State &S = Symbols[Symbol.getName()];
654 switch (S) {
655 case DefinedGlobal:
656 case Global:
657 S = DefinedGlobal;
658 break;
659 case NeverSeen:
660 case Defined:
661 case Used:
662 S = Defined;
663 break;
664 }
665 }
666 void markGlobal(const MCSymbol &Symbol) {
667 State &S = Symbols[Symbol.getName()];
668 switch (S) {
669 case DefinedGlobal:
670 case Defined:
671 S = DefinedGlobal;
672 break;
673
674 case NeverSeen:
675 case Global:
676 case Used:
677 S = Global;
678 break;
679 }
680 }
681 void markUsed(const MCSymbol &Symbol) {
682 State &S = Symbols[Symbol.getName()];
683 switch (S) {
684 case DefinedGlobal:
685 case Defined:
686 case Global:
687 break;
688
689 case NeverSeen:
690 case Used:
691 S = Used;
692 break;
693 }
694 }
695
696 // FIXME: mostly copied for the obj streamer.
697 void AddValueSymbols(const MCExpr *Value) {
698 switch (Value->getKind()) {
699 case MCExpr::Target:
700 // FIXME: What should we do in here?
701 break;
702
703 case MCExpr::Constant:
704 break;
705
706 case MCExpr::Binary: {
707 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
708 AddValueSymbols(BE->getLHS());
709 AddValueSymbols(BE->getRHS());
710 break;
711 }
712
713 case MCExpr::SymbolRef:
714 markUsed(cast<MCSymbolRefExpr>(Value)->getSymbol());
715 break;
716
717 case MCExpr::Unary:
718 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
719 break;
720 }
721 }
722
723 public:
724 typedef StringMap<State>::const_iterator const_iterator;
725
726 const_iterator begin() {
727 return Symbols.begin();
728 }
729
730 const_iterator end() {
731 return Symbols.end();
732 }
733
734 RecordStreamer(MCContext &Context) : MCStreamer(Context) {}
735
Bill Wendling90e7d4f2012-04-03 03:56:52 +0000736 virtual void EmitInstruction(const MCInst &Inst) {
737 // Scan for values.
738 for (unsigned i = Inst.getNumOperands(); i--; )
739 if (Inst.getOperand(i).isExpr())
740 AddValueSymbols(Inst.getOperand(i).getExpr());
741 }
Rafael Espindola38c4e532011-03-02 04:14:42 +0000742 virtual void EmitLabel(MCSymbol *Symbol) {
743 Symbol->setSection(*getCurrentSection());
744 markDefined(*Symbol);
745 }
Rafael Espindola38c4e532011-03-02 04:14:42 +0000746 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
747 // FIXME: should we handle aliases?
748 markDefined(*Symbol);
749 }
750 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
751 if (Attribute == MCSA_Global)
752 markGlobal(*Symbol);
753 }
Rafael Espindola38c4e532011-03-02 04:14:42 +0000754 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Chengdf42d412012-06-22 20:30:39 +0000755 uint64_t Size , unsigned ByteAlignment) {
Rafael Espindola38c4e532011-03-02 04:14:42 +0000756 markDefined(*Symbol);
757 }
Rafael Espindola38c4e532011-03-02 04:14:42 +0000758 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
759 unsigned ByteAlignment) {
760 markDefined(*Symbol);
761 }
Bill Wendling90e7d4f2012-04-03 03:56:52 +0000762
763 // Noop calls.
764 virtual void ChangeSection(const MCSection *Section) {}
765 virtual void InitSections() {}
766 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {}
767 virtual void EmitThumbFunc(MCSymbol *Func) {}
768 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {}
769 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {}
770 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {}
771 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {}
772 virtual void EmitCOFFSymbolType(int Type) {}
773 virtual void EndCOFFSymbolDef() {}
Rafael Espindola38c4e532011-03-02 04:14:42 +0000774 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {}
Benjamin Kramer36a16012011-09-01 23:04:27 +0000775 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
776 unsigned ByteAlignment) {}
Rafael Espindola38c4e532011-03-02 04:14:42 +0000777 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
778 uint64_t Size, unsigned ByteAlignment) {}
779 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) {}
780 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000781 unsigned AddrSpace) {}
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000782 virtual void EmitULEB128Value(const MCExpr *Value) {}
783 virtual void EmitSLEB128Value(const MCExpr *Value) {}
Rafael Espindola38c4e532011-03-02 04:14:42 +0000784 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
785 unsigned ValueSize,
786 unsigned MaxBytesToEmit) {}
787 virtual void EmitCodeAlignment(unsigned ByteAlignment,
788 unsigned MaxBytesToEmit) {}
Jim Grosbachebd4c052012-01-27 00:37:08 +0000789 virtual bool EmitValueToOffset(const MCExpr *Offset,
790 unsigned char Value ) { return false; }
Rafael Espindola38c4e532011-03-02 04:14:42 +0000791 virtual void EmitFileDirective(StringRef Filename) {}
792 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
793 const MCSymbol *LastLabel,
Evan Cheng672b93a2011-07-14 05:43:07 +0000794 const MCSymbol *Label,
795 unsigned PointerSize) {}
Rafael Espindola99b42372012-01-07 03:13:18 +0000796 virtual void FinishImpl() {}
Rafael Espindola38c4e532011-03-02 04:14:42 +0000797 };
Bill Wendling24b87802012-03-29 08:27:32 +0000798} // end anonymous namespace
Rafael Espindola38c4e532011-03-02 04:14:42 +0000799
Bill Wendling62cf01e2012-03-28 20:46:54 +0000800/// addAsmGlobalSymbols - Add global symbols from module-level ASM to the
801/// defined or undefined lists.
Bill Wendlingb9bff962011-11-04 09:24:40 +0000802bool LTOModule::addAsmGlobalSymbols(std::string &errMsg) {
Rafael Espindola38c4e532011-03-02 04:14:42 +0000803 const std::string &inlineAsm = _module->getModuleInlineAsm();
Ivan Krasin6d483c22011-09-08 07:38:25 +0000804 if (inlineAsm.empty())
805 return false;
Rafael Espindola38c4e532011-03-02 04:14:42 +0000806
Bill Wendlingb9bff962011-11-04 09:24:40 +0000807 OwningPtr<RecordStreamer> Streamer(new RecordStreamer(_context));
Rafael Espindola38c4e532011-03-02 04:14:42 +0000808 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(inlineAsm);
809 SourceMgr SrcMgr;
810 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000811 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr,
Bill Wendlingb9bff962011-11-04 09:24:40 +0000812 _context, *Streamer,
Rafael Espindola38c4e532011-03-02 04:14:42 +0000813 *_target->getMCAsmInfo()));
Bill Wendling56825272012-08-08 22:01:55 +0000814 const Target &T = _target->getTarget();
815 OwningPtr<MCSubtargetInfo>
816 STI(T.createMCSubtargetInfo(_target->getTargetTriple(),
817 _target->getTargetCPU(),
818 _target->getTargetFeatureString()));
819 OwningPtr<MCTargetAsmParser> TAP(T.createMCAsmParser(*STI, *Parser.get()));
Ivan Krasin603e1032011-09-08 07:36:39 +0000820 if (!TAP) {
Bill Wendling56825272012-08-08 22:01:55 +0000821 errMsg = "target " + std::string(T.getName()) +
822 " does not define AsmParser.";
Ivan Krasin603e1032011-09-08 07:36:39 +0000823 return true;
824 }
825
Rafael Espindola38c4e532011-03-02 04:14:42 +0000826 Parser->setTargetParser(*TAP);
Bill Wendling56825272012-08-08 22:01:55 +0000827 if (Parser->Run(false))
Rafael Espindola38c4e532011-03-02 04:14:42 +0000828 return true;
829
830 for (RecordStreamer::const_iterator i = Streamer->begin(),
831 e = Streamer->end(); i != e; ++i) {
832 StringRef Key = i->first();
833 RecordStreamer::State Value = i->second;
834 if (Value == RecordStreamer::DefinedGlobal)
835 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_DEFAULT);
836 else if (Value == RecordStreamer::Defined)
837 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_INTERNAL);
838 else if (Value == RecordStreamer::Global ||
839 Value == RecordStreamer::Used)
840 addAsmGlobalSymbolUndef(Key.data());
841 }
Bill Wendling56825272012-08-08 22:01:55 +0000842
Rafael Espindola38c4e532011-03-02 04:14:42 +0000843 return false;
844}
845
Bill Wendling62cf01e2012-03-28 20:46:54 +0000846/// isDeclaration - Return 'true' if the global value is a declaration.
Rafael Espindolaf19d7a72011-03-18 19:51:00 +0000847static bool isDeclaration(const GlobalValue &V) {
848 if (V.hasAvailableExternallyLinkage())
849 return true;
Bill Wendling56825272012-08-08 22:01:55 +0000850
Rafael Espindolaf19d7a72011-03-18 19:51:00 +0000851 if (V.isMaterializable())
852 return false;
Bill Wendling56825272012-08-08 22:01:55 +0000853
Rafael Espindolaf19d7a72011-03-18 19:51:00 +0000854 return V.isDeclaration();
855}
856
Bill Wendling3bb17382012-03-28 23:12:18 +0000857/// parseSymbols - Parse the symbols from the module and model-level ASM and add
Bill Wendling62cf01e2012-03-28 20:46:54 +0000858/// them to either the defined or undefined lists.
Bill Wendling3bb17382012-03-28 23:12:18 +0000859bool LTOModule::parseSymbols(std::string &errMsg) {
Daniel Dunbare41d9002010-08-10 23:46:46 +0000860 // add functions
Bill Wendling9f3b4832012-03-29 03:34:57 +0000861 for (Module::iterator f = _module->begin(), e = _module->end(); f != e; ++f) {
Rafael Espindolaf19d7a72011-03-18 19:51:00 +0000862 if (isDeclaration(*f))
Bill Wendling24b87802012-03-29 08:27:32 +0000863 addPotentialUndefinedSymbol(f, true);
Daniel Dunbare41d9002010-08-10 23:46:46 +0000864 else
Bill Wendlinga7280fd2011-11-04 09:30:19 +0000865 addDefinedFunctionSymbol(f);
Daniel Dunbare41d9002010-08-10 23:46:46 +0000866 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000867
Daniel Dunbare41d9002010-08-10 23:46:46 +0000868 // add data
869 for (Module::global_iterator v = _module->global_begin(),
870 e = _module->global_end(); v != e; ++v) {
Rafael Espindolaf19d7a72011-03-18 19:51:00 +0000871 if (isDeclaration(*v))
Bill Wendling24b87802012-03-29 08:27:32 +0000872 addPotentialUndefinedSymbol(v, false);
Daniel Dunbare41d9002010-08-10 23:46:46 +0000873 else
Bill Wendlinga7280fd2011-11-04 09:30:19 +0000874 addDefinedDataSymbol(v);
Daniel Dunbare41d9002010-08-10 23:46:46 +0000875 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000876
Daniel Dunbare41d9002010-08-10 23:46:46 +0000877 // add asm globals
Bill Wendlingb9bff962011-11-04 09:24:40 +0000878 if (addAsmGlobalSymbols(errMsg))
Rafael Espindola38c4e532011-03-02 04:14:42 +0000879 return true;
Daniel Dunbare41d9002010-08-10 23:46:46 +0000880
Rafael Espindola02003ca2010-10-20 04:57:22 +0000881 // add aliases
Bill Wendling24b87802012-03-29 08:27:32 +0000882 for (Module::alias_iterator a = _module->alias_begin(),
883 e = _module->alias_end(); a != e; ++a) {
884 if (isDeclaration(*a->getAliasedGlobal()))
Bill Wendling61476d62012-03-28 20:48:49 +0000885 // Is an alias to a declaration.
Bill Wendling24b87802012-03-29 08:27:32 +0000886 addPotentialUndefinedSymbol(a, false);
Rafael Espindola02003ca2010-10-20 04:57:22 +0000887 else
Bill Wendling24b87802012-03-29 08:27:32 +0000888 addDefinedDataSymbol(a);
Rafael Espindola02003ca2010-10-20 04:57:22 +0000889 }
890
Daniel Dunbare41d9002010-08-10 23:46:46 +0000891 // make symbols for all undefines
Bill Wendling24b87802012-03-29 08:27:32 +0000892 for (StringMap<NameAndAttributes>::iterator u =_undefines.begin(),
893 e = _undefines.end(); u != e; ++u) {
894 // If this symbol also has a definition, then don't make an undefine because
895 // it is a tentative definition.
896 if (_defines.count(u->getKey())) continue;
897 NameAndAttributes info = u->getValue();
898 _symbols.push_back(info);
Daniel Dunbarb06913d2010-08-10 23:46:39 +0000899 }
Bill Wendling24b87802012-03-29 08:27:32 +0000900
Rafael Espindola38c4e532011-03-02 04:14:42 +0000901 return false;
Nick Kledzikef194ed2008-02-27 22:25:36 +0000902}