ValueEnumerator.cpp revision 9937d116e09feb32d46a4c76eca1be6afcd3bed5
1//===-- ValueEnumerator.cpp - Number values and types for bitcode writer --===// 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 ValueEnumerator class. 11// 12//===----------------------------------------------------------------------===// 13 14#include "ValueEnumerator.h" 15#include "llvm/ADT/SmallPtrSet.h" 16#include "llvm/ADT/STLExtras.h" 17#include "llvm/Constants.h" 18#include "llvm/DerivedTypes.h" 19#include "llvm/Module.h" 20#include "llvm/ValueSymbolTable.h" 21#include "llvm/Instructions.h" 22#include <algorithm> 23using namespace llvm; 24 25namespace llvm_2_9 { 26 27static bool isIntegerValue(const std::pair<const Value*, unsigned> &V) { 28 return V.first->getType()->isIntegerTy(); 29} 30 31/// ValueEnumerator - Enumerate module-level information. 32ValueEnumerator::ValueEnumerator(const Module *M) { 33 // Enumerate the global variables. 34 for (Module::const_global_iterator I = M->global_begin(), 35 E = M->global_end(); I != E; ++I) 36 EnumerateValue(I); 37 38 // Enumerate the functions. 39 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) { 40 EnumerateValue(I); 41 EnumerateAttributes(cast<Function>(I)->getAttributes()); 42 } 43 44 // Enumerate the aliases. 45 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end(); 46 I != E; ++I) 47 EnumerateValue(I); 48 49 // Remember what is the cutoff between globalvalue's and other constants. 50 unsigned FirstConstant = Values.size(); 51 52 // Enumerate the global variable initializers. 53 for (Module::const_global_iterator I = M->global_begin(), 54 E = M->global_end(); I != E; ++I) 55 if (I->hasInitializer()) 56 EnumerateValue(I->getInitializer()); 57 58 // Enumerate the aliasees. 59 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end(); 60 I != E; ++I) 61 EnumerateValue(I->getAliasee()); 62 63 // Insert constants and metadata that are named at module level into the slot 64 // pool so that the module symbol table can refer to them... 65 EnumerateValueSymbolTable(M->getValueSymbolTable()); 66 EnumerateNamedMetadata(M); 67 68 SmallVector<std::pair<unsigned, MDNode*>, 8> MDs; 69 70 // Enumerate types used by function bodies and argument lists. 71 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) { 72 73 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); 74 I != E; ++I) 75 EnumerateType(I->getType()); 76 77 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) 78 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){ 79 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end(); 80 OI != E; ++OI) { 81 if (MDNode *MD = dyn_cast<MDNode>(*OI)) 82 if (MD->isFunctionLocal() && MD->getFunction()) 83 // These will get enumerated during function-incorporation. 84 continue; 85 EnumerateOperandType(*OI); 86 } 87 EnumerateType(I->getType()); 88 if (const CallInst *CI = dyn_cast<CallInst>(I)) 89 EnumerateAttributes(CI->getAttributes()); 90 else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) 91 EnumerateAttributes(II->getAttributes()); 92 93 // Enumerate metadata attached with this instruction. 94 MDs.clear(); 95 I->getAllMetadataOtherThanDebugLoc(MDs); 96 for (unsigned i = 0, e = MDs.size(); i != e; ++i) 97 EnumerateMetadata(MDs[i].second); 98 99 if (!I->getDebugLoc().isUnknown()) { 100 MDNode *Scope, *IA; 101 I->getDebugLoc().getScopeAndInlinedAt(Scope, IA, I->getContext()); 102 if (Scope) EnumerateMetadata(Scope); 103 if (IA) EnumerateMetadata(IA); 104 } 105 } 106 } 107 108 // Optimize constant ordering. 109 OptimizeConstants(FirstConstant, Values.size()); 110} 111 112 113unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const { 114 InstructionMapType::const_iterator I = InstructionMap.find(Inst); 115 assert(I != InstructionMap.end() && "Instruction is not mapped!"); 116 return I->second; 117} 118 119void ValueEnumerator::setInstructionID(const Instruction *I) { 120 InstructionMap[I] = InstructionCount++; 121} 122 123unsigned ValueEnumerator::getValueID(const Value *V) const { 124 if (isa<MDNode>(V) || isa<MDString>(V)) { 125 ValueMapType::const_iterator I = MDValueMap.find(V); 126 assert(I != MDValueMap.end() && "Value not in slotcalculator!"); 127 return I->second-1; 128 } 129 130 ValueMapType::const_iterator I = ValueMap.find(V); 131 assert(I != ValueMap.end() && "Value not in slotcalculator!"); 132 return I->second-1; 133} 134 135// Optimize constant ordering. 136namespace { 137 struct CstSortPredicate { 138 ValueEnumerator &VE; 139 explicit CstSortPredicate(ValueEnumerator &ve) : VE(ve) {} 140 bool operator()(const std::pair<const Value*, unsigned> &LHS, 141 const std::pair<const Value*, unsigned> &RHS) { 142 // Sort by plane. 143 if (LHS.first->getType() != RHS.first->getType()) 144 return VE.getTypeID(LHS.first->getType()) < 145 VE.getTypeID(RHS.first->getType()); 146 // Then by frequency. 147 return LHS.second > RHS.second; 148 } 149 }; 150} 151 152/// OptimizeConstants - Reorder constant pool for denser encoding. 153void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) { 154 if (CstStart == CstEnd || CstStart+1 == CstEnd) return; 155 156 CstSortPredicate P(*this); 157 std::stable_sort(Values.begin()+CstStart, Values.begin()+CstEnd, P); 158 159 // Ensure that integer constants are at the start of the constant pool. This 160 // is important so that GEP structure indices come before gep constant exprs. 161 std::partition(Values.begin()+CstStart, Values.begin()+CstEnd, 162 isIntegerValue); 163 164 // Rebuild the modified portion of ValueMap. 165 for (; CstStart != CstEnd; ++CstStart) 166 ValueMap[Values[CstStart].first] = CstStart+1; 167} 168 169 170/// EnumerateValueSymbolTable - Insert all of the values in the specified symbol 171/// table into the values table. 172void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) { 173 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end(); 174 VI != VE; ++VI) 175 EnumerateValue(VI->getValue()); 176} 177 178/// EnumerateNamedMetadata - Insert all of the values referenced by 179/// named metadata in the specified module. 180void ValueEnumerator::EnumerateNamedMetadata(const Module *M) { 181 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(), 182 E = M->named_metadata_end(); I != E; ++I) 183 EnumerateNamedMDNode(I); 184} 185 186void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) { 187 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) 188 EnumerateMetadata(MD->getOperand(i)); 189} 190 191/// EnumerateMDNodeOperands - Enumerate all non-function-local values 192/// and types referenced by the given MDNode. 193void ValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) { 194 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 195 if (Value *V = N->getOperand(i)) { 196 if (isa<MDNode>(V) || isa<MDString>(V)) 197 EnumerateMetadata(V); 198 else if (!isa<Instruction>(V) && !isa<Argument>(V)) 199 EnumerateValue(V); 200 } else 201 EnumerateType(Type::getVoidTy(N->getContext())); 202 } 203} 204 205void ValueEnumerator::EnumerateMetadata(const Value *MD) { 206 assert((isa<MDNode>(MD) || isa<MDString>(MD)) && "Invalid metadata kind"); 207 208 // Enumerate the type of this value. 209 EnumerateType(MD->getType()); 210 211 const MDNode *N = dyn_cast<MDNode>(MD); 212 213 // In the module-level pass, skip function-local nodes themselves, but 214 // do walk their operands. 215 if (N && N->isFunctionLocal() && N->getFunction()) { 216 EnumerateMDNodeOperands(N); 217 return; 218 } 219 220 // Check to see if it's already in! 221 unsigned &MDValueID = MDValueMap[MD]; 222 if (MDValueID) { 223 // Increment use count. 224 MDValues[MDValueID-1].second++; 225 return; 226 } 227 MDValues.push_back(std::make_pair(MD, 1U)); 228 MDValueID = MDValues.size(); 229 230 // Enumerate all non-function-local operands. 231 if (N) 232 EnumerateMDNodeOperands(N); 233} 234 235/// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata 236/// information reachable from the given MDNode. 237void ValueEnumerator::EnumerateFunctionLocalMetadata(const MDNode *N) { 238 assert(N->isFunctionLocal() && N->getFunction() && 239 "EnumerateFunctionLocalMetadata called on non-function-local mdnode!"); 240 241 // Enumerate the type of this value. 242 EnumerateType(N->getType()); 243 244 // Check to see if it's already in! 245 unsigned &MDValueID = MDValueMap[N]; 246 if (MDValueID) { 247 // Increment use count. 248 MDValues[MDValueID-1].second++; 249 return; 250 } 251 MDValues.push_back(std::make_pair(N, 1U)); 252 MDValueID = MDValues.size(); 253 254 // To incoroporate function-local information visit all function-local 255 // MDNodes and all function-local values they reference. 256 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) 257 if (Value *V = N->getOperand(i)) { 258 if (MDNode *O = dyn_cast<MDNode>(V)) { 259 if (O->isFunctionLocal() && O->getFunction()) 260 EnumerateFunctionLocalMetadata(O); 261 } else if (isa<Instruction>(V) || isa<Argument>(V)) 262 EnumerateValue(V); 263 } 264 265 // Also, collect all function-local MDNodes for easy access. 266 FunctionLocalMDs.push_back(N); 267} 268 269void ValueEnumerator::EnumerateValue(const Value *V) { 270 assert(!V->getType()->isVoidTy() && "Can't insert void values!"); 271 assert(!isa<MDNode>(V) && !isa<MDString>(V) && 272 "EnumerateValue doesn't handle Metadata!"); 273 274 // Check to see if it's already in! 275 unsigned &ValueID = ValueMap[V]; 276 if (ValueID) { 277 // Increment use count. 278 Values[ValueID-1].second++; 279 return; 280 } 281 282 // Enumerate the type of this value. 283 EnumerateType(V->getType()); 284 285 if (const Constant *C = dyn_cast<Constant>(V)) { 286 if (isa<GlobalValue>(C)) { 287 // Initializers for globals are handled explicitly elsewhere. 288 //} else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) { 289 // Do not enumerate the initializers for an array of simple characters. 290 // The initializers just pollute the value table, and we emit the strings 291 // specially. 292 } else if (C->getNumOperands()) { 293 // If a constant has operands, enumerate them. This makes sure that if a 294 // constant has uses (for example an array of const ints), that they are 295 // inserted also. 296 297 // We prefer to enumerate them with values before we enumerate the user 298 // itself. This makes it more likely that we can avoid forward references 299 // in the reader. We know that there can be no cycles in the constants 300 // graph that don't go through a global variable. 301 for (User::const_op_iterator I = C->op_begin(), E = C->op_end(); 302 I != E; ++I) 303 if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress. 304 EnumerateValue(*I); 305 306 // Finally, add the value. Doing this could make the ValueID reference be 307 // dangling, don't reuse it. 308 Values.push_back(std::make_pair(V, 1U)); 309 ValueMap[V] = Values.size(); 310 return; 311 } 312 } 313 314 // Add the value. 315 Values.push_back(std::make_pair(V, 1U)); 316 ValueID = Values.size(); 317} 318 319 320void ValueEnumerator::EnumerateType(Type *Ty) { 321 unsigned *TypeID = &TypeMap[Ty]; 322 323 // We've already seen this type. 324 if (*TypeID) 325 return; 326 327 // If it is a non-anonymous struct, mark the type as being visited so that we 328 // don't recursively visit it. This is safe because we allow forward 329 // references of these in the bitcode reader. 330 if (StructType *STy = dyn_cast<StructType>(Ty)) 331 if (!STy->isLiteral()) 332 *TypeID = ~0U; 333 334 // Enumerate all of the subtypes before we enumerate this type. This ensures 335 // that the type will be enumerated in an order that can be directly built. 336 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end(); 337 I != E; ++I) 338 EnumerateType(*I); 339 340 // Refresh the TypeID pointer in case the table rehashed. 341 TypeID = &TypeMap[Ty]; 342 343 // Check to see if we got the pointer another way. This can happen when 344 // enumerating recursive types that hit the base case deeper than they start. 345 // 346 // If this is actually a struct that we are treating as forward ref'able, 347 // then emit the definition now that all of its contents are available. 348 if (*TypeID && *TypeID != ~0U) 349 return; 350 351 // Add this type now that its contents are all happily enumerated. 352 Types.push_back(Ty); 353 354 *TypeID = Types.size(); 355} 356 357// Enumerate the types for the specified value. If the value is a constant, 358// walk through it, enumerating the types of the constant. 359void ValueEnumerator::EnumerateOperandType(const Value *V) { 360 EnumerateType(V->getType()); 361 362 if (const Constant *C = dyn_cast<Constant>(V)) { 363 // If this constant is already enumerated, ignore it, we know its type must 364 // be enumerated. 365 if (ValueMap.count(V)) return; 366 367 // This constant may have operands, make sure to enumerate the types in 368 // them. 369 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) { 370 const Value *Op = C->getOperand(i); 371 372 // Don't enumerate basic blocks here, this happens as operands to 373 // blockaddress. 374 if (isa<BasicBlock>(Op)) continue; 375 376 EnumerateOperandType(Op); 377 } 378 379 if (const MDNode *N = dyn_cast<MDNode>(V)) { 380 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) 381 if (Value *Elem = N->getOperand(i)) 382 EnumerateOperandType(Elem); 383 } 384 } else if (isa<MDString>(V) || isa<MDNode>(V)) 385 EnumerateMetadata(V); 386} 387 388void ValueEnumerator::EnumerateAttributes(const AttrListPtr &PAL) { 389 if (PAL.isEmpty()) return; // null is always 0. 390 // Do a lookup. 391 unsigned &Entry = AttributeMap[PAL.getRawPointer()]; 392 if (Entry == 0) { 393 // Never saw this before, add it. 394 Attributes.push_back(PAL); 395 Entry = Attributes.size(); 396 } 397} 398 399void ValueEnumerator::incorporateFunction(const Function &F) { 400 InstructionCount = 0; 401 NumModuleValues = Values.size(); 402 NumModuleMDValues = MDValues.size(); 403 404 // Adding function arguments to the value table. 405 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); 406 I != E; ++I) 407 EnumerateValue(I); 408 409 FirstFuncConstantID = Values.size(); 410 411 // Add all function-level constants to the value table. 412 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { 413 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) 414 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end(); 415 OI != E; ++OI) { 416 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) || 417 isa<InlineAsm>(*OI)) 418 EnumerateValue(*OI); 419 } 420 BasicBlocks.push_back(BB); 421 ValueMap[BB] = BasicBlocks.size(); 422 } 423 424 // Optimize the constant layout. 425 OptimizeConstants(FirstFuncConstantID, Values.size()); 426 427 // Add the function's parameter attributes so they are available for use in 428 // the function's instruction. 429 EnumerateAttributes(F.getAttributes()); 430 431 FirstInstID = Values.size(); 432 433 SmallVector<MDNode *, 8> FnLocalMDVector; 434 // Add all of the instructions. 435 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { 436 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) { 437 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end(); 438 OI != E; ++OI) { 439 if (MDNode *MD = dyn_cast<MDNode>(*OI)) 440 if (MD->isFunctionLocal() && MD->getFunction()) 441 // Enumerate metadata after the instructions they might refer to. 442 FnLocalMDVector.push_back(MD); 443 } 444 445 SmallVector<std::pair<unsigned, MDNode*>, 8> MDs; 446 I->getAllMetadataOtherThanDebugLoc(MDs); 447 for (unsigned i = 0, e = MDs.size(); i != e; ++i) { 448 MDNode *N = MDs[i].second; 449 if (N->isFunctionLocal() && N->getFunction()) 450 FnLocalMDVector.push_back(N); 451 } 452 453 if (!I->getType()->isVoidTy()) 454 EnumerateValue(I); 455 } 456 } 457 458 // Add all of the function-local metadata. 459 for (unsigned i = 0, e = FnLocalMDVector.size(); i != e; ++i) 460 EnumerateFunctionLocalMetadata(FnLocalMDVector[i]); 461} 462 463void ValueEnumerator::purgeFunction() { 464 /// Remove purged values from the ValueMap. 465 for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i) 466 ValueMap.erase(Values[i].first); 467 for (unsigned i = NumModuleMDValues, e = MDValues.size(); i != e; ++i) 468 MDValueMap.erase(MDValues[i].first); 469 for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i) 470 ValueMap.erase(BasicBlocks[i]); 471 472 Values.resize(NumModuleValues); 473 MDValues.resize(NumModuleMDValues); 474 BasicBlocks.clear(); 475 FunctionLocalMDs.clear(); 476} 477 478static void IncorporateFunctionInfoGlobalBBIDs(const Function *F, 479 DenseMap<const BasicBlock*, unsigned> &IDMap) { 480 unsigned Counter = 0; 481 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) 482 IDMap[BB] = ++Counter; 483} 484 485/// getGlobalBasicBlockID - This returns the function-specific ID for the 486/// specified basic block. This is relatively expensive information, so it 487/// should only be used by rare constructs such as address-of-label. 488unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const { 489 unsigned &Idx = GlobalBasicBlockIDs[BB]; 490 if (Idx != 0) 491 return Idx-1; 492 493 IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs); 494 return getGlobalBasicBlockID(BB); 495} 496 497} // end llvm_2_9 namespace 498 499