Strip trailing whitespace.
[oota-llvm.git] / include / llvm / Support / ValueHandle.h
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:
28   static inline void *getAsVoidPointer(ValueHandleBase** P) { return 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     Weak,
49     Callback
50   };
51 private:
52
53   PointerIntPair<ValueHandleBase**, 2, HandleBaseKind> PrevPair;
54   ValueHandleBase *Next;
55   Value *VP;
56 public:
57   explicit ValueHandleBase(HandleBaseKind Kind)
58     : PrevPair(0, Kind), Next(0), VP(0) {}
59   ValueHandleBase(HandleBaseKind Kind, Value *V)
60     : PrevPair(0, Kind), Next(0), VP(V) {
61     if (isValid(VP))
62       AddToUseList();
63   }
64   ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
65     : PrevPair(0, Kind), Next(0), VP(RHS.VP) {
66     if (isValid(VP))
67       AddToExistingUseList(RHS.getPrevPtr());
68   }
69   ~ValueHandleBase() {
70     if (isValid(VP))
71       RemoveFromUseList();
72   }
73
74   Value *operator=(Value *RHS) {
75     if (VP == RHS) return RHS;
76     if (isValid(VP)) RemoveFromUseList();
77     VP = RHS;
78     if (isValid(VP)) AddToUseList();
79     return RHS;
80   }
81
82   Value *operator=(const ValueHandleBase &RHS) {
83     if (VP == RHS.VP) return RHS.VP;
84     if (isValid(VP)) RemoveFromUseList();
85     VP = RHS.VP;
86     if (isValid(VP)) AddToExistingUseList(RHS.getPrevPtr());
87     return VP;
88   }
89
90   Value *operator->() const { return getValPtr(); }
91   Value &operator*() const { return *getValPtr(); }
92
93 protected:
94   Value *getValPtr() const { return VP; }
95 private:
96   static bool isValid(Value *V) {
97     return V &&
98            V != DenseMapInfo<Value *>::getEmptyKey() &&
99            V != DenseMapInfo<Value *>::getTombstoneKey();
100   }
101
102   // Callbacks made from Value.
103   static void ValueIsDeleted(Value *V);
104   static void ValueIsRAUWd(Value *Old, Value *New);
105
106   // Internal implementation details.
107   ValueHandleBase **getPrevPtr() const { return PrevPair.getPointer(); }
108   HandleBaseKind getKind() const { return PrevPair.getInt(); }
109   void setPrevPtr(ValueHandleBase **Ptr) { PrevPair.setPointer(Ptr); }
110
111   /// AddToExistingUseList - Add this ValueHandle to the use list for VP,
112   /// where List is known to point into the existing use list.
113   void AddToExistingUseList(ValueHandleBase **List);
114
115   /// AddToUseList - Add this ValueHandle to the use list for VP.
116   void AddToUseList();
117   /// RemoveFromUseList - Remove this ValueHandle from its current use list.
118   void RemoveFromUseList();
119 };
120
121 /// WeakVH - This is a value handle that tries hard to point to a Value, even
122 /// across RAUW operations, but will null itself out if the value is destroyed.
123 /// this is useful for advisory sorts of information, but should not be used as
124 /// the key of a map (since the map would have to rearrange itself when the
125 /// pointer changes).
126 class WeakVH : public ValueHandleBase {
127 public:
128   WeakVH() : ValueHandleBase(Weak) {}
129   WeakVH(Value *P) : ValueHandleBase(Weak, P) {}
130   WeakVH(const WeakVH &RHS)
131     : ValueHandleBase(Weak, RHS) {}
132
133   operator Value*() const {
134     return getValPtr();
135   }
136 };
137
138 // Specialize simplify_type to allow WeakVH to participate in
139 // dyn_cast, isa, etc.
140 template<typename From> struct simplify_type;
141 template<> struct simplify_type<const WeakVH> {
142   typedef Value* SimpleType;
143   static SimpleType getSimplifiedValue(const WeakVH &WVH) {
144     return static_cast<Value *>(WVH);
145   }
146 };
147 template<> struct simplify_type<WeakVH> : public simplify_type<const WeakVH> {};
148
149 /// AssertingVH - This is a Value Handle that points to a value and asserts out
150 /// if the value is destroyed while the handle is still live.  This is very
151 /// useful for catching dangling pointer bugs and other things which can be
152 /// non-obvious.  One particularly useful place to use this is as the Key of a
153 /// map.  Dangling pointer bugs often lead to really subtle bugs that only occur
154 /// if another object happens to get allocated to the same address as the old
155 /// one.  Using an AssertingVH ensures that an assert is triggered as soon as
156 /// the bad delete occurs.
157 ///
158 /// Note that an AssertingVH handle does *not* follow values across RAUW
159 /// operations.  This means that RAUW's need to explicitly update the
160 /// AssertingVH's as it moves.  This is required because in non-assert mode this
161 /// class turns into a trivial wrapper around a pointer.
162 template <typename ValueTy>
163 class AssertingVH
164 #ifndef NDEBUG
165   : public ValueHandleBase
166 #endif
167   {
168
169 #ifndef NDEBUG
170   ValueTy *getValPtr() const {
171     return static_cast<ValueTy*>(ValueHandleBase::getValPtr());
172   }
173   void setValPtr(ValueTy *P) {
174     ValueHandleBase::operator=(GetAsValue(P));
175   }
176 #else
177   ValueTy *ThePtr;
178   ValueTy *getValPtr() const { return ThePtr; }
179   void setValPtr(ValueTy *P) { ThePtr = P; }
180 #endif
181
182   // Convert a ValueTy*, which may be const, to the type the base
183   // class expects.
184   static Value *GetAsValue(Value *V) { return V; }
185   static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
186
187 public:
188 #ifndef NDEBUG
189   AssertingVH() : ValueHandleBase(Assert) {}
190   AssertingVH(ValueTy *P) : ValueHandleBase(Assert, GetAsValue(P)) {}
191   AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
192 #else
193   AssertingVH() : ThePtr(0) {}
194   AssertingVH(ValueTy *P) : ThePtr(P) {}
195 #endif
196
197   operator ValueTy*() const {
198     return getValPtr();
199   }
200
201   ValueTy *operator=(ValueTy *RHS) {
202     setValPtr(RHS);
203     return getValPtr();
204   }
205   ValueTy *operator=(const AssertingVH<ValueTy> &RHS) {
206     setValPtr(RHS.getValPtr());
207     return getValPtr();
208   }
209
210   ValueTy *operator->() const { return getValPtr(); }
211   ValueTy &operator*() const { return *getValPtr(); }
212 };
213
214 // Specialize simplify_type to allow AssertingVH to participate in
215 // dyn_cast, isa, etc.
216 template<typename From> struct simplify_type;
217 template<> struct simplify_type<const AssertingVH<Value> > {
218   typedef Value* SimpleType;
219   static SimpleType getSimplifiedValue(const AssertingVH<Value> &AVH) {
220     return static_cast<Value *>(AVH);
221   }
222 };
223 template<> struct simplify_type<AssertingVH<Value> >
224   : public simplify_type<const AssertingVH<Value> > {};
225
226 /// CallbackVH - This is a value handle that allows subclasses to define
227 /// callbacks that run when the underlying Value has RAUW called on it or is
228 /// destroyed.  This class can be used as the key of a map, as long as the user
229 /// takes it out of the map before calling setValPtr() (since the map has to
230 /// rearrange itself when the pointer changes).  Unlike ValueHandleBase, this
231 /// class has a vtable and a virtual destructor.
232 class CallbackVH : public ValueHandleBase {
233 protected:
234   CallbackVH(const CallbackVH &RHS)
235     : ValueHandleBase(Callback, RHS) {}
236
237   virtual ~CallbackVH();
238
239   void setValPtr(Value *P) {
240     ValueHandleBase::operator=(P);
241   }
242
243 public:
244   CallbackVH() : ValueHandleBase(Callback) {}
245   CallbackVH(Value *P) : ValueHandleBase(Callback, P) {}
246
247   operator Value*() const {
248     return getValPtr();
249   }
250
251   /// Called when this->getValPtr() is destroyed, inside ~Value(), so you may
252   /// call any non-virtual Value method on getValPtr(), but no subclass methods.
253   /// If WeakVH were implemented as a CallbackVH, it would use this method to
254   /// call setValPtr(NULL).  AssertingVH would use this method to cause an
255   /// assertion failure.
256   ///
257   /// All implementations must remove the reference from this object to the
258   /// Value that's being destroyed.
259   virtual void deleted() {
260     setValPtr(NULL);
261   }
262
263   /// Called when this->getValPtr()->replaceAllUsesWith(new_value) is called,
264   /// _before_ any of the uses have actually been replaced.  If WeakVH were
265   /// implemented as a CallbackVH, it would use this method to call
266   /// setValPtr(new_value).  AssertingVH would do nothing in this method.
267   virtual void allUsesReplacedWith(Value *new_value) {}
268 };
269
270 // Specialize simplify_type to allow CallbackVH to participate in
271 // dyn_cast, isa, etc.
272 template<typename From> struct simplify_type;
273 template<> struct simplify_type<const CallbackVH> {
274   typedef Value* SimpleType;
275   static SimpleType getSimplifiedValue(const CallbackVH &CVH) {
276     return static_cast<Value *>(CVH);
277   }
278 };
279 template<> struct simplify_type<CallbackVH>
280   : public simplify_type<const CallbackVH> {};
281
282 } // End llvm namespace
283
284 #endif