Skip to content

Commit 9d1b578

Browse files
authored
[RISCV] Shrink deleted dead ADDI's use if coalesced in RISCVInsertVSETVLI (#166729)
If two vsetvlis are coalesced (or during insertion when a VSETVLIInfo turns out to be compatible), we may end up with a dead ADDI that we delete. Normally these are LIs (addi $x0, imm), but it's possible for the first operand to be a virtual register. Make sure we shrink the live interval of it when we remove it to avoid crashes. Fixes #166613
1 parent 99bb789 commit 9d1b578

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,7 +1583,10 @@ void RISCVInsertVSETVLI::emitVSETVLIs(MachineBasicBlock &MBB) {
15831583
if (!TII->isAddImmediate(*DeadMI, Reg))
15841584
continue;
15851585
LIS->RemoveMachineInstrFromMaps(*DeadMI);
1586+
Register AddReg = DeadMI->getOperand(1).getReg();
15861587
DeadMI->eraseFromParent();
1588+
if (AddReg.isVirtual())
1589+
LIS->shrinkToUses(&LIS->getInterval(AddReg));
15871590
}
15881591
}
15891592
}
@@ -1869,11 +1872,15 @@ void RISCVInsertVSETVLI::coalesceVSETVLIs(MachineBasicBlock &MBB) const {
18691872
// Loop over the dead AVL values, and delete them now. This has
18701873
// to be outside the above loop to avoid invalidating iterators.
18711874
for (auto *MI : ToDelete) {
1875+
assert(MI->getOpcode() == RISCV::ADDI);
1876+
Register AddReg = MI->getOperand(1).getReg();
18721877
if (LIS) {
18731878
LIS->removeInterval(MI->getOperand(0).getReg());
18741879
LIS->RemoveMachineInstrFromMaps(*MI);
18751880
}
18761881
MI->eraseFromParent();
1882+
if (LIS && AddReg.isVirtual())
1883+
LIS->shrinkToUses(&LIS->getInterval(AddReg));
18771884
}
18781885
}
18791886

llvm/test/CodeGen/RISCV/rvv/vsetvli-insert.ll

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,3 +863,19 @@ entry:
863863
i64 2)
864864
ret <vscale x 1 x double> %2
865865
}
866+
867+
; The two vsetvlis will be coalesced so the add will be made dead and
868+
; removed. Make sure we shrink the live interval of %x.
869+
define void @non_li_addi(i64 %x, ptr %p) {
870+
; CHECK-LABEL: non_li_addi:
871+
; CHECK: # %bb.0: # %entry
872+
; CHECK-NEXT: vsetivli zero, 1, e64, m1, ta, ma
873+
; CHECK-NEXT: ret
874+
entry:
875+
%add = add i64 %x, 1
876+
%0 = tail call i64 @llvm.riscv.vsetvli(i64 %add, i64 3, i64 0)
877+
%1 = call <vscale x 8 x i8> @llvm.riscv.vle(<vscale x 8 x i8> poison, ptr %p, i64 %0)
878+
%2 = tail call i64 @llvm.riscv.vsetvli(i64 1, i64 3, i64 0)
879+
%3 = tail call { <vscale x 8 x i8>, i64 } @llvm.riscv.vleff(<vscale x 8 x i8> poison, ptr %p, i64 %2)
880+
ret void
881+
}

llvm/test/CodeGen/RISCV/rvv/vsetvli-insert.mir

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@
104104
ret void
105105
}
106106

107+
define void @non_li_addi() {
108+
ret void
109+
}
110+
107111
declare <vscale x 1 x i64> @llvm.riscv.vadd.nxv1i64.nxv1i64.i64(<vscale x 1 x i64>, <vscale x 1 x i64>, <vscale x 1 x i64>, i64) #1
108112

109113
declare <vscale x 1 x i64> @llvm.riscv.vle.nxv1i64.i64(<vscale x 1 x i64>, ptr nocapture, i64) #4
@@ -664,3 +668,23 @@ body: |
664668
bb.2:
665669
$x10 = COPY %vl
666670
PseudoRET implicit killed $x10
671+
...
672+
---
673+
# The two vsetvlis will be coalesced so the ADDI will be made dead and removed.
674+
# Make sure we shrink the live interval of %0.
675+
name: non_li_addi
676+
tracksRegLiveness: true
677+
body: |
678+
bb.0:
679+
liveins: $x10
680+
; CHECK-LABEL: name: non_li_addi
681+
; CHECK: liveins: $x10
682+
; CHECK-NEXT: {{ $}}
683+
; CHECK-NEXT: dead [[COPY:%[0-9]+]]:gpr = COPY $x10
684+
; CHECK-NEXT: dead [[PseudoVSETIVLI:%[0-9]+]]:gprnox0 = PseudoVSETIVLI 1, 216 /* e64, m1, ta, ma */, implicit-def $vl, implicit-def $vtype
685+
; CHECK-NEXT: PseudoRET
686+
%0:gpr = COPY $x10
687+
%1:gprnox0 = ADDI %0, 1
688+
%2:gprnox0 = PseudoVSETVLI %1, 216 /* e64, m1, ta, ma */, implicit-def $vl, implicit-def $vtype
689+
%3:gprnox0 = PseudoVSETIVLI 1, 216 /* e64, m1, ta, ma */, implicit-def $vl, implicit-def $vtype
690+
PseudoRET

0 commit comments

Comments
 (0)