a668656a07e485d5900b733de27d88c6fb188cf6
[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 base class the handle actually is.
43   /// This is to avoid having a vtable for the light-weight handle pointers. The
44   /// fully generally 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   ValueHandleBase(HandleBaseKind Kind) : PrevPair(0, Kind), Next(0), VP(0) {}
57   ValueHandleBase(HandleBaseKind Kind, Value *V)
58     : PrevPair(0, Kind), Next(0), VP(V) {
59     if (V)
60       AddToUseList();
61   }
62   ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
63     : PrevPair(0, Kind), Next(0), VP(RHS.VP) {
64     if (VP)
65       AddToExistingUseList(RHS.getPrevPtr());
66   }
67   ~ValueHandleBase() {
68     if (VP)
69       RemoveFromUseList();   
70   }
71   
72   Value *operator=(Value *RHS) {
73     if (VP == RHS) return RHS;
74     if (VP) RemoveFromUseList();
75     VP = RHS;
76     if (VP) AddToUseList();
77     return RHS;
78   }
79
80   Value *operator=(const ValueHandleBase &RHS) {
81     if (VP == RHS.VP) return RHS.VP;
82     if (VP) RemoveFromUseList();
83     VP = RHS.VP;
84     if (VP) AddToExistingUseList(RHS.getPrevPtr());
85     return VP;
86   }
87   
88   Value *operator->() const { return getValPtr(); }
89   Value &operator*() const { return *getValPtr(); }
90   
91   bool operator==(const Value *RHS) const { return VP == RHS; }
92   bool operator==(const ValueHandleBase &RHS) const { return VP == RHS.VP; }
93   bool operator!=(const Value *RHS) const { return VP != RHS; }
94   bool operator!=(const ValueHandleBase &RHS) const { return VP != RHS.VP; }
95   bool operator<(const Value *RHS) const { return VP < RHS; }
96   bool operator<(const ValueHandleBase &RHS) const { return VP < RHS.VP; }
97   bool operator>(const Value *RHS) const { return VP > RHS; }
98   bool operator>(const ValueHandleBase &RHS) const { return VP > RHS.VP; }
99   bool operator<=(const Value *RHS) const { return VP <= RHS; }
100   bool operator<=(const ValueHandleBase &RHS) const { return VP <= RHS.VP; }
101   bool operator>=(const Value *RHS) const { return VP >= RHS; }
102   bool operator>=(const ValueHandleBase &RHS) const { return VP >= RHS.VP; }
103   
104 protected:
105   Value *getValPtr() const { return VP; }
106 private:
107   // Callbacks made from Value.
108   static void ValueIsDeleted(Value *V);
109   static void ValueIsRAUWd(Value *Old, Value *New);
110   
111   // Internal implementation details.
112   ValueHandleBase **getPrevPtr() const { return PrevPair.getPointer(); }
113   HandleBaseKind getKind() const { return PrevPair.getInt(); }
114   void setPrevPtr(ValueHandleBase **Ptr) { PrevPair.setPointer(Ptr); }
115   
116   /// AddToUseList - Add this ValueHandle to the use list for VP, where List is
117   /// known to point into the existing use list.
118   void AddToExistingUseList(ValueHandleBase **List);
119   
120   /// AddToUseList - Add this ValueHandle to the use list for VP.
121   void AddToUseList();
122   /// RemoveFromUseList - Remove this ValueHandle from its current use list.
123   void RemoveFromUseList();
124 };
125   
126 /// WeakVH - This is a value handle that tries hard to point to a Value, even
127 /// across RAUW operations, but will null itself out if the value is destroyed.
128 /// this is useful for advisory sorts of information, but should not be used as
129 /// the key of a map (since the map would have to rearrange itself when the
130 /// pointer changes).
131 class WeakVH : public ValueHandleBase {
132 public:
133   WeakVH() : ValueHandleBase(Weak) {}
134   WeakVH(Value *P) : ValueHandleBase(Weak, P) {}
135   WeakVH(const WeakVH &RHS)
136     : ValueHandleBase(Weak, RHS) {}
137
138   operator Value*() const {
139     return getValPtr();
140   }
141 };  
142   
143 /// AssertingVH - This is a Value Handle that points to a value and asserts out
144 /// if the value is destroyed while the handle is still live.  This is very
145 /// useful for catching dangling pointer bugs and other things which can be
146 /// non-obvious.  One particularly useful place to use this is as the Key of a
147 /// map.  Dangling pointer bugs often lead to really subtle bugs that only occur
148 /// if another object happens to get allocated to the same address as the old
149 /// one.  Using an AssertingVH ensures that an assert is triggered as soon as
150 /// the bad delete occurs.
151 ///
152 /// Note that an AssertingVH handle does *not* follow values across RAUW
153 /// operations.  This means that RAUW's need to explicitly update the
154 /// AssertingVH's as it moves.  This is required because in non-assert mode this
155   /// class turns into a trivial wrapper around a pointer.
156 template <typename ValueTy>
157 class AssertingVH 
158 #ifndef NDEBUG
159   : public ValueHandleBase
160 #endif
161   {
162
163 #ifndef NDEBUG
164   ValueTy *getValPtr() const {
165     return static_cast<ValueTy*>(ValueHandleBase::getValPtr());
166   }
167   void setValPtr(ValueTy *P) {
168     ValueHandleBase::operator=(P);
169   }
170 #else
171   ValueTy *ThePtr;
172   ValueTy *getValPtr() const { return ThePtr; }
173   void setValPtr(ValueTy *P) { ThePtr = P; }
174 #endif
175
176 public:
177 #ifndef NDEBUG
178   AssertingVH() : ValueHandleBase(Assert) {}
179   AssertingVH(ValueTy *P) : ValueHandleBase(Assert, P) {}
180   AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
181 #else
182   AssertingVH() : ThePtr(0) {}
183   AssertingVH(ValueTy *P) : ThePtr(P) {}
184 #endif
185
186   operator ValueTy*() const {
187     return getValPtr();
188   }
189
190   ValueTy *operator=(ValueTy *RHS) {
191     setValPtr(RHS);
192     return getValPtr();
193   }
194   ValueTy *operator=(AssertingVH<ValueTy> &RHS) {
195     setValPtr(RHS.getValPtr());
196     return getValPtr();
197   }
198
199   ValueTy *operator->() const { return getValPtr(); }
200   ValueTy &operator*() const { return *getValPtr(); }
201
202   // Duplicate these from the base class so that they work when assertions are
203   // off.
204   bool operator==(const Value *RHS) const { return getValPtr() == RHS; }
205   bool operator!=(const Value *RHS) const { return getValPtr() != RHS; }
206   bool operator<(const Value *RHS) const { return getValPtr() < RHS; }
207   bool operator>(const Value *RHS) const { return getValPtr() > RHS; }
208   bool operator<=(const Value *RHS) const { return getValPtr() <= RHS; }
209   bool operator>=(const Value *RHS) const { return getValPtr() >= RHS; }
210   bool operator==(const AssertingVH &RHS) const {
211     return getValPtr() == RHS.getValPtr();
212   }
213   bool operator!=(const AssertingVH &RHS) const {
214     return getValPtr() != RHS.getValPtr();
215   }
216   bool operator<(const AssertingVH &RHS) const {
217     return getValPtr() < RHS.getValPtr();
218   }
219   bool operator>(const AssertingVH &RHS) const {
220     return getValPtr() > RHS.getValPtr();
221   }
222   bool operator<=(const AssertingVH &RHS) const {
223     return getValPtr() <= RHS.getValPtr();
224   }
225   bool operator>=(const AssertingVH &RHS) const {
226     return getValPtr() >= RHS.getValPtr();
227   }
228 };
229
230 } // End llvm namespace
231
232 #endif