LLVM API Documentation
00001 //===- DeadMachineInstructionElim.cpp - Remove dead machine instructions --===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This is an extremely simple MachineInstr-level dead-code-elimination pass. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #define DEBUG_TYPE "codegen-dce" 00015 #include "llvm/CodeGen/Passes.h" 00016 #include "llvm/ADT/Statistic.h" 00017 #include "llvm/CodeGen/MachineFunctionPass.h" 00018 #include "llvm/CodeGen/MachineRegisterInfo.h" 00019 #include "llvm/Pass.h" 00020 #include "llvm/Support/Debug.h" 00021 #include "llvm/Support/raw_ostream.h" 00022 #include "llvm/Target/TargetInstrInfo.h" 00023 #include "llvm/Target/TargetMachine.h" 00024 using namespace llvm; 00025 00026 STATISTIC(NumDeletes, "Number of dead instructions deleted"); 00027 00028 namespace { 00029 class DeadMachineInstructionElim : public MachineFunctionPass { 00030 virtual bool runOnMachineFunction(MachineFunction &MF); 00031 00032 const TargetRegisterInfo *TRI; 00033 const MachineRegisterInfo *MRI; 00034 const TargetInstrInfo *TII; 00035 BitVector LivePhysRegs; 00036 00037 public: 00038 static char ID; // Pass identification, replacement for typeid 00039 DeadMachineInstructionElim() : MachineFunctionPass(ID) { 00040 initializeDeadMachineInstructionElimPass(*PassRegistry::getPassRegistry()); 00041 } 00042 00043 private: 00044 bool isDead(const MachineInstr *MI) const; 00045 }; 00046 } 00047 char DeadMachineInstructionElim::ID = 0; 00048 char &llvm::DeadMachineInstructionElimID = DeadMachineInstructionElim::ID; 00049 00050 INITIALIZE_PASS(DeadMachineInstructionElim, "dead-mi-elimination", 00051 "Remove dead machine instructions", false, false) 00052 00053 bool DeadMachineInstructionElim::isDead(const MachineInstr *MI) const { 00054 // Technically speaking inline asm without side effects and no defs can still 00055 // be deleted. But there is so much bad inline asm code out there, we should 00056 // let them be. 00057 if (MI->isInlineAsm()) 00058 return false; 00059 00060 // Don't delete instructions with side effects. 00061 bool SawStore = false; 00062 if (!MI->isSafeToMove(TII, 0, SawStore) && !MI->isPHI()) 00063 return false; 00064 00065 // Examine each operand. 00066 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 00067 const MachineOperand &MO = MI->getOperand(i); 00068 if (MO.isReg() && MO.isDef()) { 00069 unsigned Reg = MO.getReg(); 00070 if (TargetRegisterInfo::isPhysicalRegister(Reg)) { 00071 // Don't delete live physreg defs, or any reserved register defs. 00072 if (LivePhysRegs.test(Reg) || MRI->isReserved(Reg)) 00073 return false; 00074 } else { 00075 if (!MRI->use_nodbg_empty(Reg)) 00076 // This def has a non-debug use. Don't delete the instruction! 00077 return false; 00078 } 00079 } 00080 } 00081 00082 // If there are no defs with uses, the instruction is dead. 00083 return true; 00084 } 00085 00086 bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) { 00087 bool AnyChanges = false; 00088 MRI = &MF.getRegInfo(); 00089 TRI = MF.getTarget().getRegisterInfo(); 00090 TII = MF.getTarget().getInstrInfo(); 00091 00092 // Loop over all instructions in all blocks, from bottom to top, so that it's 00093 // more likely that chains of dependent but ultimately dead instructions will 00094 // be cleaned up. 00095 for (MachineFunction::reverse_iterator I = MF.rbegin(), E = MF.rend(); 00096 I != E; ++I) { 00097 MachineBasicBlock *MBB = &*I; 00098 00099 // Start out assuming that reserved registers are live out of this block. 00100 LivePhysRegs = MRI->getReservedRegs(); 00101 00102 // Add live-ins from sucessors to LivePhysRegs. Normally, physregs are not 00103 // live across blocks, but some targets (x86) can have flags live out of a 00104 // block. 00105 for (MachineBasicBlock::succ_iterator S = MBB->succ_begin(), 00106 E = MBB->succ_end(); S != E; S++) 00107 for (MachineBasicBlock::livein_iterator LI = (*S)->livein_begin(); 00108 LI != (*S)->livein_end(); LI++) 00109 LivePhysRegs.set(*LI); 00110 00111 // Now scan the instructions and delete dead ones, tracking physreg 00112 // liveness as we go. 00113 for (MachineBasicBlock::reverse_iterator MII = MBB->rbegin(), 00114 MIE = MBB->rend(); MII != MIE; ) { 00115 MachineInstr *MI = &*MII; 00116 00117 // If the instruction is dead, delete it! 00118 if (isDead(MI)) { 00119 DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << *MI); 00120 // It is possible that some DBG_VALUE instructions refer to this 00121 // instruction. Examine each def operand for such references; 00122 // if found, mark the DBG_VALUE as undef (but don't delete it). 00123 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 00124 const MachineOperand &MO = MI->getOperand(i); 00125 if (!MO.isReg() || !MO.isDef()) 00126 continue; 00127 unsigned Reg = MO.getReg(); 00128 if (!TargetRegisterInfo::isVirtualRegister(Reg)) 00129 continue; 00130 MachineRegisterInfo::use_iterator nextI; 00131 for (MachineRegisterInfo::use_iterator I = MRI->use_begin(Reg), 00132 E = MRI->use_end(); I!=E; I=nextI) { 00133 nextI = llvm::next(I); // I is invalidated by the setReg 00134 MachineOperand& Use = I.getOperand(); 00135 MachineInstr *UseMI = Use.getParent(); 00136 if (UseMI==MI) 00137 continue; 00138 assert(Use.isDebug()); 00139 UseMI->getOperand(0).setReg(0U); 00140 } 00141 } 00142 AnyChanges = true; 00143 MI->eraseFromParent(); 00144 ++NumDeletes; 00145 MIE = MBB->rend(); 00146 // MII is now pointing to the next instruction to process, 00147 // so don't increment it. 00148 continue; 00149 } 00150 00151 // Record the physreg defs. 00152 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 00153 const MachineOperand &MO = MI->getOperand(i); 00154 if (MO.isReg() && MO.isDef()) { 00155 unsigned Reg = MO.getReg(); 00156 if (TargetRegisterInfo::isPhysicalRegister(Reg)) { 00157 // Check the subreg set, not the alias set, because a def 00158 // of a super-register may still be partially live after 00159 // this def. 00160 for (MCSubRegIterator SR(Reg, TRI,/*IncludeSelf=*/true); 00161 SR.isValid(); ++SR) 00162 LivePhysRegs.reset(*SR); 00163 } 00164 } else if (MO.isRegMask()) { 00165 // Register mask of preserved registers. All clobbers are dead. 00166 LivePhysRegs.clearBitsNotInMask(MO.getRegMask()); 00167 } 00168 } 00169 // Record the physreg uses, after the defs, in case a physreg is 00170 // both defined and used in the same instruction. 00171 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 00172 const MachineOperand &MO = MI->getOperand(i); 00173 if (MO.isReg() && MO.isUse()) { 00174 unsigned Reg = MO.getReg(); 00175 if (TargetRegisterInfo::isPhysicalRegister(Reg)) { 00176 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) 00177 LivePhysRegs.set(*AI); 00178 } 00179 } 00180 } 00181 00182 // We didn't delete the current instruction, so increment MII to 00183 // the next one. 00184 ++MII; 00185 } 00186 } 00187 00188 LivePhysRegs.clear(); 00189 return AnyChanges; 00190 }