Skip to content

[KnownBits] Check that mul is optimal for low order bits #113316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 22, 2024

Conversation

jayfoad
Copy link
Contributor

@jayfoad jayfoad commented Oct 22, 2024

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Oct 22, 2024

@llvm/pr-subscribers-llvm-support

Author: Jay Foad (jayfoad)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/113316.diff

1 Files Affected:

  • (modified) llvm/unittests/Support/KnownBitsTest.cpp (+34)
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

@goldsteinn
Copy link
Contributor

LGTM

Copy link
Contributor

@artagnon artagnon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@jayfoad jayfoad merged commit 40c3e58 into llvm:main Oct 22, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants