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 /// \brief This is the common base class of value handles.
27 /// ValueHandle's are smart pointers to Value's that have special behavior when
28 /// the value is deleted or ReplaceAllUsesWith'd. See the specific handles
29 /// below for details.
30 class ValueHandleBase {
33 /// \brief This indicates what sub class the handle actually is.
35 /// This is to avoid having a vtable for the light-weight handle pointers. The
36 /// fully general Callback version does have a vtable.
44 ValueHandleBase(const ValueHandleBase &RHS)
45 : ValueHandleBase(RHS.PrevPair.getInt(), RHS) {}
47 ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
48 : PrevPair(nullptr, Kind), Next(nullptr), V(RHS.V) {
50 AddToExistingUseList(RHS.getPrevPtr());
54 PointerIntPair<ValueHandleBase**, 2, HandleBaseKind> PrevPair;
55 ValueHandleBase *Next;
60 explicit ValueHandleBase(HandleBaseKind Kind)
61 : PrevPair(nullptr, Kind), Next(nullptr), V(nullptr) {}
62 ValueHandleBase(HandleBaseKind Kind, Value *V)
63 : PrevPair(nullptr, Kind), Next(nullptr), V(V) {
73 Value *operator=(Value *RHS) {
74 if (V == RHS) return RHS;
75 if (isValid(V)) RemoveFromUseList();
77 if (isValid(V)) AddToUseList();
81 Value *operator=(const ValueHandleBase &RHS) {
82 if (V == RHS.V) return RHS.V;
83 if (isValid(V)) RemoveFromUseList();
85 if (isValid(V)) AddToExistingUseList(RHS.getPrevPtr());
89 Value *operator->() const { return V; }
90 Value &operator*() const { return *V; }
93 Value *getValPtr() const { return V; }
95 static bool isValid(Value *V) {
97 V != DenseMapInfo<Value *>::getEmptyKey() &&
98 V != DenseMapInfo<Value *>::getTombstoneKey();
102 // Callbacks made from Value.
103 static void ValueIsDeleted(Value *V);
104 static void ValueIsRAUWd(Value *Old, Value *New);
107 // Internal implementation details.
108 ValueHandleBase **getPrevPtr() const { return PrevPair.getPointer(); }
109 HandleBaseKind getKind() const { return PrevPair.getInt(); }
110 void setPrevPtr(ValueHandleBase **Ptr) { PrevPair.setPointer(Ptr); }
112 /// \brief Add this ValueHandle to the use list for V.
114 /// List is the address of either the head of the list or a Next node within
115 /// the existing use list.
116 void AddToExistingUseList(ValueHandleBase **List);
118 /// \brief Add this ValueHandle to the use list after Node.
119 void AddToExistingUseListAfter(ValueHandleBase *Node);
121 /// \brief Add this ValueHandle to the use list for V.
123 /// \brief Remove this ValueHandle from its current use list.
124 void RemoveFromUseList();
127 /// \brief Value handle that is nullable, but tries to track the Value.
129 /// This is a value handle that tries hard to point to a Value, even across
130 /// RAUW operations, but will null itself out if the value is destroyed. this
131 /// is useful for advisory sorts of information, but should not be used as the
132 /// key of a map (since the map would have to rearrange itself when the pointer
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 WeakVH &operator=(const WeakVH &RHS) = default;
143 Value *operator=(Value *RHS) {
144 return ValueHandleBase::operator=(RHS);
146 Value *operator=(const ValueHandleBase &RHS) {
147 return ValueHandleBase::operator=(RHS);
150 operator Value*() const {
155 // Specialize simplify_type to allow WeakVH to participate in
156 // dyn_cast, isa, etc.
157 template <> struct simplify_type<WeakVH> {
158 typedef Value *SimpleType;
159 static SimpleType getSimplifiedValue(WeakVH &WVH) { return WVH; }
161 template <> struct simplify_type<const WeakVH> {
162 typedef Value *SimpleType;
163 static SimpleType getSimplifiedValue(const WeakVH &WVH) { return WVH; }
166 /// \brief Value handle that asserts if the Value is deleted.
168 /// This is a Value Handle that points to a value and asserts out if the value
169 /// is destroyed while the handle is still live. This is very useful for
170 /// catching dangling pointer bugs and other things which can be non-obvious.
171 /// One particularly useful place to use this is as the Key of a map. Dangling
172 /// pointer bugs often lead to really subtle bugs that only occur if another
173 /// object happens to get allocated to the same address as the old one. Using
174 /// an AssertingVH ensures that an assert is triggered as soon as the bad
177 /// Note that an AssertingVH handle does *not* follow values across RAUW
178 /// operations. This means that RAUW's need to explicitly update the
179 /// AssertingVH's as it moves. This is required because in non-assert mode this
180 /// class turns into a trivial wrapper around a pointer.
181 template <typename ValueTy>
184 : public ValueHandleBase
187 friend struct DenseMapInfo<AssertingVH<ValueTy> >;
190 Value *getRawValPtr() const { return ValueHandleBase::getValPtr(); }
191 void setRawValPtr(Value *P) { ValueHandleBase::operator=(P); }
194 Value *getRawValPtr() const { return ThePtr; }
195 void setRawValPtr(Value *P) { ThePtr = P; }
197 // Convert a ValueTy*, which may be const, to the raw Value*.
198 static Value *GetAsValue(Value *V) { return V; }
199 static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
201 ValueTy *getValPtr() const { return static_cast<ValueTy *>(getRawValPtr()); }
202 void setValPtr(ValueTy *P) { setRawValPtr(GetAsValue(P)); }
206 AssertingVH() : ValueHandleBase(Assert) {}
207 AssertingVH(ValueTy *P) : ValueHandleBase(Assert, GetAsValue(P)) {}
208 AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
210 AssertingVH() : ThePtr(nullptr) {}
211 AssertingVH(ValueTy *P) : ThePtr(GetAsValue(P)) {}
214 operator ValueTy*() const {
218 ValueTy *operator=(ValueTy *RHS) {
222 ValueTy *operator=(const AssertingVH<ValueTy> &RHS) {
223 setValPtr(RHS.getValPtr());
227 ValueTy *operator->() const { return getValPtr(); }
228 ValueTy &operator*() const { return *getValPtr(); }
231 // Specialize DenseMapInfo to allow AssertingVH to participate in DenseMap.
233 struct DenseMapInfo<AssertingVH<T> > {
234 static inline AssertingVH<T> getEmptyKey() {
236 Res.setRawValPtr(DenseMapInfo<Value *>::getEmptyKey());
239 static inline AssertingVH<T> getTombstoneKey() {
241 Res.setRawValPtr(DenseMapInfo<Value *>::getTombstoneKey());
244 static unsigned getHashValue(const AssertingVH<T> &Val) {
245 return DenseMapInfo<Value *>::getHashValue(Val.getRawValPtr());
247 static bool isEqual(const AssertingVH<T> &LHS, const AssertingVH<T> &RHS) {
248 return DenseMapInfo<Value *>::isEqual(LHS.getRawValPtr(),
253 template <typename T>
254 struct isPodLike<AssertingVH<T> > {
256 static const bool value = true;
258 static const bool value = false;
263 /// \brief Value handle that tracks a Value across RAUW.
265 /// TrackingVH is designed for situations where a client needs to hold a handle
266 /// to a Value (or subclass) across some operations which may move that value,
267 /// but should never destroy it or replace it with some unacceptable type.
269 /// It is an error to do anything with a TrackingVH whose value has been
270 /// destroyed, except to destruct it.
272 /// It is an error to attempt to replace a value with one of a type which is
273 /// incompatible with any of its outstanding TrackingVHs.
274 template<typename ValueTy>
275 class TrackingVH : public ValueHandleBase {
276 void CheckValidity() const {
277 Value *VP = ValueHandleBase::getValPtr();
279 // Null is always ok.
282 // Check that this value is valid (i.e., it hasn't been deleted). We
283 // explicitly delay this check until access to avoid requiring clients to be
284 // unnecessarily careful w.r.t. destruction.
285 assert(ValueHandleBase::isValid(VP) && "Tracked Value was deleted!");
287 // Check that the value is a member of the correct subclass. We would like
288 // to check this property on assignment for better debugging, but we don't
289 // want to require a virtual interface on this VH. Instead we allow RAUW to
290 // replace this value with a value of an invalid type, and check it here.
291 assert(isa<ValueTy>(VP) &&
292 "Tracked Value was replaced by one with an invalid type!");
295 ValueTy *getValPtr() const {
297 return (ValueTy*)ValueHandleBase::getValPtr();
299 void setValPtr(ValueTy *P) {
301 ValueHandleBase::operator=(GetAsValue(P));
304 // Convert a ValueTy*, which may be const, to the type the base
306 static Value *GetAsValue(Value *V) { return V; }
307 static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
310 TrackingVH() : ValueHandleBase(Tracking) {}
311 TrackingVH(ValueTy *P) : ValueHandleBase(Tracking, GetAsValue(P)) {}
313 operator ValueTy*() const {
317 ValueTy *operator=(ValueTy *RHS) {
322 ValueTy *operator->() const { return getValPtr(); }
323 ValueTy &operator*() const { return *getValPtr(); }
326 /// \brief Value handle with callbacks on RAUW and destruction.
328 /// This is a value handle that allows subclasses to define callbacks that run
329 /// when the underlying Value has RAUW called on it or is destroyed. This
330 /// class can be used as the key of a map, as long as the user takes it out of
331 /// the map before calling setValPtr() (since the map has to rearrange itself
332 /// when the pointer changes). Unlike ValueHandleBase, this class has a vtable.
333 class CallbackVH : public ValueHandleBase {
334 virtual void anchor();
336 ~CallbackVH() = default;
337 CallbackVH(const CallbackVH &) = default;
338 CallbackVH &operator=(const CallbackVH &) = default;
340 void setValPtr(Value *P) {
341 ValueHandleBase::operator=(P);
345 CallbackVH() : ValueHandleBase(Callback) {}
346 CallbackVH(Value *P) : ValueHandleBase(Callback, P) {}
348 operator Value*() const {
352 /// \brief Callback for Value destruction.
354 /// Called when this->getValPtr() is destroyed, inside ~Value(), so you
355 /// may call any non-virtual Value method on getValPtr(), but no subclass
356 /// methods. If WeakVH were implemented as a CallbackVH, it would use this
357 /// method to call setValPtr(NULL). AssertingVH would use this method to
358 /// cause an assertion failure.
360 /// All implementations must remove the reference from this object to the
361 /// Value that's being destroyed.
362 virtual void deleted() { setValPtr(nullptr); }
364 /// \brief Callback for Value RAUW.
366 /// Called when this->getValPtr()->replaceAllUsesWith(new_value) is called,
367 /// _before_ any of the uses have actually been replaced. If WeakVH were
368 /// implemented as a CallbackVH, it would use this method to call
369 /// setValPtr(new_value). AssertingVH would do nothing in this method.
370 virtual void allUsesReplacedWith(Value *) {}
373 } // End llvm namespace