Skip to content

Commit 40c3e58

Browse files
authored
[KnownBits] Check that mul is optimal for low order bits (llvm#113316)
1 parent 5942be0 commit 40c3e58

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

llvm/unittests/Support/KnownBitsTest.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,4 +815,38 @@ TEST(KnownBitsTest, ConcatBits) {
815815
}
816816
}
817817

818+
TEST(KnownBitsTest, MulExhaustive) {
819+
for (unsigned Bits : {1, 4}) {
820+
ForeachKnownBits(Bits, [&](const KnownBits &Known1) {
821+
ForeachKnownBits(Bits, [&](const KnownBits &Known2) {
822+
KnownBits Computed = KnownBits::mul(Known1, Known2);
823+
KnownBits Exact(Bits);
824+
Exact.Zero.setAllBits();
825+
Exact.One.setAllBits();
826+
827+
ForeachNumInKnownBits(Known1, [&](const APInt &N1) {
828+
ForeachNumInKnownBits(Known2, [&](const APInt &N2) {
829+
APInt Res = N1 * N2;
830+
Exact.One &= Res;
831+
Exact.Zero &= ~Res;
832+
});
833+
});
834+
835+
if (!Exact.hasConflict()) {
836+
// Check that the result is optimal for the contiguous known low order
837+
// bits.
838+
APInt Mask = APInt::getLowBitsSet(
839+
Bits, (Exact.Zero | Exact.One).countTrailingOnes());
840+
Exact.Zero &= Mask;
841+
Exact.One &= Mask;
842+
Computed.Zero &= Mask;
843+
Computed.One &= Mask;
844+
EXPECT_TRUE(checkResult("mul", Exact, Computed, {Known1, Known2},
845+
/*CheckOptimality=*/true));
846+
}
847+
});
848+
});
849+
}
850+
}
851+
818852
} // end anonymous namespace

0 commit comments

Comments
 (0)