[llvm] f97bcdb - [KnownBits] Speed up ForeachKnownBits in unit test. NFC. (#94939)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 10 02:10:37 PDT 2024


Author: Jay Foad
Date: 2024-06-10T10:10:33+01:00
New Revision: f97bcdbd95c075134b3d928299592c54c96ebaad

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

LOG: [KnownBits] Speed up ForeachKnownBits in unit test. NFC. (#94939)

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

Added: 
    

Modified: 
    llvm/unittests/Support/KnownBitsTest.h

Removed: 
    


################################################################################
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));
   }
 }
 


        


More information about the llvm-commits mailing list