OptimizePHIs.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //===- OptimizePHIs.cpp - Optimize machine instruction PHIs ---------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This pass optimizes machine instruction PHIs to take advantage of
  11. // opportunities created during DAG legalization.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/ADT/SmallPtrSet.h"
  15. #include "llvm/ADT/Statistic.h"
  16. #include "llvm/CodeGen/MachineBasicBlock.h"
  17. #include "llvm/CodeGen/MachineFunction.h"
  18. #include "llvm/CodeGen/MachineFunctionPass.h"
  19. #include "llvm/CodeGen/MachineInstr.h"
  20. #include "llvm/CodeGen/MachineOperand.h"
  21. #include "llvm/CodeGen/MachineRegisterInfo.h"
  22. #include "llvm/CodeGen/TargetRegisterInfo.h"
  23. #include "llvm/CodeGen/TargetSubtargetInfo.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 (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
  68. Changed |= OptimizeBB(*I);
  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.
  76. bool OptimizePHIs::IsSingleValuePHICycle(MachineInstr *MI,
  77. unsigned &SingleValReg,
  78. InstrSet &PHIsInCycle) {
  79. assert(MI->isPHI() && "IsSingleValuePHICycle expects a PHI instruction");
  80. unsigned 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. unsigned 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() &&
  95. !SrcMI->getOperand(0).getSubReg() &&
  96. !SrcMI->getOperand(1).getSubReg() &&
  97. TargetRegisterInfo::isVirtualRegister(SrcMI->getOperand(1).getReg()))
  98. SrcMI = MRI->getVRegDef(SrcMI->getOperand(1).getReg());
  99. if (!SrcMI)
  100. return false;
  101. if (SrcMI->isPHI()) {
  102. if (!IsSingleValuePHICycle(SrcMI, SingleValReg, PHIsInCycle))
  103. return false;
  104. } else {
  105. // Fail if there is more than one non-phi/non-move register.
  106. if (SingleValReg != 0)
  107. return false;
  108. SingleValReg = SrcReg;
  109. }
  110. }
  111. return true;
  112. }
  113. /// IsDeadPHICycle - Check if the register defined by a PHI is only used by
  114. /// other PHIs in a cycle.
  115. bool OptimizePHIs::IsDeadPHICycle(MachineInstr *MI, InstrSet &PHIsInCycle) {
  116. assert(MI->isPHI() && "IsDeadPHICycle expects a PHI instruction");
  117. unsigned DstReg = MI->getOperand(0).getReg();
  118. assert(TargetRegisterInfo::isVirtualRegister(DstReg) &&
  119. "PHI destination is not a virtual register");
  120. // See if we already saw this register.
  121. if (!PHIsInCycle.insert(MI).second)
  122. return true;
  123. // Don't scan crazily complex things.
  124. if (PHIsInCycle.size() == 16)
  125. return false;
  126. for (MachineInstr &UseMI : MRI->use_nodbg_instructions(DstReg)) {
  127. if (!UseMI.isPHI() || !IsDeadPHICycle(&UseMI, PHIsInCycle))
  128. return false;
  129. }
  130. return true;
  131. }
  132. /// OptimizeBB - Remove dead PHI cycles and PHI cycles that can be replaced by
  133. /// a single value.
  134. bool OptimizePHIs::OptimizeBB(MachineBasicBlock &MBB) {
  135. bool Changed = false;
  136. for (MachineBasicBlock::iterator
  137. MII = MBB.begin(), E = MBB.end(); MII != E; ) {
  138. MachineInstr *MI = &*MII++;
  139. if (!MI->isPHI())
  140. break;
  141. // Check for single-value PHI cycles.
  142. unsigned SingleValReg = 0;
  143. InstrSet PHIsInCycle;
  144. if (IsSingleValuePHICycle(MI, SingleValReg, PHIsInCycle) &&
  145. SingleValReg != 0) {
  146. unsigned OldReg = MI->getOperand(0).getReg();
  147. if (!MRI->constrainRegClass(SingleValReg, MRI->getRegClass(OldReg)))
  148. continue;
  149. MRI->replaceRegWith(OldReg, SingleValReg);
  150. MI->eraseFromParent();
  151. ++NumPHICycles;
  152. Changed = true;
  153. continue;
  154. }
  155. // Check for dead PHI cycles.
  156. PHIsInCycle.clear();
  157. if (IsDeadPHICycle(MI, PHIsInCycle)) {
  158. for (InstrSetIterator PI = PHIsInCycle.begin(), PE = PHIsInCycle.end();
  159. PI != PE; ++PI) {
  160. MachineInstr *PhiMI = *PI;
  161. if (MII == PhiMI)
  162. ++MII;
  163. PhiMI->eraseFromParent();
  164. }
  165. ++NumDeadPHICycles;
  166. Changed = true;
  167. }
  168. }
  169. return Changed;
  170. }