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 Use(const Use &U) LLVM_DELETED_FUNCTION;
71 /// Destructor - Only for zap()
73 if (Val) removeFromList();
76 enum PrevPtrTag { zeroDigitTag
82 Use(PrevPtrTag tag) : Val(0) {
87 /// Normally Use will just implicitly convert to a Value* that it holds.
88 operator Value*() const { return Val; }
90 /// If implicit conversion to Value* doesn't work, the get() method returns
92 Value *get() const { return Val; }
94 /// getUser - This returns the User that contains this Use. For an
95 /// instruction operand, for example, this will return the instruction.
96 User *getUser() const;
98 inline void set(Value *Val);
100 Value *operator=(Value *RHS) {
104 const Use &operator=(const Use &RHS) {
109 Value *operator->() { return Val; }
110 const Value *operator->() const { return Val; }
112 Use *getNext() const { return Next; }
115 /// initTags - initialize the waymarking tags on an array of Uses, so that
116 /// getUser() can find the User from any of those Uses.
117 static Use *initTags(Use *Start, Use *Stop);
119 /// zap - This is used to destroy Use operands when the number of operands of
121 static void zap(Use *Start, const Use *Stop, bool del = false);
124 const Use* getImpliedUser() const;
128 PointerIntPair<Use**, 2, PrevPtrTag> Prev;
130 void setPrev(Use **NewPrev) {
131 Prev.setPointer(NewPrev);
133 void addToList(Use **List) {
135 if (Next) Next->setPrev(&Next);
139 void removeFromList() {
140 Use **StrippedPrev = Prev.getPointer();
141 *StrippedPrev = Next;
142 if (Next) Next->setPrev(StrippedPrev);
148 // simplify_type - Allow clients to treat uses just like values when using
149 // casting operators.
150 template<> struct simplify_type<Use> {
151 typedef Value* SimpleType;
152 static SimpleType getSimplifiedValue(const Use &Val) {
153 return static_cast<SimpleType>(Val.get());
156 template<> struct simplify_type<const Use> {
157 typedef Value* SimpleType;
158 static SimpleType getSimplifiedValue(const Use &Val) {
159 return static_cast<SimpleType>(Val.get());
165 template<typename UserTy> // UserTy == 'User' or 'const User'
166 class value_use_iterator : public std::iterator<std::forward_iterator_tag,
167 UserTy*, ptrdiff_t> {
168 typedef std::iterator<std::forward_iterator_tag, UserTy*, ptrdiff_t> super;
169 typedef value_use_iterator<UserTy> _Self;
172 explicit value_use_iterator(Use *u) : U(u) {}
175 typedef typename super::reference reference;
176 typedef typename super::pointer pointer;
178 value_use_iterator(const _Self &I) : U(I.U) {}
179 value_use_iterator() {}
181 bool operator==(const _Self &x) const {
184 bool operator!=(const _Self &x) const {
185 return !operator==(x);
188 /// atEnd - return true if this iterator is equal to use_end() on the value.
189 bool atEnd() const { return U == 0; }
191 // Iterator traversal: forward iteration only
192 _Self &operator++() { // Preincrement
193 assert(U && "Cannot increment end iterator!");
197 _Self operator++(int) { // Postincrement
198 _Self tmp = *this; ++*this; return tmp;
201 // Retrieve a pointer to the current User.
202 UserTy *operator*() const {
203 assert(U && "Cannot dereference end iterator!");
207 UserTy *operator->() const { return operator*(); }
209 Use &getUse() const { return *U; }
211 /// getOperandNo - Return the operand # of this use in its User. Defined in
214 unsigned getOperandNo() const;
217 } // End llvm namespace