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