[llvm] KnownBits: refine srem for high-bits (PR #109121)

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 23 09:01:04 PDT 2024


================
@@ -1075,9 +1075,15 @@ KnownBits KnownBits::srem(const KnownBits &LHS, const KnownBits &RHS) {
 
   // The sign bit is the LHS's sign bit, except when the result of the
   // remainder is zero. The magnitude of the result should be less than or
-  // equal to the magnitude of the LHS. Therefore any leading zeros that exist
-  // in the left hand side must also exist in the result.
-  Known.Zero.setHighBits(LHS.countMinLeadingZeros());
+  // equal to the magnitude of either operand.
+  if (LHS.isNegative() && Known.isNonZero()) {
+    Known.One.setHighBits(
+        std::max(LHS.countMinLeadingOnes(), RHS.countMinSignBits()));
+  } else if (LHS.isNonNegative()) {
+    unsigned Lead =
+        std::max(LHS.countMinLeadingZeros(), RHS.countMinLeadingZeros());
+    Known.Zero.setHighBits(std::max(Lead + 1, RHS.countMinLeadingOnes()) - 1);
----------------
artagnon wrote:

The logic was suggested to me by @jayfoad, and I think this is the logic corresponding to `countMinSignBits`, but to set leading zeros. If the RHS is all ones in this case (LHS negative), there are two cases: LHS = RHS, in which case, this is 1, and RHS > LHS, in which case this is 0. Did I get something wrong?

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


More information about the llvm-commits mailing list