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/ADT/DepthFirstIterator.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/IR/BasicBlock.h"
24 #include "llvm/IR/CFG.h"
25 #include "llvm/IR/InstIterator.h"
26 #include "llvm/IR/Instructions.h"
27 #include "llvm/IR/IntrinsicInst.h"
28 #include "llvm/Pass.h"
31 STATISTIC(NumRemoved, "Number of instructions removed");
34 struct ADCE : public FunctionPass {
35 static char ID; // Pass identification, replacement for typeid
36 ADCE() : FunctionPass(ID) {
37 initializeADCEPass(*PassRegistry::getPassRegistry());
40 bool runOnFunction(Function& F) override;
42 void getAnalysisUsage(AnalysisUsage& AU) const override {
50 INITIALIZE_PASS(ADCE, "adce", "Aggressive Dead Code Elimination", false, false)
52 bool ADCE::runOnFunction(Function& F) {
53 if (skipOptnoneFunction(F))
56 SmallPtrSet<Instruction*, 128> alive;
57 SmallVector<Instruction*, 128> worklist;
59 // Collect the set of "root" instructions that are known live.
60 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
61 if (isa<TerminatorInst>(I.getInstructionIterator()) ||
62 isa<DbgInfoIntrinsic>(I.getInstructionIterator()) ||
63 isa<LandingPadInst>(I.getInstructionIterator()) ||
64 I->mayHaveSideEffects()) {
65 alive.insert(I.getInstructionIterator());
66 worklist.push_back(I.getInstructionIterator());
69 // Propagate liveness backwards to operands.
70 while (!worklist.empty()) {
71 Instruction* curr = worklist.pop_back_val();
72 for (Instruction::op_iterator OI = curr->op_begin(), OE = curr->op_end();
74 if (Instruction* Inst = dyn_cast<Instruction>(OI))
75 if (alive.insert(Inst))
76 worklist.push_back(Inst);
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 (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
84 if (!alive.count(I.getInstructionIterator())) {
85 worklist.push_back(I.getInstructionIterator());
86 I->dropAllReferences();
89 for (SmallVectorImpl<Instruction *>::iterator I = worklist.begin(),
90 E = worklist.end(); I != E; ++I) {
92 (*I)->eraseFromParent();
95 return !worklist.empty();
98 FunctionPass *llvm::createAggressiveDCEPass() {