]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/IR/ValueHandle.h
Import libxo-0.7.2; add xo_options.7.
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / IR / ValueHandle.h
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"
18 #include "llvm/ADT/PointerIntPair.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.
30 class ValueHandleBase {
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.
37   enum HandleBaseKind {
38     Assert,
39     Callback,
40     Tracking,
41     Weak
42   };
43
44   ValueHandleBase(const ValueHandleBase &RHS)
45       : ValueHandleBase(RHS.PrevPair.getInt(), RHS) {}
46
47   ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
48       : PrevPair(nullptr, Kind), Next(nullptr), V(RHS.V) {
49     if (isValid(V))
50       AddToExistingUseList(RHS.getPrevPtr());
51   }
52
53 private:
54   PointerIntPair<ValueHandleBase**, 2, HandleBaseKind> PrevPair;
55   ValueHandleBase *Next;
56
57   Value* V;
58
59 public:
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) {
64     if (isValid(V))
65       AddToUseList();
66   }
67
68   ~ValueHandleBase() {
69     if (isValid(V))
70       RemoveFromUseList();
71   }
72
73   Value *operator=(Value *RHS) {
74     if (V == RHS) return RHS;
75     if (isValid(V)) RemoveFromUseList();
76     V = RHS;
77     if (isValid(V)) AddToUseList();
78     return RHS;
79   }
80
81   Value *operator=(const ValueHandleBase &RHS) {
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 &&
97            V != DenseMapInfo<Value *>::getEmptyKey() &&
98            V != DenseMapInfo<Value *>::getTombstoneKey();
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:
136   WeakVH() : ValueHandleBase(Weak) {}
137   WeakVH(Value *P) : ValueHandleBase(Weak, P) {}
138   WeakVH(const WeakVH &RHS)
139     : ValueHandleBase(Weak, RHS) {}
140
141   WeakVH &operator=(const WeakVH &RHS) = default;
142
143   Value *operator=(Value *RHS) {
144     return ValueHandleBase::operator=(RHS);
145   }
146   Value *operator=(const ValueHandleBase &RHS) {
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>
182 class AssertingVH
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
206   AssertingVH() : ValueHandleBase(Assert) {}
207   AssertingVH(ValueTy *P) : ValueHandleBase(Assert, GetAsValue(P)) {}
208   AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
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>
233 struct DenseMapInfo<AssertingVH<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>
254 struct isPodLike<AssertingVH<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 {
277     Value *VP = ValueHandleBase::getValPtr();
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:
310   TrackingVH() : ValueHandleBase(Tracking) {}
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) {
341     ValueHandleBase::operator=(P);
342   }
343
344 public:
345   CallbackVH() : ValueHandleBase(Callback) {}
346   CallbackVH(Value *P) : ValueHandleBase(Callback, P) {}
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