MachineLoopInfo.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //===- llvm/CodeGen/MachineLoopInfo.h - Natural Loop Calculator -*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the MachineLoopInfo class that is used to identify natural
  11. // loops and determine the loop depth of various nodes of the CFG. Note that
  12. // natural loops may actually be several loops that share the same header node.
  13. //
  14. // This analysis calculates the nesting structure of loops in a function. For
  15. // each natural loop identified, this analysis identifies natural loops
  16. // contained entirely within the loop and the basic blocks the make up the loop.
  17. //
  18. // It can calculate on the fly various bits of information, for example:
  19. //
  20. // * whether there is a preheader for the loop
  21. // * the number of back edges to the header
  22. // * whether or not a particular block branches out of the loop
  23. // * the successor blocks of the loop
  24. // * the loop depth
  25. // * the trip count
  26. // * etc...
  27. //
  28. //===----------------------------------------------------------------------===//
  29. #ifndef LLVM_CODEGEN_MACHINELOOPINFO_H
  30. #define LLVM_CODEGEN_MACHINELOOPINFO_H
  31. #include "llvm/Analysis/LoopInfo.h"
  32. #include "llvm/CodeGen/MachineBasicBlock.h"
  33. #include "llvm/CodeGen/MachineFunctionPass.h"
  34. namespace llvm {
  35. // Implementation in LoopInfoImpl.h
  36. class MachineLoop;
  37. extern template class LoopBase<MachineBasicBlock, MachineLoop>;
  38. class MachineLoop : public LoopBase<MachineBasicBlock, MachineLoop> {
  39. public:
  40. MachineLoop();
  41. /// getTopBlock - Return the "top" block in the loop, which is the first
  42. /// block in the linear layout, ignoring any parts of the loop not
  43. /// contiguous with the part the contains the header.
  44. MachineBasicBlock *getTopBlock();
  45. /// getBottomBlock - Return the "bottom" block in the loop, which is the last
  46. /// block in the linear layout, ignoring any parts of the loop not
  47. /// contiguous with the part the contains the header.
  48. MachineBasicBlock *getBottomBlock();
  49. void dump() const;
  50. private:
  51. friend class LoopInfoBase<MachineBasicBlock, MachineLoop>;
  52. explicit MachineLoop(MachineBasicBlock *MBB)
  53. : LoopBase<MachineBasicBlock, MachineLoop>(MBB) {}
  54. };
  55. // Implementation in LoopInfoImpl.h
  56. extern template class LoopInfoBase<MachineBasicBlock, MachineLoop>;
  57. class MachineLoopInfo : public MachineFunctionPass {
  58. LoopInfoBase<MachineBasicBlock, MachineLoop> LI;
  59. friend class LoopBase<MachineBasicBlock, MachineLoop>;
  60. void operator=(const MachineLoopInfo &) = delete;
  61. MachineLoopInfo(const MachineLoopInfo &) = delete;
  62. public:
  63. static char ID; // Pass identification, replacement for typeid
  64. MachineLoopInfo() : MachineFunctionPass(ID) {
  65. initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
  66. }
  67. LoopInfoBase<MachineBasicBlock, MachineLoop>& getBase() { return LI; }
  68. /// iterator/begin/end - The interface to the top-level loops in the current
  69. /// function.
  70. ///
  71. typedef LoopInfoBase<MachineBasicBlock, MachineLoop>::iterator iterator;
  72. inline iterator begin() const { return LI.begin(); }
  73. inline iterator end() const { return LI.end(); }
  74. bool empty() const { return LI.empty(); }
  75. /// getLoopFor - Return the inner most loop that BB lives in. If a basic
  76. /// block is in no loop (for example the entry node), null is returned.
  77. ///
  78. inline MachineLoop *getLoopFor(const MachineBasicBlock *BB) const {
  79. return LI.getLoopFor(BB);
  80. }
  81. /// operator[] - same as getLoopFor...
  82. ///
  83. inline const MachineLoop *operator[](const MachineBasicBlock *BB) const {
  84. return LI.getLoopFor(BB);
  85. }
  86. /// getLoopDepth - Return the loop nesting level of the specified block...
  87. ///
  88. inline unsigned getLoopDepth(const MachineBasicBlock *BB) const {
  89. return LI.getLoopDepth(BB);
  90. }
  91. // isLoopHeader - True if the block is a loop header node
  92. inline bool isLoopHeader(const MachineBasicBlock *BB) const {
  93. return LI.isLoopHeader(BB);
  94. }
  95. /// runOnFunction - Calculate the natural loop information.
  96. ///
  97. bool runOnMachineFunction(MachineFunction &F) override;
  98. void releaseMemory() override { LI.releaseMemory(); }
  99. void getAnalysisUsage(AnalysisUsage &AU) const override;
  100. /// removeLoop - This removes the specified top-level loop from this loop info
  101. /// object. The loop is not deleted, as it will presumably be inserted into
  102. /// another loop.
  103. inline MachineLoop *removeLoop(iterator I) { return LI.removeLoop(I); }
  104. /// changeLoopFor - Change the top-level loop that contains BB to the
  105. /// specified loop. This should be used by transformations that restructure
  106. /// the loop hierarchy tree.
  107. inline void changeLoopFor(MachineBasicBlock *BB, MachineLoop *L) {
  108. LI.changeLoopFor(BB, L);
  109. }
  110. /// changeTopLevelLoop - Replace the specified loop in the top-level loops
  111. /// list with the indicated loop.
  112. inline void changeTopLevelLoop(MachineLoop *OldLoop, MachineLoop *NewLoop) {
  113. LI.changeTopLevelLoop(OldLoop, NewLoop);
  114. }
  115. /// addTopLevelLoop - This adds the specified loop to the collection of
  116. /// top-level loops.
  117. inline void addTopLevelLoop(MachineLoop *New) {
  118. LI.addTopLevelLoop(New);
  119. }
  120. /// removeBlock - This method completely removes BB from all data structures,
  121. /// including all of the Loop objects it is nested in and our mapping from
  122. /// MachineBasicBlocks to loops.
  123. void removeBlock(MachineBasicBlock *BB) {
  124. LI.removeBlock(BB);
  125. }
  126. };
  127. // Allow clients to walk the list of nested loops...
  128. template <> struct GraphTraits<const MachineLoop*> {
  129. typedef const MachineLoop NodeType;
  130. typedef MachineLoopInfo::iterator ChildIteratorType;
  131. static NodeType *getEntryNode(const MachineLoop *L) { return L; }
  132. static inline ChildIteratorType child_begin(NodeType *N) {
  133. return N->begin();
  134. }
  135. static inline ChildIteratorType child_end(NodeType *N) {
  136. return N->end();
  137. }
  138. };
  139. template <> struct GraphTraits<MachineLoop*> {
  140. typedef MachineLoop NodeType;
  141. typedef MachineLoopInfo::iterator ChildIteratorType;
  142. static NodeType *getEntryNode(MachineLoop *L) { return L; }
  143. static inline ChildIteratorType child_begin(NodeType *N) {
  144. return N->begin();
  145. }
  146. static inline ChildIteratorType child_end(NodeType *N) {
  147. return N->end();
  148. }
  149. };
  150. } // End llvm namespace
  151. #endif