• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ArrayRef.h"
23 #include "llvm/ADT/iterator.h"
24 #include "llvm/ADT/iterator_range.h"
25 #include "llvm/IR/Value.h"
26 #include "llvm/Support/AlignOf.h"
27 #include "llvm/Support/ErrorHandling.h"
28 
29 namespace llvm {
30 
31 /// \brief Compile-time customization of User operands.
32 ///
33 /// Customizes operand-related allocators and accessors.
34 template <class>
35 struct OperandTraits;
36 
37 class User : public Value {
38   User(const User &) = delete;
39   template <unsigned>
40   friend struct HungoffOperandTraits;
41   virtual void anchor();
42 
43   LLVM_ATTRIBUTE_ALWAYS_INLINE inline static void *
44   allocateFixedOperandUser(size_t, unsigned, unsigned);
45 
46 protected:
47   /// Allocate a User with an operand pointer co-allocated.
48   ///
49   /// This is used for subclasses which need to allocate a variable number
50   /// of operands, ie, 'hung off uses'.
51   void *operator new(size_t Size);
52 
53   /// Allocate a User with the operands co-allocated.
54   ///
55   /// This is used for subclasses which have a fixed number of operands.
56   void *operator new(size_t Size, unsigned Us);
57 
58   /// Allocate a User with the operands co-allocated.  If DescBytes is non-zero
59   /// then allocate an additional DescBytes bytes before the operands. These
60   /// bytes can be accessed by calling getDescriptor.
61   ///
62   /// DescBytes needs to be divisible by sizeof(void *).  The allocated
63   /// descriptor, if any, is aligned to sizeof(void *) bytes.
64   ///
65   /// This is used for subclasses which have a fixed number of operands.
66   void *operator new(size_t Size, unsigned Us, unsigned DescBytes);
67 
User(Type * ty,unsigned vty,Use *,unsigned NumOps)68   User(Type *ty, unsigned vty, Use *, unsigned NumOps)
69       : Value(ty, vty) {
70     assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
71     NumUserOperands = NumOps;
72     // If we have hung off uses, then the operand list should initially be
73     // null.
74     assert((!HasHungOffUses || !getOperandList()) &&
75            "Error in initializing hung off uses for User");
76   }
77 
78   /// \brief Allocate the array of Uses, followed by a pointer
79   /// (with bottom bit set) to the User.
80   /// \param IsPhi identifies callers which are phi nodes and which need
81   /// N BasicBlock* allocated along with N
82   void allocHungoffUses(unsigned N, bool IsPhi = false);
83 
84   /// \brief Grow the number of hung off uses.  Note that allocHungoffUses
85   /// should be called if there are no uses.
86   void growHungoffUses(unsigned N, bool IsPhi = false);
87 
88 public:
~User()89   ~User() override {
90   }
91   /// \brief Free memory allocated for User and Use objects.
92   void operator delete(void *Usr);
93   /// \brief Placement delete - required by std, but never called.
delete(void *,unsigned)94   void operator delete(void*, unsigned) {
95     llvm_unreachable("Constructor throws?");
96   }
97   /// \brief Placement delete - required by std, but never called.
delete(void *,unsigned,bool)98   void operator delete(void*, unsigned, bool) {
99     llvm_unreachable("Constructor throws?");
100   }
101 protected:
OpFrom(const U * that)102   template <int Idx, typename U> static Use &OpFrom(const U *that) {
103     return Idx < 0
104       ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
105       : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
106   }
Op()107   template <int Idx> Use &Op() {
108     return OpFrom<Idx>(this);
109   }
Op()110   template <int Idx> const Use &Op() const {
111     return OpFrom<Idx>(this);
112   }
113 private:
getHungOffOperands()114   Use *&getHungOffOperands() { return *(reinterpret_cast<Use **>(this) - 1); }
115 
getIntrusiveOperands()116   Use *getIntrusiveOperands() {
117     return reinterpret_cast<Use *>(this) - NumUserOperands;
118   }
119 
setOperandList(Use * NewList)120   void setOperandList(Use *NewList) {
121     assert(HasHungOffUses &&
122            "Setting operand list only required for hung off uses");
123     getHungOffOperands() = NewList;
124   }
125 public:
getOperandList()126   Use *getOperandList() {
127     return HasHungOffUses ? getHungOffOperands() : getIntrusiveOperands();
128   }
getOperandList()129   const Use *getOperandList() const {
130     return const_cast<User *>(this)->getOperandList();
131   }
getOperand(unsigned i)132   Value *getOperand(unsigned i) const {
133     assert(i < NumUserOperands && "getOperand() out of range!");
134     return getOperandList()[i];
135   }
setOperand(unsigned i,Value * Val)136   void setOperand(unsigned i, Value *Val) {
137     assert(i < NumUserOperands && "setOperand() out of range!");
138     assert((!isa<Constant>((const Value*)this) ||
139             isa<GlobalValue>((const Value*)this)) &&
140            "Cannot mutate a constant with setOperand!");
141     getOperandList()[i] = Val;
142   }
getOperandUse(unsigned i)143   const Use &getOperandUse(unsigned i) const {
144     assert(i < NumUserOperands && "getOperandUse() out of range!");
145     return getOperandList()[i];
146   }
getOperandUse(unsigned i)147   Use &getOperandUse(unsigned i) {
148     assert(i < NumUserOperands && "getOperandUse() out of range!");
149     return getOperandList()[i];
150   }
151 
getNumOperands()152   unsigned getNumOperands() const { return NumUserOperands; }
153 
154   /// Returns the descriptor co-allocated with this User instance.
155   ArrayRef<const uint8_t> getDescriptor() const;
156 
157   /// Returns the descriptor co-allocated with this User instance.
158   MutableArrayRef<uint8_t> getDescriptor();
159 
160   /// Set the number of operands on a GlobalVariable.
161   ///
162   /// GlobalVariable always allocates space for a single operands, but
163   /// doesn't always use it.
164   ///
165   /// FIXME: As that the number of operands is used to find the start of
166   /// the allocated memory in operator delete, we need to always think we have
167   /// 1 operand before delete.
setGlobalVariableNumOperands(unsigned NumOps)168   void setGlobalVariableNumOperands(unsigned NumOps) {
169     assert(NumOps <= 1 && "GlobalVariable can only have 0 or 1 operands");
170     NumUserOperands = NumOps;
171   }
172 
173   /// \brief Subclasses with hung off uses need to manage the operand count
174   /// themselves.  In these instances, the operand count isn't used to find the
175   /// OperandList, so there's no issue in having the operand count change.
setNumHungOffUseOperands(unsigned NumOps)176   void setNumHungOffUseOperands(unsigned NumOps) {
177     assert(HasHungOffUses && "Must have hung off uses to use this method");
178     assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
179     NumUserOperands = NumOps;
180   }
181 
182   // ---------------------------------------------------------------------------
183   // Operand Iterator interface...
184   //
185   typedef Use*       op_iterator;
186   typedef const Use* const_op_iterator;
187   typedef iterator_range<op_iterator> op_range;
188   typedef iterator_range<const_op_iterator> const_op_range;
189 
op_begin()190   op_iterator       op_begin()       { return getOperandList(); }
op_begin()191   const_op_iterator op_begin() const { return getOperandList(); }
op_end()192   op_iterator       op_end()         {
193     return getOperandList() + NumUserOperands;
194   }
op_end()195   const_op_iterator op_end()   const {
196     return getOperandList() + NumUserOperands;
197   }
operands()198   op_range operands() {
199     return op_range(op_begin(), op_end());
200   }
operands()201   const_op_range operands() const {
202     return const_op_range(op_begin(), op_end());
203   }
204 
205   /// \brief Iterator for directly iterating over the operand Values.
206   struct value_op_iterator
207       : iterator_adaptor_base<value_op_iterator, op_iterator,
208                               std::random_access_iterator_tag, Value *,
209                               ptrdiff_t, Value *, Value *> {
iterator_adaptor_basevalue_op_iterator210     explicit value_op_iterator(Use *U = nullptr) : iterator_adaptor_base(U) {}
211 
212     Value *operator*() const { return *I; }
213     Value *operator->() const { return operator*(); }
214   };
215 
value_op_begin()216   value_op_iterator value_op_begin() {
217     return value_op_iterator(op_begin());
218   }
value_op_end()219   value_op_iterator value_op_end() {
220     return value_op_iterator(op_end());
221   }
operand_values()222   iterator_range<value_op_iterator> operand_values() {
223     return make_range(value_op_begin(), value_op_end());
224   }
225 
226   /// \brief Drop all references to operands.
227   ///
228   /// This function is in charge of "letting go" of all objects that this User
229   /// refers to.  This allows one to 'delete' a whole class at a time, even
230   /// though there may be circular references...  First all references are
231   /// dropped, and all use counts go to zero.  Then everything is deleted for
232   /// real.  Note that no operations are valid on an object that has "dropped
233   /// all references", except operator delete.
dropAllReferences()234   void dropAllReferences() {
235     for (Use &U : operands())
236       U.set(nullptr);
237   }
238 
239   /// \brief Replace uses of one Value with another.
240   ///
241   /// Replaces all references to the "From" definition with references to the
242   /// "To" definition.
243   void replaceUsesOfWith(Value *From, Value *To);
244 
245   // Methods for support type inquiry through isa, cast, and dyn_cast:
classof(const Value * V)246   static inline bool classof(const Value *V) {
247     return isa<Instruction>(V) || isa<Constant>(V);
248   }
249 };
250 // Either Use objects, or a Use pointer can be prepended to User.
251 static_assert(AlignOf<Use>::Alignment >= AlignOf<User>::Alignment,
252               "Alignment is insufficient after objects prepended to User");
253 static_assert(AlignOf<Use *>::Alignment >= AlignOf<User>::Alignment,
254               "Alignment is insufficient after objects prepended to User");
255 
256 template<> struct simplify_type<User::op_iterator> {
257   typedef Value* SimpleType;
258   static SimpleType getSimplifiedValue(User::op_iterator &Val) {
259     return Val->get();
260   }
261 };
262 template<> struct simplify_type<User::const_op_iterator> {
263   typedef /*const*/ Value* SimpleType;
264   static SimpleType getSimplifiedValue(User::const_op_iterator &Val) {
265     return Val->get();
266   }
267 };
268 
269 } // End llvm namespace
270 
271 #endif
272