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