1 //===-- llvm/Use.h - Definition of the Use class ----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
19 #include "llvm/Support/Casting.h"
20 #include "llvm/ADT/iterator.h"
21 #include "llvm/ADT/PointerIntPair.h"
29 /// Tag - generic tag type for (at least 32 bit) pointers
30 enum Tag { noTag, tagOne, tagTwo, tagThree };
32 // Use** is only 4-byte aligned.
34 class PointerLikeTypeTraits<Use**> {
36 static inline void *getAsVoidPointer(Use** P) { return P; }
37 static inline Use **getFromVoidPointer(void *P) {
38 return static_cast<Use**>(P);
40 enum { NumLowBitsAvailable = 2 };
43 //===----------------------------------------------------------------------===//
45 //===----------------------------------------------------------------------===//
47 /// Use is here to make keeping the "use" list of a Value up-to-date really
51 /// swap - provide a fast substitute to std::swap<Use>
52 /// that also works with less standard-compliant compilers
56 /// Copy ctor - do not implement
59 /// Destructor - Only for zap()
61 if (Val) removeFromList();
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.
67 enum PrevPtrTag { zeroDigitTag = noTag
68 , oneDigitTag = tagOne
70 , fullStopTag = tagThree };
73 /// Normally Use will just implicitly convert to a Value* that it holds.
74 operator Value*() const { return Val; }
76 /// If implicit conversion to Value* doesn't work, the get() method returns
78 Value *get() const { return Val; }
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;
84 inline void set(Value *Val);
86 Value *operator=(Value *RHS) {
90 const Use &operator=(const Use &RHS) {
95 Value *operator->() { return Val; }
96 const Value *operator->() const { return Val; }
98 Use *getNext() const { return Next; }
101 /// zap - This is used to destroy Use operands when the number of operands of
103 static void zap(Use *Start, const Use *Stop, bool del = false);
105 /// getPrefix - Return deletable pointer if appropriate
108 const Use* getImpliedUser() const;
109 static Use *initTags(Use *Start, Use *Stop, ptrdiff_t Done = 0);
113 PointerIntPair<Use**, 2, PrevPtrTag> Prev;
115 void setPrev(Use **NewPrev) {
116 Prev.setPointer(NewPrev);
118 void addToList(Use **List) {
120 if (Next) Next->setPrev(&Next);
124 void removeFromList() {
125 Use **StrippedPrev = Prev.getPointer();
126 *StrippedPrev = Next;
127 if (Next) Next->setPrev(StrippedPrev);
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());
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());
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;
157 explicit value_use_iterator(Use *u) : U(u) {}
160 typedef typename super::reference reference;
161 typedef typename super::pointer pointer;
163 value_use_iterator(const _Self &I) : U(I.U) {}
164 value_use_iterator() {}
166 bool operator==(const _Self &x) const {
169 bool operator!=(const _Self &x) const {
170 return !operator==(x);
173 /// atEnd - return true if this iterator is equal to use_end() on the value.
174 bool atEnd() const { return U == 0; }
176 // Iterator traversal: forward iteration only
177 _Self &operator++() { // Preincrement
178 assert(U && "Cannot increment end iterator!");
182 _Self operator++(int) { // Postincrement
183 _Self tmp = *this; ++*this; return tmp;
186 // Retrieve a pointer to the current User.
187 UserTy *operator*() const {
188 assert(U && "Cannot dereference end iterator!");
192 UserTy *operator->() const { return operator*(); }
194 Use &getUse() const { return *U; }
196 /// getOperandNo - Return the operand # of this use in its User. Defined in
199 unsigned getOperandNo() const;
203 template<> struct simplify_type<value_use_iterator<User> > {
204 typedef User* SimpleType;
206 static SimpleType getSimplifiedValue(const value_use_iterator<User> &Val) {
211 template<> struct simplify_type<const value_use_iterator<User> >
212 : public simplify_type<value_use_iterator<User> > {};
214 template<> struct simplify_type<value_use_iterator<const User> > {
215 typedef const User* SimpleType;
217 static SimpleType getSimplifiedValue(const
218 value_use_iterator<const User> &Val) {
223 template<> struct simplify_type<const value_use_iterator<const User> >
224 : public simplify_type<value_use_iterator<const User> > {};
226 } // End llvm namespace