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