blob: 32040e580bed3b9195139e5fa426f53cc5a0c722 [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"
16#include "llvm/Pass.h"
17#include "llvm/CodeGen/MachineFunctionPass.h"
18#include "llvm/CodeGen/MachineRegisterInfo.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"
Evan Cheng00a99a32010-02-06 09:07:11 +000023#include "llvm/ADT/Statistic.h"
Dan Gohmand3ead432008-09-17 00:43:24 +000024using 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 {
Dan Gohmand3ead432008-09-17 00:43:24 +000030 virtual bool runOnMachineFunction(MachineFunction &MF);
31
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;
48
Owen Andersond13db2c2010-07-21 22:09:45 +000049INITIALIZE_PASS(DeadMachineInstructionElim, "dead-mi-elimination",
Owen Andersonce665bd2010-10-07 22:25:06 +000050 "Remove dead machine instructions", false, false)
Dan Gohmand3ead432008-09-17 00:43:24 +000051
52FunctionPass *llvm::createDeadMachineInstructionElimPass() {
53 return new DeadMachineInstructionElim();
54}
55
Dan Gohmand443ee62009-08-11 15:13:43 +000056bool DeadMachineInstructionElim::isDead(const MachineInstr *MI) const {
Evan Chengc36b7062011-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 Gohman3d84a762008-09-24 00:27:38 +000063 // Don't delete instructions with side effects.
64 bool SawStore = false;
Evan Chengac1abde2010-03-02 19:03:01 +000065 if (!MI->isSafeToMove(TII, 0, SawStore) && !MI->isPHI())
Dan Gohman3d84a762008-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 Gohmand735b802008-10-03 15:45:36 +000071 if (MO.isReg() && MO.isDef()) {
Dan Gohman3d84a762008-09-24 00:27:38 +000072 unsigned Reg = MO.getReg();
73 if (TargetRegisterInfo::isPhysicalRegister(Reg) ?
Dale Johannesen2d1ec732010-02-12 18:40:17 +000074 LivePhysRegs[Reg] : !MRI->use_nodbg_empty(Reg)) {
75 // This def has a non-debug use. Don't delete the instruction!
Dan Gohman3d84a762008-09-24 00:27:38 +000076 return false;
77 }
78 }
79 }
80
81 // If there are no defs with uses, the instruction is dead.
82 return true;
83}
84
Dan Gohmand3ead432008-09-17 00:43:24 +000085bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) {
86 bool AnyChanges = false;
Dan Gohman3d84a762008-09-24 00:27:38 +000087 MRI = &MF.getRegInfo();
88 TRI = MF.getTarget().getRegisterInfo();
89 TII = MF.getTarget().getInstrInfo();
Dan Gohmand3ead432008-09-17 00:43:24 +000090
Jakob Stoklund Olesenf14a6482010-08-31 21:51:05 +000091 // Treat reserved registers as always live.
92 BitVector ReservedRegs = TRI->getReservedRegs(MF);
Dan Gohman8468d1a2008-09-23 21:40:44 +000093
Dan Gohmand3ead432008-09-17 00:43:24 +000094 // Loop over all instructions in all blocks, from bottom to top, so that it's
95 // more likely that chains of dependent but ultimately dead instructions will
96 // be cleaned up.
97 for (MachineFunction::reverse_iterator I = MF.rbegin(), E = MF.rend();
98 I != E; ++I) {
99 MachineBasicBlock *MBB = &*I;
Dan Gohman8468d1a2008-09-23 21:40:44 +0000100
Jakob Stoklund Olesenf14a6482010-08-31 21:51:05 +0000101 // Start out assuming that reserved registers are live out of this block.
102 LivePhysRegs = ReservedRegs;
Dan Gohman8468d1a2008-09-23 21:40:44 +0000103
104 // Also add any explicit live-out physregs for this block.
105 if (!MBB->empty() && MBB->back().getDesc().isReturn())
Dan Gohman3d84a762008-09-24 00:27:38 +0000106 for (MachineRegisterInfo::liveout_iterator LOI = MRI->liveout_begin(),
107 LOE = MRI->liveout_end(); LOI != LOE; ++LOI) {
Dan Gohman8468d1a2008-09-23 21:40:44 +0000108 unsigned Reg = *LOI;
109 if (TargetRegisterInfo::isPhysicalRegister(Reg))
110 LivePhysRegs.set(Reg);
111 }
112
Jakob Stoklund Olesenf14a6482010-08-31 21:51:05 +0000113 // FIXME: Add live-ins from sucessors to LivePhysRegs. Normally, physregs
114 // are not live across blocks, but some targets (x86) can have flags live
115 // out of a block.
116
Dan Gohman8468d1a2008-09-23 21:40:44 +0000117 // Now scan the instructions and delete dead ones, tracking physreg
118 // liveness as we go.
Dan Gohmand3ead432008-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 Gohman3d84a762008-09-24 00:27:38 +0000123 // If the instruction is dead, delete it!
124 if (isDead(MI)) {
David Greene26045e22010-01-04 19:10:20 +0000125 DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << *MI);
Dale Johannesen2d1ec732010-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;
136 MachineRegisterInfo::use_iterator nextI;
137 for (MachineRegisterInfo::use_iterator I = MRI->use_begin(Reg),
138 E = MRI->use_end(); I!=E; I=nextI) {
139 nextI = llvm::next(I); // I is invalidated by the setReg
140 MachineOperand& Use = I.getOperand();
141 MachineInstr *UseMI = Use.getParent();
142 if (UseMI==MI)
143 continue;
144 assert(Use.isDebug());
145 UseMI->getOperand(0).setReg(0U);
146 }
147 }
Dan Gohman3d84a762008-09-24 00:27:38 +0000148 AnyChanges = true;
149 MI->eraseFromParent();
Evan Cheng00a99a32010-02-06 09:07:11 +0000150 ++NumDeletes;
Dan Gohman3d84a762008-09-24 00:27:38 +0000151 MIE = MBB->rend();
152 // MII is now pointing to the next instruction to process,
153 // so don't increment it.
154 continue;
Dan Gohmand3ead432008-09-17 00:43:24 +0000155 }
Dan Gohman8468d1a2008-09-23 21:40:44 +0000156
157 // Record the physreg defs.
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.isDef()) {
Dan Gohman8468d1a2008-09-23 21:40:44 +0000161 unsigned Reg = MO.getReg();
162 if (Reg != 0 && TargetRegisterInfo::isPhysicalRegister(Reg)) {
163 LivePhysRegs.reset(Reg);
Dan Gohmanb382c4d2008-10-16 00:11:23 +0000164 // Check the subreg set, not the alias set, because a def
165 // of a super-register may still be partially live after
166 // this def.
Dan Gohman131161b2008-10-16 01:06:18 +0000167 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
168 *SubRegs; ++SubRegs)
169 LivePhysRegs.reset(*SubRegs);
Dan Gohman8468d1a2008-09-23 21:40:44 +0000170 }
171 }
172 }
173 // Record the physreg uses, after the defs, in case a physreg is
174 // both defined and used in the same instruction.
175 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
176 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000177 if (MO.isReg() && MO.isUse()) {
Dan Gohman8468d1a2008-09-23 21:40:44 +0000178 unsigned Reg = MO.getReg();
179 if (Reg != 0 && TargetRegisterInfo::isPhysicalRegister(Reg)) {
180 LivePhysRegs.set(Reg);
Dan Gohman3d84a762008-09-24 00:27:38 +0000181 for (const unsigned *AliasSet = TRI->getAliasSet(Reg);
Dan Gohman8468d1a2008-09-23 21:40:44 +0000182 *AliasSet; ++AliasSet)
183 LivePhysRegs.set(*AliasSet);
184 }
185 }
186 }
187
Dan Gohmand3ead432008-09-17 00:43:24 +0000188 // We didn't delete the current instruction, so increment MII to
189 // the next one.
190 ++MII;
191 }
192 }
193
Dan Gohman3d84a762008-09-24 00:27:38 +0000194 LivePhysRegs.clear();
Dan Gohmand3ead432008-09-17 00:43:24 +0000195 return AnyChanges;
196}