• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- llvm/Support/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_SUPPORT_VALUEHANDLE_H
15 #define LLVM_SUPPORT_VALUEHANDLE_H
16 
17 #include "llvm/ADT/DenseMapInfo.h"
18 #include "llvm/ADT/PointerIntPair.h"
19 #include "llvm/Value.h"
20 
21 namespace llvm {
22 class ValueHandleBase;
23 
24 // ValueHandleBase** is only 4-byte aligned.
25 template<>
26 class PointerLikeTypeTraits<ValueHandleBase**> {
27 public:
getAsVoidPointer(ValueHandleBase ** P)28   static inline void *getAsVoidPointer(ValueHandleBase** P) { return P; }
getFromVoidPointer(void * P)29   static inline ValueHandleBase **getFromVoidPointer(void *P) {
30     return static_cast<ValueHandleBase**>(P);
31   }
32   enum { NumLowBitsAvailable = 2 };
33 };
34 
35 /// ValueHandleBase - This is the common base class of value handles.
36 /// ValueHandle's are smart pointers to Value's that have special behavior when
37 /// the value is deleted or ReplaceAllUsesWith'd.  See the specific handles
38 /// below for details.
39 ///
40 class ValueHandleBase {
41   friend class Value;
42 protected:
43   /// HandleBaseKind - This indicates what sub class the handle actually is.
44   /// This is to avoid having a vtable for the light-weight handle pointers. The
45   /// fully general Callback version does have a vtable.
46   enum HandleBaseKind {
47     Assert,
48     Callback,
49     Tracking,
50     Weak
51   };
52 
53 private:
54   PointerIntPair<ValueHandleBase**, 2, HandleBaseKind> PrevPair;
55   ValueHandleBase *Next;
56 
57   // A subclass may want to store some information along with the value
58   // pointer. Allow them to do this by making the value pointer a pointer-int
59   // pair. The 'setValPtrInt' and 'getValPtrInt' methods below give them this
60   // access.
61   PointerIntPair<Value*, 2> VP;
62 
63   explicit ValueHandleBase(const ValueHandleBase&); // DO NOT IMPLEMENT.
64 public:
ValueHandleBase(HandleBaseKind Kind)65   explicit ValueHandleBase(HandleBaseKind Kind)
66     : PrevPair(0, Kind), Next(0), VP(0, 0) {}
ValueHandleBase(HandleBaseKind Kind,Value * V)67   ValueHandleBase(HandleBaseKind Kind, Value *V)
68     : PrevPair(0, Kind), Next(0), VP(V, 0) {
69     if (isValid(VP.getPointer()))
70       AddToUseList();
71   }
ValueHandleBase(HandleBaseKind Kind,const ValueHandleBase & RHS)72   ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
73     : PrevPair(0, Kind), Next(0), VP(RHS.VP) {
74     if (isValid(VP.getPointer()))
75       AddToExistingUseList(RHS.getPrevPtr());
76   }
~ValueHandleBase()77   ~ValueHandleBase() {
78     if (isValid(VP.getPointer()))
79       RemoveFromUseList();
80   }
81 
82   Value *operator=(Value *RHS) {
83     if (VP.getPointer() == RHS) return RHS;
84     if (isValid(VP.getPointer())) RemoveFromUseList();
85     VP.setPointer(RHS);
86     if (isValid(VP.getPointer())) AddToUseList();
87     return RHS;
88   }
89 
90   Value *operator=(const ValueHandleBase &RHS) {
91     if (VP.getPointer() == RHS.VP.getPointer()) return RHS.VP.getPointer();
92     if (isValid(VP.getPointer())) RemoveFromUseList();
93     VP.setPointer(RHS.VP.getPointer());
94     if (isValid(VP.getPointer())) AddToExistingUseList(RHS.getPrevPtr());
95     return VP.getPointer();
96   }
97 
98   Value *operator->() const { return getValPtr(); }
99   Value &operator*() const { return *getValPtr(); }
100 
101 protected:
getValPtr()102   Value *getValPtr() const { return VP.getPointer(); }
103 
setValPtrInt(unsigned K)104   void setValPtrInt(unsigned K) { VP.setInt(K); }
getValPtrInt()105   unsigned getValPtrInt() const { return VP.getInt(); }
106 
isValid(Value * V)107   static bool isValid(Value *V) {
108     return V &&
109            V != DenseMapInfo<Value *>::getEmptyKey() &&
110            V != DenseMapInfo<Value *>::getTombstoneKey();
111   }
112 
113 private:
114   // Callbacks made from Value.
115   static void ValueIsDeleted(Value *V);
116   static void ValueIsRAUWd(Value *Old, Value *New);
117 
118   // Internal implementation details.
getPrevPtr()119   ValueHandleBase **getPrevPtr() const { return PrevPair.getPointer(); }
getKind()120   HandleBaseKind getKind() const { return PrevPair.getInt(); }
setPrevPtr(ValueHandleBase ** Ptr)121   void setPrevPtr(ValueHandleBase **Ptr) { PrevPair.setPointer(Ptr); }
122 
123   /// AddToExistingUseList - Add this ValueHandle to the use list for VP, where
124   /// List is the address of either the head of the list or a Next node within
125   /// the existing use list.
126   void AddToExistingUseList(ValueHandleBase **List);
127 
128   /// AddToExistingUseListAfter - Add this ValueHandle to the use list after
129   /// Node.
130   void AddToExistingUseListAfter(ValueHandleBase *Node);
131 
132   /// AddToUseList - Add this ValueHandle to the use list for VP.
133   void AddToUseList();
134   /// RemoveFromUseList - Remove this ValueHandle from its current use list.
135   void RemoveFromUseList();
136 };
137 
138 /// WeakVH - This is a value handle that tries hard to point to a Value, even
139 /// across RAUW operations, but will null itself out if the value is destroyed.
140 /// this is useful for advisory sorts of information, but should not be used as
141 /// the key of a map (since the map would have to rearrange itself when the
142 /// pointer changes).
143 class WeakVH : public ValueHandleBase {
144 public:
WeakVH()145   WeakVH() : ValueHandleBase(Weak) {}
WeakVH(Value * P)146   WeakVH(Value *P) : ValueHandleBase(Weak, P) {}
WeakVH(const WeakVH & RHS)147   WeakVH(const WeakVH &RHS)
148     : ValueHandleBase(Weak, RHS) {}
149 
150   Value *operator=(Value *RHS) {
151     return ValueHandleBase::operator=(RHS);
152   }
153   Value *operator=(const ValueHandleBase &RHS) {
154     return ValueHandleBase::operator=(RHS);
155   }
156 
157   operator Value*() const {
158     return getValPtr();
159   }
160 };
161 
162 // Specialize simplify_type to allow WeakVH to participate in
163 // dyn_cast, isa, etc.
164 template<typename From> struct simplify_type;
165 template<> struct simplify_type<const WeakVH> {
166   typedef Value* SimpleType;
167   static SimpleType getSimplifiedValue(const WeakVH &WVH) {
168     return static_cast<Value *>(WVH);
169   }
170 };
171 template<> struct simplify_type<WeakVH> : public simplify_type<const WeakVH> {};
172 
173 /// AssertingVH - This is a Value Handle that points to a value and asserts out
174 /// if the value is destroyed while the handle is still live.  This is very
175 /// useful for catching dangling pointer bugs and other things which can be
176 /// non-obvious.  One particularly useful place to use this is as the Key of a
177 /// map.  Dangling pointer bugs often lead to really subtle bugs that only occur
178 /// if another object happens to get allocated to the same address as the old
179 /// one.  Using an AssertingVH ensures that an assert is triggered as soon as
180 /// the bad 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>
187 class AssertingVH
188 #ifndef NDEBUG
189   : public ValueHandleBase
190 #endif
191   {
192 
193 #ifndef NDEBUG
194   ValueTy *getValPtr() const {
195     return static_cast<ValueTy*>(ValueHandleBase::getValPtr());
196   }
197   void setValPtr(ValueTy *P) {
198     ValueHandleBase::operator=(GetAsValue(P));
199   }
200 #else
201   ValueTy *ThePtr;
202   ValueTy *getValPtr() const { return ThePtr; }
203   void setValPtr(ValueTy *P) { ThePtr = P; }
204 #endif
205 
206   // Convert a ValueTy*, which may be const, to the type the base
207   // class expects.
208   static Value *GetAsValue(Value *V) { return V; }
209   static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
210 
211 public:
212 #ifndef NDEBUG
213   AssertingVH() : ValueHandleBase(Assert) {}
214   AssertingVH(ValueTy *P) : ValueHandleBase(Assert, GetAsValue(P)) {}
215   AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
216 #else
217   AssertingVH() : ThePtr(0) {}
218   AssertingVH(ValueTy *P) : ThePtr(P) {}
219 #endif
220 
221   operator ValueTy*() const {
222     return getValPtr();
223   }
224 
225   ValueTy *operator=(ValueTy *RHS) {
226     setValPtr(RHS);
227     return getValPtr();
228   }
229   ValueTy *operator=(const AssertingVH<ValueTy> &RHS) {
230     setValPtr(RHS.getValPtr());
231     return getValPtr();
232   }
233 
234   ValueTy *operator->() const { return getValPtr(); }
235   ValueTy &operator*() const { return *getValPtr(); }
236 };
237 
238 // Specialize simplify_type to allow AssertingVH to participate in
239 // dyn_cast, isa, etc.
240 template<typename From> struct simplify_type;
241 template<> struct simplify_type<const AssertingVH<Value> > {
242   typedef Value* SimpleType;
243   static SimpleType getSimplifiedValue(const AssertingVH<Value> &AVH) {
244     return static_cast<Value *>(AVH);
245   }
246 };
247 template<> struct simplify_type<AssertingVH<Value> >
248   : public simplify_type<const AssertingVH<Value> > {};
249 
250 // Specialize DenseMapInfo to allow AssertingVH to participate in DenseMap.
251 template<typename T>
252 struct DenseMapInfo<AssertingVH<T> > {
253   typedef DenseMapInfo<T*> PointerInfo;
254   static inline AssertingVH<T> getEmptyKey() {
255     return AssertingVH<T>(PointerInfo::getEmptyKey());
256   }
257   static inline T* getTombstoneKey() {
258     return AssertingVH<T>(PointerInfo::getTombstoneKey());
259   }
260   static unsigned getHashValue(const AssertingVH<T> &Val) {
261     return PointerInfo::getHashValue(Val);
262   }
263   static bool isEqual(const AssertingVH<T> &LHS, const AssertingVH<T> &RHS) {
264     return LHS == RHS;
265   }
266 };
267 
268 template <typename T>
269 struct isPodLike<AssertingVH<T> > {
270 #ifdef NDEBUG
271   static const bool value = true;
272 #else
273   static const bool value = false;
274 #endif
275 };
276 
277 
278 /// TrackingVH - This is a value handle that tracks a Value (or Value subclass),
279 /// even across RAUW operations.
280 ///
281 /// TrackingVH is designed for situations where a client needs to hold a handle
282 /// to a Value (or subclass) across some operations which may move that value,
283 /// but should never destroy it or replace it with some unacceptable type.
284 ///
285 /// It is an error to do anything with a TrackingVH whose value has been
286 /// destroyed, except to destruct it.
287 ///
288 /// It is an error to attempt to replace a value with one of a type which is
289 /// incompatible with any of its outstanding TrackingVHs.
290 template<typename ValueTy>
291 class TrackingVH : public ValueHandleBase {
292   void CheckValidity() const {
293     Value *VP = ValueHandleBase::getValPtr();
294 
295     // Null is always ok.
296     if (!VP) return;
297 
298     // Check that this value is valid (i.e., it hasn't been deleted). We
299     // explicitly delay this check until access to avoid requiring clients to be
300     // unnecessarily careful w.r.t. destruction.
301     assert(ValueHandleBase::isValid(VP) && "Tracked Value was deleted!");
302 
303     // Check that the value is a member of the correct subclass. We would like
304     // to check this property on assignment for better debugging, but we don't
305     // want to require a virtual interface on this VH. Instead we allow RAUW to
306     // replace this value with a value of an invalid type, and check it here.
307     assert(isa<ValueTy>(VP) &&
308            "Tracked Value was replaced by one with an invalid type!");
309   }
310 
311   ValueTy *getValPtr() const {
312     CheckValidity();
313     return (ValueTy*)ValueHandleBase::getValPtr();
314   }
315   void setValPtr(ValueTy *P) {
316     CheckValidity();
317     ValueHandleBase::operator=(GetAsValue(P));
318   }
319 
320   // Convert a ValueTy*, which may be const, to the type the base
321   // class expects.
322   static Value *GetAsValue(Value *V) { return V; }
323   static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
324 
325 public:
326   TrackingVH() : ValueHandleBase(Tracking) {}
327   TrackingVH(ValueTy *P) : ValueHandleBase(Tracking, GetAsValue(P)) {}
328   TrackingVH(const TrackingVH &RHS) : ValueHandleBase(Tracking, RHS) {}
329 
330   operator ValueTy*() const {
331     return getValPtr();
332   }
333 
334   ValueTy *operator=(ValueTy *RHS) {
335     setValPtr(RHS);
336     return getValPtr();
337   }
338   ValueTy *operator=(const TrackingVH<ValueTy> &RHS) {
339     setValPtr(RHS.getValPtr());
340     return getValPtr();
341   }
342 
343   ValueTy *operator->() const { return getValPtr(); }
344   ValueTy &operator*() const { return *getValPtr(); }
345 };
346 
347 // Specialize simplify_type to allow TrackingVH to participate in
348 // dyn_cast, isa, etc.
349 template<typename From> struct simplify_type;
350 template<> struct simplify_type<const TrackingVH<Value> > {
351   typedef Value* SimpleType;
352   static SimpleType getSimplifiedValue(const TrackingVH<Value> &AVH) {
353     return static_cast<Value *>(AVH);
354   }
355 };
356 template<> struct simplify_type<TrackingVH<Value> >
357   : public simplify_type<const TrackingVH<Value> > {};
358 
359 /// CallbackVH - This is a value handle that allows subclasses to define
360 /// callbacks that run when the underlying Value has RAUW called on it or is
361 /// destroyed.  This class can be used as the key of a map, as long as the user
362 /// takes it out of the map before calling setValPtr() (since the map has to
363 /// rearrange itself when the pointer changes).  Unlike ValueHandleBase, this
364 /// class has a vtable and a virtual destructor.
365 class CallbackVH : public ValueHandleBase {
366 protected:
367   CallbackVH(const CallbackVH &RHS)
368     : ValueHandleBase(Callback, RHS) {}
369 
370   virtual ~CallbackVH();
371 
372   void setValPtr(Value *P) {
373     ValueHandleBase::operator=(P);
374   }
375 
376 public:
377   CallbackVH() : ValueHandleBase(Callback) {}
378   CallbackVH(Value *P) : ValueHandleBase(Callback, P) {}
379 
380   operator Value*() const {
381     return getValPtr();
382   }
383 
384   /// Called when this->getValPtr() is destroyed, inside ~Value(), so you may
385   /// call any non-virtual Value method on getValPtr(), but no subclass methods.
386   /// If WeakVH were implemented as a CallbackVH, it would use this method to
387   /// call setValPtr(NULL).  AssertingVH would use this method to cause an
388   /// assertion failure.
389   ///
390   /// All implementations must remove the reference from this object to the
391   /// Value that's being destroyed.
392   virtual void deleted() {
393     setValPtr(NULL);
394   }
395 
396   /// Called when this->getValPtr()->replaceAllUsesWith(new_value) is called,
397   /// _before_ any of the uses have actually been replaced.  If WeakVH were
398   /// implemented as a CallbackVH, it would use this method to call
399   /// setValPtr(new_value).  AssertingVH would do nothing in this method.
400   virtual void allUsesReplacedWith(Value *) {}
401 };
402 
403 // Specialize simplify_type to allow CallbackVH to participate in
404 // dyn_cast, isa, etc.
405 template<typename From> struct simplify_type;
406 template<> struct simplify_type<const CallbackVH> {
407   typedef Value* SimpleType;
408   static SimpleType getSimplifiedValue(const CallbackVH &CVH) {
409     return static_cast<Value *>(CVH);
410   }
411 };
412 template<> struct simplify_type<CallbackVH>
413   : public simplify_type<const CallbackVH> {};
414 
415 } // End llvm namespace
416 
417 #endif
418