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