c4b8851d9351e8bf35511518339f09c3632a0ce7
[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/ADT/PointerIntPair.h"
29 #include <iterator>
30
31 namespace llvm {
32
33 class Value;
34 class User;
35 class Use;
36 template<typename>
37 struct simplify_type;
38
39 // Use** is only 4-byte aligned.
40 template<>
41 class PointerLikeTypeTraits<Use**> {
42 public:
43   static inline void *getAsVoidPointer(Use** P) { return P; }
44   static inline Use **getFromVoidPointer(void *P) {
45     return static_cast<Use**>(P);
46   }
47   enum { NumLowBitsAvailable = 2 };
48 };
49
50 //===----------------------------------------------------------------------===//
51 //                                  Use Class
52 //===----------------------------------------------------------------------===//
53
54 /// Use is here to make keeping the "use" list of a Value up-to-date really
55 /// easy.
56 class Use {
57 public:
58   /// swap - provide a fast substitute to std::swap<Use>
59   /// that also works with less standard-compliant compilers
60   void swap(Use &RHS);
61
62 private:
63   /// Copy ctor - do not implement
64   Use(const Use &U);
65
66   /// Destructor - Only for zap()
67   ~Use() {
68     if (Val) removeFromList();
69   }
70
71   enum PrevPtrTag { zeroDigitTag
72                   , oneDigitTag
73                   , stopTag
74                   , fullStopTag };
75
76   /// Constructor
77   Use(PrevPtrTag tag) : Val(0) {
78     Prev.setInt(tag);
79   }
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 private:
115   const Use* getImpliedUser() const;
116   static Use *initTags(Use *Start, Use *Stop);
117   
118   Value *Val;
119   Use *Next;
120   PointerIntPair<Use**, 2, PrevPtrTag> Prev;
121
122   void setPrev(Use **NewPrev) {
123     Prev.setPointer(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 = Prev.getPointer();
133     *StrippedPrev = Next;
134     if (Next) Next->setPrev(StrippedPrev);
135   }
136
137   friend class Value;
138   friend class User;
139 };
140
141 // simplify_type - Allow clients to treat uses just like values when using
142 // casting operators.
143 template<> struct simplify_type<Use> {
144   typedef Value* SimpleType;
145   static SimpleType getSimplifiedValue(const Use &Val) {
146     return static_cast<SimpleType>(Val.get());
147   }
148 };
149 template<> struct simplify_type<const Use> {
150   typedef Value* SimpleType;
151   static SimpleType getSimplifiedValue(const Use &Val) {
152     return static_cast<SimpleType>(Val.get());
153   }
154 };
155
156
157
158 template<typename UserTy>  // UserTy == 'User' or 'const User'
159 class value_use_iterator : public std::iterator<std::forward_iterator_tag,
160                                                 UserTy*, ptrdiff_t> {
161   typedef std::iterator<std::forward_iterator_tag, UserTy*, ptrdiff_t> super;
162   typedef value_use_iterator<UserTy> _Self;
163
164   Use *U;
165   explicit value_use_iterator(Use *u) : U(u) {}
166   friend class Value;
167 public:
168   typedef typename super::reference reference;
169   typedef typename super::pointer pointer;
170
171   value_use_iterator(const _Self &I) : U(I.U) {}
172   value_use_iterator() {}
173
174   bool operator==(const _Self &x) const {
175     return U == x.U;
176   }
177   bool operator!=(const _Self &x) const {
178     return !operator==(x);
179   }
180
181   /// atEnd - return true if this iterator is equal to use_end() on the value.
182   bool atEnd() const { return U == 0; }
183
184   // Iterator traversal: forward iteration only
185   _Self &operator++() {          // Preincrement
186     assert(U && "Cannot increment end iterator!");
187     U = U->getNext();
188     return *this;
189   }
190   _Self operator++(int) {        // Postincrement
191     _Self tmp = *this; ++*this; return tmp;
192   }
193
194   // Retrieve a pointer to the current User.
195   UserTy *operator*() const {
196     assert(U && "Cannot dereference end iterator!");
197     return U->getUser();
198   }
199
200   UserTy *operator->() const { return operator*(); }
201
202   Use &getUse() const { return *U; }
203   
204   /// getOperandNo - Return the operand # of this use in its User.  Defined in
205   /// User.h
206   ///
207   unsigned getOperandNo() const;
208 };
209
210 //===----------------------------------------------------------------------===//
211 //                         AugmentedUse layout struct
212 //===----------------------------------------------------------------------===//
213
214 struct AugmentedUse : public Use {
215   PointerIntPair<User*, 1, unsigned> ref;
216   AugmentedUse(); // not implemented
217 };
218
219 } // End llvm namespace
220
221 #endif