Silence a warning about "*/" outside a comment.
[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"
21
22 namespace llvm {
23
24 class Value;
25 class User;
26
27
28 //===----------------------------------------------------------------------===//
29 //                          Generic Tagging Functions
30 //===----------------------------------------------------------------------===//
31
32 /// Tag - generic tag type for (at least 32 bit) pointers
33 enum Tag { noTag, tagOne, tagTwo, tagThree };
34
35 /// addTag - insert tag bits into an (untagged) pointer
36 template <typename T, typename TAG>
37 inline T *addTag(const T *P, TAG Tag) {
38     return reinterpret_cast<T*>(ptrdiff_t(P) | Tag);
39 }
40
41 /// stripTag - remove tag bits from a pointer,
42 /// making it dereferencable
43 template <ptrdiff_t MASK, typename T>
44 inline T *stripTag(const T *P) {
45   return reinterpret_cast<T*>(ptrdiff_t(P) & ~MASK);
46 }
47
48 /// extractTag - extract tag bits from a pointer
49 template <typename TAG, TAG MASK, typename T>
50 inline TAG extractTag(const T *P) {
51   return TAG(ptrdiff_t(P) & MASK);
52 }
53
54 /// transferTag - transfer tag bits from a pointer,
55 /// to an untagged pointer
56 template <ptrdiff_t MASK, typename T>
57 inline T *transferTag(const T *From, const T *To) {
58   return reinterpret_cast<T*>((ptrdiff_t(From) & MASK) | ptrdiff_t(To));
59 }
60
61
62 //===----------------------------------------------------------------------===//
63 //                                  Use Class
64 //===----------------------------------------------------------------------===//
65
66 // Use is here to make keeping the "use" list of a Value up-to-date really easy.
67 //
68 class Use {
69 public:
70   /// init - specify Value and User
71   /// @deprecated in 2.4, will be removed soon
72   inline void init(Value *V, User *U);
73   /// swap - provide a fast substitute to std::swap<Use>
74   /// that also works with less standard-compliant compilers
75   void swap(Use &RHS);
76
77 private:
78   /// Copy ctor - do not implement
79   Use(const Use &U);
80
81   /// Destructor - Only for zap()
82   inline ~Use() {
83     if (Val) removeFromList();
84   }
85
86   /// Default ctor - This leaves the Use completely uninitialized.  The only thing
87   /// that is valid to do with this use is to call the "init" method.
88
89   inline Use() {}
90   enum PrevPtrTag { zeroDigitTag = noTag
91                   , oneDigitTag = tagOne
92                   , stopTag = tagTwo
93                   , fullStopTag = tagThree };
94
95 public:
96
97
98   operator Value*() const { return Val; }
99   Value *get() const { return Val; }
100   User *getUser() const;
101   const Use* getImpliedUser() const;
102   static Use *initTags(Use *Start, Use *Stop, ptrdiff_t Done = 0);
103   static void zap(Use *Start, const Use *Stop, bool del = false);
104
105   inline void set(Value *Val);
106
107   Value *operator=(Value *RHS) {
108     set(RHS);
109     return RHS;
110   }
111   const Use &operator=(const Use &RHS) {
112     set(RHS.Val);
113     return *this;
114   }
115
116         Value *operator->()       { return Val; }
117   const Value *operator->() const { return Val; }
118
119   Use *getNext() const { return Next; }
120 private:
121   Value *Val;
122   Use *Next, **Prev;
123
124   void setPrev(Use **NewPrev) {
125     Prev = transferTag<fullStopTag>(Prev, NewPrev);
126   }
127   void addToList(Use **List) {
128     Next = *List;
129     if (Next) Next->setPrev(&Next);
130     setPrev(List);
131     *List = this;
132   }
133   void removeFromList() {
134     Use **StrippedPrev = stripTag<fullStopTag>(Prev);
135     *StrippedPrev = Next;
136     if (Next) Next->setPrev(StrippedPrev);
137   }
138
139   friend class Value;
140 };
141
142 // simplify_type - Allow clients to treat uses just like values when using
143 // casting operators.
144 template<> struct simplify_type<Use> {
145   typedef Value* SimpleType;
146   static SimpleType getSimplifiedValue(const Use &Val) {
147     return static_cast<SimpleType>(Val.get());
148   }
149 };
150 template<> struct simplify_type<const Use> {
151   typedef Value* SimpleType;
152   static SimpleType getSimplifiedValue(const Use &Val) {
153     return static_cast<SimpleType>(Val.get());
154   }
155 };
156
157
158
159 template<typename UserTy>  // UserTy == 'User' or 'const User'
160 class value_use_iterator : public forward_iterator<UserTy*, ptrdiff_t> {
161   typedef forward_iterator<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 reference 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 template<> struct simplify_type<value_use_iterator<User> > {
212   typedef User* SimpleType;
213   
214   static SimpleType getSimplifiedValue(const value_use_iterator<User> &Val) {
215     return *Val;
216   }
217 };
218
219 template<> struct simplify_type<const value_use_iterator<User> >
220  : public simplify_type<value_use_iterator<User> > {};
221
222 template<> struct simplify_type<value_use_iterator<const User> > {
223   typedef const User* SimpleType;
224   
225   static SimpleType getSimplifiedValue(const 
226                                        value_use_iterator<const User> &Val) {
227     return *Val;
228   }
229 };
230
231 template<> struct simplify_type<const value_use_iterator<const User> >
232   : public simplify_type<value_use_iterator<const User> > {};
233
234 } // End llvm namespace
235
236 #endif