Skip to content

Commit 10654e4

Browse files
authored
[KnownBitsTest] Common up isCorrect and isOptimal. NFC. (#89585)
This de-duplicates the code that prints useful information when a test fails.
1 parent a2692ac commit 10654e4

File tree

1 file changed

+25
-40
lines changed

1 file changed

+25
-40
lines changed

llvm/unittests/Support/KnownBitsTest.cpp

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,20 @@ using BinaryBitsFn =
2525
using BinaryIntFn =
2626
llvm::function_ref<std::optional<APInt>(const APInt &, const APInt &)>;
2727

28-
static testing::AssertionResult isCorrect(const KnownBits &Exact,
29-
const KnownBits &Computed,
30-
ArrayRef<KnownBits> Inputs) {
31-
if (Computed.Zero.isSubsetOf(Exact.Zero) &&
32-
Computed.One.isSubsetOf(Exact.One))
33-
return testing::AssertionSuccess();
34-
35-
testing::AssertionResult Result = testing::AssertionFailure();
36-
Result << "Inputs = ";
37-
for (const KnownBits &Input : Inputs)
38-
Result << Input << ", ";
39-
Result << "Computed = " << Computed << ", Exact = " << Exact;
40-
return Result;
41-
}
42-
43-
static testing::AssertionResult isOptimal(const KnownBits &Exact,
44-
const KnownBits &Computed,
45-
ArrayRef<KnownBits> Inputs) {
46-
if (Computed == Exact)
47-
return testing::AssertionSuccess();
28+
static testing::AssertionResult checkResult(const KnownBits &Exact,
29+
const KnownBits &Computed,
30+
ArrayRef<KnownBits> Inputs,
31+
bool CheckOptimality) {
32+
if (CheckOptimality) {
33+
// We generally don't want to return conflicting known bits, even if it is
34+
// legal for always poison results.
35+
if (Exact.hasConflict() || Computed == Exact)
36+
return testing::AssertionSuccess();
37+
} else {
38+
if (Computed.Zero.isSubsetOf(Exact.Zero) &&
39+
Computed.One.isSubsetOf(Exact.One))
40+
return testing::AssertionSuccess();
41+
}
4842

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

7367
EXPECT_TRUE(!Computed.hasConflict());
74-
EXPECT_TRUE(isCorrect(Exact, Computed, Known));
75-
// We generally don't want to return conflicting known bits, even if it is
76-
// legal for always poison results.
77-
if (CheckOptimality && !Exact.hasConflict()) {
78-
EXPECT_TRUE(isOptimal(Exact, Computed, Known));
79-
}
68+
EXPECT_TRUE(checkResult(Exact, Computed, Known, CheckOptimality));
8069
});
8170
}
8271
}
@@ -102,12 +91,8 @@ static void testBinaryOpExhaustive(BinaryBitsFn BitsFn, BinaryIntFn IntFn,
10291
});
10392

10493
EXPECT_TRUE(!Computed.hasConflict());
105-
EXPECT_TRUE(isCorrect(Exact, Computed, {Known1, Known2}));
106-
// We generally don't want to return conflicting known bits, even if it
107-
// is legal for always poison results.
108-
if (CheckOptimality && !Exact.hasConflict()) {
109-
EXPECT_TRUE(isOptimal(Exact, Computed, {Known1, Known2}));
110-
}
94+
EXPECT_TRUE(
95+
checkResult(Exact, Computed, {Known1, Known2}, CheckOptimality));
11196
// In some cases we choose to return zero if the result is always
11297
// poison.
11398
if (RefinePoisonToZero && Exact.hasConflict()) {
@@ -201,23 +186,23 @@ static void TestAddSubExhaustive(bool IsAdd) {
201186

202187
KnownBits KnownComputed = KnownBits::computeForAddSub(
203188
IsAdd, /*NSW=*/false, /*NUW=*/false, Known1, Known2);
204-
EXPECT_TRUE(isOptimal(Known, KnownComputed, {Known1, Known2}));
189+
EXPECT_TRUE(checkResult(Known, KnownComputed, {Known1, Known2},
190+
/*CheckOptimality=*/true));
205191

206192
KnownBits KnownNSWComputed = KnownBits::computeForAddSub(
207193
IsAdd, /*NSW=*/true, /*NUW=*/false, Known1, Known2);
208-
if (!KnownNSW.hasConflict())
209-
EXPECT_TRUE(isOptimal(KnownNSW, KnownNSWComputed, {Known1, Known2}));
194+
EXPECT_TRUE(checkResult(KnownNSW, KnownNSWComputed, {Known1, Known2},
195+
/*CheckOptimality=*/true));
210196

211197
KnownBits KnownNUWComputed = KnownBits::computeForAddSub(
212198
IsAdd, /*NSW=*/false, /*NUW=*/true, Known1, Known2);
213-
if (!KnownNUW.hasConflict())
214-
EXPECT_TRUE(isOptimal(KnownNUW, KnownNUWComputed, {Known1, Known2}));
199+
EXPECT_TRUE(checkResult(KnownNUW, KnownNUWComputed, {Known1, Known2},
200+
/*CheckOptimality=*/true));
215201

216202
KnownBits KnownNSWAndNUWComputed = KnownBits::computeForAddSub(
217203
IsAdd, /*NSW=*/true, /*NUW=*/true, Known1, Known2);
218-
if (!KnownNSWAndNUW.hasConflict())
219-
EXPECT_TRUE(isOptimal(KnownNSWAndNUW, KnownNSWAndNUWComputed,
220-
{Known1, Known2}));
204+
EXPECT_TRUE(checkResult(KnownNSWAndNUW, KnownNSWAndNUWComputed,
205+
{Known1, Known2}, /*CheckOptimality=*/true));
221206
});
222207
});
223208
}

0 commit comments

Comments
 (0)