LLVM  4.0.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 /// \brief This is the common base class of value handles.
26 ///
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.
31  friend class Value;
32 protected:
33  /// \brief This indicates what sub class the handle actually is.
34  ///
35  /// This is to avoid having a vtable for the light-weight handle pointers. The
36  /// fully general Callback version does have a vtable.
42  };
43 
45  : ValueHandleBase(RHS.PrevPair.getInt(), RHS) {}
46 
48  : PrevPair(nullptr, Kind), Next(nullptr), V(RHS.V) {
49  if (isValid(V))
50  AddToExistingUseList(RHS.getPrevPtr());
51  }
52 
53 private:
55  ValueHandleBase *Next;
56 
57  Value* V;
58 
59 public:
61  : PrevPair(nullptr, Kind), Next(nullptr), V(nullptr) {}
63  : PrevPair(nullptr, Kind), Next(nullptr), V(V) {
64  if (isValid(V))
65  AddToUseList();
66  }
67 
69  if (isValid(V))
70  RemoveFromUseList();
71  }
72 
74  if (V == RHS) return RHS;
75  if (isValid(V)) RemoveFromUseList();
76  V = RHS;
77  if (isValid(V)) AddToUseList();
78  return RHS;
79  }
80 
82  if (V == RHS.V) return RHS.V;
83  if (isValid(V)) RemoveFromUseList();
84  V = RHS.V;
85  if (isValid(V)) AddToExistingUseList(RHS.getPrevPtr());
86  return V;
87  }
88 
89  Value *operator->() const { return V; }
90  Value &operator*() const { return *V; }
91 
92 protected:
93  Value *getValPtr() const { return V; }
94 
95  static bool isValid(Value *V) {
96  return V &&
99  }
100 
101 public:
102  // Callbacks made from Value.
103  static void ValueIsDeleted(Value *V);
104  static void ValueIsRAUWd(Value *Old, Value *New);
105 
106 private:
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); }
111 
112  /// \brief Add this ValueHandle to the use list for V.
113  ///
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);
117 
118  /// \brief Add this ValueHandle to the use list after Node.
119  void AddToExistingUseListAfter(ValueHandleBase *Node);
120 
121  /// \brief Add this ValueHandle to the use list for V.
122  void AddToUseList();
123  /// \brief Remove this ValueHandle from its current use list.
124  void RemoveFromUseList();
125 };
126 
127 /// \brief Value handle that is nullable, but tries to track the Value.
128 ///
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
133 /// changes).
134 class WeakVH : public ValueHandleBase {
135 public:
138  WeakVH(const WeakVH &RHS)
139  : ValueHandleBase(Weak, RHS) {}
140 
141  WeakVH &operator=(const WeakVH &RHS) = default;
142 
144  return ValueHandleBase::operator=(RHS);
145  }
147  return ValueHandleBase::operator=(RHS);
148  }
149 
150  operator Value*() const {
151  return getValPtr();
152  }
153 };
154 
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; }
160 };
161 template <> struct simplify_type<const WeakVH> {
162  typedef Value *SimpleType;
163  static SimpleType getSimplifiedValue(const WeakVH &WVH) { return WVH; }
164 };
165 
166 /// \brief Value handle that asserts if the Value is deleted.
167 ///
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
175 /// delete occurs.
176 ///
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>
183 #ifndef NDEBUG
184  : public ValueHandleBase
185 #endif
186  {
187  friend struct DenseMapInfo<AssertingVH<ValueTy> >;
188 
189 #ifndef NDEBUG
190  Value *getRawValPtr() const { return ValueHandleBase::getValPtr(); }
191  void setRawValPtr(Value *P) { ValueHandleBase::operator=(P); }
192 #else
193  Value *ThePtr;
194  Value *getRawValPtr() const { return ThePtr; }
195  void setRawValPtr(Value *P) { ThePtr = P; }
196 #endif
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); }
200 
201  ValueTy *getValPtr() const { return static_cast<ValueTy *>(getRawValPtr()); }
202  void setValPtr(ValueTy *P) { setRawValPtr(GetAsValue(P)); }
203 
204 public:
205 #ifndef NDEBUG
207  AssertingVH(ValueTy *P) : ValueHandleBase(Assert, GetAsValue(P)) {}
209 #else
210  AssertingVH() : ThePtr(nullptr) {}
211  AssertingVH(ValueTy *P) : ThePtr(GetAsValue(P)) {}
212 #endif
213 
214  operator ValueTy*() const {
215  return getValPtr();
216  }
217 
218  ValueTy *operator=(ValueTy *RHS) {
219  setValPtr(RHS);
220  return getValPtr();
221  }
222  ValueTy *operator=(const AssertingVH<ValueTy> &RHS) {
223  setValPtr(RHS.getValPtr());
224  return getValPtr();
225  }
226 
227  ValueTy *operator->() const { return getValPtr(); }
228  ValueTy &operator*() const { return *getValPtr(); }
229 };
230 
231 // Specialize DenseMapInfo to allow AssertingVH to participate in DenseMap.
232 template<typename T>
234  static inline AssertingVH<T> getEmptyKey() {
235  AssertingVH<T> Res;
236  Res.setRawValPtr(DenseMapInfo<Value *>::getEmptyKey());
237  return Res;
238  }
239  static inline AssertingVH<T> getTombstoneKey() {
240  AssertingVH<T> Res;
241  Res.setRawValPtr(DenseMapInfo<Value *>::getTombstoneKey());
242  return Res;
243  }
244  static unsigned getHashValue(const AssertingVH<T> &Val) {
245  return DenseMapInfo<Value *>::getHashValue(Val.getRawValPtr());
246  }
247  static bool isEqual(const AssertingVH<T> &LHS, const AssertingVH<T> &RHS) {
248  return DenseMapInfo<Value *>::isEqual(LHS.getRawValPtr(),
249  RHS.getRawValPtr());
250  }
251 };
252 
253 template <typename T>
255 #ifdef NDEBUG
256  static const bool value = true;
257 #else
258  static const bool value = false;
259 #endif
260 };
261 
262 
263 /// \brief Value handle that tracks a Value across RAUW.
264 ///
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.
268 ///
269 /// It is an error to do anything with a TrackingVH whose value has been
270 /// destroyed, except to destruct it.
271 ///
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 {
278 
279  // Null is always ok.
280  if (!VP) return;
281 
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!");
286 
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!");
293  }
294 
295  ValueTy *getValPtr() const {
296  CheckValidity();
297  return (ValueTy*)ValueHandleBase::getValPtr();
298  }
299  void setValPtr(ValueTy *P) {
300  CheckValidity();
301  ValueHandleBase::operator=(GetAsValue(P));
302  }
303 
304  // Convert a ValueTy*, which may be const, to the type the base
305  // class expects.
306  static Value *GetAsValue(Value *V) { return V; }
307  static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
308 
309 public:
311  TrackingVH(ValueTy *P) : ValueHandleBase(Tracking, GetAsValue(P)) {}
312 
313  operator ValueTy*() const {
314  return getValPtr();
315  }
316 
317  ValueTy *operator=(ValueTy *RHS) {
318  setValPtr(RHS);
319  return getValPtr();
320  }
321 
322  ValueTy *operator->() const { return getValPtr(); }
323  ValueTy &operator*() const { return *getValPtr(); }
324 };
325 
326 /// \brief Value handle with callbacks on RAUW and destruction.
327 ///
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();
335 protected:
336  ~CallbackVH() = default;
337  CallbackVH(const CallbackVH &) = default;
338  CallbackVH &operator=(const CallbackVH &) = default;
339 
340  void setValPtr(Value *P) {
342  }
343 
344 public:
347 
348  operator Value*() const {
349  return getValPtr();
350  }
351 
352  /// \brief Callback for Value destruction.
353  ///
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.
359  ///
360  /// All implementations must remove the reference from this object to the
361  /// Value that's being destroyed.
362  virtual void deleted() { setValPtr(nullptr); }
363 
364  /// \brief Callback for Value RAUW.
365  ///
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 *) {}
371 };
372 
373 } // End llvm namespace
374 
375 #endif
This is the common base class of value handles.
Definition: ValueHandle.h:30
ValueTy & operator*() const
Definition: ValueHandle.h:228
static void ValueIsDeleted(Value *V)
Definition: Value.cpp:781
ValueTy & operator*() const
Definition: ValueHandle.h:323
ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
Definition: ValueHandle.h:47
ValueTy * operator=(const AssertingVH< ValueTy > &RHS)
Definition: ValueHandle.h:222
friend class Value
Definition: ValueHandle.h:31
Value * operator=(Value *RHS)
Definition: ValueHandle.h:143
CallbackVH & operator=(const CallbackVH &)=default
WeakVH(Value *P)
Definition: ValueHandle.h:137
~CallbackVH()=default
static const bool value
Definition: type_traits.h:48
ValueTy * operator->() const
Definition: ValueHandle.h:322
ValueHandleBase(HandleBaseKind Kind)
Definition: ValueHandle.h:60
Value * operator->() const
Definition: ValueHandle.h:89
void setValPtr(Value *P)
Definition: ValueHandle.h:340
static unsigned getInt(StringRef R)
Get an unsigned integer, including error checks.
Definition: DataLayout.cpp:209
Value * operator=(const ValueHandleBase &RHS)
Definition: ValueHandle.h:146
virtual void allUsesReplacedWith(Value *)
Callback for Value RAUW.
Definition: ValueHandle.h:370
TrackingVH(ValueTy *P)
Definition: ValueHandle.h:311
static bool isEqual(const Function &Caller, const Function &Callee)
Value handle that is nullable, but tries to track the Value.
Definition: ValueHandle.h:134
static void ValueIsRAUWd(Value *Old, Value *New)
Definition: Value.cpp:837
static SimpleType getSimplifiedValue(const WeakVH &WVH)
Definition: ValueHandle.h:163
#define P(N)
ValueTy * operator->() const
Definition: ValueHandle.h:227
Value * getValPtr() const
Definition: ValueHandle.h:93
PointerIntPair - This class implements a pair of a pointer and small integer.
static bool isValid(Value *V)
Definition: ValueHandle.h:95
Value handle that tracks a Value across RAUW.
Definition: ValueHandle.h:275
ValueHandleBase(const ValueHandleBase &RHS)
Definition: ValueHandle.h:44
Value & operator*() const
Definition: ValueHandle.h:90
ValueHandleBase(HandleBaseKind Kind, Value *V)
Definition: ValueHandle.h:62
Value * operator=(const ValueHandleBase &RHS)
Definition: ValueHandle.h:81
static SimpleType getSimplifiedValue(WeakVH &WVH)
Definition: ValueHandle.h:159
virtual void deleted()
Callback for Value destruction.
Definition: ValueHandle.h:362
static bool isEqual(const AssertingVH< T > &LHS, const AssertingVH< T > &RHS)
Definition: ValueHandle.h:247
CallbackVH(Value *P)
Definition: ValueHandle.h:346
AssertingVH(const AssertingVH &RHS)
Definition: ValueHandle.h:208
isPodLike - This is a type trait that is used to determine whether a given type can be copied around ...
Definition: ArrayRef.h:507
ValueTy * operator=(ValueTy *RHS)
Definition: ValueHandle.h:218
WeakVH & operator=(const WeakVH &RHS)=default
Value handle that asserts if the Value is deleted.
Definition: ValueHandle.h:182
#define NDEBUG
Definition: regutils.h:48
const NodeList & List
Definition: RDFGraph.cpp:205
static unsigned getHashValue(const AssertingVH< T > &Val)
Definition: ValueHandle.h:244
Value * operator=(Value *RHS)
Definition: ValueHandle.h:73
WeakVH(const WeakVH &RHS)
Definition: ValueHandle.h:138
static AssertingVH< T > getEmptyKey()
Definition: ValueHandle.h:234
ValueTy * operator=(ValueTy *RHS)
Definition: ValueHandle.h:317
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
HandleBaseKind
This indicates what sub class the handle actually is.
Definition: ValueHandle.h:37
aarch64 promote const
LLVM Value Representation.
Definition: Value.h:71
AssertingVH(ValueTy *P)
Definition: ValueHandle.h:207
static AssertingVH< T > getTombstoneKey()
Definition: ValueHandle.h:239
Value handle with callbacks on RAUW and destruction.
Definition: ValueHandle.h:333
int * Ptr