1 //===-- llvm/User.h - User class definition ---------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
14 // * Instructions are the largest class of Users.
15 // * Constants may be users of other constants (think arrays and stuff)
17 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_IR_USER_H
20 #define LLVM_IR_USER_H
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"
29 /// \brief Compile-time customization of User operands.
31 /// Customizes operand-related allocators and accessors.
35 class User : public Value {
36 User(const User &) = delete;
37 void *operator new(size_t) = delete;
39 friend struct HungoffOperandTraits;
40 virtual void anchor();
42 /// \brief This is a pointer to the array of Uses for this User.
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.
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) {
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);
62 /// \brief Grow the number of hung off uses. Note that allocHungoffUses
63 /// should be called if there are no uses.
64 void growHungoffUses(unsigned N, bool IsPhi = false);
68 // drop the hung off uses.
69 Use::zap(OperandList, OperandList + NumOperands, HasHungOffUses);
71 OperandList = nullptr;
72 // Reset NumOperands so User::operator delete() does the right thing.
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?");
82 /// \brief Placement delete - required by std, but never called.
83 void operator delete(void*, unsigned, bool) {
84 llvm_unreachable("Constructor throws?");
87 template <int Idx, typename U> static Use &OpFrom(const U *that) {
89 ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
90 : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
92 template <int Idx> Use &Op() {
93 return OpFrom<Idx>(this);
95 template <int Idx> const Use &Op() const {
96 return OpFrom<Idx>(this);
99 Value *getOperand(unsigned i) const {
100 assert(i < NumOperands && "getOperand() out of range!");
101 return OperandList[i];
103 void setOperand(unsigned i, Value *Val) {
104 assert(i < NumOperands && "setOperand() out of range!");
105 assert((!isa<Constant>((const Value*)this) ||
106 isa<GlobalValue>((const Value*)this)) &&
107 "Cannot mutate a constant with setOperand!");
108 OperandList[i] = Val;
110 const Use &getOperandUse(unsigned i) const {
111 assert(i < NumOperands && "getOperandUse() out of range!");
112 return OperandList[i];
114 Use &getOperandUse(unsigned i) {
115 assert(i < NumOperands && "getOperandUse() out of range!");
116 return OperandList[i];
119 unsigned getNumOperands() const { return NumOperands; }
121 // ---------------------------------------------------------------------------
122 // Operand Iterator interface...
124 typedef Use* op_iterator;
125 typedef const Use* const_op_iterator;
126 typedef iterator_range<op_iterator> op_range;
127 typedef iterator_range<const_op_iterator> const_op_range;
129 inline op_iterator op_begin() { return OperandList; }
130 inline const_op_iterator op_begin() const { return OperandList; }
131 inline op_iterator op_end() { return OperandList+NumOperands; }
132 inline const_op_iterator op_end() const { return OperandList+NumOperands; }
133 inline op_range operands() {
134 return op_range(op_begin(), op_end());
136 inline const_op_range operands() const {
137 return const_op_range(op_begin(), op_end());
140 /// \brief Iterator for directly iterating over the operand Values.
141 struct value_op_iterator
142 : iterator_adaptor_base<value_op_iterator, op_iterator,
143 std::random_access_iterator_tag, Value *,
144 ptrdiff_t, Value *, Value *> {
145 explicit value_op_iterator(Use *U = nullptr) : iterator_adaptor_base(U) {}
147 Value *operator*() const { return *I; }
148 Value *operator->() const { return operator*(); }
151 inline value_op_iterator value_op_begin() {
152 return value_op_iterator(op_begin());
154 inline value_op_iterator value_op_end() {
155 return value_op_iterator(op_end());
157 inline iterator_range<value_op_iterator> operand_values() {
158 return iterator_range<value_op_iterator>(value_op_begin(), value_op_end());
161 /// \brief Drop all references to operands.
163 /// This function is in charge of "letting go" of all objects that this User
164 /// refers to. This allows one to 'delete' a whole class at a time, even
165 /// though there may be circular references... First all references are
166 /// dropped, and all use counts go to zero. Then everything is deleted for
167 /// real. Note that no operations are valid on an object that has "dropped
168 /// all references", except operator delete.
169 void dropAllReferences() {
170 for (Use &U : operands())
174 /// \brief Replace uses of one Value with another.
176 /// Replaces all references to the "From" definition with references to the
178 void replaceUsesOfWith(Value *From, Value *To);
180 // Methods for support type inquiry through isa, cast, and dyn_cast:
181 static inline bool classof(const Value *V) {
182 return isa<Instruction>(V) || isa<Constant>(V);
186 template<> struct simplify_type<User::op_iterator> {
187 typedef Value* SimpleType;
188 static SimpleType getSimplifiedValue(User::op_iterator &Val) {
192 template<> struct simplify_type<User::const_op_iterator> {
193 typedef /*const*/ Value* SimpleType;
194 static SimpleType getSimplifiedValue(User::const_op_iterator &Val) {
199 } // End llvm namespace