blob: f73a608e3c4b73d312b8ac2527cfe641b2e9a744 [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"
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +000021#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Module.h"
Rafael Espindola1e49a6d2011-03-02 04:14:42 +000023#include "llvm/MC/MCExpr.h"
24#include "llvm/MC/MCInst.h"
Joey Goulydb6144e2013-09-12 12:55:29 +000025#include "llvm/MC/MCInstrInfo.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000026#include "llvm/MC/MCParser/MCAsmParser.h"
Rafael Espindola1e49a6d2011-03-02 04:14:42 +000027#include "llvm/MC/MCStreamer.h"
Evan Cheng91111d22011-07-09 05:47:46 +000028#include "llvm/MC/MCSubtargetInfo.h"
Rafael Espindola1e49a6d2011-03-02 04:14:42 +000029#include "llvm/MC/MCSymbol.h"
Evan Cheng11424442011-07-26 00:24:13 +000030#include "llvm/MC/MCTargetAsmParser.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000031#include "llvm/MC/SubtargetFeature.h"
Bill Wendlingb8dcda72012-08-06 21:34:54 +000032#include "llvm/Support/CommandLine.h"
Rafael Espindola46ed3532013-06-11 18:05:26 +000033#include "llvm/Support/FileSystem.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000034#include "llvm/Support/Host.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000035#include "llvm/Support/MemoryBuffer.h"
36#include "llvm/Support/Path.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000037#include "llvm/Support/SourceMgr.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000038#include "llvm/Support/TargetRegistry.h"
39#include "llvm/Support/TargetSelect.h"
40#include "llvm/Support/system_error.h"
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +000041#include "llvm/Target/TargetLowering.h"
42#include "llvm/Target/TargetLoweringObjectFile.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000043#include "llvm/Target/TargetRegisterInfo.h"
Rafael Espindola282a4702013-10-31 20:51:58 +000044#include "llvm/Transforms/Utils/GlobalStatus.h"
Nick Kledzik07b4a622008-02-26 20:26:43 +000045using namespace llvm;
46
Bill Wendlingfb440502012-03-28 20:46:54 +000047LTOModule::LTOModule(llvm::Module *m, llvm::TargetMachine *t)
48 : _module(m), _target(t),
Rafael Espindolae28610d2013-12-09 20:26:40 +000049 _context(_target->getMCAsmInfo(), _target->getRegisterInfo(), &ObjFileInfo),
Rafael Espindola58873562014-01-03 19:21:54 +000050 _mangler(t->getDataLayout()) {
Rafael Espindolae28610d2013-12-09 20:26:40 +000051 ObjFileInfo.InitMCObjectFileInfo(t->getTargetTriple(),
52 t->getRelocationModel(), t->getCodeModel(),
53 _context);
54}
Bill Wendlingfb440502012-03-28 20:46:54 +000055
56/// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM
57/// bitcode.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000058bool LTOModule::isBitcodeFile(const void *mem, size_t length) {
Rafael Espindola46ed3532013-06-11 18:05:26 +000059 return sys::fs::identify_magic(StringRef((const char *)mem, length)) ==
60 sys::fs::file_magic::bitcode;
Nick Kledzik07b4a622008-02-26 20:26:43 +000061}
62
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000063bool LTOModule::isBitcodeFile(const char *path) {
Rafael Espindola71affba2013-06-12 15:13:57 +000064 sys::fs::file_magic type;
65 if (sys::fs::identify_magic(path, type))
66 return false;
67 return type == sys::fs::file_magic::bitcode;
Nick Kledzik07b4a622008-02-26 20:26:43 +000068}
69
Bill Wendlingfb440502012-03-28 20:46:54 +000070/// isBitcodeFileForTarget - Returns 'true' if the file (or memory contents) is
71/// LLVM bitcode for the specified triple.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000072bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length,
73 const char *triplePrefix) {
74 MemoryBuffer *buffer = makeBuffer(mem, length);
75 if (!buffer)
Nick Kledzikb481c202009-06-01 20:33:09 +000076 return false;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000077 return isTargetMatch(buffer, triplePrefix);
Nick Kledzikb481c202009-06-01 20:33:09 +000078}
79
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000080bool LTOModule::isBitcodeFileForTarget(const char *path,
81 const char *triplePrefix) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +000082 OwningPtr<MemoryBuffer> buffer;
83 if (MemoryBuffer::getFile(path, buffer))
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000084 return false;
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +000085 return isTargetMatch(buffer.take(), triplePrefix);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000086}
87
Bill Wendlingfb440502012-03-28 20:46:54 +000088/// isTargetMatch - Returns 'true' if the memory buffer is for the specified
89/// target triple.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000090bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) {
Bill Wendling0198ce02010-10-06 01:22:42 +000091 std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
92 delete buffer;
Bill Wendling5f689e72011-11-04 18:48:00 +000093 return strncmp(Triple.c_str(), triplePrefix, strlen(triplePrefix)) == 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000094}
95
Bill Wendlingfb440502012-03-28 20:46:54 +000096/// makeLTOModule - Create an LTOModule. N.B. These methods take ownership of
97/// the buffer.
Rafael Espindola0b385c72013-09-30 16:39:19 +000098LTOModule *LTOModule::makeLTOModule(const char *path, TargetOptions options,
99 std::string &errMsg) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000100 OwningPtr<MemoryBuffer> buffer;
101 if (error_code ec = MemoryBuffer::getFile(path, buffer)) {
Michael J. Spencerd4227232010-12-09 18:06:07 +0000102 errMsg = ec.message();
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000103 return NULL;
Michael J. Spencerd4227232010-12-09 18:06:07 +0000104 }
Rafael Espindola0b385c72013-09-30 16:39:19 +0000105 return makeLTOModule(buffer.take(), options, errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000106}
107
Rafael Espindola56e41f72011-02-08 22:40:47 +0000108LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000109 size_t size, TargetOptions options,
110 std::string &errMsg) {
111 return makeLTOModule(fd, path, size, 0, options, errMsg);
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000112}
113
114LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000115 size_t map_size,
116 off_t offset,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000117 TargetOptions options,
Rafael Espindola56e41f72011-02-08 22:40:47 +0000118 std::string &errMsg) {
119 OwningPtr<MemoryBuffer> buffer;
Rafael Espindola3d2ac2e2013-07-23 20:25:01 +0000120 if (error_code ec =
121 MemoryBuffer::getOpenFileSlice(fd, path, buffer, map_size, offset)) {
Rafael Espindola56e41f72011-02-08 22:40:47 +0000122 errMsg = ec.message();
123 return NULL;
124 }
Rafael Espindola0b385c72013-09-30 16:39:19 +0000125 return makeLTOModule(buffer.take(), options, errMsg);
Rafael Espindola56e41f72011-02-08 22:40:47 +0000126}
127
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000128LTOModule *LTOModule::makeLTOModule(const void *mem, size_t length,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000129 TargetOptions options,
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000130 std::string &errMsg) {
131 OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length));
132 if (!buffer)
133 return NULL;
Rafael Espindola0b385c72013-09-30 16:39:19 +0000134 return makeLTOModule(buffer.take(), options, errMsg);
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000135}
136
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000137LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000138 TargetOptions options,
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000139 std::string &errMsg) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000140 // parse bitcode buffer
Rafael Espindola5b6c1e82014-01-13 18:31:04 +0000141 ErrorOr<Module *> ModuleOrErr =
142 getLazyBitcodeModule(buffer, getGlobalContext());
143 if (error_code EC = ModuleOrErr.getError()) {
144 errMsg = EC.message();
Rafael Espindola5b778b22011-03-18 19:51:00 +0000145 delete buffer;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000146 return NULL;
Rafael Espindola5b778b22011-03-18 19:51:00 +0000147 }
Rafael Espindola5b6c1e82014-01-13 18:31:04 +0000148 OwningPtr<Module> m(ModuleOrErr.get());
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000149
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000150 std::string TripleStr = m->getTargetTriple();
151 if (TripleStr.empty())
152 TripleStr = sys::getDefaultTargetTriple();
153 llvm::Triple Triple(TripleStr);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000154
155 // find machine architecture for this module
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000156 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000157 if (!march)
158 return NULL;
159
Nick Lewycky364c04a2011-04-21 01:54:08 +0000160 // construct LTOModule, hand over ownership of module and target
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000161 SubtargetFeatures Features;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000162 Features.getDefaultSubtargetFeatures(Triple);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000163 std::string FeatureStr = Features.getString();
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000164 // Set a default CPU for Darwin triples.
Evan Chengfe6e4052011-06-30 01:53:36 +0000165 std::string CPU;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000166 if (Triple.isOSDarwin()) {
167 if (Triple.getArch() == llvm::Triple::x86_64)
168 CPU = "core2";
169 else if (Triple.getArch() == llvm::Triple::x86)
170 CPU = "yonah";
171 }
Rafael Espindola0b385c72013-09-30 16:39:19 +0000172
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000173 TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000174 options);
Rafael Espindolae9fab9b2014-01-14 23:51:27 +0000175 m->materializeAllPermanently();
Rafael Espindola282a4702013-10-31 20:51:58 +0000176
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000177 LTOModule *Ret = new LTOModule(m.take(), target);
Bill Wendling7e58b382012-03-28 23:12:18 +0000178 if (Ret->parseSymbols(errMsg)) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000179 delete Ret;
180 return NULL;
181 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000182
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000183 Ret->parseMetadata();
184
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000185 return Ret;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000186}
187
Bill Wendlingfb440502012-03-28 20:46:54 +0000188/// makeBuffer - Create a MemoryBuffer from a memory range.
189MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length) {
Roman Divackyad06cee2012-09-05 22:26:57 +0000190 const char *startPtr = (const char*)mem;
Bill Wendlingfb440502012-03-28 20:46:54 +0000191 return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), "", false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000192}
193
Bill Wendlingfb440502012-03-28 20:46:54 +0000194/// objcClassNameFromExpression - Get string that the data pointer points to.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000195bool
196LTOModule::objcClassNameFromExpression(const Constant *c, std::string &name) {
197 if (const ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000198 Constant *op = ce->getOperand(0);
199 if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) {
200 Constant *cn = gvn->getInitializer();
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000201 if (ConstantDataArray *ca = dyn_cast<ConstantDataArray>(cn)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000202 if (ca->isCString()) {
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000203 name = ".objc_class_name_" + ca->getAsCString().str();
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000204 return true;
Nick Kledzikb481c202009-06-01 20:33:09 +0000205 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000206 }
Nick Kledzikb481c202009-06-01 20:33:09 +0000207 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000208 }
209 return false;
210}
211
Bill Wendlingfb440502012-03-28 20:46:54 +0000212/// addObjCClass - Parse i386/ppc ObjC class data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000213void LTOModule::addObjCClass(const GlobalVariable *clgv) {
214 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000215 if (!c) return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000216
Bill Wendling5f689e72011-11-04 18:48:00 +0000217 // second slot in __OBJC,__class is pointer to superclass name
218 std::string superclassName;
219 if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
220 NameAndAttributes info;
221 StringMap<NameAndAttributes>::value_type &entry =
222 _undefines.GetOrCreateValue(superclassName);
223 if (!entry.getValue().name) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000224 const char *symbolName = entry.getKey().data();
225 info.name = symbolName;
226 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000227 info.isFunction = false;
228 info.symbol = clgv;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000229 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000230 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000231 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000232
233 // third slot in __OBJC,__class is pointer to class name
234 std::string className;
235 if (objcClassNameFromExpression(c->getOperand(2), className)) {
236 StringSet::value_type &entry = _defines.GetOrCreateValue(className);
237 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000238
Bill Wendling5f689e72011-11-04 18:48:00 +0000239 NameAndAttributes info;
240 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000241 info.attributes = LTO_SYMBOL_PERMISSIONS_DATA |
242 LTO_SYMBOL_DEFINITION_REGULAR | LTO_SYMBOL_SCOPE_DEFAULT;
243 info.isFunction = false;
244 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000245 _symbols.push_back(info);
246 }
247}
248
Bill Wendlingfb440502012-03-28 20:46:54 +0000249/// addObjCCategory - Parse i386/ppc ObjC category data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000250void LTOModule::addObjCCategory(const GlobalVariable *clgv) {
251 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000252 if (!c) return;
253
254 // second slot in __OBJC,__category is pointer to target class name
255 std::string targetclassName;
256 if (!objcClassNameFromExpression(c->getOperand(1), targetclassName))
257 return;
258
259 NameAndAttributes info;
260 StringMap<NameAndAttributes>::value_type &entry =
261 _undefines.GetOrCreateValue(targetclassName);
262
263 if (entry.getValue().name)
264 return;
265
266 const char *symbolName = entry.getKey().data();
267 info.name = symbolName;
268 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000269 info.isFunction = false;
270 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000271 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000272}
273
Bill Wendlingfb440502012-03-28 20:46:54 +0000274/// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000275void LTOModule::addObjCClassRef(const GlobalVariable *clgv) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000276 std::string targetclassName;
Bill Wendling5f689e72011-11-04 18:48:00 +0000277 if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName))
278 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000279
Bill Wendling5f689e72011-11-04 18:48:00 +0000280 NameAndAttributes info;
281 StringMap<NameAndAttributes>::value_type &entry =
282 _undefines.GetOrCreateValue(targetclassName);
283 if (entry.getValue().name)
284 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000285
Bill Wendling5f689e72011-11-04 18:48:00 +0000286 const char *symbolName = entry.getKey().data();
287 info.name = symbolName;
288 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000289 info.isFunction = false;
290 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000291 entry.setValue(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000292}
293
Bill Wendlingfb440502012-03-28 20:46:54 +0000294/// addDefinedDataSymbol - Add a data symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000295void LTOModule::addDefinedDataSymbol(const GlobalValue *v) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000296 // Add to list of defined symbols.
Bill Wendlinga2af6742011-11-04 09:30:19 +0000297 addDefinedSymbol(v, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000298
Bill Wendling45f74e32012-08-06 22:52:45 +0000299 if (!v->hasSection() /* || !isTargetDarwin */)
300 return;
301
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000302 // Special case i386/ppc ObjC data structures in magic sections:
303 // The issue is that the old ObjC object format did some strange
304 // contortions to avoid real linker symbols. For instance, the
305 // ObjC class data structure is allocated statically in the executable
306 // that defines that class. That data structures contains a pointer to
307 // its superclass. But instead of just initializing that part of the
308 // struct to the address of its superclass, and letting the static and
309 // dynamic linkers do the rest, the runtime works by having that field
310 // instead point to a C-string that is the name of the superclass.
311 // At runtime the objc initialization updates that pointer and sets
312 // it to point to the actual super class. As far as the linker
313 // knows it is just a pointer to a string. But then someone wanted the
314 // linker to issue errors at build time if the superclass was not found.
315 // So they figured out a way in mach-o object format to use an absolute
316 // symbols (.objc_class_name_Foo = 0) and a floating reference
317 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
318 // a class was missing.
319 // The following synthesizes the implicit .objc_* symbols for the linker
320 // from the ObjC data structures generated by the front end.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000321
Bill Wendling45f74e32012-08-06 22:52:45 +0000322 // special case if this data blob is an ObjC class definition
323 if (v->getSection().compare(0, 15, "__OBJC,__class,") == 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 addObjCClass(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000326 }
Bill Wendling45f74e32012-08-06 22:52:45 +0000327 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000328
Bill Wendling45f74e32012-08-06 22:52:45 +0000329 // special case if this data blob is an ObjC category definition
330 else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000331 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000332 addObjCCategory(gv);
333 }
334 }
335
336 // special case if this data blob is the list of referenced classes
337 else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000338 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000339 addObjCClassRef(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000340 }
341 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000342}
343
Bill Wendlingfb440502012-03-28 20:46:54 +0000344/// addDefinedFunctionSymbol - Add a function symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000345void LTOModule::addDefinedFunctionSymbol(const Function *f) {
Bill Wendlingfb440502012-03-28 20:46:54 +0000346 // add to list of defined symbols
347 addDefinedSymbol(f, true);
348}
349
Rafael Espindola282a4702013-10-31 20:51:58 +0000350static bool canBeHidden(const GlobalValue *GV) {
351 GlobalValue::LinkageTypes L = GV->getLinkage();
352
Rafael Espindola282a4702013-10-31 20:51:58 +0000353 if (L != GlobalValue::LinkOnceODRLinkage)
354 return false;
355
356 if (GV->hasUnnamedAddr())
357 return true;
358
359 GlobalStatus GS;
360 if (GlobalStatus::analyzeGlobal(GV, GS))
361 return false;
362
363 return !GS.IsCompared;
364}
365
Bill Wendlingfb440502012-03-28 20:46:54 +0000366/// addDefinedSymbol - Add a defined symbol to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000367void LTOModule::addDefinedSymbol(const GlobalValue *def, bool isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000368 // ignore all llvm.* symbols
369 if (def->getName().startswith("llvm."))
370 return;
371
372 // string is owned by _defines
Rafael Espindola34b59382011-02-11 05:23:09 +0000373 SmallString<64> Buffer;
Rafael Espindola117b20c2013-12-05 05:53:12 +0000374 _mangler.getNameWithPrefix(Buffer, def);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000375
376 // set alignment part log2() can have rounding errors
377 uint32_t align = def->getAlignment();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000378 uint32_t attr = align ? countTrailingZeros(def->getAlignment()) : 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000379
380 // set permissions part
Bill Wendling9ee2d332012-03-29 08:27:32 +0000381 if (isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000382 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000383 } else {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000384 const GlobalVariable *gv = dyn_cast<GlobalVariable>(def);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000385 if (gv && gv->isConstant())
386 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
387 else
388 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
389 }
390
391 // set definition part
Bill Wendling2776d462010-09-27 18:05:19 +0000392 if (def->hasWeakLinkage() || def->hasLinkOnceLinkage() ||
Bill Wendling34bc34e2012-08-17 18:33:14 +0000393 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000394 attr |= LTO_SYMBOL_DEFINITION_WEAK;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000395 else if (def->hasCommonLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000396 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000397 else
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000398 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000399
400 // set scope part
401 if (def->hasHiddenVisibility())
402 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
403 else if (def->hasProtectedVisibility())
404 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
Rafael Espindola282a4702013-10-31 20:51:58 +0000405 else if (canBeHidden(def))
406 attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000407 else if (def->hasExternalLinkage() || def->hasWeakLinkage() ||
408 def->hasLinkOnceLinkage() || def->hasCommonLinkage() ||
409 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000410 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
411 else
412 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
413
Chad Rosier772a91f2011-06-28 18:26:12 +0000414 StringSet::value_type &entry = _defines.GetOrCreateValue(Buffer);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000415 entry.setValue(1);
416
Bill Wendling9ee2d332012-03-29 08:27:32 +0000417 // fill information structure
418 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000419 StringRef Name = entry.getKey();
420 info.name = Name.data();
421 assert(info.name[Name.size()] == '\0');
Bill Wendling9ee2d332012-03-29 08:27:32 +0000422 info.attributes = attr;
423 info.isFunction = isFunction;
424 info.symbol = def;
425
426 // add to table of symbols
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000427 _symbols.push_back(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000428}
429
Bill Wendlingfb440502012-03-28 20:46:54 +0000430/// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
431/// defined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000432void LTOModule::addAsmGlobalSymbol(const char *name,
433 lto_symbol_attributes scope) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000434 StringSet::value_type &entry = _defines.GetOrCreateValue(name);
435
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000436 // only add new define if not already defined
Rafael Espindola477d11f2011-02-20 16:27:25 +0000437 if (entry.getValue())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000438 return;
439
Rafael Espindola477d11f2011-02-20 16:27:25 +0000440 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000441
442 NameAndAttributes &info = _undefines[entry.getKey().data()];
443
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000444 if (info.symbol == 0) {
Bill Wendling71b19bb2012-04-02 10:01:21 +0000445 // FIXME: This is trying to take care of module ASM like this:
446 //
447 // module asm ".zerofill __FOO, __foo, _bar_baz_qux, 0"
448 //
449 // but is gross and its mother dresses it funny. Have the ASM parser give us
450 // more details for this type of situation so that we're not guessing so
451 // much.
452
453 // fill information structure
Rafael Espindola5f4b32f2012-05-11 03:42:13 +0000454 info.name = entry.getKey().data();
Bill Wendling71b19bb2012-04-02 10:01:21 +0000455 info.attributes =
456 LTO_SYMBOL_PERMISSIONS_DATA | LTO_SYMBOL_DEFINITION_REGULAR | scope;
457 info.isFunction = false;
458 info.symbol = 0;
459
460 // add to table of symbols
461 _symbols.push_back(info);
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000462 return;
463 }
464
Bill Wendling9ee2d332012-03-29 08:27:32 +0000465 if (info.isFunction)
466 addDefinedFunctionSymbol(cast<Function>(info.symbol));
467 else
468 addDefinedDataSymbol(info.symbol);
Bill Wendling8f6c8a92012-03-30 23:26:06 +0000469
470 _symbols.back().attributes &= ~LTO_SYMBOL_SCOPE_MASK;
471 _symbols.back().attributes |= scope;
Devang Patela59fe952008-07-16 18:06:52 +0000472}
Nick Kledzik07b4a622008-02-26 20:26:43 +0000473
Bill Wendlingfb440502012-03-28 20:46:54 +0000474/// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the
475/// undefined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000476void LTOModule::addAsmGlobalSymbolUndef(const char *name) {
477 StringMap<NameAndAttributes>::value_type &entry =
478 _undefines.GetOrCreateValue(name);
479
480 _asm_undefines.push_back(entry.getKey().data());
481
482 // we already have the symbol
483 if (entry.getValue().name)
484 return;
485
486 uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;;
487 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
488 NameAndAttributes info;
489 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000490 info.attributes = attr;
491 info.isFunction = false;
492 info.symbol = 0;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000493
494 entry.setValue(info);
495}
496
Bill Wendlingfb440502012-03-28 20:46:54 +0000497/// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet to a
498/// list to be resolved later.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000499void
500LTOModule::addPotentialUndefinedSymbol(const GlobalValue *decl, bool isFunc) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000501 // ignore all llvm.* symbols
502 if (decl->getName().startswith("llvm."))
503 return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000504
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000505 // ignore all aliases
506 if (isa<GlobalAlias>(decl))
507 return;
Nick Lewycky0661b932009-07-09 06:03:04 +0000508
Rafael Espindola34b59382011-02-11 05:23:09 +0000509 SmallString<64> name;
Rafael Espindola117b20c2013-12-05 05:53:12 +0000510 _mangler.getNameWithPrefix(name, decl);
Rafael Espindola56548522009-04-24 16:55:21 +0000511
Rafael Espindola477d11f2011-02-20 16:27:25 +0000512 StringMap<NameAndAttributes>::value_type &entry =
Chad Rosier772a91f2011-06-28 18:26:12 +0000513 _undefines.GetOrCreateValue(name);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000514
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000515 // we already have the symbol
Rafael Espindola477d11f2011-02-20 16:27:25 +0000516 if (entry.getValue().name)
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000517 return;
Rafael Espindola56548522009-04-24 16:55:21 +0000518
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000519 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000520
521 info.name = entry.getKey().data();
Bill Wendlingfb440502012-03-28 20:46:54 +0000522
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000523 if (decl->hasExternalWeakLinkage())
524 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
525 else
526 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000527
Bill Wendling9ee2d332012-03-29 08:27:32 +0000528 info.isFunction = isFunc;
529 info.symbol = decl;
530
Rafael Espindola477d11f2011-02-20 16:27:25 +0000531 entry.setValue(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000532}
533
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000534namespace {
535 class RecordStreamer : public MCStreamer {
536 public:
Bill Wendling32867652012-04-03 03:56:52 +0000537 enum State { NeverSeen, Global, Defined, DefinedGlobal, Used };
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000538
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000539 private:
540 StringMap<State> Symbols;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000541
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000542 void markDefined(const MCSymbol &Symbol) {
543 State &S = Symbols[Symbol.getName()];
544 switch (S) {
545 case DefinedGlobal:
546 case Global:
547 S = DefinedGlobal;
548 break;
549 case NeverSeen:
550 case Defined:
551 case Used:
552 S = Defined;
553 break;
554 }
555 }
556 void markGlobal(const MCSymbol &Symbol) {
557 State &S = Symbols[Symbol.getName()];
558 switch (S) {
559 case DefinedGlobal:
560 case Defined:
561 S = DefinedGlobal;
562 break;
563
564 case NeverSeen:
565 case Global:
566 case Used:
567 S = Global;
568 break;
569 }
570 }
571 void markUsed(const MCSymbol &Symbol) {
572 State &S = Symbols[Symbol.getName()];
573 switch (S) {
574 case DefinedGlobal:
575 case Defined:
576 case Global:
577 break;
578
579 case NeverSeen:
580 case Used:
581 S = Used;
582 break;
583 }
584 }
585
586 // FIXME: mostly copied for the obj streamer.
587 void AddValueSymbols(const MCExpr *Value) {
588 switch (Value->getKind()) {
589 case MCExpr::Target:
590 // FIXME: What should we do in here?
591 break;
592
593 case MCExpr::Constant:
594 break;
595
596 case MCExpr::Binary: {
597 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
598 AddValueSymbols(BE->getLHS());
599 AddValueSymbols(BE->getRHS());
600 break;
601 }
602
603 case MCExpr::SymbolRef:
604 markUsed(cast<MCSymbolRefExpr>(Value)->getSymbol());
605 break;
606
607 case MCExpr::Unary:
608 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
609 break;
610 }
611 }
612
613 public:
614 typedef StringMap<State>::const_iterator const_iterator;
615
616 const_iterator begin() {
617 return Symbols.begin();
618 }
619
620 const_iterator end() {
621 return Symbols.end();
622 }
623
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000624 RecordStreamer(MCContext &Context) : MCStreamer(Context, 0) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000625
Bill Wendling32867652012-04-03 03:56:52 +0000626 virtual void EmitInstruction(const MCInst &Inst) {
627 // Scan for values.
628 for (unsigned i = Inst.getNumOperands(); i--; )
629 if (Inst.getOperand(i).isExpr())
630 AddValueSymbols(Inst.getOperand(i).getExpr());
631 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000632 virtual void EmitLabel(MCSymbol *Symbol) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000633 Symbol->setSection(*getCurrentSection().first);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000634 markDefined(*Symbol);
635 }
Reed Kotleraee4d5d12012-12-16 04:00:45 +0000636 virtual void EmitDebugLabel(MCSymbol *Symbol) {
637 EmitLabel(Symbol);
638 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000639 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
640 // FIXME: should we handle aliases?
641 markDefined(*Symbol);
642 }
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000643 virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000644 if (Attribute == MCSA_Global)
645 markGlobal(*Symbol);
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000646 return true;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000647 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000648 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Cheng95847992012-06-22 20:30:39 +0000649 uint64_t Size , unsigned ByteAlignment) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000650 markDefined(*Symbol);
651 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000652 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
653 unsigned ByteAlignment) {
654 markDefined(*Symbol);
655 }
Bill Wendling32867652012-04-03 03:56:52 +0000656
Eli Benderskyf483ff92012-12-20 19:05:53 +0000657 virtual void EmitBundleAlignMode(unsigned AlignPow2) {}
Eli Bendersky802b6282013-01-07 21:51:08 +0000658 virtual void EmitBundleLock(bool AlignToEnd) {}
Eli Benderskyf483ff92012-12-20 19:05:53 +0000659 virtual void EmitBundleUnlock() {}
660
Bill Wendling32867652012-04-03 03:56:52 +0000661 // Noop calls.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000662 virtual void ChangeSection(const MCSection *Section,
663 const MCExpr *Subsection) {}
Eli Benderskycbb25142013-01-14 19:04:57 +0000664 virtual void InitToTextSection() {}
Bill Wendling32867652012-04-03 03:56:52 +0000665 virtual void InitSections() {}
666 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {}
667 virtual void EmitThumbFunc(MCSymbol *Func) {}
668 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {}
669 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {}
670 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {}
671 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {}
672 virtual void EmitCOFFSymbolType(int Type) {}
673 virtual void EndCOFFSymbolDef() {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000674 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {}
Benjamin Kramer63970512011-09-01 23:04:27 +0000675 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
676 unsigned ByteAlignment) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000677 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
678 uint64_t Size, unsigned ByteAlignment) {}
Rafael Espindola64e1af82013-07-02 15:49:13 +0000679 virtual void EmitBytes(StringRef Data) {}
680 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {}
Rafael Espindola6aea5922011-04-21 23:39:26 +0000681 virtual void EmitULEB128Value(const MCExpr *Value) {}
682 virtual void EmitSLEB128Value(const MCExpr *Value) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000683 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
684 unsigned ValueSize,
685 unsigned MaxBytesToEmit) {}
686 virtual void EmitCodeAlignment(unsigned ByteAlignment,
687 unsigned MaxBytesToEmit) {}
Jim Grosbachb5912772012-01-27 00:37:08 +0000688 virtual bool EmitValueToOffset(const MCExpr *Offset,
689 unsigned char Value ) { return false; }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000690 virtual void EmitFileDirective(StringRef Filename) {}
691 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
692 const MCSymbol *LastLabel,
Evan Chengc7ac6902011-07-14 05:43:07 +0000693 const MCSymbol *Label,
694 unsigned PointerSize) {}
Rafael Espindola07082092012-01-07 03:13:18 +0000695 virtual void FinishImpl() {}
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000696 virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
697 RecordProcEnd(Frame);
698 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000699 };
Bill Wendling9ee2d332012-03-29 08:27:32 +0000700} // end anonymous namespace
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000701
Bill Wendlingfb440502012-03-28 20:46:54 +0000702/// addAsmGlobalSymbols - Add global symbols from module-level ASM to the
703/// defined or undefined lists.
Bill Wendlingac2abde2011-11-04 09:24:40 +0000704bool LTOModule::addAsmGlobalSymbols(std::string &errMsg) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000705 const std::string &inlineAsm = _module->getModuleInlineAsm();
Ivan Krasincc2a8012011-09-08 07:38:25 +0000706 if (inlineAsm.empty())
707 return false;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000708
Bill Wendlingac2abde2011-11-04 09:24:40 +0000709 OwningPtr<RecordStreamer> Streamer(new RecordStreamer(_context));
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000710 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(inlineAsm);
711 SourceMgr SrcMgr;
712 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach345768c2011-08-16 18:33:49 +0000713 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr,
Bill Wendlingac2abde2011-11-04 09:24:40 +0000714 _context, *Streamer,
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000715 *_target->getMCAsmInfo()));
Bill Wendling9351b3e2012-08-08 22:01:55 +0000716 const Target &T = _target->getTarget();
Joey Goulydb6144e2013-09-12 12:55:29 +0000717 OwningPtr<MCInstrInfo> MCII(T.createMCInstrInfo());
Bill Wendling9351b3e2012-08-08 22:01:55 +0000718 OwningPtr<MCSubtargetInfo>
719 STI(T.createMCSubtargetInfo(_target->getTargetTriple(),
720 _target->getTargetCPU(),
721 _target->getTargetFeatureString()));
Joey Goulydb6144e2013-09-12 12:55:29 +0000722 OwningPtr<MCTargetAsmParser> TAP(T.createMCAsmParser(*STI, *Parser.get(), *MCII));
Ivan Krasin8149dd62011-09-08 07:36:39 +0000723 if (!TAP) {
Bill Wendling9351b3e2012-08-08 22:01:55 +0000724 errMsg = "target " + std::string(T.getName()) +
725 " does not define AsmParser.";
Ivan Krasin8149dd62011-09-08 07:36:39 +0000726 return true;
727 }
728
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000729 Parser->setTargetParser(*TAP);
Bill Wendling9351b3e2012-08-08 22:01:55 +0000730 if (Parser->Run(false))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000731 return true;
732
733 for (RecordStreamer::const_iterator i = Streamer->begin(),
734 e = Streamer->end(); i != e; ++i) {
735 StringRef Key = i->first();
736 RecordStreamer::State Value = i->second;
737 if (Value == RecordStreamer::DefinedGlobal)
738 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_DEFAULT);
739 else if (Value == RecordStreamer::Defined)
740 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_INTERNAL);
741 else if (Value == RecordStreamer::Global ||
742 Value == RecordStreamer::Used)
743 addAsmGlobalSymbolUndef(Key.data());
744 }
Bill Wendling9351b3e2012-08-08 22:01:55 +0000745
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000746 return false;
747}
748
Bill Wendlingfb440502012-03-28 20:46:54 +0000749/// isDeclaration - Return 'true' if the global value is a declaration.
Rafael Espindola5b778b22011-03-18 19:51:00 +0000750static bool isDeclaration(const GlobalValue &V) {
751 if (V.hasAvailableExternallyLinkage())
752 return true;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000753
Rafael Espindola5b778b22011-03-18 19:51:00 +0000754 if (V.isMaterializable())
755 return false;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000756
Rafael Espindola5b778b22011-03-18 19:51:00 +0000757 return V.isDeclaration();
758}
759
Bill Wendling7e58b382012-03-28 23:12:18 +0000760/// parseSymbols - Parse the symbols from the module and model-level ASM and add
Bill Wendlingfb440502012-03-28 20:46:54 +0000761/// them to either the defined or undefined lists.
Bill Wendling7e58b382012-03-28 23:12:18 +0000762bool LTOModule::parseSymbols(std::string &errMsg) {
Daniel Dunbar919660b2010-08-10 23:46:46 +0000763 // add functions
Bill Wendling763acfc2012-03-29 03:34:57 +0000764 for (Module::iterator f = _module->begin(), e = _module->end(); f != e; ++f) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000765 if (isDeclaration(*f))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000766 addPotentialUndefinedSymbol(f, true);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000767 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000768 addDefinedFunctionSymbol(f);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000769 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000770
Daniel Dunbar919660b2010-08-10 23:46:46 +0000771 // add data
772 for (Module::global_iterator v = _module->global_begin(),
773 e = _module->global_end(); v != e; ++v) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000774 if (isDeclaration(*v))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000775 addPotentialUndefinedSymbol(v, false);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000776 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000777 addDefinedDataSymbol(v);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000778 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000779
Daniel Dunbar919660b2010-08-10 23:46:46 +0000780 // add asm globals
Bill Wendlingac2abde2011-11-04 09:24:40 +0000781 if (addAsmGlobalSymbols(errMsg))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000782 return true;
Daniel Dunbar919660b2010-08-10 23:46:46 +0000783
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000784 // add aliases
Bill Wendling9ee2d332012-03-29 08:27:32 +0000785 for (Module::alias_iterator a = _module->alias_begin(),
786 e = _module->alias_end(); a != e; ++a) {
787 if (isDeclaration(*a->getAliasedGlobal()))
Bill Wendlingd58ed732012-03-28 20:48:49 +0000788 // Is an alias to a declaration.
Bill Wendling9ee2d332012-03-29 08:27:32 +0000789 addPotentialUndefinedSymbol(a, false);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000790 else
Bill Wendling9ee2d332012-03-29 08:27:32 +0000791 addDefinedDataSymbol(a);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000792 }
793
Daniel Dunbar919660b2010-08-10 23:46:46 +0000794 // make symbols for all undefines
Bill Wendling9ee2d332012-03-29 08:27:32 +0000795 for (StringMap<NameAndAttributes>::iterator u =_undefines.begin(),
796 e = _undefines.end(); u != e; ++u) {
797 // If this symbol also has a definition, then don't make an undefine because
798 // it is a tentative definition.
799 if (_defines.count(u->getKey())) continue;
800 NameAndAttributes info = u->getValue();
801 _symbols.push_back(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000802 }
Bill Wendling9ee2d332012-03-29 08:27:32 +0000803
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000804 return false;
Nick Kledzik91a6dcf2008-02-27 22:25:36 +0000805}
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000806
807/// parseMetadata - Parse metadata from the module
808void LTOModule::parseMetadata() {
809 // Linker Options
810 if (Value *Val = _module->getModuleFlag("Linker Options")) {
811 MDNode *LinkerOptions = cast<MDNode>(Val);
812 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
813 MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i));
814 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
815 MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
816 StringRef Op = _linkeropt_strings.
817 GetOrCreateValue(MDOption->getString()).getKey();
818 StringRef DepLibName = _target->getTargetLowering()->
819 getObjFileLowering().getDepLibFromLinkerOpt(Op);
820 if (!DepLibName.empty())
821 _deplibs.push_back(DepLibName.data());
822 else if (!Op.empty())
823 _linkeropts.push_back(Op.data());
824 }
825 }
826 }
827
828 // Add other interesting metadata here.
829}