[ADCE] Convert another loop for a range-based for
[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 #include "llvm/Transforms/Scalar.h"
18 #include "llvm/ADT/DepthFirstIterator.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/IR/BasicBlock.h"
23 #include "llvm/IR/CFG.h"
24 #include "llvm/IR/InstIterator.h"
25 #include "llvm/IR/Instructions.h"
26 #include "llvm/IR/IntrinsicInst.h"
27 #include "llvm/Pass.h"
28 using namespace llvm;
29
30 #define DEBUG_TYPE "adce"
31
32 STATISTIC(NumRemoved, "Number of instructions removed");
33
34 namespace {
35   struct ADCE : public FunctionPass {
36     static char ID; // Pass identification, replacement for typeid
37     ADCE() : FunctionPass(ID) {
38       initializeADCEPass(*PassRegistry::getPassRegistry());
39     }
40
41     bool runOnFunction(Function& F) override;
42
43     void getAnalysisUsage(AnalysisUsage& AU) const override {
44       AU.setPreservesCFG();
45     }
46
47   };
48 }
49
50 char ADCE::ID = 0;
51 INITIALIZE_PASS(ADCE, "adce", "Aggressive Dead Code Elimination", false, false)
52
53 bool ADCE::runOnFunction(Function& F) {
54   if (skipOptnoneFunction(F))
55     return false;
56
57   SmallPtrSet<Instruction*, 128> Alive;
58   SmallVector<Instruction*, 128> Worklist;
59
60   // Collect the set of "root" instructions that are known live.
61   for (Instruction &I : inst_range(F)) {
62     if (isa<TerminatorInst>(I) || isa<DbgInfoIntrinsic>(I) ||
63         isa<LandingPadInst>(I) || I.mayHaveSideEffects()) {
64       Alive.insert(&I);
65       Worklist.push_back(&I);
66     }
67   }
68
69   // Propagate liveness backwards to operands.
70   while (!Worklist.empty()) {
71     Instruction *Curr = Worklist.pop_back_val();
72     for (Use &OI : Curr->operands()) {
73       if (Instruction *Inst = dyn_cast<Instruction>(OI))
74         if (Alive.insert(Inst).second)
75           Worklist.push_back(Inst);
76     }
77   }
78
79   // The inverse of the live set is the dead set.  These are those instructions
80   // which have no side effects and do not influence the control flow or return
81   // value of the function, and may therefore be deleted safely.
82   // NOTE: We reuse the Worklist vector here for memory efficiency.
83   for (Instruction &I : inst_range(F)) {
84     if (!Alive.count(&I)) {
85       Worklist.push_back(&I);
86       I.dropAllReferences();
87     }
88   }
89
90   for (Instruction *&I : Worklist) {
91     ++NumRemoved;
92     I->eraseFromParent();
93   }
94
95   return !Worklist.empty();
96 }
97
98 FunctionPass *llvm::createAggressiveDCEPass() {
99   return new ADCE();
100 }