blob: 87e3573de900185e5fc430634c74f7016779f790 [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"
Joey Goulydb6144e2013-09-12 12:55:29 +000024#include "llvm/MC/MCInstrInfo.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000025#include "llvm/MC/MCParser/MCAsmParser.h"
Rafael Espindola1e49a6d2011-03-02 04:14:42 +000026#include "llvm/MC/MCStreamer.h"
Evan Cheng91111d22011-07-09 05:47:46 +000027#include "llvm/MC/MCSubtargetInfo.h"
Rafael Espindola1e49a6d2011-03-02 04:14:42 +000028#include "llvm/MC/MCSymbol.h"
Evan Cheng11424442011-07-26 00:24:13 +000029#include "llvm/MC/MCTargetAsmParser.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000030#include "llvm/MC/SubtargetFeature.h"
Bill Wendlingb8dcda72012-08-06 21:34:54 +000031#include "llvm/Support/CommandLine.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000032#include "llvm/Support/Host.h"
Rafael Espindola46ed3532013-06-11 18:05:26 +000033#include "llvm/Support/FileSystem.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000034#include "llvm/Support/MemoryBuffer.h"
35#include "llvm/Support/Path.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>
Bill Wendlingb8dcda72012-08-06 21:34:54 +000054EnableUnsafeFPMath("enable-unsafe-fp-math",
55 cl::desc("Enable optimizations that may decrease FP precision"),
56 cl::init(false));
57
58static cl::opt<bool>
59EnableNoInfsFPMath("enable-no-infs-fp-math",
60 cl::desc("Enable FP math optimizations that assume no +-Infs"),
61 cl::init(false));
62
63static cl::opt<bool>
64EnableNoNaNsFPMath("enable-no-nans-fp-math",
65 cl::desc("Enable FP math optimizations that assume no NaNs"),
66 cl::init(false));
67
68static cl::opt<bool>
69EnableHonorSignDependentRoundingFPMath("enable-sign-dependent-rounding-fp-math",
70 cl::Hidden,
71 cl::desc("Force codegen to assume rounding mode can change dynamically"),
72 cl::init(false));
73
74static cl::opt<bool>
75GenerateSoftFloatCalls("soft-float",
76 cl::desc("Generate software floating point library calls"),
77 cl::init(false));
78
79static cl::opt<llvm::FloatABI::ABIType>
80FloatABIForCalls("float-abi",
81 cl::desc("Choose float ABI type"),
82 cl::init(FloatABI::Default),
83 cl::values(
84 clEnumValN(FloatABI::Default, "default",
85 "Target default float ABI type"),
86 clEnumValN(FloatABI::Soft, "soft",
87 "Soft float ABI (implied by -soft-float)"),
88 clEnumValN(FloatABI::Hard, "hard",
89 "Hard float ABI (uses FP registers)"),
90 clEnumValEnd));
91
92static cl::opt<llvm::FPOpFusion::FPOpFusionMode>
93FuseFPOps("fp-contract",
94 cl::desc("Enable aggresive formation of fused FP ops"),
95 cl::init(FPOpFusion::Standard),
96 cl::values(
97 clEnumValN(FPOpFusion::Fast, "fast",
98 "Fuse FP ops whenever profitable"),
99 clEnumValN(FPOpFusion::Standard, "on",
100 "Only fuse 'blessed' FP ops."),
101 clEnumValN(FPOpFusion::Strict, "off",
102 "Only fuse FP ops when the result won't be effected."),
103 clEnumValEnd));
104
105static cl::opt<bool>
106DontPlaceZerosInBSS("nozero-initialized-in-bss",
107 cl::desc("Don't place zero-initialized symbols into bss section"),
108 cl::init(false));
109
110static cl::opt<bool>
111EnableGuaranteedTailCallOpt("tailcallopt",
112 cl::desc("Turn fastcc calls into tail calls by (potentially) changing ABI."),
113 cl::init(false));
114
115static cl::opt<bool>
116DisableTailCalls("disable-tail-calls",
117 cl::desc("Never emit tail calls"),
118 cl::init(false));
119
120static cl::opt<unsigned>
121OverrideStackAlignment("stack-alignment",
122 cl::desc("Override default stack alignment"),
123 cl::init(0));
124
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000125static cl::opt<std::string>
126TrapFuncName("trap-func", cl::Hidden,
127 cl::desc("Emit a call to trap function rather than a trap instruction"),
128 cl::init(""));
129
130static cl::opt<bool>
131EnablePIE("enable-pie",
132 cl::desc("Assume the creation of a position independent executable."),
133 cl::init(false));
134
135static cl::opt<bool>
136SegmentedStacks("segmented-stacks",
137 cl::desc("Use segmented stacks if possible."),
138 cl::init(false));
139
140static cl::opt<bool>
141UseInitArray("use-init-array",
142 cl::desc("Use .init_array instead of .ctors."),
143 cl::init(false));
144
Bill Wendlingfb440502012-03-28 20:46:54 +0000145LTOModule::LTOModule(llvm::Module *m, llvm::TargetMachine *t)
146 : _module(m), _target(t),
Bill Wendlingbc07a892013-06-18 07:20:20 +0000147 _context(_target->getMCAsmInfo(), _target->getRegisterInfo(), NULL),
Bill Wendling70b14002013-05-29 20:37:19 +0000148 _mangler(_context, t) {}
Bill Wendlingfb440502012-03-28 20:46:54 +0000149
150/// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM
151/// bitcode.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000152bool LTOModule::isBitcodeFile(const void *mem, size_t length) {
Rafael Espindola46ed3532013-06-11 18:05:26 +0000153 return sys::fs::identify_magic(StringRef((const char *)mem, length)) ==
154 sys::fs::file_magic::bitcode;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000155}
156
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000157bool LTOModule::isBitcodeFile(const char *path) {
Rafael Espindola71affba2013-06-12 15:13:57 +0000158 sys::fs::file_magic type;
159 if (sys::fs::identify_magic(path, type))
160 return false;
161 return type == sys::fs::file_magic::bitcode;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000162}
163
Bill Wendlingfb440502012-03-28 20:46:54 +0000164/// isBitcodeFileForTarget - Returns 'true' if the file (or memory contents) is
165/// LLVM bitcode for the specified triple.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000166bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length,
167 const char *triplePrefix) {
168 MemoryBuffer *buffer = makeBuffer(mem, length);
169 if (!buffer)
Nick Kledzikb481c202009-06-01 20:33:09 +0000170 return false;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000171 return isTargetMatch(buffer, triplePrefix);
Nick Kledzikb481c202009-06-01 20:33:09 +0000172}
173
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000174bool LTOModule::isBitcodeFileForTarget(const char *path,
175 const char *triplePrefix) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000176 OwningPtr<MemoryBuffer> buffer;
177 if (MemoryBuffer::getFile(path, buffer))
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000178 return false;
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000179 return isTargetMatch(buffer.take(), triplePrefix);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000180}
181
Bill Wendlingfb440502012-03-28 20:46:54 +0000182/// isTargetMatch - Returns 'true' if the memory buffer is for the specified
183/// target triple.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000184bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) {
Bill Wendling0198ce02010-10-06 01:22:42 +0000185 std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
186 delete buffer;
Bill Wendling5f689e72011-11-04 18:48:00 +0000187 return strncmp(Triple.c_str(), triplePrefix, strlen(triplePrefix)) == 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000188}
189
Bill Wendlingfb440502012-03-28 20:46:54 +0000190/// makeLTOModule - Create an LTOModule. N.B. These methods take ownership of
191/// the buffer.
192LTOModule *LTOModule::makeLTOModule(const char *path, std::string &errMsg) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000193 OwningPtr<MemoryBuffer> buffer;
194 if (error_code ec = MemoryBuffer::getFile(path, buffer)) {
Michael J. Spencerd4227232010-12-09 18:06:07 +0000195 errMsg = ec.message();
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000196 return NULL;
Michael J. Spencerd4227232010-12-09 18:06:07 +0000197 }
Rafael Espindola5b778b22011-03-18 19:51:00 +0000198 return makeLTOModule(buffer.take(), errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000199}
200
Rafael Espindola56e41f72011-02-08 22:40:47 +0000201LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
Bill Wendling5f689e72011-11-04 18:48:00 +0000202 size_t size, std::string &errMsg) {
Rafael Espindola3d2ac2e2013-07-23 20:25:01 +0000203 return makeLTOModule(fd, path, size, 0, errMsg);
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000204}
205
206LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000207 size_t map_size,
208 off_t offset,
Rafael Espindola56e41f72011-02-08 22:40:47 +0000209 std::string &errMsg) {
210 OwningPtr<MemoryBuffer> buffer;
Rafael Espindola3d2ac2e2013-07-23 20:25:01 +0000211 if (error_code ec =
212 MemoryBuffer::getOpenFileSlice(fd, path, buffer, map_size, offset)) {
Rafael Espindola56e41f72011-02-08 22:40:47 +0000213 errMsg = ec.message();
214 return NULL;
215 }
Rafael Espindola5b778b22011-03-18 19:51:00 +0000216 return makeLTOModule(buffer.take(), errMsg);
Rafael Espindola56e41f72011-02-08 22:40:47 +0000217}
218
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000219LTOModule *LTOModule::makeLTOModule(const void *mem, size_t length,
220 std::string &errMsg) {
221 OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length));
222 if (!buffer)
223 return NULL;
Rafael Espindola5b778b22011-03-18 19:51:00 +0000224 return makeLTOModule(buffer.take(), errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000225}
226
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000227void LTOModule::getTargetOptions(TargetOptions &Options) {
228 Options.LessPreciseFPMADOption = EnableFPMAD;
229 Options.NoFramePointerElim = DisableFPElim;
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000230 Options.AllowFPOpFusion = FuseFPOps;
231 Options.UnsafeFPMath = EnableUnsafeFPMath;
232 Options.NoInfsFPMath = EnableNoInfsFPMath;
233 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
234 Options.HonorSignDependentRoundingFPMathOption =
235 EnableHonorSignDependentRoundingFPMath;
236 Options.UseSoftFloat = GenerateSoftFloatCalls;
237 if (FloatABIForCalls != FloatABI::Default)
238 Options.FloatABIType = FloatABIForCalls;
239 Options.NoZerosInBSS = DontPlaceZerosInBSS;
240 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
241 Options.DisableTailCalls = DisableTailCalls;
242 Options.StackAlignmentOverride = OverrideStackAlignment;
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000243 Options.TrapFuncName = TrapFuncName;
244 Options.PositionIndependentExecutable = EnablePIE;
245 Options.EnableSegmentedStacks = SegmentedStacks;
246 Options.UseInitArray = UseInitArray;
247}
248
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000249LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer,
250 std::string &errMsg) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000251 static bool Initialized = false;
252 if (!Initialized) {
253 InitializeAllTargets();
Evan Cheng8c886a42011-07-22 21:58:54 +0000254 InitializeAllTargetMCs();
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000255 InitializeAllAsmParsers();
256 Initialized = true;
257 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000258
259 // parse bitcode buffer
Rafael Espindola5b778b22011-03-18 19:51:00 +0000260 OwningPtr<Module> m(getLazyBitcodeModule(buffer, getGlobalContext(),
261 &errMsg));
262 if (!m) {
263 delete buffer;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000264 return NULL;
Rafael Espindola5b778b22011-03-18 19:51:00 +0000265 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000266
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000267 std::string TripleStr = m->getTargetTriple();
268 if (TripleStr.empty())
269 TripleStr = sys::getDefaultTargetTriple();
270 llvm::Triple Triple(TripleStr);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000271
272 // find machine architecture for this module
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000273 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000274 if (!march)
275 return NULL;
276
Nick Lewycky364c04a2011-04-21 01:54:08 +0000277 // construct LTOModule, hand over ownership of module and target
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000278 SubtargetFeatures Features;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000279 Features.getDefaultSubtargetFeatures(Triple);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000280 std::string FeatureStr = Features.getString();
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000281 // Set a default CPU for Darwin triples.
Evan Chengfe6e4052011-06-30 01:53:36 +0000282 std::string CPU;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000283 if (Triple.isOSDarwin()) {
284 if (Triple.getArch() == llvm::Triple::x86_64)
285 CPU = "core2";
286 else if (Triple.getArch() == llvm::Triple::x86)
287 CPU = "yonah";
288 }
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000289 TargetOptions Options;
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000290 getTargetOptions(Options);
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000291 TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000292 Options);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000293 LTOModule *Ret = new LTOModule(m.take(), target);
Bill Wendling7e58b382012-03-28 23:12:18 +0000294 if (Ret->parseSymbols(errMsg)) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000295 delete Ret;
296 return NULL;
297 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000298
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000299 return Ret;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000300}
301
Bill Wendlingfb440502012-03-28 20:46:54 +0000302/// makeBuffer - Create a MemoryBuffer from a memory range.
303MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length) {
Roman Divackyad06cee2012-09-05 22:26:57 +0000304 const char *startPtr = (const char*)mem;
Bill Wendlingfb440502012-03-28 20:46:54 +0000305 return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), "", false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000306}
307
Bill Wendlingfb440502012-03-28 20:46:54 +0000308/// objcClassNameFromExpression - Get string that the data pointer points to.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000309bool
310LTOModule::objcClassNameFromExpression(const Constant *c, std::string &name) {
311 if (const ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000312 Constant *op = ce->getOperand(0);
313 if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) {
314 Constant *cn = gvn->getInitializer();
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000315 if (ConstantDataArray *ca = dyn_cast<ConstantDataArray>(cn)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000316 if (ca->isCString()) {
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000317 name = ".objc_class_name_" + ca->getAsCString().str();
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000318 return true;
Nick Kledzikb481c202009-06-01 20:33:09 +0000319 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000320 }
Nick Kledzikb481c202009-06-01 20:33:09 +0000321 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000322 }
323 return false;
324}
325
Bill Wendlingfb440502012-03-28 20:46:54 +0000326/// addObjCClass - Parse i386/ppc ObjC class data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000327void LTOModule::addObjCClass(const GlobalVariable *clgv) {
328 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000329 if (!c) return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000330
Bill Wendling5f689e72011-11-04 18:48:00 +0000331 // second slot in __OBJC,__class is pointer to superclass name
332 std::string superclassName;
333 if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
334 NameAndAttributes info;
335 StringMap<NameAndAttributes>::value_type &entry =
336 _undefines.GetOrCreateValue(superclassName);
337 if (!entry.getValue().name) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000338 const char *symbolName = entry.getKey().data();
339 info.name = symbolName;
340 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000341 info.isFunction = false;
342 info.symbol = clgv;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000343 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000344 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000345 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000346
347 // third slot in __OBJC,__class is pointer to class name
348 std::string className;
349 if (objcClassNameFromExpression(c->getOperand(2), className)) {
350 StringSet::value_type &entry = _defines.GetOrCreateValue(className);
351 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000352
Bill Wendling5f689e72011-11-04 18:48:00 +0000353 NameAndAttributes info;
354 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000355 info.attributes = LTO_SYMBOL_PERMISSIONS_DATA |
356 LTO_SYMBOL_DEFINITION_REGULAR | LTO_SYMBOL_SCOPE_DEFAULT;
357 info.isFunction = false;
358 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000359 _symbols.push_back(info);
360 }
361}
362
Bill Wendlingfb440502012-03-28 20:46:54 +0000363/// addObjCCategory - Parse i386/ppc ObjC category data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000364void LTOModule::addObjCCategory(const GlobalVariable *clgv) {
365 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000366 if (!c) return;
367
368 // second slot in __OBJC,__category is pointer to target class name
369 std::string targetclassName;
370 if (!objcClassNameFromExpression(c->getOperand(1), targetclassName))
371 return;
372
373 NameAndAttributes info;
374 StringMap<NameAndAttributes>::value_type &entry =
375 _undefines.GetOrCreateValue(targetclassName);
376
377 if (entry.getValue().name)
378 return;
379
380 const char *symbolName = entry.getKey().data();
381 info.name = symbolName;
382 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000383 info.isFunction = false;
384 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000385 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000386}
387
Bill Wendlingfb440502012-03-28 20:46:54 +0000388/// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000389void LTOModule::addObjCClassRef(const GlobalVariable *clgv) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000390 std::string targetclassName;
Bill Wendling5f689e72011-11-04 18:48:00 +0000391 if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName))
392 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000393
Bill Wendling5f689e72011-11-04 18:48:00 +0000394 NameAndAttributes info;
395 StringMap<NameAndAttributes>::value_type &entry =
396 _undefines.GetOrCreateValue(targetclassName);
397 if (entry.getValue().name)
398 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000399
Bill Wendling5f689e72011-11-04 18:48:00 +0000400 const char *symbolName = entry.getKey().data();
401 info.name = symbolName;
402 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000403 info.isFunction = false;
404 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000405 entry.setValue(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000406}
407
Bill Wendlingfb440502012-03-28 20:46:54 +0000408/// addDefinedDataSymbol - Add a data symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000409void LTOModule::addDefinedDataSymbol(const GlobalValue *v) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000410 // Add to list of defined symbols.
Bill Wendlinga2af6742011-11-04 09:30:19 +0000411 addDefinedSymbol(v, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000412
Bill Wendling45f74e32012-08-06 22:52:45 +0000413 if (!v->hasSection() /* || !isTargetDarwin */)
414 return;
415
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000416 // Special case i386/ppc ObjC data structures in magic sections:
417 // The issue is that the old ObjC object format did some strange
418 // contortions to avoid real linker symbols. For instance, the
419 // ObjC class data structure is allocated statically in the executable
420 // that defines that class. That data structures contains a pointer to
421 // its superclass. But instead of just initializing that part of the
422 // struct to the address of its superclass, and letting the static and
423 // dynamic linkers do the rest, the runtime works by having that field
424 // instead point to a C-string that is the name of the superclass.
425 // At runtime the objc initialization updates that pointer and sets
426 // it to point to the actual super class. As far as the linker
427 // knows it is just a pointer to a string. But then someone wanted the
428 // linker to issue errors at build time if the superclass was not found.
429 // So they figured out a way in mach-o object format to use an absolute
430 // symbols (.objc_class_name_Foo = 0) and a floating reference
431 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
432 // a class was missing.
433 // The following synthesizes the implicit .objc_* symbols for the linker
434 // from the ObjC data structures generated by the front end.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000435
Bill Wendling45f74e32012-08-06 22:52:45 +0000436 // special case if this data blob is an ObjC class definition
437 if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000438 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000439 addObjCClass(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000440 }
Bill Wendling45f74e32012-08-06 22:52:45 +0000441 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000442
Bill Wendling45f74e32012-08-06 22:52:45 +0000443 // special case if this data blob is an ObjC category definition
444 else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000445 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000446 addObjCCategory(gv);
447 }
448 }
449
450 // special case if this data blob is the list of referenced classes
451 else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000452 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000453 addObjCClassRef(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000454 }
455 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000456}
457
Bill Wendlingfb440502012-03-28 20:46:54 +0000458/// addDefinedFunctionSymbol - Add a function symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000459void LTOModule::addDefinedFunctionSymbol(const Function *f) {
Bill Wendlingfb440502012-03-28 20:46:54 +0000460 // add to list of defined symbols
461 addDefinedSymbol(f, true);
462}
463
464/// addDefinedSymbol - Add a defined symbol to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000465void LTOModule::addDefinedSymbol(const GlobalValue *def, bool isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000466 // ignore all llvm.* symbols
467 if (def->getName().startswith("llvm."))
468 return;
469
470 // string is owned by _defines
Rafael Espindola34b59382011-02-11 05:23:09 +0000471 SmallString<64> Buffer;
Bill Wendlinga2af6742011-11-04 09:30:19 +0000472 _mangler.getNameWithPrefix(Buffer, def, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000473
474 // set alignment part log2() can have rounding errors
475 uint32_t align = def->getAlignment();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000476 uint32_t attr = align ? countTrailingZeros(def->getAlignment()) : 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000477
478 // set permissions part
Bill Wendling9ee2d332012-03-29 08:27:32 +0000479 if (isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000480 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000481 } else {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000482 const GlobalVariable *gv = dyn_cast<GlobalVariable>(def);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000483 if (gv && gv->isConstant())
484 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
485 else
486 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
487 }
488
489 // set definition part
Bill Wendling2776d462010-09-27 18:05:19 +0000490 if (def->hasWeakLinkage() || def->hasLinkOnceLinkage() ||
Bill Wendling34bc34e2012-08-17 18:33:14 +0000491 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000492 attr |= LTO_SYMBOL_DEFINITION_WEAK;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000493 else if (def->hasCommonLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000494 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000495 else
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000496 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000497
498 // set scope part
499 if (def->hasHiddenVisibility())
500 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
501 else if (def->hasProtectedVisibility())
502 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000503 else if (def->hasExternalLinkage() || def->hasWeakLinkage() ||
504 def->hasLinkOnceLinkage() || def->hasCommonLinkage() ||
505 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000506 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
Bill Wendling34bc34e2012-08-17 18:33:14 +0000507 else if (def->hasLinkOnceODRAutoHideLinkage())
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000508 attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000509 else
510 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
511
Chad Rosier772a91f2011-06-28 18:26:12 +0000512 StringSet::value_type &entry = _defines.GetOrCreateValue(Buffer);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000513 entry.setValue(1);
514
Bill Wendling9ee2d332012-03-29 08:27:32 +0000515 // fill information structure
516 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000517 StringRef Name = entry.getKey();
518 info.name = Name.data();
519 assert(info.name[Name.size()] == '\0');
Bill Wendling9ee2d332012-03-29 08:27:32 +0000520 info.attributes = attr;
521 info.isFunction = isFunction;
522 info.symbol = def;
523
524 // add to table of symbols
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000525 _symbols.push_back(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000526}
527
Bill Wendlingfb440502012-03-28 20:46:54 +0000528/// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
529/// defined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000530void LTOModule::addAsmGlobalSymbol(const char *name,
531 lto_symbol_attributes scope) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000532 StringSet::value_type &entry = _defines.GetOrCreateValue(name);
533
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000534 // only add new define if not already defined
Rafael Espindola477d11f2011-02-20 16:27:25 +0000535 if (entry.getValue())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000536 return;
537
Rafael Espindola477d11f2011-02-20 16:27:25 +0000538 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000539
540 NameAndAttributes &info = _undefines[entry.getKey().data()];
541
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000542 if (info.symbol == 0) {
Bill Wendling71b19bb2012-04-02 10:01:21 +0000543 // FIXME: This is trying to take care of module ASM like this:
544 //
545 // module asm ".zerofill __FOO, __foo, _bar_baz_qux, 0"
546 //
547 // but is gross and its mother dresses it funny. Have the ASM parser give us
548 // more details for this type of situation so that we're not guessing so
549 // much.
550
551 // fill information structure
Rafael Espindola5f4b32f2012-05-11 03:42:13 +0000552 info.name = entry.getKey().data();
Bill Wendling71b19bb2012-04-02 10:01:21 +0000553 info.attributes =
554 LTO_SYMBOL_PERMISSIONS_DATA | LTO_SYMBOL_DEFINITION_REGULAR | scope;
555 info.isFunction = false;
556 info.symbol = 0;
557
558 // add to table of symbols
559 _symbols.push_back(info);
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000560 return;
561 }
562
Bill Wendling9ee2d332012-03-29 08:27:32 +0000563 if (info.isFunction)
564 addDefinedFunctionSymbol(cast<Function>(info.symbol));
565 else
566 addDefinedDataSymbol(info.symbol);
Bill Wendling8f6c8a92012-03-30 23:26:06 +0000567
568 _symbols.back().attributes &= ~LTO_SYMBOL_SCOPE_MASK;
569 _symbols.back().attributes |= scope;
Devang Patela59fe952008-07-16 18:06:52 +0000570}
Nick Kledzik07b4a622008-02-26 20:26:43 +0000571
Bill Wendlingfb440502012-03-28 20:46:54 +0000572/// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the
573/// undefined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000574void LTOModule::addAsmGlobalSymbolUndef(const char *name) {
575 StringMap<NameAndAttributes>::value_type &entry =
576 _undefines.GetOrCreateValue(name);
577
578 _asm_undefines.push_back(entry.getKey().data());
579
580 // we already have the symbol
581 if (entry.getValue().name)
582 return;
583
584 uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;;
585 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
586 NameAndAttributes info;
587 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000588 info.attributes = attr;
589 info.isFunction = false;
590 info.symbol = 0;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000591
592 entry.setValue(info);
593}
594
Bill Wendlingfb440502012-03-28 20:46:54 +0000595/// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet to a
596/// list to be resolved later.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000597void
598LTOModule::addPotentialUndefinedSymbol(const GlobalValue *decl, bool isFunc) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000599 // ignore all llvm.* symbols
600 if (decl->getName().startswith("llvm."))
601 return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000602
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000603 // ignore all aliases
604 if (isa<GlobalAlias>(decl))
605 return;
Nick Lewycky0661b932009-07-09 06:03:04 +0000606
Rafael Espindola34b59382011-02-11 05:23:09 +0000607 SmallString<64> name;
Bill Wendlinga2af6742011-11-04 09:30:19 +0000608 _mangler.getNameWithPrefix(name, decl, false);
Rafael Espindola56548522009-04-24 16:55:21 +0000609
Rafael Espindola477d11f2011-02-20 16:27:25 +0000610 StringMap<NameAndAttributes>::value_type &entry =
Chad Rosier772a91f2011-06-28 18:26:12 +0000611 _undefines.GetOrCreateValue(name);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000612
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000613 // we already have the symbol
Rafael Espindola477d11f2011-02-20 16:27:25 +0000614 if (entry.getValue().name)
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000615 return;
Rafael Espindola56548522009-04-24 16:55:21 +0000616
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000617 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000618
619 info.name = entry.getKey().data();
Bill Wendlingfb440502012-03-28 20:46:54 +0000620
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000621 if (decl->hasExternalWeakLinkage())
622 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
623 else
624 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000625
Bill Wendling9ee2d332012-03-29 08:27:32 +0000626 info.isFunction = isFunc;
627 info.symbol = decl;
628
Rafael Espindola477d11f2011-02-20 16:27:25 +0000629 entry.setValue(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000630}
631
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000632namespace {
633 class RecordStreamer : public MCStreamer {
634 public:
Bill Wendling32867652012-04-03 03:56:52 +0000635 enum State { NeverSeen, Global, Defined, DefinedGlobal, Used };
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000636
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000637 private:
638 StringMap<State> Symbols;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000639
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000640 void markDefined(const MCSymbol &Symbol) {
641 State &S = Symbols[Symbol.getName()];
642 switch (S) {
643 case DefinedGlobal:
644 case Global:
645 S = DefinedGlobal;
646 break;
647 case NeverSeen:
648 case Defined:
649 case Used:
650 S = Defined;
651 break;
652 }
653 }
654 void markGlobal(const MCSymbol &Symbol) {
655 State &S = Symbols[Symbol.getName()];
656 switch (S) {
657 case DefinedGlobal:
658 case Defined:
659 S = DefinedGlobal;
660 break;
661
662 case NeverSeen:
663 case Global:
664 case Used:
665 S = Global;
666 break;
667 }
668 }
669 void markUsed(const MCSymbol &Symbol) {
670 State &S = Symbols[Symbol.getName()];
671 switch (S) {
672 case DefinedGlobal:
673 case Defined:
674 case Global:
675 break;
676
677 case NeverSeen:
678 case Used:
679 S = Used;
680 break;
681 }
682 }
683
684 // FIXME: mostly copied for the obj streamer.
685 void AddValueSymbols(const MCExpr *Value) {
686 switch (Value->getKind()) {
687 case MCExpr::Target:
688 // FIXME: What should we do in here?
689 break;
690
691 case MCExpr::Constant:
692 break;
693
694 case MCExpr::Binary: {
695 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
696 AddValueSymbols(BE->getLHS());
697 AddValueSymbols(BE->getRHS());
698 break;
699 }
700
701 case MCExpr::SymbolRef:
702 markUsed(cast<MCSymbolRefExpr>(Value)->getSymbol());
703 break;
704
705 case MCExpr::Unary:
706 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
707 break;
708 }
709 }
710
711 public:
712 typedef StringMap<State>::const_iterator const_iterator;
713
714 const_iterator begin() {
715 return Symbols.begin();
716 }
717
718 const_iterator end() {
719 return Symbols.end();
720 }
721
Chandler Carruthde093ef2013-01-31 23:29:57 +0000722 RecordStreamer(MCContext &Context)
723 : MCStreamer(SK_RecordStreamer, Context) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000724
Bill Wendling32867652012-04-03 03:56:52 +0000725 virtual void EmitInstruction(const MCInst &Inst) {
726 // Scan for values.
727 for (unsigned i = Inst.getNumOperands(); i--; )
728 if (Inst.getOperand(i).isExpr())
729 AddValueSymbols(Inst.getOperand(i).getExpr());
730 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000731 virtual void EmitLabel(MCSymbol *Symbol) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000732 Symbol->setSection(*getCurrentSection().first);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000733 markDefined(*Symbol);
734 }
Reed Kotleraee4d5d12012-12-16 04:00:45 +0000735 virtual void EmitDebugLabel(MCSymbol *Symbol) {
736 EmitLabel(Symbol);
737 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000738 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
739 // FIXME: should we handle aliases?
740 markDefined(*Symbol);
741 }
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000742 virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000743 if (Attribute == MCSA_Global)
744 markGlobal(*Symbol);
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000745 return true;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000746 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000747 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Cheng95847992012-06-22 20:30:39 +0000748 uint64_t Size , unsigned ByteAlignment) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000749 markDefined(*Symbol);
750 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000751 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
752 unsigned ByteAlignment) {
753 markDefined(*Symbol);
754 }
Bill Wendling32867652012-04-03 03:56:52 +0000755
Eli Benderskyf483ff92012-12-20 19:05:53 +0000756 virtual void EmitBundleAlignMode(unsigned AlignPow2) {}
Eli Bendersky802b6282013-01-07 21:51:08 +0000757 virtual void EmitBundleLock(bool AlignToEnd) {}
Eli Benderskyf483ff92012-12-20 19:05:53 +0000758 virtual void EmitBundleUnlock() {}
759
Bill Wendling32867652012-04-03 03:56:52 +0000760 // Noop calls.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000761 virtual void ChangeSection(const MCSection *Section,
762 const MCExpr *Subsection) {}
Eli Benderskycbb25142013-01-14 19:04:57 +0000763 virtual void InitToTextSection() {}
Bill Wendling32867652012-04-03 03:56:52 +0000764 virtual void InitSections() {}
765 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {}
766 virtual void EmitThumbFunc(MCSymbol *Func) {}
767 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {}
768 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {}
769 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {}
770 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {}
771 virtual void EmitCOFFSymbolType(int Type) {}
772 virtual void EndCOFFSymbolDef() {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000773 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {}
Benjamin Kramer63970512011-09-01 23:04:27 +0000774 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
775 unsigned ByteAlignment) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000776 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
777 uint64_t Size, unsigned ByteAlignment) {}
Rafael Espindola64e1af82013-07-02 15:49:13 +0000778 virtual void EmitBytes(StringRef Data) {}
779 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {}
Rafael Espindola6aea5922011-04-21 23:39:26 +0000780 virtual void EmitULEB128Value(const MCExpr *Value) {}
781 virtual void EmitSLEB128Value(const MCExpr *Value) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000782 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
783 unsigned ValueSize,
784 unsigned MaxBytesToEmit) {}
785 virtual void EmitCodeAlignment(unsigned ByteAlignment,
786 unsigned MaxBytesToEmit) {}
Jim Grosbachb5912772012-01-27 00:37:08 +0000787 virtual bool EmitValueToOffset(const MCExpr *Offset,
788 unsigned char Value ) { return false; }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000789 virtual void EmitFileDirective(StringRef Filename) {}
790 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
791 const MCSymbol *LastLabel,
Evan Chengc7ac6902011-07-14 05:43:07 +0000792 const MCSymbol *Label,
793 unsigned PointerSize) {}
Rafael Espindola07082092012-01-07 03:13:18 +0000794 virtual void FinishImpl() {}
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000795 virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
796 RecordProcEnd(Frame);
797 }
Chandler Carruthde093ef2013-01-31 23:29:57 +0000798
799 static bool classof(const MCStreamer *S) {
Chandler Carruth30cfaa22013-01-31 23:34:47 +0000800 return S->getKind() == SK_RecordStreamer;
Chandler Carruthde093ef2013-01-31 23:29:57 +0000801 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000802 };
Bill Wendling9ee2d332012-03-29 08:27:32 +0000803} // end anonymous namespace
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000804
Bill Wendlingfb440502012-03-28 20:46:54 +0000805/// addAsmGlobalSymbols - Add global symbols from module-level ASM to the
806/// defined or undefined lists.
Bill Wendlingac2abde2011-11-04 09:24:40 +0000807bool LTOModule::addAsmGlobalSymbols(std::string &errMsg) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000808 const std::string &inlineAsm = _module->getModuleInlineAsm();
Ivan Krasincc2a8012011-09-08 07:38:25 +0000809 if (inlineAsm.empty())
810 return false;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000811
Bill Wendlingac2abde2011-11-04 09:24:40 +0000812 OwningPtr<RecordStreamer> Streamer(new RecordStreamer(_context));
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000813 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(inlineAsm);
814 SourceMgr SrcMgr;
815 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach345768c2011-08-16 18:33:49 +0000816 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr,
Bill Wendlingac2abde2011-11-04 09:24:40 +0000817 _context, *Streamer,
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000818 *_target->getMCAsmInfo()));
Bill Wendling9351b3e2012-08-08 22:01:55 +0000819 const Target &T = _target->getTarget();
Joey Goulydb6144e2013-09-12 12:55:29 +0000820 OwningPtr<MCInstrInfo> MCII(T.createMCInstrInfo());
Bill Wendling9351b3e2012-08-08 22:01:55 +0000821 OwningPtr<MCSubtargetInfo>
822 STI(T.createMCSubtargetInfo(_target->getTargetTriple(),
823 _target->getTargetCPU(),
824 _target->getTargetFeatureString()));
Joey Goulydb6144e2013-09-12 12:55:29 +0000825 OwningPtr<MCTargetAsmParser> TAP(T.createMCAsmParser(*STI, *Parser.get(), *MCII));
Ivan Krasin8149dd62011-09-08 07:36:39 +0000826 if (!TAP) {
Bill Wendling9351b3e2012-08-08 22:01:55 +0000827 errMsg = "target " + std::string(T.getName()) +
828 " does not define AsmParser.";
Ivan Krasin8149dd62011-09-08 07:36:39 +0000829 return true;
830 }
831
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000832 Parser->setTargetParser(*TAP);
Bill Wendling9351b3e2012-08-08 22:01:55 +0000833 if (Parser->Run(false))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000834 return true;
835
836 for (RecordStreamer::const_iterator i = Streamer->begin(),
837 e = Streamer->end(); i != e; ++i) {
838 StringRef Key = i->first();
839 RecordStreamer::State Value = i->second;
840 if (Value == RecordStreamer::DefinedGlobal)
841 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_DEFAULT);
842 else if (Value == RecordStreamer::Defined)
843 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_INTERNAL);
844 else if (Value == RecordStreamer::Global ||
845 Value == RecordStreamer::Used)
846 addAsmGlobalSymbolUndef(Key.data());
847 }
Bill Wendling9351b3e2012-08-08 22:01:55 +0000848
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000849 return false;
850}
851
Bill Wendlingfb440502012-03-28 20:46:54 +0000852/// isDeclaration - Return 'true' if the global value is a declaration.
Rafael Espindola5b778b22011-03-18 19:51:00 +0000853static bool isDeclaration(const GlobalValue &V) {
854 if (V.hasAvailableExternallyLinkage())
855 return true;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000856
Rafael Espindola5b778b22011-03-18 19:51:00 +0000857 if (V.isMaterializable())
858 return false;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000859
Rafael Espindola5b778b22011-03-18 19:51:00 +0000860 return V.isDeclaration();
861}
862
Bill Wendling7e58b382012-03-28 23:12:18 +0000863/// parseSymbols - Parse the symbols from the module and model-level ASM and add
Bill Wendlingfb440502012-03-28 20:46:54 +0000864/// them to either the defined or undefined lists.
Bill Wendling7e58b382012-03-28 23:12:18 +0000865bool LTOModule::parseSymbols(std::string &errMsg) {
Daniel Dunbar919660b2010-08-10 23:46:46 +0000866 // add functions
Bill Wendling763acfc2012-03-29 03:34:57 +0000867 for (Module::iterator f = _module->begin(), e = _module->end(); f != e; ++f) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000868 if (isDeclaration(*f))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000869 addPotentialUndefinedSymbol(f, true);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000870 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000871 addDefinedFunctionSymbol(f);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000872 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000873
Daniel Dunbar919660b2010-08-10 23:46:46 +0000874 // add data
875 for (Module::global_iterator v = _module->global_begin(),
876 e = _module->global_end(); v != e; ++v) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000877 if (isDeclaration(*v))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000878 addPotentialUndefinedSymbol(v, false);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000879 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000880 addDefinedDataSymbol(v);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000881 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000882
Daniel Dunbar919660b2010-08-10 23:46:46 +0000883 // add asm globals
Bill Wendlingac2abde2011-11-04 09:24:40 +0000884 if (addAsmGlobalSymbols(errMsg))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000885 return true;
Daniel Dunbar919660b2010-08-10 23:46:46 +0000886
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000887 // add aliases
Bill Wendling9ee2d332012-03-29 08:27:32 +0000888 for (Module::alias_iterator a = _module->alias_begin(),
889 e = _module->alias_end(); a != e; ++a) {
890 if (isDeclaration(*a->getAliasedGlobal()))
Bill Wendlingd58ed732012-03-28 20:48:49 +0000891 // Is an alias to a declaration.
Bill Wendling9ee2d332012-03-29 08:27:32 +0000892 addPotentialUndefinedSymbol(a, false);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000893 else
Bill Wendling9ee2d332012-03-29 08:27:32 +0000894 addDefinedDataSymbol(a);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000895 }
896
Daniel Dunbar919660b2010-08-10 23:46:46 +0000897 // make symbols for all undefines
Bill Wendling9ee2d332012-03-29 08:27:32 +0000898 for (StringMap<NameAndAttributes>::iterator u =_undefines.begin(),
899 e = _undefines.end(); u != e; ++u) {
900 // If this symbol also has a definition, then don't make an undefine because
901 // it is a tentative definition.
902 if (_defines.count(u->getKey())) continue;
903 NameAndAttributes info = u->getValue();
904 _symbols.push_back(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000905 }
Bill Wendling9ee2d332012-03-29 08:27:32 +0000906
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000907 return false;
Nick Kledzik91a6dcf2008-02-27 22:25:36 +0000908}