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 sub class the handle actually is.
43 /// This is to avoid having a vtable for the light-weight handle pointers. The
44 /// fully general Callback version does have a vtable.
52 PointerIntPair<ValueHandleBase**, 2, HandleBaseKind> PrevPair;
53 ValueHandleBase *Next;
56 explicit ValueHandleBase(HandleBaseKind Kind)
57 : PrevPair(0, Kind), Next(0), VP(0) {}
58 ValueHandleBase(HandleBaseKind Kind, Value *V)
59 : PrevPair(0, Kind), Next(0), VP(V) {
63 ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
64 : PrevPair(0, Kind), Next(0), VP(RHS.VP) {
66 AddToExistingUseList(RHS.getPrevPtr());
73 Value *operator=(Value *RHS) {
74 if (VP == RHS) return RHS;
75 if (VP) RemoveFromUseList();
77 if (VP) AddToUseList();
81 Value *operator=(const ValueHandleBase &RHS) {
82 if (VP == RHS.VP) return RHS.VP;
83 if (VP) RemoveFromUseList();
85 if (VP) AddToExistingUseList(RHS.getPrevPtr());
89 Value *operator->() const { return getValPtr(); }
90 Value &operator*() const { return *getValPtr(); }
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) {}
126 operator Value*() const {
131 /// AssertingVH - This is a Value Handle that points to a value and asserts out
132 /// if the value is destroyed while the handle is still live. This is very
133 /// useful for catching dangling pointer bugs and other things which can be
134 /// non-obvious. One particularly useful place to use this is as the Key of a
135 /// map. Dangling pointer bugs often lead to really subtle bugs that only occur
136 /// if another object happens to get allocated to the same address as the old
137 /// one. Using an AssertingVH ensures that an assert is triggered as soon as
138 /// the bad delete occurs.
140 /// Note that an AssertingVH handle does *not* follow values across RAUW
141 /// operations. This means that RAUW's need to explicitly update the
142 /// AssertingVH's as it moves. This is required because in non-assert mode this
143 /// class turns into a trivial wrapper around a pointer.
144 template <typename ValueTy>
147 : public ValueHandleBase
152 ValueTy *getValPtr() const {
153 return static_cast<ValueTy*>(ValueHandleBase::getValPtr());
155 void setValPtr(ValueTy *P) {
156 ValueHandleBase::operator=(P);
160 ValueTy *getValPtr() const { return ThePtr; }
161 void setValPtr(ValueTy *P) { ThePtr = P; }
166 AssertingVH() : ValueHandleBase(Assert) {}
167 AssertingVH(ValueTy *P) : ValueHandleBase(Assert, P) {}
168 AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
170 AssertingVH() : ThePtr(0) {}
171 AssertingVH(ValueTy *P) : ThePtr(P) {}
174 operator ValueTy*() const {
178 ValueTy *operator=(ValueTy *RHS) {
182 ValueTy *operator=(AssertingVH<ValueTy> &RHS) {
183 setValPtr(RHS.getValPtr());
187 ValueTy *operator->() const { return getValPtr(); }
188 ValueTy &operator*() const { return *getValPtr(); }
191 /// CallbackVH - This is a value handle that allows subclasses to define
192 /// callbacks that run when the underlying Value has RAUW called on it or is
193 /// destroyed. This class can be used as the key of a map, as long as the user
194 /// takes it out of the map before calling setValPtr() (since the map has to
195 /// rearrange itself when the pointer changes). Unlike ValueHandleBase, this
196 /// class has a vtable and a virtual destructor.
197 class CallbackVH : public ValueHandleBase {
199 CallbackVH(const CallbackVH &RHS)
200 : ValueHandleBase(Callback, RHS) {}
202 virtual ~CallbackVH();
204 void setValPtr(Value *P) {
205 ValueHandleBase::operator=(P);
209 CallbackVH() : ValueHandleBase(Callback) {}
210 CallbackVH(Value *P) : ValueHandleBase(Callback, P) {}
212 operator Value*() const {
216 /// Called when this->getValPtr() is destroyed, inside ~Value(), so you may
217 /// call any non-virtual Value method on getValPtr(), but no subclass methods.
218 /// If WeakVH were implemented as a CallbackVH, it would use this method to
219 /// call setValPtr(NULL). AssertingVH would use this method to cause an
220 /// assertion failure.
222 /// All implementations must remove the reference from this object to the
223 /// Value that's being destroyed.
224 virtual void deleted() {
228 /// Called when this->getValPtr()->replaceAllUsesWith(new_value) is called,
229 /// _before_ any of the uses have actually been replaced. If WeakVH were
230 /// implemented as a CallbackVH, it would use this method to call
231 /// setValPtr(new_value). AssertingVH would do nothing in this method.
232 virtual void allUsesReplacedWith(Value *new_value) {}
235 } // End llvm namespace