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