Reverting dtor devirtualization patch.
[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 public:
41   User(const Type *Ty, unsigned vty, Use *OpList, unsigned NumOps)
42     : Value(Ty, vty), OperandList(OpList), NumOperands(NumOps) {}
43
44   Value *getOperand(unsigned i) const {
45     assert(i < NumOperands && "getOperand() out of range!");
46     return OperandList[i];
47   }
48   void setOperand(unsigned i, Value *Val) {
49     assert(i < NumOperands && "setOperand() out of range!");
50     OperandList[i] = Val;
51   }
52   unsigned getNumOperands() const { return NumOperands; }
53
54   // ---------------------------------------------------------------------------
55   // Operand Iterator interface...
56   //
57   typedef Use*       op_iterator;
58   typedef const Use* const_op_iterator;
59
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; }
64
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
71   // delete.
72   //
73   void dropAllReferences() {
74     Use *OL = OperandList;
75     for (unsigned i = 0, e = NumOperands; i != e; ++i)
76       OL[i].set(0);
77   }
78
79   /// replaceUsesOfWith - Replaces all references to the "From" definition with
80   /// references to the "To" definition.
81   ///
82   void replaceUsesOfWith(Value *From, Value *To);
83
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);
88   }
89 };
90
91 template<> struct simplify_type<User::op_iterator> {
92   typedef Value* SimpleType;
93
94   static SimpleType getSimplifiedValue(const User::op_iterator &Val) {
95     return static_cast<SimpleType>(Val->get());
96   }
97 };
98
99 template<> struct simplify_type<const User::op_iterator>
100   : public simplify_type<User::op_iterator> {};
101
102 template<> struct simplify_type<User::const_op_iterator> {
103   typedef Value* SimpleType;
104
105   static SimpleType getSimplifiedValue(const User::const_op_iterator &Val) {
106     return static_cast<SimpleType>(Val->get());
107   }
108 };
109
110 template<> struct simplify_type<const User::const_op_iterator>
111   : public simplify_type<User::const_op_iterator> {};
112
113
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();
118 }
119
120 } // End llvm namespace
121
122 #endif