blob: 793b3abb6a3adf30d6446f9055f6c5bbb0830d31 [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"
17#include <set>
18
Chris Lattner87e88062002-05-07 04:24:11 +000019//===----------------------------------------------------------------------===//
20// DeadInstElimination pass implementation
21//
22
23namespace {
24 struct DeadInstElimination : public BasicBlockPass {
25 const char *getPassName() const { return "Dead Instruction Elimination"; }
26
27 virtual bool runOnBasicBlock(BasicBlock *BB) {
28 BasicBlock::InstListType &Vals = BB->getInstList();
29 bool Changed = false;
30 for (BasicBlock::iterator DI = Vals.begin(); DI != Vals.end(); )
31 if (dceInstruction(Vals, DI))
32 Changed = true;
33 else
34 ++DI;
35 return Changed;
36 }
37
38 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
39 AU.preservesCFG();
40 }
41 };
Chris Lattner2f7c9632001-06-06 20:29:01 +000042}
43
Chris Lattner04805fa2002-02-26 21:46:54 +000044Pass *createDeadInstEliminationPass() {
45 return new DeadInstElimination();
Chris Lattnerccbd4e42002-01-23 05:48:24 +000046}
47
Chris Lattner87e88062002-05-07 04:24:11 +000048
49
50//===----------------------------------------------------------------------===//
51// DeadCodeElimination pass implementation
Chris Lattnerd821c2a2001-06-07 16:59:26 +000052//
Chris Lattner04805fa2002-02-26 21:46:54 +000053
54namespace {
Chris Lattner87e88062002-05-07 04:24:11 +000055 struct DCE : public FunctionPass {
Chris Lattner37104aa2002-04-29 14:57:45 +000056 const char *getPassName() const { return "Dead Code Elimination"; }
Chris Lattner04805fa2002-02-26 21:46:54 +000057
Chris Lattner87e88062002-05-07 04:24:11 +000058 virtual bool runOnFunction(Function *F);
59
60 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
61 AU.preservesCFG();
Chris Lattner04805fa2002-02-26 21:46:54 +000062 }
Chris Lattner87e88062002-05-07 04:24:11 +000063 };
64}
65
66bool DCE::runOnFunction(Function *F) {
67 // Start out with all of the instructions in the worklist...
68 std::vector<Instruction*> WorkList(inst_begin(F), inst_end(F));
69 std::set<Instruction*> DeadInsts;
70
71 // Loop over the worklist finding instructions that are dead. If they are
72 // dead make them drop all of their uses, making other instructions
73 // potentially dead, and work until the worklist is empty.
74 //
75 while (!WorkList.empty()) {
76 Instruction *I = WorkList.back();
77 WorkList.pop_back();
Chris Lattner04805fa2002-02-26 21:46:54 +000078
Chris Lattner560da702002-05-07 18:10:55 +000079 if (isInstructionTriviallyDead(I)) { // If the instruction is dead...
Chris Lattner87e88062002-05-07 04:24:11 +000080 // Loop over all of the values that the instruction uses, if there are
81 // instructions being used, add them to the worklist, because they might
82 // go dead after this one is removed.
83 //
84 for (User::use_iterator UI = I->use_begin(), UE = I->use_end();
85 UI != UE; ++UI)
86 if (Instruction *Used = dyn_cast<Instruction>(*UI))
87 WorkList.push_back(Used);
88
89 // Tell the instruction to let go of all of the values it uses...
90 I->dropAllReferences();
91
92 // Keep track of this instruction, because we are going to delete it later
93 DeadInsts.insert(I);
Chris Lattner04805fa2002-02-26 21:46:54 +000094 }
Chris Lattner87e88062002-05-07 04:24:11 +000095 }
96
97 // If we found no dead instructions, we haven't changed the function...
98 if (DeadInsts.empty()) return false;
99
100 // Otherwise, loop over the program, removing and deleting the instructions...
101 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
102 BasicBlock::InstListType &BBIL = (*I)->getInstList();
103 for (BasicBlock::iterator BI = BBIL.begin(); BI != BBIL.end(); )
104 if (DeadInsts.count(*BI)) { // Is this instruction dead?
105 delete BBIL.remove(BI); // Yup, remove and delete inst
106 } else { // This instruction is not dead
107 ++BI; // Continue on to the next one...
108 }
109 }
110
111 return true;
Chris Lattner04805fa2002-02-26 21:46:54 +0000112}
113
114Pass *createDeadCodeEliminationPass() {
Chris Lattner87e88062002-05-07 04:24:11 +0000115 return new DCE();
Chris Lattner04805fa2002-02-26 21:46:54 +0000116}