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/DCE.h"
13 #include "llvm/Pass.h"
14 #include "llvm/InstrTypes.h"
15 #include "llvm/Function.h"
16 #include "llvm/Support/InstIterator.h"
19 static inline bool isInstDead(Instruction *I) {
20 return I->use_empty() && !I->hasSideEffects() && !isa<TerminatorInst>(I);
23 // dceInstruction - Inspect the instruction at *BBI and figure out if it's
24 // [trivially] dead. If so, remove the instruction and update the iterator
25 // to point to the instruction that immediately succeeded the original
28 bool dceInstruction(BasicBlock::InstListType &BBIL,
29 BasicBlock::iterator &BBI) {
30 // Look for un"used" definitions...
31 if (isInstDead(*BBI)) {
32 delete BBIL.remove(BBI); // Bye bye
38 //===----------------------------------------------------------------------===//
39 // DeadInstElimination pass implementation
43 struct DeadInstElimination : public BasicBlockPass {
44 const char *getPassName() const { return "Dead Instruction Elimination"; }
46 virtual bool runOnBasicBlock(BasicBlock *BB) {
47 BasicBlock::InstListType &Vals = BB->getInstList();
49 for (BasicBlock::iterator DI = Vals.begin(); DI != Vals.end(); )
50 if (dceInstruction(Vals, DI))
57 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
63 Pass *createDeadInstEliminationPass() {
64 return new DeadInstElimination();
69 //===----------------------------------------------------------------------===//
70 // DeadCodeElimination pass implementation
74 struct DCE : public FunctionPass {
75 const char *getPassName() const { return "Dead Code Elimination"; }
77 virtual bool runOnFunction(Function *F);
79 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
85 bool DCE::runOnFunction(Function *F) {
86 // Start out with all of the instructions in the worklist...
87 std::vector<Instruction*> WorkList(inst_begin(F), inst_end(F));
88 std::set<Instruction*> DeadInsts;
90 // Loop over the worklist finding instructions that are dead. If they are
91 // dead make them drop all of their uses, making other instructions
92 // potentially dead, and work until the worklist is empty.
94 while (!WorkList.empty()) {
95 Instruction *I = WorkList.back();
98 if (isInstDead(I)) { // If the instruction is dead...
99 // Loop over all of the values that the instruction uses, if there are
100 // instructions being used, add them to the worklist, because they might
101 // go dead after this one is removed.
103 for (User::use_iterator UI = I->use_begin(), UE = I->use_end();
105 if (Instruction *Used = dyn_cast<Instruction>(*UI))
106 WorkList.push_back(Used);
108 // Tell the instruction to let go of all of the values it uses...
109 I->dropAllReferences();
111 // Keep track of this instruction, because we are going to delete it later
116 // If we found no dead instructions, we haven't changed the function...
117 if (DeadInsts.empty()) return false;
119 // Otherwise, loop over the program, removing and deleting the instructions...
120 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
121 BasicBlock::InstListType &BBIL = (*I)->getInstList();
122 for (BasicBlock::iterator BI = BBIL.begin(); BI != BBIL.end(); )
123 if (DeadInsts.count(*BI)) { // Is this instruction dead?
124 delete BBIL.remove(BI); // Yup, remove and delete inst
125 } else { // This instruction is not dead
126 ++BI; // Continue on to the next one...
133 Pass *createDeadCodeEliminationPass() {