[llvm] r299187 - [APInt] Add unittests that demonstrate how very broken APIntOps::isShiftedMask is.

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 30 23:30:25 PDT 2017


Author: ctopper
Date: Fri Mar 31 01:30:25 2017
New Revision: 299187

URL: http://llvm.org/viewvc/llvm-project?rev=299187&view=rev
Log:
[APInt] Add unittests that demonstrate how very broken APIntOps::isShiftedMask is.

Did you know that 0 is a shifted mask? But 0x0000ff00 and 0x000000ff aren't? At least we get 0xff000000 right.

I only see one usage of this function in the code base today and its in InstCombine. I think its protected against 0 being misreported as a mask. I guess we just don't have tests for the missed cases.

Modified:
    llvm/trunk/unittests/ADT/APIntTest.cpp

Modified: llvm/trunk/unittests/ADT/APIntTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/APIntTest.cpp?rev=299187&r1=299186&r2=299187&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/APIntTest.cpp (original)
+++ llvm/trunk/unittests/ADT/APIntTest.cpp Fri Mar 31 01:30:25 2017
@@ -1575,6 +1575,31 @@ TEST(APIntTest, isMask) {
   }
 }
 
+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
+
+  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
+
+    APInt One(N, 1);
+    for (int I = 1; I < N; ++I) {
+      APInt MaskVal = One.shl(I) - 1;
+      EXPECT_FALSE(APIntOps::isShiftedMask(N, MaskVal)); // BUG
+    }
+    for (int I = 1; I < N - 1; ++I) {
+      APInt MaskVal = One.shl(I);
+      EXPECT_FALSE(APIntOps::isShiftedMask(N, MaskVal)); // BUG
+    }
+    for (int I = 1; I < N; ++I) {
+      APInt MaskVal = APInt::getHighBitsSet(N, I);
+      EXPECT_TRUE(APIntOps::isShiftedMask(N, MaskVal));
+    }
+  }
+}
+
 #if defined(__clang__)
 // Disable the pragma warning from versions of Clang without -Wself-move
 #pragma clang diagnostic push




More information about the llvm-commits mailing list