fix more AST updating bugs, correcting miscompilation in PR8041
[llvm.git] / lib / Transforms / Scalar / ADCE.cpp
blobada086e9db766404980eca3732d9fb027cb95984
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.
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/IntrinsicInst.h"
22 #include "llvm/Pass.h"
23 #include "llvm/Support/CFG.h"
24 #include "llvm/Support/InstIterator.h"
25 #include "llvm/ADT/DepthFirstIterator.h"
26 #include "llvm/ADT/SmallPtrSet.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/ADT/Statistic.h"
29 using namespace llvm;
31 STATISTIC(NumRemoved, "Number of instructions removed");
33 namespace {
34 struct ADCE : public FunctionPass {
35 static char ID; // Pass identification, replacement for typeid
36 ADCE() : FunctionPass(ID) {}
38 virtual bool runOnFunction(Function& F);
40 virtual void getAnalysisUsage(AnalysisUsage& AU) const {
41 AU.setPreservesCFG();
47 char ADCE::ID = 0;
48 INITIALIZE_PASS(ADCE, "adce", "Aggressive Dead Code Elimination", false, false);
50 bool ADCE::runOnFunction(Function& F) {
51 SmallPtrSet<Instruction*, 128> alive;
52 SmallVector<Instruction*, 128> worklist;
54 // Collect the set of "root" instructions that are known live.
55 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
56 if (isa<TerminatorInst>(I.getInstructionIterator()) ||
57 isa<DbgInfoIntrinsic>(I.getInstructionIterator()) ||
58 I->mayHaveSideEffects()) {
59 alive.insert(I.getInstructionIterator());
60 worklist.push_back(I.getInstructionIterator());
63 // Propagate liveness backwards to operands.
64 while (!worklist.empty()) {
65 Instruction* curr = worklist.pop_back_val();
67 for (Instruction::op_iterator OI = curr->op_begin(), OE = curr->op_end();
68 OI != OE; ++OI)
69 if (Instruction* Inst = dyn_cast<Instruction>(OI))
70 if (alive.insert(Inst))
71 worklist.push_back(Inst);
74 // The inverse of the live set is the dead set. These are those instructions
75 // which have no side effects and do not influence the control flow or return
76 // value of the function, and may therefore be deleted safely.
77 // NOTE: We reuse the worklist vector here for memory efficiency.
78 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
79 if (!alive.count(I.getInstructionIterator())) {
80 worklist.push_back(I.getInstructionIterator());
81 I->dropAllReferences();
84 for (SmallVector<Instruction*, 1024>::iterator I = worklist.begin(),
85 E = worklist.end(); I != E; ++I) {
86 ++NumRemoved;
87 (*I)->eraseFromParent();
90 return !worklist.empty();
93 FunctionPass *llvm::createAggressiveDCEPass() {
94 return new ADCE();