blob: a5990ee79e89d863b79eafba4e40f1687b2351d3 [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;
Chris Lattnerd16cf312008-11-27 22:46:09 +000042 for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); ) {
43 Instruction *Inst = DI++;
44 if (isInstructionTriviallyDead(Inst)) {
45 Inst->eraseFromParent();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046 Changed = true;
47 ++DIEEliminated;
Chris Lattnerd16cf312008-11-27 22:46:09 +000048 }
49 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 return Changed;
51 }
52
53 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
54 AU.setPreservesCFG();
55 }
56 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057}
58
Dan Gohman089efff2008-05-13 00:00:25 +000059char DeadInstElimination::ID = 0;
60static RegisterPass<DeadInstElimination>
61X("die", "Dead Instruction Elimination");
62
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063Pass *llvm::createDeadInstEliminationPass() {
64 return new DeadInstElimination();
65}
66
67
68namespace {
69 //===--------------------------------------------------------------------===//
70 // DeadCodeElimination pass implementation
71 //
72 struct DCE : public FunctionPass {
73 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000074 DCE() : FunctionPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075
76 virtual bool runOnFunction(Function &F);
77
78 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
79 AU.setPreservesCFG();
80 }
81 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082}
83
Dan Gohman089efff2008-05-13 00:00:25 +000084char DCE::ID = 0;
85static RegisterPass<DCE> Y("dce", "Dead Code Elimination");
86
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087bool DCE::runOnFunction(Function &F) {
88 // Start out with all of the instructions in the worklist...
89 std::vector<Instruction*> WorkList;
90 for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i)
91 WorkList.push_back(&*i);
92
93 // Loop over the worklist finding instructions that are dead. If they are
94 // dead make them drop all of their uses, making other instructions
95 // potentially dead, and work until the worklist is empty.
96 //
97 bool MadeChange = false;
98 while (!WorkList.empty()) {
99 Instruction *I = WorkList.back();
100 WorkList.pop_back();
101
102 if (isInstructionTriviallyDead(I)) { // If the instruction is dead.
103 // Loop over all of the values that the instruction uses, if there are
104 // instructions being used, add them to the worklist, because they might
105 // go dead after this one is removed.
106 //
107 for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI)
108 if (Instruction *Used = dyn_cast<Instruction>(*OI))
109 WorkList.push_back(Used);
110
111 // Remove the instruction.
112 I->eraseFromParent();
113
114 // Remove the instruction from the worklist if it still exists in it.
David Greene41ebd512007-12-17 17:39:51 +0000115 for (std::vector<Instruction*>::iterator WI = WorkList.begin();
Bill Wendlingbd9e0522008-09-18 23:04:18 +0000116 WI != WorkList.end(); ) {
117 if (*WI == I)
David Greene41ebd512007-12-17 17:39:51 +0000118 WI = WorkList.erase(WI);
Bill Wendlingbd9e0522008-09-18 23:04:18 +0000119 else
120 ++WI;
121 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122
123 MadeChange = true;
124 ++DCEEliminated;
125 }
126 }
127 return MadeChange;
128}
129
130FunctionPass *llvm::createDeadCodeEliminationPass() {
131 return new DCE();
132}
133