blob: 138b83d4b667c83d51eef7e9a7366f9faae00c65 [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 Anderson9ccaf532010-08-05 23:42:04 +000039 DeadMachineInstructionElim() : MachineFunctionPass(ID) {}
Dan Gohman3d84a762008-09-24 00:27:38 +000040
41 private:
Dan Gohmand443ee62009-08-11 15:13:43 +000042 bool isDead(const MachineInstr *MI) const;
Dan Gohmand3ead432008-09-17 00:43:24 +000043 };
44}
45char DeadMachineInstructionElim::ID = 0;
46
Owen Andersond13db2c2010-07-21 22:09:45 +000047INITIALIZE_PASS(DeadMachineInstructionElim, "dead-mi-elimination",
48 "Remove dead machine instructions", false, false);
Dan Gohmand3ead432008-09-17 00:43:24 +000049
50FunctionPass *llvm::createDeadMachineInstructionElimPass() {
51 return new DeadMachineInstructionElim();
52}
53
Dan Gohmand443ee62009-08-11 15:13:43 +000054bool DeadMachineInstructionElim::isDead(const MachineInstr *MI) const {
Dan Gohman3d84a762008-09-24 00:27:38 +000055 // Don't delete instructions with side effects.
56 bool SawStore = false;
Evan Chengac1abde2010-03-02 19:03:01 +000057 if (!MI->isSafeToMove(TII, 0, SawStore) && !MI->isPHI())
Dan Gohman3d84a762008-09-24 00:27:38 +000058 return false;
59
60 // Examine each operand.
61 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
62 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +000063 if (MO.isReg() && MO.isDef()) {
Dan Gohman3d84a762008-09-24 00:27:38 +000064 unsigned Reg = MO.getReg();
65 if (TargetRegisterInfo::isPhysicalRegister(Reg) ?
Dale Johannesen2d1ec732010-02-12 18:40:17 +000066 LivePhysRegs[Reg] : !MRI->use_nodbg_empty(Reg)) {
67 // This def has a non-debug use. Don't delete the instruction!
Dan Gohman3d84a762008-09-24 00:27:38 +000068 return false;
69 }
70 }
71 }
72
73 // If there are no defs with uses, the instruction is dead.
74 return true;
75}
76
Dan Gohmand3ead432008-09-17 00:43:24 +000077bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) {
78 bool AnyChanges = false;
Dan Gohman3d84a762008-09-24 00:27:38 +000079 MRI = &MF.getRegInfo();
80 TRI = MF.getTarget().getRegisterInfo();
81 TII = MF.getTarget().getInstrInfo();
Dan Gohmand3ead432008-09-17 00:43:24 +000082
Dan Gohman8468d1a2008-09-23 21:40:44 +000083 // Compute a bitvector to represent all non-allocatable physregs.
Dan Gohman3d84a762008-09-24 00:27:38 +000084 BitVector NonAllocatableRegs = TRI->getAllocatableSet(MF);
Dan Gohman8468d1a2008-09-23 21:40:44 +000085 NonAllocatableRegs.flip();
86
Dan Gohmand3ead432008-09-17 00:43:24 +000087 // Loop over all instructions in all blocks, from bottom to top, so that it's
88 // more likely that chains of dependent but ultimately dead instructions will
89 // be cleaned up.
90 for (MachineFunction::reverse_iterator I = MF.rbegin(), E = MF.rend();
91 I != E; ++I) {
92 MachineBasicBlock *MBB = &*I;
Dan Gohman8468d1a2008-09-23 21:40:44 +000093
94 // Start out assuming that all non-allocatable registers are live
95 // out of this block.
96 LivePhysRegs = NonAllocatableRegs;
97
98 // Also add any explicit live-out physregs for this block.
99 if (!MBB->empty() && MBB->back().getDesc().isReturn())
Dan Gohman3d84a762008-09-24 00:27:38 +0000100 for (MachineRegisterInfo::liveout_iterator LOI = MRI->liveout_begin(),
101 LOE = MRI->liveout_end(); LOI != LOE; ++LOI) {
Dan Gohman8468d1a2008-09-23 21:40:44 +0000102 unsigned Reg = *LOI;
103 if (TargetRegisterInfo::isPhysicalRegister(Reg))
104 LivePhysRegs.set(Reg);
105 }
106
107 // Now scan the instructions and delete dead ones, tracking physreg
108 // liveness as we go.
Dan Gohmand3ead432008-09-17 00:43:24 +0000109 for (MachineBasicBlock::reverse_iterator MII = MBB->rbegin(),
110 MIE = MBB->rend(); MII != MIE; ) {
111 MachineInstr *MI = &*MII;
112
Dan Gohman3d84a762008-09-24 00:27:38 +0000113 // If the instruction is dead, delete it!
114 if (isDead(MI)) {
David Greene26045e22010-01-04 19:10:20 +0000115 DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << *MI);
Dale Johannesen2d1ec732010-02-12 18:40:17 +0000116 // It is possible that some DBG_VALUE instructions refer to this
117 // instruction. Examine each def operand for such references;
118 // if found, mark the DBG_VALUE as undef (but don't delete it).
119 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
120 const MachineOperand &MO = MI->getOperand(i);
121 if (!MO.isReg() || !MO.isDef())
122 continue;
123 unsigned Reg = MO.getReg();
124 if (!TargetRegisterInfo::isVirtualRegister(Reg))
125 continue;
126 MachineRegisterInfo::use_iterator nextI;
127 for (MachineRegisterInfo::use_iterator I = MRI->use_begin(Reg),
128 E = MRI->use_end(); I!=E; I=nextI) {
129 nextI = llvm::next(I); // I is invalidated by the setReg
130 MachineOperand& Use = I.getOperand();
131 MachineInstr *UseMI = Use.getParent();
132 if (UseMI==MI)
133 continue;
134 assert(Use.isDebug());
135 UseMI->getOperand(0).setReg(0U);
136 }
137 }
Dan Gohman3d84a762008-09-24 00:27:38 +0000138 AnyChanges = true;
139 MI->eraseFromParent();
Evan Cheng00a99a32010-02-06 09:07:11 +0000140 ++NumDeletes;
Dan Gohman3d84a762008-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 Gohmand3ead432008-09-17 00:43:24 +0000145 }
Dan Gohman8468d1a2008-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 Gohmand735b802008-10-03 15:45:36 +0000150 if (MO.isReg() && MO.isDef()) {
Dan Gohman8468d1a2008-09-23 21:40:44 +0000151 unsigned Reg = MO.getReg();
152 if (Reg != 0 && TargetRegisterInfo::isPhysicalRegister(Reg)) {
153 LivePhysRegs.reset(Reg);
Dan Gohmanb382c4d2008-10-16 00:11:23 +0000154 // Check the subreg set, not the alias set, because a def
155 // of a super-register may still be partially live after
156 // this def.
Dan Gohman131161b2008-10-16 01:06:18 +0000157 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
158 *SubRegs; ++SubRegs)
159 LivePhysRegs.reset(*SubRegs);
Dan Gohman8468d1a2008-09-23 21:40:44 +0000160 }
161 }
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();
169 if (Reg != 0 && TargetRegisterInfo::isPhysicalRegister(Reg)) {
170 LivePhysRegs.set(Reg);
Dan Gohman3d84a762008-09-24 00:27:38 +0000171 for (const unsigned *AliasSet = TRI->getAliasSet(Reg);
Dan Gohman8468d1a2008-09-23 21:40:44 +0000172 *AliasSet; ++AliasSet)
173 LivePhysRegs.set(*AliasSet);
174 }
175 }
176 }
177
Dan Gohmand3ead432008-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 Gohman3d84a762008-09-24 00:27:38 +0000184 LivePhysRegs.clear();
Dan Gohmand3ead432008-09-17 00:43:24 +0000185 return AnyChanges;
186}