Index: llvm/include/llvm/ADT/BitVector.h =================================================================== --- llvm/include/llvm/ADT/BitVector.h +++ llvm/include/llvm/ADT/BitVector.h @@ -217,7 +217,7 @@ unsigned BitPos = Prev % BITWORD_SIZE; BitWord Copy = Bits[WordPos]; // Mask off previous bits. - Copy &= ~0UL << BitPos; + Copy &= maskTrailingZeros(BitPos); if (Copy != 0) return WordPos * BITWORD_SIZE + countTrailingZeros(Copy); @@ -229,7 +229,7 @@ return -1; } - /// find_next_unset - Returns the index of the next usnet bit following the + /// find_next_unset - Returns the index of the next unset bit following the /// "Prev" bit. Returns -1 if all remaining bits are set. int find_next_unset(unsigned Prev) const { ++Prev; @@ -253,6 +253,33 @@ return -1; } + /// find_prev - Returns the index of the next set bit *preceding* the + /// \p Next bit. Returns -1 if all previous bits are unset. + int find_prev(unsigned Next) { + if (Next == 0) + return -1; + + --Next; + + unsigned WordPos = Next / BITWORD_SIZE; + unsigned BitPos = Next % BITWORD_SIZE; + BitWord Copy = Bits[WordPos]; + // Mask off next bits. + Copy &= maskTrailingOnes(BitPos + 1); + + if (Copy != 0) + return (WordPos + 1) * BITWORD_SIZE - countLeadingZeros(Copy) - 1; + + // Check previous words. + for (unsigned i = 1; i <= WordPos; ++i) { + unsigned Index = WordPos - i; + if (Bits[Index] == 0) + continue; + return (Index + 1) * BITWORD_SIZE - countLeadingZeros(Bits[Index]) - 1; + } + return -1; + } + /// clear - Clear all bits. void clear() { Size = 0; Index: llvm/include/llvm/ADT/SmallBitVector.h =================================================================== --- llvm/include/llvm/ADT/SmallBitVector.h +++ llvm/include/llvm/ADT/SmallBitVector.h @@ -278,6 +278,24 @@ return getPointer()->find_next_unset(Prev); } + /// find_prev - Returns the index of the next set bit *preceding* the + /// \p Next bit. Returns -1 if all previous bits are unset. + int find_prev(unsigned Next) const { + if (isSmall()) { + if (Next == 0) + return -1; + + --Next; + uintptr_t Bits = getSmallBits(); + Bits &= maskTrailingOnes(Next + 1); + if (Bits == 0) + return -1; + + return NumBaseBits - countLeadingZeros(Bits) - 1; + } + return getPointer()->find_prev(Next); + } + /// Clear all bits. void clear() { if (!isSmall()) Index: llvm/include/llvm/Support/MathExtras.h =================================================================== --- llvm/include/llvm/Support/MathExtras.h +++ llvm/include/llvm/Support/MathExtras.h @@ -214,6 +214,18 @@ return ~maskTrailingOnes(CHAR_BIT * sizeof(T) - N); } +/// \brief Create a bitmask with the N right-most bits set to 0, and all other +/// bits set to 1. Only unsigned types are allowed. +template T maskTrailingZeros(unsigned N) { + return maskLeadingOnes(CHAR_BIT * sizeof(T) - N); +} + +/// \brief Create a bitmask with the N left-most bits set to 0, and all other +/// bits set to 1. Only unsigned types are allowed. +template T maskLeadingZeros(unsigned N) { + return maskTrailingOnes(CHAR_BIT * sizeof(T) - N); +} + /// \brief Get the index of the last set bit starting from the least /// significant bit. /// Index: llvm/unittests/ADT/BitVectorTest.cpp =================================================================== --- llvm/unittests/ADT/BitVectorTest.cpp +++ llvm/unittests/ADT/BitVectorTest.cpp @@ -204,6 +204,11 @@ EXPECT_EQ(75, A.find_next(13)); EXPECT_EQ(-1, A.find_next(75)); + EXPECT_EQ(-1, A.find_prev(12)); + EXPECT_EQ(12, A.find_prev(13)); + EXPECT_EQ(13, A.find_prev(75)); + EXPECT_EQ(75, A.find_prev(90)); + EXPECT_EQ(0, A.find_first_unset()); EXPECT_EQ(99, A.find_last_unset()); EXPECT_EQ(14, A.find_next_unset(11)); @@ -227,6 +232,30 @@ EXPECT_EQ(-1, A.find_last()); EXPECT_EQ(0, A.find_first_unset()); EXPECT_EQ(99, A.find_last_unset()); + + // Also test with a vector that is small enough to fit in 1 word. + A.resize(20); + A.set(3); + A.set(4); + A.set(16); + EXPECT_EQ(16, A.find_last()); + EXPECT_EQ(3, A.find_first()); + EXPECT_EQ(3, A.find_next(1)); + EXPECT_EQ(4, A.find_next(3)); + EXPECT_EQ(16, A.find_next(4)); + EXPECT_EQ(-1, A.find_next(16)); + + EXPECT_EQ(-1, A.find_prev(3)); + EXPECT_EQ(3, A.find_prev(4)); + EXPECT_EQ(4, A.find_prev(16)); + EXPECT_EQ(16, A.find_prev(18)); + + EXPECT_EQ(0, A.find_first_unset()); + EXPECT_EQ(19, A.find_last_unset()); + EXPECT_EQ(5, A.find_next_unset(3)); + EXPECT_EQ(5, A.find_next_unset(4)); + EXPECT_EQ(13, A.find_next_unset(12)); + EXPECT_EQ(17, A.find_next_unset(15)); } TYPED_TEST(BitVectorTest, CompoundAssignment) {