blob: 078ed3d31b1c309f9d7696ddd1d09f94cb805b2c [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"
15#include "llvm/Pass.h"
16#include "llvm/CodeGen/MachineFunctionPass.h"
17#include "llvm/CodeGen/MachineRegisterInfo.h"
18#include "llvm/Support/Compiler.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
25namespace {
26 class VISIBILITY_HIDDEN DeadMachineInstructionElim :
27 public MachineFunctionPass {
28 virtual bool runOnMachineFunction(MachineFunction &MF);
29
Dan Gohman3d84a762008-09-24 00:27:38 +000030 const TargetRegisterInfo *TRI;
31 const MachineRegisterInfo *MRI;
32 const TargetInstrInfo *TII;
33 BitVector LivePhysRegs;
34
Dan Gohmand3ead432008-09-17 00:43:24 +000035 public:
36 static char ID; // Pass identification, replacement for typeid
37 DeadMachineInstructionElim() : MachineFunctionPass(&ID) {}
Dan Gohman3d84a762008-09-24 00:27:38 +000038
39 private:
Dan Gohmand443ee62009-08-11 15:13:43 +000040 bool isDead(const MachineInstr *MI) const;
Dan Gohmand3ead432008-09-17 00:43:24 +000041 };
42}
43char DeadMachineInstructionElim::ID = 0;
44
45static RegisterPass<DeadMachineInstructionElim>
46Y("dead-mi-elimination",
47 "Remove dead machine instructions");
48
49FunctionPass *llvm::createDeadMachineInstructionElimPass() {
50 return new DeadMachineInstructionElim();
51}
52
Dan Gohmand443ee62009-08-11 15:13:43 +000053bool DeadMachineInstructionElim::isDead(const MachineInstr *MI) const {
Dan Gohman3d84a762008-09-24 00:27:38 +000054 // Don't delete instructions with side effects.
55 bool SawStore = false;
Dan Gohmana70dca12009-10-09 23:27:56 +000056 if (!MI->isSafeToMove(TII, SawStore, 0))
Dan Gohman3d84a762008-09-24 00:27:38 +000057 return false;
58
59 // Examine each operand.
60 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
61 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +000062 if (MO.isReg() && MO.isDef()) {
Dan Gohman3d84a762008-09-24 00:27:38 +000063 unsigned Reg = MO.getReg();
64 if (TargetRegisterInfo::isPhysicalRegister(Reg) ?
65 LivePhysRegs[Reg] : !MRI->use_empty(Reg)) {
66 // This def has a use. Don't delete the instruction!
67 return false;
68 }
69 }
70 }
71
72 // If there are no defs with uses, the instruction is dead.
73 return true;
74}
75
Dan Gohmand3ead432008-09-17 00:43:24 +000076bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) {
77 bool AnyChanges = false;
Dan Gohman3d84a762008-09-24 00:27:38 +000078 MRI = &MF.getRegInfo();
79 TRI = MF.getTarget().getRegisterInfo();
80 TII = MF.getTarget().getInstrInfo();
Dan Gohmand3ead432008-09-17 00:43:24 +000081
Dan Gohman8468d1a2008-09-23 21:40:44 +000082 // Compute a bitvector to represent all non-allocatable physregs.
Dan Gohman3d84a762008-09-24 00:27:38 +000083 BitVector NonAllocatableRegs = TRI->getAllocatableSet(MF);
Dan Gohman8468d1a2008-09-23 21:40:44 +000084 NonAllocatableRegs.flip();
85
Dan Gohmand3ead432008-09-17 00:43:24 +000086 // Loop over all instructions in all blocks, from bottom to top, so that it's
87 // more likely that chains of dependent but ultimately dead instructions will
88 // be cleaned up.
89 for (MachineFunction::reverse_iterator I = MF.rbegin(), E = MF.rend();
90 I != E; ++I) {
91 MachineBasicBlock *MBB = &*I;
Dan Gohman8468d1a2008-09-23 21:40:44 +000092
93 // Start out assuming that all non-allocatable registers are live
94 // out of this block.
95 LivePhysRegs = NonAllocatableRegs;
96
97 // Also add any explicit live-out physregs for this block.
98 if (!MBB->empty() && MBB->back().getDesc().isReturn())
Dan Gohman3d84a762008-09-24 00:27:38 +000099 for (MachineRegisterInfo::liveout_iterator LOI = MRI->liveout_begin(),
100 LOE = MRI->liveout_end(); LOI != LOE; ++LOI) {
Dan Gohman8468d1a2008-09-23 21:40:44 +0000101 unsigned Reg = *LOI;
102 if (TargetRegisterInfo::isPhysicalRegister(Reg))
103 LivePhysRegs.set(Reg);
104 }
105
106 // Now scan the instructions and delete dead ones, tracking physreg
107 // liveness as we go.
Dan Gohmand3ead432008-09-17 00:43:24 +0000108 for (MachineBasicBlock::reverse_iterator MII = MBB->rbegin(),
109 MIE = MBB->rend(); MII != MIE; ) {
110 MachineInstr *MI = &*MII;
111
Dan Gohman3d84a762008-09-24 00:27:38 +0000112 // If the instruction is dead, delete it!
113 if (isDead(MI)) {
Bill Wendling9311a222009-08-22 20:04:03 +0000114 DEBUG(errs() << "DeadMachineInstructionElim: DELETING: " << *MI);
Dan Gohman3d84a762008-09-24 00:27:38 +0000115 AnyChanges = true;
116 MI->eraseFromParent();
117 MIE = MBB->rend();
118 // MII is now pointing to the next instruction to process,
119 // so don't increment it.
120 continue;
Dan Gohmand3ead432008-09-17 00:43:24 +0000121 }
Dan Gohman8468d1a2008-09-23 21:40:44 +0000122
123 // Record the physreg defs.
124 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
125 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000126 if (MO.isReg() && MO.isDef()) {
Dan Gohman8468d1a2008-09-23 21:40:44 +0000127 unsigned Reg = MO.getReg();
128 if (Reg != 0 && TargetRegisterInfo::isPhysicalRegister(Reg)) {
129 LivePhysRegs.reset(Reg);
Dan Gohmanb382c4d2008-10-16 00:11:23 +0000130 // Check the subreg set, not the alias set, because a def
131 // of a super-register may still be partially live after
132 // this def.
Dan Gohman131161b2008-10-16 01:06:18 +0000133 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
134 *SubRegs; ++SubRegs)
135 LivePhysRegs.reset(*SubRegs);
Dan Gohman8468d1a2008-09-23 21:40:44 +0000136 }
137 }
138 }
139 // Record the physreg uses, after the defs, in case a physreg is
140 // both defined and used in the same instruction.
141 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
142 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000143 if (MO.isReg() && MO.isUse()) {
Dan Gohman8468d1a2008-09-23 21:40:44 +0000144 unsigned Reg = MO.getReg();
145 if (Reg != 0 && TargetRegisterInfo::isPhysicalRegister(Reg)) {
146 LivePhysRegs.set(Reg);
Dan Gohman3d84a762008-09-24 00:27:38 +0000147 for (const unsigned *AliasSet = TRI->getAliasSet(Reg);
Dan Gohman8468d1a2008-09-23 21:40:44 +0000148 *AliasSet; ++AliasSet)
149 LivePhysRegs.set(*AliasSet);
150 }
151 }
152 }
153
Dan Gohmand3ead432008-09-17 00:43:24 +0000154 // We didn't delete the current instruction, so increment MII to
155 // the next one.
156 ++MII;
157 }
158 }
159
Dan Gohman3d84a762008-09-24 00:27:38 +0000160 LivePhysRegs.clear();
Dan Gohmand3ead432008-09-17 00:43:24 +0000161 return AnyChanges;
162}