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 |
| 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 | |
| 47 | static RegisterPass<DeadMachineInstructionElim> |
| 48 | Y("dead-mi-elimination", |
| 49 | "Remove dead machine instructions"); |
| 50 | |
| 51 | FunctionPass *llvm::createDeadMachineInstructionElimPass() { |
| 52 | return new DeadMachineInstructionElim(); |
| 53 | } |
| 54 | |
Dan Gohman | d443ee6 | 2009-08-11 15:13:43 +0000 | [diff] [blame] | 55 | bool DeadMachineInstructionElim::isDead(const MachineInstr *MI) const { |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 56 | // Don't delete instructions with side effects. |
| 57 | bool SawStore = false; |
Bob Wilson | 5e2b05a | 2010-02-10 22:58:57 +0000 | [diff] [blame^] | 58 | if (!MI->isSafeToMove(TII, SawStore, 0) && !MI->isPHI()) |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 59 | return false; |
| 60 | |
| 61 | // Examine each operand. |
| 62 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 63 | const MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 64 | if (MO.isReg() && MO.isDef()) { |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 65 | unsigned Reg = MO.getReg(); |
| 66 | if (TargetRegisterInfo::isPhysicalRegister(Reg) ? |
| 67 | LivePhysRegs[Reg] : !MRI->use_empty(Reg)) { |
| 68 | // This def has a use. Don't delete the instruction! |
| 69 | return false; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // If there are no defs with uses, the instruction is dead. |
| 75 | return true; |
| 76 | } |
| 77 | |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 78 | bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) { |
| 79 | bool AnyChanges = false; |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 80 | MRI = &MF.getRegInfo(); |
| 81 | TRI = MF.getTarget().getRegisterInfo(); |
| 82 | TII = MF.getTarget().getInstrInfo(); |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 83 | |
Dan Gohman | 8468d1a | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 84 | // Compute a bitvector to represent all non-allocatable physregs. |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 85 | BitVector NonAllocatableRegs = TRI->getAllocatableSet(MF); |
Dan Gohman | 8468d1a | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 86 | NonAllocatableRegs.flip(); |
| 87 | |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 88 | // Loop over all instructions in all blocks, from bottom to top, so that it's |
| 89 | // more likely that chains of dependent but ultimately dead instructions will |
| 90 | // be cleaned up. |
| 91 | for (MachineFunction::reverse_iterator I = MF.rbegin(), E = MF.rend(); |
| 92 | I != E; ++I) { |
| 93 | MachineBasicBlock *MBB = &*I; |
Dan Gohman | 8468d1a | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 94 | |
| 95 | // Start out assuming that all non-allocatable registers are live |
| 96 | // out of this block. |
| 97 | LivePhysRegs = NonAllocatableRegs; |
| 98 | |
| 99 | // Also add any explicit live-out physregs for this block. |
| 100 | if (!MBB->empty() && MBB->back().getDesc().isReturn()) |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 101 | for (MachineRegisterInfo::liveout_iterator LOI = MRI->liveout_begin(), |
| 102 | LOE = MRI->liveout_end(); LOI != LOE; ++LOI) { |
Dan Gohman | 8468d1a | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 103 | unsigned Reg = *LOI; |
| 104 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) |
| 105 | LivePhysRegs.set(Reg); |
| 106 | } |
| 107 | |
| 108 | // Now scan the instructions and delete dead ones, tracking physreg |
| 109 | // liveness as we go. |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 110 | for (MachineBasicBlock::reverse_iterator MII = MBB->rbegin(), |
| 111 | MIE = MBB->rend(); MII != MIE; ) { |
| 112 | MachineInstr *MI = &*MII; |
| 113 | |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 114 | if (MI->isDebugValue()) { |
Dale Johannesen | 3b1906f | 2010-02-10 00:44:23 +0000 | [diff] [blame] | 115 | // Don't delete the DBG_VALUE itself, but if its Value operand is |
Dale Johannesen | 9514fcb | 2010-01-27 22:12:36 +0000 | [diff] [blame] | 116 | // a vreg and this is the only use, substitute an undef operand; |
| 117 | // the former operand will then be deleted normally. |
| 118 | if (MI->getNumOperands()==3 && MI->getOperand(0).isReg()) { |
| 119 | unsigned Reg = MI->getOperand(0).getReg(); |
| 120 | MachineRegisterInfo::use_iterator I = MRI->use_begin(Reg); |
| 121 | assert(I != MRI->use_end()); |
| 122 | if (++I == MRI->use_end()) |
Dale Johannesen | 3b1906f | 2010-02-10 00:44:23 +0000 | [diff] [blame] | 123 | // only one use, which must be this DBG_VALUE. |
Dale Johannesen | 9514fcb | 2010-01-27 22:12:36 +0000 | [diff] [blame] | 124 | MI->getOperand(0).setReg(0U); |
| 125 | } |
| 126 | } |
| 127 | |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 128 | // If the instruction is dead, delete it! |
| 129 | if (isDead(MI)) { |
David Greene | 26045e2 | 2010-01-04 19:10:20 +0000 | [diff] [blame] | 130 | DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << *MI); |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 131 | AnyChanges = true; |
| 132 | MI->eraseFromParent(); |
Evan Cheng | 00a99a3 | 2010-02-06 09:07:11 +0000 | [diff] [blame] | 133 | ++NumDeletes; |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 134 | MIE = MBB->rend(); |
| 135 | // MII is now pointing to the next instruction to process, |
| 136 | // so don't increment it. |
| 137 | continue; |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 138 | } |
Dan Gohman | 8468d1a | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 139 | |
| 140 | // Record the physreg defs. |
| 141 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 142 | const MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 143 | if (MO.isReg() && MO.isDef()) { |
Dan Gohman | 8468d1a | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 144 | unsigned Reg = MO.getReg(); |
| 145 | if (Reg != 0 && TargetRegisterInfo::isPhysicalRegister(Reg)) { |
| 146 | LivePhysRegs.reset(Reg); |
Dan Gohman | b382c4d | 2008-10-16 00:11:23 +0000 | [diff] [blame] | 147 | // Check the subreg set, not the alias set, because a def |
| 148 | // of a super-register may still be partially live after |
| 149 | // this def. |
Dan Gohman | 131161b | 2008-10-16 01:06:18 +0000 | [diff] [blame] | 150 | for (const unsigned *SubRegs = TRI->getSubRegisters(Reg); |
| 151 | *SubRegs; ++SubRegs) |
| 152 | LivePhysRegs.reset(*SubRegs); |
Dan Gohman | 8468d1a | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | } |
| 156 | // Record the physreg uses, after the defs, in case a physreg is |
| 157 | // both defined and used in the same instruction. |
| 158 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 159 | const MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 160 | if (MO.isReg() && MO.isUse()) { |
Dan Gohman | 8468d1a | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 161 | unsigned Reg = MO.getReg(); |
| 162 | if (Reg != 0 && TargetRegisterInfo::isPhysicalRegister(Reg)) { |
| 163 | LivePhysRegs.set(Reg); |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 164 | for (const unsigned *AliasSet = TRI->getAliasSet(Reg); |
Dan Gohman | 8468d1a | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 165 | *AliasSet; ++AliasSet) |
| 166 | LivePhysRegs.set(*AliasSet); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 171 | // We didn't delete the current instruction, so increment MII to |
| 172 | // the next one. |
| 173 | ++MII; |
| 174 | } |
| 175 | } |
| 176 | |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 177 | LivePhysRegs.clear(); |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 178 | return AnyChanges; |
| 179 | } |