LLVM API Documentation
00001 //===-- Value.cpp - Implement the Value class -----------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the Value, ValueHandle, and User classes. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "LLVMContextImpl.h" 00015 #include "llvm/Constant.h" 00016 #include "llvm/Constants.h" 00017 #include "llvm/DerivedTypes.h" 00018 #include "llvm/InstrTypes.h" 00019 #include "llvm/Instructions.h" 00020 #include "llvm/Operator.h" 00021 #include "llvm/Module.h" 00022 #include "llvm/ValueSymbolTable.h" 00023 #include "llvm/ADT/SmallString.h" 00024 #include "llvm/Support/Debug.h" 00025 #include "llvm/Support/GetElementPtrTypeIterator.h" 00026 #include "llvm/Support/ErrorHandling.h" 00027 #include "llvm/Support/LeakDetector.h" 00028 #include "llvm/Support/ManagedStatic.h" 00029 #include "llvm/Support/ValueHandle.h" 00030 #include "llvm/ADT/DenseMap.h" 00031 #include <algorithm> 00032 using namespace llvm; 00033 00034 //===----------------------------------------------------------------------===// 00035 // Value Class 00036 //===----------------------------------------------------------------------===// 00037 00038 static inline Type *checkType(Type *Ty) { 00039 assert(Ty && "Value defined with a null type: Error!"); 00040 return const_cast<Type*>(Ty); 00041 } 00042 00043 Value::Value(Type *ty, unsigned scid) 00044 : SubclassID(scid), HasValueHandle(0), 00045 SubclassOptionalData(0), SubclassData(0), VTy((Type*)checkType(ty)), 00046 UseList(0), Name(0) { 00047 // FIXME: Why isn't this in the subclass gunk?? 00048 if (isa<CallInst>(this) || isa<InvokeInst>(this)) 00049 assert((VTy->isFirstClassType() || VTy->isVoidTy() || VTy->isStructTy()) && 00050 "invalid CallInst type!"); 00051 else if (!isa<Constant>(this) && !isa<BasicBlock>(this)) 00052 assert((VTy->isFirstClassType() || VTy->isVoidTy()) && 00053 "Cannot create non-first-class values except for constants!"); 00054 } 00055 00056 Value::~Value() { 00057 // Notify all ValueHandles (if present) that this value is going away. 00058 if (HasValueHandle) 00059 ValueHandleBase::ValueIsDeleted(this); 00060 00061 #ifndef NDEBUG // Only in -g mode... 00062 // Check to make sure that there are no uses of this value that are still 00063 // around when the value is destroyed. If there are, then we have a dangling 00064 // reference and something is wrong. This code is here to print out what is 00065 // still being referenced. The value in question should be printed as 00066 // a <badref> 00067 // 00068 if (!use_empty()) { 00069 dbgs() << "While deleting: " << *VTy << " %" << getName() << "\n"; 00070 for (use_iterator I = use_begin(), E = use_end(); I != E; ++I) 00071 dbgs() << "Use still stuck around after Def is destroyed:" 00072 << **I << "\n"; 00073 } 00074 #endif 00075 assert(use_empty() && "Uses remain when a value is destroyed!"); 00076 00077 // If this value is named, destroy the name. This should not be in a symtab 00078 // at this point. 00079 if (Name && SubclassID != MDStringVal) 00080 Name->Destroy(); 00081 00082 // There should be no uses of this object anymore, remove it. 00083 LeakDetector::removeGarbageObject(this); 00084 } 00085 00086 /// hasNUses - Return true if this Value has exactly N users. 00087 /// 00088 bool Value::hasNUses(unsigned N) const { 00089 const_use_iterator UI = use_begin(), E = use_end(); 00090 00091 for (; N; --N, ++UI) 00092 if (UI == E) return false; // Too few. 00093 return UI == E; 00094 } 00095 00096 /// hasNUsesOrMore - Return true if this value has N users or more. This is 00097 /// logically equivalent to getNumUses() >= N. 00098 /// 00099 bool Value::hasNUsesOrMore(unsigned N) const { 00100 const_use_iterator UI = use_begin(), E = use_end(); 00101 00102 for (; N; --N, ++UI) 00103 if (UI == E) return false; // Too few. 00104 00105 return true; 00106 } 00107 00108 /// isUsedInBasicBlock - Return true if this value is used in the specified 00109 /// basic block. 00110 bool Value::isUsedInBasicBlock(const BasicBlock *BB) const { 00111 // Start by scanning over the instructions looking for a use before we start 00112 // the expensive use iteration. 00113 unsigned MaxBlockSize = 3; 00114 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) { 00115 if (std::find(I->op_begin(), I->op_end(), this) != I->op_end()) 00116 return true; 00117 if (MaxBlockSize-- == 0) // If the block is larger fall back to use_iterator 00118 break; 00119 } 00120 00121 if (MaxBlockSize != 0) // We scanned the entire block and found no use. 00122 return false; 00123 00124 for (const_use_iterator I = use_begin(), E = use_end(); I != E; ++I) { 00125 const Instruction *User = dyn_cast<Instruction>(*I); 00126 if (User && User->getParent() == BB) 00127 return true; 00128 } 00129 return false; 00130 } 00131 00132 00133 /// getNumUses - This method computes the number of uses of this Value. This 00134 /// is a linear time operation. Use hasOneUse or hasNUses to check for specific 00135 /// values. 00136 unsigned Value::getNumUses() const { 00137 return (unsigned)std::distance(use_begin(), use_end()); 00138 } 00139 00140 static bool getSymTab(Value *V, ValueSymbolTable *&ST) { 00141 ST = 0; 00142 if (Instruction *I = dyn_cast<Instruction>(V)) { 00143 if (BasicBlock *P = I->getParent()) 00144 if (Function *PP = P->getParent()) 00145 ST = &PP->getValueSymbolTable(); 00146 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) { 00147 if (Function *P = BB->getParent()) 00148 ST = &P->getValueSymbolTable(); 00149 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { 00150 if (Module *P = GV->getParent()) 00151 ST = &P->getValueSymbolTable(); 00152 } else if (Argument *A = dyn_cast<Argument>(V)) { 00153 if (Function *P = A->getParent()) 00154 ST = &P->getValueSymbolTable(); 00155 } else if (isa<MDString>(V)) 00156 return true; 00157 else { 00158 assert(isa<Constant>(V) && "Unknown value type!"); 00159 return true; // no name is setable for this. 00160 } 00161 return false; 00162 } 00163 00164 StringRef Value::getName() const { 00165 // Make sure the empty string is still a C string. For historical reasons, 00166 // some clients want to call .data() on the result and expect it to be null 00167 // terminated. 00168 if (!Name) return StringRef("", 0); 00169 return Name->getKey(); 00170 } 00171 00172 void Value::setName(const Twine &NewName) { 00173 assert(SubclassID != MDStringVal && 00174 "Cannot set the name of MDString with this method!"); 00175 00176 // Fast path for common IRBuilder case of setName("") when there is no name. 00177 if (NewName.isTriviallyEmpty() && !hasName()) 00178 return; 00179 00180 SmallString<256> NameData; 00181 StringRef NameRef = NewName.toStringRef(NameData); 00182 00183 // Name isn't changing? 00184 if (getName() == NameRef) 00185 return; 00186 00187 assert(!getType()->isVoidTy() && "Cannot assign a name to void values!"); 00188 00189 // Get the symbol table to update for this object. 00190 ValueSymbolTable *ST; 00191 if (getSymTab(this, ST)) 00192 return; // Cannot set a name on this value (e.g. constant). 00193 00194 if (!ST) { // No symbol table to update? Just do the change. 00195 if (NameRef.empty()) { 00196 // Free the name for this value. 00197 Name->Destroy(); 00198 Name = 0; 00199 return; 00200 } 00201 00202 if (Name) 00203 Name->Destroy(); 00204 00205 // NOTE: Could optimize for the case the name is shrinking to not deallocate 00206 // then reallocated. 00207 00208 // Create the new name. 00209 Name = ValueName::Create(NameRef.begin(), NameRef.end()); 00210 Name->setValue(this); 00211 return; 00212 } 00213 00214 // NOTE: Could optimize for the case the name is shrinking to not deallocate 00215 // then reallocated. 00216 if (hasName()) { 00217 // Remove old name. 00218 ST->removeValueName(Name); 00219 Name->Destroy(); 00220 Name = 0; 00221 00222 if (NameRef.empty()) 00223 return; 00224 } 00225 00226 // Name is changing to something new. 00227 Name = ST->createValueName(NameRef, this); 00228 } 00229 00230 00231 /// takeName - transfer the name from V to this value, setting V's name to 00232 /// empty. It is an error to call V->takeName(V). 00233 void Value::takeName(Value *V) { 00234 assert(SubclassID != MDStringVal && "Cannot take the name of an MDString!"); 00235 00236 ValueSymbolTable *ST = 0; 00237 // If this value has a name, drop it. 00238 if (hasName()) { 00239 // Get the symtab this is in. 00240 if (getSymTab(this, ST)) { 00241 // We can't set a name on this value, but we need to clear V's name if 00242 // it has one. 00243 if (V->hasName()) V->setName(""); 00244 return; // Cannot set a name on this value (e.g. constant). 00245 } 00246 00247 // Remove old name. 00248 if (ST) 00249 ST->removeValueName(Name); 00250 Name->Destroy(); 00251 Name = 0; 00252 } 00253 00254 // Now we know that this has no name. 00255 00256 // If V has no name either, we're done. 00257 if (!V->hasName()) return; 00258 00259 // Get this's symtab if we didn't before. 00260 if (!ST) { 00261 if (getSymTab(this, ST)) { 00262 // Clear V's name. 00263 V->setName(""); 00264 return; // Cannot set a name on this value (e.g. constant). 00265 } 00266 } 00267 00268 // Get V's ST, this should always succed, because V has a name. 00269 ValueSymbolTable *VST; 00270 bool Failure = getSymTab(V, VST); 00271 assert(!Failure && "V has a name, so it should have a ST!"); (void)Failure; 00272 00273 // If these values are both in the same symtab, we can do this very fast. 00274 // This works even if both values have no symtab yet. 00275 if (ST == VST) { 00276 // Take the name! 00277 Name = V->Name; 00278 V->Name = 0; 00279 Name->setValue(this); 00280 return; 00281 } 00282 00283 // Otherwise, things are slightly more complex. Remove V's name from VST and 00284 // then reinsert it into ST. 00285 00286 if (VST) 00287 VST->removeValueName(V->Name); 00288 Name = V->Name; 00289 V->Name = 0; 00290 Name->setValue(this); 00291 00292 if (ST) 00293 ST->reinsertValue(this); 00294 } 00295 00296 00297 void Value::replaceAllUsesWith(Value *New) { 00298 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!"); 00299 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!"); 00300 assert(New->getType() == getType() && 00301 "replaceAllUses of value with new value of different type!"); 00302 00303 // Notify all ValueHandles (if present) that this value is going away. 00304 if (HasValueHandle) 00305 ValueHandleBase::ValueIsRAUWd(this, New); 00306 00307 while (!use_empty()) { 00308 Use &U = *UseList; 00309 // Must handle Constants specially, we cannot call replaceUsesOfWith on a 00310 // constant because they are uniqued. 00311 if (Constant *C = dyn_cast<Constant>(U.getUser())) { 00312 if (!isa<GlobalValue>(C)) { 00313 C->replaceUsesOfWithOnConstant(this, New, &U); 00314 continue; 00315 } 00316 } 00317 00318 U.set(New); 00319 } 00320 00321 if (BasicBlock *BB = dyn_cast<BasicBlock>(this)) 00322 BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New)); 00323 } 00324 00325 namespace { 00326 // Various metrics for how much to strip off of pointers. 00327 enum PointerStripKind { 00328 PSK_ZeroIndices, 00329 PSK_InBoundsConstantIndices, 00330 PSK_InBounds 00331 }; 00332 00333 template <PointerStripKind StripKind> 00334 static Value *stripPointerCastsAndOffsets(Value *V) { 00335 if (!V->getType()->isPointerTy()) 00336 return V; 00337 00338 // Even though we don't look through PHI nodes, we could be called on an 00339 // instruction in an unreachable block, which may be on a cycle. 00340 SmallPtrSet<Value *, 4> Visited; 00341 00342 Visited.insert(V); 00343 do { 00344 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { 00345 switch (StripKind) { 00346 case PSK_ZeroIndices: 00347 if (!GEP->hasAllZeroIndices()) 00348 return V; 00349 break; 00350 case PSK_InBoundsConstantIndices: 00351 if (!GEP->hasAllConstantIndices()) 00352 return V; 00353 // fallthrough 00354 case PSK_InBounds: 00355 if (!GEP->isInBounds()) 00356 return V; 00357 break; 00358 } 00359 V = GEP->getPointerOperand(); 00360 } else if (Operator::getOpcode(V) == Instruction::BitCast) { 00361 V = cast<Operator>(V)->getOperand(0); 00362 } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { 00363 if (GA->mayBeOverridden()) 00364 return V; 00365 V = GA->getAliasee(); 00366 } else { 00367 return V; 00368 } 00369 assert(V->getType()->isPointerTy() && "Unexpected operand type!"); 00370 } while (Visited.insert(V)); 00371 00372 return V; 00373 } 00374 } // namespace 00375 00376 Value *Value::stripPointerCasts() { 00377 return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this); 00378 } 00379 00380 Value *Value::stripInBoundsConstantOffsets() { 00381 return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this); 00382 } 00383 00384 Value *Value::stripInBoundsOffsets() { 00385 return stripPointerCastsAndOffsets<PSK_InBounds>(this); 00386 } 00387 00388 /// isDereferenceablePointer - Test if this value is always a pointer to 00389 /// allocated and suitably aligned memory for a simple load or store. 00390 static bool isDereferenceablePointer(const Value *V, 00391 SmallPtrSet<const Value *, 32> &Visited) { 00392 // Note that it is not safe to speculate into a malloc'd region because 00393 // malloc may return null. 00394 // It's also not always safe to follow a bitcast, for example: 00395 // bitcast i8* (alloca i8) to i32* 00396 // would result in a 4-byte load from a 1-byte alloca. Some cases could 00397 // be handled using TargetData to check sizes and alignments though. 00398 00399 // These are obviously ok. 00400 if (isa<AllocaInst>(V)) return true; 00401 00402 // Global variables which can't collapse to null are ok. 00403 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) 00404 return !GV->hasExternalWeakLinkage(); 00405 00406 // byval arguments are ok. 00407 if (const Argument *A = dyn_cast<Argument>(V)) 00408 return A->hasByValAttr(); 00409 00410 // For GEPs, determine if the indexing lands within the allocated object. 00411 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { 00412 // Conservatively require that the base pointer be fully dereferenceable. 00413 if (!Visited.insert(GEP->getOperand(0))) 00414 return false; 00415 if (!isDereferenceablePointer(GEP->getOperand(0), Visited)) 00416 return false; 00417 // Check the indices. 00418 gep_type_iterator GTI = gep_type_begin(GEP); 00419 for (User::const_op_iterator I = GEP->op_begin()+1, 00420 E = GEP->op_end(); I != E; ++I) { 00421 Value *Index = *I; 00422 Type *Ty = *GTI++; 00423 // Struct indices can't be out of bounds. 00424 if (isa<StructType>(Ty)) 00425 continue; 00426 ConstantInt *CI = dyn_cast<ConstantInt>(Index); 00427 if (!CI) 00428 return false; 00429 // Zero is always ok. 00430 if (CI->isZero()) 00431 continue; 00432 // Check to see that it's within the bounds of an array. 00433 ArrayType *ATy = dyn_cast<ArrayType>(Ty); 00434 if (!ATy) 00435 return false; 00436 if (CI->getValue().getActiveBits() > 64) 00437 return false; 00438 if (CI->getZExtValue() >= ATy->getNumElements()) 00439 return false; 00440 } 00441 // Indices check out; this is dereferenceable. 00442 return true; 00443 } 00444 00445 // If we don't know, assume the worst. 00446 return false; 00447 } 00448 00449 /// isDereferenceablePointer - Test if this value is always a pointer to 00450 /// allocated and suitably aligned memory for a simple load or store. 00451 bool Value::isDereferenceablePointer() const { 00452 SmallPtrSet<const Value *, 32> Visited; 00453 return ::isDereferenceablePointer(this, Visited); 00454 } 00455 00456 /// DoPHITranslation - If this value is a PHI node with CurBB as its parent, 00457 /// return the value in the PHI node corresponding to PredBB. If not, return 00458 /// ourself. This is useful if you want to know the value something has in a 00459 /// predecessor block. 00460 Value *Value::DoPHITranslation(const BasicBlock *CurBB, 00461 const BasicBlock *PredBB) { 00462 PHINode *PN = dyn_cast<PHINode>(this); 00463 if (PN && PN->getParent() == CurBB) 00464 return PN->getIncomingValueForBlock(PredBB); 00465 return this; 00466 } 00467 00468 LLVMContext &Value::getContext() const { return VTy->getContext(); } 00469 00470 //===----------------------------------------------------------------------===// 00471 // ValueHandleBase Class 00472 //===----------------------------------------------------------------------===// 00473 00474 /// AddToExistingUseList - Add this ValueHandle to the use list for VP, where 00475 /// List is known to point into the existing use list. 00476 void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) { 00477 assert(List && "Handle list is null?"); 00478 00479 // Splice ourselves into the list. 00480 Next = *List; 00481 *List = this; 00482 setPrevPtr(List); 00483 if (Next) { 00484 Next->setPrevPtr(&Next); 00485 assert(VP.getPointer() == Next->VP.getPointer() && "Added to wrong list?"); 00486 } 00487 } 00488 00489 void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) { 00490 assert(List && "Must insert after existing node"); 00491 00492 Next = List->Next; 00493 setPrevPtr(&List->Next); 00494 List->Next = this; 00495 if (Next) 00496 Next->setPrevPtr(&Next); 00497 } 00498 00499 /// AddToUseList - Add this ValueHandle to the use list for VP. 00500 void ValueHandleBase::AddToUseList() { 00501 assert(VP.getPointer() && "Null pointer doesn't have a use list!"); 00502 00503 LLVMContextImpl *pImpl = VP.getPointer()->getContext().pImpl; 00504 00505 if (VP.getPointer()->HasValueHandle) { 00506 // If this value already has a ValueHandle, then it must be in the 00507 // ValueHandles map already. 00508 ValueHandleBase *&Entry = pImpl->ValueHandles[VP.getPointer()]; 00509 assert(Entry != 0 && "Value doesn't have any handles?"); 00510 AddToExistingUseList(&Entry); 00511 return; 00512 } 00513 00514 // Ok, it doesn't have any handles yet, so we must insert it into the 00515 // DenseMap. However, doing this insertion could cause the DenseMap to 00516 // reallocate itself, which would invalidate all of the PrevP pointers that 00517 // point into the old table. Handle this by checking for reallocation and 00518 // updating the stale pointers only if needed. 00519 DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles; 00520 const void *OldBucketPtr = Handles.getPointerIntoBucketsArray(); 00521 00522 ValueHandleBase *&Entry = Handles[VP.getPointer()]; 00523 assert(Entry == 0 && "Value really did already have handles?"); 00524 AddToExistingUseList(&Entry); 00525 VP.getPointer()->HasValueHandle = true; 00526 00527 // If reallocation didn't happen or if this was the first insertion, don't 00528 // walk the table. 00529 if (Handles.isPointerIntoBucketsArray(OldBucketPtr) || 00530 Handles.size() == 1) { 00531 return; 00532 } 00533 00534 // Okay, reallocation did happen. Fix the Prev Pointers. 00535 for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(), 00536 E = Handles.end(); I != E; ++I) { 00537 assert(I->second && I->first == I->second->VP.getPointer() && 00538 "List invariant broken!"); 00539 I->second->setPrevPtr(&I->second); 00540 } 00541 } 00542 00543 /// RemoveFromUseList - Remove this ValueHandle from its current use list. 00544 void ValueHandleBase::RemoveFromUseList() { 00545 assert(VP.getPointer() && VP.getPointer()->HasValueHandle && 00546 "Pointer doesn't have a use list!"); 00547 00548 // Unlink this from its use list. 00549 ValueHandleBase **PrevPtr = getPrevPtr(); 00550 assert(*PrevPtr == this && "List invariant broken"); 00551 00552 *PrevPtr = Next; 00553 if (Next) { 00554 assert(Next->getPrevPtr() == &Next && "List invariant broken"); 00555 Next->setPrevPtr(PrevPtr); 00556 return; 00557 } 00558 00559 // If the Next pointer was null, then it is possible that this was the last 00560 // ValueHandle watching VP. If so, delete its entry from the ValueHandles 00561 // map. 00562 LLVMContextImpl *pImpl = VP.getPointer()->getContext().pImpl; 00563 DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles; 00564 if (Handles.isPointerIntoBucketsArray(PrevPtr)) { 00565 Handles.erase(VP.getPointer()); 00566 VP.getPointer()->HasValueHandle = false; 00567 } 00568 } 00569 00570 00571 void ValueHandleBase::ValueIsDeleted(Value *V) { 00572 assert(V->HasValueHandle && "Should only be called if ValueHandles present"); 00573 00574 // Get the linked list base, which is guaranteed to exist since the 00575 // HasValueHandle flag is set. 00576 LLVMContextImpl *pImpl = V->getContext().pImpl; 00577 ValueHandleBase *Entry = pImpl->ValueHandles[V]; 00578 assert(Entry && "Value bit set but no entries exist"); 00579 00580 // We use a local ValueHandleBase as an iterator so that ValueHandles can add 00581 // and remove themselves from the list without breaking our iteration. This 00582 // is not really an AssertingVH; we just have to give ValueHandleBase a kind. 00583 // Note that we deliberately do not the support the case when dropping a value 00584 // handle results in a new value handle being permanently added to the list 00585 // (as might occur in theory for CallbackVH's): the new value handle will not 00586 // be processed and the checking code will mete out righteous punishment if 00587 // the handle is still present once we have finished processing all the other 00588 // value handles (it is fine to momentarily add then remove a value handle). 00589 for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) { 00590 Iterator.RemoveFromUseList(); 00591 Iterator.AddToExistingUseListAfter(Entry); 00592 assert(Entry->Next == &Iterator && "Loop invariant broken."); 00593 00594 switch (Entry->getKind()) { 00595 case Assert: 00596 break; 00597 case Tracking: 00598 // Mark that this value has been deleted by setting it to an invalid Value 00599 // pointer. 00600 Entry->operator=(DenseMapInfo<Value *>::getTombstoneKey()); 00601 break; 00602 case Weak: 00603 // Weak just goes to null, which will unlink it from the list. 00604 Entry->operator=(0); 00605 break; 00606 case Callback: 00607 // Forward to the subclass's implementation. 00608 static_cast<CallbackVH*>(Entry)->deleted(); 00609 break; 00610 } 00611 } 00612 00613 // All callbacks, weak references, and assertingVHs should be dropped by now. 00614 if (V->HasValueHandle) { 00615 #ifndef NDEBUG // Only in +Asserts mode... 00616 dbgs() << "While deleting: " << *V->getType() << " %" << V->getName() 00617 << "\n"; 00618 if (pImpl->ValueHandles[V]->getKind() == Assert) 00619 llvm_unreachable("An asserting value handle still pointed to this" 00620 " value!"); 00621 00622 #endif 00623 llvm_unreachable("All references to V were not removed?"); 00624 } 00625 } 00626 00627 00628 void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) { 00629 assert(Old->HasValueHandle &&"Should only be called if ValueHandles present"); 00630 assert(Old != New && "Changing value into itself!"); 00631 00632 // Get the linked list base, which is guaranteed to exist since the 00633 // HasValueHandle flag is set. 00634 LLVMContextImpl *pImpl = Old->getContext().pImpl; 00635 ValueHandleBase *Entry = pImpl->ValueHandles[Old]; 00636 00637 assert(Entry && "Value bit set but no entries exist"); 00638 00639 // We use a local ValueHandleBase as an iterator so that 00640 // ValueHandles can add and remove themselves from the list without 00641 // breaking our iteration. This is not really an AssertingVH; we 00642 // just have to give ValueHandleBase some kind. 00643 for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) { 00644 Iterator.RemoveFromUseList(); 00645 Iterator.AddToExistingUseListAfter(Entry); 00646 assert(Entry->Next == &Iterator && "Loop invariant broken."); 00647 00648 switch (Entry->getKind()) { 00649 case Assert: 00650 // Asserting handle does not follow RAUW implicitly. 00651 break; 00652 case Tracking: 00653 // Tracking goes to new value like a WeakVH. Note that this may make it 00654 // something incompatible with its templated type. We don't want to have a 00655 // virtual (or inline) interface to handle this though, so instead we make 00656 // the TrackingVH accessors guarantee that a client never sees this value. 00657 00658 // FALLTHROUGH 00659 case Weak: 00660 // Weak goes to the new value, which will unlink it from Old's list. 00661 Entry->operator=(New); 00662 break; 00663 case Callback: 00664 // Forward to the subclass's implementation. 00665 static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New); 00666 break; 00667 } 00668 } 00669 00670 #ifndef NDEBUG 00671 // If any new tracking or weak value handles were added while processing the 00672 // list, then complain about it now. 00673 if (Old->HasValueHandle) 00674 for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next) 00675 switch (Entry->getKind()) { 00676 case Tracking: 00677 case Weak: 00678 dbgs() << "After RAUW from " << *Old->getType() << " %" 00679 << Old->getName() << " to " << *New->getType() << " %" 00680 << New->getName() << "\n"; 00681 llvm_unreachable("A tracking or weak value handle still pointed to the" 00682 " old value!\n"); 00683 default: 00684 break; 00685 } 00686 #endif 00687 } 00688 00689 /// ~CallbackVH. Empty, but defined here to avoid emitting the vtable 00690 /// more than once. 00691 CallbackVH::~CallbackVH() {}