Skip to content

Commit f97bcdb

Browse files
authored
[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
1 parent 23b8f59 commit f97bcdb

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

llvm/unittests/Support/KnownBitsTest.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ template <typename FnTy> void ForeachKnownBits(unsigned Bits, FnTy Fn) {
3434
template <typename FnTy>
3535
void ForeachNumInKnownBits(const KnownBits &Known, FnTy Fn) {
3636
unsigned Bits = Known.getBitWidth();
37-
unsigned Max = 1 << Bits;
37+
assert(Bits < 32);
38+
unsigned Max = 1u << Bits;
39+
unsigned Zero = Known.Zero.getZExtValue();
40+
unsigned One = Known.One.getZExtValue();
3841
for (unsigned N = 0; N < Max; ++N) {
39-
APInt Num(Bits, N);
40-
if ((Num & Known.Zero) != 0 || (~Num & Known.One) != 0)
41-
continue;
42-
43-
Fn(Num);
42+
if ((N & Zero) == 0 && (~N & One) == 0)
43+
Fn(APInt(Bits, N));
4444
}
4545
}
4646

0 commit comments

Comments
 (0)