[llvm] KnownBits: generalize high-bits of mul to overflows (PR #114211)

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 31 03:22:51 PDT 2024


================
@@ -796,19 +796,76 @@ KnownBits KnownBits::mul(const KnownBits &LHS, const KnownBits &RHS,
   assert((!NoUndefSelfMultiply || LHS == RHS) &&
          "Self multiplication knownbits mismatch");
 
-  // Compute the high known-0 bits by multiplying the unsigned max of each side.
-  // Conservatively, M active bits * N active bits results in M + N bits in the
-  // result. But if we know a value is a power-of-2 for example, then this
-  // computes one more leading zero.
-  // TODO: This could be generalized to number of sign bits (negative numbers).
-  APInt UMaxLHS = LHS.getMaxValue();
-  APInt UMaxRHS = RHS.getMaxValue();
-
-  // For leading zeros in the result to be valid, the unsigned max product must
-  // fit in the bitwidth (it must not overflow).
+  // Compute the high known-0 or known-1 bits by multiplying the min and max of
+  // each side.
+  APInt MaxLHS = LHS.isNegative() ? LHS.getMinValue().abs() : LHS.getMaxValue(),
----------------
jayfoad wrote:

Can you explain intuitively what this is supposed to calculate? If LHS is known negative it returns the largest magnitude it could have when interpreted as a signed value, if known positive it returns the largest unsigned value it could have, and if the sign bit is not known ... ?

More generally I feel that any use of `isNegative` is a bit suspect, because it suggests that values known to be negative are treated differently from values with unknown sign bit which might happen to be negative anyway.

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


More information about the llvm-commits mailing list