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