llvm.org GIT mirror llvm / d3ead43
Add a new MachineInstr-level DCE pass. It is very simple, and is intended to be used with fast-isel. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@56268 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 11 years ago
3 changed file(s) with 106 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
2828 if (std::getenv("bar") != (char*) -1)
2929 return;
3030
31 (void) llvm::createDeadMachineInstructionElimPass();
32
3133 (void) llvm::createSimpleRegisterAllocator();
3234 (void) llvm::createLocalRegisterAllocator();
3335 (void) llvm::createBigBlockRegisterAllocator();
7373 /// UnreachableMachineBlockElimination pass - This pass removes unreachable
7474 /// machine basic blocks.
7575 extern const PassInfo *const UnreachableMachineBlockElimID;
76
77 /// DeadMachineInstructionElim pass - This pass removes dead machine
78 /// instructions.
79 ///
80 FunctionPass *createDeadMachineInstructionElimPass();
7681
7782 /// Creates a register allocator as the user specified on the command line.
7883 ///
0 //===- DeadMachineInstructionElim.cpp - Remove dead machine instructions --===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This is an extremely simple MachineInstr-level dead-code-elimination pass.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/CodeGen/Passes.h"
14 #include "llvm/Pass.h"
15 #include "llvm/CodeGen/MachineFunctionPass.h"
16 #include "llvm/CodeGen/MachineRegisterInfo.h"
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Target/TargetInstrInfo.h"
19 #include "llvm/Target/TargetMachine.h"
20 using namespace llvm;
21
22 namespace {
23 class VISIBILITY_HIDDEN DeadMachineInstructionElim :
24 public MachineFunctionPass {
25 virtual bool runOnMachineFunction(MachineFunction &MF);
26
27 public:
28 static char ID; // Pass identification, replacement for typeid
29 DeadMachineInstructionElim() : MachineFunctionPass(&ID) {}
30 };
31 }
32 char DeadMachineInstructionElim::ID = 0;
33
34 static RegisterPass
35 Y("dead-mi-elimination",
36 "Remove dead machine instructions");
37
38 FunctionPass *llvm::createDeadMachineInstructionElimPass() {
39 return new DeadMachineInstructionElim();
40 }
41
42 bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) {
43 bool AnyChanges = false;
44 const MachineRegisterInfo &MRI = MF.getRegInfo();
45 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
46 bool SawStore = true;
47
48 // Loop over all instructions in all blocks, from bottom to top, so that it's
49 // more likely that chains of dependent but ultimately dead instructions will
50 // be cleaned up.
51 for (MachineFunction::reverse_iterator I = MF.rbegin(), E = MF.rend();
52 I != E; ++I) {
53 MachineBasicBlock *MBB = &*I;
54 for (MachineBasicBlock::reverse_iterator MII = MBB->rbegin(),
55 MIE = MBB->rend(); MII != MIE; ) {
56 MachineInstr *MI = &*MII;
57
58 // Don't delete instructions with side effects.
59 if (MI->isSafeToMove(&TII, SawStore)) {
60 // Examine each operand.
61 bool AllDefsDead = true;
62 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
63 const MachineOperand &MO = MI->getOperand(i);
64 if (MO.isRegister() && MO.isDef()) {
65 unsigned Reg = MO.getReg();
66 if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
67 !MRI.use_empty(Reg)) {
68 // This def has a use. Don't delete the instruction!
69 AllDefsDead = false;
70 break;
71 }
72 }
73 }
74
75 // If there are no defs with uses, the instruction is dead.
76 if (AllDefsDead) {
77 // Clear out the operands to take the registers out of their
78 // use chains.
79 while (unsigned Num = MI->getNumOperands())
80 MI->RemoveOperand(Num-1);
81
82 // Delete the actual instruction.
83 AnyChanges = true;
84 MI->eraseFromParent();
85 MIE = MBB->rend();
86 // MII is now pointing to the next instruction to process,
87 // so don't increment it.
88 continue;
89 }
90 }
91 // We didn't delete the current instruction, so increment MII to
92 // the next one.
93 ++MII;
94 }
95 }
96
97 return AnyChanges;
98 }