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/CBindingWrapping.h"
30 #include "llvm/Support/Compiler.h"
31 #include "llvm-c/Core.h"
43 // Use** is only 4-byte aligned.
45 class PointerLikeTypeTraits<Use**> {
47 static inline void *getAsVoidPointer(Use** P) { return P; }
48 static inline Use **getFromVoidPointer(void *P) {
49 return static_cast<Use**>(P);
51 enum { NumLowBitsAvailable = 2 };
54 //===----------------------------------------------------------------------===//
56 //===----------------------------------------------------------------------===//
58 /// Use is here to make keeping the "use" list of a Value up-to-date really
62 /// swap - provide a fast substitute to std::swap<Use>
63 /// that also works with less standard-compliant compilers
66 // A type for the word following an array of hung-off Uses in memory, which is
67 // a pointer back to their User with the bottom bit set.
68 typedef PointerIntPair<User*, 1, unsigned> UserRef;
71 Use(const Use &U) LLVM_DELETED_FUNCTION;
73 /// Destructor - Only for zap()
75 if (Val) removeFromList();
78 enum PrevPtrTag { zeroDigitTag
84 Use(PrevPtrTag tag) : Val(0) {
89 /// Normally Use will just implicitly convert to a Value* that it holds.
90 operator Value*() const { return Val; }
92 /// If implicit conversion to Value* doesn't work, the get() method returns
94 Value *get() const { return Val; }
96 /// getUser - This returns the User that contains this Use. For an
97 /// instruction operand, for example, this will return the instruction.
98 User *getUser() const;
100 inline void set(Value *Val);
102 Value *operator=(Value *RHS) {
106 const Use &operator=(const Use &RHS) {
111 Value *operator->() { return Val; }
112 const Value *operator->() const { return Val; }
114 Use *getNext() const { return Next; }
117 /// initTags - initialize the waymarking tags on an array of Uses, so that
118 /// getUser() can find the User from any of those Uses.
119 static Use *initTags(Use *Start, Use *Stop);
121 /// zap - This is used to destroy Use operands when the number of operands of
123 static void zap(Use *Start, const Use *Stop, bool del = false);
126 const Use* getImpliedUser() const;
130 PointerIntPair<Use**, 2, PrevPtrTag> Prev;
132 void setPrev(Use **NewPrev) {
133 Prev.setPointer(NewPrev);
135 void addToList(Use **List) {
137 if (Next) Next->setPrev(&Next);
141 void removeFromList() {
142 Use **StrippedPrev = Prev.getPointer();
143 *StrippedPrev = Next;
144 if (Next) Next->setPrev(StrippedPrev);
150 // simplify_type - Allow clients to treat uses just like values when using
151 // casting operators.
152 template<> struct simplify_type<Use> {
153 typedef Value* SimpleType;
154 static SimpleType getSimplifiedValue(Use &Val) {
158 template<> struct simplify_type<const Use> {
159 typedef /*const*/ Value* SimpleType;
160 static SimpleType getSimplifiedValue(const Use &Val) {
167 template<typename UserTy> // UserTy == 'User' or 'const User'
168 class value_use_iterator : public std::iterator<std::forward_iterator_tag,
169 UserTy*, ptrdiff_t> {
170 typedef std::iterator<std::forward_iterator_tag, UserTy*, ptrdiff_t> super;
171 typedef value_use_iterator<UserTy> _Self;
174 explicit value_use_iterator(Use *u) : U(u) {}
177 typedef typename super::reference reference;
178 typedef typename super::pointer pointer;
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 // Create wrappers for C Binding types (see CBindingWrapping.h).
219 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Use, LLVMUseRef)
221 } // End llvm namespace