Skip to content

Commit 4f11119

Browse files
authored
[KnownBits] re-introduce RefinePosionToZero in unittest (#94848)
Context: https://github.com/llvm/llvm-project/pull/94568/files#r1631349423 - Reintroduce the RefinePositionToZero path in the unittest. - Adjust some inline function argument hints to match the same style.
1 parent 346bd91 commit 4f11119

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

llvm/unittests/Support/KnownBitsTest.cpp

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ static void testUnaryOpExhaustive(StringRef Name, UnaryBitsFn BitsFn,
7777

7878
static void testBinaryOpExhaustive(StringRef Name, BinaryBitsFn BitsFn,
7979
BinaryIntFn IntFn,
80-
bool CheckOptimality = true) {
80+
bool CheckOptimality = true,
81+
bool RefinePoisonToZero = false) {
8182
for (unsigned Bits : {1, 4}) {
8283
ForeachKnownBits(Bits, [&](const KnownBits &Known1) {
8384
ForeachKnownBits(Bits, [&](const KnownBits &Known2) {
@@ -99,6 +100,12 @@ static void testBinaryOpExhaustive(StringRef Name, BinaryBitsFn BitsFn,
99100
EXPECT_TRUE(checkResult(Name, Exact, Computed, {Known1, Known2},
100101
CheckOptimality));
101102
}
103+
// In some cases we choose to return zero if the result is always
104+
// poison.
105+
if (RefinePoisonToZero && Exact.hasConflict() &&
106+
!Known1.hasConflict() && !Known2.hasConflict()) {
107+
EXPECT_TRUE(Computed.isZero());
108+
}
102109
});
103110
});
104111
}
@@ -313,7 +320,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
313320
testBinaryOpExhaustive(
314321
"udiv exact",
315322
[](const KnownBits &Known1, const KnownBits &Known2) {
316-
return KnownBits::udiv(Known1, Known2, /*Exact*/ true);
323+
return KnownBits::udiv(Known1, Known2, /*Exact=*/true);
317324
},
318325
[](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
319326
if (N2.isZero() || !N1.urem(N2).isZero())
@@ -335,7 +342,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
335342
testBinaryOpExhaustive(
336343
"sdiv exact",
337344
[](const KnownBits &Known1, const KnownBits &Known2) {
338-
return KnownBits::sdiv(Known1, Known2, /*Exact*/ true);
345+
return KnownBits::sdiv(Known1, Known2, /*Exact=*/true);
339346
},
340347
[](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
341348
if (N2.isZero() || (N1.isMinSignedValue() && N2.isAllOnes()) ||
@@ -394,11 +401,11 @@ TEST(KnownBitsTest, BinaryExhaustive) {
394401
return std::nullopt;
395402
return N1.shl(N2);
396403
},
397-
/*CheckOptimality=*/true);
404+
/*CheckOptimality=*/true, /*RefinePoisonToZero=*/true);
398405
testBinaryOpExhaustive(
399406
"ushl_ov",
400407
[](const KnownBits &Known1, const KnownBits &Known2) {
401-
return KnownBits::shl(Known1, Known2, /* NUW */ true);
408+
return KnownBits::shl(Known1, Known2, /*NUW=*/true);
402409
},
403410
[](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
404411
bool Overflow;
@@ -407,11 +414,11 @@ TEST(KnownBitsTest, BinaryExhaustive) {
407414
return std::nullopt;
408415
return Res;
409416
},
410-
/*CheckOptimality=*/true);
417+
/*CheckOptimality=*/true, /*RefinePoisonToZero=*/true);
411418
testBinaryOpExhaustive(
412419
"shl nsw",
413420
[](const KnownBits &Known1, const KnownBits &Known2) {
414-
return KnownBits::shl(Known1, Known2, /* NUW */ false, /* NSW */ true);
421+
return KnownBits::shl(Known1, Known2, /*NUW=*/false, /*NSW=*/true);
415422
},
416423
[](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
417424
bool Overflow;
@@ -420,11 +427,11 @@ TEST(KnownBitsTest, BinaryExhaustive) {
420427
return std::nullopt;
421428
return Res;
422429
},
423-
/*CheckOptimality=*/true);
430+
/*CheckOptimality=*/true, /*RefinePoisonToZero=*/true);
424431
testBinaryOpExhaustive(
425432
"shl nuw",
426433
[](const KnownBits &Known1, const KnownBits &Known2) {
427-
return KnownBits::shl(Known1, Known2, /* NUW */ true, /* NSW */ true);
434+
return KnownBits::shl(Known1, Known2, /*NUW=*/true, /*NSW=*/true);
428435
},
429436
[](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
430437
bool OverflowUnsigned, OverflowSigned;
@@ -434,7 +441,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
434441
return std::nullopt;
435442
return Res;
436443
},
437-
/*CheckOptimality=*/true);
444+
/*CheckOptimality=*/true, /*RefinePoisonToZero=*/true);
438445

439446
testBinaryOpExhaustive(
440447
"lshr",
@@ -446,7 +453,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
446453
return std::nullopt;
447454
return N1.lshr(N2);
448455
},
449-
/*CheckOptimality=*/true);
456+
/*CheckOptimality=*/true, /*RefinePoisonToZero=*/true);
450457
testBinaryOpExhaustive(
451458
"lshr exact",
452459
[](const KnownBits &Known1, const KnownBits &Known2) {
@@ -460,7 +467,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
460467
return std::nullopt;
461468
return N1.lshr(N2);
462469
},
463-
/*CheckOptimality=*/true);
470+
/*CheckOptimality=*/true, /*RefinePoisonToZero=*/true);
464471
testBinaryOpExhaustive(
465472
"ashr",
466473
[](const KnownBits &Known1, const KnownBits &Known2) {
@@ -471,7 +478,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
471478
return std::nullopt;
472479
return N1.ashr(N2);
473480
},
474-
/*CheckOptimality=*/true);
481+
/*CheckOptimality=*/true, /*RefinePoisonToZero=*/true);
475482
testBinaryOpExhaustive(
476483
"ashr exact",
477484
[](const KnownBits &Known1, const KnownBits &Known2) {
@@ -485,7 +492,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
485492
return std::nullopt;
486493
return N1.ashr(N2);
487494
},
488-
/*CheckOptimality=*/true);
495+
/*CheckOptimality=*/true, /*RefinePoisonToZero=*/true);
489496
testBinaryOpExhaustive(
490497
"mul",
491498
[](const KnownBits &Known1, const KnownBits &Known2) {
@@ -538,7 +545,7 @@ TEST(KnownBitsTest, UnaryExhaustive) {
538545
testUnaryOpExhaustive(
539546
"mul self",
540547
[](const KnownBits &Known) {
541-
return KnownBits::mul(Known, Known, /*SelfMultiply*/ true);
548+
return KnownBits::mul(Known, Known, /*SelfMultiply=*/true);
542549
},
543550
[](const APInt &N) { return N * N; }, /*CheckOptimality=*/false);
544551
}
@@ -709,8 +716,8 @@ TEST(KnownBitsTest, SExtOrTrunc) {
709716
const unsigned NarrowerSize = 4;
710717
const unsigned BaseSize = 6;
711718
const unsigned WiderSize = 8;
712-
APInt NegativeFitsNarrower(BaseSize, -4, /*isSigned*/ true);
713-
APInt NegativeDoesntFitNarrower(BaseSize, -28, /*isSigned*/ true);
719+
APInt NegativeFitsNarrower(BaseSize, -4, /*isSigned=*/true);
720+
APInt NegativeDoesntFitNarrower(BaseSize, -28, /*isSigned=*/true);
714721
APInt PositiveFitsNarrower(BaseSize, 14);
715722
APInt PositiveDoesntFitNarrower(BaseSize, 36);
716723
auto InitKnownBits = [&](KnownBits &Res, const APInt &Input) {

0 commit comments

Comments
 (0)