blob: 049ce70633075c91098ade82b563966dffb42024 [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 {
Stanislav Mekhanoshin0a118292019-03-20 21:42:05 +000084 for (const MachineInstr &Use : MRI->use_nodbg_instructions(Reg)) {
85 if (&Use != MI)
86 // This def has a non-debug use. Don't delete the instruction!
87 return false;
88 }
Dan Gohman6b33aa42008-09-24 00:27:38 +000089 }
90 }
91 }
92
93 // If there are no defs with uses, the instruction is dead.
94 return true;
95}
96
Dan Gohmanc24cd012008-09-17 00:43:24 +000097bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) {
Matthias Braunf1caa282017-12-15 22:22:58 +000098 if (skipFunction(MF.getFunction()))
Paul Robinson7c99ec52014-03-31 17:43:35 +000099 return false;
100
Dan Gohmanc24cd012008-09-17 00:43:24 +0000101 bool AnyChanges = false;
Dan Gohman6b33aa42008-09-24 00:27:38 +0000102 MRI = &MF.getRegInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +0000103 TRI = MF.getSubtarget().getRegisterInfo();
104 TII = MF.getSubtarget().getInstrInfo();
Dan Gohmanc24cd012008-09-17 00:43:24 +0000105
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 Cooper7679afd2015-07-24 21:13:43 +0000109 for (MachineBasicBlock &MBB : make_range(MF.rbegin(), MF.rend())) {
Jakob Stoklund Olesen7993dae72010-08-31 21:51:05 +0000110 // Start out assuming that reserved registers are live out of this block.
Jakob Stoklund Olesenc30a9af2012-10-15 21:57:41 +0000111 LivePhysRegs = MRI->getReservedRegs();
Dan Gohman269999c2008-09-23 21:40:44 +0000112
Matt Arsenault900b21c2017-02-15 22:19:06 +0000113 // Add live-ins from successors to LivePhysRegs. Normally, physregs are not
Jakob Stoklund Olesen79f1b712011-06-27 15:00:36 +0000114 // live across blocks, but some targets (x86) can have flags live out of a
115 // block.
Pete Cooper7679afd2015-07-24 21:13:43 +0000116 for (MachineBasicBlock::succ_iterator S = MBB.succ_begin(),
117 E = MBB.succ_end(); S != E; S++)
Matthias Braund9da1622015-09-09 18:08:03 +0000118 for (const auto &LI : (*S)->liveins())
119 LivePhysRegs.set(LI.PhysReg);
Jakob Stoklund Olesen7993dae72010-08-31 21:51:05 +0000120
Dan Gohman269999c2008-09-23 21:40:44 +0000121 // Now scan the instructions and delete dead ones, tracking physreg
122 // liveness as we go.
Pete Cooper7679afd2015-07-24 21:13:43 +0000123 for (MachineBasicBlock::reverse_iterator MII = MBB.rbegin(),
124 MIE = MBB.rend(); MII != MIE; ) {
Duncan P. N. Exon Smith18720962016-09-11 18:51:28 +0000125 MachineInstr *MI = &*MII++;
Dan Gohmanc24cd012008-09-17 00:43:24 +0000126
Dan Gohman6b33aa42008-09-24 00:27:38 +0000127 // If the instruction is dead, delete it!
128 if (isDead(MI)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000129 LLVM_DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << *MI);
Dale Johannesen30d99f42010-02-12 18:40:17 +0000130 // It is possible that some DBG_VALUE instructions refer to this
Gerolf Hoflehnercaa8bfd2014-08-13 21:15:23 +0000131 // instruction. They get marked as undef and will be deleted
132 // in the live debug variable analysis.
133 MI->eraseFromParentAndMarkDBGValuesForRemoval();
Dan Gohman6b33aa42008-09-24 00:27:38 +0000134 AnyChanges = true;
Evan Chengea5c6be2010-02-06 09:07:11 +0000135 ++NumDeletes;
Dan Gohman6b33aa42008-09-24 00:27:38 +0000136 continue;
Dan Gohmanc24cd012008-09-17 00:43:24 +0000137 }
Dan Gohman269999c2008-09-23 21:40:44 +0000138
139 // Record the physreg defs.
140 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
141 const MachineOperand &MO = MI->getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000142 if (MO.isReg() && MO.isDef()) {
Dan Gohman269999c2008-09-23 21:40:44 +0000143 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000144 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Dan Gohmanbf293c42008-10-16 00:11:23 +0000145 // 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 Rosierabdb1d62013-05-22 23:17:36 +0000148 for (MCSubRegIterator SR(Reg, TRI,/*IncludeSelf=*/true);
149 SR.isValid(); ++SR)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000150 LivePhysRegs.reset(*SR);
Dan Gohman269999c2008-09-23 21:40:44 +0000151 }
Jakob Stoklund Olesen58614f22012-01-20 22:27:09 +0000152 } else if (MO.isRegMask()) {
153 // Register mask of preserved registers. All clobbers are dead.
Jakob Stoklund Olesen5e1ac452012-02-02 23:52:57 +0000154 LivePhysRegs.clearBitsNotInMask(MO.getRegMask());
Dan Gohman269999c2008-09-23 21:40:44 +0000155 }
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 Gohman0d1e9a82008-10-03 15:45:36 +0000161 if (MO.isReg() && MO.isUse()) {
Dan Gohman269999c2008-09-23 21:40:44 +0000162 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000163 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen92a00832012-06-01 20:36:54 +0000164 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
165 LivePhysRegs.set(*AI);
Dan Gohman269999c2008-09-23 21:40:44 +0000166 }
167 }
168 }
Dan Gohmanc24cd012008-09-17 00:43:24 +0000169 }
170 }
171
Dan Gohman6b33aa42008-09-24 00:27:38 +0000172 LivePhysRegs.clear();
Dan Gohmanc24cd012008-09-17 00:43:24 +0000173 return AnyChanges;
174}