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 // We adhere to the following convention: The type of a tagged pointer
33 // to T is T volatile*. This means that functions that superpose a tag
34 // on a pointer will be supplied a T* (or T const*) and will return a
35 // tagged one: T volatile*. Untagging functions do it the other way
36 // 'round. While this scheme does not prevent dereferencing of tagged
37 // pointers, proper type annotations do catch most inappropriate uses.
39 /// Tag - generic tag type for (at least 32 bit) pointers
40 enum Tag { noTag, tagOne, tagTwo, tagThree };
42 /// addTag - insert tag bits into an (untagged) pointer
43 template <typename T, typename TAG>
44 inline volatile T *addTag(const T *P, TAG Tag) {
45 return reinterpret_cast<T*>(ptrdiff_t(P) | Tag);
48 /// stripTag - remove tag bits from a pointer,
49 /// making it dereferencable
50 template <ptrdiff_t MASK, typename T>
51 inline T *stripTag(const volatile T *P) {
52 return reinterpret_cast<T*>(ptrdiff_t(P) & ~MASK);
55 /// extractTag - extract tag bits from a pointer
56 template <typename TAG, TAG MASK, typename T>
57 inline TAG extractTag(const volatile T *P) {
58 return TAG(ptrdiff_t(P) & MASK);
61 /// transferTag - transfer tag bits from a pointer,
62 /// to an untagged pointer
63 template <ptrdiff_t MASK, typename T>
64 inline volatile T *transferTag(const volatile T *From, const T *To) {
65 return reinterpret_cast<T*>((ptrdiff_t(From) & MASK) | ptrdiff_t(To));
69 //===----------------------------------------------------------------------===//
71 //===----------------------------------------------------------------------===//
73 /// Use is here to make keeping the "use" list of a Value up-to-date really
77 /// swap - provide a fast substitute to std::swap<Use>
78 /// that also works with less standard-compliant compilers
82 /// Copy ctor - do not implement
85 /// Destructor - Only for zap()
87 if (Val) removeFromList();
90 /// Default ctor - This leaves the Use completely uninitialized. The only
91 /// thing that is valid to do with this use is to call the "init" method.
93 enum PrevPtrTag { zeroDigitTag = noTag
94 , oneDigitTag = tagOne
96 , fullStopTag = tagThree };
99 /// Normally Use will just implicitly convert to a Value* that it holds.
100 operator Value*() const { return Val; }
102 /// If implicit conversion to Value* doesn't work, the get() method returns
104 Value *get() const { return Val; }
106 /// getUser - This returns the User that contains this Use. For an
107 /// instruction operand, for example, this will return the instruction.
108 User *getUser() const;
110 inline void set(Value *Val);
112 Value *operator=(Value *RHS) {
116 const Use &operator=(const Use &RHS) {
121 Value *operator->() { return Val; }
122 const Value *operator->() const { return Val; }
124 Use *getNext() const { return Next; }
127 /// zap - This is used to destroy Use operands when the number of operands of
129 static void zap(Use *Start, const Use *Stop, bool del = false);
132 const Use* getImpliedUser() const;
133 static Use *initTags(Use *Start, Use *Stop, ptrdiff_t Done = 0);
136 Use *Next, *volatile*Prev;
138 void setPrev(Use **NewPrev) {
139 Prev = transferTag<fullStopTag>(Prev, NewPrev);
141 void addToList(Use **List) {
143 if (Next) Next->setPrev(&Next);
147 void removeFromList() {
148 Use **StrippedPrev = stripTag<fullStopTag>(Prev);
149 *StrippedPrev = Next;
150 if (Next) Next->setPrev(StrippedPrev);
157 // simplify_type - Allow clients to treat uses just like values when using
158 // casting operators.
159 template<> struct simplify_type<Use> {
160 typedef Value* SimpleType;
161 static SimpleType getSimplifiedValue(const Use &Val) {
162 return static_cast<SimpleType>(Val.get());
165 template<> struct simplify_type<const Use> {
166 typedef Value* SimpleType;
167 static SimpleType getSimplifiedValue(const Use &Val) {
168 return static_cast<SimpleType>(Val.get());
174 template<typename UserTy> // UserTy == 'User' or 'const User'
175 class value_use_iterator : public forward_iterator<UserTy*, ptrdiff_t> {
176 typedef forward_iterator<UserTy*, ptrdiff_t> super;
177 typedef value_use_iterator<UserTy> _Self;
180 explicit value_use_iterator(Use *u) : U(u) {}
183 typedef typename super::reference reference;
184 typedef typename super::pointer pointer;
186 value_use_iterator(const _Self &I) : U(I.U) {}
187 value_use_iterator() {}
189 bool operator==(const _Self &x) const {
192 bool operator!=(const _Self &x) const {
193 return !operator==(x);
196 /// atEnd - return true if this iterator is equal to use_end() on the value.
197 bool atEnd() const { return U == 0; }
199 // Iterator traversal: forward iteration only
200 _Self &operator++() { // Preincrement
201 assert(U && "Cannot increment end iterator!");
205 _Self operator++(int) { // Postincrement
206 _Self tmp = *this; ++*this; return tmp;
209 // Retrieve a pointer to the current User.
210 UserTy *operator*() const {
211 assert(U && "Cannot dereference end iterator!");
215 UserTy *operator->() const { return operator*(); }
217 Use &getUse() const { return *U; }
219 /// getOperandNo - Return the operand # of this use in its User. Defined in
222 unsigned getOperandNo() const;
226 template<> struct simplify_type<value_use_iterator<User> > {
227 typedef User* SimpleType;
229 static SimpleType getSimplifiedValue(const value_use_iterator<User> &Val) {
234 template<> struct simplify_type<const value_use_iterator<User> >
235 : public simplify_type<value_use_iterator<User> > {};
237 template<> struct simplify_type<value_use_iterator<const User> > {
238 typedef const User* SimpleType;
240 static SimpleType getSimplifiedValue(const
241 value_use_iterator<const User> &Val) {
246 template<> struct simplify_type<const value_use_iterator<const User> >
247 : public simplify_type<value_use_iterator<const User> > {};
249 } // End llvm namespace