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