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

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 5 05:30:50 PST 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(),
----------------
artagnon wrote:

Makes sense. However, do you agree that there is no way to get rid of isNegative()?

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


More information about the llvm-commits mailing list