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