MachineLoopInfo.cpp 4.7 KB

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