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

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 18 03:20:29 PDT 2024


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

KnownBits::srem does not correctly set the leader zero-bits, omitting the fact that LHS may be known-negative or known-non-negative. Fix this.

Alive2 proof: https://alive2.llvm.org/ce/z/WcDkSX

-- 8< --
Based on #109006.

>From 9014d984fbd1d9974bfbffb981eb4873b04920df Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <ramkumar.ramachandra at codasip.com>
Date: Tue, 17 Sep 2024 16:38:32 +0100
Subject: [PATCH 1/2] ValueTracking/test: cover known-high-bits of rem

There is an underlying bug in KnownBits, and we should theoretically be
able to determine the high-bits of an srem as shown in the test, just
like urem. In preparation to fix this bug, add pre-commit tests testing
high-bits of srem and urem.
---
 ...wnbits-rem-lowbits.ll => knownbits-rem.ll} | 26 +++++++++++++++++++
 1 file changed, 26 insertions(+)
 rename llvm/test/Analysis/ValueTracking/{knownbits-rem-lowbits.ll => knownbits-rem.ll} (85%)

diff --git a/llvm/test/Analysis/ValueTracking/knownbits-rem-lowbits.ll b/llvm/test/Analysis/ValueTracking/knownbits-rem.ll
similarity index 85%
rename from llvm/test/Analysis/ValueTracking/knownbits-rem-lowbits.ll
rename to llvm/test/Analysis/ValueTracking/knownbits-rem.ll
index 0521c7130055fe..f6ca6dfa729b7c 100644
--- a/llvm/test/Analysis/ValueTracking/knownbits-rem-lowbits.ll
+++ b/llvm/test/Analysis/ValueTracking/knownbits-rem.ll
@@ -12,6 +12,17 @@ define i8 @urem_low_bits_know(i8 %xx, i8 %yy) {
   ret i8 %r
 }
 
+define i8 @urem_high_bits_know(i8 %xx, i8 %yy) {
+; CHECK-LABEL: @urem_high_bits_know(
+; CHECK-NEXT:    ret i8 0
+;
+  %x = and i8 %xx, 2
+  %y = and i8 %yy, -4
+  %rem = urem i8 %x, %y
+  %r = and i8 %rem, 8
+  ret i8 %r
+}
+
 define i8 @urem_low_bits_know2(i8 %xx, i8 %yy) {
 ; CHECK-LABEL: @urem_low_bits_know2(
 ; CHECK-NEXT:    ret i8 2
@@ -80,6 +91,21 @@ define i8 @srem_low_bits_know(i8 %xx, i8 %yy) {
   ret i8 %r
 }
 
+define i8 @srem_high_bits_know(i8 %xx, i8 %yy) {
+; CHECK-LABEL: @srem_high_bits_know(
+; CHECK-NEXT:    [[X:%.*]] = or i8 [[XX:%.*]], -2
+; CHECK-NEXT:    [[Y:%.*]] = and i8 [[YY:%.*]], -4
+; CHECK-NEXT:    [[REM:%.*]] = srem i8 [[X]], [[Y]]
+; CHECK-NEXT:    [[R:%.*]] = and i8 [[REM]], 8
+; CHECK-NEXT:    ret i8 [[R]]
+;
+  %x = or i8 %xx, -2
+  %y = and i8 %yy, -4
+  %rem = srem i8 %x, %y
+  %r = and i8 %rem, 8
+  ret i8 %r
+}
+
 define i8 @srem_low_bits_know2(i8 %xx, i8 %yy) {
 ; CHECK-LABEL: @srem_low_bits_know2(
 ; CHECK-NEXT:    ret i8 1

>From ef6f01d205f2d554378563154594c668355ca1d9 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <ramkumar.ramachandra at codasip.com>
Date: Tue, 17 Sep 2024 16:02:54 +0100
Subject: [PATCH 2/2] KnownBits: refine KnownBits::srem for high-bits

KnownBits::srem does not correctly set the leader zero-bits, omitting
the fact that LHS may be known-negative or known-non-negative. Fix this.

Alive2 proof: https://alive2.llvm.org/ce/z/WcDkSX
---
 llvm/lib/Support/KnownBits.cpp                    | 10 +++++++---
 llvm/test/Analysis/ValueTracking/knownbits-rem.ll |  6 +-----
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 8e31e0ced2d731..c6f4b7255d8e15 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -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.
+  uint32_t LeadR =
+      RHS.isNegative() ? RHS.countMinLeadingOnes() : RHS.countMinLeadingZeros();
+  if (LHS.isNegative())
+    Known.One.setHighBits(std::max(LHS.countMinLeadingOnes(), LeadR));
+  else if (LHS.isNonNegative())
+    Known.Zero.setHighBits(std::max(LHS.countMinLeadingZeros(), LeadR));
   return Known;
 }
 
diff --git a/llvm/test/Analysis/ValueTracking/knownbits-rem.ll b/llvm/test/Analysis/ValueTracking/knownbits-rem.ll
index f6ca6dfa729b7c..4f60c41c581323 100644
--- a/llvm/test/Analysis/ValueTracking/knownbits-rem.ll
+++ b/llvm/test/Analysis/ValueTracking/knownbits-rem.ll
@@ -93,11 +93,7 @@ define i8 @srem_low_bits_know(i8 %xx, i8 %yy) {
 
 define i8 @srem_high_bits_know(i8 %xx, i8 %yy) {
 ; CHECK-LABEL: @srem_high_bits_know(
-; CHECK-NEXT:    [[X:%.*]] = or i8 [[XX:%.*]], -2
-; CHECK-NEXT:    [[Y:%.*]] = and i8 [[YY:%.*]], -4
-; CHECK-NEXT:    [[REM:%.*]] = srem i8 [[X]], [[Y]]
-; CHECK-NEXT:    [[R:%.*]] = and i8 [[REM]], 8
-; CHECK-NEXT:    ret i8 [[R]]
+; CHECK-NEXT:    ret i8 8
 ;
   %x = or i8 %xx, -2
   %y = and i8 %yy, -4



More information about the llvm-commits mailing list