0f1dcfe7a0947f0869a5b46ae6ac02855b0299fc
[oota-llvm.git] / include / llvm / User.h
1 //===-- llvm/User.h - User class definition ---------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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
12 // to it.
13 //
14 //  * Instructions are the largest class of User's.
15 //  * Constants may be users of other constants (think arrays and stuff)
16 //
17 //===----------------------------------------------------------------------===//
18
19 #ifndef LLVM_USER_H
20 #define LLVM_USER_H
21
22 #include "llvm/Value.h"
23
24 namespace llvm {
25
26 class User : public Value {
27   User(const User &);             // Do not implement
28 protected:
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 
34   /// virtual dtor.
35   Use *OperandList;
36
37   /// NumOperands - The number of values used by this User.
38   ///
39   unsigned NumOperands;
40
41 public:
42   User(const Type *Ty, unsigned vty, Use *OpList, unsigned NumOps)
43     : Value(Ty, vty), OperandList(OpList), NumOperands(NumOps) {}
44
45   Value *getOperand(unsigned i) const {
46     assert(i < NumOperands && "getOperand() out of range!");
47     return OperandList[i];
48   }
49   void setOperand(unsigned i, Value *Val) {
50     assert(i < NumOperands && "setOperand() out of range!");
51     OperandList[i] = Val;
52   }
53   unsigned getNumOperands() const { return NumOperands; }
54
55   // ---------------------------------------------------------------------------
56   // Operand Iterator interface...
57   //
58   typedef Use*       op_iterator;
59   typedef const Use* const_op_iterator;
60
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; }
65
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
72   // delete.
73   //
74   void dropAllReferences() {
75     Use *OL = OperandList;
76     for (unsigned i = 0, e = NumOperands; i != e; ++i)
77       OL[i].set(0);
78   }
79
80   /// replaceUsesOfWith - Replaces all references to the "From" definition with
81   /// references to the "To" definition.
82   ///
83   void replaceUsesOfWith(Value *From, Value *To);
84
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);
89   }
90 };
91
92 template<> struct simplify_type<User::op_iterator> {
93   typedef Value* SimpleType;
94
95   static SimpleType getSimplifiedValue(const User::op_iterator &Val) {
96     return static_cast<SimpleType>(Val->get());
97   }
98 };
99
100 template<> struct simplify_type<const User::op_iterator>
101   : public simplify_type<User::op_iterator> {};
102
103 template<> struct simplify_type<User::const_op_iterator> {
104   typedef Value* SimpleType;
105
106   static SimpleType getSimplifiedValue(const User::const_op_iterator &Val) {
107     return static_cast<SimpleType>(Val->get());
108   }
109 };
110
111 template<> struct simplify_type<const User::const_op_iterator>
112   : public simplify_type<User::const_op_iterator> {};
113
114
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();
119 }
120
121 } // End llvm namespace
122
123 #endif