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 'use's 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 User's.
15 // * Constants may be users of other constants (think arrays and stuff)
17 //===----------------------------------------------------------------------===//
22 #include "llvm/Value.h"
26 /// OperandTraits - Compile-time customization of
27 /// operand-related allocators and accessors
28 /// for use of the User class
34 /// OperandTraits<User> - specialization to User
36 struct OperandTraits<User> {
37 static inline Use *op_begin(User*);
38 static inline Use *op_end(User*);
39 static inline unsigned operands(const User*);
46 class User : public Value {
47 User(const User &); // Do not implement
48 void *operator new(size_t); // Do not implement
50 friend struct HungoffOperandTraits;
52 /// OperandList - This is a pointer to the array of Uses for this User.
53 /// For nodes of fixed arity (e.g. a binary operator) this array will live
54 /// prefixed to some derived class instance. For nodes of resizable variable
55 /// arity (e.g. PHINodes, SwitchInst etc.), this memory will be dynamically
56 /// allocated and should be destroyed by the classes' virtual dtor.
59 /// NumOperands - The number of values used by this User.
63 void *operator new(size_t s, unsigned Us);
64 User(const Type *ty, unsigned vty, Use *OpList, unsigned NumOps)
65 : Value(ty, vty), OperandList(OpList), NumOperands(NumOps) {}
66 Use *allocHungoffUses(unsigned) const;
67 void dropHungoffUses(Use *U) {
68 if (OperandList == U) {
72 Use::zap(U, U->getImpliedUser(), true);
76 Use::zap(OperandList, OperandList + NumOperands);
78 /// operator delete - free memory allocated for User and Use objects
79 void operator delete(void *Usr);
80 /// placement delete - required by std, but never called.
81 void operator delete(void*, unsigned) {
82 assert(0 && "Constructor throws?");
84 /// placement delete - required by std, but never called.
85 void operator delete(void*, unsigned, bool) {
86 assert(0 && "Constructor throws?");
89 template <int Idx, typename U> static Use &OpFrom(const U *that) {
91 ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
92 : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
94 template <int Idx> Use &Op() {
95 return OpFrom<Idx>(this);
97 template <int Idx> const Use &Op() const {
98 return OpFrom<Idx>(this);
101 Value *getOperand(unsigned i) const {
102 assert(i < NumOperands && "getOperand() out of range!");
103 return OperandList[i];
105 void setOperand(unsigned i, Value *Val) {
106 assert(i < NumOperands && "setOperand() out of range!");
107 assert((!isa<Constant>((const Value*)this) ||
108 isa<GlobalValue>((const Value*)this)) &&
109 "Cannot mutate a constant with setOperand!");
110 OperandList[i] = Val;
112 const Use &getOperandUse(unsigned i) const {
113 assert(i < NumOperands && "getOperand() out of range!");
114 return OperandList[i];
116 Use &getOperandUse(unsigned i) {
117 assert(i < NumOperands && "getOperand() out of range!");
118 return OperandList[i];
121 unsigned getNumOperands() const { return NumOperands; }
123 // ---------------------------------------------------------------------------
124 // Operand Iterator interface...
126 typedef Use* op_iterator;
127 typedef const Use* const_op_iterator;
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; }
134 // dropAllReferences() - This function is in charge of "letting go" of all
135 // objects that this User refers to. This allows one to
136 // 'delete' a whole class at a time, even though there may be circular
137 // references... First all references are dropped, and all use counts go to
138 // zero. Then everything is deleted for real. Note that no operations are
139 // valid on an object that has "dropped all references", except operator
142 void dropAllReferences() {
143 for (op_iterator i = op_begin(), e = op_end(); i != e; ++i)
147 /// replaceUsesOfWith - Replaces all references to the "From" definition with
148 /// references to the "To" definition.
150 void replaceUsesOfWith(Value *From, Value *To);
152 // Methods for support type inquiry through isa, cast, and dyn_cast:
153 static inline bool classof(const User *) { return true; }
154 static inline bool classof(const Value *V) {
155 return isa<Instruction>(V) || isa<Constant>(V);
159 inline Use *OperandTraits<User>::op_begin(User *U) {
160 return U->op_begin();
163 inline Use *OperandTraits<User>::op_end(User *U) {
167 inline unsigned OperandTraits<User>::operands(const User *U) {
168 return U->getNumOperands();
171 template<> struct simplify_type<User::op_iterator> {
172 typedef Value* SimpleType;
174 static SimpleType getSimplifiedValue(const User::op_iterator &Val) {
175 return static_cast<SimpleType>(Val->get());
179 template<> struct simplify_type<const User::op_iterator>
180 : public simplify_type<User::op_iterator> {};
182 template<> struct simplify_type<User::const_op_iterator> {
183 typedef Value* SimpleType;
185 static SimpleType getSimplifiedValue(const User::const_op_iterator &Val) {
186 return static_cast<SimpleType>(Val->get());
190 template<> struct simplify_type<const User::const_op_iterator>
191 : public simplify_type<User::const_op_iterator> {};
194 // value_use_iterator::getOperandNo - Requires the definition of the User class.
195 template<typename UserTy>
196 unsigned value_use_iterator<UserTy>::getOperandNo() const {
197 return U - U->getUser()->op_begin();
200 } // End llvm namespace