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 //===----------------------------------------------------------------------===//
11 /// This defines the Use class. The Use class represents the operand of an
12 /// instruction or some other User instance which refers to a Value. The Use
13 /// class keeps the "use list" of the referenced value up to date.
15 /// Pointer tagging is used to efficiently find the User corresponding to a Use
16 /// without having to store a User pointer in every Use. A User is preceded in
17 /// memory by all the Uses corresponding to its operands, and the low bits of
18 /// one of the fields (Prev) of the Use class are used to encode offsets to be
19 /// able to find that User given a pointer to any Use. For details, see:
21 /// http://www.llvm.org/docs/ProgrammersManual.html#UserLayout
23 //===----------------------------------------------------------------------===//
28 #include "llvm-c/Core.h"
29 #include "llvm/ADT/PointerIntPair.h"
30 #include "llvm/Support/CBindingWrapping.h"
31 #include "llvm/Support/Compiler.h"
40 template <typename> struct simplify_type;
42 // Use** is only 4-byte aligned.
43 template <> 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 /// \brief A Use represents the edge between a Value definition and its users.
54 /// This is notionally a two-dimensional linked list. It supports traversing
55 /// all of the uses for a particular value definition. It also supports jumping
56 /// directly to the used value when we arrive from the User's operands, and
57 /// jumping directly to the User when we arrive from the Value's uses.
59 /// The pointer to the used Value is explicit, and the pointer to the User is
60 /// implicit. The implicit pointer is found via a waymarking algorithm
61 /// described in the programmer's manual:
63 /// http://www.llvm.org/docs/ProgrammersManual.html#UserLayout
65 /// This is essentially the single most memory intensive object in LLVM because
66 /// of the number of uses in the system. At the same time, the constant time
67 /// operations it allows are essential to many optimizations having reasonable
71 /// \brief Provide a fast substitute to std::swap<Use>
72 /// that also works with less standard-compliant compilers
75 // A type for the word following an array of hung-off Uses in memory, which is
76 // a pointer back to their User with the bottom bit set.
77 typedef PointerIntPair<User *, 1, unsigned> UserRef;
80 Use(const Use &U) LLVM_DELETED_FUNCTION;
82 /// Destructor - Only for zap()
88 enum PrevPtrTag { zeroDigitTag, oneDigitTag, stopTag, fullStopTag };
91 Use(PrevPtrTag tag) : Val(0) { Prev.setInt(tag); }
94 operator Value *() const { return Val; }
95 Value *get() const { return Val; }
97 /// \brief Returns the User that contains this Use.
99 /// For an instruction operand, for example, this will return the
101 User *getUser() const;
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; }
119 /// \brief Return the operand # of this use in its User.
120 unsigned getOperandNo() const;
122 /// \brief Initializes the waymarking tags on an array of Uses.
124 /// This sets up the array of Uses such that getUser() can find the User from
125 /// any of those Uses.
126 static Use *initTags(Use *Start, Use *Stop);
128 /// \brief Destroys Use operands when the number of operands of
130 static void zap(Use *Start, const Use *Stop, bool del = false);
133 const Use *getImpliedUser() const;
137 PointerIntPair<Use **, 2, PrevPtrTag> Prev;
139 void setPrev(Use **NewPrev) { Prev.setPointer(NewPrev); }
140 void addToList(Use **List) {
143 Next->setPrev(&Next);
147 void removeFromList() {
148 Use **StrippedPrev = Prev.getPointer();
149 *StrippedPrev = Next;
151 Next->setPrev(StrippedPrev);
157 /// \brief 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(Use &Val) { return Val.get(); }
163 template <> struct simplify_type<const Use> {
164 typedef /*const*/ Value *SimpleType;
165 static SimpleType getSimplifiedValue(const Use &Val) { return Val.get(); }
168 template<typename UserTy> // UserTy == 'User' or 'const User'
169 class value_use_iterator : public std::iterator<std::forward_iterator_tag,
170 UserTy*, ptrdiff_t> {
171 typedef std::iterator<std::forward_iterator_tag, UserTy*, ptrdiff_t> super;
172 typedef value_use_iterator<UserTy> _Self;
175 explicit value_use_iterator(Use *u) : U(u) {}
178 typedef typename super::reference reference;
179 typedef typename super::pointer pointer;
181 value_use_iterator() {}
183 bool operator==(const _Self &x) const {
186 bool operator!=(const _Self &x) const {
187 return !operator==(x);
190 /// \brief Returns true if this iterator is equal to use_end() on the value.
191 bool atEnd() const { return U == 0; }
193 // Iterator traversal: forward iteration only
194 _Self &operator++() { // Preincrement
195 assert(U && "Cannot increment end iterator!");
199 _Self operator++(int) { // Postincrement
200 _Self tmp = *this; ++*this; return tmp;
203 // Retrieve a pointer to the current User.
204 UserTy *operator*() const {
205 assert(U && "Cannot dereference end iterator!");
209 UserTy *operator->() const { return operator*(); }
211 Use &getUse() const { return *U; }
213 /// \brief Return the operand # of this use in its User.
214 /// FIXME: Replace all callers with a direct call to Use::getOperandNo.
215 unsigned getOperandNo() const { return U->getOperandNo(); }
218 // Create wrappers for C Binding types (see CBindingWrapping.h).
219 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Use, LLVMUseRef)