blob: e6a54bb300f2cae235790c388de7713d1ba8f1c8 [file] [log] [blame]
Dan Gohmanc24cd012008-09-17 00:43:24 +00001//===- 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
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/ADT/Statistic.h"
Dan Gohmanc24cd012008-09-17 00:43:24 +000015#include "llvm/CodeGen/MachineFunctionPass.h"
16#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000017#include "llvm/CodeGen/Passes.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000018#include "llvm/CodeGen/TargetSubtargetInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/Pass.h"
Dan Gohmanb873aa62008-09-25 01:06:50 +000020#include "llvm/Support/Debug.h"
Bill Wendlingb4471732009-08-22 20:04:03 +000021#include "llvm/Support/raw_ostream.h"
Eric Christopherd9134482014-08-04 21:25:23 +000022
Dan Gohmanc24cd012008-09-17 00:43:24 +000023using namespace llvm;
24
Matthias Braun1527baa2017-05-25 21:26:32 +000025#define DEBUG_TYPE "dead-mi-elimination"
Chandler Carruth1b9dde02014-04-22 02:02:50 +000026
Evan Chengea5c6be2010-02-06 09:07:11 +000027STATISTIC(NumDeletes, "Number of dead instructions deleted");
28
Dan Gohmanc24cd012008-09-17 00:43:24 +000029namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000030 class DeadMachineInstructionElim : public MachineFunctionPass {
Craig Topper4584cd52014-03-07 09:26:03 +000031 bool runOnMachineFunction(MachineFunction &MF) override;
Andrew Trick9e761992012-02-08 21:22:43 +000032
Dan Gohman6b33aa42008-09-24 00:27:38 +000033 const TargetRegisterInfo *TRI;
34 const MachineRegisterInfo *MRI;
35 const TargetInstrInfo *TII;
36 BitVector LivePhysRegs;
37
Dan Gohmanc24cd012008-09-17 00:43:24 +000038 public:
39 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000040 DeadMachineInstructionElim() : MachineFunctionPass(ID) {
41 initializeDeadMachineInstructionElimPass(*PassRegistry::getPassRegistry());
42 }
Dan Gohman6b33aa42008-09-24 00:27:38 +000043
Matt Arsenault6b3e2122016-06-21 23:01:17 +000044 void getAnalysisUsage(AnalysisUsage &AU) const override {
45 AU.setPreservesCFG();
46 MachineFunctionPass::getAnalysisUsage(AU);
47 }
48
Dan Gohman6b33aa42008-09-24 00:27:38 +000049 private:
Dan Gohmane02f9ba2009-08-11 15:13:43 +000050 bool isDead(const MachineInstr *MI) const;
Dan Gohmanc24cd012008-09-17 00:43:24 +000051 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000052}
Dan Gohmanc24cd012008-09-17 00:43:24 +000053char DeadMachineInstructionElim::ID = 0;
Andrew Trick1fa5bcb2012-02-08 21:23:13 +000054char &llvm::DeadMachineInstructionElimID = DeadMachineInstructionElim::ID;
Dan Gohmanc24cd012008-09-17 00:43:24 +000055
Matthias Braun1527baa2017-05-25 21:26:32 +000056INITIALIZE_PASS(DeadMachineInstructionElim, DEBUG_TYPE,
Owen Andersondf7a4f22010-10-07 22:25:06 +000057 "Remove dead machine instructions", false, false)
Dan Gohmanc24cd012008-09-17 00:43:24 +000058
Dan Gohmane02f9ba2009-08-11 15:13:43 +000059bool DeadMachineInstructionElim::isDead(const MachineInstr *MI) const {
Evan Cheng6eb516d2011-01-07 23:50:32 +000060 // Technically speaking inline asm without side effects and no defs can still
61 // be deleted. But there is so much bad inline asm code out there, we should
62 // let them be.
63 if (MI->isInlineAsm())
64 return false;
65
Reid Klecknere9b89312015-01-13 00:48:10 +000066 // Don't delete frame allocation labels.
Reid Kleckner60381792015-07-07 22:25:32 +000067 if (MI->getOpcode() == TargetOpcode::LOCAL_ESCAPE)
Reid Klecknere9b89312015-01-13 00:48:10 +000068 return false;
69
Dan Gohman6b33aa42008-09-24 00:27:38 +000070 // Don't delete instructions with side effects.
71 bool SawStore = false;
Matthias Braun07066cc2015-05-19 21:22:20 +000072 if (!MI->isSafeToMove(nullptr, SawStore) && !MI->isPHI())
Dan Gohman6b33aa42008-09-24 00:27:38 +000073 return false;
74
75 // Examine each operand.
76 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
77 const MachineOperand &MO = MI->getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +000078 if (MO.isReg() && MO.isDef()) {
Dan Gohman6b33aa42008-09-24 00:27:38 +000079 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen5d332912012-02-09 00:15:39 +000080 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
81 // Don't delete live physreg defs, or any reserved register defs.
Jakob Stoklund Olesenc30a9af2012-10-15 21:57:41 +000082 if (LivePhysRegs.test(Reg) || MRI->isReserved(Reg))
Jakob Stoklund Olesen5d332912012-02-09 00:15:39 +000083 return false;
84 } else {
85 if (!MRI->use_nodbg_empty(Reg))
86 // This def has a non-debug use. Don't delete the instruction!
87 return false;
Dan Gohman6b33aa42008-09-24 00:27:38 +000088 }
89 }
90 }
91
92 // If there are no defs with uses, the instruction is dead.
93 return true;
94}
95
Dan Gohmanc24cd012008-09-17 00:43:24 +000096bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) {
Matthias Braunf1caa282017-12-15 22:22:58 +000097 if (skipFunction(MF.getFunction()))
Paul Robinson7c99ec52014-03-31 17:43:35 +000098 return false;
99
Dan Gohmanc24cd012008-09-17 00:43:24 +0000100 bool AnyChanges = false;
Dan Gohman6b33aa42008-09-24 00:27:38 +0000101 MRI = &MF.getRegInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +0000102 TRI = MF.getSubtarget().getRegisterInfo();
103 TII = MF.getSubtarget().getInstrInfo();
Dan Gohmanc24cd012008-09-17 00:43:24 +0000104
105 // Loop over all instructions in all blocks, from bottom to top, so that it's
106 // more likely that chains of dependent but ultimately dead instructions will
107 // be cleaned up.
Pete Cooper7679afd2015-07-24 21:13:43 +0000108 for (MachineBasicBlock &MBB : make_range(MF.rbegin(), MF.rend())) {
Jakob Stoklund Olesen7993dae72010-08-31 21:51:05 +0000109 // Start out assuming that reserved registers are live out of this block.
Jakob Stoklund Olesenc30a9af2012-10-15 21:57:41 +0000110 LivePhysRegs = MRI->getReservedRegs();
Dan Gohman269999c2008-09-23 21:40:44 +0000111
Matt Arsenault900b21c2017-02-15 22:19:06 +0000112 // Add live-ins from successors to LivePhysRegs. Normally, physregs are not
Jakob Stoklund Olesen79f1b712011-06-27 15:00:36 +0000113 // live across blocks, but some targets (x86) can have flags live out of a
114 // block.
Pete Cooper7679afd2015-07-24 21:13:43 +0000115 for (MachineBasicBlock::succ_iterator S = MBB.succ_begin(),
116 E = MBB.succ_end(); S != E; S++)
Matthias Braund9da1622015-09-09 18:08:03 +0000117 for (const auto &LI : (*S)->liveins())
118 LivePhysRegs.set(LI.PhysReg);
Jakob Stoklund Olesen7993dae72010-08-31 21:51:05 +0000119
Dan Gohman269999c2008-09-23 21:40:44 +0000120 // Now scan the instructions and delete dead ones, tracking physreg
121 // liveness as we go.
Pete Cooper7679afd2015-07-24 21:13:43 +0000122 for (MachineBasicBlock::reverse_iterator MII = MBB.rbegin(),
123 MIE = MBB.rend(); MII != MIE; ) {
Duncan P. N. Exon Smith18720962016-09-11 18:51:28 +0000124 MachineInstr *MI = &*MII++;
Dan Gohmanc24cd012008-09-17 00:43:24 +0000125
Dan Gohman6b33aa42008-09-24 00:27:38 +0000126 // If the instruction is dead, delete it!
127 if (isDead(MI)) {
David Greene7af1efc2010-01-04 19:10:20 +0000128 DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << *MI);
Dale Johannesen30d99f42010-02-12 18:40:17 +0000129 // It is possible that some DBG_VALUE instructions refer to this
Gerolf Hoflehnercaa8bfd2014-08-13 21:15:23 +0000130 // instruction. They get marked as undef and will be deleted
131 // in the live debug variable analysis.
132 MI->eraseFromParentAndMarkDBGValuesForRemoval();
Dan Gohman6b33aa42008-09-24 00:27:38 +0000133 AnyChanges = true;
Evan Chengea5c6be2010-02-06 09:07:11 +0000134 ++NumDeletes;
Dan Gohman6b33aa42008-09-24 00:27:38 +0000135 continue;
Dan Gohmanc24cd012008-09-17 00:43:24 +0000136 }
Dan Gohman269999c2008-09-23 21:40:44 +0000137
138 // Record the physreg defs.
139 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
140 const MachineOperand &MO = MI->getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000141 if (MO.isReg() && MO.isDef()) {
Dan Gohman269999c2008-09-23 21:40:44 +0000142 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000143 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Dan Gohmanbf293c42008-10-16 00:11:23 +0000144 // Check the subreg set, not the alias set, because a def
145 // of a super-register may still be partially live after
146 // this def.
Chad Rosierabdb1d62013-05-22 23:17:36 +0000147 for (MCSubRegIterator SR(Reg, TRI,/*IncludeSelf=*/true);
148 SR.isValid(); ++SR)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000149 LivePhysRegs.reset(*SR);
Dan Gohman269999c2008-09-23 21:40:44 +0000150 }
Jakob Stoklund Olesen58614f22012-01-20 22:27:09 +0000151 } else if (MO.isRegMask()) {
152 // Register mask of preserved registers. All clobbers are dead.
Jakob Stoklund Olesen5e1ac452012-02-02 23:52:57 +0000153 LivePhysRegs.clearBitsNotInMask(MO.getRegMask());
Dan Gohman269999c2008-09-23 21:40:44 +0000154 }
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 Gohman0d1e9a82008-10-03 15:45:36 +0000160 if (MO.isReg() && MO.isUse()) {
Dan Gohman269999c2008-09-23 21:40:44 +0000161 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000162 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen92a00832012-06-01 20:36:54 +0000163 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
164 LivePhysRegs.set(*AI);
Dan Gohman269999c2008-09-23 21:40:44 +0000165 }
166 }
167 }
Dan Gohmanc24cd012008-09-17 00:43:24 +0000168 }
169 }
170
Dan Gohman6b33aa42008-09-24 00:27:38 +0000171 LivePhysRegs.clear();
Dan Gohmanc24cd012008-09-17 00:43:24 +0000172 return AnyChanges;
173}