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