1 //===- llvm/Support/ValueHandle.h - Value Smart Pointer classes -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the ValueHandle class and its sub-classes.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_VALUEHANDLE_H
15 #define LLVM_SUPPORT_VALUEHANDLE_H
17 #include "llvm/ADT/PointerIntPair.h"
18 #include "llvm/Value.h"
22 /// ValueHandleBase - This is the common base class of value handles.
23 /// ValueHandle's are smart pointers to Value's that have special behavior when
24 /// the value is deleted or ReplaceAllUsesWith'd. See the specific handles
25 /// below for details.
27 class ValueHandleBase {
30 /// HandleBaseKind - This indicates what base class the handle actually is.
31 /// This is to avoid having a vtable for the light-weight handle pointers. The
32 /// fully generally Callback version does have a vtable.
40 PointerIntPair<ValueHandleBase**, 2, HandleBaseKind> PrevPair;
41 ValueHandleBase *Next;
44 ValueHandleBase(HandleBaseKind Kind) : PrevPair(0, Kind), Next(0), VP(0) {}
45 ValueHandleBase(HandleBaseKind Kind, Value *V)
46 : PrevPair(0, Kind), Next(0), VP(V) {
50 ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
51 : PrevPair(0, Kind), Next(0), VP(RHS.VP) {
53 AddToExistingUseList(RHS.getPrevPtr());
60 Value *operator=(Value *RHS) {
61 if (VP == RHS) return RHS;
62 if (VP) RemoveFromUseList();
64 if (VP) AddToUseList();
68 Value *operator=(const ValueHandleBase &RHS) {
69 if (VP == RHS.VP) return RHS.VP;
70 if (VP) RemoveFromUseList();
72 if (VP) AddToExistingUseList(RHS.getPrevPtr());
76 Value *operator->() const { return getValPtr(); }
77 Value &operator*() const { return *getValPtr(); }
79 bool operator==(const Value *RHS) const { return VP == RHS; }
80 bool operator==(const ValueHandleBase &RHS) const { return VP == RHS.VP; }
81 bool operator!=(const Value *RHS) const { return VP != RHS; }
82 bool operator!=(const ValueHandleBase &RHS) const { return VP != RHS.VP; }
83 bool operator<(const Value *RHS) const { return VP < RHS; }
84 bool operator<(const ValueHandleBase &RHS) const { return VP < RHS.VP; }
85 bool operator>(const Value *RHS) const { return VP > RHS; }
86 bool operator>(const ValueHandleBase &RHS) const { return VP > RHS.VP; }
87 bool operator<=(const Value *RHS) const { return VP <= RHS; }
88 bool operator<=(const ValueHandleBase &RHS) const { return VP <= RHS.VP; }
89 bool operator>=(const Value *RHS) const { return VP >= RHS; }
90 bool operator>=(const ValueHandleBase &RHS) const { return VP >= RHS.VP; }
93 Value *getValPtr() const { return VP; }
95 // Callbacks made from Value.
96 static void ValueIsDeleted(Value *V);
97 static void ValueIsRAUWd(Value *Old, Value *New);
99 // Internal implementation details.
100 ValueHandleBase **getPrevPtr() const { return PrevPair.getPointer(); }
101 HandleBaseKind getKind() const { return PrevPair.getInt(); }
102 void setPrevPtr(ValueHandleBase **Ptr) { PrevPair.setPointer(Ptr); }
104 /// AddToUseList - Add this ValueHandle to the use list for VP, where List is
105 /// known to point into the existing use list.
106 void AddToExistingUseList(ValueHandleBase **List);
108 /// AddToUseList - Add this ValueHandle to the use list for VP.
110 /// RemoveFromUseList - Remove this ValueHandle from its current use list.
111 void RemoveFromUseList();
114 /// WeakVH - This is a value handle that tries hard to point to a Value, even
115 /// across RAUW operations, but will null itself out if the value is destroyed.
116 /// this is useful for advisory sorts of information, but should not be used as
117 /// the key of a map (since the map would have to rearrange itself when the
118 /// pointer changes).
119 class WeakVH : public ValueHandleBase {
121 WeakVH() : ValueHandleBase(Weak) {}
122 WeakVH(Value *P) : ValueHandleBase(Weak, P) {}
123 WeakVH(const WeakVH &RHS)
124 : ValueHandleBase(Weak, RHS) {}
128 /// AssertingVH - This is a Value Handle that points to a value and asserts out
129 /// if the value is destroyed while the handle is still live. This is very
130 /// useful for catching dangling pointer bugs and other things which can be
131 /// non-obvious. One particularly useful place to use this is as the Key of a
132 /// map. Dangling pointer bugs often lead to really subtle bugs that only occur
133 /// if another object happens to get allocated to the same address as the old
134 /// one. Using an AssertingVH ensures that an assert is triggered as soon as
135 /// the bad delete occurs.
137 /// Note that an AssertingVH handle does *not* follow values across RAUW
138 /// operations. This means that RAUW's need to explicitly update the
139 /// AssertingVH's as it moves. This is required because in non-assert mode this
140 /// class turns into a trivial wrapper around a pointer.
141 template <typename ValueTy = Value>
144 : public ValueHandleBase
149 ValueTy *getValPtr() const {
150 return static_cast<ValueTy*>(ValueHandleBase::getValPtr());
152 void setValPtr(ValueTy *P) {
153 ValueHandleBase::operator=(P);
157 ValueTy *getValPtr() const { return ThePtr; }
158 void setValPtr(ValueTy *P) { ThePtr = P; }
163 AssertingVH() : ValueHandleBase(Assert) {}
164 AssertingVH(ValueTy *P) : ValueHandleBase(Assert, P) {}
165 AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
167 AssertingVH() : ThePtr(0) {}
168 AssertingVH(ValueTy *P) : ThePtr(P) {}
171 operator ValueTy*() const {
175 ValueTy *operator=(ValueTy *RHS) {
179 ValueTy *operator=(AssertingVH<ValueTy> &RHS) {
180 setValPtr(RHS.getValPtr());
184 ValueTy *operator->() const { return getValPtr(); }
185 ValueTy &operator*() const { return getValPtr(); }
187 // Duplicate these from the base class so that they work when assertions are
189 bool operator==(const Value *RHS) const { return getValPtr() == RHS; }
190 bool operator!=(const Value *RHS) const { return getValPtr() != RHS; }
191 bool operator<(const Value *RHS) const { return getValPtr() < RHS; }
192 bool operator>(const Value *RHS) const { return getValPtr() > RHS; }
193 bool operator<=(const Value *RHS) const { return getValPtr() <= RHS; }
194 bool operator>=(const Value *RHS) const { return getValPtr() >= RHS; }
195 bool operator==(const AssertingVH &RHS) const {
196 return getValPtr() == RHS.getValPtr();
198 bool operator!=(const AssertingVH &RHS) const {
199 return getValPtr() != RHS.getValPtr();
201 bool operator<(const AssertingVH &RHS) const {
202 return getValPtr() < RHS.getValPtr();
204 bool operator>(const AssertingVH &RHS) const {
205 return getValPtr() > RHS.getValPtr();
207 bool operator<=(const AssertingVH &RHS) const {
208 return getValPtr() <= RHS.getValPtr();
210 bool operator>=(const AssertingVH &RHS) const {
211 return getValPtr() >= RHS.getValPtr();
215 } // End llvm namespace