blob: a5990ee79e89d863b79eafba4e40f1687b2351d3 [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//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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"
Reid Spencer557ab152007-02-05 23:32:05 +000024#include "llvm/Support/Compiler.h"
Chris Lattner87e88062002-05-07 04:24:11 +000025#include "llvm/Support/InstIterator.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000026#include "llvm/ADT/Statistic.h"
Chris Lattner87e88062002-05-07 04:24:11 +000027#include <set>
Chris Lattner49525f82004-01-09 06:02:20 +000028using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000029
Chris Lattner79a42ac2006-12-19 21:40:18 +000030STATISTIC(DIEEliminated, "Number of insts removed by DIE pass");
31STATISTIC(DCEEliminated, "Number of insts removed");
Chris Lattnerbf3a0992002-10-01 22:38:41 +000032
Chris Lattner79a42ac2006-12-19 21:40:18 +000033namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000034 //===--------------------------------------------------------------------===//
35 // DeadInstElimination pass implementation
36 //
Reid Spencer557ab152007-02-05 23:32:05 +000037 struct VISIBILITY_HIDDEN DeadInstElimination : public BasicBlockPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000038 static char ID; // Pass identification, replacement for typeid
Devang Patel09f162c2007-05-01 21:15:47 +000039 DeadInstElimination() : BasicBlockPass(intptr_t(&ID)) {}
Chris Lattner113f4f42002-06-25 16:13:24 +000040 virtual bool runOnBasicBlock(BasicBlock &BB) {
Chris Lattner87e88062002-05-07 04:24:11 +000041 bool Changed = false;
Chris Lattnerc92fa422008-11-27 22:46:09 +000042 for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); ) {
43 Instruction *Inst = DI++;
44 if (isInstructionTriviallyDead(Inst)) {
45 Inst->eraseFromParent();
Chris Lattner87e88062002-05-07 04:24:11 +000046 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +000047 ++DIEEliminated;
Chris Lattnerc92fa422008-11-27 22:46:09 +000048 }
49 }
Chris Lattner87e88062002-05-07 04:24:11 +000050 return Changed;
51 }
52
53 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner820d9712002-10-21 20:00:28 +000054 AU.setPreservesCFG();
Chris Lattner87e88062002-05-07 04:24:11 +000055 }
56 };
Chris Lattner2f7c9632001-06-06 20:29:01 +000057}
58
Dan Gohmand78c4002008-05-13 00:00:25 +000059char DeadInstElimination::ID = 0;
60static RegisterPass<DeadInstElimination>
61X("die", "Dead Instruction Elimination");
62
Devang Patel5292e652007-01-25 23:23:25 +000063Pass *llvm::createDeadInstEliminationPass() {
Chris Lattner04805fa2002-02-26 21:46:54 +000064 return new DeadInstElimination();
Chris Lattnerccbd4e42002-01-23 05:48:24 +000065}
66
Chris Lattner87e88062002-05-07 04:24:11 +000067
Chris Lattner04805fa2002-02-26 21:46:54 +000068namespace {
Chris Lattner79a42ac2006-12-19 21:40:18 +000069 //===--------------------------------------------------------------------===//
70 // DeadCodeElimination pass implementation
71 //
Chris Lattner87e88062002-05-07 04:24:11 +000072 struct DCE : public FunctionPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000073 static char ID; // Pass identification, replacement for typeid
Dan Gohmana79db302008-09-04 17:05:41 +000074 DCE() : FunctionPass(&ID) {}
Devang Patel09f162c2007-05-01 21:15:47 +000075
Chris Lattner113f4f42002-06-25 16:13:24 +000076 virtual bool runOnFunction(Function &F);
Chris Lattner87e88062002-05-07 04:24:11 +000077
78 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner820d9712002-10-21 20:00:28 +000079 AU.setPreservesCFG();
Chris Lattner04805fa2002-02-26 21:46:54 +000080 }
Chris Lattner87e88062002-05-07 04:24:11 +000081 };
82}
83
Dan Gohmand78c4002008-05-13 00:00:25 +000084char DCE::ID = 0;
85static RegisterPass<DCE> Y("dce", "Dead Code Elimination");
86
Chris Lattner113f4f42002-06-25 16:13:24 +000087bool DCE::runOnFunction(Function &F) {
Chris Lattner87e88062002-05-07 04:24:11 +000088 // Start out with all of the instructions in the worklist...
Chris Lattner2d3a7a62004-04-27 15:13:33 +000089 std::vector<Instruction*> WorkList;
Chris Lattner49221182005-05-08 18:45:26 +000090 for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i)
91 WorkList.push_back(&*i);
Misha Brukmanb1c93172005-04-21 23:48:37 +000092
Chris Lattner87e88062002-05-07 04:24:11 +000093 // Loop over the worklist finding instructions that are dead. If they are
94 // dead make them drop all of their uses, making other instructions
95 // potentially dead, and work until the worklist is empty.
96 //
Chris Lattner49221182005-05-08 18:45:26 +000097 bool MadeChange = false;
Chris Lattner87e88062002-05-07 04:24:11 +000098 while (!WorkList.empty()) {
99 Instruction *I = WorkList.back();
100 WorkList.pop_back();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000101
Chris Lattner49221182005-05-08 18:45:26 +0000102 if (isInstructionTriviallyDead(I)) { // If the instruction is dead.
Chris Lattner87e88062002-05-07 04:24:11 +0000103 // Loop over all of the values that the instruction uses, if there are
104 // instructions being used, add them to the worklist, because they might
105 // go dead after this one is removed.
106 //
Chris Lattnerfb9a2992004-04-21 22:29:37 +0000107 for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI)
108 if (Instruction *Used = dyn_cast<Instruction>(*OI))
Chris Lattner87e88062002-05-07 04:24:11 +0000109 WorkList.push_back(Used);
110
Chris Lattner49221182005-05-08 18:45:26 +0000111 // Remove the instruction.
112 I->eraseFromParent();
Chris Lattner87e88062002-05-07 04:24:11 +0000113
Chris Lattner49221182005-05-08 18:45:26 +0000114 // Remove the instruction from the worklist if it still exists in it.
David Greene2a5967b2007-12-17 17:39:51 +0000115 for (std::vector<Instruction*>::iterator WI = WorkList.begin();
Bill Wendlinga00fa322008-09-18 23:04:18 +0000116 WI != WorkList.end(); ) {
117 if (*WI == I)
David Greene2a5967b2007-12-17 17:39:51 +0000118 WI = WorkList.erase(WI);
Bill Wendlinga00fa322008-09-18 23:04:18 +0000119 else
120 ++WI;
121 }
Chris Lattner49221182005-05-08 18:45:26 +0000122
123 MadeChange = true;
124 ++DCEEliminated;
Chris Lattner04805fa2002-02-26 21:46:54 +0000125 }
Chris Lattner87e88062002-05-07 04:24:11 +0000126 }
Chris Lattner49221182005-05-08 18:45:26 +0000127 return MadeChange;
Chris Lattner04805fa2002-02-26 21:46:54 +0000128}
129
Brian Gaeke38b79e82004-07-27 17:43:21 +0000130FunctionPass *llvm::createDeadCodeEliminationPass() {
Chris Lattner87e88062002-05-07 04:24:11 +0000131 return new DCE();
Chris Lattner04805fa2002-02-26 21:46:54 +0000132}
Brian Gaeke960707c2003-11-11 22:41:34 +0000133