Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 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 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(); |
Dan Gohman | e251b15 | 2008-09-18 18:22:32 +0000 | [diff] [blame] | 67 | if ((!MO.isImplicit() && |
| 68 | TargetRegisterInfo::isPhysicalRegister(Reg)) || |
Dan Gohman | d3ead43 | 2008-09-17 00:43:24 +0000 | [diff] [blame] | 69 | !MRI.use_empty(Reg)) { |
| 70 | // This def has a use. Don't delete the instruction! |
| 71 | AllDefsDead = false; |
| 72 | break; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // If there are no defs with uses, the instruction is dead. |
| 78 | if (AllDefsDead) { |
| 79 | // Clear out the operands to take the registers out of their |
| 80 | // use chains. |
| 81 | while (unsigned Num = MI->getNumOperands()) |
| 82 | MI->RemoveOperand(Num-1); |
| 83 | |
| 84 | // Delete the actual instruction. |
| 85 | AnyChanges = true; |
| 86 | MI->eraseFromParent(); |
| 87 | MIE = MBB->rend(); |
| 88 | // MII is now pointing to the next instruction to process, |
| 89 | // so don't increment it. |
| 90 | continue; |
| 91 | } |
| 92 | } |
| 93 | // We didn't delete the current instruction, so increment MII to |
| 94 | // the next one. |
| 95 | ++MII; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return AnyChanges; |
| 100 | } |