blob: 6626eaac47a8a7ab44d206cc1a0ecda04cb7bbf4 [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
Bill Wendlingfb440502012-03-28 20:46:54 +0000154LTOModule::LTOModule(llvm::Module *m, llvm::TargetMachine *t)
155 : _module(m), _target(t),
Bill Wendlingbc07a892013-06-18 07:20:20 +0000156 _context(_target->getMCAsmInfo(), _target->getRegisterInfo(), NULL),
Bill Wendling70b14002013-05-29 20:37:19 +0000157 _mangler(_context, t) {}
Bill Wendlingfb440502012-03-28 20:46:54 +0000158
159/// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM
160/// bitcode.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000161bool LTOModule::isBitcodeFile(const void *mem, size_t length) {
Rafael Espindola46ed3532013-06-11 18:05:26 +0000162 return sys::fs::identify_magic(StringRef((const char *)mem, length)) ==
163 sys::fs::file_magic::bitcode;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000164}
165
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000166bool LTOModule::isBitcodeFile(const char *path) {
Rafael Espindola71affba2013-06-12 15:13:57 +0000167 sys::fs::file_magic type;
168 if (sys::fs::identify_magic(path, type))
169 return false;
170 return type == sys::fs::file_magic::bitcode;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000171}
172
Bill Wendlingfb440502012-03-28 20:46:54 +0000173/// isBitcodeFileForTarget - Returns 'true' if the file (or memory contents) is
174/// LLVM bitcode for the specified triple.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000175bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length,
176 const char *triplePrefix) {
177 MemoryBuffer *buffer = makeBuffer(mem, length);
178 if (!buffer)
Nick Kledzikb481c202009-06-01 20:33:09 +0000179 return false;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000180 return isTargetMatch(buffer, triplePrefix);
Nick Kledzikb481c202009-06-01 20:33:09 +0000181}
182
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000183bool LTOModule::isBitcodeFileForTarget(const char *path,
184 const char *triplePrefix) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000185 OwningPtr<MemoryBuffer> buffer;
186 if (MemoryBuffer::getFile(path, buffer))
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000187 return false;
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000188 return isTargetMatch(buffer.take(), triplePrefix);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000189}
190
Bill Wendlingfb440502012-03-28 20:46:54 +0000191/// isTargetMatch - Returns 'true' if the memory buffer is for the specified
192/// target triple.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000193bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) {
Bill Wendling0198ce02010-10-06 01:22:42 +0000194 std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
195 delete buffer;
Bill Wendling5f689e72011-11-04 18:48:00 +0000196 return strncmp(Triple.c_str(), triplePrefix, strlen(triplePrefix)) == 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000197}
198
Bill Wendlingfb440502012-03-28 20:46:54 +0000199/// makeLTOModule - Create an LTOModule. N.B. These methods take ownership of
200/// the buffer.
201LTOModule *LTOModule::makeLTOModule(const char *path, std::string &errMsg) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000202 OwningPtr<MemoryBuffer> buffer;
203 if (error_code ec = MemoryBuffer::getFile(path, buffer)) {
Michael J. Spencerd4227232010-12-09 18:06:07 +0000204 errMsg = ec.message();
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000205 return NULL;
Michael J. Spencerd4227232010-12-09 18:06:07 +0000206 }
Rafael Espindola5b778b22011-03-18 19:51:00 +0000207 return makeLTOModule(buffer.take(), errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000208}
209
Rafael Espindola56e41f72011-02-08 22:40:47 +0000210LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
Bill Wendling5f689e72011-11-04 18:48:00 +0000211 size_t size, std::string &errMsg) {
Rafael Espindola3d2ac2e2013-07-23 20:25:01 +0000212 return makeLTOModule(fd, path, size, 0, errMsg);
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000213}
214
215LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000216 size_t map_size,
217 off_t offset,
Rafael Espindola56e41f72011-02-08 22:40:47 +0000218 std::string &errMsg) {
219 OwningPtr<MemoryBuffer> buffer;
Rafael Espindola3d2ac2e2013-07-23 20:25:01 +0000220 if (error_code ec =
221 MemoryBuffer::getOpenFileSlice(fd, path, buffer, map_size, offset)) {
Rafael Espindola56e41f72011-02-08 22:40:47 +0000222 errMsg = ec.message();
223 return NULL;
224 }
Rafael Espindola5b778b22011-03-18 19:51:00 +0000225 return makeLTOModule(buffer.take(), errMsg);
Rafael Espindola56e41f72011-02-08 22:40:47 +0000226}
227
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000228LTOModule *LTOModule::makeLTOModule(const void *mem, size_t length,
229 std::string &errMsg) {
230 OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length));
231 if (!buffer)
232 return NULL;
Rafael Espindola5b778b22011-03-18 19:51:00 +0000233 return makeLTOModule(buffer.take(), errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000234}
235
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000236void LTOModule::getTargetOptions(TargetOptions &Options) {
237 Options.LessPreciseFPMADOption = EnableFPMAD;
238 Options.NoFramePointerElim = DisableFPElim;
239 Options.NoFramePointerElimNonLeaf = DisableFPElimNonLeaf;
240 Options.AllowFPOpFusion = FuseFPOps;
241 Options.UnsafeFPMath = EnableUnsafeFPMath;
242 Options.NoInfsFPMath = EnableNoInfsFPMath;
243 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
244 Options.HonorSignDependentRoundingFPMathOption =
245 EnableHonorSignDependentRoundingFPMath;
246 Options.UseSoftFloat = GenerateSoftFloatCalls;
247 if (FloatABIForCalls != FloatABI::Default)
248 Options.FloatABIType = FloatABIForCalls;
249 Options.NoZerosInBSS = DontPlaceZerosInBSS;
250 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
251 Options.DisableTailCalls = DisableTailCalls;
252 Options.StackAlignmentOverride = OverrideStackAlignment;
253 Options.RealignStack = EnableRealignStack;
254 Options.TrapFuncName = TrapFuncName;
255 Options.PositionIndependentExecutable = EnablePIE;
256 Options.EnableSegmentedStacks = SegmentedStacks;
257 Options.UseInitArray = UseInitArray;
258}
259
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000260LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer,
261 std::string &errMsg) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000262 static bool Initialized = false;
263 if (!Initialized) {
264 InitializeAllTargets();
Evan Cheng8c886a42011-07-22 21:58:54 +0000265 InitializeAllTargetMCs();
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000266 InitializeAllAsmParsers();
267 Initialized = true;
268 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000269
270 // parse bitcode buffer
Rafael Espindola5b778b22011-03-18 19:51:00 +0000271 OwningPtr<Module> m(getLazyBitcodeModule(buffer, getGlobalContext(),
272 &errMsg));
273 if (!m) {
274 delete buffer;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000275 return NULL;
Rafael Espindola5b778b22011-03-18 19:51:00 +0000276 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000277
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000278 std::string TripleStr = m->getTargetTriple();
279 if (TripleStr.empty())
280 TripleStr = sys::getDefaultTargetTriple();
281 llvm::Triple Triple(TripleStr);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000282
283 // find machine architecture for this module
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000284 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000285 if (!march)
286 return NULL;
287
Nick Lewycky364c04a2011-04-21 01:54:08 +0000288 // construct LTOModule, hand over ownership of module and target
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000289 SubtargetFeatures Features;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000290 Features.getDefaultSubtargetFeatures(Triple);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000291 std::string FeatureStr = Features.getString();
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000292 // Set a default CPU for Darwin triples.
Evan Chengfe6e4052011-06-30 01:53:36 +0000293 std::string CPU;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000294 if (Triple.isOSDarwin()) {
295 if (Triple.getArch() == llvm::Triple::x86_64)
296 CPU = "core2";
297 else if (Triple.getArch() == llvm::Triple::x86)
298 CPU = "yonah";
299 }
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000300 TargetOptions Options;
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000301 getTargetOptions(Options);
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000302 TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000303 Options);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000304 LTOModule *Ret = new LTOModule(m.take(), target);
Bill Wendling7e58b382012-03-28 23:12:18 +0000305 if (Ret->parseSymbols(errMsg)) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000306 delete Ret;
307 return NULL;
308 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000309
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000310 return Ret;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000311}
312
Bill Wendlingfb440502012-03-28 20:46:54 +0000313/// makeBuffer - Create a MemoryBuffer from a memory range.
314MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length) {
Roman Divackyad06cee2012-09-05 22:26:57 +0000315 const char *startPtr = (const char*)mem;
Bill Wendlingfb440502012-03-28 20:46:54 +0000316 return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), "", false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000317}
318
Bill Wendlingfb440502012-03-28 20:46:54 +0000319/// objcClassNameFromExpression - Get string that the data pointer points to.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000320bool
321LTOModule::objcClassNameFromExpression(const Constant *c, std::string &name) {
322 if (const ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000323 Constant *op = ce->getOperand(0);
324 if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) {
325 Constant *cn = gvn->getInitializer();
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000326 if (ConstantDataArray *ca = dyn_cast<ConstantDataArray>(cn)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000327 if (ca->isCString()) {
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000328 name = ".objc_class_name_" + ca->getAsCString().str();
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000329 return true;
Nick Kledzikb481c202009-06-01 20:33:09 +0000330 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000331 }
Nick Kledzikb481c202009-06-01 20:33:09 +0000332 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000333 }
334 return false;
335}
336
Bill Wendlingfb440502012-03-28 20:46:54 +0000337/// addObjCClass - Parse i386/ppc ObjC class data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000338void LTOModule::addObjCClass(const GlobalVariable *clgv) {
339 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000340 if (!c) return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000341
Bill Wendling5f689e72011-11-04 18:48:00 +0000342 // second slot in __OBJC,__class is pointer to superclass name
343 std::string superclassName;
344 if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
345 NameAndAttributes info;
346 StringMap<NameAndAttributes>::value_type &entry =
347 _undefines.GetOrCreateValue(superclassName);
348 if (!entry.getValue().name) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000349 const char *symbolName = entry.getKey().data();
350 info.name = symbolName;
351 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000352 info.isFunction = false;
353 info.symbol = clgv;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000354 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000355 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000356 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000357
358 // third slot in __OBJC,__class is pointer to class name
359 std::string className;
360 if (objcClassNameFromExpression(c->getOperand(2), className)) {
361 StringSet::value_type &entry = _defines.GetOrCreateValue(className);
362 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000363
Bill Wendling5f689e72011-11-04 18:48:00 +0000364 NameAndAttributes info;
365 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000366 info.attributes = LTO_SYMBOL_PERMISSIONS_DATA |
367 LTO_SYMBOL_DEFINITION_REGULAR | LTO_SYMBOL_SCOPE_DEFAULT;
368 info.isFunction = false;
369 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000370 _symbols.push_back(info);
371 }
372}
373
Bill Wendlingfb440502012-03-28 20:46:54 +0000374/// addObjCCategory - Parse i386/ppc ObjC category data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000375void LTOModule::addObjCCategory(const GlobalVariable *clgv) {
376 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000377 if (!c) return;
378
379 // second slot in __OBJC,__category is pointer to target class name
380 std::string targetclassName;
381 if (!objcClassNameFromExpression(c->getOperand(1), targetclassName))
382 return;
383
384 NameAndAttributes info;
385 StringMap<NameAndAttributes>::value_type &entry =
386 _undefines.GetOrCreateValue(targetclassName);
387
388 if (entry.getValue().name)
389 return;
390
391 const char *symbolName = entry.getKey().data();
392 info.name = symbolName;
393 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000394 info.isFunction = false;
395 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000396 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000397}
398
Bill Wendlingfb440502012-03-28 20:46:54 +0000399/// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000400void LTOModule::addObjCClassRef(const GlobalVariable *clgv) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000401 std::string targetclassName;
Bill Wendling5f689e72011-11-04 18:48:00 +0000402 if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName))
403 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000404
Bill Wendling5f689e72011-11-04 18:48:00 +0000405 NameAndAttributes info;
406 StringMap<NameAndAttributes>::value_type &entry =
407 _undefines.GetOrCreateValue(targetclassName);
408 if (entry.getValue().name)
409 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000410
Bill Wendling5f689e72011-11-04 18:48:00 +0000411 const char *symbolName = entry.getKey().data();
412 info.name = symbolName;
413 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000414 info.isFunction = false;
415 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000416 entry.setValue(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000417}
418
Bill Wendlingfb440502012-03-28 20:46:54 +0000419/// addDefinedDataSymbol - Add a data symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000420void LTOModule::addDefinedDataSymbol(const GlobalValue *v) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000421 // Add to list of defined symbols.
Bill Wendlinga2af6742011-11-04 09:30:19 +0000422 addDefinedSymbol(v, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000423
Bill Wendling45f74e32012-08-06 22:52:45 +0000424 if (!v->hasSection() /* || !isTargetDarwin */)
425 return;
426
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000427 // Special case i386/ppc ObjC data structures in magic sections:
428 // The issue is that the old ObjC object format did some strange
429 // contortions to avoid real linker symbols. For instance, the
430 // ObjC class data structure is allocated statically in the executable
431 // that defines that class. That data structures contains a pointer to
432 // its superclass. But instead of just initializing that part of the
433 // struct to the address of its superclass, and letting the static and
434 // dynamic linkers do the rest, the runtime works by having that field
435 // instead point to a C-string that is the name of the superclass.
436 // At runtime the objc initialization updates that pointer and sets
437 // it to point to the actual super class. As far as the linker
438 // knows it is just a pointer to a string. But then someone wanted the
439 // linker to issue errors at build time if the superclass was not found.
440 // So they figured out a way in mach-o object format to use an absolute
441 // symbols (.objc_class_name_Foo = 0) and a floating reference
442 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
443 // a class was missing.
444 // The following synthesizes the implicit .objc_* symbols for the linker
445 // from the ObjC data structures generated by the front end.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000446
Bill Wendling45f74e32012-08-06 22:52:45 +0000447 // special case if this data blob is an ObjC class definition
448 if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000449 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000450 addObjCClass(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000451 }
Bill Wendling45f74e32012-08-06 22:52:45 +0000452 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000453
Bill Wendling45f74e32012-08-06 22:52:45 +0000454 // special case if this data blob is an ObjC category definition
455 else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000456 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000457 addObjCCategory(gv);
458 }
459 }
460
461 // special case if this data blob is the list of referenced classes
462 else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000463 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000464 addObjCClassRef(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000465 }
466 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000467}
468
Bill Wendlingfb440502012-03-28 20:46:54 +0000469/// addDefinedFunctionSymbol - Add a function symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000470void LTOModule::addDefinedFunctionSymbol(const Function *f) {
Bill Wendlingfb440502012-03-28 20:46:54 +0000471 // add to list of defined symbols
472 addDefinedSymbol(f, true);
473}
474
475/// addDefinedSymbol - Add a defined symbol to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000476void LTOModule::addDefinedSymbol(const GlobalValue *def, bool isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000477 // ignore all llvm.* symbols
478 if (def->getName().startswith("llvm."))
479 return;
480
481 // string is owned by _defines
Rafael Espindola34b59382011-02-11 05:23:09 +0000482 SmallString<64> Buffer;
Bill Wendlinga2af6742011-11-04 09:30:19 +0000483 _mangler.getNameWithPrefix(Buffer, def, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000484
485 // set alignment part log2() can have rounding errors
486 uint32_t align = def->getAlignment();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000487 uint32_t attr = align ? countTrailingZeros(def->getAlignment()) : 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000488
489 // set permissions part
Bill Wendling9ee2d332012-03-29 08:27:32 +0000490 if (isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000491 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000492 } else {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000493 const GlobalVariable *gv = dyn_cast<GlobalVariable>(def);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000494 if (gv && gv->isConstant())
495 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
496 else
497 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
498 }
499
500 // set definition part
Bill Wendling2776d462010-09-27 18:05:19 +0000501 if (def->hasWeakLinkage() || def->hasLinkOnceLinkage() ||
Bill Wendling34bc34e2012-08-17 18:33:14 +0000502 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000503 attr |= LTO_SYMBOL_DEFINITION_WEAK;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000504 else if (def->hasCommonLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000505 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000506 else
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000507 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000508
509 // set scope part
510 if (def->hasHiddenVisibility())
511 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
512 else if (def->hasProtectedVisibility())
513 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000514 else if (def->hasExternalLinkage() || def->hasWeakLinkage() ||
515 def->hasLinkOnceLinkage() || def->hasCommonLinkage() ||
516 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000517 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
Bill Wendling34bc34e2012-08-17 18:33:14 +0000518 else if (def->hasLinkOnceODRAutoHideLinkage())
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000519 attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000520 else
521 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
522
Chad Rosier772a91f2011-06-28 18:26:12 +0000523 StringSet::value_type &entry = _defines.GetOrCreateValue(Buffer);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000524 entry.setValue(1);
525
Bill Wendling9ee2d332012-03-29 08:27:32 +0000526 // fill information structure
527 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000528 StringRef Name = entry.getKey();
529 info.name = Name.data();
530 assert(info.name[Name.size()] == '\0');
Bill Wendling9ee2d332012-03-29 08:27:32 +0000531 info.attributes = attr;
532 info.isFunction = isFunction;
533 info.symbol = def;
534
535 // add to table of symbols
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000536 _symbols.push_back(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000537}
538
Bill Wendlingfb440502012-03-28 20:46:54 +0000539/// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
540/// defined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000541void LTOModule::addAsmGlobalSymbol(const char *name,
542 lto_symbol_attributes scope) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000543 StringSet::value_type &entry = _defines.GetOrCreateValue(name);
544
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000545 // only add new define if not already defined
Rafael Espindola477d11f2011-02-20 16:27:25 +0000546 if (entry.getValue())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000547 return;
548
Rafael Espindola477d11f2011-02-20 16:27:25 +0000549 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000550
551 NameAndAttributes &info = _undefines[entry.getKey().data()];
552
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000553 if (info.symbol == 0) {
Bill Wendling71b19bb2012-04-02 10:01:21 +0000554 // FIXME: This is trying to take care of module ASM like this:
555 //
556 // module asm ".zerofill __FOO, __foo, _bar_baz_qux, 0"
557 //
558 // but is gross and its mother dresses it funny. Have the ASM parser give us
559 // more details for this type of situation so that we're not guessing so
560 // much.
561
562 // fill information structure
Rafael Espindola5f4b32f2012-05-11 03:42:13 +0000563 info.name = entry.getKey().data();
Bill Wendling71b19bb2012-04-02 10:01:21 +0000564 info.attributes =
565 LTO_SYMBOL_PERMISSIONS_DATA | LTO_SYMBOL_DEFINITION_REGULAR | scope;
566 info.isFunction = false;
567 info.symbol = 0;
568
569 // add to table of symbols
570 _symbols.push_back(info);
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000571 return;
572 }
573
Bill Wendling9ee2d332012-03-29 08:27:32 +0000574 if (info.isFunction)
575 addDefinedFunctionSymbol(cast<Function>(info.symbol));
576 else
577 addDefinedDataSymbol(info.symbol);
Bill Wendling8f6c8a92012-03-30 23:26:06 +0000578
579 _symbols.back().attributes &= ~LTO_SYMBOL_SCOPE_MASK;
580 _symbols.back().attributes |= scope;
Devang Patela59fe952008-07-16 18:06:52 +0000581}
Nick Kledzik07b4a622008-02-26 20:26:43 +0000582
Bill Wendlingfb440502012-03-28 20:46:54 +0000583/// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the
584/// undefined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000585void LTOModule::addAsmGlobalSymbolUndef(const char *name) {
586 StringMap<NameAndAttributes>::value_type &entry =
587 _undefines.GetOrCreateValue(name);
588
589 _asm_undefines.push_back(entry.getKey().data());
590
591 // we already have the symbol
592 if (entry.getValue().name)
593 return;
594
595 uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;;
596 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
597 NameAndAttributes info;
598 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000599 info.attributes = attr;
600 info.isFunction = false;
601 info.symbol = 0;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000602
603 entry.setValue(info);
604}
605
Bill Wendlingfb440502012-03-28 20:46:54 +0000606/// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet to a
607/// list to be resolved later.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000608void
609LTOModule::addPotentialUndefinedSymbol(const GlobalValue *decl, bool isFunc) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000610 // ignore all llvm.* symbols
611 if (decl->getName().startswith("llvm."))
612 return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000613
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000614 // ignore all aliases
615 if (isa<GlobalAlias>(decl))
616 return;
Nick Lewycky0661b932009-07-09 06:03:04 +0000617
Rafael Espindola34b59382011-02-11 05:23:09 +0000618 SmallString<64> name;
Bill Wendlinga2af6742011-11-04 09:30:19 +0000619 _mangler.getNameWithPrefix(name, decl, false);
Rafael Espindola56548522009-04-24 16:55:21 +0000620
Rafael Espindola477d11f2011-02-20 16:27:25 +0000621 StringMap<NameAndAttributes>::value_type &entry =
Chad Rosier772a91f2011-06-28 18:26:12 +0000622 _undefines.GetOrCreateValue(name);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000623
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000624 // we already have the symbol
Rafael Espindola477d11f2011-02-20 16:27:25 +0000625 if (entry.getValue().name)
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000626 return;
Rafael Espindola56548522009-04-24 16:55:21 +0000627
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000628 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000629
630 info.name = entry.getKey().data();
Bill Wendlingfb440502012-03-28 20:46:54 +0000631
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000632 if (decl->hasExternalWeakLinkage())
633 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
634 else
635 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000636
Bill Wendling9ee2d332012-03-29 08:27:32 +0000637 info.isFunction = isFunc;
638 info.symbol = decl;
639
Rafael Espindola477d11f2011-02-20 16:27:25 +0000640 entry.setValue(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000641}
642
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000643namespace {
644 class RecordStreamer : public MCStreamer {
645 public:
Bill Wendling32867652012-04-03 03:56:52 +0000646 enum State { NeverSeen, Global, Defined, DefinedGlobal, Used };
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000647
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000648 private:
649 StringMap<State> Symbols;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000650
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000651 void markDefined(const MCSymbol &Symbol) {
652 State &S = Symbols[Symbol.getName()];
653 switch (S) {
654 case DefinedGlobal:
655 case Global:
656 S = DefinedGlobal;
657 break;
658 case NeverSeen:
659 case Defined:
660 case Used:
661 S = Defined;
662 break;
663 }
664 }
665 void markGlobal(const MCSymbol &Symbol) {
666 State &S = Symbols[Symbol.getName()];
667 switch (S) {
668 case DefinedGlobal:
669 case Defined:
670 S = DefinedGlobal;
671 break;
672
673 case NeverSeen:
674 case Global:
675 case Used:
676 S = Global;
677 break;
678 }
679 }
680 void markUsed(const MCSymbol &Symbol) {
681 State &S = Symbols[Symbol.getName()];
682 switch (S) {
683 case DefinedGlobal:
684 case Defined:
685 case Global:
686 break;
687
688 case NeverSeen:
689 case Used:
690 S = Used;
691 break;
692 }
693 }
694
695 // FIXME: mostly copied for the obj streamer.
696 void AddValueSymbols(const MCExpr *Value) {
697 switch (Value->getKind()) {
698 case MCExpr::Target:
699 // FIXME: What should we do in here?
700 break;
701
702 case MCExpr::Constant:
703 break;
704
705 case MCExpr::Binary: {
706 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
707 AddValueSymbols(BE->getLHS());
708 AddValueSymbols(BE->getRHS());
709 break;
710 }
711
712 case MCExpr::SymbolRef:
713 markUsed(cast<MCSymbolRefExpr>(Value)->getSymbol());
714 break;
715
716 case MCExpr::Unary:
717 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
718 break;
719 }
720 }
721
722 public:
723 typedef StringMap<State>::const_iterator const_iterator;
724
725 const_iterator begin() {
726 return Symbols.begin();
727 }
728
729 const_iterator end() {
730 return Symbols.end();
731 }
732
Chandler Carruthde093ef2013-01-31 23:29:57 +0000733 RecordStreamer(MCContext &Context)
734 : MCStreamer(SK_RecordStreamer, Context) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000735
Bill Wendling32867652012-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 Espindola1e49a6d2011-03-02 04:14:42 +0000742 virtual void EmitLabel(MCSymbol *Symbol) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000743 Symbol->setSection(*getCurrentSection().first);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000744 markDefined(*Symbol);
745 }
Reed Kotleraee4d5d12012-12-16 04:00:45 +0000746 virtual void EmitDebugLabel(MCSymbol *Symbol) {
747 EmitLabel(Symbol);
748 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000749 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
750 // FIXME: should we handle aliases?
751 markDefined(*Symbol);
752 }
753 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
754 if (Attribute == MCSA_Global)
755 markGlobal(*Symbol);
756 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000757 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Cheng95847992012-06-22 20:30:39 +0000758 uint64_t Size , unsigned ByteAlignment) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000759 markDefined(*Symbol);
760 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000761 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
762 unsigned ByteAlignment) {
763 markDefined(*Symbol);
764 }
Bill Wendling32867652012-04-03 03:56:52 +0000765
Eli Benderskyf483ff92012-12-20 19:05:53 +0000766 virtual void EmitBundleAlignMode(unsigned AlignPow2) {}
Eli Bendersky802b6282013-01-07 21:51:08 +0000767 virtual void EmitBundleLock(bool AlignToEnd) {}
Eli Benderskyf483ff92012-12-20 19:05:53 +0000768 virtual void EmitBundleUnlock() {}
769
Bill Wendling32867652012-04-03 03:56:52 +0000770 // Noop calls.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000771 virtual void ChangeSection(const MCSection *Section,
772 const MCExpr *Subsection) {}
Eli Benderskycbb25142013-01-14 19:04:57 +0000773 virtual void InitToTextSection() {}
Bill Wendling32867652012-04-03 03:56:52 +0000774 virtual void InitSections() {}
775 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {}
776 virtual void EmitThumbFunc(MCSymbol *Func) {}
777 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {}
778 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {}
779 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {}
780 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {}
781 virtual void EmitCOFFSymbolType(int Type) {}
782 virtual void EndCOFFSymbolDef() {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000783 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {}
Benjamin Kramer63970512011-09-01 23:04:27 +0000784 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
785 unsigned ByteAlignment) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000786 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
787 uint64_t Size, unsigned ByteAlignment) {}
Rafael Espindola64e1af82013-07-02 15:49:13 +0000788 virtual void EmitBytes(StringRef Data) {}
789 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {}
Rafael Espindola6aea5922011-04-21 23:39:26 +0000790 virtual void EmitULEB128Value(const MCExpr *Value) {}
791 virtual void EmitSLEB128Value(const MCExpr *Value) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000792 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
793 unsigned ValueSize,
794 unsigned MaxBytesToEmit) {}
795 virtual void EmitCodeAlignment(unsigned ByteAlignment,
796 unsigned MaxBytesToEmit) {}
Jim Grosbachb5912772012-01-27 00:37:08 +0000797 virtual bool EmitValueToOffset(const MCExpr *Offset,
798 unsigned char Value ) { return false; }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000799 virtual void EmitFileDirective(StringRef Filename) {}
800 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
801 const MCSymbol *LastLabel,
Evan Chengc7ac6902011-07-14 05:43:07 +0000802 const MCSymbol *Label,
803 unsigned PointerSize) {}
Rafael Espindola07082092012-01-07 03:13:18 +0000804 virtual void FinishImpl() {}
Chandler Carruthde093ef2013-01-31 23:29:57 +0000805
806 static bool classof(const MCStreamer *S) {
Chandler Carruth30cfaa22013-01-31 23:34:47 +0000807 return S->getKind() == SK_RecordStreamer;
Chandler Carruthde093ef2013-01-31 23:29:57 +0000808 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000809 };
Bill Wendling9ee2d332012-03-29 08:27:32 +0000810} // end anonymous namespace
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000811
Bill Wendlingfb440502012-03-28 20:46:54 +0000812/// addAsmGlobalSymbols - Add global symbols from module-level ASM to the
813/// defined or undefined lists.
Bill Wendlingac2abde2011-11-04 09:24:40 +0000814bool LTOModule::addAsmGlobalSymbols(std::string &errMsg) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000815 const std::string &inlineAsm = _module->getModuleInlineAsm();
Ivan Krasincc2a8012011-09-08 07:38:25 +0000816 if (inlineAsm.empty())
817 return false;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000818
Bill Wendlingac2abde2011-11-04 09:24:40 +0000819 OwningPtr<RecordStreamer> Streamer(new RecordStreamer(_context));
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000820 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(inlineAsm);
821 SourceMgr SrcMgr;
822 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach345768c2011-08-16 18:33:49 +0000823 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr,
Bill Wendlingac2abde2011-11-04 09:24:40 +0000824 _context, *Streamer,
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000825 *_target->getMCAsmInfo()));
Bill Wendling9351b3e2012-08-08 22:01:55 +0000826 const Target &T = _target->getTarget();
827 OwningPtr<MCSubtargetInfo>
828 STI(T.createMCSubtargetInfo(_target->getTargetTriple(),
829 _target->getTargetCPU(),
830 _target->getTargetFeatureString()));
831 OwningPtr<MCTargetAsmParser> TAP(T.createMCAsmParser(*STI, *Parser.get()));
Ivan Krasin8149dd62011-09-08 07:36:39 +0000832 if (!TAP) {
Bill Wendling9351b3e2012-08-08 22:01:55 +0000833 errMsg = "target " + std::string(T.getName()) +
834 " does not define AsmParser.";
Ivan Krasin8149dd62011-09-08 07:36:39 +0000835 return true;
836 }
837
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000838 Parser->setTargetParser(*TAP);
Bill Wendling9351b3e2012-08-08 22:01:55 +0000839 if (Parser->Run(false))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000840 return true;
841
842 for (RecordStreamer::const_iterator i = Streamer->begin(),
843 e = Streamer->end(); i != e; ++i) {
844 StringRef Key = i->first();
845 RecordStreamer::State Value = i->second;
846 if (Value == RecordStreamer::DefinedGlobal)
847 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_DEFAULT);
848 else if (Value == RecordStreamer::Defined)
849 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_INTERNAL);
850 else if (Value == RecordStreamer::Global ||
851 Value == RecordStreamer::Used)
852 addAsmGlobalSymbolUndef(Key.data());
853 }
Bill Wendling9351b3e2012-08-08 22:01:55 +0000854
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000855 return false;
856}
857
Bill Wendlingfb440502012-03-28 20:46:54 +0000858/// isDeclaration - Return 'true' if the global value is a declaration.
Rafael Espindola5b778b22011-03-18 19:51:00 +0000859static bool isDeclaration(const GlobalValue &V) {
860 if (V.hasAvailableExternallyLinkage())
861 return true;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000862
Rafael Espindola5b778b22011-03-18 19:51:00 +0000863 if (V.isMaterializable())
864 return false;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000865
Rafael Espindola5b778b22011-03-18 19:51:00 +0000866 return V.isDeclaration();
867}
868
Bill Wendling7e58b382012-03-28 23:12:18 +0000869/// parseSymbols - Parse the symbols from the module and model-level ASM and add
Bill Wendlingfb440502012-03-28 20:46:54 +0000870/// them to either the defined or undefined lists.
Bill Wendling7e58b382012-03-28 23:12:18 +0000871bool LTOModule::parseSymbols(std::string &errMsg) {
Daniel Dunbar919660b2010-08-10 23:46:46 +0000872 // add functions
Bill Wendling763acfc2012-03-29 03:34:57 +0000873 for (Module::iterator f = _module->begin(), e = _module->end(); f != e; ++f) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000874 if (isDeclaration(*f))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000875 addPotentialUndefinedSymbol(f, true);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000876 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000877 addDefinedFunctionSymbol(f);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000878 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000879
Daniel Dunbar919660b2010-08-10 23:46:46 +0000880 // add data
881 for (Module::global_iterator v = _module->global_begin(),
882 e = _module->global_end(); v != e; ++v) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000883 if (isDeclaration(*v))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000884 addPotentialUndefinedSymbol(v, false);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000885 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000886 addDefinedDataSymbol(v);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000887 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000888
Daniel Dunbar919660b2010-08-10 23:46:46 +0000889 // add asm globals
Bill Wendlingac2abde2011-11-04 09:24:40 +0000890 if (addAsmGlobalSymbols(errMsg))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000891 return true;
Daniel Dunbar919660b2010-08-10 23:46:46 +0000892
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000893 // add aliases
Bill Wendling9ee2d332012-03-29 08:27:32 +0000894 for (Module::alias_iterator a = _module->alias_begin(),
895 e = _module->alias_end(); a != e; ++a) {
896 if (isDeclaration(*a->getAliasedGlobal()))
Bill Wendlingd58ed732012-03-28 20:48:49 +0000897 // Is an alias to a declaration.
Bill Wendling9ee2d332012-03-29 08:27:32 +0000898 addPotentialUndefinedSymbol(a, false);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000899 else
Bill Wendling9ee2d332012-03-29 08:27:32 +0000900 addDefinedDataSymbol(a);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000901 }
902
Daniel Dunbar919660b2010-08-10 23:46:46 +0000903 // make symbols for all undefines
Bill Wendling9ee2d332012-03-29 08:27:32 +0000904 for (StringMap<NameAndAttributes>::iterator u =_undefines.begin(),
905 e = _undefines.end(); u != e; ++u) {
906 // If this symbol also has a definition, then don't make an undefine because
907 // it is a tentative definition.
908 if (_defines.count(u->getKey())) continue;
909 NameAndAttributes info = u->getValue();
910 _symbols.push_back(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000911 }
Bill Wendling9ee2d332012-03-29 08:27:32 +0000912
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000913 return false;
Nick Kledzik91a6dcf2008-02-27 22:25:36 +0000914}