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