MachineLoopInfo.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //===- MachineLoopInfo.cpp - Natural Loop Calculator ----------------------===//
  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. // the loops identified may actually be several natural loops that share the
  13. // same header node... not just a single natural loop.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "llvm/CodeGen/MachineLoopInfo.h"
  17. #include "llvm/Analysis/LoopInfoImpl.h"
  18. #include "llvm/CodeGen/MachineDominators.h"
  19. #include "llvm/CodeGen/Passes.h"
  20. #include "llvm/Config/llvm-config.h"
  21. #include "llvm/Support/Debug.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. using namespace llvm;
  24. // Explicitly instantiate methods in LoopInfoImpl.h for MI-level Loops.
  25. template class llvm::LoopBase<MachineBasicBlock, MachineLoop>;
  26. template class llvm::LoopInfoBase<MachineBasicBlock, MachineLoop>;
  27. char MachineLoopInfo::ID = 0;
  28. INITIALIZE_PASS_BEGIN(MachineLoopInfo, "machine-loops",
  29. "Machine Natural Loop Construction", true, true)
  30. INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
  31. INITIALIZE_PASS_END(MachineLoopInfo, "machine-loops",
  32. "Machine Natural Loop Construction", true, true)
  33. char &llvm::MachineLoopInfoID = MachineLoopInfo::ID;
  34. bool MachineLoopInfo::runOnMachineFunction(MachineFunction &) {
  35. releaseMemory();
  36. LI.analyze(getAnalysis<MachineDominatorTree>().getBase());
  37. return false;
  38. }
  39. void MachineLoopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
  40. AU.setPreservesAll();
  41. AU.addRequired<MachineDominatorTree>();
  42. MachineFunctionPass::getAnalysisUsage(AU);
  43. }
  44. MachineBasicBlock *MachineLoop::getTopBlock() {
  45. MachineBasicBlock *TopMBB = getHeader();
  46. MachineFunction::iterator Begin = TopMBB->getParent()->begin();
  47. if (TopMBB->getIterator() != Begin) {
  48. MachineBasicBlock *PriorMBB = &*std::prev(TopMBB->getIterator());
  49. while (contains(PriorMBB)) {
  50. TopMBB = PriorMBB;
  51. if (TopMBB->getIterator() == Begin)
  52. break;
  53. PriorMBB = &*std::prev(TopMBB->getIterator());
  54. }
  55. }
  56. return TopMBB;
  57. }
  58. MachineBasicBlock *MachineLoop::getBottomBlock() {
  59. MachineBasicBlock *BotMBB = getHeader();
  60. MachineFunction::iterator End = BotMBB->getParent()->end();
  61. if (BotMBB->getIterator() != std::prev(End)) {
  62. MachineBasicBlock *NextMBB = &*std::next(BotMBB->getIterator());
  63. while (contains(NextMBB)) {
  64. BotMBB = NextMBB;
  65. if (BotMBB == &*std::next(BotMBB->getIterator()))
  66. break;
  67. NextMBB = &*std::next(BotMBB->getIterator());
  68. }
  69. }
  70. return BotMBB;
  71. }
  72. MachineBasicBlock *MachineLoop::findLoopControlBlock() {
  73. if (MachineBasicBlock *Latch = getLoopLatch()) {
  74. if (isLoopExiting(Latch))
  75. return Latch;
  76. else
  77. return getExitingBlock();
  78. }
  79. return nullptr;
  80. }
  81. DebugLoc MachineLoop::getStartLoc() const {
  82. // Try the pre-header first.
  83. if (MachineBasicBlock *PHeadMBB = getLoopPreheader())
  84. if (const BasicBlock *PHeadBB = PHeadMBB->getBasicBlock())
  85. if (DebugLoc DL = PHeadBB->getTerminator()->getDebugLoc())
  86. return DL;
  87. // If we have no pre-header or there are no instructions with debug
  88. // info in it, try the header.
  89. if (MachineBasicBlock *HeadMBB = getHeader())
  90. if (const BasicBlock *HeadBB = HeadMBB->getBasicBlock())
  91. return HeadBB->getTerminator()->getDebugLoc();
  92. return DebugLoc();
  93. }
  94. MachineBasicBlock *
  95. MachineLoopInfo::findLoopPreheader(MachineLoop *L,
  96. bool SpeculativePreheader) const {
  97. if (MachineBasicBlock *PB = L->getLoopPreheader())
  98. return PB;
  99. if (!SpeculativePreheader)
  100. return nullptr;
  101. MachineBasicBlock *HB = L->getHeader(), *LB = L->getLoopLatch();
  102. if (HB->pred_size() != 2 || HB->hasAddressTaken())
  103. return nullptr;
  104. // Find the predecessor of the header that is not the latch block.
  105. MachineBasicBlock *Preheader = nullptr;
  106. for (MachineBasicBlock *P : HB->predecessors()) {
  107. if (P == LB)
  108. continue;
  109. // Sanity.
  110. if (Preheader)
  111. return nullptr;
  112. Preheader = P;
  113. }
  114. // Check if the preheader candidate is a successor of any other loop
  115. // headers. We want to avoid having two loop setups in the same block.
  116. for (MachineBasicBlock *S : Preheader->successors()) {
  117. if (S == HB)
  118. continue;
  119. MachineLoop *T = getLoopFor(S);
  120. if (T && T->getHeader() == S)
  121. return nullptr;
  122. }
  123. return Preheader;
  124. }
  125. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  126. LLVM_DUMP_METHOD void MachineLoop::dump() const {
  127. print(dbgs());
  128. }
  129. #endif