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"
27 class User : public Value {
28 User(const User &); // Do not implement
30 /// OperandList - This is a pointer to the array of Users for this operand.
31 /// For nodes of fixed arity (e.g. a binary operator) this array will live
32 /// embedded into the derived class. For nodes of variable arity
33 /// (e.g. ConstantArrays, CallInst, PHINodes, etc), this memory will be
34 /// dynamically allocated and should be destroyed by the classes virtual dtor.
37 /// NumOperands - The number of values used by this User.
42 User(const Type *Ty, unsigned vty, Use *OpList, unsigned NumOps,
43 const std::string &name = "")
44 : Value(Ty, vty, name), OperandList(OpList), NumOperands(NumOps) {}
46 Value *getOperand(unsigned i) const {
47 assert(i < NumOperands && "getOperand() out of range!");
48 return OperandList[i];
50 void setOperand(unsigned i, Value *Val) {
51 assert(i < NumOperands && "setOperand() out of range!");
54 unsigned getNumOperands() const { return NumOperands; }
56 // ---------------------------------------------------------------------------
57 // Operand Iterator interface...
59 typedef Use* op_iterator;
60 typedef const Use* const_op_iterator;
62 inline op_iterator op_begin() { return OperandList; }
63 inline const_op_iterator op_begin() const { return OperandList; }
64 inline op_iterator op_end() { return OperandList+NumOperands; }
65 inline const_op_iterator op_end() const { return OperandList+NumOperands; }
67 // dropAllReferences() - This function is in charge of "letting go" of all
68 // objects that this User refers to. This allows one to
69 // 'delete' a whole class at a time, even though there may be circular
70 // references... first all references are dropped, and all use counts go to
71 // zero. Then everything is delete'd for real. Note that no operations are
72 // valid on an object that has "dropped all references", except operator
75 void dropAllReferences() {
76 Use *OL = OperandList;
77 for (unsigned i = 0, e = NumOperands; i != e; ++i)
81 /// replaceUsesOfWith - Replaces all references to the "From" definition with
82 /// references to the "To" definition.
84 void replaceUsesOfWith(Value *From, Value *To);
86 // Methods for support type inquiry through isa, cast, and dyn_cast:
87 static inline bool classof(const User *) { return true; }
88 static inline bool classof(const Value *V) {
89 return isa<Instruction>(V) || isa<Constant>(V);
93 template<> struct simplify_type<User::op_iterator> {
94 typedef Value* SimpleType;
96 static SimpleType getSimplifiedValue(const User::op_iterator &Val) {
97 return static_cast<SimpleType>(Val->get());
101 template<> struct simplify_type<const User::op_iterator>
102 : public simplify_type<User::op_iterator> {};
104 template<> struct simplify_type<User::const_op_iterator> {
105 typedef Value* SimpleType;
107 static SimpleType getSimplifiedValue(const User::const_op_iterator &Val) {
108 return static_cast<SimpleType>(Val->get());
112 template<> struct simplify_type<const User::const_op_iterator>
113 : public simplify_type<User::const_op_iterator> {};
116 // value_use_iterator::getOperandNo - Requires the definition of the User class.
117 template<typename UserTy>
118 unsigned value_use_iterator<UserTy>::getOperandNo() const {
119 return U - U->getUser()->op_begin();
122 } // End llvm namespace