blob: fb9a0e04fd6243a9380f2d2f0f902cbb8790854a [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//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055}
56
Dan Gohman089efff2008-05-13 00:00:25 +000057char DeadInstElimination::ID = 0;
58static RegisterPass<DeadInstElimination>
59X("die", "Dead Instruction Elimination");
60
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061Pass *llvm::createDeadInstEliminationPass() {
62 return new DeadInstElimination();
63}
64
65
66namespace {
67 //===--------------------------------------------------------------------===//
68 // DeadCodeElimination pass implementation
69 //
70 struct DCE : public FunctionPass {
71 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000072 DCE() : FunctionPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073
74 virtual bool runOnFunction(Function &F);
75
76 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
77 AU.setPreservesCFG();
78 }
79 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080}
81
Dan Gohman089efff2008-05-13 00:00:25 +000082char DCE::ID = 0;
83static RegisterPass<DCE> Y("dce", "Dead Code Elimination");
84
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085bool DCE::runOnFunction(Function &F) {
86 // Start out with all of the instructions in the worklist...
87 std::vector<Instruction*> WorkList;
88 for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i)
89 WorkList.push_back(&*i);
90
91 // Loop over the worklist finding instructions that are dead. If they are
92 // dead make them drop all of their uses, making other instructions
93 // potentially dead, and work until the worklist is empty.
94 //
95 bool MadeChange = false;
96 while (!WorkList.empty()) {
97 Instruction *I = WorkList.back();
98 WorkList.pop_back();
99
100 if (isInstructionTriviallyDead(I)) { // If the instruction is dead.
101 // Loop over all of the values that the instruction uses, if there are
102 // instructions being used, add them to the worklist, because they might
103 // go dead after this one is removed.
104 //
105 for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI)
106 if (Instruction *Used = dyn_cast<Instruction>(*OI))
107 WorkList.push_back(Used);
108
109 // Remove the instruction.
110 I->eraseFromParent();
111
112 // Remove the instruction from the worklist if it still exists in it.
David Greene41ebd512007-12-17 17:39:51 +0000113 for (std::vector<Instruction*>::iterator WI = WorkList.begin();
Bill Wendlingbd9e0522008-09-18 23:04:18 +0000114 WI != WorkList.end(); ) {
115 if (*WI == I)
David Greene41ebd512007-12-17 17:39:51 +0000116 WI = WorkList.erase(WI);
Bill Wendlingbd9e0522008-09-18 23:04:18 +0000117 else
118 ++WI;
119 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120
121 MadeChange = true;
122 ++DCEEliminated;
123 }
124 }
125 return MadeChange;
126}
127
128FunctionPass *llvm::createDeadCodeEliminationPass() {
129 return new DCE();
130}
131