blob: 9a8b155275ffb3c53b04e90fefdb14d4ea0744f4 [file] [log] [blame]
Nick Kledzik77595fc2008-02-26 20:26:43 +00001//===-LTOModule.cpp - LLVM Link Time Optimizer ----------------------------===//
2//
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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Link Time Optimization library. This library is
11// intended to be used by linker to optimize code at link time.
12//
13//===----------------------------------------------------------------------===//
14
Nick Kledzikef194ed2008-02-27 22:25:36 +000015#include "LTOModule.h"
16
Nick Kledzik3eb445f2009-06-01 20:33:09 +000017#include "llvm/Constants.h"
Owen Anderson8b477ed2009-07-01 16:58:40 +000018#include "llvm/LLVMContext.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000019#include "llvm/Module.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000020#include "llvm/ModuleProvider.h"
Nick Kledzikef194ed2008-02-27 22:25:36 +000021#include "llvm/ADT/OwningPtr.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000022#include "llvm/Bitcode/ReaderWriter.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000023#include "llvm/Support/SystemUtils.h"
24#include "llvm/Support/Mangler.h"
25#include "llvm/Support/MemoryBuffer.h"
Nick Kledzikef194ed2008-02-27 22:25:36 +000026#include "llvm/Support/MathExtras.h"
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +000027#include "llvm/System/Host.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000028#include "llvm/System/Path.h"
Nick Kledzik90dcff72008-05-09 01:09:59 +000029#include "llvm/System/Process.h"
Bill Wendling604a8182008-06-18 06:35:30 +000030#include "llvm/Target/SubtargetFeature.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000031#include "llvm/Target/TargetAsmInfo.h"
Daniel Dunbarff9834a2009-07-16 02:41:19 +000032#include "llvm/Target/TargetMachine.h"
33#include "llvm/Target/TargetRegistry.h"
Nick Lewyckyd42b58b2009-07-26 22:16:39 +000034#include "llvm/Target/TargetSelect.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000035
Nick Kledzik77595fc2008-02-26 20:26:43 +000036using namespace llvm;
37
38bool LTOModule::isBitcodeFile(const void* mem, size_t length)
39{
40 return ( llvm::sys::IdentifyFileType((char*)mem, length)
41 == llvm::sys::Bitcode_FileType );
42}
43
44bool LTOModule::isBitcodeFile(const char* path)
45{
46 return llvm::sys::Path(path).isBitcodeFile();
47}
48
Chris Lattner038112a2008-04-01 18:04:03 +000049bool LTOModule::isBitcodeFileForTarget(const void* mem, size_t length,
50 const char* triplePrefix)
Nick Kledzik77595fc2008-02-26 20:26:43 +000051{
Nick Kledzik90dcff72008-05-09 01:09:59 +000052 MemoryBuffer* buffer = makeBuffer(mem, length);
Nick Kledzikef194ed2008-02-27 22:25:36 +000053 if ( buffer == NULL )
54 return false;
55 return isTargetMatch(buffer, triplePrefix);
Nick Kledzik77595fc2008-02-26 20:26:43 +000056}
57
Nick Kledzikef194ed2008-02-27 22:25:36 +000058
Nick Kledzik77595fc2008-02-26 20:26:43 +000059bool LTOModule::isBitcodeFileForTarget(const char* path,
Chris Lattner038112a2008-04-01 18:04:03 +000060 const char* triplePrefix)
Nick Kledzik77595fc2008-02-26 20:26:43 +000061{
Chris Lattner038112a2008-04-01 18:04:03 +000062 MemoryBuffer *buffer = MemoryBuffer::getFile(path);
63 if (buffer == NULL)
Nick Kledzikef194ed2008-02-27 22:25:36 +000064 return false;
65 return isTargetMatch(buffer, triplePrefix);
66}
67
68// takes ownership of buffer
69bool LTOModule::isTargetMatch(MemoryBuffer* buffer, const char* triplePrefix)
70{
Owen Anderson8b477ed2009-07-01 16:58:40 +000071 OwningPtr<ModuleProvider> mp(getBitcodeModuleProvider(buffer,
Owen Anderson0e7a5462009-07-02 00:31:14 +000072 getGlobalContext()));
Nick Kledzikef194ed2008-02-27 22:25:36 +000073 // on success, mp owns buffer and both are deleted at end of this method
74 if ( !mp ) {
75 delete buffer;
76 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +000077 }
Nick Kledzikef194ed2008-02-27 22:25:36 +000078 std::string actualTarget = mp->getModule()->getTargetTriple();
79 return ( strncmp(actualTarget.c_str(), triplePrefix,
80 strlen(triplePrefix)) == 0);
Nick Kledzik77595fc2008-02-26 20:26:43 +000081}
82
83
84LTOModule::LTOModule(Module* m, TargetMachine* t)
85 : _module(m), _target(t), _symbolsParsed(false)
86{
87}
88
Owen Anderson31895e72009-07-01 21:22:36 +000089LTOModule* LTOModule::makeLTOModule(const char* path,
Owen Anderson8b477ed2009-07-01 16:58:40 +000090 std::string& errMsg)
Nick Kledzik77595fc2008-02-26 20:26:43 +000091{
Chris Lattner038112a2008-04-01 18:04:03 +000092 OwningPtr<MemoryBuffer> buffer(MemoryBuffer::getFile(path, &errMsg));
Nick Kledzikef194ed2008-02-27 22:25:36 +000093 if ( !buffer )
94 return NULL;
Owen Anderson0e7a5462009-07-02 00:31:14 +000095 return makeLTOModule(buffer.get(), errMsg);
Nick Kledzik77595fc2008-02-26 20:26:43 +000096}
97
Nick Kledzik6b89d922008-05-09 18:44:41 +000098/// makeBuffer - create a MemoryBuffer from a memory range.
99/// MemoryBuffer requires the byte past end of the buffer to be a zero.
100/// We might get lucky and already be that way, otherwise make a copy.
101/// Also if next byte is on a different page, don't assume it is readable.
Nick Kledzik90dcff72008-05-09 01:09:59 +0000102MemoryBuffer* LTOModule::makeBuffer(const void* mem, size_t length)
103{
Nick Kledziked185d62008-05-28 00:06:14 +0000104 const char* startPtr = (char*)mem;
105 const char* endPtr = startPtr+length;
106 if ( (((uintptr_t)endPtr & (sys::Process::GetPageSize()-1)) == 0)
107 || (*endPtr != 0) )
108 return MemoryBuffer::getMemBufferCopy(startPtr, endPtr);
109 else
110 return MemoryBuffer::getMemBuffer(startPtr, endPtr);
Nick Kledzik90dcff72008-05-09 01:09:59 +0000111}
112
113
Nick Kledzik77595fc2008-02-26 20:26:43 +0000114LTOModule* LTOModule::makeLTOModule(const void* mem, size_t length,
Nick Lewyckyb454eab2009-02-06 07:01:00 +0000115 std::string& errMsg)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000116{
Nick Kledzik90dcff72008-05-09 01:09:59 +0000117 OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length));
Nick Kledzikef194ed2008-02-27 22:25:36 +0000118 if ( !buffer )
119 return NULL;
Owen Anderson0e7a5462009-07-02 00:31:14 +0000120 return makeLTOModule(buffer.get(), errMsg);
Nick Kledzikef194ed2008-02-27 22:25:36 +0000121}
122
Bill Wendlinge4242542008-06-18 21:39:02 +0000123/// getFeatureString - Return a string listing the features associated with the
124/// target triple.
125///
126/// FIXME: This is an inelegant way of specifying the features of a
127/// subtarget. It would be better if we could encode this information into the
128/// IR. See <rdar://5972456>.
129std::string getFeatureString(const char *TargetTriple) {
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000130 InitializeAllTargets();
131
Bill Wendlinge4242542008-06-18 21:39:02 +0000132 SubtargetFeatures Features;
133
134 if (strncmp(TargetTriple, "powerpc-apple-", 14) == 0) {
135 Features.AddFeature("altivec", true);
136 } else if (strncmp(TargetTriple, "powerpc64-apple-", 16) == 0) {
137 Features.AddFeature("64bit", true);
138 Features.AddFeature("altivec", true);
139 }
140
141 return Features.getString();
142}
143
Owen Anderson31895e72009-07-01 21:22:36 +0000144LTOModule* LTOModule::makeLTOModule(MemoryBuffer* buffer,
Owen Anderson8b477ed2009-07-01 16:58:40 +0000145 std::string& errMsg)
Nick Kledzikef194ed2008-02-27 22:25:36 +0000146{
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000147 InitializeAllTargets();
148
Nick Kledzikef194ed2008-02-27 22:25:36 +0000149 // parse bitcode buffer
Owen Anderson0e7a5462009-07-02 00:31:14 +0000150 OwningPtr<Module> m(ParseBitcodeFile(buffer, getGlobalContext(), &errMsg));
Nick Kledzikef194ed2008-02-27 22:25:36 +0000151 if ( !m )
152 return NULL;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000153
154 std::string Triple = m->getTargetTriple();
155 if (Triple.empty())
156 Triple = sys::getHostTriple();
157
Nick Kledzikef194ed2008-02-27 22:25:36 +0000158 // find machine architecture for this module
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000159 const Target* march = TargetRegistry::lookupTarget(Triple,
160 /*FallbackToHost=*/false,
Daniel Dunbara5881e32009-07-26 02:12:58 +0000161 /*RequireJIT=*/false,
162 errMsg);
Nick Kledzikef194ed2008-02-27 22:25:36 +0000163 if ( march == NULL )
164 return NULL;
Bill Wendling604a8182008-06-18 06:35:30 +0000165
Nick Kledzikef194ed2008-02-27 22:25:36 +0000166 // construct LTModule, hand over ownership of module and target
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000167 std::string FeatureStr = getFeatureString(Triple.c_str());
168 TargetMachine* target = march->createTargetMachine(*m, Triple, FeatureStr);
Nick Kledzikef194ed2008-02-27 22:25:36 +0000169 return new LTOModule(m.take(), target);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000170}
171
172
173const char* LTOModule::getTargetTriple()
174{
175 return _module->getTargetTriple().c_str();
176}
177
Nick Kledzikef194ed2008-02-27 22:25:36 +0000178void LTOModule::addDefinedFunctionSymbol(Function* f, Mangler &mangler)
179{
180 // add to list of defined symbols
181 addDefinedSymbol(f, mangler, true);
182
183 // add external symbols referenced by this function.
184 for (Function::iterator b = f->begin(); b != f->end(); ++b) {
185 for (BasicBlock::iterator i = b->begin(); i != b->end(); ++i) {
186 for (unsigned count = 0, total = i->getNumOperands();
187 count != total; ++count) {
188 findExternalRefs(i->getOperand(count), mangler);
189 }
190 }
191 }
192}
193
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000194// get string that data pointer points to
195bool LTOModule::objcClassNameFromExpression(Constant* c, std::string& name)
196{
197 if (ConstantExpr* ce = dyn_cast<ConstantExpr>(c)) {
198 Constant* op = ce->getOperand(0);
199 if (GlobalVariable* gvn = dyn_cast<GlobalVariable>(op)) {
200 Constant* cn = gvn->getInitializer();
201 if (ConstantArray* ca = dyn_cast<ConstantArray>(cn)) {
Owen Anderson1ca29d32009-07-13 21:27:19 +0000202 if ( ca->isCString() ) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000203 name = ".objc_class_name_" + ca->getAsString();
204 return true;
205 }
206 }
207 }
208 }
209 return false;
210}
211
212// parse i386/ppc ObjC class data structure
213void LTOModule::addObjCClass(GlobalVariable* clgv)
214{
215 if (ConstantStruct* c = dyn_cast<ConstantStruct>(clgv->getInitializer())) {
216 // second slot in __OBJC,__class is pointer to superclass name
217 std::string superclassName;
218 if ( objcClassNameFromExpression(c->getOperand(1), superclassName) ) {
219 NameAndAttributes info;
220 if ( _undefines.find(superclassName.c_str()) == _undefines.end() ) {
221 const char* symbolName = ::strdup(superclassName.c_str());
222 info.name = ::strdup(symbolName);
223 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
224 // string is owned by _undefines
225 _undefines[info.name] = info;
226 }
227 }
228 // third slot in __OBJC,__class is pointer to class name
229 std::string className;
230 if ( objcClassNameFromExpression(c->getOperand(2), className) ) {
231 const char* symbolName = ::strdup(className.c_str());
232 NameAndAttributes info;
233 info.name = symbolName;
234 info.attributes = (lto_symbol_attributes)
235 (LTO_SYMBOL_PERMISSIONS_DATA |
236 LTO_SYMBOL_DEFINITION_REGULAR |
237 LTO_SYMBOL_SCOPE_DEFAULT);
238 _symbols.push_back(info);
239 _defines[info.name] = 1;
240 }
241 }
242}
243
244
245// parse i386/ppc ObjC category data structure
246void LTOModule::addObjCCategory(GlobalVariable* clgv)
247{
248 if (ConstantStruct* c = dyn_cast<ConstantStruct>(clgv->getInitializer())) {
249 // second slot in __OBJC,__category is pointer to target class name
250 std::string targetclassName;
251 if ( objcClassNameFromExpression(c->getOperand(1), targetclassName) ) {
252 NameAndAttributes info;
253 if ( _undefines.find(targetclassName.c_str()) == _undefines.end() ){
254 const char* symbolName = ::strdup(targetclassName.c_str());
255 info.name = ::strdup(symbolName);
256 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
257 // string is owned by _undefines
258 _undefines[info.name] = info;
259 }
260 }
261 }
262}
263
264
265// parse i386/ppc ObjC class list data structure
266void LTOModule::addObjCClassRef(GlobalVariable* clgv)
267{
268 std::string targetclassName;
269 if ( objcClassNameFromExpression(clgv->getInitializer(), targetclassName) ){
270 NameAndAttributes info;
271 if ( _undefines.find(targetclassName.c_str()) == _undefines.end() ) {
272 const char* symbolName = ::strdup(targetclassName.c_str());
273 info.name = ::strdup(symbolName);
274 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
275 // string is owned by _undefines
276 _undefines[info.name] = info;
277 }
278 }
279}
280
281
282void LTOModule::addDefinedDataSymbol(GlobalValue* v, Mangler& mangler)
Nick Kledzikef194ed2008-02-27 22:25:36 +0000283{
284 // add to list of defined symbols
285 addDefinedSymbol(v, mangler, false);
286
Nick Kledzik4bdf7302009-06-01 23:41:09 +0000287 // Special case i386/ppc ObjC data structures in magic sections:
288 // The issue is that the old ObjC object format did some strange
289 // contortions to avoid real linker symbols. For instance, the
290 // ObjC class data structure is allocated statically in the executable
291 // that defines that class. That data structures contains a pointer to
292 // its superclass. But instead of just initializing that part of the
293 // struct to the address of its superclass, and letting the static and
294 // dynamic linkers do the rest, the runtime works by having that field
295 // instead point to a C-string that is the name of the superclass.
296 // At runtime the objc initialization updates that pointer and sets
297 // it to point to the actual super class. As far as the linker
298 // knows it is just a pointer to a string. But then someone wanted the
299 // linker to issue errors at build time if the superclass was not found.
300 // So they figured out a way in mach-o object format to use an absolute
301 // symbols (.objc_class_name_Foo = 0) and a floating reference
302 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
303 // a class was missing.
304 // The following synthesizes the implicit .objc_* symbols for the linker
305 // from the ObjC data structures generated by the front end.
306 if ( v->hasSection() /* && isTargetDarwin */ ) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000307 // special case if this data blob is an ObjC class definition
308 if ( v->getSection().compare(0, 15, "__OBJC,__class,") == 0 ) {
309 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(v)) {
310 addObjCClass(gv);
311 }
312 }
313
314 // special case if this data blob is an ObjC category definition
315 else if ( v->getSection().compare(0, 18, "__OBJC,__category,") == 0 ) {
316 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(v)) {
317 addObjCCategory(gv);
318 }
319 }
320
321 // special case if this data blob is the list of referenced classes
322 else if ( v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0 ) {
323 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(v)) {
324 addObjCClassRef(gv);
325 }
326 }
327 }
328
Nick Kledzikef194ed2008-02-27 22:25:36 +0000329 // add external symbols referenced by this data.
Nick Kledzik9178a652008-05-27 22:07:08 +0000330 for (unsigned count = 0, total = v->getNumOperands();
Nick Kledzikef194ed2008-02-27 22:25:36 +0000331 count != total; ++count) {
332 findExternalRefs(v->getOperand(count), mangler);
333 }
334}
335
336
Nick Kledzik77595fc2008-02-26 20:26:43 +0000337void LTOModule::addDefinedSymbol(GlobalValue* def, Mangler &mangler,
Nick Lewycky485ded02009-07-09 06:03:04 +0000338 bool isFunction)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000339{
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000340 // ignore all llvm.* symbols
Daniel Dunbar460f6562009-07-26 09:48:23 +0000341 if (def->getName().startswith("llvm."))
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000342 return;
343
Nick Kledzikef194ed2008-02-27 22:25:36 +0000344 // string is owned by _defines
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000345 const char* symbolName = ::strdup(mangler.getMangledName(def).c_str());
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000346
Nick Kledzik77595fc2008-02-26 20:26:43 +0000347 // set alignment part log2() can have rounding errors
348 uint32_t align = def->getAlignment();
Nick Kledzikef194ed2008-02-27 22:25:36 +0000349 uint32_t attr = align ? CountTrailingZeros_32(def->getAlignment()) : 0;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000350
351 // set permissions part
352 if ( isFunction )
353 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
354 else {
355 GlobalVariable* gv = dyn_cast<GlobalVariable>(def);
356 if ( (gv != NULL) && gv->isConstant() )
357 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
358 else
359 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
360 }
361
362 // set definition part
363 if ( def->hasWeakLinkage() || def->hasLinkOnceLinkage() ) {
Dale Johannesened1ec3a2008-05-23 00:15:10 +0000364 attr |= LTO_SYMBOL_DEFINITION_WEAK;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000365 }
Dale Johannesen6a6f2dd2008-05-16 22:46:40 +0000366 else if ( def->hasCommonLinkage()) {
367 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
368 }
Nick Kledzik77595fc2008-02-26 20:26:43 +0000369 else {
370 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
371 }
372
373 // set scope part
374 if ( def->hasHiddenVisibility() )
375 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
Nick Lewycky4fd40e82008-11-29 22:49:59 +0000376 else if ( def->hasProtectedVisibility() )
377 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
Devang Patelf0d286b2008-07-15 00:00:11 +0000378 else if ( def->hasExternalLinkage() || def->hasWeakLinkage()
Nick Kledzikdb6535d2008-07-19 00:58:07 +0000379 || def->hasLinkOnceLinkage() || def->hasCommonLinkage() )
Nick Kledzik77595fc2008-02-26 20:26:43 +0000380 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
381 else
382 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
383
384 // add to table of symbols
385 NameAndAttributes info;
386 info.name = symbolName;
387 info.attributes = (lto_symbol_attributes)attr;
388 _symbols.push_back(info);
389 _defines[info.name] = 1;
390}
391
Devang Patelc2aec572008-07-16 18:06:52 +0000392void LTOModule::addAsmGlobalSymbol(const char *name) {
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000393 // only add new define if not already defined
Daniel Dunbar6316fbc2009-07-23 18:17:34 +0000394 if ( _defines.count(name) == 0 )
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000395 return;
396
397 // string is owned by _defines
398 const char *symbolName = ::strdup(name);
399 uint32_t attr = LTO_SYMBOL_DEFINITION_REGULAR;
400 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
401 NameAndAttributes info;
402 info.name = symbolName;
403 info.attributes = (lto_symbol_attributes)attr;
404 _symbols.push_back(info);
405 _defines[info.name] = 1;
Devang Patelc2aec572008-07-16 18:06:52 +0000406}
Nick Kledzik77595fc2008-02-26 20:26:43 +0000407
Nick Kledzikef194ed2008-02-27 22:25:36 +0000408void LTOModule::addPotentialUndefinedSymbol(GlobalValue* decl, Mangler &mangler)
409{
Nick Kledzik77595fc2008-02-26 20:26:43 +0000410 // ignore all llvm.* symbols
Daniel Dunbar460f6562009-07-26 09:48:23 +0000411 if (decl->getName().startswith("llvm."))
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000412 return;
413
Nick Lewycky485ded02009-07-09 06:03:04 +0000414 // ignore all aliases
415 if (isa<GlobalAlias>(decl))
416 return;
417
Nick Lewyckydb1e9982009-07-28 06:53:50 +0000418 std::string name = mangler.getMangledName(decl);
Rafael Espindola7431af02009-04-24 16:55:21 +0000419
420 // we already have the symbol
421 if (_undefines.find(name) != _undefines.end())
422 return;
423
424 NameAndAttributes info;
425 // string is owned by _undefines
Nick Lewyckydb1e9982009-07-28 06:53:50 +0000426 info.name = ::strdup(name.c_str());
Rafael Espindola7431af02009-04-24 16:55:21 +0000427 if (decl->hasExternalWeakLinkage())
428 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
429 else
430 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
431 _undefines[name] = info;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000432}
433
434
435
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000436// Find external symbols referenced by VALUE. This is a recursive function.
Nick Kledzik77595fc2008-02-26 20:26:43 +0000437void LTOModule::findExternalRefs(Value* value, Mangler &mangler) {
438
439 if (GlobalValue* gv = dyn_cast<GlobalValue>(value)) {
440 if ( !gv->hasExternalLinkage() )
Nick Kledzikef194ed2008-02-27 22:25:36 +0000441 addPotentialUndefinedSymbol(gv, mangler);
Nick Kledziked185d62008-05-28 00:06:14 +0000442 // If this is a variable definition, do not recursively process
443 // initializer. It might contain a reference to this variable
444 // and cause an infinite loop. The initializer will be
445 // processed in addDefinedDataSymbol().
446 return;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000447 }
Nick Kledziked185d62008-05-28 00:06:14 +0000448
Nick Kledzik77595fc2008-02-26 20:26:43 +0000449 // GlobalValue, even with InternalLinkage type, may have operands with
450 // ExternalLinkage type. Do not ignore these operands.
451 if (Constant* c = dyn_cast<Constant>(value)) {
452 // Handle ConstantExpr, ConstantStruct, ConstantArry etc..
453 for (unsigned i = 0, e = c->getNumOperands(); i != e; ++i)
454 findExternalRefs(c->getOperand(i), mangler);
455 }
456}
457
Nick Kledzikef194ed2008-02-27 22:25:36 +0000458void LTOModule::lazyParseSymbols()
Nick Kledzik77595fc2008-02-26 20:26:43 +0000459{
460 if ( !_symbolsParsed ) {
461 _symbolsParsed = true;
462
463 // Use mangler to add GlobalPrefix to names to match linker names.
464 Mangler mangler(*_module, _target->getTargetAsmInfo()->getGlobalPrefix());
Nick Kledzik3eb445f2009-06-01 20:33:09 +0000465 // add chars used in ObjC method names so method names aren't mangled
466 mangler.markCharAcceptable('[');
467 mangler.markCharAcceptable(']');
468 mangler.markCharAcceptable('(');
469 mangler.markCharAcceptable(')');
470 mangler.markCharAcceptable('-');
471 mangler.markCharAcceptable('+');
472 mangler.markCharAcceptable(' ');
Nick Kledzik77595fc2008-02-26 20:26:43 +0000473
474 // add functions
475 for (Module::iterator f = _module->begin(); f != _module->end(); ++f) {
Nick Kledzikef194ed2008-02-27 22:25:36 +0000476 if ( f->isDeclaration() )
477 addPotentialUndefinedSymbol(f, mangler);
478 else
479 addDefinedFunctionSymbol(f, mangler);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000480 }
481
482 // add data
483 for (Module::global_iterator v = _module->global_begin(),
484 e = _module->global_end(); v != e; ++v) {
Nick Kledzikef194ed2008-02-27 22:25:36 +0000485 if ( v->isDeclaration() )
486 addPotentialUndefinedSymbol(v, mangler);
487 else
488 addDefinedDataSymbol(v, mangler);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000489 }
490
Devang Patelc2aec572008-07-16 18:06:52 +0000491 // add asm globals
492 const std::string &inlineAsm = _module->getModuleInlineAsm();
493 const std::string glbl = ".globl";
494 std::string asmSymbolName;
495 std::string::size_type pos = inlineAsm.find(glbl, 0);
496 while (pos != std::string::npos) {
497 // eat .globl
498 pos = pos + 6;
499
500 // skip white space between .globl and symbol name
501 std::string::size_type pbegin = inlineAsm.find_first_not_of(' ', pos);
502 if (pbegin == std::string::npos)
503 break;
504
505 // find end-of-line
506 std::string::size_type pend = inlineAsm.find_first_of('\n', pbegin);
507 if (pend == std::string::npos)
508 break;
509
Devang Patel41390702008-07-16 19:49:09 +0000510 asmSymbolName.assign(inlineAsm, pbegin, pend - pbegin);
Devang Patelc2aec572008-07-16 18:06:52 +0000511 addAsmGlobalSymbol(asmSymbolName.c_str());
512
513 // search next .globl
514 pos = inlineAsm.find(glbl, pend);
515 }
516
Nick Kledzik77595fc2008-02-26 20:26:43 +0000517 // make symbols for all undefines
Rafael Espindola7431af02009-04-24 16:55:21 +0000518 for (StringMap<NameAndAttributes>::iterator it=_undefines.begin();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000519 it != _undefines.end(); ++it) {
520 // if this symbol also has a definition, then don't make an undefine
521 // because it is a tentative definition
Nick Lewyckyd42b58b2009-07-26 22:16:39 +0000522 if ( _defines.count(it->getKey()) == 0 ) {
Rafael Espindola7431af02009-04-24 16:55:21 +0000523 NameAndAttributes info = it->getValue();
524 _symbols.push_back(info);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000525 }
526 }
Nick Kledzikef194ed2008-02-27 22:25:36 +0000527 }
528}
529
530
531uint32_t LTOModule::getSymbolCount()
532{
533 lazyParseSymbols();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000534 return _symbols.size();
535}
536
537
538lto_symbol_attributes LTOModule::getSymbolAttributes(uint32_t index)
539{
Nick Kledzikef194ed2008-02-27 22:25:36 +0000540 lazyParseSymbols();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000541 if ( index < _symbols.size() )
542 return _symbols[index].attributes;
543 else
544 return lto_symbol_attributes(0);
545}
546
547const char* LTOModule::getSymbolName(uint32_t index)
548{
Nick Kledzikef194ed2008-02-27 22:25:36 +0000549 lazyParseSymbols();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000550 if ( index < _symbols.size() )
551 return _symbols[index].name;
552 else
553 return NULL;
554}