LLVM  3.7.0
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 #include "llvm/Transforms/Scalar.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) {
39  }
40 
41  bool runOnFunction(Function& F) override;
42 
43  void getAnalysisUsage(AnalysisUsage& AU) const override {
44  AU.setPreservesCFG();
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) {
53  if (skipOptnoneFunction(F))
54  return false;
55 
58 
59  // Collect the set of "root" instructions that are known live.
60  for (Instruction &I : inst_range(F)) {
61  if (isa<TerminatorInst>(I) || isa<DbgInfoIntrinsic>(I) ||
62  isa<LandingPadInst>(I) || I.mayHaveSideEffects()) {
63  Alive.insert(&I);
64  Worklist.push_back(&I);
65  }
66  }
67 
68  // Propagate liveness backwards to operands.
69  while (!Worklist.empty()) {
70  Instruction *Curr = Worklist.pop_back_val();
71  for (Use &OI : Curr->operands()) {
72  if (Instruction *Inst = dyn_cast<Instruction>(OI))
73  if (Alive.insert(Inst).second)
74  Worklist.push_back(Inst);
75  }
76  }
77 
78  // The inverse of the live set is the dead set. These are those instructions
79  // which have no side effects and do not influence the control flow or return
80  // value of the function, and may therefore be deleted safely.
81  // NOTE: We reuse the Worklist vector here for memory efficiency.
82  for (Instruction &I : inst_range(F)) {
83  if (!Alive.count(&I)) {
84  Worklist.push_back(&I);
85  I.dropAllReferences();
86  }
87  }
88 
89  for (Instruction *&I : Worklist) {
90  ++NumRemoved;
91  I->eraseFromParent();
92  }
93 
94  return !Worklist.empty();
95 }
96 
98  return new ADCE();
99 }
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
STATISTIC(NumFunctions,"Total number of functions")
size_type count(PtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:276
F(f)
T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val()
Definition: SmallVector.h:406
A Use represents the edge between a Value definition and its users.
Definition: Use.h:69
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallVector.h:57
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:264
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:294
op_range operands()
Definition: User.h:191
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:56
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:299
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:263
FunctionPass * createAggressiveDCEPass()
Definition: ADCE.cpp:97
iterator_range< inst_iterator > inst_range(Function *F)
Definition: InstIterator.h:129
#define I(x, y, z)
Definition: MD5.cpp:54
void initializeADCEPass(PassRegistry &)