Skip to content

Commit 7ddf6e9

Browse files
authored
[SlotIndexes] Use upper/lower bound terminology for MBB searches. NFC. (#68802)
Rename advanceMBBIndex and findMBBIndex to getMBBLowerBound and add getMBBUpperBound. The motivations are: - Make it clear what kind of search is being done, using names inspired by std::upper/lower_bound. - Simplify getMBBFromIndex which really wants an upper bound search and previously had to work hard to get the result it wanted from a lower bound search.
1 parent c0f453c commit 7ddf6e9

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

llvm/include/llvm/CodeGen/SlotIndexes.h

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -472,18 +472,25 @@ class raw_ostream;
472472
/// begin and basic block)
473473
using MBBIndexIterator = SmallVectorImpl<IdxMBBPair>::const_iterator;
474474

475-
/// Move iterator to the next IdxMBBPair where the SlotIndex is greater or
476-
/// equal to \p To.
477-
MBBIndexIterator advanceMBBIndex(MBBIndexIterator I, SlotIndex To) const {
478-
return std::partition_point(
479-
I, idx2MBBMap.end(),
480-
[=](const IdxMBBPair &IM) { return IM.first < To; });
475+
/// Get an iterator pointing to the first IdxMBBPair with SlotIndex greater
476+
/// than or equal to \p Idx. If \p Start is provided, only search the range
477+
/// from \p Start to the end of the function.
478+
MBBIndexIterator getMBBLowerBound(MBBIndexIterator Start,
479+
SlotIndex Idx) const {
480+
return std::lower_bound(
481+
Start, MBBIndexEnd(), Idx,
482+
[](const IdxMBBPair &IM, SlotIndex Idx) { return IM.first < Idx; });
483+
}
484+
MBBIndexIterator getMBBLowerBound(SlotIndex Idx) const {
485+
return getMBBLowerBound(MBBIndexBegin(), Idx);
481486
}
482487

483-
/// Get an iterator pointing to the IdxMBBPair with the biggest SlotIndex
484-
/// that is greater or equal to \p Idx.
485-
MBBIndexIterator findMBBIndex(SlotIndex Idx) const {
486-
return advanceMBBIndex(idx2MBBMap.begin(), Idx);
488+
/// Get an iterator pointing to the first IdxMBBPair with SlotIndex greater
489+
/// than \p Idx.
490+
MBBIndexIterator getMBBUpperBound(SlotIndex Idx) const {
491+
return std::upper_bound(
492+
MBBIndexBegin(), MBBIndexEnd(), Idx,
493+
[](SlotIndex Idx, const IdxMBBPair &IM) { return Idx < IM.first; });
487494
}
488495

489496
/// Returns an iterator for the begin of the idx2MBBMap.
@@ -501,16 +508,11 @@ class raw_ostream;
501508
if (MachineInstr *MI = getInstructionFromIndex(index))
502509
return MI->getParent();
503510

504-
MBBIndexIterator I = findMBBIndex(index);
505-
// Take the pair containing the index
506-
MBBIndexIterator J =
507-
((I != MBBIndexEnd() && I->first > index) ||
508-
(I == MBBIndexEnd() && !idx2MBBMap.empty())) ? std::prev(I) : I;
509-
510-
assert(J != MBBIndexEnd() && J->first <= index &&
511-
index < getMBBEndIdx(J->second) &&
511+
MBBIndexIterator I = std::prev(getMBBUpperBound(index));
512+
assert(I != MBBIndexEnd() && I->first <= index &&
513+
index < getMBBEndIdx(I->second) &&
512514
"index does not correspond to an MBB");
513-
return J->second;
515+
return I->second;
514516
}
515517

516518
/// Insert the given machine instruction into the mapping. Returns the

llvm/lib/CodeGen/VirtRegMap.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ void VirtRegRewriter::addLiveInsForSubRanges(const LiveInterval &LI,
312312

313313
// Check all mbb start positions between First and Last while
314314
// simultaneously advancing an iterator for each subrange.
315-
for (SlotIndexes::MBBIndexIterator MBBI = Indexes->findMBBIndex(First);
315+
for (SlotIndexes::MBBIndexIterator MBBI = Indexes->getMBBLowerBound(First);
316316
MBBI != Indexes->MBBIndexEnd() && MBBI->first <= Last; ++MBBI) {
317317
SlotIndex MBBBegin = MBBI->first;
318318
// Advance all subrange iterators so that their end position is just
@@ -363,7 +363,7 @@ void VirtRegRewriter::addMBBLiveIns() {
363363
// sorted by slot indexes.
364364
SlotIndexes::MBBIndexIterator I = Indexes->MBBIndexBegin();
365365
for (const auto &Seg : LI) {
366-
I = Indexes->advanceMBBIndex(I, Seg.start);
366+
I = Indexes->getMBBLowerBound(I, Seg.start);
367367
for (; I != Indexes->MBBIndexEnd() && I->first < Seg.end; ++I) {
368368
MachineBasicBlock *MBB = I->second;
369369
MBB->addLiveIn(PhysReg);

0 commit comments

Comments
 (0)