1 //===- MemoryDependenceAnalysis.cpp - Mem Deps Implementation --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements an analysis that determines, for a given memory
11 // operation, what preceding memory operations it depends on. It builds on
12 // alias analysis information, and tries to provide a lazy, caching interface to
13 // a common kind of alias information query.
15 //===----------------------------------------------------------------------===//
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"
22 #include "llvm/Support/CFG.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Target/TargetData.h"
25 #include "llvm/ADT/Statistic.h"
27 #define DEBUG_TYPE "memdep"
31 // 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).
34 PredLimit("nonlocaldep-threshold", cl::Hidden, cl::init(50),
35 cl::desc("Control the calculation of non-local"
36 "dependencies (default = 50)"));
38 STATISTIC(NumCacheNonlocal, "Number of cached non-local responses");
39 STATISTIC(NumUncacheNonlocal, "Number of uncached non-local responses");
41 char MemoryDependenceAnalysis::ID = 0;
43 Instruction* const MemoryDependenceAnalysis::NonLocal = (Instruction*)-3;
44 Instruction* const MemoryDependenceAnalysis::None = (Instruction*)-4;
45 Instruction* const MemoryDependenceAnalysis::Dirty = (Instruction*)-5;
47 // Register this pass...
48 static RegisterPass<MemoryDependenceAnalysis> X("memdep",
49 "Memory Dependence Analysis", false, true);
51 void MemoryDependenceAnalysis::ping(Instruction *D) {
52 for (depMapType::iterator I = depGraphLocal.begin(), E = depGraphLocal.end();
54 assert(I->first != D);
55 assert(I->second.first != D);
58 for (nonLocalDepMapType::iterator I = depGraphNonLocal.begin(), E = depGraphNonLocal.end();
60 assert(I->first != D);
61 for (DenseMap<BasicBlock*, Value*>::iterator II = I->second.begin(),
62 EE = I->second.end(); II != EE; ++II)
63 assert(II->second != D);
66 for (reverseDepMapType::iterator I = reverseDep.begin(), E = reverseDep.end();
68 for (SmallPtrSet<Instruction*, 4>::iterator II = I->second.begin(), EE = I->second.end();
72 for (reverseDepMapType::iterator I = reverseDepNonLocal.begin(), E = reverseDepNonLocal.end();
74 for (SmallPtrSet<Instruction*, 4>::iterator II = I->second.begin(), EE = I->second.end();
79 /// getAnalysisUsage - Does not modify anything. It uses Alias Analysis.
81 void MemoryDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
83 AU.addRequiredTransitive<AliasAnalysis>();
84 AU.addRequiredTransitive<TargetData>();
87 /// getCallSiteDependency - Private helper for finding the local dependencies
89 Instruction* MemoryDependenceAnalysis::getCallSiteDependency(CallSite C,
93 std::pair<Instruction*, bool>& cachedResult =
94 depGraphLocal[C.getInstruction()];
95 AliasAnalysis& AA = getAnalysis<AliasAnalysis>();
96 TargetData& TD = getAnalysis<TargetData>();
97 BasicBlock::iterator blockBegin = C.getInstruction()->getParent()->begin();
98 BasicBlock::iterator QI = C.getInstruction();
100 // If the starting point was specified, use it
103 blockBegin = start->getParent()->begin();
104 // If the starting point wasn't specified, but the block was, use it
105 } else if (!start && block) {
107 blockBegin = block->begin();
110 // Walk backwards through the block, looking for dependencies
111 while (QI != blockBegin) {
114 // If this inst is a memory op, get the pointer it accessed
116 uint64_t pointerSize = 0;
117 if (StoreInst* S = dyn_cast<StoreInst>(QI)) {
118 pointer = S->getPointerOperand();
119 pointerSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
120 } else if (AllocationInst* AI = dyn_cast<AllocationInst>(QI)) {
122 if (ConstantInt* C = dyn_cast<ConstantInt>(AI->getArraySize()))
123 pointerSize = C->getZExtValue() *
124 TD.getABITypeSize(AI->getAllocatedType());
127 } else if (VAArgInst* V = dyn_cast<VAArgInst>(QI)) {
128 pointer = V->getOperand(0);
129 pointerSize = TD.getTypeStoreSize(V->getType());
130 } else if (FreeInst* F = dyn_cast<FreeInst>(QI)) {
131 pointer = F->getPointerOperand();
133 // FreeInsts erase the entire structure
135 } else if (CallSite::get(QI).getInstruction() != 0) {
136 AliasAnalysis::ModRefBehavior result =
137 AA.getModRefBehavior(CallSite::get(QI));
138 if (result != AliasAnalysis::DoesNotAccessMemory) {
139 if (!start && !block) {
140 cachedResult.first = QI;
141 cachedResult.second = true;
142 reverseDep[QI].insert(C.getInstruction());
151 if (AA.getModRefInfo(C, pointer, pointerSize) != AliasAnalysis::NoModRef) {
152 if (!start && !block) {
153 cachedResult.first = QI;
154 cachedResult.second = true;
155 reverseDep[QI].insert(C.getInstruction());
161 // No dependence found
162 cachedResult.first = NonLocal;
163 cachedResult.second = true;
164 reverseDep[NonLocal].insert(C.getInstruction());
168 /// nonLocalHelper - Private helper used to calculate non-local dependencies
169 /// by doing DFS on the predecessors of a block to find its dependencies
170 void MemoryDependenceAnalysis::nonLocalHelper(Instruction* query,
172 DenseMap<BasicBlock*, Value*>& resp) {
173 // Set of blocks that we've already visited in our DFS
174 SmallPtrSet<BasicBlock*, 4> visited;
175 // 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);
182 // Current stack of the DFS
183 SmallVector<BasicBlock*, 4> stack;
184 for (pred_iterator PI = pred_begin(block), PE = pred_end(block);
186 stack.push_back(*PI);
189 while (!stack.empty()) {
190 BasicBlock* BB = stack.back();
192 // If we've already visited this block, no need to revist
193 if (visited.count(BB)) {
198 // If we find a new block with a local dependency for query,
199 // then we insert the new dependency and backtrack.
203 Instruction* localDep = getDependency(query, 0, BB);
204 if (localDep != NonLocal) {
205 resp.insert(std::make_pair(BB, localDep));
210 // 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.
213 } else if (BB == block) {
216 Instruction* localDep = getDependency(query, 0, BB);
217 if (localDep != query)
218 resp.insert(std::make_pair(BB, localDep));
225 // If we didn't find anything, recurse on the precessors of this block
226 // Only do this for blocks with a small number of predecessors.
227 bool predOnStack = false;
228 bool inserted = false;
229 if (std::distance(pred_begin(BB), pred_end(BB)) <= PredLimit) {
230 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
232 if (!visited.count(*PI)) {
233 stack.push_back(*PI);
239 // If we inserted a new predecessor, then we'll come back to this block
242 // If we didn't insert because we have no predecessors, then this
243 // query has no dependency at all.
244 else if (!inserted && !predOnStack) {
245 resp.insert(std::make_pair(BB, None));
246 // 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
249 } else if (!inserted && predOnStack){
250 resp.insert(std::make_pair(BB, NonLocal));
257 /// 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.
260 void MemoryDependenceAnalysis::getNonLocalDependency(Instruction* query,
261 DenseMap<BasicBlock*, Value*>& resp) {
262 if (depGraphNonLocal.count(query)) {
263 DenseMap<BasicBlock*, Value*>& cached = depGraphNonLocal[query];
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);
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;
279 nonLocalHelper(query, *I, cached);
285 // Update the reverse non-local dependency cache
286 for (DenseMap<BasicBlock*, Value*>::iterator I = resp.begin(), E = resp.end();
288 reverseDepNonLocal[I->second].insert(query);
292 NumUncacheNonlocal++;
294 // If not, go ahead and search for non-local deps.
295 nonLocalHelper(query, query->getParent(), resp);
297 // Update the non-local dependency cache
298 for (DenseMap<BasicBlock*, Value*>::iterator I = resp.begin(), E = resp.end();
300 depGraphNonLocal[query].insert(*I);
301 reverseDepNonLocal[I->second].insert(query);
305 /// getDependency - Return the instruction on which a memory operation
306 /// depends. The local parameter indicates if the query should only
307 /// evaluate dependencies within the same basic block.
308 Instruction* MemoryDependenceAnalysis::getDependency(Instruction* query,
311 // Start looking for dependencies with the queried inst
312 BasicBlock::iterator QI = query;
314 // Check for a cached result
315 std::pair<Instruction*, bool>& cachedResult = depGraphLocal[query];
316 // If we have a _confirmed_ cached entry, return it
317 if (!block && !start) {
318 if (cachedResult.second)
319 return cachedResult.first;
320 else if (cachedResult.first && cachedResult.first != NonLocal)
321 // If we have an unconfirmed cached entry, we can start our search from there
322 QI = cachedResult.first;
327 else if (!start && block)
330 AliasAnalysis& AA = getAnalysis<AliasAnalysis>();
331 TargetData& TD = getAnalysis<TargetData>();
333 // Get the pointer value for which dependence will be determined
335 uint64_t dependeeSize = 0;
336 bool queryIsVolatile = false;
337 if (StoreInst* S = dyn_cast<StoreInst>(query)) {
338 dependee = S->getPointerOperand();
339 dependeeSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
340 queryIsVolatile = S->isVolatile();
341 } else if (LoadInst* L = dyn_cast<LoadInst>(query)) {
342 dependee = L->getPointerOperand();
343 dependeeSize = TD.getTypeStoreSize(L->getType());
344 queryIsVolatile = L->isVolatile();
345 } else if (VAArgInst* V = dyn_cast<VAArgInst>(query)) {
346 dependee = V->getOperand(0);
347 dependeeSize = TD.getTypeStoreSize(V->getType());
348 } else if (FreeInst* F = dyn_cast<FreeInst>(query)) {
349 dependee = F->getPointerOperand();
351 // FreeInsts erase the entire structure, not just a field
353 } else if (CallSite::get(query).getInstruction() != 0)
354 return getCallSiteDependency(CallSite::get(query), start, block);
355 else if (isa<AllocationInst>(query))
360 BasicBlock::iterator blockBegin = block ? block->begin()
361 : query->getParent()->begin();
363 // Walk backwards through the basic block, looking for dependencies
364 while (QI != blockBegin) {
367 // If this inst is a memory op, get the pointer it accessed
369 uint64_t pointerSize = 0;
370 if (StoreInst* S = dyn_cast<StoreInst>(QI)) {
371 // All volatile loads/stores depend on each other
372 if (queryIsVolatile && S->isVolatile()) {
373 if (!start && !block) {
374 cachedResult.first = S;
375 cachedResult.second = true;
376 reverseDep[S].insert(query);
382 pointer = S->getPointerOperand();
383 pointerSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
384 } else if (LoadInst* L = dyn_cast<LoadInst>(QI)) {
385 // All volatile loads/stores depend on each other
386 if (queryIsVolatile && L->isVolatile()) {
387 if (!start && !block) {
388 cachedResult.first = L;
389 cachedResult.second = true;
390 reverseDep[L].insert(query);
396 pointer = L->getPointerOperand();
397 pointerSize = TD.getTypeStoreSize(L->getType());
398 } else if (AllocationInst* AI = dyn_cast<AllocationInst>(QI)) {
400 if (ConstantInt* C = dyn_cast<ConstantInt>(AI->getArraySize()))
401 pointerSize = C->getZExtValue() *
402 TD.getABITypeSize(AI->getAllocatedType());
405 } else if (VAArgInst* V = dyn_cast<VAArgInst>(QI)) {
406 pointer = V->getOperand(0);
407 pointerSize = TD.getTypeStoreSize(V->getType());
408 } else if (FreeInst* F = dyn_cast<FreeInst>(QI)) {
409 pointer = F->getPointerOperand();
411 // FreeInsts erase the entire structure
413 } else if (CallSite::get(QI).getInstruction() != 0) {
414 // Call insts need special handling. Check if they can modify our pointer
415 AliasAnalysis::ModRefResult MR = AA.getModRefInfo(CallSite::get(QI),
416 dependee, dependeeSize);
418 if (MR != AliasAnalysis::NoModRef) {
419 // Loads don't depend on read-only calls
420 if (isa<LoadInst>(query) && MR == AliasAnalysis::Ref)
423 if (!start && !block) {
424 cachedResult.first = QI;
425 cachedResult.second = true;
426 reverseDep[QI].insert(query);
435 // If we found a pointer, check if it could be the same as our pointer
437 AliasAnalysis::AliasResult R = AA.alias(pointer, pointerSize,
438 dependee, dependeeSize);
440 if (R != AliasAnalysis::NoAlias) {
441 // May-alias loads don't depend on each other
442 if (isa<LoadInst>(query) && isa<LoadInst>(QI) &&
443 R == AliasAnalysis::MayAlias)
446 if (!start && !block) {
447 cachedResult.first = QI;
448 cachedResult.second = true;
449 reverseDep[QI].insert(query);
457 // If we found nothing, return the non-local flag
458 if (!start && !block) {
459 cachedResult.first = NonLocal;
460 cachedResult.second = true;
461 reverseDep[NonLocal].insert(query);
467 /// dropInstruction - Remove an instruction from the analysis, making
468 /// absolutely conservative assumptions when updating the cache. This is
469 /// useful, for example when an instruction is changed rather than removed.
470 void MemoryDependenceAnalysis::dropInstruction(Instruction* drop) {
471 depMapType::iterator depGraphEntry = depGraphLocal.find(drop);
472 if (depGraphEntry != depGraphLocal.end())
473 reverseDep[depGraphEntry->second.first].erase(drop);
475 // Drop dependency information for things that depended on this instr
476 SmallPtrSet<Instruction*, 4>& set = reverseDep[drop];
477 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
479 depGraphLocal.erase(*I);
481 depGraphLocal.erase(drop);
482 reverseDep.erase(drop);
484 for (DenseMap<BasicBlock*, Value*>::iterator DI =
485 depGraphNonLocal[drop].begin(), DE = depGraphNonLocal[drop].end();
487 if (DI->second != None)
488 reverseDepNonLocal[DI->second].erase(drop);
490 if (reverseDepNonLocal.count(drop)) {
491 SmallPtrSet<Instruction*, 4>& set = reverseDepNonLocal[drop];
492 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
494 for (DenseMap<BasicBlock*, Value*>::iterator DI =
495 depGraphNonLocal[*I].begin(), DE = depGraphNonLocal[*I].end();
497 if (DI->second == drop)
501 reverseDepNonLocal.erase(drop);
502 nonLocalDepMapType::iterator I = depGraphNonLocal.find(drop);
503 if (I != depGraphNonLocal.end())
504 depGraphNonLocal.erase(I);
507 /// removeInstruction - Remove an instruction from the dependence analysis,
508 /// updating the dependence of instructions that previously depended on it.
509 /// This method attempts to keep the cache coherent using the reverse map.
510 void MemoryDependenceAnalysis::removeInstruction(Instruction* rem) {
511 // Figure out the new dep for things that currently depend on rem
512 Instruction* newDep = NonLocal;
514 for (DenseMap<BasicBlock*, Value*>::iterator DI =
515 depGraphNonLocal[rem].begin(), DE = depGraphNonLocal[rem].end();
517 if (DI->second != None)
518 reverseDepNonLocal[DI->second].erase(rem);
520 depMapType::iterator depGraphEntry = depGraphLocal.find(rem);
522 if (depGraphEntry != depGraphLocal.end()) {
523 reverseDep[depGraphEntry->second.first].erase(rem);
525 if (depGraphEntry->second.first != NonLocal &&
526 depGraphEntry->second.first != None &&
527 depGraphEntry->second.second) {
528 // If we have dep info for rem, set them to it
529 BasicBlock::iterator RI = depGraphEntry->second.first;
532 // If RI is rem, then we use rem's immediate successor.
533 if (RI == (BasicBlock::iterator)rem) RI++;
536 } else if ((depGraphEntry->second.first == NonLocal ||
537 depGraphEntry->second.first == None) &&
538 depGraphEntry->second.second) {
539 // If we have a confirmed non-local flag, use it
540 newDep = depGraphEntry->second.first;
542 // Otherwise, use the immediate successor of rem
543 // NOTE: This is because, when getDependence is called, it will first
544 // check the immediate predecessor of what is in the cache.
545 BasicBlock::iterator RI = rem;
550 // Otherwise, use the immediate successor of rem
551 // NOTE: This is because, when getDependence is called, it will first
552 // check the immediate predecessor of what is in the cache.
553 BasicBlock::iterator RI = rem;
558 SmallPtrSet<Instruction*, 4>& set = reverseDep[rem];
559 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
561 // Insert the new dependencies
562 // Mark it as unconfirmed as long as it is not the non-local flag
563 depGraphLocal[*I] = std::make_pair(newDep, (newDep == NonLocal ||
567 depGraphLocal.erase(rem);
568 reverseDep.erase(rem);
570 if (reverseDepNonLocal.count(rem)) {
571 SmallPtrSet<Instruction*, 4>& set = reverseDepNonLocal[rem];
572 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
574 for (DenseMap<BasicBlock*, Value*>::iterator DI =
575 depGraphNonLocal[*I].begin(), DE = depGraphNonLocal[*I].end();
577 if (DI->second == rem)
582 reverseDepNonLocal.erase(rem);
583 nonLocalDepMapType::iterator I = depGraphNonLocal.find(rem);
584 if (I != depGraphNonLocal.end())
585 depGraphNonLocal.erase(I);
587 getAnalysis<AliasAnalysis>().deleteValue(rem);