1193323Sed//===- DCE.cpp - Code to perform dead code elimination --------------------===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This file implements the Aggressive Dead Code Elimination pass.  This pass
11193323Sed// optimistically assumes that all instructions are dead until proven otherwise,
12239462Sdim// allowing it to eliminate dead computations that other DCE passes do not
13193323Sed// catch, particularly involving loop computations.
14193323Sed//
15193323Sed//===----------------------------------------------------------------------===//
16193323Sed
17193323Sed#define DEBUG_TYPE "adce"
18193323Sed#include "llvm/Transforms/Scalar.h"
19193323Sed#include "llvm/ADT/DepthFirstIterator.h"
20193323Sed#include "llvm/ADT/SmallPtrSet.h"
21193323Sed#include "llvm/ADT/SmallVector.h"
22193323Sed#include "llvm/ADT/Statistic.h"
23249423Sdim#include "llvm/IR/BasicBlock.h"
24249423Sdim#include "llvm/IR/Instructions.h"
25249423Sdim#include "llvm/IR/IntrinsicInst.h"
26249423Sdim#include "llvm/Pass.h"
27249423Sdim#include "llvm/Support/CFG.h"
28249423Sdim#include "llvm/Support/InstIterator.h"
29193323Sedusing namespace llvm;
30193323Sed
31193323SedSTATISTIC(NumRemoved, "Number of instructions removed");
32193323Sed
33193323Sednamespace {
34198090Srdivacky  struct ADCE : public FunctionPass {
35193323Sed    static char ID; // Pass identification, replacement for typeid
36218893Sdim    ADCE() : FunctionPass(ID) {
37218893Sdim      initializeADCEPass(*PassRegistry::getPassRegistry());
38218893Sdim    }
39239462Sdim
40193323Sed    virtual bool runOnFunction(Function& F);
41239462Sdim
42193323Sed    virtual void getAnalysisUsage(AnalysisUsage& AU) const {
43193323Sed      AU.setPreservesCFG();
44193323Sed    }
45239462Sdim
46193323Sed  };
47193323Sed}
48193323Sed
49193323Sedchar ADCE::ID = 0;
50218893SdimINITIALIZE_PASS(ADCE, "adce", "Aggressive Dead Code Elimination", false, false)
51193323Sed
52193323Sedbool ADCE::runOnFunction(Function& F) {
53193323Sed  SmallPtrSet<Instruction*, 128> alive;
54193323Sed  SmallVector<Instruction*, 128> worklist;
55239462Sdim
56193323Sed  // Collect the set of "root" instructions that are known live.
57193323Sed  for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
58193323Sed    if (isa<TerminatorInst>(I.getInstructionIterator()) ||
59193323Sed        isa<DbgInfoIntrinsic>(I.getInstructionIterator()) ||
60226633Sdim        isa<LandingPadInst>(I.getInstructionIterator()) ||
61193323Sed        I->mayHaveSideEffects()) {
62193323Sed      alive.insert(I.getInstructionIterator());
63193323Sed      worklist.push_back(I.getInstructionIterator());
64193323Sed    }
65239462Sdim
66193323Sed  // Propagate liveness backwards to operands.
67193323Sed  while (!worklist.empty()) {
68202375Srdivacky    Instruction* curr = worklist.pop_back_val();
69193323Sed    for (Instruction::op_iterator OI = curr->op_begin(), OE = curr->op_end();
70193323Sed         OI != OE; ++OI)
71193323Sed      if (Instruction* Inst = dyn_cast<Instruction>(OI))
72193323Sed        if (alive.insert(Inst))
73193323Sed          worklist.push_back(Inst);
74193323Sed  }
75239462Sdim
76193323Sed  // The inverse of the live set is the dead set.  These are those instructions
77193323Sed  // which have no side effects and do not influence the control flow or return
78193323Sed  // value of the function, and may therefore be deleted safely.
79193323Sed  // NOTE: We reuse the worklist vector here for memory efficiency.
80193323Sed  for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
81193323Sed    if (!alive.count(I.getInstructionIterator())) {
82193323Sed      worklist.push_back(I.getInstructionIterator());
83193323Sed      I->dropAllReferences();
84193323Sed    }
85239462Sdim
86263508Sdim  for (SmallVectorImpl<Instruction *>::iterator I = worklist.begin(),
87193323Sed       E = worklist.end(); I != E; ++I) {
88210299Sed    ++NumRemoved;
89193323Sed    (*I)->eraseFromParent();
90193323Sed  }
91193323Sed
92193323Sed  return !worklist.empty();
93193323Sed}
94193323Sed
95193323SedFunctionPass *llvm::createAggressiveDCEPass() {
96193323Sed  return new ADCE();
97193323Sed}
98