1 #include "WebAssemblySortRegion.h"
2 #include "WebAssemblyExceptionInfo.h"
3 #include "llvm/CodeGen/MachineLoopInfo.h"
6 using namespace WebAssembly
;
9 namespace WebAssembly
{
11 bool ConcreteSortRegion
<MachineLoop
>::isLoop() const {
14 } // end namespace WebAssembly
15 } // end namespace llvm
17 const SortRegion
*SortRegionInfo::getRegionFor(const MachineBasicBlock
*MBB
) {
18 const auto *ML
= MLI
.getLoopFor(MBB
);
19 const auto *WE
= WEI
.getExceptionFor(MBB
);
22 // We determine subregion relationship by domination of their headers, i.e.,
23 // if region A's header dominates region B's header, B is a subregion of A.
24 // WebAssemblyException contains BBs in all its subregions (loops or
25 // exceptions), but MachineLoop may not, because MachineLoop does not
26 // contain BBs that don't have a path to its header even if they are
27 // dominated by its header. So here we should use
28 // WE->contains(ML->getHeader()), but not ML->contains(WE->getHeader()).
29 if ((ML
&& !WE
) || (ML
&& WE
&& WE
->contains(ML
->getHeader()))) {
30 // If the smallest region containing MBB is a loop
31 if (LoopMap
.count(ML
))
32 return LoopMap
[ML
].get();
33 LoopMap
[ML
] = std::make_unique
<ConcreteSortRegion
<MachineLoop
>>(ML
);
34 return LoopMap
[ML
].get();
36 // If the smallest region containing MBB is an exception
37 if (ExceptionMap
.count(WE
))
38 return ExceptionMap
[WE
].get();
40 std::make_unique
<ConcreteSortRegion
<WebAssemblyException
>>(WE
);
41 return ExceptionMap
[WE
].get();
45 MachineBasicBlock
*SortRegionInfo::getBottom(const SortRegion
*R
) {
47 return getBottom(MLI
.getLoopFor(R
->getHeader()));
49 return getBottom(WEI
.getExceptionFor(R
->getHeader()));
52 MachineBasicBlock
*SortRegionInfo::getBottom(const MachineLoop
*ML
) {
53 MachineBasicBlock
*Bottom
= ML
->getHeader();
54 for (MachineBasicBlock
*MBB
: ML
->blocks()) {
55 if (MBB
->getNumber() > Bottom
->getNumber())
57 // MachineLoop does not contain all BBs dominated by its header. BBs that
58 // don't have a path back to the loop header aren't included. But for the
59 // purpose of CFG sorting and stackification, we need a bottom BB among all
60 // BBs that are dominated by the loop header. So we check if there is any
61 // WebAssemblyException contained in this loop, and computes the most bottom
64 MachineBasicBlock
*ExBottom
= getBottom(WEI
.getExceptionFor(MBB
));
65 if (ExBottom
->getNumber() > Bottom
->getNumber())
72 MachineBasicBlock
*SortRegionInfo::getBottom(const WebAssemblyException
*WE
) {
73 MachineBasicBlock
*Bottom
= WE
->getHeader();
74 for (MachineBasicBlock
*MBB
: WE
->blocks())
75 if (MBB
->getNumber() > Bottom
->getNumber())