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

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 23 09:00:03 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);
----------------
jayfoad wrote:

If RHS is all ones then the result is 0. So I think this code (which I wrote) can be tightened up and simplified to:
```suggestion
    Known.Zero.setHighBits(std::max(LHS.countMinLeadingZeros(), RHS.countMinSignBits()));
```
(The reason for the `+1` and `-1` was that if $RHS$ is negative and has $n$ leading ones, $|RHS|$ is only known to have $n-1$ leading zeros. But given that the result must be _strictly_ less than $|RHS|$, I don't think that is a problem after all.)

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


More information about the llvm-commits mailing list