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