1*9880d681SAndroid Build Coastguard Worker //===- DCE.cpp - Code to perform dead code elimination --------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements dead inst elimination and dead code elimination.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker // Dead Inst Elimination performs a single pass over the function removing
13*9880d681SAndroid Build Coastguard Worker // instructions that are obviously dead. Dead Code Elimination is similar, but
14*9880d681SAndroid Build Coastguard Worker // it rechecks instructions that were used by removed instructions to see if
15*9880d681SAndroid Build Coastguard Worker // they are newly dead.
16*9880d681SAndroid Build Coastguard Worker //
17*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
18*9880d681SAndroid Build Coastguard Worker
19*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Scalar/DCE.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SetVector.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/InstIterator.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instruction.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/Pass.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/TargetLibraryInfo.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Scalar.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Utils/Local.h"
28*9880d681SAndroid Build Coastguard Worker using namespace llvm;
29*9880d681SAndroid Build Coastguard Worker
30*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "dce"
31*9880d681SAndroid Build Coastguard Worker
32*9880d681SAndroid Build Coastguard Worker STATISTIC(DIEEliminated, "Number of insts removed by DIE pass");
33*9880d681SAndroid Build Coastguard Worker STATISTIC(DCEEliminated, "Number of insts removed");
34*9880d681SAndroid Build Coastguard Worker
35*9880d681SAndroid Build Coastguard Worker namespace {
36*9880d681SAndroid Build Coastguard Worker //===--------------------------------------------------------------------===//
37*9880d681SAndroid Build Coastguard Worker // DeadInstElimination pass implementation
38*9880d681SAndroid Build Coastguard Worker //
39*9880d681SAndroid Build Coastguard Worker struct DeadInstElimination : public BasicBlockPass {
40*9880d681SAndroid Build Coastguard Worker static char ID; // Pass identification, replacement for typeid
DeadInstElimination__anon294742d70111::DeadInstElimination41*9880d681SAndroid Build Coastguard Worker DeadInstElimination() : BasicBlockPass(ID) {
42*9880d681SAndroid Build Coastguard Worker initializeDeadInstEliminationPass(*PassRegistry::getPassRegistry());
43*9880d681SAndroid Build Coastguard Worker }
runOnBasicBlock__anon294742d70111::DeadInstElimination44*9880d681SAndroid Build Coastguard Worker bool runOnBasicBlock(BasicBlock &BB) override {
45*9880d681SAndroid Build Coastguard Worker if (skipBasicBlock(BB))
46*9880d681SAndroid Build Coastguard Worker return false;
47*9880d681SAndroid Build Coastguard Worker auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
48*9880d681SAndroid Build Coastguard Worker TargetLibraryInfo *TLI = TLIP ? &TLIP->getTLI() : nullptr;
49*9880d681SAndroid Build Coastguard Worker bool Changed = false;
50*9880d681SAndroid Build Coastguard Worker for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); ) {
51*9880d681SAndroid Build Coastguard Worker Instruction *Inst = &*DI++;
52*9880d681SAndroid Build Coastguard Worker if (isInstructionTriviallyDead(Inst, TLI)) {
53*9880d681SAndroid Build Coastguard Worker Inst->eraseFromParent();
54*9880d681SAndroid Build Coastguard Worker Changed = true;
55*9880d681SAndroid Build Coastguard Worker ++DIEEliminated;
56*9880d681SAndroid Build Coastguard Worker }
57*9880d681SAndroid Build Coastguard Worker }
58*9880d681SAndroid Build Coastguard Worker return Changed;
59*9880d681SAndroid Build Coastguard Worker }
60*9880d681SAndroid Build Coastguard Worker
getAnalysisUsage__anon294742d70111::DeadInstElimination61*9880d681SAndroid Build Coastguard Worker void getAnalysisUsage(AnalysisUsage &AU) const override {
62*9880d681SAndroid Build Coastguard Worker AU.setPreservesCFG();
63*9880d681SAndroid Build Coastguard Worker }
64*9880d681SAndroid Build Coastguard Worker };
65*9880d681SAndroid Build Coastguard Worker }
66*9880d681SAndroid Build Coastguard Worker
67*9880d681SAndroid Build Coastguard Worker char DeadInstElimination::ID = 0;
68*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS(DeadInstElimination, "die",
69*9880d681SAndroid Build Coastguard Worker "Dead Instruction Elimination", false, false)
70*9880d681SAndroid Build Coastguard Worker
createDeadInstEliminationPass()71*9880d681SAndroid Build Coastguard Worker Pass *llvm::createDeadInstEliminationPass() {
72*9880d681SAndroid Build Coastguard Worker return new DeadInstElimination();
73*9880d681SAndroid Build Coastguard Worker }
74*9880d681SAndroid Build Coastguard Worker
DCEInstruction(Instruction * I,SmallSetVector<Instruction *,16> & WorkList,const TargetLibraryInfo * TLI)75*9880d681SAndroid Build Coastguard Worker static bool DCEInstruction(Instruction *I,
76*9880d681SAndroid Build Coastguard Worker SmallSetVector<Instruction *, 16> &WorkList,
77*9880d681SAndroid Build Coastguard Worker const TargetLibraryInfo *TLI) {
78*9880d681SAndroid Build Coastguard Worker if (isInstructionTriviallyDead(I, TLI)) {
79*9880d681SAndroid Build Coastguard Worker // Null out all of the instruction's operands to see if any operand becomes
80*9880d681SAndroid Build Coastguard Worker // dead as we go.
81*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
82*9880d681SAndroid Build Coastguard Worker Value *OpV = I->getOperand(i);
83*9880d681SAndroid Build Coastguard Worker I->setOperand(i, nullptr);
84*9880d681SAndroid Build Coastguard Worker
85*9880d681SAndroid Build Coastguard Worker if (!OpV->use_empty() || I == OpV)
86*9880d681SAndroid Build Coastguard Worker continue;
87*9880d681SAndroid Build Coastguard Worker
88*9880d681SAndroid Build Coastguard Worker // If the operand is an instruction that became dead as we nulled out the
89*9880d681SAndroid Build Coastguard Worker // operand, and if it is 'trivially' dead, delete it in a future loop
90*9880d681SAndroid Build Coastguard Worker // iteration.
91*9880d681SAndroid Build Coastguard Worker if (Instruction *OpI = dyn_cast<Instruction>(OpV))
92*9880d681SAndroid Build Coastguard Worker if (isInstructionTriviallyDead(OpI, TLI))
93*9880d681SAndroid Build Coastguard Worker WorkList.insert(OpI);
94*9880d681SAndroid Build Coastguard Worker }
95*9880d681SAndroid Build Coastguard Worker
96*9880d681SAndroid Build Coastguard Worker I->eraseFromParent();
97*9880d681SAndroid Build Coastguard Worker ++DCEEliminated;
98*9880d681SAndroid Build Coastguard Worker return true;
99*9880d681SAndroid Build Coastguard Worker }
100*9880d681SAndroid Build Coastguard Worker return false;
101*9880d681SAndroid Build Coastguard Worker }
102*9880d681SAndroid Build Coastguard Worker
eliminateDeadCode(Function & F,TargetLibraryInfo * TLI)103*9880d681SAndroid Build Coastguard Worker static bool eliminateDeadCode(Function &F, TargetLibraryInfo *TLI) {
104*9880d681SAndroid Build Coastguard Worker bool MadeChange = false;
105*9880d681SAndroid Build Coastguard Worker SmallSetVector<Instruction *, 16> WorkList;
106*9880d681SAndroid Build Coastguard Worker // Iterate over the original function, only adding insts to the worklist
107*9880d681SAndroid Build Coastguard Worker // if they actually need to be revisited. This avoids having to pre-init
108*9880d681SAndroid Build Coastguard Worker // the worklist with the entire function's worth of instructions.
109*9880d681SAndroid Build Coastguard Worker for (inst_iterator FI = inst_begin(F), FE = inst_end(F); FI != FE;) {
110*9880d681SAndroid Build Coastguard Worker Instruction *I = &*FI;
111*9880d681SAndroid Build Coastguard Worker ++FI;
112*9880d681SAndroid Build Coastguard Worker
113*9880d681SAndroid Build Coastguard Worker // We're visiting this instruction now, so make sure it's not in the
114*9880d681SAndroid Build Coastguard Worker // worklist from an earlier visit.
115*9880d681SAndroid Build Coastguard Worker if (!WorkList.count(I))
116*9880d681SAndroid Build Coastguard Worker MadeChange |= DCEInstruction(I, WorkList, TLI);
117*9880d681SAndroid Build Coastguard Worker }
118*9880d681SAndroid Build Coastguard Worker
119*9880d681SAndroid Build Coastguard Worker while (!WorkList.empty()) {
120*9880d681SAndroid Build Coastguard Worker Instruction *I = WorkList.pop_back_val();
121*9880d681SAndroid Build Coastguard Worker MadeChange |= DCEInstruction(I, WorkList, TLI);
122*9880d681SAndroid Build Coastguard Worker }
123*9880d681SAndroid Build Coastguard Worker return MadeChange;
124*9880d681SAndroid Build Coastguard Worker }
125*9880d681SAndroid Build Coastguard Worker
run(Function & F,AnalysisManager<Function> & AM)126*9880d681SAndroid Build Coastguard Worker PreservedAnalyses DCEPass::run(Function &F, AnalysisManager<Function> &AM) {
127*9880d681SAndroid Build Coastguard Worker if (eliminateDeadCode(F, AM.getCachedResult<TargetLibraryAnalysis>(F)))
128*9880d681SAndroid Build Coastguard Worker return PreservedAnalyses::none();
129*9880d681SAndroid Build Coastguard Worker return PreservedAnalyses::all();
130*9880d681SAndroid Build Coastguard Worker }
131*9880d681SAndroid Build Coastguard Worker
132*9880d681SAndroid Build Coastguard Worker namespace {
133*9880d681SAndroid Build Coastguard Worker struct DCELegacyPass : public FunctionPass {
134*9880d681SAndroid Build Coastguard Worker static char ID; // Pass identification, replacement for typeid
DCELegacyPass__anon294742d70211::DCELegacyPass135*9880d681SAndroid Build Coastguard Worker DCELegacyPass() : FunctionPass(ID) {
136*9880d681SAndroid Build Coastguard Worker initializeDCELegacyPassPass(*PassRegistry::getPassRegistry());
137*9880d681SAndroid Build Coastguard Worker }
138*9880d681SAndroid Build Coastguard Worker
runOnFunction__anon294742d70211::DCELegacyPass139*9880d681SAndroid Build Coastguard Worker bool runOnFunction(Function &F) override {
140*9880d681SAndroid Build Coastguard Worker if (skipFunction(F))
141*9880d681SAndroid Build Coastguard Worker return false;
142*9880d681SAndroid Build Coastguard Worker
143*9880d681SAndroid Build Coastguard Worker auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
144*9880d681SAndroid Build Coastguard Worker TargetLibraryInfo *TLI = TLIP ? &TLIP->getTLI() : nullptr;
145*9880d681SAndroid Build Coastguard Worker
146*9880d681SAndroid Build Coastguard Worker return eliminateDeadCode(F, TLI);
147*9880d681SAndroid Build Coastguard Worker }
148*9880d681SAndroid Build Coastguard Worker
getAnalysisUsage__anon294742d70211::DCELegacyPass149*9880d681SAndroid Build Coastguard Worker void getAnalysisUsage(AnalysisUsage &AU) const override {
150*9880d681SAndroid Build Coastguard Worker AU.setPreservesCFG();
151*9880d681SAndroid Build Coastguard Worker }
152*9880d681SAndroid Build Coastguard Worker };
153*9880d681SAndroid Build Coastguard Worker }
154*9880d681SAndroid Build Coastguard Worker
155*9880d681SAndroid Build Coastguard Worker char DCELegacyPass::ID = 0;
156*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS(DCELegacyPass, "dce", "Dead Code Elimination", false, false)
157*9880d681SAndroid Build Coastguard Worker
createDeadCodeEliminationPass()158*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createDeadCodeEliminationPass() {
159*9880d681SAndroid Build Coastguard Worker return new DCELegacyPass();
160*9880d681SAndroid Build Coastguard Worker }
161