1 //===- DCE.cpp - Code to perform dead code elimination --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Aggressive Dead Code Elimination pass. This pass
11 // optimistically assumes that all instructions are dead until proven otherwise,
12 // allowing it to eliminate dead computations that other DCE passes do not
13 // catch, particularly involving loop computations.
15 //===----------------------------------------------------------------------===//
17 #define DEBUG_TYPE "adce"
18 #include "llvm/Transforms/Scalar.h"
19 #include "llvm/BasicBlock.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/Pass.h"
22 #include "llvm/Support/CFG.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Support/InstIterator.h"
25 #include "llvm/ADT/DenseSet.h"
26 #include "llvm/ADT/DepthFirstIterator.h"
27 #include "llvm/ADT/Statistic.h"
28 #include "llvm/ADT/SmallVector.h"
32 STATISTIC(NumRemoved, "Number of instructions removed");
35 struct VISIBILITY_HIDDEN ADCE : public FunctionPass {
36 static char ID; // Pass identification, replacement for typeid
37 ADCE() : FunctionPass((intptr_t)&ID) {}
39 DenseSet<Instruction*> alive;
40 SmallVector<Instruction*, 1024> worklist;
42 DenseSet<BasicBlock*> reachable;
43 SmallVector<BasicBlock*, 1024> unreachable;
45 virtual bool runOnFunction(Function& F);
47 virtual void getAnalysisUsage(AnalysisUsage& AU) const {
55 static RegisterPass<ADCE> X("adce", "Aggressive Dead Code Elimination");
57 bool ADCE::runOnFunction(Function& F) {
63 // First, collect the set of reachable blocks ...
64 for (df_iterator<BasicBlock*> DI = df_begin(&F.getEntryBlock()),
65 DE = df_end(&F.getEntryBlock()); DI != DE; ++DI)
66 reachable.insert(*DI);
68 // ... and then invert it into the list of unreachable ones. These
69 // blocks will be removed from the function.
70 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
71 if (!reachable.count(FI))
72 unreachable.push_back(FI);
74 // Prepare to remove blocks by removing the PHI node entries for those blocks
75 // in their successors, and remove them from reference counting.
76 for (SmallVector<BasicBlock*, 1024>::iterator UI = unreachable.begin(),
77 UE = unreachable.end(); UI != UE; ++UI) {
79 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
81 BasicBlock* succ = *SI;
82 BasicBlock::iterator succ_inst = succ->begin();
83 while (PHINode* P = dyn_cast<PHINode>(succ_inst)) {
84 P->removeIncomingValue(BB);
89 BB->dropAllReferences();
92 // Finally, erase the unreachable blocks.
93 for (SmallVector<BasicBlock*, 1024>::iterator UI = unreachable.begin(),
94 UE = unreachable.end(); UI != UE; ++UI)
95 (*UI)->eraseFromParent();
97 // Collect the set of "root" instructions that are known live.
98 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
99 if (isa<TerminatorInst>(I.getInstructionIterator()) ||
100 I->mayWriteToMemory()) {
101 alive.insert(I.getInstructionIterator());
102 worklist.push_back(I.getInstructionIterator());
105 // Propagate liveness backwards to operands.
106 while (!worklist.empty()) {
107 Instruction* curr = worklist.back();
110 for (Instruction::op_iterator OI = curr->op_begin(), OE = curr->op_end();
112 if (Instruction* Inst = dyn_cast<Instruction>(OI))
113 if (alive.insert(Inst))
114 worklist.push_back(Inst);
117 // The inverse of the live set is the dead set. These are those instructions
118 // which have no side effects and do not influence the control flow or return
119 // value of the function, and may therefore be deleted safely.
120 // NOTE: We reuse the worklist vector here for memory efficiency.
121 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
122 if (!alive.count(I.getInstructionIterator())) {
123 worklist.push_back(I.getInstructionIterator());
124 I->dropAllReferences();
127 for (SmallVector<Instruction*, 1024>::iterator I = worklist.begin(),
128 E = worklist.end(); I != E; ++I) {
130 (*I)->eraseFromParent();
133 return !worklist.empty();
136 FunctionPass *llvm::createAggressiveDCEPass() {