Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 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 | |
Evan Cheng | 00a99a3 | 2010-02-06 09:07:11 +0000 | [diff] [blame] | 14 | #define DEBUG_TYPE "codegen-dce" |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/Passes.h" |
| 16 | #include "llvm/Pass.h" |
| 17 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 18 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 723ac37 | 2008-09-25 01:06:50 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
Bill Wendling | 9311a22 | 2009-08-22 20:04:03 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetInstrInfo.h" |
| 22 | #include "llvm/Target/TargetMachine.h" |
Evan Cheng | 00a99a3 | 2010-02-06 09:07:11 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Statistic.h" |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Evan Cheng | 00a99a3 | 2010-02-06 09:07:11 +0000 | [diff] [blame] | 26 | STATISTIC(NumDeletes, "Number of dead instructions deleted"); |
| 27 | |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 28 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 29 | class DeadMachineInstructionElim : public MachineFunctionPass { |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 30 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 31 | |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 32 | const TargetRegisterInfo *TRI; |
| 33 | const MachineRegisterInfo *MRI; |
| 34 | const TargetInstrInfo *TII; |
| 35 | BitVector LivePhysRegs; |
| 36 | |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 37 | public: |
| 38 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 9ccaf53 | 2010-08-05 23:42:04 +0000 | [diff] [blame] | 39 | DeadMachineInstructionElim() : MachineFunctionPass(ID) {} |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 40 | |
| 41 | private: |
Dan Gohman | d443ee6 | 2009-08-11 15:13:43 +0000 | [diff] [blame] | 42 | bool isDead(const MachineInstr *MI) const; |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 43 | }; |
| 44 | } |
| 45 | char DeadMachineInstructionElim::ID = 0; |
| 46 | |
Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 47 | INITIALIZE_PASS(DeadMachineInstructionElim, "dead-mi-elimination", |
| 48 | "Remove dead machine instructions", false, false); |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 49 | |
| 50 | FunctionPass *llvm::createDeadMachineInstructionElimPass() { |
| 51 | return new DeadMachineInstructionElim(); |
| 52 | } |
| 53 | |
Dan Gohman | d443ee6 | 2009-08-11 15:13:43 +0000 | [diff] [blame] | 54 | bool DeadMachineInstructionElim::isDead(const MachineInstr *MI) const { |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 55 | // Don't delete instructions with side effects. |
| 56 | bool SawStore = false; |
Evan Cheng | ac1abde | 2010-03-02 19:03:01 +0000 | [diff] [blame] | 57 | if (!MI->isSafeToMove(TII, 0, SawStore) && !MI->isPHI()) |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 58 | return false; |
| 59 | |
| 60 | // Examine each operand. |
| 61 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 62 | const MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 63 | if (MO.isReg() && MO.isDef()) { |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 64 | unsigned Reg = MO.getReg(); |
| 65 | if (TargetRegisterInfo::isPhysicalRegister(Reg) ? |
Dale Johannesen | 2d1ec73 | 2010-02-12 18:40:17 +0000 | [diff] [blame] | 66 | LivePhysRegs[Reg] : !MRI->use_nodbg_empty(Reg)) { |
| 67 | // This def has a non-debug use. Don't delete the instruction! |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 68 | return false; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // If there are no defs with uses, the instruction is dead. |
| 74 | return true; |
| 75 | } |
| 76 | |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 77 | bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) { |
| 78 | bool AnyChanges = false; |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 79 | MRI = &MF.getRegInfo(); |
| 80 | TRI = MF.getTarget().getRegisterInfo(); |
| 81 | TII = MF.getTarget().getInstrInfo(); |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 82 | |
Dan Gohman | 8468d1a | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 83 | // Compute a bitvector to represent all non-allocatable physregs. |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 84 | BitVector NonAllocatableRegs = TRI->getAllocatableSet(MF); |
Dan Gohman | 8468d1a | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 85 | NonAllocatableRegs.flip(); |
| 86 | |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 87 | // Loop over all instructions in all blocks, from bottom to top, so that it's |
| 88 | // more likely that chains of dependent but ultimately dead instructions will |
| 89 | // be cleaned up. |
| 90 | for (MachineFunction::reverse_iterator I = MF.rbegin(), E = MF.rend(); |
| 91 | I != E; ++I) { |
| 92 | MachineBasicBlock *MBB = &*I; |
Dan Gohman | 8468d1a | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 93 | |
| 94 | // Start out assuming that all non-allocatable registers are live |
| 95 | // out of this block. |
| 96 | LivePhysRegs = NonAllocatableRegs; |
| 97 | |
| 98 | // Also add any explicit live-out physregs for this block. |
| 99 | if (!MBB->empty() && MBB->back().getDesc().isReturn()) |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 100 | for (MachineRegisterInfo::liveout_iterator LOI = MRI->liveout_begin(), |
| 101 | LOE = MRI->liveout_end(); LOI != LOE; ++LOI) { |
Dan Gohman | 8468d1a | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 102 | unsigned Reg = *LOI; |
| 103 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) |
| 104 | LivePhysRegs.set(Reg); |
| 105 | } |
| 106 | |
| 107 | // Now scan the instructions and delete dead ones, tracking physreg |
| 108 | // liveness as we go. |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 109 | for (MachineBasicBlock::reverse_iterator MII = MBB->rbegin(), |
| 110 | MIE = MBB->rend(); MII != MIE; ) { |
| 111 | MachineInstr *MI = &*MII; |
| 112 | |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 113 | // If the instruction is dead, delete it! |
| 114 | if (isDead(MI)) { |
David Greene | 26045e2 | 2010-01-04 19:10:20 +0000 | [diff] [blame] | 115 | DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << *MI); |
Dale Johannesen | 2d1ec73 | 2010-02-12 18:40:17 +0000 | [diff] [blame] | 116 | // It is possible that some DBG_VALUE instructions refer to this |
| 117 | // instruction. Examine each def operand for such references; |
| 118 | // if found, mark the DBG_VALUE as undef (but don't delete it). |
| 119 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 120 | const MachineOperand &MO = MI->getOperand(i); |
| 121 | if (!MO.isReg() || !MO.isDef()) |
| 122 | continue; |
| 123 | unsigned Reg = MO.getReg(); |
| 124 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
| 125 | continue; |
| 126 | MachineRegisterInfo::use_iterator nextI; |
| 127 | for (MachineRegisterInfo::use_iterator I = MRI->use_begin(Reg), |
| 128 | E = MRI->use_end(); I!=E; I=nextI) { |
| 129 | nextI = llvm::next(I); // I is invalidated by the setReg |
| 130 | MachineOperand& Use = I.getOperand(); |
| 131 | MachineInstr *UseMI = Use.getParent(); |
| 132 | if (UseMI==MI) |
| 133 | continue; |
| 134 | assert(Use.isDebug()); |
| 135 | UseMI->getOperand(0).setReg(0U); |
| 136 | } |
| 137 | } |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 138 | AnyChanges = true; |
| 139 | MI->eraseFromParent(); |
Evan Cheng | 00a99a3 | 2010-02-06 09:07:11 +0000 | [diff] [blame] | 140 | ++NumDeletes; |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 141 | MIE = MBB->rend(); |
| 142 | // MII is now pointing to the next instruction to process, |
| 143 | // so don't increment it. |
| 144 | continue; |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 145 | } |
Dan Gohman | 8468d1a | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 146 | |
| 147 | // Record the physreg defs. |
| 148 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 149 | const MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 150 | if (MO.isReg() && MO.isDef()) { |
Dan Gohman | 8468d1a | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 151 | unsigned Reg = MO.getReg(); |
| 152 | if (Reg != 0 && TargetRegisterInfo::isPhysicalRegister(Reg)) { |
| 153 | LivePhysRegs.reset(Reg); |
Dan Gohman | b382c4d | 2008-10-16 00:11:23 +0000 | [diff] [blame] | 154 | // Check the subreg set, not the alias set, because a def |
| 155 | // of a super-register may still be partially live after |
| 156 | // this def. |
Dan Gohman | 131161b | 2008-10-16 01:06:18 +0000 | [diff] [blame] | 157 | for (const unsigned *SubRegs = TRI->getSubRegisters(Reg); |
| 158 | *SubRegs; ++SubRegs) |
| 159 | LivePhysRegs.reset(*SubRegs); |
Dan Gohman | 8468d1a | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | } |
| 163 | // Record the physreg uses, after the defs, in case a physreg is |
| 164 | // both defined and used in the same instruction. |
| 165 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 166 | const MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 167 | if (MO.isReg() && MO.isUse()) { |
Dan Gohman | 8468d1a | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 168 | unsigned Reg = MO.getReg(); |
| 169 | if (Reg != 0 && TargetRegisterInfo::isPhysicalRegister(Reg)) { |
| 170 | LivePhysRegs.set(Reg); |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 171 | for (const unsigned *AliasSet = TRI->getAliasSet(Reg); |
Dan Gohman | 8468d1a | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 172 | *AliasSet; ++AliasSet) |
| 173 | LivePhysRegs.set(*AliasSet); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 178 | // We didn't delete the current instruction, so increment MII to |
| 179 | // the next one. |
| 180 | ++MII; |
| 181 | } |
| 182 | } |
| 183 | |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 184 | LivePhysRegs.clear(); |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 185 | return AnyChanges; |
| 186 | } |