Skip to content

Commit 243c979

Browse files
committed
Reland "[RegisterCoalescer] Fix up subreg lanemasks after rematerializing. (#116191)"
This patch can now reland after 318c69d relanded #114827. This reverts commit 14a58a1.
1 parent 61653f8 commit 243c979

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

llvm/lib/CodeGen/RegisterCoalescer.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,27 @@ bool RegisterCoalescer::reMaterializeTrivialDef(const CoalescerPair &CP,
13741374
MachineInstr &NewMI = *std::prev(MII);
13751375
NewMI.setDebugLoc(DL);
13761376

1377+
// In a situation like the following:
1378+
//
1379+
// undef %2.subreg:reg = INST %1:reg ; DefMI (rematerializable),
1380+
// ; DefSubIdx = subreg
1381+
// %3:reg = COPY %2 ; SrcIdx = DstIdx = 0
1382+
// .... = SOMEINSTR %3:reg
1383+
//
1384+
// there are no subranges for %3 so after rematerialization we need
1385+
// to explicitly create them. Undefined subranges are removed later on.
1386+
if (DstReg.isVirtual() && DefSubIdx && !CP.getSrcIdx() && !CP.getDstIdx() &&
1387+
MRI->shouldTrackSubRegLiveness(DstReg)) {
1388+
LiveInterval &DstInt = LIS->getInterval(DstReg);
1389+
if (!DstInt.hasSubRanges()) {
1390+
LaneBitmask FullMask = MRI->getMaxLaneMaskForVReg(DstReg);
1391+
LaneBitmask UsedLanes = TRI->getSubRegIndexLaneMask(DefSubIdx);
1392+
LaneBitmask UnusedLanes = FullMask & ~UsedLanes;
1393+
DstInt.createSubRangeFrom(LIS->getVNInfoAllocator(), UsedLanes, DstInt);
1394+
DstInt.createSubRangeFrom(LIS->getVNInfoAllocator(), UnusedLanes, DstInt);
1395+
}
1396+
}
1397+
13771398
// In a situation like the following:
13781399
// %0:subreg = instr ; DefMI, subreg = DstIdx
13791400
// %1 = copy %0:subreg ; CopyMI, SrcIdx = 0
@@ -1486,6 +1507,7 @@ bool RegisterCoalescer::reMaterializeTrivialDef(const CoalescerPair &CP,
14861507
NewRC = TRI->getCommonSubClass(NewRC, DefRC);
14871508
assert(NewRC && "subreg chosen for remat incompatible with instruction");
14881509
}
1510+
14891511
// Remap subranges to new lanemask and change register class.
14901512
LiveInterval &DstInt = LIS->getInterval(DstReg);
14911513
for (LiveInterval::SubRange &SR : DstInt.subranges()) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# RUN: llc -mtriple=aarch64 -verify-machineinstrs -o - -run-pass=register-coalescer -aarch64-enable-subreg-liveness-tracking %s | FileCheck %s --check-prefix=CHECK
2+
# RUN: llc -mtriple=aarch64 -verify-machineinstrs -o /dev/null -run-pass=register-coalescer -aarch64-enable-subreg-liveness-tracking -debug-only=regalloc %s 2>&1 | FileCheck %s --check-prefix=CHECK-DBG
3+
# REQUIRES: asserts
4+
5+
# CHECK-DBG: ********** REGISTER COALESCER **********
6+
# CHECK-DBG: ********** Function: test
7+
# CHECK-DBG: ********** JOINING INTERVALS ***********
8+
# CHECK-DBG: ********** INTERVALS **********
9+
# CHECK-DBG: %0 [16r,32r:0) 0@16r weight:0.000000e+00
10+
# CHECK-DBG: %3 [48r,112r:0) 0@48r L0000000000000040 [48r,112r:0) 0@48r weight:0.000000e+00
11+
# CHECK-DBG: %4 [80r,112e:1)[112e,112d:0) 0@112e 1@80r L0000000000000080 [112e,112d:0) 0@112e L0000000000000040 [80r,112e:1)[112e,112d:0) 0@112e 1@80r weight:0.000000e+00
12+
# CHECK-DBG: %5 [32r,112r:1)[112r,112d:0) 0@112r 1@32r weight:0.000000e+00
13+
---
14+
name: test
15+
tracksRegLiveness: true
16+
fixedStack: []
17+
stack:
18+
- { id: 0, name: '', type: default, offset: 0, size: 65, alignment: 16,
19+
stack-id: default }
20+
body: |
21+
bb.0.entry:
22+
; CHECK-LABEL: name: test
23+
; CHECK: [[ADDXri:%[0-9]+]]:gpr64sp = ADDXri %stack.0, 0, 0
24+
; CHECK-NEXT: [[ADDXri1:%[0-9]+]]:gpr64common = nuw ADDXri [[ADDXri]], 64, 0
25+
; CHECK-NEXT: undef [[MOVi32imm:%[0-9]+]].sub_32:gpr64 = MOVi32imm 64
26+
; CHECK-NEXT: undef [[MOVi32imm1:%[0-9]+]].sub_32:gpr64 = MOVi32imm 64
27+
; CHECK-NEXT: dead [[ADDXri1]]:gpr64common, dead early-clobber [[MOVi32imm1]]:gpr64 = MOPSMemorySetPseudo [[ADDXri1]], [[MOVi32imm1]], [[MOVi32imm]], implicit-def dead $nzcv
28+
; CHECK-NEXT: RET_ReallyLR
29+
%1:gpr64sp = ADDXri %stack.0, 0, 0
30+
%2:gpr64common = nuw ADDXri killed %1, 64, 0
31+
%3:gpr32 = MOVi32imm 64
32+
%4:gpr64 = SUBREG_TO_REG 0, killed %3, %subreg.sub_32
33+
%6:gpr64 = COPY %4
34+
%5:gpr64common = COPY killed %2
35+
dead %5:gpr64common, dead early-clobber %6:gpr64 = MOPSMemorySetPseudo %5, %6, %4, implicit-def dead $nzcv
36+
RET_ReallyLR
37+
38+
...

0 commit comments

Comments
 (0)