[llvm] [DAG] Improved ISD::SHL handling in isKnownToBeAPowerOfTwo (PR #181882)

Aadarsh Keshri via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 18 02:54:02 PST 2026


https://github.com/Aadarsh-Keshri updated https://github.com/llvm/llvm-project/pull/181882

>From 78e03d2505d2fda39360fae61a572520001e4e88 Mon Sep 17 00:00:00 2001
From: Aadarsh-Keshri <keshri.aadarsh123 at gmail.com>
Date: Tue, 17 Feb 2026 23:44:57 +0530
Subject: [PATCH 1/2] [DAG] Improved ISD::SHL handling in
 isKnownToBeAPowerOfTwo

---
 llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 581553d41cb6d..707e24120514c 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -4712,10 +4712,12 @@ bool SelectionDAG::isKnownToBeAPowerOfTwo(SDValue Val,
   case ISD::SHL: {
     // A left-shift of a constant one will have exactly one bit set because
     // shifting the bit off the end is undefined.
-    auto *C = isConstOrConstSplat(Val.getOperand(0));
+    auto *C = isConstOrConstSplat(Val.getOperand(0), DemandedElts);
     if (C && C->getAPIntValue() == 1)
       return true;
-    return isKnownToBeAPowerOfTwo(Val.getOperand(0), /*OrZero=*/false,
+    if (!(OrZero || isKnownNeverZero(Val, Depth)))
+      return false;
+    return isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
                                   Depth + 1) &&
            isKnownNeverZero(Val, Depth);
   }

>From 5f013afc873bef9f152e46a2917a95fd41444b8d Mon Sep 17 00:00:00 2001
From: Aadarsh Keshri <120744385+Aadarsh-Keshri at users.noreply.github.com>
Date: Wed, 18 Feb 2026 16:23:54 +0530
Subject: [PATCH 2/2] Apply suggestion from @RKSimon

Co-authored-by: Simon Pilgrim <git at redking.me.uk>
---
 llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 707e24120514c..254d51913eda6 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -4715,7 +4715,8 @@ bool SelectionDAG::isKnownToBeAPowerOfTwo(SDValue Val,
     auto *C = isConstOrConstSplat(Val.getOperand(0), DemandedElts);
     if (C && C->getAPIntValue() == 1)
       return true;
-    if (!(OrZero || isKnownNeverZero(Val, Depth)))
+    return (OrZero || isKnownNeverZero(Val, Depth) && 
+            isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero, Depth + 1);
       return false;
     return isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
                                   Depth + 1) &&



More information about the llvm-commits mailing list