Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 1 | //===-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 Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 15 | #include "LTOModule.h" |
| 16 | |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 17 | #include "llvm/Constants.h" |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 18 | #include "llvm/LLVMContext.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 19 | #include "llvm/Module.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 20 | #include "llvm/ModuleProvider.h" |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/OwningPtr.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 22 | #include "llvm/Bitcode/ReaderWriter.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 23 | #include "llvm/Support/SystemUtils.h" |
| 24 | #include "llvm/Support/Mangler.h" |
| 25 | #include "llvm/Support/MemoryBuffer.h" |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 26 | #include "llvm/Support/MathExtras.h" |
Daniel Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 27 | #include "llvm/System/Host.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 28 | #include "llvm/System/Path.h" |
Nick Kledzik | 90dcff7 | 2008-05-09 01:09:59 +0000 | [diff] [blame] | 29 | #include "llvm/System/Process.h" |
Bill Wendling | 604a818 | 2008-06-18 06:35:30 +0000 | [diff] [blame] | 30 | #include "llvm/Target/SubtargetFeature.h" |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 31 | #include "llvm/MC/MCAsmInfo.h" |
Daniel Dunbar | ff9834a | 2009-07-16 02:41:19 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetMachine.h" |
| 33 | #include "llvm/Target/TargetRegistry.h" |
Nick Lewycky | d42b58b | 2009-07-26 22:16:39 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetSelect.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 35 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 36 | using namespace llvm; |
| 37 | |
| 38 | bool LTOModule::isBitcodeFile(const void* mem, size_t length) |
| 39 | { |
| 40 | return ( llvm::sys::IdentifyFileType((char*)mem, length) |
| 41 | == llvm::sys::Bitcode_FileType ); |
| 42 | } |
| 43 | |
| 44 | bool LTOModule::isBitcodeFile(const char* path) |
| 45 | { |
| 46 | return llvm::sys::Path(path).isBitcodeFile(); |
| 47 | } |
| 48 | |
Chris Lattner | 038112a | 2008-04-01 18:04:03 +0000 | [diff] [blame] | 49 | bool LTOModule::isBitcodeFileForTarget(const void* mem, size_t length, |
| 50 | const char* triplePrefix) |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 51 | { |
Nick Kledzik | 90dcff7 | 2008-05-09 01:09:59 +0000 | [diff] [blame] | 52 | MemoryBuffer* buffer = makeBuffer(mem, length); |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 53 | if ( buffer == NULL ) |
| 54 | return false; |
| 55 | return isTargetMatch(buffer, triplePrefix); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 58 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 59 | bool LTOModule::isBitcodeFileForTarget(const char* path, |
Chris Lattner | 038112a | 2008-04-01 18:04:03 +0000 | [diff] [blame] | 60 | const char* triplePrefix) |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 61 | { |
Chris Lattner | 038112a | 2008-04-01 18:04:03 +0000 | [diff] [blame] | 62 | MemoryBuffer *buffer = MemoryBuffer::getFile(path); |
| 63 | if (buffer == NULL) |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 64 | return false; |
| 65 | return isTargetMatch(buffer, triplePrefix); |
| 66 | } |
| 67 | |
| 68 | // takes ownership of buffer |
| 69 | bool LTOModule::isTargetMatch(MemoryBuffer* buffer, const char* triplePrefix) |
| 70 | { |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 71 | OwningPtr<ModuleProvider> mp(getBitcodeModuleProvider(buffer, |
Owen Anderson | 0e7a546 | 2009-07-02 00:31:14 +0000 | [diff] [blame] | 72 | getGlobalContext())); |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 73 | // on success, mp owns buffer and both are deleted at end of this method |
| 74 | if ( !mp ) { |
| 75 | delete buffer; |
| 76 | return false; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 77 | } |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 78 | std::string actualTarget = mp->getModule()->getTargetTriple(); |
| 79 | return ( strncmp(actualTarget.c_str(), triplePrefix, |
| 80 | strlen(triplePrefix)) == 0); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | |
| 84 | LTOModule::LTOModule(Module* m, TargetMachine* t) |
| 85 | : _module(m), _target(t), _symbolsParsed(false) |
| 86 | { |
| 87 | } |
| 88 | |
Owen Anderson | 31895e7 | 2009-07-01 21:22:36 +0000 | [diff] [blame] | 89 | LTOModule* LTOModule::makeLTOModule(const char* path, |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 90 | std::string& errMsg) |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 91 | { |
Chris Lattner | 038112a | 2008-04-01 18:04:03 +0000 | [diff] [blame] | 92 | OwningPtr<MemoryBuffer> buffer(MemoryBuffer::getFile(path, &errMsg)); |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 93 | if ( !buffer ) |
| 94 | return NULL; |
Owen Anderson | 0e7a546 | 2009-07-02 00:31:14 +0000 | [diff] [blame] | 95 | return makeLTOModule(buffer.get(), errMsg); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Nick Kledzik | 6b89d92 | 2008-05-09 18:44:41 +0000 | [diff] [blame] | 98 | /// 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 Kledzik | 90dcff7 | 2008-05-09 01:09:59 +0000 | [diff] [blame] | 102 | MemoryBuffer* LTOModule::makeBuffer(const void* mem, size_t length) |
| 103 | { |
Nick Kledzik | ed185d6 | 2008-05-28 00:06:14 +0000 | [diff] [blame] | 104 | 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 Kledzik | 90dcff7 | 2008-05-09 01:09:59 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 114 | LTOModule* LTOModule::makeLTOModule(const void* mem, size_t length, |
Nick Lewycky | b454eab | 2009-02-06 07:01:00 +0000 | [diff] [blame] | 115 | std::string& errMsg) |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 116 | { |
Nick Kledzik | 90dcff7 | 2008-05-09 01:09:59 +0000 | [diff] [blame] | 117 | OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length)); |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 118 | if ( !buffer ) |
| 119 | return NULL; |
Owen Anderson | 0e7a546 | 2009-07-02 00:31:14 +0000 | [diff] [blame] | 120 | return makeLTOModule(buffer.get(), errMsg); |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Bill Wendling | e424254 | 2008-06-18 21:39:02 +0000 | [diff] [blame] | 123 | /// 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>. |
| 129 | std::string getFeatureString(const char *TargetTriple) { |
Nick Lewycky | d42b58b | 2009-07-26 22:16:39 +0000 | [diff] [blame] | 130 | InitializeAllTargets(); |
| 131 | |
Bill Wendling | e424254 | 2008-06-18 21:39:02 +0000 | [diff] [blame] | 132 | 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 Anderson | 31895e7 | 2009-07-01 21:22:36 +0000 | [diff] [blame] | 144 | LTOModule* LTOModule::makeLTOModule(MemoryBuffer* buffer, |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 145 | std::string& errMsg) |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 146 | { |
Nick Lewycky | d42b58b | 2009-07-26 22:16:39 +0000 | [diff] [blame] | 147 | InitializeAllTargets(); |
| 148 | |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 149 | // parse bitcode buffer |
Owen Anderson | 0e7a546 | 2009-07-02 00:31:14 +0000 | [diff] [blame] | 150 | OwningPtr<Module> m(ParseBitcodeFile(buffer, getGlobalContext(), &errMsg)); |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 151 | if ( !m ) |
| 152 | return NULL; |
Daniel Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 153 | |
| 154 | std::string Triple = m->getTargetTriple(); |
| 155 | if (Triple.empty()) |
| 156 | Triple = sys::getHostTriple(); |
| 157 | |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 158 | // find machine architecture for this module |
Daniel Dunbar | 4bd03ab | 2009-08-03 04:20:57 +0000 | [diff] [blame] | 159 | const Target* march = TargetRegistry::lookupTarget(Triple, errMsg); |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 160 | if ( march == NULL ) |
| 161 | return NULL; |
Bill Wendling | 604a818 | 2008-06-18 06:35:30 +0000 | [diff] [blame] | 162 | |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 163 | // construct LTModule, hand over ownership of module and target |
Daniel Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 164 | std::string FeatureStr = getFeatureString(Triple.c_str()); |
Daniel Dunbar | 4b3d572 | 2009-08-04 04:08:40 +0000 | [diff] [blame] | 165 | TargetMachine* target = march->createTargetMachine(Triple, FeatureStr); |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 166 | return new LTOModule(m.take(), target); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | |
| 170 | const char* LTOModule::getTargetTriple() |
| 171 | { |
| 172 | return _module->getTargetTriple().c_str(); |
| 173 | } |
| 174 | |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 175 | void LTOModule::addDefinedFunctionSymbol(Function* f, Mangler &mangler) |
| 176 | { |
| 177 | // add to list of defined symbols |
| 178 | addDefinedSymbol(f, mangler, true); |
| 179 | |
| 180 | // add external symbols referenced by this function. |
| 181 | for (Function::iterator b = f->begin(); b != f->end(); ++b) { |
| 182 | for (BasicBlock::iterator i = b->begin(); i != b->end(); ++i) { |
| 183 | for (unsigned count = 0, total = i->getNumOperands(); |
| 184 | count != total; ++count) { |
| 185 | findExternalRefs(i->getOperand(count), mangler); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 191 | // get string that data pointer points to |
| 192 | bool LTOModule::objcClassNameFromExpression(Constant* c, std::string& name) |
| 193 | { |
| 194 | if (ConstantExpr* ce = dyn_cast<ConstantExpr>(c)) { |
| 195 | Constant* op = ce->getOperand(0); |
| 196 | if (GlobalVariable* gvn = dyn_cast<GlobalVariable>(op)) { |
| 197 | Constant* cn = gvn->getInitializer(); |
| 198 | if (ConstantArray* ca = dyn_cast<ConstantArray>(cn)) { |
Owen Anderson | 1ca29d3 | 2009-07-13 21:27:19 +0000 | [diff] [blame] | 199 | if ( ca->isCString() ) { |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 200 | name = ".objc_class_name_" + ca->getAsString(); |
| 201 | return true; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | // parse i386/ppc ObjC class data structure |
| 210 | void LTOModule::addObjCClass(GlobalVariable* clgv) |
| 211 | { |
| 212 | if (ConstantStruct* c = dyn_cast<ConstantStruct>(clgv->getInitializer())) { |
| 213 | // second slot in __OBJC,__class is pointer to superclass name |
| 214 | std::string superclassName; |
| 215 | if ( objcClassNameFromExpression(c->getOperand(1), superclassName) ) { |
| 216 | NameAndAttributes info; |
| 217 | if ( _undefines.find(superclassName.c_str()) == _undefines.end() ) { |
| 218 | const char* symbolName = ::strdup(superclassName.c_str()); |
| 219 | info.name = ::strdup(symbolName); |
| 220 | info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; |
| 221 | // string is owned by _undefines |
| 222 | _undefines[info.name] = info; |
| 223 | } |
| 224 | } |
| 225 | // third slot in __OBJC,__class is pointer to class name |
| 226 | std::string className; |
| 227 | if ( objcClassNameFromExpression(c->getOperand(2), className) ) { |
| 228 | const char* symbolName = ::strdup(className.c_str()); |
| 229 | NameAndAttributes info; |
| 230 | info.name = symbolName; |
| 231 | info.attributes = (lto_symbol_attributes) |
| 232 | (LTO_SYMBOL_PERMISSIONS_DATA | |
| 233 | LTO_SYMBOL_DEFINITION_REGULAR | |
| 234 | LTO_SYMBOL_SCOPE_DEFAULT); |
| 235 | _symbols.push_back(info); |
| 236 | _defines[info.name] = 1; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | |
| 242 | // parse i386/ppc ObjC category data structure |
| 243 | void LTOModule::addObjCCategory(GlobalVariable* clgv) |
| 244 | { |
| 245 | if (ConstantStruct* c = dyn_cast<ConstantStruct>(clgv->getInitializer())) { |
| 246 | // second slot in __OBJC,__category is pointer to target class name |
| 247 | std::string targetclassName; |
| 248 | if ( objcClassNameFromExpression(c->getOperand(1), targetclassName) ) { |
| 249 | NameAndAttributes info; |
| 250 | if ( _undefines.find(targetclassName.c_str()) == _undefines.end() ){ |
| 251 | const char* symbolName = ::strdup(targetclassName.c_str()); |
| 252 | info.name = ::strdup(symbolName); |
| 253 | info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; |
| 254 | // string is owned by _undefines |
| 255 | _undefines[info.name] = info; |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | |
| 262 | // parse i386/ppc ObjC class list data structure |
| 263 | void LTOModule::addObjCClassRef(GlobalVariable* clgv) |
| 264 | { |
| 265 | std::string targetclassName; |
| 266 | if ( objcClassNameFromExpression(clgv->getInitializer(), targetclassName) ){ |
| 267 | NameAndAttributes info; |
| 268 | if ( _undefines.find(targetclassName.c_str()) == _undefines.end() ) { |
| 269 | const char* symbolName = ::strdup(targetclassName.c_str()); |
| 270 | info.name = ::strdup(symbolName); |
| 271 | info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; |
| 272 | // string is owned by _undefines |
| 273 | _undefines[info.name] = info; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | |
| 279 | void LTOModule::addDefinedDataSymbol(GlobalValue* v, Mangler& mangler) |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 280 | { |
| 281 | // add to list of defined symbols |
| 282 | addDefinedSymbol(v, mangler, false); |
| 283 | |
Nick Kledzik | 4bdf730 | 2009-06-01 23:41:09 +0000 | [diff] [blame] | 284 | // Special case i386/ppc ObjC data structures in magic sections: |
| 285 | // The issue is that the old ObjC object format did some strange |
| 286 | // contortions to avoid real linker symbols. For instance, the |
| 287 | // ObjC class data structure is allocated statically in the executable |
| 288 | // that defines that class. That data structures contains a pointer to |
| 289 | // its superclass. But instead of just initializing that part of the |
| 290 | // struct to the address of its superclass, and letting the static and |
| 291 | // dynamic linkers do the rest, the runtime works by having that field |
| 292 | // instead point to a C-string that is the name of the superclass. |
| 293 | // At runtime the objc initialization updates that pointer and sets |
| 294 | // it to point to the actual super class. As far as the linker |
| 295 | // knows it is just a pointer to a string. But then someone wanted the |
| 296 | // linker to issue errors at build time if the superclass was not found. |
| 297 | // So they figured out a way in mach-o object format to use an absolute |
| 298 | // symbols (.objc_class_name_Foo = 0) and a floating reference |
| 299 | // (.reference .objc_class_name_Bar) to cause the linker into erroring when |
| 300 | // a class was missing. |
| 301 | // The following synthesizes the implicit .objc_* symbols for the linker |
| 302 | // from the ObjC data structures generated by the front end. |
| 303 | if ( v->hasSection() /* && isTargetDarwin */ ) { |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 304 | // special case if this data blob is an ObjC class definition |
| 305 | if ( v->getSection().compare(0, 15, "__OBJC,__class,") == 0 ) { |
| 306 | if (GlobalVariable* gv = dyn_cast<GlobalVariable>(v)) { |
| 307 | addObjCClass(gv); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | // special case if this data blob is an ObjC category definition |
| 312 | else if ( v->getSection().compare(0, 18, "__OBJC,__category,") == 0 ) { |
| 313 | if (GlobalVariable* gv = dyn_cast<GlobalVariable>(v)) { |
| 314 | addObjCCategory(gv); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | // special case if this data blob is the list of referenced classes |
| 319 | else if ( v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0 ) { |
| 320 | if (GlobalVariable* gv = dyn_cast<GlobalVariable>(v)) { |
| 321 | addObjCClassRef(gv); |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 326 | // add external symbols referenced by this data. |
Nick Kledzik | 9178a65 | 2008-05-27 22:07:08 +0000 | [diff] [blame] | 327 | for (unsigned count = 0, total = v->getNumOperands(); |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 328 | count != total; ++count) { |
| 329 | findExternalRefs(v->getOperand(count), mangler); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 334 | void LTOModule::addDefinedSymbol(GlobalValue* def, Mangler &mangler, |
Nick Lewycky | 485ded0 | 2009-07-09 06:03:04 +0000 | [diff] [blame] | 335 | bool isFunction) |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 336 | { |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 337 | // ignore all llvm.* symbols |
Daniel Dunbar | 460f656 | 2009-07-26 09:48:23 +0000 | [diff] [blame] | 338 | if (def->getName().startswith("llvm.")) |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 339 | return; |
| 340 | |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 341 | // string is owned by _defines |
Chris Lattner | b8158ac | 2009-07-14 18:17:16 +0000 | [diff] [blame] | 342 | const char* symbolName = ::strdup(mangler.getMangledName(def).c_str()); |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 343 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 344 | // set alignment part log2() can have rounding errors |
| 345 | uint32_t align = def->getAlignment(); |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 346 | uint32_t attr = align ? CountTrailingZeros_32(def->getAlignment()) : 0; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 347 | |
| 348 | // set permissions part |
| 349 | if ( isFunction ) |
| 350 | attr |= LTO_SYMBOL_PERMISSIONS_CODE; |
| 351 | else { |
| 352 | GlobalVariable* gv = dyn_cast<GlobalVariable>(def); |
| 353 | if ( (gv != NULL) && gv->isConstant() ) |
| 354 | attr |= LTO_SYMBOL_PERMISSIONS_RODATA; |
| 355 | else |
| 356 | attr |= LTO_SYMBOL_PERMISSIONS_DATA; |
| 357 | } |
| 358 | |
| 359 | // set definition part |
| 360 | if ( def->hasWeakLinkage() || def->hasLinkOnceLinkage() ) { |
Dale Johannesen | ed1ec3a | 2008-05-23 00:15:10 +0000 | [diff] [blame] | 361 | attr |= LTO_SYMBOL_DEFINITION_WEAK; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 362 | } |
Dale Johannesen | 6a6f2dd | 2008-05-16 22:46:40 +0000 | [diff] [blame] | 363 | else if ( def->hasCommonLinkage()) { |
| 364 | attr |= LTO_SYMBOL_DEFINITION_TENTATIVE; |
| 365 | } |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 366 | else { |
| 367 | attr |= LTO_SYMBOL_DEFINITION_REGULAR; |
| 368 | } |
| 369 | |
| 370 | // set scope part |
| 371 | if ( def->hasHiddenVisibility() ) |
| 372 | attr |= LTO_SYMBOL_SCOPE_HIDDEN; |
Nick Lewycky | 4fd40e8 | 2008-11-29 22:49:59 +0000 | [diff] [blame] | 373 | else if ( def->hasProtectedVisibility() ) |
| 374 | attr |= LTO_SYMBOL_SCOPE_PROTECTED; |
Devang Patel | f0d286b | 2008-07-15 00:00:11 +0000 | [diff] [blame] | 375 | else if ( def->hasExternalLinkage() || def->hasWeakLinkage() |
Nick Kledzik | db6535d | 2008-07-19 00:58:07 +0000 | [diff] [blame] | 376 | || def->hasLinkOnceLinkage() || def->hasCommonLinkage() ) |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 377 | attr |= LTO_SYMBOL_SCOPE_DEFAULT; |
| 378 | else |
| 379 | attr |= LTO_SYMBOL_SCOPE_INTERNAL; |
| 380 | |
| 381 | // add to table of symbols |
| 382 | NameAndAttributes info; |
| 383 | info.name = symbolName; |
| 384 | info.attributes = (lto_symbol_attributes)attr; |
| 385 | _symbols.push_back(info); |
| 386 | _defines[info.name] = 1; |
| 387 | } |
| 388 | |
Devang Patel | c2aec57 | 2008-07-16 18:06:52 +0000 | [diff] [blame] | 389 | void LTOModule::addAsmGlobalSymbol(const char *name) { |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 390 | // only add new define if not already defined |
Daniel Dunbar | 6316fbc | 2009-07-23 18:17:34 +0000 | [diff] [blame] | 391 | if ( _defines.count(name) == 0 ) |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 392 | return; |
| 393 | |
| 394 | // string is owned by _defines |
| 395 | const char *symbolName = ::strdup(name); |
| 396 | uint32_t attr = LTO_SYMBOL_DEFINITION_REGULAR; |
| 397 | attr |= LTO_SYMBOL_SCOPE_DEFAULT; |
| 398 | NameAndAttributes info; |
| 399 | info.name = symbolName; |
| 400 | info.attributes = (lto_symbol_attributes)attr; |
| 401 | _symbols.push_back(info); |
| 402 | _defines[info.name] = 1; |
Devang Patel | c2aec57 | 2008-07-16 18:06:52 +0000 | [diff] [blame] | 403 | } |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 404 | |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 405 | void LTOModule::addPotentialUndefinedSymbol(GlobalValue* decl, Mangler &mangler) |
| 406 | { |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 407 | // ignore all llvm.* symbols |
Daniel Dunbar | 460f656 | 2009-07-26 09:48:23 +0000 | [diff] [blame] | 408 | if (decl->getName().startswith("llvm.")) |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 409 | return; |
| 410 | |
Nick Lewycky | 485ded0 | 2009-07-09 06:03:04 +0000 | [diff] [blame] | 411 | // ignore all aliases |
| 412 | if (isa<GlobalAlias>(decl)) |
| 413 | return; |
| 414 | |
Nick Lewycky | db1e998 | 2009-07-28 06:53:50 +0000 | [diff] [blame] | 415 | std::string name = mangler.getMangledName(decl); |
Rafael Espindola | 7431af0 | 2009-04-24 16:55:21 +0000 | [diff] [blame] | 416 | |
| 417 | // we already have the symbol |
| 418 | if (_undefines.find(name) != _undefines.end()) |
| 419 | return; |
| 420 | |
| 421 | NameAndAttributes info; |
| 422 | // string is owned by _undefines |
Nick Lewycky | db1e998 | 2009-07-28 06:53:50 +0000 | [diff] [blame] | 423 | info.name = ::strdup(name.c_str()); |
Rafael Espindola | 7431af0 | 2009-04-24 16:55:21 +0000 | [diff] [blame] | 424 | if (decl->hasExternalWeakLinkage()) |
| 425 | info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF; |
| 426 | else |
| 427 | info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; |
| 428 | _undefines[name] = info; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | |
| 432 | |
Nick Lewycky | d42b58b | 2009-07-26 22:16:39 +0000 | [diff] [blame] | 433 | // Find external symbols referenced by VALUE. This is a recursive function. |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 434 | void LTOModule::findExternalRefs(Value* value, Mangler &mangler) { |
| 435 | |
| 436 | if (GlobalValue* gv = dyn_cast<GlobalValue>(value)) { |
| 437 | if ( !gv->hasExternalLinkage() ) |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 438 | addPotentialUndefinedSymbol(gv, mangler); |
Nick Kledzik | ed185d6 | 2008-05-28 00:06:14 +0000 | [diff] [blame] | 439 | // If this is a variable definition, do not recursively process |
| 440 | // initializer. It might contain a reference to this variable |
| 441 | // and cause an infinite loop. The initializer will be |
| 442 | // processed in addDefinedDataSymbol(). |
| 443 | return; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 444 | } |
Nick Kledzik | ed185d6 | 2008-05-28 00:06:14 +0000 | [diff] [blame] | 445 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 446 | // GlobalValue, even with InternalLinkage type, may have operands with |
| 447 | // ExternalLinkage type. Do not ignore these operands. |
| 448 | if (Constant* c = dyn_cast<Constant>(value)) { |
| 449 | // Handle ConstantExpr, ConstantStruct, ConstantArry etc.. |
| 450 | for (unsigned i = 0, e = c->getNumOperands(); i != e; ++i) |
| 451 | findExternalRefs(c->getOperand(i), mangler); |
| 452 | } |
| 453 | } |
| 454 | |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 455 | void LTOModule::lazyParseSymbols() |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 456 | { |
| 457 | if ( !_symbolsParsed ) { |
| 458 | _symbolsParsed = true; |
| 459 | |
| 460 | // Use mangler to add GlobalPrefix to names to match linker names. |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 461 | Mangler mangler(*_module, _target->getMCAsmInfo()->getGlobalPrefix()); |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 462 | // add chars used in ObjC method names so method names aren't mangled |
| 463 | mangler.markCharAcceptable('['); |
| 464 | mangler.markCharAcceptable(']'); |
| 465 | mangler.markCharAcceptable('('); |
| 466 | mangler.markCharAcceptable(')'); |
| 467 | mangler.markCharAcceptable('-'); |
| 468 | mangler.markCharAcceptable('+'); |
| 469 | mangler.markCharAcceptable(' '); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 470 | |
| 471 | // add functions |
| 472 | for (Module::iterator f = _module->begin(); f != _module->end(); ++f) { |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 473 | if ( f->isDeclaration() ) |
| 474 | addPotentialUndefinedSymbol(f, mangler); |
| 475 | else |
| 476 | addDefinedFunctionSymbol(f, mangler); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | // add data |
| 480 | for (Module::global_iterator v = _module->global_begin(), |
| 481 | e = _module->global_end(); v != e; ++v) { |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 482 | if ( v->isDeclaration() ) |
| 483 | addPotentialUndefinedSymbol(v, mangler); |
| 484 | else |
| 485 | addDefinedDataSymbol(v, mangler); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Devang Patel | c2aec57 | 2008-07-16 18:06:52 +0000 | [diff] [blame] | 488 | // add asm globals |
| 489 | const std::string &inlineAsm = _module->getModuleInlineAsm(); |
| 490 | const std::string glbl = ".globl"; |
| 491 | std::string asmSymbolName; |
| 492 | std::string::size_type pos = inlineAsm.find(glbl, 0); |
| 493 | while (pos != std::string::npos) { |
| 494 | // eat .globl |
| 495 | pos = pos + 6; |
| 496 | |
| 497 | // skip white space between .globl and symbol name |
| 498 | std::string::size_type pbegin = inlineAsm.find_first_not_of(' ', pos); |
| 499 | if (pbegin == std::string::npos) |
| 500 | break; |
| 501 | |
| 502 | // find end-of-line |
| 503 | std::string::size_type pend = inlineAsm.find_first_of('\n', pbegin); |
| 504 | if (pend == std::string::npos) |
| 505 | break; |
| 506 | |
Devang Patel | 4139070 | 2008-07-16 19:49:09 +0000 | [diff] [blame] | 507 | asmSymbolName.assign(inlineAsm, pbegin, pend - pbegin); |
Devang Patel | c2aec57 | 2008-07-16 18:06:52 +0000 | [diff] [blame] | 508 | addAsmGlobalSymbol(asmSymbolName.c_str()); |
| 509 | |
| 510 | // search next .globl |
| 511 | pos = inlineAsm.find(glbl, pend); |
| 512 | } |
| 513 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 514 | // make symbols for all undefines |
Rafael Espindola | 7431af0 | 2009-04-24 16:55:21 +0000 | [diff] [blame] | 515 | for (StringMap<NameAndAttributes>::iterator it=_undefines.begin(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 516 | it != _undefines.end(); ++it) { |
| 517 | // if this symbol also has a definition, then don't make an undefine |
| 518 | // because it is a tentative definition |
Nick Lewycky | d42b58b | 2009-07-26 22:16:39 +0000 | [diff] [blame] | 519 | if ( _defines.count(it->getKey()) == 0 ) { |
Rafael Espindola | 7431af0 | 2009-04-24 16:55:21 +0000 | [diff] [blame] | 520 | NameAndAttributes info = it->getValue(); |
| 521 | _symbols.push_back(info); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 522 | } |
| 523 | } |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
| 527 | |
| 528 | uint32_t LTOModule::getSymbolCount() |
| 529 | { |
| 530 | lazyParseSymbols(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 531 | return _symbols.size(); |
| 532 | } |
| 533 | |
| 534 | |
| 535 | lto_symbol_attributes LTOModule::getSymbolAttributes(uint32_t index) |
| 536 | { |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 537 | lazyParseSymbols(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 538 | if ( index < _symbols.size() ) |
| 539 | return _symbols[index].attributes; |
| 540 | else |
| 541 | return lto_symbol_attributes(0); |
| 542 | } |
| 543 | |
| 544 | const char* LTOModule::getSymbolName(uint32_t index) |
| 545 | { |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 546 | lazyParseSymbols(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 547 | if ( index < _symbols.size() ) |
| 548 | return _symbols[index].name; |
| 549 | else |
| 550 | return NULL; |
| 551 | } |