2
0

DeadMachineInstructionElim.cpp 6.4 KB

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