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