WebAssemblySortRegion.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "WebAssemblySortRegion.h"
  2. #include "WebAssemblyExceptionInfo.h"
  3. #include "llvm/CodeGen/MachineLoopInfo.h"
  4. using namespace llvm;
  5. using namespace WebAssembly;
  6. namespace llvm {
  7. namespace WebAssembly {
  8. template <>
  9. bool ConcreteSortRegion<MachineLoop>::isLoop() const {
  10. return true;
  11. }
  12. } // end namespace WebAssembly
  13. } // end namespace llvm
  14. const SortRegion *SortRegionInfo::getRegionFor(const MachineBasicBlock *MBB) {
  15. const auto *ML = MLI.getLoopFor(MBB);
  16. const auto *WE = WEI.getExceptionFor(MBB);
  17. if (!ML && !WE)
  18. return nullptr;
  19. // We determine subregion relationship by domination of their headers, i.e.,
  20. // if region A's header dominates region B's header, B is a subregion of A.
  21. // WebAssemblyException contains BBs in all its subregions (loops or
  22. // exceptions), but MachineLoop may not, because MachineLoop does not
  23. // contain BBs that don't have a path to its header even if they are
  24. // dominated by its header. So here we should use
  25. // WE->contains(ML->getHeader()), but not ML->contains(WE->getHeader()).
  26. if ((ML && !WE) || (ML && WE && WE->contains(ML->getHeader()))) {
  27. // If the smallest region containing MBB is a loop
  28. if (LoopMap.count(ML))
  29. return LoopMap[ML].get();
  30. LoopMap[ML] = std::make_unique<ConcreteSortRegion<MachineLoop>>(ML);
  31. return LoopMap[ML].get();
  32. } else {
  33. // If the smallest region containing MBB is an exception
  34. if (ExceptionMap.count(WE))
  35. return ExceptionMap[WE].get();
  36. ExceptionMap[WE] =
  37. std::make_unique<ConcreteSortRegion<WebAssemblyException>>(WE);
  38. return ExceptionMap[WE].get();
  39. }
  40. }
  41. MachineBasicBlock *SortRegionInfo::getBottom(const SortRegion *R) {
  42. if (R->isLoop())
  43. return getBottom(MLI.getLoopFor(R->getHeader()));
  44. else
  45. return getBottom(WEI.getExceptionFor(R->getHeader()));
  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. MachineBasicBlock *SortRegionInfo::getBottom(const WebAssemblyException *WE) {
  67. MachineBasicBlock *Bottom = WE->getHeader();
  68. for (MachineBasicBlock *MBB : WE->blocks())
  69. if (MBB->getNumber() > Bottom->getNumber())
  70. Bottom = MBB;
  71. return Bottom;
  72. }