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/BasicBlock.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/IntrinsicInst.h"
22 #include "llvm/Pass.h"
23 #include "llvm/Support/CFG.h"
24 #include "llvm/Support/Compiler.h"
25 #include "llvm/Support/InstIterator.h"
26 #include "llvm/ADT/DepthFirstIterator.h"
27 #include "llvm/ADT/SmallPtrSet.h"
28 #include "llvm/ADT/SmallVector.h"
29 #include "llvm/ADT/Statistic.h"
33 STATISTIC(NumRemoved
, "Number of instructions removed");
36 struct VISIBILITY_HIDDEN ADCE
: public FunctionPass
{
37 static char ID
; // Pass identification, replacement for typeid
38 ADCE() : FunctionPass(&ID
) {}
40 virtual bool runOnFunction(Function
& F
);
42 virtual void getAnalysisUsage(AnalysisUsage
& AU
) const {
50 static RegisterPass
<ADCE
> X("adce", "Aggressive Dead Code Elimination");
52 bool ADCE::runOnFunction(Function
& F
) {
53 SmallPtrSet
<Instruction
*, 128> alive
;
54 SmallVector
<Instruction
*, 128> worklist
;
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 I
->mayHaveSideEffects()) {
61 alive
.insert(I
.getInstructionIterator());
62 worklist
.push_back(I
.getInstructionIterator());
65 // Propagate liveness backwards to operands.
66 while (!worklist
.empty()) {
67 Instruction
* curr
= worklist
.back();
70 for (Instruction::op_iterator OI
= curr
->op_begin(), OE
= curr
->op_end();
72 if (Instruction
* Inst
= dyn_cast
<Instruction
>(OI
))
73 if (alive
.insert(Inst
))
74 worklist
.push_back(Inst
);
77 // The inverse of the live set is the dead set. These are those instructions
78 // which have no side effects and do not influence the control flow or return
79 // value of the function, and may therefore be deleted safely.
80 // NOTE: We reuse the worklist vector here for memory efficiency.
81 for (inst_iterator I
= inst_begin(F
), E
= inst_end(F
); I
!= E
; ++I
)
82 if (!alive
.count(I
.getInstructionIterator())) {
83 worklist
.push_back(I
.getInstructionIterator());
84 I
->dropAllReferences();
87 for (SmallVector
<Instruction
*, 1024>::iterator I
= worklist
.begin(),
88 E
= worklist
.end(); I
!= E
; ++I
) {
90 (*I
)->eraseFromParent();
93 return !worklist
.empty();
96 FunctionPass
*llvm::createAggressiveDCEPass() {