Dan Gohman | c24cd01 | 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" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/Statistic.h" |
Dan Gohman | c24cd01 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 17 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/Pass.h" |
Dan Gohman | b873aa6 | 2008-09-25 01:06:50 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
Bill Wendling | b447173 | 2009-08-22 20:04:03 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | c24cd01 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetInstrInfo.h" |
| 22 | #include "llvm/Target/TargetMachine.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame^] | 23 | #include "llvm/Target/TargetSubtargetInfo.h" |
| 24 | |
Dan Gohman | c24cd01 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 27 | #define DEBUG_TYPE "codegen-dce" |
| 28 | |
Evan Cheng | ea5c6be | 2010-02-06 09:07:11 +0000 | [diff] [blame] | 29 | STATISTIC(NumDeletes, "Number of dead instructions deleted"); |
| 30 | |
Dan Gohman | c24cd01 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 31 | namespace { |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 32 | class DeadMachineInstructionElim : public MachineFunctionPass { |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 33 | bool runOnMachineFunction(MachineFunction &MF) override; |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 34 | |
Dan Gohman | 6b33aa4 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 35 | const TargetRegisterInfo *TRI; |
| 36 | const MachineRegisterInfo *MRI; |
| 37 | const TargetInstrInfo *TII; |
| 38 | BitVector LivePhysRegs; |
| 39 | |
Dan Gohman | c24cd01 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 40 | public: |
| 41 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 42 | DeadMachineInstructionElim() : MachineFunctionPass(ID) { |
| 43 | initializeDeadMachineInstructionElimPass(*PassRegistry::getPassRegistry()); |
| 44 | } |
Dan Gohman | 6b33aa4 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 45 | |
| 46 | private: |
Dan Gohman | e02f9ba | 2009-08-11 15:13:43 +0000 | [diff] [blame] | 47 | bool isDead(const MachineInstr *MI) const; |
Dan Gohman | c24cd01 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 48 | }; |
| 49 | } |
| 50 | char DeadMachineInstructionElim::ID = 0; |
Andrew Trick | 1fa5bcb | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 51 | char &llvm::DeadMachineInstructionElimID = DeadMachineInstructionElim::ID; |
Dan Gohman | c24cd01 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 52 | |
Owen Anderson | a57b97e | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 53 | INITIALIZE_PASS(DeadMachineInstructionElim, "dead-mi-elimination", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 54 | "Remove dead machine instructions", false, false) |
Dan Gohman | c24cd01 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 55 | |
Dan Gohman | e02f9ba | 2009-08-11 15:13:43 +0000 | [diff] [blame] | 56 | bool DeadMachineInstructionElim::isDead(const MachineInstr *MI) const { |
Evan Cheng | 6eb516d | 2011-01-07 23:50:32 +0000 | [diff] [blame] | 57 | // Technically speaking inline asm without side effects and no defs can still |
| 58 | // be deleted. But there is so much bad inline asm code out there, we should |
| 59 | // let them be. |
| 60 | if (MI->isInlineAsm()) |
| 61 | return false; |
| 62 | |
Dan Gohman | 6b33aa4 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 63 | // Don't delete instructions with side effects. |
| 64 | bool SawStore = false; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 65 | if (!MI->isSafeToMove(TII, nullptr, SawStore) && !MI->isPHI()) |
Dan Gohman | 6b33aa4 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 66 | return false; |
| 67 | |
| 68 | // Examine each operand. |
| 69 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 70 | const MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | 0d1e9a8 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 71 | if (MO.isReg() && MO.isDef()) { |
Dan Gohman | 6b33aa4 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 72 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | 5d33291 | 2012-02-09 00:15:39 +0000 | [diff] [blame] | 73 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
| 74 | // Don't delete live physreg defs, or any reserved register defs. |
Jakob Stoklund Olesen | c30a9af | 2012-10-15 21:57:41 +0000 | [diff] [blame] | 75 | if (LivePhysRegs.test(Reg) || MRI->isReserved(Reg)) |
Jakob Stoklund Olesen | 5d33291 | 2012-02-09 00:15:39 +0000 | [diff] [blame] | 76 | return false; |
| 77 | } else { |
| 78 | if (!MRI->use_nodbg_empty(Reg)) |
| 79 | // This def has a non-debug use. Don't delete the instruction! |
| 80 | return false; |
Dan Gohman | 6b33aa4 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // If there are no defs with uses, the instruction is dead. |
| 86 | return true; |
| 87 | } |
| 88 | |
Dan Gohman | c24cd01 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 89 | bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) { |
Paul Robinson | 7c99ec5 | 2014-03-31 17:43:35 +0000 | [diff] [blame] | 90 | if (skipOptnoneFunction(*MF.getFunction())) |
| 91 | return false; |
| 92 | |
Dan Gohman | c24cd01 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 93 | bool AnyChanges = false; |
Dan Gohman | 6b33aa4 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 94 | MRI = &MF.getRegInfo(); |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame^] | 95 | TRI = MF.getTarget().getSubtargetImpl()->getRegisterInfo(); |
| 96 | TII = MF.getTarget().getSubtargetImpl()->getInstrInfo(); |
Dan Gohman | c24cd01 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 97 | |
| 98 | // Loop over all instructions in all blocks, from bottom to top, so that it's |
| 99 | // more likely that chains of dependent but ultimately dead instructions will |
| 100 | // be cleaned up. |
| 101 | for (MachineFunction::reverse_iterator I = MF.rbegin(), E = MF.rend(); |
| 102 | I != E; ++I) { |
| 103 | MachineBasicBlock *MBB = &*I; |
Dan Gohman | 269999c | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 104 | |
Jakob Stoklund Olesen | 7993dae7 | 2010-08-31 21:51:05 +0000 | [diff] [blame] | 105 | // Start out assuming that reserved registers are live out of this block. |
Jakob Stoklund Olesen | c30a9af | 2012-10-15 21:57:41 +0000 | [diff] [blame] | 106 | LivePhysRegs = MRI->getReservedRegs(); |
Dan Gohman | 269999c | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 107 | |
Jakob Stoklund Olesen | 79f1b71 | 2011-06-27 15:00:36 +0000 | [diff] [blame] | 108 | // Add live-ins from sucessors to LivePhysRegs. Normally, physregs are not |
| 109 | // live across blocks, but some targets (x86) can have flags live out of a |
| 110 | // block. |
| 111 | for (MachineBasicBlock::succ_iterator S = MBB->succ_begin(), |
| 112 | E = MBB->succ_end(); S != E; S++) |
| 113 | for (MachineBasicBlock::livein_iterator LI = (*S)->livein_begin(); |
| 114 | LI != (*S)->livein_end(); LI++) |
| 115 | LivePhysRegs.set(*LI); |
Jakob Stoklund Olesen | 7993dae7 | 2010-08-31 21:51:05 +0000 | [diff] [blame] | 116 | |
Dan Gohman | 269999c | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 117 | // Now scan the instructions and delete dead ones, tracking physreg |
| 118 | // liveness as we go. |
Dan Gohman | c24cd01 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 119 | for (MachineBasicBlock::reverse_iterator MII = MBB->rbegin(), |
| 120 | MIE = MBB->rend(); MII != MIE; ) { |
| 121 | MachineInstr *MI = &*MII; |
| 122 | |
Dan Gohman | 6b33aa4 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 123 | // If the instruction is dead, delete it! |
| 124 | if (isDead(MI)) { |
David Greene | 7af1efc | 2010-01-04 19:10:20 +0000 | [diff] [blame] | 125 | DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << *MI); |
Dale Johannesen | 30d99f4 | 2010-02-12 18:40:17 +0000 | [diff] [blame] | 126 | // It is possible that some DBG_VALUE instructions refer to this |
| 127 | // instruction. Examine each def operand for such references; |
| 128 | // if found, mark the DBG_VALUE as undef (but don't delete it). |
| 129 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 130 | const MachineOperand &MO = MI->getOperand(i); |
| 131 | if (!MO.isReg() || !MO.isDef()) |
| 132 | continue; |
| 133 | unsigned Reg = MO.getReg(); |
| 134 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
| 135 | continue; |
Ekaterina Romanova | 8d62008 | 2014-03-13 18:47:12 +0000 | [diff] [blame] | 136 | MRI->markUsesInDebugValueAsUndef(Reg); |
Dale Johannesen | 30d99f4 | 2010-02-12 18:40:17 +0000 | [diff] [blame] | 137 | } |
Dan Gohman | 6b33aa4 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 138 | AnyChanges = true; |
| 139 | MI->eraseFromParent(); |
Evan Cheng | ea5c6be | 2010-02-06 09:07:11 +0000 | [diff] [blame] | 140 | ++NumDeletes; |
Dan Gohman | 6b33aa4 | 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 | c24cd01 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 145 | } |
Dan Gohman | 269999c | 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 | 0d1e9a8 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 150 | if (MO.isReg() && MO.isDef()) { |
Dan Gohman | 269999c | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 151 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | 2fb5b31 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 152 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
Dan Gohman | bf293c4 | 2008-10-16 00:11:23 +0000 | [diff] [blame] | 153 | // Check the subreg set, not the alias set, because a def |
| 154 | // of a super-register may still be partially live after |
| 155 | // this def. |
Chad Rosier | abdb1d6 | 2013-05-22 23:17:36 +0000 | [diff] [blame] | 156 | for (MCSubRegIterator SR(Reg, TRI,/*IncludeSelf=*/true); |
| 157 | SR.isValid(); ++SR) |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 158 | LivePhysRegs.reset(*SR); |
Dan Gohman | 269999c | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 159 | } |
Jakob Stoklund Olesen | 58614f2 | 2012-01-20 22:27:09 +0000 | [diff] [blame] | 160 | } else if (MO.isRegMask()) { |
| 161 | // Register mask of preserved registers. All clobbers are dead. |
Jakob Stoklund Olesen | 5e1ac45 | 2012-02-02 23:52:57 +0000 | [diff] [blame] | 162 | LivePhysRegs.clearBitsNotInMask(MO.getRegMask()); |
Dan Gohman | 269999c | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | // Record the physreg uses, after the defs, in case a physreg is |
| 166 | // both defined and used in the same instruction. |
| 167 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 168 | const MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | 0d1e9a8 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 169 | if (MO.isReg() && MO.isUse()) { |
Dan Gohman | 269999c | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 170 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | 2fb5b31 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 171 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
Jakob Stoklund Olesen | 92a0083 | 2012-06-01 20:36:54 +0000 | [diff] [blame] | 172 | for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) |
| 173 | LivePhysRegs.set(*AI); |
Dan Gohman | 269999c | 2008-09-23 21:40:44 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
Dan Gohman | c24cd01 | 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 | 6b33aa4 | 2008-09-24 00:27:38 +0000 | [diff] [blame] | 184 | LivePhysRegs.clear(); |
Dan Gohman | c24cd01 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 185 | return AnyChanges; |
| 186 | } |