blob: 1469c666948730f963609946bbfe29728ae670de [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===- DCE.cpp - Code to perform dead code elimination --------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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 Lattner0e5f4992006-12-19 21:40:18 +000019#define DEBUG_TYPE "dce"
Chris Lattner022103b2002-05-07 20:03:00 +000020#include "llvm/Transforms/Scalar.h"
Chris Lattner2ed01d82002-05-07 18:10:55 +000021#include "llvm/Transforms/Utils/Local.h"
22#include "llvm/Instruction.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000023#include "llvm/Pass.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000024#include "llvm/Support/Compiler.h"
Chris Lattner92deeaf2002-05-07 04:24:11 +000025#include "llvm/Support/InstIterator.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/ADT/Statistic.h"
Chris Lattner92deeaf2002-05-07 04:24:11 +000027#include <set>
Chris Lattnerd7456022004-01-09 06:02:20 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Chris Lattner0e5f4992006-12-19 21:40:18 +000030STATISTIC(DIEEliminated, "Number of insts removed by DIE pass");
31STATISTIC(DCEEliminated, "Number of insts removed");
Chris Lattnera92f6962002-10-01 22:38:41 +000032
Chris Lattner0e5f4992006-12-19 21:40:18 +000033namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000034 //===--------------------------------------------------------------------===//
35 // DeadInstElimination pass implementation
36 //
Reid Spencer9133fe22007-02-05 23:32:05 +000037 struct VISIBILITY_HIDDEN DeadInstElimination : public BasicBlockPass {
Devang Patel19974732007-05-03 01:11:54 +000038 static char ID; // Pass identifcation, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000039 DeadInstElimination() : BasicBlockPass(intptr_t(&ID)) {}
Chris Lattner7e708292002-06-25 16:13:24 +000040 virtual bool runOnBasicBlock(BasicBlock &BB) {
Chris Lattner92deeaf2002-05-07 04:24:11 +000041 bool Changed = false;
Chris Lattner7e708292002-06-25 16:13:24 +000042 for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); )
Chris Lattner16da4942002-05-26 20:18:18 +000043 if (dceInstruction(DI)) {
Chris Lattner92deeaf2002-05-07 04:24:11 +000044 Changed = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +000045 ++DIEEliminated;
46 } else
Chris Lattner92deeaf2002-05-07 04:24:11 +000047 ++DI;
48 return Changed;
49 }
50
51 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercb2610e2002-10-21 20:00:28 +000052 AU.setPreservesCFG();
Chris Lattner92deeaf2002-05-07 04:24:11 +000053 }
54 };
Misha Brukmanfd939082005-04-21 23:48:37 +000055
Devang Patel19974732007-05-03 01:11:54 +000056 char DeadInstElimination::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +000057 RegisterPass<DeadInstElimination> X("die", "Dead Instruction Elimination");
Chris Lattner00950542001-06-06 20:29:01 +000058}
59
Devang Patel4d447f52007-01-25 23:23:25 +000060Pass *llvm::createDeadInstEliminationPass() {
Chris Lattnerbd0ef772002-02-26 21:46:54 +000061 return new DeadInstElimination();
Chris Lattnerc560f882002-01-23 05:48:24 +000062}
63
Chris Lattner92deeaf2002-05-07 04:24:11 +000064
Chris Lattnerbd0ef772002-02-26 21:46:54 +000065namespace {
Chris Lattner0e5f4992006-12-19 21:40:18 +000066 //===--------------------------------------------------------------------===//
67 // DeadCodeElimination pass implementation
68 //
Chris Lattner92deeaf2002-05-07 04:24:11 +000069 struct DCE : public FunctionPass {
Devang Patel19974732007-05-03 01:11:54 +000070 static char ID; // Pass identifcation, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000071 DCE() : FunctionPass((intptr_t)&ID) {}
72
Chris Lattner7e708292002-06-25 16:13:24 +000073 virtual bool runOnFunction(Function &F);
Chris Lattner92deeaf2002-05-07 04:24:11 +000074
75 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercb2610e2002-10-21 20:00:28 +000076 AU.setPreservesCFG();
Chris Lattnerbd0ef772002-02-26 21:46:54 +000077 }
Chris Lattner92deeaf2002-05-07 04:24:11 +000078 };
Chris Lattnerf6293092002-07-23 18:06:35 +000079
Devang Patel19974732007-05-03 01:11:54 +000080 char DCE::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +000081 RegisterPass<DCE> Y("dce", "Dead Code Elimination");
Chris Lattner92deeaf2002-05-07 04:24:11 +000082}
83
Chris Lattner7e708292002-06-25 16:13:24 +000084bool DCE::runOnFunction(Function &F) {
Chris Lattner92deeaf2002-05-07 04:24:11 +000085 // Start out with all of the instructions in the worklist...
Chris Lattner6ffe5512004-04-27 15:13:33 +000086 std::vector<Instruction*> WorkList;
Chris Lattner92812822005-05-08 18:45:26 +000087 for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i)
88 WorkList.push_back(&*i);
Misha Brukmanfd939082005-04-21 23:48:37 +000089
Chris Lattner92deeaf2002-05-07 04:24:11 +000090 // Loop over the worklist finding instructions that are dead. If they are
91 // dead make them drop all of their uses, making other instructions
92 // potentially dead, and work until the worklist is empty.
93 //
Chris Lattner92812822005-05-08 18:45:26 +000094 bool MadeChange = false;
Chris Lattner92deeaf2002-05-07 04:24:11 +000095 while (!WorkList.empty()) {
96 Instruction *I = WorkList.back();
97 WorkList.pop_back();
Misha Brukmanfd939082005-04-21 23:48:37 +000098
Chris Lattner92812822005-05-08 18:45:26 +000099 if (isInstructionTriviallyDead(I)) { // If the instruction is dead.
Chris Lattner92deeaf2002-05-07 04:24:11 +0000100 // Loop over all of the values that the instruction uses, if there are
101 // instructions being used, add them to the worklist, because they might
102 // go dead after this one is removed.
103 //
Chris Lattner4e5f0362004-04-21 22:29:37 +0000104 for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI)
105 if (Instruction *Used = dyn_cast<Instruction>(*OI))
Chris Lattner92deeaf2002-05-07 04:24:11 +0000106 WorkList.push_back(Used);
107
Chris Lattner92812822005-05-08 18:45:26 +0000108 // Remove the instruction.
109 I->eraseFromParent();
Chris Lattner92deeaf2002-05-07 04:24:11 +0000110
Chris Lattner92812822005-05-08 18:45:26 +0000111 // Remove the instruction from the worklist if it still exists in it.
112 for (std::vector<Instruction*>::iterator WI = WorkList.begin(),
113 E = WorkList.end(); WI != E; ++WI)
114 if (*WI == I) {
115 WorkList.erase(WI);
116 --E;
117 --WI;
118 }
119
120 MadeChange = true;
121 ++DCEEliminated;
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000122 }
Chris Lattner92deeaf2002-05-07 04:24:11 +0000123 }
Chris Lattner92812822005-05-08 18:45:26 +0000124 return MadeChange;
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000125}
126
Brian Gaeke96d4bf72004-07-27 17:43:21 +0000127FunctionPass *llvm::createDeadCodeEliminationPass() {
Chris Lattner92deeaf2002-05-07 04:24:11 +0000128 return new DCE();
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000129}
Brian Gaeked0fde302003-11-11 22:41:34 +0000130