LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ADCE.cpp
Go to the documentation of this file.
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"
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/Instructions.h"
25 #include "llvm/IR/IntrinsicInst.h"
26 #include "llvm/Pass.h"
27 #include "llvm/Support/CFG.h"
29 using namespace llvm;
30 
31 STATISTIC(NumRemoved, "Number of instructions removed");
32 
33 namespace {
34  struct ADCE : public FunctionPass {
35  static char ID; // Pass identification, replacement for typeid
36  ADCE() : FunctionPass(ID) {
38  }
39 
40  virtual bool runOnFunction(Function& F);
41 
42  virtual void getAnalysisUsage(AnalysisUsage& AU) const {
43  AU.setPreservesCFG();
44  }
45 
46  };
47 }
48 
49 char ADCE::ID = 0;
50 INITIALIZE_PASS(ADCE, "adce", "Aggressive Dead Code Elimination", false, false)
51 
52 bool ADCE::runOnFunction(Function& F) {
55 
56  // Collect the set of "root" instructions that are known live.
57  for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
58  if (isa<TerminatorInst>(I.getInstructionIterator()) ||
59  isa<DbgInfoIntrinsic>(I.getInstructionIterator()) ||
60  isa<LandingPadInst>(I.getInstructionIterator()) ||
61  I->mayHaveSideEffects()) {
62  alive.insert(I.getInstructionIterator());
63  worklist.push_back(I.getInstructionIterator());
64  }
65 
66  // Propagate liveness backwards to operands.
67  while (!worklist.empty()) {
68  Instruction* curr = worklist.pop_back_val();
69  for (Instruction::op_iterator OI = curr->op_begin(), OE = curr->op_end();
70  OI != OE; ++OI)
71  if (Instruction* Inst = dyn_cast<Instruction>(OI))
72  if (alive.insert(Inst))
73  worklist.push_back(Inst);
74  }
75 
76  // The inverse of the live set is the dead set. These are those instructions
77  // which have no side effects and do not influence the control flow or return
78  // value of the function, and may therefore be deleted safely.
79  // NOTE: We reuse the worklist vector here for memory efficiency.
80  for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
81  if (!alive.count(I.getInstructionIterator())) {
82  worklist.push_back(I.getInstructionIterator());
83  I->dropAllReferences();
84  }
85 
87  E = worklist.end(); I != E; ++I) {
88  ++NumRemoved;
89  (*I)->eraseFromParent();
90  }
91 
92  return !worklist.empty();
93 }
94 
96  return new ADCE();
97 }
static PassRegistry * getPassRegistry()
bool insert(PtrType Ptr)
Definition: SmallPtrSet.h:253
F(f)
op_iterator op_begin()
Definition: User.h:116
inst_iterator inst_begin(Function *F)
Definition: InstIterator.h:128
T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val()
Definition: SmallVector.h:430
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
bool count(PtrType Ptr) const
count - Return true if the specified pointer is in the set.
Definition: SmallPtrSet.h:264
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallVector.h:56
op_iterator op_end()
Definition: User.h:118
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:153
void setPreservesCFG()
Definition: Pass.cpp:249
FunctionPass * createAggressiveDCEPass()
Definition: ADCE.cpp:95
#define I(x, y, z)
Definition: MD5.cpp:54
Use * op_iterator
Definition: User.h:113
void initializeADCEPass(PassRegistry &)
inst_iterator inst_end(Function *F)
Definition: InstIterator.h:129
STATISTIC(NumRemoved,"Number of instructions removed")