Value.cpp revision 251662
1//===-- Value.cpp - Implement the Value class -----------------------------===//
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 Value, ValueHandle, and User classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/IR/Value.h"
15#include "LLVMContextImpl.h"
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/SmallString.h"
18#include "llvm/IR/Constant.h"
19#include "llvm/IR/Constants.h"
20#include "llvm/IR/DerivedTypes.h"
21#include "llvm/IR/InstrTypes.h"
22#include "llvm/IR/Instructions.h"
23#include "llvm/IR/Module.h"
24#include "llvm/IR/Operator.h"
25#include "llvm/IR/ValueSymbolTable.h"
26#include "llvm/Support/Debug.h"
27#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/GetElementPtrTypeIterator.h"
29#include "llvm/Support/LeakDetector.h"
30#include "llvm/Support/ManagedStatic.h"
31#include "llvm/Support/ValueHandle.h"
32#include <algorithm>
33using namespace llvm;
34
35//===----------------------------------------------------------------------===//
36//                                Value Class
37//===----------------------------------------------------------------------===//
38
39static inline Type *checkType(Type *Ty) {
40  assert(Ty && "Value defined with a null type: Error!");
41  return const_cast<Type*>(Ty);
42}
43
44Value::Value(Type *ty, unsigned scid)
45  : SubclassID(scid), HasValueHandle(0),
46    SubclassOptionalData(0), SubclassData(0), VTy((Type*)checkType(ty)),
47    UseList(0), Name(0) {
48  // FIXME: Why isn't this in the subclass gunk??
49  // Note, we cannot call isa<CallInst> before the CallInst has been
50  // constructed.
51  if (SubclassID == Instruction::Call || SubclassID == Instruction::Invoke)
52    assert((VTy->isFirstClassType() || VTy->isVoidTy() || VTy->isStructTy()) &&
53           "invalid CallInst type!");
54  else if (SubclassID != BasicBlockVal &&
55           (SubclassID < ConstantFirstVal || SubclassID > ConstantLastVal))
56    assert((VTy->isFirstClassType() || VTy->isVoidTy()) &&
57           "Cannot create non-first-class values except for constants!");
58}
59
60Value::~Value() {
61  // Notify all ValueHandles (if present) that this value is going away.
62  if (HasValueHandle)
63    ValueHandleBase::ValueIsDeleted(this);
64
65#ifndef NDEBUG      // Only in -g mode...
66  // Check to make sure that there are no uses of this value that are still
67  // around when the value is destroyed.  If there are, then we have a dangling
68  // reference and something is wrong.  This code is here to print out what is
69  // still being referenced.  The value in question should be printed as
70  // a <badref>
71  //
72  if (!use_empty()) {
73    dbgs() << "While deleting: " << *VTy << " %" << getName() << "\n";
74    for (use_iterator I = use_begin(), E = use_end(); I != E; ++I)
75      dbgs() << "Use still stuck around after Def is destroyed:"
76           << **I << "\n";
77  }
78#endif
79  assert(use_empty() && "Uses remain when a value is destroyed!");
80
81  // If this value is named, destroy the name.  This should not be in a symtab
82  // at this point.
83  if (Name && SubclassID != MDStringVal)
84    Name->Destroy();
85
86  // There should be no uses of this object anymore, remove it.
87  LeakDetector::removeGarbageObject(this);
88}
89
90/// hasNUses - Return true if this Value has exactly N users.
91///
92bool Value::hasNUses(unsigned N) const {
93  const_use_iterator UI = use_begin(), E = use_end();
94
95  for (; N; --N, ++UI)
96    if (UI == E) return false;  // Too few.
97  return UI == E;
98}
99
100/// hasNUsesOrMore - Return true if this value has N users or more.  This is
101/// logically equivalent to getNumUses() >= N.
102///
103bool Value::hasNUsesOrMore(unsigned N) const {
104  const_use_iterator UI = use_begin(), E = use_end();
105
106  for (; N; --N, ++UI)
107    if (UI == E) return false;  // Too few.
108
109  return true;
110}
111
112/// isUsedInBasicBlock - Return true if this value is used in the specified
113/// basic block.
114bool Value::isUsedInBasicBlock(const BasicBlock *BB) const {
115  // Start by scanning over the instructions looking for a use before we start
116  // the expensive use iteration.
117  unsigned MaxBlockSize = 3;
118  for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
119    if (std::find(I->op_begin(), I->op_end(), this) != I->op_end())
120      return true;
121    if (--MaxBlockSize == 0) // If the block is larger fall back to use_iterator
122      break;
123  }
124
125  if (MaxBlockSize != 0) // We scanned the entire block and found no use.
126    return false;
127
128  for (const_use_iterator I = use_begin(), E = use_end(); I != E; ++I) {
129    const Instruction *User = dyn_cast<Instruction>(*I);
130    if (User && User->getParent() == BB)
131      return true;
132  }
133  return false;
134}
135
136
137/// getNumUses - This method computes the number of uses of this Value.  This
138/// is a linear time operation.  Use hasOneUse or hasNUses to check for specific
139/// values.
140unsigned Value::getNumUses() const {
141  return (unsigned)std::distance(use_begin(), use_end());
142}
143
144static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
145  ST = 0;
146  if (Instruction *I = dyn_cast<Instruction>(V)) {
147    if (BasicBlock *P = I->getParent())
148      if (Function *PP = P->getParent())
149        ST = &PP->getValueSymbolTable();
150  } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
151    if (Function *P = BB->getParent())
152      ST = &P->getValueSymbolTable();
153  } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
154    if (Module *P = GV->getParent())
155      ST = &P->getValueSymbolTable();
156  } else if (Argument *A = dyn_cast<Argument>(V)) {
157    if (Function *P = A->getParent())
158      ST = &P->getValueSymbolTable();
159  } else if (isa<MDString>(V))
160    return true;
161  else {
162    assert(isa<Constant>(V) && "Unknown value type!");
163    return true;  // no name is setable for this.
164  }
165  return false;
166}
167
168StringRef Value::getName() const {
169  // Make sure the empty string is still a C string. For historical reasons,
170  // some clients want to call .data() on the result and expect it to be null
171  // terminated.
172  if (!Name) return StringRef("", 0);
173  return Name->getKey();
174}
175
176void Value::setName(const Twine &NewName) {
177  assert(SubclassID != MDStringVal &&
178         "Cannot set the name of MDString with this method!");
179
180  // Fast path for common IRBuilder case of setName("") when there is no name.
181  if (NewName.isTriviallyEmpty() && !hasName())
182    return;
183
184  SmallString<256> NameData;
185  StringRef NameRef = NewName.toStringRef(NameData);
186
187  // Name isn't changing?
188  if (getName() == NameRef)
189    return;
190
191  assert(!getType()->isVoidTy() && "Cannot assign a name to void values!");
192
193  // Get the symbol table to update for this object.
194  ValueSymbolTable *ST;
195  if (getSymTab(this, ST))
196    return;  // Cannot set a name on this value (e.g. constant).
197
198  if (Function *F = dyn_cast<Function>(this))
199    getContext().pImpl->IntrinsicIDCache.erase(F);
200
201  if (!ST) { // No symbol table to update?  Just do the change.
202    if (NameRef.empty()) {
203      // Free the name for this value.
204      Name->Destroy();
205      Name = 0;
206      return;
207    }
208
209    if (Name)
210      Name->Destroy();
211
212    // NOTE: Could optimize for the case the name is shrinking to not deallocate
213    // then reallocated.
214
215    // Create the new name.
216    Name = ValueName::Create(NameRef.begin(), NameRef.end());
217    Name->setValue(this);
218    return;
219  }
220
221  // NOTE: Could optimize for the case the name is shrinking to not deallocate
222  // then reallocated.
223  if (hasName()) {
224    // Remove old name.
225    ST->removeValueName(Name);
226    Name->Destroy();
227    Name = 0;
228
229    if (NameRef.empty())
230      return;
231  }
232
233  // Name is changing to something new.
234  Name = ST->createValueName(NameRef, this);
235}
236
237
238/// takeName - transfer the name from V to this value, setting V's name to
239/// empty.  It is an error to call V->takeName(V).
240void Value::takeName(Value *V) {
241  assert(SubclassID != MDStringVal && "Cannot take the name of an MDString!");
242
243  ValueSymbolTable *ST = 0;
244  // If this value has a name, drop it.
245  if (hasName()) {
246    // Get the symtab this is in.
247    if (getSymTab(this, ST)) {
248      // We can't set a name on this value, but we need to clear V's name if
249      // it has one.
250      if (V->hasName()) V->setName("");
251      return;  // Cannot set a name on this value (e.g. constant).
252    }
253
254    // Remove old name.
255    if (ST)
256      ST->removeValueName(Name);
257    Name->Destroy();
258    Name = 0;
259  }
260
261  // Now we know that this has no name.
262
263  // If V has no name either, we're done.
264  if (!V->hasName()) return;
265
266  // Get this's symtab if we didn't before.
267  if (!ST) {
268    if (getSymTab(this, ST)) {
269      // Clear V's name.
270      V->setName("");
271      return;  // Cannot set a name on this value (e.g. constant).
272    }
273  }
274
275  // Get V's ST, this should always succed, because V has a name.
276  ValueSymbolTable *VST;
277  bool Failure = getSymTab(V, VST);
278  assert(!Failure && "V has a name, so it should have a ST!"); (void)Failure;
279
280  // If these values are both in the same symtab, we can do this very fast.
281  // This works even if both values have no symtab yet.
282  if (ST == VST) {
283    // Take the name!
284    Name = V->Name;
285    V->Name = 0;
286    Name->setValue(this);
287    return;
288  }
289
290  // Otherwise, things are slightly more complex.  Remove V's name from VST and
291  // then reinsert it into ST.
292
293  if (VST)
294    VST->removeValueName(V->Name);
295  Name = V->Name;
296  V->Name = 0;
297  Name->setValue(this);
298
299  if (ST)
300    ST->reinsertValue(this);
301}
302
303
304void Value::replaceAllUsesWith(Value *New) {
305  assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
306  assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
307  assert(New->getType() == getType() &&
308         "replaceAllUses of value with new value of different type!");
309
310  // Notify all ValueHandles (if present) that this value is going away.
311  if (HasValueHandle)
312    ValueHandleBase::ValueIsRAUWd(this, New);
313
314  while (!use_empty()) {
315    Use &U = *UseList;
316    // Must handle Constants specially, we cannot call replaceUsesOfWith on a
317    // constant because they are uniqued.
318    if (Constant *C = dyn_cast<Constant>(U.getUser())) {
319      if (!isa<GlobalValue>(C)) {
320        C->replaceUsesOfWithOnConstant(this, New, &U);
321        continue;
322      }
323    }
324
325    U.set(New);
326  }
327
328  if (BasicBlock *BB = dyn_cast<BasicBlock>(this))
329    BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New));
330}
331
332namespace {
333// Various metrics for how much to strip off of pointers.
334enum PointerStripKind {
335  PSK_ZeroIndices,
336  PSK_ZeroIndicesAndAliases,
337  PSK_InBoundsConstantIndices,
338  PSK_InBounds
339};
340
341template <PointerStripKind StripKind>
342static Value *stripPointerCastsAndOffsets(Value *V) {
343  if (!V->getType()->isPointerTy())
344    return V;
345
346  // Even though we don't look through PHI nodes, we could be called on an
347  // instruction in an unreachable block, which may be on a cycle.
348  SmallPtrSet<Value *, 4> Visited;
349
350  Visited.insert(V);
351  do {
352    if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
353      switch (StripKind) {
354      case PSK_ZeroIndicesAndAliases:
355      case PSK_ZeroIndices:
356        if (!GEP->hasAllZeroIndices())
357          return V;
358        break;
359      case PSK_InBoundsConstantIndices:
360        if (!GEP->hasAllConstantIndices())
361          return V;
362        // fallthrough
363      case PSK_InBounds:
364        if (!GEP->isInBounds())
365          return V;
366        break;
367      }
368      V = GEP->getPointerOperand();
369    } else if (Operator::getOpcode(V) == Instruction::BitCast) {
370      V = cast<Operator>(V)->getOperand(0);
371    } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
372      if (StripKind == PSK_ZeroIndices || GA->mayBeOverridden())
373        return V;
374      V = GA->getAliasee();
375    } else {
376      return V;
377    }
378    assert(V->getType()->isPointerTy() && "Unexpected operand type!");
379  } while (Visited.insert(V));
380
381  return V;
382}
383} // namespace
384
385Value *Value::stripPointerCasts() {
386  return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliases>(this);
387}
388
389Value *Value::stripPointerCastsNoFollowAliases() {
390  return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this);
391}
392
393Value *Value::stripInBoundsConstantOffsets() {
394  return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this);
395}
396
397Value *Value::stripInBoundsOffsets() {
398  return stripPointerCastsAndOffsets<PSK_InBounds>(this);
399}
400
401/// isDereferenceablePointer - Test if this value is always a pointer to
402/// allocated and suitably aligned memory for a simple load or store.
403static bool isDereferenceablePointer(const Value *V,
404                                     SmallPtrSet<const Value *, 32> &Visited) {
405  // Note that it is not safe to speculate into a malloc'd region because
406  // malloc may return null.
407  // It's also not always safe to follow a bitcast, for example:
408  //   bitcast i8* (alloca i8) to i32*
409  // would result in a 4-byte load from a 1-byte alloca. Some cases could
410  // be handled using DataLayout to check sizes and alignments though.
411
412  // These are obviously ok.
413  if (isa<AllocaInst>(V)) return true;
414
415  // Global variables which can't collapse to null are ok.
416  if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
417    return !GV->hasExternalWeakLinkage();
418
419  // byval arguments are ok.
420  if (const Argument *A = dyn_cast<Argument>(V))
421    return A->hasByValAttr();
422
423  // For GEPs, determine if the indexing lands within the allocated object.
424  if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
425    // Conservatively require that the base pointer be fully dereferenceable.
426    if (!Visited.insert(GEP->getOperand(0)))
427      return false;
428    if (!isDereferenceablePointer(GEP->getOperand(0), Visited))
429      return false;
430    // Check the indices.
431    gep_type_iterator GTI = gep_type_begin(GEP);
432    for (User::const_op_iterator I = GEP->op_begin()+1,
433         E = GEP->op_end(); I != E; ++I) {
434      Value *Index = *I;
435      Type *Ty = *GTI++;
436      // Struct indices can't be out of bounds.
437      if (isa<StructType>(Ty))
438        continue;
439      ConstantInt *CI = dyn_cast<ConstantInt>(Index);
440      if (!CI)
441        return false;
442      // Zero is always ok.
443      if (CI->isZero())
444        continue;
445      // Check to see that it's within the bounds of an array.
446      ArrayType *ATy = dyn_cast<ArrayType>(Ty);
447      if (!ATy)
448        return false;
449      if (CI->getValue().getActiveBits() > 64)
450        return false;
451      if (CI->getZExtValue() >= ATy->getNumElements())
452        return false;
453    }
454    // Indices check out; this is dereferenceable.
455    return true;
456  }
457
458  // If we don't know, assume the worst.
459  return false;
460}
461
462/// isDereferenceablePointer - Test if this value is always a pointer to
463/// allocated and suitably aligned memory for a simple load or store.
464bool Value::isDereferenceablePointer() const {
465  SmallPtrSet<const Value *, 32> Visited;
466  return ::isDereferenceablePointer(this, Visited);
467}
468
469/// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
470/// return the value in the PHI node corresponding to PredBB.  If not, return
471/// ourself.  This is useful if you want to know the value something has in a
472/// predecessor block.
473Value *Value::DoPHITranslation(const BasicBlock *CurBB,
474                               const BasicBlock *PredBB) {
475  PHINode *PN = dyn_cast<PHINode>(this);
476  if (PN && PN->getParent() == CurBB)
477    return PN->getIncomingValueForBlock(PredBB);
478  return this;
479}
480
481LLVMContext &Value::getContext() const { return VTy->getContext(); }
482
483//===----------------------------------------------------------------------===//
484//                             ValueHandleBase Class
485//===----------------------------------------------------------------------===//
486
487/// AddToExistingUseList - Add this ValueHandle to the use list for VP, where
488/// List is known to point into the existing use list.
489void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
490  assert(List && "Handle list is null?");
491
492  // Splice ourselves into the list.
493  Next = *List;
494  *List = this;
495  setPrevPtr(List);
496  if (Next) {
497    Next->setPrevPtr(&Next);
498    assert(VP.getPointer() == Next->VP.getPointer() && "Added to wrong list?");
499  }
500}
501
502void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) {
503  assert(List && "Must insert after existing node");
504
505  Next = List->Next;
506  setPrevPtr(&List->Next);
507  List->Next = this;
508  if (Next)
509    Next->setPrevPtr(&Next);
510}
511
512/// AddToUseList - Add this ValueHandle to the use list for VP.
513void ValueHandleBase::AddToUseList() {
514  assert(VP.getPointer() && "Null pointer doesn't have a use list!");
515
516  LLVMContextImpl *pImpl = VP.getPointer()->getContext().pImpl;
517
518  if (VP.getPointer()->HasValueHandle) {
519    // If this value already has a ValueHandle, then it must be in the
520    // ValueHandles map already.
521    ValueHandleBase *&Entry = pImpl->ValueHandles[VP.getPointer()];
522    assert(Entry != 0 && "Value doesn't have any handles?");
523    AddToExistingUseList(&Entry);
524    return;
525  }
526
527  // Ok, it doesn't have any handles yet, so we must insert it into the
528  // DenseMap.  However, doing this insertion could cause the DenseMap to
529  // reallocate itself, which would invalidate all of the PrevP pointers that
530  // point into the old table.  Handle this by checking for reallocation and
531  // updating the stale pointers only if needed.
532  DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
533  const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
534
535  ValueHandleBase *&Entry = Handles[VP.getPointer()];
536  assert(Entry == 0 && "Value really did already have handles?");
537  AddToExistingUseList(&Entry);
538  VP.getPointer()->HasValueHandle = true;
539
540  // If reallocation didn't happen or if this was the first insertion, don't
541  // walk the table.
542  if (Handles.isPointerIntoBucketsArray(OldBucketPtr) ||
543      Handles.size() == 1) {
544    return;
545  }
546
547  // Okay, reallocation did happen.  Fix the Prev Pointers.
548  for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(),
549       E = Handles.end(); I != E; ++I) {
550    assert(I->second && I->first == I->second->VP.getPointer() &&
551           "List invariant broken!");
552    I->second->setPrevPtr(&I->second);
553  }
554}
555
556/// RemoveFromUseList - Remove this ValueHandle from its current use list.
557void ValueHandleBase::RemoveFromUseList() {
558  assert(VP.getPointer() && VP.getPointer()->HasValueHandle &&
559         "Pointer doesn't have a use list!");
560
561  // Unlink this from its use list.
562  ValueHandleBase **PrevPtr = getPrevPtr();
563  assert(*PrevPtr == this && "List invariant broken");
564
565  *PrevPtr = Next;
566  if (Next) {
567    assert(Next->getPrevPtr() == &Next && "List invariant broken");
568    Next->setPrevPtr(PrevPtr);
569    return;
570  }
571
572  // If the Next pointer was null, then it is possible that this was the last
573  // ValueHandle watching VP.  If so, delete its entry from the ValueHandles
574  // map.
575  LLVMContextImpl *pImpl = VP.getPointer()->getContext().pImpl;
576  DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
577  if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
578    Handles.erase(VP.getPointer());
579    VP.getPointer()->HasValueHandle = false;
580  }
581}
582
583
584void ValueHandleBase::ValueIsDeleted(Value *V) {
585  assert(V->HasValueHandle && "Should only be called if ValueHandles present");
586
587  // Get the linked list base, which is guaranteed to exist since the
588  // HasValueHandle flag is set.
589  LLVMContextImpl *pImpl = V->getContext().pImpl;
590  ValueHandleBase *Entry = pImpl->ValueHandles[V];
591  assert(Entry && "Value bit set but no entries exist");
592
593  // We use a local ValueHandleBase as an iterator so that ValueHandles can add
594  // and remove themselves from the list without breaking our iteration.  This
595  // is not really an AssertingVH; we just have to give ValueHandleBase a kind.
596  // Note that we deliberately do not the support the case when dropping a value
597  // handle results in a new value handle being permanently added to the list
598  // (as might occur in theory for CallbackVH's): the new value handle will not
599  // be processed and the checking code will mete out righteous punishment if
600  // the handle is still present once we have finished processing all the other
601  // value handles (it is fine to momentarily add then remove a value handle).
602  for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
603    Iterator.RemoveFromUseList();
604    Iterator.AddToExistingUseListAfter(Entry);
605    assert(Entry->Next == &Iterator && "Loop invariant broken.");
606
607    switch (Entry->getKind()) {
608    case Assert:
609      break;
610    case Tracking:
611      // Mark that this value has been deleted by setting it to an invalid Value
612      // pointer.
613      Entry->operator=(DenseMapInfo<Value *>::getTombstoneKey());
614      break;
615    case Weak:
616      // Weak just goes to null, which will unlink it from the list.
617      Entry->operator=(0);
618      break;
619    case Callback:
620      // Forward to the subclass's implementation.
621      static_cast<CallbackVH*>(Entry)->deleted();
622      break;
623    }
624  }
625
626  // All callbacks, weak references, and assertingVHs should be dropped by now.
627  if (V->HasValueHandle) {
628#ifndef NDEBUG      // Only in +Asserts mode...
629    dbgs() << "While deleting: " << *V->getType() << " %" << V->getName()
630           << "\n";
631    if (pImpl->ValueHandles[V]->getKind() == Assert)
632      llvm_unreachable("An asserting value handle still pointed to this"
633                       " value!");
634
635#endif
636    llvm_unreachable("All references to V were not removed?");
637  }
638}
639
640
641void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
642  assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");
643  assert(Old != New && "Changing value into itself!");
644
645  // Get the linked list base, which is guaranteed to exist since the
646  // HasValueHandle flag is set.
647  LLVMContextImpl *pImpl = Old->getContext().pImpl;
648  ValueHandleBase *Entry = pImpl->ValueHandles[Old];
649
650  assert(Entry && "Value bit set but no entries exist");
651
652  // We use a local ValueHandleBase as an iterator so that
653  // ValueHandles can add and remove themselves from the list without
654  // breaking our iteration.  This is not really an AssertingVH; we
655  // just have to give ValueHandleBase some kind.
656  for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
657    Iterator.RemoveFromUseList();
658    Iterator.AddToExistingUseListAfter(Entry);
659    assert(Entry->Next == &Iterator && "Loop invariant broken.");
660
661    switch (Entry->getKind()) {
662    case Assert:
663      // Asserting handle does not follow RAUW implicitly.
664      break;
665    case Tracking:
666      // Tracking goes to new value like a WeakVH. Note that this may make it
667      // something incompatible with its templated type. We don't want to have a
668      // virtual (or inline) interface to handle this though, so instead we make
669      // the TrackingVH accessors guarantee that a client never sees this value.
670
671      // FALLTHROUGH
672    case Weak:
673      // Weak goes to the new value, which will unlink it from Old's list.
674      Entry->operator=(New);
675      break;
676    case Callback:
677      // Forward to the subclass's implementation.
678      static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New);
679      break;
680    }
681  }
682
683#ifndef NDEBUG
684  // If any new tracking or weak value handles were added while processing the
685  // list, then complain about it now.
686  if (Old->HasValueHandle)
687    for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next)
688      switch (Entry->getKind()) {
689      case Tracking:
690      case Weak:
691        dbgs() << "After RAUW from " << *Old->getType() << " %"
692               << Old->getName() << " to " << *New->getType() << " %"
693               << New->getName() << "\n";
694        llvm_unreachable("A tracking or weak value handle still pointed to the"
695                         " old value!\n");
696      default:
697        break;
698      }
699#endif
700}
701
702// Default implementation for CallbackVH.
703void CallbackVH::allUsesReplacedWith(Value *) {}
704
705void CallbackVH::deleted() {
706  setValPtr(NULL);
707}
708