1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
| //===- llvm/User.h - User class definition ----------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This class defines the interface that one who uses a Value must implement.
// Each instance of the Value class keeps track of what User's have handles
// to it.
//
// * Instructions are the largest class of Users.
// * Constants may be users of other constants (think arrays and stuff)
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_IR_USER_H
#define LLVM_IR_USER_H
#include "llvm/ADT/iterator.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/IR/Use.h"
#include "llvm/IR/Value.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/ErrorHandling.h"
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <iterator>
namespace llvm {
template <typename T> class ArrayRef;
template <typename T> class MutableArrayRef;
/// Compile-time customization of User operands.
///
/// Customizes operand-related allocators and accessors.
template <class>
struct OperandTraits;
class User : public Value {
template <unsigned>
friend struct HungoffOperandTraits;
LLVM_ATTRIBUTE_ALWAYS_INLINE inline static void *
allocateFixedOperandUser(size_t, unsigned, unsigned);
protected:
/// Allocate a User with an operand pointer co-allocated.
///
/// This is used for subclasses which need to allocate a variable number
/// of operands, ie, 'hung off uses'.
void *operator new(size_t Size);
/// Allocate a User with the operands co-allocated.
///
/// This is used for subclasses which have a fixed number of operands.
void *operator new(size_t Size, unsigned Us);
/// Allocate a User with the operands co-allocated. If DescBytes is non-zero
/// then allocate an additional DescBytes bytes before the operands. These
/// bytes can be accessed by calling getDescriptor.
///
/// DescBytes needs to be divisible by sizeof(void *). The allocated
/// descriptor, if any, is aligned to sizeof(void *) bytes.
///
/// This is used for subclasses which have a fixed number of operands.
void *operator new(size_t Size, unsigned Us, unsigned DescBytes);
User(Type *ty, unsigned vty, Use *, unsigned NumOps)
: Value(ty, vty) {
assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
NumUserOperands = NumOps;
// If we have hung off uses, then the operand list should initially be
// null.
assert((!HasHungOffUses || !getOperandList()) &&
"Error in initializing hung off uses for User");
}
/// Allocate the array of Uses, followed by a pointer
/// (with bottom bit set) to the User.
/// \param IsPhi identifies callers which are phi nodes and which need
/// N BasicBlock* allocated along with N
void allocHungoffUses(unsigned N, bool IsPhi = false);
/// Grow the number of hung off uses. Note that allocHungoffUses
/// should be called if there are no uses.
void growHungoffUses(unsigned N, bool IsPhi = false);
protected:
~User() = default; // Use deleteValue() to delete a generic Instruction.
public:
User(const User &) = delete;
/// Free memory allocated for User and Use objects.
void operator delete(void *Usr);
/// Placement delete - required by std, called if the ctor throws.
void operator delete(void *Usr, unsigned) {
// Note: If a subclass manipulates the information which is required to calculate the
// Usr memory pointer, e.g. NumUserOperands, the operator delete of that subclass has
// to restore the changed information to the original value, since the dtor of that class
// is not called if the ctor fails.
User::operator delete(Usr);
#ifndef LLVM_ENABLE_EXCEPTIONS
llvm_unreachable("Constructor throws?");
#endif
}
/// Placement delete - required by std, called if the ctor throws.
void operator delete(void *Usr, unsigned, unsigned) {
// Note: If a subclass manipulates the information which is required to calculate the
// Usr memory pointer, e.g. NumUserOperands, the operator delete of that subclass has
// to restore the changed information to the original value, since the dtor of that class
// is not called if the ctor fails.
User::operator delete(Usr);
#ifndef LLVM_ENABLE_EXCEPTIONS
llvm_unreachable("Constructor throws?");
#endif
}
protected:
template <int Idx, typename U> static Use &OpFrom(const U *that) {
return Idx < 0
? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
: OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
}
template <int Idx> Use &Op() {
return OpFrom<Idx>(this);
}
template <int Idx> const Use &Op() const {
return OpFrom<Idx>(this);
}
private:
const Use *getHungOffOperands() const {
return *(reinterpret_cast<const Use *const *>(this) - 1);
}
Use *&getHungOffOperands() { return *(reinterpret_cast<Use **>(this) - 1); }
const Use *getIntrusiveOperands() const {
return reinterpret_cast<const Use *>(this) - NumUserOperands;
}
Use *getIntrusiveOperands() {
return reinterpret_cast<Use *>(this) - NumUserOperands;
}
void setOperandList(Use *NewList) {
assert(HasHungOffUses &&
"Setting operand list only required for hung off uses");
getHungOffOperands() = NewList;
}
public:
const Use *getOperandList() const {
return HasHungOffUses ? getHungOffOperands() : getIntrusiveOperands();
}
Use *getOperandList() {
return const_cast<Use *>(static_cast<const User *>(this)->getOperandList());
}
Value *getOperand(unsigned i) const {
assert(i < NumUserOperands && "getOperand() out of range!");
return getOperandList()[i];
}
void setOperand(unsigned i, Value *Val) {
assert(i < NumUserOperands && "setOperand() out of range!");
assert((!isa<Constant>((const Value*)this) ||
isa<GlobalValue>((const Value*)this)) &&
"Cannot mutate a constant with setOperand!");
getOperandList()[i] = Val;
}
const Use &getOperandUse(unsigned i) const {
assert(i < NumUserOperands && "getOperandUse() out of range!");
return getOperandList()[i];
}
Use &getOperandUse(unsigned i) {
assert(i < NumUserOperands && "getOperandUse() out of range!");
return getOperandList()[i];
}
unsigned getNumOperands() const { return NumUserOperands; }
/// Returns the descriptor co-allocated with this User instance.
ArrayRef<const uint8_t> getDescriptor() const;
/// Returns the descriptor co-allocated with this User instance.
MutableArrayRef<uint8_t> getDescriptor();
/// Set the number of operands on a GlobalVariable.
///
/// GlobalVariable always allocates space for a single operands, but
/// doesn't always use it.
///
/// FIXME: As that the number of operands is used to find the start of
/// the allocated memory in operator delete, we need to always think we have
/// 1 operand before delete.
void setGlobalVariableNumOperands(unsigned NumOps) {
assert(NumOps <= 1 && "GlobalVariable can only have 0 or 1 operands");
NumUserOperands = NumOps;
}
/// Subclasses with hung off uses need to manage the operand count
/// themselves. In these instances, the operand count isn't used to find the
/// OperandList, so there's no issue in having the operand count change.
void setNumHungOffUseOperands(unsigned NumOps) {
assert(HasHungOffUses && "Must have hung off uses to use this method");
assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
NumUserOperands = NumOps;
}
// ---------------------------------------------------------------------------
// Operand Iterator interface...
//
using op_iterator = Use*;
using const_op_iterator = const Use*;
using op_range = iterator_range<op_iterator>;
using const_op_range = iterator_range<const_op_iterator>;
op_iterator op_begin() { return getOperandList(); }
const_op_iterator op_begin() const { return getOperandList(); }
op_iterator op_end() {
return getOperandList() + NumUserOperands;
}
const_op_iterator op_end() const {
return getOperandList() + NumUserOperands;
}
op_range operands() {
return op_range(op_begin(), op_end());
}
const_op_range operands() const {
return const_op_range(op_begin(), op_end());
}
/// Iterator for directly iterating over the operand Values.
struct value_op_iterator
: iterator_adaptor_base<value_op_iterator, op_iterator,
std::random_access_iterator_tag, Value *,
ptrdiff_t, Value *, Value *> {
explicit value_op_iterator(Use *U = nullptr) : iterator_adaptor_base(U) {}
Value *operator*() const { return *I; }
Value *operator->() const { return operator*(); }
};
value_op_iterator value_op_begin() {
return value_op_iterator(op_begin());
}
value_op_iterator value_op_end() {
return value_op_iterator(op_end());
}
iterator_range<value_op_iterator> operand_values() {
return make_range(value_op_begin(), value_op_end());
}
struct const_value_op_iterator
: iterator_adaptor_base<const_value_op_iterator, const_op_iterator,
std::random_access_iterator_tag, const Value *,
ptrdiff_t, const Value *, const Value *> {
explicit const_value_op_iterator(const Use *U = nullptr) :
iterator_adaptor_base(U) {}
const Value *operator*() const { return *I; }
const Value *operator->() const { return operator*(); }
};
const_value_op_iterator value_op_begin() const {
return const_value_op_iterator(op_begin());
}
const_value_op_iterator value_op_end() const {
return const_value_op_iterator(op_end());
}
iterator_range<const_value_op_iterator> operand_values() const {
return make_range(value_op_begin(), value_op_end());
}
/// Drop all references to operands.
///
/// This function is in charge of "letting go" of all objects that this User
/// refers to. This allows one to 'delete' a whole class at a time, even
/// though there may be circular references... First all references are
/// dropped, and all use counts go to zero. Then everything is deleted for
/// real. Note that no operations are valid on an object that has "dropped
/// all references", except operator delete.
void dropAllReferences() {
for (Use &U : operands())
U.set(nullptr);
}
/// Replace uses of one Value with another.
///
/// Replaces all references to the "From" definition with references to the
/// "To" definition.
void replaceUsesOfWith(Value *From, Value *To);
// Methods for support type inquiry through isa, cast, and dyn_cast:
static bool classof(const Value *V) {
return isa<Instruction>(V) || isa<Constant>(V);
}
};
// Either Use objects, or a Use pointer can be prepended to User.
static_assert(alignof(Use) >= alignof(User),
"Alignment is insufficient after objects prepended to User");
static_assert(alignof(Use *) >= alignof(User),
"Alignment is insufficient after objects prepended to User");
template<> struct simplify_type<User::op_iterator> {
using SimpleType = Value*;
static SimpleType getSimplifiedValue(User::op_iterator &Val) {
return Val->get();
}
};
template<> struct simplify_type<User::const_op_iterator> {
using SimpleType = /*const*/ Value*;
static SimpleType getSimplifiedValue(User::const_op_iterator &Val) {
return Val->get();
}
};
} // end namespace llvm
#endif // LLVM_IR_USER_H
|