blob: 91240aa5a0010d082c0fd6dd5191d3e49206900c [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"
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),
Bill Wendlingbc07a892013-06-18 07:20:20 +000046 _context(_target->getMCAsmInfo(), _target->getRegisterInfo(), NULL),
Rafael Espindolae133ed82013-10-29 17:28:26 +000047 _mangler(t) {}
Bill Wendlingfb440502012-03-28 20:46:54 +000048
49/// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM
50/// bitcode.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000051bool LTOModule::isBitcodeFile(const void *mem, size_t length) {
Rafael Espindola46ed3532013-06-11 18:05:26 +000052 return sys::fs::identify_magic(StringRef((const char *)mem, length)) ==
53 sys::fs::file_magic::bitcode;
Nick Kledzik07b4a622008-02-26 20:26:43 +000054}
55
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000056bool LTOModule::isBitcodeFile(const char *path) {
Rafael Espindola71affba2013-06-12 15:13:57 +000057 sys::fs::file_magic type;
58 if (sys::fs::identify_magic(path, type))
59 return false;
60 return type == sys::fs::file_magic::bitcode;
Nick Kledzik07b4a622008-02-26 20:26:43 +000061}
62
Bill Wendlingfb440502012-03-28 20:46:54 +000063/// isBitcodeFileForTarget - Returns 'true' if the file (or memory contents) is
64/// LLVM bitcode for the specified triple.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000065bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length,
66 const char *triplePrefix) {
67 MemoryBuffer *buffer = makeBuffer(mem, length);
68 if (!buffer)
Nick Kledzikb481c202009-06-01 20:33:09 +000069 return false;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000070 return isTargetMatch(buffer, triplePrefix);
Nick Kledzikb481c202009-06-01 20:33:09 +000071}
72
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000073bool LTOModule::isBitcodeFileForTarget(const char *path,
74 const char *triplePrefix) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +000075 OwningPtr<MemoryBuffer> buffer;
76 if (MemoryBuffer::getFile(path, buffer))
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000077 return false;
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +000078 return isTargetMatch(buffer.take(), triplePrefix);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000079}
80
Bill Wendlingfb440502012-03-28 20:46:54 +000081/// isTargetMatch - Returns 'true' if the memory buffer is for the specified
82/// target triple.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000083bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) {
Bill Wendling0198ce02010-10-06 01:22:42 +000084 std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
85 delete buffer;
Bill Wendling5f689e72011-11-04 18:48:00 +000086 return strncmp(Triple.c_str(), triplePrefix, strlen(triplePrefix)) == 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000087}
88
Bill Wendlingfb440502012-03-28 20:46:54 +000089/// makeLTOModule - Create an LTOModule. N.B. These methods take ownership of
90/// the buffer.
Rafael Espindola0b385c72013-09-30 16:39:19 +000091LTOModule *LTOModule::makeLTOModule(const char *path, TargetOptions options,
92 std::string &errMsg) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +000093 OwningPtr<MemoryBuffer> buffer;
94 if (error_code ec = MemoryBuffer::getFile(path, buffer)) {
Michael J. Spencerd4227232010-12-09 18:06:07 +000095 errMsg = ec.message();
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000096 return NULL;
Michael J. Spencerd4227232010-12-09 18:06:07 +000097 }
Rafael Espindola0b385c72013-09-30 16:39:19 +000098 return makeLTOModule(buffer.take(), options, errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000099}
100
Rafael Espindola56e41f72011-02-08 22:40:47 +0000101LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000102 size_t size, TargetOptions options,
103 std::string &errMsg) {
104 return makeLTOModule(fd, path, size, 0, options, errMsg);
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000105}
106
107LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000108 size_t map_size,
109 off_t offset,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000110 TargetOptions options,
Rafael Espindola56e41f72011-02-08 22:40:47 +0000111 std::string &errMsg) {
112 OwningPtr<MemoryBuffer> buffer;
Rafael Espindola3d2ac2e2013-07-23 20:25:01 +0000113 if (error_code ec =
114 MemoryBuffer::getOpenFileSlice(fd, path, buffer, map_size, offset)) {
Rafael Espindola56e41f72011-02-08 22:40:47 +0000115 errMsg = ec.message();
116 return NULL;
117 }
Rafael Espindola0b385c72013-09-30 16:39:19 +0000118 return makeLTOModule(buffer.take(), options, errMsg);
Rafael Espindola56e41f72011-02-08 22:40:47 +0000119}
120
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000121LTOModule *LTOModule::makeLTOModule(const void *mem, size_t length,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000122 TargetOptions options,
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000123 std::string &errMsg) {
124 OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length));
125 if (!buffer)
126 return NULL;
Rafael Espindola0b385c72013-09-30 16:39:19 +0000127 return makeLTOModule(buffer.take(), options, errMsg);
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000128}
129
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000130LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000131 TargetOptions options,
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000132 std::string &errMsg) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000133 // parse bitcode buffer
Rafael Espindola5b778b22011-03-18 19:51:00 +0000134 OwningPtr<Module> m(getLazyBitcodeModule(buffer, getGlobalContext(),
135 &errMsg));
136 if (!m) {
137 delete buffer;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000138 return NULL;
Rafael Espindola5b778b22011-03-18 19:51:00 +0000139 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000140
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000141 std::string TripleStr = m->getTargetTriple();
142 if (TripleStr.empty())
143 TripleStr = sys::getDefaultTargetTriple();
144 llvm::Triple Triple(TripleStr);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000145
146 // find machine architecture for this module
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000147 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000148 if (!march)
149 return NULL;
150
Nick Lewycky364c04a2011-04-21 01:54:08 +0000151 // construct LTOModule, hand over ownership of module and target
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000152 SubtargetFeatures Features;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000153 Features.getDefaultSubtargetFeatures(Triple);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000154 std::string FeatureStr = Features.getString();
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000155 // Set a default CPU for Darwin triples.
Evan Chengfe6e4052011-06-30 01:53:36 +0000156 std::string CPU;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000157 if (Triple.isOSDarwin()) {
158 if (Triple.getArch() == llvm::Triple::x86_64)
159 CPU = "core2";
160 else if (Triple.getArch() == llvm::Triple::x86)
161 CPU = "yonah";
162 }
Rafael Espindola0b385c72013-09-30 16:39:19 +0000163
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000164 TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000165 options);
Rafael Espindola282a4702013-10-31 20:51:58 +0000166 m->MaterializeAllPermanently();
167
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000168 LTOModule *Ret = new LTOModule(m.take(), target);
Bill Wendling7e58b382012-03-28 23:12:18 +0000169 if (Ret->parseSymbols(errMsg)) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000170 delete Ret;
171 return NULL;
172 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000173
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000174 return Ret;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000175}
176
Bill Wendlingfb440502012-03-28 20:46:54 +0000177/// makeBuffer - Create a MemoryBuffer from a memory range.
178MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length) {
Roman Divackyad06cee2012-09-05 22:26:57 +0000179 const char *startPtr = (const char*)mem;
Bill Wendlingfb440502012-03-28 20:46:54 +0000180 return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), "", false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000181}
182
Bill Wendlingfb440502012-03-28 20:46:54 +0000183/// objcClassNameFromExpression - Get string that the data pointer points to.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000184bool
185LTOModule::objcClassNameFromExpression(const Constant *c, std::string &name) {
186 if (const ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000187 Constant *op = ce->getOperand(0);
188 if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) {
189 Constant *cn = gvn->getInitializer();
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000190 if (ConstantDataArray *ca = dyn_cast<ConstantDataArray>(cn)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000191 if (ca->isCString()) {
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000192 name = ".objc_class_name_" + ca->getAsCString().str();
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000193 return true;
Nick Kledzikb481c202009-06-01 20:33:09 +0000194 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000195 }
Nick Kledzikb481c202009-06-01 20:33:09 +0000196 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000197 }
198 return false;
199}
200
Bill Wendlingfb440502012-03-28 20:46:54 +0000201/// addObjCClass - Parse i386/ppc ObjC class data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000202void LTOModule::addObjCClass(const GlobalVariable *clgv) {
203 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000204 if (!c) return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000205
Bill Wendling5f689e72011-11-04 18:48:00 +0000206 // second slot in __OBJC,__class is pointer to superclass name
207 std::string superclassName;
208 if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
209 NameAndAttributes info;
210 StringMap<NameAndAttributes>::value_type &entry =
211 _undefines.GetOrCreateValue(superclassName);
212 if (!entry.getValue().name) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000213 const char *symbolName = entry.getKey().data();
214 info.name = symbolName;
215 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000216 info.isFunction = false;
217 info.symbol = clgv;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000218 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000219 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000220 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000221
222 // third slot in __OBJC,__class is pointer to class name
223 std::string className;
224 if (objcClassNameFromExpression(c->getOperand(2), className)) {
225 StringSet::value_type &entry = _defines.GetOrCreateValue(className);
226 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000227
Bill Wendling5f689e72011-11-04 18:48:00 +0000228 NameAndAttributes info;
229 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000230 info.attributes = LTO_SYMBOL_PERMISSIONS_DATA |
231 LTO_SYMBOL_DEFINITION_REGULAR | LTO_SYMBOL_SCOPE_DEFAULT;
232 info.isFunction = false;
233 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000234 _symbols.push_back(info);
235 }
236}
237
Bill Wendlingfb440502012-03-28 20:46:54 +0000238/// addObjCCategory - Parse i386/ppc ObjC category data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000239void LTOModule::addObjCCategory(const GlobalVariable *clgv) {
240 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000241 if (!c) return;
242
243 // second slot in __OBJC,__category is pointer to target class name
244 std::string targetclassName;
245 if (!objcClassNameFromExpression(c->getOperand(1), targetclassName))
246 return;
247
248 NameAndAttributes info;
249 StringMap<NameAndAttributes>::value_type &entry =
250 _undefines.GetOrCreateValue(targetclassName);
251
252 if (entry.getValue().name)
253 return;
254
255 const char *symbolName = entry.getKey().data();
256 info.name = symbolName;
257 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000258 info.isFunction = false;
259 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000260 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000261}
262
Bill Wendlingfb440502012-03-28 20:46:54 +0000263/// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000264void LTOModule::addObjCClassRef(const GlobalVariable *clgv) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000265 std::string targetclassName;
Bill Wendling5f689e72011-11-04 18:48:00 +0000266 if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName))
267 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000268
Bill Wendling5f689e72011-11-04 18:48:00 +0000269 NameAndAttributes info;
270 StringMap<NameAndAttributes>::value_type &entry =
271 _undefines.GetOrCreateValue(targetclassName);
272 if (entry.getValue().name)
273 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000274
Bill Wendling5f689e72011-11-04 18:48:00 +0000275 const char *symbolName = entry.getKey().data();
276 info.name = symbolName;
277 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000278 info.isFunction = false;
279 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000280 entry.setValue(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000281}
282
Bill Wendlingfb440502012-03-28 20:46:54 +0000283/// addDefinedDataSymbol - Add a data symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000284void LTOModule::addDefinedDataSymbol(const GlobalValue *v) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000285 // Add to list of defined symbols.
Bill Wendlinga2af6742011-11-04 09:30:19 +0000286 addDefinedSymbol(v, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000287
Bill Wendling45f74e32012-08-06 22:52:45 +0000288 if (!v->hasSection() /* || !isTargetDarwin */)
289 return;
290
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000291 // Special case i386/ppc ObjC data structures in magic sections:
292 // The issue is that the old ObjC object format did some strange
293 // contortions to avoid real linker symbols. For instance, the
294 // ObjC class data structure is allocated statically in the executable
295 // that defines that class. That data structures contains a pointer to
296 // its superclass. But instead of just initializing that part of the
297 // struct to the address of its superclass, and letting the static and
298 // dynamic linkers do the rest, the runtime works by having that field
299 // instead point to a C-string that is the name of the superclass.
300 // At runtime the objc initialization updates that pointer and sets
301 // it to point to the actual super class. As far as the linker
302 // knows it is just a pointer to a string. But then someone wanted the
303 // linker to issue errors at build time if the superclass was not found.
304 // So they figured out a way in mach-o object format to use an absolute
305 // symbols (.objc_class_name_Foo = 0) and a floating reference
306 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
307 // a class was missing.
308 // The following synthesizes the implicit .objc_* symbols for the linker
309 // from the ObjC data structures generated by the front end.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000310
Bill Wendling45f74e32012-08-06 22:52:45 +0000311 // special case if this data blob is an ObjC class definition
312 if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000313 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000314 addObjCClass(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000315 }
Bill Wendling45f74e32012-08-06 22:52:45 +0000316 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000317
Bill Wendling45f74e32012-08-06 22:52:45 +0000318 // special case if this data blob is an ObjC category definition
319 else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000320 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000321 addObjCCategory(gv);
322 }
323 }
324
325 // special case if this data blob is the list of referenced classes
326 else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000327 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000328 addObjCClassRef(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000329 }
330 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000331}
332
Bill Wendlingfb440502012-03-28 20:46:54 +0000333/// addDefinedFunctionSymbol - Add a function symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000334void LTOModule::addDefinedFunctionSymbol(const Function *f) {
Bill Wendlingfb440502012-03-28 20:46:54 +0000335 // add to list of defined symbols
336 addDefinedSymbol(f, true);
337}
338
Rafael Espindola282a4702013-10-31 20:51:58 +0000339static bool canBeHidden(const GlobalValue *GV) {
340 GlobalValue::LinkageTypes L = GV->getLinkage();
341
342 if (L == GlobalValue::LinkOnceODRAutoHideLinkage)
343 return true;
344
345 if (L != GlobalValue::LinkOnceODRLinkage)
346 return false;
347
348 if (GV->hasUnnamedAddr())
349 return true;
350
351 GlobalStatus GS;
352 if (GlobalStatus::analyzeGlobal(GV, GS))
353 return false;
354
355 return !GS.IsCompared;
356}
357
Bill Wendlingfb440502012-03-28 20:46:54 +0000358/// addDefinedSymbol - Add a defined symbol to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000359void LTOModule::addDefinedSymbol(const GlobalValue *def, bool isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000360 // ignore all llvm.* symbols
361 if (def->getName().startswith("llvm."))
362 return;
363
364 // string is owned by _defines
Rafael Espindola34b59382011-02-11 05:23:09 +0000365 SmallString<64> Buffer;
Bill Wendlinga2af6742011-11-04 09:30:19 +0000366 _mangler.getNameWithPrefix(Buffer, def, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000367
368 // set alignment part log2() can have rounding errors
369 uint32_t align = def->getAlignment();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000370 uint32_t attr = align ? countTrailingZeros(def->getAlignment()) : 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000371
372 // set permissions part
Bill Wendling9ee2d332012-03-29 08:27:32 +0000373 if (isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000374 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000375 } else {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000376 const GlobalVariable *gv = dyn_cast<GlobalVariable>(def);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000377 if (gv && gv->isConstant())
378 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
379 else
380 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
381 }
382
383 // set definition part
Bill Wendling2776d462010-09-27 18:05:19 +0000384 if (def->hasWeakLinkage() || def->hasLinkOnceLinkage() ||
Bill Wendling34bc34e2012-08-17 18:33:14 +0000385 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000386 attr |= LTO_SYMBOL_DEFINITION_WEAK;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000387 else if (def->hasCommonLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000388 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000389 else
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000390 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000391
392 // set scope part
393 if (def->hasHiddenVisibility())
394 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
395 else if (def->hasProtectedVisibility())
396 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
Rafael Espindola282a4702013-10-31 20:51:58 +0000397 else if (canBeHidden(def))
398 attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000399 else if (def->hasExternalLinkage() || def->hasWeakLinkage() ||
400 def->hasLinkOnceLinkage() || def->hasCommonLinkage() ||
401 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000402 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
403 else
404 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
405
Chad Rosier772a91f2011-06-28 18:26:12 +0000406 StringSet::value_type &entry = _defines.GetOrCreateValue(Buffer);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000407 entry.setValue(1);
408
Bill Wendling9ee2d332012-03-29 08:27:32 +0000409 // fill information structure
410 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000411 StringRef Name = entry.getKey();
412 info.name = Name.data();
413 assert(info.name[Name.size()] == '\0');
Bill Wendling9ee2d332012-03-29 08:27:32 +0000414 info.attributes = attr;
415 info.isFunction = isFunction;
416 info.symbol = def;
417
418 // add to table of symbols
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000419 _symbols.push_back(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000420}
421
Bill Wendlingfb440502012-03-28 20:46:54 +0000422/// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
423/// defined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000424void LTOModule::addAsmGlobalSymbol(const char *name,
425 lto_symbol_attributes scope) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000426 StringSet::value_type &entry = _defines.GetOrCreateValue(name);
427
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000428 // only add new define if not already defined
Rafael Espindola477d11f2011-02-20 16:27:25 +0000429 if (entry.getValue())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000430 return;
431
Rafael Espindola477d11f2011-02-20 16:27:25 +0000432 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000433
434 NameAndAttributes &info = _undefines[entry.getKey().data()];
435
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000436 if (info.symbol == 0) {
Bill Wendling71b19bb2012-04-02 10:01:21 +0000437 // FIXME: This is trying to take care of module ASM like this:
438 //
439 // module asm ".zerofill __FOO, __foo, _bar_baz_qux, 0"
440 //
441 // but is gross and its mother dresses it funny. Have the ASM parser give us
442 // more details for this type of situation so that we're not guessing so
443 // much.
444
445 // fill information structure
Rafael Espindola5f4b32f2012-05-11 03:42:13 +0000446 info.name = entry.getKey().data();
Bill Wendling71b19bb2012-04-02 10:01:21 +0000447 info.attributes =
448 LTO_SYMBOL_PERMISSIONS_DATA | LTO_SYMBOL_DEFINITION_REGULAR | scope;
449 info.isFunction = false;
450 info.symbol = 0;
451
452 // add to table of symbols
453 _symbols.push_back(info);
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000454 return;
455 }
456
Bill Wendling9ee2d332012-03-29 08:27:32 +0000457 if (info.isFunction)
458 addDefinedFunctionSymbol(cast<Function>(info.symbol));
459 else
460 addDefinedDataSymbol(info.symbol);
Bill Wendling8f6c8a92012-03-30 23:26:06 +0000461
462 _symbols.back().attributes &= ~LTO_SYMBOL_SCOPE_MASK;
463 _symbols.back().attributes |= scope;
Devang Patela59fe952008-07-16 18:06:52 +0000464}
Nick Kledzik07b4a622008-02-26 20:26:43 +0000465
Bill Wendlingfb440502012-03-28 20:46:54 +0000466/// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the
467/// undefined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000468void LTOModule::addAsmGlobalSymbolUndef(const char *name) {
469 StringMap<NameAndAttributes>::value_type &entry =
470 _undefines.GetOrCreateValue(name);
471
472 _asm_undefines.push_back(entry.getKey().data());
473
474 // we already have the symbol
475 if (entry.getValue().name)
476 return;
477
478 uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;;
479 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
480 NameAndAttributes info;
481 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000482 info.attributes = attr;
483 info.isFunction = false;
484 info.symbol = 0;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000485
486 entry.setValue(info);
487}
488
Bill Wendlingfb440502012-03-28 20:46:54 +0000489/// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet to a
490/// list to be resolved later.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000491void
492LTOModule::addPotentialUndefinedSymbol(const GlobalValue *decl, bool isFunc) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000493 // ignore all llvm.* symbols
494 if (decl->getName().startswith("llvm."))
495 return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000496
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000497 // ignore all aliases
498 if (isa<GlobalAlias>(decl))
499 return;
Nick Lewycky0661b932009-07-09 06:03:04 +0000500
Rafael Espindola34b59382011-02-11 05:23:09 +0000501 SmallString<64> name;
Bill Wendlinga2af6742011-11-04 09:30:19 +0000502 _mangler.getNameWithPrefix(name, decl, false);
Rafael Espindola56548522009-04-24 16:55:21 +0000503
Rafael Espindola477d11f2011-02-20 16:27:25 +0000504 StringMap<NameAndAttributes>::value_type &entry =
Chad Rosier772a91f2011-06-28 18:26:12 +0000505 _undefines.GetOrCreateValue(name);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000506
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000507 // we already have the symbol
Rafael Espindola477d11f2011-02-20 16:27:25 +0000508 if (entry.getValue().name)
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000509 return;
Rafael Espindola56548522009-04-24 16:55:21 +0000510
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000511 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000512
513 info.name = entry.getKey().data();
Bill Wendlingfb440502012-03-28 20:46:54 +0000514
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000515 if (decl->hasExternalWeakLinkage())
516 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
517 else
518 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000519
Bill Wendling9ee2d332012-03-29 08:27:32 +0000520 info.isFunction = isFunc;
521 info.symbol = decl;
522
Rafael Espindola477d11f2011-02-20 16:27:25 +0000523 entry.setValue(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000524}
525
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000526namespace {
527 class RecordStreamer : public MCStreamer {
528 public:
Bill Wendling32867652012-04-03 03:56:52 +0000529 enum State { NeverSeen, Global, Defined, DefinedGlobal, Used };
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000530
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000531 private:
532 StringMap<State> Symbols;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000533
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000534 void markDefined(const MCSymbol &Symbol) {
535 State &S = Symbols[Symbol.getName()];
536 switch (S) {
537 case DefinedGlobal:
538 case Global:
539 S = DefinedGlobal;
540 break;
541 case NeverSeen:
542 case Defined:
543 case Used:
544 S = Defined;
545 break;
546 }
547 }
548 void markGlobal(const MCSymbol &Symbol) {
549 State &S = Symbols[Symbol.getName()];
550 switch (S) {
551 case DefinedGlobal:
552 case Defined:
553 S = DefinedGlobal;
554 break;
555
556 case NeverSeen:
557 case Global:
558 case Used:
559 S = Global;
560 break;
561 }
562 }
563 void markUsed(const MCSymbol &Symbol) {
564 State &S = Symbols[Symbol.getName()];
565 switch (S) {
566 case DefinedGlobal:
567 case Defined:
568 case Global:
569 break;
570
571 case NeverSeen:
572 case Used:
573 S = Used;
574 break;
575 }
576 }
577
578 // FIXME: mostly copied for the obj streamer.
579 void AddValueSymbols(const MCExpr *Value) {
580 switch (Value->getKind()) {
581 case MCExpr::Target:
582 // FIXME: What should we do in here?
583 break;
584
585 case MCExpr::Constant:
586 break;
587
588 case MCExpr::Binary: {
589 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
590 AddValueSymbols(BE->getLHS());
591 AddValueSymbols(BE->getRHS());
592 break;
593 }
594
595 case MCExpr::SymbolRef:
596 markUsed(cast<MCSymbolRefExpr>(Value)->getSymbol());
597 break;
598
599 case MCExpr::Unary:
600 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
601 break;
602 }
603 }
604
605 public:
606 typedef StringMap<State>::const_iterator const_iterator;
607
608 const_iterator begin() {
609 return Symbols.begin();
610 }
611
612 const_iterator end() {
613 return Symbols.end();
614 }
615
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000616 RecordStreamer(MCContext &Context) : MCStreamer(Context, 0) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000617
Bill Wendling32867652012-04-03 03:56:52 +0000618 virtual void EmitInstruction(const MCInst &Inst) {
619 // Scan for values.
620 for (unsigned i = Inst.getNumOperands(); i--; )
621 if (Inst.getOperand(i).isExpr())
622 AddValueSymbols(Inst.getOperand(i).getExpr());
623 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000624 virtual void EmitLabel(MCSymbol *Symbol) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000625 Symbol->setSection(*getCurrentSection().first);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000626 markDefined(*Symbol);
627 }
Reed Kotleraee4d5d12012-12-16 04:00:45 +0000628 virtual void EmitDebugLabel(MCSymbol *Symbol) {
629 EmitLabel(Symbol);
630 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000631 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
632 // FIXME: should we handle aliases?
633 markDefined(*Symbol);
634 }
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000635 virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000636 if (Attribute == MCSA_Global)
637 markGlobal(*Symbol);
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000638 return true;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000639 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000640 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Cheng95847992012-06-22 20:30:39 +0000641 uint64_t Size , unsigned ByteAlignment) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000642 markDefined(*Symbol);
643 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000644 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
645 unsigned ByteAlignment) {
646 markDefined(*Symbol);
647 }
Bill Wendling32867652012-04-03 03:56:52 +0000648
Eli Benderskyf483ff92012-12-20 19:05:53 +0000649 virtual void EmitBundleAlignMode(unsigned AlignPow2) {}
Eli Bendersky802b6282013-01-07 21:51:08 +0000650 virtual void EmitBundleLock(bool AlignToEnd) {}
Eli Benderskyf483ff92012-12-20 19:05:53 +0000651 virtual void EmitBundleUnlock() {}
652
Bill Wendling32867652012-04-03 03:56:52 +0000653 // Noop calls.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000654 virtual void ChangeSection(const MCSection *Section,
655 const MCExpr *Subsection) {}
Eli Benderskycbb25142013-01-14 19:04:57 +0000656 virtual void InitToTextSection() {}
Bill Wendling32867652012-04-03 03:56:52 +0000657 virtual void InitSections() {}
658 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {}
659 virtual void EmitThumbFunc(MCSymbol *Func) {}
660 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {}
661 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {}
662 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {}
663 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {}
664 virtual void EmitCOFFSymbolType(int Type) {}
665 virtual void EndCOFFSymbolDef() {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000666 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {}
Benjamin Kramer63970512011-09-01 23:04:27 +0000667 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
668 unsigned ByteAlignment) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000669 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
670 uint64_t Size, unsigned ByteAlignment) {}
Rafael Espindola64e1af82013-07-02 15:49:13 +0000671 virtual void EmitBytes(StringRef Data) {}
672 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {}
Rafael Espindola6aea5922011-04-21 23:39:26 +0000673 virtual void EmitULEB128Value(const MCExpr *Value) {}
674 virtual void EmitSLEB128Value(const MCExpr *Value) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000675 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
676 unsigned ValueSize,
677 unsigned MaxBytesToEmit) {}
678 virtual void EmitCodeAlignment(unsigned ByteAlignment,
679 unsigned MaxBytesToEmit) {}
Jim Grosbachb5912772012-01-27 00:37:08 +0000680 virtual bool EmitValueToOffset(const MCExpr *Offset,
681 unsigned char Value ) { return false; }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000682 virtual void EmitFileDirective(StringRef Filename) {}
683 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
684 const MCSymbol *LastLabel,
Evan Chengc7ac6902011-07-14 05:43:07 +0000685 const MCSymbol *Label,
686 unsigned PointerSize) {}
Rafael Espindola07082092012-01-07 03:13:18 +0000687 virtual void FinishImpl() {}
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000688 virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
689 RecordProcEnd(Frame);
690 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000691 };
Bill Wendling9ee2d332012-03-29 08:27:32 +0000692} // end anonymous namespace
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000693
Bill Wendlingfb440502012-03-28 20:46:54 +0000694/// addAsmGlobalSymbols - Add global symbols from module-level ASM to the
695/// defined or undefined lists.
Bill Wendlingac2abde2011-11-04 09:24:40 +0000696bool LTOModule::addAsmGlobalSymbols(std::string &errMsg) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000697 const std::string &inlineAsm = _module->getModuleInlineAsm();
Ivan Krasincc2a8012011-09-08 07:38:25 +0000698 if (inlineAsm.empty())
699 return false;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000700
Bill Wendlingac2abde2011-11-04 09:24:40 +0000701 OwningPtr<RecordStreamer> Streamer(new RecordStreamer(_context));
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000702 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(inlineAsm);
703 SourceMgr SrcMgr;
704 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach345768c2011-08-16 18:33:49 +0000705 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr,
Bill Wendlingac2abde2011-11-04 09:24:40 +0000706 _context, *Streamer,
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000707 *_target->getMCAsmInfo()));
Bill Wendling9351b3e2012-08-08 22:01:55 +0000708 const Target &T = _target->getTarget();
Joey Goulydb6144e2013-09-12 12:55:29 +0000709 OwningPtr<MCInstrInfo> MCII(T.createMCInstrInfo());
Bill Wendling9351b3e2012-08-08 22:01:55 +0000710 OwningPtr<MCSubtargetInfo>
711 STI(T.createMCSubtargetInfo(_target->getTargetTriple(),
712 _target->getTargetCPU(),
713 _target->getTargetFeatureString()));
Joey Goulydb6144e2013-09-12 12:55:29 +0000714 OwningPtr<MCTargetAsmParser> TAP(T.createMCAsmParser(*STI, *Parser.get(), *MCII));
Ivan Krasin8149dd62011-09-08 07:36:39 +0000715 if (!TAP) {
Bill Wendling9351b3e2012-08-08 22:01:55 +0000716 errMsg = "target " + std::string(T.getName()) +
717 " does not define AsmParser.";
Ivan Krasin8149dd62011-09-08 07:36:39 +0000718 return true;
719 }
720
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000721 Parser->setTargetParser(*TAP);
Bill Wendling9351b3e2012-08-08 22:01:55 +0000722 if (Parser->Run(false))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000723 return true;
724
725 for (RecordStreamer::const_iterator i = Streamer->begin(),
726 e = Streamer->end(); i != e; ++i) {
727 StringRef Key = i->first();
728 RecordStreamer::State Value = i->second;
729 if (Value == RecordStreamer::DefinedGlobal)
730 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_DEFAULT);
731 else if (Value == RecordStreamer::Defined)
732 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_INTERNAL);
733 else if (Value == RecordStreamer::Global ||
734 Value == RecordStreamer::Used)
735 addAsmGlobalSymbolUndef(Key.data());
736 }
Bill Wendling9351b3e2012-08-08 22:01:55 +0000737
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000738 return false;
739}
740
Bill Wendlingfb440502012-03-28 20:46:54 +0000741/// isDeclaration - Return 'true' if the global value is a declaration.
Rafael Espindola5b778b22011-03-18 19:51:00 +0000742static bool isDeclaration(const GlobalValue &V) {
743 if (V.hasAvailableExternallyLinkage())
744 return true;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000745
Rafael Espindola5b778b22011-03-18 19:51:00 +0000746 if (V.isMaterializable())
747 return false;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000748
Rafael Espindola5b778b22011-03-18 19:51:00 +0000749 return V.isDeclaration();
750}
751
Bill Wendling7e58b382012-03-28 23:12:18 +0000752/// parseSymbols - Parse the symbols from the module and model-level ASM and add
Bill Wendlingfb440502012-03-28 20:46:54 +0000753/// them to either the defined or undefined lists.
Bill Wendling7e58b382012-03-28 23:12:18 +0000754bool LTOModule::parseSymbols(std::string &errMsg) {
Daniel Dunbar919660b2010-08-10 23:46:46 +0000755 // add functions
Bill Wendling763acfc2012-03-29 03:34:57 +0000756 for (Module::iterator f = _module->begin(), e = _module->end(); f != e; ++f) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000757 if (isDeclaration(*f))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000758 addPotentialUndefinedSymbol(f, true);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000759 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000760 addDefinedFunctionSymbol(f);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000761 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000762
Daniel Dunbar919660b2010-08-10 23:46:46 +0000763 // add data
764 for (Module::global_iterator v = _module->global_begin(),
765 e = _module->global_end(); v != e; ++v) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000766 if (isDeclaration(*v))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000767 addPotentialUndefinedSymbol(v, false);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000768 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000769 addDefinedDataSymbol(v);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000770 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000771
Daniel Dunbar919660b2010-08-10 23:46:46 +0000772 // add asm globals
Bill Wendlingac2abde2011-11-04 09:24:40 +0000773 if (addAsmGlobalSymbols(errMsg))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000774 return true;
Daniel Dunbar919660b2010-08-10 23:46:46 +0000775
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000776 // add aliases
Bill Wendling9ee2d332012-03-29 08:27:32 +0000777 for (Module::alias_iterator a = _module->alias_begin(),
778 e = _module->alias_end(); a != e; ++a) {
779 if (isDeclaration(*a->getAliasedGlobal()))
Bill Wendlingd58ed732012-03-28 20:48:49 +0000780 // Is an alias to a declaration.
Bill Wendling9ee2d332012-03-29 08:27:32 +0000781 addPotentialUndefinedSymbol(a, false);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000782 else
Bill Wendling9ee2d332012-03-29 08:27:32 +0000783 addDefinedDataSymbol(a);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000784 }
785
Daniel Dunbar919660b2010-08-10 23:46:46 +0000786 // make symbols for all undefines
Bill Wendling9ee2d332012-03-29 08:27:32 +0000787 for (StringMap<NameAndAttributes>::iterator u =_undefines.begin(),
788 e = _undefines.end(); u != e; ++u) {
789 // If this symbol also has a definition, then don't make an undefine because
790 // it is a tentative definition.
791 if (_defines.count(u->getKey())) continue;
792 NameAndAttributes info = u->getValue();
793 _symbols.push_back(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000794 }
Bill Wendling9ee2d332012-03-29 08:27:32 +0000795
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000796 return false;
Nick Kledzik91a6dcf2008-02-27 22:25:36 +0000797}