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, ReturnInst etc), this memory
33 /// will be dynamically allocated and should be destroyed by the classes
37 /// NumOperands - The number of values used by this User.
42 User(const Type *Ty, unsigned vty, Use *OpList, unsigned NumOps)
43 : Value(Ty, vty), OperandList(OpList), NumOperands(NumOps) {}
45 Value *getOperand(unsigned i) const {
46 assert(i < NumOperands && "getOperand() out of range!");
47 return OperandList[i];
49 void setOperand(unsigned i, Value *Val) {
50 assert(i < NumOperands && "setOperand() out of range!");
53 unsigned getNumOperands() const { return NumOperands; }
55 // ---------------------------------------------------------------------------
56 // Operand Iterator interface...
58 typedef Use* op_iterator;
59 typedef const Use* const_op_iterator;
61 inline op_iterator op_begin() { return OperandList; }
62 inline const_op_iterator op_begin() const { return OperandList; }
63 inline op_iterator op_end() { return OperandList+NumOperands; }
64 inline const_op_iterator op_end() const { return OperandList+NumOperands; }
66 // dropAllReferences() - This function is in charge of "letting go" of all
67 // objects that this User refers to. This allows one to
68 // 'delete' a whole class at a time, even though there may be circular
69 // references... first all references are dropped, and all use counts go to
70 // zero. Then everything is delete'd for real. Note that no operations are
71 // valid on an object that has "dropped all references", except operator
74 void dropAllReferences() {
75 Use *OL = OperandList;
76 for (unsigned i = 0, e = NumOperands; i != e; ++i)
80 /// replaceUsesOfWith - Replaces all references to the "From" definition with
81 /// references to the "To" definition.
83 void replaceUsesOfWith(Value *From, Value *To);
85 // Methods for support type inquiry through isa, cast, and dyn_cast:
86 static inline bool classof(const User *) { return true; }
87 static inline bool classof(const Value *V) {
88 return isa<Instruction>(V) || isa<Constant>(V);
92 template<> struct simplify_type<User::op_iterator> {
93 typedef Value* SimpleType;
95 static SimpleType getSimplifiedValue(const User::op_iterator &Val) {
96 return static_cast<SimpleType>(Val->get());
100 template<> struct simplify_type<const User::op_iterator>
101 : public simplify_type<User::op_iterator> {};
103 template<> struct simplify_type<User::const_op_iterator> {
104 typedef Value* SimpleType;
106 static SimpleType getSimplifiedValue(const User::const_op_iterator &Val) {
107 return static_cast<SimpleType>(Val->get());
111 template<> struct simplify_type<const User::const_op_iterator>
112 : public simplify_type<User::const_op_iterator> {};
115 // value_use_iterator::getOperandNo - Requires the definition of the User class.
116 template<typename UserTy>
117 unsigned value_use_iterator<UserTy>::getOperandNo() const {
118 return U - U->getUser()->op_begin();
121 } // End llvm namespace