Move the special Phi logic for hung off uses in to User::allocHungOffUses. 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 *OperandList;
49
50   void *operator new(size_t s, unsigned Us);
51   User(Type *ty, unsigned vty, Use *OpList, unsigned NumOps)
52       : Value(ty, vty), OperandList(OpList) {
53     NumOperands = NumOps;
54   }
55
56   /// \brief Allocate the array of Uses, followed by a pointer
57   /// (with bottom bit set) to the User.
58   /// \param IsPhi identifies callers which are phi nodes and which need
59   /// N BasicBlock* allocated along with N
60   Use *allocHungoffUses(unsigned N, bool IsPhi = false) const;
61   void dropHungoffUses() {
62     Use::zap(OperandList, OperandList + NumOperands, true);
63     OperandList = nullptr;
64     // Reset NumOperands so User::operator delete() does the right thing.
65     NumOperands = 0;
66   }
67 public:
68   ~User() override { Use::zap(OperandList, OperandList + NumOperands); }
69   /// \brief Free memory allocated for User and Use objects.
70   void operator delete(void *Usr);
71   /// \brief Placement delete - required by std, but never called.
72   void operator delete(void*, unsigned) {
73     llvm_unreachable("Constructor throws?");
74   }
75   /// \brief Placement delete - required by std, but never called.
76   void operator delete(void*, unsigned, bool) {
77     llvm_unreachable("Constructor throws?");
78   }
79 protected:
80   template <int Idx, typename U> static Use &OpFrom(const U *that) {
81     return Idx < 0
82       ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
83       : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
84   }
85   template <int Idx> Use &Op() {
86     return OpFrom<Idx>(this);
87   }
88   template <int Idx> const Use &Op() const {
89     return OpFrom<Idx>(this);
90   }
91 public:
92   Value *getOperand(unsigned i) const {
93     assert(i < NumOperands && "getOperand() out of range!");
94     return OperandList[i];
95   }
96   void setOperand(unsigned i, Value *Val) {
97     assert(i < NumOperands && "setOperand() out of range!");
98     assert((!isa<Constant>((const Value*)this) ||
99             isa<GlobalValue>((const Value*)this)) &&
100            "Cannot mutate a constant with setOperand!");
101     OperandList[i] = Val;
102   }
103   const Use &getOperandUse(unsigned i) const {
104     assert(i < NumOperands && "getOperandUse() out of range!");
105     return OperandList[i];
106   }
107   Use &getOperandUse(unsigned i) {
108     assert(i < NumOperands && "getOperandUse() out of range!");
109     return OperandList[i];
110   }
111
112   unsigned getNumOperands() const { return NumOperands; }
113
114   // ---------------------------------------------------------------------------
115   // Operand Iterator interface...
116   //
117   typedef Use*       op_iterator;
118   typedef const Use* const_op_iterator;
119   typedef iterator_range<op_iterator> op_range;
120   typedef iterator_range<const_op_iterator> const_op_range;
121
122   inline op_iterator       op_begin()       { return OperandList; }
123   inline const_op_iterator op_begin() const { return OperandList; }
124   inline op_iterator       op_end()         { return OperandList+NumOperands; }
125   inline const_op_iterator op_end()   const { return OperandList+NumOperands; }
126   inline op_range operands() {
127     return op_range(op_begin(), op_end());
128   }
129   inline const_op_range operands() const {
130     return const_op_range(op_begin(), op_end());
131   }
132
133   /// \brief Iterator for directly iterating over the operand Values.
134   struct value_op_iterator
135       : iterator_adaptor_base<value_op_iterator, op_iterator,
136                               std::random_access_iterator_tag, Value *,
137                               ptrdiff_t, Value *, Value *> {
138     explicit value_op_iterator(Use *U = nullptr) : iterator_adaptor_base(U) {}
139
140     Value *operator*() const { return *I; }
141     Value *operator->() const { return operator*(); }
142   };
143
144   inline value_op_iterator value_op_begin() {
145     return value_op_iterator(op_begin());
146   }
147   inline value_op_iterator value_op_end() {
148     return value_op_iterator(op_end());
149   }
150   inline iterator_range<value_op_iterator> operand_values() {
151     return iterator_range<value_op_iterator>(value_op_begin(), value_op_end());
152   }
153
154   /// \brief Drop all references to operands.
155   ///
156   /// This function is in charge of "letting go" of all objects that this User
157   /// refers to.  This allows one to 'delete' a whole class at a time, even
158   /// though there may be circular references...  First all references are
159   /// dropped, and all use counts go to zero.  Then everything is deleted for
160   /// real.  Note that no operations are valid on an object that has "dropped
161   /// all references", except operator delete.
162   void dropAllReferences() {
163     for (Use &U : operands())
164       U.set(nullptr);
165   }
166
167   /// \brief Replace uses of one Value with another.
168   ///
169   /// Replaces all references to the "From" definition with references to the
170   /// "To" definition.
171   void replaceUsesOfWith(Value *From, Value *To);
172
173   // Methods for support type inquiry through isa, cast, and dyn_cast:
174   static inline bool classof(const Value *V) {
175     return isa<Instruction>(V) || isa<Constant>(V);
176   }
177 };
178
179 template<> struct simplify_type<User::op_iterator> {
180   typedef Value* SimpleType;
181   static SimpleType getSimplifiedValue(User::op_iterator &Val) {
182     return Val->get();
183   }
184 };
185 template<> struct simplify_type<User::const_op_iterator> {
186   typedef /*const*/ Value* SimpleType;
187   static SimpleType getSimplifiedValue(User::const_op_iterator &Val) {
188     return Val->get();
189   }
190 };
191
192 } // End llvm namespace
193
194 #endif