Remove a bunch of inline keywords from User. NFC.
[oota-llvm.git] / include / llvm / IR / User.h
1 //===-- llvm/User.h - User class definition ---------------------*- 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 //
10 // This class defines the interface that one who uses a Value must implement.
11 // Each instance of the Value class keeps track of what User's have handles
12 // to it.
13 //
14 //  * Instructions are the largest class of Users.
15 //  * Constants may be users of other constants (think arrays and stuff)
16 //
17 //===----------------------------------------------------------------------===//
18
19 #ifndef LLVM_IR_USER_H
20 #define LLVM_IR_USER_H
21
22 #include "llvm/ADT/iterator.h"
23 #include "llvm/ADT/iterator_range.h"
24 #include "llvm/IR/Value.h"
25 #include "llvm/Support/ErrorHandling.h"
26
27 namespace llvm {
28
29 /// \brief Compile-time customization of User operands.
30 ///
31 /// Customizes operand-related allocators and accessors.
32 template <class>
33 struct OperandTraits;
34
35 class User : public Value {
36   User(const User &) = delete;
37   template <unsigned>
38   friend struct HungoffOperandTraits;
39   virtual void anchor();
40
41 protected:
42   /// Allocate a User with an operand pointer co-allocated.
43   ///
44   /// This is used for subclasses which need to allocate a variable number
45   /// of operands, ie, 'hung off uses'.
46   void *operator new(size_t Size);
47
48   /// Allocate a User with the operands co-allocated.
49   ///
50   /// This is used for subclasses which have a fixed number of operands.
51   void *operator new(size_t Size, unsigned Us);
52
53   User(Type *ty, unsigned vty, Use *OpList, unsigned NumOps)
54       : Value(ty, vty) {
55     assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
56     NumUserOperands = NumOps;
57     // If we have hung off uses, then the operand list should initially be
58     // null.
59     assert((!HasHungOffUses || !getOperandList()) &&
60            "Error in initializing hung off uses for User");
61   }
62
63   /// \brief Allocate the array of Uses, followed by a pointer
64   /// (with bottom bit set) to the User.
65   /// \param IsPhi identifies callers which are phi nodes and which need
66   /// N BasicBlock* allocated along with N
67   void allocHungoffUses(unsigned N, bool IsPhi = false);
68
69   /// \brief Grow the number of hung off uses.  Note that allocHungoffUses
70   /// should be called if there are no uses.
71   void growHungoffUses(unsigned N, bool IsPhi = false);
72
73 public:
74   ~User() override {
75   }
76   /// \brief Free memory allocated for User and Use objects.
77   void operator delete(void *Usr);
78   /// \brief Placement delete - required by std, but never called.
79   void operator delete(void*, unsigned) {
80     llvm_unreachable("Constructor throws?");
81   }
82   /// \brief Placement delete - required by std, but never called.
83   void operator delete(void*, unsigned, bool) {
84     llvm_unreachable("Constructor throws?");
85   }
86 protected:
87   template <int Idx, typename U> static Use &OpFrom(const U *that) {
88     return Idx < 0
89       ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
90       : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
91   }
92   template <int Idx> Use &Op() {
93     return OpFrom<Idx>(this);
94   }
95   template <int Idx> const Use &Op() const {
96     return OpFrom<Idx>(this);
97   }
98 private:
99   Use *&getHungOffOperands() { return *(reinterpret_cast<Use **>(this) - 1); }
100
101   Use *getIntrusiveOperands() {
102     return reinterpret_cast<Use *>(this) - NumUserOperands;
103   }
104
105   void setOperandList(Use *NewList) {
106     assert(HasHungOffUses &&
107            "Setting operand list only required for hung off uses");
108     getHungOffOperands() = NewList;
109   }
110 public:
111   Use *getOperandList() {
112     return HasHungOffUses ? getHungOffOperands() : getIntrusiveOperands();
113   }
114   const Use *getOperandList() const {
115     return const_cast<User *>(this)->getOperandList();
116   }
117   Value *getOperand(unsigned i) const {
118     assert(i < NumUserOperands && "getOperand() out of range!");
119     return getOperandList()[i];
120   }
121   void setOperand(unsigned i, Value *Val) {
122     assert(i < NumUserOperands && "setOperand() out of range!");
123     assert((!isa<Constant>((const Value*)this) ||
124             isa<GlobalValue>((const Value*)this)) &&
125            "Cannot mutate a constant with setOperand!");
126     getOperandList()[i] = Val;
127   }
128   const Use &getOperandUse(unsigned i) const {
129     assert(i < NumUserOperands && "getOperandUse() out of range!");
130     return getOperandList()[i];
131   }
132   Use &getOperandUse(unsigned i) {
133     assert(i < NumUserOperands && "getOperandUse() out of range!");
134     return getOperandList()[i];
135   }
136
137   unsigned getNumOperands() const { return NumUserOperands; }
138
139   /// Set the number of operands on a GlobalVariable.
140   ///
141   /// GlobalVariable always allocates space for a single operands, but
142   /// doesn't always use it.
143   ///
144   /// FIXME: As that the number of operands is used to find the start of
145   /// the allocated memory in operator delete, we need to always think we have
146   /// 1 operand before delete.
147   void setGlobalVariableNumOperands(unsigned NumOps) {
148     assert(NumOps <= 1 && "GlobalVariable can only have 0 or 1 operands");
149     NumUserOperands = NumOps;
150   }
151
152   /// \brief Subclasses with hung off uses need to manage the operand count
153   /// themselves.  In these instances, the operand count isn't used to find the
154   /// OperandList, so there's no issue in having the operand count change.
155   void setNumHungOffUseOperands(unsigned NumOps) {
156     assert(HasHungOffUses && "Must have hung off uses to use this method");
157     assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
158     NumUserOperands = NumOps;
159   }
160
161   // ---------------------------------------------------------------------------
162   // Operand Iterator interface...
163   //
164   typedef Use*       op_iterator;
165   typedef const Use* const_op_iterator;
166   typedef iterator_range<op_iterator> op_range;
167   typedef iterator_range<const_op_iterator> const_op_range;
168
169   op_iterator       op_begin()       { return getOperandList(); }
170   const_op_iterator op_begin() const { return getOperandList(); }
171   op_iterator       op_end()         {
172     return getOperandList() + NumUserOperands;
173   }
174   const_op_iterator op_end()   const {
175     return getOperandList() + NumUserOperands;
176   }
177   op_range operands() {
178     return op_range(op_begin(), op_end());
179   }
180   const_op_range operands() const {
181     return const_op_range(op_begin(), op_end());
182   }
183
184   /// \brief Iterator for directly iterating over the operand Values.
185   struct value_op_iterator
186       : iterator_adaptor_base<value_op_iterator, op_iterator,
187                               std::random_access_iterator_tag, Value *,
188                               ptrdiff_t, Value *, Value *> {
189     explicit value_op_iterator(Use *U = nullptr) : iterator_adaptor_base(U) {}
190
191     Value *operator*() const { return *I; }
192     Value *operator->() const { return operator*(); }
193   };
194
195   value_op_iterator value_op_begin() {
196     return value_op_iterator(op_begin());
197   }
198   value_op_iterator value_op_end() {
199     return value_op_iterator(op_end());
200   }
201   iterator_range<value_op_iterator> operand_values() {
202     return iterator_range<value_op_iterator>(value_op_begin(), value_op_end());
203   }
204
205   /// \brief Drop all references to operands.
206   ///
207   /// This function is in charge of "letting go" of all objects that this User
208   /// refers to.  This allows one to 'delete' a whole class at a time, even
209   /// though there may be circular references...  First all references are
210   /// dropped, and all use counts go to zero.  Then everything is deleted for
211   /// real.  Note that no operations are valid on an object that has "dropped
212   /// all references", except operator delete.
213   void dropAllReferences() {
214     for (Use &U : operands())
215       U.set(nullptr);
216   }
217
218   /// \brief Replace uses of one Value with another.
219   ///
220   /// Replaces all references to the "From" definition with references to the
221   /// "To" definition.
222   void replaceUsesOfWith(Value *From, Value *To);
223
224   // Methods for support type inquiry through isa, cast, and dyn_cast:
225   static inline bool classof(const Value *V) {
226     return isa<Instruction>(V) || isa<Constant>(V);
227   }
228 };
229
230 template<> struct simplify_type<User::op_iterator> {
231   typedef Value* SimpleType;
232   static SimpleType getSimplifiedValue(User::op_iterator &Val) {
233     return Val->get();
234   }
235 };
236 template<> struct simplify_type<User::const_op_iterator> {
237   typedef /*const*/ Value* SimpleType;
238   static SimpleType getSimplifiedValue(User::const_op_iterator &Val) {
239     return Val->get();
240   }
241 };
242
243 } // End llvm namespace
244
245 #endif