blob: 36c7de67f5d8299f390e3c3bc32824c72d58d4ae [file] [log] [blame]
Dan Gohmanc24cd012008-09-17 00:43:24 +00001//===- DeadMachineInstructionElim.cpp - Remove dead machine instructions --===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Dan Gohmanc24cd012008-09-17 00:43:24 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This is an extremely simple MachineInstr-level dead-code-elimination pass.
10//
11//===----------------------------------------------------------------------===//
12
Chandler Carruthed0881b2012-12-03 16:50:05 +000013#include "llvm/ADT/Statistic.h"
Dan Gohmanc24cd012008-09-17 00:43:24 +000014#include "llvm/CodeGen/MachineFunctionPass.h"
15#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "llvm/CodeGen/Passes.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000017#include "llvm/CodeGen/TargetSubtargetInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/Pass.h"
Dan Gohmanb873aa62008-09-25 01:06:50 +000019#include "llvm/Support/Debug.h"
Bill Wendlingb4471732009-08-22 20:04:03 +000020#include "llvm/Support/raw_ostream.h"
Eric Christopherd9134482014-08-04 21:25:23 +000021
Dan Gohmanc24cd012008-09-17 00:43:24 +000022using namespace llvm;
23
Matthias Braun1527baa2017-05-25 21:26:32 +000024#define DEBUG_TYPE "dead-mi-elimination"
Chandler Carruth1b9dde02014-04-22 02:02:50 +000025
Evan Chengea5c6be2010-02-06 09:07:11 +000026STATISTIC(NumDeletes, "Number of dead instructions deleted");
27
Dan Gohmanc24cd012008-09-17 00:43:24 +000028namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000029 class DeadMachineInstructionElim : public MachineFunctionPass {
Craig Topper4584cd52014-03-07 09:26:03 +000030 bool runOnMachineFunction(MachineFunction &MF) override;
Andrew Trick9e761992012-02-08 21:22:43 +000031
Dan Gohman6b33aa42008-09-24 00:27:38 +000032 const TargetRegisterInfo *TRI;
33 const MachineRegisterInfo *MRI;
34 const TargetInstrInfo *TII;
35 BitVector LivePhysRegs;
36
Dan Gohmanc24cd012008-09-17 00:43:24 +000037 public:
38 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000039 DeadMachineInstructionElim() : MachineFunctionPass(ID) {
40 initializeDeadMachineInstructionElimPass(*PassRegistry::getPassRegistry());
41 }
Dan Gohman6b33aa42008-09-24 00:27:38 +000042
Matt Arsenault6b3e2122016-06-21 23:01:17 +000043 void getAnalysisUsage(AnalysisUsage &AU) const override {
44 AU.setPreservesCFG();
45 MachineFunctionPass::getAnalysisUsage(AU);
46 }
47
Dan Gohman6b33aa42008-09-24 00:27:38 +000048 private:
Dan Gohmane02f9ba2009-08-11 15:13:43 +000049 bool isDead(const MachineInstr *MI) const;
Dan Gohmanc24cd012008-09-17 00:43:24 +000050 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000051}
Dan Gohmanc24cd012008-09-17 00:43:24 +000052char DeadMachineInstructionElim::ID = 0;
Andrew Trick1fa5bcb2012-02-08 21:23:13 +000053char &llvm::DeadMachineInstructionElimID = DeadMachineInstructionElim::ID;
Dan Gohmanc24cd012008-09-17 00:43:24 +000054
Matthias Braun1527baa2017-05-25 21:26:32 +000055INITIALIZE_PASS(DeadMachineInstructionElim, DEBUG_TYPE,
Owen Andersondf7a4f22010-10-07 22:25:06 +000056 "Remove dead machine instructions", false, false)
Dan Gohmanc24cd012008-09-17 00:43:24 +000057
Dan Gohmane02f9ba2009-08-11 15:13:43 +000058bool DeadMachineInstructionElim::isDead(const MachineInstr *MI) const {
Evan Cheng6eb516d2011-01-07 23:50:32 +000059 // Technically speaking inline asm without side effects and no defs can still
60 // be deleted. But there is so much bad inline asm code out there, we should
61 // let them be.
62 if (MI->isInlineAsm())
63 return false;
64
Reid Klecknere9b89312015-01-13 00:48:10 +000065 // Don't delete frame allocation labels.
Reid Kleckner60381792015-07-07 22:25:32 +000066 if (MI->getOpcode() == TargetOpcode::LOCAL_ESCAPE)
Reid Klecknere9b89312015-01-13 00:48:10 +000067 return false;
68
Dan Gohman6b33aa42008-09-24 00:27:38 +000069 // Don't delete instructions with side effects.
70 bool SawStore = false;
Matthias Braun07066cc2015-05-19 21:22:20 +000071 if (!MI->isSafeToMove(nullptr, SawStore) && !MI->isPHI())
Dan Gohman6b33aa42008-09-24 00:27:38 +000072 return false;
73
74 // Examine each operand.
75 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
76 const MachineOperand &MO = MI->getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +000077 if (MO.isReg() && MO.isDef()) {
Dan Gohman6b33aa42008-09-24 00:27:38 +000078 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen5d332912012-02-09 00:15:39 +000079 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
80 // Don't delete live physreg defs, or any reserved register defs.
Jakob Stoklund Olesenc30a9af2012-10-15 21:57:41 +000081 if (LivePhysRegs.test(Reg) || MRI->isReserved(Reg))
Jakob Stoklund Olesen5d332912012-02-09 00:15:39 +000082 return false;
83 } else {
84 if (!MRI->use_nodbg_empty(Reg))
85 // This def has a non-debug use. Don't delete the instruction!
86 return false;
Dan Gohman6b33aa42008-09-24 00:27:38 +000087 }
88 }
89 }
90
91 // If there are no defs with uses, the instruction is dead.
92 return true;
93}
94
Dan Gohmanc24cd012008-09-17 00:43:24 +000095bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) {
Matthias Braunf1caa282017-12-15 22:22:58 +000096 if (skipFunction(MF.getFunction()))
Paul Robinson7c99ec52014-03-31 17:43:35 +000097 return false;
98
Dan Gohmanc24cd012008-09-17 00:43:24 +000099 bool AnyChanges = false;
Dan Gohman6b33aa42008-09-24 00:27:38 +0000100 MRI = &MF.getRegInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +0000101 TRI = MF.getSubtarget().getRegisterInfo();
102 TII = MF.getSubtarget().getInstrInfo();
Dan Gohmanc24cd012008-09-17 00:43:24 +0000103
104 // Loop over all instructions in all blocks, from bottom to top, so that it's
105 // more likely that chains of dependent but ultimately dead instructions will
106 // be cleaned up.
Pete Cooper7679afd2015-07-24 21:13:43 +0000107 for (MachineBasicBlock &MBB : make_range(MF.rbegin(), MF.rend())) {
Jakob Stoklund Olesen7993dae72010-08-31 21:51:05 +0000108 // Start out assuming that reserved registers are live out of this block.
Jakob Stoklund Olesenc30a9af2012-10-15 21:57:41 +0000109 LivePhysRegs = MRI->getReservedRegs();
Dan Gohman269999c2008-09-23 21:40:44 +0000110
Matt Arsenault900b21c2017-02-15 22:19:06 +0000111 // Add live-ins from successors to LivePhysRegs. Normally, physregs are not
Jakob Stoklund Olesen79f1b712011-06-27 15:00:36 +0000112 // live across blocks, but some targets (x86) can have flags live out of a
113 // block.
Pete Cooper7679afd2015-07-24 21:13:43 +0000114 for (MachineBasicBlock::succ_iterator S = MBB.succ_begin(),
115 E = MBB.succ_end(); S != E; S++)
Matthias Braund9da1622015-09-09 18:08:03 +0000116 for (const auto &LI : (*S)->liveins())
117 LivePhysRegs.set(LI.PhysReg);
Jakob Stoklund Olesen7993dae72010-08-31 21:51:05 +0000118
Dan Gohman269999c2008-09-23 21:40:44 +0000119 // Now scan the instructions and delete dead ones, tracking physreg
120 // liveness as we go.
Pete Cooper7679afd2015-07-24 21:13:43 +0000121 for (MachineBasicBlock::reverse_iterator MII = MBB.rbegin(),
122 MIE = MBB.rend(); MII != MIE; ) {
Duncan P. N. Exon Smith18720962016-09-11 18:51:28 +0000123 MachineInstr *MI = &*MII++;
Dan Gohmanc24cd012008-09-17 00:43:24 +0000124
Dan Gohman6b33aa42008-09-24 00:27:38 +0000125 // If the instruction is dead, delete it!
126 if (isDead(MI)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000127 LLVM_DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << *MI);
Dale Johannesen30d99f42010-02-12 18:40:17 +0000128 // It is possible that some DBG_VALUE instructions refer to this
Gerolf Hoflehnercaa8bfd2014-08-13 21:15:23 +0000129 // instruction. They get marked as undef and will be deleted
130 // in the live debug variable analysis.
131 MI->eraseFromParentAndMarkDBGValuesForRemoval();
Dan Gohman6b33aa42008-09-24 00:27:38 +0000132 AnyChanges = true;
Evan Chengea5c6be2010-02-06 09:07:11 +0000133 ++NumDeletes;
Dan Gohman6b33aa42008-09-24 00:27:38 +0000134 continue;
Dan Gohmanc24cd012008-09-17 00:43:24 +0000135 }
Dan Gohman269999c2008-09-23 21:40:44 +0000136
137 // Record the physreg defs.
138 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
139 const MachineOperand &MO = MI->getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000140 if (MO.isReg() && MO.isDef()) {
Dan Gohman269999c2008-09-23 21:40:44 +0000141 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000142 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Dan Gohmanbf293c42008-10-16 00:11:23 +0000143 // Check the subreg set, not the alias set, because a def
144 // of a super-register may still be partially live after
145 // this def.
Chad Rosierabdb1d62013-05-22 23:17:36 +0000146 for (MCSubRegIterator SR(Reg, TRI,/*IncludeSelf=*/true);
147 SR.isValid(); ++SR)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000148 LivePhysRegs.reset(*SR);
Dan Gohman269999c2008-09-23 21:40:44 +0000149 }
Jakob Stoklund Olesen58614f22012-01-20 22:27:09 +0000150 } else if (MO.isRegMask()) {
151 // Register mask of preserved registers. All clobbers are dead.
Jakob Stoklund Olesen5e1ac452012-02-02 23:52:57 +0000152 LivePhysRegs.clearBitsNotInMask(MO.getRegMask());
Dan Gohman269999c2008-09-23 21:40:44 +0000153 }
154 }
155 // Record the physreg uses, after the defs, in case a physreg is
156 // both defined and used in the same instruction.
157 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
158 const MachineOperand &MO = MI->getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000159 if (MO.isReg() && MO.isUse()) {
Dan Gohman269999c2008-09-23 21:40:44 +0000160 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000161 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen92a00832012-06-01 20:36:54 +0000162 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
163 LivePhysRegs.set(*AI);
Dan Gohman269999c2008-09-23 21:40:44 +0000164 }
165 }
166 }
Dan Gohmanc24cd012008-09-17 00:43:24 +0000167 }
168 }
169
Dan Gohman6b33aa42008-09-24 00:27:38 +0000170 LivePhysRegs.clear();
Dan Gohmanc24cd012008-09-17 00:43:24 +0000171 return AnyChanges;
172}