blob: cfeb86858935b6de8b0df28c172632a01b9fb05c [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 Lattnerbf3a0992002-10-01 22:38:41 +000017#include "Support/Statistic.h"
Chris Lattner87e88062002-05-07 04:24:11 +000018#include <set>
19
Chris Lattner87e88062002-05-07 04:24:11 +000020namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000021 Statistic<> DIEEliminated("die", "Number of insts removed");
22 Statistic<> DCEEliminated("dce", "Number of insts removed");
23
24 //===--------------------------------------------------------------------===//
25 // DeadInstElimination pass implementation
26 //
27
Chris Lattner87e88062002-05-07 04:24:11 +000028 struct DeadInstElimination : public BasicBlockPass {
Chris Lattner113f4f42002-06-25 16:13:24 +000029 virtual bool runOnBasicBlock(BasicBlock &BB) {
Chris Lattner87e88062002-05-07 04:24:11 +000030 bool Changed = false;
Chris Lattner113f4f42002-06-25 16:13:24 +000031 for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); )
Chris Lattnerab038d42002-05-26 20:18:18 +000032 if (dceInstruction(DI)) {
Chris Lattner87e88062002-05-07 04:24:11 +000033 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +000034 ++DIEEliminated;
35 } else
Chris Lattner87e88062002-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 Lattnerb28b6802002-07-23 18:06:35 +000044
Chris Lattnerc8b70922002-07-26 21:12:46 +000045 RegisterOpt<DeadInstElimination> X("die", "Dead Instruction Elimination");
Chris Lattner2f7c9632001-06-06 20:29:01 +000046}
47
Chris Lattner04805fa2002-02-26 21:46:54 +000048Pass *createDeadInstEliminationPass() {
49 return new DeadInstElimination();
Chris Lattnerccbd4e42002-01-23 05:48:24 +000050}
51
Chris Lattner87e88062002-05-07 04:24:11 +000052
53
54//===----------------------------------------------------------------------===//
55// DeadCodeElimination pass implementation
Chris Lattnerd821c2a2001-06-07 16:59:26 +000056//
Chris Lattner04805fa2002-02-26 21:46:54 +000057
58namespace {
Chris Lattner87e88062002-05-07 04:24:11 +000059 struct DCE : public FunctionPass {
Chris Lattner113f4f42002-06-25 16:13:24 +000060 virtual bool runOnFunction(Function &F);
Chris Lattner87e88062002-05-07 04:24:11 +000061
62 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
63 AU.preservesCFG();
Chris Lattner04805fa2002-02-26 21:46:54 +000064 }
Chris Lattner87e88062002-05-07 04:24:11 +000065 };
Chris Lattnerb28b6802002-07-23 18:06:35 +000066
Chris Lattnerc8b70922002-07-26 21:12:46 +000067 RegisterOpt<DCE> Y("dce", "Dead Code Elimination");
Chris Lattner87e88062002-05-07 04:24:11 +000068}
69
Chris Lattner113f4f42002-06-25 16:13:24 +000070bool DCE::runOnFunction(Function &F) {
Chris Lattner87e88062002-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 Lattner04805fa2002-02-26 21:46:54 +000082
Chris Lattner560da702002-05-07 18:10:55 +000083 if (isInstructionTriviallyDead(I)) { // If the instruction is dead...
Chris Lattner87e88062002-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 Lattner04805fa2002-02-26 21:46:54 +000098 }
Chris Lattner87e88062002-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 Lattner113f4f42002-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 Lattner0b18c1d2002-05-10 15:38:35 +0000109 ++DCEEliminated;
Chris Lattner87e88062002-05-07 04:24:11 +0000110 } else { // This instruction is not dead
111 ++BI; // Continue on to the next one...
112 }
Chris Lattner87e88062002-05-07 04:24:11 +0000113
114 return true;
Chris Lattner04805fa2002-02-26 21:46:54 +0000115}
116
117Pass *createDeadCodeEliminationPass() {
Chris Lattner87e88062002-05-07 04:24:11 +0000118 return new DCE();
Chris Lattner04805fa2002-02-26 21:46:54 +0000119}