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/StatisticReporter.h"
20 static Statistic<> DIEEliminated("die\t\t- Number of insts removed");
21 static Statistic<> DCEEliminated("dce\t\t- Number of insts removed");
23 //===----------------------------------------------------------------------===//
24 // DeadInstElimination pass implementation
28 struct DeadInstElimination : public BasicBlockPass {
29 const char *getPassName() const { return "Dead Instruction Elimination"; }
31 virtual bool runOnBasicBlock(BasicBlock *BB) {
32 BasicBlock::InstListType &Vals = BB->getInstList();
34 for (BasicBlock::iterator DI = Vals.begin(); DI != Vals.end(); )
35 if (dceInstruction(Vals, DI)) {
43 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
49 Pass *createDeadInstEliminationPass() {
50 return new DeadInstElimination();
55 //===----------------------------------------------------------------------===//
56 // DeadCodeElimination pass implementation
60 struct DCE : public FunctionPass {
61 const char *getPassName() const { return "Dead Code Elimination"; }
63 virtual bool runOnFunction(Function *F);
65 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
71 bool DCE::runOnFunction(Function *F) {
72 // Start out with all of the instructions in the worklist...
73 std::vector<Instruction*> WorkList(inst_begin(F), inst_end(F));
74 std::set<Instruction*> DeadInsts;
76 // Loop over the worklist finding instructions that are dead. If they are
77 // dead make them drop all of their uses, making other instructions
78 // potentially dead, and work until the worklist is empty.
80 while (!WorkList.empty()) {
81 Instruction *I = WorkList.back();
84 if (isInstructionTriviallyDead(I)) { // If the instruction is dead...
85 // Loop over all of the values that the instruction uses, if there are
86 // instructions being used, add them to the worklist, because they might
87 // go dead after this one is removed.
89 for (User::use_iterator UI = I->use_begin(), UE = I->use_end();
91 if (Instruction *Used = dyn_cast<Instruction>(*UI))
92 WorkList.push_back(Used);
94 // Tell the instruction to let go of all of the values it uses...
95 I->dropAllReferences();
97 // Keep track of this instruction, because we are going to delete it later
102 // If we found no dead instructions, we haven't changed the function...
103 if (DeadInsts.empty()) return false;
105 // Otherwise, loop over the program, removing and deleting the instructions...
106 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
107 BasicBlock::InstListType &BBIL = (*I)->getInstList();
108 for (BasicBlock::iterator BI = BBIL.begin(); BI != BBIL.end(); )
109 if (DeadInsts.count(*BI)) { // Is this instruction dead?
110 delete BBIL.remove(BI); // Yup, remove and delete inst
112 } else { // This instruction is not dead
113 ++BI; // Continue on to the next one...
120 Pass *createDeadCodeEliminationPass() {