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 | |
| 14 | #include "llvm/CodeGen/Passes.h" |
| 15 | #include "llvm/Pass.h" |
| 16 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 17 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 18 | #include "llvm/Support/Compiler.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" |
| 23 | using namespace llvm; |
| 24 | |
| 25 | namespace { |
| 26 | class VISIBILITY_HIDDEN DeadMachineInstructionElim : |
| 27 | public MachineFunctionPass { |
| 28 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 29 | |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 30 | const TargetRegisterInfo *TRI; |
| 31 | const MachineRegisterInfo *MRI; |
| 32 | const TargetInstrInfo *TII; |
| 33 | BitVector LivePhysRegs; |
| 34 | |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 35 | public: |
| 36 | static char ID; // Pass identification, replacement for typeid |
| 37 | DeadMachineInstructionElim() : MachineFunctionPass(&ID) {} |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 38 | |
| 39 | private: |
Dan Gohman | d443ee6 | 2009-08-11 15:13:43 +0000 | [diff] [blame] | 40 | bool isDead(const MachineInstr *MI) const; |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 41 | }; |
| 42 | } |
| 43 | char DeadMachineInstructionElim::ID = 0; |
| 44 | |
| 45 | static RegisterPass<DeadMachineInstructionElim> |
| 46 | Y("dead-mi-elimination", |
| 47 | "Remove dead machine instructions"); |
| 48 | |
| 49 | FunctionPass *llvm::createDeadMachineInstructionElimPass() { |
| 50 | return new DeadMachineInstructionElim(); |
| 51 | } |
| 52 | |
Dan Gohman | d443ee6 | 2009-08-11 15:13:43 +0000 | [diff] [blame] | 53 | bool DeadMachineInstructionElim::isDead(const MachineInstr *MI) const { |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 54 | // Don't delete instructions with side effects. |
| 55 | bool SawStore = false; |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame^] | 56 | if (!MI->isSafeToMove(TII, SawStore, 0)) |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 57 | return false; |
| 58 | |
| 59 | // Examine each operand. |
| 60 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 61 | const MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 62 | if (MO.isReg() && MO.isDef()) { |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 63 | unsigned Reg = MO.getReg(); |
| 64 | if (TargetRegisterInfo::isPhysicalRegister(Reg) ? |
| 65 | LivePhysRegs[Reg] : !MRI->use_empty(Reg)) { |
| 66 | // This def has a use. Don't delete the instruction! |
| 67 | return false; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // If there are no defs with uses, the instruction is dead. |
| 73 | return true; |
| 74 | } |
| 75 | |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 76 | bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) { |
| 77 | bool AnyChanges = false; |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 78 | MRI = &MF.getRegInfo(); |
| 79 | TRI = MF.getTarget().getRegisterInfo(); |
| 80 | TII = MF.getTarget().getInstrInfo(); |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 81 | |
Dan Gohman | 8468d1a | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 82 | // Compute a bitvector to represent all non-allocatable physregs. |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 83 | BitVector NonAllocatableRegs = TRI->getAllocatableSet(MF); |
Dan Gohman | 8468d1a | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 84 | NonAllocatableRegs.flip(); |
| 85 | |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 86 | // Loop over all instructions in all blocks, from bottom to top, so that it's |
| 87 | // more likely that chains of dependent but ultimately dead instructions will |
| 88 | // be cleaned up. |
| 89 | for (MachineFunction::reverse_iterator I = MF.rbegin(), E = MF.rend(); |
| 90 | I != E; ++I) { |
| 91 | MachineBasicBlock *MBB = &*I; |
Dan Gohman | 8468d1a | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 92 | |
| 93 | // Start out assuming that all non-allocatable registers are live |
| 94 | // out of this block. |
| 95 | LivePhysRegs = NonAllocatableRegs; |
| 96 | |
| 97 | // Also add any explicit live-out physregs for this block. |
| 98 | if (!MBB->empty() && MBB->back().getDesc().isReturn()) |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 99 | for (MachineRegisterInfo::liveout_iterator LOI = MRI->liveout_begin(), |
| 100 | LOE = MRI->liveout_end(); LOI != LOE; ++LOI) { |
Dan Gohman | 8468d1a | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 101 | unsigned Reg = *LOI; |
| 102 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) |
| 103 | LivePhysRegs.set(Reg); |
| 104 | } |
| 105 | |
| 106 | // Now scan the instructions and delete dead ones, tracking physreg |
| 107 | // liveness as we go. |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 108 | for (MachineBasicBlock::reverse_iterator MII = MBB->rbegin(), |
| 109 | MIE = MBB->rend(); MII != MIE; ) { |
| 110 | MachineInstr *MI = &*MII; |
| 111 | |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 112 | // If the instruction is dead, delete it! |
| 113 | if (isDead(MI)) { |
Bill Wendling | 9311a22 | 2009-08-22 20:04:03 +0000 | [diff] [blame] | 114 | DEBUG(errs() << "DeadMachineInstructionElim: DELETING: " << *MI); |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 115 | AnyChanges = true; |
| 116 | MI->eraseFromParent(); |
| 117 | MIE = MBB->rend(); |
| 118 | // MII is now pointing to the next instruction to process, |
| 119 | // so don't increment it. |
| 120 | continue; |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 121 | } |
Dan Gohman | 8468d1a | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 122 | |
| 123 | // Record the physreg defs. |
| 124 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 125 | const MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 126 | if (MO.isReg() && MO.isDef()) { |
Dan Gohman | 8468d1a | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 127 | unsigned Reg = MO.getReg(); |
| 128 | if (Reg != 0 && TargetRegisterInfo::isPhysicalRegister(Reg)) { |
| 129 | LivePhysRegs.reset(Reg); |
Dan Gohman | b382c4d | 2008-10-16 00:11:23 +0000 | [diff] [blame] | 130 | // Check the subreg set, not the alias set, because a def |
| 131 | // of a super-register may still be partially live after |
| 132 | // this def. |
Dan Gohman | 131161b | 2008-10-16 01:06:18 +0000 | [diff] [blame] | 133 | for (const unsigned *SubRegs = TRI->getSubRegisters(Reg); |
| 134 | *SubRegs; ++SubRegs) |
| 135 | LivePhysRegs.reset(*SubRegs); |
Dan Gohman | 8468d1a | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | } |
| 139 | // Record the physreg uses, after the defs, in case a physreg is |
| 140 | // both defined and used in the same instruction. |
| 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.isUse()) { |
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.set(Reg); |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 147 | for (const unsigned *AliasSet = TRI->getAliasSet(Reg); |
Dan Gohman | 8468d1a | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 148 | *AliasSet; ++AliasSet) |
| 149 | LivePhysRegs.set(*AliasSet); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 154 | // We didn't delete the current instruction, so increment MII to |
| 155 | // the next one. |
| 156 | ++MII; |
| 157 | } |
| 158 | } |
| 159 | |
Dan Gohman | 3d84a76 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 160 | LivePhysRegs.clear(); |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 161 | return AnyChanges; |
| 162 | } |