LLVM  3.7.0
ValueHandle.h
Go to the documentation of this file.
1 //===- ValueHandle.h - Value Smart Pointer classes --------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the ValueHandle class and its sub-classes.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_IR_VALUEHANDLE_H
15 #define LLVM_IR_VALUEHANDLE_H
16 
17 #include "llvm/ADT/DenseMapInfo.h"
19 #include "llvm/IR/Value.h"
20 
21 namespace llvm {
22 class ValueHandleBase;
23 template<typename From> struct simplify_type;
24 
25 // ValueHandleBase** is only 4-byte aligned.
26 template<>
28 public:
29  static inline void *getAsVoidPointer(ValueHandleBase** P) { return P; }
30  static inline ValueHandleBase **getFromVoidPointer(void *P) {
31  return static_cast<ValueHandleBase**>(P);
32  }
33  enum { NumLowBitsAvailable = 2 };
34 };
35 
36 /// \brief This is the common base class of value handles.
37 ///
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.
42  friend class Value;
43 protected:
44  /// \brief This indicates what sub class the handle actually is.
45  ///
46  /// This is to avoid having a vtable for the light-weight handle pointers. The
47  /// fully general Callback version does have a vtable.
53  };
54 
55 private:
57  ValueHandleBase *Next;
58 
59  Value* V;
60 
61  ValueHandleBase(const ValueHandleBase&) = delete;
62 public:
64  : PrevPair(nullptr, Kind), Next(nullptr), V(nullptr) {}
66  : PrevPair(nullptr, Kind), Next(nullptr), V(V) {
67  if (isValid(V))
68  AddToUseList();
69  }
71  : PrevPair(nullptr, Kind), Next(nullptr), V(RHS.V) {
72  if (isValid(V))
73  AddToExistingUseList(RHS.getPrevPtr());
74  }
76  if (isValid(V))
77  RemoveFromUseList();
78  }
79 
81  if (V == RHS) return RHS;
82  if (isValid(V)) RemoveFromUseList();
83  V = RHS;
84  if (isValid(V)) AddToUseList();
85  return RHS;
86  }
87 
89  if (V == RHS.V) return RHS.V;
90  if (isValid(V)) RemoveFromUseList();
91  V = RHS.V;
92  if (isValid(V)) AddToExistingUseList(RHS.getPrevPtr());
93  return V;
94  }
95 
96  Value *operator->() const { return V; }
97  Value &operator*() const { return *V; }
98 
99 protected:
100  Value *getValPtr() const { return V; }
101 
102  static bool isValid(Value *V) {
103  return V &&
106  }
107 
108 public:
109  // Callbacks made from Value.
110  static void ValueIsDeleted(Value *V);
111  static void ValueIsRAUWd(Value *Old, Value *New);
112 
113 private:
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); }
118 
119  /// \brief Add this ValueHandle to the use list for V.
120  ///
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);
124 
125  /// \brief Add this ValueHandle to the use list after Node.
126  void AddToExistingUseListAfter(ValueHandleBase *Node);
127 
128  /// \brief Add this ValueHandle to the use list for V.
129  void AddToUseList();
130  /// \brief Remove this ValueHandle from its current use list.
131  void RemoveFromUseList();
132 };
133 
134 /// \brief Value handle that is nullable, but tries to track the Value.
135 ///
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
140 /// changes).
141 class WeakVH : public ValueHandleBase {
142 public:
145  WeakVH(const WeakVH &RHS)
146  : ValueHandleBase(Weak, RHS) {}
147 
149  return ValueHandleBase::operator=(RHS);
150  }
152  return ValueHandleBase::operator=(RHS);
153  }
154 
155  operator Value*() const {
156  return getValPtr();
157  }
158 };
159 
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) { return WVH; }
165 };
166 template <> struct simplify_type<const WeakVH> {
167  typedef Value *SimpleType;
168  static SimpleType getSimplifiedValue(const WeakVH &WVH) { return WVH; }
169 };
170 
171 /// \brief Value handle that asserts if the Value is deleted.
172 ///
173 /// This is a Value Handle that points to a value and asserts out if the value
174 /// is destroyed while the handle is still live. This is very useful for
175 /// catching dangling pointer bugs and other things which can be non-obvious.
176 /// One particularly useful place to use this is as the Key of a map. Dangling
177 /// pointer bugs often lead to really subtle bugs that only occur if another
178 /// object happens to get allocated to the same address as the old one. Using
179 /// an AssertingVH ensures that an assert is triggered as soon as the bad
180 /// delete occurs.
181 ///
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>
188 #ifndef NDEBUG
189  : public ValueHandleBase
190 #endif
191  {
192  friend struct DenseMapInfo<AssertingVH<ValueTy> >;
193 
194 #ifndef NDEBUG
195  Value *getRawValPtr() const { return ValueHandleBase::getValPtr(); }
196  void setRawValPtr(Value *P) { ValueHandleBase::operator=(P); }
197 #else
198  Value *ThePtr;
199  Value *getRawValPtr() const { return ThePtr; }
200  void setRawValPtr(Value *P) { ThePtr = P; }
201 #endif
202  // Convert a ValueTy*, which may be const, to the raw Value*.
203  static Value *GetAsValue(Value *V) { return V; }
204  static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
205 
206  ValueTy *getValPtr() const { return static_cast<ValueTy *>(getRawValPtr()); }
207  void setValPtr(ValueTy *P) { setRawValPtr(GetAsValue(P)); }
208 
209 public:
210 #ifndef NDEBUG
212  AssertingVH(ValueTy *P) : ValueHandleBase(Assert, GetAsValue(P)) {}
214 #else
215  AssertingVH() : ThePtr(nullptr) {}
216  AssertingVH(ValueTy *P) : ThePtr(GetAsValue(P)) {}
217 #endif
218 
219  operator ValueTy*() const {
220  return getValPtr();
221  }
222 
223  ValueTy *operator=(ValueTy *RHS) {
224  setValPtr(RHS);
225  return getValPtr();
226  }
227  ValueTy *operator=(const AssertingVH<ValueTy> &RHS) {
228  setValPtr(RHS.getValPtr());
229  return getValPtr();
230  }
231 
232  ValueTy *operator->() const { return getValPtr(); }
233  ValueTy &operator*() const { return *getValPtr(); }
234 };
235 
236 // Specialize DenseMapInfo to allow AssertingVH to participate in DenseMap.
237 template<typename T>
239  static inline AssertingVH<T> getEmptyKey() {
240  AssertingVH<T> Res;
241  Res.setRawValPtr(DenseMapInfo<Value *>::getEmptyKey());
242  return Res;
243  }
244  static inline AssertingVH<T> getTombstoneKey() {
245  AssertingVH<T> Res;
246  Res.setRawValPtr(DenseMapInfo<Value *>::getTombstoneKey());
247  return Res;
248  }
249  static unsigned getHashValue(const AssertingVH<T> &Val) {
250  return DenseMapInfo<Value *>::getHashValue(Val.getRawValPtr());
251  }
252  static bool isEqual(const AssertingVH<T> &LHS, const AssertingVH<T> &RHS) {
253  return DenseMapInfo<Value *>::isEqual(LHS.getRawValPtr(),
254  RHS.getRawValPtr());
255  }
256 };
257 
258 template <typename T>
260 #ifdef NDEBUG
261  static const bool value = true;
262 #else
263  static const bool value = false;
264 #endif
265 };
266 
267 
268 /// \brief Value handle that tracks a Value across RAUW.
269 ///
270 /// TrackingVH is designed for situations where a client needs to hold a handle
271 /// to a Value (or subclass) across some operations which may move that value,
272 /// but should never destroy it or replace it with some unacceptable type.
273 ///
274 /// It is an error to do anything with a TrackingVH whose value has been
275 /// destroyed, except to destruct it.
276 ///
277 /// It is an error to attempt to replace a value with one of a type which is
278 /// incompatible with any of its outstanding TrackingVHs.
279 template<typename ValueTy>
280 class TrackingVH : public ValueHandleBase {
281  void CheckValidity() const {
283 
284  // Null is always ok.
285  if (!VP) return;
286 
287  // Check that this value is valid (i.e., it hasn't been deleted). We
288  // explicitly delay this check until access to avoid requiring clients to be
289  // unnecessarily careful w.r.t. destruction.
290  assert(ValueHandleBase::isValid(VP) && "Tracked Value was deleted!");
291 
292  // Check that the value is a member of the correct subclass. We would like
293  // to check this property on assignment for better debugging, but we don't
294  // want to require a virtual interface on this VH. Instead we allow RAUW to
295  // replace this value with a value of an invalid type, and check it here.
296  assert(isa<ValueTy>(VP) &&
297  "Tracked Value was replaced by one with an invalid type!");
298  }
299 
300  ValueTy *getValPtr() const {
301  CheckValidity();
302  return (ValueTy*)ValueHandleBase::getValPtr();
303  }
304  void setValPtr(ValueTy *P) {
305  CheckValidity();
306  ValueHandleBase::operator=(GetAsValue(P));
307  }
308 
309  // Convert a ValueTy*, which may be const, to the type the base
310  // class expects.
311  static Value *GetAsValue(Value *V) { return V; }
312  static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
313 
314 public:
316  TrackingVH(ValueTy *P) : ValueHandleBase(Tracking, GetAsValue(P)) {}
318 
319  operator ValueTy*() const {
320  return getValPtr();
321  }
322 
323  ValueTy *operator=(ValueTy *RHS) {
324  setValPtr(RHS);
325  return getValPtr();
326  }
327  ValueTy *operator=(const TrackingVH<ValueTy> &RHS) {
328  setValPtr(RHS.getValPtr());
329  return getValPtr();
330  }
331 
332  ValueTy *operator->() const { return getValPtr(); }
333  ValueTy &operator*() const { return *getValPtr(); }
334 };
335 
336 /// \brief Value handle with callbacks on RAUW and destruction.
337 ///
338 /// This is a value handle that allows subclasses to define callbacks that run
339 /// when the underlying Value has RAUW called on it or is destroyed. This
340 /// class can be used as the key of a map, as long as the user takes it out of
341 /// the map before calling setValPtr() (since the map has to rearrange itself
342 /// when the pointer changes). Unlike ValueHandleBase, this class has a vtable
343 /// and a virtual destructor.
344 class CallbackVH : public ValueHandleBase {
345  virtual void anchor();
346 protected:
347  CallbackVH(const CallbackVH &RHS)
348  : ValueHandleBase(Callback, RHS) {}
349 
350  virtual ~CallbackVH() {}
351 
352  void setValPtr(Value *P) {
354  }
355 
356 public:
359 
360  operator Value*() const {
361  return getValPtr();
362  }
363 
364  /// \brief Callback for Value destruction.
365  ///
366  /// Called when this->getValPtr() is destroyed, inside ~Value(), so you
367  /// may call any non-virtual Value method on getValPtr(), but no subclass
368  /// methods. If WeakVH were implemented as a CallbackVH, it would use this
369  /// method to call setValPtr(NULL). AssertingVH would use this method to
370  /// cause an assertion failure.
371  ///
372  /// All implementations must remove the reference from this object to the
373  /// Value that's being destroyed.
374  virtual void deleted() { setValPtr(nullptr); }
375 
376  /// \brief Callback for Value RAUW.
377  ///
378  /// Called when this->getValPtr()->replaceAllUsesWith(new_value) is called,
379  /// _before_ any of the uses have actually been replaced. If WeakVH were
380  /// implemented as a CallbackVH, it would use this method to call
381  /// setValPtr(new_value). AssertingVH would do nothing in this method.
382  virtual void allUsesReplacedWith(Value *) {}
383 };
384 
385 } // End llvm namespace
386 
387 #endif
This is the common base class of value handles.
Definition: ValueHandle.h:41
ValueTy & operator*() const
Definition: ValueHandle.h:233
CallbackVH(const CallbackVH &RHS)
Definition: ValueHandle.h:347
static void ValueIsDeleted(Value *V)
Definition: Value.cpp:637
ValueTy & operator*() const
Definition: ValueHandle.h:333
ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
Definition: ValueHandle.h:70
ValueTy * operator=(const AssertingVH< ValueTy > &RHS)
Definition: ValueHandle.h:227
friend class Value
Definition: ValueHandle.h:42
Value * operator=(Value *RHS)
Definition: ValueHandle.h:148
WeakVH(Value *P)
Definition: ValueHandle.h:144
static const bool value
Definition: type_traits.h:46
ValueTy * operator->() const
Definition: ValueHandle.h:332
ValueHandleBase(HandleBaseKind Kind)
Definition: ValueHandle.h:63
PointerLikeTypeTraits - This is a traits object that is used to handle pointer types and things that ...
Value * operator->() const
Definition: ValueHandle.h:96
void setValPtr(Value *P)
Definition: ValueHandle.h:352
static void * getAsVoidPointer(ValueHandleBase **P)
Definition: ValueHandle.h:29
Value * operator=(const ValueHandleBase &RHS)
Definition: ValueHandle.h:151
virtual void allUsesReplacedWith(Value *)
Callback for Value RAUW.
Definition: ValueHandle.h:382
TrackingVH(ValueTy *P)
Definition: ValueHandle.h:316
Value handle that is nullable, but tries to track the Value.
Definition: ValueHandle.h:141
static void ValueIsRAUWd(Value *Old, Value *New)
Definition: Value.cpp:694
static SimpleType getSimplifiedValue(const WeakVH &WVH)
Definition: ValueHandle.h:168
#define P(N)
ValueTy * operator->() const
Definition: ValueHandle.h:232
Value * getValPtr() const
Definition: ValueHandle.h:100
PointerIntPair - This class implements a pair of a pointer and small integer.
static bool isValid(Value *V)
Definition: ValueHandle.h:102
Value handle that tracks a Value across RAUW.
Definition: ValueHandle.h:280
Value & operator*() const
Definition: ValueHandle.h:97
ValueHandleBase(HandleBaseKind Kind, Value *V)
Definition: ValueHandle.h:65
Value * operator=(const ValueHandleBase &RHS)
Definition: ValueHandle.h:88
static SimpleType getSimplifiedValue(WeakVH &WVH)
Definition: ValueHandle.h:164
virtual void deleted()
Callback for Value destruction.
Definition: ValueHandle.h:374
static bool isEqual(const AssertingVH< T > &LHS, const AssertingVH< T > &RHS)
Definition: ValueHandle.h:252
CallbackVH(Value *P)
Definition: ValueHandle.h:358
static ValueHandleBase ** getFromVoidPointer(void *P)
Definition: ValueHandle.h:30
ValueTy * operator=(const TrackingVH< ValueTy > &RHS)
Definition: ValueHandle.h:327
AssertingVH(const AssertingVH &RHS)
Definition: ValueHandle.h:213
isPodLike - This is a type trait that is used to determine whether a given type can be copied around ...
Definition: ArrayRef.h:365
ValueTy * operator=(ValueTy *RHS)
Definition: ValueHandle.h:223
Value handle that asserts if the Value is deleted.
Definition: ValueHandle.h:187
virtual ~CallbackVH()
Definition: ValueHandle.h:350
#define NDEBUG
Definition: regutils.h:48
static unsigned getHashValue(const AssertingVH< T > &Val)
Definition: ValueHandle.h:249
Value * operator=(Value *RHS)
Definition: ValueHandle.h:80
WeakVH(const WeakVH &RHS)
Definition: ValueHandle.h:145
TrackingVH(const TrackingVH &RHS)
Definition: ValueHandle.h:317
static AssertingVH< T > getEmptyKey()
Definition: ValueHandle.h:239
ValueTy * operator=(ValueTy *RHS)
Definition: ValueHandle.h:323
HandleBaseKind
This indicates what sub class the handle actually is.
Definition: ValueHandle.h:48
const ARM::ArchExtKind Kind
aarch64 promote const
LLVM Value Representation.
Definition: Value.h:69
AssertingVH(ValueTy *P)
Definition: ValueHandle.h:212
static AssertingVH< T > getTombstoneKey()
Definition: ValueHandle.h:244
Value handle with callbacks on RAUW and destruction.
Definition: ValueHandle.h:344