blob: 98c5416d8d1c691a71e6878603c41dd16d8a8c1d [file] [log] [blame]
Chris Lattner2eff5052010-03-12 18:44:54 +00001//===-- LTOModule.cpp - LLVM Link Time Optimizer --------------------------===//
Nick Kledzik07b4a622008-02-26 20:26:43 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +00007//
Nick Kledzik07b4a622008-02-26 20:26:43 +00008//===----------------------------------------------------------------------===//
9//
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000010// This file implements the Link Time Optimization library. This library is
Nick Kledzik07b4a622008-02-26 20:26:43 +000011// intended to be used by linker to optimize code at link time.
12//
13//===----------------------------------------------------------------------===//
14
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000015#include "llvm/LTO/LTOModule.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000016#include "llvm/ADT/OwningPtr.h"
17#include "llvm/ADT/Triple.h"
18#include "llvm/Bitcode/ReaderWriter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Constants.h"
20#include "llvm/IR/LLVMContext.h"
21#include "llvm/IR/Module.h"
Rafael Espindola1e49a6d2011-03-02 04:14:42 +000022#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCInst.h"
Joey Goulydb6144e2013-09-12 12:55:29 +000024#include "llvm/MC/MCInstrInfo.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000025#include "llvm/MC/MCParser/MCAsmParser.h"
Rafael Espindola1e49a6d2011-03-02 04:14:42 +000026#include "llvm/MC/MCStreamer.h"
Evan Cheng91111d22011-07-09 05:47:46 +000027#include "llvm/MC/MCSubtargetInfo.h"
Rafael Espindola1e49a6d2011-03-02 04:14:42 +000028#include "llvm/MC/MCSymbol.h"
Evan Cheng11424442011-07-26 00:24:13 +000029#include "llvm/MC/MCTargetAsmParser.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000030#include "llvm/MC/SubtargetFeature.h"
Bill Wendlingb8dcda72012-08-06 21:34:54 +000031#include "llvm/Support/CommandLine.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000032#include "llvm/Support/Host.h"
Rafael Espindola46ed3532013-06-11 18:05:26 +000033#include "llvm/Support/FileSystem.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000034#include "llvm/Support/MemoryBuffer.h"
35#include "llvm/Support/Path.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000036#include "llvm/Support/SourceMgr.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000037#include "llvm/Support/TargetRegistry.h"
38#include "llvm/Support/TargetSelect.h"
39#include "llvm/Support/system_error.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000040#include "llvm/Target/TargetRegisterInfo.h"
Nick Kledzik07b4a622008-02-26 20:26:43 +000041using namespace llvm;
42
Bill Wendlingfb440502012-03-28 20:46:54 +000043LTOModule::LTOModule(llvm::Module *m, llvm::TargetMachine *t)
44 : _module(m), _target(t),
Bill Wendlingbc07a892013-06-18 07:20:20 +000045 _context(_target->getMCAsmInfo(), _target->getRegisterInfo(), NULL),
Bill Wendling70b14002013-05-29 20:37:19 +000046 _mangler(_context, t) {}
Bill Wendlingfb440502012-03-28 20:46:54 +000047
48/// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM
49/// bitcode.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000050bool LTOModule::isBitcodeFile(const void *mem, size_t length) {
Rafael Espindola46ed3532013-06-11 18:05:26 +000051 return sys::fs::identify_magic(StringRef((const char *)mem, length)) ==
52 sys::fs::file_magic::bitcode;
Nick Kledzik07b4a622008-02-26 20:26:43 +000053}
54
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000055bool LTOModule::isBitcodeFile(const char *path) {
Rafael Espindola71affba2013-06-12 15:13:57 +000056 sys::fs::file_magic type;
57 if (sys::fs::identify_magic(path, type))
58 return false;
59 return type == sys::fs::file_magic::bitcode;
Nick Kledzik07b4a622008-02-26 20:26:43 +000060}
61
Bill Wendlingfb440502012-03-28 20:46:54 +000062/// isBitcodeFileForTarget - Returns 'true' if the file (or memory contents) is
63/// LLVM bitcode for the specified triple.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000064bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length,
65 const char *triplePrefix) {
66 MemoryBuffer *buffer = makeBuffer(mem, length);
67 if (!buffer)
Nick Kledzikb481c202009-06-01 20:33:09 +000068 return false;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000069 return isTargetMatch(buffer, triplePrefix);
Nick Kledzikb481c202009-06-01 20:33:09 +000070}
71
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000072bool LTOModule::isBitcodeFileForTarget(const char *path,
73 const char *triplePrefix) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +000074 OwningPtr<MemoryBuffer> buffer;
75 if (MemoryBuffer::getFile(path, buffer))
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000076 return false;
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +000077 return isTargetMatch(buffer.take(), triplePrefix);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000078}
79
Bill Wendlingfb440502012-03-28 20:46:54 +000080/// isTargetMatch - Returns 'true' if the memory buffer is for the specified
81/// target triple.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000082bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) {
Bill Wendling0198ce02010-10-06 01:22:42 +000083 std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
84 delete buffer;
Bill Wendling5f689e72011-11-04 18:48:00 +000085 return strncmp(Triple.c_str(), triplePrefix, strlen(triplePrefix)) == 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000086}
87
Bill Wendlingfb440502012-03-28 20:46:54 +000088/// makeLTOModule - Create an LTOModule. N.B. These methods take ownership of
89/// the buffer.
Rafael Espindola0b385c72013-09-30 16:39:19 +000090LTOModule *LTOModule::makeLTOModule(const char *path, TargetOptions options,
91 std::string &errMsg) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +000092 OwningPtr<MemoryBuffer> buffer;
93 if (error_code ec = MemoryBuffer::getFile(path, buffer)) {
Michael J. Spencerd4227232010-12-09 18:06:07 +000094 errMsg = ec.message();
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000095 return NULL;
Michael J. Spencerd4227232010-12-09 18:06:07 +000096 }
Rafael Espindola0b385c72013-09-30 16:39:19 +000097 return makeLTOModule(buffer.take(), options, errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000098}
99
Rafael Espindola56e41f72011-02-08 22:40:47 +0000100LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000101 size_t size, TargetOptions options,
102 std::string &errMsg) {
103 return makeLTOModule(fd, path, size, 0, options, errMsg);
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000104}
105
106LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000107 size_t map_size,
108 off_t offset,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000109 TargetOptions options,
Rafael Espindola56e41f72011-02-08 22:40:47 +0000110 std::string &errMsg) {
111 OwningPtr<MemoryBuffer> buffer;
Rafael Espindola3d2ac2e2013-07-23 20:25:01 +0000112 if (error_code ec =
113 MemoryBuffer::getOpenFileSlice(fd, path, buffer, map_size, offset)) {
Rafael Espindola56e41f72011-02-08 22:40:47 +0000114 errMsg = ec.message();
115 return NULL;
116 }
Rafael Espindola0b385c72013-09-30 16:39:19 +0000117 return makeLTOModule(buffer.take(), options, errMsg);
Rafael Espindola56e41f72011-02-08 22:40:47 +0000118}
119
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000120LTOModule *LTOModule::makeLTOModule(const void *mem, size_t length,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000121 TargetOptions options,
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000122 std::string &errMsg) {
123 OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length));
124 if (!buffer)
125 return NULL;
Rafael Espindola0b385c72013-09-30 16:39:19 +0000126 return makeLTOModule(buffer.take(), options, errMsg);
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000127}
128
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000129LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000130 TargetOptions options,
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000131 std::string &errMsg) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000132 // parse bitcode buffer
Rafael Espindola5b778b22011-03-18 19:51:00 +0000133 OwningPtr<Module> m(getLazyBitcodeModule(buffer, getGlobalContext(),
134 &errMsg));
135 if (!m) {
136 delete buffer;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000137 return NULL;
Rafael Espindola5b778b22011-03-18 19:51:00 +0000138 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000139
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000140 std::string TripleStr = m->getTargetTriple();
141 if (TripleStr.empty())
142 TripleStr = sys::getDefaultTargetTriple();
143 llvm::Triple Triple(TripleStr);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000144
145 // find machine architecture for this module
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000146 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000147 if (!march)
148 return NULL;
149
Nick Lewycky364c04a2011-04-21 01:54:08 +0000150 // construct LTOModule, hand over ownership of module and target
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000151 SubtargetFeatures Features;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000152 Features.getDefaultSubtargetFeatures(Triple);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000153 std::string FeatureStr = Features.getString();
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000154 // Set a default CPU for Darwin triples.
Evan Chengfe6e4052011-06-30 01:53:36 +0000155 std::string CPU;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000156 if (Triple.isOSDarwin()) {
157 if (Triple.getArch() == llvm::Triple::x86_64)
158 CPU = "core2";
159 else if (Triple.getArch() == llvm::Triple::x86)
160 CPU = "yonah";
161 }
Rafael Espindola0b385c72013-09-30 16:39:19 +0000162
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000163 TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000164 options);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000165 LTOModule *Ret = new LTOModule(m.take(), target);
Bill Wendling7e58b382012-03-28 23:12:18 +0000166 if (Ret->parseSymbols(errMsg)) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000167 delete Ret;
168 return NULL;
169 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000170
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000171 return Ret;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000172}
173
Bill Wendlingfb440502012-03-28 20:46:54 +0000174/// makeBuffer - Create a MemoryBuffer from a memory range.
175MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length) {
Roman Divackyad06cee2012-09-05 22:26:57 +0000176 const char *startPtr = (const char*)mem;
Bill Wendlingfb440502012-03-28 20:46:54 +0000177 return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), "", false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000178}
179
Bill Wendlingfb440502012-03-28 20:46:54 +0000180/// objcClassNameFromExpression - Get string that the data pointer points to.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000181bool
182LTOModule::objcClassNameFromExpression(const Constant *c, std::string &name) {
183 if (const ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000184 Constant *op = ce->getOperand(0);
185 if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) {
186 Constant *cn = gvn->getInitializer();
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000187 if (ConstantDataArray *ca = dyn_cast<ConstantDataArray>(cn)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000188 if (ca->isCString()) {
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000189 name = ".objc_class_name_" + ca->getAsCString().str();
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000190 return true;
Nick Kledzikb481c202009-06-01 20:33:09 +0000191 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000192 }
Nick Kledzikb481c202009-06-01 20:33:09 +0000193 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000194 }
195 return false;
196}
197
Bill Wendlingfb440502012-03-28 20:46:54 +0000198/// addObjCClass - Parse i386/ppc ObjC class data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000199void LTOModule::addObjCClass(const GlobalVariable *clgv) {
200 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000201 if (!c) return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000202
Bill Wendling5f689e72011-11-04 18:48:00 +0000203 // second slot in __OBJC,__class is pointer to superclass name
204 std::string superclassName;
205 if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
206 NameAndAttributes info;
207 StringMap<NameAndAttributes>::value_type &entry =
208 _undefines.GetOrCreateValue(superclassName);
209 if (!entry.getValue().name) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000210 const char *symbolName = entry.getKey().data();
211 info.name = symbolName;
212 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000213 info.isFunction = false;
214 info.symbol = clgv;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000215 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000216 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000217 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000218
219 // third slot in __OBJC,__class is pointer to class name
220 std::string className;
221 if (objcClassNameFromExpression(c->getOperand(2), className)) {
222 StringSet::value_type &entry = _defines.GetOrCreateValue(className);
223 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000224
Bill Wendling5f689e72011-11-04 18:48:00 +0000225 NameAndAttributes info;
226 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000227 info.attributes = LTO_SYMBOL_PERMISSIONS_DATA |
228 LTO_SYMBOL_DEFINITION_REGULAR | LTO_SYMBOL_SCOPE_DEFAULT;
229 info.isFunction = false;
230 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000231 _symbols.push_back(info);
232 }
233}
234
Bill Wendlingfb440502012-03-28 20:46:54 +0000235/// addObjCCategory - Parse i386/ppc ObjC category data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000236void LTOModule::addObjCCategory(const GlobalVariable *clgv) {
237 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000238 if (!c) return;
239
240 // second slot in __OBJC,__category is pointer to target class name
241 std::string targetclassName;
242 if (!objcClassNameFromExpression(c->getOperand(1), targetclassName))
243 return;
244
245 NameAndAttributes info;
246 StringMap<NameAndAttributes>::value_type &entry =
247 _undefines.GetOrCreateValue(targetclassName);
248
249 if (entry.getValue().name)
250 return;
251
252 const char *symbolName = entry.getKey().data();
253 info.name = symbolName;
254 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000255 info.isFunction = false;
256 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000257 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000258}
259
Bill Wendlingfb440502012-03-28 20:46:54 +0000260/// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000261void LTOModule::addObjCClassRef(const GlobalVariable *clgv) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000262 std::string targetclassName;
Bill Wendling5f689e72011-11-04 18:48:00 +0000263 if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName))
264 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000265
Bill Wendling5f689e72011-11-04 18:48:00 +0000266 NameAndAttributes info;
267 StringMap<NameAndAttributes>::value_type &entry =
268 _undefines.GetOrCreateValue(targetclassName);
269 if (entry.getValue().name)
270 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000271
Bill Wendling5f689e72011-11-04 18:48:00 +0000272 const char *symbolName = entry.getKey().data();
273 info.name = symbolName;
274 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000275 info.isFunction = false;
276 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000277 entry.setValue(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000278}
279
Bill Wendlingfb440502012-03-28 20:46:54 +0000280/// addDefinedDataSymbol - Add a data symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000281void LTOModule::addDefinedDataSymbol(const GlobalValue *v) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000282 // Add to list of defined symbols.
Bill Wendlinga2af6742011-11-04 09:30:19 +0000283 addDefinedSymbol(v, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000284
Bill Wendling45f74e32012-08-06 22:52:45 +0000285 if (!v->hasSection() /* || !isTargetDarwin */)
286 return;
287
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000288 // Special case i386/ppc ObjC data structures in magic sections:
289 // The issue is that the old ObjC object format did some strange
290 // contortions to avoid real linker symbols. For instance, the
291 // ObjC class data structure is allocated statically in the executable
292 // that defines that class. That data structures contains a pointer to
293 // its superclass. But instead of just initializing that part of the
294 // struct to the address of its superclass, and letting the static and
295 // dynamic linkers do the rest, the runtime works by having that field
296 // instead point to a C-string that is the name of the superclass.
297 // At runtime the objc initialization updates that pointer and sets
298 // it to point to the actual super class. As far as the linker
299 // knows it is just a pointer to a string. But then someone wanted the
300 // linker to issue errors at build time if the superclass was not found.
301 // So they figured out a way in mach-o object format to use an absolute
302 // symbols (.objc_class_name_Foo = 0) and a floating reference
303 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
304 // a class was missing.
305 // The following synthesizes the implicit .objc_* symbols for the linker
306 // from the ObjC data structures generated by the front end.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000307
Bill Wendling45f74e32012-08-06 22:52:45 +0000308 // special case if this data blob is an ObjC class definition
309 if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000310 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000311 addObjCClass(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000312 }
Bill Wendling45f74e32012-08-06 22:52:45 +0000313 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000314
Bill Wendling45f74e32012-08-06 22:52:45 +0000315 // special case if this data blob is an ObjC category definition
316 else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000317 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000318 addObjCCategory(gv);
319 }
320 }
321
322 // special case if this data blob is the list of referenced classes
323 else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000324 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000325 addObjCClassRef(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000326 }
327 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000328}
329
Bill Wendlingfb440502012-03-28 20:46:54 +0000330/// addDefinedFunctionSymbol - Add a function symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000331void LTOModule::addDefinedFunctionSymbol(const Function *f) {
Bill Wendlingfb440502012-03-28 20:46:54 +0000332 // add to list of defined symbols
333 addDefinedSymbol(f, true);
334}
335
336/// addDefinedSymbol - Add a defined symbol to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000337void LTOModule::addDefinedSymbol(const GlobalValue *def, bool isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000338 // ignore all llvm.* symbols
339 if (def->getName().startswith("llvm."))
340 return;
341
342 // string is owned by _defines
Rafael Espindola34b59382011-02-11 05:23:09 +0000343 SmallString<64> Buffer;
Bill Wendlinga2af6742011-11-04 09:30:19 +0000344 _mangler.getNameWithPrefix(Buffer, def, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000345
346 // set alignment part log2() can have rounding errors
347 uint32_t align = def->getAlignment();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000348 uint32_t attr = align ? countTrailingZeros(def->getAlignment()) : 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000349
350 // set permissions part
Bill Wendling9ee2d332012-03-29 08:27:32 +0000351 if (isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000352 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000353 } else {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000354 const GlobalVariable *gv = dyn_cast<GlobalVariable>(def);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000355 if (gv && gv->isConstant())
356 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
357 else
358 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
359 }
360
361 // set definition part
Bill Wendling2776d462010-09-27 18:05:19 +0000362 if (def->hasWeakLinkage() || def->hasLinkOnceLinkage() ||
Bill Wendling34bc34e2012-08-17 18:33:14 +0000363 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000364 attr |= LTO_SYMBOL_DEFINITION_WEAK;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000365 else if (def->hasCommonLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000366 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000367 else
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000368 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000369
370 // set scope part
371 if (def->hasHiddenVisibility())
372 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
373 else if (def->hasProtectedVisibility())
374 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000375 else if (def->hasExternalLinkage() || def->hasWeakLinkage() ||
376 def->hasLinkOnceLinkage() || def->hasCommonLinkage() ||
377 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000378 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
Bill Wendling34bc34e2012-08-17 18:33:14 +0000379 else if (def->hasLinkOnceODRAutoHideLinkage())
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000380 attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000381 else
382 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
383
Chad Rosier772a91f2011-06-28 18:26:12 +0000384 StringSet::value_type &entry = _defines.GetOrCreateValue(Buffer);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000385 entry.setValue(1);
386
Bill Wendling9ee2d332012-03-29 08:27:32 +0000387 // fill information structure
388 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000389 StringRef Name = entry.getKey();
390 info.name = Name.data();
391 assert(info.name[Name.size()] == '\0');
Bill Wendling9ee2d332012-03-29 08:27:32 +0000392 info.attributes = attr;
393 info.isFunction = isFunction;
394 info.symbol = def;
395
396 // add to table of symbols
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000397 _symbols.push_back(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000398}
399
Bill Wendlingfb440502012-03-28 20:46:54 +0000400/// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
401/// defined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000402void LTOModule::addAsmGlobalSymbol(const char *name,
403 lto_symbol_attributes scope) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000404 StringSet::value_type &entry = _defines.GetOrCreateValue(name);
405
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000406 // only add new define if not already defined
Rafael Espindola477d11f2011-02-20 16:27:25 +0000407 if (entry.getValue())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000408 return;
409
Rafael Espindola477d11f2011-02-20 16:27:25 +0000410 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000411
412 NameAndAttributes &info = _undefines[entry.getKey().data()];
413
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000414 if (info.symbol == 0) {
Bill Wendling71b19bb2012-04-02 10:01:21 +0000415 // FIXME: This is trying to take care of module ASM like this:
416 //
417 // module asm ".zerofill __FOO, __foo, _bar_baz_qux, 0"
418 //
419 // but is gross and its mother dresses it funny. Have the ASM parser give us
420 // more details for this type of situation so that we're not guessing so
421 // much.
422
423 // fill information structure
Rafael Espindola5f4b32f2012-05-11 03:42:13 +0000424 info.name = entry.getKey().data();
Bill Wendling71b19bb2012-04-02 10:01:21 +0000425 info.attributes =
426 LTO_SYMBOL_PERMISSIONS_DATA | LTO_SYMBOL_DEFINITION_REGULAR | scope;
427 info.isFunction = false;
428 info.symbol = 0;
429
430 // add to table of symbols
431 _symbols.push_back(info);
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000432 return;
433 }
434
Bill Wendling9ee2d332012-03-29 08:27:32 +0000435 if (info.isFunction)
436 addDefinedFunctionSymbol(cast<Function>(info.symbol));
437 else
438 addDefinedDataSymbol(info.symbol);
Bill Wendling8f6c8a92012-03-30 23:26:06 +0000439
440 _symbols.back().attributes &= ~LTO_SYMBOL_SCOPE_MASK;
441 _symbols.back().attributes |= scope;
Devang Patela59fe952008-07-16 18:06:52 +0000442}
Nick Kledzik07b4a622008-02-26 20:26:43 +0000443
Bill Wendlingfb440502012-03-28 20:46:54 +0000444/// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the
445/// undefined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000446void LTOModule::addAsmGlobalSymbolUndef(const char *name) {
447 StringMap<NameAndAttributes>::value_type &entry =
448 _undefines.GetOrCreateValue(name);
449
450 _asm_undefines.push_back(entry.getKey().data());
451
452 // we already have the symbol
453 if (entry.getValue().name)
454 return;
455
456 uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;;
457 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
458 NameAndAttributes info;
459 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000460 info.attributes = attr;
461 info.isFunction = false;
462 info.symbol = 0;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000463
464 entry.setValue(info);
465}
466
Bill Wendlingfb440502012-03-28 20:46:54 +0000467/// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet to a
468/// list to be resolved later.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000469void
470LTOModule::addPotentialUndefinedSymbol(const GlobalValue *decl, bool isFunc) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000471 // ignore all llvm.* symbols
472 if (decl->getName().startswith("llvm."))
473 return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000474
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000475 // ignore all aliases
476 if (isa<GlobalAlias>(decl))
477 return;
Nick Lewycky0661b932009-07-09 06:03:04 +0000478
Rafael Espindola34b59382011-02-11 05:23:09 +0000479 SmallString<64> name;
Bill Wendlinga2af6742011-11-04 09:30:19 +0000480 _mangler.getNameWithPrefix(name, decl, false);
Rafael Espindola56548522009-04-24 16:55:21 +0000481
Rafael Espindola477d11f2011-02-20 16:27:25 +0000482 StringMap<NameAndAttributes>::value_type &entry =
Chad Rosier772a91f2011-06-28 18:26:12 +0000483 _undefines.GetOrCreateValue(name);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000484
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000485 // we already have the symbol
Rafael Espindola477d11f2011-02-20 16:27:25 +0000486 if (entry.getValue().name)
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000487 return;
Rafael Espindola56548522009-04-24 16:55:21 +0000488
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000489 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000490
491 info.name = entry.getKey().data();
Bill Wendlingfb440502012-03-28 20:46:54 +0000492
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000493 if (decl->hasExternalWeakLinkage())
494 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
495 else
496 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000497
Bill Wendling9ee2d332012-03-29 08:27:32 +0000498 info.isFunction = isFunc;
499 info.symbol = decl;
500
Rafael Espindola477d11f2011-02-20 16:27:25 +0000501 entry.setValue(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000502}
503
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000504namespace {
505 class RecordStreamer : public MCStreamer {
506 public:
Bill Wendling32867652012-04-03 03:56:52 +0000507 enum State { NeverSeen, Global, Defined, DefinedGlobal, Used };
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000508
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000509 private:
510 StringMap<State> Symbols;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000511
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000512 void markDefined(const MCSymbol &Symbol) {
513 State &S = Symbols[Symbol.getName()];
514 switch (S) {
515 case DefinedGlobal:
516 case Global:
517 S = DefinedGlobal;
518 break;
519 case NeverSeen:
520 case Defined:
521 case Used:
522 S = Defined;
523 break;
524 }
525 }
526 void markGlobal(const MCSymbol &Symbol) {
527 State &S = Symbols[Symbol.getName()];
528 switch (S) {
529 case DefinedGlobal:
530 case Defined:
531 S = DefinedGlobal;
532 break;
533
534 case NeverSeen:
535 case Global:
536 case Used:
537 S = Global;
538 break;
539 }
540 }
541 void markUsed(const MCSymbol &Symbol) {
542 State &S = Symbols[Symbol.getName()];
543 switch (S) {
544 case DefinedGlobal:
545 case Defined:
546 case Global:
547 break;
548
549 case NeverSeen:
550 case Used:
551 S = Used;
552 break;
553 }
554 }
555
556 // FIXME: mostly copied for the obj streamer.
557 void AddValueSymbols(const MCExpr *Value) {
558 switch (Value->getKind()) {
559 case MCExpr::Target:
560 // FIXME: What should we do in here?
561 break;
562
563 case MCExpr::Constant:
564 break;
565
566 case MCExpr::Binary: {
567 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
568 AddValueSymbols(BE->getLHS());
569 AddValueSymbols(BE->getRHS());
570 break;
571 }
572
573 case MCExpr::SymbolRef:
574 markUsed(cast<MCSymbolRefExpr>(Value)->getSymbol());
575 break;
576
577 case MCExpr::Unary:
578 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
579 break;
580 }
581 }
582
583 public:
584 typedef StringMap<State>::const_iterator const_iterator;
585
586 const_iterator begin() {
587 return Symbols.begin();
588 }
589
590 const_iterator end() {
591 return Symbols.end();
592 }
593
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000594 RecordStreamer(MCContext &Context) : MCStreamer(Context, 0) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000595
Bill Wendling32867652012-04-03 03:56:52 +0000596 virtual void EmitInstruction(const MCInst &Inst) {
597 // Scan for values.
598 for (unsigned i = Inst.getNumOperands(); i--; )
599 if (Inst.getOperand(i).isExpr())
600 AddValueSymbols(Inst.getOperand(i).getExpr());
601 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000602 virtual void EmitLabel(MCSymbol *Symbol) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000603 Symbol->setSection(*getCurrentSection().first);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000604 markDefined(*Symbol);
605 }
Reed Kotleraee4d5d12012-12-16 04:00:45 +0000606 virtual void EmitDebugLabel(MCSymbol *Symbol) {
607 EmitLabel(Symbol);
608 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000609 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
610 // FIXME: should we handle aliases?
611 markDefined(*Symbol);
612 }
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000613 virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000614 if (Attribute == MCSA_Global)
615 markGlobal(*Symbol);
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000616 return true;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000617 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000618 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Cheng95847992012-06-22 20:30:39 +0000619 uint64_t Size , unsigned ByteAlignment) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000620 markDefined(*Symbol);
621 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000622 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
623 unsigned ByteAlignment) {
624 markDefined(*Symbol);
625 }
Bill Wendling32867652012-04-03 03:56:52 +0000626
Eli Benderskyf483ff92012-12-20 19:05:53 +0000627 virtual void EmitBundleAlignMode(unsigned AlignPow2) {}
Eli Bendersky802b6282013-01-07 21:51:08 +0000628 virtual void EmitBundleLock(bool AlignToEnd) {}
Eli Benderskyf483ff92012-12-20 19:05:53 +0000629 virtual void EmitBundleUnlock() {}
630
Bill Wendling32867652012-04-03 03:56:52 +0000631 // Noop calls.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000632 virtual void ChangeSection(const MCSection *Section,
633 const MCExpr *Subsection) {}
Eli Benderskycbb25142013-01-14 19:04:57 +0000634 virtual void InitToTextSection() {}
Bill Wendling32867652012-04-03 03:56:52 +0000635 virtual void InitSections() {}
636 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {}
637 virtual void EmitThumbFunc(MCSymbol *Func) {}
638 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {}
639 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {}
640 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {}
641 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {}
642 virtual void EmitCOFFSymbolType(int Type) {}
643 virtual void EndCOFFSymbolDef() {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000644 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {}
Benjamin Kramer63970512011-09-01 23:04:27 +0000645 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
646 unsigned ByteAlignment) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000647 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
648 uint64_t Size, unsigned ByteAlignment) {}
Rafael Espindola64e1af82013-07-02 15:49:13 +0000649 virtual void EmitBytes(StringRef Data) {}
650 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {}
Rafael Espindola6aea5922011-04-21 23:39:26 +0000651 virtual void EmitULEB128Value(const MCExpr *Value) {}
652 virtual void EmitSLEB128Value(const MCExpr *Value) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000653 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
654 unsigned ValueSize,
655 unsigned MaxBytesToEmit) {}
656 virtual void EmitCodeAlignment(unsigned ByteAlignment,
657 unsigned MaxBytesToEmit) {}
Jim Grosbachb5912772012-01-27 00:37:08 +0000658 virtual bool EmitValueToOffset(const MCExpr *Offset,
659 unsigned char Value ) { return false; }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000660 virtual void EmitFileDirective(StringRef Filename) {}
661 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
662 const MCSymbol *LastLabel,
Evan Chengc7ac6902011-07-14 05:43:07 +0000663 const MCSymbol *Label,
664 unsigned PointerSize) {}
Rafael Espindola07082092012-01-07 03:13:18 +0000665 virtual void FinishImpl() {}
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000666 virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
667 RecordProcEnd(Frame);
668 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000669 };
Bill Wendling9ee2d332012-03-29 08:27:32 +0000670} // end anonymous namespace
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000671
Bill Wendlingfb440502012-03-28 20:46:54 +0000672/// addAsmGlobalSymbols - Add global symbols from module-level ASM to the
673/// defined or undefined lists.
Bill Wendlingac2abde2011-11-04 09:24:40 +0000674bool LTOModule::addAsmGlobalSymbols(std::string &errMsg) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000675 const std::string &inlineAsm = _module->getModuleInlineAsm();
Ivan Krasincc2a8012011-09-08 07:38:25 +0000676 if (inlineAsm.empty())
677 return false;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000678
Bill Wendlingac2abde2011-11-04 09:24:40 +0000679 OwningPtr<RecordStreamer> Streamer(new RecordStreamer(_context));
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000680 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(inlineAsm);
681 SourceMgr SrcMgr;
682 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach345768c2011-08-16 18:33:49 +0000683 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr,
Bill Wendlingac2abde2011-11-04 09:24:40 +0000684 _context, *Streamer,
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000685 *_target->getMCAsmInfo()));
Bill Wendling9351b3e2012-08-08 22:01:55 +0000686 const Target &T = _target->getTarget();
Joey Goulydb6144e2013-09-12 12:55:29 +0000687 OwningPtr<MCInstrInfo> MCII(T.createMCInstrInfo());
Bill Wendling9351b3e2012-08-08 22:01:55 +0000688 OwningPtr<MCSubtargetInfo>
689 STI(T.createMCSubtargetInfo(_target->getTargetTriple(),
690 _target->getTargetCPU(),
691 _target->getTargetFeatureString()));
Joey Goulydb6144e2013-09-12 12:55:29 +0000692 OwningPtr<MCTargetAsmParser> TAP(T.createMCAsmParser(*STI, *Parser.get(), *MCII));
Ivan Krasin8149dd62011-09-08 07:36:39 +0000693 if (!TAP) {
Bill Wendling9351b3e2012-08-08 22:01:55 +0000694 errMsg = "target " + std::string(T.getName()) +
695 " does not define AsmParser.";
Ivan Krasin8149dd62011-09-08 07:36:39 +0000696 return true;
697 }
698
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000699 Parser->setTargetParser(*TAP);
Bill Wendling9351b3e2012-08-08 22:01:55 +0000700 if (Parser->Run(false))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000701 return true;
702
703 for (RecordStreamer::const_iterator i = Streamer->begin(),
704 e = Streamer->end(); i != e; ++i) {
705 StringRef Key = i->first();
706 RecordStreamer::State Value = i->second;
707 if (Value == RecordStreamer::DefinedGlobal)
708 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_DEFAULT);
709 else if (Value == RecordStreamer::Defined)
710 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_INTERNAL);
711 else if (Value == RecordStreamer::Global ||
712 Value == RecordStreamer::Used)
713 addAsmGlobalSymbolUndef(Key.data());
714 }
Bill Wendling9351b3e2012-08-08 22:01:55 +0000715
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000716 return false;
717}
718
Bill Wendlingfb440502012-03-28 20:46:54 +0000719/// isDeclaration - Return 'true' if the global value is a declaration.
Rafael Espindola5b778b22011-03-18 19:51:00 +0000720static bool isDeclaration(const GlobalValue &V) {
721 if (V.hasAvailableExternallyLinkage())
722 return true;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000723
Rafael Espindola5b778b22011-03-18 19:51:00 +0000724 if (V.isMaterializable())
725 return false;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000726
Rafael Espindola5b778b22011-03-18 19:51:00 +0000727 return V.isDeclaration();
728}
729
Bill Wendling7e58b382012-03-28 23:12:18 +0000730/// parseSymbols - Parse the symbols from the module and model-level ASM and add
Bill Wendlingfb440502012-03-28 20:46:54 +0000731/// them to either the defined or undefined lists.
Bill Wendling7e58b382012-03-28 23:12:18 +0000732bool LTOModule::parseSymbols(std::string &errMsg) {
Daniel Dunbar919660b2010-08-10 23:46:46 +0000733 // add functions
Bill Wendling763acfc2012-03-29 03:34:57 +0000734 for (Module::iterator f = _module->begin(), e = _module->end(); f != e; ++f) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000735 if (isDeclaration(*f))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000736 addPotentialUndefinedSymbol(f, true);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000737 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000738 addDefinedFunctionSymbol(f);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000739 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000740
Daniel Dunbar919660b2010-08-10 23:46:46 +0000741 // add data
742 for (Module::global_iterator v = _module->global_begin(),
743 e = _module->global_end(); v != e; ++v) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000744 if (isDeclaration(*v))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000745 addPotentialUndefinedSymbol(v, false);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000746 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000747 addDefinedDataSymbol(v);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000748 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000749
Daniel Dunbar919660b2010-08-10 23:46:46 +0000750 // add asm globals
Bill Wendlingac2abde2011-11-04 09:24:40 +0000751 if (addAsmGlobalSymbols(errMsg))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000752 return true;
Daniel Dunbar919660b2010-08-10 23:46:46 +0000753
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000754 // add aliases
Bill Wendling9ee2d332012-03-29 08:27:32 +0000755 for (Module::alias_iterator a = _module->alias_begin(),
756 e = _module->alias_end(); a != e; ++a) {
757 if (isDeclaration(*a->getAliasedGlobal()))
Bill Wendlingd58ed732012-03-28 20:48:49 +0000758 // Is an alias to a declaration.
Bill Wendling9ee2d332012-03-29 08:27:32 +0000759 addPotentialUndefinedSymbol(a, false);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000760 else
Bill Wendling9ee2d332012-03-29 08:27:32 +0000761 addDefinedDataSymbol(a);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000762 }
763
Daniel Dunbar919660b2010-08-10 23:46:46 +0000764 // make symbols for all undefines
Bill Wendling9ee2d332012-03-29 08:27:32 +0000765 for (StringMap<NameAndAttributes>::iterator u =_undefines.begin(),
766 e = _undefines.end(); u != e; ++u) {
767 // If this symbol also has a definition, then don't make an undefine because
768 // it is a tentative definition.
769 if (_defines.count(u->getKey())) continue;
770 NameAndAttributes info = u->getValue();
771 _symbols.push_back(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000772 }
Bill Wendling9ee2d332012-03-29 08:27:32 +0000773
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000774 return false;
Nick Kledzik91a6dcf2008-02-27 22:25:36 +0000775}