MachineLoopInfo.h 7.9 KB

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