OptimizePHIs.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //===- OptimizePHIs.cpp - Optimize machine instruction PHIs ---------------===//
  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 pass optimizes machine instruction PHIs to take advantage of
  10. // opportunities created during DAG legalization.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/ADT/SmallPtrSet.h"
  14. #include "llvm/ADT/Statistic.h"
  15. #include "llvm/CodeGen/MachineBasicBlock.h"
  16. #include "llvm/CodeGen/MachineFunction.h"
  17. #include "llvm/CodeGen/MachineFunctionPass.h"
  18. #include "llvm/CodeGen/MachineInstr.h"
  19. #include "llvm/CodeGen/MachineOperand.h"
  20. #include "llvm/CodeGen/MachineRegisterInfo.h"
  21. #include "llvm/CodeGen/TargetRegisterInfo.h"
  22. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  23. #include "llvm/InitializePasses.h"
  24. #include "llvm/Pass.h"
  25. #include <cassert>
  26. using namespace llvm;
  27. #define DEBUG_TYPE "opt-phis"
  28. STATISTIC(NumPHICycles, "Number of PHI cycles replaced");
  29. STATISTIC(NumDeadPHICycles, "Number of dead PHI cycles");
  30. namespace {
  31. class OptimizePHIs : public MachineFunctionPass {
  32. MachineRegisterInfo *MRI;
  33. const TargetInstrInfo *TII;
  34. public:
  35. static char ID; // Pass identification
  36. OptimizePHIs() : MachineFunctionPass(ID) {
  37. initializeOptimizePHIsPass(*PassRegistry::getPassRegistry());
  38. }
  39. bool runOnMachineFunction(MachineFunction &Fn) override;
  40. void getAnalysisUsage(AnalysisUsage &AU) const override {
  41. AU.setPreservesCFG();
  42. MachineFunctionPass::getAnalysisUsage(AU);
  43. }
  44. private:
  45. using InstrSet = SmallPtrSet<MachineInstr *, 16>;
  46. using InstrSetIterator = SmallPtrSetIterator<MachineInstr *>;
  47. bool IsSingleValuePHICycle(MachineInstr *MI, unsigned &SingleValReg,
  48. InstrSet &PHIsInCycle);
  49. bool IsDeadPHICycle(MachineInstr *MI, InstrSet &PHIsInCycle);
  50. bool OptimizeBB(MachineBasicBlock &MBB);
  51. };
  52. } // end anonymous namespace
  53. char OptimizePHIs::ID = 0;
  54. char &llvm::OptimizePHIsID = OptimizePHIs::ID;
  55. INITIALIZE_PASS(OptimizePHIs, DEBUG_TYPE,
  56. "Optimize machine instruction PHIs", false, false)
  57. bool OptimizePHIs::runOnMachineFunction(MachineFunction &Fn) {
  58. if (skipFunction(Fn.getFunction()))
  59. return false;
  60. MRI = &Fn.getRegInfo();
  61. TII = Fn.getSubtarget().getInstrInfo();
  62. // Find dead PHI cycles and PHI cycles that can be replaced by a single
  63. // value. InstCombine does these optimizations, but DAG legalization may
  64. // introduce new opportunities, e.g., when i64 values are split up for
  65. // 32-bit targets.
  66. bool Changed = false;
  67. for (MachineBasicBlock &MBB : Fn)
  68. Changed |= OptimizeBB(MBB);
  69. return Changed;
  70. }
  71. /// IsSingleValuePHICycle - Check if MI is a PHI where all the source operands
  72. /// are copies of SingleValReg, possibly via copies through other PHIs. If
  73. /// SingleValReg is zero on entry, it is set to the register with the single
  74. /// non-copy value. PHIsInCycle is a set used to keep track of the PHIs that
  75. /// have been scanned. PHIs may be grouped by cycle, several cycles or chains.
  76. bool OptimizePHIs::IsSingleValuePHICycle(MachineInstr *MI,
  77. unsigned &SingleValReg,
  78. InstrSet &PHIsInCycle) {
  79. assert(MI->isPHI() && "IsSingleValuePHICycle expects a PHI instruction");
  80. Register DstReg = MI->getOperand(0).getReg();
  81. // See if we already saw this register.
  82. if (!PHIsInCycle.insert(MI).second)
  83. return true;
  84. // Don't scan crazily complex things.
  85. if (PHIsInCycle.size() == 16)
  86. return false;
  87. // Scan the PHI operands.
  88. for (unsigned i = 1; i != MI->getNumOperands(); i += 2) {
  89. Register SrcReg = MI->getOperand(i).getReg();
  90. if (SrcReg == DstReg)
  91. continue;
  92. MachineInstr *SrcMI = MRI->getVRegDef(SrcReg);
  93. // Skip over register-to-register moves.
  94. if (SrcMI && SrcMI->isCopy() && !SrcMI->getOperand(0).getSubReg() &&
  95. !SrcMI->getOperand(1).getSubReg() &&
  96. Register::isVirtualRegister(SrcMI->getOperand(1).getReg())) {
  97. SrcReg = SrcMI->getOperand(1).getReg();
  98. SrcMI = MRI->getVRegDef(SrcReg);
  99. }
  100. if (!SrcMI)
  101. return false;
  102. if (SrcMI->isPHI()) {
  103. if (!IsSingleValuePHICycle(SrcMI, SingleValReg, PHIsInCycle))
  104. return false;
  105. } else {
  106. // Fail if there is more than one non-phi/non-move register.
  107. if (SingleValReg != 0 && SingleValReg != SrcReg)
  108. return false;
  109. SingleValReg = SrcReg;
  110. }
  111. }
  112. return true;
  113. }
  114. /// IsDeadPHICycle - Check if the register defined by a PHI is only used by
  115. /// other PHIs in a cycle.
  116. bool OptimizePHIs::IsDeadPHICycle(MachineInstr *MI, InstrSet &PHIsInCycle) {
  117. assert(MI->isPHI() && "IsDeadPHICycle expects a PHI instruction");
  118. Register DstReg = MI->getOperand(0).getReg();
  119. assert(Register::isVirtualRegister(DstReg) &&
  120. "PHI destination is not a virtual register");
  121. // See if we already saw this register.
  122. if (!PHIsInCycle.insert(MI).second)
  123. return true;
  124. // Don't scan crazily complex things.
  125. if (PHIsInCycle.size() == 16)
  126. return false;
  127. for (MachineInstr &UseMI : MRI->use_nodbg_instructions(DstReg)) {
  128. if (!UseMI.isPHI() || !IsDeadPHICycle(&UseMI, PHIsInCycle))
  129. return false;
  130. }
  131. return true;
  132. }
  133. /// OptimizeBB - Remove dead PHI cycles and PHI cycles that can be replaced by
  134. /// a single value.
  135. bool OptimizePHIs::OptimizeBB(MachineBasicBlock &MBB) {
  136. bool Changed = false;
  137. for (MachineBasicBlock::iterator
  138. MII = MBB.begin(), E = MBB.end(); MII != E; ) {
  139. MachineInstr *MI = &*MII++;
  140. if (!MI->isPHI())
  141. break;
  142. // Check for single-value PHI cycles.
  143. unsigned SingleValReg = 0;
  144. InstrSet PHIsInCycle;
  145. if (IsSingleValuePHICycle(MI, SingleValReg, PHIsInCycle) &&
  146. SingleValReg != 0) {
  147. Register OldReg = MI->getOperand(0).getReg();
  148. if (!MRI->constrainRegClass(SingleValReg, MRI->getRegClass(OldReg)))
  149. continue;
  150. MRI->replaceRegWith(OldReg, SingleValReg);
  151. MI->eraseFromParent();
  152. // The kill flags on OldReg and SingleValReg may no longer be correct.
  153. MRI->clearKillFlags(SingleValReg);
  154. ++NumPHICycles;
  155. Changed = true;
  156. continue;
  157. }
  158. // Check for dead PHI cycles.
  159. PHIsInCycle.clear();
  160. if (IsDeadPHICycle(MI, PHIsInCycle)) {
  161. for (MachineInstr *PhiMI : PHIsInCycle) {
  162. if (MII == PhiMI)
  163. ++MII;
  164. PhiMI->eraseFromParent();
  165. }
  166. ++NumDeadPHICycles;
  167. Changed = true;
  168. }
  169. }
  170. return Changed;
  171. }