blob: 2903699a8d16697353492452bcc3269bf06ba029 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===- DCE.cpp - Code to perform dead code elimination --------------------===//
2//
Chris Lattner92deeaf2002-05-07 04:24:11 +00003// This file implements dead inst elimination and dead code elimination.
Chris Lattner00950542001-06-06 20:29:01 +00004//
Chris Lattner92deeaf2002-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 Lattner00950542001-06-06 20:29:01 +00009//
10//===----------------------------------------------------------------------===//
11
Chris Lattner022103b2002-05-07 20:03:00 +000012#include "llvm/Transforms/Scalar.h"
Chris Lattner2ed01d82002-05-07 18:10:55 +000013#include "llvm/Transforms/Utils/Local.h"
14#include "llvm/Instruction.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000015#include "llvm/Pass.h"
Chris Lattner92deeaf2002-05-07 04:24:11 +000016#include "llvm/Support/InstIterator.h"
Chris Lattner3dec1f22002-05-10 15:38:35 +000017#include "Support/StatisticReporter.h"
Chris Lattner92deeaf2002-05-07 04:24:11 +000018#include <set>
19
Chris Lattner3dec1f22002-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 Lattner92deeaf2002-05-07 04:24:11 +000023//===----------------------------------------------------------------------===//
24// DeadInstElimination pass implementation
25//
26
27namespace {
28 struct DeadInstElimination : public BasicBlockPass {
Chris Lattner7e708292002-06-25 16:13:24 +000029 virtual bool runOnBasicBlock(BasicBlock &BB) {
Chris Lattner92deeaf2002-05-07 04:24:11 +000030 bool Changed = false;
Chris Lattner7e708292002-06-25 16:13:24 +000031 for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); )
Chris Lattner16da4942002-05-26 20:18:18 +000032 if (dceInstruction(DI)) {
Chris Lattner92deeaf2002-05-07 04:24:11 +000033 Changed = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +000034 ++DIEEliminated;
35 } else
Chris Lattner92deeaf2002-05-07 04:24:11 +000036 ++DI;
37 return Changed;
38 }
39
40 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
41 AU.preservesCFG();
42 }
43 };
Chris Lattnerf6293092002-07-23 18:06:35 +000044
Chris Lattnera6275cc2002-07-26 21:12:46 +000045 RegisterOpt<DeadInstElimination> X("die", "Dead Instruction Elimination");
Chris Lattner00950542001-06-06 20:29:01 +000046}
47
Chris Lattnerbd0ef772002-02-26 21:46:54 +000048Pass *createDeadInstEliminationPass() {
49 return new DeadInstElimination();
Chris Lattnerc560f882002-01-23 05:48:24 +000050}
51
Chris Lattner92deeaf2002-05-07 04:24:11 +000052
53
54//===----------------------------------------------------------------------===//
55// DeadCodeElimination pass implementation
Chris Lattnerf155e132001-06-07 16:59:26 +000056//
Chris Lattnerbd0ef772002-02-26 21:46:54 +000057
58namespace {
Chris Lattner92deeaf2002-05-07 04:24:11 +000059 struct DCE : public FunctionPass {
Chris Lattner7e708292002-06-25 16:13:24 +000060 virtual bool runOnFunction(Function &F);
Chris Lattner92deeaf2002-05-07 04:24:11 +000061
62 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
63 AU.preservesCFG();
Chris Lattnerbd0ef772002-02-26 21:46:54 +000064 }
Chris Lattner92deeaf2002-05-07 04:24:11 +000065 };
Chris Lattnerf6293092002-07-23 18:06:35 +000066
Chris Lattnera6275cc2002-07-26 21:12:46 +000067 RegisterOpt<DCE> Y("dce", "Dead Code Elimination");
Chris Lattner92deeaf2002-05-07 04:24:11 +000068}
69
Chris Lattner7e708292002-06-25 16:13:24 +000070bool DCE::runOnFunction(Function &F) {
Chris Lattner92deeaf2002-05-07 04:24:11 +000071 // 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;
74
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.
78 //
79 while (!WorkList.empty()) {
80 Instruction *I = WorkList.back();
81 WorkList.pop_back();
Chris Lattnerbd0ef772002-02-26 21:46:54 +000082
Chris Lattner2ed01d82002-05-07 18:10:55 +000083 if (isInstructionTriviallyDead(I)) { // If the instruction is dead...
Chris Lattner92deeaf2002-05-07 04:24:11 +000084 // 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.
87 //
88 for (User::use_iterator UI = I->use_begin(), UE = I->use_end();
89 UI != UE; ++UI)
90 if (Instruction *Used = dyn_cast<Instruction>(*UI))
91 WorkList.push_back(Used);
92
93 // Tell the instruction to let go of all of the values it uses...
94 I->dropAllReferences();
95
96 // Keep track of this instruction, because we are going to delete it later
97 DeadInsts.insert(I);
Chris Lattnerbd0ef772002-02-26 21:46:54 +000098 }
Chris Lattner92deeaf2002-05-07 04:24:11 +000099 }
100
101 // If we found no dead instructions, we haven't changed the function...
102 if (DeadInsts.empty()) return false;
103
104 // Otherwise, loop over the program, removing and deleting the instructions...
Chris Lattner7e708292002-06-25 16:13:24 +0000105 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
Chris Lattner3dec1f22002-05-10 15:38:35 +0000109 ++DCEEliminated;
Chris Lattner92deeaf2002-05-07 04:24:11 +0000110 } else { // This instruction is not dead
111 ++BI; // Continue on to the next one...
112 }
Chris Lattner92deeaf2002-05-07 04:24:11 +0000113
114 return true;
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000115}
116
117Pass *createDeadCodeEliminationPass() {
Chris Lattner92deeaf2002-05-07 04:24:11 +0000118 return new DCE();
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000119}