blob: ff44c5660baddb766d4ed73b20d9e835443b551d [file] [log] [blame]
Dan Gohmand3ead432008-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 Carruthd04a8d42012-12-03 16:50:05 +000014#include "llvm/ADT/Statistic.h"
Dan Gohmand3ead432008-09-17 00:43:24 +000015#include "llvm/CodeGen/MachineFunctionPass.h"
16#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruthe3e43d92017-06-06 11:49:48 +000017#include "llvm/CodeGen/Passes.h"
David Blaikiee3a9b4c2017-11-17 01:07:10 +000018#include "llvm/CodeGen/TargetSubtargetInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/Pass.h"
Dan Gohman723ac372008-09-25 01:06:50 +000020#include "llvm/Support/Debug.h"
Bill Wendling9311a222009-08-22 20:04:03 +000021#include "llvm/Support/raw_ostream.h"
Eric Christopher9f85dcc2014-08-04 21:25:23 +000022
Dan Gohmand3ead432008-09-17 00:43:24 +000023using namespace llvm;
24
Matthias Braun94c49042017-05-25 21:26:32 +000025#define DEBUG_TYPE "dead-mi-elimination"
Chandler Carruth8677f2f2014-04-22 02:02:50 +000026
Evan Cheng00a99a32010-02-06 09:07:11 +000027STATISTIC(NumDeletes, "Number of dead instructions deleted");
28
Dan Gohmand3ead432008-09-17 00:43:24 +000029namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000030 class DeadMachineInstructionElim : public MachineFunctionPass {
Craig Topper9f998de2014-03-07 09:26:03 +000031 bool runOnMachineFunction(MachineFunction &MF) override;
Andrew Trick1df91b02012-02-08 21:22:43 +000032
Dan Gohman3d84a762008-09-24 00:27:38 +000033 const TargetRegisterInfo *TRI;
34 const MachineRegisterInfo *MRI;
35 const TargetInstrInfo *TII;
36 BitVector LivePhysRegs;
37
Dan Gohmand3ead432008-09-17 00:43:24 +000038 public:
39 static char ID; // Pass identification, replacement for typeid
Owen Anderson081c34b2010-10-19 17:21:58 +000040 DeadMachineInstructionElim() : MachineFunctionPass(ID) {
41 initializeDeadMachineInstructionElimPass(*PassRegistry::getPassRegistry());
42 }
Dan Gohman3d84a762008-09-24 00:27:38 +000043
Matt Arsenaultb24e29e2016-06-21 23:01:17 +000044 void getAnalysisUsage(AnalysisUsage &AU) const override {
45 AU.setPreservesCFG();
46 MachineFunctionPass::getAnalysisUsage(AU);
47 }
48
Dan Gohman3d84a762008-09-24 00:27:38 +000049 private:
Dan Gohmand443ee62009-08-11 15:13:43 +000050 bool isDead(const MachineInstr *MI) const;
Dan Gohmand3ead432008-09-17 00:43:24 +000051 };
Alexander Kornienkocd52a7a2015-06-23 09:49:53 +000052}
Dan Gohmand3ead432008-09-17 00:43:24 +000053char DeadMachineInstructionElim::ID = 0;
Andrew Trick1dd8c852012-02-08 21:23:13 +000054char &llvm::DeadMachineInstructionElimID = DeadMachineInstructionElim::ID;
Dan Gohmand3ead432008-09-17 00:43:24 +000055
Matthias Braun94c49042017-05-25 21:26:32 +000056INITIALIZE_PASS(DeadMachineInstructionElim, DEBUG_TYPE,
Owen Andersonce665bd2010-10-07 22:25:06 +000057 "Remove dead machine instructions", false, false)
Dan Gohmand3ead432008-09-17 00:43:24 +000058
Dan Gohmand443ee62009-08-11 15:13:43 +000059bool DeadMachineInstructionElim::isDead(const MachineInstr *MI) const {
Evan Chengc36b7062011-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 Kleckner221a7072015-01-13 00:48:10 +000066 // Don't delete frame allocation labels.
Reid Kleckner8f32e5f2015-07-07 22:25:32 +000067 if (MI->getOpcode() == TargetOpcode::LOCAL_ESCAPE)
Reid Kleckner221a7072015-01-13 00:48:10 +000068 return false;
69
Dan Gohman3d84a762008-09-24 00:27:38 +000070 // Don't delete instructions with side effects.
71 bool SawStore = false;
Matthias Braundfc41db2015-05-19 21:22:20 +000072 if (!MI->isSafeToMove(nullptr, SawStore) && !MI->isPHI())
Dan Gohman3d84a762008-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 Gohmand735b802008-10-03 15:45:36 +000078 if (MO.isReg() && MO.isDef()) {
Dan Gohman3d84a762008-09-24 00:27:38 +000079 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen39284d12012-02-09 00:15:39 +000080 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
81 // Don't delete live physreg defs, or any reserved register defs.
Jakob Stoklund Olesenfb9ebbf2012-10-15 21:57:41 +000082 if (LivePhysRegs.test(Reg) || MRI->isReserved(Reg))
Jakob Stoklund Olesen39284d12012-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 Gohman3d84a762008-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 Gohmand3ead432008-09-17 00:43:24 +000096bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) {
Matthias Braund3181392017-12-15 22:22:58 +000097 if (skipFunction(MF.getFunction()))
Paul Robinson5fa58a52014-03-31 17:43:35 +000098 return false;
99
Dan Gohmand3ead432008-09-17 00:43:24 +0000100 bool AnyChanges = false;
Dan Gohman3d84a762008-09-24 00:27:38 +0000101 MRI = &MF.getRegInfo();
Eric Christopher60355182014-08-05 02:39:49 +0000102 TRI = MF.getSubtarget().getRegisterInfo();
103 TII = MF.getSubtarget().getInstrInfo();
Dan Gohmand3ead432008-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 Cooper9f2f1652015-07-24 21:13:43 +0000108 for (MachineBasicBlock &MBB : make_range(MF.rbegin(), MF.rend())) {
Jakob Stoklund Olesenf14a6482010-08-31 21:51:05 +0000109 // Start out assuming that reserved registers are live out of this block.
Jakob Stoklund Olesenfb9ebbf2012-10-15 21:57:41 +0000110 LivePhysRegs = MRI->getReservedRegs();
Dan Gohman8468d1a2008-09-23 21:40:44 +0000111
Matt Arsenaultad35e222017-02-15 22:19:06 +0000112 // Add live-ins from successors to LivePhysRegs. Normally, physregs are not
Jakob Stoklund Olesenf27229e2011-06-27 15:00:36 +0000113 // live across blocks, but some targets (x86) can have flags live out of a
114 // block.
Pete Cooper9f2f1652015-07-24 21:13:43 +0000115 for (MachineBasicBlock::succ_iterator S = MBB.succ_begin(),
116 E = MBB.succ_end(); S != E; S++)
Matthias Braunaf5ff602015-09-09 18:08:03 +0000117 for (const auto &LI : (*S)->liveins())
118 LivePhysRegs.set(LI.PhysReg);
Jakob Stoklund Olesenf14a6482010-08-31 21:51:05 +0000119
Dan Gohman8468d1a2008-09-23 21:40:44 +0000120 // Now scan the instructions and delete dead ones, tracking physreg
121 // liveness as we go.
Pete Cooper9f2f1652015-07-24 21:13:43 +0000122 for (MachineBasicBlock::reverse_iterator MII = MBB.rbegin(),
123 MIE = MBB.rend(); MII != MIE; ) {
Duncan P. N. Exon Smithd546df82016-09-11 18:51:28 +0000124 MachineInstr *MI = &*MII++;
Dan Gohmand3ead432008-09-17 00:43:24 +0000125
Dan Gohman3d84a762008-09-24 00:27:38 +0000126 // If the instruction is dead, delete it!
127 if (isDead(MI)) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000128 LLVM_DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << *MI);
Dale Johannesen2d1ec732010-02-12 18:40:17 +0000129 // It is possible that some DBG_VALUE instructions refer to this
Gerolf Hoflehner4e917a22014-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 Gohman3d84a762008-09-24 00:27:38 +0000133 AnyChanges = true;
Evan Cheng00a99a32010-02-06 09:07:11 +0000134 ++NumDeletes;
Dan Gohman3d84a762008-09-24 00:27:38 +0000135 continue;
Dan Gohmand3ead432008-09-17 00:43:24 +0000136 }
Dan Gohman8468d1a2008-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 Gohmand735b802008-10-03 15:45:36 +0000141 if (MO.isReg() && MO.isDef()) {
Dan Gohman8468d1a2008-09-23 21:40:44 +0000142 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000143 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Dan Gohmanb382c4d2008-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 Rosier62c320a2013-05-22 23:17:36 +0000147 for (MCSubRegIterator SR(Reg, TRI,/*IncludeSelf=*/true);
148 SR.isValid(); ++SR)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000149 LivePhysRegs.reset(*SR);
Dan Gohman8468d1a2008-09-23 21:40:44 +0000150 }
Jakob Stoklund Olesen6b88c182012-01-20 22:27:09 +0000151 } else if (MO.isRegMask()) {
152 // Register mask of preserved registers. All clobbers are dead.
Jakob Stoklund Olesen478a8a02012-02-02 23:52:57 +0000153 LivePhysRegs.clearBitsNotInMask(MO.getRegMask());
Dan Gohman8468d1a2008-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 Gohmand735b802008-10-03 15:45:36 +0000160 if (MO.isReg() && MO.isUse()) {
Dan Gohman8468d1a2008-09-23 21:40:44 +0000161 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000162 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesenf152fe82012-06-01 20:36:54 +0000163 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
164 LivePhysRegs.set(*AI);
Dan Gohman8468d1a2008-09-23 21:40:44 +0000165 }
166 }
167 }
Dan Gohmand3ead432008-09-17 00:43:24 +0000168 }
169 }
170
Dan Gohman3d84a762008-09-24 00:27:38 +0000171 LivePhysRegs.clear();
Dan Gohmand3ead432008-09-17 00:43:24 +0000172 return AnyChanges;
173}