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