[cleanup] Run clang-format over the Use code. It was *really*
[oota-llvm.git] / include / llvm / IR / Use.h
1 //===-- llvm/Use.h - Definition of the Use class ----------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 /// \file
10 ///
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.
14 ///
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:
20 ///
21 ///   http://www.llvm.org/docs/ProgrammersManual.html#UserLayout
22 ///
23 //===----------------------------------------------------------------------===//
24
25 #ifndef LLVM_IR_USE_H
26 #define LLVM_IR_USE_H
27
28 #include "llvm-c/Core.h"
29 #include "llvm/ADT/PointerIntPair.h"
30 #include "llvm/Support/CBindingWrapping.h"
31 #include "llvm/Support/Compiler.h"
32 #include <cstddef>
33 #include <iterator>
34
35 namespace llvm {
36
37 class Value;
38 class User;
39 class Use;
40 template <typename> struct simplify_type;
41
42 // Use** is only 4-byte aligned.
43 template <> class PointerLikeTypeTraits<Use **> {
44 public:
45   static inline void *getAsVoidPointer(Use **P) { return P; }
46   static inline Use **getFromVoidPointer(void *P) {
47     return static_cast<Use **>(P);
48   }
49   enum { NumLowBitsAvailable = 2 };
50 };
51
52 /// \brief A Use represents the edge between a Value definition and its users.
53 ///
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.
58 ///
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:
62 ///
63 ///   http://www.llvm.org/docs/ProgrammersManual.html#UserLayout
64 ///
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
68 /// time complexity.
69 class Use {
70 public:
71   /// \brief Provide a fast substitute to std::swap<Use>
72   /// that also works with less standard-compliant compilers
73   void swap(Use &RHS);
74
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;
78
79 private:
80   Use(const Use &U) LLVM_DELETED_FUNCTION;
81
82   /// Destructor - Only for zap()
83   ~Use() {
84     if (Val)
85       removeFromList();
86   }
87
88   enum PrevPtrTag { zeroDigitTag, oneDigitTag, stopTag, fullStopTag };
89
90   /// Constructor
91   Use(PrevPtrTag tag) : Val(0) { Prev.setInt(tag); }
92
93 public:
94   operator Value *() const { return Val; }
95   Value *get() const { return Val; }
96
97   /// \brief Returns the User that contains this Use.
98   ///
99   /// For an instruction operand, for example, this will return the
100   /// instruction.
101   User *getUser() const;
102
103   inline void set(Value *Val);
104
105   Value *operator=(Value *RHS) {
106     set(RHS);
107     return RHS;
108   }
109   const Use &operator=(const Use &RHS) {
110     set(RHS.Val);
111     return *this;
112   }
113
114   Value *operator->() { return Val; }
115   const Value *operator->() const { return Val; }
116
117   Use *getNext() const { return Next; }
118
119   /// \brief Initializes the waymarking tags on an array of Uses.
120   ///
121   /// This sets up the array of Uses such that getUser() can find the User from
122   /// any of those Uses.
123   static Use *initTags(Use *Start, Use *Stop);
124
125   /// \brief Destroys Use operands when the number of operands of
126   /// a User changes.
127   static void zap(Use *Start, const Use *Stop, bool del = false);
128
129 private:
130   const Use *getImpliedUser() const;
131
132   Value *Val;
133   Use *Next;
134   PointerIntPair<Use **, 2, PrevPtrTag> Prev;
135
136   void setPrev(Use **NewPrev) { Prev.setPointer(NewPrev); }
137   void addToList(Use **List) {
138     Next = *List;
139     if (Next)
140       Next->setPrev(&Next);
141     setPrev(List);
142     *List = this;
143   }
144   void removeFromList() {
145     Use **StrippedPrev = Prev.getPointer();
146     *StrippedPrev = Next;
147     if (Next)
148       Next->setPrev(StrippedPrev);
149   }
150
151   friend class Value;
152 };
153
154 /// \brief Allow clients to treat uses just like values when using
155 /// casting operators.
156 template <> struct simplify_type<Use> {
157   typedef Value *SimpleType;
158   static SimpleType getSimplifiedValue(Use &Val) { return Val.get(); }
159 };
160 template <> struct simplify_type<const Use> {
161   typedef /*const*/ Value *SimpleType;
162   static SimpleType getSimplifiedValue(const Use &Val) { return Val.get(); }
163 };
164
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;
170
171   Use *U;
172   explicit value_use_iterator(Use *u) : U(u) {}
173   friend class Value;
174 public:
175   typedef typename super::reference reference;
176   typedef typename super::pointer pointer;
177
178   value_use_iterator() {}
179
180   bool operator==(const _Self &x) const {
181     return U == x.U;
182   }
183   bool operator!=(const _Self &x) const {
184     return !operator==(x);
185   }
186
187   /// \brief Returns true if this iterator is equal to use_end() on the value.
188   bool atEnd() const { return U == 0; }
189
190   // Iterator traversal: forward iteration only
191   _Self &operator++() {          // Preincrement
192     assert(U && "Cannot increment end iterator!");
193     U = U->getNext();
194     return *this;
195   }
196   _Self operator++(int) {        // Postincrement
197     _Self tmp = *this; ++*this; return tmp;
198   }
199
200   // Retrieve a pointer to the current User.
201   UserTy *operator*() const {
202     assert(U && "Cannot dereference end iterator!");
203     return U->getUser();
204   }
205
206   UserTy *operator->() const { return operator*(); }
207
208   Use &getUse() const { return *U; }
209
210   /// \brief Return the operand # of this use in its User.
211   ///
212   /// Defined in User.h
213   unsigned getOperandNo() const;
214 };
215
216 // Create wrappers for C Binding types (see CBindingWrapping.h).
217 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Use, LLVMUseRef)
218
219 }
220
221 #endif