blob: fa2392fbb4248b0f44e8fe235d3158bf09857566 [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===- DCE.cpp - Code to perform dead code elimination --------------------===//
2//
Chris Lattner87e88062002-05-07 04:24:11 +00003// This file implements dead inst elimination and dead code elimination.
Chris Lattner2f7c9632001-06-06 20:29:01 +00004//
Chris Lattner87e88062002-05-07 04:24:11 +00005// 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.
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
10//===----------------------------------------------------------------------===//
11
Chris Lattnerb4cfa7f2002-05-07 20:03:00 +000012#include "llvm/Transforms/Scalar.h"
Chris Lattner560da702002-05-07 18:10:55 +000013#include "llvm/Transforms/Utils/Local.h"
14#include "llvm/Instruction.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000015#include "llvm/Pass.h"
Chris Lattner87e88062002-05-07 04:24:11 +000016#include "llvm/Support/InstIterator.h"
Chris Lattner0b18c1d2002-05-10 15:38:35 +000017#include "Support/StatisticReporter.h"
Chris Lattner87e88062002-05-07 04:24:11 +000018#include <set>
19
Chris Lattner0b18c1d2002-05-10 15:38:35 +000020static Statistic<> DIEEliminated("die\t\t- Number of insts removed");
21static Statistic<> DCEEliminated("dce\t\t- Number of insts removed");
22
Chris Lattner87e88062002-05-07 04:24:11 +000023//===----------------------------------------------------------------------===//
24// DeadInstElimination pass implementation
25//
26
27namespace {
28 struct DeadInstElimination : public BasicBlockPass {
29 const char *getPassName() const { return "Dead Instruction Elimination"; }
30
31 virtual bool runOnBasicBlock(BasicBlock *BB) {
32 BasicBlock::InstListType &Vals = BB->getInstList();
33 bool Changed = false;
34 for (BasicBlock::iterator DI = Vals.begin(); DI != Vals.end(); )
Chris Lattnerab038d42002-05-26 20:18:18 +000035 if (dceInstruction(DI)) {
Chris Lattner87e88062002-05-07 04:24:11 +000036 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +000037 ++DIEEliminated;
38 } else
Chris Lattner87e88062002-05-07 04:24:11 +000039 ++DI;
40 return Changed;
41 }
42
43 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
44 AU.preservesCFG();
45 }
46 };
Chris Lattner2f7c9632001-06-06 20:29:01 +000047}
48
Chris Lattner04805fa2002-02-26 21:46:54 +000049Pass *createDeadInstEliminationPass() {
50 return new DeadInstElimination();
Chris Lattnerccbd4e42002-01-23 05:48:24 +000051}
52
Chris Lattner87e88062002-05-07 04:24:11 +000053
54
55//===----------------------------------------------------------------------===//
56// DeadCodeElimination pass implementation
Chris Lattnerd821c2a2001-06-07 16:59:26 +000057//
Chris Lattner04805fa2002-02-26 21:46:54 +000058
59namespace {
Chris Lattner87e88062002-05-07 04:24:11 +000060 struct DCE : public FunctionPass {
Chris Lattner37104aa2002-04-29 14:57:45 +000061 const char *getPassName() const { return "Dead Code Elimination"; }
Chris Lattner04805fa2002-02-26 21:46:54 +000062
Chris Lattner87e88062002-05-07 04:24:11 +000063 virtual bool runOnFunction(Function *F);
64
65 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
66 AU.preservesCFG();
Chris Lattner04805fa2002-02-26 21:46:54 +000067 }
Chris Lattner87e88062002-05-07 04:24:11 +000068 };
69}
70
71bool 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;
75
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.
79 //
80 while (!WorkList.empty()) {
81 Instruction *I = WorkList.back();
82 WorkList.pop_back();
Chris Lattner04805fa2002-02-26 21:46:54 +000083
Chris Lattner560da702002-05-07 18:10:55 +000084 if (isInstructionTriviallyDead(I)) { // If the instruction is dead...
Chris Lattner87e88062002-05-07 04:24:11 +000085 // 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.
88 //
89 for (User::use_iterator UI = I->use_begin(), UE = I->use_end();
90 UI != UE; ++UI)
91 if (Instruction *Used = dyn_cast<Instruction>(*UI))
92 WorkList.push_back(Used);
93
94 // Tell the instruction to let go of all of the values it uses...
95 I->dropAllReferences();
96
97 // Keep track of this instruction, because we are going to delete it later
98 DeadInsts.insert(I);
Chris Lattner04805fa2002-02-26 21:46:54 +000099 }
Chris Lattner87e88062002-05-07 04:24:11 +0000100 }
101
102 // If we found no dead instructions, we haven't changed the function...
103 if (DeadInsts.empty()) return false;
104
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
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000111 ++DCEEliminated;
Chris Lattner87e88062002-05-07 04:24:11 +0000112 } else { // This instruction is not dead
113 ++BI; // Continue on to the next one...
114 }
115 }
116
117 return true;
Chris Lattner04805fa2002-02-26 21:46:54 +0000118}
119
120Pass *createDeadCodeEliminationPass() {
Chris Lattner87e88062002-05-07 04:24:11 +0000121 return new DCE();
Chris Lattner04805fa2002-02-26 21:46:54 +0000122}