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 // Specialize simplify_type to allow WeakVH to participate in
132 // dyn_cast, isa, etc.
133 template<typename From> struct simplify_type;
134 template<> struct simplify_type<const WeakVH> {
135 typedef Value* SimpleType;
136 static SimpleType getSimplifiedValue(const WeakVH &WVH) {
137 return static_cast<Value *>(WVH);
140 template<> struct simplify_type<WeakVH> : public simplify_type<const WeakVH> {};
142 /// AssertingVH - This is a Value Handle that points to a value and asserts out
143 /// if the value is destroyed while the handle is still live. This is very
144 /// useful for catching dangling pointer bugs and other things which can be
145 /// non-obvious. One particularly useful place to use this is as the Key of a
146 /// map. Dangling pointer bugs often lead to really subtle bugs that only occur
147 /// if another object happens to get allocated to the same address as the old
148 /// one. Using an AssertingVH ensures that an assert is triggered as soon as
149 /// the bad delete occurs.
151 /// Note that an AssertingVH handle does *not* follow values across RAUW
152 /// operations. This means that RAUW's need to explicitly update the
153 /// AssertingVH's as it moves. This is required because in non-assert mode this
154 /// class turns into a trivial wrapper around a pointer.
155 template <typename ValueTy>
158 : public ValueHandleBase
163 ValueTy *getValPtr() const {
164 return static_cast<ValueTy*>(ValueHandleBase::getValPtr());
166 void setValPtr(ValueTy *P) {
167 ValueHandleBase::operator=(P);
171 ValueTy *getValPtr() const { return ThePtr; }
172 void setValPtr(ValueTy *P) { ThePtr = P; }
177 AssertingVH() : ValueHandleBase(Assert) {}
178 AssertingVH(ValueTy *P) : ValueHandleBase(Assert, P) {}
179 AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
181 AssertingVH() : ThePtr(0) {}
182 AssertingVH(ValueTy *P) : ThePtr(P) {}
185 operator ValueTy*() const {
189 ValueTy *operator=(ValueTy *RHS) {
193 ValueTy *operator=(AssertingVH<ValueTy> &RHS) {
194 setValPtr(RHS.getValPtr());
198 ValueTy *operator->() const { return getValPtr(); }
199 ValueTy &operator*() const { return *getValPtr(); }
202 // Specialize simplify_type to allow AssertingVH to participate in
203 // dyn_cast, isa, etc.
204 template<typename From> struct simplify_type;
205 template<> struct simplify_type<const AssertingVH<Value> > {
206 typedef Value* SimpleType;
207 static SimpleType getSimplifiedValue(const AssertingVH<Value> &AVH) {
208 return static_cast<Value *>(AVH);
211 template<> struct simplify_type<AssertingVH<Value> >
212 : public simplify_type<const AssertingVH<Value> > {};
214 /// CallbackVH - This is a value handle that allows subclasses to define
215 /// callbacks that run when the underlying Value has RAUW called on it or is
216 /// destroyed. This class can be used as the key of a map, as long as the user
217 /// takes it out of the map before calling setValPtr() (since the map has to
218 /// rearrange itself when the pointer changes). Unlike ValueHandleBase, this
219 /// class has a vtable and a virtual destructor.
220 class CallbackVH : public ValueHandleBase {
222 CallbackVH(const CallbackVH &RHS)
223 : ValueHandleBase(Callback, RHS) {}
225 virtual ~CallbackVH();
227 void setValPtr(Value *P) {
228 ValueHandleBase::operator=(P);
232 CallbackVH() : ValueHandleBase(Callback) {}
233 CallbackVH(Value *P) : ValueHandleBase(Callback, P) {}
235 operator Value*() const {
239 /// Called when this->getValPtr() is destroyed, inside ~Value(), so you may
240 /// call any non-virtual Value method on getValPtr(), but no subclass methods.
241 /// If WeakVH were implemented as a CallbackVH, it would use this method to
242 /// call setValPtr(NULL). AssertingVH would use this method to cause an
243 /// assertion failure.
245 /// All implementations must remove the reference from this object to the
246 /// Value that's being destroyed.
247 virtual void deleted() {
251 /// Called when this->getValPtr()->replaceAllUsesWith(new_value) is called,
252 /// _before_ any of the uses have actually been replaced. If WeakVH were
253 /// implemented as a CallbackVH, it would use this method to call
254 /// setValPtr(new_value). AssertingVH would do nothing in this method.
255 virtual void allUsesReplacedWith(Value *new_value) {}
258 // Specialize simplify_type to allow CallbackVH to participate in
259 // dyn_cast, isa, etc.
260 template<typename From> struct simplify_type;
261 template<> struct simplify_type<const CallbackVH> {
262 typedef Value* SimpleType;
263 static SimpleType getSimplifiedValue(const CallbackVH &CVH) {
264 return static_cast<Value *>(CVH);
267 template<> struct simplify_type<CallbackVH>
268 : public simplify_type<const CallbackVH> {};
270 } // End llvm namespace