960ce8f5e9135061545d59f5a8f271046fc3a816
[oota-llvm.git] / lib / Transforms / Scalar / ADCE.cpp
1 //===- DCE.cpp - Code to perform dead code elimination --------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #define DEBUG_TYPE "adce"
18 #include "llvm/Transforms/Scalar.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Pass.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/Support/InstIterator.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/ADT/SmallPtrSet.h"
25 #include "llvm/ADT/SmallVector.h"
26
27 using namespace llvm;
28
29 STATISTIC(NumRemoved, "Number of instructions removed");
30
31 namespace {
32   struct VISIBILITY_HIDDEN ADCE : public FunctionPass {
33     static char ID; // Pass identification, replacement for typeid
34     ADCE() : FunctionPass((intptr_t)&ID) {}
35     
36     SmallPtrSet<Instruction*, 1024> alive;
37     SmallVector<Instruction*, 1024> worklist;
38     
39     virtual bool runOnFunction(Function& F);
40     
41     virtual void getAnalysisUsage(AnalysisUsage& AU) const {
42       AU.setPreservesCFG();
43     }
44     
45   };
46 }
47
48 char ADCE::ID = 0;
49 static RegisterPass<ADCE> X("adce", "Aggressive Dead Code Elimination");
50
51 bool ADCE::runOnFunction(Function& F) {
52   alive.clear();
53   worklist.clear();
54   
55   // Collect the set of "root" instructions that are known live.
56   for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
57     if (isa<TerminatorInst>(I.getInstructionIterator()) ||
58         I->mayWriteToMemory()) {
59       alive.insert(I.getInstructionIterator());
60       worklist.push_back(I.getInstructionIterator());
61     }
62   
63   // Propagate liveness backwards to operands.
64   while (!worklist.empty()) {
65     Instruction* curr = worklist.back();
66     worklist.pop_back();
67     
68     for (Instruction::op_iterator OI = curr->op_begin(), OE = curr->op_end();
69          OI != OE; ++OI)
70       if (Instruction* Inst = dyn_cast<Instruction>(OI))
71         if (alive.insert(Inst))
72           worklist.push_back(Inst);
73   }
74   
75   // The inverse of the live set is the dead set.  These are those instructions
76   // which have no side effects and do not influence the control flow or return
77   // value of the function, and may therefore be deleted safely.
78   // NOTE: We reuse the worklist vector here for memory efficiency.
79   for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
80     if (!alive.count(I.getInstructionIterator())) {
81       worklist.push_back(I.getInstructionIterator());
82       I->dropAllReferences();
83     }
84   
85   for (SmallVector<Instruction*, 1024>::iterator I = worklist.begin(),
86        E = worklist.end(); I != E; ++I) {
87     NumRemoved++;
88     (*I)->eraseFromParent();
89   }
90     
91   return !worklist.empty();
92 }
93
94 FunctionPass *llvm::createAggressiveDCEPass() {
95   return new ADCE();
96 }