MachineLoopInfo.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/CodeGen/MachineLoopInfo.h - Natural Loop Calculator -*- 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. // This file defines the MachineLoopInfo class that is used to identify natural
  15. // loops and determine the loop depth of various nodes of the CFG. Note that
  16. // natural loops may actually be several loops that share the same header node.
  17. //
  18. // This analysis calculates the nesting structure of loops in a function. For
  19. // each natural loop identified, this analysis identifies natural loops
  20. // contained entirely within the loop and the basic blocks the make up the loop.
  21. //
  22. // It can calculate on the fly various bits of information, for example:
  23. //
  24. // * whether there is a preheader for the loop
  25. // * the number of back edges to the header
  26. // * whether or not a particular block branches out of the loop
  27. // * the successor blocks of the loop
  28. // * the loop depth
  29. // * the trip count
  30. // * etc...
  31. //
  32. //===----------------------------------------------------------------------===//
  33. #ifndef LLVM_CODEGEN_MACHINELOOPINFO_H
  34. #define LLVM_CODEGEN_MACHINELOOPINFO_H
  35. #include "llvm/Analysis/LoopInfo.h"
  36. #include "llvm/CodeGen/MachineBasicBlock.h"
  37. #include "llvm/CodeGen/MachineFunctionPass.h"
  38. #include "llvm/IR/DebugLoc.h"
  39. namespace llvm {
  40. class MachineDominatorTree;
  41. // Implementation in LoopInfoImpl.h
  42. class MachineLoop;
  43. extern template class LoopBase<MachineBasicBlock, MachineLoop>;
  44. class MachineLoop : public LoopBase<MachineBasicBlock, MachineLoop> {
  45. public:
  46. /// Return the "top" block in the loop, which is the first block in the linear
  47. /// layout, ignoring any parts of the loop not contiguous with the part that
  48. /// contains the header.
  49. MachineBasicBlock *getTopBlock();
  50. /// Return the "bottom" block in the loop, which is the last block in the
  51. /// linear layout, ignoring any parts of the loop not contiguous with the part
  52. /// that contains the header.
  53. MachineBasicBlock *getBottomBlock();
  54. /// Find the block that contains the loop control variable and the
  55. /// loop test. This will return the latch block if it's one of the exiting
  56. /// blocks. Otherwise, return the exiting block. Return 'null' when
  57. /// multiple exiting blocks are present.
  58. MachineBasicBlock *findLoopControlBlock();
  59. /// Return the debug location of the start of this loop.
  60. /// This looks for a BB terminating instruction with a known debug
  61. /// location by looking at the preheader and header blocks. If it
  62. /// cannot find a terminating instruction with location information,
  63. /// it returns an unknown location.
  64. DebugLoc getStartLoc() const;
  65. /// Returns true if the instruction is loop invariant.
  66. /// I.e., all virtual register operands are defined outside of the loop,
  67. /// physical registers aren't accessed explicitly, and there are no side
  68. /// effects that aren't captured by the operands or other flags.
  69. bool isLoopInvariant(MachineInstr &I) const;
  70. void dump() const;
  71. private:
  72. friend class LoopInfoBase<MachineBasicBlock, MachineLoop>;
  73. explicit MachineLoop(MachineBasicBlock *MBB)
  74. : LoopBase<MachineBasicBlock, MachineLoop>(MBB) {}
  75. MachineLoop() = default;
  76. };
  77. // Implementation in LoopInfoImpl.h
  78. extern template class LoopInfoBase<MachineBasicBlock, MachineLoop>;
  79. class MachineLoopInfo : public MachineFunctionPass {
  80. friend class LoopBase<MachineBasicBlock, MachineLoop>;
  81. LoopInfoBase<MachineBasicBlock, MachineLoop> LI;
  82. public:
  83. static char ID; // Pass identification, replacement for typeid
  84. MachineLoopInfo();
  85. explicit MachineLoopInfo(MachineDominatorTree &MDT)
  86. : MachineFunctionPass(ID) {
  87. calculate(MDT);
  88. }
  89. MachineLoopInfo(const MachineLoopInfo &) = delete;
  90. MachineLoopInfo &operator=(const MachineLoopInfo &) = delete;
  91. LoopInfoBase<MachineBasicBlock, MachineLoop>& getBase() { return LI; }
  92. /// Find the block that either is the loop preheader, or could
  93. /// speculatively be used as the preheader. This is e.g. useful to place
  94. /// loop setup code. Code that cannot be speculated should not be placed
  95. /// here. SpeculativePreheader is controlling whether it also tries to
  96. /// find the speculative preheader if the regular preheader is not present.
  97. /// With FindMultiLoopPreheader = false, nullptr will be returned if the found
  98. /// preheader is the preheader of multiple loops.
  99. MachineBasicBlock *
  100. findLoopPreheader(MachineLoop *L, bool SpeculativePreheader = false,
  101. bool FindMultiLoopPreheader = false) const;
  102. /// The iterator interface to the top-level loops in the current function.
  103. using iterator = LoopInfoBase<MachineBasicBlock, MachineLoop>::iterator;
  104. inline iterator begin() const { return LI.begin(); }
  105. inline iterator end() const { return LI.end(); }
  106. bool empty() const { return LI.empty(); }
  107. /// Return the innermost loop that BB lives in. If a basic block is in no loop
  108. /// (for example the entry node), null is returned.
  109. inline MachineLoop *getLoopFor(const MachineBasicBlock *BB) const {
  110. return LI.getLoopFor(BB);
  111. }
  112. /// Same as getLoopFor.
  113. inline const MachineLoop *operator[](const MachineBasicBlock *BB) const {
  114. return LI.getLoopFor(BB);
  115. }
  116. /// Return the loop nesting level of the specified block.
  117. inline unsigned getLoopDepth(const MachineBasicBlock *BB) const {
  118. return LI.getLoopDepth(BB);
  119. }
  120. /// True if the block is a loop header node.
  121. inline bool isLoopHeader(const MachineBasicBlock *BB) const {
  122. return LI.isLoopHeader(BB);
  123. }
  124. /// Calculate the natural loop information.
  125. bool runOnMachineFunction(MachineFunction &F) override;
  126. void calculate(MachineDominatorTree &MDT);
  127. void releaseMemory() override { LI.releaseMemory(); }
  128. void getAnalysisUsage(AnalysisUsage &AU) const override;
  129. /// This removes the specified top-level loop from this loop info object. The
  130. /// loop is not deleted, as it will presumably be inserted into another loop.
  131. inline MachineLoop *removeLoop(iterator I) { return LI.removeLoop(I); }
  132. /// Change the top-level loop that contains BB to the specified loop. This
  133. /// should be used by transformations that restructure the loop hierarchy
  134. /// tree.
  135. inline void changeLoopFor(MachineBasicBlock *BB, MachineLoop *L) {
  136. LI.changeLoopFor(BB, L);
  137. }
  138. /// Replace the specified loop in the top-level loops list with the indicated
  139. /// loop.
  140. inline void changeTopLevelLoop(MachineLoop *OldLoop, MachineLoop *NewLoop) {
  141. LI.changeTopLevelLoop(OldLoop, NewLoop);
  142. }
  143. /// This adds the specified loop to the collection of top-level loops.
  144. inline void addTopLevelLoop(MachineLoop *New) {
  145. LI.addTopLevelLoop(New);
  146. }
  147. /// This method completely removes BB from all data structures, including all
  148. /// of the Loop objects it is nested in and our mapping from
  149. /// MachineBasicBlocks to loops.
  150. void removeBlock(MachineBasicBlock *BB) {
  151. LI.removeBlock(BB);
  152. }
  153. };
  154. // Allow clients to walk the list of nested loops...
  155. template <> struct GraphTraits<const MachineLoop*> {
  156. using NodeRef = const MachineLoop *;
  157. using ChildIteratorType = MachineLoopInfo::iterator;
  158. static NodeRef getEntryNode(const MachineLoop *L) { return L; }
  159. static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
  160. static ChildIteratorType child_end(NodeRef N) { return N->end(); }
  161. };
  162. template <> struct GraphTraits<MachineLoop*> {
  163. using NodeRef = MachineLoop *;
  164. using ChildIteratorType = MachineLoopInfo::iterator;
  165. static NodeRef getEntryNode(MachineLoop *L) { return L; }
  166. static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
  167. static ChildIteratorType child_end(NodeRef N) { return N->end(); }
  168. };
  169. } // end namespace llvm
  170. #endif // LLVM_CODEGEN_MACHINELOOPINFO_H
  171. #ifdef __GNUC__
  172. #pragma GCC diagnostic pop
  173. #endif