blob: 3b262a23091f5e5c271a33aedcd8dd29f97d4f39 [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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/ADT/Statistic.h"
Chandler Carruth83948572014-03-04 10:30:26 +000021#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Instruction.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000023#include "llvm/Pass.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000024#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Transforms/Utils/Local.h"
Chris Lattner49525f82004-01-09 06:02:20 +000026using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000027
Chandler Carruth964daaa2014-04-22 02:55:47 +000028#define DEBUG_TYPE "dce"
29
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 //
Chris Lattner2dd09db2009-09-02 06:11:42 +000037 struct DeadInstElimination : public BasicBlockPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000038 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000039 DeadInstElimination() : BasicBlockPass(ID) {
40 initializeDeadInstEliminationPass(*PassRegistry::getPassRegistry());
41 }
Craig Topper3e4c6972014-03-05 09:10:37 +000042 bool runOnBasicBlock(BasicBlock &BB) override {
Paul Robinsonaf4e64d2014-02-06 00:07:05 +000043 if (skipOptnoneFunction(BB))
44 return false;
Chandler Carruthb98f63d2015-01-15 10:41:28 +000045 auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
46 TargetLibraryInfo *TLI = TLIP ? &TLIP->getTLI() : nullptr;
Chris Lattner87e88062002-05-07 04:24:11 +000047 bool Changed = false;
Chris Lattnerc92fa422008-11-27 22:46:09 +000048 for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); ) {
49 Instruction *Inst = DI++;
Benjamin Kramer8bcc9712012-08-29 15:32:21 +000050 if (isInstructionTriviallyDead(Inst, TLI)) {
Chris Lattnerc92fa422008-11-27 22:46:09 +000051 Inst->eraseFromParent();
Chris Lattner87e88062002-05-07 04:24:11 +000052 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +000053 ++DIEEliminated;
Chris Lattnerc92fa422008-11-27 22:46:09 +000054 }
55 }
Chris Lattner87e88062002-05-07 04:24:11 +000056 return Changed;
57 }
58
Craig Topper3e4c6972014-03-05 09:10:37 +000059 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner820d9712002-10-21 20:00:28 +000060 AU.setPreservesCFG();
Chris Lattner87e88062002-05-07 04:24:11 +000061 }
62 };
Chris Lattner2f7c9632001-06-06 20:29:01 +000063}
64
Dan Gohmand78c4002008-05-13 00:00:25 +000065char DeadInstElimination::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +000066INITIALIZE_PASS(DeadInstElimination, "die",
Owen Andersondf7a4f22010-10-07 22:25:06 +000067 "Dead Instruction Elimination", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000068
Devang Patel5292e652007-01-25 23:23:25 +000069Pass *llvm::createDeadInstEliminationPass() {
Chris Lattner04805fa2002-02-26 21:46:54 +000070 return new DeadInstElimination();
Chris Lattnerccbd4e42002-01-23 05:48:24 +000071}
72
Chris Lattner87e88062002-05-07 04:24:11 +000073
Chris Lattner04805fa2002-02-26 21:46:54 +000074namespace {
Chris Lattner79a42ac2006-12-19 21:40:18 +000075 //===--------------------------------------------------------------------===//
76 // DeadCodeElimination pass implementation
77 //
Chris Lattner87e88062002-05-07 04:24:11 +000078 struct DCE : public FunctionPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000079 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000080 DCE() : FunctionPass(ID) {
81 initializeDCEPass(*PassRegistry::getPassRegistry());
82 }
Devang Patel09f162c2007-05-01 21:15:47 +000083
Craig Topper3e4c6972014-03-05 09:10:37 +000084 bool runOnFunction(Function &F) override;
Chris Lattner87e88062002-05-07 04:24:11 +000085
Craig Topper3e4c6972014-03-05 09:10:37 +000086 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner820d9712002-10-21 20:00:28 +000087 AU.setPreservesCFG();
Chris Lattner04805fa2002-02-26 21:46:54 +000088 }
Chris Lattner87e88062002-05-07 04:24:11 +000089 };
90}
91
Dan Gohmand78c4002008-05-13 00:00:25 +000092char DCE::ID = 0;
Owen Andersondf7a4f22010-10-07 22:25:06 +000093INITIALIZE_PASS(DCE, "dce", "Dead Code Elimination", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000094
Chris Lattner113f4f42002-06-25 16:13:24 +000095bool DCE::runOnFunction(Function &F) {
Paul Robinsonaf4e64d2014-02-06 00:07:05 +000096 if (skipOptnoneFunction(F))
97 return false;
98
Chandler Carruthb98f63d2015-01-15 10:41:28 +000099 auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
100 TargetLibraryInfo *TLI = TLIP ? &TLIP->getTLI() : nullptr;
Benjamin Kramer8bcc9712012-08-29 15:32:21 +0000101
Chris Lattner87e88062002-05-07 04:24:11 +0000102 // Start out with all of the instructions in the worklist...
Chris Lattner2d3a7a62004-04-27 15:13:33 +0000103 std::vector<Instruction*> WorkList;
Chris Lattner49221182005-05-08 18:45:26 +0000104 for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i)
105 WorkList.push_back(&*i);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000106
Chris Lattner87e88062002-05-07 04:24:11 +0000107 // Loop over the worklist finding instructions that are dead. If they are
108 // dead make them drop all of their uses, making other instructions
109 // potentially dead, and work until the worklist is empty.
110 //
Chris Lattner49221182005-05-08 18:45:26 +0000111 bool MadeChange = false;
Chris Lattner87e88062002-05-07 04:24:11 +0000112 while (!WorkList.empty()) {
113 Instruction *I = WorkList.back();
114 WorkList.pop_back();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000115
Benjamin Kramer8bcc9712012-08-29 15:32:21 +0000116 if (isInstructionTriviallyDead(I, TLI)) { // If the instruction is dead.
Chris Lattner87e88062002-05-07 04:24:11 +0000117 // Loop over all of the values that the instruction uses, if there are
118 // instructions being used, add them to the worklist, because they might
119 // go dead after this one is removed.
120 //
Chris Lattnerfb9a2992004-04-21 22:29:37 +0000121 for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI)
122 if (Instruction *Used = dyn_cast<Instruction>(*OI))
Chris Lattner87e88062002-05-07 04:24:11 +0000123 WorkList.push_back(Used);
124
Chris Lattner49221182005-05-08 18:45:26 +0000125 // Remove the instruction.
126 I->eraseFromParent();
Chris Lattner87e88062002-05-07 04:24:11 +0000127
Chris Lattner49221182005-05-08 18:45:26 +0000128 // Remove the instruction from the worklist if it still exists in it.
Jakub Staszak25dcab12012-10-16 19:39:40 +0000129 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I),
130 WorkList.end());
Chris Lattner49221182005-05-08 18:45:26 +0000131
132 MadeChange = true;
133 ++DCEEliminated;
Chris Lattner04805fa2002-02-26 21:46:54 +0000134 }
Chris Lattner87e88062002-05-07 04:24:11 +0000135 }
Chris Lattner49221182005-05-08 18:45:26 +0000136 return MadeChange;
Chris Lattner04805fa2002-02-26 21:46:54 +0000137}
138
Brian Gaeke38b79e82004-07-27 17:43:21 +0000139FunctionPass *llvm::createDeadCodeEliminationPass() {
Chris Lattner87e88062002-05-07 04:24:11 +0000140 return new DCE();
Chris Lattner04805fa2002-02-26 21:46:54 +0000141}
Brian Gaeke960707c2003-11-11 22:41:34 +0000142