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"
28 //===----------------------------------------------------------------------===//
29 // Generic Tagging Functions
30 //===----------------------------------------------------------------------===//
32 /// Tag - generic tag type for (at least 32 bit) pointers
33 enum Tag { noTag, tagOne, tagTwo, tagThree };
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);
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);
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);
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));
62 //===----------------------------------------------------------------------===//
64 //===----------------------------------------------------------------------===//
66 /// Use is here to make keeping the "use" list of a Value up-to-date really
70 /// swap - provide a fast substitute to std::swap<Use>
71 /// that also works with less standard-compliant compilers
75 /// Copy ctor - do not implement
78 /// Destructor - Only for zap()
80 if (Val) removeFromList();
83 /// Default ctor - This leaves the Use completely uninitialized. The only
84 /// thing that is valid to do with this use is to call the "init" method.
86 enum PrevPtrTag { zeroDigitTag = noTag
87 , oneDigitTag = tagOne
89 , fullStopTag = tagThree };
92 /// Normally Use will just implicitly convert to a Value* that it holds.
93 operator Value*() const { return Val; }
95 /// If implicit conversion to Value* doesn't work, the get() method returns
97 Value *get() const { return Val; }
99 /// getUser - This returns the User that contains this Use. For an
100 /// instruction operand, for example, this will return the instruction.
101 User *getUser() const;
103 inline void set(Value *Val);
105 Value *operator=(Value *RHS) {
109 const Use &operator=(const Use &RHS) {
114 Value *operator->() { return Val; }
115 const Value *operator->() const { return Val; }
117 Use *getNext() const { return Next; }
120 /// zap - This is used to destroy Use operands when the number of operands of
122 static void zap(Use *Start, const Use *Stop, bool del = false);
125 const Use* getImpliedUser() const;
126 static Use *initTags(Use *Start, Use *Stop, ptrdiff_t Done = 0);
131 void setPrev(Use **NewPrev) {
132 Prev = transferTag<fullStopTag>(Prev, NewPrev);
134 void addToList(Use **List) {
136 if (Next) Next->setPrev(&Next);
140 void removeFromList() {
141 Use **StrippedPrev = stripTag<fullStopTag>(Prev);
142 *StrippedPrev = Next;
143 if (Next) Next->setPrev(StrippedPrev);
150 // simplify_type - Allow clients to treat uses just like values when using
151 // casting operators.
152 template<> struct simplify_type<Use> {
153 typedef Value* SimpleType;
154 static SimpleType getSimplifiedValue(const Use &Val) {
155 return static_cast<SimpleType>(Val.get());
158 template<> struct simplify_type<const Use> {
159 typedef Value* SimpleType;
160 static SimpleType getSimplifiedValue(const Use &Val) {
161 return static_cast<SimpleType>(Val.get());
167 template<typename UserTy> // UserTy == 'User' or 'const User'
168 class value_use_iterator : public forward_iterator<UserTy*, ptrdiff_t> {
169 typedef forward_iterator<UserTy*, ptrdiff_t> super;
170 typedef value_use_iterator<UserTy> _Self;
173 explicit value_use_iterator(Use *u) : U(u) {}
176 typedef typename super::reference reference;
177 typedef typename super::pointer pointer;
179 value_use_iterator(const _Self &I) : U(I.U) {}
180 value_use_iterator() {}
182 bool operator==(const _Self &x) const {
185 bool operator!=(const _Self &x) const {
186 return !operator==(x);
189 /// atEnd - return true if this iterator is equal to use_end() on the value.
190 bool atEnd() const { return U == 0; }
192 // Iterator traversal: forward iteration only
193 _Self &operator++() { // Preincrement
194 assert(U && "Cannot increment end iterator!");
198 _Self operator++(int) { // Postincrement
199 _Self tmp = *this; ++*this; return tmp;
202 // Retrieve a pointer to the current User.
203 UserTy *operator*() const {
204 assert(U && "Cannot dereference end iterator!");
208 UserTy *operator->() const { return operator*(); }
210 Use &getUse() const { return *U; }
212 /// getOperandNo - Return the operand # of this use in its User. Defined in
215 unsigned getOperandNo() const;
219 template<> struct simplify_type<value_use_iterator<User> > {
220 typedef User* SimpleType;
222 static SimpleType getSimplifiedValue(const value_use_iterator<User> &Val) {
227 template<> struct simplify_type<const value_use_iterator<User> >
228 : public simplify_type<value_use_iterator<User> > {};
230 template<> struct simplify_type<value_use_iterator<const User> > {
231 typedef const User* SimpleType;
233 static SimpleType getSimplifiedValue(const
234 value_use_iterator<const User> &Val) {
239 template<> struct simplify_type<const value_use_iterator<const User> >
240 : public simplify_type<value_use_iterator<const User> > {};
242 } // End llvm namespace