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 dead inst elimination and dead code elimination.
12 // Dead Inst Elimination performs a single pass over the function removing
13 // instructions that are obviously dead. Dead Code Elimination is similar, but
14 // it rechecks instructions that were used by removed instructions to see if
15 // they are newly dead.
17 //===----------------------------------------------------------------------===//
19 #include "llvm/Transforms/Scalar.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/IR/InstIterator.h"
22 #include "llvm/IR/Instruction.h"
23 #include "llvm/Pass.h"
24 #include "llvm/Target/TargetLibraryInfo.h"
25 #include "llvm/Transforms/Utils/Local.h"
28 #define DEBUG_TYPE "dce"
30 STATISTIC(DIEEliminated, "Number of insts removed by DIE pass");
31 STATISTIC(DCEEliminated, "Number of insts removed");
34 //===--------------------------------------------------------------------===//
35 // DeadInstElimination pass implementation
37 struct DeadInstElimination : public BasicBlockPass {
38 static char ID; // Pass identification, replacement for typeid
39 DeadInstElimination() : BasicBlockPass(ID) {
40 initializeDeadInstEliminationPass(*PassRegistry::getPassRegistry());
42 bool runOnBasicBlock(BasicBlock &BB) override {
43 if (skipOptnoneFunction(BB))
45 TargetLibraryInfo *TLI = getAnalysisIfAvailable<TargetLibraryInfo>();
47 for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); ) {
48 Instruction *Inst = DI++;
49 if (isInstructionTriviallyDead(Inst, TLI)) {
50 Inst->eraseFromParent();
58 void getAnalysisUsage(AnalysisUsage &AU) const override {
64 char DeadInstElimination::ID = 0;
65 INITIALIZE_PASS(DeadInstElimination, "die",
66 "Dead Instruction Elimination", false, false)
68 Pass *llvm::createDeadInstEliminationPass() {
69 return new DeadInstElimination();
74 //===--------------------------------------------------------------------===//
75 // DeadCodeElimination pass implementation
77 struct DCE : public FunctionPass {
78 static char ID; // Pass identification, replacement for typeid
79 DCE() : FunctionPass(ID) {
80 initializeDCEPass(*PassRegistry::getPassRegistry());
83 bool runOnFunction(Function &F) override;
85 void getAnalysisUsage(AnalysisUsage &AU) const override {
92 INITIALIZE_PASS(DCE, "dce", "Dead Code Elimination", false, false)
94 bool DCE::runOnFunction(Function &F) {
95 if (skipOptnoneFunction(F))
98 TargetLibraryInfo *TLI = getAnalysisIfAvailable<TargetLibraryInfo>();
100 // Start out with all of the instructions in the worklist...
101 std::vector<Instruction*> WorkList;
102 for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i)
103 WorkList.push_back(&*i);
105 // Loop over the worklist finding instructions that are dead. If they are
106 // dead make them drop all of their uses, making other instructions
107 // potentially dead, and work until the worklist is empty.
109 bool MadeChange = false;
110 while (!WorkList.empty()) {
111 Instruction *I = WorkList.back();
114 if (isInstructionTriviallyDead(I, TLI)) { // If the instruction is dead.
115 // Loop over all of the values that the instruction uses, if there are
116 // instructions being used, add them to the worklist, because they might
117 // go dead after this one is removed.
119 for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI)
120 if (Instruction *Used = dyn_cast<Instruction>(*OI))
121 WorkList.push_back(Used);
123 // Remove the instruction.
124 I->eraseFromParent();
126 // Remove the instruction from the worklist if it still exists in it.
127 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I),
137 FunctionPass *llvm::createDeadCodeEliminationPass() {