blob: 392e3b1b778067841eaadc23ccc1fc92c01d519f [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"
19#include "llvm/Target/TargetInstrInfo.h"
20#include "llvm/Target/TargetMachine.h"
21using namespace llvm;
22
23namespace {
24 class VISIBILITY_HIDDEN DeadMachineInstructionElim :
25 public MachineFunctionPass {
26 virtual bool runOnMachineFunction(MachineFunction &MF);
27
28 public:
29 static char ID; // Pass identification, replacement for typeid
30 DeadMachineInstructionElim() : MachineFunctionPass(&ID) {}
31 };
32}
33char DeadMachineInstructionElim::ID = 0;
34
35static RegisterPass<DeadMachineInstructionElim>
36Y("dead-mi-elimination",
37 "Remove dead machine instructions");
38
39FunctionPass *llvm::createDeadMachineInstructionElimPass() {
40 return new DeadMachineInstructionElim();
41}
42
43bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) {
44 bool AnyChanges = false;
Dan Gohman8468d1a2008-09-23 21:40:44 +000045 const TargetRegisterInfo &TRI = *MF.getTarget().getRegisterInfo();
Dan Gohmand3ead432008-09-17 00:43:24 +000046 const MachineRegisterInfo &MRI = MF.getRegInfo();
47 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
Dan Gohman8468d1a2008-09-23 21:40:44 +000048 BitVector LivePhysRegs;
Dan Gohman035268e2008-09-24 00:07:08 +000049 bool SawStore;
Dan Gohmand3ead432008-09-17 00:43:24 +000050
Dan Gohman8468d1a2008-09-23 21:40:44 +000051 // Compute a bitvector to represent all non-allocatable physregs.
52 BitVector NonAllocatableRegs = TRI.getAllocatableSet(MF);
53 NonAllocatableRegs.flip();
54
Dan Gohmand3ead432008-09-17 00:43:24 +000055 // Loop over all instructions in all blocks, from bottom to top, so that it's
56 // more likely that chains of dependent but ultimately dead instructions will
57 // be cleaned up.
58 for (MachineFunction::reverse_iterator I = MF.rbegin(), E = MF.rend();
59 I != E; ++I) {
60 MachineBasicBlock *MBB = &*I;
Dan Gohman8468d1a2008-09-23 21:40:44 +000061
62 // Start out assuming that all non-allocatable registers are live
63 // out of this block.
64 LivePhysRegs = NonAllocatableRegs;
65
66 // Also add any explicit live-out physregs for this block.
67 if (!MBB->empty() && MBB->back().getDesc().isReturn())
68 for (MachineRegisterInfo::liveout_iterator LOI = MRI.liveout_begin(),
69 LOE = MRI.liveout_end(); LOI != LOE; ++LOI) {
70 unsigned Reg = *LOI;
71 if (TargetRegisterInfo::isPhysicalRegister(Reg))
72 LivePhysRegs.set(Reg);
73 }
74
75 // Now scan the instructions and delete dead ones, tracking physreg
76 // liveness as we go.
Dan Gohmand3ead432008-09-17 00:43:24 +000077 for (MachineBasicBlock::reverse_iterator MII = MBB->rbegin(),
78 MIE = MBB->rend(); MII != MIE; ) {
79 MachineInstr *MI = &*MII;
80
81 // Don't delete instructions with side effects.
Dan Gohman035268e2008-09-24 00:07:08 +000082 SawStore = false;
Dan Gohmand3ead432008-09-17 00:43:24 +000083 if (MI->isSafeToMove(&TII, SawStore)) {
84 // Examine each operand.
85 bool AllDefsDead = true;
86 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
87 const MachineOperand &MO = MI->getOperand(i);
88 if (MO.isRegister() && MO.isDef()) {
89 unsigned Reg = MO.getReg();
Dan Gohman8468d1a2008-09-23 21:40:44 +000090 if (TargetRegisterInfo::isPhysicalRegister(Reg) ?
91 LivePhysRegs[Reg] : !MRI.use_empty(Reg)) {
Dan Gohmand3ead432008-09-17 00:43:24 +000092 // This def has a use. Don't delete the instruction!
93 AllDefsDead = false;
94 break;
95 }
96 }
97 }
98
99 // If there are no defs with uses, the instruction is dead.
100 if (AllDefsDead) {
Dan Gohmand3ead432008-09-17 00:43:24 +0000101 AnyChanges = true;
102 MI->eraseFromParent();
103 MIE = MBB->rend();
104 // MII is now pointing to the next instruction to process,
105 // so don't increment it.
106 continue;
107 }
108 }
Dan Gohman8468d1a2008-09-23 21:40:44 +0000109
110 // Record the physreg defs.
111 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
112 const MachineOperand &MO = MI->getOperand(i);
113 if (MO.isRegister() && MO.isDef()) {
114 unsigned Reg = MO.getReg();
115 if (Reg != 0 && TargetRegisterInfo::isPhysicalRegister(Reg)) {
116 LivePhysRegs.reset(Reg);
117 for (const unsigned *AliasSet = TRI.getAliasSet(Reg);
118 *AliasSet; ++AliasSet)
119 LivePhysRegs.reset(*AliasSet);
120 }
121 }
122 }
123 // Record the physreg uses, after the defs, in case a physreg is
124 // both defined and used in the same instruction.
125 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
126 const MachineOperand &MO = MI->getOperand(i);
127 if (MO.isRegister() && MO.isUse()) {
128 unsigned Reg = MO.getReg();
129 if (Reg != 0 && TargetRegisterInfo::isPhysicalRegister(Reg)) {
130 LivePhysRegs.set(Reg);
131 for (const unsigned *AliasSet = TRI.getAliasSet(Reg);
132 *AliasSet; ++AliasSet)
133 LivePhysRegs.set(*AliasSet);
134 }
135 }
136 }
137
Dan Gohmand3ead432008-09-17 00:43:24 +0000138 // We didn't delete the current instruction, so increment MII to
139 // the next one.
140 ++MII;
141 }
142 }
143
144 return AnyChanges;
145}