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
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "rcc/CodeGen/GlobalISel/GIMatchTableExecutor.h"
#include "rcc/CodeGen/LowLevelType.h"
#include "rcc/CodeGen/MachineFunction.h"
#include "rcc/CodeGen/MachineInstr.h"
#include "rcc/CodeGen/MachineOperand.h"
#include "rcc/CodeGen/MachineRegisterInfo.h"
#include "rcc/CodeGen/TargetDescription.h"
#include <cstddef>
#include <optional>
#include <vector>
namespace rcc {
namespace {
[[nodiscard]] Expected<std::optional<LowLevelType>> operandType(const MachineInstr &Instruction,
const MachineFunction &Function,
std::size_t OperandIndex) {
const MachineOperand *Operand = Instruction.operandAt(OperandIndex);
const Register *Reg = Operand == nullptr ? nullptr : Operand->getIf<Register>();
if (Reg == nullptr || !Reg->isVirtual())
return std::optional<LowLevelType>{};
auto Info = Function.registerInfo().virtualRegisterInfo(*Reg);
if (!Info)
return std::unexpected(Info.error());
return Info->get().Type;
}
[[nodiscard]] Expected<bool> matchesType(const MachineInstr &Instruction,
const MachineFunction &Function,
const GIMatchTableInstruction &Step) {
auto Type = operandType(Instruction, Function, Step.OperandIndex);
if (!Type)
return std::unexpected(Type.error());
if (!*Type)
return false;
const auto Kind = static_cast<GIMatchTypeKind>(Step.Auxiliary);
switch (Kind) {
case GIMatchTypeKind::Any:
return Step.Value == 0 || (*Type)->scalarBitWidth() == Step.Value;
case GIMatchTypeKind::Scalar:
return (*Type)->isCategoryNeutralScalar() && (*Type)->scalarBitWidth() == Step.Value;
case GIMatchTypeKind::Pointer:
return (*Type)->isPointer() && (*Type)->scalarBitWidth() == Step.Value;
}
return makeUnexpected(ErrorCode::VerificationError,
"generated match table has an invalid type predicate");
}
[[nodiscard]] Expected<bool> matchesBank(const MachineInstr &Instruction,
const MachineFunction &Function,
const GIMatchTableInstruction &Step) {
const MachineOperand *Operand = Instruction.operandAt(Step.OperandIndex);
const Register *Reg = Operand == nullptr ? nullptr : Operand->getIf<Register>();
if (Reg == nullptr || !Reg->isVirtual())
return false;
auto Info = Function.registerInfo().virtualRegisterInfo(*Reg);
if (!Info)
return std::unexpected(Info.error());
const VirtualRegisterInfo &RegisterInfo = Info->get();
if (!RegisterInfo.Bank)
return false;
return RegisterInfo.Bank->value() == Step.Value;
}
[[nodiscard]] Expected<bool> matchesClass(const MachineInstr &Instruction,
const MachineFunction &Function,
const GIMatchTableInstruction &Step) {
const MachineOperand *Operand = Instruction.operandAt(Step.OperandIndex);
const Register *Reg = Operand == nullptr ? nullptr : Operand->getIf<Register>();
if (Reg == nullptr || !Reg->isVirtual())
return false;
auto Info = Function.registerInfo().virtualRegisterInfo(*Reg);
if (!Info)
return std::unexpected(Info.error());
const VirtualRegisterInfo &RegisterInfo = Info->get();
if (!RegisterInfo.RegClass)
return false;
return RegisterInfo.RegClass->value() == Step.Value;
}
struct MatchedRule final {
Opcode ResultOpcode;
std::vector<std::int64_t> AppendedImmediates;
};
[[nodiscard]] Expected<std::optional<MatchedRule>> matchRule(const GIMatchTable &Table,
const GIMatchTableRule &Rule,
const MachineInstr &Instruction,
const MachineFunction &Function) {
const std::size_t First = Rule.FirstInstruction;
const std::size_t Count = Rule.InstructionCount;
if (First > Table.Instructions.size() || Count > Table.Instructions.size() - First) {
return makeUnexpected(ErrorCode::VerificationError,
"generated match table rule has an invalid instruction range");
}
MatchedRule Result;
bool HasResultOpcode = false;
bool Applying = false;
bool Complete = false;
for (std::size_t Index = First; Index < First + Count; ++Index) {
const GIMatchTableInstruction &Step = Table.Instructions[Index];
switch (Step.Opcode) {
case GIMatchTableOpcode::CheckOpcode:
if (Applying)
return makeUnexpected(ErrorCode::VerificationError,
"generated match table performs a predicate after rendering");
if (Instruction.opcode().value() != Step.Value)
return std::optional<MatchedRule>{};
break;
case GIMatchTableOpcode::CheckOperandType: {
if (Applying)
return makeUnexpected(ErrorCode::VerificationError,
"generated match table performs a predicate after rendering");
auto Matched = matchesType(Instruction, Function, Step);
if (!Matched)
return std::unexpected(Matched.error());
if (!*Matched)
return std::optional<MatchedRule>{};
break;
}
case GIMatchTableOpcode::CheckOperandBank: {
if (Applying)
return makeUnexpected(ErrorCode::VerificationError,
"generated match table performs a predicate after rendering");
auto Matched = matchesBank(Instruction, Function, Step);
if (!Matched)
return std::unexpected(Matched.error());
if (!*Matched)
return std::optional<MatchedRule>{};
break;
}
case GIMatchTableOpcode::CheckOperandClass: {
if (Applying)
return makeUnexpected(ErrorCode::VerificationError,
"generated match table performs a predicate after rendering");
auto Matched = matchesClass(Instruction, Function, Step);
if (!Matched)
return std::unexpected(Matched.error());
if (!*Matched)
return std::optional<MatchedRule>{};
break;
}
case GIMatchTableOpcode::MutateOpcode:
if (Complete || HasResultOpcode || Step.Value == 0)
return makeUnexpected(ErrorCode::VerificationError,
"generated match table has an invalid opcode mutation");
Applying = true;
Result.ResultOpcode = Opcode::fromValue(Step.Value);
HasResultOpcode = true;
break;
case GIMatchTableOpcode::AppendImmediate:
if (Complete)
return makeUnexpected(ErrorCode::VerificationError,
"generated match table renders after completion");
Applying = true;
Result.AppendedImmediates.push_back(Step.Immediate);
break;
case GIMatchTableOpcode::Complete:
if (Complete || !HasResultOpcode || Index + 1 != First + Count)
return makeUnexpected(ErrorCode::VerificationError,
"generated match table has an invalid completion action");
Complete = true;
break;
}
}
if (!Complete || !HasResultOpcode)
return makeUnexpected(ErrorCode::VerificationError,
"generated match table rule does not complete rendering");
return std::optional<MatchedRule>(std::move(Result));
}
[[nodiscard]] Expected<void> applyRule(const MatchedRule &Rule, MachineInstr &Instruction,
const TargetDescription &Target) {
const InstructionDescriptor *Descriptor = Target.instructionDescriptor(Rule.ResultOpcode);
if (Descriptor == nullptr)
return makeUnexpected(ErrorCode::VerificationError,
"generated match table selects an unknown instruction opcode");
if (!Descriptor->Variadic &&
Descriptor->Operands.size() != Instruction.operandCount() + Rule.AppendedImmediates.size()) {
return makeUnexpected(ErrorCode::VerificationError,
"generated match table result has an incompatible operand count");
}
const Opcode OriginalOpcode = Instruction.opcode();
auto SetOpcode = Instruction.setOpcode(Rule.ResultOpcode);
if (!SetOpcode)
return std::unexpected(SetOpcode.error());
const std::size_t OriginalOperandCount = Instruction.operandCount();
for (const std::int64_t Immediate : Rule.AppendedImmediates) {
auto Added = Instruction.addOperand(MachineOperand::immediate(Immediate));
if (Added)
continue;
while (Instruction.operandCount() > OriginalOperandCount) {
auto Removed = Instruction.eraseOperand(Instruction.operandCount() - 1U);
if (!Removed)
return std::unexpected(Removed.error());
}
auto Restored = Instruction.setOpcode(OriginalOpcode);
if (!Restored)
return std::unexpected(Restored.error());
return std::unexpected(Added.error());
}
return {};
}
} // namespace
Expected<bool> GIMatchTableExecutor::match(const GIMatchTable &Table,
const MachineInstr &Instruction,
const MachineFunction &Function,
const TargetDescription &Target) const {
for (const GIMatchTableRule &Rule : Table.Rules) {
auto Match = matchRule(Table, Rule, Instruction, Function);
if (!Match)
return std::unexpected(Match.error());
if (!*Match)
continue;
if (Target.instructionDescriptor((*Match)->ResultOpcode) == nullptr) {
return makeUnexpected(ErrorCode::VerificationError,
"generated match table selects an unknown instruction opcode");
}
return true;
}
return false;
}
Expected<bool> GIMatchTableExecutor::select(const GIMatchTable &Table, MachineInstr &Instruction,
MachineFunction &Function,
const TargetDescription &Target) const {
for (const GIMatchTableRule &Rule : Table.Rules) {
auto Match = matchRule(Table, Rule, Instruction, Function);
if (!Match)
return std::unexpected(Match.error());
if (!*Match)
continue;
auto Applied = applyRule(**Match, Instruction, Target);
if (!Applied)
return std::unexpected(Applied.error());
return true;
}
return false;
}
} // namespace rcc
|