Skip to content

Commit e62d25e

Browse files
authored
RegisterCoalescer: Relax assert for super register def rematerialization (#69088)
1 parent 748f861 commit e62d25e

File tree

3 files changed

+139
-4
lines changed

3 files changed

+139
-4
lines changed

llvm/lib/CodeGen/RegisterCoalescer.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,9 @@ bool RegisterCoalescer::reMaterializeTrivialDef(const CoalescerPair &CP,
14151415
// from SUBREG_TO_REG, such as:
14161416
// $edi = MOV32r0 implicit-def dead $eflags, implicit-def $rdi
14171417
// undef %0.sub_32bit = MOV32r0 implicit-def dead $eflags, implicit-def %0
1418+
//
1419+
// The implicit-def of the super register may have been reduced to
1420+
// subregisters depending on the uses.
14181421

14191422
bool NewMIDefinesFullReg = false;
14201423

@@ -1432,12 +1435,14 @@ bool RegisterCoalescer::reMaterializeTrivialDef(const CoalescerPair &CP,
14321435
assert(MO.isImplicit() && MO.getReg().isPhysical() &&
14331436
(MO.isDead() ||
14341437
(DefSubIdx &&
1435-
(TRI->getSubReg(MO.getReg(), DefSubIdx) ==
1436-
MCRegister((unsigned)NewMI.getOperand(0).getReg())))));
1438+
((TRI->getSubReg(MO.getReg(), DefSubIdx) ==
1439+
MCRegister((unsigned)NewMI.getOperand(0).getReg())) ||
1440+
TRI->isSubRegisterEq(NewMI.getOperand(0).getReg(),
1441+
MO.getReg())))));
14371442
NewMIImplDefs.push_back(MO.getReg().asMCReg());
14381443
} else {
1439-
assert(MO.getReg() == NewMI.getOperand(0).getReg() &&
1440-
MO.getSubReg() == 0);
1444+
assert(MO.getReg() == NewMI.getOperand(0).getReg());
1445+
14411446
// We're only expecting another def of the main output, so the range
14421447
// should get updated with the regular output range.
14431448
//
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# RUN: llc -mtriple=x86_64-unknown-linux-gnu -run-pass=register-coalescer -o - %s
2+
---
3+
name: rematerialize_subreg_to_reg_added_impdef_1
4+
tracksRegLiveness: true
5+
body: |
6+
bb.0:
7+
successors: %bb.1(0x2aaaaaab), %bb.2(0x55555555)
8+
liveins: $edi
9+
10+
%0:gr32 = MOV32r0 implicit-def dead $eflags
11+
%1:gr8 = COPY %0.sub_8bit
12+
%2:gr64 = SUBREG_TO_REG 0, killed %0, %subreg.sub_32bit
13+
JCC_1 %bb.2, 5, implicit killed undef $eflags
14+
15+
bb.1:
16+
successors: %bb.3(0x80000000)
17+
18+
JMP_1 %bb.3
19+
20+
bb.2:
21+
successors: %bb.3(0x80000000)
22+
23+
%5:gr64 = IMPLICIT_DEF
24+
%2:gr64 = COPY killed %5
25+
26+
bb.3:
27+
successors: %bb.4(0x30000000), %bb.5(0x50000000)
28+
29+
JCC_1 %bb.5, 5, implicit killed undef $eflags
30+
31+
bb.4:
32+
$al = COPY killed %1
33+
RET 0, killed undef $al
34+
35+
bb.5:
36+
MOV64mr undef $noreg, 1, undef $noreg, 0, undef $noreg, killed %2 :: (store (s64))
37+
RET 0, killed undef $al
38+
39+
...
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# RUN: llc -mtriple=x86_64-unknown-linux-gnu -run-pass=register-coalescer -o - %s
2+
3+
# An implicit-def will be added to SUBREG_TO_REG during coalescing
4+
---
5+
name: rematerialize_subreg_to_reg_added_impdef_0
6+
tracksRegLiveness: true
7+
body: |
8+
bb.0:
9+
successors: %bb.1(0x2aaaaaab), %bb.2(0x55555555)
10+
liveins: $edi
11+
12+
%0:gr32 = MOV32r0 implicit-def dead $eflags
13+
%1:gr8 = COPY %0.sub_8bit
14+
%2:gr64 = SUBREG_TO_REG 0, killed %0, %subreg.sub_32bit
15+
JCC_1 %bb.2, 5, implicit killed undef $eflags
16+
17+
bb.1:
18+
%4:gr8 = COPY %1
19+
%5:gr8 = COPY killed undef %1
20+
JMP_1 %bb.5
21+
22+
bb.2:
23+
%6:gr64 = IMPLICIT_DEF
24+
%2:gr64 = COPY killed %6
25+
%5:gr8 = MOV8ri 1
26+
27+
bb.5:
28+
successors: %bb.6(0x30000000), %bb.7(0x50000000)
29+
30+
TEST8rr killed undef %5, %5, implicit-def $eflags
31+
JCC_1 %bb.7, 5, implicit killed undef $eflags
32+
33+
bb.6:
34+
$al = COPY killed %1
35+
RET 0, killed undef $al
36+
37+
bb.7:
38+
MOV64mr undef $noreg, 1, undef $noreg, 0, undef $noreg, killed %2 :: (store (s64))
39+
RET 0, killed undef $al
40+
41+
...
42+
43+
44+
# Reduced version of previous with the SUBREG_TO_REG already folded
45+
# away.
46+
#
47+
# The mov32r0 defines a subregister and has an implicit-def of the
48+
# super register. After coalescing, the full register implicit def of
49+
# %2 becomes a different subregister def.
50+
51+
---
52+
name: rematerialize_subreg_to_reg_coalesces_to_subreg_impdef
53+
tracksRegLiveness: true
54+
body: |
55+
bb.0:
56+
successors: %bb.1(0x2aaaaaab), %bb.2(0x55555555)
57+
liveins: $edi
58+
59+
undef %2.sub_32bit:gr64_with_sub_8bit = MOV32r0 implicit-def dead $eflags, implicit-def %2
60+
%1:gr8 = COPY %2.sub_8bit
61+
JCC_1 %bb.2, 5, implicit killed undef $eflags
62+
63+
bb.1:
64+
successors: %bb.3(0x80000000)
65+
66+
dead %3:gr8 = COPY %1
67+
%4:gr8 = COPY undef %1
68+
JMP_1 %bb.3
69+
70+
bb.2:
71+
successors: %bb.3(0x80000000)
72+
73+
%5:gr64 = IMPLICIT_DEF
74+
%2:gr64_with_sub_8bit = COPY %5
75+
%4:gr8 = MOV8ri 1
76+
77+
bb.3:
78+
successors: %bb.4(0x30000000), %bb.5(0x50000000)
79+
80+
TEST8rr undef %4, %4, implicit-def $eflags
81+
JCC_1 %bb.5, 5, implicit killed undef $eflags
82+
83+
bb.4:
84+
$al = COPY %1
85+
RET 0, killed undef $al
86+
87+
bb.5:
88+
MOV64mr undef $noreg, 1, undef $noreg, 0, undef $noreg, %2 :: (store (s64))
89+
RET 0, killed undef $al
90+
91+
...

0 commit comments

Comments
 (0)