1 //===- DCE.cpp - Code to perform dead code elimination --------------------===//
3 // This file implements dead inst elimination and dead code elimination.
5 // Dead Inst Elimination performs a single pass over the function removing
6 // instructions that are obviously dead. Dead Code Elimination is similar, but
7 // it rechecks instructions that were used by removed instructions to see if
8 // they are newly dead.
10 //===----------------------------------------------------------------------===//
12 #include "llvm/Transforms/Scalar.h"
13 #include "llvm/Transforms/Utils/Local.h"
14 #include "llvm/Instruction.h"
15 #include "llvm/Pass.h"
16 #include "llvm/Support/InstIterator.h"
17 #include "Support/Statistic.h"
21 Statistic<> DIEEliminated("die", "Number of insts removed");
22 Statistic<> DCEEliminated("dce", "Number of insts removed");
24 //===--------------------------------------------------------------------===//
25 // DeadInstElimination pass implementation
28 struct DeadInstElimination : public BasicBlockPass {
29 virtual bool runOnBasicBlock(BasicBlock &BB) {
31 for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); )
32 if (dceInstruction(DI)) {
40 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
45 RegisterOpt<DeadInstElimination> X("die", "Dead Instruction Elimination");
48 Pass *createDeadInstEliminationPass() {
49 return new DeadInstElimination();
54 //===----------------------------------------------------------------------===//
55 // DeadCodeElimination pass implementation
59 struct DCE : public FunctionPass {
60 virtual bool runOnFunction(Function &F);
62 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
67 RegisterOpt<DCE> Y("dce", "Dead Code Elimination");
70 bool DCE::runOnFunction(Function &F) {
71 // Start out with all of the instructions in the worklist...
72 std::vector<Instruction*> WorkList(inst_begin(F), inst_end(F));
73 std::set<Instruction*> DeadInsts;
75 // Loop over the worklist finding instructions that are dead. If they are
76 // dead make them drop all of their uses, making other instructions
77 // potentially dead, and work until the worklist is empty.
79 while (!WorkList.empty()) {
80 Instruction *I = WorkList.back();
83 if (isInstructionTriviallyDead(I)) { // If the instruction is dead...
84 // Loop over all of the values that the instruction uses, if there are
85 // instructions being used, add them to the worklist, because they might
86 // go dead after this one is removed.
88 for (User::use_iterator UI = I->use_begin(), UE = I->use_end();
90 if (Instruction *Used = dyn_cast<Instruction>(*UI))
91 WorkList.push_back(Used);
93 // Tell the instruction to let go of all of the values it uses...
94 I->dropAllReferences();
96 // Keep track of this instruction, because we are going to delete it later
101 // If we found no dead instructions, we haven't changed the function...
102 if (DeadInsts.empty()) return false;
104 // Otherwise, loop over the program, removing and deleting the instructions...
105 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
106 for (BasicBlock::iterator BI = I->begin(); BI != I->end(); )
107 if (DeadInsts.count(BI)) { // Is this instruction dead?
108 BI = I->getInstList().erase(BI); // Yup, remove and delete inst
110 } else { // This instruction is not dead
111 ++BI; // Continue on to the next one...
117 Pass *createDeadCodeEliminationPass() {