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 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.
41 User(const Type *Ty, unsigned vty, Use *OpList, unsigned NumOps)
42 : Value(Ty, vty), OperandList(OpList), NumOperands(NumOps) {}
44 Value *getOperand(unsigned i) const {
45 assert(i < NumOperands && "getOperand() out of range!");
46 return OperandList[i];
48 void setOperand(unsigned i, Value *Val) {
49 assert(i < NumOperands && "setOperand() out of range!");
52 unsigned getNumOperands() const { return NumOperands; }
54 // ---------------------------------------------------------------------------
55 // Operand Iterator interface...
57 typedef Use* op_iterator;
58 typedef const Use* const_op_iterator;
60 inline op_iterator op_begin() { return OperandList; }
61 inline const_op_iterator op_begin() const { return OperandList; }
62 inline op_iterator op_end() { return OperandList+NumOperands; }
63 inline const_op_iterator op_end() const { return OperandList+NumOperands; }
65 // dropAllReferences() - This function is in charge of "letting go" of all
66 // objects that this User refers to. This allows one to
67 // 'delete' a whole class at a time, even though there may be circular
68 // references... first all references are dropped, and all use counts go to
69 // zero. Then everything is delete'd for real. Note that no operations are
70 // valid on an object that has "dropped all references", except operator
73 void dropAllReferences() {
74 Use *OL = OperandList;
75 for (unsigned i = 0, e = NumOperands; i != e; ++i)
79 /// replaceUsesOfWith - Replaces all references to the "From" definition with
80 /// references to the "To" definition.
82 void replaceUsesOfWith(Value *From, Value *To);
84 // Methods for support type inquiry through isa, cast, and dyn_cast:
85 static inline bool classof(const User *) { return true; }
86 static inline bool classof(const Value *V) {
87 return isa<Instruction>(V) || isa<Constant>(V);
91 template<> struct simplify_type<User::op_iterator> {
92 typedef Value* SimpleType;
94 static SimpleType getSimplifiedValue(const User::op_iterator &Val) {
95 return static_cast<SimpleType>(Val->get());
99 template<> struct simplify_type<const User::op_iterator>
100 : public simplify_type<User::op_iterator> {};
102 template<> struct simplify_type<User::const_op_iterator> {
103 typedef Value* SimpleType;
105 static SimpleType getSimplifiedValue(const User::const_op_iterator &Val) {
106 return static_cast<SimpleType>(Val->get());
110 template<> struct simplify_type<const User::const_op_iterator>
111 : public simplify_type<User::const_op_iterator> {};
114 // value_use_iterator::getOperandNo - Requires the definition of the User class.
115 template<typename UserTy>
116 unsigned value_use_iterator<UserTy>::getOperandNo() const {
117 return U - U->getUser()->op_begin();
120 } // End llvm namespace