aboutsummaryrefslogtreecommitdiff
path: root/backend/unittests/CodeGen/LiveRegMatrixTest.cpp
blob: 21c07785bf8edb0715aa226edc795eaab350d78f (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
// SPDX-License-Identifier: GPL-3.0-or-later

#include "rcc/CodeGen/LiveRegMatrix.h"

#include "CodeGen/RegisterAllocationTestSupport.h"

#include <algorithm>
#include <iostream>

namespace {

using namespace rcc;
using namespace rcc::test;
using namespace rcc::test::ra;

void testLaneAndAssignment(TestContext &Context, const TargetDescription &Target) {
  auto Fixture = createFunction("matrix-lanes", 1);
  if (!Fixture)
    return;
  auto Low = createVReg(*Fixture->Function);
  auto High = createVReg(*Fixture->Function);
  if (!Low || !High ||
      !buildDef(*Fixture->Function, Target, *Fixture->Block, *Low, 1, TestSubReg::Low32) ||
      !buildDef(*Fixture->Function, Target, *Fixture->Block, *High, 2, TestSubReg::High32) ||
      !buildUse(*Fixture->Function, Target, *Fixture->Block, *High, TestSubReg::High32) ||
      !buildUse(*Fixture->Function, Target, *Fixture->Block, *Low, TestSubReg::Low32))
    return;

  MachineAnalysisManager Analyses;
  if (!registerStage11MachineAnalyses(Analyses))
    return;
  auto Intervals = Analyses.getResult<LiveIntervalsAnalysis>(*Fixture->Function, Target);
  if (!Intervals)
    return;
  const PhysicalRegister G0 = physical(TestRegister::G0);
  LiveRegMatrix Matrix(*Fixture->Function, Target, **Intervals);
  Context.expect(Matrix.assign(*Low, G0).has_value() &&
                     Matrix.checkInterference(*High, G0).Kind == RegisterInterferenceKind::Free &&
                     Matrix.assign(*High, G0).has_value() && Matrix.assignment(*Low) == G0,
                 "LiveRegMatrix must allow disjoint lanes and publish assignments");
  Context.expect(Matrix.unassign(*Low).has_value() && !Matrix.assignment(*Low),
                 "LiveRegMatrix must remove an assignment without affecting other lanes");
}

void testFixedAndMaskInterference(TestContext &Context, const TargetDescription &Target) {
  auto Fixture = createFunction("matrix-fixed", 2);
  if (!Fixture)
    return;
  auto Value = createVReg(*Fixture->Function);
  const PhysicalRegister G0 = physical(TestRegister::G0);
  const PhysicalRegister G1 = physical(TestRegister::G1);
  if (!Value || !buildDef(*Fixture->Function, Target, *Fixture->Block, *Value, 7) ||
      !buildMask(*Fixture->Function, Target, *Fixture->Block, createTestCallPreservedMask()) ||
      !buildUse(*Fixture->Function, Target, *Fixture->Block, Register::fromPhysical(G0)) ||
      !buildUse(*Fixture->Function, Target, *Fixture->Block, *Value))
    return;

  MachineAnalysisManager Analyses;
  if (!registerStage11MachineAnalyses(Analyses))
    return;
  auto Intervals = Analyses.getResult<LiveIntervalsAnalysis>(*Fixture->Function, Target);
  if (!Intervals)
    return;
  LiveRegMatrix Matrix(*Fixture->Function, Target, **Intervals);
  const auto G0Interferences = Matrix.collectInterferences(*Value, G0);
  const auto G1Interferences = Matrix.collectInterferences(*Value, G1);
  const auto HasKind = [](std::span<const RegisterInterference> Values,
                          RegisterInterferenceKind Kind) {
    return std::ranges::any_of(
        Values, [Kind](const RegisterInterference &Value) { return Value.Kind == Kind; });
  };
  Context.expect(HasKind(G0Interferences, RegisterInterferenceKind::FixedRegister) &&
                     HasKind(G0Interferences, RegisterInterferenceKind::RegisterMask) &&
                     !HasKind(G1Interferences, RegisterInterferenceKind::FixedRegister) &&
                     Matrix.assign(*Value, G1).has_value(),
                 "LiveRegMatrix must distinguish fixed-register and RegMask interference");
}

} // namespace

int main() {
  TestContext Context;
  auto Target = createTarget();
  Context.expect(Target.has_value(), "LiveRegMatrix test target must be constructible");
  if (!Target) {
    std::cerr << Target.error().message() << '\n';
    return Context.result();
  }
  testLaneAndAssignment(Context, *Target);
  testFixedAndMaskInterference(Context, *Target);
  return Context.result();
}