Skip to content

Commit 046ddd4

Browse files
committed
efficient implementation of MachineOutliner::findCandidates()
1 parent 799316f commit 046ddd4

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

llvm/lib/CodeGen/MachineOutliner.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,11 @@ void MachineOutliner::findCandidates(
593593
unsigned NumDiscarded = 0;
594594
unsigned NumKept = 0;
595595
#endif
596-
for (const unsigned &StartIdx : RS.StartIndices) {
596+
// Sort the start indices so that we can efficiently check if candidates
597+
// overlap with each other in MachineOutliner::findCandidates().
598+
SmallVector<unsigned> SortedStartIndices(RS.StartIndices);
599+
llvm::sort(SortedStartIndices);
600+
for (const unsigned &StartIdx : SortedStartIndices) {
597601
// Trick: Discard some candidates that would be incompatible with the
598602
// ones we've already found for this sequence. This will save us some
599603
// work in candidate selection.
@@ -616,17 +620,15 @@ void MachineOutliner::findCandidates(
616620
// * End before the other starts
617621
// * Start after the other ends
618622
unsigned EndIdx = StartIdx + StringLen - 1;
619-
auto FirstOverlap = find_if(
620-
CandidatesForRepeatedSeq, [StartIdx, EndIdx](const Candidate &C) {
621-
return EndIdx >= C.getStartIdx() && StartIdx <= C.getEndIdx();
622-
});
623-
if (FirstOverlap != CandidatesForRepeatedSeq.end()) {
623+
if (CandidatesForRepeatedSeq.size() > 0 &&
624+
StartIdx <= CandidatesForRepeatedSeq.back().getEndIdx()) {
624625
#ifndef NDEBUG
625626
++NumDiscarded;
626-
LLVM_DEBUG(dbgs() << " .. DISCARD candidate @ [" << StartIdx
627-
<< ", " << EndIdx << "]; overlaps with candidate @ ["
628-
<< FirstOverlap->getStartIdx() << ", "
629-
<< FirstOverlap->getEndIdx() << "]\n");
627+
LLVM_DEBUG(dbgs() << " .. DISCARD candidate @ [" << StartIdx << ", "
628+
<< EndIdx << "]; overlaps with candidate @ ["
629+
<< CandidatesForRepeatedSeq.back().getStartIdx()
630+
<< ", " << CandidatesForRepeatedSeq.back().getEndIdx()
631+
<< "]\n");
630632
#endif
631633
continue;
632634
}

llvm/test/CodeGen/AArch64/machine-outliner-overlap.mir

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,27 @@
88
# CHECK-NEXT: Candidates discarded: 0
99
# CHECK-NEXT: Candidates kept: 2
1010
# CHECK-DAG: Sequence length: 8
11-
# CHECK-NEXT: .. DISCARD candidate @ [5, 12]; overlaps with candidate @ [12, 19]
11+
# CHECK-NEXT: .. DISCARD candidate @ [12, 19]; overlaps with candidate @ [5, 12]
1212
# CHECK-NEXT: Candidates discarded: 1
1313
# CHECK-NEXT: Candidates kept: 1
1414
# CHECK-DAG: Sequence length: 9
15-
# CHECK-NEXT: .. DISCARD candidate @ [4, 12]; overlaps with candidate @ [11, 19]
15+
# CHECK-NEXT: .. DISCARD candidate @ [11, 19]; overlaps with candidate @ [4, 12]
1616
# CHECK-NEXT: Candidates discarded: 1
1717
# CHECK-NEXT: Candidates kept: 1
1818
# CHECK-DAG: Sequence length: 10
19-
# CHECK-NEXT: .. DISCARD candidate @ [3, 12]; overlaps with candidate @ [10, 19]
19+
# CHECK-NEXT: .. DISCARD candidate @ [10, 19]; overlaps with candidate @ [3, 12]
2020
# CHECK-NEXT: Candidates discarded: 1
2121
# CHECK-NEXT: Candidates kept: 1
2222
# CHECK-DAG: Sequence length: 11
23-
# CHECK-NEXT: .. DISCARD candidate @ [2, 12]; overlaps with candidate @ [9, 19]
23+
# CHECK-NEXT: .. DISCARD candidate @ [9, 19]; overlaps with candidate @ [2, 12]
2424
# CHECK-NEXT: Candidates discarded: 1
2525
# CHECK-NEXT: Candidates kept: 1
2626
# CHECK-DAG: Sequence length: 12
27-
# CHECK-NEXT: .. DISCARD candidate @ [1, 12]; overlaps with candidate @ [8, 19]
27+
# CHECK-NEXT: .. DISCARD candidate @ [8, 19]; overlaps with candidate @ [1, 12]
2828
# CHECK-NEXT: Candidates discarded: 1
2929
# CHECK-NEXT: Candidates kept: 1
3030
# CHECK-DAG: Sequence length: 13
31-
# CHECK-NEXT: .. DISCARD candidate @ [0, 12]; overlaps with candidate @ [7, 19]
31+
# CHECK-NEXT: .. DISCARD candidate @ [7, 19]; overlaps with candidate @ [0, 12]
3232
# CHECK-NEXT: Candidates discarded: 1
3333
# CHECK-NEXT: Candidates kept: 1
3434

0 commit comments

Comments
 (0)