blob: a3bba61e4a2307a15a19a09e1e744b3e6a2d3b72 [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;
45 const MachineRegisterInfo &MRI = MF.getRegInfo();
46 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
47 bool SawStore = true;
48
49 // Loop over all instructions in all blocks, from bottom to top, so that it's
50 // more likely that chains of dependent but ultimately dead instructions will
51 // be cleaned up.
52 for (MachineFunction::reverse_iterator I = MF.rbegin(), E = MF.rend();
53 I != E; ++I) {
54 MachineBasicBlock *MBB = &*I;
55 for (MachineBasicBlock::reverse_iterator MII = MBB->rbegin(),
56 MIE = MBB->rend(); MII != MIE; ) {
57 MachineInstr *MI = &*MII;
58
59 // Don't delete instructions with side effects.
60 if (MI->isSafeToMove(&TII, SawStore)) {
61 // Examine each operand.
62 bool AllDefsDead = true;
63 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
64 const MachineOperand &MO = MI->getOperand(i);
65 if (MO.isRegister() && MO.isDef()) {
66 unsigned Reg = MO.getReg();
67 if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
68 !MRI.use_empty(Reg)) {
69 // This def has a use. Don't delete the instruction!
70 AllDefsDead = false;
71 break;
72 }
73 }
74 }
75
76 // If there are no defs with uses, the instruction is dead.
77 if (AllDefsDead) {
78 // Clear out the operands to take the registers out of their
79 // use chains.
80 while (unsigned Num = MI->getNumOperands())
81 MI->RemoveOperand(Num-1);
82
83 // Delete the actual instruction.
84 AnyChanges = true;
85 MI->eraseFromParent();
86 MIE = MBB->rend();
87 // MII is now pointing to the next instruction to process,
88 // so don't increment it.
89 continue;
90 }
91 }
92 // We didn't delete the current instruction, so increment MII to
93 // the next one.
94 ++MII;
95 }
96 }
97
98 return AnyChanges;
99}