Skip to content

Commit

Permalink
[Support] Add KnownBits::abds signed absolute difference and rename a…
Browse files Browse the repository at this point in the history
…bsdiff -> abdu (#84897)

When I created KnownBits::absdiff, I totally missed that we already have ISD::ABDS/ABDU nodes, and we use this term in other places/targets as well.

I've added the KnownBits::abds implementation and renamed KnownBits::absdiff to KnownBits::abdu.

Followup to #84791
  • Loading branch information
RKSimon authored Mar 12, 2024
1 parent 4e3310a commit ffe4181
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 11 deletions.
7 changes: 5 additions & 2 deletions llvm/include/llvm/Support/KnownBits.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,11 @@ 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 abdu(LHS, RHS).
static KnownBits abdu(const KnownBits &LHS, const KnownBits &RHS);

/// Compute known bits for abds(LHS, RHS).
static KnownBits abds(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.
Expand Down
23 changes: 21 additions & 2 deletions llvm/lib/Support/KnownBits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ 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 KnownBits::abdu(const KnownBits &LHS, const KnownBits &RHS) {
// abdu(LHS,RHS) = sub(umax(LHS,RHS), umin(LHS,RHS)).
KnownBits UMaxValue = umax(LHS, RHS);
KnownBits UMinValue = umin(LHS, RHS);
KnownBits MinMaxDiff = computeForAddSub(/*Add=*/false, /*NSW=*/false,
Expand All @@ -250,6 +250,25 @@ KnownBits KnownBits::absdiff(const KnownBits &LHS, const KnownBits &RHS) {
return KnownAbsDiff;
}

KnownBits KnownBits::abds(const KnownBits &LHS, const KnownBits &RHS) {
// abds(LHS,RHS) = sub(smax(LHS,RHS), smin(LHS,RHS)).
KnownBits SMaxValue = smax(LHS, RHS);
KnownBits SMinValue = smin(LHS, RHS);
KnownBits MinMaxDiff = computeForAddSub(/*Add=*/false, /*NSW=*/false,
/*NUW=*/false, SMaxValue, SMinValue);

// find the common bits between sub(LHS,RHS) and sub(RHS,LHS).
KnownBits Diff0 =
computeForAddSub(/*Add=*/false, /*NSW=*/false, /*NUW=*/false, LHS, RHS);
KnownBits Diff1 =
computeForAddSub(/*Add=*/false, /*NSW=*/false, /*NUW=*/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);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/X86/X86ISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36753,7 +36753,7 @@ static void computeKnownBitsForPSADBW(SDValue LHS, SDValue RHS,
APInt DemandedSrcElts = APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
Known = DAG.computeKnownBits(RHS, DemandedSrcElts, Depth + 1);
Known2 = DAG.computeKnownBits(LHS, DemandedSrcElts, Depth + 1);
Known = KnownBits::absdiff(Known, Known2).zext(16);
Known = KnownBits::abdu(Known, Known2).zext(16);
// Known = (((D0 + D1) + (D2 + D3)) + ((D4 + D5) + (D6 + D7)))
Known = KnownBits::computeForAddSub(/*Add=*/true, /*NSW=*/true, /*NUW=*/true,
Known, Known);
Expand Down
44 changes: 38 additions & 6 deletions llvm/unittests/Support/KnownBitsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,18 +294,18 @@ TEST(KnownBitsTest, SignBitUnknown) {
EXPECT_TRUE(Known.isSignUnknown());
}

TEST(KnownBitsTest, AbsDiffSpecialCase) {
// There are 2 implementation of absdiff - both are currently needed to cover
TEST(KnownBitsTest, ABDUSpecialCase) {
// There are 2 implementations of abdu - both are currently needed to cover
// extra cases.
KnownBits LHS, RHS, Res;

// absdiff(LHS,RHS) = sub(umax(LHS,RHS), umin(LHS,RHS)).
// abdu(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);
Res = KnownBits::abdu(LHS, RHS);
EXPECT_EQ(0b0000ul, Res.One.getZExtValue());
EXPECT_EQ(0b1110ul, Res.Zero.getZExtValue());

Expand All @@ -315,11 +315,37 @@ TEST(KnownBitsTest, AbsDiffSpecialCase) {
RHS.One = APInt(4, 0b1000);
LHS.Zero = APInt(4, 0b0000);
RHS.Zero = APInt(4, 0b0111);
Res = KnownBits::absdiff(LHS, RHS);
Res = KnownBits::abdu(LHS, RHS);
EXPECT_EQ(0b0001ul, Res.One.getZExtValue());
EXPECT_EQ(0b0000ul, Res.Zero.getZExtValue());
}

TEST(KnownBitsTest, ABDSSpecialCase) {
// There are 2 implementations of abds - both are currently needed to cover
// extra cases.
KnownBits LHS, RHS, Res;

// abds(LHS,RHS) = sub(smax(LHS,RHS), smin(LHS,RHS)).
// Actual: false (Inputs = 1011, 10??, Computed = ????, Exact = 00??)
LHS.One = APInt(4, 0b1011);
RHS.One = APInt(4, 0b1000);
LHS.Zero = APInt(4, 0b0100);
RHS.Zero = APInt(4, 0b0100);
Res = KnownBits::abds(LHS, RHS);
EXPECT_EQ(0, Res.One.getSExtValue());
EXPECT_EQ(-4, Res.Zero.getSExtValue());

// 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::abds(LHS, RHS);
EXPECT_EQ(1, Res.One.getSExtValue());
EXPECT_EQ(0, Res.Zero.getSExtValue());
}

TEST(KnownBitsTest, BinaryExhaustive) {
testBinaryOpExhaustive(
[](const KnownBits &Known1, const KnownBits &Known2) {
Expand Down Expand Up @@ -359,10 +385,16 @@ TEST(KnownBitsTest, BinaryExhaustive) {
[](const APInt &N1, const APInt &N2) { return APIntOps::smin(N1, N2); });
testBinaryOpExhaustive(
[](const KnownBits &Known1, const KnownBits &Known2) {
return KnownBits::absdiff(Known1, Known2);
return KnownBits::abdu(Known1, Known2);
},
[](const APInt &N1, const APInt &N2) { return APIntOps::abdu(N1, N2); },
checkCorrectnessOnlyBinary);
testBinaryOpExhaustive(
[](const KnownBits &Known1, const KnownBits &Known2) {
return KnownBits::abds(Known1, Known2);
},
[](const APInt &N1, const APInt &N2) { return APIntOps::abds(N1, N2); },
checkCorrectnessOnlyBinary);
testBinaryOpExhaustive(
[](const KnownBits &Known1, const KnownBits &Known2) {
return KnownBits::udiv(Known1, Known2);
Expand Down

0 comments on commit ffe4181

Please sign in to comment.