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;
61 ValueHandleBase(const ValueHandleBase&) = delete;
63 explicit ValueHandleBase(HandleBaseKind Kind)
64 : PrevPair(nullptr, Kind), Next(nullptr), V(nullptr) {}
65 ValueHandleBase(HandleBaseKind Kind, Value *V)
66 : PrevPair(nullptr, Kind), Next(nullptr), V(V) {
70 ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
71 : PrevPair(nullptr, Kind), Next(nullptr), V(RHS.V) {
73 AddToExistingUseList(RHS.getPrevPtr());
80 Value *operator=(Value *RHS) {
81 if (V == RHS) return RHS;
82 if (isValid(V)) RemoveFromUseList();
84 if (isValid(V)) AddToUseList();
88 Value *operator=(const ValueHandleBase &RHS) {
89 if (V == RHS.V) return RHS.V;
90 if (isValid(V)) RemoveFromUseList();
92 if (isValid(V)) AddToExistingUseList(RHS.getPrevPtr());
96 Value *operator->() const { return V; }
97 Value &operator*() const { return *V; }
100 Value *getValPtr() const { return V; }
102 static bool isValid(Value *V) {
104 V != DenseMapInfo<Value *>::getEmptyKey() &&
105 V != DenseMapInfo<Value *>::getTombstoneKey();
109 // Callbacks made from Value.
110 static void ValueIsDeleted(Value *V);
111 static void ValueIsRAUWd(Value *Old, Value *New);
114 // Internal implementation details.
115 ValueHandleBase **getPrevPtr() const { return PrevPair.getPointer(); }
116 HandleBaseKind getKind() const { return PrevPair.getInt(); }
117 void setPrevPtr(ValueHandleBase **Ptr) { PrevPair.setPointer(Ptr); }
119 /// \brief Add this ValueHandle to the use list for V.
121 /// List is the address of either the head of the list or a Next node within
122 /// the existing use list.
123 void AddToExistingUseList(ValueHandleBase **List);
125 /// \brief Add this ValueHandle to the use list after Node.
126 void AddToExistingUseListAfter(ValueHandleBase *Node);
128 /// \brief Add this ValueHandle to the use list for V.
130 /// \brief Remove this ValueHandle from its current use list.
131 void RemoveFromUseList();
134 /// \brief Value handle that is nullable, but tries to track the Value.
136 /// This is a value handle that tries hard to point to a Value, even across
137 /// RAUW operations, but will null itself out if the value is destroyed. this
138 /// is useful for advisory sorts of information, but should not be used as the
139 /// key of a map (since the map would have to rearrange itself when the pointer
141 class WeakVH : public ValueHandleBase {
143 WeakVH() : ValueHandleBase(Weak) {}
144 WeakVH(Value *P) : ValueHandleBase(Weak, P) {}
145 WeakVH(const WeakVH &RHS)
146 : ValueHandleBase(Weak, RHS) {}
148 Value *operator=(Value *RHS) {
149 return ValueHandleBase::operator=(RHS);
151 Value *operator=(const ValueHandleBase &RHS) {
152 return ValueHandleBase::operator=(RHS);
155 operator Value*() const {
160 // Specialize simplify_type to allow WeakVH to participate in
161 // dyn_cast, isa, etc.
162 template<> struct simplify_type<WeakVH> {
163 typedef Value* SimpleType;
164 static SimpleType getSimplifiedValue(WeakVH &WVH) {
169 /// \brief Value handle that asserts if the Value is deleted.
171 /// This is a Value Handle that points to a value and asserts out if the value
172 /// is destroyed while the handle is still live. This is very useful for
173 /// catching dangling pointer bugs and other things which can be non-obvious.
174 /// One particularly useful place to use this is as the Key of a map. Dangling
175 /// pointer bugs often lead to really subtle bugs that only occur if another
176 /// object happens to get allocated to the same address as the old one. Using
177 /// an AssertingVH ensures that an assert is triggered as soon as the bad
180 /// Note that an AssertingVH handle does *not* follow values across RAUW
181 /// operations. This means that RAUW's need to explicitly update the
182 /// AssertingVH's as it moves. This is required because in non-assert mode this
183 /// class turns into a trivial wrapper around a pointer.
184 template <typename ValueTy>
187 : public ValueHandleBase
190 friend struct DenseMapInfo<AssertingVH<ValueTy> >;
193 Value *getRawValPtr() const { return ValueHandleBase::getValPtr(); }
194 void setRawValPtr(Value *P) { ValueHandleBase::operator=(P); }
197 Value *getRawValPtr() const { return ThePtr; }
198 void setRawValPtr(Value *P) { ThePtr = P; }
200 // Convert a ValueTy*, which may be const, to the raw Value*.
201 static Value *GetAsValue(Value *V) { return V; }
202 static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
204 ValueTy *getValPtr() const { return static_cast<ValueTy *>(getRawValPtr()); }
205 void setValPtr(ValueTy *P) { setRawValPtr(GetAsValue(P)); }
209 AssertingVH() : ValueHandleBase(Assert) {}
210 AssertingVH(ValueTy *P) : ValueHandleBase(Assert, GetAsValue(P)) {}
211 AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
213 AssertingVH() : ThePtr(nullptr) {}
214 AssertingVH(ValueTy *P) : ThePtr(GetAsValue(P)) {}
217 operator ValueTy*() const {
221 ValueTy *operator=(ValueTy *RHS) {
225 ValueTy *operator=(const AssertingVH<ValueTy> &RHS) {
226 setValPtr(RHS.getValPtr());
230 ValueTy *operator->() const { return getValPtr(); }
231 ValueTy &operator*() const { return *getValPtr(); }
234 // Specialize DenseMapInfo to allow AssertingVH to participate in DenseMap.
236 struct DenseMapInfo<AssertingVH<T> > {
237 static inline AssertingVH<T> getEmptyKey() {
239 Res.setRawValPtr(DenseMapInfo<Value *>::getEmptyKey());
242 static inline AssertingVH<T> getTombstoneKey() {
244 Res.setRawValPtr(DenseMapInfo<Value *>::getTombstoneKey());
247 static unsigned getHashValue(const AssertingVH<T> &Val) {
248 return DenseMapInfo<Value *>::getHashValue(Val.getRawValPtr());
250 static bool isEqual(const AssertingVH<T> &LHS, const AssertingVH<T> &RHS) {
251 return DenseMapInfo<Value *>::isEqual(LHS.getRawValPtr(),
256 template <typename T>
257 struct isPodLike<AssertingVH<T> > {
259 static const bool value = true;
261 static const bool value = false;
266 /// \brief Value handle that tracks a Value across RAUW.
268 /// TrackingVH is designed for situations where a client needs to hold a handle
269 /// to a Value (or subclass) across some operations which may move that value,
270 /// but should never destroy it or replace it with some unacceptable type.
272 /// It is an error to do anything with a TrackingVH whose value has been
273 /// destroyed, except to destruct it.
275 /// It is an error to attempt to replace a value with one of a type which is
276 /// incompatible with any of its outstanding TrackingVHs.
277 template<typename ValueTy>
278 class TrackingVH : public ValueHandleBase {
279 void CheckValidity() const {
280 Value *VP = ValueHandleBase::getValPtr();
282 // Null is always ok.
285 // Check that this value is valid (i.e., it hasn't been deleted). We
286 // explicitly delay this check until access to avoid requiring clients to be
287 // unnecessarily careful w.r.t. destruction.
288 assert(ValueHandleBase::isValid(VP) && "Tracked Value was deleted!");
290 // Check that the value is a member of the correct subclass. We would like
291 // to check this property on assignment for better debugging, but we don't
292 // want to require a virtual interface on this VH. Instead we allow RAUW to
293 // replace this value with a value of an invalid type, and check it here.
294 assert(isa<ValueTy>(VP) &&
295 "Tracked Value was replaced by one with an invalid type!");
298 ValueTy *getValPtr() const {
300 return (ValueTy*)ValueHandleBase::getValPtr();
302 void setValPtr(ValueTy *P) {
304 ValueHandleBase::operator=(GetAsValue(P));
307 // Convert a ValueTy*, which may be const, to the type the base
309 static Value *GetAsValue(Value *V) { return V; }
310 static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
313 TrackingVH() : ValueHandleBase(Tracking) {}
314 TrackingVH(ValueTy *P) : ValueHandleBase(Tracking, GetAsValue(P)) {}
315 TrackingVH(const TrackingVH &RHS) : ValueHandleBase(Tracking, RHS) {}
317 operator ValueTy*() const {
321 ValueTy *operator=(ValueTy *RHS) {
325 ValueTy *operator=(const TrackingVH<ValueTy> &RHS) {
326 setValPtr(RHS.getValPtr());
330 ValueTy *operator->() const { return getValPtr(); }
331 ValueTy &operator*() const { return *getValPtr(); }
334 /// \brief Value handle with callbacks on RAUW and destruction.
336 /// This is a value handle that allows subclasses to define callbacks that run
337 /// when the underlying Value has RAUW called on it or is destroyed. This
338 /// class can be used as the key of a map, as long as the user takes it out of
339 /// the map before calling setValPtr() (since the map has to rearrange itself
340 /// when the pointer changes). Unlike ValueHandleBase, this class has a vtable
341 /// and a virtual destructor.
342 class CallbackVH : public ValueHandleBase {
343 virtual void anchor();
345 CallbackVH(const CallbackVH &RHS)
346 : ValueHandleBase(Callback, RHS) {}
348 virtual ~CallbackVH() {}
350 void setValPtr(Value *P) {
351 ValueHandleBase::operator=(P);
355 CallbackVH() : ValueHandleBase(Callback) {}
356 CallbackVH(Value *P) : ValueHandleBase(Callback, P) {}
358 operator Value*() const {
362 /// \brief Callback for Value destruction.
364 /// Called when this->getValPtr() is destroyed, inside ~Value(), so you
365 /// may call any non-virtual Value method on getValPtr(), but no subclass
366 /// methods. If WeakVH were implemented as a CallbackVH, it would use this
367 /// method to call setValPtr(NULL). AssertingVH would use this method to
368 /// cause an assertion failure.
370 /// All implementations must remove the reference from this object to the
371 /// Value that's being destroyed.
372 virtual void deleted() { setValPtr(nullptr); }
374 /// \brief Callback for Value RAUW.
376 /// Called when this->getValPtr()->replaceAllUsesWith(new_value) is called,
377 /// _before_ any of the uses have actually been replaced. If WeakVH were
378 /// implemented as a CallbackVH, it would use this method to call
379 /// setValPtr(new_value). AssertingVH would do nothing in this method.
380 virtual void allUsesReplacedWith(Value *) {}
383 } // End llvm namespace