1 //===- 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_IR_VALUEHANDLE_H
15 #define LLVM_IR_VALUEHANDLE_H
17 #include "llvm/ADT/DenseMapInfo.h"
18 #include "llvm/ADT/PointerIntPair.h"
19 #include "llvm/IR/Value.h"
22 class ValueHandleBase;
23 template<typename From> struct simplify_type;
25 // ValueHandleBase** is only 4-byte aligned.
27 class PointerLikeTypeTraits<ValueHandleBase**> {
29 static inline void *getAsVoidPointer(ValueHandleBase** P) { return P; }
30 static inline ValueHandleBase **getFromVoidPointer(void *P) {
31 return static_cast<ValueHandleBase**>(P);
33 enum { NumLowBitsAvailable = 2 };
36 /// ValueHandleBase - This is the common base class of value handles.
37 /// ValueHandle's are smart pointers to Value's that have special behavior when
38 /// the value is deleted or ReplaceAllUsesWith'd. See the specific handles
39 /// below for details.
41 class ValueHandleBase {
44 /// HandleBaseKind - This indicates what sub class the handle actually is.
45 /// This is to avoid having a vtable for the light-weight handle pointers. The
46 /// fully general Callback version does have a vtable.
55 PointerIntPair<ValueHandleBase**, 2, HandleBaseKind> PrevPair;
56 ValueHandleBase *Next;
58 // A subclass may want to store some information along with the value
59 // pointer. Allow them to do this by making the value pointer a pointer-int
60 // pair. The 'setValPtrInt' and 'getValPtrInt' methods below give them this
62 PointerIntPair<Value*, 2> VP;
64 ValueHandleBase(const ValueHandleBase&) LLVM_DELETED_FUNCTION;
66 explicit ValueHandleBase(HandleBaseKind Kind)
67 : PrevPair(nullptr, Kind), Next(nullptr), VP(nullptr, 0) {}
68 ValueHandleBase(HandleBaseKind Kind, Value *V)
69 : PrevPair(nullptr, Kind), Next(nullptr), VP(V, 0) {
70 if (isValid(VP.getPointer()))
73 ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
74 : PrevPair(nullptr, Kind), Next(nullptr), VP(RHS.VP) {
75 if (isValid(VP.getPointer()))
76 AddToExistingUseList(RHS.getPrevPtr());
79 if (isValid(VP.getPointer()))
83 Value *operator=(Value *RHS) {
84 if (VP.getPointer() == RHS) return RHS;
85 if (isValid(VP.getPointer())) RemoveFromUseList();
87 if (isValid(VP.getPointer())) AddToUseList();
91 Value *operator=(const ValueHandleBase &RHS) {
92 if (VP.getPointer() == RHS.VP.getPointer()) return RHS.VP.getPointer();
93 if (isValid(VP.getPointer())) RemoveFromUseList();
94 VP.setPointer(RHS.VP.getPointer());
95 if (isValid(VP.getPointer())) AddToExistingUseList(RHS.getPrevPtr());
96 return VP.getPointer();
99 Value *operator->() const { return getValPtr(); }
100 Value &operator*() const { return *getValPtr(); }
103 Value *getValPtr() const { return VP.getPointer(); }
105 void setValPtrInt(unsigned K) { VP.setInt(K); }
106 unsigned getValPtrInt() const { return VP.getInt(); }
108 static bool isValid(Value *V) {
110 V != DenseMapInfo<Value *>::getEmptyKey() &&
111 V != DenseMapInfo<Value *>::getTombstoneKey();
115 // Callbacks made from Value.
116 static void ValueIsDeleted(Value *V);
117 static void ValueIsRAUWd(Value *Old, Value *New);
120 // Internal implementation details.
121 ValueHandleBase **getPrevPtr() const { return PrevPair.getPointer(); }
122 HandleBaseKind getKind() const { return PrevPair.getInt(); }
123 void setPrevPtr(ValueHandleBase **Ptr) { PrevPair.setPointer(Ptr); }
125 /// AddToExistingUseList - Add this ValueHandle to the use list for VP, where
126 /// List is the address of either the head of the list or a Next node within
127 /// the existing use list.
128 void AddToExistingUseList(ValueHandleBase **List);
130 /// AddToExistingUseListAfter - Add this ValueHandle to the use list after
132 void AddToExistingUseListAfter(ValueHandleBase *Node);
134 /// AddToUseList - Add this ValueHandle to the use list for VP.
136 /// RemoveFromUseList - Remove this ValueHandle from its current use list.
137 void RemoveFromUseList();
140 /// WeakVH - This is a value handle that tries hard to point to a Value, even
141 /// across RAUW operations, but will null itself out if the value is destroyed.
142 /// this is useful for advisory sorts of information, but should not be used as
143 /// the key of a map (since the map would have to rearrange itself when the
144 /// pointer changes).
145 class WeakVH : public ValueHandleBase {
147 WeakVH() : ValueHandleBase(Weak) {}
148 WeakVH(Value *P) : ValueHandleBase(Weak, P) {}
149 WeakVH(const WeakVH &RHS)
150 : ValueHandleBase(Weak, RHS) {}
152 Value *operator=(Value *RHS) {
153 return ValueHandleBase::operator=(RHS);
155 Value *operator=(const ValueHandleBase &RHS) {
156 return ValueHandleBase::operator=(RHS);
159 operator Value*() const {
164 // Specialize simplify_type to allow WeakVH to participate in
165 // dyn_cast, isa, etc.
166 template<> struct simplify_type<WeakVH> {
167 typedef Value* SimpleType;
168 static SimpleType getSimplifiedValue(WeakVH &WVH) {
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(nullptr) {}
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 DenseMapInfo to allow AssertingVH to participate in DenseMap.
240 struct DenseMapInfo<AssertingVH<T> > {
241 typedef DenseMapInfo<T*> PointerInfo;
242 static inline AssertingVH<T> getEmptyKey() {
243 return AssertingVH<T>(PointerInfo::getEmptyKey());
245 static inline T* getTombstoneKey() {
246 return AssertingVH<T>(PointerInfo::getTombstoneKey());
248 static unsigned getHashValue(const AssertingVH<T> &Val) {
249 return PointerInfo::getHashValue(Val);
251 static bool isEqual(const AssertingVH<T> &LHS, const AssertingVH<T> &RHS) {
256 template <typename T>
257 struct isPodLike<AssertingVH<T> > {
259 static const bool value = true;
261 static const bool value = false;
266 /// TrackingVH - This is a value handle that tracks a Value (or Value subclass),
267 /// even across RAUW operations.
269 /// TrackingVH is designed for situations where a client needs to hold a handle
270 /// to a Value (or subclass) across some operations which may move that value,
271 /// but should never destroy it or replace it with some unacceptable type.
273 /// It is an error to do anything with a TrackingVH whose value has been
274 /// destroyed, except to destruct it.
276 /// It is an error to attempt to replace a value with one of a type which is
277 /// incompatible with any of its outstanding TrackingVHs.
278 template<typename ValueTy>
279 class TrackingVH : public ValueHandleBase {
280 void CheckValidity() const {
281 Value *VP = ValueHandleBase::getValPtr();
283 // Null is always ok.
286 // Check that this value is valid (i.e., it hasn't been deleted). We
287 // explicitly delay this check until access to avoid requiring clients to be
288 // unnecessarily careful w.r.t. destruction.
289 assert(ValueHandleBase::isValid(VP) && "Tracked Value was deleted!");
291 // Check that the value is a member of the correct subclass. We would like
292 // to check this property on assignment for better debugging, but we don't
293 // want to require a virtual interface on this VH. Instead we allow RAUW to
294 // replace this value with a value of an invalid type, and check it here.
295 assert(isa<ValueTy>(VP) &&
296 "Tracked Value was replaced by one with an invalid type!");
299 ValueTy *getValPtr() const {
301 return (ValueTy*)ValueHandleBase::getValPtr();
303 void setValPtr(ValueTy *P) {
305 ValueHandleBase::operator=(GetAsValue(P));
308 // Convert a ValueTy*, which may be const, to the type the base
310 static Value *GetAsValue(Value *V) { return V; }
311 static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
314 TrackingVH() : ValueHandleBase(Tracking) {}
315 TrackingVH(ValueTy *P) : ValueHandleBase(Tracking, GetAsValue(P)) {}
316 TrackingVH(const TrackingVH &RHS) : ValueHandleBase(Tracking, RHS) {}
318 operator ValueTy*() const {
322 ValueTy *operator=(ValueTy *RHS) {
326 ValueTy *operator=(const TrackingVH<ValueTy> &RHS) {
327 setValPtr(RHS.getValPtr());
331 ValueTy *operator->() const { return getValPtr(); }
332 ValueTy &operator*() const { return *getValPtr(); }
335 /// CallbackVH - This is a value handle that allows subclasses to define
336 /// callbacks that run when the underlying Value has RAUW called on it or is
337 /// destroyed. This class can be used as the key of a map, as long as the user
338 /// takes it out of the map before calling setValPtr() (since the map has to
339 /// rearrange itself when the pointer changes). Unlike ValueHandleBase, this
340 /// class has a vtable and a virtual destructor.
341 class CallbackVH : public ValueHandleBase {
342 virtual void anchor();
344 CallbackVH(const CallbackVH &RHS)
345 : ValueHandleBase(Callback, RHS) {}
347 virtual ~CallbackVH() {}
349 void setValPtr(Value *P) {
350 ValueHandleBase::operator=(P);
354 CallbackVH() : ValueHandleBase(Callback) {}
355 CallbackVH(Value *P) : ValueHandleBase(Callback, P) {}
357 operator Value*() const {
361 /// Called when this->getValPtr() is destroyed, inside ~Value(), so you may
362 /// call any non-virtual Value method on getValPtr(), but no subclass methods.
363 /// If WeakVH were implemented as a CallbackVH, it would use this method to
364 /// call setValPtr(NULL). AssertingVH would use this method to cause an
365 /// assertion failure.
367 /// All implementations must remove the reference from this object to the
368 /// Value that's being destroyed.
369 virtual void deleted() { setValPtr(nullptr); }
371 /// Called when this->getValPtr()->replaceAllUsesWith(new_value) is called,
372 /// _before_ any of the uses have actually been replaced. If WeakVH were
373 /// implemented as a CallbackVH, it would use this method to call
374 /// setValPtr(new_value). AssertingVH would do nothing in this method.
375 virtual void allUsesReplacedWith(Value *) {}
378 } // End llvm namespace