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 the Aggressive Dead Code Elimination pass. This pass 00011 // optimistically assumes that all instructions are dead until proven otherwise, 00012 // allowing it to eliminate dead computations that other DCE passes do not 00013 // catch, particularly involving loop computations. 00014 // 00015 //===----------------------------------------------------------------------===// 00016 00017 #define DEBUG_TYPE "adce" 00018 #include "llvm/Transforms/Scalar.h" 00019 #include "llvm/BasicBlock.h" 00020 #include "llvm/Instructions.h" 00021 #include "llvm/Pass.h" 00022 #include "llvm/Support/CFG.h" 00023 #include "llvm/Support/Compiler.h" 00024 #include "llvm/Support/InstIterator.h" 00025 #include "llvm/ADT/DepthFirstIterator.h" 00026 #include "llvm/ADT/SmallPtrSet.h" 00027 #include "llvm/ADT/SmallVector.h" 00028 #include "llvm/ADT/Statistic.h" 00029 00030 00031 using namespace llvm; 00032 00033 STATISTIC(NumRemoved, "Number of instructions removed"); 00034 00035 namespace { 00036 struct VISIBILITY_HIDDEN ADCE : public FunctionPass { 00037 static char ID; // Pass identification, replacement for typeid 00038 ADCE() : FunctionPass(&ID) {} 00039 00040 virtual bool runOnFunction(Function& F); 00041 00042 virtual void getAnalysisUsage(AnalysisUsage& AU) const { 00043 AU.setPreservesCFG(); 00044 } 00045 00046 }; 00047 } 00048 00049 char ADCE::ID = 0; 00050 static RegisterPass<ADCE> X("adce", "Aggressive Dead Code Elimination"); 00051 00052 bool ADCE::runOnFunction(Function& F) { 00053 SmallPtrSet<Instruction*, 128> alive; 00054 SmallVector<Instruction*, 128> worklist; 00055 00056 // Collect the set of "root" instructions that are known live. 00057 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) 00058 if (isa<TerminatorInst>(I.getInstructionIterator()) || 00059 I->mayWriteToMemory()) { 00060 alive.insert(I.getInstructionIterator()); 00061 worklist.push_back(I.getInstructionIterator()); 00062 } 00063 00064 // Propagate liveness backwards to operands. 00065 while (!worklist.empty()) { 00066 Instruction* curr = worklist.back(); 00067 worklist.pop_back(); 00068 00069 for (Instruction::op_iterator OI = curr->op_begin(), OE = curr->op_end(); 00070 OI != OE; ++OI) 00071 if (Instruction* Inst = dyn_cast<Instruction>(OI)) 00072 if (alive.insert(Inst)) 00073 worklist.push_back(Inst); 00074 } 00075 00076 // The inverse of the live set is the dead set. These are those instructions 00077 // which have no side effects and do not influence the control flow or return 00078 // value of the function, and may therefore be deleted safely. 00079 // NOTE: We reuse the worklist vector here for memory efficiency. 00080 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) 00081 if (!alive.count(I.getInstructionIterator())) { 00082 worklist.push_back(I.getInstructionIterator()); 00083 I->dropAllReferences(); 00084 } 00085 00086 for (SmallVector<Instruction*, 1024>::iterator I = worklist.begin(), 00087 E = worklist.end(); I != E; ++I) { 00088 NumRemoved++; 00089 (*I)->eraseFromParent(); 00090 } 00091 00092 return !worklist.empty(); 00093 } 00094 00095 FunctionPass *llvm::createAggressiveDCEPass() { 00096 return new ADCE(); 00097 }