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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "rcc/CodeGen/InstructionDescriptor.h"
#include "rcc/CodeGen/MachineInstr.h"
#include "rcc/CodeGen/MachineMemOperand.h"
#include "rcc/CodeGen/MachineRegisterInfo.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
namespace rcc {
namespace {
[[nodiscard]] bool isRegisterOnly(const OperandKindSet &Kinds) noexcept {
OperandKindSet RegisterKind{MachineOperand::Kind::Register};
return Kinds == RegisterKind;
}
[[nodiscard]] bool isDefRole(RegisterOperandRole Role) noexcept {
return Role == RegisterOperandRole::Def || Role == RegisterOperandRole::DefUse;
}
[[nodiscard]] bool isUseRole(RegisterOperandRole Role) noexcept {
return Role == RegisterOperandRole::Use || Role == RegisterOperandRole::DefUse;
}
[[nodiscard]] bool formsDefUsePair(RegisterOperandRole Left, RegisterOperandRole Right) noexcept {
return (Left == RegisterOperandRole::Def && Right == RegisterOperandRole::Use) ||
(Left == RegisterOperandRole::Use && Right == RegisterOperandRole::Def);
}
[[nodiscard]] bool isPureDef(const MachineOperand &Operand) noexcept {
return Operand.isRegister() && Operand.isDef() && !Operand.isUse();
}
[[nodiscard]] bool isPureUse(const MachineOperand &Operand) noexcept {
return Operand.isRegister() && Operand.isUse() && !Operand.isDef();
}
[[nodiscard]] Expected<void> validateDescriptorStructure(const InstructionDescriptor &Descriptor) {
if (!Descriptor.InstructionOpcode.isValid())
return makeUnexpected(ErrorCode::InvalidArgument, "instruction descriptor opcode is invalid");
if (Descriptor.Name.empty())
return makeUnexpected(ErrorCode::InvalidArgument, "instruction descriptor name is empty");
if (Descriptor.NumExplicitDefs > Descriptor.Operands.size()) {
return makeUnexpected(ErrorCode::InvalidArgument,
"instruction descriptor explicit def count is out of range");
}
bool SawOptional = false;
std::optional<std::size_t> VariadicBegin;
std::optional<std::size_t> VariadicEnd;
for (std::size_t Index = 0; Index < Descriptor.Operands.size(); ++Index) {
const OperandDescriptor &Operand = Descriptor.Operands[Index];
if (Operand.Kinds.empty()) {
return makeUnexpected(ErrorCode::InvalidArgument,
"operand descriptor must accept at least one operand kind");
}
const bool IsRegister = Operand.Kinds.contains(MachineOperand::Kind::Register);
if (IsRegister != (Operand.RegisterRole != RegisterOperandRole::None)) {
return makeUnexpected(ErrorCode::InvalidArgument,
"register-capable operand descriptor must declare a register role");
}
if (isDefRole(Operand.RegisterRole) && !isRegisterOnly(Operand.Kinds)) {
return makeUnexpected(ErrorCode::InvalidArgument,
"register definition descriptor must accept only registers");
}
if (!IsRegister &&
(Operand.RegClass || Operand.RegBank || Operand.EarlyClobber || Operand.AllowSubReg)) {
return makeUnexpected(ErrorCode::InvalidArgument,
"non-register operand descriptor has register constraints");
}
if (Operand.TypeConstraint) {
if (!isRegisterOnly(Operand.Kinds) || Operand.TypeConstraint->Kinds.empty() ||
Operand.TypeConstraint->Kinds.contains(LLT::Kind::Invalid)) {
return makeUnexpected(ErrorCode::InvalidArgument,
"typed operand descriptor requires a nonempty register type set");
}
}
if (Operand.EarlyClobber && !isDefRole(Operand.RegisterRole)) {
return makeUnexpected(ErrorCode::InvalidArgument,
"early-clobber operand descriptor must define a register");
}
if (Descriptor.Variadic && Operand.Optional) {
return makeUnexpected(ErrorCode::InvalidArgument,
"variadic instruction descriptor cannot contain optional operands");
}
if (Operand.Optional)
SawOptional = true;
else if (SawOptional && !Operand.VariadicGroup) {
return makeUnexpected(ErrorCode::InvalidArgument,
"optional operand descriptors must form a trailing suffix");
}
if (Operand.VariadicGroup && (Operand.Optional || Operand.DefaultTiedOperand)) {
return makeUnexpected(ErrorCode::InvalidArgument,
"variadic operand descriptor cannot be optional or have a default tie");
}
if (Operand.VariadicGroup) {
if (!Descriptor.Variadic) {
return makeUnexpected(ErrorCode::InvalidArgument,
"non-variadic instruction descriptor has a variadic group");
}
if (!VariadicBegin)
VariadicBegin = Index;
VariadicEnd = Index + 1;
} else if (VariadicEnd) {
const bool LaterGroup =
std::ranges::any_of(Descriptor.Operands.begin() + static_cast<std::ptrdiff_t>(Index),
Descriptor.Operands.end(), [](const OperandDescriptor &Candidate) {
return Candidate.VariadicGroup;
});
if (LaterGroup) {
return makeUnexpected(ErrorCode::InvalidArgument,
"variadic operand group descriptors must be contiguous");
}
}
}
if (Descriptor.Variadic && !VariadicBegin) {
return makeUnexpected(ErrorCode::InvalidArgument,
"variadic instruction descriptor requires a variadic group");
}
if (!Descriptor.Variadic && Descriptor.MinimumVariadicRepetitions != 0) {
return makeUnexpected(ErrorCode::InvalidArgument,
"non-variadic instruction has a minimum variadic repetition count");
}
bool SawUse = false;
std::size_t MinimumDefs = 0;
for (std::size_t Index = 0; Index < Descriptor.Operands.size(); ++Index) {
const OperandDescriptor &Operand = Descriptor.Operands[Index];
const std::size_t Repetitions =
Operand.VariadicGroup ? Descriptor.MinimumVariadicRepetitions : 1;
for (std::size_t Repeat = 0; Repeat < Repetitions; ++Repeat) {
if (isDefRole(Operand.RegisterRole)) {
if (SawUse) {
return makeUnexpected(ErrorCode::InvalidArgument,
"register definition appears after an instruction use");
}
++MinimumDefs;
} else {
SawUse = true;
}
}
}
if (Descriptor.NumExplicitDefs != MinimumDefs) {
return makeUnexpected(ErrorCode::InvalidArgument,
"instruction descriptor explicit def count does not match its shape");
}
for (std::size_t Index = 0; Index < Descriptor.Operands.size(); ++Index) {
const OperandDescriptor &Operand = Descriptor.Operands[Index];
if (!Operand.DefaultTiedOperand)
continue;
const std::size_t OtherIndex = *Operand.DefaultTiedOperand;
if (OtherIndex >= Descriptor.Operands.size() || OtherIndex == Index ||
Descriptor.Operands[OtherIndex].DefaultTiedOperand != Index) {
return makeUnexpected(ErrorCode::InvalidArgument,
"default operand tie must be reciprocal and in range");
}
if (Operand.Optional || Descriptor.Operands[OtherIndex].Optional ||
!isRegisterOnly(Operand.Kinds) || !isRegisterOnly(Descriptor.Operands[OtherIndex].Kinds) ||
!formsDefUsePair(Operand.RegisterRole, Descriptor.Operands[OtherIndex].RegisterRole)) {
return makeUnexpected(ErrorCode::InvalidArgument,
"default operand tie must connect required register def and use");
}
}
std::unordered_set<NamedOperandID> OperandNames;
for (const OperandDescriptor &Operand : Descriptor.Operands) {
if (Operand.Name && !OperandNames.insert(*Operand.Name).second) {
return makeUnexpected(ErrorCode::InvalidArgument,
"instruction descriptor has duplicate named operands");
}
}
std::unordered_set<PhysicalRegister> ImplicitDefs;
for (const ImplicitRegisterDescriptor &Implicit : Descriptor.ImplicitDefs) {
if (!Implicit.Reg.isValid() || !ImplicitDefs.insert(Implicit.Reg).second) {
return makeUnexpected(ErrorCode::InvalidArgument,
"implicit definitions must name unique physical registers");
}
}
std::unordered_set<PhysicalRegister> ImplicitUses;
for (const ImplicitRegisterDescriptor &Implicit : Descriptor.ImplicitUses) {
if (!Implicit.Reg.isValid() || Implicit.EarlyClobber ||
!ImplicitUses.insert(Implicit.Reg).second) {
return makeUnexpected(ErrorCode::InvalidArgument,
"implicit uses must name unique physical registers and cannot "
"early-clobber");
}
}
const bool IsBranch = Descriptor.Flags.has(InstructionFlag::Branch);
const bool IsTerminator = Descriptor.Flags.has(InstructionFlag::Terminator);
if ((IsBranch || Descriptor.Flags.has(InstructionFlag::Return) ||
Descriptor.Flags.has(InstructionFlag::Barrier) ||
Descriptor.Flags.has(InstructionFlag::IndirectBranch)) &&
!IsTerminator) {
return makeUnexpected(ErrorCode::InvalidArgument,
"branch, return, and barrier descriptors must be terminators");
}
if (Descriptor.Flags.has(InstructionFlag::IndirectBranch) && !IsBranch) {
return makeUnexpected(ErrorCode::InvalidArgument,
"indirect branch descriptor must also be a branch");
}
const bool IsPseudo = Descriptor.Flags.has(InstructionFlag::Pseudo);
const bool IsMeta = Descriptor.Flags.has(InstructionFlag::Meta);
if (IsMeta && (!IsPseudo || Descriptor.Flags.has(InstructionFlag::ProducesCode))) {
return makeUnexpected(ErrorCode::InvalidArgument,
"meta instruction must be a non-emitting pseudo");
}
if (!IsPseudo && Descriptor.Owner != PseudoOwner::None) {
return makeUnexpected(ErrorCode::InvalidArgument,
"non-pseudo instruction cannot have a pseudo owner");
}
if (IsPseudo && !IsMeta && Descriptor.Owner == PseudoOwner::None) {
return makeUnexpected(ErrorCode::InvalidArgument,
"executable pseudo must declare its expansion owner");
}
if (Descriptor.Flags.has(InstructionFlag::TerminalEmission) &&
(!IsPseudo || Descriptor.Owner != PseudoOwner::AsmPrinter ||
!Descriptor.Flags.has(InstructionFlag::ProducesCode))) {
return makeUnexpected(ErrorCode::InvalidArgument,
"terminal emission pseudo must produce code and be owned by AsmPrinter");
}
if (Descriptor.RequiresMemoryOperands && !Descriptor.Flags.has(InstructionFlag::MayLoad) &&
!Descriptor.Flags.has(InstructionFlag::MayStore)) {
return makeUnexpected(ErrorCode::InvalidArgument, "MMO requirement needs mayLoad or mayStore");
}
return {};
}
struct OperandShape final {
std::size_t VariadicBegin = 0;
std::size_t VariadicEnd = 0;
std::size_t Repetitions = 0;
};
[[nodiscard]] Expected<OperandShape> resolveOperandShape(const InstructionDescriptor &Descriptor,
std::size_t OperandCount) {
if (!Descriptor.Variadic) {
std::size_t RequiredCount = Descriptor.Operands.size();
while (RequiredCount != 0 && Descriptor.Operands[RequiredCount - 1].Optional)
--RequiredCount;
if (OperandCount < RequiredCount || OperandCount > Descriptor.Operands.size()) {
return makeUnexpected(ErrorCode::VerificationError,
"instruction has an invalid explicit operand count");
}
return OperandShape{};
}
const auto Begin = std::ranges::find_if(
Descriptor.Operands, [](const OperandDescriptor &Operand) { return Operand.VariadicGroup; });
const auto End = std::ranges::find_if_not(
Begin, Descriptor.Operands.end(),
[](const OperandDescriptor &Operand) { return Operand.VariadicGroup; });
const std::size_t VariadicBegin =
static_cast<std::size_t>(std::distance(Descriptor.Operands.begin(), Begin));
const std::size_t VariadicEnd =
static_cast<std::size_t>(std::distance(Descriptor.Operands.begin(), End));
const std::size_t GroupSize = VariadicEnd - VariadicBegin;
const std::size_t FixedCount = Descriptor.Operands.size() - GroupSize;
if (GroupSize == 0 || OperandCount < FixedCount) {
return makeUnexpected(ErrorCode::VerificationError,
"instruction has an invalid variadic operand count");
}
const std::size_t VariableCount = OperandCount - FixedCount;
if (VariableCount % GroupSize != 0 ||
VariableCount / GroupSize < Descriptor.MinimumVariadicRepetitions) {
return makeUnexpected(ErrorCode::VerificationError,
"instruction has an incomplete or undersized variadic operand group");
}
return OperandShape{VariadicBegin, VariadicEnd, VariableCount / GroupSize};
}
[[nodiscard]] const OperandDescriptor &descriptorForOperand(const InstructionDescriptor &Descriptor,
const OperandShape &Shape,
std::size_t Index) {
if (!Descriptor.Variadic)
return Descriptor.Operands[Index];
if (Index < Shape.VariadicBegin)
return Descriptor.Operands[Index];
const std::size_t GroupSize = Shape.VariadicEnd - Shape.VariadicBegin;
const std::size_t VariableCount = Shape.Repetitions * GroupSize;
if (Index < Shape.VariadicBegin + VariableCount) {
return Descriptor.Operands[Shape.VariadicBegin + (Index - Shape.VariadicBegin) % GroupSize];
}
return Descriptor.Operands[Shape.VariadicEnd + (Index - Shape.VariadicBegin - VariableCount)];
}
} // namespace
OperandKindSet::OperandKindSet(std::initializer_list<MachineOperand::Kind> Kinds) noexcept {
for (const MachineOperand::Kind Kind : Kinds)
add(Kind);
}
LLTKindSet::LLTKindSet(std::initializer_list<LLT::Kind> Kinds) noexcept {
for (const LLT::Kind Kind : Kinds)
add(Kind);
}
InstructionFlags::InstructionFlags(std::initializer_list<InstructionFlag> Flags) noexcept {
for (const InstructionFlag Flag : Flags)
set(Flag);
}
Expected<InstructionSet>
InstructionSet::create(std::vector<InstructionDescriptor> Descriptors,
std::vector<ImmediatePredicate> ImmediatePredicates) {
if (Descriptors.empty()) {
return makeUnexpected(ErrorCode::InvalidArgument,
"instruction set requires at least one descriptor");
}
InstructionSet Result(std::move(Descriptors), std::move(ImmediatePredicates));
std::unordered_set<std::string> PredicateNames;
for (std::size_t Index = 0; Index < Result.ImmediatePredicates.size(); ++Index) {
const ImmediatePredicate &Predicate = Result.ImmediatePredicates[Index];
if (Predicate.Name.empty() || !Predicate.Predicate) {
return makeUnexpected(ErrorCode::InvalidArgument,
"immediate predicate requires a name and callable");
}
if (!Result.PredicateToIndex.emplace(Predicate.ID, Index).second) {
return makeUnexpected(ErrorCode::InvalidArgument, "duplicate immediate predicate ID");
}
if (!PredicateNames.insert(Predicate.Name).second) {
return makeUnexpected(ErrorCode::InvalidArgument, "duplicate immediate predicate name");
}
}
for (std::size_t Index = 0; Index < Result.Descriptors.size(); ++Index) {
const InstructionDescriptor &Descriptor = Result.Descriptors[Index];
const auto Validation = validateDescriptorStructure(Descriptor);
if (!Validation)
return std::unexpected(Validation.error());
if (!Result.OpcodeToIndex.emplace(Descriptor.InstructionOpcode, Index).second)
return makeUnexpected(ErrorCode::InvalidArgument, "duplicate instruction opcode");
if (!Result.NameToIndex.emplace(Descriptor.Name, Index).second)
return makeUnexpected(ErrorCode::InvalidArgument, "duplicate instruction name");
for (const OperandDescriptor &Operand : Descriptor.Operands) {
if (Operand.ImmediatePredicate &&
Result.immediatePredicate(*Operand.ImmediatePredicate) == nullptr) {
return makeUnexpected(ErrorCode::InvalidArgument,
"operand descriptor references an unknown immediate predicate");
}
}
}
return Result;
}
const InstructionDescriptor *InstructionSet::descriptor(Opcode InstructionOpcode) const noexcept {
const auto Position = OpcodeToIndex.find(InstructionOpcode);
if (Position == OpcodeToIndex.end())
return nullptr;
return &Descriptors[Position->second];
}
const InstructionDescriptor *InstructionSet::descriptor(std::string_view Name) const noexcept {
const auto Position = NameToIndex.find(std::string(Name));
if (Position == NameToIndex.end())
return nullptr;
return &Descriptors[Position->second];
}
Expected<void> InstructionSet::validateExplicitOperands(const InstructionDescriptor &Descriptor,
std::span<const MachineOperand> Operands,
const MachineRegisterInfo &RegInfo) const {
auto Shape = resolveOperandShape(Descriptor, Operands.size());
if (!Shape)
return std::unexpected(Shape.error());
bool SawUse = false;
std::unordered_map<std::uint16_t, LLT> EqualityGroups;
for (std::size_t Index = 0; Index < Operands.size(); ++Index) {
const MachineOperand &Operand = Operands[Index];
if (Operand.isImplicit()) {
return makeUnexpected(ErrorCode::VerificationError,
"explicit operand sequence contains an implicit register");
}
const OperandDescriptor &OperandDesc = descriptorForOperand(Descriptor, *Shape, Index);
const auto Validation = validateOperand(OperandDesc, Operand, RegInfo);
if (!Validation) {
return makeUnexpected(
Validation.error().code(),
"operand " + std::to_string(Index) + " of '" + Descriptor.Name + "': " +
std::string(Validation.error().message()));
}
if (Operand.isRegister()) {
if (isDefRole(OperandDesc.RegisterRole)) {
if (SawUse) {
return makeUnexpected(ErrorCode::VerificationError,
"instruction register definition appears after a use");
}
} else {
SawUse = true;
}
} else {
SawUse = true;
}
if (!OperandDesc.TypeConstraint)
continue;
const Register *Reg = Operand.getIf<Register>();
if (Reg == nullptr || !Reg->isVirtual()) {
return makeUnexpected(ErrorCode::VerificationError,
"typed instruction operand must use a virtual register");
}
auto Info = RegInfo.virtualRegisterInfo(*Reg);
if (!Info)
return std::unexpected(Info.error());
const std::optional<LLT> &RegisterType = Info->get().Type;
if (!RegisterType) {
return makeUnexpected(ErrorCode::VerificationError,
"typed instruction operand virtual register has no LLT");
}
const LLT Type = RegisterType.value();
if (!OperandDesc.TypeConstraint->Kinds.contains(Type.kind())) {
return makeUnexpected(ErrorCode::VerificationError,
"instruction operand LLT kind is not permitted by its descriptor");
}
if (OperandDesc.TypeConstraint->EqualityGroup) {
const auto [Position, Inserted] =
EqualityGroups.emplace(*OperandDesc.TypeConstraint->EqualityGroup, Type);
if (!Inserted && Position->second != Type) {
return makeUnexpected(ErrorCode::VerificationError,
"instruction operands in one LLT group have different types");
}
}
}
if (Descriptor.VerifyExplicitOperands != nullptr)
return Descriptor.VerifyExplicitOperands(Descriptor.InstructionOpcode, Operands, RegInfo);
return {};
}
Expected<void> InstructionSet::validateExplicitOperands(Opcode InstructionOpcode,
std::span<const MachineOperand> Operands,
const MachineRegisterInfo &RegInfo) const {
const InstructionDescriptor *Descriptor = descriptor(InstructionOpcode);
if (Descriptor == nullptr) {
return makeUnexpected(ErrorCode::VerificationError, "instruction opcode is not registered");
}
return validateExplicitOperands(*Descriptor, Operands, RegInfo);
}
Expected<void> InstructionSet::validateOperandTies(const InstructionDescriptor &Descriptor,
std::span<const MachineOperand> ExplicitOperands,
std::span<const MachineOperandTie> Ties) const {
std::vector<std::optional<std::size_t>> Partners(ExplicitOperands.size());
for (const MachineOperandTie Tie : Ties) {
if (Tie.DefOperand >= ExplicitOperands.size() || Tie.UseOperand >= ExplicitOperands.size() ||
Tie.DefOperand == Tie.UseOperand || !isPureDef(ExplicitOperands[Tie.DefOperand]) ||
!isPureUse(ExplicitOperands[Tie.UseOperand])) {
return makeUnexpected(ErrorCode::VerificationError,
"instruction tie must connect an in-range pure register def and use");
}
if (Partners[Tie.DefOperand] || Partners[Tie.UseOperand]) {
return makeUnexpected(ErrorCode::VerificationError,
"instruction operand participates in duplicate or conflicting ties");
}
Partners[Tie.DefOperand] = Tie.UseOperand;
Partners[Tie.UseOperand] = Tie.DefOperand;
}
for (std::size_t Index = 0; Index < Descriptor.Operands.size(); ++Index) {
const OperandDescriptor &Operand = Descriptor.Operands[Index];
if (Operand.DefaultTiedOperand &&
(Index >= Partners.size() || Partners[Index] != Operand.DefaultTiedOperand)) {
return makeUnexpected(ErrorCode::VerificationError,
"required descriptor tie is not materialized");
}
}
return {};
}
Expected<void> InstructionSet::validateOperandTies(Opcode InstructionOpcode,
std::span<const MachineOperand> ExplicitOperands,
std::span<const MachineOperandTie> Ties) const {
const InstructionDescriptor *Descriptor = descriptor(InstructionOpcode);
if (Descriptor == nullptr) {
return makeUnexpected(ErrorCode::VerificationError, "instruction opcode is not registered");
}
return validateOperandTies(*Descriptor, ExplicitOperands, Ties);
}
Expected<void> InstructionSet::validateImplicitOperands(
const InstructionDescriptor &Descriptor,
std::span<const MachineOperand *const> ImplicitOperands) const {
const std::size_t DefaultCount = Descriptor.ImplicitDefs.size() + Descriptor.ImplicitUses.size();
if (ImplicitOperands.size() < DefaultCount ||
(!Descriptor.AllowExtraImplicitOperands && ImplicitOperands.size() != DefaultCount)) {
return makeUnexpected(ErrorCode::VerificationError,
"instruction implicit operand count does not match its descriptor");
}
std::size_t Index = 0;
auto ValidateDefault = [&](const ImplicitRegisterDescriptor &Implicit,
bool IsDef) -> Expected<void> {
const MachineOperand *Operand = ImplicitOperands[Index++];
const Register *Reg = Operand == nullptr ? nullptr : Operand->getIf<Register>();
if (Reg == nullptr || Reg->physicalRegister() != Implicit.Reg || Operand->isDef() != IsDef ||
Operand->isUse() == IsDef || !Operand->isImplicit() ||
Operand->registerFlags().has(RegisterOperandFlag::EarlyClobber) != Implicit.EarlyClobber ||
Operand->registerFlags().has(RegisterOperandFlag::Renamable) != Implicit.Renamable) {
return makeUnexpected(ErrorCode::VerificationError,
"default implicit register operand does not match its descriptor");
}
return {};
};
for (const ImplicitRegisterDescriptor &Implicit : Descriptor.ImplicitDefs) {
auto Validation = ValidateDefault(Implicit, true);
if (!Validation)
return std::unexpected(Validation.error());
}
for (const ImplicitRegisterDescriptor &Implicit : Descriptor.ImplicitUses) {
auto Validation = ValidateDefault(Implicit, false);
if (!Validation)
return std::unexpected(Validation.error());
}
for (; Index < ImplicitOperands.size(); ++Index) {
const MachineOperand *Operand = ImplicitOperands[Index];
if (Operand == nullptr || !Operand->isRegister() || !Operand->isImplicit()) {
return makeUnexpected(ErrorCode::VerificationError,
"dynamic implicit operand must be an implicit register");
}
}
return {};
}
Expected<void> InstructionSet::validateImplicitOperands(
Opcode InstructionOpcode, std::span<const MachineOperand *const> ImplicitOperands) const {
const InstructionDescriptor *Descriptor = descriptor(InstructionOpcode);
if (Descriptor == nullptr) {
return makeUnexpected(ErrorCode::VerificationError, "instruction opcode is not registered");
}
return validateImplicitOperands(*Descriptor, ImplicitOperands);
}
Expected<void>
InstructionSet::validateMemOperands(const InstructionDescriptor &Descriptor,
std::span<const MachineMemOperand *const> MemOperands) const {
const bool MayLoad = Descriptor.Flags.has(InstructionFlag::MayLoad);
const bool MayStore = Descriptor.Flags.has(InstructionFlag::MayStore);
if (Descriptor.RequiresMemoryOperands && MemOperands.empty()) {
return makeUnexpected(ErrorCode::VerificationError,
"memory instruction is missing MachineMemOperand");
}
if (!MayLoad && !MayStore && !MemOperands.empty()) {
return makeUnexpected(ErrorCode::VerificationError,
"non-memory instruction carries MachineMemOperand");
}
for (const MachineMemOperand *MemOperand : MemOperands) {
if (MemOperand == nullptr || (MemOperand->isLoad() && !MayLoad) ||
(MemOperand->isStore() && !MayStore)) {
return makeUnexpected(ErrorCode::VerificationError,
"MachineMemOperand access conflicts with instruction descriptor");
}
}
return {};
}
Expected<void>
InstructionSet::validateMemOperands(Opcode InstructionOpcode,
std::span<const MachineMemOperand *const> MemOperands) const {
const InstructionDescriptor *Descriptor = descriptor(InstructionOpcode);
if (Descriptor == nullptr) {
return makeUnexpected(ErrorCode::VerificationError, "instruction opcode is not registered");
}
return validateMemOperands(*Descriptor, MemOperands);
}
Expected<void> InstructionSet::validateOperand(const OperandDescriptor &Descriptor,
const MachineOperand &Operand,
const MachineRegisterInfo &RegInfo) const {
if (!Descriptor.Kinds.contains(Operand.kind())) {
return makeUnexpected(ErrorCode::VerificationError,
"instruction operand kind does not match its descriptor");
}
if (!Descriptor.AllowAnyTargetFlags &&
(Operand.targetFlags() & ~Descriptor.AllowedTargetFlags) != 0) {
return makeUnexpected(ErrorCode::VerificationError,
"instruction operand uses unregistered target flags");
}
if (Operand.isRegister()) {
const bool IsDef = Operand.isDef();
const bool IsUse = Operand.isUse();
const bool ExpectedDef = isDefRole(Descriptor.RegisterRole);
const bool ExpectedUse = isUseRole(Descriptor.RegisterRole);
if (Descriptor.RegisterRole == RegisterOperandRole::Any) {
if (!IsDef && !IsUse) {
return makeUnexpected(ErrorCode::VerificationError,
"unclassified register operand must define or use its register");
}
} else if (IsDef != ExpectedDef || IsUse != ExpectedUse) {
return makeUnexpected(ErrorCode::VerificationError,
"register operand def/use role does not match its descriptor");
}
if (Operand.registerFlags().has(RegisterOperandFlag::EarlyClobber) != Descriptor.EarlyClobber) {
return makeUnexpected(ErrorCode::VerificationError,
"register operand early-clobber does not match its descriptor");
}
if (!Descriptor.AllowSubReg && Operand.subReg().value() != 0) {
return makeUnexpected(ErrorCode::VerificationError,
"register operand has a disallowed subregister index");
}
const Register *Reg = Operand.getIf<Register>();
if (Reg == nullptr) {
return makeUnexpected(ErrorCode::InternalError, "register operand has inconsistent storage");
}
if (Reg->isVirtual()) {
auto Info = RegInfo.virtualRegisterInfo(*Reg);
if (!Info)
return std::unexpected(Info.error());
}
}
if (Descriptor.ImmediatePredicate && Operand.kind() == MachineOperand::Kind::Immediate) {
const ImmediatePredicate *Predicate = immediatePredicate(*Descriptor.ImmediatePredicate);
if (Predicate == nullptr || !Predicate->Predicate(Operand)) {
return makeUnexpected(ErrorCode::VerificationError,
"instruction operand fails its immediate predicate");
}
}
return {};
}
Expected<void> InstructionSet::validateInstruction(const MachineInstr &Instruction,
const MachineRegisterInfo &RegInfo) const {
const InstructionDescriptor *Descriptor = descriptor(Instruction.opcode());
if (Descriptor == nullptr)
return makeUnexpected(ErrorCode::VerificationError, "instruction opcode is not registered");
const auto AllOperands = Instruction.operands();
std::size_t ExplicitCount = 0;
while (ExplicitCount < AllOperands.size() && !AllOperands[ExplicitCount].isImplicit())
++ExplicitCount;
for (std::size_t Index = ExplicitCount; Index < AllOperands.size(); ++Index) {
if (!AllOperands[Index].isImplicit()) {
return makeUnexpected(ErrorCode::VerificationError,
"explicit operand appears after implicit operands");
}
}
const auto ExplicitValidation =
validateExplicitOperands(*Descriptor, AllOperands.first(ExplicitCount), RegInfo);
if (!ExplicitValidation)
return std::unexpected(ExplicitValidation.error());
std::vector<MachineOperandTie> Ties;
for (std::size_t Index = 0; Index < ExplicitCount; ++Index) {
const MachineOperand &Operand = AllOperands[Index];
if (!Operand.tiedOperandIndex())
continue;
const std::size_t OtherIndex = *Operand.tiedOperandIndex();
if (OtherIndex >= ExplicitCount || AllOperands[OtherIndex].tiedOperandIndex() != Index) {
return makeUnexpected(ErrorCode::VerificationError,
"instruction operand tie is out of range or not reciprocal");
}
if (Index > OtherIndex)
continue;
if (isPureUse(Operand) && isPureDef(AllOperands[OtherIndex]))
Ties.push_back(MachineOperandTie{OtherIndex, Index});
else
Ties.push_back(MachineOperandTie{Index, OtherIndex});
}
auto TieValidation = validateOperandTies(*Descriptor, AllOperands.first(ExplicitCount), Ties);
if (!TieValidation)
return std::unexpected(TieValidation.error());
std::vector<const MachineOperand *> ImplicitOperands;
ImplicitOperands.reserve(AllOperands.size() - ExplicitCount);
for (std::size_t Index = ExplicitCount; Index < AllOperands.size(); ++Index) {
if (AllOperands[Index].tiedOperandIndex()) {
return makeUnexpected(ErrorCode::VerificationError,
"implicit register operand cannot participate in a tie");
}
ImplicitOperands.push_back(&AllOperands[Index]);
}
auto ImplicitValidation = validateImplicitOperands(*Descriptor, ImplicitOperands);
if (!ImplicitValidation)
return std::unexpected(ImplicitValidation.error());
auto MemoryValidation = validateMemOperands(*Descriptor, Instruction.memOperands());
if (!MemoryValidation)
return std::unexpected(MemoryValidation.error());
if (Descriptor->VerifyInstructionSemantics != nullptr)
return Descriptor->VerifyInstructionSemantics(Instruction, RegInfo);
return {};
}
Expected<std::vector<MachineOperand>>
InstructionSet::createDefaultImplicitOperands(const InstructionDescriptor &Descriptor) const {
std::vector<MachineOperand> Result;
Result.reserve(Descriptor.ImplicitDefs.size() + Descriptor.ImplicitUses.size());
auto AppendImplicit = [&Result](const ImplicitRegisterDescriptor &Implicit,
bool IsDef) -> Expected<void> {
RegisterOperandFlags Flags{
IsDef ? RegisterOperandFlag::Define : RegisterOperandFlag::Use,
RegisterOperandFlag::Implicit,
};
if (Implicit.EarlyClobber)
Flags.set(RegisterOperandFlag::EarlyClobber);
if (Implicit.Renamable)
Flags.set(RegisterOperandFlag::Renamable);
auto Operand = MachineOperand::registerOperand(Register::fromPhysical(Implicit.Reg), Flags);
if (!Operand)
return std::unexpected(Operand.error());
Result.push_back(std::move(*Operand));
return {};
};
for (const ImplicitRegisterDescriptor &Implicit : Descriptor.ImplicitDefs) {
auto AppendResult = AppendImplicit(Implicit, true);
if (!AppendResult)
return std::unexpected(AppendResult.error());
}
for (const ImplicitRegisterDescriptor &Implicit : Descriptor.ImplicitUses) {
auto AppendResult = AppendImplicit(Implicit, false);
if (!AppendResult)
return std::unexpected(AppendResult.error());
}
return Result;
}
const ImmediatePredicate *
InstructionSet::immediatePredicate(ImmediatePredicateID ID) const noexcept {
const auto Position = PredicateToIndex.find(ID);
if (Position == PredicateToIndex.end())
return nullptr;
return &ImmediatePredicates[Position->second];
}
} // namespace rcc
|