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;
57 // A subclass may want to store some information along with the value
58 // pointer. Allow them to do this by making the value pointer a pointer-int
59 // pair. The 'setValPtrInt' and 'getValPtrInt' methods below give them this
61 PointerIntPair<Value*, 2> VP;
63 explicit ValueHandleBase(const ValueHandleBase&); // DO NOT IMPLEMENT.
65 explicit ValueHandleBase(HandleBaseKind Kind)
66 : PrevPair(0, Kind), Next(0), VP(0, 0) {}
67 ValueHandleBase(HandleBaseKind Kind, Value *V)
68 : PrevPair(0, Kind), Next(0), VP(V, 0) {
69 if (isValid(VP.getPointer()))
72 ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
73 : PrevPair(0, Kind), Next(0), VP(RHS.VP) {
74 if (isValid(VP.getPointer()))
75 AddToExistingUseList(RHS.getPrevPtr());
78 if (isValid(VP.getPointer()))
82 Value *operator=(Value *RHS) {
83 if (VP.getPointer() == RHS) return RHS;
84 if (isValid(VP.getPointer())) RemoveFromUseList();
86 if (isValid(VP.getPointer())) AddToUseList();
90 Value *operator=(const ValueHandleBase &RHS) {
91 if (VP.getPointer() == RHS.VP.getPointer()) return RHS.VP.getPointer();
92 if (isValid(VP.getPointer())) RemoveFromUseList();
93 VP.setPointer(RHS.VP.getPointer());
94 if (isValid(VP.getPointer())) AddToExistingUseList(RHS.getPrevPtr());
95 return VP.getPointer();
98 Value *operator->() const { return getValPtr(); }
99 Value &operator*() const { return *getValPtr(); }
102 Value *getValPtr() const { return VP.getPointer(); }
104 void setValPtrInt(unsigned K) { VP.setInt(K); }
105 unsigned getValPtrInt() const { return VP.getInt(); }
107 static bool isValid(Value *V) {
109 V != DenseMapInfo<Value *>::getEmptyKey() &&
110 V != DenseMapInfo<Value *>::getTombstoneKey();
114 // Callbacks made from Value.
115 static void ValueIsDeleted(Value *V);
116 static void ValueIsRAUWd(Value *Old, Value *New);
118 // Internal implementation details.
119 ValueHandleBase **getPrevPtr() const { return PrevPair.getPointer(); }
120 HandleBaseKind getKind() const { return PrevPair.getInt(); }
121 void setPrevPtr(ValueHandleBase **Ptr) { PrevPair.setPointer(Ptr); }
123 /// AddToExistingUseList - Add this ValueHandle to the use list for VP, where
124 /// List is the address of either the head of the list or a Next node within
125 /// the existing use list.
126 void AddToExistingUseList(ValueHandleBase **List);
128 /// AddToExistingUseListAfter - Add this ValueHandle to the use list after
130 void AddToExistingUseListAfter(ValueHandleBase *Node);
132 /// AddToUseList - Add this ValueHandle to the use list for VP.
134 /// RemoveFromUseList - Remove this ValueHandle from its current use list.
135 void RemoveFromUseList();
138 /// WeakVH - This is a value handle that tries hard to point to a Value, even
139 /// across RAUW operations, but will null itself out if the value is destroyed.
140 /// this is useful for advisory sorts of information, but should not be used as
141 /// the key of a map (since the map would have to rearrange itself when the
142 /// pointer changes).
143 class WeakVH : public ValueHandleBase {
145 WeakVH() : ValueHandleBase(Weak) {}
146 WeakVH(Value *P) : ValueHandleBase(Weak, P) {}
147 WeakVH(const WeakVH &RHS)
148 : ValueHandleBase(Weak, RHS) {}
150 Value *operator=(Value *RHS) {
151 return ValueHandleBase::operator=(RHS);
153 Value *operator=(const ValueHandleBase &RHS) {
154 return ValueHandleBase::operator=(RHS);
157 operator Value*() const {
162 // Specialize simplify_type to allow WeakVH to participate in
163 // dyn_cast, isa, etc.
164 template<typename From> struct simplify_type;
165 template<> struct simplify_type<const WeakVH> {
166 typedef Value* SimpleType;
167 static SimpleType getSimplifiedValue(const WeakVH &WVH) {
168 return static_cast<Value *>(WVH);
171 template<> struct simplify_type<WeakVH> : public simplify_type<const WeakVH> {};
173 /// AssertingVH - This is a Value Handle that points to a value and asserts out
174 /// if the value is destroyed while the handle is still live. This is very
175 /// useful for catching dangling pointer bugs and other things which can be
176 /// non-obvious. One particularly useful place to use this is as the Key of a
177 /// map. Dangling pointer bugs often lead to really subtle bugs that only occur
178 /// if another object happens to get allocated to the same address as the old
179 /// one. Using an AssertingVH ensures that an assert is triggered as soon as
180 /// the bad delete occurs.
182 /// Note that an AssertingVH handle does *not* follow values across RAUW
183 /// operations. This means that RAUW's need to explicitly update the
184 /// AssertingVH's as it moves. This is required because in non-assert mode this
185 /// class turns into a trivial wrapper around a pointer.
186 template <typename ValueTy>
189 : public ValueHandleBase
194 ValueTy *getValPtr() const {
195 return static_cast<ValueTy*>(ValueHandleBase::getValPtr());
197 void setValPtr(ValueTy *P) {
198 ValueHandleBase::operator=(GetAsValue(P));
202 ValueTy *getValPtr() const { return ThePtr; }
203 void setValPtr(ValueTy *P) { ThePtr = P; }
206 // Convert a ValueTy*, which may be const, to the type the base
208 static Value *GetAsValue(Value *V) { return V; }
209 static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
213 AssertingVH() : ValueHandleBase(Assert) {}
214 AssertingVH(ValueTy *P) : ValueHandleBase(Assert, GetAsValue(P)) {}
215 AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
217 AssertingVH() : ThePtr(0) {}
218 AssertingVH(ValueTy *P) : ThePtr(P) {}
221 operator ValueTy*() const {
225 ValueTy *operator=(ValueTy *RHS) {
229 ValueTy *operator=(const AssertingVH<ValueTy> &RHS) {
230 setValPtr(RHS.getValPtr());
234 ValueTy *operator->() const { return getValPtr(); }
235 ValueTy &operator*() const { return *getValPtr(); }
238 // Specialize simplify_type to allow AssertingVH to participate in
239 // dyn_cast, isa, etc.
240 template<typename From> struct simplify_type;
241 template<> struct simplify_type<const AssertingVH<Value> > {
242 typedef Value* SimpleType;
243 static SimpleType getSimplifiedValue(const AssertingVH<Value> &AVH) {
244 return static_cast<Value *>(AVH);
247 template<> struct simplify_type<AssertingVH<Value> >
248 : public simplify_type<const AssertingVH<Value> > {};
250 // Specialize DenseMapInfo to allow AssertingVH to participate in DenseMap.
252 struct DenseMapInfo<AssertingVH<T> > {
253 typedef DenseMapInfo<T*> PointerInfo;
254 static inline AssertingVH<T> getEmptyKey() {
255 return AssertingVH<T>(PointerInfo::getEmptyKey());
257 static inline T* getTombstoneKey() {
258 return AssertingVH<T>(PointerInfo::getTombstoneKey());
260 static unsigned getHashValue(const AssertingVH<T> &Val) {
261 return PointerInfo::getHashValue(Val);
263 static bool isEqual(const AssertingVH<T> &LHS, const AssertingVH<T> &RHS) {
268 template <typename T>
269 struct isPodLike<AssertingVH<T> > {
271 static const bool value = true;
273 static const bool value = false;
278 /// TrackingVH - This is a value handle that tracks a Value (or Value subclass),
279 /// even across RAUW operations.
281 /// TrackingVH is designed for situations where a client needs to hold a handle
282 /// to a Value (or subclass) across some operations which may move that value,
283 /// but should never destroy it or replace it with some unacceptable type.
285 /// It is an error to do anything with a TrackingVH whose value has been
286 /// destroyed, except to destruct it.
288 /// It is an error to attempt to replace a value with one of a type which is
289 /// incompatible with any of its outstanding TrackingVHs.
290 template<typename ValueTy>
291 class TrackingVH : public ValueHandleBase {
292 void CheckValidity() const {
293 Value *VP = ValueHandleBase::getValPtr();
295 // Null is always ok.
298 // Check that this value is valid (i.e., it hasn't been deleted). We
299 // explicitly delay this check until access to avoid requiring clients to be
300 // unnecessarily careful w.r.t. destruction.
301 assert(ValueHandleBase::isValid(VP) && "Tracked Value was deleted!");
303 // Check that the value is a member of the correct subclass. We would like
304 // to check this property on assignment for better debugging, but we don't
305 // want to require a virtual interface on this VH. Instead we allow RAUW to
306 // replace this value with a value of an invalid type, and check it here.
307 assert(isa<ValueTy>(VP) &&
308 "Tracked Value was replaced by one with an invalid type!");
311 ValueTy *getValPtr() const {
313 return (ValueTy*)ValueHandleBase::getValPtr();
315 void setValPtr(ValueTy *P) {
317 ValueHandleBase::operator=(GetAsValue(P));
320 // Convert a ValueTy*, which may be const, to the type the base
322 static Value *GetAsValue(Value *V) { return V; }
323 static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
326 TrackingVH() : ValueHandleBase(Tracking) {}
327 TrackingVH(ValueTy *P) : ValueHandleBase(Tracking, GetAsValue(P)) {}
328 TrackingVH(const TrackingVH &RHS) : ValueHandleBase(Tracking, RHS) {}
330 operator ValueTy*() const {
334 ValueTy *operator=(ValueTy *RHS) {
338 ValueTy *operator=(const TrackingVH<ValueTy> &RHS) {
339 setValPtr(RHS.getValPtr());
343 ValueTy *operator->() const { return getValPtr(); }
344 ValueTy &operator*() const { return *getValPtr(); }
347 // Specialize simplify_type to allow TrackingVH to participate in
348 // dyn_cast, isa, etc.
349 template<typename From> struct simplify_type;
350 template<> struct simplify_type<const TrackingVH<Value> > {
351 typedef Value* SimpleType;
352 static SimpleType getSimplifiedValue(const TrackingVH<Value> &AVH) {
353 return static_cast<Value *>(AVH);
356 template<> struct simplify_type<TrackingVH<Value> >
357 : public simplify_type<const TrackingVH<Value> > {};
359 /// CallbackVH - This is a value handle that allows subclasses to define
360 /// callbacks that run when the underlying Value has RAUW called on it or is
361 /// destroyed. This class can be used as the key of a map, as long as the user
362 /// takes it out of the map before calling setValPtr() (since the map has to
363 /// rearrange itself when the pointer changes). Unlike ValueHandleBase, this
364 /// class has a vtable and a virtual destructor.
365 class CallbackVH : public ValueHandleBase {
367 CallbackVH(const CallbackVH &RHS)
368 : ValueHandleBase(Callback, RHS) {}
370 virtual ~CallbackVH() {}
372 void setValPtr(Value *P) {
373 ValueHandleBase::operator=(P);
377 CallbackVH() : ValueHandleBase(Callback) {}
378 CallbackVH(Value *P) : ValueHandleBase(Callback, P) {}
380 operator Value*() const {
384 /// Called when this->getValPtr() is destroyed, inside ~Value(), so you may
385 /// call any non-virtual Value method on getValPtr(), but no subclass methods.
386 /// If WeakVH were implemented as a CallbackVH, it would use this method to
387 /// call setValPtr(NULL). AssertingVH would use this method to cause an
388 /// assertion failure.
390 /// All implementations must remove the reference from this object to the
391 /// Value that's being destroyed.
392 virtual void deleted();
394 /// Called when this->getValPtr()->replaceAllUsesWith(new_value) is called,
395 /// _before_ any of the uses have actually been replaced. If WeakVH were
396 /// implemented as a CallbackVH, it would use this method to call
397 /// setValPtr(new_value). AssertingVH would do nothing in this method.
398 virtual void allUsesReplacedWith(Value *);
401 // Specialize simplify_type to allow CallbackVH to participate in
402 // dyn_cast, isa, etc.
403 template<typename From> struct simplify_type;
404 template<> struct simplify_type<const CallbackVH> {
405 typedef Value* SimpleType;
406 static SimpleType getSimplifiedValue(const CallbackVH &CVH) {
407 return static_cast<Value *>(CVH);
410 template<> struct simplify_type<CallbackVH>
411 : public simplify_type<const CallbackVH> {};
413 } // End llvm namespace