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 /// \brief This is the common base class of value handles.
38 /// ValueHandle's are smart pointers to Value's that have special behavior when
39 /// the value is deleted or ReplaceAllUsesWith'd. See the specific handles
40 /// below for details.
41 class ValueHandleBase {
44 /// \brief This indicates what sub class the handle actually is.
46 /// This is to avoid having a vtable for the light-weight handle pointers. The
47 /// fully general Callback version does have a vtable.
56 PointerIntPair<ValueHandleBase**, 2, HandleBaseKind> PrevPair;
57 ValueHandleBase *Next;
59 // A subclass may want to store some information along with the value
60 // pointer. Allow them to do this by making the value pointer a pointer-int
61 // pair. The 'setValPtrInt' and 'getValPtrInt' methods below give them this
63 PointerIntPair<Value*, 2> VP;
65 ValueHandleBase(const ValueHandleBase&) LLVM_DELETED_FUNCTION;
67 explicit ValueHandleBase(HandleBaseKind Kind)
68 : PrevPair(nullptr, Kind), Next(nullptr), VP(nullptr, 0) {}
69 ValueHandleBase(HandleBaseKind Kind, Value *V)
70 : PrevPair(nullptr, Kind), Next(nullptr), VP(V, 0) {
71 if (isValid(VP.getPointer()))
74 ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
75 : PrevPair(nullptr, Kind), Next(nullptr), VP(RHS.VP) {
76 if (isValid(VP.getPointer()))
77 AddToExistingUseList(RHS.getPrevPtr());
80 if (isValid(VP.getPointer()))
84 Value *operator=(Value *RHS) {
85 if (VP.getPointer() == RHS) return RHS;
86 if (isValid(VP.getPointer())) RemoveFromUseList();
88 if (isValid(VP.getPointer())) AddToUseList();
92 Value *operator=(const ValueHandleBase &RHS) {
93 if (VP.getPointer() == RHS.VP.getPointer()) return RHS.VP.getPointer();
94 if (isValid(VP.getPointer())) RemoveFromUseList();
95 VP.setPointer(RHS.VP.getPointer());
96 if (isValid(VP.getPointer())) AddToExistingUseList(RHS.getPrevPtr());
97 return VP.getPointer();
100 Value *operator->() const { return getValPtr(); }
101 Value &operator*() const { return *getValPtr(); }
104 Value *getValPtr() const { return VP.getPointer(); }
106 void setValPtrInt(unsigned K) { VP.setInt(K); }
107 unsigned getValPtrInt() const { return VP.getInt(); }
109 static bool isValid(Value *V) {
111 V != DenseMapInfo<Value *>::getEmptyKey() &&
112 V != DenseMapInfo<Value *>::getTombstoneKey();
116 // Callbacks made from Value.
117 static void ValueIsDeleted(Value *V);
118 static void ValueIsRAUWd(Value *Old, Value *New);
121 // Internal implementation details.
122 ValueHandleBase **getPrevPtr() const { return PrevPair.getPointer(); }
123 HandleBaseKind getKind() const { return PrevPair.getInt(); }
124 void setPrevPtr(ValueHandleBase **Ptr) { PrevPair.setPointer(Ptr); }
126 /// \brief Add this ValueHandle to the use list for VP.
128 /// List is the address of either the head of the list or a Next node within
129 /// the existing use list.
130 void AddToExistingUseList(ValueHandleBase **List);
132 /// \brief Add this ValueHandle to the use list after Node.
133 void AddToExistingUseListAfter(ValueHandleBase *Node);
135 /// \brief Add this ValueHandle to the use list for VP.
137 /// \brief Remove this ValueHandle from its current use list.
138 void RemoveFromUseList();
141 /// \brief Value handle that is nullable, but tries to track the Value.
143 /// This is a value handle that tries hard to point to a Value, even across
144 /// RAUW operations, but will null itself out if the value is destroyed. this
145 /// is useful for advisory sorts of information, but should not be used as the
146 /// key of a map (since the map would have to rearrange itself when the pointer
148 class WeakVH : public ValueHandleBase {
150 WeakVH() : ValueHandleBase(Weak) {}
151 WeakVH(Value *P) : ValueHandleBase(Weak, P) {}
152 WeakVH(const WeakVH &RHS)
153 : ValueHandleBase(Weak, RHS) {}
155 Value *operator=(Value *RHS) {
156 return ValueHandleBase::operator=(RHS);
158 Value *operator=(const ValueHandleBase &RHS) {
159 return ValueHandleBase::operator=(RHS);
162 operator Value*() const {
167 // Specialize simplify_type to allow WeakVH to participate in
168 // dyn_cast, isa, etc.
169 template<> struct simplify_type<WeakVH> {
170 typedef Value* SimpleType;
171 static SimpleType getSimplifiedValue(WeakVH &WVH) {
176 /// \brief Value handle that asserts if the Value is deleted.
178 /// This is a Value Handle that points to a value and asserts out if the value
179 /// is destroyed while the handle is still live. This is very useful for
180 /// catching dangling pointer bugs and other things which can be non-obvious.
181 /// One particularly useful place to use this is as the Key of a map. Dangling
182 /// pointer bugs often lead to really subtle bugs that only occur if another
183 /// object happens to get allocated to the same address as the old one. Using
184 /// an AssertingVH ensures that an assert is triggered as soon as the bad
187 /// Note that an AssertingVH handle does *not* follow values across RAUW
188 /// operations. This means that RAUW's need to explicitly update the
189 /// AssertingVH's as it moves. This is required because in non-assert mode this
190 /// class turns into a trivial wrapper around a pointer.
191 template <typename ValueTy>
194 : public ValueHandleBase
197 friend struct DenseMapInfo<AssertingVH<ValueTy> >;
200 ValueTy *getValPtr() const {
201 return static_cast<ValueTy*>(ValueHandleBase::getValPtr());
203 void setValPtr(ValueTy *P) {
204 ValueHandleBase::operator=(GetAsValue(P));
208 ValueTy *getValPtr() const { return ThePtr; }
209 void setValPtr(ValueTy *P) { ThePtr = P; }
212 // Convert a ValueTy*, which may be const, to the type the base
214 static Value *GetAsValue(Value *V) { return V; }
215 static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
219 AssertingVH() : ValueHandleBase(Assert) {}
220 AssertingVH(ValueTy *P) : ValueHandleBase(Assert, GetAsValue(P)) {}
221 AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
223 AssertingVH() : ThePtr(nullptr) {}
224 AssertingVH(ValueTy *P) : ThePtr(P) {}
227 operator ValueTy*() const {
231 ValueTy *operator=(ValueTy *RHS) {
235 ValueTy *operator=(const AssertingVH<ValueTy> &RHS) {
236 setValPtr(RHS.getValPtr());
240 ValueTy *operator->() const { return getValPtr(); }
241 ValueTy &operator*() const { return *getValPtr(); }
244 // Specialize DenseMapInfo to allow AssertingVH to participate in DenseMap.
246 struct DenseMapInfo<AssertingVH<T> > {
247 typedef DenseMapInfo<T*> PointerInfo;
248 static inline AssertingVH<T> getEmptyKey() {
249 return AssertingVH<T>(PointerInfo::getEmptyKey());
251 static inline T* getTombstoneKey() {
252 return AssertingVH<T>(PointerInfo::getTombstoneKey());
254 static unsigned getHashValue(const AssertingVH<T> &Val) {
255 return PointerInfo::getHashValue(Val);
258 static bool isEqual(const AssertingVH<T> &LHS, const AssertingVH<T> &RHS) {
259 // Avoid downcasting AssertingVH<T> to T*, as empty/tombstone keys may not
260 // be properly aligned pointers to T*.
261 return LHS.ValueHandleBase::getValPtr() == RHS.ValueHandleBase::getValPtr();
264 static bool isEqual(const AssertingVH<T> &LHS, const AssertingVH<T> &RHS) {
270 template <typename T>
271 struct isPodLike<AssertingVH<T> > {
273 static const bool value = true;
275 static const bool value = false;
280 /// \brief Value handle that tracks a Value across RAUW.
282 /// TrackingVH is designed for situations where a client needs to hold a handle
283 /// to a Value (or subclass) across some operations which may move that value,
284 /// but should never destroy it or replace it with some unacceptable type.
286 /// It is an error to do anything with a TrackingVH whose value has been
287 /// destroyed, except to destruct it.
289 /// It is an error to attempt to replace a value with one of a type which is
290 /// incompatible with any of its outstanding TrackingVHs.
291 template<typename ValueTy>
292 class TrackingVH : public ValueHandleBase {
293 void CheckValidity() const {
294 Value *VP = ValueHandleBase::getValPtr();
296 // Null is always ok.
299 // Check that this value is valid (i.e., it hasn't been deleted). We
300 // explicitly delay this check until access to avoid requiring clients to be
301 // unnecessarily careful w.r.t. destruction.
302 assert(ValueHandleBase::isValid(VP) && "Tracked Value was deleted!");
304 // Check that the value is a member of the correct subclass. We would like
305 // to check this property on assignment for better debugging, but we don't
306 // want to require a virtual interface on this VH. Instead we allow RAUW to
307 // replace this value with a value of an invalid type, and check it here.
308 assert(isa<ValueTy>(VP) &&
309 "Tracked Value was replaced by one with an invalid type!");
312 ValueTy *getValPtr() const {
314 return (ValueTy*)ValueHandleBase::getValPtr();
316 void setValPtr(ValueTy *P) {
318 ValueHandleBase::operator=(GetAsValue(P));
321 // Convert a ValueTy*, which may be const, to the type the base
323 static Value *GetAsValue(Value *V) { return V; }
324 static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
327 TrackingVH() : ValueHandleBase(Tracking) {}
328 TrackingVH(ValueTy *P) : ValueHandleBase(Tracking, GetAsValue(P)) {}
329 TrackingVH(const TrackingVH &RHS) : ValueHandleBase(Tracking, RHS) {}
331 operator ValueTy*() const {
335 ValueTy *operator=(ValueTy *RHS) {
339 ValueTy *operator=(const TrackingVH<ValueTy> &RHS) {
340 setValPtr(RHS.getValPtr());
344 ValueTy *operator->() const { return getValPtr(); }
345 ValueTy &operator*() const { return *getValPtr(); }
348 /// \brief Value handle with callbacks on RAUW and destruction.
350 /// This is a value handle that allows subclasses to define callbacks that run
351 /// when the underlying Value has RAUW called on it or is destroyed. This
352 /// class can be used as the key of a map, as long as the user takes it out of
353 /// the map before calling setValPtr() (since the map has to rearrange itself
354 /// when the pointer changes). Unlike ValueHandleBase, this class has a vtable
355 /// and a virtual destructor.
356 class CallbackVH : public ValueHandleBase {
357 virtual void anchor();
359 CallbackVH(const CallbackVH &RHS)
360 : ValueHandleBase(Callback, RHS) {}
362 virtual ~CallbackVH() {}
364 void setValPtr(Value *P) {
365 ValueHandleBase::operator=(P);
369 CallbackVH() : ValueHandleBase(Callback) {}
370 CallbackVH(Value *P) : ValueHandleBase(Callback, P) {}
372 operator Value*() const {
376 /// \brief Callback for Value destruction.
378 /// Called when this->getValPtr() is destroyed, inside ~Value(), so you
379 /// may call any non-virtual Value method on getValPtr(), but no subclass
380 /// methods. If WeakVH were implemented as a CallbackVH, it would use this
381 /// method to call setValPtr(NULL). AssertingVH would use this method to
382 /// cause an assertion failure.
384 /// All implementations must remove the reference from this object to the
385 /// Value that's being destroyed.
386 virtual void deleted() { setValPtr(nullptr); }
388 /// \brief Callback for Value RAUW.
390 /// Called when this->getValPtr()->replaceAllUsesWith(new_value) is called,
391 /// _before_ any of the uses have actually been replaced. If WeakVH were
392 /// implemented as a CallbackVH, it would use this method to call
393 /// setValPtr(new_value). AssertingVH would do nothing in this method.
394 virtual void allUsesReplacedWith(Value *) {}
397 } // End llvm namespace