blob: cf1a7fc877d83844da71a95b4cc65deba7c405d9 [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===- DCE.cpp - Code to perform dead code elimination --------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// 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.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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 Lattner79a42ac2006-12-19 21:40:18 +000019#define DEBUG_TYPE "dce"
Chris Lattnerb4cfa7f2002-05-07 20:03:00 +000020#include "llvm/Transforms/Scalar.h"
Chris Lattner560da702002-05-07 18:10:55 +000021#include "llvm/Transforms/Utils/Local.h"
22#include "llvm/Instruction.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000023#include "llvm/Pass.h"
Chris Lattner87e88062002-05-07 04:24:11 +000024#include "llvm/Support/InstIterator.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000025#include "llvm/ADT/Statistic.h"
Chris Lattner87e88062002-05-07 04:24:11 +000026#include <set>
Chris Lattner49525f82004-01-09 06:02:20 +000027using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000028
Chris Lattner79a42ac2006-12-19 21:40:18 +000029STATISTIC(DIEEliminated, "Number of insts removed by DIE pass");
30STATISTIC(DCEEliminated, "Number of insts removed");
Chris Lattnerbf3a0992002-10-01 22:38:41 +000031
Chris Lattner79a42ac2006-12-19 21:40:18 +000032namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000033 //===--------------------------------------------------------------------===//
34 // DeadInstElimination pass implementation
35 //
Chris Lattner87e88062002-05-07 04:24:11 +000036 struct DeadInstElimination : public BasicBlockPass {
Chris Lattner113f4f42002-06-25 16:13:24 +000037 virtual bool runOnBasicBlock(BasicBlock &BB) {
Chris Lattner87e88062002-05-07 04:24:11 +000038 bool Changed = false;
Chris Lattner113f4f42002-06-25 16:13:24 +000039 for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); )
Chris Lattnerab038d42002-05-26 20:18:18 +000040 if (dceInstruction(DI)) {
Chris Lattner87e88062002-05-07 04:24:11 +000041 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +000042 ++DIEEliminated;
43 } else
Chris Lattner87e88062002-05-07 04:24:11 +000044 ++DI;
45 return Changed;
46 }
47
48 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner820d9712002-10-21 20:00:28 +000049 AU.setPreservesCFG();
Chris Lattner87e88062002-05-07 04:24:11 +000050 }
51 };
Misha Brukmanb1c93172005-04-21 23:48:37 +000052
Chris Lattnerc2d3d312006-08-27 22:42:52 +000053 RegisterPass<DeadInstElimination> X("die", "Dead Instruction Elimination");
Chris Lattner2f7c9632001-06-06 20:29:01 +000054}
55
Chris Lattner3e860842004-09-20 04:43:15 +000056FunctionPass *llvm::createDeadInstEliminationPass() {
Chris Lattner04805fa2002-02-26 21:46:54 +000057 return new DeadInstElimination();
Chris Lattnerccbd4e42002-01-23 05:48:24 +000058}
59
Chris Lattner87e88062002-05-07 04:24:11 +000060
Chris Lattner04805fa2002-02-26 21:46:54 +000061namespace {
Chris Lattner79a42ac2006-12-19 21:40:18 +000062 //===--------------------------------------------------------------------===//
63 // DeadCodeElimination pass implementation
64 //
Chris Lattner87e88062002-05-07 04:24:11 +000065 struct DCE : public FunctionPass {
Chris Lattner113f4f42002-06-25 16:13:24 +000066 virtual bool runOnFunction(Function &F);
Chris Lattner87e88062002-05-07 04:24:11 +000067
68 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner820d9712002-10-21 20:00:28 +000069 AU.setPreservesCFG();
Chris Lattner04805fa2002-02-26 21:46:54 +000070 }
Chris Lattner87e88062002-05-07 04:24:11 +000071 };
Chris Lattnerb28b6802002-07-23 18:06:35 +000072
Chris Lattnerc2d3d312006-08-27 22:42:52 +000073 RegisterPass<DCE> Y("dce", "Dead Code Elimination");
Chris Lattner87e88062002-05-07 04:24:11 +000074}
75
Chris Lattner113f4f42002-06-25 16:13:24 +000076bool DCE::runOnFunction(Function &F) {
Chris Lattner87e88062002-05-07 04:24:11 +000077 // Start out with all of the instructions in the worklist...
Chris Lattner2d3a7a62004-04-27 15:13:33 +000078 std::vector<Instruction*> WorkList;
Chris Lattner49221182005-05-08 18:45:26 +000079 for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i)
80 WorkList.push_back(&*i);
Misha Brukmanb1c93172005-04-21 23:48:37 +000081
Chris Lattner87e88062002-05-07 04:24:11 +000082 // Loop over the worklist finding instructions that are dead. If they are
83 // dead make them drop all of their uses, making other instructions
84 // potentially dead, and work until the worklist is empty.
85 //
Chris Lattner49221182005-05-08 18:45:26 +000086 bool MadeChange = false;
Chris Lattner87e88062002-05-07 04:24:11 +000087 while (!WorkList.empty()) {
88 Instruction *I = WorkList.back();
89 WorkList.pop_back();
Misha Brukmanb1c93172005-04-21 23:48:37 +000090
Chris Lattner49221182005-05-08 18:45:26 +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 //
Chris Lattnerfb9a2992004-04-21 22:29:37 +000096 for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI)
97 if (Instruction *Used = dyn_cast<Instruction>(*OI))
Chris Lattner87e88062002-05-07 04:24:11 +000098 WorkList.push_back(Used);
99
Chris Lattner49221182005-05-08 18:45:26 +0000100 // Remove the instruction.
101 I->eraseFromParent();
Chris Lattner87e88062002-05-07 04:24:11 +0000102
Chris Lattner49221182005-05-08 18:45:26 +0000103 // Remove the instruction from the worklist if it still exists in it.
104 for (std::vector<Instruction*>::iterator WI = WorkList.begin(),
105 E = WorkList.end(); WI != E; ++WI)
106 if (*WI == I) {
107 WorkList.erase(WI);
108 --E;
109 --WI;
110 }
111
112 MadeChange = true;
113 ++DCEEliminated;
Chris Lattner04805fa2002-02-26 21:46:54 +0000114 }
Chris Lattner87e88062002-05-07 04:24:11 +0000115 }
Chris Lattner49221182005-05-08 18:45:26 +0000116 return MadeChange;
Chris Lattner04805fa2002-02-26 21:46:54 +0000117}
118
Brian Gaeke38b79e82004-07-27 17:43:21 +0000119FunctionPass *llvm::createDeadCodeEliminationPass() {
Chris Lattner87e88062002-05-07 04:24:11 +0000120 return new DCE();
Chris Lattner04805fa2002-02-26 21:46:54 +0000121}
Brian Gaeke960707c2003-11-11 22:41:34 +0000122