0494b27019cf697612fb522f0634110dae8a8809
[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
22 /// ValueHandleBase - This is the common base class of value handles.
23 /// ValueHandle's are smart pointers to Value's that have special behavior when
24 /// the value is deleted or ReplaceAllUsesWith'd.  See the specific handles
25 /// below for details.
26 ///
27 class ValueHandleBase {
28   friend class Value;
29 protected:
30   /// HandleBaseKind - This indicates what base class the handle actually is.
31   /// This is to avoid having a vtable for the light-weight handle pointers. The
32   /// fully generally Callback version does have a vtable.
33   enum HandleBaseKind {
34     Assert,
35     Weak,
36     Callback
37   };
38 private:
39   
40   PointerIntPair<ValueHandleBase**, 2, HandleBaseKind> PrevPair;
41   ValueHandleBase *Next;
42   Value *VP;
43 public:
44   ValueHandleBase(HandleBaseKind Kind) : PrevPair(0, Kind), Next(0), VP(0) {}
45   ValueHandleBase(HandleBaseKind Kind, Value *V)
46     : PrevPair(0, Kind), Next(0), VP(V) {
47     if (V)
48       AddToUseList();
49   }
50   ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
51     : PrevPair(0, Kind), Next(0), VP(RHS.VP) {
52     if (VP)
53       AddToExistingUseList(RHS.getPrevPtr());
54   }
55   ~ValueHandleBase() {
56     if (VP)
57       RemoveFromUseList();   
58   }
59   
60   Value *operator=(Value *RHS) {
61     if (VP == RHS) return RHS;
62     if (VP) RemoveFromUseList();
63     VP = RHS;
64     if (VP) AddToUseList();
65     return RHS;
66   }
67
68   Value *operator=(const ValueHandleBase &RHS) {
69     if (VP == RHS.VP) return RHS.VP;
70     if (VP) RemoveFromUseList();
71     VP = RHS.VP;
72     if (VP) AddToExistingUseList(RHS.getPrevPtr());
73     return VP;
74   }
75   
76   Value *operator->() const { return getValPtr(); }
77   Value &operator*() const { return *getValPtr(); }
78   
79   bool operator==(const Value *RHS) const { return VP == RHS; }
80   bool operator==(const ValueHandleBase &RHS) const { return VP == RHS.VP; }
81   bool operator!=(const Value *RHS) const { return VP != RHS; }
82   bool operator!=(const ValueHandleBase &RHS) const { return VP != RHS.VP; }
83   bool operator<(const Value *RHS) const { return VP < RHS; }
84   bool operator<(const ValueHandleBase &RHS) const { return VP < RHS.VP; }
85   bool operator>(const Value *RHS) const { return VP > RHS; }
86   bool operator>(const ValueHandleBase &RHS) const { return VP > RHS.VP; }
87   bool operator<=(const Value *RHS) const { return VP <= RHS; }
88   bool operator<=(const ValueHandleBase &RHS) const { return VP <= RHS.VP; }
89   bool operator>=(const Value *RHS) const { return VP >= RHS; }
90   bool operator>=(const ValueHandleBase &RHS) const { return VP >= RHS.VP; }
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 };  
127   
128 /// AssertingVH - This is a Value Handle that points to a value and asserts out
129 /// if the value is destroyed while the handle is still live.  This is very
130 /// useful for catching dangling pointer bugs and other things which can be
131 /// non-obvious.  One particularly useful place to use this is as the Key of a
132 /// map.  Dangling pointer bugs often lead to really subtle bugs that only occur
133 /// if another object happens to get allocated to the same address as the old
134 /// one.  Using an AssertingVH ensures that an assert is triggered as soon as
135 /// the bad delete occurs.
136 ///
137 /// Note that an AssertingVH handle does *not* follow values across RAUW
138 /// operations.  This means that RAUW's need to explicitly update the
139 /// AssertingVH's as it moves.  This is required because in non-assert mode this
140   /// class turns into a trivial wrapper around a pointer.
141 template <typename ValueTy = Value>
142 class AssertingVH 
143 #ifndef NDEBUG
144   : public ValueHandleBase
145 #endif
146   {
147
148 #ifndef NDEBUG
149   ValueTy *getValPtr() const {
150     return static_cast<ValueTy*>(ValueHandleBase::getValPtr());
151   }
152   void setValPtr(ValueTy *P) {
153     ValueHandleBase::operator=(P);
154   }
155 #else
156   ValueTy *ThePtr;
157   ValueTy *getValPtr() const { return ThePtr; }
158   void setValPtr(ValueTy *P) { ThePtr = P; }
159 #endif
160
161 public:
162 #ifndef NDEBUG
163   AssertingVH() : ValueHandleBase(Assert) {}
164   AssertingVH(ValueTy *P) : ValueHandleBase(Assert, P) {}
165   AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
166 #else
167   AssertingVH() : ThePtr(0) {}
168   AssertingVH(ValueTy *P) : ThePtr(P) {}
169 #endif
170
171   operator ValueTy*() const {
172     return getValPtr();
173   }
174
175   ValueTy *operator=(ValueTy *RHS) {
176     setValPtr(RHS);
177     return getValPtr();
178   }
179   ValueTy *operator=(AssertingVH<ValueTy> &RHS) {
180     setValPtr(RHS.getValPtr());
181     return getValPtr();
182   }
183
184   ValueTy *operator->() const { return getValPtr(); }
185   ValueTy &operator*() const { return getValPtr(); }
186
187   // Duplicate these from the base class so that they work when assertions are
188   // off.
189   bool operator==(const Value *RHS) const { return getValPtr() == RHS; }
190   bool operator!=(const Value *RHS) const { return getValPtr() != RHS; }
191   bool operator<(const Value *RHS) const { return getValPtr() < RHS; }
192   bool operator>(const Value *RHS) const { return getValPtr() > RHS; }
193   bool operator<=(const Value *RHS) const { return getValPtr() <= RHS; }
194   bool operator>=(const Value *RHS) const { return getValPtr() >= RHS; }
195   bool operator==(const AssertingVH &RHS) const {
196     return getValPtr() == RHS.getValPtr();
197   }
198   bool operator!=(const AssertingVH &RHS) const {
199     return getValPtr() != RHS.getValPtr();
200   }
201   bool operator<(const AssertingVH &RHS) const {
202     return getValPtr() < RHS.getValPtr();
203   }
204   bool operator>(const AssertingVH &RHS) const {
205     return getValPtr() > RHS.getValPtr();
206   }
207   bool operator<=(const AssertingVH &RHS) const {
208     return getValPtr() <= RHS.getValPtr();
209   }
210   bool operator>=(const AssertingVH &RHS) const {
211     return getValPtr() >= RHS.getValPtr();
212   }
213 };
214
215 } // End llvm namespace
216
217 #endif