LoopTraversal.h 4.4 KB

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