diff options
author | Simon Pilgrim <RKSimon@users.noreply.github.com> | 2024-03-01 19:24:27 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-01 19:24:27 +0000 |
commit | fcdf818a7779871990214cc1035bb2c36426f459 (patch) | |
tree | 2d79ccee4a2ac2ffb28564a500e580fa9a791b7b | |
parent | 1e8d3c357e1fd79bf3a641767d95147a8c0f54bd (diff) | |
download | llvm-fcdf818a7779871990214cc1035bb2c36426f459.zip llvm-fcdf818a7779871990214cc1035bb2c36426f459.tar.gz llvm-fcdf818a7779871990214cc1035bb2c36426f459.tar.bz2 |
[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.
-rw-r--r-- | llvm/include/llvm/Support/KnownBits.h | 3 | ||||
-rw-r--r-- | llvm/lib/Support/KnownBits.cpp | 16 | ||||
-rw-r--r-- | llvm/unittests/Support/KnownBitsTest.cpp | 35 |
3 files changed, 53 insertions, 1 deletions
diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h index fb034e0..69c569b 100644 --- a/llvm/include/llvm/Support/KnownBits.h +++ b/llvm/include/llvm/Support/KnownBits.h @@ -385,6 +385,9 @@ public: /// 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 770e405..c44a08c 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::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 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 c0377d4..9cc5741 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 absdiff - both are currently needed to cover + // extra cases. + KnownBits LHS, RHS, Res; + + // absdiff(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::absdiff(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::absdiff(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::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); |