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 };
33 //===----------------------------------------------------------------------===//
35 //===----------------------------------------------------------------------===//
37 /// Use is here to make keeping the "use" list of a Value up-to-date really
41 /// swap - provide a fast substitute to std::swap<Use>
42 /// that also works with less standard-compliant compilers
46 /// Copy ctor - do not implement
49 /// Destructor - Only for zap()
51 if (Val) removeFromList();
54 /// Default ctor - This leaves the Use completely uninitialized. The only
55 /// thing that is valid to do with this use is to call the "init" method.
57 enum PrevPtrTag { zeroDigitTag = noTag
58 , oneDigitTag = tagOne
60 , fullStopTag = tagThree };
63 /// Normally Use will just implicitly convert to a Value* that it holds.
64 operator Value*() const { return Val; }
66 /// If implicit conversion to Value* doesn't work, the get() method returns
68 Value *get() const { return Val; }
70 /// getUser - This returns the User that contains this Use. For an
71 /// instruction operand, for example, this will return the instruction.
72 User *getUser() const;
74 inline void set(Value *Val);
76 Value *operator=(Value *RHS) {
80 const Use &operator=(const Use &RHS) {
85 Value *operator->() { return Val; }
86 const Value *operator->() const { return Val; }
88 Use *getNext() const { return Next; }
91 /// zap - This is used to destroy Use operands when the number of operands of
93 static void zap(Use *Start, const Use *Stop, bool del = false);
96 const Use* getImpliedUser() const;
97 static Use *initTags(Use *Start, Use *Stop, ptrdiff_t Done = 0);
101 PointerIntPair<Use**, 2, PrevPtrTag> Prev;
103 void setPrev(Use **NewPrev) {
104 Prev.setPointer(NewPrev);
106 void addToList(Use **List) {
108 if (Next) Next->setPrev(&Next);
112 void removeFromList() {
113 Use **StrippedPrev = Prev.getPointer();
114 *StrippedPrev = Next;
115 if (Next) Next->setPrev(StrippedPrev);
122 // simplify_type - Allow clients to treat uses just like values when using
123 // casting operators.
124 template<> struct simplify_type<Use> {
125 typedef Value* SimpleType;
126 static SimpleType getSimplifiedValue(const Use &Val) {
127 return static_cast<SimpleType>(Val.get());
130 template<> struct simplify_type<const Use> {
131 typedef Value* SimpleType;
132 static SimpleType getSimplifiedValue(const Use &Val) {
133 return static_cast<SimpleType>(Val.get());
139 template<typename UserTy> // UserTy == 'User' or 'const User'
140 class value_use_iterator : public forward_iterator<UserTy*, ptrdiff_t> {
141 typedef forward_iterator<UserTy*, ptrdiff_t> super;
142 typedef value_use_iterator<UserTy> _Self;
145 explicit value_use_iterator(Use *u) : U(u) {}
148 typedef typename super::reference reference;
149 typedef typename super::pointer pointer;
151 value_use_iterator(const _Self &I) : U(I.U) {}
152 value_use_iterator() {}
154 bool operator==(const _Self &x) const {
157 bool operator!=(const _Self &x) const {
158 return !operator==(x);
161 /// atEnd - return true if this iterator is equal to use_end() on the value.
162 bool atEnd() const { return U == 0; }
164 // Iterator traversal: forward iteration only
165 _Self &operator++() { // Preincrement
166 assert(U && "Cannot increment end iterator!");
170 _Self operator++(int) { // Postincrement
171 _Self tmp = *this; ++*this; return tmp;
174 // Retrieve a pointer to the current User.
175 UserTy *operator*() const {
176 assert(U && "Cannot dereference end iterator!");
180 UserTy *operator->() const { return operator*(); }
182 Use &getUse() const { return *U; }
184 /// getOperandNo - Return the operand # of this use in its User. Defined in
187 unsigned getOperandNo() const;
191 template<> struct simplify_type<value_use_iterator<User> > {
192 typedef User* SimpleType;
194 static SimpleType getSimplifiedValue(const value_use_iterator<User> &Val) {
199 template<> struct simplify_type<const value_use_iterator<User> >
200 : public simplify_type<value_use_iterator<User> > {};
202 template<> struct simplify_type<value_use_iterator<const User> > {
203 typedef const User* SimpleType;
205 static SimpleType getSimplifiedValue(const
206 value_use_iterator<const User> &Val) {
211 template<> struct simplify_type<const value_use_iterator<const User> >
212 : public simplify_type<value_use_iterator<const User> > {};
214 } // End llvm namespace