[llvm] [ConstantRange] Bail out early in unsignedMulMayOverflow for wide integer types (PR #192275)

Max Graey via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 16 05:02:13 PDT 2026


================
@@ -2025,6 +2025,28 @@ APInt APInt::umul_ov(const APInt &RHS, bool &Overflow) const {
   return Res;
 }
 
+bool APInt::umul_overflows(const APInt &RHS) const {
+  // 0 * x = 0, 1 * x = x — neither overflows an n-bit unsigned integer.
+  if (isZero() || RHS.isZero() || isOne() || RHS.isOne())
+    return false;
+  // Fast path: overflow is certain based on leading-zero count (O(n)).
+  // Avoids the O(n^2) product computation in the overflow-certain branch of
+  // umul_ov — critical for very wide integer types (e.g. i5754496).
+  if (countl_zero() + RHS.countl_zero() + 2 <= BitWidth)
+    return true;
----------------
MaxGraey wrote:

Before slow path with actually exists in pretty narrow bounds you can add lower bound when overflow never happening:
```cpp
unsigned LZ = countl_zero() + RHS.countl_zero();

// no-overflow case: bitlen(x) + bitlen(y) <= BitWidth.
if (LZ >= BitWidth)
  return false;

// guaranteed-overflow case: bitlen(x) + bitlen(y) - 1 > BitWidth.
if (LZ < BitWidth - 1)
  return true;

// fallback to  slow path
```

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


More information about the llvm-commits mailing list