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