blob: 07a5d38db03fc513845493f7ede86ee0c43e7efc [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"
Dan Gohman723ac372008-09-25 01:06:50 +000018#include "llvm/Support/Debug.h"
Bill Wendling9311a222009-08-22 20:04:03 +000019#include "llvm/Support/raw_ostream.h"
Dan Gohmand3ead432008-09-17 00:43:24 +000020#include "llvm/Target/TargetInstrInfo.h"
21#include "llvm/Target/TargetMachine.h"
22using namespace llvm;
23
24namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000025 class DeadMachineInstructionElim : public MachineFunctionPass {
Dan Gohmand3ead432008-09-17 00:43:24 +000026 virtual bool runOnMachineFunction(MachineFunction &MF);
27
Dan Gohman3d84a762008-09-24 00:27:38 +000028 const TargetRegisterInfo *TRI;
29 const MachineRegisterInfo *MRI;
30 const TargetInstrInfo *TII;
31 BitVector LivePhysRegs;
32
Dan Gohmand3ead432008-09-17 00:43:24 +000033 public:
34 static char ID; // Pass identification, replacement for typeid
35 DeadMachineInstructionElim() : MachineFunctionPass(&ID) {}
Dan Gohman3d84a762008-09-24 00:27:38 +000036
37 private:
Dan Gohmand443ee62009-08-11 15:13:43 +000038 bool isDead(const MachineInstr *MI) const;
Dan Gohmand3ead432008-09-17 00:43:24 +000039 };
40}
41char DeadMachineInstructionElim::ID = 0;
42
43static RegisterPass<DeadMachineInstructionElim>
44Y("dead-mi-elimination",
45 "Remove dead machine instructions");
46
47FunctionPass *llvm::createDeadMachineInstructionElimPass() {
48 return new DeadMachineInstructionElim();
49}
50
Dan Gohmand443ee62009-08-11 15:13:43 +000051bool DeadMachineInstructionElim::isDead(const MachineInstr *MI) const {
Dan Gohman3d84a762008-09-24 00:27:38 +000052 // Don't delete instructions with side effects.
53 bool SawStore = false;
Dan Gohmana70dca12009-10-09 23:27:56 +000054 if (!MI->isSafeToMove(TII, SawStore, 0))
Dan Gohman3d84a762008-09-24 00:27:38 +000055 return false;
56
57 // Examine each operand.
58 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
59 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +000060 if (MO.isReg() && MO.isDef()) {
Dan Gohman3d84a762008-09-24 00:27:38 +000061 unsigned Reg = MO.getReg();
62 if (TargetRegisterInfo::isPhysicalRegister(Reg) ?
63 LivePhysRegs[Reg] : !MRI->use_empty(Reg)) {
64 // This def has a use. Don't delete the instruction!
65 return false;
66 }
67 }
68 }
69
70 // If there are no defs with uses, the instruction is dead.
71 return true;
72}
73
Dan Gohmand3ead432008-09-17 00:43:24 +000074bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) {
75 bool AnyChanges = false;
Dan Gohman3d84a762008-09-24 00:27:38 +000076 MRI = &MF.getRegInfo();
77 TRI = MF.getTarget().getRegisterInfo();
78 TII = MF.getTarget().getInstrInfo();
Dan Gohmand3ead432008-09-17 00:43:24 +000079
Dan Gohman8468d1a2008-09-23 21:40:44 +000080 // Compute a bitvector to represent all non-allocatable physregs.
Dan Gohman3d84a762008-09-24 00:27:38 +000081 BitVector NonAllocatableRegs = TRI->getAllocatableSet(MF);
Dan Gohman8468d1a2008-09-23 21:40:44 +000082 NonAllocatableRegs.flip();
83
Dan Gohmand3ead432008-09-17 00:43:24 +000084 // Loop over all instructions in all blocks, from bottom to top, so that it's
85 // more likely that chains of dependent but ultimately dead instructions will
86 // be cleaned up.
87 for (MachineFunction::reverse_iterator I = MF.rbegin(), E = MF.rend();
88 I != E; ++I) {
89 MachineBasicBlock *MBB = &*I;
Dan Gohman8468d1a2008-09-23 21:40:44 +000090
91 // Start out assuming that all non-allocatable registers are live
92 // out of this block.
93 LivePhysRegs = NonAllocatableRegs;
94
95 // Also add any explicit live-out physregs for this block.
96 if (!MBB->empty() && MBB->back().getDesc().isReturn())
Dan Gohman3d84a762008-09-24 00:27:38 +000097 for (MachineRegisterInfo::liveout_iterator LOI = MRI->liveout_begin(),
98 LOE = MRI->liveout_end(); LOI != LOE; ++LOI) {
Dan Gohman8468d1a2008-09-23 21:40:44 +000099 unsigned Reg = *LOI;
100 if (TargetRegisterInfo::isPhysicalRegister(Reg))
101 LivePhysRegs.set(Reg);
102 }
103
104 // Now scan the instructions and delete dead ones, tracking physreg
105 // liveness as we go.
Dan Gohmand3ead432008-09-17 00:43:24 +0000106 for (MachineBasicBlock::reverse_iterator MII = MBB->rbegin(),
107 MIE = MBB->rend(); MII != MIE; ) {
108 MachineInstr *MI = &*MII;
109
Dan Gohman3d84a762008-09-24 00:27:38 +0000110 // If the instruction is dead, delete it!
111 if (isDead(MI)) {
Bill Wendling9311a222009-08-22 20:04:03 +0000112 DEBUG(errs() << "DeadMachineInstructionElim: DELETING: " << *MI);
Dan Gohman3d84a762008-09-24 00:27:38 +0000113 AnyChanges = true;
114 MI->eraseFromParent();
115 MIE = MBB->rend();
116 // MII is now pointing to the next instruction to process,
117 // so don't increment it.
118 continue;
Dan Gohmand3ead432008-09-17 00:43:24 +0000119 }
Dan Gohman8468d1a2008-09-23 21:40:44 +0000120
121 // Record the physreg defs.
122 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
123 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000124 if (MO.isReg() && MO.isDef()) {
Dan Gohman8468d1a2008-09-23 21:40:44 +0000125 unsigned Reg = MO.getReg();
126 if (Reg != 0 && TargetRegisterInfo::isPhysicalRegister(Reg)) {
127 LivePhysRegs.reset(Reg);
Dan Gohmanb382c4d2008-10-16 00:11:23 +0000128 // Check the subreg set, not the alias set, because a def
129 // of a super-register may still be partially live after
130 // this def.
Dan Gohman131161b2008-10-16 01:06:18 +0000131 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
132 *SubRegs; ++SubRegs)
133 LivePhysRegs.reset(*SubRegs);
Dan Gohman8468d1a2008-09-23 21:40:44 +0000134 }
135 }
136 }
137 // Record the physreg uses, after the defs, in case a physreg is
138 // both defined and used in the same instruction.
139 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
140 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000141 if (MO.isReg() && MO.isUse()) {
Dan Gohman8468d1a2008-09-23 21:40:44 +0000142 unsigned Reg = MO.getReg();
143 if (Reg != 0 && TargetRegisterInfo::isPhysicalRegister(Reg)) {
144 LivePhysRegs.set(Reg);
Dan Gohman3d84a762008-09-24 00:27:38 +0000145 for (const unsigned *AliasSet = TRI->getAliasSet(Reg);
Dan Gohman8468d1a2008-09-23 21:40:44 +0000146 *AliasSet; ++AliasSet)
147 LivePhysRegs.set(*AliasSet);
148 }
149 }
150 }
151
Dan Gohmand3ead432008-09-17 00:43:24 +0000152 // We didn't delete the current instruction, so increment MII to
153 // the next one.
154 ++MII;
155 }
156 }
157
Dan Gohman3d84a762008-09-24 00:27:38 +0000158 LivePhysRegs.clear();
Dan Gohmand3ead432008-09-17 00:43:24 +0000159 return AnyChanges;
160}