Skip to content

Commit 9966fe8

Browse files
committed
[WinEH] Moved funclet pads should be in relative order
We shifted the MachineBasicBlocks to the end of the MachineFunction in DFS order. This will not ensure that MachineBasicBlocks which fell through to one another will remain contiguous. Instead, implement a stable sort algorithm for iplist. This partially reverts commit r214150. llvm-svn: 247978
1 parent 36b6c39 commit 9966fe8

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

llvm/include/llvm/ADT/ilist.h

+47
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,53 @@ class iplist : public Traits {
579579
void splice(iterator where, iplist &L2, iterator first, iterator last) {
580580
if (first != last) transfer(where, L2, first, last);
581581
}
582+
583+
template <class Compare>
584+
void merge(iplist &Right, Compare comp) {
585+
if (this == &Right)
586+
return;
587+
iterator First1 = begin(), Last1 = end();
588+
iterator First2 = Right.begin(), Last2 = Right.end();
589+
while (First1 != Last1 && First2 != Last2) {
590+
if (comp(*First2, *First1)) {
591+
iterator Next = First2;
592+
transfer(First1, Right, First2, ++Next);
593+
First2 = Next;
594+
} else {
595+
++First1;
596+
}
597+
}
598+
if (First2 != Last2)
599+
transfer(Last1, Right, First2, Last2);
600+
}
601+
void merge(iplist &Right) { return merge(Right, op_less); }
602+
603+
template <class Compare>
604+
void sort(Compare comp) {
605+
// The list is empty, vacuously sorted.
606+
if (empty())
607+
return;
608+
// The list has a single element, vacuously sorted.
609+
if (std::next(begin()) == end())
610+
return;
611+
// Find the split point for the list.
612+
iterator Center = begin(), End = begin();
613+
while (End != end() && std::next(End) != end()) {
614+
Center = std::next(Center);
615+
End = std::next(std::next(End));
616+
}
617+
// Split the list into two.
618+
iplist RightHalf;
619+
RightHalf.splice(RightHalf.begin(), *this, Center, end());
620+
621+
// Sort the two sublists.
622+
sort(comp);
623+
RightHalf.sort(comp);
624+
625+
// Merge the two sublists back together.
626+
merge(RightHalf, comp);
627+
}
628+
void sort() { sort(op_less); }
582629
};
583630

584631

llvm/include/llvm/CodeGen/MachineFunction.h

+5
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,11 @@ class MachineFunction {
375375
BasicBlocks.erase(MBBI);
376376
}
377377

378+
template <typename Comp>
379+
void sort(Comp comp) {
380+
BasicBlocks.sort(comp);
381+
}
382+
378383
//===--------------------------------------------------------------------===//
379384
// Internal functions used to automatically number MachineBasicBlocks
380385
//

llvm/lib/CodeGen/FuncletLayout.cpp

+5-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414
#include "llvm/CodeGen/Passes.h"
15-
#include "llvm/ADT/MapVector.h"
1615
#include "llvm/CodeGen/MachineBasicBlock.h"
1716
#include "llvm/CodeGen/MachineFunction.h"
1817
#include "llvm/CodeGen/MachineFunctionPass.h"
@@ -34,7 +33,7 @@ class FuncletLayout : public MachineFunctionPass {
3433
}
3534

3635
static void
37-
collectFuncletMembers(MapVector<MachineBasicBlock *, int> &FuncletMembership,
36+
collectFuncletMembers(DenseMap<MachineBasicBlock *, int> &FuncletMembership,
3837
int Funclet, MachineBasicBlock *MBB) {
3938
// Don't revisit blocks.
4039
if (FuncletMembership.count(MBB) > 0)
@@ -81,16 +80,13 @@ bool FuncletLayout::runOnMachineFunction(MachineFunction &F) {
8180
if (FuncletBlocks.empty())
8281
return false;
8382

84-
MapVector<MachineBasicBlock *, int> FuncletMembership;
83+
DenseMap<MachineBasicBlock *, int> FuncletMembership;
8584
for (MachineBasicBlock *MBB : FuncletBlocks)
8685
collectFuncletMembers(FuncletMembership, MBB->getNumber(), MBB);
8786

88-
for (std::pair<llvm::MachineBasicBlock *, int> &FuncletMember :
89-
FuncletMembership) {
90-
// Move this block to the end of the function.
91-
MachineBasicBlock *MBB = FuncletMember.first;
92-
MBB->moveAfter(--F.end());
93-
}
87+
F.sort([&](MachineBasicBlock &x, MachineBasicBlock &y) {
88+
return FuncletMembership[&x] < FuncletMembership[&y];
89+
});
9490

9591
// Conservatively assume we changed something.
9692
return true;

0 commit comments

Comments
 (0)