aboutsummaryrefslogtreecommitdiff
path: root/backend/unittests/CodeGen/SpillerTest.cpp
diff options
context:
space:
mode:
authorRichard-Qin-X <richard.qin.001@gmail.com>2026-08-09 04:40:17 +0800
committerRichard-Qin-X <richard.qin.001@gmail.com>2026-08-09 04:40:17 +0800
commite5fdceb9b869f010a2a75638fd38aaf1867f3008 (patch)
tree023d3e18960956f5ce369739c68aaa2a3e8400e7 /backend/unittests/CodeGen/SpillerTest.cpp
parent6c7cecc81a4c7e06e26027e21edd2cfd95a37a0b (diff)
downloadrcc-project-e5fdceb9b869f010a2a75638fd38aaf1867f3008.tar.gz
rcc-project-e5fdceb9b869f010a2a75638fd38aaf1867f3008.tar.xz
[CodeGen] Complete register allocation infrastructure
Diffstat (limited to 'backend/unittests/CodeGen/SpillerTest.cpp')
-rw-r--r--backend/unittests/CodeGen/SpillerTest.cpp94
1 files changed, 94 insertions, 0 deletions
diff --git a/backend/unittests/CodeGen/SpillerTest.cpp b/backend/unittests/CodeGen/SpillerTest.cpp
new file mode 100644
index 0000000..1f22d8f
--- /dev/null
+++ b/backend/unittests/CodeGen/SpillerTest.cpp
@@ -0,0 +1,94 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+#include "rcc/CodeGen/Spiller.h"
+
+#include "CodeGen/RegisterAllocationTestSupport.h"
+
+#include <iostream>
+#include <optional>
+
+namespace {
+
+using namespace rcc;
+using namespace rcc::test;
+using namespace rcc::test::ra;
+
+void testRematerialization(TestContext &Context, const TargetDescription &Target) {
+ auto Fixture = createFunction("spiller-remat", 20);
+ if (!Fixture)
+ return;
+ auto Value = createVReg(*Fixture->Function);
+ if (!Value || !buildDef(*Fixture->Function, Target, *Fixture->Block, *Value, 42, {}, true) ||
+ !buildUse(*Fixture->Function, Target, *Fixture->Block, *Value) ||
+ !buildUse(*Fixture->Function, Target, *Fixture->Block, *Value))
+ return;
+ VirtRegMap Mapping(*Fixture->Function);
+ TestRegisterAllocationInfo Info;
+ if (!Mapping.add(*Value))
+ return;
+ Spiller ValueSpiller(*Fixture->Function, Target, Info, Mapping, true);
+ auto Children = ValueSpiller.spill(*Value);
+ Context.expect(Children && Children->size() == 2 &&
+ Fixture->Function->frameInfo().liveObjectCount() == 0 &&
+ countOpcode(*Fixture->Function, testOpcode(RematerializableDefOpcode)) == 2 &&
+ countOpcode(*Fixture->Function, testOpcode(SpillLoadOpcode)) == 0,
+ "Spiller must rematerialize each use without allocating a stack slot");
+}
+
+void testDirectSpillAndDebugUse(TestContext &Context, const TargetDescription &Target) {
+ auto Fixture = createFunction("spiller-debug", 21);
+ if (!Fixture)
+ return;
+ auto Value = createVReg(*Fixture->Function);
+ if (!Value || !buildDef(*Fixture->Function, Target, *Fixture->Block, *Value, 7) ||
+ !buildUse(*Fixture->Function, Target, *Fixture->Block, *Value, {}, true) ||
+ !buildUse(*Fixture->Function, Target, *Fixture->Block, *Value))
+ return;
+ VirtRegMap Mapping(*Fixture->Function);
+ TestRegisterAllocationInfo Info;
+ if (!Mapping.add(*Value))
+ return;
+ Spiller ValueSpiller(*Fixture->Function, Target, Info, Mapping, false);
+ auto Children = ValueSpiller.spill(*Value);
+ const MachineOperand *DebugOperand = nullptr;
+ for (const MachineInstr *Instruction = Fixture->Block->firstInstruction(); Instruction != nullptr;
+ Instruction = Instruction->next()) {
+ for (const MachineOperand &Operand : Instruction->operands()) {
+ if (Operand.isDebugUse()) {
+ DebugOperand = &Operand;
+ break;
+ }
+ }
+ if (DebugOperand != nullptr)
+ break;
+ }
+ const std::optional<Register> DebugRegister =
+ DebugOperand == nullptr ? std::nullopt : DebugOperand->registerValue();
+ const bool DebugRegisterCleared = DebugRegister && DebugRegister.value().isNoRegister();
+ Context.expect(Children && !Children->empty() &&
+ countOpcode(*Fixture->Function, testOpcode(SpillLoadOpcode)) != 0 &&
+ countOpcode(*Fixture->Function, testOpcode(SpillStoreOpcode)) != 0 &&
+ DebugOperand != nullptr && DebugRegisterCleared &&
+ DebugOperand->registerFlags().has(RegisterOperandFlag::Undef),
+ "Spiller must preserve direct spill code and clear debug-only uses");
+
+ auto Unspillable = createVReg(*Fixture->Function);
+ if (!Unspillable || !Mapping.add(*Unspillable, false))
+ return;
+ auto Rejected = ValueSpiller.spill(*Unspillable);
+ Context.expect(!Rejected && Rejected.error().code() == ErrorCode::RegisterAllocationFailed,
+ "Spiller must reject an explicitly non-spillable value");
+}
+
+} // namespace
+
+int main() {
+ TestContext Context;
+ auto Target = createTarget();
+ Context.expect(Target.has_value(), "Spiller test target must be constructible");
+ if (!Target)
+ return Context.result();
+ testRematerialization(Context, *Target);
+ testDirectSpillAndDebugUse(Context, *Target);
+ return Context.result();
+}