[llvm] [KnownBits] Fix SDiv for Unknown Bit (PR #227264)

via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 29 03:34:53 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-support

Author: Anand.k.vinu (anandkvinu)

<details>
<summary>Changes</summary>

This change detects a zero value from getSignedMinValue() and sets the lowest unknown bit using countMinTrailingZeros() to get the minimum non-zero denominator.

For example, a denominator such as 0b0?00 can represent {0, 4}. Since zero is not a valid divisor, the minimum non-zero denominator should be used for the sdiv.
tested for 0b0?00, 0b0???, 0b0??0, 0b000?.

Added regression test.
SupportTests --gtest_filter=KnownBitsTest.NonZeroDenomSDiv
ninja -C build check-llvm-unit
ninja -C build check-llvm
git diff --check

Formatted using git clang-format HEAD

AI disclosure: No AI was used to generate or find the fix for this issue, including tests.

Fixes https://github.com/llvm/llvm-project/issues/226296

---
Full diff: https://github.com/llvm/llvm-project/pull/227264.diff


2 Files Affected:

- (modified) llvm/lib/Support/KnownBits.cpp (+5-1) 
- (modified) llvm/unittests/Support/KnownBitsTest.cpp (+23) 


``````````diff
diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 70f7a2f4fc887..81221081a4339 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -1255,7 +1255,11 @@ KnownBits KnownBits::sdiv(const KnownBits &LHS, const KnownBits &RHS,
     if (Exact || (-LHS.getSignedMaxValue()).uge(RHS.getSignedMaxValue())) {
       APInt Denom = RHS.getSignedMinValue();
       APInt Num = LHS.getSignedMinValue();
-      Res = Denom.isZero() ? Num : Num.sdiv(Denom);
+      // fix to set the lowest unknown bit to get the minimum non-zero
+      // denominator
+      if (Denom.isZero())
+        Denom.setBit(RHS.countMinTrailingZeros());
+      Res = Num.sdiv(Denom);
     }
   } else if (LHS.isStrictlyPositive() && RHS.isNegative()) {
     // Result is negative if Exact OR LHS u>= -RHS.
diff --git a/llvm/unittests/Support/KnownBitsTest.cpp b/llvm/unittests/Support/KnownBitsTest.cpp
index d4b8dff54f3d6..5033fc415b520 100644
--- a/llvm/unittests/Support/KnownBitsTest.cpp
+++ b/llvm/unittests/Support/KnownBitsTest.cpp
@@ -693,6 +693,29 @@ TEST(KnownBitsTest, UnaryExhaustive) {
       },
       [](const APInt &N) { return N * N; }, /*CheckOptimality=*/false);
 }
+// implementing googletest for SDIV having unknown bit in denominator.
+// To set the lowest unknown bit to get the minimum non-zero denominator.
+
+TEST(KnownBitsTest, NonZeroDenomSDiv) {
+
+  KnownBits Num(4);
+  KnownBits Denom(4);
+  // setting the masks for Ones and Zeros Num = -8.
+  Num.One.setBit(3);
+  Num.Zero.setBit(2);
+  Num.Zero.setBit(1);
+  Num.Zero.setBit(0);
+  // comment to test each edge cases. Denom = 0b0?00.
+  Denom.Zero.setBit(3);
+  // Denom.Zero.setBit(2);
+  Denom.Zero.setBit(1);
+  Denom.Zero.setBit(0);
+  KnownBits Result = KnownBits::sdiv(Num, Denom);
+  KnownBits Answer(4);
+  Answer.One.setHighBits(3);
+
+  EXPECT_EQ(Result, Answer);
+}
 
 TEST(KnownBitsTest, FunnelShiftExhaustive) {
   unsigned Bits = 4;

``````````

</details>


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


More information about the llvm-commits mailing list