blob: 314d6e4b3a7e0e995a731ac76c14e2f7dd3384d9 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- MemoryDependenceAnalysis.cpp - Mem Deps Implementation --*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Andersonafe840e2007-08-08 22:01:54 +000012// alias analysis information, and tries to provide a lazy, caching interface to
Dan Gohmanf17a25c2007-07-18 16:29:46 +000013// a common kind of alias information query.
14//
15//===----------------------------------------------------------------------===//
16
17#include "llvm/Analysis/MemoryDependenceAnalysis.h"
18#include "llvm/Constants.h"
19#include "llvm/Instructions.h"
20#include "llvm/Function.h"
21#include "llvm/Analysis/AliasAnalysis.h"
Owen Anderson4c295472007-07-24 21:52:37 +000022#include "llvm/Support/CFG.h"
Tanya Lattner8edb2b72008-02-06 00:54:55 +000023#include "llvm/Support/CommandLine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024#include "llvm/Target/TargetData.h"
Owen Andersond6c7fea2007-09-09 21:43:49 +000025#include "llvm/ADT/Statistic.h"
26
27#define DEBUG_TYPE "memdep"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028
29using namespace llvm;
30
Tanya Lattner8edb2b72008-02-06 00:54:55 +000031namespace {
32 // 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).
34 cl::opt<int>
35 PredLimit("nonlocaldep-threshold", cl::Hidden, cl::init(50),
36 cl::desc("Control the calculation of non-local"
37 "dependencies (default = 50)"));
38}
39
Owen Andersond6c7fea2007-09-09 21:43:49 +000040STATISTIC(NumCacheNonlocal, "Number of cached non-local responses");
41STATISTIC(NumUncacheNonlocal, "Number of uncached non-local responses");
42
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043char MemoryDependenceAnalysis::ID = 0;
44
Owen Anderson935e39b2007-08-09 04:42:44 +000045Instruction* const MemoryDependenceAnalysis::NonLocal = (Instruction*)-3;
46Instruction* const MemoryDependenceAnalysis::None = (Instruction*)-4;
Owen Anderson7e447562007-09-19 16:13:57 +000047Instruction* const MemoryDependenceAnalysis::Dirty = (Instruction*)-5;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048
49// Register this pass...
50static RegisterPass<MemoryDependenceAnalysis> X("memdep",
Devang Patelbdfd1862008-03-20 02:25:21 +000051 "Memory Dependence Analysis", false, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052
Owen Andersonc772be72007-12-08 01:37:09 +000053void MemoryDependenceAnalysis::ping(Instruction *D) {
54 for (depMapType::iterator I = depGraphLocal.begin(), E = depGraphLocal.end();
55 I != E; ++I) {
56 assert(I->first != D);
57 assert(I->second.first != D);
58 }
59
60 for (nonLocalDepMapType::iterator I = depGraphNonLocal.begin(), E = depGraphNonLocal.end();
61 I != E; ++I) {
62 assert(I->first != D);
63 }
64
65 for (reverseDepMapType::iterator I = reverseDep.begin(), E = reverseDep.end();
66 I != E; ++I)
67 for (SmallPtrSet<Instruction*, 4>::iterator II = I->second.begin(), EE = I->second.end();
68 II != EE; ++II)
69 assert(*II != D);
70
71 for (reverseDepMapType::iterator I = reverseDepNonLocal.begin(), E = reverseDepNonLocal.end();
72 I != E; ++I)
73 for (SmallPtrSet<Instruction*, 4>::iterator II = I->second.begin(), EE = I->second.end();
74 II != EE; ++II)
75 assert(*II != D);
76}
77
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078/// getAnalysisUsage - Does not modify anything. It uses Alias Analysis.
79///
80void MemoryDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
81 AU.setPreservesAll();
82 AU.addRequiredTransitive<AliasAnalysis>();
83 AU.addRequiredTransitive<TargetData>();
84}
85
Owen Anderson3de3c532007-08-08 22:26:03 +000086/// getCallSiteDependency - Private helper for finding the local dependencies
87/// of a call site.
Owen Anderson935e39b2007-08-09 04:42:44 +000088Instruction* MemoryDependenceAnalysis::getCallSiteDependency(CallSite C,
Owen Andersonafe840e2007-08-08 22:01:54 +000089 Instruction* start,
90 BasicBlock* block) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091
Owen Andersonc772be72007-12-08 01:37:09 +000092 std::pair<Instruction*, bool>& cachedResult =
93 depGraphLocal[C.getInstruction()];
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 AliasAnalysis& AA = getAnalysis<AliasAnalysis>();
95 TargetData& TD = getAnalysis<TargetData>();
96 BasicBlock::iterator blockBegin = C.getInstruction()->getParent()->begin();
97 BasicBlock::iterator QI = C.getInstruction();
98
Owen Anderson3de3c532007-08-08 22:26:03 +000099 // If the starting point was specifiy, use it
Owen Andersone84f4bc2007-08-07 00:33:45 +0000100 if (start) {
101 QI = start;
Dan Gohmand4752682008-03-31 22:08:00 +0000102 blockBegin = start->getParent()->begin();
Owen Anderson3de3c532007-08-08 22:26:03 +0000103 // If the starting point wasn't specified, but the block was, use it
Owen Andersone84f4bc2007-08-07 00:33:45 +0000104 } else if (!start && block) {
105 QI = block->end();
Dan Gohmand4752682008-03-31 22:08:00 +0000106 blockBegin = block->begin();
Owen Andersone84f4bc2007-08-07 00:33:45 +0000107 }
108
Owen Anderson3de3c532007-08-08 22:26:03 +0000109 // Walk backwards through the block, looking for dependencies
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 while (QI != blockBegin) {
111 --QI;
112
113 // If this inst is a memory op, get the pointer it accessed
114 Value* pointer = 0;
115 uint64_t pointerSize = 0;
116 if (StoreInst* S = dyn_cast<StoreInst>(QI)) {
117 pointer = S->getPointerOperand();
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000118 pointerSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 } else if (AllocationInst* AI = dyn_cast<AllocationInst>(QI)) {
120 pointer = AI;
121 if (ConstantInt* C = dyn_cast<ConstantInt>(AI->getArraySize()))
Owen Andersonafe840e2007-08-08 22:01:54 +0000122 pointerSize = C->getZExtValue() * \
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000123 TD.getABITypeSize(AI->getAllocatedType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 else
125 pointerSize = ~0UL;
126 } else if (VAArgInst* V = dyn_cast<VAArgInst>(QI)) {
127 pointer = V->getOperand(0);
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000128 pointerSize = TD.getTypeStoreSize(V->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 } else if (FreeInst* F = dyn_cast<FreeInst>(QI)) {
130 pointer = F->getPointerOperand();
131
132 // FreeInsts erase the entire structure
133 pointerSize = ~0UL;
Duncan Sands00b24b52007-12-01 07:51:45 +0000134 } else if (isa<CallInst>(QI)) {
Owen Anderson8b6f04e2007-11-26 02:26:36 +0000135 AliasAnalysis::ModRefBehavior result =
Duncan Sands00b24b52007-12-01 07:51:45 +0000136 AA.getModRefBehavior(CallSite::get(QI));
Owen Anderson8b6f04e2007-11-26 02:26:36 +0000137 if (result != AliasAnalysis::DoesNotAccessMemory &&
138 result != AliasAnalysis::OnlyReadsMemory) {
Owen Andersone84f4bc2007-08-07 00:33:45 +0000139 if (!start && !block) {
Owen Andersonc772be72007-12-08 01:37:09 +0000140 cachedResult.first = QI;
141 cachedResult.second = true;
Owen Andersone84f4bc2007-08-07 00:33:45 +0000142 reverseDep[QI].insert(C.getInstruction());
143 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 return QI;
145 } else {
146 continue;
147 }
148 } else
149 continue;
150
151 if (AA.getModRefInfo(C, pointer, pointerSize) != AliasAnalysis::NoModRef) {
Owen Andersone84f4bc2007-08-07 00:33:45 +0000152 if (!start && !block) {
Owen Andersonc772be72007-12-08 01:37:09 +0000153 cachedResult.first = QI;
154 cachedResult.second = true;
Owen Andersone84f4bc2007-08-07 00:33:45 +0000155 reverseDep[QI].insert(C.getInstruction());
156 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 return QI;
158 }
159 }
160
161 // No dependence found
Owen Andersonc772be72007-12-08 01:37:09 +0000162 cachedResult.first = NonLocal;
163 cachedResult.second = true;
Owen Andersone84f4bc2007-08-07 00:33:45 +0000164 reverseDep[NonLocal].insert(C.getInstruction());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 return NonLocal;
166}
167
Owen Anderson3de3c532007-08-08 22:26:03 +0000168/// nonLocalHelper - Private helper used to calculate non-local dependencies
169/// by doing DFS on the predecessors of a block to find its dependencies
Owen Anderson3f75d122007-08-01 22:01:54 +0000170void MemoryDependenceAnalysis::nonLocalHelper(Instruction* query,
Owen Anderson5d72a422007-07-25 19:57:03 +0000171 BasicBlock* block,
Owen Andersonafe840e2007-08-08 22:01:54 +0000172 DenseMap<BasicBlock*, Value*>& resp) {
Owen Anderson3de3c532007-08-08 22:26:03 +0000173 // Set of blocks that we've already visited in our DFS
Owen Anderson3f75d122007-08-01 22:01:54 +0000174 SmallPtrSet<BasicBlock*, 4> visited;
Owen Anderson05749072007-09-21 03:53:52 +0000175 // If we're updating a dirtied cache entry, we don't need to reprocess
176 // already computed entries.
177 for (DenseMap<BasicBlock*, Value*>::iterator I = resp.begin(),
178 E = resp.end(); I != E; ++I)
179 if (I->second != Dirty)
180 visited.insert(I->first);
181
Owen Anderson3de3c532007-08-08 22:26:03 +0000182 // Current stack of the DFS
Owen Anderson3f75d122007-08-01 22:01:54 +0000183 SmallVector<BasicBlock*, 4> stack;
Owen Anderson2fecdf12008-04-10 22:13:32 +0000184 for (pred_iterator PI = pred_begin(block), PE = pred_end(block);
185 PI != PE; ++PI)
186 stack.push_back(*PI);
Owen Anderson4c295472007-07-24 21:52:37 +0000187
Owen Anderson3de3c532007-08-08 22:26:03 +0000188 // Do a basic DFS
Owen Anderson3f75d122007-08-01 22:01:54 +0000189 while (!stack.empty()) {
190 BasicBlock* BB = stack.back();
191
Owen Anderson3de3c532007-08-08 22:26:03 +0000192 // If we've already visited this block, no need to revist
Owen Andersonc6a31b92007-08-02 17:56:05 +0000193 if (visited.count(BB)) {
Owen Anderson3f75d122007-08-01 22:01:54 +0000194 stack.pop_back();
195 continue;
196 }
197
Owen Anderson3de3c532007-08-08 22:26:03 +0000198 // If we find a new block with a local dependency for query,
199 // then we insert the new dependency and backtrack.
Owen Anderson3f75d122007-08-01 22:01:54 +0000200 if (BB != block) {
Owen Andersonc6a31b92007-08-02 17:56:05 +0000201 visited.insert(BB);
202
Owen Anderson935e39b2007-08-09 04:42:44 +0000203 Instruction* localDep = getDependency(query, 0, BB);
Owen Anderson3f75d122007-08-01 22:01:54 +0000204 if (localDep != NonLocal) {
Owen Anderson935e39b2007-08-09 04:42:44 +0000205 resp.insert(std::make_pair(BB, localDep));
Owen Andersonc6a31b92007-08-02 17:56:05 +0000206 stack.pop_back();
207
Owen Anderson3f75d122007-08-01 22:01:54 +0000208 continue;
209 }
Owen Anderson3de3c532007-08-08 22:26:03 +0000210 // If we re-encounter the starting block, we still need to search it
211 // because there might be a dependency in the starting block AFTER
212 // the position of the query. This is necessary to get loops right.
Owen Anderson2fecdf12008-04-10 22:13:32 +0000213 } else if (BB == block) {
Owen Andersonc6a31b92007-08-02 17:56:05 +0000214 visited.insert(BB);
215
Owen Anderson935e39b2007-08-09 04:42:44 +0000216 Instruction* localDep = getDependency(query, 0, BB);
Owen Andersonc6a31b92007-08-02 17:56:05 +0000217 if (localDep != query)
Owen Anderson935e39b2007-08-09 04:42:44 +0000218 resp.insert(std::make_pair(BB, localDep));
Owen Andersonc6a31b92007-08-02 17:56:05 +0000219
220 stack.pop_back();
221
222 continue;
Owen Anderson3f75d122007-08-01 22:01:54 +0000223 }
224
Owen Anderson3de3c532007-08-08 22:26:03 +0000225 // If we didn't find anything, recurse on the precessors of this block
Tanya Lattner8edb2b72008-02-06 00:54:55 +0000226 // Only do this for blocks with a small number of predecessors.
Owen Anderson3f75d122007-08-01 22:01:54 +0000227 bool predOnStack = false;
228 bool inserted = false;
Tanya Lattner8edb2b72008-02-06 00:54:55 +0000229 if (std::distance(pred_begin(BB), pred_end(BB)) <= PredLimit) {
230 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
231 PI != PE; ++PI)
232 if (!visited.count(*PI)) {
233 stack.push_back(*PI);
234 inserted = true;
235 } else
236 predOnStack = true;
237 }
Owen Anderson3f75d122007-08-01 22:01:54 +0000238
Owen Anderson3de3c532007-08-08 22:26:03 +0000239 // If we inserted a new predecessor, then we'll come back to this block
Owen Anderson3f75d122007-08-01 22:01:54 +0000240 if (inserted)
241 continue;
Owen Anderson3de3c532007-08-08 22:26:03 +0000242 // If we didn't insert because we have no predecessors, then this
243 // query has no dependency at all.
Owen Anderson3f75d122007-08-01 22:01:54 +0000244 else if (!inserted && !predOnStack) {
Owen Anderson935e39b2007-08-09 04:42:44 +0000245 resp.insert(std::make_pair(BB, None));
Owen Anderson3de3c532007-08-08 22:26:03 +0000246 // If we didn't insert because our predecessors are already on the stack,
247 // then we might still have a dependency, but it will be discovered during
248 // backtracking.
Owen Anderson3f75d122007-08-01 22:01:54 +0000249 } else if (!inserted && predOnStack){
Owen Anderson935e39b2007-08-09 04:42:44 +0000250 resp.insert(std::make_pair(BB, NonLocal));
Owen Anderson3f75d122007-08-01 22:01:54 +0000251 }
252
253 stack.pop_back();
Owen Anderson4c295472007-07-24 21:52:37 +0000254 }
Owen Anderson4c295472007-07-24 21:52:37 +0000255}
256
Owen Andersonafe840e2007-08-08 22:01:54 +0000257/// getNonLocalDependency - Fills the passed-in map with the non-local
258/// dependencies of the queries. The map will contain NonLocal for
259/// blocks between the query and its dependencies.
Owen Anderson3f75d122007-08-01 22:01:54 +0000260void MemoryDependenceAnalysis::getNonLocalDependency(Instruction* query,
Owen Andersonafe840e2007-08-08 22:01:54 +0000261 DenseMap<BasicBlock*, Value*>& resp) {
Owen Anderson2bd46a52007-08-16 21:27:05 +0000262 if (depGraphNonLocal.count(query)) {
Owen Anderson05749072007-09-21 03:53:52 +0000263 DenseMap<BasicBlock*, Value*>& cached = depGraphNonLocal[query];
Owen Andersond6c7fea2007-09-09 21:43:49 +0000264 NumCacheNonlocal++;
Owen Anderson05749072007-09-21 03:53:52 +0000265
266 SmallVector<BasicBlock*, 4> dirtied;
267 for (DenseMap<BasicBlock*, Value*>::iterator I = cached.begin(),
268 E = cached.end(); I != E; ++I)
269 if (I->second == Dirty)
270 dirtied.push_back(I->first);
271
272 for (SmallVector<BasicBlock*, 4>::iterator I = dirtied.begin(),
273 E = dirtied.end(); I != E; ++I) {
274 Instruction* localDep = getDependency(query, 0, *I);
275 if (localDep != NonLocal)
276 cached[*I] = localDep;
277 else {
278 cached.erase(*I);
279 nonLocalHelper(query, *I, cached);
280 }
281 }
282
283 resp = cached;
284
Owen Anderson2bd46a52007-08-16 21:27:05 +0000285 return;
Owen Andersond6c7fea2007-09-09 21:43:49 +0000286 } else
287 NumUncacheNonlocal++;
Owen Anderson2bd46a52007-08-16 21:27:05 +0000288
Owen Andersond6c7fea2007-09-09 21:43:49 +0000289 // If not, go ahead and search for non-local deps.
Owen Anderson3f75d122007-08-01 22:01:54 +0000290 nonLocalHelper(query, query->getParent(), resp);
Owen Anderson2bd46a52007-08-16 21:27:05 +0000291
292 // Update the non-local dependency cache
293 for (DenseMap<BasicBlock*, Value*>::iterator I = resp.begin(), E = resp.end();
294 I != E; ++I) {
295 depGraphNonLocal[query].insert(*I);
296 reverseDepNonLocal[I->second].insert(query);
297 }
Owen Anderson4c295472007-07-24 21:52:37 +0000298}
299
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000300/// getDependency - Return the instruction on which a memory operation
Dan Gohmanf1f99a22008-04-10 23:02:38 +0000301/// depends. The local parameter indicates if the query should only
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302/// evaluate dependencies within the same basic block.
Owen Anderson935e39b2007-08-09 04:42:44 +0000303Instruction* MemoryDependenceAnalysis::getDependency(Instruction* query,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304 Instruction* start,
Owen Anderson4c295472007-07-24 21:52:37 +0000305 BasicBlock* block) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306 // Start looking for dependencies with the queried inst
307 BasicBlock::iterator QI = query;
308
309 // Check for a cached result
Owen Andersonc772be72007-12-08 01:37:09 +0000310 std::pair<Instruction*, bool>& cachedResult = depGraphLocal[query];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311 // If we have a _confirmed_ cached entry, return it
Owen Andersonc772be72007-12-08 01:37:09 +0000312 if (!block && !start) {
313 if (cachedResult.second)
314 return cachedResult.first;
315 else if (cachedResult.first && cachedResult.first != NonLocal)
316 // If we have an unconfirmed cached entry, we can start our search from there
317 QI = cachedResult.first;
318 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319
320 if (start)
321 QI = start;
Owen Anderson5d72a422007-07-25 19:57:03 +0000322 else if (!start && block)
323 QI = block->end();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324
325 AliasAnalysis& AA = getAnalysis<AliasAnalysis>();
326 TargetData& TD = getAnalysis<TargetData>();
327
328 // Get the pointer value for which dependence will be determined
329 Value* dependee = 0;
330 uint64_t dependeeSize = 0;
331 bool queryIsVolatile = false;
332 if (StoreInst* S = dyn_cast<StoreInst>(query)) {
333 dependee = S->getPointerOperand();
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000334 dependeeSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000335 queryIsVolatile = S->isVolatile();
336 } else if (LoadInst* L = dyn_cast<LoadInst>(query)) {
337 dependee = L->getPointerOperand();
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000338 dependeeSize = TD.getTypeStoreSize(L->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 queryIsVolatile = L->isVolatile();
340 } else if (VAArgInst* V = dyn_cast<VAArgInst>(query)) {
341 dependee = V->getOperand(0);
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000342 dependeeSize = TD.getTypeStoreSize(V->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343 } else if (FreeInst* F = dyn_cast<FreeInst>(query)) {
344 dependee = F->getPointerOperand();
345
346 // FreeInsts erase the entire structure, not just a field
347 dependeeSize = ~0UL;
348 } else if (CallSite::get(query).getInstruction() != 0)
Owen Andersone84f4bc2007-08-07 00:33:45 +0000349 return getCallSiteDependency(CallSite::get(query), start, block);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000350 else if (isa<AllocationInst>(query))
351 return None;
352 else
353 return None;
354
Owen Anderson4c295472007-07-24 21:52:37 +0000355 BasicBlock::iterator blockBegin = block ? block->begin()
356 : query->getParent()->begin();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000357
Owen Anderson3de3c532007-08-08 22:26:03 +0000358 // Walk backwards through the basic block, looking for dependencies
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359 while (QI != blockBegin) {
360 --QI;
361
362 // If this inst is a memory op, get the pointer it accessed
363 Value* pointer = 0;
364 uint64_t pointerSize = 0;
365 if (StoreInst* S = dyn_cast<StoreInst>(QI)) {
366 // All volatile loads/stores depend on each other
367 if (queryIsVolatile && S->isVolatile()) {
Owen Andersone84f4bc2007-08-07 00:33:45 +0000368 if (!start && !block) {
Owen Andersonc772be72007-12-08 01:37:09 +0000369 cachedResult.first = S;
370 cachedResult.second = true;
Owen Andersone84f4bc2007-08-07 00:33:45 +0000371 reverseDep[S].insert(query);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 }
373
374 return S;
375 }
376
377 pointer = S->getPointerOperand();
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000378 pointerSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000379 } else if (LoadInst* L = dyn_cast<LoadInst>(QI)) {
380 // All volatile loads/stores depend on each other
381 if (queryIsVolatile && L->isVolatile()) {
Owen Andersone84f4bc2007-08-07 00:33:45 +0000382 if (!start && !block) {
Owen Andersonc772be72007-12-08 01:37:09 +0000383 cachedResult.first = L;
384 cachedResult.second = true;
Owen Andersone84f4bc2007-08-07 00:33:45 +0000385 reverseDep[L].insert(query);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386 }
387
388 return L;
389 }
390
391 pointer = L->getPointerOperand();
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000392 pointerSize = TD.getTypeStoreSize(L->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000393 } else if (AllocationInst* AI = dyn_cast<AllocationInst>(QI)) {
394 pointer = AI;
395 if (ConstantInt* C = dyn_cast<ConstantInt>(AI->getArraySize()))
Owen Andersonafe840e2007-08-08 22:01:54 +0000396 pointerSize = C->getZExtValue() * \
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000397 TD.getABITypeSize(AI->getAllocatedType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398 else
399 pointerSize = ~0UL;
400 } else if (VAArgInst* V = dyn_cast<VAArgInst>(QI)) {
401 pointer = V->getOperand(0);
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000402 pointerSize = TD.getTypeStoreSize(V->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403 } else if (FreeInst* F = dyn_cast<FreeInst>(QI)) {
404 pointer = F->getPointerOperand();
405
406 // FreeInsts erase the entire structure
407 pointerSize = ~0UL;
408 } else if (CallSite::get(QI).getInstruction() != 0) {
Owen Andersonafe840e2007-08-08 22:01:54 +0000409 // Call insts need special handling. Check if they can modify our pointer
Owen Anderson151f7692007-08-06 23:26:03 +0000410 AliasAnalysis::ModRefResult MR = AA.getModRefInfo(CallSite::get(QI),
411 dependee, dependeeSize);
412
413 if (MR != AliasAnalysis::NoModRef) {
414 // Loads don't depend on read-only calls
415 if (isa<LoadInst>(query) && MR == AliasAnalysis::Ref)
416 continue;
417
Owen Andersone84f4bc2007-08-07 00:33:45 +0000418 if (!start && !block) {
Owen Andersonc772be72007-12-08 01:37:09 +0000419 cachedResult.first = QI;
420 cachedResult.second = true;
Owen Andersone84f4bc2007-08-07 00:33:45 +0000421 reverseDep[QI].insert(query);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000422 }
423
424 return QI;
425 } else {
426 continue;
427 }
428 }
429
430 // If we found a pointer, check if it could be the same as our pointer
431 if (pointer) {
432 AliasAnalysis::AliasResult R = AA.alias(pointer, pointerSize,
433 dependee, dependeeSize);
434
435 if (R != AliasAnalysis::NoAlias) {
Owen Anderson151f7692007-08-06 23:26:03 +0000436 // May-alias loads don't depend on each other
437 if (isa<LoadInst>(query) && isa<LoadInst>(QI) &&
438 R == AliasAnalysis::MayAlias)
439 continue;
440
Owen Andersone84f4bc2007-08-07 00:33:45 +0000441 if (!start && !block) {
Owen Andersonc772be72007-12-08 01:37:09 +0000442 cachedResult.first = QI;
443 cachedResult.second = true;
Owen Andersone84f4bc2007-08-07 00:33:45 +0000444 reverseDep[QI].insert(query);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445 }
446
447 return QI;
448 }
449 }
450 }
451
452 // If we found nothing, return the non-local flag
Owen Andersone84f4bc2007-08-07 00:33:45 +0000453 if (!start && !block) {
Owen Andersonc772be72007-12-08 01:37:09 +0000454 cachedResult.first = NonLocal;
455 cachedResult.second = true;
Owen Andersone84f4bc2007-08-07 00:33:45 +0000456 reverseDep[NonLocal].insert(query);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000457 }
458
459 return NonLocal;
460}
461
Owen Anderson8d272d52008-02-12 21:15:18 +0000462/// dropInstruction - Remove an instruction from the analysis, making
463/// absolutely conservative assumptions when updating the cache. This is
464/// useful, for example when an instruction is changed rather than removed.
465void MemoryDependenceAnalysis::dropInstruction(Instruction* drop) {
466 depMapType::iterator depGraphEntry = depGraphLocal.find(drop);
467 if (depGraphEntry != depGraphLocal.end())
468 reverseDep[depGraphEntry->second.first].erase(drop);
469
470 // Drop dependency information for things that depended on this instr
471 SmallPtrSet<Instruction*, 4>& set = reverseDep[drop];
472 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
473 I != E; ++I)
474 depGraphLocal.erase(*I);
475
476 depGraphLocal.erase(drop);
477 reverseDep.erase(drop);
478
479 for (DenseMap<BasicBlock*, Value*>::iterator DI =
480 depGraphNonLocal[drop].begin(), DE = depGraphNonLocal[drop].end();
481 DI != DE; ++DI)
482 if (DI->second != None)
483 reverseDepNonLocal[DI->second].erase(drop);
484
485 if (reverseDepNonLocal.count(drop)) {
486 SmallPtrSet<Instruction*, 4>& set = reverseDepNonLocal[drop];
487 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
488 I != E; ++I)
489 for (DenseMap<BasicBlock*, Value*>::iterator DI =
490 depGraphNonLocal[*I].begin(), DE = depGraphNonLocal[*I].end();
491 DI != DE; ++DI)
492 if (DI->second == drop)
493 DI->second = Dirty;
494 }
495
496 reverseDepNonLocal.erase(drop);
497 nonLocalDepMapType::iterator I = depGraphNonLocal.find(drop);
498 if (I != depGraphNonLocal.end())
499 depGraphNonLocal.erase(I);
500}
501
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000502/// removeInstruction - Remove an instruction from the dependence analysis,
503/// updating the dependence of instructions that previously depended on it.
Owen Anderson3de3c532007-08-08 22:26:03 +0000504/// This method attempts to keep the cache coherent using the reverse map.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505void MemoryDependenceAnalysis::removeInstruction(Instruction* rem) {
506 // Figure out the new dep for things that currently depend on rem
Owen Anderson935e39b2007-08-09 04:42:44 +0000507 Instruction* newDep = NonLocal;
David Greene701171c2007-07-31 20:01:27 +0000508
Owen Andersonc772be72007-12-08 01:37:09 +0000509 for (DenseMap<BasicBlock*, Value*>::iterator DI =
510 depGraphNonLocal[rem].begin(), DE = depGraphNonLocal[rem].end();
511 DI != DE; ++DI)
512 if (DI->second != None)
513 reverseDepNonLocal[DI->second].erase(rem);
514
David Greene701171c2007-07-31 20:01:27 +0000515 depMapType::iterator depGraphEntry = depGraphLocal.find(rem);
David Greene701171c2007-07-31 20:01:27 +0000516
517 if (depGraphEntry != depGraphLocal.end()) {
Owen Anderson8d272d52008-02-12 21:15:18 +0000518 reverseDep[depGraphEntry->second.first].erase(rem);
Owen Anderson25296a22008-01-30 01:24:05 +0000519
David Greene701171c2007-07-31 20:01:27 +0000520 if (depGraphEntry->second.first != NonLocal &&
Owen Anderson6487cf52008-02-05 04:34:03 +0000521 depGraphEntry->second.first != None &&
David Greene701171c2007-07-31 20:01:27 +0000522 depGraphEntry->second.second) {
523 // If we have dep info for rem, set them to it
Owen Anderson935e39b2007-08-09 04:42:44 +0000524 BasicBlock::iterator RI = depGraphEntry->second.first;
David Greene701171c2007-07-31 20:01:27 +0000525 RI++;
526 newDep = RI;
Owen Anderson6487cf52008-02-05 04:34:03 +0000527 } else if ( (depGraphEntry->second.first == NonLocal ||
528 depGraphEntry->second.first == None ) &&
David Greene701171c2007-07-31 20:01:27 +0000529 depGraphEntry->second.second ) {
530 // If we have a confirmed non-local flag, use it
Owen Anderson6487cf52008-02-05 04:34:03 +0000531 newDep = depGraphEntry->second.first;
David Greene701171c2007-07-31 20:01:27 +0000532 } else {
533 // Otherwise, use the immediate successor of rem
Owen Andersonafe840e2007-08-08 22:01:54 +0000534 // NOTE: This is because, when getDependence is called, it will first
535 // check the immediate predecessor of what is in the cache.
David Greene701171c2007-07-31 20:01:27 +0000536 BasicBlock::iterator RI = rem;
537 RI++;
538 newDep = RI;
539 }
Owen Anderson6487cf52008-02-05 04:34:03 +0000540 } else {
541 // Otherwise, use the immediate successor of rem
542 // NOTE: This is because, when getDependence is called, it will first
543 // check the immediate predecessor of what is in the cache.
544 BasicBlock::iterator RI = rem;
545 RI++;
546 newDep = RI;
547 }
548
549 SmallPtrSet<Instruction*, 4>& set = reverseDep[rem];
550 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
551 I != E; ++I) {
552 // Insert the new dependencies
553 // Mark it as unconfirmed as long as it is not the non-local flag
554 depGraphLocal[*I] = std::make_pair(newDep, (newDep == NonLocal ||
555 newDep == None));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000556 }
Owen Anderson2bd46a52007-08-16 21:27:05 +0000557
Owen Andersonc772be72007-12-08 01:37:09 +0000558 depGraphLocal.erase(rem);
559 reverseDep.erase(rem);
560
Owen Anderson0ceeca52007-09-11 04:31:00 +0000561 if (reverseDepNonLocal.count(rem)) {
Owen Anderson2bd46a52007-08-16 21:27:05 +0000562 SmallPtrSet<Instruction*, 4>& set = reverseDepNonLocal[rem];
563 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
564 I != E; ++I)
Owen Anderson05749072007-09-21 03:53:52 +0000565 for (DenseMap<BasicBlock*, Value*>::iterator DI =
566 depGraphNonLocal[*I].begin(), DE = depGraphNonLocal[*I].end();
567 DI != DE; ++DI)
568 if (DI->second == rem)
569 DI->second = Dirty;
Owen Anderson2bd46a52007-08-16 21:27:05 +0000570
Owen Anderson2bd46a52007-08-16 21:27:05 +0000571 }
Owen Andersonc772be72007-12-08 01:37:09 +0000572
573 reverseDepNonLocal.erase(rem);
574 nonLocalDepMapType::iterator I = depGraphNonLocal.find(rem);
575 if (I != depGraphNonLocal.end())
576 depGraphNonLocal.erase(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000577
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000578 getAnalysis<AliasAnalysis>().deleteValue(rem);
579}