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