Add a deterministic finite automaton based packetizer for VLIW architectures
[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 /// OperandTraits - Compile-time customization of
27 /// operand-related allocators and accessors
28 /// for use of the User class
29 template <class>
30 struct OperandTraits;
31
32 class User : public Value {
33   User(const User &);             // Do not implement
34   void *operator new(size_t);     // Do not implement
35   template <unsigned>
36   friend struct HungoffOperandTraits;
37   virtual void anchor();
38 protected:
39   /// OperandList - This is a pointer to the array of Uses for this User.
40   /// For nodes of fixed arity (e.g. a binary operator) this array will live
41   /// prefixed to some derived class instance.  For nodes of resizable variable
42   /// arity (e.g. PHINodes, SwitchInst etc.), this memory will be dynamically
43   /// allocated and should be destroyed by the classes' virtual dtor.
44   Use *OperandList;
45
46   /// NumOperands - The number of values used by this User.
47   ///
48   unsigned NumOperands;
49
50   void *operator new(size_t s, unsigned Us);
51   User(Type *ty, unsigned vty, Use *OpList, unsigned NumOps)
52     : Value(ty, vty), OperandList(OpList), NumOperands(NumOps) {}
53   Use *allocHungoffUses(unsigned) const;
54   void dropHungoffUses() {
55     Use::zap(OperandList, OperandList + NumOperands, true);
56     OperandList = 0;
57     // Reset NumOperands so User::operator delete() does the right thing.
58     NumOperands = 0;
59   }
60 public:
61   ~User() {
62     Use::zap(OperandList, OperandList + NumOperands);
63   }
64   /// operator delete - free memory allocated for User and Use objects
65   void operator delete(void *Usr);
66   /// placement delete - required by std, but never called.
67   void operator delete(void*, unsigned) {
68     assert(0 && "Constructor throws?");
69   }
70   /// placement delete - required by std, but never called.
71   void operator delete(void*, unsigned, bool) {
72     assert(0 && "Constructor throws?");
73   }
74 protected:
75   template <int Idx, typename U> static Use &OpFrom(const U *that) {
76     return Idx < 0
77       ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
78       : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
79   }
80   template <int Idx> Use &Op() {
81     return OpFrom<Idx>(this);
82   }
83   template <int Idx> const Use &Op() const {
84     return OpFrom<Idx>(this);
85   }
86 public:
87   Value *getOperand(unsigned i) const {
88     assert(i < NumOperands && "getOperand() out of range!");
89     return OperandList[i];
90   }
91   void setOperand(unsigned i, Value *Val) {
92     assert(i < NumOperands && "setOperand() out of range!");
93     assert((!isa<Constant>((const Value*)this) ||
94             isa<GlobalValue>((const Value*)this)) &&
95            "Cannot mutate a constant with setOperand!");
96     OperandList[i] = Val;
97   }
98   const Use &getOperandUse(unsigned i) const {
99     assert(i < NumOperands && "getOperandUse() out of range!");
100     return OperandList[i];
101   }
102   Use &getOperandUse(unsigned i) {
103     assert(i < NumOperands && "getOperandUse() out of range!");
104     return OperandList[i];
105   }
106   
107   unsigned getNumOperands() const { return NumOperands; }
108
109   // ---------------------------------------------------------------------------
110   // Operand Iterator interface...
111   //
112   typedef Use*       op_iterator;
113   typedef const Use* const_op_iterator;
114
115   inline op_iterator       op_begin()       { return OperandList; }
116   inline const_op_iterator op_begin() const { return OperandList; }
117   inline op_iterator       op_end()         { return OperandList+NumOperands; }
118   inline const_op_iterator op_end()   const { return OperandList+NumOperands; }
119
120   // dropAllReferences() - This function is in charge of "letting go" of all
121   // objects that this User refers to.  This allows one to
122   // 'delete' a whole class at a time, even though there may be circular
123   // references...  First all references are dropped, and all use counts go to
124   // zero.  Then everything is deleted for real.  Note that no operations are
125   // valid on an object that has "dropped all references", except operator
126   // delete.
127   //
128   void dropAllReferences() {
129     for (op_iterator i = op_begin(), e = op_end(); i != e; ++i)
130       i->set(0);
131   }
132
133   /// replaceUsesOfWith - Replaces all references to the "From" definition with
134   /// references to the "To" definition.
135   ///
136   void replaceUsesOfWith(Value *From, Value *To);
137
138   // Methods for support type inquiry through isa, cast, and dyn_cast:
139   static inline bool classof(const User *) { return true; }
140   static inline bool classof(const Value *V) {
141     return isa<Instruction>(V) || isa<Constant>(V);
142   }
143 };
144
145 template<> struct simplify_type<User::op_iterator> {
146   typedef Value* SimpleType;
147
148   static SimpleType getSimplifiedValue(const User::op_iterator &Val) {
149     return static_cast<SimpleType>(Val->get());
150   }
151 };
152
153 template<> struct simplify_type<const User::op_iterator>
154   : public simplify_type<User::op_iterator> {};
155
156 template<> struct simplify_type<User::const_op_iterator> {
157   typedef Value* SimpleType;
158
159   static SimpleType getSimplifiedValue(const User::const_op_iterator &Val) {
160     return static_cast<SimpleType>(Val->get());
161   }
162 };
163
164 template<> struct simplify_type<const User::const_op_iterator>
165   : public simplify_type<User::const_op_iterator> {};
166
167
168 // value_use_iterator::getOperandNo - Requires the definition of the User class.
169 template<typename UserTy>
170 unsigned value_use_iterator<UserTy>::getOperandNo() const {
171   return U - U->getUser()->op_begin();
172 }
173
174 } // End llvm namespace
175
176 #endif