blob: aa03e7752a2b967f899304c3a5122a7d7b604b5f [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
Evan Cheng00a99a32010-02-06 09:07:11 +000014#define DEBUG_TYPE "codegen-dce"
Dan Gohmand3ead432008-09-17 00:43:24 +000015#include "llvm/CodeGen/Passes.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/ADT/Statistic.h"
Dan Gohmand3ead432008-09-17 00:43:24 +000017#include "llvm/CodeGen/MachineFunctionPass.h"
18#include "llvm/CodeGen/MachineRegisterInfo.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"
Dan Gohmand3ead432008-09-17 00:43:24 +000022#include "llvm/Target/TargetInstrInfo.h"
23#include "llvm/Target/TargetMachine.h"
24using namespace llvm;
25
Evan Cheng00a99a32010-02-06 09:07:11 +000026STATISTIC(NumDeletes, "Number of dead instructions deleted");
27
Dan Gohmand3ead432008-09-17 00:43:24 +000028namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000029 class DeadMachineInstructionElim : public MachineFunctionPass {
Stephen Hines36b56882014-04-23 16:57:46 -070030 bool runOnMachineFunction(MachineFunction &MF) override;
Andrew Trick1df91b02012-02-08 21:22:43 +000031
Dan Gohman3d84a762008-09-24 00:27:38 +000032 const TargetRegisterInfo *TRI;
33 const MachineRegisterInfo *MRI;
34 const TargetInstrInfo *TII;
35 BitVector LivePhysRegs;
36
Dan Gohmand3ead432008-09-17 00:43:24 +000037 public:
38 static char ID; // Pass identification, replacement for typeid
Owen Anderson081c34b2010-10-19 17:21:58 +000039 DeadMachineInstructionElim() : MachineFunctionPass(ID) {
40 initializeDeadMachineInstructionElimPass(*PassRegistry::getPassRegistry());
41 }
Dan Gohman3d84a762008-09-24 00:27:38 +000042
43 private:
Dan Gohmand443ee62009-08-11 15:13:43 +000044 bool isDead(const MachineInstr *MI) const;
Dan Gohmand3ead432008-09-17 00:43:24 +000045 };
46}
47char DeadMachineInstructionElim::ID = 0;
Andrew Trick1dd8c852012-02-08 21:23:13 +000048char &llvm::DeadMachineInstructionElimID = DeadMachineInstructionElim::ID;
Dan Gohmand3ead432008-09-17 00:43:24 +000049
Owen Andersond13db2c2010-07-21 22:09:45 +000050INITIALIZE_PASS(DeadMachineInstructionElim, "dead-mi-elimination",
Owen Andersonce665bd2010-10-07 22:25:06 +000051 "Remove dead machine instructions", false, false)
Dan Gohmand3ead432008-09-17 00:43:24 +000052
Dan Gohmand443ee62009-08-11 15:13:43 +000053bool DeadMachineInstructionElim::isDead(const MachineInstr *MI) const {
Evan Chengc36b7062011-01-07 23:50:32 +000054 // Technically speaking inline asm without side effects and no defs can still
55 // be deleted. But there is so much bad inline asm code out there, we should
56 // let them be.
57 if (MI->isInlineAsm())
58 return false;
59
Dan Gohman3d84a762008-09-24 00:27:38 +000060 // Don't delete instructions with side effects.
61 bool SawStore = false;
Evan Chengac1abde2010-03-02 19:03:01 +000062 if (!MI->isSafeToMove(TII, 0, SawStore) && !MI->isPHI())
Dan Gohman3d84a762008-09-24 00:27:38 +000063 return false;
64
65 // Examine each operand.
66 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
67 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +000068 if (MO.isReg() && MO.isDef()) {
Dan Gohman3d84a762008-09-24 00:27:38 +000069 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen39284d12012-02-09 00:15:39 +000070 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
71 // Don't delete live physreg defs, or any reserved register defs.
Jakob Stoklund Olesenfb9ebbf2012-10-15 21:57:41 +000072 if (LivePhysRegs.test(Reg) || MRI->isReserved(Reg))
Jakob Stoklund Olesen39284d12012-02-09 00:15:39 +000073 return false;
74 } else {
75 if (!MRI->use_nodbg_empty(Reg))
76 // This def has a non-debug use. Don't delete the instruction!
77 return false;
Dan Gohman3d84a762008-09-24 00:27:38 +000078 }
79 }
80 }
81
82 // If there are no defs with uses, the instruction is dead.
83 return true;
84}
85
Dan Gohmand3ead432008-09-17 00:43:24 +000086bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) {
Stephen Hines36b56882014-04-23 16:57:46 -070087 if (skipOptnoneFunction(*MF.getFunction()))
88 return false;
89
Dan Gohmand3ead432008-09-17 00:43:24 +000090 bool AnyChanges = false;
Dan Gohman3d84a762008-09-24 00:27:38 +000091 MRI = &MF.getRegInfo();
92 TRI = MF.getTarget().getRegisterInfo();
93 TII = MF.getTarget().getInstrInfo();
Dan Gohmand3ead432008-09-17 00:43:24 +000094
95 // Loop over all instructions in all blocks, from bottom to top, so that it's
96 // more likely that chains of dependent but ultimately dead instructions will
97 // be cleaned up.
98 for (MachineFunction::reverse_iterator I = MF.rbegin(), E = MF.rend();
99 I != E; ++I) {
100 MachineBasicBlock *MBB = &*I;
Dan Gohman8468d1a2008-09-23 21:40:44 +0000101
Jakob Stoklund Olesenf14a6482010-08-31 21:51:05 +0000102 // Start out assuming that reserved registers are live out of this block.
Jakob Stoklund Olesenfb9ebbf2012-10-15 21:57:41 +0000103 LivePhysRegs = MRI->getReservedRegs();
Dan Gohman8468d1a2008-09-23 21:40:44 +0000104
Jakob Stoklund Olesenf27229e2011-06-27 15:00:36 +0000105 // Add live-ins from sucessors to LivePhysRegs. Normally, physregs are not
106 // live across blocks, but some targets (x86) can have flags live out of a
107 // block.
108 for (MachineBasicBlock::succ_iterator S = MBB->succ_begin(),
109 E = MBB->succ_end(); S != E; S++)
110 for (MachineBasicBlock::livein_iterator LI = (*S)->livein_begin();
111 LI != (*S)->livein_end(); LI++)
112 LivePhysRegs.set(*LI);
Jakob Stoklund Olesenf14a6482010-08-31 21:51:05 +0000113
Dan Gohman8468d1a2008-09-23 21:40:44 +0000114 // Now scan the instructions and delete dead ones, tracking physreg
115 // liveness as we go.
Dan Gohmand3ead432008-09-17 00:43:24 +0000116 for (MachineBasicBlock::reverse_iterator MII = MBB->rbegin(),
117 MIE = MBB->rend(); MII != MIE; ) {
118 MachineInstr *MI = &*MII;
119
Dan Gohman3d84a762008-09-24 00:27:38 +0000120 // If the instruction is dead, delete it!
121 if (isDead(MI)) {
David Greene26045e22010-01-04 19:10:20 +0000122 DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << *MI);
Dale Johannesen2d1ec732010-02-12 18:40:17 +0000123 // It is possible that some DBG_VALUE instructions refer to this
124 // instruction. Examine each def operand for such references;
125 // if found, mark the DBG_VALUE as undef (but don't delete it).
126 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
127 const MachineOperand &MO = MI->getOperand(i);
128 if (!MO.isReg() || !MO.isDef())
129 continue;
130 unsigned Reg = MO.getReg();
131 if (!TargetRegisterInfo::isVirtualRegister(Reg))
132 continue;
Stephen Hines36b56882014-04-23 16:57:46 -0700133 MRI->markUsesInDebugValueAsUndef(Reg);
Dale Johannesen2d1ec732010-02-12 18:40:17 +0000134 }
Dan Gohman3d84a762008-09-24 00:27:38 +0000135 AnyChanges = true;
136 MI->eraseFromParent();
Evan Cheng00a99a32010-02-06 09:07:11 +0000137 ++NumDeletes;
Dan Gohman3d84a762008-09-24 00:27:38 +0000138 MIE = MBB->rend();
139 // MII is now pointing to the next instruction to process,
140 // so don't increment it.
141 continue;
Dan Gohmand3ead432008-09-17 00:43:24 +0000142 }
Dan Gohman8468d1a2008-09-23 21:40:44 +0000143
144 // Record the physreg defs.
145 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
146 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000147 if (MO.isReg() && MO.isDef()) {
Dan Gohman8468d1a2008-09-23 21:40:44 +0000148 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000149 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Dan Gohmanb382c4d2008-10-16 00:11:23 +0000150 // Check the subreg set, not the alias set, because a def
151 // of a super-register may still be partially live after
152 // this def.
Chad Rosier62c320a2013-05-22 23:17:36 +0000153 for (MCSubRegIterator SR(Reg, TRI,/*IncludeSelf=*/true);
154 SR.isValid(); ++SR)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000155 LivePhysRegs.reset(*SR);
Dan Gohman8468d1a2008-09-23 21:40:44 +0000156 }
Jakob Stoklund Olesen6b88c182012-01-20 22:27:09 +0000157 } else if (MO.isRegMask()) {
158 // Register mask of preserved registers. All clobbers are dead.
Jakob Stoklund Olesen478a8a02012-02-02 23:52:57 +0000159 LivePhysRegs.clearBitsNotInMask(MO.getRegMask());
Dan Gohman8468d1a2008-09-23 21:40:44 +0000160 }
161 }
162 // Record the physreg uses, after the defs, in case a physreg is
163 // both defined and used in the same instruction.
164 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
165 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000166 if (MO.isReg() && MO.isUse()) {
Dan Gohman8468d1a2008-09-23 21:40:44 +0000167 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000168 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesenf152fe82012-06-01 20:36:54 +0000169 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
170 LivePhysRegs.set(*AI);
Dan Gohman8468d1a2008-09-23 21:40:44 +0000171 }
172 }
173 }
174
Dan Gohmand3ead432008-09-17 00:43:24 +0000175 // We didn't delete the current instruction, so increment MII to
176 // the next one.
177 ++MII;
178 }
179 }
180
Dan Gohman3d84a762008-09-24 00:27:38 +0000181 LivePhysRegs.clear();
Dan Gohmand3ead432008-09-17 00:43:24 +0000182 return AnyChanges;
183}