blob: 7a6633f8fed03a0770d1f43ef2a1a88bf668f8fb [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
Dan Gohman089efff2008-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 Lattner8edb2b72008-02-06 00:54:55 +000037
Owen Andersond6c7fea2007-09-09 21:43:49 +000038STATISTIC(NumCacheNonlocal, "Number of cached non-local responses");
39STATISTIC(NumUncacheNonlocal, "Number of uncached non-local responses");
40
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041char MemoryDependenceAnalysis::ID = 0;
42
Owen Anderson935e39b2007-08-09 04:42:44 +000043Instruction* const MemoryDependenceAnalysis::NonLocal = (Instruction*)-3;
44Instruction* const MemoryDependenceAnalysis::None = (Instruction*)-4;
Owen Anderson7e447562007-09-19 16:13:57 +000045Instruction* const MemoryDependenceAnalysis::Dirty = (Instruction*)-5;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046
47// Register this pass...
48static RegisterPass<MemoryDependenceAnalysis> X("memdep",
Devang Patelbdfd1862008-03-20 02:25:21 +000049 "Memory Dependence Analysis", false, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050
Owen Andersonc772be72007-12-08 01:37:09 +000051void MemoryDependenceAnalysis::ping(Instruction *D) {
52 for (depMapType::iterator I = depGraphLocal.begin(), E = depGraphLocal.end();
53 I != E; ++I) {
54 assert(I->first != D);
55 assert(I->second.first != D);
56 }
57
58 for (nonLocalDepMapType::iterator I = depGraphNonLocal.begin(), E = depGraphNonLocal.end();
59 I != E; ++I) {
60 assert(I->first != D);
61 }
62
63 for (reverseDepMapType::iterator I = reverseDep.begin(), E = reverseDep.end();
64 I != E; ++I)
65 for (SmallPtrSet<Instruction*, 4>::iterator II = I->second.begin(), EE = I->second.end();
66 II != EE; ++II)
67 assert(*II != D);
68
69 for (reverseDepMapType::iterator I = reverseDepNonLocal.begin(), E = reverseDepNonLocal.end();
70 I != E; ++I)
71 for (SmallPtrSet<Instruction*, 4>::iterator II = I->second.begin(), EE = I->second.end();
72 II != EE; ++II)
73 assert(*II != D);
74}
75
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076/// getAnalysisUsage - Does not modify anything. It uses Alias Analysis.
77///
78void MemoryDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
79 AU.setPreservesAll();
80 AU.addRequiredTransitive<AliasAnalysis>();
81 AU.addRequiredTransitive<TargetData>();
82}
83
Owen Anderson3de3c532007-08-08 22:26:03 +000084/// getCallSiteDependency - Private helper for finding the local dependencies
85/// of a call site.
Owen Anderson935e39b2007-08-09 04:42:44 +000086Instruction* MemoryDependenceAnalysis::getCallSiteDependency(CallSite C,
Owen Andersonafe840e2007-08-08 22:01:54 +000087 Instruction* start,
88 BasicBlock* block) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089
Owen Andersonc772be72007-12-08 01:37:09 +000090 std::pair<Instruction*, bool>& cachedResult =
91 depGraphLocal[C.getInstruction()];
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092 AliasAnalysis& AA = getAnalysis<AliasAnalysis>();
93 TargetData& TD = getAnalysis<TargetData>();
94 BasicBlock::iterator blockBegin = C.getInstruction()->getParent()->begin();
95 BasicBlock::iterator QI = C.getInstruction();
96
Owen Anderson3de3c532007-08-08 22:26:03 +000097 // If the starting point was specifiy, use it
Owen Andersone84f4bc2007-08-07 00:33:45 +000098 if (start) {
99 QI = start;
Dan Gohmand4752682008-03-31 22:08:00 +0000100 blockBegin = start->getParent()->begin();
Owen Anderson3de3c532007-08-08 22:26:03 +0000101 // If the starting point wasn't specified, but the block was, use it
Owen Andersone84f4bc2007-08-07 00:33:45 +0000102 } else if (!start && block) {
103 QI = block->end();
Dan Gohmand4752682008-03-31 22:08:00 +0000104 blockBegin = block->begin();
Owen Andersone84f4bc2007-08-07 00:33:45 +0000105 }
106
Owen Anderson3de3c532007-08-08 22:26:03 +0000107 // Walk backwards through the block, looking for dependencies
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108 while (QI != blockBegin) {
109 --QI;
110
111 // If this inst is a memory op, get the pointer it accessed
112 Value* pointer = 0;
113 uint64_t pointerSize = 0;
114 if (StoreInst* S = dyn_cast<StoreInst>(QI)) {
115 pointer = S->getPointerOperand();
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000116 pointerSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 } else if (AllocationInst* AI = dyn_cast<AllocationInst>(QI)) {
118 pointer = AI;
119 if (ConstantInt* C = dyn_cast<ConstantInt>(AI->getArraySize()))
Owen Andersonafe840e2007-08-08 22:01:54 +0000120 pointerSize = C->getZExtValue() * \
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000121 TD.getABITypeSize(AI->getAllocatedType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 else
123 pointerSize = ~0UL;
124 } else if (VAArgInst* V = dyn_cast<VAArgInst>(QI)) {
125 pointer = V->getOperand(0);
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000126 pointerSize = TD.getTypeStoreSize(V->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 } else if (FreeInst* F = dyn_cast<FreeInst>(QI)) {
128 pointer = F->getPointerOperand();
129
130 // FreeInsts erase the entire structure
131 pointerSize = ~0UL;
Owen Andersona10020b2008-05-13 21:25:37 +0000132 } else if (CallSite::get(QI).getInstruction() != 0) {
Owen Anderson8b6f04e2007-11-26 02:26:36 +0000133 AliasAnalysis::ModRefBehavior result =
Duncan Sands00b24b52007-12-01 07:51:45 +0000134 AA.getModRefBehavior(CallSite::get(QI));
Owen Andersona0b2b8e2008-04-17 05:36:50 +0000135 if (result != AliasAnalysis::DoesNotAccessMemory) {
Owen Andersone84f4bc2007-08-07 00:33:45 +0000136 if (!start && !block) {
Owen Andersonc772be72007-12-08 01:37:09 +0000137 cachedResult.first = QI;
138 cachedResult.second = true;
Owen Andersone84f4bc2007-08-07 00:33:45 +0000139 reverseDep[QI].insert(C.getInstruction());
140 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 return QI;
142 } else {
143 continue;
144 }
145 } else
146 continue;
147
148 if (AA.getModRefInfo(C, pointer, pointerSize) != AliasAnalysis::NoModRef) {
Owen Andersone84f4bc2007-08-07 00:33:45 +0000149 if (!start && !block) {
Owen Andersonc772be72007-12-08 01:37:09 +0000150 cachedResult.first = QI;
151 cachedResult.second = true;
Owen Andersone84f4bc2007-08-07 00:33:45 +0000152 reverseDep[QI].insert(C.getInstruction());
153 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 return QI;
155 }
156 }
157
158 // No dependence found
Owen Andersonc772be72007-12-08 01:37:09 +0000159 cachedResult.first = NonLocal;
160 cachedResult.second = true;
Owen Andersone84f4bc2007-08-07 00:33:45 +0000161 reverseDep[NonLocal].insert(C.getInstruction());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 return NonLocal;
163}
164
Owen Anderson3de3c532007-08-08 22:26:03 +0000165/// nonLocalHelper - Private helper used to calculate non-local dependencies
166/// by doing DFS on the predecessors of a block to find its dependencies
Owen Anderson3f75d122007-08-01 22:01:54 +0000167void MemoryDependenceAnalysis::nonLocalHelper(Instruction* query,
Owen Anderson5d72a422007-07-25 19:57:03 +0000168 BasicBlock* block,
Owen Andersonafe840e2007-08-08 22:01:54 +0000169 DenseMap<BasicBlock*, Value*>& resp) {
Owen Anderson3de3c532007-08-08 22:26:03 +0000170 // Set of blocks that we've already visited in our DFS
Owen Anderson3f75d122007-08-01 22:01:54 +0000171 SmallPtrSet<BasicBlock*, 4> visited;
Owen Anderson05749072007-09-21 03:53:52 +0000172 // If we're updating a dirtied cache entry, we don't need to reprocess
173 // already computed entries.
174 for (DenseMap<BasicBlock*, Value*>::iterator I = resp.begin(),
175 E = resp.end(); I != E; ++I)
176 if (I->second != Dirty)
177 visited.insert(I->first);
178
Owen Anderson3de3c532007-08-08 22:26:03 +0000179 // Current stack of the DFS
Owen Anderson3f75d122007-08-01 22:01:54 +0000180 SmallVector<BasicBlock*, 4> stack;
Owen Anderson2fecdf12008-04-10 22:13:32 +0000181 for (pred_iterator PI = pred_begin(block), PE = pred_end(block);
182 PI != PE; ++PI)
183 stack.push_back(*PI);
Owen Anderson4c295472007-07-24 21:52:37 +0000184
Owen Anderson3de3c532007-08-08 22:26:03 +0000185 // Do a basic DFS
Owen Anderson3f75d122007-08-01 22:01:54 +0000186 while (!stack.empty()) {
187 BasicBlock* BB = stack.back();
188
Owen Anderson3de3c532007-08-08 22:26:03 +0000189 // If we've already visited this block, no need to revist
Owen Andersonc6a31b92007-08-02 17:56:05 +0000190 if (visited.count(BB)) {
Owen Anderson3f75d122007-08-01 22:01:54 +0000191 stack.pop_back();
192 continue;
193 }
194
Owen Anderson3de3c532007-08-08 22:26:03 +0000195 // If we find a new block with a local dependency for query,
196 // then we insert the new dependency and backtrack.
Owen Anderson3f75d122007-08-01 22:01:54 +0000197 if (BB != block) {
Owen Andersonc6a31b92007-08-02 17:56:05 +0000198 visited.insert(BB);
199
Owen Anderson935e39b2007-08-09 04:42:44 +0000200 Instruction* localDep = getDependency(query, 0, BB);
Owen Anderson3f75d122007-08-01 22:01:54 +0000201 if (localDep != NonLocal) {
Owen Anderson935e39b2007-08-09 04:42:44 +0000202 resp.insert(std::make_pair(BB, localDep));
Owen Andersonc6a31b92007-08-02 17:56:05 +0000203 stack.pop_back();
204
Owen Anderson3f75d122007-08-01 22:01:54 +0000205 continue;
206 }
Owen Anderson3de3c532007-08-08 22:26:03 +0000207 // If we re-encounter the starting block, we still need to search it
208 // because there might be a dependency in the starting block AFTER
209 // the position of the query. This is necessary to get loops right.
Owen Anderson2fecdf12008-04-10 22:13:32 +0000210 } else if (BB == block) {
Owen Andersonc6a31b92007-08-02 17:56:05 +0000211 visited.insert(BB);
212
Owen Anderson935e39b2007-08-09 04:42:44 +0000213 Instruction* localDep = getDependency(query, 0, BB);
Owen Andersonc6a31b92007-08-02 17:56:05 +0000214 if (localDep != query)
Owen Anderson935e39b2007-08-09 04:42:44 +0000215 resp.insert(std::make_pair(BB, localDep));
Owen Andersonc6a31b92007-08-02 17:56:05 +0000216
217 stack.pop_back();
218
219 continue;
Owen Anderson3f75d122007-08-01 22:01:54 +0000220 }
221
Owen Anderson3de3c532007-08-08 22:26:03 +0000222 // If we didn't find anything, recurse on the precessors of this block
Tanya Lattner8edb2b72008-02-06 00:54:55 +0000223 // Only do this for blocks with a small number of predecessors.
Owen Anderson3f75d122007-08-01 22:01:54 +0000224 bool predOnStack = false;
225 bool inserted = false;
Tanya Lattner8edb2b72008-02-06 00:54:55 +0000226 if (std::distance(pred_begin(BB), pred_end(BB)) <= PredLimit) {
227 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
228 PI != PE; ++PI)
229 if (!visited.count(*PI)) {
230 stack.push_back(*PI);
231 inserted = true;
232 } else
233 predOnStack = true;
234 }
Owen Anderson3f75d122007-08-01 22:01:54 +0000235
Owen Anderson3de3c532007-08-08 22:26:03 +0000236 // If we inserted a new predecessor, then we'll come back to this block
Owen Anderson3f75d122007-08-01 22:01:54 +0000237 if (inserted)
238 continue;
Owen Anderson3de3c532007-08-08 22:26:03 +0000239 // If we didn't insert because we have no predecessors, then this
240 // query has no dependency at all.
Owen Anderson3f75d122007-08-01 22:01:54 +0000241 else if (!inserted && !predOnStack) {
Owen Anderson935e39b2007-08-09 04:42:44 +0000242 resp.insert(std::make_pair(BB, None));
Owen Anderson3de3c532007-08-08 22:26:03 +0000243 // If we didn't insert because our predecessors are already on the stack,
244 // then we might still have a dependency, but it will be discovered during
245 // backtracking.
Owen Anderson3f75d122007-08-01 22:01:54 +0000246 } else if (!inserted && predOnStack){
Owen Anderson935e39b2007-08-09 04:42:44 +0000247 resp.insert(std::make_pair(BB, NonLocal));
Owen Anderson3f75d122007-08-01 22:01:54 +0000248 }
249
250 stack.pop_back();
Owen Anderson4c295472007-07-24 21:52:37 +0000251 }
Owen Anderson4c295472007-07-24 21:52:37 +0000252}
253
Owen Andersonafe840e2007-08-08 22:01:54 +0000254/// getNonLocalDependency - Fills the passed-in map with the non-local
255/// dependencies of the queries. The map will contain NonLocal for
256/// blocks between the query and its dependencies.
Owen Anderson3f75d122007-08-01 22:01:54 +0000257void MemoryDependenceAnalysis::getNonLocalDependency(Instruction* query,
Owen Andersonafe840e2007-08-08 22:01:54 +0000258 DenseMap<BasicBlock*, Value*>& resp) {
Owen Anderson2bd46a52007-08-16 21:27:05 +0000259 if (depGraphNonLocal.count(query)) {
Owen Anderson05749072007-09-21 03:53:52 +0000260 DenseMap<BasicBlock*, Value*>& cached = depGraphNonLocal[query];
Owen Andersond6c7fea2007-09-09 21:43:49 +0000261 NumCacheNonlocal++;
Owen Anderson05749072007-09-21 03:53:52 +0000262
263 SmallVector<BasicBlock*, 4> dirtied;
264 for (DenseMap<BasicBlock*, Value*>::iterator I = cached.begin(),
265 E = cached.end(); I != E; ++I)
266 if (I->second == Dirty)
267 dirtied.push_back(I->first);
268
269 for (SmallVector<BasicBlock*, 4>::iterator I = dirtied.begin(),
270 E = dirtied.end(); I != E; ++I) {
271 Instruction* localDep = getDependency(query, 0, *I);
272 if (localDep != NonLocal)
273 cached[*I] = localDep;
274 else {
275 cached.erase(*I);
276 nonLocalHelper(query, *I, cached);
277 }
278 }
279
280 resp = cached;
281
Owen Anderson2bd46a52007-08-16 21:27:05 +0000282 return;
Owen Andersond6c7fea2007-09-09 21:43:49 +0000283 } else
284 NumUncacheNonlocal++;
Owen Anderson2bd46a52007-08-16 21:27:05 +0000285
Owen Andersond6c7fea2007-09-09 21:43:49 +0000286 // If not, go ahead and search for non-local deps.
Owen Anderson3f75d122007-08-01 22:01:54 +0000287 nonLocalHelper(query, query->getParent(), resp);
Owen Anderson2bd46a52007-08-16 21:27:05 +0000288
289 // Update the non-local dependency cache
290 for (DenseMap<BasicBlock*, Value*>::iterator I = resp.begin(), E = resp.end();
291 I != E; ++I) {
292 depGraphNonLocal[query].insert(*I);
293 reverseDepNonLocal[I->second].insert(query);
294 }
Owen Anderson4c295472007-07-24 21:52:37 +0000295}
296
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297/// getDependency - Return the instruction on which a memory operation
Dan Gohmanf1f99a22008-04-10 23:02:38 +0000298/// depends. The local parameter indicates if the query should only
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299/// evaluate dependencies within the same basic block.
Owen Anderson935e39b2007-08-09 04:42:44 +0000300Instruction* MemoryDependenceAnalysis::getDependency(Instruction* query,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301 Instruction* start,
Owen Anderson4c295472007-07-24 21:52:37 +0000302 BasicBlock* block) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000303 // Start looking for dependencies with the queried inst
304 BasicBlock::iterator QI = query;
305
306 // Check for a cached result
Owen Andersonc772be72007-12-08 01:37:09 +0000307 std::pair<Instruction*, bool>& cachedResult = depGraphLocal[query];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308 // If we have a _confirmed_ cached entry, return it
Owen Andersonc772be72007-12-08 01:37:09 +0000309 if (!block && !start) {
310 if (cachedResult.second)
311 return cachedResult.first;
312 else if (cachedResult.first && cachedResult.first != NonLocal)
313 // If we have an unconfirmed cached entry, we can start our search from there
314 QI = cachedResult.first;
315 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316
317 if (start)
318 QI = start;
Owen Anderson5d72a422007-07-25 19:57:03 +0000319 else if (!start && block)
320 QI = block->end();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000321
322 AliasAnalysis& AA = getAnalysis<AliasAnalysis>();
323 TargetData& TD = getAnalysis<TargetData>();
324
325 // Get the pointer value for which dependence will be determined
326 Value* dependee = 0;
327 uint64_t dependeeSize = 0;
328 bool queryIsVolatile = false;
329 if (StoreInst* S = dyn_cast<StoreInst>(query)) {
330 dependee = S->getPointerOperand();
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000331 dependeeSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332 queryIsVolatile = S->isVolatile();
333 } else if (LoadInst* L = dyn_cast<LoadInst>(query)) {
334 dependee = L->getPointerOperand();
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000335 dependeeSize = TD.getTypeStoreSize(L->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000336 queryIsVolatile = L->isVolatile();
337 } else if (VAArgInst* V = dyn_cast<VAArgInst>(query)) {
338 dependee = V->getOperand(0);
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000339 dependeeSize = TD.getTypeStoreSize(V->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340 } else if (FreeInst* F = dyn_cast<FreeInst>(query)) {
341 dependee = F->getPointerOperand();
342
343 // FreeInsts erase the entire structure, not just a field
344 dependeeSize = ~0UL;
345 } else if (CallSite::get(query).getInstruction() != 0)
Owen Andersone84f4bc2007-08-07 00:33:45 +0000346 return getCallSiteDependency(CallSite::get(query), start, block);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000347 else if (isa<AllocationInst>(query))
348 return None;
349 else
350 return None;
351
Owen Anderson4c295472007-07-24 21:52:37 +0000352 BasicBlock::iterator blockBegin = block ? block->begin()
353 : query->getParent()->begin();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000354
Owen Anderson3de3c532007-08-08 22:26:03 +0000355 // Walk backwards through the basic block, looking for dependencies
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356 while (QI != blockBegin) {
357 --QI;
358
359 // If this inst is a memory op, get the pointer it accessed
360 Value* pointer = 0;
361 uint64_t pointerSize = 0;
362 if (StoreInst* S = dyn_cast<StoreInst>(QI)) {
363 // All volatile loads/stores depend on each other
364 if (queryIsVolatile && S->isVolatile()) {
Owen Andersone84f4bc2007-08-07 00:33:45 +0000365 if (!start && !block) {
Owen Andersonc772be72007-12-08 01:37:09 +0000366 cachedResult.first = S;
367 cachedResult.second = true;
Owen Andersone84f4bc2007-08-07 00:33:45 +0000368 reverseDep[S].insert(query);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000369 }
370
371 return S;
372 }
373
374 pointer = S->getPointerOperand();
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000375 pointerSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 } else if (LoadInst* L = dyn_cast<LoadInst>(QI)) {
377 // All volatile loads/stores depend on each other
378 if (queryIsVolatile && L->isVolatile()) {
Owen Andersone84f4bc2007-08-07 00:33:45 +0000379 if (!start && !block) {
Owen Andersonc772be72007-12-08 01:37:09 +0000380 cachedResult.first = L;
381 cachedResult.second = true;
Owen Andersone84f4bc2007-08-07 00:33:45 +0000382 reverseDep[L].insert(query);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383 }
384
385 return L;
386 }
387
388 pointer = L->getPointerOperand();
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000389 pointerSize = TD.getTypeStoreSize(L->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000390 } else if (AllocationInst* AI = dyn_cast<AllocationInst>(QI)) {
391 pointer = AI;
392 if (ConstantInt* C = dyn_cast<ConstantInt>(AI->getArraySize()))
Owen Andersonafe840e2007-08-08 22:01:54 +0000393 pointerSize = C->getZExtValue() * \
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000394 TD.getABITypeSize(AI->getAllocatedType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000395 else
396 pointerSize = ~0UL;
397 } else if (VAArgInst* V = dyn_cast<VAArgInst>(QI)) {
398 pointer = V->getOperand(0);
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000399 pointerSize = TD.getTypeStoreSize(V->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000400 } else if (FreeInst* F = dyn_cast<FreeInst>(QI)) {
401 pointer = F->getPointerOperand();
402
403 // FreeInsts erase the entire structure
404 pointerSize = ~0UL;
405 } else if (CallSite::get(QI).getInstruction() != 0) {
Owen Andersonafe840e2007-08-08 22:01:54 +0000406 // Call insts need special handling. Check if they can modify our pointer
Owen Anderson151f7692007-08-06 23:26:03 +0000407 AliasAnalysis::ModRefResult MR = AA.getModRefInfo(CallSite::get(QI),
408 dependee, dependeeSize);
409
410 if (MR != AliasAnalysis::NoModRef) {
411 // Loads don't depend on read-only calls
412 if (isa<LoadInst>(query) && MR == AliasAnalysis::Ref)
413 continue;
414
Owen Andersone84f4bc2007-08-07 00:33:45 +0000415 if (!start && !block) {
Owen Andersonc772be72007-12-08 01:37:09 +0000416 cachedResult.first = QI;
417 cachedResult.second = true;
Owen Andersone84f4bc2007-08-07 00:33:45 +0000418 reverseDep[QI].insert(query);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000419 }
420
421 return QI;
422 } else {
423 continue;
424 }
425 }
426
427 // If we found a pointer, check if it could be the same as our pointer
428 if (pointer) {
429 AliasAnalysis::AliasResult R = AA.alias(pointer, pointerSize,
430 dependee, dependeeSize);
431
432 if (R != AliasAnalysis::NoAlias) {
Owen Anderson151f7692007-08-06 23:26:03 +0000433 // May-alias loads don't depend on each other
434 if (isa<LoadInst>(query) && isa<LoadInst>(QI) &&
435 R == AliasAnalysis::MayAlias)
436 continue;
437
Owen Andersone84f4bc2007-08-07 00:33:45 +0000438 if (!start && !block) {
Owen Andersonc772be72007-12-08 01:37:09 +0000439 cachedResult.first = QI;
440 cachedResult.second = true;
Owen Andersone84f4bc2007-08-07 00:33:45 +0000441 reverseDep[QI].insert(query);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442 }
443
444 return QI;
445 }
446 }
447 }
448
449 // If we found nothing, return the non-local flag
Owen Andersone84f4bc2007-08-07 00:33:45 +0000450 if (!start && !block) {
Owen Andersonc772be72007-12-08 01:37:09 +0000451 cachedResult.first = NonLocal;
452 cachedResult.second = true;
Owen Andersone84f4bc2007-08-07 00:33:45 +0000453 reverseDep[NonLocal].insert(query);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000454 }
455
456 return NonLocal;
457}
458
Owen Anderson8d272d52008-02-12 21:15:18 +0000459/// dropInstruction - Remove an instruction from the analysis, making
460/// absolutely conservative assumptions when updating the cache. This is
461/// useful, for example when an instruction is changed rather than removed.
462void MemoryDependenceAnalysis::dropInstruction(Instruction* drop) {
463 depMapType::iterator depGraphEntry = depGraphLocal.find(drop);
464 if (depGraphEntry != depGraphLocal.end())
465 reverseDep[depGraphEntry->second.first].erase(drop);
466
467 // Drop dependency information for things that depended on this instr
468 SmallPtrSet<Instruction*, 4>& set = reverseDep[drop];
469 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
470 I != E; ++I)
471 depGraphLocal.erase(*I);
472
473 depGraphLocal.erase(drop);
474 reverseDep.erase(drop);
475
476 for (DenseMap<BasicBlock*, Value*>::iterator DI =
477 depGraphNonLocal[drop].begin(), DE = depGraphNonLocal[drop].end();
478 DI != DE; ++DI)
479 if (DI->second != None)
480 reverseDepNonLocal[DI->second].erase(drop);
481
482 if (reverseDepNonLocal.count(drop)) {
483 SmallPtrSet<Instruction*, 4>& set = reverseDepNonLocal[drop];
484 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
485 I != E; ++I)
486 for (DenseMap<BasicBlock*, Value*>::iterator DI =
487 depGraphNonLocal[*I].begin(), DE = depGraphNonLocal[*I].end();
488 DI != DE; ++DI)
489 if (DI->second == drop)
490 DI->second = Dirty;
491 }
492
493 reverseDepNonLocal.erase(drop);
494 nonLocalDepMapType::iterator I = depGraphNonLocal.find(drop);
495 if (I != depGraphNonLocal.end())
496 depGraphNonLocal.erase(I);
497}
498
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000499/// removeInstruction - Remove an instruction from the dependence analysis,
500/// updating the dependence of instructions that previously depended on it.
Owen Anderson3de3c532007-08-08 22:26:03 +0000501/// This method attempts to keep the cache coherent using the reverse map.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000502void MemoryDependenceAnalysis::removeInstruction(Instruction* rem) {
503 // Figure out the new dep for things that currently depend on rem
Owen Anderson935e39b2007-08-09 04:42:44 +0000504 Instruction* newDep = NonLocal;
David Greene701171c2007-07-31 20:01:27 +0000505
Owen Andersonc772be72007-12-08 01:37:09 +0000506 for (DenseMap<BasicBlock*, Value*>::iterator DI =
507 depGraphNonLocal[rem].begin(), DE = depGraphNonLocal[rem].end();
508 DI != DE; ++DI)
509 if (DI->second != None)
510 reverseDepNonLocal[DI->second].erase(rem);
511
David Greene701171c2007-07-31 20:01:27 +0000512 depMapType::iterator depGraphEntry = depGraphLocal.find(rem);
David Greene701171c2007-07-31 20:01:27 +0000513
514 if (depGraphEntry != depGraphLocal.end()) {
Owen Anderson8d272d52008-02-12 21:15:18 +0000515 reverseDep[depGraphEntry->second.first].erase(rem);
Owen Anderson25296a22008-01-30 01:24:05 +0000516
David Greene701171c2007-07-31 20:01:27 +0000517 if (depGraphEntry->second.first != NonLocal &&
Owen Anderson6487cf52008-02-05 04:34:03 +0000518 depGraphEntry->second.first != None &&
David Greene701171c2007-07-31 20:01:27 +0000519 depGraphEntry->second.second) {
520 // If we have dep info for rem, set them to it
Owen Anderson935e39b2007-08-09 04:42:44 +0000521 BasicBlock::iterator RI = depGraphEntry->second.first;
David Greene701171c2007-07-31 20:01:27 +0000522 RI++;
523 newDep = RI;
Owen Anderson6487cf52008-02-05 04:34:03 +0000524 } else if ( (depGraphEntry->second.first == NonLocal ||
525 depGraphEntry->second.first == None ) &&
David Greene701171c2007-07-31 20:01:27 +0000526 depGraphEntry->second.second ) {
527 // If we have a confirmed non-local flag, use it
Owen Anderson6487cf52008-02-05 04:34:03 +0000528 newDep = depGraphEntry->second.first;
David Greene701171c2007-07-31 20:01:27 +0000529 } else {
530 // Otherwise, use the immediate successor of rem
Owen Andersonafe840e2007-08-08 22:01:54 +0000531 // NOTE: This is because, when getDependence is called, it will first
532 // check the immediate predecessor of what is in the cache.
David Greene701171c2007-07-31 20:01:27 +0000533 BasicBlock::iterator RI = rem;
534 RI++;
535 newDep = RI;
536 }
Owen Anderson6487cf52008-02-05 04:34:03 +0000537 } else {
538 // Otherwise, use the immediate successor of rem
539 // NOTE: This is because, when getDependence is called, it will first
540 // check the immediate predecessor of what is in the cache.
541 BasicBlock::iterator RI = rem;
542 RI++;
543 newDep = RI;
544 }
545
546 SmallPtrSet<Instruction*, 4>& set = reverseDep[rem];
547 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
548 I != E; ++I) {
549 // Insert the new dependencies
550 // Mark it as unconfirmed as long as it is not the non-local flag
551 depGraphLocal[*I] = std::make_pair(newDep, (newDep == NonLocal ||
552 newDep == None));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000553 }
Owen Anderson2bd46a52007-08-16 21:27:05 +0000554
Owen Andersonc772be72007-12-08 01:37:09 +0000555 depGraphLocal.erase(rem);
556 reverseDep.erase(rem);
557
Owen Anderson0ceeca52007-09-11 04:31:00 +0000558 if (reverseDepNonLocal.count(rem)) {
Owen Anderson2bd46a52007-08-16 21:27:05 +0000559 SmallPtrSet<Instruction*, 4>& set = reverseDepNonLocal[rem];
560 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
561 I != E; ++I)
Owen Anderson05749072007-09-21 03:53:52 +0000562 for (DenseMap<BasicBlock*, Value*>::iterator DI =
563 depGraphNonLocal[*I].begin(), DE = depGraphNonLocal[*I].end();
564 DI != DE; ++DI)
565 if (DI->second == rem)
566 DI->second = Dirty;
Owen Anderson2bd46a52007-08-16 21:27:05 +0000567
Owen Anderson2bd46a52007-08-16 21:27:05 +0000568 }
Owen Andersonc772be72007-12-08 01:37:09 +0000569
570 reverseDepNonLocal.erase(rem);
571 nonLocalDepMapType::iterator I = depGraphNonLocal.find(rem);
572 if (I != depGraphNonLocal.end())
573 depGraphNonLocal.erase(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000574
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000575 getAnalysis<AliasAnalysis>().deleteValue(rem);
576}