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/DenseMapInfo.h"
18 #include "llvm/ADT/PointerIntPair.h"
19 #include "llvm/Value.h"
22 class ValueHandleBase;
24 // ValueHandleBase** is only 4-byte aligned.
26 class PointerLikeTypeTraits<ValueHandleBase**> {
28 static inline void *getAsVoidPointer(ValueHandleBase** P) { return P; }
29 static inline ValueHandleBase **getFromVoidPointer(void *P) {
30 return static_cast<ValueHandleBase**>(P);
32 enum { NumLowBitsAvailable = 2 };
35 /// ValueHandleBase - This is the common base class of value handles.
36 /// ValueHandle's are smart pointers to Value's that have special behavior when
37 /// the value is deleted or ReplaceAllUsesWith'd. See the specific handles
38 /// below for details.
40 class ValueHandleBase {
43 /// HandleBaseKind - This indicates what sub class the handle actually is.
44 /// This is to avoid having a vtable for the light-weight handle pointers. The
45 /// fully general Callback version does have a vtable.
53 PointerIntPair<ValueHandleBase**, 2, HandleBaseKind> PrevPair;
54 ValueHandleBase *Next;
57 explicit ValueHandleBase(HandleBaseKind Kind)
58 : PrevPair(0, Kind), Next(0), VP(0) {}
59 ValueHandleBase(HandleBaseKind Kind, Value *V)
60 : PrevPair(0, Kind), Next(0), VP(V) {
64 ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
65 : PrevPair(0, Kind), Next(0), VP(RHS.VP) {
67 AddToExistingUseList(RHS.getPrevPtr());
74 Value *operator=(Value *RHS) {
75 if (VP == RHS) return RHS;
76 if (isValid(VP)) RemoveFromUseList();
78 if (isValid(VP)) AddToUseList();
82 Value *operator=(const ValueHandleBase &RHS) {
83 if (VP == RHS.VP) return RHS.VP;
84 if (isValid(VP)) RemoveFromUseList();
86 if (isValid(VP)) AddToExistingUseList(RHS.getPrevPtr());
90 Value *operator->() const { return getValPtr(); }
91 Value &operator*() const { return *getValPtr(); }
94 Value *getValPtr() const { return VP; }
96 static bool isValid(Value *V) {
98 V != DenseMapInfo<Value *>::getEmptyKey() &&
99 V != DenseMapInfo<Value *>::getTombstoneKey();
102 // Callbacks made from Value.
103 static void ValueIsDeleted(Value *V);
104 static void ValueIsRAUWd(Value *Old, Value *New);
106 // Internal implementation details.
107 ValueHandleBase **getPrevPtr() const { return PrevPair.getPointer(); }
108 HandleBaseKind getKind() const { return PrevPair.getInt(); }
109 void setPrevPtr(ValueHandleBase **Ptr) { PrevPair.setPointer(Ptr); }
111 /// AddToExistingUseList - Add this ValueHandle to the use list for VP,
112 /// where List is known to point into the existing use list.
113 void AddToExistingUseList(ValueHandleBase **List);
115 /// AddToUseList - Add this ValueHandle to the use list for VP.
117 /// RemoveFromUseList - Remove this ValueHandle from its current use list.
118 void RemoveFromUseList();
121 /// WeakVH - This is a value handle that tries hard to point to a Value, even
122 /// across RAUW operations, but will null itself out if the value is destroyed.
123 /// this is useful for advisory sorts of information, but should not be used as
124 /// the key of a map (since the map would have to rearrange itself when the
125 /// pointer changes).
126 class WeakVH : public ValueHandleBase {
128 WeakVH() : ValueHandleBase(Weak) {}
129 WeakVH(Value *P) : ValueHandleBase(Weak, P) {}
130 WeakVH(const WeakVH &RHS)
131 : ValueHandleBase(Weak, RHS) {}
133 operator Value*() const {
138 // Specialize simplify_type to allow WeakVH to participate in
139 // dyn_cast, isa, etc.
140 template<typename From> struct simplify_type;
141 template<> struct simplify_type<const WeakVH> {
142 typedef Value* SimpleType;
143 static SimpleType getSimplifiedValue(const WeakVH &WVH) {
144 return static_cast<Value *>(WVH);
147 template<> struct simplify_type<WeakVH> : public simplify_type<const WeakVH> {};
149 /// AssertingVH - This is a Value Handle that points to a value and asserts out
150 /// if the value is destroyed while the handle is still live. This is very
151 /// useful for catching dangling pointer bugs and other things which can be
152 /// non-obvious. One particularly useful place to use this is as the Key of a
153 /// map. Dangling pointer bugs often lead to really subtle bugs that only occur
154 /// if another object happens to get allocated to the same address as the old
155 /// one. Using an AssertingVH ensures that an assert is triggered as soon as
156 /// the bad delete occurs.
158 /// Note that an AssertingVH handle does *not* follow values across RAUW
159 /// operations. This means that RAUW's need to explicitly update the
160 /// AssertingVH's as it moves. This is required because in non-assert mode this
161 /// class turns into a trivial wrapper around a pointer.
162 template <typename ValueTy>
165 : public ValueHandleBase
170 ValueTy *getValPtr() const {
171 return static_cast<ValueTy*>(ValueHandleBase::getValPtr());
173 void setValPtr(ValueTy *P) {
174 ValueHandleBase::operator=(GetAsValue(P));
178 ValueTy *getValPtr() const { return ThePtr; }
179 void setValPtr(ValueTy *P) { ThePtr = P; }
182 // Convert a ValueTy*, which may be const, to the type the base
184 static Value *GetAsValue(Value *V) { return V; }
185 static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
189 AssertingVH() : ValueHandleBase(Assert) {}
190 AssertingVH(ValueTy *P) : ValueHandleBase(Assert, GetAsValue(P)) {}
191 AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
193 AssertingVH() : ThePtr(0) {}
194 AssertingVH(ValueTy *P) : ThePtr(P) {}
197 operator ValueTy*() const {
201 ValueTy *operator=(ValueTy *RHS) {
205 ValueTy *operator=(const AssertingVH<ValueTy> &RHS) {
206 setValPtr(RHS.getValPtr());
210 ValueTy *operator->() const { return getValPtr(); }
211 ValueTy &operator*() const { return *getValPtr(); }
214 // Specialize simplify_type to allow AssertingVH to participate in
215 // dyn_cast, isa, etc.
216 template<typename From> struct simplify_type;
217 template<> struct simplify_type<const AssertingVH<Value> > {
218 typedef Value* SimpleType;
219 static SimpleType getSimplifiedValue(const AssertingVH<Value> &AVH) {
220 return static_cast<Value *>(AVH);
223 template<> struct simplify_type<AssertingVH<Value> >
224 : public simplify_type<const AssertingVH<Value> > {};
226 /// CallbackVH - This is a value handle that allows subclasses to define
227 /// callbacks that run when the underlying Value has RAUW called on it or is
228 /// destroyed. This class can be used as the key of a map, as long as the user
229 /// takes it out of the map before calling setValPtr() (since the map has to
230 /// rearrange itself when the pointer changes). Unlike ValueHandleBase, this
231 /// class has a vtable and a virtual destructor.
232 class CallbackVH : public ValueHandleBase {
234 CallbackVH(const CallbackVH &RHS)
235 : ValueHandleBase(Callback, RHS) {}
237 virtual ~CallbackVH();
239 void setValPtr(Value *P) {
240 ValueHandleBase::operator=(P);
244 CallbackVH() : ValueHandleBase(Callback) {}
245 CallbackVH(Value *P) : ValueHandleBase(Callback, P) {}
247 operator Value*() const {
251 /// Called when this->getValPtr() is destroyed, inside ~Value(), so you may
252 /// call any non-virtual Value method on getValPtr(), but no subclass methods.
253 /// If WeakVH were implemented as a CallbackVH, it would use this method to
254 /// call setValPtr(NULL). AssertingVH would use this method to cause an
255 /// assertion failure.
257 /// All implementations must remove the reference from this object to the
258 /// Value that's being destroyed.
259 virtual void deleted() {
263 /// Called when this->getValPtr()->replaceAllUsesWith(new_value) is called,
264 /// _before_ any of the uses have actually been replaced. If WeakVH were
265 /// implemented as a CallbackVH, it would use this method to call
266 /// setValPtr(new_value). AssertingVH would do nothing in this method.
267 virtual void allUsesReplacedWith(Value *new_value) {}
270 // Specialize simplify_type to allow CallbackVH to participate in
271 // dyn_cast, isa, etc.
272 template<typename From> struct simplify_type;
273 template<> struct simplify_type<const CallbackVH> {
274 typedef Value* SimpleType;
275 static SimpleType getSimplifiedValue(const CallbackVH &CVH) {
276 return static_cast<Value *>(CVH);
279 template<> struct simplify_type<CallbackVH>
280 : public simplify_type<const CallbackVH> {};
282 } // End llvm namespace