DeadMachineInstructionElim.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //===- DeadMachineInstructionElim.cpp - Remove dead machine instructions --===//
  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 is an extremely simple MachineInstr-level dead-code-elimination pass.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/ADT/PostOrderIterator.h"
  13. #include "llvm/ADT/Statistic.h"
  14. #include "llvm/CodeGen/MachineFunctionPass.h"
  15. #include "llvm/CodeGen/MachineRegisterInfo.h"
  16. #include "llvm/CodeGen/Passes.h"
  17. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  18. #include "llvm/InitializePasses.h"
  19. #include "llvm/Pass.h"
  20. #include "llvm/Support/Debug.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. using namespace llvm;
  23. #define DEBUG_TYPE "dead-mi-elimination"
  24. STATISTIC(NumDeletes, "Number of dead instructions deleted");
  25. namespace {
  26. class DeadMachineInstructionElim : public MachineFunctionPass {
  27. bool runOnMachineFunction(MachineFunction &MF) override;
  28. const TargetRegisterInfo *TRI;
  29. const MachineRegisterInfo *MRI;
  30. const TargetInstrInfo *TII;
  31. BitVector LivePhysRegs;
  32. public:
  33. static char ID; // Pass identification, replacement for typeid
  34. DeadMachineInstructionElim() : MachineFunctionPass(ID) {
  35. initializeDeadMachineInstructionElimPass(*PassRegistry::getPassRegistry());
  36. }
  37. void getAnalysisUsage(AnalysisUsage &AU) const override {
  38. AU.setPreservesCFG();
  39. MachineFunctionPass::getAnalysisUsage(AU);
  40. }
  41. private:
  42. bool isDead(const MachineInstr *MI) const;
  43. bool eliminateDeadMI(MachineFunction &MF);
  44. };
  45. }
  46. char DeadMachineInstructionElim::ID = 0;
  47. char &llvm::DeadMachineInstructionElimID = DeadMachineInstructionElim::ID;
  48. INITIALIZE_PASS(DeadMachineInstructionElim, DEBUG_TYPE,
  49. "Remove dead machine instructions", false, false)
  50. bool DeadMachineInstructionElim::isDead(const MachineInstr *MI) const {
  51. // Technically speaking inline asm without side effects and no defs can still
  52. // be deleted. But there is so much bad inline asm code out there, we should
  53. // let them be.
  54. if (MI->isInlineAsm())
  55. return false;
  56. // Don't delete frame allocation labels.
  57. if (MI->getOpcode() == TargetOpcode::LOCAL_ESCAPE)
  58. return false;
  59. // Don't delete instructions with side effects.
  60. bool SawStore = false;
  61. if (!MI->isSafeToMove(nullptr, SawStore) && !MI->isPHI())
  62. return false;
  63. // Examine each operand.
  64. for (const MachineOperand &MO : MI->operands()) {
  65. if (MO.isReg() && MO.isDef()) {
  66. Register Reg = MO.getReg();
  67. if (Register::isPhysicalRegister(Reg)) {
  68. // Don't delete live physreg defs, or any reserved register defs.
  69. if (LivePhysRegs.test(Reg) || MRI->isReserved(Reg))
  70. return false;
  71. } else {
  72. if (MO.isDead()) {
  73. #ifndef NDEBUG
  74. // Baisc check on the register. All of them should be
  75. // 'undef'.
  76. for (auto &U : MRI->use_nodbg_operands(Reg))
  77. assert(U.isUndef() && "'Undef' use on a 'dead' register is found!");
  78. #endif
  79. continue;
  80. }
  81. for (const MachineInstr &Use : MRI->use_nodbg_instructions(Reg)) {
  82. if (&Use != MI)
  83. // This def has a non-debug use. Don't delete the instruction!
  84. return false;
  85. }
  86. }
  87. }
  88. }
  89. // If there are no defs with uses, the instruction is dead.
  90. return true;
  91. }
  92. bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) {
  93. if (skipFunction(MF.getFunction()))
  94. return false;
  95. bool AnyChanges = eliminateDeadMI(MF);
  96. while (AnyChanges && eliminateDeadMI(MF))
  97. ;
  98. return AnyChanges;
  99. }
  100. bool DeadMachineInstructionElim::eliminateDeadMI(MachineFunction &MF) {
  101. bool AnyChanges = false;
  102. MRI = &MF.getRegInfo();
  103. TRI = MF.getSubtarget().getRegisterInfo();
  104. TII = MF.getSubtarget().getInstrInfo();
  105. // Loop over all instructions in all blocks, from bottom to top, so that it's
  106. // more likely that chains of dependent but ultimately dead instructions will
  107. // be cleaned up.
  108. for (MachineBasicBlock *MBB : post_order(&MF)) {
  109. // Start out assuming that reserved registers are live out of this block.
  110. LivePhysRegs = MRI->getReservedRegs();
  111. // Add live-ins from successors to LivePhysRegs. Normally, physregs are not
  112. // live across blocks, but some targets (x86) can have flags live out of a
  113. // block.
  114. for (const MachineBasicBlock *Succ : MBB->successors())
  115. for (const auto &LI : Succ->liveins())
  116. LivePhysRegs.set(LI.PhysReg);
  117. // Now scan the instructions and delete dead ones, tracking physreg
  118. // liveness as we go.
  119. for (MachineInstr &MI : llvm::make_early_inc_range(llvm::reverse(*MBB))) {
  120. // If the instruction is dead, delete it!
  121. if (isDead(&MI)) {
  122. LLVM_DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << MI);
  123. // It is possible that some DBG_VALUE instructions refer to this
  124. // instruction. They will be deleted in the live debug variable
  125. // analysis.
  126. MI.eraseFromParent();
  127. AnyChanges = true;
  128. ++NumDeletes;
  129. continue;
  130. }
  131. // Record the physreg defs.
  132. for (const MachineOperand &MO : MI.operands()) {
  133. if (MO.isReg() && MO.isDef()) {
  134. Register Reg = MO.getReg();
  135. if (Register::isPhysicalRegister(Reg)) {
  136. // Check the subreg set, not the alias set, because a def
  137. // of a super-register may still be partially live after
  138. // this def.
  139. for (MCSubRegIterator SR(Reg, TRI,/*IncludeSelf=*/true);
  140. SR.isValid(); ++SR)
  141. LivePhysRegs.reset(*SR);
  142. }
  143. } else if (MO.isRegMask()) {
  144. // Register mask of preserved registers. All clobbers are dead.
  145. LivePhysRegs.clearBitsNotInMask(MO.getRegMask());
  146. }
  147. }
  148. // Record the physreg uses, after the defs, in case a physreg is
  149. // both defined and used in the same instruction.
  150. for (const MachineOperand &MO : MI.operands()) {
  151. if (MO.isReg() && MO.isUse()) {
  152. Register Reg = MO.getReg();
  153. if (Register::isPhysicalRegister(Reg)) {
  154. for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
  155. LivePhysRegs.set(*AI);
  156. }
  157. }
  158. }
  159. }
  160. }
  161. LivePhysRegs.clear();
  162. return AnyChanges;
  163. }