Constant folding support for calls to umul.with.overflow(), basically identical to...
[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 // Pointer tagging is used to efficiently find the User corresponding
15 // to a Use without having to store a User pointer in every Use. A
16 // User is preceded in memory by all the Uses corresponding to its
17 // operands, and the low bits of one of the fields (Prev) of the Use
18 // class are used to encode offsets to be able to find that User given
19 // a pointer to any Use. For details, see:
20 //
21 //   http://www.llvm.org/docs/ProgrammersManual.html#UserLayout
22 //
23 //===----------------------------------------------------------------------===//
24
25 #ifndef LLVM_USE_H
26 #define LLVM_USE_H
27
28 #include "llvm/ADT/PointerIntPair.h"
29 #include <cstddef>
30 #include <iterator>
31
32 namespace llvm {
33
34 class Value;
35 class User;
36 class Use;
37 template<typename>
38 struct simplify_type;
39
40 // Use** is only 4-byte aligned.
41 template<>
42 class PointerLikeTypeTraits<Use**> {
43 public:
44   static inline void *getAsVoidPointer(Use** P) { return P; }
45   static inline Use **getFromVoidPointer(void *P) {
46     return static_cast<Use**>(P);
47   }
48   enum { NumLowBitsAvailable = 2 };
49 };
50
51 //===----------------------------------------------------------------------===//
52 //                                  Use Class
53 //===----------------------------------------------------------------------===//
54
55 /// Use is here to make keeping the "use" list of a Value up-to-date really
56 /// easy.
57 class Use {
58 public:
59   /// swap - provide a fast substitute to std::swap<Use>
60   /// that also works with less standard-compliant compilers
61   void swap(Use &RHS);
62
63 private:
64   /// Copy ctor - do not implement
65   Use(const Use &U);
66
67   /// Destructor - Only for zap()
68   ~Use() {
69     if (Val) removeFromList();
70   }
71
72   enum PrevPtrTag { zeroDigitTag
73                   , oneDigitTag
74                   , stopTag
75                   , fullStopTag };
76
77   /// Constructor
78   Use(PrevPtrTag tag) : Val(0) {
79     Prev.setInt(tag);
80   }
81
82 public:
83   /// Normally Use will just implicitly convert to a Value* that it holds.
84   operator Value*() const { return Val; }
85   
86   /// If implicit conversion to Value* doesn't work, the get() method returns
87   /// the Value*.
88   Value *get() const { return Val; }
89   
90   /// getUser - This returns the User that contains this Use.  For an
91   /// instruction operand, for example, this will return the instruction.
92   User *getUser() const;
93
94   inline void set(Value *Val);
95
96   Value *operator=(Value *RHS) {
97     set(RHS);
98     return RHS;
99   }
100   const Use &operator=(const Use &RHS) {
101     set(RHS.Val);
102     return *this;
103   }
104
105         Value *operator->()       { return Val; }
106   const Value *operator->() const { return Val; }
107
108   Use *getNext() const { return Next; }
109
110   
111   /// zap - This is used to destroy Use operands when the number of operands of
112   /// a User changes.
113   static void zap(Use *Start, const Use *Stop, bool del = false);
114
115 private:
116   const Use* getImpliedUser() const;
117   static Use *initTags(Use *Start, Use *Stop);
118   
119   Value *Val;
120   Use *Next;
121   PointerIntPair<Use**, 2, PrevPtrTag> Prev;
122
123   void setPrev(Use **NewPrev) {
124     Prev.setPointer(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 = Prev.getPointer();
134     *StrippedPrev = Next;
135     if (Next) Next->setPrev(StrippedPrev);
136   }
137
138   friend class Value;
139   friend class User;
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 std::iterator<std::forward_iterator_tag,
161                                                 UserTy*, ptrdiff_t> {
162   typedef std::iterator<std::forward_iterator_tag, UserTy*, ptrdiff_t> super;
163   typedef value_use_iterator<UserTy> _Self;
164
165   Use *U;
166   explicit value_use_iterator(Use *u) : U(u) {}
167   friend class Value;
168 public:
169   typedef typename super::reference reference;
170   typedef typename super::pointer pointer;
171
172   value_use_iterator(const _Self &I) : U(I.U) {}
173   value_use_iterator() {}
174
175   bool operator==(const _Self &x) const {
176     return U == x.U;
177   }
178   bool operator!=(const _Self &x) const {
179     return !operator==(x);
180   }
181
182   /// atEnd - return true if this iterator is equal to use_end() on the value.
183   bool atEnd() const { return U == 0; }
184
185   // Iterator traversal: forward iteration only
186   _Self &operator++() {          // Preincrement
187     assert(U && "Cannot increment end iterator!");
188     U = U->getNext();
189     return *this;
190   }
191   _Self operator++(int) {        // Postincrement
192     _Self tmp = *this; ++*this; return tmp;
193   }
194
195   // Retrieve a pointer to the current User.
196   UserTy *operator*() const {
197     assert(U && "Cannot dereference end iterator!");
198     return U->getUser();
199   }
200
201   UserTy *operator->() const { return operator*(); }
202
203   Use &getUse() const { return *U; }
204   
205   /// getOperandNo - Return the operand # of this use in its User.  Defined in
206   /// User.h
207   ///
208   unsigned getOperandNo() const;
209 };
210
211 //===----------------------------------------------------------------------===//
212 //                         AugmentedUse layout struct
213 //===----------------------------------------------------------------------===//
214
215 struct AugmentedUse : public Use {
216   PointerIntPair<User*, 1, unsigned> ref;
217   AugmentedUse(); // not implemented
218 };
219
220 } // End llvm namespace
221
222 #endif