[cleanup] Tidy up and modernize comments and the definition order for
[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>
41 struct simplify_type;
42
43 // Use** is only 4-byte aligned.
44 template<>
45 class PointerLikeTypeTraits<Use**> {
46 public:
47   static inline void *getAsVoidPointer(Use** P) { return P; }
48   static inline Use **getFromVoidPointer(void *P) {
49     return static_cast<Use**>(P);
50   }
51   enum { NumLowBitsAvailable = 2 };
52 };
53
54 /// \brief A Use represents the edge between a Value definition and its users.
55 ///
56 /// This is notionally a two-dimensional linked list. It supports traversing
57 /// all of the uses for a particular value definition. It also supports jumping
58 /// directly to the used value when we arrive from the User's operands, and
59 /// jumping directly to the User when we arrive from the Value's uses.
60 ///
61 /// The pointer to the used Value is explicit, and the pointer to the User is
62 /// implicit. The implicit pointer is found via a waymarking algorithm
63 /// described in the programmer's manual:
64 ///
65 ///   http://www.llvm.org/docs/ProgrammersManual.html#UserLayout
66 ///
67 /// This is essentially the single most memory intensive object in LLVM because
68 /// of the number of uses in the system. At the same time, the constant time
69 /// operations it allows are essential to many optimizations having reasonable
70 /// time complexity.
71 class Use {
72 public:
73   /// \brief Provide a fast substitute to std::swap<Use>
74   /// that also works with less standard-compliant compilers
75   void swap(Use &RHS);
76
77   // A type for the word following an array of hung-off Uses in memory, which is
78   // a pointer back to their User with the bottom bit set.
79   typedef PointerIntPair<User*, 1, unsigned> UserRef;
80
81 private:
82   Use(const Use &U) LLVM_DELETED_FUNCTION;
83
84   /// Destructor - Only for zap()
85   ~Use() {
86     if (Val) removeFromList();
87   }
88
89   enum PrevPtrTag { zeroDigitTag
90                   , oneDigitTag
91                   , stopTag
92                   , fullStopTag };
93
94   /// Constructor
95   Use(PrevPtrTag tag) : Val(0) {
96     Prev.setInt(tag);
97   }
98
99 public:
100   operator Value*() const { return Val; }
101   Value *get() const { return Val; }
102
103   /// \brief Returns the User that contains this Use.
104   ///
105   /// For an instruction operand, for example, this will return the
106   /// instruction.
107   User *getUser() const;
108
109   inline void set(Value *Val);
110
111   Value *operator=(Value *RHS) {
112     set(RHS);
113     return RHS;
114   }
115   const Use &operator=(const Use &RHS) {
116     set(RHS.Val);
117     return *this;
118   }
119
120         Value *operator->()       { return Val; }
121   const Value *operator->() const { return Val; }
122
123   Use *getNext() const { return Next; }
124
125   /// \brief Initializes the waymarking tags on an array of Uses.
126   ///
127   /// This sets up the array of Uses such that getUser() can find the User from
128   /// any of those Uses.
129   static Use *initTags(Use *Start, Use *Stop);
130
131   /// \brief Destroys Use operands when the number of operands of
132   /// a User changes.
133   static void zap(Use *Start, const Use *Stop, bool del = false);
134
135 private:
136   const Use* getImpliedUser() const;
137
138   Value *Val;
139   Use *Next;
140   PointerIntPair<Use**, 2, PrevPtrTag> Prev;
141
142   void setPrev(Use **NewPrev) {
143     Prev.setPointer(NewPrev);
144   }
145   void addToList(Use **List) {
146     Next = *List;
147     if (Next) Next->setPrev(&Next);
148     setPrev(List);
149     *List = this;
150   }
151   void removeFromList() {
152     Use **StrippedPrev = Prev.getPointer();
153     *StrippedPrev = Next;
154     if (Next) Next->setPrev(StrippedPrev);
155   }
156
157   friend class Value;
158 };
159
160 /// \brief Allow clients to treat uses just like values when using
161 /// casting operators.
162 template<> struct simplify_type<Use> {
163   typedef Value* SimpleType;
164   static SimpleType getSimplifiedValue(Use &Val) {
165     return Val.get();
166   }
167 };
168 template<> struct simplify_type<const Use> {
169   typedef /*const*/ Value* SimpleType;
170   static SimpleType getSimplifiedValue(const Use &Val) {
171     return Val.get();
172   }
173 };
174
175
176
177 template<typename UserTy>  // UserTy == 'User' or 'const User'
178 class value_use_iterator : public std::iterator<std::forward_iterator_tag,
179                                                 UserTy*, ptrdiff_t> {
180   typedef std::iterator<std::forward_iterator_tag, UserTy*, ptrdiff_t> super;
181   typedef value_use_iterator<UserTy> _Self;
182
183   Use *U;
184   explicit value_use_iterator(Use *u) : U(u) {}
185   friend class Value;
186 public:
187   typedef typename super::reference reference;
188   typedef typename super::pointer pointer;
189
190   value_use_iterator() {}
191
192   bool operator==(const _Self &x) const {
193     return U == x.U;
194   }
195   bool operator!=(const _Self &x) const {
196     return !operator==(x);
197   }
198
199   /// \brief Returns true if this iterator is equal to use_end() on the value.
200   bool atEnd() const { return U == 0; }
201
202   // Iterator traversal: forward iteration only
203   _Self &operator++() {          // Preincrement
204     assert(U && "Cannot increment end iterator!");
205     U = U->getNext();
206     return *this;
207   }
208   _Self operator++(int) {        // Postincrement
209     _Self tmp = *this; ++*this; return tmp;
210   }
211
212   // Retrieve a pointer to the current User.
213   UserTy *operator*() const {
214     assert(U && "Cannot dereference end iterator!");
215     return U->getUser();
216   }
217
218   UserTy *operator->() const { return operator*(); }
219
220   Use &getUse() const { return *U; }
221
222   /// \brief Return the operand # of this use in its User.
223   ///
224   /// Defined in User.h
225   unsigned getOperandNo() const;
226 };
227
228 // Create wrappers for C Binding types (see CBindingWrapping.h).
229 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Use, LLVMUseRef)
230
231 }
232
233 #endif