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