Make dataflow iteration possible on Value*, not only on User*: df_ext_iterator<Value...
[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 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 protected:
92   Value *getValPtr() const { return VP; }
93 private:
94   // Callbacks made from Value.
95   static void ValueIsDeleted(Value *V);
96   static void ValueIsRAUWd(Value *Old, Value *New);
97   
98   // Internal implementation details.
99   ValueHandleBase **getPrevPtr() const { return PrevPair.getPointer(); }
100   HandleBaseKind getKind() const { return PrevPair.getInt(); }
101   void setPrevPtr(ValueHandleBase **Ptr) { PrevPair.setPointer(Ptr); }
102   
103   /// AddToUseList - Add this ValueHandle to the use list for VP, where List is
104   /// known to point into the existing use list.
105   void AddToExistingUseList(ValueHandleBase **List);
106   
107   /// AddToUseList - Add this ValueHandle to the use list for VP.
108   void AddToUseList();
109   /// RemoveFromUseList - Remove this ValueHandle from its current use list.
110   void RemoveFromUseList();
111 };
112   
113 /// WeakVH - This is a value handle that tries hard to point to a Value, even
114 /// across RAUW operations, but will null itself out if the value is destroyed.
115 /// this is useful for advisory sorts of information, but should not be used as
116 /// the key of a map (since the map would have to rearrange itself when the
117 /// pointer changes).
118 class WeakVH : public ValueHandleBase {
119 public:
120   WeakVH() : ValueHandleBase(Weak) {}
121   WeakVH(Value *P) : ValueHandleBase(Weak, P) {}
122   WeakVH(const WeakVH &RHS)
123     : ValueHandleBase(Weak, RHS) {}
124
125   operator Value*() const {
126     return getValPtr();
127   }
128 };  
129   
130 /// AssertingVH - This is a Value Handle that points to a value and asserts out
131 /// if the value is destroyed while the handle is still live.  This is very
132 /// useful for catching dangling pointer bugs and other things which can be
133 /// non-obvious.  One particularly useful place to use this is as the Key of a
134 /// map.  Dangling pointer bugs often lead to really subtle bugs that only occur
135 /// if another object happens to get allocated to the same address as the old
136 /// one.  Using an AssertingVH ensures that an assert is triggered as soon as
137 /// the bad delete occurs.
138 ///
139 /// Note that an AssertingVH handle does *not* follow values across RAUW
140 /// operations.  This means that RAUW's need to explicitly update the
141 /// AssertingVH's as it moves.  This is required because in non-assert mode this
142 /// class turns into a trivial wrapper around a pointer.
143 template <typename ValueTy>
144 class AssertingVH 
145 #ifndef NDEBUG
146   : public ValueHandleBase
147 #endif
148   {
149
150 #ifndef NDEBUG
151   ValueTy *getValPtr() const {
152     return static_cast<ValueTy*>(ValueHandleBase::getValPtr());
153   }
154   void setValPtr(ValueTy *P) {
155     ValueHandleBase::operator=(P);
156   }
157 #else
158   ValueTy *ThePtr;
159   ValueTy *getValPtr() const { return ThePtr; }
160   void setValPtr(ValueTy *P) { ThePtr = P; }
161 #endif
162
163 public:
164 #ifndef NDEBUG
165   AssertingVH() : ValueHandleBase(Assert) {}
166   AssertingVH(ValueTy *P) : ValueHandleBase(Assert, P) {}
167   AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
168 #else
169   AssertingVH() : ThePtr(0) {}
170   AssertingVH(ValueTy *P) : ThePtr(P) {}
171 #endif
172
173   operator ValueTy*() const {
174     return getValPtr();
175   }
176
177   ValueTy *operator=(ValueTy *RHS) {
178     setValPtr(RHS);
179     return getValPtr();
180   }
181   ValueTy *operator=(AssertingVH<ValueTy> &RHS) {
182     setValPtr(RHS.getValPtr());
183     return getValPtr();
184   }
185
186   ValueTy *operator->() const { return getValPtr(); }
187   ValueTy &operator*() const { return *getValPtr(); }
188 };
189
190 } // End llvm namespace
191
192 #endif