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 | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 18 | #include "llvm/ModuleProvider.h" |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/OwningPtr.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 20 | #include "llvm/Bitcode/ReaderWriter.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 21 | #include "llvm/Support/SystemUtils.h" |
| 22 | #include "llvm/Support/Mangler.h" |
| 23 | #include "llvm/Support/MemoryBuffer.h" |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MathExtras.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 25 | #include "llvm/System/Path.h" |
Nick Kledzik | 90dcff7 | 2008-05-09 01:09:59 +0000 | [diff] [blame] | 26 | #include "llvm/System/Process.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetMachine.h" |
| 28 | #include "llvm/Target/TargetMachineRegistry.h" |
| 29 | #include "llvm/Target/TargetAsmInfo.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 30 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 31 | |
| 32 | #include <fstream> |
| 33 | |
| 34 | using namespace llvm; |
| 35 | |
| 36 | bool LTOModule::isBitcodeFile(const void* mem, size_t length) |
| 37 | { |
| 38 | return ( llvm::sys::IdentifyFileType((char*)mem, length) |
| 39 | == llvm::sys::Bitcode_FileType ); |
| 40 | } |
| 41 | |
| 42 | bool LTOModule::isBitcodeFile(const char* path) |
| 43 | { |
| 44 | return llvm::sys::Path(path).isBitcodeFile(); |
| 45 | } |
| 46 | |
Chris Lattner | 038112a | 2008-04-01 18:04:03 +0000 | [diff] [blame] | 47 | bool LTOModule::isBitcodeFileForTarget(const void* mem, size_t length, |
| 48 | const char* triplePrefix) |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 49 | { |
Nick Kledzik | 90dcff7 | 2008-05-09 01:09:59 +0000 | [diff] [blame] | 50 | MemoryBuffer* buffer = makeBuffer(mem, length); |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 51 | if ( buffer == NULL ) |
| 52 | return false; |
| 53 | return isTargetMatch(buffer, triplePrefix); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 56 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 57 | bool LTOModule::isBitcodeFileForTarget(const char* path, |
Chris Lattner | 038112a | 2008-04-01 18:04:03 +0000 | [diff] [blame] | 58 | const char* triplePrefix) |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 59 | { |
Chris Lattner | 038112a | 2008-04-01 18:04:03 +0000 | [diff] [blame] | 60 | MemoryBuffer *buffer = MemoryBuffer::getFile(path); |
| 61 | if (buffer == NULL) |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 62 | return false; |
| 63 | return isTargetMatch(buffer, triplePrefix); |
| 64 | } |
| 65 | |
| 66 | // takes ownership of buffer |
| 67 | bool LTOModule::isTargetMatch(MemoryBuffer* buffer, const char* triplePrefix) |
| 68 | { |
| 69 | OwningPtr<ModuleProvider> mp(getBitcodeModuleProvider(buffer)); |
| 70 | // on success, mp owns buffer and both are deleted at end of this method |
| 71 | if ( !mp ) { |
| 72 | delete buffer; |
| 73 | return false; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 74 | } |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 75 | std::string actualTarget = mp->getModule()->getTargetTriple(); |
| 76 | return ( strncmp(actualTarget.c_str(), triplePrefix, |
| 77 | strlen(triplePrefix)) == 0); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | |
| 81 | LTOModule::LTOModule(Module* m, TargetMachine* t) |
| 82 | : _module(m), _target(t), _symbolsParsed(false) |
| 83 | { |
| 84 | } |
| 85 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 86 | LTOModule* LTOModule::makeLTOModule(const char* path, std::string& errMsg) |
| 87 | { |
Chris Lattner | 038112a | 2008-04-01 18:04:03 +0000 | [diff] [blame] | 88 | OwningPtr<MemoryBuffer> buffer(MemoryBuffer::getFile(path, &errMsg)); |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 89 | if ( !buffer ) |
| 90 | return NULL; |
| 91 | return makeLTOModule(buffer.get(), errMsg); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Nick Kledzik | 6b89d92 | 2008-05-09 18:44:41 +0000 | [diff] [blame] | 94 | /// makeBuffer - create a MemoryBuffer from a memory range. |
| 95 | /// MemoryBuffer requires the byte past end of the buffer to be a zero. |
| 96 | /// We might get lucky and already be that way, otherwise make a copy. |
| 97 | /// 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] | 98 | MemoryBuffer* LTOModule::makeBuffer(const void* mem, size_t length) |
| 99 | { |
Nick Kledzik | 90dcff7 | 2008-05-09 01:09:59 +0000 | [diff] [blame] | 100 | const char* startPtr = (char*)mem; |
| 101 | const char* endPtr = startPtr+length; |
| 102 | if ( (((uintptr_t)endPtr & (sys::Process::GetPageSize()-1)) == 0) |
| 103 | || (*endPtr != 0) ) |
| 104 | return MemoryBuffer::getMemBufferCopy(startPtr, endPtr); |
| 105 | else |
| 106 | return MemoryBuffer::getMemBuffer(startPtr, endPtr); |
| 107 | } |
| 108 | |
| 109 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 110 | LTOModule* LTOModule::makeLTOModule(const void* mem, size_t length, |
| 111 | std::string& errMsg) |
| 112 | { |
Nick Kledzik | 90dcff7 | 2008-05-09 01:09:59 +0000 | [diff] [blame] | 113 | OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length)); |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 114 | if ( !buffer ) |
| 115 | return NULL; |
| 116 | return makeLTOModule(buffer.get(), errMsg); |
| 117 | } |
| 118 | |
| 119 | LTOModule* LTOModule::makeLTOModule(MemoryBuffer* buffer, std::string& errMsg) |
| 120 | { |
| 121 | // parse bitcode buffer |
| 122 | OwningPtr<Module> m(ParseBitcodeFile(buffer, &errMsg)); |
| 123 | if ( !m ) |
| 124 | return NULL; |
| 125 | // find machine architecture for this module |
| 126 | const TargetMachineRegistry::entry* march = |
| 127 | TargetMachineRegistry::getClosestStaticTargetForModule(*m, errMsg); |
| 128 | if ( march == NULL ) |
| 129 | return NULL; |
| 130 | // construct LTModule, hand over ownership of module and target |
| 131 | std::string features; |
| 132 | TargetMachine* target = march->CtorFn(*m, features); |
| 133 | return new LTOModule(m.take(), target); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | |
| 137 | const char* LTOModule::getTargetTriple() |
| 138 | { |
| 139 | return _module->getTargetTriple().c_str(); |
| 140 | } |
| 141 | |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 142 | void LTOModule::addDefinedFunctionSymbol(Function* f, Mangler &mangler) |
| 143 | { |
| 144 | // add to list of defined symbols |
| 145 | addDefinedSymbol(f, mangler, true); |
| 146 | |
| 147 | // add external symbols referenced by this function. |
| 148 | for (Function::iterator b = f->begin(); b != f->end(); ++b) { |
| 149 | for (BasicBlock::iterator i = b->begin(); i != b->end(); ++i) { |
| 150 | for (unsigned count = 0, total = i->getNumOperands(); |
| 151 | count != total; ++count) { |
| 152 | findExternalRefs(i->getOperand(count), mangler); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | void LTOModule::addDefinedDataSymbol(GlobalValue* v, Mangler &mangler) |
| 159 | { |
| 160 | // add to list of defined symbols |
| 161 | addDefinedSymbol(v, mangler, false); |
| 162 | |
| 163 | // add external symbols referenced by this data. |
| 164 | for (unsigned count = 0, total = v->getNumOperands();\ |
| 165 | count != total; ++count) { |
| 166 | findExternalRefs(v->getOperand(count), mangler); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 171 | void LTOModule::addDefinedSymbol(GlobalValue* def, Mangler &mangler, |
| 172 | bool isFunction) |
| 173 | { |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 174 | // string is owned by _defines |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 175 | const char* symbolName = ::strdup(mangler.getValueName(def).c_str()); |
| 176 | |
| 177 | // set alignment part log2() can have rounding errors |
| 178 | uint32_t align = def->getAlignment(); |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 179 | uint32_t attr = align ? CountTrailingZeros_32(def->getAlignment()) : 0; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 180 | |
| 181 | // set permissions part |
| 182 | if ( isFunction ) |
| 183 | attr |= LTO_SYMBOL_PERMISSIONS_CODE; |
| 184 | else { |
| 185 | GlobalVariable* gv = dyn_cast<GlobalVariable>(def); |
| 186 | if ( (gv != NULL) && gv->isConstant() ) |
| 187 | attr |= LTO_SYMBOL_PERMISSIONS_RODATA; |
| 188 | else |
| 189 | attr |= LTO_SYMBOL_PERMISSIONS_DATA; |
| 190 | } |
| 191 | |
| 192 | // set definition part |
| 193 | if ( def->hasWeakLinkage() || def->hasLinkOnceLinkage() ) { |
| 194 | // lvm bitcode does not differenciate between weak def data |
| 195 | // and tentative definitions! |
| 196 | // HACK HACK HACK |
| 197 | // C++ does not use tentative definitions, but does use weak symbols |
| 198 | // so guess that anything that looks like a C++ symbol is weak and others |
| 199 | // are tentative definitions |
| 200 | if ( (strncmp(symbolName, "__Z", 3) == 0) ) |
| 201 | attr |= LTO_SYMBOL_DEFINITION_WEAK; |
| 202 | else { |
| 203 | attr |= LTO_SYMBOL_DEFINITION_TENTATIVE; |
| 204 | } |
| 205 | } |
| 206 | else { |
| 207 | attr |= LTO_SYMBOL_DEFINITION_REGULAR; |
| 208 | } |
| 209 | |
| 210 | // set scope part |
| 211 | if ( def->hasHiddenVisibility() ) |
| 212 | attr |= LTO_SYMBOL_SCOPE_HIDDEN; |
| 213 | else if ( def->hasExternalLinkage() || def->hasWeakLinkage() ) |
| 214 | attr |= LTO_SYMBOL_SCOPE_DEFAULT; |
| 215 | else |
| 216 | attr |= LTO_SYMBOL_SCOPE_INTERNAL; |
| 217 | |
| 218 | // add to table of symbols |
| 219 | NameAndAttributes info; |
| 220 | info.name = symbolName; |
| 221 | info.attributes = (lto_symbol_attributes)attr; |
| 222 | _symbols.push_back(info); |
| 223 | _defines[info.name] = 1; |
| 224 | } |
| 225 | |
| 226 | |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 227 | void LTOModule::addPotentialUndefinedSymbol(GlobalValue* decl, Mangler &mangler) |
| 228 | { |
| 229 | const char* name = mangler.getValueName(decl).c_str(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 230 | // ignore all llvm.* symbols |
| 231 | if ( strncmp(name, "llvm.", 5) != 0 ) { |
| 232 | _undefines[name] = 1; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | |
| 237 | |
| 238 | // Find exeternal symbols referenced by VALUE. This is a recursive function. |
| 239 | void LTOModule::findExternalRefs(Value* value, Mangler &mangler) { |
| 240 | |
| 241 | if (GlobalValue* gv = dyn_cast<GlobalValue>(value)) { |
| 242 | if ( !gv->hasExternalLinkage() ) |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 243 | addPotentialUndefinedSymbol(gv, mangler); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | // GlobalValue, even with InternalLinkage type, may have operands with |
| 247 | // ExternalLinkage type. Do not ignore these operands. |
| 248 | if (Constant* c = dyn_cast<Constant>(value)) { |
| 249 | // Handle ConstantExpr, ConstantStruct, ConstantArry etc.. |
| 250 | for (unsigned i = 0, e = c->getNumOperands(); i != e; ++i) |
| 251 | findExternalRefs(c->getOperand(i), mangler); |
| 252 | } |
| 253 | } |
| 254 | |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 255 | void LTOModule::lazyParseSymbols() |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 256 | { |
| 257 | if ( !_symbolsParsed ) { |
| 258 | _symbolsParsed = true; |
| 259 | |
| 260 | // Use mangler to add GlobalPrefix to names to match linker names. |
| 261 | Mangler mangler(*_module, _target->getTargetAsmInfo()->getGlobalPrefix()); |
| 262 | |
| 263 | // add functions |
| 264 | for (Module::iterator f = _module->begin(); f != _module->end(); ++f) { |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 265 | if ( f->isDeclaration() ) |
| 266 | addPotentialUndefinedSymbol(f, mangler); |
| 267 | else |
| 268 | addDefinedFunctionSymbol(f, mangler); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | // add data |
| 272 | for (Module::global_iterator v = _module->global_begin(), |
| 273 | e = _module->global_end(); v != e; ++v) { |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 274 | if ( v->isDeclaration() ) |
| 275 | addPotentialUndefinedSymbol(v, mangler); |
| 276 | else |
| 277 | addDefinedDataSymbol(v, mangler); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | // make symbols for all undefines |
| 281 | for (StringSet::iterator it=_undefines.begin(); |
| 282 | it != _undefines.end(); ++it) { |
| 283 | // if this symbol also has a definition, then don't make an undefine |
| 284 | // because it is a tentative definition |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 285 | if ( _defines.count(it->getKeyData(), it->getKeyData()+ |
| 286 | it->getKeyLength()) == 0 ) { |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 287 | NameAndAttributes info; |
| 288 | info.name = it->getKeyData(); |
| 289 | info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; |
| 290 | _symbols.push_back(info); |
| 291 | } |
| 292 | } |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
| 296 | |
| 297 | uint32_t LTOModule::getSymbolCount() |
| 298 | { |
| 299 | lazyParseSymbols(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 300 | return _symbols.size(); |
| 301 | } |
| 302 | |
| 303 | |
| 304 | lto_symbol_attributes LTOModule::getSymbolAttributes(uint32_t index) |
| 305 | { |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 306 | lazyParseSymbols(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 307 | if ( index < _symbols.size() ) |
| 308 | return _symbols[index].attributes; |
| 309 | else |
| 310 | return lto_symbol_attributes(0); |
| 311 | } |
| 312 | |
| 313 | const char* LTOModule::getSymbolName(uint32_t index) |
| 314 | { |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 315 | lazyParseSymbols(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 316 | if ( index < _symbols.size() ) |
| 317 | return _symbols[index].name; |
| 318 | else |
| 319 | return NULL; |
| 320 | } |
| 321 | |