Skip to content

Commit 82917c8

Browse files
authored
[RISCV] Add SRAW to ComputeNumSignBitsForTargetNode. (#155564)
1 parent 249167a commit 82917c8

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21470,8 +21470,16 @@ unsigned RISCVTargetLowering::ComputeNumSignBitsForTargetNode(
2147021470
if (Tmp < 33) return 1;
2147121471
return 33;
2147221472
}
21473+
case RISCVISD::SRAW: {
21474+
unsigned Tmp =
21475+
DAG.ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
21476+
// sraw produces at least 33 sign bits. If the input already has more than
21477+
// 33 sign bits sraw, will preserve them.
21478+
// TODO: A more precise answer could be calculated depending on known bits
21479+
// in the shift amount.
21480+
return std::max(Tmp, 33U);
21481+
}
2147321482
case RISCVISD::SLLW:
21474-
case RISCVISD::SRAW:
2147521483
case RISCVISD::SRLW:
2147621484
case RISCVISD::DIVW:
2147721485
case RISCVISD::DIVUW:
@@ -21482,9 +21490,7 @@ unsigned RISCVTargetLowering::ComputeNumSignBitsForTargetNode(
2148221490
case RISCVISD::FCVT_WU_RV64:
2148321491
case RISCVISD::STRICT_FCVT_W_RV64:
2148421492
case RISCVISD::STRICT_FCVT_WU_RV64:
21485-
// TODO: As the result is sign-extended, this is conservatively correct. A
21486-
// more precise answer could be calculated for SRAW depending on known
21487-
// bits in the shift amount.
21493+
// TODO: As the result is sign-extended, this is conservatively correct.
2148821494
return 33;
2148921495
case RISCVISD::VMV_X_S: {
2149021496
// The number of sign bits of the scalar result is computed by obtaining the

llvm/test/CodeGen/RISCV/rv64i-shift-sext.ll

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,18 @@ define signext i32 @test14(ptr %0, ptr %1, i64 %2) {
220220
%12 = add i32 %9, %11
221221
ret i32 %12
222222
}
223+
224+
; Test that we can propagate sign bits through sraw. We should use an slli
225+
; instead of slliw.
226+
define signext i32 @test15(i32 signext %x, i32 signext %y) {
227+
; RV64I-LABEL: test15:
228+
; RV64I: # %bb.0:
229+
; RV64I-NEXT: srli a0, a0, 1
230+
; RV64I-NEXT: sraw a0, a0, a1
231+
; RV64I-NEXT: slli a0, a0, 1
232+
; RV64I-NEXT: ret
233+
%a = ashr i32 %x, 1
234+
%b = ashr i32 %a, %y
235+
%c = shl i32 %b, 1
236+
ret i32 %c
237+
}

0 commit comments

Comments
 (0)