blob: 39f2506c1636866820de3bfdd5c08ce293e5e7d7 [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"
Chris Lattner3844c302004-07-22 08:00:28 +000029using namespace llvm;
30
Chris Lattner79a42ac2006-12-19 21:40:18 +000031STATISTIC(NumStores, "Number of stores deleted");
32STATISTIC(NumOther , "Number of other instrs removed");
Chris Lattner3844c302004-07-22 08:00:28 +000033
Chris Lattner79a42ac2006-12-19 21:40:18 +000034namespace {
Chris Lattner3844c302004-07-22 08:00:28 +000035 struct DSE : public FunctionPass {
36
37 virtual bool runOnFunction(Function &F) {
38 bool Changed = false;
39 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
40 Changed |= runOnBasicBlock(*I);
41 return Changed;
42 }
Misha Brukmanb1c93172005-04-21 23:48:37 +000043
Chris Lattner3844c302004-07-22 08:00:28 +000044 bool runOnBasicBlock(BasicBlock &BB);
Misha Brukmanb1c93172005-04-21 23:48:37 +000045
Chris Lattner7b25bcd2004-07-25 11:09:56 +000046 void DeleteDeadInstructionChains(Instruction *I,
47 SetVector<Instruction*> &DeadInsts);
Chris Lattner3844c302004-07-22 08:00:28 +000048
49 // getAnalysisUsage - We require post dominance frontiers (aka Control
50 // Dependence Graph)
51 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerbad64782004-07-24 07:51:27 +000052 AU.setPreservesCFG();
Chris Lattner3844c302004-07-22 08:00:28 +000053 AU.addRequired<TargetData>();
54 AU.addRequired<AliasAnalysis>();
55 AU.addPreserved<AliasAnalysis>();
56 }
57 };
Chris Lattnerc2d3d312006-08-27 22:42:52 +000058 RegisterPass<DSE> X("dse", "Dead Store Elimination");
Chris Lattner3844c302004-07-22 08:00:28 +000059}
60
Chris Lattner3e860842004-09-20 04:43:15 +000061FunctionPass *llvm::createDeadStoreEliminationPass() { return new DSE(); }
Chris Lattner3844c302004-07-22 08:00:28 +000062
63bool DSE::runOnBasicBlock(BasicBlock &BB) {
64 TargetData &TD = getAnalysis<TargetData>();
65 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
66 AliasSetTracker KillLocs(AA);
67
Chris Lattner6ea28882004-11-28 20:44:37 +000068 // If this block ends in a return, unwind, unreachable, and eventually
69 // tailcall, then all allocas are dead at its end.
Chris Lattner3844c302004-07-22 08:00:28 +000070 if (BB.getTerminator()->getNumSuccessors() == 0) {
Chris Lattnerf2980712004-07-26 06:14:11 +000071 BasicBlock *Entry = BB.getParent()->begin();
72 for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I)
Chris Lattner6ea28882004-11-28 20:44:37 +000073 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
74 unsigned Size = ~0U;
75 if (!AI->isArrayAllocation() &&
76 AI->getType()->getElementType()->isSized())
Chris Lattnerfdfe3e492005-01-08 19:42:22 +000077 Size = (unsigned)TD.getTypeSize(AI->getType()->getElementType());
Chris Lattner6ea28882004-11-28 20:44:37 +000078 KillLocs.add(AI, Size);
79 }
Chris Lattner3844c302004-07-22 08:00:28 +000080 }
81
Chris Lattner7b25bcd2004-07-25 11:09:56 +000082 // PotentiallyDeadInsts - Deleting dead stores from the program can make other
83 // instructions die if they were only used as operands to stores. Keep track
84 // of the operands to stores so that we can try deleting them at the end of
85 // the traversal.
86 SetVector<Instruction*> PotentiallyDeadInsts;
87
Chris Lattner3844c302004-07-22 08:00:28 +000088 bool MadeChange = false;
89 for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ) {
90 Instruction *I = --BBI; // Keep moving iterator backwards
Misha Brukmanb1c93172005-04-21 23:48:37 +000091
Chris Lattner7b25bcd2004-07-25 11:09:56 +000092 // If this is a free instruction, it makes the free'd location dead!
93 if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
Chris Lattner3844c302004-07-22 08:00:28 +000094 // Free instructions make any stores to the free'd location dead.
Chris Lattner7b25bcd2004-07-25 11:09:56 +000095 KillLocs.add(FI);
96 continue;
Chris Lattner3844c302004-07-22 08:00:28 +000097 }
Chris Lattner3844c302004-07-22 08:00:28 +000098
Chris Lattner7b25bcd2004-07-25 11:09:56 +000099 if (!isa<StoreInst>(I) || cast<StoreInst>(I)->isVolatile()) {
Chris Lattnerdc4ffef2005-11-30 19:38:22 +0000100 // If this is a vaarg instruction, it reads its operand. We don't model
101 // it correctly, so just conservatively remove all entries.
102 if (isa<VAArgInst>(I)) {
103 KillLocs.clear();
104 continue;
105 }
106
Chris Lattner3844c302004-07-22 08:00:28 +0000107 // If this is a non-store instruction, it makes everything referenced no
108 // longer killed. Remove anything aliased from the alias set tracker.
109 KillLocs.remove(I);
110 continue;
111 }
112
113 // If this is a non-volatile store instruction, and if it is already in
114 // the stored location is already in the tracker, then this is a dead
115 // store. We can just delete it here, but while we're at it, we also
116 // delete any trivially dead expression chains.
Chris Lattnerfdfe3e492005-01-08 19:42:22 +0000117 unsigned ValSize = (unsigned)TD.getTypeSize(I->getOperand(0)->getType());
Chris Lattner7b25bcd2004-07-25 11:09:56 +0000118 Value *Ptr = I->getOperand(1);
Chris Lattner4c1c1ac2004-07-25 07:58:38 +0000119
Chris Lattner3844c302004-07-22 08:00:28 +0000120 if (AliasSet *AS = KillLocs.getAliasSetForPointerIfExists(Ptr, ValSize))
121 for (AliasSet::iterator ASI = AS->begin(), E = AS->end(); ASI != E; ++ASI)
Chris Lattner13516fe2004-12-29 04:36:02 +0000122 if (ASI.getSize() >= ValSize && // Overwriting all of this store.
123 AA.alias(ASI.getPointer(), ASI.getSize(), Ptr, ValSize)
Chris Lattner3844c302004-07-22 08:00:28 +0000124 == AliasAnalysis::MustAlias) {
125 // If we found a must alias in the killed set, then this store really
Chris Lattner7b25bcd2004-07-25 11:09:56 +0000126 // is dead. Remember that the various operands of the store now have
127 // fewer users. At the end we will see if we can delete any values
128 // that are dead as part of the store becoming dead.
129 if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(0)))
130 PotentiallyDeadInsts.insert(Op);
131 if (Instruction *Op = dyn_cast<Instruction>(Ptr))
132 PotentiallyDeadInsts.insert(Op);
133
134 // Delete it now.
Chris Lattner3844c302004-07-22 08:00:28 +0000135 ++BBI; // Don't invalidate iterator.
Chris Lattner3844c302004-07-22 08:00:28 +0000136 BB.getInstList().erase(I); // Nuke the store!
137 ++NumStores;
Chris Lattner3844c302004-07-22 08:00:28 +0000138 MadeChange = true;
139 goto BigContinue;
140 }
141
142 // Otherwise, this is a non-dead store just add it to the set of dead
143 // locations.
Chris Lattner7b25bcd2004-07-25 11:09:56 +0000144 KillLocs.add(cast<StoreInst>(I));
Chris Lattner3844c302004-07-22 08:00:28 +0000145 BigContinue:;
146 }
Chris Lattner7b25bcd2004-07-25 11:09:56 +0000147
148 while (!PotentiallyDeadInsts.empty()) {
149 Instruction *I = PotentiallyDeadInsts.back();
150 PotentiallyDeadInsts.pop_back();
151 DeleteDeadInstructionChains(I, PotentiallyDeadInsts);
152 }
Chris Lattner3844c302004-07-22 08:00:28 +0000153 return MadeChange;
154}
155
Chris Lattner7b25bcd2004-07-25 11:09:56 +0000156void DSE::DeleteDeadInstructionChains(Instruction *I,
157 SetVector<Instruction*> &DeadInsts) {
158 // Instruction must be dead.
159 if (!I->use_empty() || !isInstructionTriviallyDead(I)) return;
Chris Lattner3844c302004-07-22 08:00:28 +0000160
Chris Lattner7b25bcd2004-07-25 11:09:56 +0000161 // Let the alias analysis know that we have nuked a value.
162 getAnalysis<AliasAnalysis>().deleteValue(I);
Chris Lattner3844c302004-07-22 08:00:28 +0000163
Chris Lattner7b25bcd2004-07-25 11:09:56 +0000164 // See if this made any operands dead. We do it this way in case the
165 // instruction uses the same operand twice. We don't want to delete a
166 // value then reference it.
Chris Lattnerd8e20182005-01-29 00:39:08 +0000167 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
168 if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i)))
169 DeadInsts.insert(Op); // Attempt to nuke it later.
170 I->setOperand(i, 0); // Drop from the operand list.
Chris Lattner7b25bcd2004-07-25 11:09:56 +0000171 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000172
Chris Lattnerd8e20182005-01-29 00:39:08 +0000173 I->eraseFromParent();
Chris Lattner7b25bcd2004-07-25 11:09:56 +0000174 ++NumOther;
Chris Lattner3844c302004-07-22 08:00:28 +0000175}