@@ -77,7 +77,8 @@ static void testUnaryOpExhaustive(StringRef Name, UnaryBitsFn BitsFn,
77
77
78
78
static void testBinaryOpExhaustive (StringRef Name, BinaryBitsFn BitsFn,
79
79
BinaryIntFn IntFn,
80
- bool CheckOptimality = true ) {
80
+ bool CheckOptimality = true ,
81
+ bool RefinePoisonToZero = false ) {
81
82
for (unsigned Bits : {1 , 4 }) {
82
83
ForeachKnownBits (Bits, [&](const KnownBits &Known1) {
83
84
ForeachKnownBits (Bits, [&](const KnownBits &Known2) {
@@ -99,6 +100,12 @@ static void testBinaryOpExhaustive(StringRef Name, BinaryBitsFn BitsFn,
99
100
EXPECT_TRUE (checkResult (Name, Exact, Computed, {Known1, Known2},
100
101
CheckOptimality));
101
102
}
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
+ }
102
109
});
103
110
});
104
111
}
@@ -313,7 +320,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
313
320
testBinaryOpExhaustive (
314
321
" udiv exact" ,
315
322
[](const KnownBits &Known1, const KnownBits &Known2) {
316
- return KnownBits::udiv (Known1, Known2, /* Exact*/ true );
323
+ return KnownBits::udiv (Known1, Known2, /* Exact= */ true );
317
324
},
318
325
[](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
319
326
if (N2.isZero () || !N1.urem (N2).isZero ())
@@ -335,7 +342,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
335
342
testBinaryOpExhaustive (
336
343
" sdiv exact" ,
337
344
[](const KnownBits &Known1, const KnownBits &Known2) {
338
- return KnownBits::sdiv (Known1, Known2, /* Exact*/ true );
345
+ return KnownBits::sdiv (Known1, Known2, /* Exact= */ true );
339
346
},
340
347
[](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
341
348
if (N2.isZero () || (N1.isMinSignedValue () && N2.isAllOnes ()) ||
@@ -394,11 +401,11 @@ TEST(KnownBitsTest, BinaryExhaustive) {
394
401
return std::nullopt;
395
402
return N1.shl (N2);
396
403
},
397
- /* CheckOptimality=*/ true );
404
+ /* CheckOptimality=*/ true , /* RefinePoisonToZero= */ true );
398
405
testBinaryOpExhaustive (
399
406
" ushl_ov" ,
400
407
[](const KnownBits &Known1, const KnownBits &Known2) {
401
- return KnownBits::shl (Known1, Known2, /* NUW */ true );
408
+ return KnownBits::shl (Known1, Known2, /* NUW= */ true );
402
409
},
403
410
[](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
404
411
bool Overflow;
@@ -407,11 +414,11 @@ TEST(KnownBitsTest, BinaryExhaustive) {
407
414
return std::nullopt;
408
415
return Res;
409
416
},
410
- /* CheckOptimality=*/ true );
417
+ /* CheckOptimality=*/ true , /* RefinePoisonToZero= */ true );
411
418
testBinaryOpExhaustive (
412
419
" shl nsw" ,
413
420
[](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 );
415
422
},
416
423
[](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
417
424
bool Overflow;
@@ -420,11 +427,11 @@ TEST(KnownBitsTest, BinaryExhaustive) {
420
427
return std::nullopt;
421
428
return Res;
422
429
},
423
- /* CheckOptimality=*/ true );
430
+ /* CheckOptimality=*/ true , /* RefinePoisonToZero= */ true );
424
431
testBinaryOpExhaustive (
425
432
" shl nuw" ,
426
433
[](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 );
428
435
},
429
436
[](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
430
437
bool OverflowUnsigned, OverflowSigned;
@@ -434,7 +441,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
434
441
return std::nullopt;
435
442
return Res;
436
443
},
437
- /* CheckOptimality=*/ true );
444
+ /* CheckOptimality=*/ true , /* RefinePoisonToZero= */ true );
438
445
439
446
testBinaryOpExhaustive (
440
447
" lshr" ,
@@ -446,7 +453,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
446
453
return std::nullopt;
447
454
return N1.lshr (N2);
448
455
},
449
- /* CheckOptimality=*/ true );
456
+ /* CheckOptimality=*/ true , /* RefinePoisonToZero= */ true );
450
457
testBinaryOpExhaustive (
451
458
" lshr exact" ,
452
459
[](const KnownBits &Known1, const KnownBits &Known2) {
@@ -460,7 +467,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
460
467
return std::nullopt;
461
468
return N1.lshr (N2);
462
469
},
463
- /* CheckOptimality=*/ true );
470
+ /* CheckOptimality=*/ true , /* RefinePoisonToZero= */ true );
464
471
testBinaryOpExhaustive (
465
472
" ashr" ,
466
473
[](const KnownBits &Known1, const KnownBits &Known2) {
@@ -471,7 +478,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
471
478
return std::nullopt;
472
479
return N1.ashr (N2);
473
480
},
474
- /* CheckOptimality=*/ true );
481
+ /* CheckOptimality=*/ true , /* RefinePoisonToZero= */ true );
475
482
testBinaryOpExhaustive (
476
483
" ashr exact" ,
477
484
[](const KnownBits &Known1, const KnownBits &Known2) {
@@ -485,7 +492,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
485
492
return std::nullopt;
486
493
return N1.ashr (N2);
487
494
},
488
- /* CheckOptimality=*/ true );
495
+ /* CheckOptimality=*/ true , /* RefinePoisonToZero= */ true );
489
496
testBinaryOpExhaustive (
490
497
" mul" ,
491
498
[](const KnownBits &Known1, const KnownBits &Known2) {
@@ -538,7 +545,7 @@ TEST(KnownBitsTest, UnaryExhaustive) {
538
545
testUnaryOpExhaustive (
539
546
" mul self" ,
540
547
[](const KnownBits &Known) {
541
- return KnownBits::mul (Known, Known, /* SelfMultiply*/ true );
548
+ return KnownBits::mul (Known, Known, /* SelfMultiply= */ true );
542
549
},
543
550
[](const APInt &N) { return N * N; }, /* CheckOptimality=*/ false );
544
551
}
@@ -709,8 +716,8 @@ TEST(KnownBitsTest, SExtOrTrunc) {
709
716
const unsigned NarrowerSize = 4 ;
710
717
const unsigned BaseSize = 6 ;
711
718
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 );
714
721
APInt PositiveFitsNarrower (BaseSize, 14 );
715
722
APInt PositiveDoesntFitNarrower (BaseSize, 36 );
716
723
auto InitKnownBits = [&](KnownBits &Res, const APInt &Input) {
0 commit comments