merge of use-diet branch to trunk
[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"
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 easy.
67 //
68 class Use {
69 public:
70   inline void init(Value *V, User *U);
71
72 private:
73   /// Allow std::swap some intimacy
74   template <typename U> friend void std::swap(U&, U&);
75
76   /// Copy ctor - Only for std::swap
77   Use(const Use &U) { init(U.get(), 0); }
78
79   /// Destructor - Only for zap() and std::swap
80   inline ~Use() {
81     if (get()) removeFromList();
82   }
83
84   /// Default ctor - This leaves the Use completely uninitialized.  The only thing
85   /// that is valid to do with this use is to call the "init" method.
86
87   inline Use() {}
88   enum PrevPtrTag { zeroDigitTag = noTag
89                   , oneDigitTag = tagOne
90                   , stopTag = tagTwo
91                   , fullStopTag = tagThree };
92
93 public:
94
95
96   operator Value*() const { return Val; }
97   Value *get() const { return Val; }
98   User *getUser() const;
99   const Use* getImpliedUser() const;
100   static Use *initTags(Use *Start, Use *Stop, ptrdiff_t Done = 0);
101   static void zap(Use *Start, const Use *Stop, bool del = false);
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.get());
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 private:
119   Value *Val;
120   Use *Next, **Prev;
121
122   void setPrev(Use **NewPrev) {
123     Prev = transferTag<fullStopTag>(Prev, NewPrev);
124   }
125   void addToList(Use **List) {
126     Next = *List;
127     if (Next) Next->setPrev(&Next);
128     setPrev(List);
129     *List = this;
130   }
131   void removeFromList() {
132     Use **StrippedPrev = stripTag<fullStopTag>(Prev);
133     *StrippedPrev = Next;
134     if (Next) Next->setPrev(StrippedPrev);
135   }
136
137   friend class Value;
138 };
139
140 // simplify_type - Allow clients to treat uses just like values when using
141 // casting operators.
142 template<> struct simplify_type<Use> {
143   typedef Value* SimpleType;
144   static SimpleType getSimplifiedValue(const Use &Val) {
145     return static_cast<SimpleType>(Val.get());
146   }
147 };
148 template<> struct simplify_type<const Use> {
149   typedef Value* SimpleType;
150   static SimpleType getSimplifiedValue(const Use &Val) {
151     return static_cast<SimpleType>(Val.get());
152   }
153 };
154
155
156
157 template<typename UserTy>  // UserTy == 'User' or 'const User'
158 class value_use_iterator : public forward_iterator<UserTy*, ptrdiff_t> {
159   typedef forward_iterator<UserTy*, ptrdiff_t> super;
160   typedef value_use_iterator<UserTy> _Self;
161
162   Use *U;
163   explicit value_use_iterator(Use *u) : U(u) {}
164   friend class Value;
165 public:
166   typedef typename super::reference reference;
167   typedef typename super::pointer pointer;
168
169   value_use_iterator(const _Self &I) : U(I.U) {}
170   value_use_iterator() {}
171
172   bool operator==(const _Self &x) const {
173     return U == x.U;
174   }
175   bool operator!=(const _Self &x) const {
176     return !operator==(x);
177   }
178
179   /// atEnd - return true if this iterator is equal to use_end() on the value.
180   bool atEnd() const { return U == 0; }
181
182   // Iterator traversal: forward iteration only
183   _Self &operator++() {          // Preincrement
184     assert(U && "Cannot increment end iterator!");
185     U = U->getNext();
186     return *this;
187   }
188   _Self operator++(int) {        // Postincrement
189     _Self tmp = *this; ++*this; return tmp;
190   }
191
192   // Retrieve a reference to the current User
193   UserTy *operator*() const {
194     assert(U && "Cannot dereference end iterator!");
195     return U->getUser();
196   }
197
198   UserTy *operator->() const { return operator*(); }
199
200   Use &getUse() const { return *U; }
201   
202   /// getOperandNo - Return the operand # of this use in its User.  Defined in
203   /// User.h
204   ///
205   unsigned getOperandNo() const;
206 };
207
208
209 template<> struct simplify_type<value_use_iterator<User> > {
210   typedef User* SimpleType;
211   
212   static SimpleType getSimplifiedValue(const value_use_iterator<User> &Val) {
213     return *Val;
214   }
215 };
216
217 template<> struct simplify_type<const value_use_iterator<User> >
218  : public simplify_type<value_use_iterator<User> > {};
219
220 template<> struct simplify_type<value_use_iterator<const User> > {
221   typedef const User* SimpleType;
222   
223   static SimpleType getSimplifiedValue(const 
224                                        value_use_iterator<const User> &Val) {
225     return *Val;
226   }
227 };
228
229 template<> struct simplify_type<const value_use_iterator<const User> >
230   : public simplify_type<value_use_iterator<const User> > {};
231
232 } // End llvm namespace
233
234 #endif