[llvm] KnownBits: refine high-bits of mul in signed case (PR #113051)

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 5 05:46:28 PST 2024


================
@@ -796,19 +796,25 @@ 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
+  // Compute the high known-0 or known-1 bits by multiplying the max of each
----------------
jayfoad wrote:

I think you can wrap the whole of this section in `if (!LHS.isSignUnknown() && !RHS.isSignUnknown)`. If either sign was unknown then we would not get any useful high-zeros or high-ones info from this calculation. That would resolve my confusion about your use of `isNegative` below by making it clear that there is no "unknown sign" case to worry about - both operand are known negative or known positive.

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


More information about the llvm-commits mailing list