Set SetStore to false, to allow this pass to delete
[oota-llvm.git] / lib / CodeGen / DeadMachineInstructionElim.cpp
1 //===- 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"
21 using namespace llvm;
22
23 namespace {
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 }
33 char DeadMachineInstructionElim::ID = 0;
34
35 static RegisterPass<DeadMachineInstructionElim>
36 Y("dead-mi-elimination",
37   "Remove dead machine instructions");
38
39 FunctionPass *llvm::createDeadMachineInstructionElimPass() {
40   return new DeadMachineInstructionElim();
41 }
42
43 bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) {
44   bool AnyChanges = false;
45   const TargetRegisterInfo &TRI = *MF.getTarget().getRegisterInfo();
46   const MachineRegisterInfo &MRI = MF.getRegInfo();
47   const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
48   BitVector LivePhysRegs;
49   bool SawStore;
50
51   // Compute a bitvector to represent all non-allocatable physregs.
52   BitVector NonAllocatableRegs = TRI.getAllocatableSet(MF);
53   NonAllocatableRegs.flip();
54
55   // 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;
61
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.
77     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.
82       SawStore = false;
83       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();
90             if (TargetRegisterInfo::isPhysicalRegister(Reg) ?
91                 LivePhysRegs[Reg] : !MRI.use_empty(Reg)) {
92               // 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) {
101           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       }
109
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
138       // We didn't delete the current instruction, so increment MII to
139       // the next one.
140       ++MII;
141     }
142   }
143
144   return AnyChanges;
145 }