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