WebAssemblyExceptionInfo.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //===-- WebAssemblyExceptionInfo.h - WebAssembly Exception Info -*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// \file
  10. /// \brief This file implements WebAssemblyException information analysis.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYEXCEPTIONINFO_H
  14. #define LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYEXCEPTIONINFO_H
  15. #include "WebAssembly.h"
  16. #include "llvm/CodeGen/MachineFunctionPass.h"
  17. namespace llvm {
  18. class MachineDominatorTree;
  19. class MachineDominanceFrontier;
  20. // WebAssembly instructions for exception handling are structured as follows:
  21. // try
  22. // instructions*
  23. // catch ----|
  24. // instructions* | -> A WebAssemblyException consists of this region
  25. // end ----|
  26. //
  27. // A WebAssemblyException object contains BBs that belong to a 'catch' part of
  28. // the try-catch-end structure to be created later. 'try' and 'end' markers
  29. // are not present at this stage and will be generated in CFGStackify pass.
  30. // Because CFGSort requires all the BBs within a catch part to be sorted
  31. // together as it does for loops, this pass calculates the nesting structure of
  32. // catch part of exceptions in a function.
  33. //
  34. // An exception catch part is defined as a BB with catch instruction and all
  35. // other BBs dominated by this BB.
  36. class WebAssemblyException {
  37. MachineBasicBlock *EHPad = nullptr;
  38. WebAssemblyException *ParentException = nullptr;
  39. std::vector<std::unique_ptr<WebAssemblyException>> SubExceptions;
  40. std::vector<MachineBasicBlock *> Blocks;
  41. SmallPtrSet<MachineBasicBlock *, 8> BlockSet;
  42. public:
  43. WebAssemblyException(MachineBasicBlock *EHPad) : EHPad(EHPad) {}
  44. WebAssemblyException(const WebAssemblyException &) = delete;
  45. const WebAssemblyException &operator=(const WebAssemblyException &) = delete;
  46. MachineBasicBlock *getEHPad() const { return EHPad; }
  47. MachineBasicBlock *getHeader() const { return EHPad; }
  48. WebAssemblyException *getParentException() const { return ParentException; }
  49. void setParentException(WebAssemblyException *WE) { ParentException = WE; }
  50. bool contains(const WebAssemblyException *WE) const {
  51. if (WE == this)
  52. return true;
  53. if (!WE)
  54. return false;
  55. return contains(WE->getParentException());
  56. }
  57. bool contains(const MachineBasicBlock *MBB) const {
  58. return BlockSet.count(MBB);
  59. }
  60. void addToBlocksSet(MachineBasicBlock *MBB) { BlockSet.insert(MBB); }
  61. void removeFromBlocksSet(MachineBasicBlock *MBB) { BlockSet.erase(MBB); }
  62. void addToBlocksVector(MachineBasicBlock *MBB) { Blocks.push_back(MBB); }
  63. void addBlock(MachineBasicBlock *MBB) {
  64. Blocks.push_back(MBB);
  65. BlockSet.insert(MBB);
  66. }
  67. ArrayRef<MachineBasicBlock *> getBlocks() const { return Blocks; }
  68. using block_iterator = typename ArrayRef<MachineBasicBlock *>::const_iterator;
  69. block_iterator block_begin() const { return getBlocks().begin(); }
  70. block_iterator block_end() const { return getBlocks().end(); }
  71. inline iterator_range<block_iterator> blocks() const {
  72. return make_range(block_begin(), block_end());
  73. }
  74. unsigned getNumBlocks() const { return Blocks.size(); }
  75. std::vector<MachineBasicBlock *> &getBlocksVector() { return Blocks; }
  76. SmallPtrSetImpl<MachineBasicBlock *> &getBlocksSet() { return BlockSet; }
  77. const std::vector<std::unique_ptr<WebAssemblyException>> &
  78. getSubExceptions() const {
  79. return SubExceptions;
  80. }
  81. std::vector<std::unique_ptr<WebAssemblyException>> &getSubExceptions() {
  82. return SubExceptions;
  83. }
  84. void addSubException(std::unique_ptr<WebAssemblyException> E) {
  85. SubExceptions.push_back(std::move(E));
  86. }
  87. using iterator = typename decltype(SubExceptions)::const_iterator;
  88. iterator begin() const { return SubExceptions.begin(); }
  89. iterator end() const { return SubExceptions.end(); }
  90. void reserveBlocks(unsigned Size) { Blocks.reserve(Size); }
  91. void reverseBlock(unsigned From = 0) {
  92. std::reverse(Blocks.begin() + From, Blocks.end());
  93. }
  94. // Return the nesting level. An outermost one has depth 1.
  95. unsigned getExceptionDepth() const {
  96. unsigned D = 1;
  97. for (const WebAssemblyException *CurException = ParentException;
  98. CurException; CurException = CurException->ParentException)
  99. ++D;
  100. return D;
  101. }
  102. void print(raw_ostream &OS, unsigned Depth = 0) const;
  103. void dump() const;
  104. };
  105. raw_ostream &operator<<(raw_ostream &OS, const WebAssemblyException &WE);
  106. class WebAssemblyExceptionInfo final : public MachineFunctionPass {
  107. // Mapping of basic blocks to the innermost exception they occur in
  108. DenseMap<const MachineBasicBlock *, WebAssemblyException *> BBMap;
  109. std::vector<std::unique_ptr<WebAssemblyException>> TopLevelExceptions;
  110. void discoverAndMapException(WebAssemblyException *WE,
  111. const MachineDominatorTree &MDT,
  112. const MachineDominanceFrontier &MDF);
  113. WebAssemblyException *getOutermostException(MachineBasicBlock *MBB) const;
  114. public:
  115. static char ID;
  116. WebAssemblyExceptionInfo() : MachineFunctionPass(ID) {
  117. initializeWebAssemblyExceptionInfoPass(*PassRegistry::getPassRegistry());
  118. }
  119. ~WebAssemblyExceptionInfo() override { releaseMemory(); }
  120. WebAssemblyExceptionInfo(const WebAssemblyExceptionInfo &) = delete;
  121. WebAssemblyExceptionInfo &
  122. operator=(const WebAssemblyExceptionInfo &) = delete;
  123. bool runOnMachineFunction(MachineFunction &) override;
  124. void releaseMemory() override;
  125. void recalculate(MachineFunction &MF, MachineDominatorTree &MDT,
  126. const MachineDominanceFrontier &MDF);
  127. void getAnalysisUsage(AnalysisUsage &AU) const override;
  128. bool empty() const { return TopLevelExceptions.empty(); }
  129. // Return the innermost exception that MBB lives in. If the block is not in an
  130. // exception, null is returned.
  131. WebAssemblyException *getExceptionFor(const MachineBasicBlock *MBB) const {
  132. return BBMap.lookup(MBB);
  133. }
  134. void changeExceptionFor(const MachineBasicBlock *MBB,
  135. WebAssemblyException *WE) {
  136. if (!WE) {
  137. BBMap.erase(MBB);
  138. return;
  139. }
  140. BBMap[MBB] = WE;
  141. }
  142. void addTopLevelException(std::unique_ptr<WebAssemblyException> WE) {
  143. assert(!WE->getParentException() && "Not a top level exception!");
  144. TopLevelExceptions.push_back(std::move(WE));
  145. }
  146. void print(raw_ostream &OS, const Module *M = nullptr) const override;
  147. };
  148. } // end namespace llvm
  149. #endif