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