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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
// SPDX-License-Identifier: GPL-3.0-or-later
#ifndef RCC_CODEGEN_INSTRUCTIONDESCRIPTOR_H
#define RCC_CODEGEN_INSTRUCTIONDESCRIPTOR_H
#include "rcc/CodeGen/LowLevelType.h"
#include "rcc/CodeGen/MachineIRTypes.h"
#include "rcc/CodeGen/MachineOperand.h"
#include "rcc/CodeGen/Register.h"
#include "rcc/Support/Error.h"
#include <cstddef>
#include <cstdint>
#include <functional>
#include <initializer_list>
#include <optional>
#include <span>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include <vector>
namespace rcc {
class MachineInstr;
class MachineMemOperand;
class MachineRegisterInfo;
using ExplicitOperandVerifier = Expected<void> (*)(Opcode, std::span<const MachineOperand>,
const MachineRegisterInfo &);
using InstructionSemanticVerifier = Expected<void> (*)(const MachineInstr &,
const MachineRegisterInfo &);
class OperandKindSet final {
public:
static_assert(static_cast<std::uint32_t>(MachineOperand::Kind::Count) <= 32);
constexpr OperandKindSet() noexcept = default;
OperandKindSet(std::initializer_list<MachineOperand::Kind> Kinds) noexcept;
[[nodiscard]] constexpr bool empty() const noexcept {
return Mask == 0;
}
[[nodiscard]] constexpr bool contains(MachineOperand::Kind Kind) const noexcept {
const auto Index = static_cast<std::uint32_t>(Kind);
if (Index >= static_cast<std::uint32_t>(MachineOperand::Kind::Count))
return false;
return (Mask & (std::uint32_t{1} << Index)) != 0;
}
constexpr void add(MachineOperand::Kind Kind) noexcept {
const auto Index = static_cast<std::uint32_t>(Kind);
if (Index < static_cast<std::uint32_t>(MachineOperand::Kind::Count))
Mask |= std::uint32_t{1} << Index;
}
[[nodiscard]] constexpr std::uint32_t rawValue() const noexcept {
return Mask;
}
friend constexpr bool operator==(OperandKindSet Left, OperandKindSet Right) noexcept = default;
private:
std::uint32_t Mask = 0;
};
class LowLevelTypeKindSet final {
public:
static_assert(static_cast<std::uint32_t>(LowLevelType::Kind::VectorPointer) < 16);
constexpr LowLevelTypeKindSet() noexcept = default;
LowLevelTypeKindSet(std::initializer_list<LowLevelType::Kind> Kinds) noexcept;
[[nodiscard]] constexpr bool empty() const noexcept {
return Mask == 0;
}
[[nodiscard]] constexpr bool contains(LowLevelType::Kind Kind) const noexcept {
const auto Index = static_cast<std::uint32_t>(Kind);
return Index < 16 && (Mask & (std::uint16_t{1} << Index)) != 0;
}
constexpr void add(LowLevelType::Kind Kind) noexcept {
const auto Index = static_cast<std::uint32_t>(Kind);
if (Index < 16)
Mask |= std::uint16_t{1} << Index;
}
[[nodiscard]] constexpr std::uint16_t rawValue() const noexcept {
return Mask;
}
friend constexpr bool operator==(LowLevelTypeKindSet Left,
LowLevelTypeKindSet Right) noexcept = default;
private:
std::uint16_t Mask = 0;
};
enum class RegisterOperandRole : std::uint8_t {
None,
Def,
Use,
DefUse,
Any,
};
enum class MemoryOperandRole : std::uint8_t {
None,
Address,
Value,
Size,
Ordering,
};
struct OperandTypeConstraint final {
LowLevelTypeKindSet Kinds;
std::optional<std::uint16_t> EqualityGroup;
};
enum class InstructionFlag : std::uint8_t {
Terminator,
Branch,
IndirectBranch,
Call,
Return,
Barrier,
MayLoad,
MayStore,
HasSideEffects,
Pseudo,
Meta,
AllowedPreLegalizer,
ProducesCode,
Commutable,
Rematerializable,
Predicable,
NotDuplicable,
Convergent,
UnmodeledSideEffects,
MayRaiseFPException,
Phi,
FakeUse,
Generic,
TerminalEmission,
Count,
};
class InstructionFlags final {
public:
static_assert(static_cast<std::uint32_t>(InstructionFlag::Count) <= 32);
constexpr InstructionFlags() noexcept = default;
InstructionFlags(std::initializer_list<InstructionFlag> Flags) noexcept;
[[nodiscard]] constexpr bool has(InstructionFlag Flag) const noexcept {
const auto Index = static_cast<std::uint32_t>(Flag);
if (Index >= static_cast<std::uint32_t>(InstructionFlag::Count))
return false;
return (Mask & (std::uint32_t{1} << Index)) != 0;
}
constexpr void set(InstructionFlag Flag) noexcept {
const auto Index = static_cast<std::uint32_t>(Flag);
if (Index < static_cast<std::uint32_t>(InstructionFlag::Count))
Mask |= std::uint32_t{1} << Index;
}
constexpr void clear(InstructionFlag Flag) noexcept {
const auto Index = static_cast<std::uint32_t>(Flag);
if (Index < static_cast<std::uint32_t>(InstructionFlag::Count))
Mask &= ~(std::uint32_t{1} << Index);
}
[[nodiscard]] constexpr std::uint32_t rawValue() const noexcept {
return Mask;
}
friend constexpr bool operator==(InstructionFlags Left,
InstructionFlags Right) noexcept = default;
private:
std::uint32_t Mask = 0;
};
enum class PseudoOwner : std::uint8_t {
None,
Legalizer,
RegisterBankSelect,
InstructionSelect,
PhiElimination,
TwoAddressRewrite,
PreRAExpansion,
RegisterAllocator,
PrePEIExpansion,
PrologueEpilogueInsertion,
PostRAExpansion,
Finalization,
AsmPrinter,
TargetLateExpansion,
};
enum class PipelineStage : std::uint8_t {
MachineIRInput,
Legalized,
RegBankSelected,
Selected,
SSAEliminated,
TiedOpsRewritten,
RegisterAllocated,
FrameFinalized,
PostRAFinalized,
Emission,
};
struct OperandDescriptor final {
OperandKindSet Kinds;
RegisterOperandRole RegisterRole = RegisterOperandRole::None;
std::optional<RegisterClassID> RegClass;
std::optional<RegisterBankID> RegBank;
std::optional<ImmediatePredicateID> ImmediatePredicate;
MemoryOperandRole MemoryRole = MemoryOperandRole::None;
std::optional<std::size_t> DefaultTiedOperand;
bool EarlyClobber = false;
bool Optional = false;
bool VariadicGroup = false;
bool AllowSubReg = false;
bool AllowAnyTargetFlags = false;
std::optional<NamedOperandID> Name;
std::optional<TargetOperandTypeID> TargetOperandType;
std::uint32_t AllowedTargetFlags = 0;
std::optional<OperandTypeConstraint> TypeConstraint;
};
struct ImplicitRegisterDescriptor final {
PhysicalRegister Reg;
bool EarlyClobber = false;
bool Renamable = false;
friend bool operator==(const ImplicitRegisterDescriptor &Left,
const ImplicitRegisterDescriptor &Right) = default;
};
struct InstructionDescriptor final {
Opcode InstructionOpcode;
std::string Name;
std::vector<OperandDescriptor> Operands;
std::size_t NumExplicitDefs = 0;
bool Variadic = false;
std::size_t MinimumVariadicRepetitions = 0;
std::vector<ImplicitRegisterDescriptor> ImplicitDefs;
std::vector<ImplicitRegisterDescriptor> ImplicitUses;
bool AllowExtraImplicitOperands = false;
InstructionFlags Flags;
SchedulingClassID SchedulingClass;
EncodingCategoryID EncodingCategory;
std::uint32_t SizeInBytes = 0;
AssemblyFormatID AssemblyFormat;
bool RequiresMemoryOperands = false;
PseudoOwner Owner = PseudoOwner::None;
PipelineStage Deadline = PipelineStage::Emission;
ExplicitOperandVerifier VerifyExplicitOperands = nullptr;
InstructionSemanticVerifier VerifyInstructionSemantics = nullptr;
};
[[nodiscard]] Expected<std::vector<const OperandDescriptor *>>
resolveExplicitOperandDescriptors(const InstructionDescriptor &Descriptor,
std::size_t OperandCount);
struct ImmediatePredicate final {
ImmediatePredicateID ID;
std::string Name;
std::function<bool(const MachineOperand &)> Predicate;
};
struct MachineOperandTie final {
std::size_t DefOperand;
std::size_t UseOperand;
friend bool operator==(MachineOperandTie Left, MachineOperandTie Right) noexcept = default;
};
class InstructionSet final {
public:
[[nodiscard]] static Expected<InstructionSet>
create(std::vector<InstructionDescriptor> Descriptors,
std::vector<ImmediatePredicate> ImmediatePredicates = {});
[[nodiscard]] const InstructionDescriptor *descriptor(Opcode InstructionOpcode) const noexcept;
[[nodiscard]] const InstructionDescriptor *descriptor(std::string_view Name) const noexcept;
[[nodiscard]] std::span<const InstructionDescriptor> descriptors() const noexcept {
return Descriptors;
}
[[nodiscard]] Expected<void> validateExplicitOperands(Opcode InstructionOpcode,
std::span<const MachineOperand> Operands,
const MachineRegisterInfo &RegInfo) const;
[[nodiscard]] Expected<void> validateOperandTies(Opcode InstructionOpcode,
std::span<const MachineOperand> ExplicitOperands,
std::span<const MachineOperandTie> Ties) const;
[[nodiscard]] Expected<void>
validateImplicitOperands(Opcode InstructionOpcode,
std::span<const MachineOperand *const> ImplicitOperands) const;
[[nodiscard]] Expected<void>
validateMemOperands(Opcode InstructionOpcode,
std::span<const MachineMemOperand *const> MemOperands) const;
[[nodiscard]] Expected<void> validateInstruction(const MachineInstr &Instruction,
const MachineRegisterInfo &RegInfo) const;
[[nodiscard]] Expected<std::vector<MachineOperand>>
createDefaultImplicitOperands(const InstructionDescriptor &Descriptor) const;
private:
InstructionSet(std::vector<InstructionDescriptor> Descriptors,
std::vector<ImmediatePredicate> ImmediatePredicates)
: Descriptors(std::move(Descriptors)), ImmediatePredicates(std::move(ImmediatePredicates)) {}
[[nodiscard]] const ImmediatePredicate *
immediatePredicate(ImmediatePredicateID ID) const noexcept;
[[nodiscard]] Expected<void> validateExplicitOperands(const InstructionDescriptor &Descriptor,
std::span<const MachineOperand> Operands,
const MachineRegisterInfo &RegInfo) const;
[[nodiscard]] Expected<void> validateOperandTies(const InstructionDescriptor &Descriptor,
std::span<const MachineOperand> ExplicitOperands,
std::span<const MachineOperandTie> Ties) const;
[[nodiscard]] Expected<void>
validateImplicitOperands(const InstructionDescriptor &Descriptor,
std::span<const MachineOperand *const> ImplicitOperands) const;
[[nodiscard]] Expected<void>
validateMemOperands(const InstructionDescriptor &Descriptor,
std::span<const MachineMemOperand *const> MemOperands) const;
[[nodiscard]] Expected<void> validateOperand(const OperandDescriptor &Descriptor,
const MachineOperand &Operand,
const MachineRegisterInfo &RegInfo) const;
std::vector<InstructionDescriptor> Descriptors;
std::vector<ImmediatePredicate> ImmediatePredicates;
std::unordered_map<Opcode, std::size_t> OpcodeToIndex;
std::unordered_map<std::string, std::size_t> NameToIndex;
std::unordered_map<ImmediatePredicateID, std::size_t> PredicateToIndex;
};
} // namespace rcc
#endif // RCC_CODEGEN_INSTRUCTIONDESCRIPTOR_H
|