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

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 19 02:59:30 PST 2026


================
@@ -4712,12 +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,
-                                  Depth + 1) &&
-           isKnownNeverZero(Val, Depth);
+    return (OrZero || isKnownNeverZero(Val, Depth)) &&
+           isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
+                                  Depth + 1);
----------------
arsenm wrote:

Requiring 2 value tracking calls to do this feels heavy. Could you equivalently do computeKnownBits, and check the result for both of these properties? Or will that give a slightly weaker result (similar to how ComputeNumSignBits is stronger). Though there were already the two calls to start 

https://github.com/llvm/llvm-project/pull/181882


More information about the llvm-commits mailing list