Rename NumOperands to make it clear its managed by the 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   void *operator new(size_t) = delete;
38   template <unsigned>
39   friend struct HungoffOperandTraits;
40   virtual void anchor();
41 protected:
42   /// \brief This is a pointer to the array of Uses for this User.
43   ///
44   /// For nodes of fixed arity (e.g. a binary operator) this array will live
45   /// prefixed to some derived class instance.  For nodes of resizable variable
46   /// arity (e.g. PHINodes, SwitchInst etc.), this memory will be dynamically
47   /// allocated and should be destroyed by the classes' virtual dtor.
48   Use *LegacyOperandList;
49
50 protected:
51   void *operator new(size_t s, unsigned Us);
52
53   User(Type *ty, unsigned vty, Use *OpList, unsigned NumOps)
54       : Value(ty, vty) {
55     setOperandList(OpList);
56     assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
57     NumUserOperands = NumOps;
58   }
59
60   /// \brief Allocate the array of Uses, followed by a pointer
61   /// (with bottom bit set) to the User.
62   /// \param IsPhi identifies callers which are phi nodes and which need
63   /// N BasicBlock* allocated along with N
64   void allocHungoffUses(unsigned N, bool IsPhi = false);
65
66   /// \brief Grow the number of hung off uses.  Note that allocHungoffUses
67   /// should be called if there are no uses.
68   void growHungoffUses(unsigned N, bool IsPhi = false);
69
70 public:
71   ~User() override {
72     // drop the hung off uses.
73     Use::zap(getOperandList(), getOperandList() + NumUserOperands,
74              HasHungOffUses);
75     if (HasHungOffUses) {
76       setOperandList(nullptr);
77       // Reset NumOperands so User::operator delete() does the right thing.
78       NumUserOperands = 0;
79     }
80   }
81   /// \brief Free memory allocated for User and Use objects.
82   void operator delete(void *Usr);
83   /// \brief Placement delete - required by std, but never called.
84   void operator delete(void*, unsigned) {
85     llvm_unreachable("Constructor throws?");
86   }
87   /// \brief Placement delete - required by std, but never called.
88   void operator delete(void*, unsigned, bool) {
89     llvm_unreachable("Constructor throws?");
90   }
91 protected:
92   template <int Idx, typename U> static Use &OpFrom(const U *that) {
93     return Idx < 0
94       ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
95       : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
96   }
97   template <int Idx> Use &Op() {
98     return OpFrom<Idx>(this);
99   }
100   template <int Idx> const Use &Op() const {
101     return OpFrom<Idx>(this);
102   }
103 private:
104   void setOperandList(Use *NewList) {
105     LegacyOperandList = NewList;
106   }
107 public:
108   Use *getOperandList() const {
109     return LegacyOperandList;
110   }
111   Value *getOperand(unsigned i) const {
112     assert(i < NumUserOperands && "getOperand() out of range!");
113     return getOperandList()[i];
114   }
115   void setOperand(unsigned i, Value *Val) {
116     assert(i < NumUserOperands && "setOperand() out of range!");
117     assert((!isa<Constant>((const Value*)this) ||
118             isa<GlobalValue>((const Value*)this)) &&
119            "Cannot mutate a constant with setOperand!");
120     getOperandList()[i] = Val;
121   }
122   const Use &getOperandUse(unsigned i) const {
123     assert(i < NumUserOperands && "getOperandUse() out of range!");
124     return getOperandList()[i];
125   }
126   Use &getOperandUse(unsigned i) {
127     assert(i < NumUserOperands && "getOperandUse() out of range!");
128     return getOperandList()[i];
129   }
130
131   unsigned getNumOperands() const { return NumUserOperands; }
132
133   /// Set the number of operands on a GlobalVariable.
134   ///
135   /// GlobalVariable always allocates space for a single operands, but
136   /// doesn't always use it.
137   ///
138   /// FIXME: As that the number of operands is used to find the start of
139   /// the allocated memory in operator delete, we need to always think we have
140   /// 1 operand before delete.
141   void setGlobalVariableNumOperands(unsigned NumOps) {
142     assert(NumOps <= 1 && "GlobalVariable can only have 0 or 1 operands");
143     NumUserOperands = NumOps;
144   }
145
146   /// \brief Subclasses with hung off uses need to manage the operand count
147   /// themselves.  In these instances, the operand count isn't used to find the
148   /// OperandList, so there's no issue in having the operand count change.
149   void setNumHungOffUseOperands(unsigned NumOps) {
150     assert(HasHungOffUses && "Must have hung off uses to use this method");
151     assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
152     NumUserOperands = NumOps;
153   }
154
155   // ---------------------------------------------------------------------------
156   // Operand Iterator interface...
157   //
158   typedef Use*       op_iterator;
159   typedef const Use* const_op_iterator;
160   typedef iterator_range<op_iterator> op_range;
161   typedef iterator_range<const_op_iterator> const_op_range;
162
163   inline op_iterator       op_begin()       { return getOperandList(); }
164   inline const_op_iterator op_begin() const { return getOperandList(); }
165   inline op_iterator       op_end()         {
166     return getOperandList() + NumUserOperands;
167   }
168   inline const_op_iterator op_end()   const {
169     return getOperandList() + NumUserOperands;
170   }
171   inline op_range operands() {
172     return op_range(op_begin(), op_end());
173   }
174   inline const_op_range operands() const {
175     return const_op_range(op_begin(), op_end());
176   }
177
178   /// \brief Iterator for directly iterating over the operand Values.
179   struct value_op_iterator
180       : iterator_adaptor_base<value_op_iterator, op_iterator,
181                               std::random_access_iterator_tag, Value *,
182                               ptrdiff_t, Value *, Value *> {
183     explicit value_op_iterator(Use *U = nullptr) : iterator_adaptor_base(U) {}
184
185     Value *operator*() const { return *I; }
186     Value *operator->() const { return operator*(); }
187   };
188
189   inline value_op_iterator value_op_begin() {
190     return value_op_iterator(op_begin());
191   }
192   inline value_op_iterator value_op_end() {
193     return value_op_iterator(op_end());
194   }
195   inline iterator_range<value_op_iterator> operand_values() {
196     return iterator_range<value_op_iterator>(value_op_begin(), value_op_end());
197   }
198
199   /// \brief Drop all references to operands.
200   ///
201   /// This function is in charge of "letting go" of all objects that this User
202   /// refers to.  This allows one to 'delete' a whole class at a time, even
203   /// though there may be circular references...  First all references are
204   /// dropped, and all use counts go to zero.  Then everything is deleted for
205   /// real.  Note that no operations are valid on an object that has "dropped
206   /// all references", except operator delete.
207   void dropAllReferences() {
208     for (Use &U : operands())
209       U.set(nullptr);
210   }
211
212   /// \brief Replace uses of one Value with another.
213   ///
214   /// Replaces all references to the "From" definition with references to the
215   /// "To" definition.
216   void replaceUsesOfWith(Value *From, Value *To);
217
218   // Methods for support type inquiry through isa, cast, and dyn_cast:
219   static inline bool classof(const Value *V) {
220     return isa<Instruction>(V) || isa<Constant>(V);
221   }
222 };
223
224 template<> struct simplify_type<User::op_iterator> {
225   typedef Value* SimpleType;
226   static SimpleType getSimplifiedValue(User::op_iterator &Val) {
227     return Val->get();
228   }
229 };
230 template<> struct simplify_type<User::const_op_iterator> {
231   typedef /*const*/ Value* SimpleType;
232   static SimpleType getSimplifiedValue(User::const_op_iterator &Val) {
233     return Val->get();
234   }
235 };
236
237 } // End llvm namespace
238
239 #endif