[llvm] [KnownBits] Add KnownBits::absdiff to compute the absolute difference of 2 unsigned values (PR #82354)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 20 04:49:07 PST 2024


https://github.com/RKSimon created https://github.com/llvm/llvm-project/pull/82354

Equivalent to "umax(A, B) - umin(A, B)"

This is just an initial correctness implementation, hopefully we can make this optimal in the future.

>From 453ef7842d6e507a1ed81768fbd50bd74f79bb49 Mon Sep 17 00:00:00 2001
From: Simon Pilgrim <llvm-dev at redking.me.uk>
Date: Tue, 20 Feb 2024 12:46:48 +0000
Subject: [PATCH] [KnownBits] Add KnownBits::absdiff to compute the absolute
 difference of 2 unsigned values

Equivalent to "umax(A, B) - umin(A, B)"

This is just an initial correctness implementation, hopefully we can make this optimal in the future.
---
 llvm/include/llvm/Support/KnownBits.h    |  3 +++
 llvm/lib/Support/KnownBits.cpp           | 17 +++++++++++++++++
 llvm/unittests/Support/KnownBitsTest.cpp |  9 ++++++++-
 3 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index fb034e0b9e3baf..69c569b97ccae1 100644
--- a/llvm/include/llvm/Support/KnownBits.h
+++ b/llvm/include/llvm/Support/KnownBits.h
@@ -385,6 +385,9 @@ struct KnownBits {
   /// Compute known bits for smin(LHS, RHS).
   static KnownBits smin(const KnownBits &LHS, const KnownBits &RHS);
 
+  /// Compute known bits for absdiff(LHS, RHS).
+  static KnownBits absdiff(const KnownBits &LHS, const KnownBits &RHS);
+
   /// Compute known bits for shl(LHS, RHS).
   /// NOTE: RHS (shift amount) bitwidth doesn't need to be the same as LHS.
   static KnownBits shl(const KnownBits &LHS, const KnownBits &RHS,
diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 770e4051ca3ffa..e0ecdd56711959 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -176,6 +176,23 @@ KnownBits KnownBits::smin(const KnownBits &LHS, const KnownBits &RHS) {
   return Flip(umax(Flip(LHS), Flip(RHS)));
 }
 
+KnownBits KnownBits::absdiff(const KnownBits &LHS, const KnownBits &RHS) {
+  // absdiff(LHS,RHS) = sub(umax(LHS,RHS), umin(LHS,RHS)).
+  KnownBits UMaxValue = umax(LHS, RHS);
+  KnownBits UMinValue = umin(LHS, RHS);
+  KnownBits KnownAbsDiff = computeForAddSub(false, false, UMaxValue, UMinValue);
+
+  // fallback - find the common bits between sub(LHS,RHS) and sub(RHS,LHS).
+  if (KnownAbsDiff.isUnknown()) {
+    KnownBits Diff0 = computeForAddSub(false, false, LHS, RHS);
+    KnownBits Diff1 = computeForAddSub(false, false, RHS, LHS);
+    KnownAbsDiff = Diff0.intersectWith(Diff1);
+  }
+
+  assert(!KnownAbsDiff.hasConflict() && "Bad Output");
+  return KnownAbsDiff;
+}
+
 static unsigned getMaxShiftAmount(const APInt &MaxValue, unsigned BitWidth) {
   if (isPowerOf2_32(BitWidth))
     return MaxValue.extractBitsAsZExtValue(Log2_32(BitWidth), 0);
diff --git a/llvm/unittests/Support/KnownBitsTest.cpp b/llvm/unittests/Support/KnownBitsTest.cpp
index c0377d45c303a1..dfa3fe06a32647 100644
--- a/llvm/unittests/Support/KnownBitsTest.cpp
+++ b/llvm/unittests/Support/KnownBitsTest.cpp
@@ -281,7 +281,14 @@ TEST(KnownBitsTest, BinaryExhaustive) {
         return KnownBits::smin(Known1, Known2);
       },
       [](const APInt &N1, const APInt &N2) { return APIntOps::smin(N1, N2); });
-
+  testBinaryOpExhaustive(
+      [](const KnownBits &Known1, const KnownBits &Known2) {
+        return KnownBits::absdiff(Known1, Known2);
+      },
+      [](const APInt &N1, const APInt &N2) {
+        return APIntOps::absdiff(N1, N2);
+      },
+      checkCorrectnessOnlyBinary);
   testBinaryOpExhaustive(
       [](const KnownBits &Known1, const KnownBits &Known2) {
         return KnownBits::udiv(Known1, Known2);



More information about the llvm-commits mailing list