DeadMachineInstructionElim.cpp 6.2 KB

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