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.
54 PointerIntPair<ValueHandleBase**, 2, HandleBaseKind> PrevPair;
55 ValueHandleBase *Next;
58 explicit ValueHandleBase(const ValueHandleBase&); // DO NOT IMPLEMENT.
60 explicit ValueHandleBase(HandleBaseKind Kind)
61 : PrevPair(0, Kind), Next(0), VP(0) {}
62 ValueHandleBase(HandleBaseKind Kind, Value *V)
63 : PrevPair(0, Kind), Next(0), VP(V) {
67 ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
68 : PrevPair(0, Kind), Next(0), VP(RHS.VP) {
70 AddToExistingUseList(RHS.getPrevPtr());
77 Value *operator=(Value *RHS) {
78 if (VP == RHS) return RHS;
79 if (isValid(VP)) RemoveFromUseList();
81 if (isValid(VP)) AddToUseList();
85 Value *operator=(const ValueHandleBase &RHS) {
86 if (VP == RHS.VP) return RHS.VP;
87 if (isValid(VP)) RemoveFromUseList();
89 if (isValid(VP)) AddToExistingUseList(RHS.getPrevPtr());
93 Value *operator->() const { return getValPtr(); }
94 Value &operator*() const { return *getValPtr(); }
97 Value *getValPtr() const { return VP; }
98 static bool isValid(Value *V) {
100 V != DenseMapInfo<Value *>::getEmptyKey() &&
101 V != DenseMapInfo<Value *>::getTombstoneKey();
105 // Callbacks made from Value.
106 static void ValueIsDeleted(Value *V);
107 static void ValueIsRAUWd(Value *Old, Value *New);
109 // Internal implementation details.
110 ValueHandleBase **getPrevPtr() const { return PrevPair.getPointer(); }
111 HandleBaseKind getKind() const { return PrevPair.getInt(); }
112 void setPrevPtr(ValueHandleBase **Ptr) { PrevPair.setPointer(Ptr); }
114 /// AddToExistingUseList - Add this ValueHandle to the use list for VP, where
115 /// List is the address of either the head of the list or a Next node within
116 /// the existing use list.
117 void AddToExistingUseList(ValueHandleBase **List);
119 /// AddToExistingUseListAfter - Add this ValueHandle to the use list after
121 void AddToExistingUseListAfter(ValueHandleBase *Node);
123 /// AddToUseList - Add this ValueHandle to the use list for VP.
125 /// RemoveFromUseList - Remove this ValueHandle from its current use list.
126 void RemoveFromUseList();
129 /// WeakVH - This is a value handle that tries hard to point to a Value, even
130 /// across RAUW operations, but will null itself out if the value is destroyed.
131 /// this is useful for advisory sorts of information, but should not be used as
132 /// the key of a map (since the map would have to rearrange itself when the
133 /// pointer changes).
134 class WeakVH : public ValueHandleBase {
136 WeakVH() : ValueHandleBase(Weak) {}
137 WeakVH(Value *P) : ValueHandleBase(Weak, P) {}
138 WeakVH(const WeakVH &RHS)
139 : ValueHandleBase(Weak, RHS) {}
141 Value *operator=(Value *RHS) {
142 return ValueHandleBase::operator=(RHS);
144 Value *operator=(const ValueHandleBase &RHS) {
145 return ValueHandleBase::operator=(RHS);
148 operator Value*() const {
153 // Specialize simplify_type to allow WeakVH to participate in
154 // dyn_cast, isa, etc.
155 template<typename From> struct simplify_type;
156 template<> struct simplify_type<const WeakVH> {
157 typedef Value* SimpleType;
158 static SimpleType getSimplifiedValue(const WeakVH &WVH) {
159 return static_cast<Value *>(WVH);
162 template<> struct simplify_type<WeakVH> : public simplify_type<const WeakVH> {};
164 /// AssertingVH - This is a Value Handle that points to a value and asserts out
165 /// if the value is destroyed while the handle is still live. This is very
166 /// useful for catching dangling pointer bugs and other things which can be
167 /// non-obvious. One particularly useful place to use this is as the Key of a
168 /// map. Dangling pointer bugs often lead to really subtle bugs that only occur
169 /// if another object happens to get allocated to the same address as the old
170 /// one. Using an AssertingVH ensures that an assert is triggered as soon as
171 /// the bad delete occurs.
173 /// Note that an AssertingVH handle does *not* follow values across RAUW
174 /// operations. This means that RAUW's need to explicitly update the
175 /// AssertingVH's as it moves. This is required because in non-assert mode this
176 /// class turns into a trivial wrapper around a pointer.
177 template <typename ValueTy>
180 : public ValueHandleBase
185 ValueTy *getValPtr() const {
186 return static_cast<ValueTy*>(ValueHandleBase::getValPtr());
188 void setValPtr(ValueTy *P) {
189 ValueHandleBase::operator=(GetAsValue(P));
193 ValueTy *getValPtr() const { return ThePtr; }
194 void setValPtr(ValueTy *P) { ThePtr = P; }
197 // Convert a ValueTy*, which may be const, to the type the base
199 static Value *GetAsValue(Value *V) { return V; }
200 static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
204 AssertingVH() : ValueHandleBase(Assert) {}
205 AssertingVH(ValueTy *P) : ValueHandleBase(Assert, GetAsValue(P)) {}
206 AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
208 AssertingVH() : ThePtr(0) {}
209 AssertingVH(ValueTy *P) : ThePtr(P) {}
212 operator ValueTy*() const {
216 ValueTy *operator=(ValueTy *RHS) {
220 ValueTy *operator=(const AssertingVH<ValueTy> &RHS) {
221 setValPtr(RHS.getValPtr());
225 ValueTy *operator->() const { return getValPtr(); }
226 ValueTy &operator*() const { return *getValPtr(); }
229 // Specialize simplify_type to allow AssertingVH to participate in
230 // dyn_cast, isa, etc.
231 template<typename From> struct simplify_type;
232 template<> struct simplify_type<const AssertingVH<Value> > {
233 typedef Value* SimpleType;
234 static SimpleType getSimplifiedValue(const AssertingVH<Value> &AVH) {
235 return static_cast<Value *>(AVH);
238 template<> struct simplify_type<AssertingVH<Value> >
239 : public simplify_type<const AssertingVH<Value> > {};
241 /// TrackingVH - This is a value handle that tracks a Value (or Value subclass),
242 /// even across RAUW operations.
244 /// TrackingVH is designed for situations where a client needs to hold a handle
245 /// to a Value (or subclass) across some operations which may move that value,
246 /// but should never destroy it or replace it with some unacceptable type.
248 /// It is an error to do anything with a TrackingVH whose value has been
249 /// destroyed, except to destruct it.
251 /// It is an error to attempt to replace a value with one of a type which is
252 /// incompatible with any of its outstanding TrackingVHs.
253 template<typename ValueTy>
254 class TrackingVH : public ValueHandleBase {
255 void CheckValidity() const {
256 Value *VP = ValueHandleBase::getValPtr();
258 // Null is always ok.
262 // Check that this value is valid (i.e., it hasn't been deleted). We
263 // explicitly delay this check until access to avoid requiring clients to be
264 // unnecessarily careful w.r.t. destruction.
265 assert(ValueHandleBase::isValid(VP) && "Tracked Value was deleted!");
267 // Check that the value is a member of the correct subclass. We would like
268 // to check this property on assignment for better debugging, but we don't
269 // want to require a virtual interface on this VH. Instead we allow RAUW to
270 // replace this value with a value of an invalid type, and check it here.
271 assert(isa<ValueTy>(VP) &&
272 "Tracked Value was replaced by one with an invalid type!");
275 ValueTy *getValPtr() const {
277 return static_cast<ValueTy*>(ValueHandleBase::getValPtr());
279 void setValPtr(ValueTy *P) {
281 ValueHandleBase::operator=(GetAsValue(P));
284 // Convert a ValueTy*, which may be const, to the type the base
286 static Value *GetAsValue(Value *V) { return V; }
287 static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
290 TrackingVH() : ValueHandleBase(Tracking) {}
291 TrackingVH(ValueTy *P) : ValueHandleBase(Tracking, P) {}
292 TrackingVH(const TrackingVH &RHS) : ValueHandleBase(Tracking, RHS) {}
294 operator ValueTy*() const {
298 ValueTy *operator=(ValueTy *RHS) {
302 ValueTy *operator=(const TrackingVH<ValueTy> &RHS) {
303 setValPtr(RHS.getValPtr());
307 ValueTy *operator->() const { return getValPtr(); }
308 ValueTy &operator*() const { return *getValPtr(); }
311 // Specialize simplify_type to allow TrackingVH to participate in
312 // dyn_cast, isa, etc.
313 template<typename From> struct simplify_type;
314 template<> struct simplify_type<const TrackingVH<Value> > {
315 typedef Value* SimpleType;
316 static SimpleType getSimplifiedValue(const TrackingVH<Value> &AVH) {
317 return static_cast<Value *>(AVH);
320 template<> struct simplify_type<TrackingVH<Value> >
321 : public simplify_type<const TrackingVH<Value> > {};
323 /// CallbackVH - This is a value handle that allows subclasses to define
324 /// callbacks that run when the underlying Value has RAUW called on it or is
325 /// destroyed. This class can be used as the key of a map, as long as the user
326 /// takes it out of the map before calling setValPtr() (since the map has to
327 /// rearrange itself when the pointer changes). Unlike ValueHandleBase, this
328 /// class has a vtable and a virtual destructor.
329 class CallbackVH : public ValueHandleBase {
331 CallbackVH(const CallbackVH &RHS)
332 : ValueHandleBase(Callback, RHS) {}
334 virtual ~CallbackVH();
336 void setValPtr(Value *P) {
337 ValueHandleBase::operator=(P);
341 CallbackVH() : ValueHandleBase(Callback) {}
342 CallbackVH(Value *P) : ValueHandleBase(Callback, P) {}
344 operator Value*() const {
348 /// Called when this->getValPtr() is destroyed, inside ~Value(), so you may
349 /// call any non-virtual Value method on getValPtr(), but no subclass methods.
350 /// If WeakVH were implemented as a CallbackVH, it would use this method to
351 /// call setValPtr(NULL). AssertingVH would use this method to cause an
352 /// assertion failure.
354 /// All implementations must remove the reference from this object to the
355 /// Value that's being destroyed.
356 virtual void deleted() {
360 /// Called when this->getValPtr()->replaceAllUsesWith(new_value) is called,
361 /// _before_ any of the uses have actually been replaced. If WeakVH were
362 /// implemented as a CallbackVH, it would use this method to call
363 /// setValPtr(new_value). AssertingVH would do nothing in this method.
364 virtual void allUsesReplacedWith(Value *) {}
367 // Specialize simplify_type to allow CallbackVH to participate in
368 // dyn_cast, isa, etc.
369 template<typename From> struct simplify_type;
370 template<> struct simplify_type<const CallbackVH> {
371 typedef Value* SimpleType;
372 static SimpleType getSimplifiedValue(const CallbackVH &CVH) {
373 return static_cast<Value *>(CVH);
376 template<> struct simplify_type<CallbackVH>
377 : public simplify_type<const CallbackVH> {};
379 } // End llvm namespace