[llvm] 188702e - [DAGCombiner] Fold smax(X, -1)/smin(X, 0) to bitwise shift forms (#206242)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 8 06:11:26 PDT 2026


Author: Aayush Shrivastava
Date: 2026-07-08T13:11:20Z
New Revision: 188702eeb742a66b8cf32722ca4d8a8d64e77f83

URL: https://github.com/llvm/llvm-project/commit/188702eeb742a66b8cf32722ca4d8a8d64e77f83
DIFF: https://github.com/llvm/llvm-project/commit/188702eeb742a66b8cf32722ca4d8a8d64e77f83.diff

LOG: [DAGCombiner] Fold smax(X, -1)/smin(X, 0) to bitwise shift forms (#206242)

Fixes #206153

`smax(X, -1) = or(X,  ashr(X, BW-1))`
`smin(X,  0) = and(X, ashr(X, BW-1))`

`ashr(X, BW-1)` sign-extends the sign bit to all bits 0 for X≥0, -1 for
X<0. OR-ing with X yields X or -1 = smax(X,-1), AND-ing yields 0 or X =
smin(X,0). Both replace a `compare+cmov` with two cheap bitwise
instructions.

The fold is unconditional (not gated on code size), firing whenever the
target has no native `SMAX/SMIN` instruction for the type
(`isOperationExpand`), the type is legal (`isTypeLegal`), and the input
is not a min/max chain (preserving RISCV-P `sati` saturation patterns).

Added: 
    llvm/test/CodeGen/AArch64/smax-allones.ll

Modified: 
    llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
    llvm/test/CodeGen/RISCV/fpclamptosat.ll
    llvm/test/CodeGen/RISCV/rvv/fpclamptosat_vec.ll
    llvm/test/CodeGen/WebAssembly/fpclamptosat.ll
    llvm/test/CodeGen/X86/combine-smax.ll
    llvm/test/CodeGen/X86/combine-smin.ll
    llvm/test/CodeGen/X86/known-never-zero.ll
    llvm/test/CodeGen/X86/probe-stack-eflags.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 9b8aeccda782b..c81c43c94b0f0 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -6337,6 +6337,40 @@ SDValue DAGCombiner::visitIMINMAX(SDNode *N) {
   if (SDValue RMINMAX = reassociateOps(Opcode, DL, N0, N1, N->getFlags()))
     return RMINMAX;
 
+  // Fold sign-extension masks using arithmetic shift:
+  //   smax(X, -1) -> or(X,  ashr(X, BW-1))
+  //   smin(X,  0) -> and(X, ashr(X, BW-1))
+  // ashr(X, BW-1) sign-extends the sign bit: 0 for X>=0, -1 for X<0.
+  // OR with X yields X (non-negative) or -1 (negative) = smax(X,-1).
+  // AND with X yields 0 (non-negative) or X (negative)  = smin(X, 0).
+  // Both reduce to two instructions vs. a compare+cmov on x86-64.
+  // Only fold when the target has no native SMAX/SMIN instruction for this
+  // type (isOperationExpand), the type is legal (not needing splitting),
+  // the operand is not a min/max chain (preserving target combine patterns
+  // that fold smax(smin(x,C),D) into a single saturation instruction), and
+  // for smax(X,-1) the operand is not a sign extension (doubling its use
+  // count can cause the target to lower the extension less efficiently).
+  APInt C;
+  if (TLI.isTypeLegal(VT) &&
+      !TLI.shouldAvoidTransformToShift(VT, VT.getScalarSizeInBits() - 1) &&
+      sd_match(N1, m_ConstInt(C))) {
+    if (Opcode == ISD::SMAX && TLI.isOperationExpand(ISD::SMAX, VT) &&
+        N0.getOpcode() != ISD::SMIN && N0.getOpcode() != ISD::SIGN_EXTEND &&
+        C.isAllOnes()) {
+      SDValue ShiftAmt =
+          DAG.getShiftAmountConstant(VT.getScalarSizeInBits() - 1, VT, DL);
+      SDValue Shift = DAG.getNode(ISD::SRA, DL, VT, N0, ShiftAmt);
+      return DAG.getNode(ISD::OR, DL, VT, N0, Shift);
+    }
+    if (Opcode == ISD::SMIN && TLI.isOperationExpand(ISD::SMIN, VT) &&
+        N0.getOpcode() != ISD::SMAX && C.isZero()) {
+      SDValue ShiftAmt =
+          DAG.getShiftAmountConstant(VT.getScalarSizeInBits() - 1, VT, DL);
+      SDValue Shift = DAG.getNode(ISD::SRA, DL, VT, N0, ShiftAmt);
+      return DAG.getNode(ISD::AND, DL, VT, N0, Shift);
+    }
+  }
+
   // If both operands are known to have the same sign (both non-negative or both
   // negative), flip between UMIN/UMAX and SMIN/SMAX.
   // Only do this if:

diff  --git a/llvm/test/CodeGen/AArch64/smax-allones.ll b/llvm/test/CodeGen/AArch64/smax-allones.ll
new file mode 100644
index 0000000000000..f22af8756a866
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/smax-allones.ll
@@ -0,0 +1,63 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
+; RUN: llc < %s -mtriple=aarch64-linux | FileCheck %s --check-prefixes=NOCSSC
+; RUN: llc < %s -mtriple=aarch64-linux -mattr=+cssc | FileCheck %s --check-prefixes=CSSC
+;
+; Verify smax(X,-1) and smin(X,0) fold to shifted bitwise forms on AArch64
+; without CSSC (1 shifted ORR/AND), and use native instructions with CSSC.
+; LLVM issue #206153.
+
+define i32 @smax_allones_i32(i32 %x) {
+; NOCSSC-LABEL: smax_allones_i32:
+; NOCSSC:       // %bb.0:
+; NOCSSC-NEXT:    orr w0, w0, w0, asr #31
+; NOCSSC-NEXT:    ret
+;
+; CSSC-LABEL: smax_allones_i32:
+; CSSC:       // %bb.0:
+; CSSC-NEXT:    smax w0, w0, #-1
+; CSSC-NEXT:    ret
+  %r = call i32 @llvm.smax.i32(i32 %x, i32 -1)
+  ret i32 %r
+}
+
+define i64 @smax_allones_i64(i64 %x) {
+; NOCSSC-LABEL: smax_allones_i64:
+; NOCSSC:       // %bb.0:
+; NOCSSC-NEXT:    orr x0, x0, x0, asr #63
+; NOCSSC-NEXT:    ret
+;
+; CSSC-LABEL: smax_allones_i64:
+; CSSC:       // %bb.0:
+; CSSC-NEXT:    smax x0, x0, #-1
+; CSSC-NEXT:    ret
+  %r = call i64 @llvm.smax.i64(i64 %x, i64 -1)
+  ret i64 %r
+}
+
+define i32 @smin_zero_i32(i32 %x) {
+; NOCSSC-LABEL: smin_zero_i32:
+; NOCSSC:       // %bb.0:
+; NOCSSC-NEXT:    and w0, w0, w0, asr #31
+; NOCSSC-NEXT:    ret
+;
+; CSSC-LABEL: smin_zero_i32:
+; CSSC:       // %bb.0:
+; CSSC-NEXT:    smin w0, w0, #0
+; CSSC-NEXT:    ret
+  %r = call i32 @llvm.smin.i32(i32 %x, i32 0)
+  ret i32 %r
+}
+
+define i64 @smin_zero_i64(i64 %x) {
+; NOCSSC-LABEL: smin_zero_i64:
+; NOCSSC:       // %bb.0:
+; NOCSSC-NEXT:    and x0, x0, x0, asr #63
+; NOCSSC-NEXT:    ret
+;
+; CSSC-LABEL: smin_zero_i64:
+; CSSC:       // %bb.0:
+; CSSC-NEXT:    smin x0, x0, #0
+; CSSC-NEXT:    ret
+  %r = call i64 @llvm.smin.i64(i64 %x, i64 0)
+  ret i64 %r
+}

diff  --git a/llvm/test/CodeGen/RISCV/fpclamptosat.ll b/llvm/test/CodeGen/RISCV/fpclamptosat.ll
index 489277cb46ed7..47b258b6fdb70 100644
--- a/llvm/test/CodeGen/RISCV/fpclamptosat.ll
+++ b/llvm/test/CodeGen/RISCV/fpclamptosat.ll
@@ -6555,7 +6555,7 @@ define i32 @ustest_f16i32_nsat(half %x) {
 ; RV32-NEXT:    call __extendhfsf2
 ; RV32-NEXT:    fcvt.w.s a0, fa0, rtz
 ; RV32-NEXT:    srai a1, a0, 31
-; RV32-NEXT:    and a0, a1, a0
+; RV32-NEXT:    and a0, a0, a1
 ; RV32-NEXT:    sgtz a1, a0
 ; RV32-NEXT:    neg a1, a1
 ; RV32-NEXT:    and a0, a1, a0
@@ -6574,7 +6574,7 @@ define i32 @ustest_f16i32_nsat(half %x) {
 ; RV64-NEXT:    call __extendhfsf2
 ; RV64-NEXT:    fcvt.l.s a0, fa0, rtz
 ; RV64-NEXT:    srai a1, a0, 63
-; RV64-NEXT:    and a0, a1, a0
+; RV64-NEXT:    and a0, a0, a1
 ; RV64-NEXT:    sgtz a1, a0
 ; RV64-NEXT:    neg a1, a1
 ; RV64-NEXT:    and a0, a1, a0

diff  --git a/llvm/test/CodeGen/RISCV/rvv/fpclamptosat_vec.ll b/llvm/test/CodeGen/RISCV/rvv/fpclamptosat_vec.ll
index 7129d4cbe4bba..96dfda743b721 100644
--- a/llvm/test/CodeGen/RISCV/rvv/fpclamptosat_vec.ll
+++ b/llvm/test/CodeGen/RISCV/rvv/fpclamptosat_vec.ll
@@ -7090,16 +7090,16 @@ define <4 x i32> @ustest_f16i32_nsat(<4 x half> %x) {
 ; CHECK-NOV-NEXT:    fcvt.l.s a0, fa0, rtz
 ; CHECK-NOV-NEXT:    srai a1, s4, 63
 ; CHECK-NOV-NEXT:    srai a2, s1, 63
-; CHECK-NOV-NEXT:    and a2, a2, s1
+; CHECK-NOV-NEXT:    and a2, s1, a2
 ; CHECK-NOV-NEXT:    srai a3, s2, 63
-; CHECK-NOV-NEXT:    and a1, a1, s4
+; CHECK-NOV-NEXT:    and a1, s4, a1
 ; CHECK-NOV-NEXT:    sgtz a4, a2
-; CHECK-NOV-NEXT:    and a3, a3, s2
+; CHECK-NOV-NEXT:    and a3, s2, a3
 ; CHECK-NOV-NEXT:    neg a4, a4
 ; CHECK-NOV-NEXT:    and a2, a4, a2
 ; CHECK-NOV-NEXT:    srai a4, a0, 63
 ; CHECK-NOV-NEXT:    sgtz a5, a3
-; CHECK-NOV-NEXT:    and a0, a4, a0
+; CHECK-NOV-NEXT:    and a0, a0, a4
 ; CHECK-NOV-NEXT:    sgtz a4, a1
 ; CHECK-NOV-NEXT:    neg a5, a5
 ; CHECK-NOV-NEXT:    and a3, a5, a3

diff  --git a/llvm/test/CodeGen/WebAssembly/fpclamptosat.ll b/llvm/test/CodeGen/WebAssembly/fpclamptosat.ll
index 267d94b601666..158e97c28ca31 100644
--- a/llvm/test/CodeGen/WebAssembly/fpclamptosat.ll
+++ b/llvm/test/CodeGen/WebAssembly/fpclamptosat.ll
@@ -1731,9 +1731,9 @@ define i32 @ustest_f16i32_nsat(half %x) {
 ; CHECK-NEXT:    call __extendhfsf2
 ; CHECK-NEXT:    i32.trunc_sat_f32_s
 ; CHECK-NEXT:    local.tee 0
+; CHECK-NEXT:    local.get 0
 ; CHECK-NEXT:    i32.const 31
 ; CHECK-NEXT:    i32.shr_s
-; CHECK-NEXT:    local.get 0
 ; CHECK-NEXT:    i32.and
 ; CHECK-NEXT:    local.tee 0
 ; CHECK-NEXT:    i32.const 0

diff  --git a/llvm/test/CodeGen/X86/combine-smax.ll b/llvm/test/CodeGen/X86/combine-smax.ll
index ac6f347edcd0d..8a0ec0aac7605 100644
--- a/llvm/test/CodeGen/X86/combine-smax.ll
+++ b/llvm/test/CodeGen/X86/combine-smax.ll
@@ -199,5 +199,52 @@ define <16 x i8> @test_v16i8_demandedbits(<16 x i8> %x, <16 x i8> %y, <16 x i8>
   ret <16 x i8> %res
 }
 
-declare i8 @llvm.smax.i8(i8, i8)
-declare <16 x i8> @llvm.smax.v16i8(<16 x i8>, <16 x i8>)
+; smax(X, -1) -> or(X, ashr(X, BW-1)): saves a byte vs compare+cmov on x86-64.
+define i32 @smax_allones_i32(i32 %x) {
+; CHECK-LABEL: smax_allones_i32:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    movl %edi, %eax
+; CHECK-NEXT:    sarl $31, %eax
+; CHECK-NEXT:    orl %edi, %eax
+; CHECK-NEXT:    retq
+  %r = call i32 @llvm.smax.i32(i32 %x, i32 -1)
+  ret i32 %r
+}
+
+define i64 @smax_allones_i64(i64 %x) {
+; CHECK-LABEL: smax_allones_i64:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    movq %rdi, %rax
+; CHECK-NEXT:    sarq $63, %rax
+; CHECK-NEXT:    orq %rdi, %rax
+; CHECK-NEXT:    retq
+  %r = call i64 @llvm.smax.i64(i64 %x, i64 -1)
+  ret i64 %r
+}
+
+; smax(sext(X), -1) should NOT use the shift fold -- doing so doubles the use
+; count of the sext node and causes worse codegen under register pressure.
+define i64 @smax_allones_sext_i32(i32 %x) {
+; CHECK-LABEL: smax_allones_sext_i32:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    movslq %edi, %rcx
+; CHECK-NEXT:    testq %rcx, %rcx
+; CHECK-NEXT:    movq $-1, %rax
+; CHECK-NEXT:    cmovnsq %rcx, %rax
+; CHECK-NEXT:    retq
+  %sext = sext i32 %x to i64
+  %r = call i64 @llvm.smax.i64(i64 %sext, i64 -1)
+  ret i64 %r
+}
+
+; smax(X, 0) should NOT transform -- no 2-instruction bitwise form exists.
+define i32 @smax_zero_i32(i32 %x) {
+; CHECK-LABEL: smax_zero_i32:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    xorl %eax, %eax
+; CHECK-NEXT:    testl %edi, %edi
+; CHECK-NEXT:    cmovgl %edi, %eax
+; CHECK-NEXT:    retq
+  %r = call i32 @llvm.smax.i32(i32 %x, i32 0)
+  ret i32 %r
+}

diff  --git a/llvm/test/CodeGen/X86/combine-smin.ll b/llvm/test/CodeGen/X86/combine-smin.ll
index 35b1c54835e7b..c948817bfa001 100644
--- a/llvm/test/CodeGen/X86/combine-smin.ll
+++ b/llvm/test/CodeGen/X86/combine-smin.ll
@@ -198,5 +198,37 @@ define <16 x i8> @test_v16i8_demandedbits(<16 x i8> %x, <16 x i8> %y, <16 x i8>
   ret <16 x i8> %res
 }
 
-declare i8 @llvm.smin.i8(i8, i8)
-declare <16 x i8> @llvm.smin.v16i8(<16 x i8>, <16 x i8>)
+; smin(X, 0) -> and(X, ashr(X, BW-1)): saves a byte vs compare+cmov on x86-64.
+define i32 @smin_zero_i32(i32 %x) {
+; CHECK-LABEL: smin_zero_i32:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    movl %edi, %eax
+; CHECK-NEXT:    sarl $31, %eax
+; CHECK-NEXT:    andl %edi, %eax
+; CHECK-NEXT:    retq
+  %r = call i32 @llvm.smin.i32(i32 %x, i32 0)
+  ret i32 %r
+}
+
+define i64 @smin_zero_i64(i64 %x) {
+; CHECK-LABEL: smin_zero_i64:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    movq %rdi, %rax
+; CHECK-NEXT:    sarq $63, %rax
+; CHECK-NEXT:    andq %rdi, %rax
+; CHECK-NEXT:    retq
+  %r = call i64 @llvm.smin.i64(i64 %x, i64 0)
+  ret i64 %r
+}
+
+; smin(X, -1) should NOT transform -- no 2-instruction bitwise form exists.
+define i32 @smin_allones_i32(i32 %x) {
+; CHECK-LABEL: smin_allones_i32:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    cmpl $-1, %edi
+; CHECK-NEXT:    movl $-1, %eax
+; CHECK-NEXT:    cmovll %edi, %eax
+; CHECK-NEXT:    retq
+  %r = call i32 @llvm.smin.i32(i32 %x, i32 -1)
+  ret i32 %r
+}

diff  --git a/llvm/test/CodeGen/X86/known-never-zero.ll b/llvm/test/CodeGen/X86/known-never-zero.ll
index c90ab282d2255..478449bb1702d 100644
--- a/llvm/test/CodeGen/X86/known-never-zero.ll
+++ b/llvm/test/CodeGen/X86/known-never-zero.ll
@@ -927,9 +927,9 @@ define i32 @smax_known_zero(i32 %x, i32 %y) {
 ; X86-LABEL: smax_known_zero:
 ; X86:       # %bb.0:
 ; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
-; X86-NEXT:    testl %eax, %eax
-; X86-NEXT:    movl $-1, %ecx
-; X86-NEXT:    cmovnsl %eax, %ecx
+; X86-NEXT:    movl %eax, %ecx
+; X86-NEXT:    sarl $31, %ecx
+; X86-NEXT:    orl %eax, %ecx
 ; X86-NEXT:    bsfl %ecx, %ecx
 ; X86-NEXT:    movl $32, %eax
 ; X86-NEXT:    cmovnel %ecx, %eax
@@ -937,9 +937,9 @@ define i32 @smax_known_zero(i32 %x, i32 %y) {
 ;
 ; X64-LABEL: smax_known_zero:
 ; X64:       # %bb.0:
-; X64-NEXT:    testl %edi, %edi
-; X64-NEXT:    movl $-1, %ecx
-; X64-NEXT:    cmovnsl %edi, %ecx
+; X64-NEXT:    movl %edi, %ecx
+; X64-NEXT:    sarl $31, %ecx
+; X64-NEXT:    orl %edi, %ecx
 ; X64-NEXT:    movl $32, %eax
 ; X64-NEXT:    rep bsfl %ecx, %eax
 ; X64-NEXT:    retq

diff  --git a/llvm/test/CodeGen/X86/probe-stack-eflags.ll b/llvm/test/CodeGen/X86/probe-stack-eflags.ll
index cc1839bba7e89..de65288a1c780 100644
--- a/llvm/test/CodeGen/X86/probe-stack-eflags.ll
+++ b/llvm/test/CodeGen/X86/probe-stack-eflags.ll
@@ -78,24 +78,26 @@ exit2: ; preds = %bb70.i, %bb13.i
 define void @CSE_eflags_live(i64 %length) "probe-stack"="__chkstk_darwin" {
 ; CHECK-LABEL: CSE_eflags_live:
 ; CHECK:       # %bb.0: # %entry
+; CHECK-NEXT:    testq %rdi, %rdi
+; CHECK-NEXT:    je .LBB1_2
+; CHECK-NEXT:  # %bb.1: # %if.end6
 ; CHECK-NEXT:    pushq %rbp
 ; CHECK-NEXT:    .cfi_def_cfa_offset 16
 ; CHECK-NEXT:    .cfi_offset %rbp, -16
 ; CHECK-NEXT:    movq %rsp, %rbp
 ; CHECK-NEXT:    .cfi_def_cfa_register %rbp
-; CHECK-NEXT:    testq %rdi, %rdi
-; CHECK-NEXT:    je .LBB1_2
-; CHECK-NEXT:  # %bb.1: # %if.end6
-; CHECK-NEXT:    movq $-1, %rax
-; CHECK-NEXT:    cmovnsq %rdi, %rax
+; CHECK-NEXT:    movq %rdi, %rax
+; CHECK-NEXT:    sarq $63, %rax
+; CHECK-NEXT:    orq %rdi, %rax
 ; CHECK-NEXT:    addq $15, %rax
 ; CHECK-NEXT:    andq $-16, %rax
 ; CHECK-NEXT:    callq __chkstk_darwin
 ; CHECK-NEXT:    subq %rax, %rsp
-; CHECK-NEXT:  .LBB1_2: # %cleanup78
 ; CHECK-NEXT:    movq %rbp, %rsp
 ; CHECK-NEXT:    popq %rbp
 ; CHECK-NEXT:    .cfi_def_cfa %rsp, 8
+; CHECK-NEXT:    .cfi_restore %rbp
+; CHECK-NEXT:  .LBB1_2: # %cleanup78
 ; CHECK-NEXT:    retq
 entry:
   %cmp4 = icmp eq i64 %length, 0


        


More information about the llvm-commits mailing list