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 // Pointer tagging is used to efficiently find the User corresponding
15 // to a Use without having to store a User pointer in every Use. A
16 // User is preceded in memory by all the Uses corresponding to its
17 // operands, and the low bits of one of the fields (Prev) of the Use
18 // class are used to encode offsets to be able to find that User given
19 // a pointer to any Use. For details, see:
21 // http://www.llvm.org/docs/ProgrammersManual.html#UserLayout
23 //===----------------------------------------------------------------------===//
28 #include "llvm/ADT/PointerIntPair.h"
29 #include "llvm/Support/Compiler.h"
41 // Use** is only 4-byte aligned.
43 class PointerLikeTypeTraits<Use**> {
45 static inline void *getAsVoidPointer(Use** P) { return P; }
46 static inline Use **getFromVoidPointer(void *P) {
47 return static_cast<Use**>(P);
49 enum { NumLowBitsAvailable = 2 };
52 //===----------------------------------------------------------------------===//
54 //===----------------------------------------------------------------------===//
56 /// Use is here to make keeping the "use" list of a Value up-to-date really
60 /// swap - provide a fast substitute to std::swap<Use>
61 /// that also works with less standard-compliant compilers
64 // A type for the word following an array of hung-off Uses in memory, which is
65 // a pointer back to their User with the bottom bit set.
66 typedef PointerIntPair<User*, 1, unsigned> UserRef;
69 /// Copy ctor - do not implement
70 Use(const Use &U) LLVM_DELETED_FUNCTION;
72 /// Destructor - Only for zap()
74 if (Val) removeFromList();
77 enum PrevPtrTag { zeroDigitTag
83 Use(PrevPtrTag tag) : Val(0) {
88 /// Normally Use will just implicitly convert to a Value* that it holds.
89 operator Value*() const { return Val; }
91 /// If implicit conversion to Value* doesn't work, the get() method returns
93 Value *get() const { return Val; }
95 /// getUser - This returns the User that contains this Use. For an
96 /// instruction operand, for example, this will return the instruction.
97 User *getUser() const;
99 inline void set(Value *Val);
101 Value *operator=(Value *RHS) {
105 const Use &operator=(const Use &RHS) {
110 Value *operator->() { return Val; }
111 const Value *operator->() const { return Val; }
113 Use *getNext() const { return Next; }
116 /// initTags - initialize the waymarking tags on an array of Uses, so that
117 /// getUser() can find the User from any of those Uses.
118 static Use *initTags(Use *Start, Use *Stop);
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;
129 PointerIntPair<Use**, 2, PrevPtrTag> Prev;
131 void setPrev(Use **NewPrev) {
132 Prev.setPointer(NewPrev);
134 void addToList(Use **List) {
136 if (Next) Next->setPrev(&Next);
140 void removeFromList() {
141 Use **StrippedPrev = Prev.getPointer();
142 *StrippedPrev = Next;
143 if (Next) Next->setPrev(StrippedPrev);
149 // simplify_type - Allow clients to treat uses just like values when using
150 // casting operators.
151 template<> struct simplify_type<Use> {
152 typedef Value* SimpleType;
153 static SimpleType getSimplifiedValue(const Use &Val) {
154 return static_cast<SimpleType>(Val.get());
157 template<> struct simplify_type<const Use> {
158 typedef Value* SimpleType;
159 static SimpleType getSimplifiedValue(const Use &Val) {
160 return static_cast<SimpleType>(Val.get());
166 template<typename UserTy> // UserTy == 'User' or 'const User'
167 class value_use_iterator : public std::iterator<std::forward_iterator_tag,
168 UserTy*, ptrdiff_t> {
169 typedef std::iterator<std::forward_iterator_tag, 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;
218 } // End llvm namespace