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

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 19 02:40:24 PDT 2024


================
@@ -1075,9 +1075,13 @@ 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.countMinLeadingOnes()));
----------------
jayfoad wrote:

```suggestion
        std::max(LHS.countMinLeadingOnes(), RHS.countMinSignBits()));
```

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


More information about the llvm-commits mailing list