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 inline void init(Value *V, User *U);
73 /// Allow std::swap some intimacy
74 template <typename U> friend void std::swap(U&, U&);
76 /// Copy ctor - Only for std::swap
77 Use(const Use &U) { init(U.get(), 0); }
79 /// Destructor - Only for zap() and std::swap
81 if (get()) removeFromList();
84 /// Default ctor - This leaves the Use completely uninitialized. The only thing
85 /// that is valid to do with this use is to call the "init" method.
88 enum PrevPtrTag { zeroDigitTag = noTag
89 , oneDigitTag = tagOne
91 , fullStopTag = tagThree };
96 operator Value*() const { return Val; }
97 Value *get() const { return Val; }
98 User *getUser() const;
99 const Use* getImpliedUser() const;
100 static Use *initTags(Use *Start, Use *Stop, ptrdiff_t Done = 0);
101 static void zap(Use *Start, const Use *Stop, bool del = false);
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; }
122 void setPrev(Use **NewPrev) {
123 Prev = transferTag<fullStopTag>(Prev, NewPrev);
125 void addToList(Use **List) {
127 if (Next) Next->setPrev(&Next);
131 void removeFromList() {
132 Use **StrippedPrev = stripTag<fullStopTag>(Prev);
133 *StrippedPrev = Next;
134 if (Next) Next->setPrev(StrippedPrev);
140 // simplify_type - Allow clients to treat uses just like values when using
141 // casting operators.
142 template<> struct simplify_type<Use> {
143 typedef Value* SimpleType;
144 static SimpleType getSimplifiedValue(const Use &Val) {
145 return static_cast<SimpleType>(Val.get());
148 template<> struct simplify_type<const Use> {
149 typedef Value* SimpleType;
150 static SimpleType getSimplifiedValue(const Use &Val) {
151 return static_cast<SimpleType>(Val.get());
157 template<typename UserTy> // UserTy == 'User' or 'const User'
158 class value_use_iterator : public forward_iterator<UserTy*, ptrdiff_t> {
159 typedef forward_iterator<UserTy*, ptrdiff_t> super;
160 typedef value_use_iterator<UserTy> _Self;
163 explicit value_use_iterator(Use *u) : U(u) {}
166 typedef typename super::reference reference;
167 typedef typename super::pointer pointer;
169 value_use_iterator(const _Self &I) : U(I.U) {}
170 value_use_iterator() {}
172 bool operator==(const _Self &x) const {
175 bool operator!=(const _Self &x) const {
176 return !operator==(x);
179 /// atEnd - return true if this iterator is equal to use_end() on the value.
180 bool atEnd() const { return U == 0; }
182 // Iterator traversal: forward iteration only
183 _Self &operator++() { // Preincrement
184 assert(U && "Cannot increment end iterator!");
188 _Self operator++(int) { // Postincrement
189 _Self tmp = *this; ++*this; return tmp;
192 // Retrieve a reference to the current User
193 UserTy *operator*() const {
194 assert(U && "Cannot dereference end iterator!");
198 UserTy *operator->() const { return operator*(); }
200 Use &getUse() const { return *U; }
202 /// getOperandNo - Return the operand # of this use in its User. Defined in
205 unsigned getOperandNo() const;
209 template<> struct simplify_type<value_use_iterator<User> > {
210 typedef User* SimpleType;
212 static SimpleType getSimplifiedValue(const value_use_iterator<User> &Val) {
217 template<> struct simplify_type<const value_use_iterator<User> >
218 : public simplify_type<value_use_iterator<User> > {};
220 template<> struct simplify_type<value_use_iterator<const User> > {
221 typedef const User* SimpleType;
223 static SimpleType getSimplifiedValue(const
224 value_use_iterator<const User> &Val) {
229 template<> struct simplify_type<const value_use_iterator<const User> >
230 : public simplify_type<value_use_iterator<const User> > {};
232 } // End llvm namespace