[APInt] Fix bugs in isShiftedMask to match behavior of the similar function in MathExtras.h
This removes a parameter from the routine that was responsible for a lot of the issue. It was a bit count that had to be set to the BitWidth of the APInt and would get passed to getLowBitsSet. This guaranteed the call to getLowBitsSet would create an all ones value. This was then compared to (V | (V-1)). So the only shifted masks we detected had to have the MSB set.
The one in tree user is a transform in InstCombine that never fires due to earlier transforms covering the case better. I've submitted a patch to remove it completely, but for now I've just adapted it to the new interface for isShiftedMask.
llvm-svn: 299273
diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp
index 51c6347..bf7d8d8 100644
--- a/llvm/unittests/ADT/APIntTest.cpp
+++ b/llvm/unittests/ADT/APIntTest.cpp
@@ -1576,26 +1576,26 @@
}
TEST(APIntTest, isShiftedMask) {
- EXPECT_FALSE(APIntOps::isShiftedMask(32, APInt(32, 0x01010101)));
- EXPECT_TRUE(APIntOps::isShiftedMask(32, APInt(32, 0xf0000000)));
- EXPECT_TRUE(APIntOps::isShiftedMask(32, APInt(32, 0xffff0000)));
- EXPECT_FALSE(APIntOps::isShiftedMask(32, APInt(32, 0xff << 1))); // BUG
+ EXPECT_FALSE(APIntOps::isShiftedMask(APInt(32, 0x01010101)));
+ EXPECT_TRUE(APIntOps::isShiftedMask(APInt(32, 0xf0000000)));
+ EXPECT_TRUE(APIntOps::isShiftedMask(APInt(32, 0xffff0000)));
+ EXPECT_TRUE(APIntOps::isShiftedMask(APInt(32, 0xff << 1)));
for (int N : { 1, 2, 3, 4, 7, 8, 16, 32, 64, 127, 128, 129, 256 }) {
- EXPECT_TRUE(APIntOps::isShiftedMask(N, APInt(N, 0))); // BUG
+ EXPECT_FALSE(APIntOps::isShiftedMask(APInt(N, 0)));
APInt One(N, 1);
for (int I = 1; I < N; ++I) {
APInt MaskVal = One.shl(I) - 1;
- EXPECT_FALSE(APIntOps::isShiftedMask(N, MaskVal)); // BUG
+ EXPECT_TRUE(APIntOps::isShiftedMask(MaskVal));
}
for (int I = 1; I < N - 1; ++I) {
APInt MaskVal = One.shl(I);
- EXPECT_FALSE(APIntOps::isShiftedMask(N, MaskVal)); // BUG
+ EXPECT_TRUE(APIntOps::isShiftedMask(MaskVal));
}
for (int I = 1; I < N; ++I) {
APInt MaskVal = APInt::getHighBitsSet(N, I);
- EXPECT_TRUE(APIntOps::isShiftedMask(N, MaskVal));
+ EXPECT_TRUE(APIntOps::isShiftedMask(MaskVal));
}
}
}