[llvm] [KnownBits] Use min non-zero divisor and quotient lower bound in udiv (PR #209360)

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 14 05:47:20 PDT 2026


================
@@ -1292,16 +1292,27 @@ KnownBits KnownBits::udiv(const KnownBits &LHS, const KnownBits &RHS,
     return Known;
   }
 
-  // We can figure out the minimum number of upper zero bits by doing
-  // MaxNumerator / MinDenominator. If the Numerator gets smaller or Denominator
-  // gets larger, the number of upper zero bits increases.
+  // A zero denominator is UB, so the minimum attainable denominator is the
+  // smallest non-zero value consistent with the known bits.
   APInt MinDenom = RHS.getMinValue();
-  APInt MaxNum = LHS.getMaxValue();
-  APInt MaxRes = MinDenom.isZero() ? MaxNum : MaxNum.udiv(MinDenom);
-
-  unsigned LeadZ = MaxRes.countLeadingZeros();
+  if (MinDenom.isZero())
+    MinDenom = APInt::getOneBitSet(BitWidth, RHS.countMinTrailingZeros());
----------------
jayfoad wrote:

Nit
```suggestion
    MinDenom.setBit(RHS.countMinTrailingZeros());
```

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


More information about the llvm-commits mailing list