1 //===-- llvm/User.h - User class definition ---------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 class User : public Value {
27 User(const User &); // Do not implement
29 /// OperandList - This is a pointer to the array of Users for this operand.
30 /// For nodes of fixed arity (e.g. a binary operator) this array will live
31 /// embedded into the derived class. For nodes of variable arity
32 /// (e.g. ConstantArrays, CallInst, PHINodes, etc), this memory will be
33 /// dynamically allocated and should be destroyed by the classes virtual dtor.
36 /// NumOperands - The number of values used by this User.
40 static void destroyThis(User*v) {
41 Value::destroyThis(v);
45 User(const Type *Ty, unsigned vty, Use *OpList, unsigned NumOps)
46 : Value(Ty, vty), OperandList(OpList), NumOperands(NumOps) {}
48 Value *getOperand(unsigned i) const {
49 assert(i < NumOperands && "getOperand() out of range!");
50 return OperandList[i];
52 void setOperand(unsigned i, Value *Val) {
53 assert(i < NumOperands && "setOperand() out of range!");
56 unsigned getNumOperands() const { return NumOperands; }
58 // ---------------------------------------------------------------------------
59 // Operand Iterator interface...
61 typedef Use* op_iterator;
62 typedef const Use* const_op_iterator;
64 inline op_iterator op_begin() { return OperandList; }
65 inline const_op_iterator op_begin() const { return OperandList; }
66 inline op_iterator op_end() { return OperandList+NumOperands; }
67 inline const_op_iterator op_end() const { return OperandList+NumOperands; }
69 // dropAllReferences() - This function is in charge of "letting go" of all
70 // objects that this User refers to. This allows one to
71 // 'delete' a whole class at a time, even though there may be circular
72 // references... first all references are dropped, and all use counts go to
73 // zero. Then everything is delete'd for real. Note that no operations are
74 // valid on an object that has "dropped all references", except operator
77 void dropAllReferences() {
78 Use *OL = OperandList;
79 for (unsigned i = 0, e = NumOperands; i != e; ++i)
83 /// replaceUsesOfWith - Replaces all references to the "From" definition with
84 /// references to the "To" definition.
86 void replaceUsesOfWith(Value *From, Value *To);
88 // Methods for support type inquiry through isa, cast, and dyn_cast:
89 static inline bool classof(const User *) { return true; }
90 static inline bool classof(const Value *V) {
91 return isa<Instruction>(V) || isa<Constant>(V);
95 template<> struct simplify_type<User::op_iterator> {
96 typedef Value* SimpleType;
98 static SimpleType getSimplifiedValue(const User::op_iterator &Val) {
99 return static_cast<SimpleType>(Val->get());
103 template<> struct simplify_type<const User::op_iterator>
104 : public simplify_type<User::op_iterator> {};
106 template<> struct simplify_type<User::const_op_iterator> {
107 typedef Value* SimpleType;
109 static SimpleType getSimplifiedValue(const User::const_op_iterator &Val) {
110 return static_cast<SimpleType>(Val->get());
114 template<> struct simplify_type<const User::const_op_iterator>
115 : public simplify_type<User::const_op_iterator> {};
118 // value_use_iterator::getOperandNo - Requires the definition of the User class.
119 template<typename UserTy>
120 unsigned value_use_iterator<UserTy>::getOperandNo() const {
121 return U - U->getUser()->op_begin();
124 } // End llvm namespace