MachineRegionInfo.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/CodeGen/MachineRegionInfo.h -------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CODEGEN_MACHINEREGIONINFO_H
  14. #define LLVM_CODEGEN_MACHINEREGIONINFO_H
  15. #include "llvm/ADT/DepthFirstIterator.h"
  16. #include "llvm/Analysis/RegionInfo.h"
  17. #include "llvm/Analysis/RegionIterator.h"
  18. #include "llvm/CodeGen/MachineBasicBlock.h"
  19. #include "llvm/CodeGen/MachineDominanceFrontier.h"
  20. #include "llvm/CodeGen/MachineDominators.h"
  21. #include "llvm/CodeGen/MachineFunction.h"
  22. #include "llvm/CodeGen/MachineFunctionPass.h"
  23. #include "llvm/CodeGen/MachineLoopInfo.h"
  24. #include <cassert>
  25. namespace llvm {
  26. class MachinePostDominatorTree;
  27. class MachineRegion;
  28. class MachineRegionNode;
  29. class MachineRegionInfo;
  30. template <> struct RegionTraits<MachineFunction> {
  31. using FuncT = MachineFunction;
  32. using BlockT = MachineBasicBlock;
  33. using RegionT = MachineRegion;
  34. using RegionNodeT = MachineRegionNode;
  35. using RegionInfoT = MachineRegionInfo;
  36. using DomTreeT = MachineDominatorTree;
  37. using DomTreeNodeT = MachineDomTreeNode;
  38. using PostDomTreeT = MachinePostDominatorTree;
  39. using DomFrontierT = MachineDominanceFrontier;
  40. using InstT = MachineInstr;
  41. using LoopT = MachineLoop;
  42. using LoopInfoT = MachineLoopInfo;
  43. static unsigned getNumSuccessors(MachineBasicBlock *BB) {
  44. return BB->succ_size();
  45. }
  46. };
  47. class MachineRegionNode : public RegionNodeBase<RegionTraits<MachineFunction>> {
  48. public:
  49. inline MachineRegionNode(MachineRegion *Parent, MachineBasicBlock *Entry,
  50. bool isSubRegion = false)
  51. : RegionNodeBase<RegionTraits<MachineFunction>>(Parent, Entry,
  52. isSubRegion) {}
  53. bool operator==(const MachineRegion &RN) const {
  54. return this == reinterpret_cast<const MachineRegionNode *>(&RN);
  55. }
  56. };
  57. class MachineRegion : public RegionBase<RegionTraits<MachineFunction>> {
  58. public:
  59. MachineRegion(MachineBasicBlock *Entry, MachineBasicBlock *Exit,
  60. MachineRegionInfo *RI, MachineDominatorTree *DT,
  61. MachineRegion *Parent = nullptr);
  62. ~MachineRegion();
  63. bool operator==(const MachineRegionNode &RN) const {
  64. return &RN == reinterpret_cast<const MachineRegionNode *>(this);
  65. }
  66. };
  67. class MachineRegionInfo : public RegionInfoBase<RegionTraits<MachineFunction>> {
  68. public:
  69. explicit MachineRegionInfo();
  70. ~MachineRegionInfo() override;
  71. // updateStatistics - Update statistic about created regions.
  72. void updateStatistics(MachineRegion *R) final;
  73. void recalculate(MachineFunction &F, MachineDominatorTree *DT,
  74. MachinePostDominatorTree *PDT, MachineDominanceFrontier *DF);
  75. };
  76. class MachineRegionInfoPass : public MachineFunctionPass {
  77. MachineRegionInfo RI;
  78. public:
  79. static char ID;
  80. explicit MachineRegionInfoPass();
  81. ~MachineRegionInfoPass() override;
  82. MachineRegionInfo &getRegionInfo() { return RI; }
  83. const MachineRegionInfo &getRegionInfo() const { return RI; }
  84. /// @name MachineFunctionPass interface
  85. //@{
  86. bool runOnMachineFunction(MachineFunction &F) override;
  87. void releaseMemory() override;
  88. void verifyAnalysis() const override;
  89. void getAnalysisUsage(AnalysisUsage &AU) const override;
  90. void print(raw_ostream &OS, const Module *) const override;
  91. void dump() const;
  92. //@}
  93. };
  94. template <>
  95. template <>
  96. inline MachineBasicBlock *
  97. RegionNodeBase<RegionTraits<MachineFunction>>::getNodeAs<MachineBasicBlock>()
  98. const {
  99. assert(!isSubRegion() && "This is not a MachineBasicBlock RegionNode!");
  100. return getEntry();
  101. }
  102. template <>
  103. template <>
  104. inline MachineRegion *
  105. RegionNodeBase<RegionTraits<MachineFunction>>::getNodeAs<MachineRegion>()
  106. const {
  107. assert(isSubRegion() && "This is not a subregion RegionNode!");
  108. auto Unconst =
  109. const_cast<RegionNodeBase<RegionTraits<MachineFunction>> *>(this);
  110. return reinterpret_cast<MachineRegion *>(Unconst);
  111. }
  112. RegionNodeGraphTraits(MachineRegionNode, MachineBasicBlock, MachineRegion);
  113. RegionNodeGraphTraits(const MachineRegionNode, MachineBasicBlock,
  114. MachineRegion);
  115. RegionGraphTraits(MachineRegion, MachineRegionNode);
  116. RegionGraphTraits(const MachineRegion, const MachineRegionNode);
  117. template <>
  118. struct GraphTraits<MachineRegionInfo *>
  119. : public GraphTraits<FlatIt<MachineRegionNode *>> {
  120. using nodes_iterator = df_iterator<NodeRef, df_iterator_default_set<NodeRef>,
  121. false, GraphTraits<FlatIt<NodeRef>>>;
  122. static NodeRef getEntryNode(MachineRegionInfo *RI) {
  123. return GraphTraits<FlatIt<MachineRegion *>>::getEntryNode(
  124. RI->getTopLevelRegion());
  125. }
  126. static nodes_iterator nodes_begin(MachineRegionInfo *RI) {
  127. return nodes_iterator::begin(getEntryNode(RI));
  128. }
  129. static nodes_iterator nodes_end(MachineRegionInfo *RI) {
  130. return nodes_iterator::end(getEntryNode(RI));
  131. }
  132. };
  133. template <>
  134. struct GraphTraits<MachineRegionInfoPass *>
  135. : public GraphTraits<MachineRegionInfo *> {
  136. using nodes_iterator = df_iterator<NodeRef, df_iterator_default_set<NodeRef>,
  137. false, GraphTraits<FlatIt<NodeRef>>>;
  138. static NodeRef getEntryNode(MachineRegionInfoPass *RI) {
  139. return GraphTraits<MachineRegionInfo *>::getEntryNode(&RI->getRegionInfo());
  140. }
  141. static nodes_iterator nodes_begin(MachineRegionInfoPass *RI) {
  142. return GraphTraits<MachineRegionInfo *>::nodes_begin(&RI->getRegionInfo());
  143. }
  144. static nodes_iterator nodes_end(MachineRegionInfoPass *RI) {
  145. return GraphTraits<MachineRegionInfo *>::nodes_end(&RI->getRegionInfo());
  146. }
  147. };
  148. extern template class RegionBase<RegionTraits<MachineFunction>>;
  149. extern template class RegionNodeBase<RegionTraits<MachineFunction>>;
  150. extern template class RegionInfoBase<RegionTraits<MachineFunction>>;
  151. } // end namespace llvm
  152. #endif // LLVM_CODEGEN_MACHINEREGIONINFO_H
  153. #ifdef __GNUC__
  154. #pragma GCC diagnostic pop
  155. #endif