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