blob: 3e63cadd3e51b4aa897361ab19538f571f9c7406 [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===- DCE.cpp - Code to perform dead code elimination --------------------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner87e88062002-05-07 04:24:11 +000010// This file implements dead inst elimination and dead code elimination.
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
Chris Lattner87e88062002-05-07 04:24:11 +000012// 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.
Chris Lattner2f7c9632001-06-06 20:29:01 +000016//
17//===----------------------------------------------------------------------===//
18
Chris Lattnerb4cfa7f2002-05-07 20:03:00 +000019#include "llvm/Transforms/Scalar.h"
Chris Lattner560da702002-05-07 18:10:55 +000020#include "llvm/Transforms/Utils/Local.h"
21#include "llvm/Instruction.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000022#include "llvm/Pass.h"
Chris Lattner87e88062002-05-07 04:24:11 +000023#include "llvm/Support/InstIterator.h"
Chris Lattnerbf3a0992002-10-01 22:38:41 +000024#include "Support/Statistic.h"
Chris Lattner87e88062002-05-07 04:24:11 +000025#include <set>
26
Brian Gaeke960707c2003-11-11 22:41:34 +000027namespace llvm {
28
Chris Lattner87e88062002-05-07 04:24:11 +000029namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000030 Statistic<> DIEEliminated("die", "Number of insts removed");
31 Statistic<> DCEEliminated("dce", "Number of insts removed");
32
33 //===--------------------------------------------------------------------===//
34 // DeadInstElimination pass implementation
35 //
36
Chris Lattner87e88062002-05-07 04:24:11 +000037 struct DeadInstElimination : public BasicBlockPass {
Chris Lattner113f4f42002-06-25 16:13:24 +000038 virtual bool runOnBasicBlock(BasicBlock &BB) {
Chris Lattner87e88062002-05-07 04:24:11 +000039 bool Changed = false;
Chris Lattner113f4f42002-06-25 16:13:24 +000040 for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); )
Chris Lattnerab038d42002-05-26 20:18:18 +000041 if (dceInstruction(DI)) {
Chris Lattner87e88062002-05-07 04:24:11 +000042 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +000043 ++DIEEliminated;
44 } else
Chris Lattner87e88062002-05-07 04:24:11 +000045 ++DI;
46 return Changed;
47 }
48
49 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner820d9712002-10-21 20:00:28 +000050 AU.setPreservesCFG();
Chris Lattner87e88062002-05-07 04:24:11 +000051 }
52 };
Chris Lattnerb28b6802002-07-23 18:06:35 +000053
Chris Lattnerc8b70922002-07-26 21:12:46 +000054 RegisterOpt<DeadInstElimination> X("die", "Dead Instruction Elimination");
Chris Lattner2f7c9632001-06-06 20:29:01 +000055}
56
Chris Lattner04805fa2002-02-26 21:46:54 +000057Pass *createDeadInstEliminationPass() {
58 return new DeadInstElimination();
Chris Lattnerccbd4e42002-01-23 05:48:24 +000059}
60
Chris Lattner87e88062002-05-07 04:24:11 +000061
Chris Lattner87e88062002-05-07 04:24:11 +000062//===----------------------------------------------------------------------===//
63// DeadCodeElimination pass implementation
Chris Lattnerd821c2a2001-06-07 16:59:26 +000064//
Chris Lattner04805fa2002-02-26 21:46:54 +000065
66namespace {
Chris Lattner87e88062002-05-07 04:24:11 +000067 struct DCE : public FunctionPass {
Chris Lattner113f4f42002-06-25 16:13:24 +000068 virtual bool runOnFunction(Function &F);
Chris Lattner87e88062002-05-07 04:24:11 +000069
70 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner820d9712002-10-21 20:00:28 +000071 AU.setPreservesCFG();
Chris Lattner04805fa2002-02-26 21:46:54 +000072 }
Chris Lattner87e88062002-05-07 04:24:11 +000073 };
Chris Lattnerb28b6802002-07-23 18:06:35 +000074
Chris Lattnerc8b70922002-07-26 21:12:46 +000075 RegisterOpt<DCE> Y("dce", "Dead Code Elimination");
Chris Lattner87e88062002-05-07 04:24:11 +000076}
77
Chris Lattner113f4f42002-06-25 16:13:24 +000078bool DCE::runOnFunction(Function &F) {
Chris Lattner87e88062002-05-07 04:24:11 +000079 // Start out with all of the instructions in the worklist...
80 std::vector<Instruction*> WorkList(inst_begin(F), inst_end(F));
81 std::set<Instruction*> DeadInsts;
82
83 // Loop over the worklist finding instructions that are dead. If they are
84 // dead make them drop all of their uses, making other instructions
85 // potentially dead, and work until the worklist is empty.
86 //
87 while (!WorkList.empty()) {
88 Instruction *I = WorkList.back();
89 WorkList.pop_back();
Chris Lattner04805fa2002-02-26 21:46:54 +000090
Chris Lattner560da702002-05-07 18:10:55 +000091 if (isInstructionTriviallyDead(I)) { // If the instruction is dead...
Chris Lattner87e88062002-05-07 04:24:11 +000092 // Loop over all of the values that the instruction uses, if there are
93 // instructions being used, add them to the worklist, because they might
94 // go dead after this one is removed.
95 //
96 for (User::use_iterator UI = I->use_begin(), UE = I->use_end();
97 UI != UE; ++UI)
98 if (Instruction *Used = dyn_cast<Instruction>(*UI))
99 WorkList.push_back(Used);
100
101 // Tell the instruction to let go of all of the values it uses...
102 I->dropAllReferences();
103
104 // Keep track of this instruction, because we are going to delete it later
105 DeadInsts.insert(I);
Chris Lattner04805fa2002-02-26 21:46:54 +0000106 }
Chris Lattner87e88062002-05-07 04:24:11 +0000107 }
108
109 // If we found no dead instructions, we haven't changed the function...
110 if (DeadInsts.empty()) return false;
111
112 // Otherwise, loop over the program, removing and deleting the instructions...
Chris Lattner113f4f42002-06-25 16:13:24 +0000113 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
114 for (BasicBlock::iterator BI = I->begin(); BI != I->end(); )
115 if (DeadInsts.count(BI)) { // Is this instruction dead?
116 BI = I->getInstList().erase(BI); // Yup, remove and delete inst
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000117 ++DCEEliminated;
Chris Lattner87e88062002-05-07 04:24:11 +0000118 } else { // This instruction is not dead
119 ++BI; // Continue on to the next one...
120 }
Chris Lattner87e88062002-05-07 04:24:11 +0000121
122 return true;
Chris Lattner04805fa2002-02-26 21:46:54 +0000123}
124
125Pass *createDeadCodeEliminationPass() {
Chris Lattner87e88062002-05-07 04:24:11 +0000126 return new DCE();
Chris Lattner04805fa2002-02-26 21:46:54 +0000127}
Brian Gaeke960707c2003-11-11 22:41:34 +0000128
129} // End llvm namespace