blob: c1a9f80b52876dae90b38308ea57ecdb139d89b2 [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 Espindola20fcda72014-01-22 22:11:14 +000027#include "llvm/MC/MCSection.h"
Rafael Espindola1e49a6d2011-03-02 04:14:42 +000028#include "llvm/MC/MCStreamer.h"
Evan Cheng91111d22011-07-09 05:47:46 +000029#include "llvm/MC/MCSubtargetInfo.h"
Rafael Espindola1e49a6d2011-03-02 04:14:42 +000030#include "llvm/MC/MCSymbol.h"
Evan Cheng11424442011-07-26 00:24:13 +000031#include "llvm/MC/MCTargetAsmParser.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000032#include "llvm/MC/SubtargetFeature.h"
Bill Wendlingb8dcda72012-08-06 21:34:54 +000033#include "llvm/Support/CommandLine.h"
Rafael Espindola46ed3532013-06-11 18:05:26 +000034#include "llvm/Support/FileSystem.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000035#include "llvm/Support/Host.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000036#include "llvm/Support/MemoryBuffer.h"
37#include "llvm/Support/Path.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000038#include "llvm/Support/SourceMgr.h"
Bill Wendling8f6c8a92012-03-30 23:26:06 +000039#include "llvm/Support/TargetRegistry.h"
40#include "llvm/Support/TargetSelect.h"
41#include "llvm/Support/system_error.h"
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +000042#include "llvm/Target/TargetLowering.h"
43#include "llvm/Target/TargetLoweringObjectFile.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000044#include "llvm/Target/TargetRegisterInfo.h"
Rafael Espindola282a4702013-10-31 20:51:58 +000045#include "llvm/Transforms/Utils/GlobalStatus.h"
Nick Kledzik07b4a622008-02-26 20:26:43 +000046using namespace llvm;
47
Bill Wendlingfb440502012-03-28 20:46:54 +000048LTOModule::LTOModule(llvm::Module *m, llvm::TargetMachine *t)
49 : _module(m), _target(t),
Rafael Espindolae28610d2013-12-09 20:26:40 +000050 _context(_target->getMCAsmInfo(), _target->getRegisterInfo(), &ObjFileInfo),
Rafael Espindola58873562014-01-03 19:21:54 +000051 _mangler(t->getDataLayout()) {
Rafael Espindolae28610d2013-12-09 20:26:40 +000052 ObjFileInfo.InitMCObjectFileInfo(t->getTargetTriple(),
53 t->getRelocationModel(), t->getCodeModel(),
54 _context);
55}
Bill Wendlingfb440502012-03-28 20:46:54 +000056
57/// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM
58/// bitcode.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000059bool LTOModule::isBitcodeFile(const void *mem, size_t length) {
Rafael Espindola46ed3532013-06-11 18:05:26 +000060 return sys::fs::identify_magic(StringRef((const char *)mem, length)) ==
61 sys::fs::file_magic::bitcode;
Nick Kledzik07b4a622008-02-26 20:26:43 +000062}
63
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000064bool LTOModule::isBitcodeFile(const char *path) {
Rafael Espindola71affba2013-06-12 15:13:57 +000065 sys::fs::file_magic type;
66 if (sys::fs::identify_magic(path, type))
67 return false;
68 return type == sys::fs::file_magic::bitcode;
Nick Kledzik07b4a622008-02-26 20:26:43 +000069}
70
Bill Wendlingfb440502012-03-28 20:46:54 +000071/// isBitcodeFileForTarget - Returns 'true' if the file (or memory contents) is
72/// LLVM bitcode for the specified triple.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000073bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length,
74 const char *triplePrefix) {
75 MemoryBuffer *buffer = makeBuffer(mem, length);
76 if (!buffer)
Nick Kledzikb481c202009-06-01 20:33:09 +000077 return false;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000078 return isTargetMatch(buffer, triplePrefix);
Nick Kledzikb481c202009-06-01 20:33:09 +000079}
80
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000081bool LTOModule::isBitcodeFileForTarget(const char *path,
82 const char *triplePrefix) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +000083 OwningPtr<MemoryBuffer> buffer;
84 if (MemoryBuffer::getFile(path, buffer))
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000085 return false;
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +000086 return isTargetMatch(buffer.take(), triplePrefix);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000087}
88
Bill Wendlingfb440502012-03-28 20:46:54 +000089/// isTargetMatch - Returns 'true' if the memory buffer is for the specified
90/// target triple.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000091bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) {
Bill Wendling0198ce02010-10-06 01:22:42 +000092 std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
93 delete buffer;
Bill Wendling5f689e72011-11-04 18:48:00 +000094 return strncmp(Triple.c_str(), triplePrefix, strlen(triplePrefix)) == 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +000095}
96
Bill Wendlingfb440502012-03-28 20:46:54 +000097/// makeLTOModule - Create an LTOModule. N.B. These methods take ownership of
98/// the buffer.
Rafael Espindola0b385c72013-09-30 16:39:19 +000099LTOModule *LTOModule::makeLTOModule(const char *path, TargetOptions options,
100 std::string &errMsg) {
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000101 OwningPtr<MemoryBuffer> buffer;
102 if (error_code ec = MemoryBuffer::getFile(path, buffer)) {
Michael J. Spencerd4227232010-12-09 18:06:07 +0000103 errMsg = ec.message();
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000104 return NULL;
Michael J. Spencerd4227232010-12-09 18:06:07 +0000105 }
Rafael Espindola0b385c72013-09-30 16:39:19 +0000106 return makeLTOModule(buffer.take(), options, errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000107}
108
Rafael Espindola56e41f72011-02-08 22:40:47 +0000109LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000110 size_t size, TargetOptions options,
111 std::string &errMsg) {
112 return makeLTOModule(fd, path, size, 0, options, errMsg);
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000113}
114
115LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000116 size_t map_size,
117 off_t offset,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000118 TargetOptions options,
Rafael Espindola56e41f72011-02-08 22:40:47 +0000119 std::string &errMsg) {
120 OwningPtr<MemoryBuffer> buffer;
Rafael Espindola3d2ac2e2013-07-23 20:25:01 +0000121 if (error_code ec =
122 MemoryBuffer::getOpenFileSlice(fd, path, buffer, map_size, offset)) {
Rafael Espindola56e41f72011-02-08 22:40:47 +0000123 errMsg = ec.message();
124 return NULL;
125 }
Rafael Espindola0b385c72013-09-30 16:39:19 +0000126 return makeLTOModule(buffer.take(), options, errMsg);
Rafael Espindola56e41f72011-02-08 22:40:47 +0000127}
128
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000129LTOModule *LTOModule::makeLTOModule(const void *mem, size_t length,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000130 TargetOptions options,
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000131 std::string &errMsg) {
132 OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length));
133 if (!buffer)
134 return NULL;
Rafael Espindola0b385c72013-09-30 16:39:19 +0000135 return makeLTOModule(buffer.take(), options, errMsg);
Bill Wendlingb8dcda72012-08-06 21:34:54 +0000136}
137
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000138LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000139 TargetOptions options,
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000140 std::string &errMsg) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000141 // parse bitcode buffer
Rafael Espindola5b6c1e82014-01-13 18:31:04 +0000142 ErrorOr<Module *> ModuleOrErr =
143 getLazyBitcodeModule(buffer, getGlobalContext());
144 if (error_code EC = ModuleOrErr.getError()) {
145 errMsg = EC.message();
Rafael Espindola5b778b22011-03-18 19:51:00 +0000146 delete buffer;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000147 return NULL;
Rafael Espindola5b778b22011-03-18 19:51:00 +0000148 }
Rafael Espindola5b6c1e82014-01-13 18:31:04 +0000149 OwningPtr<Module> m(ModuleOrErr.get());
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000150
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000151 std::string TripleStr = m->getTargetTriple();
152 if (TripleStr.empty())
153 TripleStr = sys::getDefaultTargetTriple();
154 llvm::Triple Triple(TripleStr);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000155
156 // find machine architecture for this module
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000157 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000158 if (!march)
159 return NULL;
160
Nick Lewycky364c04a2011-04-21 01:54:08 +0000161 // construct LTOModule, hand over ownership of module and target
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000162 SubtargetFeatures Features;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000163 Features.getDefaultSubtargetFeatures(Triple);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000164 std::string FeatureStr = Features.getString();
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000165 // Set a default CPU for Darwin triples.
Evan Chengfe6e4052011-06-30 01:53:36 +0000166 std::string CPU;
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000167 if (Triple.isOSDarwin()) {
168 if (Triple.getArch() == llvm::Triple::x86_64)
169 CPU = "core2";
170 else if (Triple.getArch() == llvm::Triple::x86)
171 CPU = "yonah";
172 }
Rafael Espindola0b385c72013-09-30 16:39:19 +0000173
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000174 TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr,
Rafael Espindola0b385c72013-09-30 16:39:19 +0000175 options);
Rafael Espindolae9fab9b2014-01-14 23:51:27 +0000176 m->materializeAllPermanently();
Rafael Espindola282a4702013-10-31 20:51:58 +0000177
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000178 LTOModule *Ret = new LTOModule(m.take(), target);
Bill Wendling7e58b382012-03-28 23:12:18 +0000179 if (Ret->parseSymbols(errMsg)) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000180 delete Ret;
181 return NULL;
182 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000183
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000184 Ret->parseMetadata();
185
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000186 return Ret;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000187}
188
Bill Wendlingfb440502012-03-28 20:46:54 +0000189/// makeBuffer - Create a MemoryBuffer from a memory range.
190MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length) {
Roman Divackyad06cee2012-09-05 22:26:57 +0000191 const char *startPtr = (const char*)mem;
Bill Wendlingfb440502012-03-28 20:46:54 +0000192 return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), "", false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000193}
194
Bill Wendlingfb440502012-03-28 20:46:54 +0000195/// objcClassNameFromExpression - Get string that the data pointer points to.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000196bool
197LTOModule::objcClassNameFromExpression(const Constant *c, std::string &name) {
198 if (const ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000199 Constant *op = ce->getOperand(0);
200 if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) {
201 Constant *cn = gvn->getInitializer();
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000202 if (ConstantDataArray *ca = dyn_cast<ConstantDataArray>(cn)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000203 if (ca->isCString()) {
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000204 name = ".objc_class_name_" + ca->getAsCString().str();
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000205 return true;
Nick Kledzikb481c202009-06-01 20:33:09 +0000206 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000207 }
Nick Kledzikb481c202009-06-01 20:33:09 +0000208 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000209 }
210 return false;
211}
212
Bill Wendlingfb440502012-03-28 20:46:54 +0000213/// addObjCClass - Parse i386/ppc ObjC class data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000214void LTOModule::addObjCClass(const GlobalVariable *clgv) {
215 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000216 if (!c) return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000217
Bill Wendling5f689e72011-11-04 18:48:00 +0000218 // second slot in __OBJC,__class is pointer to superclass name
219 std::string superclassName;
220 if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
221 NameAndAttributes info;
222 StringMap<NameAndAttributes>::value_type &entry =
223 _undefines.GetOrCreateValue(superclassName);
224 if (!entry.getValue().name) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000225 const char *symbolName = entry.getKey().data();
226 info.name = symbolName;
227 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000228 info.isFunction = false;
229 info.symbol = clgv;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000230 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000231 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000232 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000233
234 // third slot in __OBJC,__class is pointer to class name
235 std::string className;
236 if (objcClassNameFromExpression(c->getOperand(2), className)) {
237 StringSet::value_type &entry = _defines.GetOrCreateValue(className);
238 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000239
Bill Wendling5f689e72011-11-04 18:48:00 +0000240 NameAndAttributes info;
241 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000242 info.attributes = LTO_SYMBOL_PERMISSIONS_DATA |
243 LTO_SYMBOL_DEFINITION_REGULAR | LTO_SYMBOL_SCOPE_DEFAULT;
244 info.isFunction = false;
245 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000246 _symbols.push_back(info);
247 }
248}
249
Bill Wendlingfb440502012-03-28 20:46:54 +0000250/// addObjCCategory - Parse i386/ppc ObjC category data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000251void LTOModule::addObjCCategory(const GlobalVariable *clgv) {
252 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000253 if (!c) return;
254
255 // second slot in __OBJC,__category is pointer to target class name
256 std::string targetclassName;
257 if (!objcClassNameFromExpression(c->getOperand(1), targetclassName))
258 return;
259
260 NameAndAttributes info;
261 StringMap<NameAndAttributes>::value_type &entry =
262 _undefines.GetOrCreateValue(targetclassName);
263
264 if (entry.getValue().name)
265 return;
266
267 const char *symbolName = entry.getKey().data();
268 info.name = symbolName;
269 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000270 info.isFunction = false;
271 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000272 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000273}
274
Bill Wendlingfb440502012-03-28 20:46:54 +0000275/// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000276void LTOModule::addObjCClassRef(const GlobalVariable *clgv) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000277 std::string targetclassName;
Bill Wendling5f689e72011-11-04 18:48:00 +0000278 if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName))
279 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000280
Bill Wendling5f689e72011-11-04 18:48:00 +0000281 NameAndAttributes info;
282 StringMap<NameAndAttributes>::value_type &entry =
283 _undefines.GetOrCreateValue(targetclassName);
284 if (entry.getValue().name)
285 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000286
Bill Wendling5f689e72011-11-04 18:48:00 +0000287 const char *symbolName = entry.getKey().data();
288 info.name = symbolName;
289 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000290 info.isFunction = false;
291 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000292 entry.setValue(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000293}
294
Bill Wendlingfb440502012-03-28 20:46:54 +0000295/// addDefinedDataSymbol - Add a data symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000296void LTOModule::addDefinedDataSymbol(const GlobalValue *v) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000297 // Add to list of defined symbols.
Bill Wendlinga2af6742011-11-04 09:30:19 +0000298 addDefinedSymbol(v, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000299
Bill Wendling45f74e32012-08-06 22:52:45 +0000300 if (!v->hasSection() /* || !isTargetDarwin */)
301 return;
302
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000303 // Special case i386/ppc ObjC data structures in magic sections:
304 // The issue is that the old ObjC object format did some strange
305 // contortions to avoid real linker symbols. For instance, the
306 // ObjC class data structure is allocated statically in the executable
307 // that defines that class. That data structures contains a pointer to
308 // its superclass. But instead of just initializing that part of the
309 // struct to the address of its superclass, and letting the static and
310 // dynamic linkers do the rest, the runtime works by having that field
311 // instead point to a C-string that is the name of the superclass.
312 // At runtime the objc initialization updates that pointer and sets
313 // it to point to the actual super class. As far as the linker
314 // knows it is just a pointer to a string. But then someone wanted the
315 // linker to issue errors at build time if the superclass was not found.
316 // So they figured out a way in mach-o object format to use an absolute
317 // symbols (.objc_class_name_Foo = 0) and a floating reference
318 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
319 // a class was missing.
320 // The following synthesizes the implicit .objc_* symbols for the linker
321 // from the ObjC data structures generated by the front end.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000322
Bill Wendling45f74e32012-08-06 22:52:45 +0000323 // special case if this data blob is an ObjC class definition
324 if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000325 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000326 addObjCClass(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000327 }
Bill Wendling45f74e32012-08-06 22:52:45 +0000328 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000329
Bill Wendling45f74e32012-08-06 22:52:45 +0000330 // special case if this data blob is an ObjC category definition
331 else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000332 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000333 addObjCCategory(gv);
334 }
335 }
336
337 // special case if this data blob is the list of referenced classes
338 else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000339 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000340 addObjCClassRef(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000341 }
342 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000343}
344
Bill Wendlingfb440502012-03-28 20:46:54 +0000345/// addDefinedFunctionSymbol - Add a function symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000346void LTOModule::addDefinedFunctionSymbol(const Function *f) {
Bill Wendlingfb440502012-03-28 20:46:54 +0000347 // add to list of defined symbols
348 addDefinedSymbol(f, true);
349}
350
Rafael Espindola282a4702013-10-31 20:51:58 +0000351static bool canBeHidden(const GlobalValue *GV) {
352 GlobalValue::LinkageTypes L = GV->getLinkage();
353
Rafael Espindola282a4702013-10-31 20:51:58 +0000354 if (L != GlobalValue::LinkOnceODRLinkage)
355 return false;
356
357 if (GV->hasUnnamedAddr())
358 return true;
359
360 GlobalStatus GS;
361 if (GlobalStatus::analyzeGlobal(GV, GS))
362 return false;
363
364 return !GS.IsCompared;
365}
366
Bill Wendlingfb440502012-03-28 20:46:54 +0000367/// addDefinedSymbol - Add a defined symbol to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000368void LTOModule::addDefinedSymbol(const GlobalValue *def, bool isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000369 // ignore all llvm.* symbols
370 if (def->getName().startswith("llvm."))
371 return;
372
373 // string is owned by _defines
Rafael Espindola34b59382011-02-11 05:23:09 +0000374 SmallString<64> Buffer;
Rafael Espindola117b20c2013-12-05 05:53:12 +0000375 _mangler.getNameWithPrefix(Buffer, def);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000376
377 // set alignment part log2() can have rounding errors
378 uint32_t align = def->getAlignment();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000379 uint32_t attr = align ? countTrailingZeros(def->getAlignment()) : 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000380
381 // set permissions part
Bill Wendling9ee2d332012-03-29 08:27:32 +0000382 if (isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000383 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000384 } else {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000385 const GlobalVariable *gv = dyn_cast<GlobalVariable>(def);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000386 if (gv && gv->isConstant())
387 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
388 else
389 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
390 }
391
392 // set definition part
Bill Wendling2776d462010-09-27 18:05:19 +0000393 if (def->hasWeakLinkage() || def->hasLinkOnceLinkage() ||
Bill Wendling34bc34e2012-08-17 18:33:14 +0000394 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000395 attr |= LTO_SYMBOL_DEFINITION_WEAK;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000396 else if (def->hasCommonLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000397 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000398 else
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000399 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000400
401 // set scope part
402 if (def->hasHiddenVisibility())
403 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
404 else if (def->hasProtectedVisibility())
405 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
Rafael Espindola282a4702013-10-31 20:51:58 +0000406 else if (canBeHidden(def))
407 attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000408 else if (def->hasExternalLinkage() || def->hasWeakLinkage() ||
409 def->hasLinkOnceLinkage() || def->hasCommonLinkage() ||
410 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000411 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
412 else
413 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
414
Chad Rosier772a91f2011-06-28 18:26:12 +0000415 StringSet::value_type &entry = _defines.GetOrCreateValue(Buffer);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000416 entry.setValue(1);
417
Bill Wendling9ee2d332012-03-29 08:27:32 +0000418 // fill information structure
419 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000420 StringRef Name = entry.getKey();
421 info.name = Name.data();
422 assert(info.name[Name.size()] == '\0');
Bill Wendling9ee2d332012-03-29 08:27:32 +0000423 info.attributes = attr;
424 info.isFunction = isFunction;
425 info.symbol = def;
426
427 // add to table of symbols
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000428 _symbols.push_back(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000429}
430
Bill Wendlingfb440502012-03-28 20:46:54 +0000431/// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
432/// defined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000433void LTOModule::addAsmGlobalSymbol(const char *name,
434 lto_symbol_attributes scope) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000435 StringSet::value_type &entry = _defines.GetOrCreateValue(name);
436
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000437 // only add new define if not already defined
Rafael Espindola477d11f2011-02-20 16:27:25 +0000438 if (entry.getValue())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000439 return;
440
Rafael Espindola477d11f2011-02-20 16:27:25 +0000441 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000442
443 NameAndAttributes &info = _undefines[entry.getKey().data()];
444
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000445 if (info.symbol == 0) {
Bill Wendling71b19bb2012-04-02 10:01:21 +0000446 // FIXME: This is trying to take care of module ASM like this:
447 //
448 // module asm ".zerofill __FOO, __foo, _bar_baz_qux, 0"
449 //
450 // but is gross and its mother dresses it funny. Have the ASM parser give us
451 // more details for this type of situation so that we're not guessing so
452 // much.
453
454 // fill information structure
Rafael Espindola5f4b32f2012-05-11 03:42:13 +0000455 info.name = entry.getKey().data();
Bill Wendling71b19bb2012-04-02 10:01:21 +0000456 info.attributes =
457 LTO_SYMBOL_PERMISSIONS_DATA | LTO_SYMBOL_DEFINITION_REGULAR | scope;
458 info.isFunction = false;
459 info.symbol = 0;
460
461 // add to table of symbols
462 _symbols.push_back(info);
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000463 return;
464 }
465
Bill Wendling9ee2d332012-03-29 08:27:32 +0000466 if (info.isFunction)
467 addDefinedFunctionSymbol(cast<Function>(info.symbol));
468 else
469 addDefinedDataSymbol(info.symbol);
Bill Wendling8f6c8a92012-03-30 23:26:06 +0000470
471 _symbols.back().attributes &= ~LTO_SYMBOL_SCOPE_MASK;
472 _symbols.back().attributes |= scope;
Devang Patela59fe952008-07-16 18:06:52 +0000473}
Nick Kledzik07b4a622008-02-26 20:26:43 +0000474
Bill Wendlingfb440502012-03-28 20:46:54 +0000475/// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the
476/// undefined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000477void LTOModule::addAsmGlobalSymbolUndef(const char *name) {
478 StringMap<NameAndAttributes>::value_type &entry =
479 _undefines.GetOrCreateValue(name);
480
481 _asm_undefines.push_back(entry.getKey().data());
482
483 // we already have the symbol
484 if (entry.getValue().name)
485 return;
486
487 uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;;
488 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
489 NameAndAttributes info;
490 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000491 info.attributes = attr;
492 info.isFunction = false;
493 info.symbol = 0;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000494
495 entry.setValue(info);
496}
497
Bill Wendlingfb440502012-03-28 20:46:54 +0000498/// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet to a
499/// list to be resolved later.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000500void
501LTOModule::addPotentialUndefinedSymbol(const GlobalValue *decl, bool isFunc) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000502 // ignore all llvm.* symbols
503 if (decl->getName().startswith("llvm."))
504 return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000505
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000506 // ignore all aliases
507 if (isa<GlobalAlias>(decl))
508 return;
Nick Lewycky0661b932009-07-09 06:03:04 +0000509
Rafael Espindola34b59382011-02-11 05:23:09 +0000510 SmallString<64> name;
Rafael Espindola117b20c2013-12-05 05:53:12 +0000511 _mangler.getNameWithPrefix(name, decl);
Rafael Espindola56548522009-04-24 16:55:21 +0000512
Rafael Espindola477d11f2011-02-20 16:27:25 +0000513 StringMap<NameAndAttributes>::value_type &entry =
Chad Rosier772a91f2011-06-28 18:26:12 +0000514 _undefines.GetOrCreateValue(name);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000515
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000516 // we already have the symbol
Rafael Espindola477d11f2011-02-20 16:27:25 +0000517 if (entry.getValue().name)
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000518 return;
Rafael Espindola56548522009-04-24 16:55:21 +0000519
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000520 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000521
522 info.name = entry.getKey().data();
Bill Wendlingfb440502012-03-28 20:46:54 +0000523
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000524 if (decl->hasExternalWeakLinkage())
525 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
526 else
527 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000528
Bill Wendling9ee2d332012-03-29 08:27:32 +0000529 info.isFunction = isFunc;
530 info.symbol = decl;
531
Rafael Espindola477d11f2011-02-20 16:27:25 +0000532 entry.setValue(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000533}
534
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000535namespace {
Rafael Espindola20fcda72014-01-22 22:11:14 +0000536
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000537 class RecordStreamer : public MCStreamer {
538 public:
Bill Wendling32867652012-04-03 03:56:52 +0000539 enum State { NeverSeen, Global, Defined, DefinedGlobal, Used };
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000540
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000541 private:
542 StringMap<State> Symbols;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000543
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000544 void markDefined(const MCSymbol &Symbol) {
545 State &S = Symbols[Symbol.getName()];
546 switch (S) {
547 case DefinedGlobal:
548 case Global:
549 S = DefinedGlobal;
550 break;
551 case NeverSeen:
552 case Defined:
553 case Used:
554 S = Defined;
555 break;
556 }
557 }
558 void markGlobal(const MCSymbol &Symbol) {
559 State &S = Symbols[Symbol.getName()];
560 switch (S) {
561 case DefinedGlobal:
562 case Defined:
563 S = DefinedGlobal;
564 break;
565
566 case NeverSeen:
567 case Global:
568 case Used:
569 S = Global;
570 break;
571 }
572 }
573 void markUsed(const MCSymbol &Symbol) {
574 State &S = Symbols[Symbol.getName()];
575 switch (S) {
576 case DefinedGlobal:
577 case Defined:
578 case Global:
579 break;
580
581 case NeverSeen:
582 case Used:
583 S = Used;
584 break;
585 }
586 }
587
588 // FIXME: mostly copied for the obj streamer.
589 void AddValueSymbols(const MCExpr *Value) {
590 switch (Value->getKind()) {
591 case MCExpr::Target:
592 // FIXME: What should we do in here?
593 break;
594
595 case MCExpr::Constant:
596 break;
597
598 case MCExpr::Binary: {
599 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
600 AddValueSymbols(BE->getLHS());
601 AddValueSymbols(BE->getRHS());
602 break;
603 }
604
605 case MCExpr::SymbolRef:
606 markUsed(cast<MCSymbolRefExpr>(Value)->getSymbol());
607 break;
608
609 case MCExpr::Unary:
610 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
611 break;
612 }
613 }
614
615 public:
616 typedef StringMap<State>::const_iterator const_iterator;
617
618 const_iterator begin() {
619 return Symbols.begin();
620 }
621
622 const_iterator end() {
623 return Symbols.end();
624 }
625
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000626 RecordStreamer(MCContext &Context) : MCStreamer(Context) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000627
Bill Wendling32867652012-04-03 03:56:52 +0000628 virtual void EmitInstruction(const MCInst &Inst) {
629 // Scan for values.
630 for (unsigned i = Inst.getNumOperands(); i--; )
631 if (Inst.getOperand(i).isExpr())
632 AddValueSymbols(Inst.getOperand(i).getExpr());
633 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000634 virtual void EmitLabel(MCSymbol *Symbol) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000635 Symbol->setSection(*getCurrentSection().first);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000636 markDefined(*Symbol);
637 }
Reed Kotleraee4d5d12012-12-16 04:00:45 +0000638 virtual void EmitDebugLabel(MCSymbol *Symbol) {
639 EmitLabel(Symbol);
640 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000641 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
642 // FIXME: should we handle aliases?
643 markDefined(*Symbol);
644 }
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000645 virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000646 if (Attribute == MCSA_Global)
647 markGlobal(*Symbol);
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000648 return true;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000649 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000650 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Cheng95847992012-06-22 20:30:39 +0000651 uint64_t Size , unsigned ByteAlignment) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000652 markDefined(*Symbol);
653 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000654 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
655 unsigned ByteAlignment) {
656 markDefined(*Symbol);
657 }
Bill Wendling32867652012-04-03 03:56:52 +0000658
Eli Benderskyf483ff92012-12-20 19:05:53 +0000659 virtual void EmitBundleAlignMode(unsigned AlignPow2) {}
Eli Bendersky802b6282013-01-07 21:51:08 +0000660 virtual void EmitBundleLock(bool AlignToEnd) {}
Eli Benderskyf483ff92012-12-20 19:05:53 +0000661 virtual void EmitBundleUnlock() {}
662
Bill Wendling32867652012-04-03 03:56:52 +0000663 // Noop calls.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000664 virtual void ChangeSection(const MCSection *Section,
665 const MCExpr *Subsection) {}
Bill Wendling32867652012-04-03 03:56:52 +0000666 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}