DeadMachineInstructionElim.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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/Pass.h"
  15. #include "llvm/CodeGen/MachineFunctionPass.h"
  16. #include "llvm/CodeGen/MachineRegisterInfo.h"
  17. #include "llvm/Support/Debug.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. #include "llvm/Target/TargetInstrInfo.h"
  20. #include "llvm/Target/TargetMachine.h"
  21. using namespace llvm;
  22. namespace {
  23. class DeadMachineInstructionElim : public MachineFunctionPass {
  24. virtual bool runOnMachineFunction(MachineFunction &MF);
  25. const TargetRegisterInfo *TRI;
  26. const MachineRegisterInfo *MRI;
  27. const TargetInstrInfo *TII;
  28. BitVector LivePhysRegs;
  29. public:
  30. static char ID; // Pass identification, replacement for typeid
  31. DeadMachineInstructionElim() : MachineFunctionPass(&ID) {}
  32. private:
  33. bool isDead(const MachineInstr *MI) const;
  34. };
  35. }
  36. char DeadMachineInstructionElim::ID = 0;
  37. static RegisterPass<DeadMachineInstructionElim>
  38. Y("dead-mi-elimination",
  39. "Remove dead machine instructions");
  40. FunctionPass *llvm::createDeadMachineInstructionElimPass() {
  41. return new DeadMachineInstructionElim();
  42. }
  43. bool DeadMachineInstructionElim::isDead(const MachineInstr *MI) const {
  44. // Don't delete instructions with side effects.
  45. bool SawStore = false;
  46. if (!MI->isSafeToMove(TII, SawStore, 0))
  47. return false;
  48. // Examine each operand.
  49. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  50. const MachineOperand &MO = MI->getOperand(i);
  51. if (MO.isReg() && MO.isDef()) {
  52. unsigned Reg = MO.getReg();
  53. if (TargetRegisterInfo::isPhysicalRegister(Reg) ?
  54. LivePhysRegs[Reg] : !MRI->use_empty(Reg)) {
  55. // This def has a use. Don't delete the instruction!
  56. return false;
  57. }
  58. }
  59. }
  60. // If there are no defs with uses, the instruction is dead.
  61. return true;
  62. }
  63. bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) {
  64. bool AnyChanges = false;
  65. MRI = &MF.getRegInfo();
  66. TRI = MF.getTarget().getRegisterInfo();
  67. TII = MF.getTarget().getInstrInfo();
  68. // Compute a bitvector to represent all non-allocatable physregs.
  69. BitVector NonAllocatableRegs = TRI->getAllocatableSet(MF);
  70. NonAllocatableRegs.flip();
  71. // Loop over all instructions in all blocks, from bottom to top, so that it's
  72. // more likely that chains of dependent but ultimately dead instructions will
  73. // be cleaned up.
  74. for (MachineFunction::reverse_iterator I = MF.rbegin(), E = MF.rend();
  75. I != E; ++I) {
  76. MachineBasicBlock *MBB = &*I;
  77. // Start out assuming that all non-allocatable registers are live
  78. // out of this block.
  79. LivePhysRegs = NonAllocatableRegs;
  80. // Also add any explicit live-out physregs for this block.
  81. if (!MBB->empty() && MBB->back().getDesc().isReturn())
  82. for (MachineRegisterInfo::liveout_iterator LOI = MRI->liveout_begin(),
  83. LOE = MRI->liveout_end(); LOI != LOE; ++LOI) {
  84. unsigned Reg = *LOI;
  85. if (TargetRegisterInfo::isPhysicalRegister(Reg))
  86. LivePhysRegs.set(Reg);
  87. }
  88. // Now scan the instructions and delete dead ones, tracking physreg
  89. // liveness as we go.
  90. for (MachineBasicBlock::reverse_iterator MII = MBB->rbegin(),
  91. MIE = MBB->rend(); MII != MIE; ) {
  92. MachineInstr *MI = &*MII;
  93. // If the instruction is dead, delete it!
  94. if (isDead(MI)) {
  95. DEBUG(errs() << "DeadMachineInstructionElim: DELETING: " << *MI);
  96. AnyChanges = true;
  97. MI->eraseFromParent();
  98. MIE = MBB->rend();
  99. // MII is now pointing to the next instruction to process,
  100. // so don't increment it.
  101. continue;
  102. }
  103. // Record the physreg defs.
  104. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  105. const MachineOperand &MO = MI->getOperand(i);
  106. if (MO.isReg() && MO.isDef()) {
  107. unsigned Reg = MO.getReg();
  108. if (Reg != 0 && TargetRegisterInfo::isPhysicalRegister(Reg)) {
  109. LivePhysRegs.reset(Reg);
  110. // Check the subreg set, not the alias set, because a def
  111. // of a super-register may still be partially live after
  112. // this def.
  113. for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
  114. *SubRegs; ++SubRegs)
  115. LivePhysRegs.reset(*SubRegs);
  116. }
  117. }
  118. }
  119. // Record the physreg uses, after the defs, in case a physreg is
  120. // both defined and used in the same instruction.
  121. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  122. const MachineOperand &MO = MI->getOperand(i);
  123. if (MO.isReg() && MO.isUse()) {
  124. unsigned Reg = MO.getReg();
  125. if (Reg != 0 && TargetRegisterInfo::isPhysicalRegister(Reg)) {
  126. LivePhysRegs.set(Reg);
  127. for (const unsigned *AliasSet = TRI->getAliasSet(Reg);
  128. *AliasSet; ++AliasSet)
  129. LivePhysRegs.set(*AliasSet);
  130. }
  131. }
  132. }
  133. // We didn't delete the current instruction, so increment MII to
  134. // the next one.
  135. ++MII;
  136. }
  137. }
  138. LivePhysRegs.clear();
  139. return AnyChanges;
  140. }