Simplify SmallBitVector::applyMask by consolidating common code for 32-bit and 64...
authorYaron Keren <yaron.keren@gmail.com>
Fri, 18 Sep 2015 06:35:12 +0000 (06:35 +0000)
committerYaron Keren <yaron.keren@gmail.com>
Fri, 18 Sep 2015 06:35:12 +0000 (06:35 +0000)
Extend mask value to 64 bits before taking its complement and assert when mask is
too large to apply in the small case (previously the extra words were silently ignored).

http://reviews.llvm.org/D11890

Patch by James Touton!

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@247972 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/SmallBitVector.h
unittests/ADT/BitVectorTest.cpp

index ae3d645396fdb1f808634a44a15fbd9e3bbd0120..39d2e14a63d5562aaae75b77e150c6c76be0fba4 100644 (file)
@@ -553,17 +553,15 @@ public:
 private:
   template<bool AddBits, bool InvertMask>
   void applyMask(const uint32_t *Mask, unsigned MaskWords) {
-    if (NumBaseBits == 64 && MaskWords >= 2) {
-      uint64_t M = Mask[0] | (uint64_t(Mask[1]) << 32);
-      if (InvertMask) M = ~M;
-      if (AddBits) setSmallBits(getSmallBits() | M);
-      else         setSmallBits(getSmallBits() & ~M);
-    } else {
-      uint32_t M = Mask[0];
-      if (InvertMask) M = ~M;
-      if (AddBits) setSmallBits(getSmallBits() | M);
-      else         setSmallBits(getSmallBits() & ~M);
+    uintptr_t M = Mask[0];
+    if (MaskWords != 1) {
+      assert(NumBaseBits == 64 && MaskWords == 2 &&
+             "Mask is larger than base!");
+      M |= uintptr_t(Mask[1]) << 32;
     }
+    if (InvertMask) M = ~M;
+    if (AddBits) setSmallBits(getSmallBits() | M);
+    else         setSmallBits(getSmallBits() & ~M);
   }
 };
 
index 3deaff0fe3569b447451a95ebea6bd162614f1a7..5d99e1d27a557ee8e09da42e1eccaa9a7759ac81 100644 (file)
@@ -235,12 +235,12 @@ TYPED_TEST(BitVectorTest, PortableBitMask) {
   const uint32_t Mask1[] = { 0x80000000, 6, 5 };
 
   A.resize(10);
-  A.setBitsInMask(Mask1, 3);
+  A.setBitsInMask(Mask1, 2);
   EXPECT_EQ(10u, A.size());
   EXPECT_FALSE(A.test(0));
 
   A.resize(32);
-  A.setBitsInMask(Mask1, 3);
+  A.setBitsInMask(Mask1, 2);
   EXPECT_FALSE(A.test(0));
   EXPECT_TRUE(A.test(31));
   EXPECT_EQ(1u, A.count());