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