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