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 #include "llvm/CodeGen/Passes.h" 00015 #include "llvm/Pass.h" 00016 #include "llvm/CodeGen/MachineFunctionPass.h" 00017 #include "llvm/CodeGen/MachineRegisterInfo.h" 00018 #include "llvm/Support/Compiler.h" 00019 #include "llvm/Support/Debug.h" 00020 #include "llvm/Target/TargetInstrInfo.h" 00021 #include "llvm/Target/TargetMachine.h" 00022 using namespace llvm; 00023 00024 namespace { 00025 class VISIBILITY_HIDDEN DeadMachineInstructionElim : 00026 public MachineFunctionPass { 00027 virtual bool runOnMachineFunction(MachineFunction &MF); 00028 00029 const TargetRegisterInfo *TRI; 00030 const MachineRegisterInfo *MRI; 00031 const TargetInstrInfo *TII; 00032 BitVector LivePhysRegs; 00033 00034 public: 00035 static char ID; // Pass identification, replacement for typeid 00036 DeadMachineInstructionElim() : MachineFunctionPass(&ID) {} 00037 00038 private: 00039 bool isDead(MachineInstr *MI) const; 00040 }; 00041 } 00042 char DeadMachineInstructionElim::ID = 0; 00043 00044 static RegisterPass<DeadMachineInstructionElim> 00045 Y("dead-mi-elimination", 00046 "Remove dead machine instructions"); 00047 00048 FunctionPass *llvm::createDeadMachineInstructionElimPass() { 00049 return new DeadMachineInstructionElim(); 00050 } 00051 00052 bool DeadMachineInstructionElim::isDead(MachineInstr *MI) const { 00053 // Don't delete instructions with side effects. 00054 bool SawStore = false; 00055 if (!MI->isSafeToMove(TII, SawStore)) 00056 return false; 00057 00058 // Examine each operand. 00059 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 00060 const MachineOperand &MO = MI->getOperand(i); 00061 if (MO.isReg() && MO.isDef()) { 00062 unsigned Reg = MO.getReg(); 00063 if (TargetRegisterInfo::isPhysicalRegister(Reg) ? 00064 LivePhysRegs[Reg] : !MRI->use_empty(Reg)) { 00065 // This def has a use. Don't delete the instruction! 00066 return false; 00067 } 00068 } 00069 } 00070 00071 // If there are no defs with uses, the instruction is dead. 00072 return true; 00073 } 00074 00075 bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) { 00076 bool AnyChanges = false; 00077 MRI = &MF.getRegInfo(); 00078 TRI = MF.getTarget().getRegisterInfo(); 00079 TII = MF.getTarget().getInstrInfo(); 00080 00081 // Compute a bitvector to represent all non-allocatable physregs. 00082 BitVector NonAllocatableRegs = TRI->getAllocatableSet(MF); 00083 NonAllocatableRegs.flip(); 00084 00085 // Loop over all instructions in all blocks, from bottom to top, so that it's 00086 // more likely that chains of dependent but ultimately dead instructions will 00087 // be cleaned up. 00088 for (MachineFunction::reverse_iterator I = MF.rbegin(), E = MF.rend(); 00089 I != E; ++I) { 00090 MachineBasicBlock *MBB = &*I; 00091 00092 // Start out assuming that all non-allocatable registers are live 00093 // out of this block. 00094 LivePhysRegs = NonAllocatableRegs; 00095 00096 // Also add any explicit live-out physregs for this block. 00097 if (!MBB->empty() && MBB->back().getDesc().isReturn()) 00098 for (MachineRegisterInfo::liveout_iterator LOI = MRI->liveout_begin(), 00099 LOE = MRI->liveout_end(); LOI != LOE; ++LOI) { 00100 unsigned Reg = *LOI; 00101 if (TargetRegisterInfo::isPhysicalRegister(Reg)) 00102 LivePhysRegs.set(Reg); 00103 } 00104 00105 // Now scan the instructions and delete dead ones, tracking physreg 00106 // liveness as we go. 00107 for (MachineBasicBlock::reverse_iterator MII = MBB->rbegin(), 00108 MIE = MBB->rend(); MII != MIE; ) { 00109 MachineInstr *MI = &*MII; 00110 00111 // If the instruction is dead, delete it! 00112 if (isDead(MI)) { 00113 DOUT << "DeadMachineInstructionElim: DELETING: " << *MI; 00114 AnyChanges = true; 00115 MI->eraseFromParent(); 00116 MIE = MBB->rend(); 00117 // MII is now pointing to the next instruction to process, 00118 // so don't increment it. 00119 continue; 00120 } 00121 00122 // Record the physreg defs. 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 unsigned Reg = MO.getReg(); 00127 if (Reg != 0 && TargetRegisterInfo::isPhysicalRegister(Reg)) { 00128 LivePhysRegs.reset(Reg); 00129 // Check the subreg set, not the alias set, because a def 00130 // of a super-register may still be partially live after 00131 // this def. 00132 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg); 00133 *SubRegs; ++SubRegs) 00134 LivePhysRegs.reset(*SubRegs); 00135 } 00136 } 00137 } 00138 // Record the physreg uses, after the defs, in case a physreg is 00139 // both defined and used in the same instruction. 00140 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 00141 const MachineOperand &MO = MI->getOperand(i); 00142 if (MO.isReg() && MO.isUse()) { 00143 unsigned Reg = MO.getReg(); 00144 if (Reg != 0 && TargetRegisterInfo::isPhysicalRegister(Reg)) { 00145 LivePhysRegs.set(Reg); 00146 for (const unsigned *AliasSet = TRI->getAliasSet(Reg); 00147 *AliasSet; ++AliasSet) 00148 LivePhysRegs.set(*AliasSet); 00149 } 00150 } 00151 } 00152 00153 // We didn't delete the current instruction, so increment MII to 00154 // the next one. 00155 ++MII; 00156 } 00157 } 00158 00159 LivePhysRegs.clear(); 00160 return AnyChanges; 00161 }