[llvm] 40c3e58 - [KnownBits] Check that mul is optimal for low order bits (#113316)

via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 22 08:36:10 PDT 2024


Author: Jay Foad
Date: 2024-10-22T16:36:06+01:00
New Revision: 40c3e583b7c29c94b3fdd2361c5288b2dcc3be83

URL: https://github.com/llvm/llvm-project/commit/40c3e583b7c29c94b3fdd2361c5288b2dcc3be83
DIFF: https://github.com/llvm/llvm-project/commit/40c3e583b7c29c94b3fdd2361c5288b2dcc3be83.diff

LOG: [KnownBits] Check that mul is optimal for low order bits (#113316)

Added: 
    

Modified: 
    llvm/unittests/Support/KnownBitsTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/unittests/Support/KnownBitsTest.cpp b/llvm/unittests/Support/KnownBitsTest.cpp
index 551c1a8107494b..b16368de176481 100644
--- a/llvm/unittests/Support/KnownBitsTest.cpp
+++ b/llvm/unittests/Support/KnownBitsTest.cpp
@@ -815,4 +815,38 @@ TEST(KnownBitsTest, ConcatBits) {
   }
 }
 
+TEST(KnownBitsTest, MulExhaustive) {
+  for (unsigned Bits : {1, 4}) {
+    ForeachKnownBits(Bits, [&](const KnownBits &Known1) {
+      ForeachKnownBits(Bits, [&](const KnownBits &Known2) {
+        KnownBits Computed = KnownBits::mul(Known1, Known2);
+        KnownBits Exact(Bits);
+        Exact.Zero.setAllBits();
+        Exact.One.setAllBits();
+
+        ForeachNumInKnownBits(Known1, [&](const APInt &N1) {
+          ForeachNumInKnownBits(Known2, [&](const APInt &N2) {
+            APInt Res = N1 * N2;
+            Exact.One &= Res;
+            Exact.Zero &= ~Res;
+          });
+        });
+
+        if (!Exact.hasConflict()) {
+          // Check that the result is optimal for the contiguous known low order
+          // bits.
+          APInt Mask = APInt::getLowBitsSet(
+              Bits, (Exact.Zero | Exact.One).countTrailingOnes());
+          Exact.Zero &= Mask;
+          Exact.One &= Mask;
+          Computed.Zero &= Mask;
+          Computed.One &= Mask;
+          EXPECT_TRUE(checkResult("mul", Exact, Computed, {Known1, Known2},
+                                  /*CheckOptimality=*/true));
+        }
+      });
+    });
+  }
+}
+
 } // end anonymous namespace


        


More information about the llvm-commits mailing list