blob: 6baeceb5ee213edb49f141fba119de74c8a050b7 [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//
Chris Lattner4ee451d2007-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 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"
Chris Lattner92deeaf2002-05-07 04:24:11 +000024#include "llvm/Support/InstIterator.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000025#include "llvm/ADT/Statistic.h"
Chris Lattner92deeaf2002-05-07 04:24:11 +000026#include <set>
Chris Lattnerd7456022004-01-09 06:02:20 +000027using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Chris Lattner0e5f4992006-12-19 21:40:18 +000029STATISTIC(DIEEliminated, "Number of insts removed by DIE pass");
30STATISTIC(DCEEliminated, "Number of insts removed");
Chris Lattnera92f6962002-10-01 22:38:41 +000031
Chris Lattner0e5f4992006-12-19 21:40:18 +000032namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000033 //===--------------------------------------------------------------------===//
34 // DeadInstElimination pass implementation
35 //
Chris Lattner3e8b6632009-09-02 06:11:42 +000036 struct DeadInstElimination : public BasicBlockPass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +000037 static char ID; // Pass identification, replacement for typeid
Dan Gohman865f0062009-02-18 05:09:16 +000038 DeadInstElimination() : BasicBlockPass(&ID) {}
Chris Lattner7e708292002-06-25 16:13:24 +000039 virtual bool runOnBasicBlock(BasicBlock &BB) {
Chris Lattner92deeaf2002-05-07 04:24:11 +000040 bool Changed = false;
Chris Lattnercb03f852008-11-27 22:46:09 +000041 for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); ) {
42 Instruction *Inst = DI++;
43 if (isInstructionTriviallyDead(Inst)) {
44 Inst->eraseFromParent();
Chris Lattner92deeaf2002-05-07 04:24:11 +000045 Changed = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +000046 ++DIEEliminated;
Chris Lattnercb03f852008-11-27 22:46:09 +000047 }
48 }
Chris Lattner92deeaf2002-05-07 04:24:11 +000049 return Changed;
50 }
51
52 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercb2610e2002-10-21 20:00:28 +000053 AU.setPreservesCFG();
Chris Lattner92deeaf2002-05-07 04:24:11 +000054 }
55 };
Chris Lattner00950542001-06-06 20:29:01 +000056}
57
Dan Gohman844731a2008-05-13 00:00:25 +000058char DeadInstElimination::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000059INITIALIZE_PASS(DeadInstElimination, "die",
60 "Dead Instruction Elimination", false, false);
Dan Gohman844731a2008-05-13 00:00:25 +000061
Devang Patel4d447f52007-01-25 23:23:25 +000062Pass *llvm::createDeadInstEliminationPass() {
Chris Lattnerbd0ef772002-02-26 21:46:54 +000063 return new DeadInstElimination();
Chris Lattnerc560f882002-01-23 05:48:24 +000064}
65
Chris Lattner92deeaf2002-05-07 04:24:11 +000066
Chris Lattnerbd0ef772002-02-26 21:46:54 +000067namespace {
Chris Lattner0e5f4992006-12-19 21:40:18 +000068 //===--------------------------------------------------------------------===//
69 // DeadCodeElimination pass implementation
70 //
Chris Lattner92deeaf2002-05-07 04:24:11 +000071 struct DCE : public FunctionPass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +000072 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000073 DCE() : FunctionPass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000074
Chris Lattner7e708292002-06-25 16:13:24 +000075 virtual bool runOnFunction(Function &F);
Chris Lattner92deeaf2002-05-07 04:24:11 +000076
77 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercb2610e2002-10-21 20:00:28 +000078 AU.setPreservesCFG();
Chris Lattnerbd0ef772002-02-26 21:46:54 +000079 }
Chris Lattner92deeaf2002-05-07 04:24:11 +000080 };
81}
82
Dan Gohman844731a2008-05-13 00:00:25 +000083char DCE::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000084INITIALIZE_PASS(DCE, "dce", "Dead Code Elimination", false, false);
Dan Gohman844731a2008-05-13 00:00:25 +000085
Chris Lattner7e708292002-06-25 16:13:24 +000086bool DCE::runOnFunction(Function &F) {
Chris Lattner92deeaf2002-05-07 04:24:11 +000087 // Start out with all of the instructions in the worklist...
Chris Lattner6ffe5512004-04-27 15:13:33 +000088 std::vector<Instruction*> WorkList;
Chris Lattner92812822005-05-08 18:45:26 +000089 for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i)
90 WorkList.push_back(&*i);
Misha Brukmanfd939082005-04-21 23:48:37 +000091
Chris Lattner92deeaf2002-05-07 04:24:11 +000092 // Loop over the worklist finding instructions that are dead. If they are
93 // dead make them drop all of their uses, making other instructions
94 // potentially dead, and work until the worklist is empty.
95 //
Chris Lattner92812822005-05-08 18:45:26 +000096 bool MadeChange = false;
Chris Lattner92deeaf2002-05-07 04:24:11 +000097 while (!WorkList.empty()) {
98 Instruction *I = WorkList.back();
99 WorkList.pop_back();
Misha Brukmanfd939082005-04-21 23:48:37 +0000100
Chris Lattner92812822005-05-08 18:45:26 +0000101 if (isInstructionTriviallyDead(I)) { // If the instruction is dead.
Chris Lattner92deeaf2002-05-07 04:24:11 +0000102 // Loop over all of the values that the instruction uses, if there are
103 // instructions being used, add them to the worklist, because they might
104 // go dead after this one is removed.
105 //
Chris Lattner4e5f0362004-04-21 22:29:37 +0000106 for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI)
107 if (Instruction *Used = dyn_cast<Instruction>(*OI))
Chris Lattner92deeaf2002-05-07 04:24:11 +0000108 WorkList.push_back(Used);
109
Chris Lattner92812822005-05-08 18:45:26 +0000110 // Remove the instruction.
111 I->eraseFromParent();
Chris Lattner92deeaf2002-05-07 04:24:11 +0000112
Chris Lattner92812822005-05-08 18:45:26 +0000113 // Remove the instruction from the worklist if it still exists in it.
David Greene08d5fd92007-12-17 17:39:51 +0000114 for (std::vector<Instruction*>::iterator WI = WorkList.begin();
Bill Wendling670ed092008-09-18 23:04:18 +0000115 WI != WorkList.end(); ) {
116 if (*WI == I)
David Greene08d5fd92007-12-17 17:39:51 +0000117 WI = WorkList.erase(WI);
Bill Wendling670ed092008-09-18 23:04:18 +0000118 else
119 ++WI;
120 }
Chris Lattner92812822005-05-08 18:45:26 +0000121
122 MadeChange = true;
123 ++DCEEliminated;
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000124 }
Chris Lattner92deeaf2002-05-07 04:24:11 +0000125 }
Chris Lattner92812822005-05-08 18:45:26 +0000126 return MadeChange;
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000127}
128
Brian Gaeke96d4bf72004-07-27 17:43:21 +0000129FunctionPass *llvm::createDeadCodeEliminationPass() {
Chris Lattner92deeaf2002-05-07 04:24:11 +0000130 return new DCE();
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000131}
Brian Gaeked0fde302003-11-11 22:41:34 +0000132