[llvm] [DAG] ComputeNumSignBits - use computeKnownBits instead of isConstOrConstSplat to detect legalised constants (PR #152991)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 22 03:40:10 PDT 2025


https://github.com/RKSimon updated https://github.com/llvm/llvm-project/pull/152991

>From dc7d9cdf3bd92f811d0f9b9ee158e5f8c2636fd6 Mon Sep 17 00:00:00 2001
From: Simon Pilgrim <llvm-dev at redking.me.uk>
Date: Mon, 11 Aug 2025 11:40:23 +0100
Subject: [PATCH] [DAG] ComputeNumSignBits - use computeKnownBits instead of
 isConstOrConstSplat to detect legalised constants

Helps us detect constants that have been lowered to materialisable nodes
---
 .../lib/CodeGen/SelectionDAG/SelectionDAG.cpp | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 5ef1746333040..19cadfa6de95f 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -4946,17 +4946,18 @@ unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
     // If we have a clamp pattern, we know that the number of sign bits will be
     // the minimum of the clamp min/max range.
     bool IsMax = (Opcode == ISD::SMAX);
-    ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
-    if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
+    KnownBits KnownLow, KnownHigh;
+    KnownLow = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
+    if (KnownLow.isConstant())
       if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
-        CstHigh =
-            isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
-    if (CstLow && CstHigh) {
+        KnownHigh = computeKnownBits(Op.getOperand(0).getOperand(1),
+                                     DemandedElts, Depth + 2);
+    if (KnownLow.isConstant() && KnownHigh.isConstant()) {
       if (!IsMax)
-        std::swap(CstLow, CstHigh);
-      if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
-        Tmp = CstLow->getAPIntValue().getNumSignBits();
-        Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
+        std::swap(KnownLow, KnownHigh);
+      if (KnownLow.getConstant().sle(KnownHigh.getConstant())) {
+        Tmp = KnownLow.getConstant().getNumSignBits();
+        Tmp2 = KnownHigh.getConstant().getNumSignBits();
         return std::min(Tmp, Tmp2);
       }
     }



More information about the llvm-commits mailing list