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