blob: 2b144d89e651ec7c9cd439cf8700f0c4e569d6e7 [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
14#include "llvm/CodeGen/Passes.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "llvm/ADT/Statistic.h"
Dan Gohmand3ead432008-09-17 00:43:24 +000016#include "llvm/CodeGen/MachineFunctionPass.h"
17#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "llvm/Pass.h"
Dan Gohman723ac372008-09-25 01:06:50 +000019#include "llvm/Support/Debug.h"
Bill Wendling9311a222009-08-22 20:04:03 +000020#include "llvm/Support/raw_ostream.h"
Dan Gohmand3ead432008-09-17 00:43:24 +000021#include "llvm/Target/TargetInstrInfo.h"
22#include "llvm/Target/TargetMachine.h"
23using namespace llvm;
24
Stephen Hinesdce4a402014-05-29 02:49:00 -070025#define DEBUG_TYPE "codegen-dce"
26
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 {
Stephen Hines36b56882014-04-23 16:57:46 -070031 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
44 private:
Dan Gohmand443ee62009-08-11 15:13:43 +000045 bool isDead(const MachineInstr *MI) const;
Dan Gohmand3ead432008-09-17 00:43:24 +000046 };
47}
48char DeadMachineInstructionElim::ID = 0;
Andrew Trick1dd8c852012-02-08 21:23:13 +000049char &llvm::DeadMachineInstructionElimID = DeadMachineInstructionElim::ID;
Dan Gohmand3ead432008-09-17 00:43:24 +000050
Owen Andersond13db2c2010-07-21 22:09:45 +000051INITIALIZE_PASS(DeadMachineInstructionElim, "dead-mi-elimination",
Owen Andersonce665bd2010-10-07 22:25:06 +000052 "Remove dead machine instructions", false, false)
Dan Gohmand3ead432008-09-17 00:43:24 +000053
Dan Gohmand443ee62009-08-11 15:13:43 +000054bool DeadMachineInstructionElim::isDead(const MachineInstr *MI) const {
Evan Chengc36b7062011-01-07 23:50:32 +000055 // Technically speaking inline asm without side effects and no defs can still
56 // be deleted. But there is so much bad inline asm code out there, we should
57 // let them be.
58 if (MI->isInlineAsm())
59 return false;
60
Dan Gohman3d84a762008-09-24 00:27:38 +000061 // Don't delete instructions with side effects.
62 bool SawStore = false;
Stephen Hinesdce4a402014-05-29 02:49:00 -070063 if (!MI->isSafeToMove(TII, nullptr, SawStore) && !MI->isPHI())
Dan Gohman3d84a762008-09-24 00:27:38 +000064 return false;
65
66 // Examine each operand.
67 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
68 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +000069 if (MO.isReg() && MO.isDef()) {
Dan Gohman3d84a762008-09-24 00:27:38 +000070 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen39284d12012-02-09 00:15:39 +000071 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
72 // Don't delete live physreg defs, or any reserved register defs.
Jakob Stoklund Olesenfb9ebbf2012-10-15 21:57:41 +000073 if (LivePhysRegs.test(Reg) || MRI->isReserved(Reg))
Jakob Stoklund Olesen39284d12012-02-09 00:15:39 +000074 return false;
75 } else {
76 if (!MRI->use_nodbg_empty(Reg))
77 // This def has a non-debug use. Don't delete the instruction!
78 return false;
Dan Gohman3d84a762008-09-24 00:27:38 +000079 }
80 }
81 }
82
83 // If there are no defs with uses, the instruction is dead.
84 return true;
85}
86
Dan Gohmand3ead432008-09-17 00:43:24 +000087bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) {
Stephen Hines36b56882014-04-23 16:57:46 -070088 if (skipOptnoneFunction(*MF.getFunction()))
89 return false;
90
Dan Gohmand3ead432008-09-17 00:43:24 +000091 bool AnyChanges = false;
Dan Gohman3d84a762008-09-24 00:27:38 +000092 MRI = &MF.getRegInfo();
93 TRI = MF.getTarget().getRegisterInfo();
94 TII = MF.getTarget().getInstrInfo();
Dan Gohmand3ead432008-09-17 00:43:24 +000095
96 // Loop over all instructions in all blocks, from bottom to top, so that it's
97 // more likely that chains of dependent but ultimately dead instructions will
98 // be cleaned up.
99 for (MachineFunction::reverse_iterator I = MF.rbegin(), E = MF.rend();
100 I != E; ++I) {
101 MachineBasicBlock *MBB = &*I;
Dan Gohman8468d1a2008-09-23 21:40:44 +0000102
Jakob Stoklund Olesenf14a6482010-08-31 21:51:05 +0000103 // Start out assuming that reserved registers are live out of this block.
Jakob Stoklund Olesenfb9ebbf2012-10-15 21:57:41 +0000104 LivePhysRegs = MRI->getReservedRegs();
Dan Gohman8468d1a2008-09-23 21:40:44 +0000105
Jakob Stoklund Olesenf27229e2011-06-27 15:00:36 +0000106 // Add live-ins from sucessors to LivePhysRegs. Normally, physregs are not
107 // live across blocks, but some targets (x86) can have flags live out of a
108 // block.
109 for (MachineBasicBlock::succ_iterator S = MBB->succ_begin(),
110 E = MBB->succ_end(); S != E; S++)
111 for (MachineBasicBlock::livein_iterator LI = (*S)->livein_begin();
112 LI != (*S)->livein_end(); LI++)
113 LivePhysRegs.set(*LI);
Jakob Stoklund Olesenf14a6482010-08-31 21:51:05 +0000114
Dan Gohman8468d1a2008-09-23 21:40:44 +0000115 // Now scan the instructions and delete dead ones, tracking physreg
116 // liveness as we go.
Dan Gohmand3ead432008-09-17 00:43:24 +0000117 for (MachineBasicBlock::reverse_iterator MII = MBB->rbegin(),
118 MIE = MBB->rend(); MII != MIE; ) {
119 MachineInstr *MI = &*MII;
120
Dan Gohman3d84a762008-09-24 00:27:38 +0000121 // If the instruction is dead, delete it!
122 if (isDead(MI)) {
David Greene26045e22010-01-04 19:10:20 +0000123 DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << *MI);
Dale Johannesen2d1ec732010-02-12 18:40:17 +0000124 // It is possible that some DBG_VALUE instructions refer to this
125 // instruction. Examine each def operand for such references;
126 // if found, mark the DBG_VALUE as undef (but don't delete it).
127 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
128 const MachineOperand &MO = MI->getOperand(i);
129 if (!MO.isReg() || !MO.isDef())
130 continue;
131 unsigned Reg = MO.getReg();
132 if (!TargetRegisterInfo::isVirtualRegister(Reg))
133 continue;
Stephen Hines36b56882014-04-23 16:57:46 -0700134 MRI->markUsesInDebugValueAsUndef(Reg);
Dale Johannesen2d1ec732010-02-12 18:40:17 +0000135 }
Dan Gohman3d84a762008-09-24 00:27:38 +0000136 AnyChanges = true;
137 MI->eraseFromParent();
Evan Cheng00a99a32010-02-06 09:07:11 +0000138 ++NumDeletes;
Dan Gohman3d84a762008-09-24 00:27:38 +0000139 MIE = MBB->rend();
140 // MII is now pointing to the next instruction to process,
141 // so don't increment it.
142 continue;
Dan Gohmand3ead432008-09-17 00:43:24 +0000143 }
Dan Gohman8468d1a2008-09-23 21:40:44 +0000144
145 // Record the physreg defs.
146 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
147 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000148 if (MO.isReg() && MO.isDef()) {
Dan Gohman8468d1a2008-09-23 21:40:44 +0000149 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000150 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Dan Gohmanb382c4d2008-10-16 00:11:23 +0000151 // Check the subreg set, not the alias set, because a def
152 // of a super-register may still be partially live after
153 // this def.
Chad Rosier62c320a2013-05-22 23:17:36 +0000154 for (MCSubRegIterator SR(Reg, TRI,/*IncludeSelf=*/true);
155 SR.isValid(); ++SR)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000156 LivePhysRegs.reset(*SR);
Dan Gohman8468d1a2008-09-23 21:40:44 +0000157 }
Jakob Stoklund Olesen6b88c182012-01-20 22:27:09 +0000158 } else if (MO.isRegMask()) {
159 // Register mask of preserved registers. All clobbers are dead.
Jakob Stoklund Olesen478a8a02012-02-02 23:52:57 +0000160 LivePhysRegs.clearBitsNotInMask(MO.getRegMask());
Dan Gohman8468d1a2008-09-23 21:40:44 +0000161 }
162 }
163 // Record the physreg uses, after the defs, in case a physreg is
164 // both defined and used in the same instruction.
165 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
166 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000167 if (MO.isReg() && MO.isUse()) {
Dan Gohman8468d1a2008-09-23 21:40:44 +0000168 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000169 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesenf152fe82012-06-01 20:36:54 +0000170 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
171 LivePhysRegs.set(*AI);
Dan Gohman8468d1a2008-09-23 21:40:44 +0000172 }
173 }
174 }
175
Dan Gohmand3ead432008-09-17 00:43:24 +0000176 // We didn't delete the current instruction, so increment MII to
177 // the next one.
178 ++MII;
179 }
180 }
181
Dan Gohman3d84a762008-09-24 00:27:38 +0000182 LivePhysRegs.clear();
Dan Gohmand3ead432008-09-17 00:43:24 +0000183 return AnyChanges;
184}