[llvm] fcdf818 - [KnownBits] Add KnownBits::absdiff to compute the absolute difference of 2 unsigned values (#82354)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Mar 1 11:24:31 PST 2024
Author: Simon Pilgrim
Date: 2024-03-01T19:24:27Z
New Revision: fcdf818a7779871990214cc1035bb2c36426f459
URL: https://github.com/llvm/llvm-project/commit/fcdf818a7779871990214cc1035bb2c36426f459
DIFF: https://github.com/llvm/llvm-project/commit/fcdf818a7779871990214cc1035bb2c36426f459.diff
LOG: [KnownBits] Add KnownBits::absdiff to compute the absolute difference of 2 unsigned values (#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.
Added:
Modified:
llvm/include/llvm/Support/KnownBits.h
llvm/lib/Support/KnownBits.cpp
llvm/unittests/Support/KnownBitsTest.cpp
Removed:
################################################################################
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 abs
diff (LHS, RHS).
+ static KnownBits abs
diff (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..c44a08cc1c2e6a 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -176,6 +176,22 @@ KnownBits KnownBits::smin(const KnownBits &LHS, const KnownBits &RHS) {
return Flip(umax(Flip(LHS), Flip(RHS)));
}
+KnownBits KnownBits::abs
diff (const KnownBits &LHS, const KnownBits &RHS) {
+ // abs
diff (LHS,RHS) = sub(umax(LHS,RHS), umin(LHS,RHS)).
+ KnownBits UMaxValue = umax(LHS, RHS);
+ KnownBits UMinValue = umin(LHS, RHS);
+ KnownBits MinMaxDiff = computeForAddSub(false, false, UMaxValue, UMinValue);
+
+ // find the common bits between sub(LHS,RHS) and sub(RHS,LHS).
+ KnownBits Diff0 = computeForAddSub(false, false, LHS, RHS);
+ KnownBits Diff1 = computeForAddSub(false, false, RHS, LHS);
+ KnownBits SubDiff = Diff0.intersectWith(Diff1);
+
+ KnownBits KnownAbsDiff = MinMaxDiff.unionWith(SubDiff);
+ 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..9cc5741ad73c45 100644
--- a/llvm/unittests/Support/KnownBitsTest.cpp
+++ b/llvm/unittests/Support/KnownBitsTest.cpp
@@ -244,6 +244,32 @@ TEST(KnownBitsTest, SubBorrowExhaustive) {
});
}
+TEST(KnownBitsTest, AbsDiffSpecialCase) {
+ // There are 2 implementation of abs
diff - both are currently needed to cover
+ // extra cases.
+ KnownBits LHS, RHS, Res;
+
+ // abs
diff (LHS,RHS) = sub(umax(LHS,RHS), umin(LHS,RHS)).
+ // Actual: false (Inputs = 1011, 101?, Computed = 000?, Exact = 000?)
+ LHS.One = APInt(4, 0b1011);
+ RHS.One = APInt(4, 0b1010);
+ LHS.Zero = APInt(4, 0b0100);
+ RHS.Zero = APInt(4, 0b0100);
+ Res = KnownBits::abs
diff (LHS, RHS);
+ EXPECT_EQ(0b0000, Res.One.getZExtValue());
+ EXPECT_EQ(0b1110, Res.Zero.getZExtValue());
+
+ // find the common bits between sub(LHS,RHS) and sub(RHS,LHS).
+ // Actual: false (Inputs = ???1, 1000, Computed = ???1, Exact = 0??1)
+ LHS.One = APInt(4, 0b0001);
+ RHS.One = APInt(4, 0b1000);
+ LHS.Zero = APInt(4, 0b0000);
+ RHS.Zero = APInt(4, 0b0111);
+ Res = KnownBits::abs
diff (LHS, RHS);
+ EXPECT_EQ(0b0001, Res.One.getZExtValue());
+ EXPECT_EQ(0b0000, Res.Zero.getZExtValue());
+}
+
TEST(KnownBitsTest, BinaryExhaustive) {
testBinaryOpExhaustive(
[](const KnownBits &Known1, const KnownBits &Known2) {
@@ -281,7 +307,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::abs
diff (Known1, Known2);
+ },
+ [](const APInt &N1, const APInt &N2) {
+ return APIntOps::abs
diff (N1, N2);
+ },
+ checkCorrectnessOnlyBinary);
testBinaryOpExhaustive(
[](const KnownBits &Known1, const KnownBits &Known2) {
return KnownBits::udiv(Known1, Known2);
More information about the llvm-commits
mailing list