DeadMachineInstructionElim.cpp revision 263508
1169691Skan//===- DeadMachineInstructionElim.cpp - Remove dead machine instructions --===// 2169691Skan// 3169691Skan// The LLVM Compiler Infrastructure 4169691Skan// 5169691Skan// This file is distributed under the University of Illinois Open Source 6169691Skan// License. See LICENSE.TXT for details. 7169691Skan// 8169691Skan//===----------------------------------------------------------------------===// 9169691Skan// 10169691Skan// This is an extremely simple MachineInstr-level dead-code-elimination pass. 11169691Skan// 12169691Skan//===----------------------------------------------------------------------===// 13169691Skan 14169691Skan#define DEBUG_TYPE "codegen-dce" 15169691Skan#include "llvm/CodeGen/Passes.h" 16169691Skan#include "llvm/ADT/Statistic.h" 17169691Skan#include "llvm/CodeGen/MachineFunctionPass.h" 18169691Skan#include "llvm/CodeGen/MachineRegisterInfo.h" 19169691Skan#include "llvm/Pass.h" 20169691Skan#include "llvm/Support/Debug.h" 21169691Skan#include "llvm/Support/raw_ostream.h" 22169691Skan#include "llvm/Target/TargetInstrInfo.h" 23169691Skan#include "llvm/Target/TargetMachine.h" 24169691Skanusing namespace llvm; 25169691Skan 26169691SkanSTATISTIC(NumDeletes, "Number of dead instructions deleted"); 27169691Skan 28169691Skannamespace { 29169691Skan class DeadMachineInstructionElim : public MachineFunctionPass { 30169691Skan virtual bool runOnMachineFunction(MachineFunction &MF); 31169691Skan 32169691Skan const TargetRegisterInfo *TRI; 33169691Skan const MachineRegisterInfo *MRI; 34169691Skan const TargetInstrInfo *TII; 35169691Skan BitVector LivePhysRegs; 36169691Skan 37169691Skan public: 38169691Skan static char ID; // Pass identification, replacement for typeid 39169691Skan DeadMachineInstructionElim() : MachineFunctionPass(ID) { 40169691Skan initializeDeadMachineInstructionElimPass(*PassRegistry::getPassRegistry()); 41169691Skan } 42169691Skan 43169691Skan private: 44169691Skan bool isDead(const MachineInstr *MI) const; 45169691Skan }; 46169691Skan} 47169691Skanchar DeadMachineInstructionElim::ID = 0; 48169691Skanchar &llvm::DeadMachineInstructionElimID = DeadMachineInstructionElim::ID; 49169691Skan 50169691SkanINITIALIZE_PASS(DeadMachineInstructionElim, "dead-mi-elimination", 51169691Skan "Remove dead machine instructions", false, false) 52169691Skan 53169691Skanbool DeadMachineInstructionElim::isDead(const MachineInstr *MI) const { 54169691Skan // Technically speaking inline asm without side effects and no defs can still 55169691Skan // be deleted. But there is so much bad inline asm code out there, we should 56169691Skan // let them be. 57169691Skan if (MI->isInlineAsm()) 58169691Skan return false; 59169691Skan 60169691Skan // Don't delete instructions with side effects. 61169691Skan bool SawStore = false; 62169691Skan if (!MI->isSafeToMove(TII, 0, SawStore) && !MI->isPHI()) 63169691Skan return false; 64169691Skan 65169691Skan // Examine each operand. 66169691Skan for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 67169691Skan const MachineOperand &MO = MI->getOperand(i); 68169691Skan if (MO.isReg() && MO.isDef()) { 69169691Skan unsigned Reg = MO.getReg(); 70169691Skan if (TargetRegisterInfo::isPhysicalRegister(Reg)) { 71169691Skan // Don't delete live physreg defs, or any reserved register defs. 72169691Skan if (LivePhysRegs.test(Reg) || MRI->isReserved(Reg)) 73169691Skan return false; 74169691Skan } else { 75169691Skan if (!MRI->use_nodbg_empty(Reg)) 76169691Skan // This def has a non-debug use. Don't delete the instruction! 77169691Skan return false; 78169691Skan } 79169691Skan } 80169691Skan } 81169691Skan 82169691Skan // If there are no defs with uses, the instruction is dead. 83169691Skan return true; 84169691Skan} 85169691Skan 86169691Skanbool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) { 87169691Skan bool AnyChanges = false; 88169691Skan MRI = &MF.getRegInfo(); 89169691Skan TRI = MF.getTarget().getRegisterInfo(); 90169691Skan TII = MF.getTarget().getInstrInfo(); 91169691Skan 92169691Skan // Loop over all instructions in all blocks, from bottom to top, so that it's 93169691Skan // more likely that chains of dependent but ultimately dead instructions will 94169691Skan // be cleaned up. 95169691Skan for (MachineFunction::reverse_iterator I = MF.rbegin(), E = MF.rend(); 96169691Skan I != E; ++I) { 97169691Skan MachineBasicBlock *MBB = &*I; 98169691Skan 99169691Skan // Start out assuming that reserved registers are live out of this block. 100169691Skan LivePhysRegs = MRI->getReservedRegs(); 101169691Skan 102169691Skan // Add live-ins from sucessors to LivePhysRegs. Normally, physregs are not 103169691Skan // live across blocks, but some targets (x86) can have flags live out of a 104169691Skan // block. 105169691Skan for (MachineBasicBlock::succ_iterator S = MBB->succ_begin(), 106169691Skan E = MBB->succ_end(); S != E; S++) 107169691Skan for (MachineBasicBlock::livein_iterator LI = (*S)->livein_begin(); 108169691Skan LI != (*S)->livein_end(); LI++) 109169691Skan LivePhysRegs.set(*LI); 110169691Skan 111169691Skan // Now scan the instructions and delete dead ones, tracking physreg 112169691Skan // liveness as we go. 113169691Skan for (MachineBasicBlock::reverse_iterator MII = MBB->rbegin(), 114169691Skan MIE = MBB->rend(); MII != MIE; ) { 115169691Skan MachineInstr *MI = &*MII; 116169691Skan 117169691Skan // If the instruction is dead, delete it! 118 if (isDead(MI)) { 119 DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << *MI); 120 // It is possible that some DBG_VALUE instructions refer to this 121 // instruction. Examine each def operand for such references; 122 // if found, mark the DBG_VALUE as undef (but don't delete it). 123 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 124 const MachineOperand &MO = MI->getOperand(i); 125 if (!MO.isReg() || !MO.isDef()) 126 continue; 127 unsigned Reg = MO.getReg(); 128 if (!TargetRegisterInfo::isVirtualRegister(Reg)) 129 continue; 130 MachineRegisterInfo::use_iterator nextI; 131 for (MachineRegisterInfo::use_iterator I = MRI->use_begin(Reg), 132 E = MRI->use_end(); I!=E; I=nextI) { 133 nextI = llvm::next(I); // I is invalidated by the setReg 134 MachineOperand& Use = I.getOperand(); 135 MachineInstr *UseMI = Use.getParent(); 136 if (UseMI==MI) 137 continue; 138 assert(Use.isDebug()); 139 UseMI->getOperand(0).setReg(0U); 140 } 141 } 142 AnyChanges = true; 143 MI->eraseFromParent(); 144 ++NumDeletes; 145 MIE = MBB->rend(); 146 // MII is now pointing to the next instruction to process, 147 // so don't increment it. 148 continue; 149 } 150 151 // Record the physreg defs. 152 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 153 const MachineOperand &MO = MI->getOperand(i); 154 if (MO.isReg() && MO.isDef()) { 155 unsigned Reg = MO.getReg(); 156 if (TargetRegisterInfo::isPhysicalRegister(Reg)) { 157 // Check the subreg set, not the alias set, because a def 158 // of a super-register may still be partially live after 159 // this def. 160 for (MCSubRegIterator SR(Reg, TRI,/*IncludeSelf=*/true); 161 SR.isValid(); ++SR) 162 LivePhysRegs.reset(*SR); 163 } 164 } else if (MO.isRegMask()) { 165 // Register mask of preserved registers. All clobbers are dead. 166 LivePhysRegs.clearBitsNotInMask(MO.getRegMask()); 167 } 168 } 169 // Record the physreg uses, after the defs, in case a physreg is 170 // both defined and used in the same instruction. 171 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 172 const MachineOperand &MO = MI->getOperand(i); 173 if (MO.isReg() && MO.isUse()) { 174 unsigned Reg = MO.getReg(); 175 if (TargetRegisterInfo::isPhysicalRegister(Reg)) { 176 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) 177 LivePhysRegs.set(*AI); 178 } 179 } 180 } 181 182 // We didn't delete the current instruction, so increment MII to 183 // the next one. 184 ++MII; 185 } 186 } 187 188 LivePhysRegs.clear(); 189 return AnyChanges; 190} 191