blob: 79cff1f68fe783001fee103b804bc446778c6daa [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"
Rafael Espindola46ed3532013-06-11 18:05:26 +000032#include "llvm/Support/FileSystem.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000033#include "llvm/Support/Host.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"
Rafael Espindola282a4702013-10-31 20:51:58 +000041#include "llvm/Transforms/Utils/GlobalStatus.h"
Nick Kledzik07b4a622008-02-26 20:26:43 +000042using namespace llvm;
43
Bill Wendlingfb440502012-03-28 20:46:54 +000044LTOModule::LTOModule(llvm::Module *m, llvm::TargetMachine *t)
45 : _module(m), _target(t),
Rafael Espindolae28610d2013-12-09 20:26:40 +000046 _context(_target->getMCAsmInfo(), _target->getRegisterInfo(), &ObjFileInfo),
Rafael Espindola58873562014-01-03 19:21:54 +000047 _mangler(t->getDataLayout()) {
Rafael Espindolae28610d2013-12-09 20:26:40 +000048 ObjFileInfo.InitMCObjectFileInfo(t->getTargetTriple(),
49 t->getRelocationModel(), t->getCodeModel(),
50 _context);
51}
Bill Wendlingfb440502012-03-28 20:46:54 +000052
53/// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM
54/// bitcode.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000055bool LTOModule::isBitcodeFile(const void *mem, size_t length) {
Rafael Espindola46ed3532013-06-11 18:05:26 +000056 return sys::fs::identify_magic(StringRef((const char *)mem, length)) ==
57 sys::fs::file_magic::bitcode;
Nick Kledzik07b4a622008-02-26 20:26:43 +000058}
59
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000060bool LTOModule::isBitcodeFile(const char *path) {
Rafael Espindola71affba2013-06-12 15:13:57 +000061 sys::fs::file_magic type;
62 if (sys::fs::identify_magic(path, type))
63 return false;
64 return type == sys::fs::file_magic::bitcode;
Nick Kledzik07b4a622008-02-26 20:26:43 +000065}
66
Bill Wendlingfb440502012-03-28 20:46:54 +000067/// isBitcodeFileForTarget - Returns 'true' if the file (or memory contents) is
68/// LLVM bitcode for the specified triple.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000069bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length,
70 const char *triplePrefix) {
71 MemoryBuffer *buffer = makeBuffer(mem, length);
72 if (!buffer)
Nick Kledzikb481c202009-06-01 20:33:09 +000073 return false;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000074 return isTargetMatch(buffer, triplePrefix);
Nick Kledzikb481c202009-06-01 20:33:09 +000075}
76
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000077bool LTOModule::isBitcodeFileForTarget(const char *path,
78 const char *triplePrefix) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +000079 OwningPtr<MemoryBuffer> buffer;
80 if (MemoryBuffer::getFile(path, buffer))
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000081 return false;
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +000082 return isTargetMatch(buffer.take(), triplePrefix);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000083}
84
Bill Wendlingfb440502012-03-28 20:46:54 +000085/// isTargetMatch - Returns 'true' if the memory buffer is for the specified
86/// target triple.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000087bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) {
Bill Wendling0198ce02010-10-06 01:22:42 +000088 std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
89 delete buffer;
Bill Wendling5f689e72011-11-04 18:48:00 +000090 return strncmp(Triple.c_str(), triplePrefix, strlen(triplePrefix)) == 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000091}
92
Bill Wendlingfb440502012-03-28 20:46:54 +000093/// makeLTOModule - Create an LTOModule. N.B. These methods take ownership of
94/// the buffer.
Rafael Espindola0b385c72013-09-30 16:39:19 +000095LTOModule *LTOModule::makeLTOModule(const char *path, TargetOptions options,
96 std::string &errMsg) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +000097 OwningPtr<MemoryBuffer> buffer;
98 if (error_code ec = MemoryBuffer::getFile(path, buffer)) {
Michael J. Spencerd4227232010-12-09 18:06:07 +000099 errMsg = ec.message();
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000100 return NULL;
Michael J. Spencerd4227232010-12-09 18:06:07 +0000101 }
Rafael Espindola0b385c72013-09-30 16:39:19 +0000102 return makeLTOModule(buffer.take(), options, errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000103}
104
Rafael Espindola56e41f72011-02-08 22:40:47 +0000105LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000106 size_t size, TargetOptions options,
107 std::string &errMsg) {
108 return makeLTOModule(fd, path, size, 0, options, errMsg);
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000109}
110
111LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000112 size_t map_size,
113 off_t offset,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000114 TargetOptions options,
Rafael Espindola56e41f72011-02-08 22:40:47 +0000115 std::string &errMsg) {
116 OwningPtr<MemoryBuffer> buffer;
Rafael Espindola3d2ac2e2013-07-23 20:25:01 +0000117 if (error_code ec =
118 MemoryBuffer::getOpenFileSlice(fd, path, buffer, map_size, offset)) {
Rafael Espindola56e41f72011-02-08 22:40:47 +0000119 errMsg = ec.message();
120 return NULL;
121 }
Rafael Espindola0b385c72013-09-30 16:39:19 +0000122 return makeLTOModule(buffer.take(), options, errMsg);
Rafael Espindola56e41f72011-02-08 22:40:47 +0000123}
124
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000125LTOModule *LTOModule::makeLTOModule(const void *mem, size_t length,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000126 TargetOptions options,
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000127 std::string &errMsg) {
128 OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length));
129 if (!buffer)
130 return NULL;
Rafael Espindola0b385c72013-09-30 16:39:19 +0000131 return makeLTOModule(buffer.take(), options, errMsg);
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000132}
133
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000134LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000135 TargetOptions options,
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000136 std::string &errMsg) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000137 // parse bitcode buffer
Rafael Espindola5b6c1e82014-01-13 18:31:04 +0000138 ErrorOr<Module *> ModuleOrErr =
139 getLazyBitcodeModule(buffer, getGlobalContext());
140 if (error_code EC = ModuleOrErr.getError()) {
141 errMsg = EC.message();
Rafael Espindola5b778b22011-03-18 19:51:00 +0000142 delete buffer;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000143 return NULL;
Rafael Espindola5b778b22011-03-18 19:51:00 +0000144 }
Rafael Espindola5b6c1e82014-01-13 18:31:04 +0000145 OwningPtr<Module> m(ModuleOrErr.get());
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000146
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000147 std::string TripleStr = m->getTargetTriple();
148 if (TripleStr.empty())
149 TripleStr = sys::getDefaultTargetTriple();
150 llvm::Triple Triple(TripleStr);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000151
152 // find machine architecture for this module
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000153 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000154 if (!march)
155 return NULL;
156
Nick Lewycky364c04a2011-04-21 01:54:08 +0000157 // construct LTOModule, hand over ownership of module and target
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000158 SubtargetFeatures Features;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000159 Features.getDefaultSubtargetFeatures(Triple);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000160 std::string FeatureStr = Features.getString();
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000161 // Set a default CPU for Darwin triples.
Evan Chengfe6e4052011-06-30 01:53:36 +0000162 std::string CPU;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000163 if (Triple.isOSDarwin()) {
164 if (Triple.getArch() == llvm::Triple::x86_64)
165 CPU = "core2";
166 else if (Triple.getArch() == llvm::Triple::x86)
167 CPU = "yonah";
168 }
Rafael Espindola0b385c72013-09-30 16:39:19 +0000169
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000170 TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000171 options);
Rafael Espindolae9fab9b2014-01-14 23:51:27 +0000172 m->materializeAllPermanently();
Rafael Espindola282a4702013-10-31 20:51:58 +0000173
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000174 LTOModule *Ret = new LTOModule(m.take(), target);
Bill Wendling7e58b382012-03-28 23:12:18 +0000175 if (Ret->parseSymbols(errMsg)) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000176 delete Ret;
177 return NULL;
178 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000179
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000180 return Ret;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000181}
182
Bill Wendlingfb440502012-03-28 20:46:54 +0000183/// makeBuffer - Create a MemoryBuffer from a memory range.
184MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length) {
Roman Divackyad06cee2012-09-05 22:26:57 +0000185 const char *startPtr = (const char*)mem;
Bill Wendlingfb440502012-03-28 20:46:54 +0000186 return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), "", false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000187}
188
Bill Wendlingfb440502012-03-28 20:46:54 +0000189/// objcClassNameFromExpression - Get string that the data pointer points to.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000190bool
191LTOModule::objcClassNameFromExpression(const Constant *c, std::string &name) {
192 if (const ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000193 Constant *op = ce->getOperand(0);
194 if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) {
195 Constant *cn = gvn->getInitializer();
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000196 if (ConstantDataArray *ca = dyn_cast<ConstantDataArray>(cn)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000197 if (ca->isCString()) {
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000198 name = ".objc_class_name_" + ca->getAsCString().str();
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000199 return true;
Nick Kledzikb481c202009-06-01 20:33:09 +0000200 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000201 }
Nick Kledzikb481c202009-06-01 20:33:09 +0000202 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000203 }
204 return false;
205}
206
Bill Wendlingfb440502012-03-28 20:46:54 +0000207/// addObjCClass - Parse i386/ppc ObjC class data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000208void LTOModule::addObjCClass(const GlobalVariable *clgv) {
209 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000210 if (!c) return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000211
Bill Wendling5f689e72011-11-04 18:48:00 +0000212 // second slot in __OBJC,__class is pointer to superclass name
213 std::string superclassName;
214 if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
215 NameAndAttributes info;
216 StringMap<NameAndAttributes>::value_type &entry =
217 _undefines.GetOrCreateValue(superclassName);
218 if (!entry.getValue().name) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000219 const char *symbolName = entry.getKey().data();
220 info.name = symbolName;
221 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000222 info.isFunction = false;
223 info.symbol = clgv;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000224 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000225 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000226 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000227
228 // third slot in __OBJC,__class is pointer to class name
229 std::string className;
230 if (objcClassNameFromExpression(c->getOperand(2), className)) {
231 StringSet::value_type &entry = _defines.GetOrCreateValue(className);
232 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000233
Bill Wendling5f689e72011-11-04 18:48:00 +0000234 NameAndAttributes info;
235 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000236 info.attributes = LTO_SYMBOL_PERMISSIONS_DATA |
237 LTO_SYMBOL_DEFINITION_REGULAR | LTO_SYMBOL_SCOPE_DEFAULT;
238 info.isFunction = false;
239 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000240 _symbols.push_back(info);
241 }
242}
243
Bill Wendlingfb440502012-03-28 20:46:54 +0000244/// addObjCCategory - Parse i386/ppc ObjC category data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000245void LTOModule::addObjCCategory(const GlobalVariable *clgv) {
246 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000247 if (!c) return;
248
249 // second slot in __OBJC,__category is pointer to target class name
250 std::string targetclassName;
251 if (!objcClassNameFromExpression(c->getOperand(1), targetclassName))
252 return;
253
254 NameAndAttributes info;
255 StringMap<NameAndAttributes>::value_type &entry =
256 _undefines.GetOrCreateValue(targetclassName);
257
258 if (entry.getValue().name)
259 return;
260
261 const char *symbolName = entry.getKey().data();
262 info.name = symbolName;
263 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000264 info.isFunction = false;
265 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000266 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000267}
268
Bill Wendlingfb440502012-03-28 20:46:54 +0000269/// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000270void LTOModule::addObjCClassRef(const GlobalVariable *clgv) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000271 std::string targetclassName;
Bill Wendling5f689e72011-11-04 18:48:00 +0000272 if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName))
273 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000274
Bill Wendling5f689e72011-11-04 18:48:00 +0000275 NameAndAttributes info;
276 StringMap<NameAndAttributes>::value_type &entry =
277 _undefines.GetOrCreateValue(targetclassName);
278 if (entry.getValue().name)
279 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000280
Bill Wendling5f689e72011-11-04 18:48:00 +0000281 const char *symbolName = entry.getKey().data();
282 info.name = symbolName;
283 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000284 info.isFunction = false;
285 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000286 entry.setValue(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000287}
288
Bill Wendlingfb440502012-03-28 20:46:54 +0000289/// addDefinedDataSymbol - Add a data symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000290void LTOModule::addDefinedDataSymbol(const GlobalValue *v) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000291 // Add to list of defined symbols.
Bill Wendlinga2af6742011-11-04 09:30:19 +0000292 addDefinedSymbol(v, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000293
Bill Wendling45f74e32012-08-06 22:52:45 +0000294 if (!v->hasSection() /* || !isTargetDarwin */)
295 return;
296
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000297 // Special case i386/ppc ObjC data structures in magic sections:
298 // The issue is that the old ObjC object format did some strange
299 // contortions to avoid real linker symbols. For instance, the
300 // ObjC class data structure is allocated statically in the executable
301 // that defines that class. That data structures contains a pointer to
302 // its superclass. But instead of just initializing that part of the
303 // struct to the address of its superclass, and letting the static and
304 // dynamic linkers do the rest, the runtime works by having that field
305 // instead point to a C-string that is the name of the superclass.
306 // At runtime the objc initialization updates that pointer and sets
307 // it to point to the actual super class. As far as the linker
308 // knows it is just a pointer to a string. But then someone wanted the
309 // linker to issue errors at build time if the superclass was not found.
310 // So they figured out a way in mach-o object format to use an absolute
311 // symbols (.objc_class_name_Foo = 0) and a floating reference
312 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
313 // a class was missing.
314 // The following synthesizes the implicit .objc_* symbols for the linker
315 // from the ObjC data structures generated by the front end.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000316
Bill Wendling45f74e32012-08-06 22:52:45 +0000317 // special case if this data blob is an ObjC class definition
318 if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000319 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000320 addObjCClass(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000321 }
Bill Wendling45f74e32012-08-06 22:52:45 +0000322 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000323
Bill Wendling45f74e32012-08-06 22:52:45 +0000324 // special case if this data blob is an ObjC category definition
325 else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000326 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000327 addObjCCategory(gv);
328 }
329 }
330
331 // special case if this data blob is the list of referenced classes
332 else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000333 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000334 addObjCClassRef(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000335 }
336 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000337}
338
Bill Wendlingfb440502012-03-28 20:46:54 +0000339/// addDefinedFunctionSymbol - Add a function symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000340void LTOModule::addDefinedFunctionSymbol(const Function *f) {
Bill Wendlingfb440502012-03-28 20:46:54 +0000341 // add to list of defined symbols
342 addDefinedSymbol(f, true);
343}
344
Rafael Espindola282a4702013-10-31 20:51:58 +0000345static bool canBeHidden(const GlobalValue *GV) {
346 GlobalValue::LinkageTypes L = GV->getLinkage();
347
Rafael Espindola282a4702013-10-31 20:51:58 +0000348 if (L != GlobalValue::LinkOnceODRLinkage)
349 return false;
350
351 if (GV->hasUnnamedAddr())
352 return true;
353
354 GlobalStatus GS;
355 if (GlobalStatus::analyzeGlobal(GV, GS))
356 return false;
357
358 return !GS.IsCompared;
359}
360
Bill Wendlingfb440502012-03-28 20:46:54 +0000361/// addDefinedSymbol - Add a defined symbol to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000362void LTOModule::addDefinedSymbol(const GlobalValue *def, bool isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000363 // ignore all llvm.* symbols
364 if (def->getName().startswith("llvm."))
365 return;
366
367 // string is owned by _defines
Rafael Espindola34b59382011-02-11 05:23:09 +0000368 SmallString<64> Buffer;
Rafael Espindola117b20c2013-12-05 05:53:12 +0000369 _mangler.getNameWithPrefix(Buffer, def);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000370
371 // set alignment part log2() can have rounding errors
372 uint32_t align = def->getAlignment();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000373 uint32_t attr = align ? countTrailingZeros(def->getAlignment()) : 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000374
375 // set permissions part
Bill Wendling9ee2d332012-03-29 08:27:32 +0000376 if (isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000377 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000378 } else {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000379 const GlobalVariable *gv = dyn_cast<GlobalVariable>(def);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000380 if (gv && gv->isConstant())
381 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
382 else
383 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
384 }
385
386 // set definition part
Bill Wendling2776d462010-09-27 18:05:19 +0000387 if (def->hasWeakLinkage() || def->hasLinkOnceLinkage() ||
Bill Wendling34bc34e2012-08-17 18:33:14 +0000388 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000389 attr |= LTO_SYMBOL_DEFINITION_WEAK;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000390 else if (def->hasCommonLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000391 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000392 else
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000393 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000394
395 // set scope part
396 if (def->hasHiddenVisibility())
397 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
398 else if (def->hasProtectedVisibility())
399 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
Rafael Espindola282a4702013-10-31 20:51:58 +0000400 else if (canBeHidden(def))
401 attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000402 else if (def->hasExternalLinkage() || def->hasWeakLinkage() ||
403 def->hasLinkOnceLinkage() || def->hasCommonLinkage() ||
404 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000405 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
406 else
407 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
408
Chad Rosier772a91f2011-06-28 18:26:12 +0000409 StringSet::value_type &entry = _defines.GetOrCreateValue(Buffer);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000410 entry.setValue(1);
411
Bill Wendling9ee2d332012-03-29 08:27:32 +0000412 // fill information structure
413 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000414 StringRef Name = entry.getKey();
415 info.name = Name.data();
416 assert(info.name[Name.size()] == '\0');
Bill Wendling9ee2d332012-03-29 08:27:32 +0000417 info.attributes = attr;
418 info.isFunction = isFunction;
419 info.symbol = def;
420
421 // add to table of symbols
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000422 _symbols.push_back(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000423}
424
Bill Wendlingfb440502012-03-28 20:46:54 +0000425/// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
426/// defined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000427void LTOModule::addAsmGlobalSymbol(const char *name,
428 lto_symbol_attributes scope) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000429 StringSet::value_type &entry = _defines.GetOrCreateValue(name);
430
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000431 // only add new define if not already defined
Rafael Espindola477d11f2011-02-20 16:27:25 +0000432 if (entry.getValue())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000433 return;
434
Rafael Espindola477d11f2011-02-20 16:27:25 +0000435 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000436
437 NameAndAttributes &info = _undefines[entry.getKey().data()];
438
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000439 if (info.symbol == 0) {
Bill Wendling71b19bb2012-04-02 10:01:21 +0000440 // FIXME: This is trying to take care of module ASM like this:
441 //
442 // module asm ".zerofill __FOO, __foo, _bar_baz_qux, 0"
443 //
444 // but is gross and its mother dresses it funny. Have the ASM parser give us
445 // more details for this type of situation so that we're not guessing so
446 // much.
447
448 // fill information structure
Rafael Espindola5f4b32f2012-05-11 03:42:13 +0000449 info.name = entry.getKey().data();
Bill Wendling71b19bb2012-04-02 10:01:21 +0000450 info.attributes =
451 LTO_SYMBOL_PERMISSIONS_DATA | LTO_SYMBOL_DEFINITION_REGULAR | scope;
452 info.isFunction = false;
453 info.symbol = 0;
454
455 // add to table of symbols
456 _symbols.push_back(info);
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000457 return;
458 }
459
Bill Wendling9ee2d332012-03-29 08:27:32 +0000460 if (info.isFunction)
461 addDefinedFunctionSymbol(cast<Function>(info.symbol));
462 else
463 addDefinedDataSymbol(info.symbol);
Bill Wendling8f6c8a92012-03-30 23:26:06 +0000464
465 _symbols.back().attributes &= ~LTO_SYMBOL_SCOPE_MASK;
466 _symbols.back().attributes |= scope;
Devang Patela59fe952008-07-16 18:06:52 +0000467}
Nick Kledzik07b4a622008-02-26 20:26:43 +0000468
Bill Wendlingfb440502012-03-28 20:46:54 +0000469/// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the
470/// undefined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000471void LTOModule::addAsmGlobalSymbolUndef(const char *name) {
472 StringMap<NameAndAttributes>::value_type &entry =
473 _undefines.GetOrCreateValue(name);
474
475 _asm_undefines.push_back(entry.getKey().data());
476
477 // we already have the symbol
478 if (entry.getValue().name)
479 return;
480
481 uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;;
482 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
483 NameAndAttributes info;
484 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000485 info.attributes = attr;
486 info.isFunction = false;
487 info.symbol = 0;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000488
489 entry.setValue(info);
490}
491
Bill Wendlingfb440502012-03-28 20:46:54 +0000492/// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet to a
493/// list to be resolved later.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000494void
495LTOModule::addPotentialUndefinedSymbol(const GlobalValue *decl, bool isFunc) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000496 // ignore all llvm.* symbols
497 if (decl->getName().startswith("llvm."))
498 return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000499
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000500 // ignore all aliases
501 if (isa<GlobalAlias>(decl))
502 return;
Nick Lewycky0661b932009-07-09 06:03:04 +0000503
Rafael Espindola34b59382011-02-11 05:23:09 +0000504 SmallString<64> name;
Rafael Espindola117b20c2013-12-05 05:53:12 +0000505 _mangler.getNameWithPrefix(name, decl);
Rafael Espindola56548522009-04-24 16:55:21 +0000506
Rafael Espindola477d11f2011-02-20 16:27:25 +0000507 StringMap<NameAndAttributes>::value_type &entry =
Chad Rosier772a91f2011-06-28 18:26:12 +0000508 _undefines.GetOrCreateValue(name);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000509
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000510 // we already have the symbol
Rafael Espindola477d11f2011-02-20 16:27:25 +0000511 if (entry.getValue().name)
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000512 return;
Rafael Espindola56548522009-04-24 16:55:21 +0000513
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000514 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000515
516 info.name = entry.getKey().data();
Bill Wendlingfb440502012-03-28 20:46:54 +0000517
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000518 if (decl->hasExternalWeakLinkage())
519 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
520 else
521 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000522
Bill Wendling9ee2d332012-03-29 08:27:32 +0000523 info.isFunction = isFunc;
524 info.symbol = decl;
525
Rafael Espindola477d11f2011-02-20 16:27:25 +0000526 entry.setValue(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000527}
528
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000529namespace {
530 class RecordStreamer : public MCStreamer {
531 public:
Bill Wendling32867652012-04-03 03:56:52 +0000532 enum State { NeverSeen, Global, Defined, DefinedGlobal, Used };
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000533
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000534 private:
535 StringMap<State> Symbols;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000536
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000537 void markDefined(const MCSymbol &Symbol) {
538 State &S = Symbols[Symbol.getName()];
539 switch (S) {
540 case DefinedGlobal:
541 case Global:
542 S = DefinedGlobal;
543 break;
544 case NeverSeen:
545 case Defined:
546 case Used:
547 S = Defined;
548 break;
549 }
550 }
551 void markGlobal(const MCSymbol &Symbol) {
552 State &S = Symbols[Symbol.getName()];
553 switch (S) {
554 case DefinedGlobal:
555 case Defined:
556 S = DefinedGlobal;
557 break;
558
559 case NeverSeen:
560 case Global:
561 case Used:
562 S = Global;
563 break;
564 }
565 }
566 void markUsed(const MCSymbol &Symbol) {
567 State &S = Symbols[Symbol.getName()];
568 switch (S) {
569 case DefinedGlobal:
570 case Defined:
571 case Global:
572 break;
573
574 case NeverSeen:
575 case Used:
576 S = Used;
577 break;
578 }
579 }
580
581 // FIXME: mostly copied for the obj streamer.
582 void AddValueSymbols(const MCExpr *Value) {
583 switch (Value->getKind()) {
584 case MCExpr::Target:
585 // FIXME: What should we do in here?
586 break;
587
588 case MCExpr::Constant:
589 break;
590
591 case MCExpr::Binary: {
592 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
593 AddValueSymbols(BE->getLHS());
594 AddValueSymbols(BE->getRHS());
595 break;
596 }
597
598 case MCExpr::SymbolRef:
599 markUsed(cast<MCSymbolRefExpr>(Value)->getSymbol());
600 break;
601
602 case MCExpr::Unary:
603 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
604 break;
605 }
606 }
607
608 public:
609 typedef StringMap<State>::const_iterator const_iterator;
610
611 const_iterator begin() {
612 return Symbols.begin();
613 }
614
615 const_iterator end() {
616 return Symbols.end();
617 }
618
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000619 RecordStreamer(MCContext &Context) : MCStreamer(Context, 0) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000620
Bill Wendling32867652012-04-03 03:56:52 +0000621 virtual void EmitInstruction(const MCInst &Inst) {
622 // Scan for values.
623 for (unsigned i = Inst.getNumOperands(); i--; )
624 if (Inst.getOperand(i).isExpr())
625 AddValueSymbols(Inst.getOperand(i).getExpr());
626 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000627 virtual void EmitLabel(MCSymbol *Symbol) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000628 Symbol->setSection(*getCurrentSection().first);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000629 markDefined(*Symbol);
630 }
Reed Kotleraee4d5d12012-12-16 04:00:45 +0000631 virtual void EmitDebugLabel(MCSymbol *Symbol) {
632 EmitLabel(Symbol);
633 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000634 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
635 // FIXME: should we handle aliases?
636 markDefined(*Symbol);
637 }
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000638 virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000639 if (Attribute == MCSA_Global)
640 markGlobal(*Symbol);
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000641 return true;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000642 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000643 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Cheng95847992012-06-22 20:30:39 +0000644 uint64_t Size , unsigned ByteAlignment) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000645 markDefined(*Symbol);
646 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000647 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
648 unsigned ByteAlignment) {
649 markDefined(*Symbol);
650 }
Bill Wendling32867652012-04-03 03:56:52 +0000651
Eli Benderskyf483ff92012-12-20 19:05:53 +0000652 virtual void EmitBundleAlignMode(unsigned AlignPow2) {}
Eli Bendersky802b6282013-01-07 21:51:08 +0000653 virtual void EmitBundleLock(bool AlignToEnd) {}
Eli Benderskyf483ff92012-12-20 19:05:53 +0000654 virtual void EmitBundleUnlock() {}
655
Bill Wendling32867652012-04-03 03:56:52 +0000656 // Noop calls.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000657 virtual void ChangeSection(const MCSection *Section,
658 const MCExpr *Subsection) {}
Eli Benderskycbb25142013-01-14 19:04:57 +0000659 virtual void InitToTextSection() {}
Bill Wendling32867652012-04-03 03:56:52 +0000660 virtual void InitSections() {}
661 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {}
662 virtual void EmitThumbFunc(MCSymbol *Func) {}
663 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {}
664 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {}
665 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {}
666 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {}
667 virtual void EmitCOFFSymbolType(int Type) {}
668 virtual void EndCOFFSymbolDef() {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000669 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {}
Benjamin Kramer63970512011-09-01 23:04:27 +0000670 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
671 unsigned ByteAlignment) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000672 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
673 uint64_t Size, unsigned ByteAlignment) {}
Rafael Espindola64e1af82013-07-02 15:49:13 +0000674 virtual void EmitBytes(StringRef Data) {}
675 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {}
Rafael Espindola6aea5922011-04-21 23:39:26 +0000676 virtual void EmitULEB128Value(const MCExpr *Value) {}
677 virtual void EmitSLEB128Value(const MCExpr *Value) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000678 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
679 unsigned ValueSize,
680 unsigned MaxBytesToEmit) {}
681 virtual void EmitCodeAlignment(unsigned ByteAlignment,
682 unsigned MaxBytesToEmit) {}
Jim Grosbachb5912772012-01-27 00:37:08 +0000683 virtual bool EmitValueToOffset(const MCExpr *Offset,
684 unsigned char Value ) { return false; }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000685 virtual void EmitFileDirective(StringRef Filename) {}
686 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
687 const MCSymbol *LastLabel,
Evan Chengc7ac6902011-07-14 05:43:07 +0000688 const MCSymbol *Label,
689 unsigned PointerSize) {}
Rafael Espindola07082092012-01-07 03:13:18 +0000690 virtual void FinishImpl() {}
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000691 virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
692 RecordProcEnd(Frame);
693 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000694 };
Bill Wendling9ee2d332012-03-29 08:27:32 +0000695} // end anonymous namespace
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000696
Bill Wendlingfb440502012-03-28 20:46:54 +0000697/// addAsmGlobalSymbols - Add global symbols from module-level ASM to the
698/// defined or undefined lists.
Bill Wendlingac2abde2011-11-04 09:24:40 +0000699bool LTOModule::addAsmGlobalSymbols(std::string &errMsg) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000700 const std::string &inlineAsm = _module->getModuleInlineAsm();
Ivan Krasincc2a8012011-09-08 07:38:25 +0000701 if (inlineAsm.empty())
702 return false;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000703
Bill Wendlingac2abde2011-11-04 09:24:40 +0000704 OwningPtr<RecordStreamer> Streamer(new RecordStreamer(_context));
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000705 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(inlineAsm);
706 SourceMgr SrcMgr;
707 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach345768c2011-08-16 18:33:49 +0000708 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr,
Bill Wendlingac2abde2011-11-04 09:24:40 +0000709 _context, *Streamer,
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000710 *_target->getMCAsmInfo()));
Bill Wendling9351b3e2012-08-08 22:01:55 +0000711 const Target &T = _target->getTarget();
Joey Goulydb6144e2013-09-12 12:55:29 +0000712 OwningPtr<MCInstrInfo> MCII(T.createMCInstrInfo());
Bill Wendling9351b3e2012-08-08 22:01:55 +0000713 OwningPtr<MCSubtargetInfo>
714 STI(T.createMCSubtargetInfo(_target->getTargetTriple(),
715 _target->getTargetCPU(),
716 _target->getTargetFeatureString()));
Joey Goulydb6144e2013-09-12 12:55:29 +0000717 OwningPtr<MCTargetAsmParser> TAP(T.createMCAsmParser(*STI, *Parser.get(), *MCII));
Ivan Krasin8149dd62011-09-08 07:36:39 +0000718 if (!TAP) {
Bill Wendling9351b3e2012-08-08 22:01:55 +0000719 errMsg = "target " + std::string(T.getName()) +
720 " does not define AsmParser.";
Ivan Krasin8149dd62011-09-08 07:36:39 +0000721 return true;
722 }
723
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000724 Parser->setTargetParser(*TAP);
Bill Wendling9351b3e2012-08-08 22:01:55 +0000725 if (Parser->Run(false))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000726 return true;
727
728 for (RecordStreamer::const_iterator i = Streamer->begin(),
729 e = Streamer->end(); i != e; ++i) {
730 StringRef Key = i->first();
731 RecordStreamer::State Value = i->second;
732 if (Value == RecordStreamer::DefinedGlobal)
733 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_DEFAULT);
734 else if (Value == RecordStreamer::Defined)
735 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_INTERNAL);
736 else if (Value == RecordStreamer::Global ||
737 Value == RecordStreamer::Used)
738 addAsmGlobalSymbolUndef(Key.data());
739 }
Bill Wendling9351b3e2012-08-08 22:01:55 +0000740
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000741 return false;
742}
743
Bill Wendlingfb440502012-03-28 20:46:54 +0000744/// isDeclaration - Return 'true' if the global value is a declaration.
Rafael Espindola5b778b22011-03-18 19:51:00 +0000745static bool isDeclaration(const GlobalValue &V) {
746 if (V.hasAvailableExternallyLinkage())
747 return true;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000748
Rafael Espindola5b778b22011-03-18 19:51:00 +0000749 if (V.isMaterializable())
750 return false;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000751
Rafael Espindola5b778b22011-03-18 19:51:00 +0000752 return V.isDeclaration();
753}
754
Bill Wendling7e58b382012-03-28 23:12:18 +0000755/// parseSymbols - Parse the symbols from the module and model-level ASM and add
Bill Wendlingfb440502012-03-28 20:46:54 +0000756/// them to either the defined or undefined lists.
Bill Wendling7e58b382012-03-28 23:12:18 +0000757bool LTOModule::parseSymbols(std::string &errMsg) {
Daniel Dunbar919660b2010-08-10 23:46:46 +0000758 // add functions
Bill Wendling763acfc2012-03-29 03:34:57 +0000759 for (Module::iterator f = _module->begin(), e = _module->end(); f != e; ++f) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000760 if (isDeclaration(*f))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000761 addPotentialUndefinedSymbol(f, true);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000762 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000763 addDefinedFunctionSymbol(f);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000764 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000765
Daniel Dunbar919660b2010-08-10 23:46:46 +0000766 // add data
767 for (Module::global_iterator v = _module->global_begin(),
768 e = _module->global_end(); v != e; ++v) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000769 if (isDeclaration(*v))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000770 addPotentialUndefinedSymbol(v, false);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000771 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000772 addDefinedDataSymbol(v);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000773 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000774
Daniel Dunbar919660b2010-08-10 23:46:46 +0000775 // add asm globals
Bill Wendlingac2abde2011-11-04 09:24:40 +0000776 if (addAsmGlobalSymbols(errMsg))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000777 return true;
Daniel Dunbar919660b2010-08-10 23:46:46 +0000778
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000779 // add aliases
Bill Wendling9ee2d332012-03-29 08:27:32 +0000780 for (Module::alias_iterator a = _module->alias_begin(),
781 e = _module->alias_end(); a != e; ++a) {
782 if (isDeclaration(*a->getAliasedGlobal()))
Bill Wendlingd58ed732012-03-28 20:48:49 +0000783 // Is an alias to a declaration.
Bill Wendling9ee2d332012-03-29 08:27:32 +0000784 addPotentialUndefinedSymbol(a, false);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000785 else
Bill Wendling9ee2d332012-03-29 08:27:32 +0000786 addDefinedDataSymbol(a);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000787 }
788
Daniel Dunbar919660b2010-08-10 23:46:46 +0000789 // make symbols for all undefines
Bill Wendling9ee2d332012-03-29 08:27:32 +0000790 for (StringMap<NameAndAttributes>::iterator u =_undefines.begin(),
791 e = _undefines.end(); u != e; ++u) {
792 // If this symbol also has a definition, then don't make an undefine because
793 // it is a tentative definition.
794 if (_defines.count(u->getKey())) continue;
795 NameAndAttributes info = u->getValue();
796 _symbols.push_back(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000797 }
Bill Wendling9ee2d332012-03-29 08:27:32 +0000798
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000799 return false;
Nick Kledzik91a6dcf2008-02-27 22:25:36 +0000800}