blob: 1457f2f5a21ae01b6a290af8b2980ce135d17bf6 [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
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000015#include "llvm/LTO/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) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000251 // parse bitcode buffer
Rafael Espindola5b778b22011-03-18 19:51:00 +0000252 OwningPtr<Module> m(getLazyBitcodeModule(buffer, getGlobalContext(),
253 &errMsg));
254 if (!m) {
255 delete buffer;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000256 return NULL;
Rafael Espindola5b778b22011-03-18 19:51:00 +0000257 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000258
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000259 std::string TripleStr = m->getTargetTriple();
260 if (TripleStr.empty())
261 TripleStr = sys::getDefaultTargetTriple();
262 llvm::Triple Triple(TripleStr);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000263
264 // find machine architecture for this module
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000265 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000266 if (!march)
267 return NULL;
268
Nick Lewycky364c04a2011-04-21 01:54:08 +0000269 // construct LTOModule, hand over ownership of module and target
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000270 SubtargetFeatures Features;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000271 Features.getDefaultSubtargetFeatures(Triple);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000272 std::string FeatureStr = Features.getString();
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000273 // Set a default CPU for Darwin triples.
Evan Chengfe6e4052011-06-30 01:53:36 +0000274 std::string CPU;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000275 if (Triple.isOSDarwin()) {
276 if (Triple.getArch() == llvm::Triple::x86_64)
277 CPU = "core2";
278 else if (Triple.getArch() == llvm::Triple::x86)
279 CPU = "yonah";
280 }
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000281 TargetOptions Options;
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000282 getTargetOptions(Options);
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000283 TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000284 Options);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000285 LTOModule *Ret = new LTOModule(m.take(), target);
Bill Wendling7e58b382012-03-28 23:12:18 +0000286 if (Ret->parseSymbols(errMsg)) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000287 delete Ret;
288 return NULL;
289 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000290
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000291 return Ret;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000292}
293
Bill Wendlingfb440502012-03-28 20:46:54 +0000294/// makeBuffer - Create a MemoryBuffer from a memory range.
295MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length) {
Roman Divackyad06cee2012-09-05 22:26:57 +0000296 const char *startPtr = (const char*)mem;
Bill Wendlingfb440502012-03-28 20:46:54 +0000297 return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), "", false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000298}
299
Bill Wendlingfb440502012-03-28 20:46:54 +0000300/// objcClassNameFromExpression - Get string that the data pointer points to.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000301bool
302LTOModule::objcClassNameFromExpression(const Constant *c, std::string &name) {
303 if (const ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000304 Constant *op = ce->getOperand(0);
305 if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) {
306 Constant *cn = gvn->getInitializer();
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000307 if (ConstantDataArray *ca = dyn_cast<ConstantDataArray>(cn)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000308 if (ca->isCString()) {
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000309 name = ".objc_class_name_" + ca->getAsCString().str();
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000310 return true;
Nick Kledzikb481c202009-06-01 20:33:09 +0000311 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000312 }
Nick Kledzikb481c202009-06-01 20:33:09 +0000313 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000314 }
315 return false;
316}
317
Bill Wendlingfb440502012-03-28 20:46:54 +0000318/// addObjCClass - Parse i386/ppc ObjC class data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000319void LTOModule::addObjCClass(const GlobalVariable *clgv) {
320 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000321 if (!c) return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000322
Bill Wendling5f689e72011-11-04 18:48:00 +0000323 // second slot in __OBJC,__class is pointer to superclass name
324 std::string superclassName;
325 if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
326 NameAndAttributes info;
327 StringMap<NameAndAttributes>::value_type &entry =
328 _undefines.GetOrCreateValue(superclassName);
329 if (!entry.getValue().name) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000330 const char *symbolName = entry.getKey().data();
331 info.name = symbolName;
332 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000333 info.isFunction = false;
334 info.symbol = clgv;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000335 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000336 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000337 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000338
339 // third slot in __OBJC,__class is pointer to class name
340 std::string className;
341 if (objcClassNameFromExpression(c->getOperand(2), className)) {
342 StringSet::value_type &entry = _defines.GetOrCreateValue(className);
343 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000344
Bill Wendling5f689e72011-11-04 18:48:00 +0000345 NameAndAttributes info;
346 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000347 info.attributes = LTO_SYMBOL_PERMISSIONS_DATA |
348 LTO_SYMBOL_DEFINITION_REGULAR | LTO_SYMBOL_SCOPE_DEFAULT;
349 info.isFunction = false;
350 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000351 _symbols.push_back(info);
352 }
353}
354
Bill Wendlingfb440502012-03-28 20:46:54 +0000355/// addObjCCategory - Parse i386/ppc ObjC category data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000356void LTOModule::addObjCCategory(const GlobalVariable *clgv) {
357 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000358 if (!c) return;
359
360 // second slot in __OBJC,__category is pointer to target class name
361 std::string targetclassName;
362 if (!objcClassNameFromExpression(c->getOperand(1), targetclassName))
363 return;
364
365 NameAndAttributes info;
366 StringMap<NameAndAttributes>::value_type &entry =
367 _undefines.GetOrCreateValue(targetclassName);
368
369 if (entry.getValue().name)
370 return;
371
372 const char *symbolName = entry.getKey().data();
373 info.name = symbolName;
374 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000375 info.isFunction = false;
376 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000377 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000378}
379
Bill Wendlingfb440502012-03-28 20:46:54 +0000380/// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000381void LTOModule::addObjCClassRef(const GlobalVariable *clgv) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000382 std::string targetclassName;
Bill Wendling5f689e72011-11-04 18:48:00 +0000383 if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName))
384 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000385
Bill Wendling5f689e72011-11-04 18:48:00 +0000386 NameAndAttributes info;
387 StringMap<NameAndAttributes>::value_type &entry =
388 _undefines.GetOrCreateValue(targetclassName);
389 if (entry.getValue().name)
390 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000391
Bill Wendling5f689e72011-11-04 18:48:00 +0000392 const char *symbolName = entry.getKey().data();
393 info.name = symbolName;
394 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000395 info.isFunction = false;
396 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000397 entry.setValue(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000398}
399
Bill Wendlingfb440502012-03-28 20:46:54 +0000400/// addDefinedDataSymbol - Add a data symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000401void LTOModule::addDefinedDataSymbol(const GlobalValue *v) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000402 // Add to list of defined symbols.
Bill Wendlinga2af6742011-11-04 09:30:19 +0000403 addDefinedSymbol(v, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000404
Bill Wendling45f74e32012-08-06 22:52:45 +0000405 if (!v->hasSection() /* || !isTargetDarwin */)
406 return;
407
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000408 // Special case i386/ppc ObjC data structures in magic sections:
409 // The issue is that the old ObjC object format did some strange
410 // contortions to avoid real linker symbols. For instance, the
411 // ObjC class data structure is allocated statically in the executable
412 // that defines that class. That data structures contains a pointer to
413 // its superclass. But instead of just initializing that part of the
414 // struct to the address of its superclass, and letting the static and
415 // dynamic linkers do the rest, the runtime works by having that field
416 // instead point to a C-string that is the name of the superclass.
417 // At runtime the objc initialization updates that pointer and sets
418 // it to point to the actual super class. As far as the linker
419 // knows it is just a pointer to a string. But then someone wanted the
420 // linker to issue errors at build time if the superclass was not found.
421 // So they figured out a way in mach-o object format to use an absolute
422 // symbols (.objc_class_name_Foo = 0) and a floating reference
423 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
424 // a class was missing.
425 // The following synthesizes the implicit .objc_* symbols for the linker
426 // from the ObjC data structures generated by the front end.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000427
Bill Wendling45f74e32012-08-06 22:52:45 +0000428 // special case if this data blob is an ObjC class definition
429 if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000430 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000431 addObjCClass(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000432 }
Bill Wendling45f74e32012-08-06 22:52:45 +0000433 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000434
Bill Wendling45f74e32012-08-06 22:52:45 +0000435 // special case if this data blob is an ObjC category definition
436 else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000437 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000438 addObjCCategory(gv);
439 }
440 }
441
442 // special case if this data blob is the list of referenced classes
443 else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000444 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000445 addObjCClassRef(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000446 }
447 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000448}
449
Bill Wendlingfb440502012-03-28 20:46:54 +0000450/// addDefinedFunctionSymbol - Add a function symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000451void LTOModule::addDefinedFunctionSymbol(const Function *f) {
Bill Wendlingfb440502012-03-28 20:46:54 +0000452 // add to list of defined symbols
453 addDefinedSymbol(f, true);
454}
455
456/// addDefinedSymbol - Add a defined symbol to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000457void LTOModule::addDefinedSymbol(const GlobalValue *def, bool isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000458 // ignore all llvm.* symbols
459 if (def->getName().startswith("llvm."))
460 return;
461
462 // string is owned by _defines
Rafael Espindola34b59382011-02-11 05:23:09 +0000463 SmallString<64> Buffer;
Bill Wendlinga2af6742011-11-04 09:30:19 +0000464 _mangler.getNameWithPrefix(Buffer, def, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000465
466 // set alignment part log2() can have rounding errors
467 uint32_t align = def->getAlignment();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000468 uint32_t attr = align ? countTrailingZeros(def->getAlignment()) : 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000469
470 // set permissions part
Bill Wendling9ee2d332012-03-29 08:27:32 +0000471 if (isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000472 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000473 } else {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000474 const GlobalVariable *gv = dyn_cast<GlobalVariable>(def);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000475 if (gv && gv->isConstant())
476 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
477 else
478 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
479 }
480
481 // set definition part
Bill Wendling2776d462010-09-27 18:05:19 +0000482 if (def->hasWeakLinkage() || def->hasLinkOnceLinkage() ||
Bill Wendling34bc34e2012-08-17 18:33:14 +0000483 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000484 attr |= LTO_SYMBOL_DEFINITION_WEAK;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000485 else if (def->hasCommonLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000486 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000487 else
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000488 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000489
490 // set scope part
491 if (def->hasHiddenVisibility())
492 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
493 else if (def->hasProtectedVisibility())
494 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000495 else if (def->hasExternalLinkage() || def->hasWeakLinkage() ||
496 def->hasLinkOnceLinkage() || def->hasCommonLinkage() ||
497 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000498 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
Bill Wendling34bc34e2012-08-17 18:33:14 +0000499 else if (def->hasLinkOnceODRAutoHideLinkage())
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000500 attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000501 else
502 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
503
Chad Rosier772a91f2011-06-28 18:26:12 +0000504 StringSet::value_type &entry = _defines.GetOrCreateValue(Buffer);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000505 entry.setValue(1);
506
Bill Wendling9ee2d332012-03-29 08:27:32 +0000507 // fill information structure
508 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000509 StringRef Name = entry.getKey();
510 info.name = Name.data();
511 assert(info.name[Name.size()] == '\0');
Bill Wendling9ee2d332012-03-29 08:27:32 +0000512 info.attributes = attr;
513 info.isFunction = isFunction;
514 info.symbol = def;
515
516 // add to table of symbols
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000517 _symbols.push_back(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000518}
519
Bill Wendlingfb440502012-03-28 20:46:54 +0000520/// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
521/// defined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000522void LTOModule::addAsmGlobalSymbol(const char *name,
523 lto_symbol_attributes scope) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000524 StringSet::value_type &entry = _defines.GetOrCreateValue(name);
525
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000526 // only add new define if not already defined
Rafael Espindola477d11f2011-02-20 16:27:25 +0000527 if (entry.getValue())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000528 return;
529
Rafael Espindola477d11f2011-02-20 16:27:25 +0000530 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000531
532 NameAndAttributes &info = _undefines[entry.getKey().data()];
533
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000534 if (info.symbol == 0) {
Bill Wendling71b19bb2012-04-02 10:01:21 +0000535 // FIXME: This is trying to take care of module ASM like this:
536 //
537 // module asm ".zerofill __FOO, __foo, _bar_baz_qux, 0"
538 //
539 // but is gross and its mother dresses it funny. Have the ASM parser give us
540 // more details for this type of situation so that we're not guessing so
541 // much.
542
543 // fill information structure
Rafael Espindola5f4b32f2012-05-11 03:42:13 +0000544 info.name = entry.getKey().data();
Bill Wendling71b19bb2012-04-02 10:01:21 +0000545 info.attributes =
546 LTO_SYMBOL_PERMISSIONS_DATA | LTO_SYMBOL_DEFINITION_REGULAR | scope;
547 info.isFunction = false;
548 info.symbol = 0;
549
550 // add to table of symbols
551 _symbols.push_back(info);
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000552 return;
553 }
554
Bill Wendling9ee2d332012-03-29 08:27:32 +0000555 if (info.isFunction)
556 addDefinedFunctionSymbol(cast<Function>(info.symbol));
557 else
558 addDefinedDataSymbol(info.symbol);
Bill Wendling8f6c8a92012-03-30 23:26:06 +0000559
560 _symbols.back().attributes &= ~LTO_SYMBOL_SCOPE_MASK;
561 _symbols.back().attributes |= scope;
Devang Patela59fe952008-07-16 18:06:52 +0000562}
Nick Kledzik07b4a622008-02-26 20:26:43 +0000563
Bill Wendlingfb440502012-03-28 20:46:54 +0000564/// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the
565/// undefined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000566void LTOModule::addAsmGlobalSymbolUndef(const char *name) {
567 StringMap<NameAndAttributes>::value_type &entry =
568 _undefines.GetOrCreateValue(name);
569
570 _asm_undefines.push_back(entry.getKey().data());
571
572 // we already have the symbol
573 if (entry.getValue().name)
574 return;
575
576 uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;;
577 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
578 NameAndAttributes info;
579 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000580 info.attributes = attr;
581 info.isFunction = false;
582 info.symbol = 0;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000583
584 entry.setValue(info);
585}
586
Bill Wendlingfb440502012-03-28 20:46:54 +0000587/// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet to a
588/// list to be resolved later.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000589void
590LTOModule::addPotentialUndefinedSymbol(const GlobalValue *decl, bool isFunc) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000591 // ignore all llvm.* symbols
592 if (decl->getName().startswith("llvm."))
593 return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000594
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000595 // ignore all aliases
596 if (isa<GlobalAlias>(decl))
597 return;
Nick Lewycky0661b932009-07-09 06:03:04 +0000598
Rafael Espindola34b59382011-02-11 05:23:09 +0000599 SmallString<64> name;
Bill Wendlinga2af6742011-11-04 09:30:19 +0000600 _mangler.getNameWithPrefix(name, decl, false);
Rafael Espindola56548522009-04-24 16:55:21 +0000601
Rafael Espindola477d11f2011-02-20 16:27:25 +0000602 StringMap<NameAndAttributes>::value_type &entry =
Chad Rosier772a91f2011-06-28 18:26:12 +0000603 _undefines.GetOrCreateValue(name);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000604
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000605 // we already have the symbol
Rafael Espindola477d11f2011-02-20 16:27:25 +0000606 if (entry.getValue().name)
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000607 return;
Rafael Espindola56548522009-04-24 16:55:21 +0000608
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000609 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000610
611 info.name = entry.getKey().data();
Bill Wendlingfb440502012-03-28 20:46:54 +0000612
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000613 if (decl->hasExternalWeakLinkage())
614 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
615 else
616 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000617
Bill Wendling9ee2d332012-03-29 08:27:32 +0000618 info.isFunction = isFunc;
619 info.symbol = decl;
620
Rafael Espindola477d11f2011-02-20 16:27:25 +0000621 entry.setValue(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000622}
623
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000624namespace {
625 class RecordStreamer : public MCStreamer {
626 public:
Bill Wendling32867652012-04-03 03:56:52 +0000627 enum State { NeverSeen, Global, Defined, DefinedGlobal, Used };
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000628
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000629 private:
630 StringMap<State> Symbols;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000631
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000632 void markDefined(const MCSymbol &Symbol) {
633 State &S = Symbols[Symbol.getName()];
634 switch (S) {
635 case DefinedGlobal:
636 case Global:
637 S = DefinedGlobal;
638 break;
639 case NeverSeen:
640 case Defined:
641 case Used:
642 S = Defined;
643 break;
644 }
645 }
646 void markGlobal(const MCSymbol &Symbol) {
647 State &S = Symbols[Symbol.getName()];
648 switch (S) {
649 case DefinedGlobal:
650 case Defined:
651 S = DefinedGlobal;
652 break;
653
654 case NeverSeen:
655 case Global:
656 case Used:
657 S = Global;
658 break;
659 }
660 }
661 void markUsed(const MCSymbol &Symbol) {
662 State &S = Symbols[Symbol.getName()];
663 switch (S) {
664 case DefinedGlobal:
665 case Defined:
666 case Global:
667 break;
668
669 case NeverSeen:
670 case Used:
671 S = Used;
672 break;
673 }
674 }
675
676 // FIXME: mostly copied for the obj streamer.
677 void AddValueSymbols(const MCExpr *Value) {
678 switch (Value->getKind()) {
679 case MCExpr::Target:
680 // FIXME: What should we do in here?
681 break;
682
683 case MCExpr::Constant:
684 break;
685
686 case MCExpr::Binary: {
687 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
688 AddValueSymbols(BE->getLHS());
689 AddValueSymbols(BE->getRHS());
690 break;
691 }
692
693 case MCExpr::SymbolRef:
694 markUsed(cast<MCSymbolRefExpr>(Value)->getSymbol());
695 break;
696
697 case MCExpr::Unary:
698 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
699 break;
700 }
701 }
702
703 public:
704 typedef StringMap<State>::const_iterator const_iterator;
705
706 const_iterator begin() {
707 return Symbols.begin();
708 }
709
710 const_iterator end() {
711 return Symbols.end();
712 }
713
Chandler Carruthde093ef2013-01-31 23:29:57 +0000714 RecordStreamer(MCContext &Context)
715 : MCStreamer(SK_RecordStreamer, Context) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000716
Bill Wendling32867652012-04-03 03:56:52 +0000717 virtual void EmitInstruction(const MCInst &Inst) {
718 // Scan for values.
719 for (unsigned i = Inst.getNumOperands(); i--; )
720 if (Inst.getOperand(i).isExpr())
721 AddValueSymbols(Inst.getOperand(i).getExpr());
722 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000723 virtual void EmitLabel(MCSymbol *Symbol) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000724 Symbol->setSection(*getCurrentSection().first);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000725 markDefined(*Symbol);
726 }
Reed Kotleraee4d5d12012-12-16 04:00:45 +0000727 virtual void EmitDebugLabel(MCSymbol *Symbol) {
728 EmitLabel(Symbol);
729 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000730 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
731 // FIXME: should we handle aliases?
732 markDefined(*Symbol);
733 }
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000734 virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000735 if (Attribute == MCSA_Global)
736 markGlobal(*Symbol);
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000737 return true;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000738 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000739 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Cheng95847992012-06-22 20:30:39 +0000740 uint64_t Size , unsigned ByteAlignment) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000741 markDefined(*Symbol);
742 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000743 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
744 unsigned ByteAlignment) {
745 markDefined(*Symbol);
746 }
Bill Wendling32867652012-04-03 03:56:52 +0000747
Eli Benderskyf483ff92012-12-20 19:05:53 +0000748 virtual void EmitBundleAlignMode(unsigned AlignPow2) {}
Eli Bendersky802b6282013-01-07 21:51:08 +0000749 virtual void EmitBundleLock(bool AlignToEnd) {}
Eli Benderskyf483ff92012-12-20 19:05:53 +0000750 virtual void EmitBundleUnlock() {}
751
Bill Wendling32867652012-04-03 03:56:52 +0000752 // Noop calls.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000753 virtual void ChangeSection(const MCSection *Section,
754 const MCExpr *Subsection) {}
Eli Benderskycbb25142013-01-14 19:04:57 +0000755 virtual void InitToTextSection() {}
Bill Wendling32867652012-04-03 03:56:52 +0000756 virtual void InitSections() {}
757 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {}
758 virtual void EmitThumbFunc(MCSymbol *Func) {}
759 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {}
760 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {}
761 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {}
762 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {}
763 virtual void EmitCOFFSymbolType(int Type) {}
764 virtual void EndCOFFSymbolDef() {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000765 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {}
Benjamin Kramer63970512011-09-01 23:04:27 +0000766 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
767 unsigned ByteAlignment) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000768 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
769 uint64_t Size, unsigned ByteAlignment) {}
Rafael Espindola64e1af82013-07-02 15:49:13 +0000770 virtual void EmitBytes(StringRef Data) {}
771 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {}
Rafael Espindola6aea5922011-04-21 23:39:26 +0000772 virtual void EmitULEB128Value(const MCExpr *Value) {}
773 virtual void EmitSLEB128Value(const MCExpr *Value) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000774 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
775 unsigned ValueSize,
776 unsigned MaxBytesToEmit) {}
777 virtual void EmitCodeAlignment(unsigned ByteAlignment,
778 unsigned MaxBytesToEmit) {}
Jim Grosbachb5912772012-01-27 00:37:08 +0000779 virtual bool EmitValueToOffset(const MCExpr *Offset,
780 unsigned char Value ) { return false; }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000781 virtual void EmitFileDirective(StringRef Filename) {}
782 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
783 const MCSymbol *LastLabel,
Evan Chengc7ac6902011-07-14 05:43:07 +0000784 const MCSymbol *Label,
785 unsigned PointerSize) {}
Rafael Espindola07082092012-01-07 03:13:18 +0000786 virtual void FinishImpl() {}
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000787 virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
788 RecordProcEnd(Frame);
789 }
Chandler Carruthde093ef2013-01-31 23:29:57 +0000790
791 static bool classof(const MCStreamer *S) {
Chandler Carruth30cfaa22013-01-31 23:34:47 +0000792 return S->getKind() == SK_RecordStreamer;
Chandler Carruthde093ef2013-01-31 23:29:57 +0000793 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000794 };
Bill Wendling9ee2d332012-03-29 08:27:32 +0000795} // end anonymous namespace
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000796
Bill Wendlingfb440502012-03-28 20:46:54 +0000797/// addAsmGlobalSymbols - Add global symbols from module-level ASM to the
798/// defined or undefined lists.
Bill Wendlingac2abde2011-11-04 09:24:40 +0000799bool LTOModule::addAsmGlobalSymbols(std::string &errMsg) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000800 const std::string &inlineAsm = _module->getModuleInlineAsm();
Ivan Krasincc2a8012011-09-08 07:38:25 +0000801 if (inlineAsm.empty())
802 return false;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000803
Bill Wendlingac2abde2011-11-04 09:24:40 +0000804 OwningPtr<RecordStreamer> Streamer(new RecordStreamer(_context));
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000805 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(inlineAsm);
806 SourceMgr SrcMgr;
807 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach345768c2011-08-16 18:33:49 +0000808 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr,
Bill Wendlingac2abde2011-11-04 09:24:40 +0000809 _context, *Streamer,
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000810 *_target->getMCAsmInfo()));
Bill Wendling9351b3e2012-08-08 22:01:55 +0000811 const Target &T = _target->getTarget();
Joey Goulydb6144e2013-09-12 12:55:29 +0000812 OwningPtr<MCInstrInfo> MCII(T.createMCInstrInfo());
Bill Wendling9351b3e2012-08-08 22:01:55 +0000813 OwningPtr<MCSubtargetInfo>
814 STI(T.createMCSubtargetInfo(_target->getTargetTriple(),
815 _target->getTargetCPU(),
816 _target->getTargetFeatureString()));
Joey Goulydb6144e2013-09-12 12:55:29 +0000817 OwningPtr<MCTargetAsmParser> TAP(T.createMCAsmParser(*STI, *Parser.get(), *MCII));
Ivan Krasin8149dd62011-09-08 07:36:39 +0000818 if (!TAP) {
Bill Wendling9351b3e2012-08-08 22:01:55 +0000819 errMsg = "target " + std::string(T.getName()) +
820 " does not define AsmParser.";
Ivan Krasin8149dd62011-09-08 07:36:39 +0000821 return true;
822 }
823
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000824 Parser->setTargetParser(*TAP);
Bill Wendling9351b3e2012-08-08 22:01:55 +0000825 if (Parser->Run(false))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000826 return true;
827
828 for (RecordStreamer::const_iterator i = Streamer->begin(),
829 e = Streamer->end(); i != e; ++i) {
830 StringRef Key = i->first();
831 RecordStreamer::State Value = i->second;
832 if (Value == RecordStreamer::DefinedGlobal)
833 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_DEFAULT);
834 else if (Value == RecordStreamer::Defined)
835 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_INTERNAL);
836 else if (Value == RecordStreamer::Global ||
837 Value == RecordStreamer::Used)
838 addAsmGlobalSymbolUndef(Key.data());
839 }
Bill Wendling9351b3e2012-08-08 22:01:55 +0000840
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000841 return false;
842}
843
Bill Wendlingfb440502012-03-28 20:46:54 +0000844/// isDeclaration - Return 'true' if the global value is a declaration.
Rafael Espindola5b778b22011-03-18 19:51:00 +0000845static bool isDeclaration(const GlobalValue &V) {
846 if (V.hasAvailableExternallyLinkage())
847 return true;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000848
Rafael Espindola5b778b22011-03-18 19:51:00 +0000849 if (V.isMaterializable())
850 return false;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000851
Rafael Espindola5b778b22011-03-18 19:51:00 +0000852 return V.isDeclaration();
853}
854
Bill Wendling7e58b382012-03-28 23:12:18 +0000855/// parseSymbols - Parse the symbols from the module and model-level ASM and add
Bill Wendlingfb440502012-03-28 20:46:54 +0000856/// them to either the defined or undefined lists.
Bill Wendling7e58b382012-03-28 23:12:18 +0000857bool LTOModule::parseSymbols(std::string &errMsg) {
Daniel Dunbar919660b2010-08-10 23:46:46 +0000858 // add functions
Bill Wendling763acfc2012-03-29 03:34:57 +0000859 for (Module::iterator f = _module->begin(), e = _module->end(); f != e; ++f) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000860 if (isDeclaration(*f))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000861 addPotentialUndefinedSymbol(f, true);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000862 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000863 addDefinedFunctionSymbol(f);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000864 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000865
Daniel Dunbar919660b2010-08-10 23:46:46 +0000866 // add data
867 for (Module::global_iterator v = _module->global_begin(),
868 e = _module->global_end(); v != e; ++v) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000869 if (isDeclaration(*v))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000870 addPotentialUndefinedSymbol(v, false);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000871 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000872 addDefinedDataSymbol(v);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000873 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000874
Daniel Dunbar919660b2010-08-10 23:46:46 +0000875 // add asm globals
Bill Wendlingac2abde2011-11-04 09:24:40 +0000876 if (addAsmGlobalSymbols(errMsg))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000877 return true;
Daniel Dunbar919660b2010-08-10 23:46:46 +0000878
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000879 // add aliases
Bill Wendling9ee2d332012-03-29 08:27:32 +0000880 for (Module::alias_iterator a = _module->alias_begin(),
881 e = _module->alias_end(); a != e; ++a) {
882 if (isDeclaration(*a->getAliasedGlobal()))
Bill Wendlingd58ed732012-03-28 20:48:49 +0000883 // Is an alias to a declaration.
Bill Wendling9ee2d332012-03-29 08:27:32 +0000884 addPotentialUndefinedSymbol(a, false);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000885 else
Bill Wendling9ee2d332012-03-29 08:27:32 +0000886 addDefinedDataSymbol(a);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000887 }
888
Daniel Dunbar919660b2010-08-10 23:46:46 +0000889 // make symbols for all undefines
Bill Wendling9ee2d332012-03-29 08:27:32 +0000890 for (StringMap<NameAndAttributes>::iterator u =_undefines.begin(),
891 e = _undefines.end(); u != e; ++u) {
892 // If this symbol also has a definition, then don't make an undefine because
893 // it is a tentative definition.
894 if (_defines.count(u->getKey())) continue;
895 NameAndAttributes info = u->getValue();
896 _symbols.push_back(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000897 }
Bill Wendling9ee2d332012-03-29 08:27:32 +0000898
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000899 return false;
Nick Kledzik91a6dcf2008-02-27 22:25:36 +0000900}