Apply the VISIBILITY_HIDDEN field to the remaining anonymous classes in
[oota-llvm.git] / lib / Transforms / Scalar / DCE.cpp
1 //===- 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>
28 using namespace llvm;
29
30 STATISTIC(DIEEliminated, "Number of insts removed by DIE pass");
31 STATISTIC(DCEEliminated, "Number of insts removed");
32
33 namespace {
34   //===--------------------------------------------------------------------===//
35   // DeadInstElimination pass implementation
36   //
37   struct VISIBILITY_HIDDEN DeadInstElimination : public BasicBlockPass {
38     virtual bool runOnBasicBlock(BasicBlock &BB) {
39       bool Changed = false;
40       for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); )
41         if (dceInstruction(DI)) {
42           Changed = true;
43           ++DIEEliminated;
44         } else
45           ++DI;
46       return Changed;
47     }
48
49     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
50       AU.setPreservesCFG();
51     }
52   };
53
54   RegisterPass<DeadInstElimination> X("die", "Dead Instruction Elimination");
55 }
56
57 Pass *llvm::createDeadInstEliminationPass() {
58   return new DeadInstElimination();
59 }
60
61
62 namespace {
63   //===--------------------------------------------------------------------===//
64   // DeadCodeElimination pass implementation
65   //
66   struct DCE : public FunctionPass {
67     virtual bool runOnFunction(Function &F);
68
69      virtual void getAnalysisUsage(AnalysisUsage &AU) const {
70       AU.setPreservesCFG();
71     }
72  };
73
74   RegisterPass<DCE> Y("dce", "Dead Code Elimination");
75 }
76
77 bool DCE::runOnFunction(Function &F) {
78   // Start out with all of the instructions in the worklist...
79   std::vector<Instruction*> WorkList;
80   for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i)
81     WorkList.push_back(&*i);
82
83   // Loop over the worklist finding instructions that are dead.  If they are
84   // dead make them drop all of their uses, making other instructions
85   // potentially dead, and work until the worklist is empty.
86   //
87   bool MadeChange = false;
88   while (!WorkList.empty()) {
89     Instruction *I = WorkList.back();
90     WorkList.pop_back();
91
92     if (isInstructionTriviallyDead(I)) {       // If the instruction is dead.
93       // Loop over all of the values that the instruction uses, if there are
94       // instructions being used, add them to the worklist, because they might
95       // go dead after this one is removed.
96       //
97       for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI)
98         if (Instruction *Used = dyn_cast<Instruction>(*OI))
99           WorkList.push_back(Used);
100
101       // Remove the instruction.
102       I->eraseFromParent();
103
104       // Remove the instruction from the worklist if it still exists in it.
105       for (std::vector<Instruction*>::iterator WI = WorkList.begin(),
106              E = WorkList.end(); WI != E; ++WI)
107         if (*WI == I) {
108           WorkList.erase(WI);
109           --E;
110           --WI;
111         }
112
113       MadeChange = true;
114       ++DCEEliminated;
115     }
116   }
117   return MadeChange;
118 }
119
120 FunctionPass *llvm::createDeadCodeEliminationPass() {
121   return new DCE();
122 }
123