xref: /llvm-project/llvm/lib/Target/WebAssembly/WebAssemblySortRegion.cpp (revision 3632f765dc6a95a903864585056d1caa9185bd00)
1276f9e8cSHeejin Ahn #include "WebAssemblySortRegion.h"
2276f9e8cSHeejin Ahn #include "WebAssemblyExceptionInfo.h"
3276f9e8cSHeejin Ahn #include "llvm/CodeGen/MachineLoopInfo.h"
4276f9e8cSHeejin Ahn 
5276f9e8cSHeejin Ahn using namespace llvm;
6276f9e8cSHeejin Ahn using namespace WebAssembly;
7276f9e8cSHeejin Ahn 
8*3632f765SCraig Topper namespace llvm {
9*3632f765SCraig Topper namespace WebAssembly {
10b7292f2dSHans Wennborg template <>
isLoop() const11*3632f765SCraig Topper bool ConcreteSortRegion<MachineLoop>::isLoop() const {
12276f9e8cSHeejin Ahn   return true;
13276f9e8cSHeejin Ahn }
14*3632f765SCraig Topper } // end namespace WebAssembly
15*3632f765SCraig Topper } // end namespace llvm
16276f9e8cSHeejin Ahn 
getRegionFor(const MachineBasicBlock * MBB)17276f9e8cSHeejin Ahn const SortRegion *SortRegionInfo::getRegionFor(const MachineBasicBlock *MBB) {
18276f9e8cSHeejin Ahn   const auto *ML = MLI.getLoopFor(MBB);
19276f9e8cSHeejin Ahn   const auto *WE = WEI.getExceptionFor(MBB);
20276f9e8cSHeejin Ahn   if (!ML && !WE)
21276f9e8cSHeejin Ahn     return nullptr;
22276f9e8cSHeejin Ahn   // We determine subregion relationship by domination of their headers, i.e.,
23276f9e8cSHeejin Ahn   // if region A's header dominates region B's header, B is a subregion of A.
24276f9e8cSHeejin Ahn   // WebAssemblyException contains BBs in all its subregions (loops or
25276f9e8cSHeejin Ahn   // exceptions), but MachineLoop may not, because MachineLoop does not
26276f9e8cSHeejin Ahn   // contain BBs that don't have a path to its header even if they are
27276f9e8cSHeejin Ahn   // dominated by its header. So here we should use
28276f9e8cSHeejin Ahn   // WE->contains(ML->getHeader()), but not ML->contains(WE->getHeader()).
29276f9e8cSHeejin Ahn   if ((ML && !WE) || (ML && WE && WE->contains(ML->getHeader()))) {
30276f9e8cSHeejin Ahn     // If the smallest region containing MBB is a loop
31276f9e8cSHeejin Ahn     if (LoopMap.count(ML))
32276f9e8cSHeejin Ahn       return LoopMap[ML].get();
33276f9e8cSHeejin Ahn     LoopMap[ML] = std::make_unique<ConcreteSortRegion<MachineLoop>>(ML);
34276f9e8cSHeejin Ahn     return LoopMap[ML].get();
35276f9e8cSHeejin Ahn   } else {
36276f9e8cSHeejin Ahn     // If the smallest region containing MBB is an exception
37276f9e8cSHeejin Ahn     if (ExceptionMap.count(WE))
38276f9e8cSHeejin Ahn       return ExceptionMap[WE].get();
39276f9e8cSHeejin Ahn     ExceptionMap[WE] =
40276f9e8cSHeejin Ahn         std::make_unique<ConcreteSortRegion<WebAssemblyException>>(WE);
41276f9e8cSHeejin Ahn     return ExceptionMap[WE].get();
42276f9e8cSHeejin Ahn   }
43276f9e8cSHeejin Ahn }
44276f9e8cSHeejin Ahn 
getBottom(const SortRegion * R)45276f9e8cSHeejin Ahn MachineBasicBlock *SortRegionInfo::getBottom(const SortRegion *R) {
46276f9e8cSHeejin Ahn   if (R->isLoop())
47276f9e8cSHeejin Ahn     return getBottom(MLI.getLoopFor(R->getHeader()));
48276f9e8cSHeejin Ahn   else
49276f9e8cSHeejin Ahn     return getBottom(WEI.getExceptionFor(R->getHeader()));
50276f9e8cSHeejin Ahn }
51276f9e8cSHeejin Ahn 
getBottom(const MachineLoop * ML)52276f9e8cSHeejin Ahn MachineBasicBlock *SortRegionInfo::getBottom(const MachineLoop *ML) {
53276f9e8cSHeejin Ahn   MachineBasicBlock *Bottom = ML->getHeader();
54276f9e8cSHeejin Ahn   for (MachineBasicBlock *MBB : ML->blocks()) {
55276f9e8cSHeejin Ahn     if (MBB->getNumber() > Bottom->getNumber())
56276f9e8cSHeejin Ahn       Bottom = MBB;
57276f9e8cSHeejin Ahn     // MachineLoop does not contain all BBs dominated by its header. BBs that
58276f9e8cSHeejin Ahn     // don't have a path back to the loop header aren't included. But for the
59276f9e8cSHeejin Ahn     // purpose of CFG sorting and stackification, we need a bottom BB among all
60276f9e8cSHeejin Ahn     // BBs that are dominated by the loop header. So we check if there is any
61276f9e8cSHeejin Ahn     // WebAssemblyException contained in this loop, and computes the most bottom
62276f9e8cSHeejin Ahn     // BB of them all.
63276f9e8cSHeejin Ahn     if (MBB->isEHPad()) {
64276f9e8cSHeejin Ahn       MachineBasicBlock *ExBottom = getBottom(WEI.getExceptionFor(MBB));
65276f9e8cSHeejin Ahn       if (ExBottom->getNumber() > Bottom->getNumber())
66276f9e8cSHeejin Ahn         Bottom = ExBottom;
67276f9e8cSHeejin Ahn     }
68276f9e8cSHeejin Ahn   }
69276f9e8cSHeejin Ahn   return Bottom;
70276f9e8cSHeejin Ahn }
71276f9e8cSHeejin Ahn 
getBottom(const WebAssemblyException * WE)72276f9e8cSHeejin Ahn MachineBasicBlock *SortRegionInfo::getBottom(const WebAssemblyException *WE) {
73276f9e8cSHeejin Ahn   MachineBasicBlock *Bottom = WE->getHeader();
74276f9e8cSHeejin Ahn   for (MachineBasicBlock *MBB : WE->blocks())
75276f9e8cSHeejin Ahn     if (MBB->getNumber() > Bottom->getNumber())
76276f9e8cSHeejin Ahn       Bottom = MBB;
77276f9e8cSHeejin Ahn   return Bottom;
78276f9e8cSHeejin Ahn }
79