[llvm] 5aa6bbe - [KnownBits] Check functions that return zero for poison results

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Thu May 25 08:02:23 PDT 2023


Author: Jay Foad
Date: 2023-05-25T16:02:16+01:00
New Revision: 5aa6bbe7454147662f8a59e9b3eab96caa739977

URL: https://github.com/llvm/llvm-project/commit/5aa6bbe7454147662f8a59e9b3eab96caa739977
DIFF: https://github.com/llvm/llvm-project/commit/5aa6bbe7454147662f8a59e9b3eab96caa739977.diff

LOG: [KnownBits] Check functions that return zero for poison results

Differential Revision: https://reviews.llvm.org/D151456

Added: 
    

Modified: 
    llvm/unittests/Support/KnownBitsTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/unittests/Support/KnownBitsTest.cpp b/llvm/unittests/Support/KnownBitsTest.cpp
index 427b323912abb..4ca9d5250943a 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) {


        


More information about the llvm-commits mailing list