d9667032652d14a2d461db41230878a4fdcdf451
[oota-llvm.git] / include / llvm / Use.h
1 //===-- llvm/Use.h - Definition of the Use class ----------------*- 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 defines the Use class.  The Use class represents the operand of an
11 // instruction or some other User instance which refers to a Value.  The Use
12 // class keeps the "use list" of the referenced value up to date.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_USE_H
17 #define LLVM_USE_H
18
19 #include "llvm/Support/Casting.h"
20 #include "llvm/ADT/iterator.h"
21
22 namespace llvm {
23
24 class Value;
25 class User;
26
27
28 //===----------------------------------------------------------------------===//
29 //                          Generic Tagging Functions
30 //===----------------------------------------------------------------------===//
31
32 /// Tag - generic tag type for (at least 32 bit) pointers
33 enum Tag { noTag, tagOne, tagTwo, tagThree };
34
35 /// addTag - insert tag bits into an (untagged) pointer
36 template <typename T, typename TAG>
37 inline T *addTag(const T *P, TAG Tag) {
38     return reinterpret_cast<T*>(ptrdiff_t(P) | Tag);
39 }
40
41 /// stripTag - remove tag bits from a pointer,
42 /// making it dereferencable
43 template <ptrdiff_t MASK, typename T>
44 inline T *stripTag(const T *P) {
45   return reinterpret_cast<T*>(ptrdiff_t(P) & ~MASK);
46 }
47
48 /// extractTag - extract tag bits from a pointer
49 template <typename TAG, TAG MASK, typename T>
50 inline TAG extractTag(const T *P) {
51   return TAG(ptrdiff_t(P) & MASK);
52 }
53
54 /// transferTag - transfer tag bits from a pointer,
55 /// to an untagged pointer
56 template <ptrdiff_t MASK, typename T>
57 inline T *transferTag(const T *From, const T *To) {
58   return reinterpret_cast<T*>((ptrdiff_t(From) & MASK) | ptrdiff_t(To));
59 }
60
61
62 //===----------------------------------------------------------------------===//
63 //                                  Use Class
64 //===----------------------------------------------------------------------===//
65
66 /// Use is here to make keeping the "use" list of a Value up-to-date really
67 /// easy.
68 class Use {
69 public:
70   /// swap - provide a fast substitute to std::swap<Use>
71   /// that also works with less standard-compliant compilers
72   void swap(Use &RHS);
73
74 private:
75   /// Copy ctor - do not implement
76   Use(const Use &U);
77
78   /// Destructor - Only for zap()
79   inline ~Use() {
80     if (Val) removeFromList();
81   }
82
83   /// Default ctor - This leaves the Use completely uninitialized.  The only
84   /// thing that is valid to do with this use is to call the "init" method.
85   inline Use() {}
86   enum PrevPtrTag { zeroDigitTag = noTag
87                   , oneDigitTag = tagOne
88                   , stopTag = tagTwo
89                   , fullStopTag = tagThree };
90
91 public:
92   /// Normally Use will just implicitly convert to a Value* that it holds.
93   operator Value*() const { return Val; }
94   
95   /// If implicit conversion to Value* doesn't work, the get() method returns
96   /// the Value*.
97   Value *get() const { return Val; }
98   
99   /// getUser - This returns the User that contains this Use.  For an
100   /// instruction operand, for example, this will return the instruction.
101   User *getUser() const;
102
103   inline void set(Value *Val);
104
105   Value *operator=(Value *RHS) {
106     set(RHS);
107     return RHS;
108   }
109   const Use &operator=(const Use &RHS) {
110     set(RHS.Val);
111     return *this;
112   }
113
114         Value *operator->()       { return Val; }
115   const Value *operator->() const { return Val; }
116
117   Use *getNext() const { return Next; }
118
119   
120   /// zap - This is used to destroy Use operands when the number of operands of
121   /// a User changes.
122   static void zap(Use *Start, const Use *Stop, bool del = false);
123
124 private:
125   const Use* getImpliedUser() const;
126   static Use *initTags(Use *Start, Use *Stop, ptrdiff_t Done = 0);
127   
128   Value *Val;
129   Use *Next, **Prev;
130
131   void setPrev(Use **NewPrev) {
132     Prev = transferTag<fullStopTag>(Prev, NewPrev);
133   }
134   void addToList(Use **List) {
135     Next = *List;
136     if (Next) Next->setPrev(&Next);
137     setPrev(List);
138     *List = this;
139   }
140   void removeFromList() {
141     Use **StrippedPrev = stripTag<fullStopTag>(Prev);
142     *StrippedPrev = Next;
143     if (Next) Next->setPrev(StrippedPrev);
144   }
145
146   friend class Value;
147   friend class User;
148 };
149
150 // simplify_type - Allow clients to treat uses just like values when using
151 // casting operators.
152 template<> struct simplify_type<Use> {
153   typedef Value* SimpleType;
154   static SimpleType getSimplifiedValue(const Use &Val) {
155     return static_cast<SimpleType>(Val.get());
156   }
157 };
158 template<> struct simplify_type<const Use> {
159   typedef Value* SimpleType;
160   static SimpleType getSimplifiedValue(const Use &Val) {
161     return static_cast<SimpleType>(Val.get());
162   }
163 };
164
165
166
167 template<typename UserTy>  // UserTy == 'User' or 'const User'
168 class value_use_iterator : public forward_iterator<UserTy*, ptrdiff_t> {
169   typedef forward_iterator<UserTy*, ptrdiff_t> super;
170   typedef value_use_iterator<UserTy> _Self;
171
172   Use *U;
173   explicit value_use_iterator(Use *u) : U(u) {}
174   friend class Value;
175 public:
176   typedef typename super::reference reference;
177   typedef typename super::pointer pointer;
178
179   value_use_iterator(const _Self &I) : U(I.U) {}
180   value_use_iterator() {}
181
182   bool operator==(const _Self &x) const {
183     return U == x.U;
184   }
185   bool operator!=(const _Self &x) const {
186     return !operator==(x);
187   }
188
189   /// atEnd - return true if this iterator is equal to use_end() on the value.
190   bool atEnd() const { return U == 0; }
191
192   // Iterator traversal: forward iteration only
193   _Self &operator++() {          // Preincrement
194     assert(U && "Cannot increment end iterator!");
195     U = U->getNext();
196     return *this;
197   }
198   _Self operator++(int) {        // Postincrement
199     _Self tmp = *this; ++*this; return tmp;
200   }
201
202   // Retrieve a pointer to the current User.
203   UserTy *operator*() const {
204     assert(U && "Cannot dereference end iterator!");
205     return U->getUser();
206   }
207
208   UserTy *operator->() const { return operator*(); }
209
210   Use &getUse() const { return *U; }
211   
212   /// getOperandNo - Return the operand # of this use in its User.  Defined in
213   /// User.h
214   ///
215   unsigned getOperandNo() const;
216 };
217
218
219 template<> struct simplify_type<value_use_iterator<User> > {
220   typedef User* SimpleType;
221   
222   static SimpleType getSimplifiedValue(const value_use_iterator<User> &Val) {
223     return *Val;
224   }
225 };
226
227 template<> struct simplify_type<const value_use_iterator<User> >
228  : public simplify_type<value_use_iterator<User> > {};
229
230 template<> struct simplify_type<value_use_iterator<const User> > {
231   typedef const User* SimpleType;
232   
233   static SimpleType getSimplifiedValue(const 
234                                        value_use_iterator<const User> &Val) {
235     return *Val;
236   }
237 };
238
239 template<> struct simplify_type<const value_use_iterator<const User> >
240   : public simplify_type<value_use_iterator<const User> > {};
241
242 } // End llvm namespace
243
244 #endif