blob: 9cad23629542a567f433632db858922537d2d2f4 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- DCE.cpp - Code to perform dead code elimination --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements dead inst elimination and dead code elimination.
11//
12// Dead Inst Elimination performs a single pass over the function removing
13// instructions that are obviously dead. Dead Code Elimination is similar, but
14// it rechecks instructions that were used by removed instructions to see if
15// they are newly dead.
16//
17//===----------------------------------------------------------------------===//
18
19#define DEBUG_TYPE "dce"
20#include "llvm/Transforms/Scalar.h"
21#include "llvm/Transforms/Utils/Local.h"
22#include "llvm/Instruction.h"
23#include "llvm/Pass.h"
24#include "llvm/Support/Compiler.h"
25#include "llvm/Support/InstIterator.h"
26#include "llvm/ADT/Statistic.h"
27#include <set>
28using namespace llvm;
29
30STATISTIC(DIEEliminated, "Number of insts removed by DIE pass");
31STATISTIC(DCEEliminated, "Number of insts removed");
32
33namespace {
34 //===--------------------------------------------------------------------===//
35 // DeadInstElimination pass implementation
36 //
37 struct VISIBILITY_HIDDEN DeadInstElimination : public BasicBlockPass {
38 static char ID; // Pass identification, replacement for typeid
39 DeadInstElimination() : BasicBlockPass(intptr_t(&ID)) {}
40 virtual bool runOnBasicBlock(BasicBlock &BB) {
41 bool Changed = false;
42 for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); )
43 if (dceInstruction(DI)) {
44 Changed = true;
45 ++DIEEliminated;
46 } else
47 ++DI;
48 return Changed;
49 }
50
51 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
52 AU.setPreservesCFG();
53 }
54 };
55
56 char DeadInstElimination::ID = 0;
57 RegisterPass<DeadInstElimination> X("die", "Dead Instruction Elimination");
58}
59
60Pass *llvm::createDeadInstEliminationPass() {
61 return new DeadInstElimination();
62}
63
64
65namespace {
66 //===--------------------------------------------------------------------===//
67 // DeadCodeElimination pass implementation
68 //
69 struct DCE : public FunctionPass {
70 static char ID; // Pass identification, replacement for typeid
71 DCE() : FunctionPass((intptr_t)&ID) {}
72
73 virtual bool runOnFunction(Function &F);
74
75 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
76 AU.setPreservesCFG();
77 }
78 };
79
80 char DCE::ID = 0;
81 RegisterPass<DCE> Y("dce", "Dead Code Elimination");
82}
83
84bool DCE::runOnFunction(Function &F) {
85 // Start out with all of the instructions in the worklist...
86 std::vector<Instruction*> WorkList;
87 for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i)
88 WorkList.push_back(&*i);
89
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.
93 //
94 bool MadeChange = false;
95 while (!WorkList.empty()) {
96 Instruction *I = WorkList.back();
97 WorkList.pop_back();
98
99 if (isInstructionTriviallyDead(I)) { // If the instruction is dead.
100 // Loop over all of the values that the instruction uses, if there are
101 // instructions being used, add them to the worklist, because they might
102 // go dead after this one is removed.
103 //
104 for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI)
105 if (Instruction *Used = dyn_cast<Instruction>(*OI))
106 WorkList.push_back(Used);
107
108 // Remove the instruction.
109 I->eraseFromParent();
110
111 // Remove the instruction from the worklist if it still exists in it.
David Greene41ebd512007-12-17 17:39:51 +0000112 for (std::vector<Instruction*>::iterator WI = WorkList.begin();
113 WI != WorkList.end(); ++WI)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 if (*WI == I) {
David Greene41ebd512007-12-17 17:39:51 +0000115 WI = WorkList.erase(WI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 --WI;
117 }
118
119 MadeChange = true;
120 ++DCEEliminated;
121 }
122 }
123 return MadeChange;
124}
125
126FunctionPass *llvm::createDeadCodeEliminationPass() {
127 return new DCE();
128}
129