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