1 //===- DCE.cpp - Code to perform dead code elimination --------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements dead inst elimination and dead code elimination.
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.
17 //===----------------------------------------------------------------------===//
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"
30 STATISTIC(DIEEliminated, "Number of insts removed by DIE pass");
31 STATISTIC(DCEEliminated, "Number of insts removed");
34 //===--------------------------------------------------------------------===//
35 // DeadInstElimination pass implementation
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) {
42 for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); )
43 if (dceInstruction(DI)) {
51 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
56 char DeadInstElimination::ID = 0;
57 RegisterPass<DeadInstElimination> X("die", "Dead Instruction Elimination");
60 Pass *llvm::createDeadInstEliminationPass() {
61 return new DeadInstElimination();
66 //===--------------------------------------------------------------------===//
67 // DeadCodeElimination pass implementation
69 struct DCE : public FunctionPass {
70 static char ID; // Pass identification, replacement for typeid
71 DCE() : FunctionPass((intptr_t)&ID) {}
73 virtual bool runOnFunction(Function &F);
75 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
81 RegisterPass<DCE> Y("dce", "Dead Code Elimination");
84 bool 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);
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.
94 bool MadeChange = false;
95 while (!WorkList.empty()) {
96 Instruction *I = WorkList.back();
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.
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);
108 // Remove the instruction.
109 I->eraseFromParent();
111 // Remove the instruction from the worklist if it still exists in it.
112 for (std::vector<Instruction*>::iterator WI = WorkList.begin(),
113 E = WorkList.end(); WI != E; ++WI)
127 FunctionPass *llvm::createDeadCodeEliminationPass() {