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
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "RISCV.h"
#include "rcc/CodeGen/MachineBasicBlock.h"
#include "rcc/CodeGen/MachineFunction.h"
#include "rcc/CodeGen/MachineFunctionAttributes.h"
#include "rcc/CodeGen/MachineIRBuilder.h"
#include "rcc/CodeGen/MachineInstr.h"
#include "rcc/CodeGen/MachineOperand.h"
#include "rcc/CodeGen/MachinePassManager.h"
#include "rcc/CodeGen/RegisterMask.h"
#include "rcc/CodeGen/RegisterScavenger.h"
#include "rcc/CodeGen/TargetDescription.h"
#include "rcc/Target/RISCV/RISCVInstructions.h"
#include "rcc/Target/RISCV/RISCVRegisters.h"
#include <cstddef>
#include <cstdint>
#include <memory>
#include <optional>
#include <span>
#include <utility>
#include <vector>
namespace rcc::riscv {
namespace {
struct RegImmPair final {
Register Reg;
std::int64_t Imm = 0;
};
[[nodiscard]] constexpr RegisterClassID registerClassID(RegisterClass Value) noexcept {
return RegisterClassID::fromValue(static_cast<std::uint32_t>(Value));
}
[[nodiscard]] PhysicalRegister physicalRegister(PhysicalRegisterID Value) noexcept {
const auto Created = PhysicalRegister::create(static_cast<std::uint32_t>(Value));
return *Created;
}
[[nodiscard]] bool isOpcode(const MachineInstr &Instruction, InstructionOpcode Value) noexcept {
return Instruction.opcode() == instructionOpcode(Value);
}
[[nodiscard]] std::optional<PhysicalRegister>
physicalRegisterValue(const MachineOperand *Operand) noexcept {
const auto Reg = Operand == nullptr ? std::nullopt : Operand->registerValue();
return Reg ? Reg->physicalRegister() : std::nullopt;
}
[[nodiscard]] bool classContains(RegisterClass Class, Register Reg,
const TargetDescription &Target) noexcept {
const auto Physical = Reg.physicalRegister();
return Physical && Target.registers().registerClassContains(registerClassID(Class), *Physical);
}
[[nodiscard]] bool isCompressedRegister(Register Reg, const TargetDescription &Target) noexcept {
return classContains(RegisterClass::GPRC, Reg, Target) ||
classContains(RegisterClass::FPR32C, Reg, Target) ||
classContains(RegisterClass::FPR64C, Reg, Target);
}
[[nodiscard]] bool isCompressibleLoad(const MachineInstr &Instruction,
const RISCVSubtarget &Subtarget) noexcept {
if (!Subtarget.features().has(RISCVExtension::C))
return false;
if (isOpcode(Instruction, InstructionOpcode::LW) ||
isOpcode(Instruction, InstructionOpcode::LD)) {
return true;
}
return isOpcode(Instruction, InstructionOpcode::FLD) &&
Subtarget.features().has(RISCVExtension::D);
}
[[nodiscard]] bool isCompressibleStore(const MachineInstr &Instruction,
const RISCVSubtarget &Subtarget) noexcept {
if (!Subtarget.features().has(RISCVExtension::C))
return false;
if (isOpcode(Instruction, InstructionOpcode::SW) ||
isOpcode(Instruction, InstructionOpcode::SD)) {
return true;
}
return isOpcode(Instruction, InstructionOpcode::FSD) &&
Subtarget.features().has(RISCVExtension::D);
}
[[nodiscard]] std::optional<unsigned> loadStoreWidth(const MachineInstr &Instruction) noexcept {
if (isOpcode(Instruction, InstructionOpcode::LW) ||
isOpcode(Instruction, InstructionOpcode::SW)) {
return 2U;
}
if (isOpcode(Instruction, InstructionOpcode::LD) ||
isOpcode(Instruction, InstructionOpcode::SD) ||
isOpcode(Instruction, InstructionOpcode::FLD) ||
isOpcode(Instruction, InstructionOpcode::FSD)) {
return 3U;
}
return std::nullopt;
}
[[nodiscard]] std::int64_t compressedOffsetMask(const MachineInstr &Instruction) noexcept {
const std::optional<unsigned> Width = loadStoreWidth(Instruction);
if (!Width)
return 0;
return static_cast<std::int64_t>(std::uint64_t{0x1f} << *Width);
}
[[nodiscard]] bool isCompressibleStackOffset(std::int64_t Offset,
const MachineInstr &Instruction) noexcept {
const std::optional<unsigned> Width = loadStoreWidth(Instruction);
if (!Width || Offset < 0)
return false;
const std::uint64_t Value = static_cast<std::uint64_t>(Offset);
const std::uint64_t AlignmentMask = (std::uint64_t{1} << *Width) - 1U;
return (Value & AlignmentMask) == 0 && (Value >> *Width) < 64U;
}
[[nodiscard]] std::optional<RegImmPair>
regImmPreventingCompression(MachineInstr &Instruction, const RISCVSubtarget &Subtarget,
const TargetDescription &Target) noexcept {
const bool IsLoad = isCompressibleLoad(Instruction, Subtarget);
const bool IsStore = isCompressibleStore(Instruction, Subtarget);
if (!IsLoad && !IsStore)
return std::nullopt;
MachineOperand *ValueOperand = Instruction.operandAt(0);
MachineOperand *BaseOperand = Instruction.operandAt(1);
MachineOperand *OffsetOperand = Instruction.operandAt(2);
const auto Value = physicalRegisterValue(ValueOperand);
const auto Base = physicalRegisterValue(BaseOperand);
const auto *Offset = OffsetOperand == nullptr ? nullptr : OffsetOperand->getIf<std::int64_t>();
if (!Value || !Base || Offset == nullptr)
return std::nullopt;
const std::int64_t Mask = compressedOffsetMask(Instruction);
const std::int64_t BaseAdjustment = *Offset & ~Mask;
const Register BaseRegister = Register::fromPhysical(*Base);
if (*Base == physicalRegister(PhysicalRegisterID::X2)) {
if (!isCompressibleStackOffset(*Offset, Instruction) && BaseAdjustment != 0)
return RegImmPair{BaseRegister, BaseAdjustment};
return std::nullopt;
}
const Register ValueRegister = Register::fromPhysical(*Value);
const bool ValueCompressed = isCompressedRegister(ValueRegister, Target);
const bool BaseCompressed = isCompressedRegister(BaseRegister, Target);
if ((!BaseCompressed || BaseAdjustment != 0) && ValueCompressed)
return RegImmPair{BaseRegister, BaseAdjustment};
if (IsStore && !ValueCompressed && (BaseCompressed || *Value == *Base) && BaseAdjustment == 0) {
return RegImmPair{ValueRegister, 0};
}
return std::nullopt;
}
[[nodiscard]] bool maskClobbers(const RegisterMask &Mask, PhysicalRegister Reg,
const TargetDescription &Target) noexcept {
for (const PhysicalRegisterDesc &Description : Target.registers().physicalRegisters()) {
if (Target.registers().regsOverlap(Description.Reg, Reg) && Mask.clobbers(Description.Reg))
return true;
}
return false;
}
[[nodiscard]] bool instructionModifies(const MachineInstr &Instruction, Register Reg,
const TargetDescription &Target) noexcept {
const auto Physical = Reg.physicalRegister();
if (!Physical)
return false;
for (const MachineOperand &Operand : Instruction.operands()) {
if (const auto OperandPhysical = physicalRegisterValue(&Operand);
OperandPhysical && Operand.isDef() &&
Target.registers().regsOverlap(*OperandPhysical, *Physical)) {
return true;
}
const auto *Mask = Operand.getIf<RegisterMaskOperand>();
if (Mask != nullptr && Mask->Mask && maskClobbers(*Mask->Mask, *Physical, Target))
return true;
}
return false;
}
[[nodiscard]] bool instructionTouches(const MachineInstr &Instruction, PhysicalRegister Reg,
const TargetDescription &Target) noexcept {
for (const MachineOperand &Operand : Instruction.operands()) {
if (const auto OperandPhysical = physicalRegisterValue(&Operand);
OperandPhysical && Target.registers().regsOverlap(*OperandPhysical, Reg)) {
return true;
}
const auto *Mask = Operand.getIf<RegisterMaskOperand>();
if (Mask != nullptr && Mask->Mask && maskClobbers(*Mask->Mask, Reg, Target))
return true;
}
return false;
}
[[nodiscard]] std::optional<RegisterClass>
compressedClassFor(Register Reg, const TargetDescription &Target) noexcept {
if (classContains(RegisterClass::GPR, Reg, Target))
return RegisterClass::GPRC;
if (classContains(RegisterClass::FPR32, Reg, Target))
return RegisterClass::FPR32C;
if (classContains(RegisterClass::FPR64, Reg, Target))
return RegisterClass::FPR64C;
return std::nullopt;
}
[[nodiscard]] Expected<std::optional<PhysicalRegister>>
scavengeCompressedRegister(MachineFunction &Function, MachineInstr &First, MachineInstr &Last,
Register OldReg, const TargetDescription &Target) {
const auto Class = compressedClassFor(OldReg, Target);
if (!Class)
return std::optional<PhysicalRegister>{};
const RegisterClassDesc *Description = Target.registers().registerClass(registerClassID(*Class));
if (Description == nullptr)
return makeUnexpected(ErrorCode::InternalError,
"RISC-V compressed register class is unavailable");
std::vector<PhysicalRegister> Candidates;
for (PhysicalRegister Candidate : Description->AllocationOrder) {
bool Touched = false;
for (MachineInstr *Instruction = &First; Instruction != nullptr;
Instruction = Instruction->next()) {
Touched |= instructionTouches(*Instruction, Candidate, Target);
if (Instruction == &Last)
break;
}
if (!Touched)
Candidates.push_back(Candidate);
}
if (Candidates.empty())
return std::optional<PhysicalRegister>{};
RegisterScavenger Scavenger(Function, Target);
auto Scavenged = Scavenger.scavenge(First, Candidates);
if (!Scavenged) {
if (Scavenged.error().code() == ErrorCode::RegisterAllocationFailed)
return std::optional<PhysicalRegister>{};
return std::unexpected(Scavenged.error());
}
if (Scavenged->RequiresSpill)
return std::optional<PhysicalRegister>{};
return std::optional<PhysicalRegister>(Scavenged->Reg);
}
[[nodiscard]] Expected<std::optional<PhysicalRegister>>
analyzeCompressibleUses(MachineFunction &Function, MachineInstr &First, RegImmPair RegImm,
std::vector<MachineInstr *> &Instructions, const RISCVSubtarget &Subtarget,
const TargetDescription &Target) {
for (MachineInstr *Instruction = &First; Instruction != nullptr;
Instruction = Instruction->next()) {
const auto Candidate = regImmPreventingCompression(*Instruction, Subtarget, Target);
if (Candidate && Candidate->Reg == RegImm.Reg && Candidate->Imm == RegImm.Imm)
Instructions.push_back(Instruction);
if (instructionModifies(*Instruction, RegImm.Reg, Target))
break;
}
if (Instructions.size() <= 1U || (RegImm.Imm != 0 && Instructions.size() < 3U))
return std::optional<PhysicalRegister>{};
return scavengeCompressedRegister(Function, First, *Instructions.back(), RegImm.Reg, Target);
}
[[nodiscard]] Expected<MachineInstr *>
insertRegisterCopy(MachineFunction &Function, MachineInstr &Before, PhysicalRegister Destination,
RegImmPair Source, const TargetDescription &Target) {
MachineIRBuilder Builder(Function, Target);
auto InsertPoint = Builder.setInsertPointBefore(Before);
if (!InsertPoint)
return std::unexpected(InsertPoint.error());
InstructionOpcode Opcode = InstructionOpcode::ADDI;
const bool IsGPR = classContains(RegisterClass::GPR, Source.Reg, Target);
if (!IsGPR) {
if (Source.Imm != 0)
return makeUnexpected(ErrorCode::VerificationError,
"RISC-V floating-point compressed copy has an offset");
if (classContains(RegisterClass::FPR32, Source.Reg, Target))
Opcode = InstructionOpcode::FSGNJ_S;
else if (classContains(RegisterClass::FPR64, Source.Reg, Target))
Opcode = InstructionOpcode::FSGNJ_D;
else
return makeUnexpected(ErrorCode::VerificationError,
"RISC-V compressed copy has an unsupported register class");
} else if (Source.Imm < -2048 || Source.Imm > 2047) {
return makeUnexpected(ErrorCode::VerificationError,
"RISC-V compressed base adjustment is outside ADDI range");
}
auto Pending = Builder.buildInstr(instructionOpcode(Opcode));
if (!Pending)
return std::unexpected(Pending.error());
auto Added = Pending->addDef(Register::fromPhysical(Destination));
if (!Added)
return std::unexpected(Added.error());
Added = Pending->addUse(Source.Reg);
if (!Added)
return std::unexpected(Added.error());
if (IsGPR)
Added = Pending->addImmediate(Source.Imm);
else
Added = Pending->addUse(Source.Reg);
if (!Added)
return std::unexpected(Added.error());
if (Before.debugLocation()) {
Added = Pending->setDebugLocation(*Before.debugLocation());
if (!Added)
return std::unexpected(Added.error());
}
return Pending->build();
}
[[nodiscard]] Expected<void> updateOperands(MachineInstr &Instruction, RegImmPair OldRegImm,
PhysicalRegister NewReg,
const RISCVSubtarget &Subtarget) {
const std::size_t FirstOperand =
isCompressibleStore(Instruction, Subtarget) && OldRegImm.Imm != 0 ? 1U : 0U;
for (std::size_t Index = FirstOperand; Index < Instruction.operandCount(); ++Index) {
MachineOperand *Operand = Instruction.operandAt(Index);
if (Operand == nullptr || Operand->isDef() || Operand->registerValue() != OldRegImm.Reg)
continue;
auto Updated = Operand->setRegister(Register::fromPhysical(NewReg));
if (!Updated)
return std::unexpected(Updated.error());
}
MachineOperand *Offset = Instruction.operandAt(2);
const auto *OldOffset = Offset == nullptr ? nullptr : Offset->getIf<std::int64_t>();
if (OldOffset == nullptr)
return makeUnexpected(ErrorCode::VerificationError,
"RISC-V compressible load/store has no immediate offset");
return Offset->setImmediate(*OldOffset & compressedOffsetMask(Instruction));
}
[[nodiscard]] Expected<bool> makeBlockCompressible(MachineFunction &Function,
MachineBasicBlock &Block,
const RISCVSubtarget &Subtarget,
const TargetDescription &Target) {
bool Changed = false;
for (MachineInstr *Instruction = Block.firstInstruction(); Instruction != nullptr;
Instruction = Instruction->next()) {
const auto RegImm = regImmPreventingCompression(*Instruction, Subtarget, Target);
if (!RegImm)
continue;
std::vector<MachineInstr *> Uses;
auto NewReg = analyzeCompressibleUses(Function, *Instruction, *RegImm, Uses, Subtarget, Target);
if (!NewReg)
return std::unexpected(NewReg.error());
const PhysicalRegister Scavenged = NewReg->value_or(PhysicalRegister{});
if (!Scavenged.isValid())
continue;
auto Copy = insertRegisterCopy(Function, *Instruction, Scavenged, *RegImm, Target);
if (!Copy)
return std::unexpected(Copy.error());
for (MachineInstr *Use : Uses) {
auto Updated = updateOperands(*Use, *RegImm, Scavenged, Subtarget);
if (!Updated)
return std::unexpected(Updated.error());
}
Changed = true;
}
return Changed;
}
class RISCVMakeCompressiblePass final : public MachineFunctionPass {
public:
explicit RISCVMakeCompressiblePass(const RISCVSubtarget &Subtarget) noexcept
: Subtarget(&Subtarget) {}
[[nodiscard]] const MachinePassContract &contract() const noexcept override {
static const MachinePassContract Contract = [] {
MachinePassContract Value{
.Name = "riscv-make-compressible",
.Requires = {},
.Sets = {},
.Clears = {},
.RequiredAnalyses = {},
.VerificationStage = std::nullopt,
};
Value.ThreadSafety = PassThreadSafety::ThreadSafe;
Value.DeclaredChanges = {MachinePassChange::Instructions, MachinePassChange::Operands,
MachinePassChange::Registers};
return Value;
}();
return Contract;
}
[[nodiscard]] Expected<MachinePassResult> run(MachineFunction &Function,
const TargetDescription &Target,
MachineFunctionAnalysisManager &) override {
MachinePassResult Result;
Result.Preserved = PreservedAnalyses::all();
if (Subtarget == nullptr || !Subtarget->features().has(RISCVExtension::C) ||
!Function.attributes().has(MachineFunctionAttribute::MinSize)) {
return Result;
}
for (MachineBasicBlock *Block = Function.firstBlock(); Block != nullptr;
Block = Block->next()) {
auto BlockResult = makeBlockCompressible(Function, *Block, *Subtarget, Target);
if (!BlockResult)
return std::unexpected(BlockResult.error());
Result.Changed |= *BlockResult;
}
if (Result.Changed) {
Result.Changes = {MachinePassChange::Instructions, MachinePassChange::Operands,
MachinePassChange::Registers};
Result.Preserved = PreservedAnalyses::none();
}
return Result;
}
private:
const RISCVSubtarget *Subtarget = nullptr;
};
} // namespace
std::unique_ptr<MachineFunctionPass> createMakeCompressiblePass(const RISCVSubtarget &Subtarget) {
return std::make_unique<RISCVMakeCompressiblePass>(Subtarget);
}
} // namespace rcc::riscv
|