DeadMachineInstructionElim.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/LiveRegUnits.h"
  15. #include "llvm/CodeGen/MachineFunctionPass.h"
  16. #include "llvm/CodeGen/MachineRegisterInfo.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 MachineRegisterInfo *MRI;
  29. const TargetInstrInfo *TII;
  30. LiveRegUnits LivePhysRegs;
  31. public:
  32. static char ID; // Pass identification, replacement for typeid
  33. DeadMachineInstructionElim() : MachineFunctionPass(ID) {
  34. initializeDeadMachineInstructionElimPass(*PassRegistry::getPassRegistry());
  35. }
  36. void getAnalysisUsage(AnalysisUsage &AU) const override {
  37. AU.setPreservesCFG();
  38. MachineFunctionPass::getAnalysisUsage(AU);
  39. }
  40. private:
  41. bool isDead(const MachineInstr *MI) const;
  42. bool eliminateDeadMI(MachineFunction &MF);
  43. };
  44. }
  45. char DeadMachineInstructionElim::ID = 0;
  46. char &llvm::DeadMachineInstructionElimID = DeadMachineInstructionElim::ID;
  47. INITIALIZE_PASS(DeadMachineInstructionElim, DEBUG_TYPE,
  48. "Remove dead machine instructions", false, false)
  49. bool DeadMachineInstructionElim::isDead(const MachineInstr *MI) const {
  50. // Technically speaking inline asm without side effects and no defs can still
  51. // be deleted. But there is so much bad inline asm code out there, we should
  52. // let them be.
  53. if (MI->isInlineAsm())
  54. return false;
  55. // Don't delete frame allocation labels.
  56. if (MI->getOpcode() == TargetOpcode::LOCAL_ESCAPE)
  57. return false;
  58. // Don't delete instructions with side effects.
  59. bool SawStore = false;
  60. if (!MI->isSafeToMove(nullptr, SawStore) && !MI->isPHI())
  61. return false;
  62. // Examine each operand.
  63. for (const MachineOperand &MO : MI->operands()) {
  64. if (MO.isReg() && MO.isDef()) {
  65. Register Reg = MO.getReg();
  66. if (Reg.isPhysical()) {
  67. // Don't delete live physreg defs, or any reserved register defs.
  68. if (!LivePhysRegs.available(Reg) || MRI->isReserved(Reg))
  69. return false;
  70. } else {
  71. if (MO.isDead()) {
  72. #ifndef NDEBUG
  73. // Basic check on the register. All of them should be 'undef'.
  74. for (auto &U : MRI->use_nodbg_operands(Reg))
  75. assert(U.isUndef() && "'Undef' use on a 'dead' register is found!");
  76. #endif
  77. continue;
  78. }
  79. for (const MachineInstr &Use : MRI->use_nodbg_instructions(Reg)) {
  80. if (&Use != MI)
  81. // This def has a non-debug use. Don't delete the instruction!
  82. return false;
  83. }
  84. }
  85. }
  86. }
  87. // If there are no defs with uses, the instruction is dead.
  88. return true;
  89. }
  90. bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) {
  91. if (skipFunction(MF.getFunction()))
  92. return false;
  93. MRI = &MF.getRegInfo();
  94. const TargetSubtargetInfo &ST = MF.getSubtarget();
  95. TII = ST.getInstrInfo();
  96. LivePhysRegs.init(*ST.getRegisterInfo());
  97. bool AnyChanges = eliminateDeadMI(MF);
  98. while (AnyChanges && eliminateDeadMI(MF))
  99. ;
  100. return AnyChanges;
  101. }
  102. bool DeadMachineInstructionElim::eliminateDeadMI(MachineFunction &MF) {
  103. bool AnyChanges = false;
  104. // Loop over all instructions in all blocks, from bottom to top, so that it's
  105. // more likely that chains of dependent but ultimately dead instructions will
  106. // be cleaned up.
  107. for (MachineBasicBlock *MBB : post_order(&MF)) {
  108. LivePhysRegs.addLiveOuts(*MBB);
  109. // Now scan the instructions and delete dead ones, tracking physreg
  110. // liveness as we go.
  111. for (MachineInstr &MI : make_early_inc_range(reverse(*MBB))) {
  112. // If the instruction is dead, delete it!
  113. if (isDead(&MI)) {
  114. LLVM_DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << MI);
  115. // It is possible that some DBG_VALUE instructions refer to this
  116. // instruction. They will be deleted in the live debug variable
  117. // analysis.
  118. MI.eraseFromParent();
  119. AnyChanges = true;
  120. ++NumDeletes;
  121. continue;
  122. }
  123. LivePhysRegs.stepBackward(MI);
  124. }
  125. }
  126. LivePhysRegs.clear();
  127. return AnyChanges;
  128. }