blob: 27c43c735e979860bc2c06984031d7900a6457fe [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 Anderson7fad7e32007-09-09 21:43:49 +000029
Owen Anderson78e02f72007-07-06 23:14:35 +000030using namespace llvm;
31
Dan Gohman844731a2008-05-13 00:00:25 +000032// Control the calculation of non-local dependencies by only examining the
33// predecessors if the basic block has less than X amount (50 by default).
34static cl::opt<int>
35PredLimit("nonlocaldep-threshold", cl::Hidden, cl::init(50),
36 cl::desc("Control the calculation of non-local"
37 "dependencies (default = 50)"));
Tanya Lattner63aa1602008-02-06 00:54:55 +000038
Owen Anderson7fad7e32007-09-09 21:43:49 +000039STATISTIC(NumCacheNonlocal, "Number of cached non-local responses");
40STATISTIC(NumUncacheNonlocal, "Number of uncached non-local responses");
41
Owen Anderson78e02f72007-07-06 23:14:35 +000042char MemoryDependenceAnalysis::ID = 0;
43
Owen Anderson9528f112007-08-09 04:42:44 +000044Instruction* const MemoryDependenceAnalysis::NonLocal = (Instruction*)-3;
45Instruction* const MemoryDependenceAnalysis::None = (Instruction*)-4;
Owen Anderson742f9b62007-09-19 16:13:57 +000046Instruction* const MemoryDependenceAnalysis::Dirty = (Instruction*)-5;
Owen Anderson78e02f72007-07-06 23:14:35 +000047
48// Register this pass...
Owen Anderson776ee1f2007-07-10 20:21:08 +000049static RegisterPass<MemoryDependenceAnalysis> X("memdep",
Chris Lattner0e575f42008-11-28 21:45:17 +000050 "Memory Dependence Analysis", false, true);
Owen Anderson78e02f72007-07-06 23:14:35 +000051
Chris Lattner0e575f42008-11-28 21:45:17 +000052/// verifyRemoved - Verify that the specified instruction does not occur
53/// in our internal data structures.
Chris Lattner8b589fa2008-11-28 21:42:09 +000054void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const {
55 for (depMapType::const_iterator I = depGraphLocal.begin(),
56 E = depGraphLocal.end(); I != E; ++I) {
Chris Lattner0e575f42008-11-28 21:45:17 +000057 assert(I->first != D && "Inst occurs in data structures");
58 assert(I->second.first != D && "Inst occurs in data structures");
Owen Anderson5fc4aba2007-12-08 01:37:09 +000059 }
60
Chris Lattner8b589fa2008-11-28 21:42:09 +000061 for (nonLocalDepMapType::const_iterator I = depGraphNonLocal.begin(),
62 E = depGraphNonLocal.end(); I != E; ++I) {
Chris Lattner0e575f42008-11-28 21:45:17 +000063 assert(I->first != D && "Inst occurs in data structures");
Owen Andersond8f34fa2008-06-01 20:51:41 +000064 for (DenseMap<BasicBlock*, Value*>::iterator II = I->second.begin(),
65 EE = I->second.end(); II != EE; ++II)
Chris Lattner0e575f42008-11-28 21:45:17 +000066 assert(II->second != D && "Inst occurs in data structures");
Owen Anderson5fc4aba2007-12-08 01:37:09 +000067 }
68
Chris Lattner8b589fa2008-11-28 21:42:09 +000069 for (reverseDepMapType::const_iterator I = reverseDep.begin(),
70 E = reverseDep.end(); I != E; ++I)
71 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
72 EE = I->second.end(); II != EE; ++II)
Chris Lattner0e575f42008-11-28 21:45:17 +000073 assert(*II != D && "Inst occurs in data structures");
Owen Anderson5fc4aba2007-12-08 01:37:09 +000074
Chris Lattner8b589fa2008-11-28 21:42:09 +000075 for (reverseDepMapType::const_iterator I = reverseDepNonLocal.begin(),
76 E = reverseDepNonLocal.end();
Owen Anderson5fc4aba2007-12-08 01:37:09 +000077 I != E; ++I)
Chris Lattner8b589fa2008-11-28 21:42:09 +000078 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
79 EE = I->second.end(); II != EE; ++II)
Chris Lattner0e575f42008-11-28 21:45:17 +000080 assert(*II != D && "Inst occurs in data structures");
Owen Anderson5fc4aba2007-12-08 01:37:09 +000081}
82
Owen Anderson78e02f72007-07-06 23:14:35 +000083/// getAnalysisUsage - Does not modify anything. It uses Alias Analysis.
84///
85void MemoryDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
86 AU.setPreservesAll();
87 AU.addRequiredTransitive<AliasAnalysis>();
88 AU.addRequiredTransitive<TargetData>();
89}
90
Owen Anderson642a9e32007-08-08 22:26:03 +000091/// getCallSiteDependency - Private helper for finding the local dependencies
92/// of a call site.
Owen Anderson9528f112007-08-09 04:42:44 +000093Instruction* MemoryDependenceAnalysis::getCallSiteDependency(CallSite C,
Chris Lattner0e575f42008-11-28 21:45:17 +000094 Instruction* start,
Owen Anderson80b1f092007-08-08 22:01:54 +000095 BasicBlock* block) {
Owen Anderson5f323202007-07-10 17:59:22 +000096
Owen Anderson5fc4aba2007-12-08 01:37:09 +000097 std::pair<Instruction*, bool>& cachedResult =
98 depGraphLocal[C.getInstruction()];
Owen Anderson5f323202007-07-10 17:59:22 +000099 AliasAnalysis& AA = getAnalysis<AliasAnalysis>();
100 TargetData& TD = getAnalysis<TargetData>();
101 BasicBlock::iterator blockBegin = C.getInstruction()->getParent()->begin();
102 BasicBlock::iterator QI = C.getInstruction();
103
Duncan Sandsaf8bc262008-09-11 19:41:10 +0000104 // If the starting point was specified, use it
Owen Andersondbbe8162007-08-07 00:33:45 +0000105 if (start) {
106 QI = start;
Dan Gohman6faaef52008-03-31 22:08:00 +0000107 blockBegin = start->getParent()->begin();
Owen Anderson642a9e32007-08-08 22:26:03 +0000108 // If the starting point wasn't specified, but the block was, use it
Owen Andersondbbe8162007-08-07 00:33:45 +0000109 } else if (!start && block) {
110 QI = block->end();
Dan Gohman6faaef52008-03-31 22:08:00 +0000111 blockBegin = block->begin();
Owen Andersondbbe8162007-08-07 00:33:45 +0000112 }
113
Owen Anderson642a9e32007-08-08 22:26:03 +0000114 // Walk backwards through the block, looking for dependencies
Owen Anderson5f323202007-07-10 17:59:22 +0000115 while (QI != blockBegin) {
116 --QI;
117
118 // If this inst is a memory op, get the pointer it accessed
119 Value* pointer = 0;
120 uint64_t pointerSize = 0;
121 if (StoreInst* S = dyn_cast<StoreInst>(QI)) {
122 pointer = S->getPointerOperand();
Duncan Sands514ab342007-11-01 20:53:16 +0000123 pointerSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
Owen Anderson5f323202007-07-10 17:59:22 +0000124 } else if (AllocationInst* AI = dyn_cast<AllocationInst>(QI)) {
125 pointer = AI;
126 if (ConstantInt* C = dyn_cast<ConstantInt>(AI->getArraySize()))
Chris Lattner57d40122008-11-28 21:16:44 +0000127 pointerSize = C->getZExtValue() *
Duncan Sands514ab342007-11-01 20:53:16 +0000128 TD.getABITypeSize(AI->getAllocatedType());
Owen Anderson5f323202007-07-10 17:59:22 +0000129 else
130 pointerSize = ~0UL;
Owen Anderson06b6e822007-07-10 18:43:15 +0000131 } else if (VAArgInst* V = dyn_cast<VAArgInst>(QI)) {
132 pointer = V->getOperand(0);
Duncan Sands514ab342007-11-01 20:53:16 +0000133 pointerSize = TD.getTypeStoreSize(V->getType());
Owen Anderson5f323202007-07-10 17:59:22 +0000134 } else if (FreeInst* F = dyn_cast<FreeInst>(QI)) {
135 pointer = F->getPointerOperand();
136
137 // FreeInsts erase the entire structure
138 pointerSize = ~0UL;
Owen Andersonff5a5352008-05-13 21:25:37 +0000139 } else if (CallSite::get(QI).getInstruction() != 0) {
Owen Anderson00a6d142007-11-26 02:26:36 +0000140 AliasAnalysis::ModRefBehavior result =
Duncan Sandsdff67102007-12-01 07:51:45 +0000141 AA.getModRefBehavior(CallSite::get(QI));
Owen Anderson241f6532008-04-17 05:36:50 +0000142 if (result != AliasAnalysis::DoesNotAccessMemory) {
Owen Andersondbbe8162007-08-07 00:33:45 +0000143 if (!start && !block) {
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000144 cachedResult.first = QI;
145 cachedResult.second = true;
Owen Andersondbbe8162007-08-07 00:33:45 +0000146 reverseDep[QI].insert(C.getInstruction());
147 }
Owen Anderson5f323202007-07-10 17:59:22 +0000148 return QI;
149 } else {
150 continue;
151 }
Owen Anderson202da142007-07-10 20:39:07 +0000152 } else
153 continue;
Owen Anderson5f323202007-07-10 17:59:22 +0000154
155 if (AA.getModRefInfo(C, pointer, pointerSize) != AliasAnalysis::NoModRef) {
Owen Andersondbbe8162007-08-07 00:33:45 +0000156 if (!start && !block) {
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000157 cachedResult.first = QI;
158 cachedResult.second = true;
Owen Andersondbbe8162007-08-07 00:33:45 +0000159 reverseDep[QI].insert(C.getInstruction());
160 }
Owen Anderson5f323202007-07-10 17:59:22 +0000161 return QI;
162 }
163 }
164
165 // No dependence found
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000166 cachedResult.first = NonLocal;
167 cachedResult.second = true;
Owen Andersondbbe8162007-08-07 00:33:45 +0000168 reverseDep[NonLocal].insert(C.getInstruction());
Owen Anderson5f323202007-07-10 17:59:22 +0000169 return NonLocal;
170}
171
Owen Anderson642a9e32007-08-08 22:26:03 +0000172/// nonLocalHelper - Private helper used to calculate non-local dependencies
173/// by doing DFS on the predecessors of a block to find its dependencies
Owen Anderson90660202007-08-01 22:01:54 +0000174void MemoryDependenceAnalysis::nonLocalHelper(Instruction* query,
Owen Anderson0cd32032007-07-25 19:57:03 +0000175 BasicBlock* block,
Owen Anderson80b1f092007-08-08 22:01:54 +0000176 DenseMap<BasicBlock*, Value*>& resp) {
Owen Anderson642a9e32007-08-08 22:26:03 +0000177 // Set of blocks that we've already visited in our DFS
Owen Anderson90660202007-08-01 22:01:54 +0000178 SmallPtrSet<BasicBlock*, 4> visited;
Owen Andersonce4d88a2007-09-21 03:53:52 +0000179 // If we're updating a dirtied cache entry, we don't need to reprocess
180 // already computed entries.
181 for (DenseMap<BasicBlock*, Value*>::iterator I = resp.begin(),
182 E = resp.end(); I != E; ++I)
183 if (I->second != Dirty)
184 visited.insert(I->first);
185
Owen Anderson642a9e32007-08-08 22:26:03 +0000186 // Current stack of the DFS
Owen Anderson90660202007-08-01 22:01:54 +0000187 SmallVector<BasicBlock*, 4> stack;
Owen Andersonf062f102008-04-10 22:13:32 +0000188 for (pred_iterator PI = pred_begin(block), PE = pred_end(block);
189 PI != PE; ++PI)
190 stack.push_back(*PI);
Owen Anderson4beedbd2007-07-24 21:52:37 +0000191
Owen Anderson642a9e32007-08-08 22:26:03 +0000192 // Do a basic DFS
Owen Anderson90660202007-08-01 22:01:54 +0000193 while (!stack.empty()) {
194 BasicBlock* BB = stack.back();
195
Owen Anderson642a9e32007-08-08 22:26:03 +0000196 // If we've already visited this block, no need to revist
Owen Anderson1c2763d2007-08-02 17:56:05 +0000197 if (visited.count(BB)) {
Owen Anderson90660202007-08-01 22:01:54 +0000198 stack.pop_back();
199 continue;
200 }
201
Owen Anderson642a9e32007-08-08 22:26:03 +0000202 // If we find a new block with a local dependency for query,
203 // then we insert the new dependency and backtrack.
Owen Anderson90660202007-08-01 22:01:54 +0000204 if (BB != block) {
Owen Anderson1c2763d2007-08-02 17:56:05 +0000205 visited.insert(BB);
206
Owen Anderson9528f112007-08-09 04:42:44 +0000207 Instruction* localDep = getDependency(query, 0, BB);
Owen Anderson90660202007-08-01 22:01:54 +0000208 if (localDep != NonLocal) {
Owen Anderson9528f112007-08-09 04:42:44 +0000209 resp.insert(std::make_pair(BB, localDep));
Owen Anderson1c2763d2007-08-02 17:56:05 +0000210 stack.pop_back();
211
Owen Anderson90660202007-08-01 22:01:54 +0000212 continue;
213 }
Owen Anderson642a9e32007-08-08 22:26:03 +0000214 // If we re-encounter the starting block, we still need to search it
215 // because there might be a dependency in the starting block AFTER
216 // the position of the query. This is necessary to get loops right.
Owen Andersonf062f102008-04-10 22:13:32 +0000217 } else if (BB == block) {
Owen Anderson1c2763d2007-08-02 17:56:05 +0000218 visited.insert(BB);
219
Owen Anderson9528f112007-08-09 04:42:44 +0000220 Instruction* localDep = getDependency(query, 0, BB);
Owen Anderson1c2763d2007-08-02 17:56:05 +0000221 if (localDep != query)
Owen Anderson9528f112007-08-09 04:42:44 +0000222 resp.insert(std::make_pair(BB, localDep));
Owen Anderson1c2763d2007-08-02 17:56:05 +0000223
224 stack.pop_back();
225
226 continue;
Owen Anderson90660202007-08-01 22:01:54 +0000227 }
228
Owen Anderson642a9e32007-08-08 22:26:03 +0000229 // If we didn't find anything, recurse on the precessors of this block
Tanya Lattner63aa1602008-02-06 00:54:55 +0000230 // Only do this for blocks with a small number of predecessors.
Owen Anderson90660202007-08-01 22:01:54 +0000231 bool predOnStack = false;
232 bool inserted = false;
Tanya Lattner63aa1602008-02-06 00:54:55 +0000233 if (std::distance(pred_begin(BB), pred_end(BB)) <= PredLimit) {
234 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
235 PI != PE; ++PI)
236 if (!visited.count(*PI)) {
237 stack.push_back(*PI);
238 inserted = true;
239 } else
240 predOnStack = true;
241 }
Owen Anderson90660202007-08-01 22:01:54 +0000242
Owen Anderson642a9e32007-08-08 22:26:03 +0000243 // If we inserted a new predecessor, then we'll come back to this block
Owen Anderson90660202007-08-01 22:01:54 +0000244 if (inserted)
245 continue;
Owen Anderson642a9e32007-08-08 22:26:03 +0000246 // If we didn't insert because we have no predecessors, then this
247 // query has no dependency at all.
Owen Anderson90660202007-08-01 22:01:54 +0000248 else if (!inserted && !predOnStack) {
Owen Anderson9528f112007-08-09 04:42:44 +0000249 resp.insert(std::make_pair(BB, None));
Owen Anderson642a9e32007-08-08 22:26:03 +0000250 // If we didn't insert because our predecessors are already on the stack,
251 // then we might still have a dependency, but it will be discovered during
252 // backtracking.
Owen Anderson90660202007-08-01 22:01:54 +0000253 } else if (!inserted && predOnStack){
Owen Anderson9528f112007-08-09 04:42:44 +0000254 resp.insert(std::make_pair(BB, NonLocal));
Owen Anderson90660202007-08-01 22:01:54 +0000255 }
256
257 stack.pop_back();
Owen Anderson4beedbd2007-07-24 21:52:37 +0000258 }
Owen Anderson4beedbd2007-07-24 21:52:37 +0000259}
260
Owen Anderson80b1f092007-08-08 22:01:54 +0000261/// getNonLocalDependency - Fills the passed-in map with the non-local
262/// dependencies of the queries. The map will contain NonLocal for
263/// blocks between the query and its dependencies.
Owen Anderson90660202007-08-01 22:01:54 +0000264void MemoryDependenceAnalysis::getNonLocalDependency(Instruction* query,
Owen Anderson80b1f092007-08-08 22:01:54 +0000265 DenseMap<BasicBlock*, Value*>& resp) {
Owen Anderson4d13de42007-08-16 21:27:05 +0000266 if (depGraphNonLocal.count(query)) {
Owen Andersonce4d88a2007-09-21 03:53:52 +0000267 DenseMap<BasicBlock*, Value*>& cached = depGraphNonLocal[query];
Owen Anderson7fad7e32007-09-09 21:43:49 +0000268 NumCacheNonlocal++;
Owen Andersonce4d88a2007-09-21 03:53:52 +0000269
270 SmallVector<BasicBlock*, 4> dirtied;
271 for (DenseMap<BasicBlock*, Value*>::iterator I = cached.begin(),
272 E = cached.end(); I != E; ++I)
273 if (I->second == Dirty)
274 dirtied.push_back(I->first);
275
276 for (SmallVector<BasicBlock*, 4>::iterator I = dirtied.begin(),
277 E = dirtied.end(); I != E; ++I) {
278 Instruction* localDep = getDependency(query, 0, *I);
279 if (localDep != NonLocal)
280 cached[*I] = localDep;
281 else {
282 cached.erase(*I);
283 nonLocalHelper(query, *I, cached);
284 }
285 }
286
287 resp = cached;
288
Owen Anderson6bd15ce2008-06-01 21:03:52 +0000289 // Update the reverse non-local dependency cache
290 for (DenseMap<BasicBlock*, Value*>::iterator I = resp.begin(), E = resp.end();
291 I != E; ++I)
292 reverseDepNonLocal[I->second].insert(query);
293
Owen Anderson4d13de42007-08-16 21:27:05 +0000294 return;
Owen Anderson7fad7e32007-09-09 21:43:49 +0000295 } else
296 NumUncacheNonlocal++;
Owen Anderson4d13de42007-08-16 21:27:05 +0000297
Owen Anderson7fad7e32007-09-09 21:43:49 +0000298 // If not, go ahead and search for non-local deps.
Owen Anderson90660202007-08-01 22:01:54 +0000299 nonLocalHelper(query, query->getParent(), resp);
Owen Anderson4d13de42007-08-16 21:27:05 +0000300
301 // Update the non-local dependency cache
302 for (DenseMap<BasicBlock*, Value*>::iterator I = resp.begin(), E = resp.end();
303 I != E; ++I) {
304 depGraphNonLocal[query].insert(*I);
305 reverseDepNonLocal[I->second].insert(query);
306 }
Owen Anderson4beedbd2007-07-24 21:52:37 +0000307}
308
Owen Anderson78e02f72007-07-06 23:14:35 +0000309/// getDependency - Return the instruction on which a memory operation
Dan Gohmanc04575f2008-04-10 23:02:38 +0000310/// depends. The local parameter indicates if the query should only
Owen Anderson6b278fc2007-07-10 17:08:11 +0000311/// evaluate dependencies within the same basic block.
Owen Anderson9528f112007-08-09 04:42:44 +0000312Instruction* MemoryDependenceAnalysis::getDependency(Instruction* query,
Owen Andersonfaac5182007-07-16 21:52:50 +0000313 Instruction* start,
Owen Anderson4beedbd2007-07-24 21:52:37 +0000314 BasicBlock* block) {
Owen Anderson78e02f72007-07-06 23:14:35 +0000315 // Start looking for dependencies with the queried inst
316 BasicBlock::iterator QI = query;
317
318 // Check for a cached result
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000319 std::pair<Instruction*, bool>& cachedResult = depGraphLocal[query];
Owen Anderson78e02f72007-07-06 23:14:35 +0000320 // If we have a _confirmed_ cached entry, return it
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000321 if (!block && !start) {
322 if (cachedResult.second)
323 return cachedResult.first;
324 else if (cachedResult.first && cachedResult.first != NonLocal)
325 // If we have an unconfirmed cached entry, we can start our search from there
326 QI = cachedResult.first;
327 }
Owen Anderson78e02f72007-07-06 23:14:35 +0000328
Owen Andersonfaac5182007-07-16 21:52:50 +0000329 if (start)
330 QI = start;
Owen Anderson0cd32032007-07-25 19:57:03 +0000331 else if (!start && block)
332 QI = block->end();
Owen Andersonfaac5182007-07-16 21:52:50 +0000333
Owen Anderson78e02f72007-07-06 23:14:35 +0000334 AliasAnalysis& AA = getAnalysis<AliasAnalysis>();
Owen Anderson6b278fc2007-07-10 17:08:11 +0000335 TargetData& TD = getAnalysis<TargetData>();
Owen Anderson78e02f72007-07-06 23:14:35 +0000336
337 // Get the pointer value for which dependence will be determined
338 Value* dependee = 0;
Owen Anderson6b278fc2007-07-10 17:08:11 +0000339 uint64_t dependeeSize = 0;
Owen Andersone314eb32007-07-10 18:11:42 +0000340 bool queryIsVolatile = false;
Owen Andersonfaac5182007-07-16 21:52:50 +0000341 if (StoreInst* S = dyn_cast<StoreInst>(query)) {
Owen Anderson78e02f72007-07-06 23:14:35 +0000342 dependee = S->getPointerOperand();
Duncan Sands514ab342007-11-01 20:53:16 +0000343 dependeeSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
Owen Andersone314eb32007-07-10 18:11:42 +0000344 queryIsVolatile = S->isVolatile();
Owen Andersonfaac5182007-07-16 21:52:50 +0000345 } else if (LoadInst* L = dyn_cast<LoadInst>(query)) {
Owen Anderson78e02f72007-07-06 23:14:35 +0000346 dependee = L->getPointerOperand();
Duncan Sands514ab342007-11-01 20:53:16 +0000347 dependeeSize = TD.getTypeStoreSize(L->getType());
Owen Andersone314eb32007-07-10 18:11:42 +0000348 queryIsVolatile = L->isVolatile();
Owen Andersonfaac5182007-07-16 21:52:50 +0000349 } else if (VAArgInst* V = dyn_cast<VAArgInst>(query)) {
Owen Anderson06b6e822007-07-10 18:43:15 +0000350 dependee = V->getOperand(0);
Duncan Sands514ab342007-11-01 20:53:16 +0000351 dependeeSize = TD.getTypeStoreSize(V->getType());
Owen Andersonfaac5182007-07-16 21:52:50 +0000352 } else if (FreeInst* F = dyn_cast<FreeInst>(query)) {
Owen Anderson78e02f72007-07-06 23:14:35 +0000353 dependee = F->getPointerOperand();
Owen Anderson6b278fc2007-07-10 17:08:11 +0000354
355 // FreeInsts erase the entire structure, not just a field
356 dependeeSize = ~0UL;
Owen Andersonfaac5182007-07-16 21:52:50 +0000357 } else if (CallSite::get(query).getInstruction() != 0)
Owen Andersondbbe8162007-08-07 00:33:45 +0000358 return getCallSiteDependency(CallSite::get(query), start, block);
Owen Anderson5f323202007-07-10 17:59:22 +0000359 else if (isa<AllocationInst>(query))
Owen Anderson78e02f72007-07-06 23:14:35 +0000360 return None;
Owen Anderson7a616a12007-07-10 17:25:03 +0000361 else
Owen Anderson6b278fc2007-07-10 17:08:11 +0000362 return None;
Owen Anderson78e02f72007-07-06 23:14:35 +0000363
Owen Anderson4beedbd2007-07-24 21:52:37 +0000364 BasicBlock::iterator blockBegin = block ? block->begin()
365 : query->getParent()->begin();
Owen Anderson78e02f72007-07-06 23:14:35 +0000366
Owen Anderson642a9e32007-08-08 22:26:03 +0000367 // Walk backwards through the basic block, looking for dependencies
Owen Anderson78e02f72007-07-06 23:14:35 +0000368 while (QI != blockBegin) {
Owen Anderson6b278fc2007-07-10 17:08:11 +0000369 --QI;
370
Owen Anderson78e02f72007-07-06 23:14:35 +0000371 // If this inst is a memory op, get the pointer it accessed
372 Value* pointer = 0;
Owen Anderson6b278fc2007-07-10 17:08:11 +0000373 uint64_t pointerSize = 0;
374 if (StoreInst* S = dyn_cast<StoreInst>(QI)) {
Owen Andersone314eb32007-07-10 18:11:42 +0000375 // All volatile loads/stores depend on each other
376 if (queryIsVolatile && S->isVolatile()) {
Owen Andersondbbe8162007-08-07 00:33:45 +0000377 if (!start && !block) {
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000378 cachedResult.first = S;
379 cachedResult.second = true;
Owen Andersondbbe8162007-08-07 00:33:45 +0000380 reverseDep[S].insert(query);
Owen Andersonfaac5182007-07-16 21:52:50 +0000381 }
382
Owen Andersone314eb32007-07-10 18:11:42 +0000383 return S;
384 }
385
Owen Anderson78e02f72007-07-06 23:14:35 +0000386 pointer = S->getPointerOperand();
Duncan Sands514ab342007-11-01 20:53:16 +0000387 pointerSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
Owen Anderson6b278fc2007-07-10 17:08:11 +0000388 } else if (LoadInst* L = dyn_cast<LoadInst>(QI)) {
Owen Andersone314eb32007-07-10 18:11:42 +0000389 // All volatile loads/stores depend on each other
390 if (queryIsVolatile && L->isVolatile()) {
Owen Andersondbbe8162007-08-07 00:33:45 +0000391 if (!start && !block) {
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000392 cachedResult.first = L;
393 cachedResult.second = true;
Owen Andersondbbe8162007-08-07 00:33:45 +0000394 reverseDep[L].insert(query);
Owen Andersonfaac5182007-07-16 21:52:50 +0000395 }
396
Owen Andersone314eb32007-07-10 18:11:42 +0000397 return L;
398 }
399
Owen Anderson78e02f72007-07-06 23:14:35 +0000400 pointer = L->getPointerOperand();
Duncan Sands514ab342007-11-01 20:53:16 +0000401 pointerSize = TD.getTypeStoreSize(L->getType());
Owen Anderson6b278fc2007-07-10 17:08:11 +0000402 } else if (AllocationInst* AI = dyn_cast<AllocationInst>(QI)) {
403 pointer = AI;
Owen Anderson7a616a12007-07-10 17:25:03 +0000404 if (ConstantInt* C = dyn_cast<ConstantInt>(AI->getArraySize()))
Chris Lattner57d40122008-11-28 21:16:44 +0000405 pointerSize = C->getZExtValue() *
Duncan Sands514ab342007-11-01 20:53:16 +0000406 TD.getABITypeSize(AI->getAllocatedType());
Owen Anderson6b278fc2007-07-10 17:08:11 +0000407 else
408 pointerSize = ~0UL;
Owen Anderson06b6e822007-07-10 18:43:15 +0000409 } else if (VAArgInst* V = dyn_cast<VAArgInst>(QI)) {
410 pointer = V->getOperand(0);
Duncan Sands514ab342007-11-01 20:53:16 +0000411 pointerSize = TD.getTypeStoreSize(V->getType());
Owen Anderson6b278fc2007-07-10 17:08:11 +0000412 } else if (FreeInst* F = dyn_cast<FreeInst>(QI)) {
Owen Anderson78e02f72007-07-06 23:14:35 +0000413 pointer = F->getPointerOperand();
Owen Anderson6b278fc2007-07-10 17:08:11 +0000414
415 // FreeInsts erase the entire structure
416 pointerSize = ~0UL;
Owen Anderson5f323202007-07-10 17:59:22 +0000417 } else if (CallSite::get(QI).getInstruction() != 0) {
Owen Anderson80b1f092007-08-08 22:01:54 +0000418 // Call insts need special handling. Check if they can modify our pointer
Owen Anderson8f353152007-08-06 23:26:03 +0000419 AliasAnalysis::ModRefResult MR = AA.getModRefInfo(CallSite::get(QI),
420 dependee, dependeeSize);
421
422 if (MR != AliasAnalysis::NoModRef) {
423 // Loads don't depend on read-only calls
424 if (isa<LoadInst>(query) && MR == AliasAnalysis::Ref)
425 continue;
426
Owen Andersondbbe8162007-08-07 00:33:45 +0000427 if (!start && !block) {
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000428 cachedResult.first = QI;
429 cachedResult.second = true;
Owen Andersondbbe8162007-08-07 00:33:45 +0000430 reverseDep[QI].insert(query);
Owen Andersonfaac5182007-07-16 21:52:50 +0000431 }
432
Owen Anderson5f323202007-07-10 17:59:22 +0000433 return QI;
Owen Anderson7a616a12007-07-10 17:25:03 +0000434 } else {
435 continue;
436 }
Owen Anderson78e02f72007-07-06 23:14:35 +0000437 }
438
439 // If we found a pointer, check if it could be the same as our pointer
440 if (pointer) {
Owen Anderson6b278fc2007-07-10 17:08:11 +0000441 AliasAnalysis::AliasResult R = AA.alias(pointer, pointerSize,
442 dependee, dependeeSize);
Owen Anderson78e02f72007-07-06 23:14:35 +0000443
444 if (R != AliasAnalysis::NoAlias) {
Owen Anderson8f353152007-08-06 23:26:03 +0000445 // May-alias loads don't depend on each other
446 if (isa<LoadInst>(query) && isa<LoadInst>(QI) &&
447 R == AliasAnalysis::MayAlias)
448 continue;
449
Owen Andersondbbe8162007-08-07 00:33:45 +0000450 if (!start && !block) {
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000451 cachedResult.first = QI;
452 cachedResult.second = true;
Owen Andersondbbe8162007-08-07 00:33:45 +0000453 reverseDep[QI].insert(query);
Owen Andersonfaac5182007-07-16 21:52:50 +0000454 }
455
Owen Anderson78e02f72007-07-06 23:14:35 +0000456 return QI;
457 }
458 }
Owen Anderson78e02f72007-07-06 23:14:35 +0000459 }
460
461 // If we found nothing, return the non-local flag
Owen Andersondbbe8162007-08-07 00:33:45 +0000462 if (!start && !block) {
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000463 cachedResult.first = NonLocal;
464 cachedResult.second = true;
Owen Andersondbbe8162007-08-07 00:33:45 +0000465 reverseDep[NonLocal].insert(query);
Owen Andersonfaac5182007-07-16 21:52:50 +0000466 }
Owen Anderson78e02f72007-07-06 23:14:35 +0000467
468 return NonLocal;
469}
470
Owen Anderson30b4bd42008-02-12 21:15:18 +0000471/// dropInstruction - Remove an instruction from the analysis, making
472/// absolutely conservative assumptions when updating the cache. This is
473/// useful, for example when an instruction is changed rather than removed.
474void MemoryDependenceAnalysis::dropInstruction(Instruction* drop) {
475 depMapType::iterator depGraphEntry = depGraphLocal.find(drop);
476 if (depGraphEntry != depGraphLocal.end())
477 reverseDep[depGraphEntry->second.first].erase(drop);
478
479 // Drop dependency information for things that depended on this instr
480 SmallPtrSet<Instruction*, 4>& set = reverseDep[drop];
481 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
482 I != E; ++I)
483 depGraphLocal.erase(*I);
484
485 depGraphLocal.erase(drop);
486 reverseDep.erase(drop);
487
488 for (DenseMap<BasicBlock*, Value*>::iterator DI =
489 depGraphNonLocal[drop].begin(), DE = depGraphNonLocal[drop].end();
490 DI != DE; ++DI)
491 if (DI->second != None)
492 reverseDepNonLocal[DI->second].erase(drop);
493
494 if (reverseDepNonLocal.count(drop)) {
495 SmallPtrSet<Instruction*, 4>& set = reverseDepNonLocal[drop];
496 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
497 I != E; ++I)
498 for (DenseMap<BasicBlock*, Value*>::iterator DI =
499 depGraphNonLocal[*I].begin(), DE = depGraphNonLocal[*I].end();
500 DI != DE; ++DI)
501 if (DI->second == drop)
502 DI->second = Dirty;
503 }
504
505 reverseDepNonLocal.erase(drop);
506 nonLocalDepMapType::iterator I = depGraphNonLocal.find(drop);
507 if (I != depGraphNonLocal.end())
508 depGraphNonLocal.erase(I);
509}
510
Owen Anderson78e02f72007-07-06 23:14:35 +0000511/// removeInstruction - Remove an instruction from the dependence analysis,
512/// updating the dependence of instructions that previously depended on it.
Owen Anderson642a9e32007-08-08 22:26:03 +0000513/// This method attempts to keep the cache coherent using the reverse map.
Chris Lattner5f589dc2008-11-28 22:04:47 +0000514void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) {
Chris Lattner5f589dc2008-11-28 22:04:47 +0000515 // Walk through the Non-local dependencies, removing this one as the value
516 // for any cached queries.
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000517 for (DenseMap<BasicBlock*, Value*>::iterator DI =
Chris Lattner5f589dc2008-11-28 22:04:47 +0000518 depGraphNonLocal[RemInst].begin(), DE = depGraphNonLocal[RemInst].end();
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000519 DI != DE; ++DI)
520 if (DI->second != None)
Chris Lattner5f589dc2008-11-28 22:04:47 +0000521 reverseDepNonLocal[DI->second].erase(RemInst);
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000522
Chris Lattnerbaad8882008-11-28 22:28:27 +0000523 // Shortly after this, we will look for things that depend on RemInst. In
524 // order to update these, we'll need a new dependency to base them on. We
525 // could completely delete any entries that depend on this, but it is better
526 // to make a more accurate approximation where possible. Compute that better
527 // approximation if we can.
528 Instruction *NewDependency = 0;
529 bool NewDependencyConfirmed = false;
530
Chris Lattner5f589dc2008-11-28 22:04:47 +0000531 // If we have a cached local dependence query for this instruction, remove it.
Chris Lattnerbaad8882008-11-28 22:28:27 +0000532 //
533 depMapType::iterator LocalDepEntry = depGraphLocal.find(RemInst);
534 if (LocalDepEntry != depGraphLocal.end()) {
535 Instruction *LocalDepInst = LocalDepEntry->second.first;
536 bool IsConfirmed = LocalDepEntry->second.second;
Owen Anderson9a8ff8c2008-01-30 01:24:05 +0000537
Chris Lattnerbaad8882008-11-28 22:28:27 +0000538 // Remove this local dependency info.
539 depGraphLocal.erase(LocalDepEntry);
Chris Lattner5f589dc2008-11-28 22:04:47 +0000540
Chris Lattnerbaad8882008-11-28 22:28:27 +0000541 // Remove us from DepInst's reverse set now that the local dep info is gone.
542 reverseDep[LocalDepInst].erase(RemInst);
543
544 // If we have unconfirmed info, don't trust it.
545 if (IsConfirmed) {
546 // If we have a confirmed non-local flag, use it.
547 if (LocalDepInst == NonLocal || LocalDepInst == None) {
548 NewDependency = LocalDepInst;
549 NewDependencyConfirmed = true;
550 } else {
551 // If we have dep info for RemInst, set them to it.
552 NewDependency = next(BasicBlock::iterator(LocalDepInst));
553
554 // Don't use RI for the new dependency!
555 if (NewDependency == RemInst)
556 NewDependency = 0;
557 }
David Greenedf464192007-07-31 20:01:27 +0000558 }
Owen Andersona8701a62008-02-05 04:34:03 +0000559 }
560
Chris Lattnerbaad8882008-11-28 22:28:27 +0000561 // If we don't already have a local dependency answer for this instruction,
562 // use the immediate successor of RemInst. We use the successor because
563 // getDependence starts by checking the immediate predecessor of what is in
564 // the cache.
565 if (NewDependency == 0)
566 NewDependency = next(BasicBlock::iterator(RemInst));
567
Chris Lattner5f589dc2008-11-28 22:04:47 +0000568 SmallPtrSet<Instruction*, 4>& set = reverseDep[RemInst];
Owen Andersona8701a62008-02-05 04:34:03 +0000569 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
570 I != E; ++I) {
571 // Insert the new dependencies
572 // Mark it as unconfirmed as long as it is not the non-local flag
Chris Lattnerbaad8882008-11-28 22:28:27 +0000573 depGraphLocal[*I] = std::make_pair(NewDependency, NewDependencyConfirmed);
Owen Anderson78e02f72007-07-06 23:14:35 +0000574 }
Owen Anderson4d13de42007-08-16 21:27:05 +0000575
Chris Lattner5f589dc2008-11-28 22:04:47 +0000576 reverseDep.erase(RemInst);
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000577
Chris Lattner5f589dc2008-11-28 22:04:47 +0000578 if (reverseDepNonLocal.count(RemInst)) {
579 SmallPtrSet<Instruction*, 4>& set = reverseDepNonLocal[RemInst];
Owen Anderson4d13de42007-08-16 21:27:05 +0000580 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
581 I != E; ++I)
Owen Andersonce4d88a2007-09-21 03:53:52 +0000582 for (DenseMap<BasicBlock*, Value*>::iterator DI =
583 depGraphNonLocal[*I].begin(), DE = depGraphNonLocal[*I].end();
584 DI != DE; ++DI)
Chris Lattner5f589dc2008-11-28 22:04:47 +0000585 if (DI->second == RemInst)
Owen Andersonce4d88a2007-09-21 03:53:52 +0000586 DI->second = Dirty;
Owen Anderson4d13de42007-08-16 21:27:05 +0000587
Owen Anderson4d13de42007-08-16 21:27:05 +0000588 }
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000589
Chris Lattner5f589dc2008-11-28 22:04:47 +0000590 reverseDepNonLocal.erase(RemInst);
591 depGraphNonLocal.erase(RemInst);
Owen Anderson78e02f72007-07-06 23:14:35 +0000592
Chris Lattner5f589dc2008-11-28 22:04:47 +0000593 getAnalysis<AliasAnalysis>().deleteValue(RemInst);
Chris Lattner0e575f42008-11-28 21:45:17 +0000594
Chris Lattner5f589dc2008-11-28 22:04:47 +0000595 DEBUG(verifyRemoved(RemInst));
Owen Anderson78e02f72007-07-06 23:14:35 +0000596}