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"
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 easy.
70 /// init - specify Value and User
71 /// @deprecated in 2.4, will be removed soon
72 inline void init(Value *V, User *U);
73 /// swap - provide a fast substitute to std::swap<Use>
74 /// that also works with less standard-compliant compilers
78 /// Copy ctor - do not implement
81 /// Destructor - Only for zap()
83 if (Val) removeFromList();
86 /// Default ctor - This leaves the Use completely uninitialized. The only thing
87 /// that is valid to do with this use is to call the "init" method.
90 enum PrevPtrTag { zeroDigitTag = noTag
91 , oneDigitTag = tagOne
93 , fullStopTag = tagThree };
98 operator Value*() const { return Val; }
99 Value *get() const { return Val; }
100 User *getUser() const;
101 const Use* getImpliedUser() const;
102 static Use *initTags(Use *Start, Use *Stop, ptrdiff_t Done = 0);
103 static void zap(Use *Start, const Use *Stop, bool del = false);
105 inline void set(Value *Val);
107 Value *operator=(Value *RHS) {
111 const Use &operator=(const Use &RHS) {
116 Value *operator->() { return Val; }
117 const Value *operator->() const { return Val; }
119 Use *getNext() const { return Next; }
124 void setPrev(Use **NewPrev) {
125 Prev = transferTag<fullStopTag>(Prev, NewPrev);
127 void addToList(Use **List) {
129 if (Next) Next->setPrev(&Next);
133 void removeFromList() {
134 Use **StrippedPrev = stripTag<fullStopTag>(Prev);
135 *StrippedPrev = Next;
136 if (Next) Next->setPrev(StrippedPrev);
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());
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());
159 template<typename UserTy> // UserTy == 'User' or 'const User'
160 class value_use_iterator : public forward_iterator<UserTy*, ptrdiff_t> {
161 typedef forward_iterator<UserTy*, ptrdiff_t> super;
162 typedef value_use_iterator<UserTy> _Self;
165 explicit value_use_iterator(Use *u) : U(u) {}
168 typedef typename super::reference reference;
169 typedef typename super::pointer pointer;
171 value_use_iterator(const _Self &I) : U(I.U) {}
172 value_use_iterator() {}
174 bool operator==(const _Self &x) const {
177 bool operator!=(const _Self &x) const {
178 return !operator==(x);
181 /// atEnd - return true if this iterator is equal to use_end() on the value.
182 bool atEnd() const { return U == 0; }
184 // Iterator traversal: forward iteration only
185 _Self &operator++() { // Preincrement
186 assert(U && "Cannot increment end iterator!");
190 _Self operator++(int) { // Postincrement
191 _Self tmp = *this; ++*this; return tmp;
194 // Retrieve a reference to the current User
195 UserTy *operator*() const {
196 assert(U && "Cannot dereference end iterator!");
200 UserTy *operator->() const { return operator*(); }
202 Use &getUse() const { return *U; }
204 /// getOperandNo - Return the operand # of this use in its User. Defined in
207 unsigned getOperandNo() const;
211 template<> struct simplify_type<value_use_iterator<User> > {
212 typedef User* SimpleType;
214 static SimpleType getSimplifiedValue(const value_use_iterator<User> &Val) {
219 template<> struct simplify_type<const value_use_iterator<User> >
220 : public simplify_type<value_use_iterator<User> > {};
222 template<> struct simplify_type<value_use_iterator<const User> > {
223 typedef const User* SimpleType;
225 static SimpleType getSimplifiedValue(const
226 value_use_iterator<const User> &Val) {
231 template<> struct simplify_type<const value_use_iterator<const User> >
232 : public simplify_type<value_use_iterator<const User> > {};
234 } // End llvm namespace