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 // Specialize DenseMapInfo to allow AssertingVH to participate in DenseMap.
243 struct DenseMapInfo<AssertingVH<T> > {
244 typedef DenseMapInfo<T*> PointerInfo;
245 static inline AssertingVH<T> getEmptyKey() {
246 return AssertingVH<T>(PointerInfo::getEmptyKey());
248 static inline T* getTombstoneKey() {
249 return AssertingVH<T>(PointerInfo::getTombstoneKey());
251 static unsigned getHashValue(const AssertingVH<T> &Val) {
252 return PointerInfo::getHashValue(Val);
254 static bool isEqual(const AssertingVH<T> &LHS, const AssertingVH<T> &RHS) {
259 template <typename T>
260 struct isPodLike<AssertingVH<T> > {
262 static const bool value = true;
264 static const bool value = false;
269 /// TrackingVH - This is a value handle that tracks a Value (or Value subclass),
270 /// even across RAUW operations.
272 /// TrackingVH is designed for situations where a client needs to hold a handle
273 /// to a Value (or subclass) across some operations which may move that value,
274 /// but should never destroy it or replace it with some unacceptable type.
276 /// It is an error to do anything with a TrackingVH whose value has been
277 /// destroyed, except to destruct it.
279 /// It is an error to attempt to replace a value with one of a type which is
280 /// incompatible with any of its outstanding TrackingVHs.
281 template<typename ValueTy>
282 class TrackingVH : public ValueHandleBase {
283 void CheckValidity() const {
284 Value *VP = ValueHandleBase::getValPtr();
286 // Null is always ok.
289 // Check that this value is valid (i.e., it hasn't been deleted). We
290 // explicitly delay this check until access to avoid requiring clients to be
291 // unnecessarily careful w.r.t. destruction.
292 assert(ValueHandleBase::isValid(VP) && "Tracked Value was deleted!");
294 // Check that the value is a member of the correct subclass. We would like
295 // to check this property on assignment for better debugging, but we don't
296 // want to require a virtual interface on this VH. Instead we allow RAUW to
297 // replace this value with a value of an invalid type, and check it here.
298 assert(isa<ValueTy>(VP) &&
299 "Tracked Value was replaced by one with an invalid type!");
302 ValueTy *getValPtr() const {
304 return (ValueTy*)ValueHandleBase::getValPtr();
306 void setValPtr(ValueTy *P) {
308 ValueHandleBase::operator=(GetAsValue(P));
311 // Convert a ValueTy*, which may be const, to the type the base
313 static Value *GetAsValue(Value *V) { return V; }
314 static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
317 TrackingVH() : ValueHandleBase(Tracking) {}
318 TrackingVH(ValueTy *P) : ValueHandleBase(Tracking, GetAsValue(P)) {}
319 TrackingVH(const TrackingVH &RHS) : ValueHandleBase(Tracking, RHS) {}
321 operator ValueTy*() const {
325 ValueTy *operator=(ValueTy *RHS) {
329 ValueTy *operator=(const TrackingVH<ValueTy> &RHS) {
330 setValPtr(RHS.getValPtr());
334 ValueTy *operator->() const { return getValPtr(); }
335 ValueTy &operator*() const { return *getValPtr(); }
338 // Specialize simplify_type to allow TrackingVH to participate in
339 // dyn_cast, isa, etc.
340 template<typename From> struct simplify_type;
341 template<> struct simplify_type<const TrackingVH<Value> > {
342 typedef Value* SimpleType;
343 static SimpleType getSimplifiedValue(const TrackingVH<Value> &AVH) {
344 return static_cast<Value *>(AVH);
347 template<> struct simplify_type<TrackingVH<Value> >
348 : public simplify_type<const TrackingVH<Value> > {};
350 /// CallbackVH - This is a value handle that allows subclasses to define
351 /// callbacks that run when the underlying Value has RAUW called on it or is
352 /// destroyed. This class can be used as the key of a map, as long as the user
353 /// takes it out of the map before calling setValPtr() (since the map has to
354 /// rearrange itself when the pointer changes). Unlike ValueHandleBase, this
355 /// class has a vtable and a virtual destructor.
356 class CallbackVH : public ValueHandleBase {
358 CallbackVH(const CallbackVH &RHS)
359 : ValueHandleBase(Callback, RHS) {}
361 virtual ~CallbackVH();
363 void setValPtr(Value *P) {
364 ValueHandleBase::operator=(P);
368 CallbackVH() : ValueHandleBase(Callback) {}
369 CallbackVH(Value *P) : ValueHandleBase(Callback, P) {}
371 operator Value*() const {
375 /// Called when this->getValPtr() is destroyed, inside ~Value(), so you may
376 /// call any non-virtual Value method on getValPtr(), but no subclass methods.
377 /// If WeakVH were implemented as a CallbackVH, it would use this method to
378 /// call setValPtr(NULL). AssertingVH would use this method to cause an
379 /// assertion failure.
381 /// All implementations must remove the reference from this object to the
382 /// Value that's being destroyed.
383 virtual void deleted() {
387 /// Called when this->getValPtr()->replaceAllUsesWith(new_value) is called,
388 /// _before_ any of the uses have actually been replaced. If WeakVH were
389 /// implemented as a CallbackVH, it would use this method to call
390 /// setValPtr(new_value). AssertingVH would do nothing in this method.
391 virtual void allUsesReplacedWith(Value *) {}
394 // Specialize simplify_type to allow CallbackVH to participate in
395 // dyn_cast, isa, etc.
396 template<typename From> struct simplify_type;
397 template<> struct simplify_type<const CallbackVH> {
398 typedef Value* SimpleType;
399 static SimpleType getSimplifiedValue(const CallbackVH &CVH) {
400 return static_cast<Value *>(CVH);
403 template<> struct simplify_type<CallbackVH>
404 : public simplify_type<const CallbackVH> {};
406 } // End llvm namespace