[llvm] [RISCV] Fix infinite DAGCombine loop with SETCC and SIGN_EXTEND_INREG (PR #221593)

via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 6 10:45:53 PDT 2026


https://github.com/AZero13 created https://github.com/llvm/llvm-project/pull/221593

We generalized (X & -(1 << C1) & 0xffffffff) == C2 << C1 using `sraiw`. The combine generates a `SIGN_EXTEND_INREG` when simplifying.

However, when C1 is 0 and the sign bit (bit 31) of X is already known to be zero, `DAGCombiner` sees that `SIGN_EXTEND_INREG` is semantically equivalent to `AND X, 0xFFFFFFFF` (zext). Because `DAGCombiner` considers `AND` to be more canonical than `SIGN_EXTEND_INREG` in this context, it immediately reverts the node back to `AND`.

This caused an infinite DAGCombine loop. This patch prevents folding back in this state.

Fixes: https://github.com/llvm/llvm-project/issues/221521

>From a9fc9c6431c4e0e41571ecbea8f64c31ab6305cf Mon Sep 17 00:00:00 2001
From: AZero13 <gfunni234 at gmail.com>
Date: Sun, 6 Sep 2026 13:40:32 -0400
Subject: [PATCH] [RISCV] Fix infinite DAGCombine loop with SETCC and
 SIGN_EXTEND_INREG

We generalized (X & -(1 << C1) & 0xffffffff) == C2 << C1
using `sraiw`. The combine generates a `SIGN_EXTEND_INREG` when simplifying.

However, when C1 is 0 and the sign bit (bit 31) of X is already known to be zero, `DAGCombiner` sees that `SIGN_EXTEND_INREG` is semantically equivalent to `AND X, 0xFFFFFFFF` (zext). Because `DAGCombiner` considers `AND` to be more canonical than `SIGN_EXTEND_INREG` in this context, it immediately reverts the node back to `AND`.

This caused an infinite DAGCombine loop. This patch prevents folding back in this state.

Fixes: https://github.com/llvm/llvm-project/issues/221521
---
 llvm/lib/Target/RISCV/RISCVISelLowering.cpp    |  8 ++++++++
 .../RISCV/dagcombine-setcc-sext-and-loop.ll    | 18 ++++++++++++++++++
 2 files changed, 26 insertions(+)
 create mode 100644 llvm/test/CodeGen/RISCV/dagcombine-setcc-sext-and-loop.ll

diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index b2f684952dd90..fa601a8b88d6c 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -20046,6 +20046,14 @@ static SDValue performSETCCCombine(SDNode *N,
       unsigned ShiftBits = llvm::countr_zero(AndRHSInt);
       int64_t NewC = SignExtend64<32>(N1Int) >> ShiftBits;
       if (NewC >= -2048 && NewC <= 2048) {
+        // If ShiftBits is 0 and the sign bit of X is known zero, DAGCombiner
+        // will fold SIGN_EXTEND_INREG back to AND, causing an infinite loop.
+        if (ShiftBits == 0 &&
+            DAG.MaskedValueIsZero(
+                N0.getOperand(0),
+                APInt::getOneBitSet(OpVT.getScalarSizeInBits(), 31)))
+          return SDValue();
+
         SDValue SExt =
             DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, OpVT, N0.getOperand(0),
                         DAG.getValueType(MVT::i32));
diff --git a/llvm/test/CodeGen/RISCV/dagcombine-setcc-sext-and-loop.ll b/llvm/test/CodeGen/RISCV/dagcombine-setcc-sext-and-loop.ll
new file mode 100644
index 0000000000000..6bb7e9d5fec03
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/dagcombine-setcc-sext-and-loop.ll
@@ -0,0 +1,18 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc -mtriple=riscv64 < %s | FileCheck %s
+
+; This test ensures that the DAGCombiner doesn't get stuck in an infinite loop
+; between RISCVISelLowering converting (X & 0xFFFFFFFF) == C to sext_inreg
+; and DAGCombiner converting sext_inreg back to AND.
+
+define i64 @reproduce(i1 %0) {
+; CHECK-LABEL: reproduce:
+; CHECK:       # %bb.0:
+; CHECK:         ret
+  %2 = select i1 %0, i64 44587060572238, i64 0
+  %3 = and i64 %2, 1005072462
+  %4 = icmp eq i64 %3, 0
+  %5 = zext i1 %4 to i64
+  %6 = or i64 %2, %5
+  ret i64 %6
+}



More information about the llvm-commits mailing list