[llvm] [RISCV] Fold (sub 0, (srl (and X, (1 << ShAmt)), ShAmt)) -> (sra (shl X, ShAmt2), bits-1) (PR #229509)
Craig Topper via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 6 14:50:25 PDT 2026
https://github.com/topperc updated https://github.com/llvm/llvm-project/pull/229509
>From 1fc862fd3796d0cceee5c6237ad63c042ce60be6 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Tue, 6 Oct 2026 10:23:07 -0700
Subject: [PATCH 1/2] Precommit tests
---
llvm/test/CodeGen/RISCV/fclass-select.ll | 120 +++++++++++++++++++++++
1 file changed, 120 insertions(+)
create mode 100644 llvm/test/CodeGen/RISCV/fclass-select.ll
diff --git a/llvm/test/CodeGen/RISCV/fclass-select.ll b/llvm/test/CodeGen/RISCV/fclass-select.ll
new file mode 100644
index 0000000000000..5b8c558e9a265
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/fclass-select.ll
@@ -0,0 +1,120 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
+; RUN: llc -mtriple=riscv32 -mattr=+d < %s | FileCheck %s --check-prefix=RV32
+; RUN: llc -mtriple=riscv64 -mattr=+d < %s | FileCheck %s --check-prefix=RV64
+
+; Fold (sub 0, (srl (and X, (1 << ShAmt)), ShAmt)) ->
+; (sra (shl X, bits-1-ShAmt), bits-1)
+; This turns an and+srl+neg sequence into a sign-extension of a single bit
+; via shl+sra. llvm.is.fpclass with a single-bit mask is a convenient way to
+; generate the and+srl+sub pattern.
+
+define i32 @is_snan_sext_f64(double %x) nounwind {
+; RV32-LABEL: is_snan_sext_f64:
+; RV32: # %bb.0:
+; RV32-NEXT: fclass.d a0, fa0
+; RV32-NEXT: slli a0, a0, 23
+; RV32-NEXT: srli a0, a0, 31
+; RV32-NEXT: neg a0, a0
+; RV32-NEXT: ret
+;
+; RV64-LABEL: is_snan_sext_f64:
+; RV64: # %bb.0:
+; RV64-NEXT: fclass.d a0, fa0
+; RV64-NEXT: slli a0, a0, 55
+; RV64-NEXT: srli a0, a0, 63
+; RV64-NEXT: neg a0, a0
+; RV64-NEXT: ret
+ %c = call i1 @llvm.is.fpclass.f64(double %x, i32 1) ; fcSNan
+ %s = select i1 %c, i32 -1, i32 0
+ ret i32 %s
+}
+
+define i32 @is_snan_sext_f32(float %x) nounwind {
+; RV32-LABEL: is_snan_sext_f32:
+; RV32: # %bb.0:
+; RV32-NEXT: fclass.s a0, fa0
+; RV32-NEXT: slli a0, a0, 23
+; RV32-NEXT: srli a0, a0, 31
+; RV32-NEXT: neg a0, a0
+; RV32-NEXT: ret
+;
+; RV64-LABEL: is_snan_sext_f32:
+; RV64: # %bb.0:
+; RV64-NEXT: fclass.s a0, fa0
+; RV64-NEXT: slli a0, a0, 55
+; RV64-NEXT: srli a0, a0, 63
+; RV64-NEXT: neg a0, a0
+; RV64-NEXT: ret
+ %c = call i1 @llvm.is.fpclass.f32(float %x, i32 1) ; fcSNan
+ %s = select i1 %c, i32 -1, i32 0
+ ret i32 %s
+}
+
+; The and gets combined away by DAGCombiner before performSUBCombine runs
+; (it can prove the high bits are already zero), so this case doesn't match
+; the and+srl pattern and keeps the srli+neg form instead.
+define i32 @is_qnan_sext_f64(double %x) nounwind {
+; RV32-LABEL: is_qnan_sext_f64:
+; RV32: # %bb.0:
+; RV32-NEXT: fclass.d a0, fa0
+; RV32-NEXT: srli a0, a0, 9
+; RV32-NEXT: neg a0, a0
+; RV32-NEXT: ret
+;
+; RV64-LABEL: is_qnan_sext_f64:
+; RV64: # %bb.0:
+; RV64-NEXT: fclass.d a0, fa0
+; RV64-NEXT: srli a0, a0, 9
+; RV64-NEXT: neg a0, a0
+; RV64-NEXT: ret
+ %c = call i1 @llvm.is.fpclass.f64(double %x, i32 2) ; fcQNan
+ %s = select i1 %c, i32 -1, i32 0
+ ret i32 %s
+}
+
+define i32 @is_posinf_sext_f64(double %x) nounwind {
+; RV32-LABEL: is_posinf_sext_f64:
+; RV32: # %bb.0:
+; RV32-NEXT: fclass.d a0, fa0
+; RV32-NEXT: slli a0, a0, 24
+; RV32-NEXT: srli a0, a0, 31
+; RV32-NEXT: neg a0, a0
+; RV32-NEXT: ret
+;
+; RV64-LABEL: is_posinf_sext_f64:
+; RV64: # %bb.0:
+; RV64-NEXT: fclass.d a0, fa0
+; RV64-NEXT: slli a0, a0, 56
+; RV64-NEXT: srli a0, a0, 63
+; RV64-NEXT: neg a0, a0
+; RV64-NEXT: ret
+ %c = call i1 @llvm.is.fpclass.f64(double %x, i32 512) ; fcPosInf
+ %s = select i1 %c, i32 -1, i32 0
+ ret i32 %s
+}
+
+; A multi-bit mask isn't a single-bit extraction, so this should not fold to
+; the shl+sra pattern.
+define i32 @is_nan_sext_f64_no_fold(double %x) nounwind {
+; RV32-LABEL: is_nan_sext_f64_no_fold:
+; RV32: # %bb.0:
+; RV32-NEXT: fclass.d a0, fa0
+; RV32-NEXT: andi a0, a0, 768
+; RV32-NEXT: seqz a0, a0
+; RV32-NEXT: addi a0, a0, -1
+; RV32-NEXT: ret
+;
+; RV64-LABEL: is_nan_sext_f64_no_fold:
+; RV64: # %bb.0:
+; RV64-NEXT: fclass.d a0, fa0
+; RV64-NEXT: andi a0, a0, 768
+; RV64-NEXT: seqz a0, a0
+; RV64-NEXT: addi a0, a0, -1
+; RV64-NEXT: ret
+ %c = call i1 @llvm.is.fpclass.f64(double %x, i32 3) ; fcNan (two bits)
+ %s = select i1 %c, i32 -1, i32 0
+ ret i32 %s
+}
+
+declare i1 @llvm.is.fpclass.f64(double, i32)
+declare i1 @llvm.is.fpclass.f32(float, i32)
>From b4a5037fee40f2b1d8ccdc1e9d100555b93ab548 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Tue, 6 Oct 2026 10:28:39 -0700
Subject: [PATCH 2/2] [RISCV] Fold (sub 0, (srl (and X, (1 << ShAmt)), ShAmt))
-> (sra (shl X, ShAmt2), bits-1)
Improves codegen of is.fpclass+select. The AND to test bits of
is.fpclass may get turned into and+srl while the select emits a
neg. Isel will turn the and+srl into shl+srl but it's too late to
fold the neg.
Assisted-by: Claude
---
llvm/lib/Target/RISCV/RISCVISelLowering.cpp | 23 +++++++++++++++++++++
llvm/test/CodeGen/RISCV/fclass-select.ll | 18 ++++++----------
2 files changed, 29 insertions(+), 12 deletions(-)
diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index a2588e661347f..0fa1220af3cdd 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -19495,6 +19495,29 @@ static SDValue performSUBCombine(SDNode *N, SelectionDAG &DAG,
}
}
+ // fold (sub 0, (srl (and X, (1 << ShAmt)), ShAmt)) ->
+ // (sra (shl X, ShAmt2), bits - 1)
+ // where ShAmt2 = (bits - 1) - ShAmt. This extracts bit ShAmt of X and
+ // sign-extends it across the whole register.
+ {
+ using namespace SDPatternMatch;
+ SDValue X;
+ uint64_t Mask, ShAmt;
+ if (isNullConstant(N0) &&
+ sd_match(N1,
+ m_OneUse(m_Srl(m_OneUse(m_And(m_Value(X), m_ConstInt(Mask))),
+ m_ConstInt(ShAmt)))) &&
+ isPowerOf2_64(Mask) && Log2_64(Mask) == ShAmt) {
+ SDLoc DL(N);
+ unsigned Bits = VT.getSizeInBits();
+ unsigned ShAmt2 = Bits - 1 - ShAmt;
+ SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, X,
+ DAG.getShiftAmountConstant(ShAmt2, VT, DL));
+ return DAG.getNode(ISD::SRA, DL, VT, Shl,
+ DAG.getShiftAmountConstant(Bits - 1, VT, DL));
+ }
+ }
+
if (SDValue V = combinePExtWideningSubAcc(N, DAG, Subtarget))
return V;
if (SDValue V = combinePExtWideningAddSub(N, DAG, Subtarget))
diff --git a/llvm/test/CodeGen/RISCV/fclass-select.ll b/llvm/test/CodeGen/RISCV/fclass-select.ll
index 5b8c558e9a265..34e54f1a8c79a 100644
--- a/llvm/test/CodeGen/RISCV/fclass-select.ll
+++ b/llvm/test/CodeGen/RISCV/fclass-select.ll
@@ -13,16 +13,14 @@ define i32 @is_snan_sext_f64(double %x) nounwind {
; RV32: # %bb.0:
; RV32-NEXT: fclass.d a0, fa0
; RV32-NEXT: slli a0, a0, 23
-; RV32-NEXT: srli a0, a0, 31
-; RV32-NEXT: neg a0, a0
+; RV32-NEXT: srai a0, a0, 31
; RV32-NEXT: ret
;
; RV64-LABEL: is_snan_sext_f64:
; RV64: # %bb.0:
; RV64-NEXT: fclass.d a0, fa0
; RV64-NEXT: slli a0, a0, 55
-; RV64-NEXT: srli a0, a0, 63
-; RV64-NEXT: neg a0, a0
+; RV64-NEXT: srai a0, a0, 63
; RV64-NEXT: ret
%c = call i1 @llvm.is.fpclass.f64(double %x, i32 1) ; fcSNan
%s = select i1 %c, i32 -1, i32 0
@@ -34,16 +32,14 @@ define i32 @is_snan_sext_f32(float %x) nounwind {
; RV32: # %bb.0:
; RV32-NEXT: fclass.s a0, fa0
; RV32-NEXT: slli a0, a0, 23
-; RV32-NEXT: srli a0, a0, 31
-; RV32-NEXT: neg a0, a0
+; RV32-NEXT: srai a0, a0, 31
; RV32-NEXT: ret
;
; RV64-LABEL: is_snan_sext_f32:
; RV64: # %bb.0:
; RV64-NEXT: fclass.s a0, fa0
; RV64-NEXT: slli a0, a0, 55
-; RV64-NEXT: srli a0, a0, 63
-; RV64-NEXT: neg a0, a0
+; RV64-NEXT: srai a0, a0, 63
; RV64-NEXT: ret
%c = call i1 @llvm.is.fpclass.f32(float %x, i32 1) ; fcSNan
%s = select i1 %c, i32 -1, i32 0
@@ -77,16 +73,14 @@ define i32 @is_posinf_sext_f64(double %x) nounwind {
; RV32: # %bb.0:
; RV32-NEXT: fclass.d a0, fa0
; RV32-NEXT: slli a0, a0, 24
-; RV32-NEXT: srli a0, a0, 31
-; RV32-NEXT: neg a0, a0
+; RV32-NEXT: srai a0, a0, 31
; RV32-NEXT: ret
;
; RV64-LABEL: is_posinf_sext_f64:
; RV64: # %bb.0:
; RV64-NEXT: fclass.d a0, fa0
; RV64-NEXT: slli a0, a0, 56
-; RV64-NEXT: srli a0, a0, 63
-; RV64-NEXT: neg a0, a0
+; RV64-NEXT: srai a0, a0, 63
; RV64-NEXT: ret
%c = call i1 @llvm.is.fpclass.f64(double %x, i32 512) ; fcPosInf
%s = select i1 %c, i32 -1, i32 0
More information about the llvm-commits
mailing list