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