Skip to content

Commit 28b7d66

Browse files
authored
[TableGen][CodeGen] Give every leaf register a unique regunit (#139526)
Give every leaf register a unique regunit, even if it has ad hoc aliases. Previously only leaf registers *without* ad hoc aliases would get a unique regunit, but that caused situations where regunits could not be used to distinguish a register from its subregs. For example: - Registers A and B alias. They both get regunit 0 only. - Register C has subregs A and B. It inherits regunits from its subregs, so it also gets regunit 0 only. After this fix, registers A and B will get a unique regunit in addition to the regunit representing the alias, for example: - A will get regunits 0 and 1. - B will get regunits 0 and 2. - C will get regunits 0, 1 and 2.
1 parent 671cef0 commit 28b7d66

File tree

3 files changed

+93
-16
lines changed

3 files changed

+93
-16
lines changed
+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// RUN: llvm-tblgen -gen-register-info -register-info-debug -I %p/../../include %s -o /dev/null 2>&1 | FileCheck %s
2+
include "llvm/Target/Target.td"
3+
def TestTarget : Target;
4+
5+
def lo : SubRegIndex<32, 0>;
6+
def hi : SubRegIndex<32, 32>;
7+
8+
// One superreg with two subregs.
9+
def Alo : Register<"">;
10+
def Ahi : Register<"">;
11+
def A : Register<""> {
12+
let SubRegs = [Alo, Ahi];
13+
let SubRegIndices = [lo, hi];
14+
}
15+
16+
// Same but the subregs alias.
17+
def Blo : Register<"">;
18+
def Bhi : Register<""> {
19+
let Aliases = [Blo];
20+
}
21+
def B : Register<""> {
22+
let SubRegs = [Blo, Bhi];
23+
let SubRegIndices = [lo, hi];
24+
}
25+
26+
// Same but the superreg has an alias.
27+
def Clo : Register<"">;
28+
def Chi : Register<"">;
29+
def D : Register<"">;
30+
def C : Register<""> {
31+
let SubRegs = [Clo, Chi];
32+
let SubRegIndices = [lo, hi];
33+
let Aliases = [D];
34+
}
35+
36+
def TestRC : RegisterClass<"Test", [i64], 0, (add A, B)>;
37+
38+
// CHECK-LABEL: Register A:
39+
// CHECK: SubReg hi = Ahi
40+
// CHECK: SubReg lo = Alo
41+
// CHECK: RegUnit 0
42+
// CHECK: RegUnit 1
43+
44+
// CHECK-LABEL: Register Ahi:
45+
// CHECK: RegUnit 1
46+
47+
// CHECK-LABEL: Register Alo:
48+
// CHECK: RegUnit 0
49+
50+
// CHECK-LABEL: Register B:
51+
// CHECK: SubReg hi = Bhi
52+
// CHECK: SubReg lo = Blo
53+
// CHECK: RegUnit 2
54+
// CHECK: RegUnit 3
55+
// CHECK: RegUnit 4
56+
57+
// CHECK-LABEL: Register Bhi:
58+
// CHECK: RegUnit 3
59+
// CHECK: RegUnit 4
60+
61+
// CHECK-LABEL: Register Blo:
62+
// CHECK: RegUnit 2
63+
// CHECK: RegUnit 3
64+
65+
// CHECK-LABEL: Register C:
66+
// CHECK: SubReg hi = Chi
67+
// CHECK: SubReg lo = Clo
68+
// CHECK: RegUnit 5
69+
// CHECK: RegUnit 6
70+
// CHECK: RegUnit 7
71+
72+
// CHECK-LABEL: Register Chi:
73+
// CHECK: RegUnit 6
74+
75+
// CHECK-LABEL: Register Clo:
76+
// CHECK: RegUnit 5
77+
78+
// CHECK-LABEL: Register D:
79+
// CHECK: RegUnit 7
80+
// CHECK: RegUnit 8

llvm/utils/TableGen/Common/CodeGenRegisters.cpp

+11-16
Original file line numberDiff line numberDiff line change
@@ -414,16 +414,17 @@ CodeGenRegister::computeSubRegs(CodeGenRegBank &RegBank) {
414414

415415
// Initialize RegUnitList. Because getSubRegs is called recursively, this
416416
// processes the register hierarchy in postorder.
417-
//
418-
// Inherit all sub-register units. It is good enough to look at the explicit
419-
// sub-registers, the other registers won't contribute any more units.
420-
for (const CodeGenRegister *SR : ExplicitSubRegs)
421-
RegUnits |= SR->RegUnits;
422-
423-
// Absent any ad hoc aliasing, we create one register unit per leaf register.
424-
// These units correspond to the maximal cliques in the register overlap
425-
// graph which is optimal.
426-
//
417+
if (ExplicitSubRegs.empty()) {
418+
// Create one register unit per leaf register. These units correspond to the
419+
// maximal cliques in the register overlap graph which is optimal.
420+
RegUnits.set(RegBank.newRegUnit(this));
421+
} else {
422+
// Inherit all sub-register units. It is good enough to look at the explicit
423+
// sub-registers, the other registers won't contribute any more units.
424+
for (const CodeGenRegister *SR : ExplicitSubRegs)
425+
RegUnits |= SR->RegUnits;
426+
}
427+
427428
// When there is ad hoc aliasing, we simply create one unit per edge in the
428429
// undirected ad hoc aliasing graph. Technically, we could do better by
429430
// identifying maximal cliques in the ad hoc graph, but cliques larger than 2
@@ -440,12 +441,6 @@ CodeGenRegister::computeSubRegs(CodeGenRegBank &RegBank) {
440441
AR->RegUnits.set(Unit);
441442
}
442443

443-
// Finally, create units for leaf registers without ad hoc aliases. Note that
444-
// a leaf register with ad hoc aliases doesn't get its own unit - it isn't
445-
// necessary. This means the aliasing leaf registers can share a single unit.
446-
if (RegUnits.empty())
447-
RegUnits.set(RegBank.newRegUnit(this));
448-
449444
// We have now computed the native register units. More may be adopted later
450445
// for balancing purposes.
451446
NativeRegUnits = RegUnits;

llvm/utils/TableGen/RegisterInfoEmitter.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -1934,6 +1934,8 @@ void RegisterInfoEmitter::debugDump(raw_ostream &OS) {
19341934
OS << "\tSubReg " << SubIdx->getName() << " = " << SubReg->getName()
19351935
<< '\n';
19361936
}
1937+
for (unsigned U : R.getNativeRegUnits())
1938+
OS << "\tRegUnit " << U << '\n';
19371939
}
19381940
}
19391941

0 commit comments

Comments
 (0)