aboutsummaryrefslogtreecommitdiff
path: root/backend/unittests/CodeGen/SpillerTest.cpp
blob: 1f22d8f30517eef34c4e4d664cc4755c3267b224 (plain)
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
// 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();
}