blob: 909b92e888662000e80be0113f6d15ceb2e5bd4c [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,
Manman Ren03456a12014-02-10 23:26:14 +0000131 std::string &errMsg, StringRef path) {
132 OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length, path));
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000133 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
Manman Ren03456a12014-02-10 23:26:14 +0000189/// Create a MemoryBuffer from a memory range with an optional name.
190MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length,
191 StringRef name) {
Roman Divackyad06cee2012-09-05 22:26:57 +0000192 const char *startPtr = (const char*)mem;
Manman Ren03456a12014-02-10 23:26:14 +0000193 return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), name, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000194}
195
Bill Wendlingfb440502012-03-28 20:46:54 +0000196/// objcClassNameFromExpression - Get string that the data pointer points to.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000197bool
198LTOModule::objcClassNameFromExpression(const Constant *c, std::string &name) {
199 if (const ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000200 Constant *op = ce->getOperand(0);
201 if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) {
202 Constant *cn = gvn->getInitializer();
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000203 if (ConstantDataArray *ca = dyn_cast<ConstantDataArray>(cn)) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000204 if (ca->isCString()) {
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000205 name = ".objc_class_name_" + ca->getAsCString().str();
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000206 return true;
Nick Kledzikb481c202009-06-01 20:33:09 +0000207 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000208 }
Nick Kledzikb481c202009-06-01 20:33:09 +0000209 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000210 }
211 return false;
212}
213
Bill Wendlingfb440502012-03-28 20:46:54 +0000214/// addObjCClass - Parse i386/ppc ObjC class data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000215void LTOModule::addObjCClass(const GlobalVariable *clgv) {
216 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000217 if (!c) return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000218
Bill Wendling5f689e72011-11-04 18:48:00 +0000219 // second slot in __OBJC,__class is pointer to superclass name
220 std::string superclassName;
221 if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
222 NameAndAttributes info;
223 StringMap<NameAndAttributes>::value_type &entry =
224 _undefines.GetOrCreateValue(superclassName);
225 if (!entry.getValue().name) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000226 const char *symbolName = entry.getKey().data();
227 info.name = symbolName;
228 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000229 info.isFunction = false;
230 info.symbol = clgv;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000231 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000232 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000233 }
Bill Wendling5f689e72011-11-04 18:48:00 +0000234
235 // third slot in __OBJC,__class is pointer to class name
236 std::string className;
237 if (objcClassNameFromExpression(c->getOperand(2), className)) {
238 StringSet::value_type &entry = _defines.GetOrCreateValue(className);
239 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000240
Bill Wendling5f689e72011-11-04 18:48:00 +0000241 NameAndAttributes info;
242 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000243 info.attributes = LTO_SYMBOL_PERMISSIONS_DATA |
244 LTO_SYMBOL_DEFINITION_REGULAR | LTO_SYMBOL_SCOPE_DEFAULT;
245 info.isFunction = false;
246 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000247 _symbols.push_back(info);
248 }
249}
250
Bill Wendlingfb440502012-03-28 20:46:54 +0000251/// addObjCCategory - Parse i386/ppc ObjC category data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000252void LTOModule::addObjCCategory(const GlobalVariable *clgv) {
253 const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
Bill Wendling5f689e72011-11-04 18:48:00 +0000254 if (!c) return;
255
256 // second slot in __OBJC,__category is pointer to target class name
257 std::string targetclassName;
258 if (!objcClassNameFromExpression(c->getOperand(1), targetclassName))
259 return;
260
261 NameAndAttributes info;
262 StringMap<NameAndAttributes>::value_type &entry =
263 _undefines.GetOrCreateValue(targetclassName);
264
265 if (entry.getValue().name)
266 return;
267
268 const char *symbolName = entry.getKey().data();
269 info.name = symbolName;
270 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000271 info.isFunction = false;
272 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000273 entry.setValue(info);
Nick Kledzikb481c202009-06-01 20:33:09 +0000274}
275
Bill Wendlingfb440502012-03-28 20:46:54 +0000276/// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000277void LTOModule::addObjCClassRef(const GlobalVariable *clgv) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000278 std::string targetclassName;
Bill Wendling5f689e72011-11-04 18:48:00 +0000279 if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName))
280 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000281
Bill Wendling5f689e72011-11-04 18:48:00 +0000282 NameAndAttributes info;
283 StringMap<NameAndAttributes>::value_type &entry =
284 _undefines.GetOrCreateValue(targetclassName);
285 if (entry.getValue().name)
286 return;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000287
Bill Wendling5f689e72011-11-04 18:48:00 +0000288 const char *symbolName = entry.getKey().data();
289 info.name = symbolName;
290 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000291 info.isFunction = false;
292 info.symbol = clgv;
Bill Wendling5f689e72011-11-04 18:48:00 +0000293 entry.setValue(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000294}
295
Bill Wendlingfb440502012-03-28 20:46:54 +0000296/// addDefinedDataSymbol - Add a data symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000297void LTOModule::addDefinedDataSymbol(const GlobalValue *v) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000298 // Add to list of defined symbols.
Bill Wendlinga2af6742011-11-04 09:30:19 +0000299 addDefinedSymbol(v, false);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000300
Bill Wendling45f74e32012-08-06 22:52:45 +0000301 if (!v->hasSection() /* || !isTargetDarwin */)
302 return;
303
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000304 // Special case i386/ppc ObjC data structures in magic sections:
305 // The issue is that the old ObjC object format did some strange
306 // contortions to avoid real linker symbols. For instance, the
307 // ObjC class data structure is allocated statically in the executable
308 // that defines that class. That data structures contains a pointer to
309 // its superclass. But instead of just initializing that part of the
310 // struct to the address of its superclass, and letting the static and
311 // dynamic linkers do the rest, the runtime works by having that field
312 // instead point to a C-string that is the name of the superclass.
313 // At runtime the objc initialization updates that pointer and sets
314 // it to point to the actual super class. As far as the linker
315 // knows it is just a pointer to a string. But then someone wanted the
316 // linker to issue errors at build time if the superclass was not found.
317 // So they figured out a way in mach-o object format to use an absolute
318 // symbols (.objc_class_name_Foo = 0) and a floating reference
319 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
320 // a class was missing.
321 // The following synthesizes the implicit .objc_* symbols for the linker
322 // from the ObjC data structures generated by the front end.
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000323
Bill Wendling45f74e32012-08-06 22:52:45 +0000324 // special case if this data blob is an ObjC class definition
325 if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000326 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000327 addObjCClass(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000328 }
Bill Wendling45f74e32012-08-06 22:52:45 +0000329 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000330
Bill Wendling45f74e32012-08-06 22:52:45 +0000331 // special case if this data blob is an ObjC category definition
332 else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000333 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000334 addObjCCategory(gv);
335 }
336 }
337
338 // special case if this data blob is the list of referenced classes
339 else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000340 if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000341 addObjCClassRef(gv);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000342 }
343 }
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000344}
345
Bill Wendlingfb440502012-03-28 20:46:54 +0000346/// addDefinedFunctionSymbol - Add a function symbol as defined to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000347void LTOModule::addDefinedFunctionSymbol(const Function *f) {
Bill Wendlingfb440502012-03-28 20:46:54 +0000348 // add to list of defined symbols
349 addDefinedSymbol(f, true);
350}
351
Rafael Espindola282a4702013-10-31 20:51:58 +0000352static bool canBeHidden(const GlobalValue *GV) {
Rafael Espindola66f273b2014-02-07 19:04:43 +0000353 // FIXME: this is duplicated with another static function in AsmPrinter.cpp
Rafael Espindola282a4702013-10-31 20:51:58 +0000354 GlobalValue::LinkageTypes L = GV->getLinkage();
355
Rafael Espindola282a4702013-10-31 20:51:58 +0000356 if (L != GlobalValue::LinkOnceODRLinkage)
357 return false;
358
359 if (GV->hasUnnamedAddr())
360 return true;
361
Rafael Espindola66f273b2014-02-07 19:04:43 +0000362 // If it is a non constant variable, it needs to be uniqued across shared
363 // objects.
364 if (const GlobalVariable *Var = dyn_cast<GlobalVariable>(GV)) {
365 if (!Var->isConstant())
366 return false;
367 }
368
Rafael Espindola282a4702013-10-31 20:51:58 +0000369 GlobalStatus GS;
370 if (GlobalStatus::analyzeGlobal(GV, GS))
371 return false;
372
373 return !GS.IsCompared;
374}
375
Bill Wendlingfb440502012-03-28 20:46:54 +0000376/// addDefinedSymbol - Add a defined symbol to the list.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000377void LTOModule::addDefinedSymbol(const GlobalValue *def, bool isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000378 // ignore all llvm.* symbols
379 if (def->getName().startswith("llvm."))
380 return;
381
382 // string is owned by _defines
Rafael Espindola34b59382011-02-11 05:23:09 +0000383 SmallString<64> Buffer;
Daniel Jasper7e198ad2014-02-19 12:26:01 +0000384 _mangler.getNameWithPrefix(Buffer, def);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000385
386 // set alignment part log2() can have rounding errors
387 uint32_t align = def->getAlignment();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000388 uint32_t attr = align ? countTrailingZeros(def->getAlignment()) : 0;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000389
390 // set permissions part
Bill Wendling9ee2d332012-03-29 08:27:32 +0000391 if (isFunction) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000392 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
Bill Wendling9ee2d332012-03-29 08:27:32 +0000393 } else {
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000394 const GlobalVariable *gv = dyn_cast<GlobalVariable>(def);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000395 if (gv && gv->isConstant())
396 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
397 else
398 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
399 }
400
401 // set definition part
Bill Wendling2776d462010-09-27 18:05:19 +0000402 if (def->hasWeakLinkage() || def->hasLinkOnceLinkage() ||
Bill Wendling34bc34e2012-08-17 18:33:14 +0000403 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000404 attr |= LTO_SYMBOL_DEFINITION_WEAK;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000405 else if (def->hasCommonLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000406 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000407 else
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000408 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000409
410 // set scope part
411 if (def->hasHiddenVisibility())
412 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
413 else if (def->hasProtectedVisibility())
414 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
Rafael Espindola282a4702013-10-31 20:51:58 +0000415 else if (canBeHidden(def))
416 attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
Bill Wendlingdcd7c2b2010-09-27 20:17:45 +0000417 else if (def->hasExternalLinkage() || def->hasWeakLinkage() ||
418 def->hasLinkOnceLinkage() || def->hasCommonLinkage() ||
419 def->hasLinkerPrivateWeakLinkage())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000420 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
421 else
422 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
423
Chad Rosier772a91f2011-06-28 18:26:12 +0000424 StringSet::value_type &entry = _defines.GetOrCreateValue(Buffer);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000425 entry.setValue(1);
426
Bill Wendling9ee2d332012-03-29 08:27:32 +0000427 // fill information structure
428 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000429 StringRef Name = entry.getKey();
430 info.name = Name.data();
431 assert(info.name[Name.size()] == '\0');
Bill Wendling9ee2d332012-03-29 08:27:32 +0000432 info.attributes = attr;
433 info.isFunction = isFunction;
434 info.symbol = def;
435
436 // add to table of symbols
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000437 _symbols.push_back(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000438}
439
Bill Wendlingfb440502012-03-28 20:46:54 +0000440/// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
441/// defined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000442void LTOModule::addAsmGlobalSymbol(const char *name,
443 lto_symbol_attributes scope) {
Rafael Espindola477d11f2011-02-20 16:27:25 +0000444 StringSet::value_type &entry = _defines.GetOrCreateValue(name);
445
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000446 // only add new define if not already defined
Rafael Espindola477d11f2011-02-20 16:27:25 +0000447 if (entry.getValue())
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000448 return;
449
Rafael Espindola477d11f2011-02-20 16:27:25 +0000450 entry.setValue(1);
Bill Wendling9ee2d332012-03-29 08:27:32 +0000451
452 NameAndAttributes &info = _undefines[entry.getKey().data()];
453
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000454 if (info.symbol == 0) {
Bill Wendling71b19bb2012-04-02 10:01:21 +0000455 // FIXME: This is trying to take care of module ASM like this:
456 //
457 // module asm ".zerofill __FOO, __foo, _bar_baz_qux, 0"
458 //
459 // but is gross and its mother dresses it funny. Have the ASM parser give us
460 // more details for this type of situation so that we're not guessing so
461 // much.
462
463 // fill information structure
Rafael Espindola5f4b32f2012-05-11 03:42:13 +0000464 info.name = entry.getKey().data();
Bill Wendling71b19bb2012-04-02 10:01:21 +0000465 info.attributes =
466 LTO_SYMBOL_PERMISSIONS_DATA | LTO_SYMBOL_DEFINITION_REGULAR | scope;
467 info.isFunction = false;
468 info.symbol = 0;
469
470 // add to table of symbols
471 _symbols.push_back(info);
Bill Wendling3a0bcf02012-04-02 03:33:31 +0000472 return;
473 }
474
Bill Wendling9ee2d332012-03-29 08:27:32 +0000475 if (info.isFunction)
476 addDefinedFunctionSymbol(cast<Function>(info.symbol));
477 else
478 addDefinedDataSymbol(info.symbol);
Bill Wendling8f6c8a92012-03-30 23:26:06 +0000479
480 _symbols.back().attributes &= ~LTO_SYMBOL_SCOPE_MASK;
481 _symbols.back().attributes |= scope;
Devang Patela59fe952008-07-16 18:06:52 +0000482}
Nick Kledzik07b4a622008-02-26 20:26:43 +0000483
Bill Wendlingfb440502012-03-28 20:46:54 +0000484/// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the
485/// undefined list.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000486void LTOModule::addAsmGlobalSymbolUndef(const char *name) {
487 StringMap<NameAndAttributes>::value_type &entry =
488 _undefines.GetOrCreateValue(name);
489
490 _asm_undefines.push_back(entry.getKey().data());
491
492 // we already have the symbol
493 if (entry.getValue().name)
494 return;
495
496 uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;;
497 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
498 NameAndAttributes info;
499 info.name = entry.getKey().data();
Bill Wendling9ee2d332012-03-29 08:27:32 +0000500 info.attributes = attr;
501 info.isFunction = false;
502 info.symbol = 0;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000503
504 entry.setValue(info);
505}
506
Bill Wendlingfb440502012-03-28 20:46:54 +0000507/// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet to a
508/// list to be resolved later.
Rafael Espindola6ee19d22012-12-11 03:10:43 +0000509void
510LTOModule::addPotentialUndefinedSymbol(const GlobalValue *decl, bool isFunc) {
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000511 // ignore all llvm.* symbols
512 if (decl->getName().startswith("llvm."))
513 return;
Nick Kledzikb481c202009-06-01 20:33:09 +0000514
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000515 // ignore all aliases
516 if (isa<GlobalAlias>(decl))
517 return;
Nick Lewycky0661b932009-07-09 06:03:04 +0000518
Rafael Espindola34b59382011-02-11 05:23:09 +0000519 SmallString<64> name;
Daniel Jasper7e198ad2014-02-19 12:26:01 +0000520 _mangler.getNameWithPrefix(name, decl);
Rafael Espindola56548522009-04-24 16:55:21 +0000521
Rafael Espindola477d11f2011-02-20 16:27:25 +0000522 StringMap<NameAndAttributes>::value_type &entry =
Chad Rosier772a91f2011-06-28 18:26:12 +0000523 _undefines.GetOrCreateValue(name);
Rafael Espindola477d11f2011-02-20 16:27:25 +0000524
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000525 // we already have the symbol
Rafael Espindola477d11f2011-02-20 16:27:25 +0000526 if (entry.getValue().name)
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000527 return;
Rafael Espindola56548522009-04-24 16:55:21 +0000528
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000529 NameAndAttributes info;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000530
531 info.name = entry.getKey().data();
Bill Wendlingfb440502012-03-28 20:46:54 +0000532
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000533 if (decl->hasExternalWeakLinkage())
534 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
535 else
536 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
Rafael Espindola477d11f2011-02-20 16:27:25 +0000537
Bill Wendling9ee2d332012-03-29 08:27:32 +0000538 info.isFunction = isFunc;
539 info.symbol = decl;
540
Rafael Espindola477d11f2011-02-20 16:27:25 +0000541 entry.setValue(info);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000542}
543
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000544namespace {
Rafael Espindola20fcda72014-01-22 22:11:14 +0000545
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000546 class RecordStreamer : public MCStreamer {
547 public:
Bill Wendling32867652012-04-03 03:56:52 +0000548 enum State { NeverSeen, Global, Defined, DefinedGlobal, Used };
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000549
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000550 private:
551 StringMap<State> Symbols;
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000552
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000553 void markDefined(const MCSymbol &Symbol) {
554 State &S = Symbols[Symbol.getName()];
555 switch (S) {
556 case DefinedGlobal:
557 case Global:
558 S = DefinedGlobal;
559 break;
560 case NeverSeen:
561 case Defined:
562 case Used:
563 S = Defined;
564 break;
565 }
566 }
567 void markGlobal(const MCSymbol &Symbol) {
568 State &S = Symbols[Symbol.getName()];
569 switch (S) {
570 case DefinedGlobal:
571 case Defined:
572 S = DefinedGlobal;
573 break;
574
575 case NeverSeen:
576 case Global:
577 case Used:
578 S = Global;
579 break;
580 }
581 }
582 void markUsed(const MCSymbol &Symbol) {
583 State &S = Symbols[Symbol.getName()];
584 switch (S) {
585 case DefinedGlobal:
586 case Defined:
587 case Global:
588 break;
589
590 case NeverSeen:
591 case Used:
592 S = Used;
593 break;
594 }
595 }
596
597 // FIXME: mostly copied for the obj streamer.
598 void AddValueSymbols(const MCExpr *Value) {
599 switch (Value->getKind()) {
600 case MCExpr::Target:
601 // FIXME: What should we do in here?
602 break;
603
604 case MCExpr::Constant:
605 break;
606
607 case MCExpr::Binary: {
608 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
609 AddValueSymbols(BE->getLHS());
610 AddValueSymbols(BE->getRHS());
611 break;
612 }
613
614 case MCExpr::SymbolRef:
615 markUsed(cast<MCSymbolRefExpr>(Value)->getSymbol());
616 break;
617
618 case MCExpr::Unary:
619 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
620 break;
621 }
622 }
623
624 public:
625 typedef StringMap<State>::const_iterator const_iterator;
626
627 const_iterator begin() {
628 return Symbols.begin();
629 }
630
631 const_iterator end() {
632 return Symbols.end();
633 }
634
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000635 RecordStreamer(MCContext &Context) : MCStreamer(Context) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000636
David Woodhousee6c13e42014-01-28 23:12:42 +0000637 virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) {
Bill Wendling32867652012-04-03 03:56:52 +0000638 // Scan for values.
639 for (unsigned i = Inst.getNumOperands(); i--; )
640 if (Inst.getOperand(i).isExpr())
641 AddValueSymbols(Inst.getOperand(i).getExpr());
642 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000643 virtual void EmitLabel(MCSymbol *Symbol) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000644 Symbol->setSection(*getCurrentSection().first);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000645 markDefined(*Symbol);
646 }
Reed Kotleraee4d5d12012-12-16 04:00:45 +0000647 virtual void EmitDebugLabel(MCSymbol *Symbol) {
648 EmitLabel(Symbol);
649 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000650 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
651 // FIXME: should we handle aliases?
652 markDefined(*Symbol);
653 }
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000654 virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000655 if (Attribute == MCSA_Global)
656 markGlobal(*Symbol);
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000657 return true;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000658 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000659 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Cheng95847992012-06-22 20:30:39 +0000660 uint64_t Size , unsigned ByteAlignment) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000661 markDefined(*Symbol);
662 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000663 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
664 unsigned ByteAlignment) {
665 markDefined(*Symbol);
666 }
Bill Wendling32867652012-04-03 03:56:52 +0000667
Eli Benderskyf483ff92012-12-20 19:05:53 +0000668 virtual void EmitBundleAlignMode(unsigned AlignPow2) {}
Eli Bendersky802b6282013-01-07 21:51:08 +0000669 virtual void EmitBundleLock(bool AlignToEnd) {}
Eli Benderskyf483ff92012-12-20 19:05:53 +0000670 virtual void EmitBundleUnlock() {}
671
Bill Wendling32867652012-04-03 03:56:52 +0000672 // Noop calls.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000673 virtual void ChangeSection(const MCSection *Section,
674 const MCExpr *Subsection) {}
Bill Wendling32867652012-04-03 03:56:52 +0000675 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {}
676 virtual void EmitThumbFunc(MCSymbol *Func) {}
677 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {}
678 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {}
679 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {}
680 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {}
681 virtual void EmitCOFFSymbolType(int Type) {}
682 virtual void EndCOFFSymbolDef() {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000683 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {}
Benjamin Kramer63970512011-09-01 23:04:27 +0000684 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
685 unsigned ByteAlignment) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000686 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
687 uint64_t Size, unsigned ByteAlignment) {}
Rafael Espindola64e1af82013-07-02 15:49:13 +0000688 virtual void EmitBytes(StringRef Data) {}
689 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {}
Rafael Espindola6aea5922011-04-21 23:39:26 +0000690 virtual void EmitULEB128Value(const MCExpr *Value) {}
691 virtual void EmitSLEB128Value(const MCExpr *Value) {}
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000692 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
693 unsigned ValueSize,
694 unsigned MaxBytesToEmit) {}
695 virtual void EmitCodeAlignment(unsigned ByteAlignment,
696 unsigned MaxBytesToEmit) {}
Jim Grosbachb5912772012-01-27 00:37:08 +0000697 virtual bool EmitValueToOffset(const MCExpr *Offset,
698 unsigned char Value ) { return false; }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000699 virtual void EmitFileDirective(StringRef Filename) {}
700 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
701 const MCSymbol *LastLabel,
Evan Chengc7ac6902011-07-14 05:43:07 +0000702 const MCSymbol *Label,
703 unsigned PointerSize) {}
Rafael Espindola07082092012-01-07 03:13:18 +0000704 virtual void FinishImpl() {}
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000705 virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
706 RecordProcEnd(Frame);
707 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000708 };
Bill Wendling9ee2d332012-03-29 08:27:32 +0000709} // end anonymous namespace
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000710
Bill Wendlingfb440502012-03-28 20:46:54 +0000711/// addAsmGlobalSymbols - Add global symbols from module-level ASM to the
712/// defined or undefined lists.
Bill Wendlingac2abde2011-11-04 09:24:40 +0000713bool LTOModule::addAsmGlobalSymbols(std::string &errMsg) {
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000714 const std::string &inlineAsm = _module->getModuleInlineAsm();
Ivan Krasincc2a8012011-09-08 07:38:25 +0000715 if (inlineAsm.empty())
716 return false;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000717
Bill Wendlingac2abde2011-11-04 09:24:40 +0000718 OwningPtr<RecordStreamer> Streamer(new RecordStreamer(_context));
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000719 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(inlineAsm);
720 SourceMgr SrcMgr;
721 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach345768c2011-08-16 18:33:49 +0000722 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr,
Bill Wendlingac2abde2011-11-04 09:24:40 +0000723 _context, *Streamer,
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000724 *_target->getMCAsmInfo()));
Bill Wendling9351b3e2012-08-08 22:01:55 +0000725 const Target &T = _target->getTarget();
Joey Goulydb6144e2013-09-12 12:55:29 +0000726 OwningPtr<MCInstrInfo> MCII(T.createMCInstrInfo());
Bill Wendling9351b3e2012-08-08 22:01:55 +0000727 OwningPtr<MCSubtargetInfo>
728 STI(T.createMCSubtargetInfo(_target->getTargetTriple(),
729 _target->getTargetCPU(),
730 _target->getTargetFeatureString()));
Joey Goulydb6144e2013-09-12 12:55:29 +0000731 OwningPtr<MCTargetAsmParser> TAP(T.createMCAsmParser(*STI, *Parser.get(), *MCII));
Ivan Krasin8149dd62011-09-08 07:36:39 +0000732 if (!TAP) {
Bill Wendling9351b3e2012-08-08 22:01:55 +0000733 errMsg = "target " + std::string(T.getName()) +
734 " does not define AsmParser.";
Ivan Krasin8149dd62011-09-08 07:36:39 +0000735 return true;
736 }
737
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000738 Parser->setTargetParser(*TAP);
Bill Wendling9351b3e2012-08-08 22:01:55 +0000739 if (Parser->Run(false))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000740 return true;
741
742 for (RecordStreamer::const_iterator i = Streamer->begin(),
743 e = Streamer->end(); i != e; ++i) {
744 StringRef Key = i->first();
745 RecordStreamer::State Value = i->second;
746 if (Value == RecordStreamer::DefinedGlobal)
747 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_DEFAULT);
748 else if (Value == RecordStreamer::Defined)
749 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_INTERNAL);
750 else if (Value == RecordStreamer::Global ||
751 Value == RecordStreamer::Used)
752 addAsmGlobalSymbolUndef(Key.data());
753 }
Bill Wendling9351b3e2012-08-08 22:01:55 +0000754
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000755 return false;
756}
757
Bill Wendlingfb440502012-03-28 20:46:54 +0000758/// isDeclaration - Return 'true' if the global value is a declaration.
Rafael Espindola5b778b22011-03-18 19:51:00 +0000759static bool isDeclaration(const GlobalValue &V) {
760 if (V.hasAvailableExternallyLinkage())
761 return true;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000762
Rafael Espindola5b778b22011-03-18 19:51:00 +0000763 if (V.isMaterializable())
764 return false;
Bill Wendling9351b3e2012-08-08 22:01:55 +0000765
Rafael Espindola5b778b22011-03-18 19:51:00 +0000766 return V.isDeclaration();
767}
768
Bill Wendling7e58b382012-03-28 23:12:18 +0000769/// parseSymbols - Parse the symbols from the module and model-level ASM and add
Bill Wendlingfb440502012-03-28 20:46:54 +0000770/// them to either the defined or undefined lists.
Bill Wendling7e58b382012-03-28 23:12:18 +0000771bool LTOModule::parseSymbols(std::string &errMsg) {
Daniel Dunbar919660b2010-08-10 23:46:46 +0000772 // add functions
Bill Wendling763acfc2012-03-29 03:34:57 +0000773 for (Module::iterator f = _module->begin(), e = _module->end(); f != e; ++f) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000774 if (isDeclaration(*f))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000775 addPotentialUndefinedSymbol(f, true);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000776 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000777 addDefinedFunctionSymbol(f);
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 data
781 for (Module::global_iterator v = _module->global_begin(),
782 e = _module->global_end(); v != e; ++v) {
Rafael Espindola5b778b22011-03-18 19:51:00 +0000783 if (isDeclaration(*v))
Bill Wendling9ee2d332012-03-29 08:27:32 +0000784 addPotentialUndefinedSymbol(v, false);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000785 else
Bill Wendlinga2af6742011-11-04 09:30:19 +0000786 addDefinedDataSymbol(v);
Daniel Dunbar919660b2010-08-10 23:46:46 +0000787 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000788
Daniel Dunbar919660b2010-08-10 23:46:46 +0000789 // add asm globals
Bill Wendlingac2abde2011-11-04 09:24:40 +0000790 if (addAsmGlobalSymbols(errMsg))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000791 return true;
Daniel Dunbar919660b2010-08-10 23:46:46 +0000792
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000793 // add aliases
Bill Wendling9ee2d332012-03-29 08:27:32 +0000794 for (Module::alias_iterator a = _module->alias_begin(),
795 e = _module->alias_end(); a != e; ++a) {
796 if (isDeclaration(*a->getAliasedGlobal()))
Bill Wendlingd58ed732012-03-28 20:48:49 +0000797 // Is an alias to a declaration.
Bill Wendling9ee2d332012-03-29 08:27:32 +0000798 addPotentialUndefinedSymbol(a, false);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000799 else
Bill Wendling9ee2d332012-03-29 08:27:32 +0000800 addDefinedDataSymbol(a);
Rafael Espindolaa8a74ec2010-10-20 04:57:22 +0000801 }
802
Daniel Dunbar919660b2010-08-10 23:46:46 +0000803 // make symbols for all undefines
Bill Wendling9ee2d332012-03-29 08:27:32 +0000804 for (StringMap<NameAndAttributes>::iterator u =_undefines.begin(),
805 e = _undefines.end(); u != e; ++u) {
806 // If this symbol also has a definition, then don't make an undefine because
807 // it is a tentative definition.
808 if (_defines.count(u->getKey())) continue;
809 NameAndAttributes info = u->getValue();
810 _symbols.push_back(info);
Daniel Dunbar5657e7b2010-08-10 23:46:39 +0000811 }
Bill Wendling9ee2d332012-03-29 08:27:32 +0000812
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000813 return false;
Nick Kledzik91a6dcf2008-02-27 22:25:36 +0000814}
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000815
816/// parseMetadata - Parse metadata from the module
817void LTOModule::parseMetadata() {
818 // Linker Options
819 if (Value *Val = _module->getModuleFlag("Linker Options")) {
820 MDNode *LinkerOptions = cast<MDNode>(Val);
821 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
822 MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i));
823 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
824 MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
825 StringRef Op = _linkeropt_strings.
826 GetOrCreateValue(MDOption->getString()).getKey();
827 StringRef DepLibName = _target->getTargetLowering()->
828 getObjFileLowering().getDepLibFromLinkerOpt(Op);
829 if (!DepLibName.empty())
830 _deplibs.push_back(DepLibName.data());
831 else if (!Op.empty())
832 _linkeropts.push_back(Op.data());
833 }
834 }
835 }
836
837 // Add other interesting metadata here.
838}