LLVM API Documentation
00001 //===- DCE.cpp - Code to perform dead code elimination --------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements dead inst elimination and dead code elimination. 00011 // 00012 // Dead Inst Elimination performs a single pass over the function removing 00013 // instructions that are obviously dead. Dead Code Elimination is similar, but 00014 // it rechecks instructions that were used by removed instructions to see if 00015 // they are newly dead. 00016 // 00017 //===----------------------------------------------------------------------===// 00018 00019 #define DEBUG_TYPE "dce" 00020 #include "llvm/Transforms/Scalar.h" 00021 #include "llvm/Transforms/Utils/Local.h" 00022 #include "llvm/Instruction.h" 00023 #include "llvm/Pass.h" 00024 #include "llvm/Support/Compiler.h" 00025 #include "llvm/Support/InstIterator.h" 00026 #include "llvm/ADT/Statistic.h" 00027 #include <set> 00028 using namespace llvm; 00029 00030 STATISTIC(DIEEliminated, "Number of insts removed by DIE pass"); 00031 STATISTIC(DCEEliminated, "Number of insts removed"); 00032 00033 namespace { 00034 //===--------------------------------------------------------------------===// 00035 // DeadInstElimination pass implementation 00036 // 00037 struct VISIBILITY_HIDDEN DeadInstElimination : public BasicBlockPass { 00038 static char ID; // Pass identification, replacement for typeid 00039 DeadInstElimination() : BasicBlockPass(intptr_t(&ID)) {} 00040 virtual bool runOnBasicBlock(BasicBlock &BB) { 00041 bool Changed = false; 00042 for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); ) { 00043 Instruction *Inst = DI++; 00044 if (isInstructionTriviallyDead(Inst)) { 00045 Inst->eraseFromParent(); 00046 Changed = true; 00047 ++DIEEliminated; 00048 } 00049 } 00050 return Changed; 00051 } 00052 00053 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00054 AU.setPreservesCFG(); 00055 } 00056 }; 00057 } 00058 00059 char DeadInstElimination::ID = 0; 00060 static RegisterPass<DeadInstElimination> 00061 X("die", "Dead Instruction Elimination"); 00062 00063 Pass *llvm::createDeadInstEliminationPass() { 00064 return new DeadInstElimination(); 00065 } 00066 00067 00068 namespace { 00069 //===--------------------------------------------------------------------===// 00070 // DeadCodeElimination pass implementation 00071 // 00072 struct DCE : public FunctionPass { 00073 static char ID; // Pass identification, replacement for typeid 00074 DCE() : FunctionPass(&ID) {} 00075 00076 virtual bool runOnFunction(Function &F); 00077 00078 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00079 AU.setPreservesCFG(); 00080 } 00081 }; 00082 } 00083 00084 char DCE::ID = 0; 00085 static RegisterPass<DCE> Y("dce", "Dead Code Elimination"); 00086 00087 bool DCE::runOnFunction(Function &F) { 00088 // Start out with all of the instructions in the worklist... 00089 std::vector<Instruction*> WorkList; 00090 for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) 00091 WorkList.push_back(&*i); 00092 00093 // Loop over the worklist finding instructions that are dead. If they are 00094 // dead make them drop all of their uses, making other instructions 00095 // potentially dead, and work until the worklist is empty. 00096 // 00097 bool MadeChange = false; 00098 while (!WorkList.empty()) { 00099 Instruction *I = WorkList.back(); 00100 WorkList.pop_back(); 00101 00102 if (isInstructionTriviallyDead(I)) { // If the instruction is dead. 00103 // Loop over all of the values that the instruction uses, if there are 00104 // instructions being used, add them to the worklist, because they might 00105 // go dead after this one is removed. 00106 // 00107 for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI) 00108 if (Instruction *Used = dyn_cast<Instruction>(*OI)) 00109 WorkList.push_back(Used); 00110 00111 // Remove the instruction. 00112 I->eraseFromParent(); 00113 00114 // Remove the instruction from the worklist if it still exists in it. 00115 for (std::vector<Instruction*>::iterator WI = WorkList.begin(); 00116 WI != WorkList.end(); ) { 00117 if (*WI == I) 00118 WI = WorkList.erase(WI); 00119 else 00120 ++WI; 00121 } 00122 00123 MadeChange = true; 00124 ++DCEEliminated; 00125 } 00126 } 00127 return MadeChange; 00128 } 00129 00130 FunctionPass *llvm::createDeadCodeEliminationPass() { 00131 return new DCE(); 00132 } 00133