blob: 9ebb9b4974be60851a0885979d7229300f003ab9 [file] [log] [blame]
Chris Lattner3844c302004-07-22 08:00:28 +00001//===- DeadStoreElimination.cpp - Dead Store Elimination ------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Chris Lattner3844c302004-07-22 08:00:28 +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 Brukmanb1c93172005-04-21 23:48:37 +00007//
Chris Lattner3844c302004-07-22 08:00:28 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements a trivial dead store elimination that only considers
11// basic-block local redundant stores.
12//
13// FIXME: This should eventually be extended to be a post-dominator tree
14// traversal. Doing so would be pretty trivial.
15//
16//===----------------------------------------------------------------------===//
17
Chris Lattner79a42ac2006-12-19 21:40:18 +000018#define DEBUG_TYPE "dse"
Chris Lattner3844c302004-07-22 08:00:28 +000019#include "llvm/Transforms/Scalar.h"
Chris Lattner6ea28882004-11-28 20:44:37 +000020#include "llvm/DerivedTypes.h"
Chris Lattner3844c302004-07-22 08:00:28 +000021#include "llvm/Function.h"
22#include "llvm/Instructions.h"
23#include "llvm/Analysis/AliasAnalysis.h"
24#include "llvm/Analysis/AliasSetTracker.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Transforms/Utils/Local.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000027#include "llvm/ADT/SetVector.h"
28#include "llvm/ADT/Statistic.h"
Reid Spencer557ab152007-02-05 23:32:05 +000029#include "llvm/Support/Compiler.h"
Chris Lattner3844c302004-07-22 08:00:28 +000030using namespace llvm;
31
Chris Lattner79a42ac2006-12-19 21:40:18 +000032STATISTIC(NumStores, "Number of stores deleted");
33STATISTIC(NumOther , "Number of other instrs removed");
Chris Lattner3844c302004-07-22 08:00:28 +000034
Chris Lattner79a42ac2006-12-19 21:40:18 +000035namespace {
Reid Spencer557ab152007-02-05 23:32:05 +000036 struct VISIBILITY_HIDDEN DSE : public FunctionPass {
Chris Lattner3844c302004-07-22 08:00:28 +000037
38 virtual bool runOnFunction(Function &F) {
39 bool Changed = false;
40 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
41 Changed |= runOnBasicBlock(*I);
42 return Changed;
43 }
Misha Brukmanb1c93172005-04-21 23:48:37 +000044
Chris Lattner3844c302004-07-22 08:00:28 +000045 bool runOnBasicBlock(BasicBlock &BB);
Misha Brukmanb1c93172005-04-21 23:48:37 +000046
Chris Lattner7b25bcd2004-07-25 11:09:56 +000047 void DeleteDeadInstructionChains(Instruction *I,
48 SetVector<Instruction*> &DeadInsts);
Chris Lattner3844c302004-07-22 08:00:28 +000049
50 // getAnalysisUsage - We require post dominance frontiers (aka Control
51 // Dependence Graph)
52 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerbad64782004-07-24 07:51:27 +000053 AU.setPreservesCFG();
Chris Lattner3844c302004-07-22 08:00:28 +000054 AU.addRequired<TargetData>();
55 AU.addRequired<AliasAnalysis>();
56 AU.addPreserved<AliasAnalysis>();
57 }
58 };
Chris Lattnerc2d3d312006-08-27 22:42:52 +000059 RegisterPass<DSE> X("dse", "Dead Store Elimination");
Chris Lattner3844c302004-07-22 08:00:28 +000060}
61
Chris Lattner3e860842004-09-20 04:43:15 +000062FunctionPass *llvm::createDeadStoreEliminationPass() { return new DSE(); }
Chris Lattner3844c302004-07-22 08:00:28 +000063
64bool DSE::runOnBasicBlock(BasicBlock &BB) {
65 TargetData &TD = getAnalysis<TargetData>();
66 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
67 AliasSetTracker KillLocs(AA);
68
Chris Lattner6ea28882004-11-28 20:44:37 +000069 // If this block ends in a return, unwind, unreachable, and eventually
70 // tailcall, then all allocas are dead at its end.
Chris Lattner3844c302004-07-22 08:00:28 +000071 if (BB.getTerminator()->getNumSuccessors() == 0) {
Chris Lattnerf2980712004-07-26 06:14:11 +000072 BasicBlock *Entry = BB.getParent()->begin();
73 for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I)
Chris Lattner6ea28882004-11-28 20:44:37 +000074 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
75 unsigned Size = ~0U;
76 if (!AI->isArrayAllocation() &&
77 AI->getType()->getElementType()->isSized())
Chris Lattnerfdfe3e492005-01-08 19:42:22 +000078 Size = (unsigned)TD.getTypeSize(AI->getType()->getElementType());
Chris Lattner6ea28882004-11-28 20:44:37 +000079 KillLocs.add(AI, Size);
80 }
Chris Lattner3844c302004-07-22 08:00:28 +000081 }
82
Chris Lattner7b25bcd2004-07-25 11:09:56 +000083 // PotentiallyDeadInsts - Deleting dead stores from the program can make other
84 // instructions die if they were only used as operands to stores. Keep track
85 // of the operands to stores so that we can try deleting them at the end of
86 // the traversal.
87 SetVector<Instruction*> PotentiallyDeadInsts;
88
Chris Lattner3844c302004-07-22 08:00:28 +000089 bool MadeChange = false;
90 for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ) {
91 Instruction *I = --BBI; // Keep moving iterator backwards
Misha Brukmanb1c93172005-04-21 23:48:37 +000092
Chris Lattner7b25bcd2004-07-25 11:09:56 +000093 // If this is a free instruction, it makes the free'd location dead!
94 if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
Chris Lattner3844c302004-07-22 08:00:28 +000095 // Free instructions make any stores to the free'd location dead.
Chris Lattner7b25bcd2004-07-25 11:09:56 +000096 KillLocs.add(FI);
97 continue;
Chris Lattner3844c302004-07-22 08:00:28 +000098 }
Chris Lattner3844c302004-07-22 08:00:28 +000099
Chris Lattner7b25bcd2004-07-25 11:09:56 +0000100 if (!isa<StoreInst>(I) || cast<StoreInst>(I)->isVolatile()) {
Chris Lattnerdc4ffef2005-11-30 19:38:22 +0000101 // If this is a vaarg instruction, it reads its operand. We don't model
102 // it correctly, so just conservatively remove all entries.
103 if (isa<VAArgInst>(I)) {
104 KillLocs.clear();
105 continue;
106 }
107
Chris Lattner3844c302004-07-22 08:00:28 +0000108 // If this is a non-store instruction, it makes everything referenced no
109 // longer killed. Remove anything aliased from the alias set tracker.
110 KillLocs.remove(I);
111 continue;
112 }
113
114 // If this is a non-volatile store instruction, and if it is already in
115 // the stored location is already in the tracker, then this is a dead
116 // store. We can just delete it here, but while we're at it, we also
117 // delete any trivially dead expression chains.
Chris Lattnerfdfe3e492005-01-08 19:42:22 +0000118 unsigned ValSize = (unsigned)TD.getTypeSize(I->getOperand(0)->getType());
Chris Lattner7b25bcd2004-07-25 11:09:56 +0000119 Value *Ptr = I->getOperand(1);
Chris Lattner4c1c1ac2004-07-25 07:58:38 +0000120
Chris Lattner3844c302004-07-22 08:00:28 +0000121 if (AliasSet *AS = KillLocs.getAliasSetForPointerIfExists(Ptr, ValSize))
122 for (AliasSet::iterator ASI = AS->begin(), E = AS->end(); ASI != E; ++ASI)
Chris Lattner13516fe2004-12-29 04:36:02 +0000123 if (ASI.getSize() >= ValSize && // Overwriting all of this store.
124 AA.alias(ASI.getPointer(), ASI.getSize(), Ptr, ValSize)
Chris Lattner3844c302004-07-22 08:00:28 +0000125 == AliasAnalysis::MustAlias) {
126 // If we found a must alias in the killed set, then this store really
Chris Lattner7b25bcd2004-07-25 11:09:56 +0000127 // is dead. Remember that the various operands of the store now have
128 // fewer users. At the end we will see if we can delete any values
129 // that are dead as part of the store becoming dead.
130 if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(0)))
131 PotentiallyDeadInsts.insert(Op);
132 if (Instruction *Op = dyn_cast<Instruction>(Ptr))
133 PotentiallyDeadInsts.insert(Op);
134
135 // Delete it now.
Chris Lattner3844c302004-07-22 08:00:28 +0000136 ++BBI; // Don't invalidate iterator.
Chris Lattner3844c302004-07-22 08:00:28 +0000137 BB.getInstList().erase(I); // Nuke the store!
138 ++NumStores;
Chris Lattner3844c302004-07-22 08:00:28 +0000139 MadeChange = true;
140 goto BigContinue;
141 }
142
143 // Otherwise, this is a non-dead store just add it to the set of dead
144 // locations.
Chris Lattner7b25bcd2004-07-25 11:09:56 +0000145 KillLocs.add(cast<StoreInst>(I));
Chris Lattner3844c302004-07-22 08:00:28 +0000146 BigContinue:;
147 }
Chris Lattner7b25bcd2004-07-25 11:09:56 +0000148
149 while (!PotentiallyDeadInsts.empty()) {
150 Instruction *I = PotentiallyDeadInsts.back();
151 PotentiallyDeadInsts.pop_back();
152 DeleteDeadInstructionChains(I, PotentiallyDeadInsts);
153 }
Chris Lattner3844c302004-07-22 08:00:28 +0000154 return MadeChange;
155}
156
Chris Lattner7b25bcd2004-07-25 11:09:56 +0000157void DSE::DeleteDeadInstructionChains(Instruction *I,
158 SetVector<Instruction*> &DeadInsts) {
159 // Instruction must be dead.
160 if (!I->use_empty() || !isInstructionTriviallyDead(I)) return;
Chris Lattner3844c302004-07-22 08:00:28 +0000161
Chris Lattner7b25bcd2004-07-25 11:09:56 +0000162 // Let the alias analysis know that we have nuked a value.
163 getAnalysis<AliasAnalysis>().deleteValue(I);
Chris Lattner3844c302004-07-22 08:00:28 +0000164
Chris Lattner7b25bcd2004-07-25 11:09:56 +0000165 // See if this made any operands dead. We do it this way in case the
166 // instruction uses the same operand twice. We don't want to delete a
167 // value then reference it.
Chris Lattnerd8e20182005-01-29 00:39:08 +0000168 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
169 if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i)))
170 DeadInsts.insert(Op); // Attempt to nuke it later.
171 I->setOperand(i, 0); // Drop from the operand list.
Chris Lattner7b25bcd2004-07-25 11:09:56 +0000172 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000173
Chris Lattnerd8e20182005-01-29 00:39:08 +0000174 I->eraseFromParent();
Chris Lattner7b25bcd2004-07-25 11:09:56 +0000175 ++NumOther;
Chris Lattner3844c302004-07-22 08:00:28 +0000176}