blob: 09b5e931b6ff13b0449fd59941fdb82de926aaf0 [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
14#include "llvm/CodeGen/Passes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/Statistic.h"
Dan Gohmanc24cd012008-09-17 00:43:24 +000016#include "llvm/CodeGen/MachineFunctionPass.h"
17#include "llvm/CodeGen/MachineRegisterInfo.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"
Dan Gohmanc24cd012008-09-17 00:43:24 +000021#include "llvm/Target/TargetInstrInfo.h"
22#include "llvm/Target/TargetMachine.h"
Eric Christopherd9134482014-08-04 21:25:23 +000023#include "llvm/Target/TargetSubtargetInfo.h"
24
Dan Gohmanc24cd012008-09-17 00:43:24 +000025using namespace llvm;
26
Chandler Carruth1b9dde02014-04-22 02:02:50 +000027#define DEBUG_TYPE "codegen-dce"
28
Evan Chengea5c6be2010-02-06 09:07:11 +000029STATISTIC(NumDeletes, "Number of dead instructions deleted");
30
Dan Gohmanc24cd012008-09-17 00:43:24 +000031namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000032 class DeadMachineInstructionElim : public MachineFunctionPass {
Craig Topper4584cd52014-03-07 09:26:03 +000033 bool runOnMachineFunction(MachineFunction &MF) override;
Andrew Trick9e761992012-02-08 21:22:43 +000034
Dan Gohman6b33aa42008-09-24 00:27:38 +000035 const TargetRegisterInfo *TRI;
36 const MachineRegisterInfo *MRI;
37 const TargetInstrInfo *TII;
38 BitVector LivePhysRegs;
39
Dan Gohmanc24cd012008-09-17 00:43:24 +000040 public:
41 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000042 DeadMachineInstructionElim() : MachineFunctionPass(ID) {
43 initializeDeadMachineInstructionElimPass(*PassRegistry::getPassRegistry());
44 }
Dan Gohman6b33aa42008-09-24 00:27:38 +000045
46 private:
Dan Gohmane02f9ba2009-08-11 15:13:43 +000047 bool isDead(const MachineInstr *MI) const;
Dan Gohmanc24cd012008-09-17 00:43:24 +000048 };
49}
50char DeadMachineInstructionElim::ID = 0;
Andrew Trick1fa5bcb2012-02-08 21:23:13 +000051char &llvm::DeadMachineInstructionElimID = DeadMachineInstructionElim::ID;
Dan Gohmanc24cd012008-09-17 00:43:24 +000052
Owen Andersona57b97e2010-07-21 22:09:45 +000053INITIALIZE_PASS(DeadMachineInstructionElim, "dead-mi-elimination",
Owen Andersondf7a4f22010-10-07 22:25:06 +000054 "Remove dead machine instructions", false, false)
Dan Gohmanc24cd012008-09-17 00:43:24 +000055
Dan Gohmane02f9ba2009-08-11 15:13:43 +000056bool DeadMachineInstructionElim::isDead(const MachineInstr *MI) const {
Evan Cheng6eb516d2011-01-07 23:50:32 +000057 // Technically speaking inline asm without side effects and no defs can still
58 // be deleted. But there is so much bad inline asm code out there, we should
59 // let them be.
60 if (MI->isInlineAsm())
61 return false;
62
Dan Gohman6b33aa42008-09-24 00:27:38 +000063 // Don't delete instructions with side effects.
64 bool SawStore = false;
Craig Topperc0196b12014-04-14 00:51:57 +000065 if (!MI->isSafeToMove(TII, nullptr, SawStore) && !MI->isPHI())
Dan Gohman6b33aa42008-09-24 00:27:38 +000066 return false;
67
68 // Examine each operand.
69 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
70 const MachineOperand &MO = MI->getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +000071 if (MO.isReg() && MO.isDef()) {
Dan Gohman6b33aa42008-09-24 00:27:38 +000072 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen5d332912012-02-09 00:15:39 +000073 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
74 // Don't delete live physreg defs, or any reserved register defs.
Jakob Stoklund Olesenc30a9af2012-10-15 21:57:41 +000075 if (LivePhysRegs.test(Reg) || MRI->isReserved(Reg))
Jakob Stoklund Olesen5d332912012-02-09 00:15:39 +000076 return false;
77 } else {
78 if (!MRI->use_nodbg_empty(Reg))
79 // This def has a non-debug use. Don't delete the instruction!
80 return false;
Dan Gohman6b33aa42008-09-24 00:27:38 +000081 }
82 }
83 }
84
85 // If there are no defs with uses, the instruction is dead.
86 return true;
87}
88
Dan Gohmanc24cd012008-09-17 00:43:24 +000089bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) {
Paul Robinson7c99ec52014-03-31 17:43:35 +000090 if (skipOptnoneFunction(*MF.getFunction()))
91 return false;
92
Dan Gohmanc24cd012008-09-17 00:43:24 +000093 bool AnyChanges = false;
Dan Gohman6b33aa42008-09-24 00:27:38 +000094 MRI = &MF.getRegInfo();
Eric Christopherd9134482014-08-04 21:25:23 +000095 TRI = MF.getTarget().getSubtargetImpl()->getRegisterInfo();
96 TII = MF.getTarget().getSubtargetImpl()->getInstrInfo();
Dan Gohmanc24cd012008-09-17 00:43:24 +000097
98 // Loop over all instructions in all blocks, from bottom to top, so that it's
99 // more likely that chains of dependent but ultimately dead instructions will
100 // be cleaned up.
101 for (MachineFunction::reverse_iterator I = MF.rbegin(), E = MF.rend();
102 I != E; ++I) {
103 MachineBasicBlock *MBB = &*I;
Dan Gohman269999c2008-09-23 21:40:44 +0000104
Jakob Stoklund Olesen7993dae72010-08-31 21:51:05 +0000105 // Start out assuming that reserved registers are live out of this block.
Jakob Stoklund Olesenc30a9af2012-10-15 21:57:41 +0000106 LivePhysRegs = MRI->getReservedRegs();
Dan Gohman269999c2008-09-23 21:40:44 +0000107
Jakob Stoklund Olesen79f1b712011-06-27 15:00:36 +0000108 // Add live-ins from sucessors to LivePhysRegs. Normally, physregs are not
109 // live across blocks, but some targets (x86) can have flags live out of a
110 // block.
111 for (MachineBasicBlock::succ_iterator S = MBB->succ_begin(),
112 E = MBB->succ_end(); S != E; S++)
113 for (MachineBasicBlock::livein_iterator LI = (*S)->livein_begin();
114 LI != (*S)->livein_end(); LI++)
115 LivePhysRegs.set(*LI);
Jakob Stoklund Olesen7993dae72010-08-31 21:51:05 +0000116
Dan Gohman269999c2008-09-23 21:40:44 +0000117 // Now scan the instructions and delete dead ones, tracking physreg
118 // liveness as we go.
Dan Gohmanc24cd012008-09-17 00:43:24 +0000119 for (MachineBasicBlock::reverse_iterator MII = MBB->rbegin(),
120 MIE = MBB->rend(); MII != MIE; ) {
121 MachineInstr *MI = &*MII;
122
Dan Gohman6b33aa42008-09-24 00:27:38 +0000123 // If the instruction is dead, delete it!
124 if (isDead(MI)) {
David Greene7af1efc2010-01-04 19:10:20 +0000125 DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << *MI);
Dale Johannesen30d99f42010-02-12 18:40:17 +0000126 // It is possible that some DBG_VALUE instructions refer to this
127 // instruction. Examine each def operand for such references;
128 // if found, mark the DBG_VALUE as undef (but don't delete it).
129 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
130 const MachineOperand &MO = MI->getOperand(i);
131 if (!MO.isReg() || !MO.isDef())
132 continue;
133 unsigned Reg = MO.getReg();
134 if (!TargetRegisterInfo::isVirtualRegister(Reg))
135 continue;
Ekaterina Romanova8d620082014-03-13 18:47:12 +0000136 MRI->markUsesInDebugValueAsUndef(Reg);
Dale Johannesen30d99f42010-02-12 18:40:17 +0000137 }
Dan Gohman6b33aa42008-09-24 00:27:38 +0000138 AnyChanges = true;
139 MI->eraseFromParent();
Evan Chengea5c6be2010-02-06 09:07:11 +0000140 ++NumDeletes;
Dan Gohman6b33aa42008-09-24 00:27:38 +0000141 MIE = MBB->rend();
142 // MII is now pointing to the next instruction to process,
143 // so don't increment it.
144 continue;
Dan Gohmanc24cd012008-09-17 00:43:24 +0000145 }
Dan Gohman269999c2008-09-23 21:40:44 +0000146
147 // Record the physreg defs.
148 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
149 const MachineOperand &MO = MI->getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000150 if (MO.isReg() && MO.isDef()) {
Dan Gohman269999c2008-09-23 21:40:44 +0000151 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000152 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Dan Gohmanbf293c42008-10-16 00:11:23 +0000153 // Check the subreg set, not the alias set, because a def
154 // of a super-register may still be partially live after
155 // this def.
Chad Rosierabdb1d62013-05-22 23:17:36 +0000156 for (MCSubRegIterator SR(Reg, TRI,/*IncludeSelf=*/true);
157 SR.isValid(); ++SR)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000158 LivePhysRegs.reset(*SR);
Dan Gohman269999c2008-09-23 21:40:44 +0000159 }
Jakob Stoklund Olesen58614f22012-01-20 22:27:09 +0000160 } else if (MO.isRegMask()) {
161 // Register mask of preserved registers. All clobbers are dead.
Jakob Stoklund Olesen5e1ac452012-02-02 23:52:57 +0000162 LivePhysRegs.clearBitsNotInMask(MO.getRegMask());
Dan Gohman269999c2008-09-23 21:40:44 +0000163 }
164 }
165 // Record the physreg uses, after the defs, in case a physreg is
166 // both defined and used in the same instruction.
167 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
168 const MachineOperand &MO = MI->getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000169 if (MO.isReg() && MO.isUse()) {
Dan Gohman269999c2008-09-23 21:40:44 +0000170 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000171 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen92a00832012-06-01 20:36:54 +0000172 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
173 LivePhysRegs.set(*AI);
Dan Gohman269999c2008-09-23 21:40:44 +0000174 }
175 }
176 }
177
Dan Gohmanc24cd012008-09-17 00:43:24 +0000178 // We didn't delete the current instruction, so increment MII to
179 // the next one.
180 ++MII;
181 }
182 }
183
Dan Gohman6b33aa42008-09-24 00:27:38 +0000184 LivePhysRegs.clear();
Dan Gohmanc24cd012008-09-17 00:43:24 +0000185 return AnyChanges;
186}