0392e20bda31114ff12e7a15dda3a96043ac6046
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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, etc), this memory will be
33   /// dynamically allocated and should be destroyed by the classes virtual dtor.
34   Use *OperandList;
35
36   /// NumOperands - The number of values used by this User.
37   ///
38   unsigned NumOperands;
39
40   static void destroyThis(User*v) {
41     Value::destroyThis(v);
42   }
43   friend class Value;
44 public:
45   User(const Type *Ty, unsigned vty, Use *OpList, unsigned NumOps)
46     : Value(Ty, vty), OperandList(OpList), NumOperands(NumOps) {}
47
48   Value *getOperand(unsigned i) const {
49     assert(i < NumOperands && "getOperand() out of range!");
50     return OperandList[i];
51   }
52   void setOperand(unsigned i, Value *Val) {
53     assert(i < NumOperands && "setOperand() out of range!");
54     OperandList[i] = Val;
55   }
56   unsigned getNumOperands() const { return NumOperands; }
57
58   // ---------------------------------------------------------------------------
59   // Operand Iterator interface...
60   //
61   typedef Use*       op_iterator;
62   typedef const Use* const_op_iterator;
63
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; }
68
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
75   // delete.
76   //
77   void dropAllReferences() {
78     Use *OL = OperandList;
79     for (unsigned i = 0, e = NumOperands; i != e; ++i)
80       OL[i].set(0);
81   }
82
83   /// replaceUsesOfWith - Replaces all references to the "From" definition with
84   /// references to the "To" definition.
85   ///
86   void replaceUsesOfWith(Value *From, Value *To);
87
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);
92   }
93 };
94
95 template<> struct simplify_type<User::op_iterator> {
96   typedef Value* SimpleType;
97
98   static SimpleType getSimplifiedValue(const User::op_iterator &Val) {
99     return static_cast<SimpleType>(Val->get());
100   }
101 };
102
103 template<> struct simplify_type<const User::op_iterator>
104   : public simplify_type<User::op_iterator> {};
105
106 template<> struct simplify_type<User::const_op_iterator> {
107   typedef Value* SimpleType;
108
109   static SimpleType getSimplifiedValue(const User::const_op_iterator &Val) {
110     return static_cast<SimpleType>(Val->get());
111   }
112 };
113
114 template<> struct simplify_type<const User::const_op_iterator>
115   : public simplify_type<User::const_op_iterator> {};
116
117
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();
122 }
123
124 } // End llvm namespace
125
126 #endif