Skip to content

Commit d2a3dbe

Browse files
Adding bidirectional iterator support to const_set_bits_iterator_impl
1 parent a71642f commit d2a3dbe

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

llvm/include/llvm/ADT/BitVector.h

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,25 @@ template <typename BitVectorT> class const_set_bits_iterator_impl {
4040
Current = Parent.find_next(Current);
4141
}
4242

43+
void retreat() {
44+
// For bidirectional iteration to work with reverse_iterator,
45+
// we need to handle the case where Current might be at end (-1)
46+
// or at a position where we need to find the previous set bit.
47+
if (Current == -1) {
48+
// If we're at the end, go to the last set bit
49+
Current = Parent.find_last();
50+
} else {
51+
// Otherwise find the previous set bit before Current
52+
Current = Parent.find_prev(Current);
53+
}
54+
}
55+
4356
public:
44-
using iterator_category = std::forward_iterator_tag;
57+
using iterator_category = std::bidirectional_iterator_tag;
4558
using difference_type = std::ptrdiff_t;
46-
using value_type = int;
47-
using pointer = value_type*;
48-
using reference = value_type&;
59+
using value_type = unsigned;
60+
using pointer = const value_type*;
61+
using reference = value_type;
4962

5063
const_set_bits_iterator_impl(const BitVectorT &Parent, int Current)
5164
: Parent(Parent), Current(Current) {}
@@ -64,6 +77,17 @@ template <typename BitVectorT> class const_set_bits_iterator_impl {
6477
return *this;
6578
}
6679

80+
const_set_bits_iterator_impl operator--(int) {
81+
auto Prev = *this;
82+
retreat();
83+
return Prev;
84+
}
85+
86+
const_set_bits_iterator_impl &operator--() {
87+
retreat();
88+
return *this;
89+
}
90+
6791
unsigned operator*() const { return Current; }
6892

6993
bool operator==(const const_set_bits_iterator_impl &Other) const {

mlir/lib/Transforms/RemoveDeadValues.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,8 @@ static void cleanUpDeadVals(RDVFinalCleanupList &list) {
772772
LDBG() << "Cleaning up " << list.operands.size() << " operand lists";
773773
for (OperationToCleanup &o : list.operands) {
774774
// Handle call-specific cleanup only when we have a cached callee reference.
775-
// This avoids expensive symbol lookup and is defensive against future changes.
775+
// This avoids expensive symbol lookup and is defensive against future
776+
// changes.
776777
bool handledAsCall = false;
777778
if (o.callee && isa<CallOpInterface>(o.op)) {
778779
auto call = cast<CallOpInterface>(o.op);
@@ -781,11 +782,9 @@ static void cleanUpDeadVals(RDVFinalCleanupList &list) {
781782
const BitVector &deadArgIdxs = it->second;
782783
MutableOperandRange args = call.getArgOperandsMutable();
783784
// First, erase the call arguments corresponding to erased callee
784-
// args.
785-
for (int i = static_cast<int>(args.size()) - 1; i >= 0; --i) {
786-
if (i < static_cast<int>(deadArgIdxs.size()) && deadArgIdxs.test(i))
787-
args.erase(i);
788-
}
785+
// args. We iterate backwards to preserve indices.
786+
for (unsigned argIdx : llvm::reverse(deadArgIdxs.set_bits()))
787+
args.erase(argIdx);
789788
// If this operand cleanup entry also has a generic nonLive bitvector,
790789
// clear bits for call arguments we already erased above to avoid
791790
// double-erasing (which could impact other segments of ops with

0 commit comments

Comments
 (0)