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