diff options
author | Jay Foad <jay.foad@amd.com> | 2023-05-25 15:51:00 +0100 |
---|---|---|
committer | Jay Foad <jay.foad@amd.com> | 2023-05-25 16:02:16 +0100 |
commit | 5aa6bbe7454147662f8a59e9b3eab96caa739977 (patch) | |
tree | 96d427a0a77a8d219db5eca37192778b794d339b /llvm/unittests/Support/KnownBitsTest.cpp | |
parent | 6bc78f0cdba4ffc7fcf26215b33f6d01f2059032 (diff) | |
download | llvm-5aa6bbe7454147662f8a59e9b3eab96caa739977.zip llvm-5aa6bbe7454147662f8a59e9b3eab96caa739977.tar.gz llvm-5aa6bbe7454147662f8a59e9b3eab96caa739977.tar.bz2 |
[KnownBits] Check functions that return zero for poison results
Differential Revision: https://reviews.llvm.org/D151456
Diffstat (limited to 'llvm/unittests/Support/KnownBitsTest.cpp')
-rw-r--r-- | llvm/unittests/Support/KnownBitsTest.cpp | 41 |
1 files changed, 27 insertions, 14 deletions
diff --git a/llvm/unittests/Support/KnownBitsTest.cpp b/llvm/unittests/Support/KnownBitsTest.cpp index 427b323..4ca9d52 100644 --- a/llvm/unittests/Support/KnownBitsTest.cpp +++ b/llvm/unittests/Support/KnownBitsTest.cpp @@ -28,7 +28,11 @@ using BinaryIntFn = using BinaryCheckFn = llvm::function_ref<bool(const KnownBits &, const KnownBits &)>; +static bool checkOptimalityUnary(const KnownBits &) { return true; } static bool checkCorrectnessOnlyUnary(const KnownBits &) { return false; } +static bool checkOptimalityBinary(const KnownBits &, const KnownBits &) { + return true; +} static bool checkCorrectnessOnlyBinary(const KnownBits &, const KnownBits &) { return false; } @@ -62,9 +66,9 @@ static testing::AssertionResult isOptimal(const KnownBits &Exact, return Result; } -static void testUnaryOpExhaustive( - UnaryBitsFn BitsFn, UnaryIntFn IntFn, - UnaryCheckFn CheckOptimalityFn = [](const KnownBits &) { return true; }) { +static void +testUnaryOpExhaustive(UnaryBitsFn BitsFn, UnaryIntFn IntFn, + UnaryCheckFn CheckOptimalityFn = checkOptimalityUnary) { unsigned Bits = 4; ForeachKnownBits(Bits, [&](const KnownBits &Known) { KnownBits Computed = BitsFn(Known); @@ -89,11 +93,10 @@ static void testUnaryOpExhaustive( }); } -static void testBinaryOpExhaustive( - BinaryBitsFn BitsFn, BinaryIntFn IntFn, - BinaryCheckFn CheckOptimalityFn = [](const KnownBits &, const KnownBits &) { - return true; - }) { +static void +testBinaryOpExhaustive(BinaryBitsFn BitsFn, BinaryIntFn IntFn, + BinaryCheckFn CheckOptimalityFn = checkOptimalityBinary, + bool RefinePoisonToZero = false) { unsigned Bits = 4; ForeachKnownBits(Bits, [&](const KnownBits &Known1) { ForeachKnownBits(Bits, [&](const KnownBits &Known2) { @@ -118,6 +121,10 @@ static void testBinaryOpExhaustive( if (CheckOptimalityFn(Known1, Known2) && !Exact.hasConflict()) { EXPECT_TRUE(isOptimal(Exact, Computed, {Known1, Known2})); } + // In some cases we choose to return zero if the result is always poison. + if (RefinePoisonToZero && Exact.hasConflict()) { + EXPECT_TRUE(Computed.isZero()); + } }); }); } @@ -342,7 +349,8 @@ TEST(KnownBitsTest, BinaryExhaustive) { if (N2.uge(N2.getBitWidth())) return std::nullopt; return N1.shl(N2); - }); + }, + checkOptimalityBinary, /* RefinePoisonToZero */ true); testBinaryOpExhaustive( [](const KnownBits &Known1, const KnownBits &Known2) { return KnownBits::shl(Known1, Known2, /* NUW */ true); @@ -353,7 +361,8 @@ TEST(KnownBitsTest, BinaryExhaustive) { if (Overflow) return std::nullopt; return Res; - }); + }, + checkOptimalityBinary, /* RefinePoisonToZero */ true); testBinaryOpExhaustive( [](const KnownBits &Known1, const KnownBits &Known2) { return KnownBits::shl(Known1, Known2, /* NUW */ false, /* NSW */ true); @@ -364,7 +373,8 @@ TEST(KnownBitsTest, BinaryExhaustive) { if (Overflow) return std::nullopt; return Res; - }); + }, + checkOptimalityBinary, /* RefinePoisonToZero */ true); testBinaryOpExhaustive( [](const KnownBits &Known1, const KnownBits &Known2) { return KnownBits::shl(Known1, Known2, /* NUW */ true, /* NSW */ true); @@ -376,7 +386,8 @@ TEST(KnownBitsTest, BinaryExhaustive) { if (OverflowUnsigned || OverflowSigned) return std::nullopt; return Res; - }); + }, + checkOptimalityBinary, /* RefinePoisonToZero */ true); testBinaryOpExhaustive( [](const KnownBits &Known1, const KnownBits &Known2) { @@ -386,7 +397,8 @@ TEST(KnownBitsTest, BinaryExhaustive) { if (N2.uge(N2.getBitWidth())) return std::nullopt; return N1.lshr(N2); - }); + }, + checkOptimalityBinary, /* RefinePoisonToZero */ true); testBinaryOpExhaustive( [](const KnownBits &Known1, const KnownBits &Known2) { return KnownBits::ashr(Known1, Known2); @@ -395,7 +407,8 @@ TEST(KnownBitsTest, BinaryExhaustive) { if (N2.uge(N2.getBitWidth())) return std::nullopt; return N1.ashr(N2); - }); + }, + checkOptimalityBinary, /* RefinePoisonToZero */ true); testBinaryOpExhaustive( [](const KnownBits &Known1, const KnownBits &Known2) { |