WebAssemblySortRegion.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //===-- WebAssemblySortRegion.h - WebAssembly Sort SortRegion ----*- 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 regions used in CFGSort and CFGStackify.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYSORTREGION_H
  14. #define LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYSORTREGION_H
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/iterator_range.h"
  18. namespace llvm {
  19. class MachineBasicBlock;
  20. class MachineLoop;
  21. class MachineLoopInfo;
  22. class WebAssemblyException;
  23. class WebAssemblyExceptionInfo;
  24. namespace WebAssembly {
  25. // Wrapper for loops and exceptions
  26. class SortRegion {
  27. public:
  28. virtual ~SortRegion() = default;
  29. virtual MachineBasicBlock *getHeader() const = 0;
  30. virtual bool contains(const MachineBasicBlock *MBB) const = 0;
  31. virtual unsigned getNumBlocks() const = 0;
  32. using block_iterator = typename ArrayRef<MachineBasicBlock *>::const_iterator;
  33. virtual iterator_range<block_iterator> blocks() const = 0;
  34. virtual bool isLoop() const = 0;
  35. };
  36. template <typename T> class ConcreteSortRegion : public SortRegion {
  37. const T *Unit;
  38. public:
  39. ConcreteSortRegion(const T *Unit) : Unit(Unit) {}
  40. MachineBasicBlock *getHeader() const override { return Unit->getHeader(); }
  41. bool contains(const MachineBasicBlock *MBB) const override {
  42. return Unit->contains(MBB);
  43. }
  44. unsigned getNumBlocks() const override { return Unit->getNumBlocks(); }
  45. iterator_range<block_iterator> blocks() const override {
  46. return Unit->blocks();
  47. }
  48. bool isLoop() const override { return false; }
  49. };
  50. // This class has information of nested SortRegions; this is analogous to what
  51. // LoopInfo is for loops.
  52. class SortRegionInfo {
  53. friend class ConcreteSortRegion<MachineLoopInfo>;
  54. friend class ConcreteSortRegion<WebAssemblyException>;
  55. const MachineLoopInfo &MLI;
  56. const WebAssemblyExceptionInfo &WEI;
  57. DenseMap<const MachineLoop *, std::unique_ptr<SortRegion>> LoopMap;
  58. DenseMap<const WebAssemblyException *, std::unique_ptr<SortRegion>>
  59. ExceptionMap;
  60. public:
  61. SortRegionInfo(const MachineLoopInfo &MLI,
  62. const WebAssemblyExceptionInfo &WEI)
  63. : MLI(MLI), WEI(WEI) {}
  64. // Returns a smallest loop or exception that contains MBB
  65. const SortRegion *getRegionFor(const MachineBasicBlock *MBB);
  66. // Return the "bottom" block among all blocks dominated by the region
  67. // (MachineLoop or WebAssemblyException) header. This works when the entity is
  68. // discontiguous.
  69. MachineBasicBlock *getBottom(const SortRegion *R);
  70. MachineBasicBlock *getBottom(const MachineLoop *ML);
  71. MachineBasicBlock *getBottom(const WebAssemblyException *WE);
  72. };
  73. } // end namespace WebAssembly
  74. } // end namespace llvm
  75. #endif