MachineLoopUtils.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //=- MachineLoopUtils.cpp - Functions for manipulating loops ----------------=//
  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. #include "llvm/CodeGen/MachineLoopInfo.h"
  9. #include "llvm/CodeGen/MachineLoopUtils.h"
  10. #include "llvm/CodeGen/MachineBasicBlock.h"
  11. #include "llvm/CodeGen/MachineRegisterInfo.h"
  12. #include "llvm/CodeGen/TargetInstrInfo.h"
  13. using namespace llvm;
  14. namespace {
  15. // MI's parent and BB are clones of each other. Find the equivalent copy of MI
  16. // in BB.
  17. MachineInstr &findEquivalentInstruction(MachineInstr &MI,
  18. MachineBasicBlock *BB) {
  19. MachineBasicBlock *PB = MI.getParent();
  20. unsigned Offset = std::distance(PB->instr_begin(), MachineBasicBlock::instr_iterator(MI));
  21. return *std::next(BB->instr_begin(), Offset);
  22. }
  23. } // namespace
  24. MachineBasicBlock *llvm::PeelSingleBlockLoop(LoopPeelDirection Direction,
  25. MachineBasicBlock *Loop,
  26. MachineRegisterInfo &MRI,
  27. const TargetInstrInfo *TII) {
  28. MachineFunction &MF = *Loop->getParent();
  29. MachineBasicBlock *Preheader = *Loop->pred_begin();
  30. if (Preheader == Loop)
  31. Preheader = *std::next(Loop->pred_begin());
  32. MachineBasicBlock *Exit = *Loop->succ_begin();
  33. if (Exit == Loop)
  34. Exit = *std::next(Loop->succ_begin());
  35. MachineBasicBlock *NewBB = MF.CreateMachineBasicBlock(Loop->getBasicBlock());
  36. if (Direction == LPD_Front)
  37. MF.insert(Loop->getIterator(), NewBB);
  38. else
  39. MF.insert(std::next(Loop->getIterator()), NewBB);
  40. DenseMap<Register, Register> Remaps;
  41. auto InsertPt = NewBB->end();
  42. for (MachineInstr &MI : *Loop) {
  43. MachineInstr *NewMI = MF.CloneMachineInstr(&MI);
  44. NewBB->insert(InsertPt, NewMI);
  45. for (MachineOperand &MO : NewMI->defs()) {
  46. Register OrigR = MO.getReg();
  47. if (OrigR.isPhysical())
  48. continue;
  49. Register &R = Remaps[OrigR];
  50. R = MRI.createVirtualRegister(MRI.getRegClass(OrigR));
  51. MO.setReg(R);
  52. if (Direction == LPD_Back) {
  53. // Replace all uses outside the original loop with the new register.
  54. // FIXME: is the use_iterator stable enough to mutate register uses
  55. // while iterating?
  56. SmallVector<MachineOperand *, 4> Uses;
  57. for (auto &Use : MRI.use_operands(OrigR))
  58. if (Use.getParent()->getParent() != Loop)
  59. Uses.push_back(&Use);
  60. for (auto *Use : Uses) {
  61. MRI.constrainRegClass(R, MRI.getRegClass(Use->getReg()));
  62. Use->setReg(R);
  63. }
  64. }
  65. }
  66. }
  67. for (auto I = NewBB->getFirstNonPHI(); I != NewBB->end(); ++I)
  68. for (MachineOperand &MO : I->uses())
  69. if (MO.isReg() && Remaps.count(MO.getReg()))
  70. MO.setReg(Remaps[MO.getReg()]);
  71. for (auto I = NewBB->begin(); I->isPHI(); ++I) {
  72. MachineInstr &MI = *I;
  73. unsigned LoopRegIdx = 3, InitRegIdx = 1;
  74. if (MI.getOperand(2).getMBB() != Preheader)
  75. std::swap(LoopRegIdx, InitRegIdx);
  76. MachineInstr &OrigPhi = findEquivalentInstruction(MI, Loop);
  77. assert(OrigPhi.isPHI());
  78. if (Direction == LPD_Front) {
  79. // When peeling front, we are only left with the initial value from the
  80. // preheader.
  81. Register R = MI.getOperand(LoopRegIdx).getReg();
  82. if (Remaps.count(R))
  83. R = Remaps[R];
  84. OrigPhi.getOperand(InitRegIdx).setReg(R);
  85. MI.RemoveOperand(LoopRegIdx + 1);
  86. MI.RemoveOperand(LoopRegIdx + 0);
  87. } else {
  88. // When peeling back, the initial value is the loop-carried value from
  89. // the original loop.
  90. Register LoopReg = OrigPhi.getOperand(LoopRegIdx).getReg();
  91. MI.getOperand(LoopRegIdx).setReg(LoopReg);
  92. MI.RemoveOperand(InitRegIdx + 1);
  93. MI.RemoveOperand(InitRegIdx + 0);
  94. }
  95. }
  96. DebugLoc DL;
  97. if (Direction == LPD_Front) {
  98. Preheader->replaceSuccessor(Loop, NewBB);
  99. NewBB->addSuccessor(Loop);
  100. Loop->replacePhiUsesWith(Preheader, NewBB);
  101. if (TII->removeBranch(*Preheader) > 0)
  102. TII->insertBranch(*Preheader, NewBB, nullptr, {}, DL);
  103. TII->removeBranch(*NewBB);
  104. TII->insertBranch(*NewBB, Loop, nullptr, {}, DL);
  105. } else {
  106. Loop->replaceSuccessor(Exit, NewBB);
  107. Exit->replacePhiUsesWith(Loop, NewBB);
  108. NewBB->addSuccessor(Exit);
  109. MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
  110. SmallVector<MachineOperand, 4> Cond;
  111. bool CanAnalyzeBr = !TII->analyzeBranch(*Loop, TBB, FBB, Cond);
  112. (void)CanAnalyzeBr;
  113. assert(CanAnalyzeBr && "Must be able to analyze the loop branch!");
  114. TII->removeBranch(*Loop);
  115. TII->insertBranch(*Loop, TBB == Exit ? NewBB : TBB,
  116. FBB == Exit ? NewBB : FBB, Cond, DL);
  117. if (TII->removeBranch(*NewBB) > 0)
  118. TII->insertBranch(*NewBB, Exit, nullptr, {}, DL);
  119. }
  120. return NewBB;
  121. }