[llvm] [SelectionDAG]: Add more cases for UDIV, SDIV, SRA, and SRL (PR #89522)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 20 19:28:57 PDT 2024


================
@@ -5443,19 +5443,50 @@ bool SelectionDAG::isKnownNeverZero(SDValue Op, unsigned Depth) const {
     if (ValKnown.isNegative())
       return true;
     // If max shift cnt of known ones is non-zero, result is non-zero.
-    APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
+    const KnownBits Shift = computeKnownBits(Op.getOperand(1), Depth + 1);
+    APInt MaxCnt = Shift.getMaxValue();
     if (MaxCnt.ult(ValKnown.getBitWidth()) &&
         !ValKnown.One.lshr(MaxCnt).isZero())
       return true;
+    // Similar to udiv but we try to see if we can turn it into a division
+    const KnownBits One =
+        KnownBits::makeConstant(APInt(ValKnown.getBitWidth(), 1));
+
+    if (KnownBits::uge(ValKnown, KnownBits::lshr(One, Shift)))
----------------
topperc wrote:

KnownBits::uge returns a std::optional. This is only checking that the optional has a value, not what the value is.

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


More information about the llvm-commits mailing list