Skip to content

[KnownBits] Speed up ForeachKnownBits in unit test. NFC. #94939

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
Jun 10, 2024

Conversation

jayfoad
Copy link
Contributor

@jayfoad jayfoad commented Jun 10, 2024

Use fast unsigned arithmetic before constructing an APInt. This gives
me a ~2x speed up when running this in my Release+Asserts build:

$ unittests/Support/SupportTests --gtest_filter=KnownBitsTest.*Exhaustive

Use fast unsigned arithmetic before constructing an APInt. This gives
me a ~2x speed up when running this in my Release+Asserts build:

$ unittests/Support/SupportTests --gtest_filter=KnownBitsTest.*Exhaustive
@llvmbot
Copy link
Member

llvmbot commented Jun 10, 2024

@llvm/pr-subscribers-llvm-support

Author: Jay Foad (jayfoad)

Changes

Use fast unsigned arithmetic before constructing an APInt. This gives
me a ~2x speed up when running this in my Release+Asserts build:

$ unittests/Support/SupportTests --gtest_filter=KnownBitsTest.*Exhaustive


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

1 Files Affected:

  • (modified) llvm/unittests/Support/KnownBitsTest.h (+6-6)
diff --git a/llvm/unittests/Support/KnownBitsTest.h b/llvm/unittests/Support/KnownBitsTest.h
index 556da2b9ecdad..974051891d48e 100644
--- a/llvm/unittests/Support/KnownBitsTest.h
+++ b/llvm/unittests/Support/KnownBitsTest.h
@@ -34,13 +34,13 @@ template <typename FnTy> void ForeachKnownBits(unsigned Bits, FnTy Fn) {
 template <typename FnTy>
 void ForeachNumInKnownBits(const KnownBits &Known, FnTy Fn) {
   unsigned Bits = Known.getBitWidth();
-  unsigned Max = 1 << Bits;
+  assert(Bits < 32);
+  unsigned Max = 1u << Bits;
+  unsigned Zero = Known.Zero.getZExtValue();
+  unsigned One = Known.One.getZExtValue();
   for (unsigned N = 0; N < Max; ++N) {
-    APInt Num(Bits, N);
-    if ((Num & Known.Zero) != 0 || (~Num & Known.One) != 0)
-      continue;
-
-    Fn(Num);
+    if ((N & Zero) == 0 && (~N & One) == 0)
+      Fn(APInt(Bits, N));
   }
 }
 

@jayfoad jayfoad merged commit f97bcdb into llvm:main Jun 10, 2024
6 of 8 checks passed
@jayfoad jayfoad deleted the foreachnuminknownbits-speedup branch June 10, 2024 09:10
@HerrCai0907 HerrCai0907 mentioned this pull request Jun 13, 2024
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.

3 participants