blob: 65416bed92773f4e5b156815cd47c5336bf0ed69 [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
Rafael Espindola282a4702013-10-31 20:51:58 +0000342 if (L != GlobalValue::LinkOnceODRLinkage)
343 return false;
344
345 if (GV->hasUnnamedAddr())
346 return true;
347
348 GlobalStatus GS;
349 if (GlobalStatus::analyzeGlobal(GV, GS))
350 return false;
351
352 return !GS.IsCompared;
353}
354
Bill Wendlingfb440502012-03-28 20:46:54 +0000355/// addDefinedSymbol - Add a defined symbol to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000356void LTOModule::addDefinedSymbol(const GlobalValue *def, bool isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000357 // ignore all llvm.* symbols
358 if (def->getName().startswith("llvm."))
359 return;
360
361 // string is owned by _defines
Rafael Espindola34b59382011-02-11 05:23:09 +0000362 SmallString<64> Buffer;
Bill Wendlinga2af6742011-11-04 09:30:19 +0000363 _mangler.getNameWithPrefix(Buffer, def, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000364
365 // set alignment part log2() can have rounding errors
366 uint32_t align = def->getAlignment();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000367 uint32_t attr = align ? countTrailingZeros(def->getAlignment()) : 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000368
369 // set permissions part
Bill Wendling9ee2d332012-03-29 08:27:32 +0000370 if (isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000371 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000372 } else {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000373 const GlobalVariable *gv = dyn_cast<GlobalVariable>(def);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000374 if (gv && gv->isConstant())
375 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
376 else
377 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
378 }
379
380 // set definition part
Bill Wendling2776d462010-09-27 18:05:19 +0000381 if (def->hasWeakLinkage() || def->hasLinkOnceLinkage() ||
Bill Wendling34bc34e2012-08-17 18:33:14 +0000382 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000383 attr |= LTO_SYMBOL_DEFINITION_WEAK;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000384 else if (def->hasCommonLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000385 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000386 else
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000387 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000388
389 // set scope part
390 if (def->hasHiddenVisibility())
391 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
392 else if (def->hasProtectedVisibility())
393 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
Rafael Espindola282a4702013-10-31 20:51:58 +0000394 else if (canBeHidden(def))
395 attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000396 else if (def->hasExternalLinkage() || def->hasWeakLinkage() ||
397 def->hasLinkOnceLinkage() || def->hasCommonLinkage() ||
398 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000399 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
400 else
401 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
402
Chad Rosier772a91f2011-06-28 18:26:12 +0000403 StringSet::value_type &entry = _defines.GetOrCreateValue(Buffer);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000404 entry.setValue(1);
405
Bill Wendling9ee2d332012-03-29 08:27:32 +0000406 // fill information structure
407 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000408 StringRef Name = entry.getKey();
409 info.name = Name.data();
410 assert(info.name[Name.size()] == '\0');
Bill Wendling9ee2d332012-03-29 08:27:32 +0000411 info.attributes = attr;
412 info.isFunction = isFunction;
413 info.symbol = def;
414
415 // add to table of symbols
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000416 _symbols.push_back(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000417}
418
Bill Wendlingfb440502012-03-28 20:46:54 +0000419/// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
420/// defined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000421void LTOModule::addAsmGlobalSymbol(const char *name,
422 lto_symbol_attributes scope) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000423 StringSet::value_type &entry = _defines.GetOrCreateValue(name);
424
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000425 // only add new define if not already defined
Rafael Espindola477d11f2011-02-20 16:27:25 +0000426 if (entry.getValue())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000427 return;
428
Rafael Espindola477d11f2011-02-20 16:27:25 +0000429 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000430
431 NameAndAttributes &info = _undefines[entry.getKey().data()];
432
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000433 if (info.symbol == 0) {
Bill Wendling71b19bb2012-04-02 10:01:21 +0000434 // FIXME: This is trying to take care of module ASM like this:
435 //
436 // module asm ".zerofill __FOO, __foo, _bar_baz_qux, 0"
437 //
438 // but is gross and its mother dresses it funny. Have the ASM parser give us
439 // more details for this type of situation so that we're not guessing so
440 // much.
441
442 // fill information structure
Rafael Espindola5f4b32f2012-05-11 03:42:13 +0000443 info.name = entry.getKey().data();
Bill Wendling71b19bb2012-04-02 10:01:21 +0000444 info.attributes =
445 LTO_SYMBOL_PERMISSIONS_DATA | LTO_SYMBOL_DEFINITION_REGULAR | scope;
446 info.isFunction = false;
447 info.symbol = 0;
448
449 // add to table of symbols
450 _symbols.push_back(info);
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000451 return;
452 }
453
Bill Wendling9ee2d332012-03-29 08:27:32 +0000454 if (info.isFunction)
455 addDefinedFunctionSymbol(cast<Function>(info.symbol));
456 else
457 addDefinedDataSymbol(info.symbol);
Bill Wendling8f6c8a92012-03-30 23:26:06 +0000458
459 _symbols.back().attributes &= ~LTO_SYMBOL_SCOPE_MASK;
460 _symbols.back().attributes |= scope;
Devang Patela59fe952008-07-16 18:06:52 +0000461}
Nick Kledzik07b4a622008-02-26 20:26:43 +0000462
Bill Wendlingfb440502012-03-28 20:46:54 +0000463/// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the
464/// undefined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000465void LTOModule::addAsmGlobalSymbolUndef(const char *name) {
466 StringMap<NameAndAttributes>::value_type &entry =
467 _undefines.GetOrCreateValue(name);
468
469 _asm_undefines.push_back(entry.getKey().data());
470
471 // we already have the symbol
472 if (entry.getValue().name)
473 return;
474
475 uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;;
476 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
477 NameAndAttributes info;
478 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000479 info.attributes = attr;
480 info.isFunction = false;
481 info.symbol = 0;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000482
483 entry.setValue(info);
484}
485
Bill Wendlingfb440502012-03-28 20:46:54 +0000486/// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet to a
487/// list to be resolved later.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000488void
489LTOModule::addPotentialUndefinedSymbol(const GlobalValue *decl, bool isFunc) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000490 // ignore all llvm.* symbols
491 if (decl->getName().startswith("llvm."))
492 return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000493
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000494 // ignore all aliases
495 if (isa<GlobalAlias>(decl))
496 return;
Nick Lewycky0661b932009-07-09 06:03:04 +0000497
Rafael Espindola34b59382011-02-11 05:23:09 +0000498 SmallString<64> name;
Bill Wendlinga2af6742011-11-04 09:30:19 +0000499 _mangler.getNameWithPrefix(name, decl, false);
Rafael Espindola56548522009-04-24 16:55:21 +0000500
Rafael Espindola477d11f2011-02-20 16:27:25 +0000501 StringMap<NameAndAttributes>::value_type &entry =
Chad Rosier772a91f2011-06-28 18:26:12 +0000502 _undefines.GetOrCreateValue(name);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000503
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000504 // we already have the symbol
Rafael Espindola477d11f2011-02-20 16:27:25 +0000505 if (entry.getValue().name)
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000506 return;
Rafael Espindola56548522009-04-24 16:55:21 +0000507
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000508 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000509
510 info.name = entry.getKey().data();
Bill Wendlingfb440502012-03-28 20:46:54 +0000511
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000512 if (decl->hasExternalWeakLinkage())
513 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
514 else
515 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000516
Bill Wendling9ee2d332012-03-29 08:27:32 +0000517 info.isFunction = isFunc;
518 info.symbol = decl;
519
Rafael Espindola477d11f2011-02-20 16:27:25 +0000520 entry.setValue(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000521}
522
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000523namespace {
524 class RecordStreamer : public MCStreamer {
525 public:
Bill Wendling32867652012-04-03 03:56:52 +0000526 enum State { NeverSeen, Global, Defined, DefinedGlobal, Used };
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000527
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000528 private:
529 StringMap<State> Symbols;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000530
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000531 void markDefined(const MCSymbol &Symbol) {
532 State &S = Symbols[Symbol.getName()];
533 switch (S) {
534 case DefinedGlobal:
535 case Global:
536 S = DefinedGlobal;
537 break;
538 case NeverSeen:
539 case Defined:
540 case Used:
541 S = Defined;
542 break;
543 }
544 }
545 void markGlobal(const MCSymbol &Symbol) {
546 State &S = Symbols[Symbol.getName()];
547 switch (S) {
548 case DefinedGlobal:
549 case Defined:
550 S = DefinedGlobal;
551 break;
552
553 case NeverSeen:
554 case Global:
555 case Used:
556 S = Global;
557 break;
558 }
559 }
560 void markUsed(const MCSymbol &Symbol) {
561 State &S = Symbols[Symbol.getName()];
562 switch (S) {
563 case DefinedGlobal:
564 case Defined:
565 case Global:
566 break;
567
568 case NeverSeen:
569 case Used:
570 S = Used;
571 break;
572 }
573 }
574
575 // FIXME: mostly copied for the obj streamer.
576 void AddValueSymbols(const MCExpr *Value) {
577 switch (Value->getKind()) {
578 case MCExpr::Target:
579 // FIXME: What should we do in here?
580 break;
581
582 case MCExpr::Constant:
583 break;
584
585 case MCExpr::Binary: {
586 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
587 AddValueSymbols(BE->getLHS());
588 AddValueSymbols(BE->getRHS());
589 break;
590 }
591
592 case MCExpr::SymbolRef:
593 markUsed(cast<MCSymbolRefExpr>(Value)->getSymbol());
594 break;
595
596 case MCExpr::Unary:
597 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
598 break;
599 }
600 }
601
602 public:
603 typedef StringMap<State>::const_iterator const_iterator;
604
605 const_iterator begin() {
606 return Symbols.begin();
607 }
608
609 const_iterator end() {
610 return Symbols.end();
611 }
612
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000613 RecordStreamer(MCContext &Context) : MCStreamer(Context, 0) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000614
Bill Wendling32867652012-04-03 03:56:52 +0000615 virtual void EmitInstruction(const MCInst &Inst) {
616 // Scan for values.
617 for (unsigned i = Inst.getNumOperands(); i--; )
618 if (Inst.getOperand(i).isExpr())
619 AddValueSymbols(Inst.getOperand(i).getExpr());
620 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000621 virtual void EmitLabel(MCSymbol *Symbol) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000622 Symbol->setSection(*getCurrentSection().first);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000623 markDefined(*Symbol);
624 }
Reed Kotleraee4d5d12012-12-16 04:00:45 +0000625 virtual void EmitDebugLabel(MCSymbol *Symbol) {
626 EmitLabel(Symbol);
627 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000628 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
629 // FIXME: should we handle aliases?
630 markDefined(*Symbol);
631 }
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000632 virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000633 if (Attribute == MCSA_Global)
634 markGlobal(*Symbol);
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000635 return true;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000636 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000637 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Cheng95847992012-06-22 20:30:39 +0000638 uint64_t Size , unsigned ByteAlignment) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000639 markDefined(*Symbol);
640 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000641 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
642 unsigned ByteAlignment) {
643 markDefined(*Symbol);
644 }
Bill Wendling32867652012-04-03 03:56:52 +0000645
Eli Benderskyf483ff92012-12-20 19:05:53 +0000646 virtual void EmitBundleAlignMode(unsigned AlignPow2) {}
Eli Bendersky802b6282013-01-07 21:51:08 +0000647 virtual void EmitBundleLock(bool AlignToEnd) {}
Eli Benderskyf483ff92012-12-20 19:05:53 +0000648 virtual void EmitBundleUnlock() {}
649
Bill Wendling32867652012-04-03 03:56:52 +0000650 // Noop calls.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000651 virtual void ChangeSection(const MCSection *Section,
652 const MCExpr *Subsection) {}
Eli Benderskycbb25142013-01-14 19:04:57 +0000653 virtual void InitToTextSection() {}
Bill Wendling32867652012-04-03 03:56:52 +0000654 virtual void InitSections() {}
655 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {}
656 virtual void EmitThumbFunc(MCSymbol *Func) {}
657 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {}
658 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {}
659 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {}
660 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {}
661 virtual void EmitCOFFSymbolType(int Type) {}
662 virtual void EndCOFFSymbolDef() {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000663 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {}
Benjamin Kramer63970512011-09-01 23:04:27 +0000664 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
665 unsigned ByteAlignment) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000666 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
667 uint64_t Size, unsigned ByteAlignment) {}
Rafael Espindola64e1af82013-07-02 15:49:13 +0000668 virtual void EmitBytes(StringRef Data) {}
669 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {}
Rafael Espindola6aea5922011-04-21 23:39:26 +0000670 virtual void EmitULEB128Value(const MCExpr *Value) {}
671 virtual void EmitSLEB128Value(const MCExpr *Value) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000672 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
673 unsigned ValueSize,
674 unsigned MaxBytesToEmit) {}
675 virtual void EmitCodeAlignment(unsigned ByteAlignment,
676 unsigned MaxBytesToEmit) {}
Jim Grosbachb5912772012-01-27 00:37:08 +0000677 virtual bool EmitValueToOffset(const MCExpr *Offset,
678 unsigned char Value ) { return false; }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000679 virtual void EmitFileDirective(StringRef Filename) {}
680 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
681 const MCSymbol *LastLabel,
Evan Chengc7ac6902011-07-14 05:43:07 +0000682 const MCSymbol *Label,
683 unsigned PointerSize) {}
Rafael Espindola07082092012-01-07 03:13:18 +0000684 virtual void FinishImpl() {}
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000685 virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
686 RecordProcEnd(Frame);
687 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000688 };
Bill Wendling9ee2d332012-03-29 08:27:32 +0000689} // end anonymous namespace
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000690
Bill Wendlingfb440502012-03-28 20:46:54 +0000691/// addAsmGlobalSymbols - Add global symbols from module-level ASM to the
692/// defined or undefined lists.
Bill Wendlingac2abde2011-11-04 09:24:40 +0000693bool LTOModule::addAsmGlobalSymbols(std::string &errMsg) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000694 const std::string &inlineAsm = _module->getModuleInlineAsm();
Ivan Krasincc2a8012011-09-08 07:38:25 +0000695 if (inlineAsm.empty())
696 return false;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000697
Bill Wendlingac2abde2011-11-04 09:24:40 +0000698 OwningPtr<RecordStreamer> Streamer(new RecordStreamer(_context));
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000699 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(inlineAsm);
700 SourceMgr SrcMgr;
701 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach345768c2011-08-16 18:33:49 +0000702 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr,
Bill Wendlingac2abde2011-11-04 09:24:40 +0000703 _context, *Streamer,
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000704 *_target->getMCAsmInfo()));
Bill Wendling9351b3e2012-08-08 22:01:55 +0000705 const Target &T = _target->getTarget();
Joey Goulydb6144e2013-09-12 12:55:29 +0000706 OwningPtr<MCInstrInfo> MCII(T.createMCInstrInfo());
Bill Wendling9351b3e2012-08-08 22:01:55 +0000707 OwningPtr<MCSubtargetInfo>
708 STI(T.createMCSubtargetInfo(_target->getTargetTriple(),
709 _target->getTargetCPU(),
710 _target->getTargetFeatureString()));
Joey Goulydb6144e2013-09-12 12:55:29 +0000711 OwningPtr<MCTargetAsmParser> TAP(T.createMCAsmParser(*STI, *Parser.get(), *MCII));
Ivan Krasin8149dd62011-09-08 07:36:39 +0000712 if (!TAP) {
Bill Wendling9351b3e2012-08-08 22:01:55 +0000713 errMsg = "target " + std::string(T.getName()) +
714 " does not define AsmParser.";
Ivan Krasin8149dd62011-09-08 07:36:39 +0000715 return true;
716 }
717
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000718 Parser->setTargetParser(*TAP);
Bill Wendling9351b3e2012-08-08 22:01:55 +0000719 if (Parser->Run(false))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000720 return true;
721
722 for (RecordStreamer::const_iterator i = Streamer->begin(),
723 e = Streamer->end(); i != e; ++i) {
724 StringRef Key = i->first();
725 RecordStreamer::State Value = i->second;
726 if (Value == RecordStreamer::DefinedGlobal)
727 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_DEFAULT);
728 else if (Value == RecordStreamer::Defined)
729 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_INTERNAL);
730 else if (Value == RecordStreamer::Global ||
731 Value == RecordStreamer::Used)
732 addAsmGlobalSymbolUndef(Key.data());
733 }
Bill Wendling9351b3e2012-08-08 22:01:55 +0000734
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000735 return false;
736}
737
Bill Wendlingfb440502012-03-28 20:46:54 +0000738/// isDeclaration - Return 'true' if the global value is a declaration.
Rafael Espindola5b778b22011-03-18 19:51:00 +0000739static bool isDeclaration(const GlobalValue &V) {
740 if (V.hasAvailableExternallyLinkage())
741 return true;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000742
Rafael Espindola5b778b22011-03-18 19:51:00 +0000743 if (V.isMaterializable())
744 return false;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000745
Rafael Espindola5b778b22011-03-18 19:51:00 +0000746 return V.isDeclaration();
747}
748
Bill Wendling7e58b382012-03-28 23:12:18 +0000749/// parseSymbols - Parse the symbols from the module and model-level ASM and add
Bill Wendlingfb440502012-03-28 20:46:54 +0000750/// them to either the defined or undefined lists.
Bill Wendling7e58b382012-03-28 23:12:18 +0000751bool LTOModule::parseSymbols(std::string &errMsg) {
Daniel Dunbar919660b2010-08-10 23:46:46 +0000752 // add functions
Bill Wendling763acfc2012-03-29 03:34:57 +0000753 for (Module::iterator f = _module->begin(), e = _module->end(); f != e; ++f) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000754 if (isDeclaration(*f))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000755 addPotentialUndefinedSymbol(f, true);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000756 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000757 addDefinedFunctionSymbol(f);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000758 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000759
Daniel Dunbar919660b2010-08-10 23:46:46 +0000760 // add data
761 for (Module::global_iterator v = _module->global_begin(),
762 e = _module->global_end(); v != e; ++v) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000763 if (isDeclaration(*v))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000764 addPotentialUndefinedSymbol(v, false);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000765 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000766 addDefinedDataSymbol(v);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000767 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000768
Daniel Dunbar919660b2010-08-10 23:46:46 +0000769 // add asm globals
Bill Wendlingac2abde2011-11-04 09:24:40 +0000770 if (addAsmGlobalSymbols(errMsg))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000771 return true;
Daniel Dunbar919660b2010-08-10 23:46:46 +0000772
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000773 // add aliases
Bill Wendling9ee2d332012-03-29 08:27:32 +0000774 for (Module::alias_iterator a = _module->alias_begin(),
775 e = _module->alias_end(); a != e; ++a) {
776 if (isDeclaration(*a->getAliasedGlobal()))
Bill Wendlingd58ed732012-03-28 20:48:49 +0000777 // Is an alias to a declaration.
Bill Wendling9ee2d332012-03-29 08:27:32 +0000778 addPotentialUndefinedSymbol(a, false);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000779 else
Bill Wendling9ee2d332012-03-29 08:27:32 +0000780 addDefinedDataSymbol(a);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000781 }
782
Daniel Dunbar919660b2010-08-10 23:46:46 +0000783 // make symbols for all undefines
Bill Wendling9ee2d332012-03-29 08:27:32 +0000784 for (StringMap<NameAndAttributes>::iterator u =_undefines.begin(),
785 e = _undefines.end(); u != e; ++u) {
786 // If this symbol also has a definition, then don't make an undefine because
787 // it is a tentative definition.
788 if (_defines.count(u->getKey())) continue;
789 NameAndAttributes info = u->getValue();
790 _symbols.push_back(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000791 }
Bill Wendling9ee2d332012-03-29 08:27:32 +0000792
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000793 return false;
Nick Kledzik91a6dcf2008-02-27 22:25:36 +0000794}