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

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 22 06:50:58 PDT 2024


https://github.com/jayfoad created https://github.com/llvm/llvm-project/pull/113316

None

>From 4c8bc18cd8e30e4b0cd4a7e16b23ffeaf94d1b3b Mon Sep 17 00:00:00 2001
From: Jay Foad <jay.foad at amd.com>
Date: Tue, 22 Oct 2024 14:47:06 +0100
Subject: [PATCH] [KnownBits] Check that mul is optimal for low order bits

---
 llvm/unittests/Support/KnownBitsTest.cpp | 34 ++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

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