LoopTraversal.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //==------ llvm/CodeGen/LoopTraversal.h - Loop Traversal -*- 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. //
  14. /// \file Loop Traversal logic.
  15. ///
  16. /// This class provides the basic blocks traversal order used by passes like
  17. /// ReachingDefAnalysis and ExecutionDomainFix.
  18. /// It identifies basic blocks that are part of loops and should to be visited
  19. /// twice and returns efficient traversal order for all the blocks.
  20. //
  21. //===----------------------------------------------------------------------===//
  22. #ifndef LLVM_CODEGEN_LOOPTRAVERSAL_H
  23. #define LLVM_CODEGEN_LOOPTRAVERSAL_H
  24. #include "llvm/ADT/SmallVector.h"
  25. namespace llvm {
  26. class MachineBasicBlock;
  27. class MachineFunction;
  28. /// This class provides the basic blocks traversal order used by passes like
  29. /// ReachingDefAnalysis and ExecutionDomainFix.
  30. /// It identifies basic blocks that are part of loops and should to be visited
  31. /// twice and returns efficient traversal order for all the blocks.
  32. ///
  33. /// We want to visit every instruction in every basic block in order to update
  34. /// it's execution domain or collect clearance information. However, for the
  35. /// clearance calculation, we need to know clearances from all predecessors
  36. /// (including any backedges), therfore we need to visit some blocks twice.
  37. /// As an example, consider the following loop.
  38. ///
  39. ///
  40. /// PH -> A -> B (xmm<Undef> -> xmm<Def>) -> C -> D -> EXIT
  41. /// ^ |
  42. /// +----------------------------------+
  43. ///
  44. /// The iteration order this pass will return is as follows:
  45. /// Optimized: PH A B C A' B' C' D
  46. ///
  47. /// The basic block order is constructed as follows:
  48. /// Once we finish processing some block, we update the counters in MBBInfos
  49. /// and re-process any successors that are now 'done'.
  50. /// We call a block that is ready for its final round of processing `done`
  51. /// (isBlockDone), e.g. when all predecessor information is known.
  52. ///
  53. /// Note that a naive traversal order would be to do two complete passes over
  54. /// all basic blocks/instructions, the first for recording clearances, the
  55. /// second for updating clearance based on backedges.
  56. /// However, for functions without backedges, or functions with a lot of
  57. /// straight-line code, and a small loop, that would be a lot of unnecessary
  58. /// work (since only the BBs that are part of the loop require two passes).
  59. ///
  60. /// E.g., the naive iteration order for the above exmple is as follows:
  61. /// Naive: PH A B C D A' B' C' D'
  62. ///
  63. /// In the optimized approach we avoid processing D twice, because we
  64. /// can entirely process the predecessors before getting to D.
  65. class LoopTraversal {
  66. private:
  67. struct MBBInfo {
  68. /// Whether we have gotten to this block in primary processing yet.
  69. bool PrimaryCompleted = false;
  70. /// The number of predecessors for which primary processing has completed
  71. unsigned IncomingProcessed = 0;
  72. /// The value of `IncomingProcessed` at the start of primary processing
  73. unsigned PrimaryIncoming = 0;
  74. /// The number of predecessors for which all processing steps are done.
  75. unsigned IncomingCompleted = 0;
  76. MBBInfo() = default;
  77. };
  78. using MBBInfoMap = SmallVector<MBBInfo, 4>;
  79. /// Helps keep track if we proccessed this block and all its predecessors.
  80. MBBInfoMap MBBInfos;
  81. public:
  82. struct TraversedMBBInfo {
  83. /// The basic block.
  84. MachineBasicBlock *MBB = nullptr;
  85. /// True if this is the first time we process the basic block.
  86. bool PrimaryPass = true;
  87. /// True if the block that is ready for its final round of processing.
  88. bool IsDone = true;
  89. TraversedMBBInfo(MachineBasicBlock *BB = nullptr, bool Primary = true,
  90. bool Done = true)
  91. : MBB(BB), PrimaryPass(Primary), IsDone(Done) {}
  92. };
  93. LoopTraversal() = default;
  94. /// Identifies basic blocks that are part of loops and should to be
  95. /// visited twice and returns efficient traversal order for all the blocks.
  96. typedef SmallVector<TraversedMBBInfo, 4> TraversalOrder;
  97. TraversalOrder traverse(MachineFunction &MF);
  98. private:
  99. /// Returens true if the block is ready for its final round of processing.
  100. bool isBlockDone(MachineBasicBlock *MBB);
  101. };
  102. } // namespace llvm
  103. #endif // LLVM_CODEGEN_LOOPTRAVERSAL_H
  104. #ifdef __GNUC__
  105. #pragma GCC diagnostic pop
  106. #endif