Skip to content

Commit defcb19

Browse files
committed
Some micro-optimizations
1 parent 1063eca commit defcb19

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

llvm/include/llvm/ADT/SparseBitVector.h

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -143,24 +143,31 @@ template <unsigned ElementSize = 128> struct SparseBitVectorElement {
143143
llvm_unreachable("Illegal empty element");
144144
}
145145

146-
/// find_next - Returns the index of the next set bit starting from the
147-
/// "Curr" bit. Returns -1 if the next set bit is not found.
148-
int find_next(int Curr) const {
149-
assert(Curr >= 0 && Curr < BITS_PER_ELEMENT);
146+
/// find_next - Sets Curr to the index of the next set bit starting after the
147+
/// Curr bit and returns false, or sets Curr to ~0U and returns true if no
148+
/// next bit is found.
149+
bool find_next(unsigned &Curr) const {
150+
assert(Curr < BITS_PER_ELEMENT);
150151

151152
unsigned WordPos = Curr / BITWORD_SIZE;
152153
unsigned BitPos = Curr % BITWORD_SIZE;
153154

154155
// Mask off previous bits.
155156
BitWord Copy = Bits[WordPos] & ~1UL << BitPos;
156-
if (Copy != 0)
157-
return WordPos * BITWORD_SIZE + llvm::countr_zero(Copy);
157+
if (Copy != 0) {
158+
Curr = WordPos * BITWORD_SIZE + llvm::countr_zero(Copy);
159+
return false;
160+
}
158161

159162
// Check subsequent words.
160-
for (unsigned i = WordPos+1; i < BITWORDS_PER_ELEMENT; ++i)
161-
if (Bits[i] != 0)
162-
return i * BITWORD_SIZE + llvm::countr_zero(Bits[i]);
163-
return -1;
163+
for (unsigned i = WordPos + 1; i < BITWORDS_PER_ELEMENT; ++i) {
164+
if (Bits[i] != 0) {
165+
Curr = i * BITWORD_SIZE + llvm::countr_zero(Bits[i]);
166+
return false;
167+
}
168+
}
169+
Curr = ~0U;
170+
return true;
164171
}
165172

166173
// Union this element with RHS and return true if this one changed.
@@ -312,9 +319,9 @@ class SparseBitVector {
312319
// Iterator to walk set bits in the bitvector.
313320
class SparseBitVectorIterator {
314321
private:
315-
// Current bit number within the current element, or -1 if we are at the
322+
// Current bit number within the current element, or ~0U if we are at the
316323
// end.
317-
int BitPos = -1;
324+
unsigned BitPos = ~0U;
318325

319326
// Iterators to the current element and the end of the bitvector. These are
320327
// only valid when BitPos >= 0.
@@ -334,8 +341,7 @@ class SparseBitVector {
334341

335342
// Preincrement.
336343
inline SparseBitVectorIterator& operator++() {
337-
BitPos = Iter->find_next(BitPos);
338-
if (BitPos < 0 && ++Iter != End)
344+
if (Iter->find_next(BitPos) && ++Iter != End)
339345
BitPos = Iter->find_first();
340346
return *this;
341347
}
@@ -349,7 +355,7 @@ class SparseBitVector {
349355

350356
// Return the current set bit number.
351357
unsigned operator*() const {
352-
assert(BitPos >= 0);
358+
assert(BitPos != ~0U);
353359
return Iter->index() * ElementSize + BitPos;
354360
}
355361

0 commit comments

Comments
 (0)