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
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "rcc/CodeGen/MachineCombiner.h"
#include "rcc/CodeGen/InstructionDescriptor.h"
#include "rcc/CodeGen/LiveVariables.h"
#include "rcc/CodeGen/MachineBasicBlock.h"
#include "rcc/CodeGen/MachineFunction.h"
#include "rcc/CodeGen/MachineFunctionCloner.h"
#include "rcc/CodeGen/MachineInstr.h"
#include "rcc/CodeGen/MachineOperand.h"
#include "rcc/CodeGen/MachineRegisterInfo.h"
#include "rcc/CodeGen/TargetDescription.h"
#include "rcc/CodeGen/TargetRegisterInfo.h"
#include <cstddef>
#include <cstdint>
#include <optional>
#include <span>
#include <string>
#include <utility>
#include <vector>
namespace rcc {
namespace {
[[nodiscard]] const MachinePassContract &combinerContract(MachineCombinerStage Stage) {
static const MachinePassContract PreLegalizer = [] {
MachinePassContract Value{
.Name = "riscv-prelegalizer-combiner",
.Requires = {MachineFunctionProperty::IsSSA},
.Sets = {},
.Clears = {},
.RequiredAnalyses = {},
.VerificationStage = PipelineStage::MachineIRInput,
};
Value.ThreadSafety = PassThreadSafety::ThreadSafe;
Value.DeclaredChanges = {MachinePassChange::Instructions, MachinePassChange::Operands,
MachinePassChange::Registers};
return Value;
}();
static const MachinePassContract SelectedSSA = [] {
MachinePassContract Value{
.Name = "machine-combiner-selected-ssa",
.Requires = {MachineFunctionProperty::Selected, MachineFunctionProperty::IsSSA},
.Sets = {},
.Clears = {},
.RequiredAnalyses = {},
.VerificationStage = PipelineStage::Selected,
};
Value.ThreadSafety = PassThreadSafety::ThreadSafe;
Value.DeclaredChanges = {MachinePassChange::Instructions, MachinePassChange::Operands,
MachinePassChange::Registers};
return Value;
}();
static const MachinePassContract PreRA = [] {
MachinePassContract Value{
.Name = "machine-combiner-pre-ra",
.Requires = {MachineFunctionProperty::Selected, MachineFunctionProperty::NoPHIs,
MachineFunctionProperty::TiedOpsRewritten},
.Sets = {},
.Clears = {},
.RequiredAnalyses = {},
.VerificationStage = PipelineStage::TiedOpsRewritten,
};
Value.ThreadSafety = PassThreadSafety::ThreadSafe;
Value.DeclaredChanges = {MachinePassChange::Instructions, MachinePassChange::Operands,
MachinePassChange::Registers};
return Value;
}();
static const MachinePassContract PostRA = [] {
MachinePassContract Value{
.Name = "machine-combiner-post-ra",
.Requires = {},
.Sets = {},
.Clears = {},
.RequiredAnalyses = {},
.VerificationStage = PipelineStage::RegisterAllocated,
};
Value.ThreadSafety = PassThreadSafety::ThreadSafe;
Value.DeclaredChanges = {MachinePassChange::Instructions, MachinePassChange::Operands,
MachinePassChange::Registers};
return Value;
}();
switch (Stage) {
case MachineCombinerStage::PreLegalizer:
return PreLegalizer;
case MachineCombinerStage::SelectedSSA:
return SelectedSSA;
case MachineCombinerStage::PreRegisterAllocation:
return PreRA;
case MachineCombinerStage::PostRegisterAllocation:
return PostRA;
}
return PreLegalizer;
}
[[nodiscard]] bool hasProtectedAttachment(const MachineInstr &Instruction) noexcept {
return Instruction.flags().has(MachineInstrFlag::FrameSetup) ||
Instruction.flags().has(MachineInstrFlag::FrameDestroy) ||
Instruction.debugInstrNumber() != 0 || Instruction.preInstrSymbol().has_value() ||
Instruction.postInstrSymbol().has_value() || Instruction.heapAllocMarker().has_value() ||
Instruction.pcSections().has_value() || Instruction.mmra().has_value() ||
Instruction.cfiType().has_value() || Instruction.deactivationSymbol().has_value();
}
[[nodiscard]] bool matchesPredicate(const MachineInstr &Instruction,
const MachineCombinerPredicate &Predicate) {
const MachineOperand *Operand = Instruction.operandAt(Predicate.Operand);
if (Operand == nullptr)
return false;
switch (Predicate.Kind) {
case MachineCombinerPredicateKind::ImmediateEquals: {
const auto *Immediate = Operand->getIf<std::int64_t>();
return Immediate != nullptr && *Immediate == Predicate.Value;
}
case MachineCombinerPredicateKind::RegisterEquals: {
const MachineOperand *Other = Instruction.operandAt(Predicate.OtherOperand);
const Register *LeftReg = Operand->getIf<Register>();
const Register *RightReg = Other == nullptr ? nullptr : Other->getIf<Register>();
return LeftReg != nullptr && RightReg != nullptr && *LeftReg == *RightReg &&
Operand->subReg() == Other->subReg();
}
case MachineCombinerPredicateKind::PhysicalRegisterEquals: {
const Register *Reg = Operand->getIf<Register>();
const std::optional<PhysicalRegister> Physical =
Reg == nullptr ? std::nullopt : Reg->physicalRegister();
return Physical && Physical->number() == static_cast<std::uint32_t>(Predicate.Value);
}
case MachineCombinerPredicateKind::RegisterIsVirtual: {
const Register *Reg = Operand->getIf<Register>();
return Reg != nullptr && Reg->isVirtual();
}
case MachineCombinerPredicateKind::RegisterIsPhysical: {
const Register *Reg = Operand->getIf<Register>();
return Reg != nullptr && Reg->isPhysical();
}
}
return false;
}
[[nodiscard]] bool matchesPattern(const MachineInstr &Instruction,
const MachineCombinerPattern &Pattern,
MachineCombinerStage Stage) {
if ((Pattern.Stages & machineCombinerStageMask(Stage)) == 0)
return false;
for (const MachineCombinerPredicate &Predicate : Pattern.Predicates) {
if (!matchesPredicate(Instruction, Predicate))
return false;
}
return true;
}
[[nodiscard]] bool compatibleReplacement(Register Destination, Register Source,
const MachineFunction &Function,
const TargetDescription &Target) {
if (!Destination.isVirtual() || Source.isNoRegister())
return false;
auto DestinationInfo = Function.registerInfo().virtualRegisterInfo(Destination);
if (!DestinationInfo)
return false;
const VirtualRegisterInfo &DestinationValue = DestinationInfo->get();
if (Source.isPhysical()) {
const std::optional<PhysicalRegister> Physical = Source.physicalRegister();
if (!Physical)
return false;
if (DestinationValue.RegClass &&
!Target.registers().registerClassContains(*DestinationValue.RegClass, *Physical)) {
return false;
}
return !DestinationValue.Bank ||
Target.registers().bankContainsRegister(*DestinationValue.Bank, *Physical);
}
auto SourceInfo = Function.registerInfo().virtualRegisterInfo(Source);
if (!SourceInfo)
return false;
const VirtualRegisterInfo &SourceValue = SourceInfo->get();
if (DestinationValue.Type != SourceValue.Type)
return false;
if (DestinationValue.Bank &&
(!SourceValue.Bank ||
!Target.registers().registerBanksOverlap(*DestinationValue.Bank, *SourceValue.Bank))) {
return false;
}
if (DestinationValue.RegClass &&
(!SourceValue.RegClass || !Target.registers().registerClassesOverlap(
*DestinationValue.RegClass, *SourceValue.RegClass))) {
return false;
}
return true;
}
[[nodiscard]] Expected<bool> executeReplacement(MachineInstr &Instruction,
const MachineCombinerAction &Action,
MachineFunction &Function,
const TargetDescription &Target) {
MachineOperand *Definition = Instruction.operandAt(Action.DefinitionOperand);
const MachineOperand *SourceOperand = Instruction.operandAt(Action.SourceOperand);
const Register *Destination = Definition == nullptr ? nullptr : Definition->getIf<Register>();
const Register *Source = SourceOperand == nullptr ? nullptr : SourceOperand->getIf<Register>();
if (Destination == nullptr || Source == nullptr || !Definition->isDef() || Definition->isUse() ||
!SourceOperand->isUse() || Definition->subReg().value() != 0 ||
SourceOperand->subReg().value() != 0 || Destination->isNoRegister() ||
Source->isNoRegister()) {
return false;
}
MachineBasicBlock *Block = Instruction.parent();
if (Block == nullptr)
return makeUnexpected(ErrorCode::InternalError, "machine combiner root is detached");
if (*Destination == *Source) {
auto Removed = Block->removeInstruction(Instruction);
if (!Removed)
return std::unexpected(Removed.error());
return true;
}
if (!Destination->isVirtual() ||
Function.registerInfo().getUniqueVRegDef(*Destination) != Definition ||
!compatibleReplacement(*Destination, *Source, Function, Target)) {
return false;
}
std::vector<MachineOperand *> Uses;
for (MachineOperand &Use : Function.registerInfo().useOperands(*Destination, true))
Uses.push_back(&Use);
for (MachineOperand *Use : Uses) {
auto Replaced = Use->setRegister(*Source);
if (!Replaced)
return std::unexpected(Replaced.error());
}
auto Removed = Block->removeInstruction(Instruction);
if (!Removed)
return std::unexpected(Removed.error());
if (!Function.registerInfo().useEmpty(*Destination, true) ||
Function.registerInfo().getUniqueVRegDef(*Destination) != nullptr) {
return makeUnexpected(ErrorCode::InternalError,
"machine combiner left references to a replaced result");
}
auto Erased = Function.registerInfo().eraseVirtualRegister(*Destination);
if (!Erased)
return std::unexpected(Erased.error());
return true;
}
[[nodiscard]] Expected<bool> executePattern(MachineInstr &Instruction,
const MachineCombinerPattern &Pattern,
MachineFunction &Function,
const TargetDescription &Target) {
const InstructionDescriptor *Descriptor = Target.instructionDescriptor(Instruction.opcode());
if (Descriptor == nullptr)
return makeUnexpected(ErrorCode::VerificationError, "machine combiner root has no descriptor");
const InstructionFlags Flags = Descriptor->Flags;
if (Flags.has(InstructionFlag::Terminator) || Flags.has(InstructionFlag::Call) ||
Flags.has(InstructionFlag::Barrier) || Flags.has(InstructionFlag::MayLoad) ||
Flags.has(InstructionFlag::MayStore) || Flags.has(InstructionFlag::HasSideEffects) ||
Flags.has(InstructionFlag::UnmodeledSideEffects) || !Instruction.memOperands().empty() ||
hasProtectedAttachment(Instruction)) {
return false;
}
switch (Pattern.Action.Kind) {
case MachineCombinerActionKind::ReplaceDefWithOperandAndErase:
return executeReplacement(Instruction, Pattern.Action, Function, Target);
}
return false;
}
} // namespace
const MachinePassContract &MachineCombinerPass::contract() const noexcept {
return combinerContract(Stage);
}
Expected<MachinePassResult> MachineCombinerPass::run(MachineFunction &Function,
const TargetDescription &Target,
MachineFunctionAnalysisManager &Analyses) {
if (!Function.properties().containsAll(contract().Requires)) {
return makeUnexpected(ErrorCode::VerificationError,
"machine combiner input does not satisfy its stage contract");
}
auto Trial = cloneMachineFunction(
Function, Target,
MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
if (!Trial) {
return std::unexpected(
withErrorContext(Trial.error(), "machine combiner transaction setup failed: "));
}
MachineActionReplay Actions(Analyses, Trial->InstructionStableIDs);
auto TrialResult = runImplementation(*Trial->ClonedFunction, Target, Actions);
if (!TrialResult)
return std::unexpected(TrialResult.error());
Actions.beginReplay();
auto Result = runImplementation(Function, Target, Actions);
if (!Result) {
return std::unexpected(withErrorContext(
Result.error(), "machine combiner diverged after a successful transaction preflight: "));
}
auto Replayed = Actions.finishReplay();
if (!Replayed)
return std::unexpected(Replayed.error());
return Result;
}
Expected<MachinePassResult>
MachineCombinerPass::runImplementation(MachineFunction &Function, const TargetDescription &Target,
MachineActionReplay &Actions) const {
if (Options.MaximumIterations == 0)
return makeUnexpected(ErrorCode::InvalidArgument,
"machine combiner iteration budget must be nonzero");
MachinePassResult Result;
std::size_t Rewrites = 0;
for (std::size_t Iteration = 0; Iteration < Options.MaximumIterations; ++Iteration) {
bool IterationChanged = false;
for (MachineBasicBlock *Block = Function.firstBlock(); Block != nullptr;
Block = Block->next()) {
for (MachineInstr *Instruction = Block->firstInstruction(); Instruction != nullptr;) {
MachineInstr *Next = Instruction->next();
for (const MachineCombinerPattern &Pattern : Info->patterns(Instruction->opcode())) {
if (!matchesPattern(*Instruction, Pattern, Stage))
continue;
if (Rewrites == Options.MaximumRewrites)
break;
std::string Rule = "combine.";
Rule.append(Pattern.Name);
auto Accepted = Actions.request(Rule, Function, Instruction->stableID());
if (!Accepted)
return std::unexpected(Accepted.error());
if (!*Accepted)
continue;
auto Executed = executePattern(*Instruction, Pattern, Function, Target);
if (!Executed)
return std::unexpected(Executed.error());
if (!*Executed)
continue;
++Rewrites;
IterationChanged = true;
Result.Changed = true;
break;
}
Instruction = Next;
}
}
if (!IterationChanged || Rewrites == Options.MaximumRewrites)
break;
}
if (!Result.Changed) {
Result.Preserved = PreservedAnalyses::all();
return Result;
}
if (Function.properties().has(MachineFunctionProperty::TracksLiveness)) {
auto Rebuilt = rebuildMachineLiveness(Function, Target);
if (!Rebuilt)
return std::unexpected(Rebuilt.error());
}
Result.Changes = {MachinePassChange::Instructions, MachinePassChange::Operands,
MachinePassChange::Registers};
Result.Preserved = PreservedAnalyses::none();
return Result;
}
} // namespace rcc
|