blob: ae61208b522fd39901a919e0e6b99f06104bd19c [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===- DCE.cpp - Code to perform dead code elimination --------------------===//
John Criswellb576c942003-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 Lattner00950542001-06-06 20:29:01 +00009//
Chris Lattner92deeaf2002-05-07 04:24:11 +000010// This file implements dead inst elimination and dead code elimination.
Chris Lattner00950542001-06-06 20:29:01 +000011//
Chris Lattner92deeaf2002-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 Lattner00950542001-06-06 20:29:01 +000016//
17//===----------------------------------------------------------------------===//
18
Chris Lattner022103b2002-05-07 20:03:00 +000019#include "llvm/Transforms/Scalar.h"
Chris Lattner2ed01d82002-05-07 18:10:55 +000020#include "llvm/Transforms/Utils/Local.h"
21#include "llvm/Instruction.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000022#include "llvm/Pass.h"
Chris Lattner92deeaf2002-05-07 04:24:11 +000023#include "llvm/Support/InstIterator.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/ADT/Statistic.h"
Chris Lattner92deeaf2002-05-07 04:24:11 +000025#include <set>
Chris Lattnerd7456022004-01-09 06:02:20 +000026using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Chris Lattner92deeaf2002-05-07 04:24:11 +000028namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000029 Statistic<> DIEEliminated("die", "Number of insts removed");
30 Statistic<> DCEEliminated("dce", "Number of insts removed");
31
32 //===--------------------------------------------------------------------===//
33 // DeadInstElimination pass implementation
34 //
35
Chris Lattner92deeaf2002-05-07 04:24:11 +000036 struct DeadInstElimination : public BasicBlockPass {
Chris Lattner7e708292002-06-25 16:13:24 +000037 virtual bool runOnBasicBlock(BasicBlock &BB) {
Chris Lattner92deeaf2002-05-07 04:24:11 +000038 bool Changed = false;
Chris Lattner7e708292002-06-25 16:13:24 +000039 for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); )
Chris Lattner16da4942002-05-26 20:18:18 +000040 if (dceInstruction(DI)) {
Chris Lattner92deeaf2002-05-07 04:24:11 +000041 Changed = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +000042 ++DIEEliminated;
43 } else
Chris Lattner92deeaf2002-05-07 04:24:11 +000044 ++DI;
45 return Changed;
46 }
47
48 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercb2610e2002-10-21 20:00:28 +000049 AU.setPreservesCFG();
Chris Lattner92deeaf2002-05-07 04:24:11 +000050 }
51 };
Chris Lattnerf6293092002-07-23 18:06:35 +000052
Chris Lattnera6275cc2002-07-26 21:12:46 +000053 RegisterOpt<DeadInstElimination> X("die", "Dead Instruction Elimination");
Chris Lattner00950542001-06-06 20:29:01 +000054}
55
Chris Lattner4b501562004-09-20 04:43:15 +000056FunctionPass *llvm::createDeadInstEliminationPass() {
Chris Lattnerbd0ef772002-02-26 21:46:54 +000057 return new DeadInstElimination();
Chris Lattnerc560f882002-01-23 05:48:24 +000058}
59
Chris Lattner92deeaf2002-05-07 04:24:11 +000060
Chris Lattner92deeaf2002-05-07 04:24:11 +000061//===----------------------------------------------------------------------===//
62// DeadCodeElimination pass implementation
Chris Lattnerf155e132001-06-07 16:59:26 +000063//
Chris Lattnerbd0ef772002-02-26 21:46:54 +000064
65namespace {
Chris Lattner92deeaf2002-05-07 04:24:11 +000066 struct DCE : public FunctionPass {
Chris Lattner7e708292002-06-25 16:13:24 +000067 virtual bool runOnFunction(Function &F);
Chris Lattner92deeaf2002-05-07 04:24:11 +000068
69 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercb2610e2002-10-21 20:00:28 +000070 AU.setPreservesCFG();
Chris Lattnerbd0ef772002-02-26 21:46:54 +000071 }
Chris Lattner92deeaf2002-05-07 04:24:11 +000072 };
Chris Lattnerf6293092002-07-23 18:06:35 +000073
Chris Lattnera6275cc2002-07-26 21:12:46 +000074 RegisterOpt<DCE> Y("dce", "Dead Code Elimination");
Chris Lattner92deeaf2002-05-07 04:24:11 +000075}
76
Chris Lattner7e708292002-06-25 16:13:24 +000077bool DCE::runOnFunction(Function &F) {
Chris Lattner92deeaf2002-05-07 04:24:11 +000078 // Start out with all of the instructions in the worklist...
Chris Lattner6ffe5512004-04-27 15:13:33 +000079 std::vector<Instruction*> WorkList;
80 for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) {
81 WorkList.push_back(&*i);
82 }
Chris Lattner92deeaf2002-05-07 04:24:11 +000083 std::set<Instruction*> DeadInsts;
84
85 // Loop over the worklist finding instructions that are dead. If they are
86 // dead make them drop all of their uses, making other instructions
87 // potentially dead, and work until the worklist is empty.
88 //
89 while (!WorkList.empty()) {
90 Instruction *I = WorkList.back();
91 WorkList.pop_back();
Chris Lattnerbd0ef772002-02-26 21:46:54 +000092
Chris Lattner2ed01d82002-05-07 18:10:55 +000093 if (isInstructionTriviallyDead(I)) { // If the instruction is dead...
Chris Lattner92deeaf2002-05-07 04:24:11 +000094 // Loop over all of the values that the instruction uses, if there are
95 // instructions being used, add them to the worklist, because they might
96 // go dead after this one is removed.
97 //
Chris Lattner4e5f0362004-04-21 22:29:37 +000098 for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI)
99 if (Instruction *Used = dyn_cast<Instruction>(*OI))
Chris Lattner92deeaf2002-05-07 04:24:11 +0000100 WorkList.push_back(Used);
101
102 // Tell the instruction to let go of all of the values it uses...
103 I->dropAllReferences();
104
105 // Keep track of this instruction, because we are going to delete it later
106 DeadInsts.insert(I);
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000107 }
Chris Lattner92deeaf2002-05-07 04:24:11 +0000108 }
109
110 // If we found no dead instructions, we haven't changed the function...
111 if (DeadInsts.empty()) return false;
112
113 // Otherwise, loop over the program, removing and deleting the instructions...
Chris Lattner7e708292002-06-25 16:13:24 +0000114 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
115 for (BasicBlock::iterator BI = I->begin(); BI != I->end(); )
116 if (DeadInsts.count(BI)) { // Is this instruction dead?
117 BI = I->getInstList().erase(BI); // Yup, remove and delete inst
Chris Lattner3dec1f22002-05-10 15:38:35 +0000118 ++DCEEliminated;
Chris Lattner92deeaf2002-05-07 04:24:11 +0000119 } else { // This instruction is not dead
120 ++BI; // Continue on to the next one...
121 }
Chris Lattner92deeaf2002-05-07 04:24:11 +0000122
123 return true;
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000124}
125
Brian Gaeke96d4bf72004-07-27 17:43:21 +0000126FunctionPass *llvm::createDeadCodeEliminationPass() {
Chris Lattner92deeaf2002-05-07 04:24:11 +0000127 return new DCE();
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000128}
Brian Gaeked0fde302003-11-11 22:41:34 +0000129