Skip to content

Commit 52646d0

Browse files
authored
[GISel] Teach computeKnownBitsImpl to handle COPY instructions that change bit width. (#118924)
The sexti32 ComplexRenderFn on RISCV calls computeNumSignBits which calls computeKnownBits. I encountered a case where we looked through a G_PHI and found a COPY that was created from an already selected G_TRUNC from s64 to s32. s32 and s64 integers on RISC-V end up in the same register class. s32 G_PHI is legal to allow f32 phis on RV64. The COPY inherited the types from the original G_TRUNC so the source and destination virtual registers have different widths. This patch uses KnownBits::anyextOrTrunc to adjust the width when they mismatch.
1 parent 3d6b37e commit 52646d0

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ void GISelKnownBits::computeKnownBitsImpl(Register R, KnownBits &Known,
253253
// For COPYs we don't do anything, don't increase the depth.
254254
computeKnownBitsImpl(SrcReg, Known2, DemandedElts,
255255
Depth + (Opcode != TargetOpcode::COPY));
256+
Known2 = Known2.anyextOrTrunc(BitWidth);
256257
Known = Known.intersectWith(Known2);
257258
// If we reach a point where we don't know anything
258259
// just stop looking through the operands.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
2+
# RUN: llc -mtriple=riscv64 -mattr=+zbb,+f -run-pass=instruction-select %s -o - | FileCheck %s
3+
4+
---
5+
name: foo
6+
legalized: true
7+
regBankSelected: true
8+
tracksRegLiveness: true
9+
body: |
10+
; CHECK-LABEL: name: foo
11+
; CHECK: bb.0:
12+
; CHECK-NEXT: successors: %bb.1(0x80000000)
13+
; CHECK-NEXT: {{ $}}
14+
; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr = COPY $x0
15+
; CHECK-NEXT: {{ $}}
16+
; CHECK-NEXT: bb.1:
17+
; CHECK-NEXT: successors: %bb.1(0x80000000)
18+
; CHECK-NEXT: {{ $}}
19+
; CHECK-NEXT: [[PHI:%[0-9]+]]:gpr = PHI [[COPY]], %bb.0, %6, %bb.1
20+
; CHECK-NEXT: [[ADDIW:%[0-9]+]]:gpr = ADDIW [[PHI]], 0
21+
; CHECK-NEXT: [[FCVT_S_W:%[0-9]+]]:fpr32 = nofpexcept FCVT_S_W [[ADDIW]], 7
22+
; CHECK-NEXT: [[FCVT_W_S:%[0-9]+]]:gpr = nofpexcept FCVT_W_S [[FCVT_S_W]], 1
23+
; CHECK-NEXT: PseudoBR %bb.1
24+
bb.1:
25+
%7:gprb(s64) = G_CONSTANT i64 0
26+
%3:gprb(s32) = G_TRUNC %7(s64)
27+
28+
bb.2:
29+
%0:gprb(s32) = G_PHI %3(s32), %bb.1, %2(s32), %bb.2
30+
%6:gprb(s64) = G_SEXT %0(s32)
31+
%1:fprb(s32) = G_SITOFP %6(s64)
32+
%5:gprb(s64) = G_FCVT_W_RV64 %1(s32), 1
33+
%2:gprb(s32) = G_TRUNC %5(s64)
34+
G_BR %bb.2
35+
36+
...

0 commit comments

Comments
 (0)