Skip to content

Commit ee47427

Browse files
authored
[RegisterCoalescer] Fix subrange update when rematerialization widens a def (#151974)
Currently, when an instruction rematerialized by the register coalescer defines more subregs of the destination register than the original COPY instruction did, we only add dead defs for the newly defined subregs if they were not defined anywhere else. For example, consider something like this before rematerialization: ``` %0:reg64 = CONSTANT 1 %1:reg128.sub_lo64_lo32 = COPY %0.lo32 %1:reg128.sub_lo64_hi32 = ... ... ``` that would look like this after rematerializing `%0`: ``` %0:reg64 = CONSTANT 2 %1:reg128.sub_lo64 = CONSTANT 2 %1:reg128.sub_lo64_hi32 = ... ... ``` A dead def would not be added for `%1.sub_lo64_hi32` at the 2nd instruction because it's subrange wasn't empty beforehand.
1 parent fd07d90 commit ee47427

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

llvm/lib/CodeGen/RegisterCoalescer.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,11 +1624,11 @@ bool RegisterCoalescer::reMaterializeTrivialDef(const CoalescerPair &CP,
16241624
UpdatedSubRanges = true;
16251625
} else {
16261626
// We know that this lane is defined by this instruction,
1627-
// but at this point it may be empty because it is not used by
1628-
// anything. This happens when updateRegDefUses adds the missing
1629-
// lanes. Assign that lane a dead def so that the interferences
1630-
// are properly modeled.
1631-
if (SR.empty())
1627+
// but at this point it might not be live because it was not defined
1628+
// by the original instruction. This happens when the
1629+
// rematerialization widens the defined register. Assign that lane a
1630+
// dead def so that the interferences are properly modeled.
1631+
if (!SR.liveAt(DefIndex))
16321632
SR.createDeadDef(DefIndex, Alloc);
16331633
}
16341634
}

llvm/test/CodeGen/SystemZ/regcoal-subranges-update-remat.mir

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,28 @@ body: |
4343
%3:gr32bit = COPY killed %1
4444
Return implicit %3
4545
...
46+
47+
---
48+
name: test_dead_at_remat_later_defined
49+
tracksRegLiveness: true
50+
body: |
51+
bb.0:
52+
; CHECK-LABEL: name: test_dead_at_remat_later_defined
53+
; CHECK: undef [[LHI:%[0-9]+]].subreg_l32:gr128bit = LHI 0
54+
; CHECK-NEXT: [[LHI:%[0-9]+]].subreg_l64:gr128bit = LGHI 2
55+
; CHECK-NEXT: [[LHI1:%[0-9]+]]:gr32bit = LHI 1
56+
; CHECK-NEXT: [[LHI:%[0-9]+]].subreg_lh32:gr128bit = COPY [[LHI1]]
57+
; CHECK-NEXT: [[LGHI:%[0-9]+]]:gr64bit = LGHI 2
58+
; CHECK-NEXT: [[LHI:%[0-9]+]].subreg_h32:gr128bit = COPY [[LGHI]].subreg_l32
59+
; CHECK-NEXT: $r0q = COPY [[LHI]]
60+
; CHECK-NEXT: $r4d = COPY [[LGHI]].subreg_h32
61+
%0:gr64bit = LGHI 2
62+
%1:gr32bit = LHI 0
63+
%2:gr32bit = LHI 1
64+
undef %3.subreg_ll32:gr128bit = COPY %0.subreg_l32
65+
%3.subreg_lh32:gr128bit = COPY %2
66+
%3.subreg_l32:gr128bit = COPY %1
67+
%3.subreg_h32:gr128bit = COPY %0.subreg_l32
68+
$r0q = COPY %3
69+
$r4d = COPY %0.subreg_h32
70+
...

0 commit comments

Comments
 (0)