MachineLoopInfo.cpp 7.1 KB

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