blob: 02ca53c7aa9e57a6c54bfa56660bee66128477bd [file] [log] [blame]
Owen Anderson78e02f72007-07-06 23:14:35 +00001//===- MemoryDependenceAnalysis.cpp - Mem Deps Implementation --*- C++ -*-===//
2//
3// 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.
Owen Anderson78e02f72007-07-06 23:14:35 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements an analysis that determines, for a given memory
11// operation, what preceding memory operations it depends on. It builds on
Owen Anderson80b1f092007-08-08 22:01:54 +000012// alias analysis information, and tries to provide a lazy, caching interface to
Owen Anderson78e02f72007-07-06 23:14:35 +000013// a common kind of alias information query.
14//
15//===----------------------------------------------------------------------===//
16
Chris Lattner0e575f42008-11-28 21:45:17 +000017#define DEBUG_TYPE "memdep"
Owen Anderson78e02f72007-07-06 23:14:35 +000018#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Owen Anderson7a616a12007-07-10 17:25:03 +000019#include "llvm/Constants.h"
Owen Anderson78e02f72007-07-06 23:14:35 +000020#include "llvm/Instructions.h"
21#include "llvm/Function.h"
22#include "llvm/Analysis/AliasAnalysis.h"
Chris Lattnerbaad8882008-11-28 22:28:27 +000023#include "llvm/ADT/Statistic.h"
24#include "llvm/ADT/STLExtras.h"
Owen Anderson4beedbd2007-07-24 21:52:37 +000025#include "llvm/Support/CFG.h"
Tanya Lattner63aa1602008-02-06 00:54:55 +000026#include "llvm/Support/CommandLine.h"
Chris Lattner0e575f42008-11-28 21:45:17 +000027#include "llvm/Support/Debug.h"
Owen Anderson78e02f72007-07-06 23:14:35 +000028#include "llvm/Target/TargetData.h"
Owen Anderson78e02f72007-07-06 23:14:35 +000029using namespace llvm;
30
Dan Gohman844731a2008-05-13 00:00:25 +000031// Control the calculation of non-local dependencies by only examining the
32// predecessors if the basic block has less than X amount (50 by default).
33static cl::opt<int>
34PredLimit("nonlocaldep-threshold", cl::Hidden, cl::init(50),
35 cl::desc("Control the calculation of non-local"
36 "dependencies (default = 50)"));
Tanya Lattner63aa1602008-02-06 00:54:55 +000037
Owen Anderson7fad7e32007-09-09 21:43:49 +000038STATISTIC(NumCacheNonlocal, "Number of cached non-local responses");
39STATISTIC(NumUncacheNonlocal, "Number of uncached non-local responses");
40
Owen Anderson78e02f72007-07-06 23:14:35 +000041char MemoryDependenceAnalysis::ID = 0;
42
Owen Anderson78e02f72007-07-06 23:14:35 +000043// Register this pass...
Owen Anderson776ee1f2007-07-10 20:21:08 +000044static RegisterPass<MemoryDependenceAnalysis> X("memdep",
Chris Lattner0e575f42008-11-28 21:45:17 +000045 "Memory Dependence Analysis", false, true);
Owen Anderson78e02f72007-07-06 23:14:35 +000046
Chris Lattner0e575f42008-11-28 21:45:17 +000047/// verifyRemoved - Verify that the specified instruction does not occur
48/// in our internal data structures.
Chris Lattner8b589fa2008-11-28 21:42:09 +000049void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const {
Chris Lattner39f372e2008-11-29 01:43:36 +000050 for (LocalDepMapType::const_iterator I = LocalDeps.begin(),
51 E = LocalDeps.end(); I != E; ++I) {
Chris Lattner0e575f42008-11-28 21:45:17 +000052 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner7f524222008-11-29 03:22:12 +000053 assert(I->second.getPointer() != D &&
Chris Lattner39f372e2008-11-29 01:43:36 +000054 "Inst occurs in data structures");
Owen Anderson5fc4aba2007-12-08 01:37:09 +000055 }
56
Chris Lattner8b589fa2008-11-28 21:42:09 +000057 for (nonLocalDepMapType::const_iterator I = depGraphNonLocal.begin(),
58 E = depGraphNonLocal.end(); I != E; ++I) {
Chris Lattner0e575f42008-11-28 21:45:17 +000059 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner39f372e2008-11-29 01:43:36 +000060 for (DenseMap<BasicBlock*, DepResultTy>::iterator II = I->second.begin(),
Owen Andersond8f34fa2008-06-01 20:51:41 +000061 EE = I->second.end(); II != EE; ++II)
Chris Lattner39f372e2008-11-29 01:43:36 +000062 assert(II->second.getPointer() != D && "Inst occurs in data structures");
Owen Anderson5fc4aba2007-12-08 01:37:09 +000063 }
64
Chris Lattner8b589fa2008-11-28 21:42:09 +000065 for (reverseDepMapType::const_iterator I = reverseDep.begin(),
66 E = reverseDep.end(); I != E; ++I)
67 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
68 EE = I->second.end(); II != EE; ++II)
Chris Lattner0e575f42008-11-28 21:45:17 +000069 assert(*II != D && "Inst occurs in data structures");
Owen Anderson5fc4aba2007-12-08 01:37:09 +000070
Chris Lattner8b589fa2008-11-28 21:42:09 +000071 for (reverseDepMapType::const_iterator I = reverseDepNonLocal.begin(),
72 E = reverseDepNonLocal.end();
Owen Anderson5fc4aba2007-12-08 01:37:09 +000073 I != E; ++I)
Chris Lattner8b589fa2008-11-28 21:42:09 +000074 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
75 EE = I->second.end(); II != EE; ++II)
Chris Lattner0e575f42008-11-28 21:45:17 +000076 assert(*II != D && "Inst occurs in data structures");
Owen Anderson5fc4aba2007-12-08 01:37:09 +000077}
78
Owen Anderson78e02f72007-07-06 23:14:35 +000079/// getAnalysisUsage - Does not modify anything. It uses Alias Analysis.
80///
81void MemoryDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
82 AU.setPreservesAll();
83 AU.addRequiredTransitive<AliasAnalysis>();
84 AU.addRequiredTransitive<TargetData>();
85}
86
Owen Anderson642a9e32007-08-08 22:26:03 +000087/// getCallSiteDependency - Private helper for finding the local dependencies
88/// of a call site.
Chris Lattner4c724002008-11-29 02:29:27 +000089MemDepResult MemoryDependenceAnalysis::
Chris Lattner5391a1d2008-11-29 03:47:00 +000090getCallSiteDependency(CallSite C, BasicBlock::iterator ScanIt,
91 BasicBlock *BB) {
Chris Lattner7f524222008-11-29 03:22:12 +000092 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
93 TargetData &TD = getAnalysis<TargetData>();
Owen Andersondbbe8162007-08-07 00:33:45 +000094
Owen Anderson642a9e32007-08-08 22:26:03 +000095 // Walk backwards through the block, looking for dependencies
Chris Lattner5391a1d2008-11-29 03:47:00 +000096 while (ScanIt != BB->begin()) {
97 Instruction *Inst = --ScanIt;
Owen Anderson5f323202007-07-10 17:59:22 +000098
99 // If this inst is a memory op, get the pointer it accessed
100 Value* pointer = 0;
101 uint64_t pointerSize = 0;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000102 if (StoreInst* S = dyn_cast<StoreInst>(Inst)) {
Owen Anderson5f323202007-07-10 17:59:22 +0000103 pointer = S->getPointerOperand();
Duncan Sands514ab342007-11-01 20:53:16 +0000104 pointerSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
Chris Lattner5391a1d2008-11-29 03:47:00 +0000105 } else if (AllocationInst* AI = dyn_cast<AllocationInst>(Inst)) {
Owen Anderson5f323202007-07-10 17:59:22 +0000106 pointer = AI;
107 if (ConstantInt* C = dyn_cast<ConstantInt>(AI->getArraySize()))
Chris Lattner57d40122008-11-28 21:16:44 +0000108 pointerSize = C->getZExtValue() *
Duncan Sands514ab342007-11-01 20:53:16 +0000109 TD.getABITypeSize(AI->getAllocatedType());
Owen Anderson5f323202007-07-10 17:59:22 +0000110 else
111 pointerSize = ~0UL;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000112 } else if (VAArgInst* V = dyn_cast<VAArgInst>(Inst)) {
Owen Anderson06b6e822007-07-10 18:43:15 +0000113 pointer = V->getOperand(0);
Duncan Sands514ab342007-11-01 20:53:16 +0000114 pointerSize = TD.getTypeStoreSize(V->getType());
Chris Lattner5391a1d2008-11-29 03:47:00 +0000115 } else if (FreeInst* F = dyn_cast<FreeInst>(Inst)) {
Owen Anderson5f323202007-07-10 17:59:22 +0000116 pointer = F->getPointerOperand();
117
118 // FreeInsts erase the entire structure
119 pointerSize = ~0UL;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000120 } else if (CallSite::get(Inst).getInstruction() != 0) {
121 if (AA.getModRefBehavior(CallSite::get(Inst)) !=
122 AliasAnalysis::DoesNotAccessMemory)
123 return MemDepResult::get(Inst);
124 continue;
Owen Anderson202da142007-07-10 20:39:07 +0000125 } else
126 continue;
Owen Anderson5f323202007-07-10 17:59:22 +0000127
Chris Lattner5391a1d2008-11-29 03:47:00 +0000128 if (AA.getModRefInfo(C, pointer, pointerSize) != AliasAnalysis::NoModRef)
129 return MemDepResult::get(Inst);
Owen Anderson5f323202007-07-10 17:59:22 +0000130 }
131
Chris Lattner5391a1d2008-11-29 03:47:00 +0000132 // No dependence found.
Chris Lattner4c724002008-11-29 02:29:27 +0000133 return MemDepResult::getNonLocal();
Owen Anderson5f323202007-07-10 17:59:22 +0000134}
135
Owen Anderson642a9e32007-08-08 22:26:03 +0000136/// nonLocalHelper - Private helper used to calculate non-local dependencies
Chris Lattner39f372e2008-11-29 01:43:36 +0000137/// by doing DFS on the predecessors of a block to find its dependencies.
Owen Anderson90660202007-08-01 22:01:54 +0000138void MemoryDependenceAnalysis::nonLocalHelper(Instruction* query,
Owen Anderson0cd32032007-07-25 19:57:03 +0000139 BasicBlock* block,
Chris Lattner39f372e2008-11-29 01:43:36 +0000140 DenseMap<BasicBlock*, DepResultTy> &resp) {
Owen Anderson642a9e32007-08-08 22:26:03 +0000141 // Set of blocks that we've already visited in our DFS
Owen Anderson90660202007-08-01 22:01:54 +0000142 SmallPtrSet<BasicBlock*, 4> visited;
Owen Andersonce4d88a2007-09-21 03:53:52 +0000143 // If we're updating a dirtied cache entry, we don't need to reprocess
144 // already computed entries.
Chris Lattner39f372e2008-11-29 01:43:36 +0000145 for (DenseMap<BasicBlock*, DepResultTy>::iterator I = resp.begin(),
Owen Andersonce4d88a2007-09-21 03:53:52 +0000146 E = resp.end(); I != E; ++I)
Chris Lattner39f372e2008-11-29 01:43:36 +0000147 if (I->second.getInt() != Dirty)
Owen Andersonce4d88a2007-09-21 03:53:52 +0000148 visited.insert(I->first);
149
Owen Anderson642a9e32007-08-08 22:26:03 +0000150 // Current stack of the DFS
Owen Anderson90660202007-08-01 22:01:54 +0000151 SmallVector<BasicBlock*, 4> stack;
Owen Andersonf062f102008-04-10 22:13:32 +0000152 for (pred_iterator PI = pred_begin(block), PE = pred_end(block);
153 PI != PE; ++PI)
154 stack.push_back(*PI);
Owen Anderson4beedbd2007-07-24 21:52:37 +0000155
Owen Anderson642a9e32007-08-08 22:26:03 +0000156 // Do a basic DFS
Owen Anderson90660202007-08-01 22:01:54 +0000157 while (!stack.empty()) {
158 BasicBlock* BB = stack.back();
159
Owen Anderson642a9e32007-08-08 22:26:03 +0000160 // If we've already visited this block, no need to revist
Owen Anderson1c2763d2007-08-02 17:56:05 +0000161 if (visited.count(BB)) {
Owen Anderson90660202007-08-01 22:01:54 +0000162 stack.pop_back();
163 continue;
164 }
165
Owen Anderson642a9e32007-08-08 22:26:03 +0000166 // If we find a new block with a local dependency for query,
167 // then we insert the new dependency and backtrack.
Owen Anderson90660202007-08-01 22:01:54 +0000168 if (BB != block) {
Owen Anderson1c2763d2007-08-02 17:56:05 +0000169 visited.insert(BB);
170
Chris Lattner5391a1d2008-11-29 03:47:00 +0000171 MemDepResult localDep = getDependencyFrom(query, BB->end(), BB);
Chris Lattner4c724002008-11-29 02:29:27 +0000172 if (!localDep.isNonLocal()) {
173 resp.insert(std::make_pair(BB, ConvFromResult(localDep)));
Owen Anderson1c2763d2007-08-02 17:56:05 +0000174 stack.pop_back();
Owen Anderson90660202007-08-01 22:01:54 +0000175 continue;
176 }
Owen Anderson642a9e32007-08-08 22:26:03 +0000177 // If we re-encounter the starting block, we still need to search it
178 // because there might be a dependency in the starting block AFTER
179 // the position of the query. This is necessary to get loops right.
Owen Andersonf062f102008-04-10 22:13:32 +0000180 } else if (BB == block) {
Owen Anderson1c2763d2007-08-02 17:56:05 +0000181 visited.insert(BB);
182
Chris Lattner5391a1d2008-11-29 03:47:00 +0000183 MemDepResult localDep = getDependencyFrom(query, BB->end(), BB);
Chris Lattner4c724002008-11-29 02:29:27 +0000184 if (localDep.getInst() != query)
185 resp.insert(std::make_pair(BB, ConvFromResult(localDep)));
Owen Anderson1c2763d2007-08-02 17:56:05 +0000186
187 stack.pop_back();
Owen Anderson1c2763d2007-08-02 17:56:05 +0000188 continue;
Owen Anderson90660202007-08-01 22:01:54 +0000189 }
190
Owen Anderson642a9e32007-08-08 22:26:03 +0000191 // If we didn't find anything, recurse on the precessors of this block
Tanya Lattner63aa1602008-02-06 00:54:55 +0000192 // Only do this for blocks with a small number of predecessors.
Owen Anderson90660202007-08-01 22:01:54 +0000193 bool predOnStack = false;
194 bool inserted = false;
Tanya Lattner63aa1602008-02-06 00:54:55 +0000195 if (std::distance(pred_begin(BB), pred_end(BB)) <= PredLimit) {
196 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
197 PI != PE; ++PI)
198 if (!visited.count(*PI)) {
199 stack.push_back(*PI);
200 inserted = true;
201 } else
202 predOnStack = true;
203 }
Owen Anderson90660202007-08-01 22:01:54 +0000204
Owen Anderson642a9e32007-08-08 22:26:03 +0000205 // If we inserted a new predecessor, then we'll come back to this block
Owen Anderson90660202007-08-01 22:01:54 +0000206 if (inserted)
207 continue;
Owen Anderson642a9e32007-08-08 22:26:03 +0000208 // If we didn't insert because we have no predecessors, then this
209 // query has no dependency at all.
Owen Anderson90660202007-08-01 22:01:54 +0000210 else if (!inserted && !predOnStack) {
Chris Lattner39f372e2008-11-29 01:43:36 +0000211 resp.insert(std::make_pair(BB, DepResultTy(0, None)));
Owen Anderson642a9e32007-08-08 22:26:03 +0000212 // If we didn't insert because our predecessors are already on the stack,
213 // then we might still have a dependency, but it will be discovered during
214 // backtracking.
Owen Anderson90660202007-08-01 22:01:54 +0000215 } else if (!inserted && predOnStack){
Chris Lattner39f372e2008-11-29 01:43:36 +0000216 resp.insert(std::make_pair(BB, DepResultTy(0, NonLocal)));
Owen Anderson90660202007-08-01 22:01:54 +0000217 }
218
219 stack.pop_back();
Owen Anderson4beedbd2007-07-24 21:52:37 +0000220 }
Owen Anderson4beedbd2007-07-24 21:52:37 +0000221}
222
Owen Anderson80b1f092007-08-08 22:01:54 +0000223/// getNonLocalDependency - Fills the passed-in map with the non-local
224/// dependencies of the queries. The map will contain NonLocal for
225/// blocks between the query and its dependencies.
Owen Anderson90660202007-08-01 22:01:54 +0000226void MemoryDependenceAnalysis::getNonLocalDependency(Instruction* query,
Chris Lattner4c724002008-11-29 02:29:27 +0000227 DenseMap<BasicBlock*, MemDepResult> &resp) {
Owen Anderson4d13de42007-08-16 21:27:05 +0000228 if (depGraphNonLocal.count(query)) {
Chris Lattner39f372e2008-11-29 01:43:36 +0000229 DenseMap<BasicBlock*, DepResultTy> &cached = depGraphNonLocal[query];
Owen Anderson7fad7e32007-09-09 21:43:49 +0000230 NumCacheNonlocal++;
Owen Andersonce4d88a2007-09-21 03:53:52 +0000231
232 SmallVector<BasicBlock*, 4> dirtied;
Chris Lattner39f372e2008-11-29 01:43:36 +0000233 for (DenseMap<BasicBlock*, DepResultTy>::iterator I = cached.begin(),
Owen Andersonce4d88a2007-09-21 03:53:52 +0000234 E = cached.end(); I != E; ++I)
Chris Lattner39f372e2008-11-29 01:43:36 +0000235 if (I->second.getInt() == Dirty)
Owen Andersonce4d88a2007-09-21 03:53:52 +0000236 dirtied.push_back(I->first);
237
238 for (SmallVector<BasicBlock*, 4>::iterator I = dirtied.begin(),
239 E = dirtied.end(); I != E; ++I) {
Chris Lattner5391a1d2008-11-29 03:47:00 +0000240 MemDepResult localDep = getDependencyFrom(query, (*I)->end(), *I);
Chris Lattner4c724002008-11-29 02:29:27 +0000241 if (!localDep.isNonLocal())
242 cached[*I] = ConvFromResult(localDep);
Owen Andersonce4d88a2007-09-21 03:53:52 +0000243 else {
244 cached.erase(*I);
245 nonLocalHelper(query, *I, cached);
246 }
247 }
248
Chris Lattner4c724002008-11-29 02:29:27 +0000249 // Update the reverse non-local dependency cache.
250 for (DenseMap<BasicBlock*, DepResultTy>::iterator I = cached.begin(),
251 E = cached.end(); I != E; ++I) {
Chris Lattner7f524222008-11-29 03:22:12 +0000252 if (Instruction *Inst = I->second.getPointer())
253 reverseDepNonLocal[Inst].insert(query);
Chris Lattner4c724002008-11-29 02:29:27 +0000254 resp[I->first] = ConvToResult(I->second);
255 }
Owen Anderson6bd15ce2008-06-01 21:03:52 +0000256
Owen Anderson4d13de42007-08-16 21:27:05 +0000257 return;
Chris Lattner4c724002008-11-29 02:29:27 +0000258 }
259
260 NumUncacheNonlocal++;
Owen Anderson4d13de42007-08-16 21:27:05 +0000261
Owen Anderson7fad7e32007-09-09 21:43:49 +0000262 // If not, go ahead and search for non-local deps.
Chris Lattner4c724002008-11-29 02:29:27 +0000263 DenseMap<BasicBlock*, DepResultTy> &cached = depGraphNonLocal[query];
264 nonLocalHelper(query, query->getParent(), cached);
265
Owen Anderson4d13de42007-08-16 21:27:05 +0000266 // Update the non-local dependency cache
Chris Lattner4c724002008-11-29 02:29:27 +0000267 for (DenseMap<BasicBlock*, DepResultTy>::iterator I = cached.begin(),
268 E = cached.end(); I != E; ++I) {
269 // FIXME: Merge with the code above!
Chris Lattner7f524222008-11-29 03:22:12 +0000270 if (Instruction *Inst = I->second.getPointer())
271 reverseDepNonLocal[Inst].insert(query);
Chris Lattner4c724002008-11-29 02:29:27 +0000272 resp[I->first] = ConvToResult(I->second);
Owen Anderson4d13de42007-08-16 21:27:05 +0000273 }
Owen Anderson4beedbd2007-07-24 21:52:37 +0000274}
275
Owen Anderson78e02f72007-07-06 23:14:35 +0000276/// getDependency - Return the instruction on which a memory operation
Dan Gohmanc04575f2008-04-10 23:02:38 +0000277/// depends. The local parameter indicates if the query should only
Owen Anderson6b278fc2007-07-10 17:08:11 +0000278/// evaluate dependencies within the same basic block.
Chris Lattner7f524222008-11-29 03:22:12 +0000279/// FIXME: ELIMINATE START/BLOCK and make the caching happen in a higher level
280/// METHOD.
Chris Lattner5391a1d2008-11-29 03:47:00 +0000281MemDepResult MemoryDependenceAnalysis::
282getDependencyFrom(Instruction *QueryInst, BasicBlock::iterator ScanIt,
283 BasicBlock *BB) {
284 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
285 TargetData &TD = getAnalysis<TargetData>();
Owen Anderson78e02f72007-07-06 23:14:35 +0000286
287 // Get the pointer value for which dependence will be determined
288 Value* dependee = 0;
Owen Anderson6b278fc2007-07-10 17:08:11 +0000289 uint64_t dependeeSize = 0;
Owen Andersone314eb32007-07-10 18:11:42 +0000290 bool queryIsVolatile = false;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000291
292 if (StoreInst* S = dyn_cast<StoreInst>(QueryInst)) {
Owen Anderson78e02f72007-07-06 23:14:35 +0000293 dependee = S->getPointerOperand();
Duncan Sands514ab342007-11-01 20:53:16 +0000294 dependeeSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
Owen Andersone314eb32007-07-10 18:11:42 +0000295 queryIsVolatile = S->isVolatile();
Chris Lattner5391a1d2008-11-29 03:47:00 +0000296 } else if (LoadInst* L = dyn_cast<LoadInst>(QueryInst)) {
Owen Anderson78e02f72007-07-06 23:14:35 +0000297 dependee = L->getPointerOperand();
Duncan Sands514ab342007-11-01 20:53:16 +0000298 dependeeSize = TD.getTypeStoreSize(L->getType());
Owen Andersone314eb32007-07-10 18:11:42 +0000299 queryIsVolatile = L->isVolatile();
Chris Lattner5391a1d2008-11-29 03:47:00 +0000300 } else if (VAArgInst* V = dyn_cast<VAArgInst>(QueryInst)) {
Owen Anderson06b6e822007-07-10 18:43:15 +0000301 dependee = V->getOperand(0);
Duncan Sands514ab342007-11-01 20:53:16 +0000302 dependeeSize = TD.getTypeStoreSize(V->getType());
Chris Lattner5391a1d2008-11-29 03:47:00 +0000303 } else if (FreeInst* F = dyn_cast<FreeInst>(QueryInst)) {
Owen Anderson78e02f72007-07-06 23:14:35 +0000304 dependee = F->getPointerOperand();
Owen Anderson6b278fc2007-07-10 17:08:11 +0000305 // FreeInsts erase the entire structure, not just a field
306 dependeeSize = ~0UL;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000307 } else if (CallSite::get(QueryInst).getInstruction() != 0)
308 return getCallSiteDependency(CallSite::get(QueryInst), ScanIt, BB);
Owen Anderson7a616a12007-07-10 17:25:03 +0000309 else
Chris Lattner4c724002008-11-29 02:29:27 +0000310 return MemDepResult::getNone();
Owen Anderson78e02f72007-07-06 23:14:35 +0000311
Owen Anderson642a9e32007-08-08 22:26:03 +0000312 // Walk backwards through the basic block, looking for dependencies
Chris Lattner5391a1d2008-11-29 03:47:00 +0000313 while (ScanIt != BB->begin()) {
314 Instruction *Inst = --ScanIt;
Owen Anderson6b278fc2007-07-10 17:08:11 +0000315
Owen Anderson78e02f72007-07-06 23:14:35 +0000316 // If this inst is a memory op, get the pointer it accessed
317 Value* pointer = 0;
Owen Anderson6b278fc2007-07-10 17:08:11 +0000318 uint64_t pointerSize = 0;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000319 if (StoreInst* S = dyn_cast<StoreInst>(Inst)) {
Owen Andersone314eb32007-07-10 18:11:42 +0000320 // All volatile loads/stores depend on each other
Chris Lattner5391a1d2008-11-29 03:47:00 +0000321 if (queryIsVolatile && S->isVolatile())
Chris Lattner4c724002008-11-29 02:29:27 +0000322 return MemDepResult::get(S);
Owen Andersone314eb32007-07-10 18:11:42 +0000323
Owen Anderson78e02f72007-07-06 23:14:35 +0000324 pointer = S->getPointerOperand();
Duncan Sands514ab342007-11-01 20:53:16 +0000325 pointerSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
Chris Lattner5391a1d2008-11-29 03:47:00 +0000326 } else if (LoadInst* L = dyn_cast<LoadInst>(Inst)) {
Owen Andersone314eb32007-07-10 18:11:42 +0000327 // All volatile loads/stores depend on each other
Chris Lattner5391a1d2008-11-29 03:47:00 +0000328 if (queryIsVolatile && L->isVolatile())
Chris Lattner4c724002008-11-29 02:29:27 +0000329 return MemDepResult::get(L);
Owen Andersone314eb32007-07-10 18:11:42 +0000330
Owen Anderson78e02f72007-07-06 23:14:35 +0000331 pointer = L->getPointerOperand();
Duncan Sands514ab342007-11-01 20:53:16 +0000332 pointerSize = TD.getTypeStoreSize(L->getType());
Chris Lattner5391a1d2008-11-29 03:47:00 +0000333 } else if (AllocationInst* AI = dyn_cast<AllocationInst>(Inst)) {
Owen Anderson6b278fc2007-07-10 17:08:11 +0000334 pointer = AI;
Owen Anderson7a616a12007-07-10 17:25:03 +0000335 if (ConstantInt* C = dyn_cast<ConstantInt>(AI->getArraySize()))
Chris Lattner57d40122008-11-28 21:16:44 +0000336 pointerSize = C->getZExtValue() *
Duncan Sands514ab342007-11-01 20:53:16 +0000337 TD.getABITypeSize(AI->getAllocatedType());
Owen Anderson6b278fc2007-07-10 17:08:11 +0000338 else
339 pointerSize = ~0UL;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000340 } else if (VAArgInst* V = dyn_cast<VAArgInst>(Inst)) {
Owen Anderson06b6e822007-07-10 18:43:15 +0000341 pointer = V->getOperand(0);
Duncan Sands514ab342007-11-01 20:53:16 +0000342 pointerSize = TD.getTypeStoreSize(V->getType());
Chris Lattner5391a1d2008-11-29 03:47:00 +0000343 } else if (FreeInst* F = dyn_cast<FreeInst>(Inst)) {
Owen Anderson78e02f72007-07-06 23:14:35 +0000344 pointer = F->getPointerOperand();
Owen Anderson6b278fc2007-07-10 17:08:11 +0000345
346 // FreeInsts erase the entire structure
347 pointerSize = ~0UL;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000348 } else if (isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) {
Owen Anderson80b1f092007-08-08 22:01:54 +0000349 // Call insts need special handling. Check if they can modify our pointer
Chris Lattner5391a1d2008-11-29 03:47:00 +0000350 AliasAnalysis::ModRefResult MR = AA.getModRefInfo(CallSite::get(Inst),
Chris Lattner39f372e2008-11-29 01:43:36 +0000351 dependee, dependeeSize);
Owen Anderson8f353152007-08-06 23:26:03 +0000352
353 if (MR != AliasAnalysis::NoModRef) {
354 // Loads don't depend on read-only calls
Chris Lattner5391a1d2008-11-29 03:47:00 +0000355 if (isa<LoadInst>(QueryInst) && MR == AliasAnalysis::Ref)
Owen Anderson8f353152007-08-06 23:26:03 +0000356 continue;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000357 return MemDepResult::get(Inst);
Owen Anderson7a616a12007-07-10 17:25:03 +0000358 }
Chris Lattner5391a1d2008-11-29 03:47:00 +0000359
360 continue;
Owen Anderson78e02f72007-07-06 23:14:35 +0000361 }
362
363 // If we found a pointer, check if it could be the same as our pointer
364 if (pointer) {
Owen Anderson6b278fc2007-07-10 17:08:11 +0000365 AliasAnalysis::AliasResult R = AA.alias(pointer, pointerSize,
366 dependee, dependeeSize);
Owen Anderson78e02f72007-07-06 23:14:35 +0000367
368 if (R != AliasAnalysis::NoAlias) {
Owen Anderson8f353152007-08-06 23:26:03 +0000369 // May-alias loads don't depend on each other
Chris Lattner5391a1d2008-11-29 03:47:00 +0000370 if (isa<LoadInst>(QueryInst) && isa<LoadInst>(Inst) &&
Owen Anderson8f353152007-08-06 23:26:03 +0000371 R == AliasAnalysis::MayAlias)
372 continue;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000373 return MemDepResult::get(Inst);
Owen Anderson78e02f72007-07-06 23:14:35 +0000374 }
375 }
Owen Anderson78e02f72007-07-06 23:14:35 +0000376 }
377
Chris Lattner5391a1d2008-11-29 03:47:00 +0000378 // If we found nothing, return the non-local flag.
Chris Lattner4c724002008-11-29 02:29:27 +0000379 return MemDepResult::getNonLocal();
Owen Anderson78e02f72007-07-06 23:14:35 +0000380}
381
Chris Lattner5391a1d2008-11-29 03:47:00 +0000382/// getDependency - Return the instruction on which a memory operation
383/// depends.
384MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) {
385 Instruction *ScanPos = QueryInst;
386
387 // Check for a cached result
388 DepResultTy &LocalCache = LocalDeps[QueryInst];
389
390 // If the cached entry is non-dirty, just return it.
391 if (LocalCache.getInt() != Dirty)
392 return ConvToResult(LocalCache);
393
394 // Otherwise, if we have a dirty entry, we know we can start the scan at that
395 // instruction, which may save us some work.
396 if (Instruction *Inst = LocalCache.getPointer())
397 ScanPos = Inst;
398
399 // Do the scan.
400 MemDepResult Res =
401 getDependencyFrom(QueryInst, ScanPos, QueryInst->getParent());
402
403 // Remember the result!
404 // FIXME: Don't convert back and forth! Make a shared helper function.
405 LocalCache = ConvFromResult(Res);
406 if (Instruction *I = Res.getInst())
407 reverseDep[I].insert(QueryInst);
408
409 return Res;
410}
411
412
Owen Anderson30b4bd42008-02-12 21:15:18 +0000413/// dropInstruction - Remove an instruction from the analysis, making
414/// absolutely conservative assumptions when updating the cache. This is
415/// useful, for example when an instruction is changed rather than removed.
416void MemoryDependenceAnalysis::dropInstruction(Instruction* drop) {
Chris Lattner39f372e2008-11-29 01:43:36 +0000417 LocalDepMapType::iterator depGraphEntry = LocalDeps.find(drop);
418 if (depGraphEntry != LocalDeps.end())
Chris Lattner7f524222008-11-29 03:22:12 +0000419 if (Instruction *Inst = depGraphEntry->second.getPointer())
420 reverseDep[Inst].erase(drop);
Owen Anderson30b4bd42008-02-12 21:15:18 +0000421
422 // Drop dependency information for things that depended on this instr
Chris Lattner7f524222008-11-29 03:22:12 +0000423 SmallPtrSet<Instruction*, 4>& set = reverseDep[drop];
Owen Anderson30b4bd42008-02-12 21:15:18 +0000424 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
425 I != E; ++I)
Chris Lattner39f372e2008-11-29 01:43:36 +0000426 LocalDeps.erase(*I);
Owen Anderson30b4bd42008-02-12 21:15:18 +0000427
Chris Lattner39f372e2008-11-29 01:43:36 +0000428 LocalDeps.erase(drop);
Chris Lattner7f524222008-11-29 03:22:12 +0000429 reverseDep.erase(drop);
Owen Anderson30b4bd42008-02-12 21:15:18 +0000430
Chris Lattner39f372e2008-11-29 01:43:36 +0000431 for (DenseMap<BasicBlock*, DepResultTy>::iterator DI =
432 depGraphNonLocal[drop].begin(), DE = depGraphNonLocal[drop].end();
Owen Anderson30b4bd42008-02-12 21:15:18 +0000433 DI != DE; ++DI)
Chris Lattner7f524222008-11-29 03:22:12 +0000434 if (Instruction *Inst = DI->second.getPointer())
435 reverseDepNonLocal[Inst].erase(drop);
Owen Anderson30b4bd42008-02-12 21:15:18 +0000436
Chris Lattner7f524222008-11-29 03:22:12 +0000437 if (reverseDepNonLocal.count(drop)) {
Chris Lattner39f372e2008-11-29 01:43:36 +0000438 SmallPtrSet<Instruction*, 4>& set =
Chris Lattner7f524222008-11-29 03:22:12 +0000439 reverseDepNonLocal[drop];
Owen Anderson30b4bd42008-02-12 21:15:18 +0000440 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
441 I != E; ++I)
Chris Lattner39f372e2008-11-29 01:43:36 +0000442 for (DenseMap<BasicBlock*, DepResultTy>::iterator DI =
Owen Anderson30b4bd42008-02-12 21:15:18 +0000443 depGraphNonLocal[*I].begin(), DE = depGraphNonLocal[*I].end();
444 DI != DE; ++DI)
Chris Lattner39f372e2008-11-29 01:43:36 +0000445 if (DI->second == DepResultTy(drop, Normal))
Chris Lattner7f524222008-11-29 03:22:12 +0000446 // FIXME: Why not remember the old insertion point??
Chris Lattner39f372e2008-11-29 01:43:36 +0000447 DI->second = DepResultTy(0, Dirty);
Owen Anderson30b4bd42008-02-12 21:15:18 +0000448 }
449
Chris Lattner7f524222008-11-29 03:22:12 +0000450 reverseDepNonLocal.erase(drop);
Chris Lattner39f372e2008-11-29 01:43:36 +0000451 depGraphNonLocal.erase(drop);
Owen Anderson30b4bd42008-02-12 21:15:18 +0000452}
453
Owen Anderson78e02f72007-07-06 23:14:35 +0000454/// removeInstruction - Remove an instruction from the dependence analysis,
455/// updating the dependence of instructions that previously depended on it.
Owen Anderson642a9e32007-08-08 22:26:03 +0000456/// This method attempts to keep the cache coherent using the reverse map.
Chris Lattner5f589dc2008-11-28 22:04:47 +0000457void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) {
Chris Lattner5f589dc2008-11-28 22:04:47 +0000458 // Walk through the Non-local dependencies, removing this one as the value
459 // for any cached queries.
Chris Lattner39f372e2008-11-29 01:43:36 +0000460 for (DenseMap<BasicBlock*, DepResultTy>::iterator DI =
Chris Lattner5f589dc2008-11-28 22:04:47 +0000461 depGraphNonLocal[RemInst].begin(), DE = depGraphNonLocal[RemInst].end();
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000462 DI != DE; ++DI)
Chris Lattner7f524222008-11-29 03:22:12 +0000463 if (Instruction *Inst = DI->second.getPointer())
464 reverseDepNonLocal[Inst].erase(RemInst);
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000465
Chris Lattnerbaad8882008-11-28 22:28:27 +0000466 // Shortly after this, we will look for things that depend on RemInst. In
467 // order to update these, we'll need a new dependency to base them on. We
468 // could completely delete any entries that depend on this, but it is better
469 // to make a more accurate approximation where possible. Compute that better
470 // approximation if we can.
Chris Lattner39f372e2008-11-29 01:43:36 +0000471 DepResultTy NewDependency;
Chris Lattnerbaad8882008-11-28 22:28:27 +0000472
Chris Lattner5f589dc2008-11-28 22:04:47 +0000473 // If we have a cached local dependence query for this instruction, remove it.
Chris Lattnerbaad8882008-11-28 22:28:27 +0000474 //
Chris Lattner39f372e2008-11-29 01:43:36 +0000475 LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst);
476 if (LocalDepEntry != LocalDeps.end()) {
Chris Lattner7f524222008-11-29 03:22:12 +0000477 DepResultTy LocalDep = LocalDepEntry->second;
Owen Anderson9a8ff8c2008-01-30 01:24:05 +0000478
Chris Lattnerbaad8882008-11-28 22:28:27 +0000479 // Remove this local dependency info.
Chris Lattner39f372e2008-11-29 01:43:36 +0000480 LocalDeps.erase(LocalDepEntry);
Chris Lattner5f589dc2008-11-28 22:04:47 +0000481
Chris Lattnerbaad8882008-11-28 22:28:27 +0000482 // Remove us from DepInst's reverse set now that the local dep info is gone.
Chris Lattner7f524222008-11-29 03:22:12 +0000483 if (Instruction *Inst = LocalDep.getPointer())
484 reverseDep[Inst].erase(RemInst);
Chris Lattnerbaad8882008-11-28 22:28:27 +0000485
486 // If we have unconfirmed info, don't trust it.
Chris Lattner7f524222008-11-29 03:22:12 +0000487 if (LocalDep.getInt() != Dirty) {
Chris Lattnerbaad8882008-11-28 22:28:27 +0000488 // If we have a confirmed non-local flag, use it.
Chris Lattner39f372e2008-11-29 01:43:36 +0000489 if (LocalDep.getInt() == NonLocal || LocalDep.getInt() == None) {
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000490 // The only time this dependency is confirmed is if it is non-local.
Chris Lattner39f372e2008-11-29 01:43:36 +0000491 NewDependency = LocalDep;
Chris Lattnerbaad8882008-11-28 22:28:27 +0000492 } else {
493 // If we have dep info for RemInst, set them to it.
Chris Lattner39f372e2008-11-29 01:43:36 +0000494 Instruction *NDI = next(BasicBlock::iterator(LocalDep.getPointer()));
495 if (NDI != RemInst) // Don't use RemInst for the new dependency!
Chris Lattner7f524222008-11-29 03:22:12 +0000496 NewDependency = DepResultTy(NDI, Dirty);
Chris Lattnerbaad8882008-11-28 22:28:27 +0000497 }
David Greenedf464192007-07-31 20:01:27 +0000498 }
Owen Andersona8701a62008-02-05 04:34:03 +0000499 }
500
Chris Lattnerbaad8882008-11-28 22:28:27 +0000501 // If we don't already have a local dependency answer for this instruction,
502 // use the immediate successor of RemInst. We use the successor because
503 // getDependence starts by checking the immediate predecessor of what is in
504 // the cache.
Chris Lattner7f524222008-11-29 03:22:12 +0000505 if (NewDependency == DepResultTy(0, Dirty))
506 NewDependency = DepResultTy(next(BasicBlock::iterator(RemInst)), Dirty);
Chris Lattnerbaad8882008-11-28 22:28:27 +0000507
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000508 // Loop over all of the things that depend on the instruction we're removing.
509 //
Chris Lattner7f524222008-11-29 03:22:12 +0000510 reverseDepMapType::iterator ReverseDepIt = reverseDep.find(RemInst);
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000511 if (ReverseDepIt != reverseDep.end()) {
512 SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second;
513 for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(),
514 E = ReverseDeps.end(); I != E; ++I) {
515 Instruction *InstDependingOnRemInst = *I;
516
517 // If we thought the instruction depended on itself (possible for
518 // unconfirmed dependencies) ignore the update.
519 if (InstDependingOnRemInst == RemInst) continue;
520
521 // Insert the new dependencies.
Chris Lattner7f524222008-11-29 03:22:12 +0000522 LocalDeps[InstDependingOnRemInst] = NewDependency;
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000523
524 // If our NewDependency is an instruction, make sure to remember that new
525 // things depend on it.
Chris Lattner7f524222008-11-29 03:22:12 +0000526 if (Instruction *Inst = NewDependency.getPointer())
527 reverseDep[Inst].insert(InstDependingOnRemInst);
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000528 }
Chris Lattner7f524222008-11-29 03:22:12 +0000529 reverseDep.erase(RemInst);
Owen Anderson78e02f72007-07-06 23:14:35 +0000530 }
Owen Anderson4d13de42007-08-16 21:27:05 +0000531
Chris Lattner7f524222008-11-29 03:22:12 +0000532 ReverseDepIt = reverseDepNonLocal.find(RemInst);
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000533 if (ReverseDepIt != reverseDepNonLocal.end()) {
534 SmallPtrSet<Instruction*, 4>& set = ReverseDepIt->second;
Owen Anderson4d13de42007-08-16 21:27:05 +0000535 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
536 I != E; ++I)
Chris Lattner39f372e2008-11-29 01:43:36 +0000537 for (DenseMap<BasicBlock*, DepResultTy>::iterator DI =
Owen Andersonce4d88a2007-09-21 03:53:52 +0000538 depGraphNonLocal[*I].begin(), DE = depGraphNonLocal[*I].end();
539 DI != DE; ++DI)
Chris Lattner39f372e2008-11-29 01:43:36 +0000540 if (DI->second == DepResultTy(RemInst, Normal))
Chris Lattner7f524222008-11-29 03:22:12 +0000541 // FIXME: Why not remember the old insertion point??
Chris Lattner39f372e2008-11-29 01:43:36 +0000542 DI->second = DepResultTy(0, Dirty);
543 reverseDepNonLocal.erase(ReverseDepIt);
Owen Anderson4d13de42007-08-16 21:27:05 +0000544 }
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000545
Chris Lattner5f589dc2008-11-28 22:04:47 +0000546 depGraphNonLocal.erase(RemInst);
Owen Anderson78e02f72007-07-06 23:14:35 +0000547
Chris Lattner5f589dc2008-11-28 22:04:47 +0000548 getAnalysis<AliasAnalysis>().deleteValue(RemInst);
Chris Lattner0e575f42008-11-28 21:45:17 +0000549
Chris Lattner5f589dc2008-11-28 22:04:47 +0000550 DEBUG(verifyRemoved(RemInst));
Owen Anderson78e02f72007-07-06 23:14:35 +0000551}