Skip to content

Commit 495d3ea

Browse files
authored
[MachineSink][RISCV] Only call isConstantPhysReg or isIgnorableUse for uses. (#99363)
The included test case contains X0 as a def register. X0 is considered a constant register when it is a use. When its a def, it means to throw away the result value. If we treat it as a constant register here, we will execute the continue and not assign `DefReg` to any register. This will cause a crash when trying to get the register class for `DefReg` after the loop. By only checking isConstantPhysReg for uses, we will reach the `return false` a little further down and stop processing this instruction.
1 parent 8044a86 commit 495d3ea

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

llvm/lib/CodeGen/MachineSink.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ bool MachineSinking::PerformSinkAndFold(MachineInstr &MI,
417417
continue;
418418
}
419419

420-
if (Reg.isPhysical() &&
420+
if (Reg.isPhysical() && MO.isUse() &&
421421
(MRI->isConstantPhysReg(Reg) || TII->isIgnorableUse(MO)))
422422
continue;
423423

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
2+
# RUN: llc %s -mtriple=riscv64 -run-pass=machine-sink -o - | FileCheck %s
3+
4+
---
5+
name: main
6+
tracksRegLiveness: true
7+
registers:
8+
- { id: 0, class: fpr32 }
9+
- { id: 1, class: fpr32 }
10+
- { id: 2, class: gpr }
11+
- { id: 3, class: gpr }
12+
liveins:
13+
- { reg: '$f10_f', virtual-reg: '%0' }
14+
- { reg: '$f11_f', virtual-reg: '%1' }
15+
body: |
16+
bb.0.entry:
17+
liveins: $f10_f, $f11_f
18+
19+
; CHECK-LABEL: name: main
20+
; CHECK: liveins: $f10_f, $f11_f
21+
; CHECK-NEXT: {{ $}}
22+
; CHECK-NEXT: [[COPY:%[0-9]+]]:fpr32 = COPY $f11_f
23+
; CHECK-NEXT: [[COPY1:%[0-9]+]]:fpr32 = COPY $f10_f
24+
; CHECK-NEXT: [[ReadFFLAGS:%[0-9]+]]:gpr = ReadFFLAGS implicit $fflags
25+
; CHECK-NEXT: [[FLE_S:%[0-9]+]]:gpr = nofpexcept FLE_S [[COPY1]], [[COPY]]
26+
; CHECK-NEXT: WriteFFLAGS killed [[ReadFFLAGS]], implicit-def $fflags
27+
; CHECK-NEXT: $x0 = nofpexcept FEQ_S [[COPY1]], [[COPY]]
28+
; CHECK-NEXT: $x10 = COPY [[FLE_S]]
29+
; CHECK-NEXT: PseudoRET implicit $x10
30+
%1:fpr32 = COPY $f11_f
31+
%0:fpr32 = COPY $f10_f
32+
%3:gpr = ReadFFLAGS implicit $fflags
33+
%2:gpr = nofpexcept FLE_S %0, %1
34+
WriteFFLAGS killed %3, implicit-def $fflags
35+
$x0 = nofpexcept FEQ_S %0, %1
36+
$x10 = COPY %2
37+
PseudoRET implicit $x10
38+
39+
...

0 commit comments

Comments
 (0)