LLVM 22.0.0git
User.h
Go to the documentation of this file.
1//===- llvm/User.h - User class definition ----------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This class defines the interface that one who uses a Value must implement.
10// Each instance of the Value class keeps track of what User's have handles
11// to it.
12//
13// * Instructions are the largest class of Users.
14// * Constants may be users of other constants (think arrays and stuff)
15//
16//===----------------------------------------------------------------------===//
17
18#ifndef LLVM_IR_USER_H
19#define LLVM_IR_USER_H
20
21#include "llvm/ADT/iterator.h"
23#include "llvm/IR/Use.h"
24#include "llvm/IR/Value.h"
28#include <cassert>
29#include <cstddef>
30#include <cstdint>
31#include <iterator>
32
33namespace llvm {
34
35template <typename T> class ArrayRef;
36template <typename T> class MutableArrayRef;
37
38/// Compile-time customization of User operands.
39///
40/// Customizes operand-related allocators and accessors.
41template <class>
43
44class User : public Value {
45 friend struct HungoffOperandTraits;
46 template <class ConstantClass> friend struct ConstantAggrKeyType;
47
49 allocateFixedOperandUser(size_t, unsigned, unsigned);
50
51protected:
52 // Disable the default operator new, as all subclasses must use one of the
53 // custom operators below depending on how they store their operands.
54 void *operator new(size_t Size) = delete;
55
56 /// Indicates this User has operands "hung off" in another allocation.
58
59 /// Indicates this User has operands co-allocated.
61 /// The number of operands for this User.
62 const unsigned NumOps;
63 };
64
65 /// Indicates this User has operands and a descriptor co-allocated .
67 /// The number of operands for this User.
68 const unsigned NumOps;
69 /// The number of bytes to allocate for the descriptor. Must be divisible by
70 /// `sizeof(void *)`.
71 const unsigned DescBytes;
72 };
73
74 /// Information about how a User object was allocated, to be passed into the
75 /// User constructor.
76 ///
77 /// DO NOT USE DIRECTLY. Use one of the `AllocMarker` structs instead, they
78 /// call all be implicitly converted to `AllocInfo`.
99
100 /// Allocate a User with an operand pointer co-allocated.
101 ///
102 /// This is used for subclasses which need to allocate a variable number
103 /// of operands, ie, 'hung off uses'.
104 LLVM_ABI void *operator new(size_t Size, HungOffOperandsAllocMarker);
105
106 /// Allocate a User with the operands co-allocated.
107 ///
108 /// This is used for subclasses which have a fixed number of operands.
109 LLVM_ABI void *operator new(size_t Size,
110 IntrusiveOperandsAllocMarker allocTrait);
111
112 /// Allocate a User with the operands co-allocated. If DescBytes is non-zero
113 /// then allocate an additional DescBytes bytes before the operands. These
114 /// bytes can be accessed by calling getDescriptor.
115 LLVM_ABI void *
116 operator new(size_t Size,
117 IntrusiveOperandsAndDescriptorAllocMarker allocTrait);
118
119 User(Type *ty, unsigned vty, AllocInfo AllocInfo) : Value(ty, vty) {
121 "Too many operands");
124 "Cannot have both hung off uses and a descriptor");
127 // If we have hung off uses, then the operand list should initially be
128 // null.
130 "Error in initializing hung off uses for User");
131 }
132
133 /// Allocate the array of Uses, followed by a pointer
134 /// (with bottom bit set) to the User.
135 /// \param WithExtraValues identifies callers which need N Value* allocated
136 /// along the N operands.
137 LLVM_ABI void allocHungoffUses(unsigned N, bool WithExtraValues = false);
138
139 /// Grow the number of hung off uses. Note that allocHungoffUses
140 /// should be called if there are no uses.
141 LLVM_ABI void growHungoffUses(unsigned N, bool WithExtraValues = false);
142
143protected:
144 // Use deleteValue() to delete a generic User.
145 ~User();
146
147public:
148 User(const User &) = delete;
149
150 /// Free memory allocated for User and Use objects.
151 LLVM_ABI void operator delete(void *Usr);
152 /// Placement delete - required by std, called if the ctor throws.
153 void operator delete(void *Usr, HungOffOperandsAllocMarker) {
154 // Note: If a subclass manipulates the information which is required to
155 // calculate the Usr memory pointer, e.g. NumUserOperands, the operator
156 // delete of that subclass has to restore the changed information to the
157 // original value, since the dtor of that class is not called if the ctor
158 // fails.
159 User::operator delete(Usr);
160
161#ifndef LLVM_ENABLE_EXCEPTIONS
162 llvm_unreachable("Constructor throws?");
163#endif
164 }
165 /// Placement delete - required by std, called if the ctor throws.
166 void operator delete(void *Usr, IntrusiveOperandsAllocMarker) {
167 // Note: If a subclass manipulates the information which is required to calculate the
168 // Usr memory pointer, e.g. NumUserOperands, the operator delete of that subclass has
169 // to restore the changed information to the original value, since the dtor of that class
170 // is not called if the ctor fails.
171 User::operator delete(Usr);
172
173#ifndef LLVM_ENABLE_EXCEPTIONS
174 llvm_unreachable("Constructor throws?");
175#endif
176 }
177 /// Placement delete - required by std, called if the ctor throws.
178 void operator delete(void *Usr, IntrusiveOperandsAndDescriptorAllocMarker) {
179 // Note: If a subclass manipulates the information which is required to calculate the
180 // Usr memory pointer, e.g. NumUserOperands, the operator delete of that subclass has
181 // to restore the changed information to the original value, since the dtor of that class
182 // is not called if the ctor fails.
183 User::operator delete(Usr);
184
185#ifndef LLVM_ENABLE_EXCEPTIONS
186 llvm_unreachable("Constructor throws?");
187#endif
188 }
189
190protected:
191 template <int Idx, typename U> static Use &OpFrom(const U *that) {
192 return Idx < 0
193 ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
194 : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
195 }
196
197 template <int Idx> Use &Op() {
198 return OpFrom<Idx>(this);
199 }
200 template <int Idx> const Use &Op() const {
201 return OpFrom<Idx>(this);
202 }
203
204private:
205 const Use *getHungOffOperands() const {
206 return *(reinterpret_cast<const Use *const *>(this) - 1);
207 }
208
209 Use *&getHungOffOperands() { return *(reinterpret_cast<Use **>(this) - 1); }
210
211 const Use *getIntrusiveOperands() const {
212 return reinterpret_cast<const Use *>(this) - NumUserOperands;
213 }
214
215 Use *getIntrusiveOperands() {
216 return reinterpret_cast<Use *>(this) - NumUserOperands;
217 }
218
219 void setOperandList(Use *NewList) {
221 "Setting operand list only required for hung off uses");
222 getHungOffOperands() = NewList;
223 }
224
225public:
226 const Use *getOperandList() const {
227 return HasHungOffUses ? getHungOffOperands() : getIntrusiveOperands();
228 }
230 return const_cast<Use *>(static_cast<const User *>(this)->getOperandList());
231 }
232
233 Value *getOperand(unsigned i) const {
234 assert(i < NumUserOperands && "getOperand() out of range!");
235 return getOperandList()[i];
236 }
237
238 void setOperand(unsigned i, Value *Val) {
239 assert(i < NumUserOperands && "setOperand() out of range!");
240 assert((!isa<Constant>((const Value*)this) ||
241 isa<GlobalValue>((const Value*)this)) &&
242 "Cannot mutate a constant with setOperand!");
243 getOperandList()[i] = Val;
244 }
245
246 const Use &getOperandUse(unsigned i) const {
247 assert(i < NumUserOperands && "getOperandUse() out of range!");
248 return getOperandList()[i];
249 }
250 Use &getOperandUse(unsigned i) {
251 assert(i < NumUserOperands && "getOperandUse() out of range!");
252 return getOperandList()[i];
253 }
254
255 unsigned getNumOperands() const { return NumUserOperands; }
256
257 /// Returns the descriptor co-allocated with this User instance.
259
260 /// Returns the descriptor co-allocated with this User instance.
262
263 /// Subclasses with hung off uses need to manage the operand count
264 /// themselves. In these instances, the operand count isn't used to find the
265 /// OperandList, so there's no issue in having the operand count change.
267 assert(HasHungOffUses && "Must have hung off uses to use this method");
268 assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
270 }
271
272 /// A droppable user is a user for which uses can be dropped without affecting
273 /// correctness and should be dropped rather than preventing a transformation
274 /// from happening.
275 LLVM_ABI bool isDroppable() const;
276
277 // ---------------------------------------------------------------------------
278 // Operand Iterator interface...
279 //
280 using op_iterator = Use*;
281 using const_op_iterator = const Use*;
284
294 return op_range(op_begin(), op_end());
295 }
297 return const_op_range(op_begin(), op_end());
298 }
299
300 /// Iterator for directly iterating over the operand Values.
302 : iterator_adaptor_base<value_op_iterator, op_iterator,
303 std::random_access_iterator_tag, Value *,
304 ptrdiff_t, Value *, Value *> {
305 explicit value_op_iterator(Use *U = nullptr) : iterator_adaptor_base(U) {}
306
307 Value *operator*() const { return *I; }
308 Value *operator->() const { return operator*(); }
309 };
310
320
322 : iterator_adaptor_base<const_value_op_iterator, const_op_iterator,
323 std::random_access_iterator_tag, const Value *,
324 ptrdiff_t, const Value *, const Value *> {
325 explicit const_value_op_iterator(const Use *U = nullptr) :
327
328 const Value *operator*() const { return *I; }
329 const Value *operator->() const { return operator*(); }
330 };
331
341
342 /// Drop all references to operands.
343 ///
344 /// This function is in charge of "letting go" of all objects that this User
345 /// refers to. This allows one to 'delete' a whole class at a time, even
346 /// though there may be circular references... First all references are
347 /// dropped, and all use counts go to zero. Then everything is deleted for
348 /// real. Note that no operations are valid on an object that has "dropped
349 /// all references", except operator delete.
351 for (Use &U : operands())
352 U.set(nullptr);
353 }
354
355 /// Replace uses of one Value with another.
356 ///
357 /// Replaces all references to the "From" definition with references to the
358 /// "To" definition. Returns whether any uses were replaced.
359 LLVM_ABI bool replaceUsesOfWith(Value *From, Value *To);
360
361 // Methods for support type inquiry through isa, cast, and dyn_cast:
362 static bool classof(const Value *V) {
363 return isa<Instruction>(V) || isa<Constant>(V);
364 }
365};
366
367// Either Use objects, or a Use pointer can be prepended to User.
368static_assert(alignof(Use) >= alignof(User),
369 "Alignment is insufficient after objects prepended to User");
370static_assert(alignof(Use *) >= alignof(User),
371 "Alignment is insufficient after objects prepended to User");
372
373template<> struct simplify_type<User::op_iterator> {
375
377 return Val->get();
378 }
379};
380template<> struct simplify_type<User::const_op_iterator> {
381 using SimpleType = /*const*/ Value*;
382
384 return Val->get();
385 }
386};
387
388} // end namespace llvm
389
390#endif // LLVM_IR_USER_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
#define LLVM_PREFERRED_TYPE(T)
\macro LLVM_PREFERRED_TYPE Adjust type of bit-field in debug info.
Definition Compiler.h:706
#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:356
#define LLVM_ABI
Definition Compiler.h:213
This defines the Use class.
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition ArrayRef.h:298
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
iterator_range< const_op_iterator > const_op_range
Definition User.h:283
Use * op_iterator
Definition User.h:280
const Use * getOperandList() const
Definition User.h:226
op_range operands()
Definition User.h:293
User(Type *ty, unsigned vty, AllocInfo AllocInfo)
Definition User.h:119
op_iterator op_begin()
Definition User.h:285
LLVM_ABI void allocHungoffUses(unsigned N, bool WithExtraValues=false)
Allocate the array of Uses, followed by a pointer (with bottom bit set) to the User.
Definition User.cpp:54
const Use & getOperandUse(unsigned i) const
Definition User.h:246
const_op_iterator op_begin() const
Definition User.h:286
void dropAllReferences()
Drop all references to operands.
Definition User.h:350
const_op_iterator op_end() const
Definition User.h:290
LLVM_ABI ArrayRef< const uint8_t > getDescriptor() const
Returns the descriptor co-allocated with this User instance.
Definition User.cpp:103
value_op_iterator value_op_end()
Definition User.h:314
void setOperand(unsigned i, Value *Val)
Definition User.h:238
friend struct HungoffOperandTraits
Definition User.h:45
Use * getOperandList()
Definition User.h:229
const Use * const_op_iterator
Definition User.h:281
Use & getOperandUse(unsigned i)
Definition User.h:250
const_value_op_iterator value_op_begin() const
Definition User.h:332
void setNumHungOffUseOperands(unsigned NumOps)
Subclasses with hung off uses need to manage the operand count themselves.
Definition User.h:266
Use & Op()
Definition User.h:197
static bool classof(const Value *V)
Definition User.h:362
iterator_range< op_iterator > op_range
Definition User.h:282
LLVM_ABI bool isDroppable() const
A droppable user is a user for which uses can be dropped without affecting correctness and should be ...
Definition User.cpp:119
LLVM_ABI bool replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Definition User.cpp:25
LLVM_ABI void growHungoffUses(unsigned N, bool WithExtraValues=false)
Grow the number of hung off uses.
Definition User.cpp:71
static Use & OpFrom(const U *that)
Definition User.h:191
Value * getOperand(unsigned i) const
Definition User.h:233
User(const User &)=delete
iterator_range< const_value_op_iterator > operand_values() const
Definition User.h:338
const Use & Op() const
Definition User.h:200
const_op_range operands() const
Definition User.h:296
friend struct ConstantAggrKeyType
Definition User.h:46
value_op_iterator value_op_begin()
Definition User.h:311
unsigned getNumOperands() const
Definition User.h:255
iterator_range< value_op_iterator > operand_values()
Definition User.h:317
op_iterator op_end()
Definition User.h:287
const_value_op_iterator value_op_end() const
Definition User.h:335
LLVM Value Representation.
Definition Value.h:75
LLVM_ABI Value(Type *Ty, unsigned scid)
Definition Value.cpp:53
@ NumUserOperandsBits
Definition Value.h:108
unsigned NumUserOperands
Definition Value.h:109
unsigned HasHungOffUses
Definition Value.h:115
unsigned HasDescriptor
Definition Value.h:116
A range adaptor for a pair of iterators.
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
NodeAddr< UseNode * > Use
Definition RDFGraph.h:385
This is an optimization pass for GlobalISel generic memory operations.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
#define N
Compile-time customization of User operands.
Definition User.h:42
Information about how a User object was allocated, to be passed into the User constructor.
Definition User.h:79
const unsigned HasHungOffUses
Definition User.h:83
constexpr AllocInfo(const IntrusiveOperandsAllocMarker Alloc)
Definition User.h:92
constexpr AllocInfo(const IntrusiveOperandsAndDescriptorAllocMarker Alloc)
Definition User.h:95
const unsigned HasDescriptor
Definition User.h:85
const unsigned NumOps
Definition User.h:81
Indicates this User has operands "hung off" in another allocation.
Definition User.h:57
Indicates this User has operands co-allocated.
Definition User.h:60
const unsigned NumOps
The number of operands for this User.
Definition User.h:62
Indicates this User has operands and a descriptor co-allocated .
Definition User.h:66
const unsigned NumOps
The number of operands for this User.
Definition User.h:68
const unsigned DescBytes
The number of bytes to allocate for the descriptor.
Definition User.h:71
const_value_op_iterator(const Use *U=nullptr)
Definition User.h:325
const Value * operator*() const
Definition User.h:328
const Value * operator->() const
Definition User.h:329
Iterator for directly iterating over the operand Values.
Definition User.h:304
Value * operator->() const
Definition User.h:308
Value * operator*() const
Definition User.h:307
value_op_iterator(Use *U=nullptr)
Definition User.h:305
static SimpleType getSimplifiedValue(User::const_op_iterator &Val)
Definition User.h:383
static SimpleType getSimplifiedValue(User::op_iterator &Val)
Definition User.h:376
Define a template that can be specialized by smart pointers to reflect the fact that they are automat...
Definition Casting.h:34