Added ImmutableMap constructor that accepts a const TreeTy*.
[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
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 private:
70   /// init - specify Value and User
71   /// @deprecated in 2.4, will be removed soon
72   inline void init(Value *V, User *U);
73 public:
74   /// swap - provide a fast substitute to std::swap<Use>
75   /// that also works with less standard-compliant compilers
76   void swap(Use &RHS);
77
78 private:
79   /// Copy ctor - do not implement
80   Use(const Use &U);
81
82   /// Destructor - Only for zap()
83   inline ~Use() {
84     if (Val) removeFromList();
85   }
86
87   /// Default ctor - This leaves the Use completely uninitialized.  The only thing
88   /// that is valid to do with this use is to call the "init" method.
89
90   inline Use() {}
91   enum PrevPtrTag { zeroDigitTag = noTag
92                   , oneDigitTag = tagOne
93                   , stopTag = tagTwo
94                   , fullStopTag = tagThree };
95
96 public:
97   operator Value*() const { return Val; }
98   Value *get() const { return Val; }
99   User *getUser() const;
100   const Use* getImpliedUser() const;
101   static Use *initTags(Use *Start, Use *Stop, ptrdiff_t Done = 0);
102   static void zap(Use *Start, const Use *Stop, bool del = false);
103
104   inline void set(Value *Val);
105
106   Value *operator=(Value *RHS) {
107     set(RHS);
108     return RHS;
109   }
110   const Use &operator=(const Use &RHS) {
111     set(RHS.Val);
112     return *this;
113   }
114
115         Value *operator->()       { return Val; }
116   const Value *operator->() const { return Val; }
117
118   Use *getNext() const { return Next; }
119 private:
120   Value *Val;
121   Use *Next, **Prev;
122
123   void setPrev(Use **NewPrev) {
124     Prev = transferTag<fullStopTag>(Prev, NewPrev);
125   }
126   void addToList(Use **List) {
127     Next = *List;
128     if (Next) Next->setPrev(&Next);
129     setPrev(List);
130     *List = this;
131   }
132   void removeFromList() {
133     Use **StrippedPrev = stripTag<fullStopTag>(Prev);
134     *StrippedPrev = Next;
135     if (Next) Next->setPrev(StrippedPrev);
136   }
137
138   friend class Value;
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 forward_iterator<UserTy*, ptrdiff_t> {
160   typedef forward_iterator<UserTy*, ptrdiff_t> super;
161   typedef value_use_iterator<UserTy> _Self;
162
163   Use *U;
164   explicit value_use_iterator(Use *u) : U(u) {}
165   friend class Value;
166 public:
167   typedef typename super::reference reference;
168   typedef typename super::pointer pointer;
169
170   value_use_iterator(const _Self &I) : U(I.U) {}
171   value_use_iterator() {}
172
173   bool operator==(const _Self &x) const {
174     return U == x.U;
175   }
176   bool operator!=(const _Self &x) const {
177     return !operator==(x);
178   }
179
180   /// atEnd - return true if this iterator is equal to use_end() on the value.
181   bool atEnd() const { return U == 0; }
182
183   // Iterator traversal: forward iteration only
184   _Self &operator++() {          // Preincrement
185     assert(U && "Cannot increment end iterator!");
186     U = U->getNext();
187     return *this;
188   }
189   _Self operator++(int) {        // Postincrement
190     _Self tmp = *this; ++*this; return tmp;
191   }
192
193   // Retrieve a reference to the current User
194   UserTy *operator*() const {
195     assert(U && "Cannot dereference end iterator!");
196     return U->getUser();
197   }
198
199   UserTy *operator->() const { return operator*(); }
200
201   Use &getUse() const { return *U; }
202   
203   /// getOperandNo - Return the operand # of this use in its User.  Defined in
204   /// User.h
205   ///
206   unsigned getOperandNo() const;
207 };
208
209
210 template<> struct simplify_type<value_use_iterator<User> > {
211   typedef User* SimpleType;
212   
213   static SimpleType getSimplifiedValue(const value_use_iterator<User> &Val) {
214     return *Val;
215   }
216 };
217
218 template<> struct simplify_type<const value_use_iterator<User> >
219  : public simplify_type<value_use_iterator<User> > {};
220
221 template<> struct simplify_type<value_use_iterator<const User> > {
222   typedef const User* SimpleType;
223   
224   static SimpleType getSimplifiedValue(const 
225                                        value_use_iterator<const User> &Val) {
226     return *Val;
227   }
228 };
229
230 template<> struct simplify_type<const value_use_iterator<const User> >
231   : public simplify_type<value_use_iterator<const User> > {};
232
233 } // End llvm namespace
234
235 #endif