blob: e8a090af40c316f1d3d09b53d00b926f67b633aa [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000021#include "llvm/ADT/Statistic.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000022#include "llvm/IR/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"
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +000025#include "llvm/Target/TargetLibraryInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000026#include "llvm/Transforms/Utils/Local.h"
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
Owen Anderson081c34b2010-10-19 17:21:58 +000038 DeadInstElimination() : BasicBlockPass(ID) {
39 initializeDeadInstEliminationPass(*PassRegistry::getPassRegistry());
40 }
Chris Lattner7e708292002-06-25 16:13:24 +000041 virtual bool runOnBasicBlock(BasicBlock &BB) {
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +000042 TargetLibraryInfo *TLI = getAnalysisIfAvailable<TargetLibraryInfo>();
Chris Lattner92deeaf2002-05-07 04:24:11 +000043 bool Changed = false;
Chris Lattnercb03f852008-11-27 22:46:09 +000044 for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); ) {
45 Instruction *Inst = DI++;
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +000046 if (isInstructionTriviallyDead(Inst, TLI)) {
Chris Lattnercb03f852008-11-27 22:46:09 +000047 Inst->eraseFromParent();
Chris Lattner92deeaf2002-05-07 04:24:11 +000048 Changed = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +000049 ++DIEEliminated;
Chris Lattnercb03f852008-11-27 22:46:09 +000050 }
51 }
Chris Lattner92deeaf2002-05-07 04:24:11 +000052 return Changed;
53 }
54
55 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercb2610e2002-10-21 20:00:28 +000056 AU.setPreservesCFG();
Chris Lattner92deeaf2002-05-07 04:24:11 +000057 }
58 };
Chris Lattner00950542001-06-06 20:29:01 +000059}
60
Dan Gohman844731a2008-05-13 00:00:25 +000061char DeadInstElimination::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000062INITIALIZE_PASS(DeadInstElimination, "die",
Owen Andersonce665bd2010-10-07 22:25:06 +000063 "Dead Instruction Elimination", false, false)
Dan Gohman844731a2008-05-13 00:00:25 +000064
Devang Patel4d447f52007-01-25 23:23:25 +000065Pass *llvm::createDeadInstEliminationPass() {
Chris Lattnerbd0ef772002-02-26 21:46:54 +000066 return new DeadInstElimination();
Chris Lattnerc560f882002-01-23 05:48:24 +000067}
68
Chris Lattner92deeaf2002-05-07 04:24:11 +000069
Chris Lattnerbd0ef772002-02-26 21:46:54 +000070namespace {
Chris Lattner0e5f4992006-12-19 21:40:18 +000071 //===--------------------------------------------------------------------===//
72 // DeadCodeElimination pass implementation
73 //
Chris Lattner92deeaf2002-05-07 04:24:11 +000074 struct DCE : public FunctionPass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +000075 static char ID; // Pass identification, replacement for typeid
Owen Anderson081c34b2010-10-19 17:21:58 +000076 DCE() : FunctionPass(ID) {
77 initializeDCEPass(*PassRegistry::getPassRegistry());
78 }
Devang Patel794fd752007-05-01 21:15:47 +000079
Chris Lattner7e708292002-06-25 16:13:24 +000080 virtual bool runOnFunction(Function &F);
Chris Lattner92deeaf2002-05-07 04:24:11 +000081
82 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercb2610e2002-10-21 20:00:28 +000083 AU.setPreservesCFG();
Chris Lattnerbd0ef772002-02-26 21:46:54 +000084 }
Chris Lattner92deeaf2002-05-07 04:24:11 +000085 };
86}
87
Dan Gohman844731a2008-05-13 00:00:25 +000088char DCE::ID = 0;
Owen Andersonce665bd2010-10-07 22:25:06 +000089INITIALIZE_PASS(DCE, "dce", "Dead Code Elimination", false, false)
Dan Gohman844731a2008-05-13 00:00:25 +000090
Chris Lattner7e708292002-06-25 16:13:24 +000091bool DCE::runOnFunction(Function &F) {
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +000092 TargetLibraryInfo *TLI = getAnalysisIfAvailable<TargetLibraryInfo>();
93
Chris Lattner92deeaf2002-05-07 04:24:11 +000094 // Start out with all of the instructions in the worklist...
Chris Lattner6ffe5512004-04-27 15:13:33 +000095 std::vector<Instruction*> WorkList;
Chris Lattner92812822005-05-08 18:45:26 +000096 for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i)
97 WorkList.push_back(&*i);
Misha Brukmanfd939082005-04-21 23:48:37 +000098
Chris Lattner92deeaf2002-05-07 04:24:11 +000099 // Loop over the worklist finding instructions that are dead. If they are
100 // dead make them drop all of their uses, making other instructions
101 // potentially dead, and work until the worklist is empty.
102 //
Chris Lattner92812822005-05-08 18:45:26 +0000103 bool MadeChange = false;
Chris Lattner92deeaf2002-05-07 04:24:11 +0000104 while (!WorkList.empty()) {
105 Instruction *I = WorkList.back();
106 WorkList.pop_back();
Misha Brukmanfd939082005-04-21 23:48:37 +0000107
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000108 if (isInstructionTriviallyDead(I, TLI)) { // If the instruction is dead.
Chris Lattner92deeaf2002-05-07 04:24:11 +0000109 // Loop over all of the values that the instruction uses, if there are
110 // instructions being used, add them to the worklist, because they might
111 // go dead after this one is removed.
112 //
Chris Lattner4e5f0362004-04-21 22:29:37 +0000113 for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI)
114 if (Instruction *Used = dyn_cast<Instruction>(*OI))
Chris Lattner92deeaf2002-05-07 04:24:11 +0000115 WorkList.push_back(Used);
116
Chris Lattner92812822005-05-08 18:45:26 +0000117 // Remove the instruction.
118 I->eraseFromParent();
Chris Lattner92deeaf2002-05-07 04:24:11 +0000119
Chris Lattner92812822005-05-08 18:45:26 +0000120 // Remove the instruction from the worklist if it still exists in it.
Jakub Staszak59de6222012-10-16 19:39:40 +0000121 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I),
122 WorkList.end());
Chris Lattner92812822005-05-08 18:45:26 +0000123
124 MadeChange = true;
125 ++DCEEliminated;
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000126 }
Chris Lattner92deeaf2002-05-07 04:24:11 +0000127 }
Chris Lattner92812822005-05-08 18:45:26 +0000128 return MadeChange;
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000129}
130
Brian Gaeke96d4bf72004-07-27 17:43:21 +0000131FunctionPass *llvm::createDeadCodeEliminationPass() {
Chris Lattner92deeaf2002-05-07 04:24:11 +0000132 return new DCE();
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000133}
Brian Gaeked0fde302003-11-11 22:41:34 +0000134