blob: 39940c35da5d5f076857ec31822b62b2dcaf292b [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"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024#include "llvm/Support/InstIterator.h"
25#include "llvm/ADT/Statistic.h"
26#include <set>
27using namespace llvm;
28
29STATISTIC(DIEEliminated, "Number of insts removed by DIE pass");
30STATISTIC(DCEEliminated, "Number of insts removed");
31
32namespace {
33 //===--------------------------------------------------------------------===//
34 // DeadInstElimination pass implementation
35 //
Chris Lattnerfa2d1ba2009-09-02 06:11:42 +000036 struct DeadInstElimination : public BasicBlockPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037 static char ID; // Pass identification, replacement for typeid
Dan Gohmanc74a1972009-02-18 05:09:16 +000038 DeadInstElimination() : BasicBlockPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039 virtual bool runOnBasicBlock(BasicBlock &BB) {
40 bool Changed = false;
Chris Lattnerd16cf312008-11-27 22:46:09 +000041 for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); ) {
42 Instruction *Inst = DI++;
43 if (isInstructionTriviallyDead(Inst)) {
44 Inst->eraseFromParent();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045 Changed = true;
46 ++DIEEliminated;
Chris Lattnerd16cf312008-11-27 22:46:09 +000047 }
48 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 return Changed;
50 }
51
52 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
53 AU.setPreservesCFG();
54 }
55 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056}
57
Dan Gohman089efff2008-05-13 00:00:25 +000058char DeadInstElimination::ID = 0;
59static RegisterPass<DeadInstElimination>
60X("die", "Dead Instruction Elimination");
61
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062Pass *llvm::createDeadInstEliminationPass() {
63 return new DeadInstElimination();
64}
65
66
67namespace {
68 //===--------------------------------------------------------------------===//
69 // DeadCodeElimination pass implementation
70 //
71 struct DCE : public FunctionPass {
72 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000073 DCE() : FunctionPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074
75 virtual bool runOnFunction(Function &F);
76
77 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
78 AU.setPreservesCFG();
79 }
80 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081}
82
Dan Gohman089efff2008-05-13 00:00:25 +000083char DCE::ID = 0;
84static RegisterPass<DCE> Y("dce", "Dead Code Elimination");
85
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086bool DCE::runOnFunction(Function &F) {
87 // Start out with all of the instructions in the worklist...
88 std::vector<Instruction*> WorkList;
89 for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i)
90 WorkList.push_back(&*i);
91
92 // Loop over the worklist finding instructions that are dead. If they are
93 // dead make them drop all of their uses, making other instructions
94 // potentially dead, and work until the worklist is empty.
95 //
96 bool MadeChange = false;
97 while (!WorkList.empty()) {
98 Instruction *I = WorkList.back();
99 WorkList.pop_back();
100
101 if (isInstructionTriviallyDead(I)) { // If the instruction is dead.
102 // Loop over all of the values that the instruction uses, if there are
103 // instructions being used, add them to the worklist, because they might
104 // go dead after this one is removed.
105 //
106 for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI)
107 if (Instruction *Used = dyn_cast<Instruction>(*OI))
108 WorkList.push_back(Used);
109
110 // Remove the instruction.
111 I->eraseFromParent();
112
113 // Remove the instruction from the worklist if it still exists in it.
David Greene41ebd512007-12-17 17:39:51 +0000114 for (std::vector<Instruction*>::iterator WI = WorkList.begin();
Bill Wendlingbd9e0522008-09-18 23:04:18 +0000115 WI != WorkList.end(); ) {
116 if (*WI == I)
David Greene41ebd512007-12-17 17:39:51 +0000117 WI = WorkList.erase(WI);
Bill Wendlingbd9e0522008-09-18 23:04:18 +0000118 else
119 ++WI;
120 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121
122 MadeChange = true;
123 ++DCEEliminated;
124 }
125 }
126 return MadeChange;
127}
128
129FunctionPass *llvm::createDeadCodeEliminationPass() {
130 return new DCE();
131}
132