Skip to content

[KnownBitsTest] Common up isCorrect and isOptimal. NFC. #89585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 25 additions & 40 deletions llvm/unittests/Support/KnownBitsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,20 @@ using BinaryBitsFn =
using BinaryIntFn =
llvm::function_ref<std::optional<APInt>(const APInt &, const APInt &)>;

static testing::AssertionResult isCorrect(const KnownBits &Exact,
const KnownBits &Computed,
ArrayRef<KnownBits> Inputs) {
if (Computed.Zero.isSubsetOf(Exact.Zero) &&
Computed.One.isSubsetOf(Exact.One))
return testing::AssertionSuccess();

testing::AssertionResult Result = testing::AssertionFailure();
Result << "Inputs = ";
for (const KnownBits &Input : Inputs)
Result << Input << ", ";
Result << "Computed = " << Computed << ", Exact = " << Exact;
return Result;
}

static testing::AssertionResult isOptimal(const KnownBits &Exact,
const KnownBits &Computed,
ArrayRef<KnownBits> Inputs) {
if (Computed == Exact)
return testing::AssertionSuccess();
static testing::AssertionResult checkResult(const KnownBits &Exact,
const KnownBits &Computed,
ArrayRef<KnownBits> Inputs,
bool CheckOptimality) {
if (CheckOptimality) {
// We generally don't want to return conflicting known bits, even if it is
// legal for always poison results.
if (Exact.hasConflict() || Computed == Exact)
return testing::AssertionSuccess();
} else {
if (Computed.Zero.isSubsetOf(Exact.Zero) &&
Computed.One.isSubsetOf(Exact.One))
return testing::AssertionSuccess();
}

testing::AssertionResult Result = testing::AssertionFailure();
Result << "Inputs = ";
Expand All @@ -71,12 +65,7 @@ static void testUnaryOpExhaustive(UnaryBitsFn BitsFn, UnaryIntFn IntFn,
});

EXPECT_TRUE(!Computed.hasConflict());
EXPECT_TRUE(isCorrect(Exact, Computed, Known));
// We generally don't want to return conflicting known bits, even if it is
// legal for always poison results.
if (CheckOptimality && !Exact.hasConflict()) {
EXPECT_TRUE(isOptimal(Exact, Computed, Known));
}
EXPECT_TRUE(checkResult(Exact, Computed, Known, CheckOptimality));
});
}
}
Expand All @@ -102,12 +91,8 @@ static void testBinaryOpExhaustive(BinaryBitsFn BitsFn, BinaryIntFn IntFn,
});

EXPECT_TRUE(!Computed.hasConflict());
EXPECT_TRUE(isCorrect(Exact, Computed, {Known1, Known2}));
// We generally don't want to return conflicting known bits, even if it
// is legal for always poison results.
if (CheckOptimality && !Exact.hasConflict()) {
EXPECT_TRUE(isOptimal(Exact, Computed, {Known1, Known2}));
}
EXPECT_TRUE(
checkResult(Exact, Computed, {Known1, Known2}, CheckOptimality));
// In some cases we choose to return zero if the result is always
// poison.
if (RefinePoisonToZero && Exact.hasConflict()) {
Expand Down Expand Up @@ -201,23 +186,23 @@ static void TestAddSubExhaustive(bool IsAdd) {

KnownBits KnownComputed = KnownBits::computeForAddSub(
IsAdd, /*NSW=*/false, /*NUW=*/false, Known1, Known2);
EXPECT_TRUE(isOptimal(Known, KnownComputed, {Known1, Known2}));
EXPECT_TRUE(checkResult(Known, KnownComputed, {Known1, Known2},
/*CheckOptimality=*/true));

KnownBits KnownNSWComputed = KnownBits::computeForAddSub(
IsAdd, /*NSW=*/true, /*NUW=*/false, Known1, Known2);
if (!KnownNSW.hasConflict())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if is now unnecessary since I moved the handling of always-poison results into checkResult. Same below.

EXPECT_TRUE(isOptimal(KnownNSW, KnownNSWComputed, {Known1, Known2}));
EXPECT_TRUE(checkResult(KnownNSW, KnownNSWComputed, {Known1, Known2},
/*CheckOptimality=*/true));

KnownBits KnownNUWComputed = KnownBits::computeForAddSub(
IsAdd, /*NSW=*/false, /*NUW=*/true, Known1, Known2);
if (!KnownNUW.hasConflict())
EXPECT_TRUE(isOptimal(KnownNUW, KnownNUWComputed, {Known1, Known2}));
EXPECT_TRUE(checkResult(KnownNUW, KnownNUWComputed, {Known1, Known2},
/*CheckOptimality=*/true));

KnownBits KnownNSWAndNUWComputed = KnownBits::computeForAddSub(
IsAdd, /*NSW=*/true, /*NUW=*/true, Known1, Known2);
if (!KnownNSWAndNUW.hasConflict())
EXPECT_TRUE(isOptimal(KnownNSWAndNUW, KnownNSWAndNUWComputed,
{Known1, Known2}));
EXPECT_TRUE(checkResult(KnownNSWAndNUW, KnownNSWAndNUWComputed,
{Known1, Known2}, /*CheckOptimality=*/true));
});
});
}
Expand Down