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