MachineLoopInfo.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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/MachineRegisterInfo.h"
  19. #include "llvm/CodeGen/Passes.h"
  20. #include "llvm/CodeGen/TargetInstrInfo.h"
  21. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  22. #include "llvm/Config/llvm-config.h"
  23. #include "llvm/InitializePasses.h"
  24. #include "llvm/Support/Debug.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. using namespace llvm;
  27. // Explicitly instantiate methods in LoopInfoImpl.h for MI-level Loops.
  28. template class llvm::LoopBase<MachineBasicBlock, MachineLoop>;
  29. template class llvm::LoopInfoBase<MachineBasicBlock, MachineLoop>;
  30. char MachineLoopInfo::ID = 0;
  31. MachineLoopInfo::MachineLoopInfo() : MachineFunctionPass(ID) {
  32. initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
  33. }
  34. INITIALIZE_PASS_BEGIN(MachineLoopInfo, "machine-loops",
  35. "Machine Natural Loop Construction", true, true)
  36. INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
  37. INITIALIZE_PASS_END(MachineLoopInfo, "machine-loops",
  38. "Machine Natural Loop Construction", true, true)
  39. char &llvm::MachineLoopInfoID = MachineLoopInfo::ID;
  40. bool MachineLoopInfo::runOnMachineFunction(MachineFunction &) {
  41. calculate(getAnalysis<MachineDominatorTree>());
  42. return false;
  43. }
  44. void MachineLoopInfo::calculate(MachineDominatorTree &MDT) {
  45. releaseMemory();
  46. LI.analyze(MDT.getBase());
  47. }
  48. void MachineLoopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
  49. AU.setPreservesAll();
  50. AU.addRequired<MachineDominatorTree>();
  51. MachineFunctionPass::getAnalysisUsage(AU);
  52. }
  53. MachineBasicBlock *MachineLoop::getTopBlock() {
  54. MachineBasicBlock *TopMBB = getHeader();
  55. MachineFunction::iterator Begin = TopMBB->getParent()->begin();
  56. if (TopMBB->getIterator() != Begin) {
  57. MachineBasicBlock *PriorMBB = &*std::prev(TopMBB->getIterator());
  58. while (contains(PriorMBB)) {
  59. TopMBB = PriorMBB;
  60. if (TopMBB->getIterator() == Begin)
  61. break;
  62. PriorMBB = &*std::prev(TopMBB->getIterator());
  63. }
  64. }
  65. return TopMBB;
  66. }
  67. MachineBasicBlock *MachineLoop::getBottomBlock() {
  68. MachineBasicBlock *BotMBB = getHeader();
  69. MachineFunction::iterator End = BotMBB->getParent()->end();
  70. if (BotMBB->getIterator() != std::prev(End)) {
  71. MachineBasicBlock *NextMBB = &*std::next(BotMBB->getIterator());
  72. while (contains(NextMBB)) {
  73. BotMBB = NextMBB;
  74. if (BotMBB == &*std::next(BotMBB->getIterator()))
  75. break;
  76. NextMBB = &*std::next(BotMBB->getIterator());
  77. }
  78. }
  79. return BotMBB;
  80. }
  81. MachineBasicBlock *MachineLoop::findLoopControlBlock() {
  82. if (MachineBasicBlock *Latch = getLoopLatch()) {
  83. if (isLoopExiting(Latch))
  84. return Latch;
  85. else
  86. return getExitingBlock();
  87. }
  88. return nullptr;
  89. }
  90. DebugLoc MachineLoop::getStartLoc() const {
  91. // Try the pre-header first.
  92. if (MachineBasicBlock *PHeadMBB = getLoopPreheader())
  93. if (const BasicBlock *PHeadBB = PHeadMBB->getBasicBlock())
  94. if (DebugLoc DL = PHeadBB->getTerminator()->getDebugLoc())
  95. return DL;
  96. // If we have no pre-header or there are no instructions with debug
  97. // info in it, try the header.
  98. if (MachineBasicBlock *HeadMBB = getHeader())
  99. if (const BasicBlock *HeadBB = HeadMBB->getBasicBlock())
  100. return HeadBB->getTerminator()->getDebugLoc();
  101. return DebugLoc();
  102. }
  103. MachineBasicBlock *
  104. MachineLoopInfo::findLoopPreheader(MachineLoop *L, bool SpeculativePreheader,
  105. bool FindMultiLoopPreheader) const {
  106. if (MachineBasicBlock *PB = L->getLoopPreheader())
  107. return PB;
  108. if (!SpeculativePreheader)
  109. return nullptr;
  110. MachineBasicBlock *HB = L->getHeader(), *LB = L->getLoopLatch();
  111. if (HB->pred_size() != 2 || HB->hasAddressTaken())
  112. return nullptr;
  113. // Find the predecessor of the header that is not the latch block.
  114. MachineBasicBlock *Preheader = nullptr;
  115. for (MachineBasicBlock *P : HB->predecessors()) {
  116. if (P == LB)
  117. continue;
  118. // Sanity.
  119. if (Preheader)
  120. return nullptr;
  121. Preheader = P;
  122. }
  123. // Check if the preheader candidate is a successor of any other loop
  124. // headers. We want to avoid having two loop setups in the same block.
  125. if (!FindMultiLoopPreheader) {
  126. for (MachineBasicBlock *S : Preheader->successors()) {
  127. if (S == HB)
  128. continue;
  129. MachineLoop *T = getLoopFor(S);
  130. if (T && T->getHeader() == S)
  131. return nullptr;
  132. }
  133. }
  134. return Preheader;
  135. }
  136. bool MachineLoop::isLoopInvariant(MachineInstr &I) const {
  137. MachineFunction *MF = I.getParent()->getParent();
  138. MachineRegisterInfo *MRI = &MF->getRegInfo();
  139. const TargetSubtargetInfo &ST = MF->getSubtarget();
  140. const TargetRegisterInfo *TRI = ST.getRegisterInfo();
  141. const TargetInstrInfo *TII = ST.getInstrInfo();
  142. // The instruction is loop invariant if all of its operands are.
  143. for (const MachineOperand &MO : I.operands()) {
  144. if (!MO.isReg())
  145. continue;
  146. Register Reg = MO.getReg();
  147. if (Reg == 0) continue;
  148. // An instruction that uses or defines a physical register can't e.g. be
  149. // hoisted, so mark this as not invariant.
  150. if (Register::isPhysicalRegister(Reg)) {
  151. if (MO.isUse()) {
  152. // If the physreg has no defs anywhere, it's just an ambient register
  153. // and we can freely move its uses. Alternatively, if it's allocatable,
  154. // it could get allocated to something with a def during allocation.
  155. // However, if the physreg is known to always be caller saved/restored
  156. // then this use is safe to hoist.
  157. if (!MRI->isConstantPhysReg(Reg) &&
  158. !(TRI->isCallerPreservedPhysReg(Reg.asMCReg(), *I.getMF())) &&
  159. !TII->isIgnorableUse(MO))
  160. return false;
  161. // Otherwise it's safe to move.
  162. continue;
  163. } else if (!MO.isDead()) {
  164. // A def that isn't dead can't be moved.
  165. return false;
  166. } else if (getHeader()->isLiveIn(Reg)) {
  167. // If the reg is live into the loop, we can't hoist an instruction
  168. // which would clobber it.
  169. return false;
  170. }
  171. }
  172. if (!MO.isUse())
  173. continue;
  174. assert(MRI->getVRegDef(Reg) &&
  175. "Machine instr not mapped for this vreg?!");
  176. // If the loop contains the definition of an operand, then the instruction
  177. // isn't loop invariant.
  178. if (contains(MRI->getVRegDef(Reg)))
  179. return false;
  180. }
  181. // If we got this far, the instruction is loop invariant!
  182. return true;
  183. }
  184. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  185. LLVM_DUMP_METHOD void MachineLoop::dump() const {
  186. print(dbgs());
  187. }
  188. #endif