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"
21 class ValueHandleBase;
23 // ValueHandleBase** is only 4-byte aligned.
25 class PointerLikeTypeTraits<ValueHandleBase**> {
27 static inline void *getAsVoidPointer(ValueHandleBase** P) { return P; }
28 static inline ValueHandleBase **getFromVoidPointer(void *P) {
29 return static_cast<ValueHandleBase**>(P);
31 enum { NumLowBitsAvailable = 2 };
34 /// ValueHandleBase - This is the common base class of value handles.
35 /// ValueHandle's are smart pointers to Value's that have special behavior when
36 /// the value is deleted or ReplaceAllUsesWith'd. See the specific handles
37 /// below for details.
39 class ValueHandleBase {
42 /// HandleBaseKind - This indicates what base class the handle actually is.
43 /// This is to avoid having a vtable for the light-weight handle pointers. The
44 /// fully generally Callback version does have a vtable.
52 PointerIntPair<ValueHandleBase**, 2, HandleBaseKind> PrevPair;
53 ValueHandleBase *Next;
56 ValueHandleBase(HandleBaseKind Kind) : PrevPair(0, Kind), Next(0), VP(0) {}
57 ValueHandleBase(HandleBaseKind Kind, Value *V)
58 : PrevPair(0, Kind), Next(0), VP(V) {
62 ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
63 : PrevPair(0, Kind), Next(0), VP(RHS.VP) {
65 AddToExistingUseList(RHS.getPrevPtr());
72 Value *operator=(Value *RHS) {
73 if (VP == RHS) return RHS;
74 if (VP) RemoveFromUseList();
76 if (VP) AddToUseList();
80 Value *operator=(const ValueHandleBase &RHS) {
81 if (VP == RHS.VP) return RHS.VP;
82 if (VP) RemoveFromUseList();
84 if (VP) AddToExistingUseList(RHS.getPrevPtr());
88 Value *operator->() const { return getValPtr(); }
89 Value &operator*() const { return *getValPtr(); }
91 bool operator==(const Value *RHS) const { return VP == RHS; }
92 bool operator==(const ValueHandleBase &RHS) const { return VP == RHS.VP; }
93 bool operator!=(const Value *RHS) const { return VP != RHS; }
94 bool operator!=(const ValueHandleBase &RHS) const { return VP != RHS.VP; }
95 bool operator<(const Value *RHS) const { return VP < RHS; }
96 bool operator<(const ValueHandleBase &RHS) const { return VP < RHS.VP; }
97 bool operator>(const Value *RHS) const { return VP > RHS; }
98 bool operator>(const ValueHandleBase &RHS) const { return VP > RHS.VP; }
99 bool operator<=(const Value *RHS) const { return VP <= RHS; }
100 bool operator<=(const ValueHandleBase &RHS) const { return VP <= RHS.VP; }
101 bool operator>=(const Value *RHS) const { return VP >= RHS; }
102 bool operator>=(const ValueHandleBase &RHS) const { return VP >= RHS.VP; }
105 Value *getValPtr() const { return VP; }
107 // Callbacks made from Value.
108 static void ValueIsDeleted(Value *V);
109 static void ValueIsRAUWd(Value *Old, Value *New);
111 // Internal implementation details.
112 ValueHandleBase **getPrevPtr() const { return PrevPair.getPointer(); }
113 HandleBaseKind getKind() const { return PrevPair.getInt(); }
114 void setPrevPtr(ValueHandleBase **Ptr) { PrevPair.setPointer(Ptr); }
116 /// AddToUseList - Add this ValueHandle to the use list for VP, where List is
117 /// known to point into the existing use list.
118 void AddToExistingUseList(ValueHandleBase **List);
120 /// AddToUseList - Add this ValueHandle to the use list for VP.
122 /// RemoveFromUseList - Remove this ValueHandle from its current use list.
123 void RemoveFromUseList();
126 /// WeakVH - This is a value handle that tries hard to point to a Value, even
127 /// across RAUW operations, but will null itself out if the value is destroyed.
128 /// this is useful for advisory sorts of information, but should not be used as
129 /// the key of a map (since the map would have to rearrange itself when the
130 /// pointer changes).
131 class WeakVH : public ValueHandleBase {
133 WeakVH() : ValueHandleBase(Weak) {}
134 WeakVH(Value *P) : ValueHandleBase(Weak, P) {}
135 WeakVH(const WeakVH &RHS)
136 : ValueHandleBase(Weak, RHS) {}
138 operator Value*() const {
143 /// AssertingVH - This is a Value Handle that points to a value and asserts out
144 /// if the value is destroyed while the handle is still live. This is very
145 /// useful for catching dangling pointer bugs and other things which can be
146 /// non-obvious. One particularly useful place to use this is as the Key of a
147 /// map. Dangling pointer bugs often lead to really subtle bugs that only occur
148 /// if another object happens to get allocated to the same address as the old
149 /// one. Using an AssertingVH ensures that an assert is triggered as soon as
150 /// the bad delete occurs.
152 /// Note that an AssertingVH handle does *not* follow values across RAUW
153 /// operations. This means that RAUW's need to explicitly update the
154 /// AssertingVH's as it moves. This is required because in non-assert mode this
155 /// class turns into a trivial wrapper around a pointer.
156 template <typename ValueTy>
159 : public ValueHandleBase
164 ValueTy *getValPtr() const {
165 return static_cast<ValueTy*>(ValueHandleBase::getValPtr());
167 void setValPtr(ValueTy *P) {
168 ValueHandleBase::operator=(P);
172 ValueTy *getValPtr() const { return ThePtr; }
173 void setValPtr(ValueTy *P) { ThePtr = P; }
178 AssertingVH() : ValueHandleBase(Assert) {}
179 AssertingVH(ValueTy *P) : ValueHandleBase(Assert, P) {}
180 AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
182 AssertingVH() : ThePtr(0) {}
183 AssertingVH(ValueTy *P) : ThePtr(P) {}
186 operator ValueTy*() const {
190 ValueTy *operator=(ValueTy *RHS) {
194 ValueTy *operator=(AssertingVH<ValueTy> &RHS) {
195 setValPtr(RHS.getValPtr());
199 ValueTy *operator->() const { return getValPtr(); }
200 ValueTy &operator*() const { return *getValPtr(); }
202 // Duplicate these from the base class so that they work when assertions are
204 bool operator==(const Value *RHS) const { return getValPtr() == RHS; }
205 bool operator!=(const Value *RHS) const { return getValPtr() != RHS; }
206 bool operator<(const Value *RHS) const { return getValPtr() < RHS; }
207 bool operator>(const Value *RHS) const { return getValPtr() > RHS; }
208 bool operator<=(const Value *RHS) const { return getValPtr() <= RHS; }
209 bool operator>=(const Value *RHS) const { return getValPtr() >= RHS; }
210 bool operator==(const AssertingVH &RHS) const {
211 return getValPtr() == RHS.getValPtr();
213 bool operator!=(const AssertingVH &RHS) const {
214 return getValPtr() != RHS.getValPtr();
216 bool operator<(const AssertingVH &RHS) const {
217 return getValPtr() < RHS.getValPtr();
219 bool operator>(const AssertingVH &RHS) const {
220 return getValPtr() > RHS.getValPtr();
222 bool operator<=(const AssertingVH &RHS) const {
223 return getValPtr() <= RHS.getValPtr();
225 bool operator>=(const AssertingVH &RHS) const {
226 return getValPtr() >= RHS.getValPtr();
230 } // End llvm namespace