blob: b67c3c7742fd7d37bbf53b9c6e603c7880f0cd91 [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 Lattnerb4cfa7f2002-05-07 20:03:00 +000019#include "llvm/Transforms/Scalar.h"
Fiona Glaserb0c6d912015-09-30 17:49:49 +000020#include "llvm/ADT/SetVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/ADT/Statistic.h"
Chandler Carruth83948572014-03-04 10:30:26 +000022#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Instruction.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000024#include "llvm/Pass.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000025#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/Transforms/Utils/Local.h"
Chris Lattner49525f82004-01-09 06:02:20 +000027using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000028
Chandler Carruth964daaa2014-04-22 02:55:47 +000029#define DEBUG_TYPE "dce"
30
Chris Lattner79a42ac2006-12-19 21:40:18 +000031STATISTIC(DIEEliminated, "Number of insts removed by DIE pass");
32STATISTIC(DCEEliminated, "Number of insts removed");
Chris Lattnerbf3a0992002-10-01 22:38:41 +000033
Chris Lattner79a42ac2006-12-19 21:40:18 +000034namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000035 //===--------------------------------------------------------------------===//
36 // DeadInstElimination pass implementation
37 //
Chris Lattner2dd09db2009-09-02 06:11:42 +000038 struct DeadInstElimination : public BasicBlockPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000039 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000040 DeadInstElimination() : BasicBlockPass(ID) {
41 initializeDeadInstEliminationPass(*PassRegistry::getPassRegistry());
42 }
Craig Topper3e4c6972014-03-05 09:10:37 +000043 bool runOnBasicBlock(BasicBlock &BB) override {
Vedant Kumar6013f452016-04-22 06:51:37 +000044 if (skipOptnoneFunction(BB))
Paul Robinsonaf4e64d2014-02-06 00:07:05 +000045 return false;
Chandler Carruthb98f63d2015-01-15 10:41:28 +000046 auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
47 TargetLibraryInfo *TLI = TLIP ? &TLIP->getTLI() : nullptr;
Chris Lattner87e88062002-05-07 04:24:11 +000048 bool Changed = false;
Chris Lattnerc92fa422008-11-27 22:46:09 +000049 for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); ) {
Duncan P. N. Exon Smith3a9c9e32015-10-13 18:26:00 +000050 Instruction *Inst = &*DI++;
Benjamin Kramer8bcc9712012-08-29 15:32:21 +000051 if (isInstructionTriviallyDead(Inst, TLI)) {
Chris Lattnerc92fa422008-11-27 22:46:09 +000052 Inst->eraseFromParent();
Chris Lattner87e88062002-05-07 04:24:11 +000053 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +000054 ++DIEEliminated;
Chris Lattnerc92fa422008-11-27 22:46:09 +000055 }
56 }
Chris Lattner87e88062002-05-07 04:24:11 +000057 return Changed;
58 }
59
Craig Topper3e4c6972014-03-05 09:10:37 +000060 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner820d9712002-10-21 20:00:28 +000061 AU.setPreservesCFG();
Chris Lattner87e88062002-05-07 04:24:11 +000062 }
63 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000064}
Chris Lattner2f7c9632001-06-06 20:29:01 +000065
Dan Gohmand78c4002008-05-13 00:00:25 +000066char DeadInstElimination::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +000067INITIALIZE_PASS(DeadInstElimination, "die",
Owen Andersondf7a4f22010-10-07 22:25:06 +000068 "Dead Instruction Elimination", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000069
Devang Patel5292e652007-01-25 23:23:25 +000070Pass *llvm::createDeadInstEliminationPass() {
Chris Lattner04805fa2002-02-26 21:46:54 +000071 return new DeadInstElimination();
Chris Lattnerccbd4e42002-01-23 05:48:24 +000072}
73
Chris Lattner87e88062002-05-07 04:24:11 +000074
Chris Lattner04805fa2002-02-26 21:46:54 +000075namespace {
Chris Lattner79a42ac2006-12-19 21:40:18 +000076 //===--------------------------------------------------------------------===//
77 // DeadCodeElimination pass implementation
78 //
Chris Lattner87e88062002-05-07 04:24:11 +000079 struct DCE : public FunctionPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000080 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000081 DCE() : FunctionPass(ID) {
82 initializeDCEPass(*PassRegistry::getPassRegistry());
83 }
Devang Patel09f162c2007-05-01 21:15:47 +000084
Craig Topper3e4c6972014-03-05 09:10:37 +000085 bool runOnFunction(Function &F) override;
Chris Lattner87e88062002-05-07 04:24:11 +000086
Craig Topper3e4c6972014-03-05 09:10:37 +000087 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner820d9712002-10-21 20:00:28 +000088 AU.setPreservesCFG();
Chris Lattner04805fa2002-02-26 21:46:54 +000089 }
Chris Lattner87e88062002-05-07 04:24:11 +000090 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000091}
Chris Lattner87e88062002-05-07 04:24:11 +000092
Dan Gohmand78c4002008-05-13 00:00:25 +000093char DCE::ID = 0;
Owen Andersondf7a4f22010-10-07 22:25:06 +000094INITIALIZE_PASS(DCE, "dce", "Dead Code Elimination", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000095
Fiona Glaserb0c6d912015-09-30 17:49:49 +000096static bool DCEInstruction(Instruction *I,
97 SmallSetVector<Instruction *, 16> &WorkList,
98 const TargetLibraryInfo *TLI) {
99 if (isInstructionTriviallyDead(I, TLI)) {
100 // Null out all of the instruction's operands to see if any operand becomes
101 // dead as we go.
102 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
103 Value *OpV = I->getOperand(i);
104 I->setOperand(i, nullptr);
105
106 if (!OpV->use_empty() || I == OpV)
107 continue;
108
109 // If the operand is an instruction that became dead as we nulled out the
110 // operand, and if it is 'trivially' dead, delete it in a future loop
111 // iteration.
112 if (Instruction *OpI = dyn_cast<Instruction>(OpV))
113 if (isInstructionTriviallyDead(OpI, TLI))
114 WorkList.insert(OpI);
115 }
116
117 I->eraseFromParent();
118 ++DCEEliminated;
119 return true;
120 }
121 return false;
122}
123
Chris Lattner113f4f42002-06-25 16:13:24 +0000124bool DCE::runOnFunction(Function &F) {
Vedant Kumar6013f452016-04-22 06:51:37 +0000125 if (skipOptnoneFunction(F))
Paul Robinsonaf4e64d2014-02-06 00:07:05 +0000126 return false;
127
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000128 auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
129 TargetLibraryInfo *TLI = TLIP ? &TLIP->getTLI() : nullptr;
Benjamin Kramer8bcc9712012-08-29 15:32:21 +0000130
Chris Lattner49221182005-05-08 18:45:26 +0000131 bool MadeChange = false;
Fiona Glaserb0c6d912015-09-30 17:49:49 +0000132 SmallSetVector<Instruction *, 16> WorkList;
133 // Iterate over the original function, only adding insts to the worklist
134 // if they actually need to be revisited. This avoids having to pre-init
135 // the worklist with the entire function's worth of instructions.
136 for (inst_iterator FI = inst_begin(F), FE = inst_end(F); FI != FE;) {
137 Instruction *I = &*FI;
138 ++FI;
139
140 // We're visiting this instruction now, so make sure it's not in the
141 // worklist from an earlier visit.
142 if (!WorkList.count(I))
143 MadeChange |= DCEInstruction(I, WorkList, TLI);
144 }
145
Chris Lattner87e88062002-05-07 04:24:11 +0000146 while (!WorkList.empty()) {
Fiona Glaserb0c6d912015-09-30 17:49:49 +0000147 Instruction *I = WorkList.pop_back_val();
148 MadeChange |= DCEInstruction(I, WorkList, TLI);
Chris Lattner87e88062002-05-07 04:24:11 +0000149 }
Chris Lattner49221182005-05-08 18:45:26 +0000150 return MadeChange;
Chris Lattner04805fa2002-02-26 21:46:54 +0000151}
152
Brian Gaeke38b79e82004-07-27 17:43:21 +0000153FunctionPass *llvm::createDeadCodeEliminationPass() {
Chris Lattner87e88062002-05-07 04:24:11 +0000154 return new DCE();
Chris Lattner04805fa2002-02-26 21:46:54 +0000155}
Brian Gaeke960707c2003-11-11 22:41:34 +0000156