MemoryDependenceAnalysis.cpp revision 745291a6ce841f30a8a9e536071bd1b4cf540c55
1//===- MemoryDependenceAnalysis.cpp - Mem Deps Implementation  --*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
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
12// alias analysis information, and tries to provide a lazy, caching interface to
13// a common kind of alias information query.
14//
15//===----------------------------------------------------------------------===//
16
17#define DEBUG_TYPE "memdep"
18#include "llvm/Analysis/MemoryDependenceAnalysis.h"
19#include "llvm/Constants.h"
20#include "llvm/Instructions.h"
21#include "llvm/Function.h"
22#include "llvm/Analysis/AliasAnalysis.h"
23#include "llvm/ADT/Statistic.h"
24#include "llvm/ADT/STLExtras.h"
25#include "llvm/Support/CFG.h"
26#include "llvm/Support/CommandLine.h"
27#include "llvm/Support/Debug.h"
28#include "llvm/Target/TargetData.h"
29using namespace llvm;
30
31STATISTIC(NumCacheNonLocal, "Number of fully cached non-local responses");
32STATISTIC(NumCacheDirtyNonLocal, "Number of dirty cached non-local responses");
33STATISTIC(NumUncacheNonLocal, "Number of uncached non-local responses");
34char MemoryDependenceAnalysis::ID = 0;
35
36// Register this pass...
37static RegisterPass<MemoryDependenceAnalysis> X("memdep",
38                                     "Memory Dependence Analysis", false, true);
39
40/// getAnalysisUsage - Does not modify anything.  It uses Alias Analysis.
41///
42void MemoryDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
43  AU.setPreservesAll();
44  AU.addRequiredTransitive<AliasAnalysis>();
45  AU.addRequiredTransitive<TargetData>();
46}
47
48bool MemoryDependenceAnalysis::runOnFunction(Function &) {
49  AA = &getAnalysis<AliasAnalysis>();
50  TD = &getAnalysis<TargetData>();
51  return false;
52}
53
54
55/// getCallSiteDependency - Private helper for finding the local dependencies
56/// of a call site.
57MemDepResult MemoryDependenceAnalysis::
58getCallSiteDependency(CallSite CS, BasicBlock::iterator ScanIt, BasicBlock *BB) {
59  // Walk backwards through the block, looking for dependencies
60  while (ScanIt != BB->begin()) {
61    Instruction *Inst = --ScanIt;
62
63    // If this inst is a memory op, get the pointer it accessed
64    Value *Pointer = 0;
65    uint64_t PointerSize = 0;
66    if (StoreInst *S = dyn_cast<StoreInst>(Inst)) {
67      Pointer = S->getPointerOperand();
68      PointerSize = TD->getTypeStoreSize(S->getOperand(0)->getType());
69    } else if (VAArgInst *V = dyn_cast<VAArgInst>(Inst)) {
70      Pointer = V->getOperand(0);
71      PointerSize = TD->getTypeStoreSize(V->getType());
72    } else if (FreeInst *F = dyn_cast<FreeInst>(Inst)) {
73      Pointer = F->getPointerOperand();
74
75      // FreeInsts erase the entire structure
76      PointerSize = ~0UL;
77    } else if (isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) {
78      CallSite InstCS = CallSite::get(Inst);
79      // If these two calls do not interfere, look past it.
80      if (AA->getModRefInfo(CS, InstCS) == AliasAnalysis::NoModRef)
81        continue;
82
83      // FIXME: If this is a ref/ref result, we should ignore it!
84      //  X = strlen(P);
85      //  Y = strlen(Q);
86      //  Z = strlen(P);  // Z = X
87
88      // If they interfere, we generally return clobber.  However, if they are
89      // calls to the same read-only functions we return Def.
90      if (!AA->onlyReadsMemory(CS) || CS.getCalledFunction() == 0 ||
91          CS.getCalledFunction() != InstCS.getCalledFunction())
92        return MemDepResult::getClobber(Inst);
93      return MemDepResult::getDef(Inst);
94    } else {
95      // Non-memory instruction.
96      continue;
97    }
98
99    if (AA->getModRefInfo(CS, Pointer, PointerSize) != AliasAnalysis::NoModRef)
100      return MemDepResult::getClobber(Inst);
101  }
102
103  // No dependence found.
104  return MemDepResult::getNonLocal();
105}
106
107/// getDependencyFrom - Return the instruction on which a memory operation
108/// depends.
109MemDepResult MemoryDependenceAnalysis::
110getDependencyFrom(Instruction *QueryInst, BasicBlock::iterator ScanIt,
111                  BasicBlock *BB) {
112  // The first instruction in a block is always non-local.
113  if (ScanIt == BB->begin())
114    return MemDepResult::getNonLocal();
115
116  // Get the pointer value for which dependence will be determined
117  Value *MemPtr = 0;
118  uint64_t MemSize = 0;
119
120  if (StoreInst* S = dyn_cast<StoreInst>(QueryInst)) {
121    // If this is a volatile store, don't mess around with it.  Just return the
122    // previous instruction as a clobber.
123    if (S->isVolatile())
124      return MemDepResult::getClobber(--ScanIt);
125
126    MemPtr = S->getPointerOperand();
127    MemSize = TD->getTypeStoreSize(S->getOperand(0)->getType());
128  } else if (LoadInst* LI = dyn_cast<LoadInst>(QueryInst)) {
129    // If this is a volatile load, don't mess around with it.  Just return the
130    // previous instruction as a clobber.
131    if (S->isVolatile())
132      return MemDepResult::getClobber(--ScanIt);
133
134    MemPtr = LI->getPointerOperand();
135    MemSize = TD->getTypeStoreSize(LI->getType());
136  } else if (FreeInst* F = dyn_cast<FreeInst>(QueryInst)) {
137    MemPtr = F->getPointerOperand();
138    // FreeInsts erase the entire structure, not just a field.
139    MemSize = ~0UL;
140  } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst)) {
141    return getCallSiteDependency(CallSite::get(QueryInst), ScanIt, BB);
142  } else {
143    // Otherwise, this is a vaarg or non-memory instruction, just return a
144    // clobber dependency on the previous inst.
145    return MemDepResult::getClobber(--ScanIt);
146  }
147
148  // Walk backwards through the basic block, looking for dependencies
149  while (ScanIt != BB->begin()) {
150    Instruction *Inst = --ScanIt;
151
152    // Values depend on loads if the pointers are must aliased.  This means that
153    // a load depends on another must aliased load from the same value.
154    if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
155      Value *Pointer = LI->getPointerOperand();
156      uint64_t PointerSize = TD->getTypeStoreSize(LI->getType());
157
158      // If we found a pointer, check if it could be the same as our pointer.
159      AliasAnalysis::AliasResult R =
160        AA->alias(Pointer, PointerSize, MemPtr, MemSize);
161      if (R == AliasAnalysis::NoAlias)
162        continue;
163
164      // May-alias loads don't depend on each other without a dependence.
165      if (isa<LoadInst>(QueryInst) && R == AliasAnalysis::MayAlias)
166        continue;
167      return MemDepResult::getDef(Inst);
168    }
169
170    if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
171      Value *Pointer = SI->getPointerOperand();
172      uint64_t PointerSize = TD->getTypeStoreSize(SI->getOperand(0)->getType());
173
174      // If we found a pointer, check if it could be the same as our pointer.
175      AliasAnalysis::AliasResult R =
176        AA->alias(Pointer, PointerSize, MemPtr, MemSize);
177
178      if (R == AliasAnalysis::NoAlias)
179        continue;
180      if (R == AliasAnalysis::MayAlias)
181        return MemDepResult::getClobber(Inst);
182      return MemDepResult::getDef(Inst);
183    }
184
185    // If this is an allocation, and if we know that the accessed pointer is to
186    // the allocation, return Def.  This means that there is no dependence and
187    // the access can be optimized based on that.  For example, a load could
188    // turn into undef.
189    if (AllocationInst *AI = dyn_cast<AllocationInst>(Inst)) {
190      Value *AccessPtr = MemPtr->getUnderlyingObject();
191
192      if (AccessPtr == AI ||
193          AA->alias(AI, 1, AccessPtr, 1) == AliasAnalysis::MustAlias)
194        return MemDepResult::getDef(AI);
195      continue;
196    }
197
198    // See if this instruction (e.g. a call or vaarg) mod/ref's the pointer.
199    if (AA->getModRefInfo(Inst, MemPtr, MemSize) == AliasAnalysis::NoModRef)
200      continue;
201
202    // Otherwise, there is a dependence.
203    return MemDepResult::getClobber(Inst);
204  }
205
206  // If we found nothing, return the non-local flag.
207  return MemDepResult::getNonLocal();
208}
209
210/// getDependency - Return the instruction on which a memory operation
211/// depends.
212MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) {
213  Instruction *ScanPos = QueryInst;
214
215  // Check for a cached result
216  MemDepResult &LocalCache = LocalDeps[QueryInst];
217
218  // If the cached entry is non-dirty, just return it.  Note that this depends
219  // on MemDepResult's default constructing to 'dirty'.
220  if (!LocalCache.isDirty())
221    return LocalCache;
222
223  // Otherwise, if we have a dirty entry, we know we can start the scan at that
224  // instruction, which may save us some work.
225  if (Instruction *Inst = LocalCache.getInst()) {
226    ScanPos = Inst;
227
228    SmallPtrSet<Instruction*, 4> &InstMap = ReverseLocalDeps[Inst];
229    InstMap.erase(QueryInst);
230    if (InstMap.empty())
231      ReverseLocalDeps.erase(Inst);
232  }
233
234  // Do the scan.
235  LocalCache = getDependencyFrom(QueryInst, ScanPos, QueryInst->getParent());
236
237  // Remember the result!
238  if (Instruction *I = LocalCache.getInst())
239    ReverseLocalDeps[I].insert(QueryInst);
240
241  return LocalCache;
242}
243
244/// getNonLocalDependency - Perform a full dependency query for the
245/// specified instruction, returning the set of blocks that the value is
246/// potentially live across.  The returned set of results will include a
247/// "NonLocal" result for all blocks where the value is live across.
248///
249/// This method assumes the instruction returns a "nonlocal" dependency
250/// within its own block.
251///
252const MemoryDependenceAnalysis::NonLocalDepInfo &
253MemoryDependenceAnalysis::getNonLocalDependency(Instruction *QueryInst) {
254  assert(getDependency(QueryInst).isNonLocal() &&
255     "getNonLocalDependency should only be used on insts with non-local deps!");
256  PerInstNLInfo &CacheP = NonLocalDeps[QueryInst];
257
258  NonLocalDepInfo &Cache = CacheP.first;
259
260  /// DirtyBlocks - This is the set of blocks that need to be recomputed.  In
261  /// the cached case, this can happen due to instructions being deleted etc. In
262  /// the uncached case, this starts out as the set of predecessors we care
263  /// about.
264  SmallVector<BasicBlock*, 32> DirtyBlocks;
265
266  if (!Cache.empty()) {
267    // Okay, we have a cache entry.  If we know it is not dirty, just return it
268    // with no computation.
269    if (!CacheP.second) {
270      NumCacheNonLocal++;
271      return Cache;
272    }
273
274    // If we already have a partially computed set of results, scan them to
275    // determine what is dirty, seeding our initial DirtyBlocks worklist.
276    for (NonLocalDepInfo::iterator I = Cache.begin(), E = Cache.end();
277       I != E; ++I)
278      if (I->second.isDirty())
279        DirtyBlocks.push_back(I->first);
280
281    // Sort the cache so that we can do fast binary search lookups below.
282    std::sort(Cache.begin(), Cache.end());
283
284    ++NumCacheDirtyNonLocal;
285    //cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: "
286    //     << Cache.size() << " cached: " << *QueryInst;
287  } else {
288    // Seed DirtyBlocks with each of the preds of QueryInst's block.
289    BasicBlock *QueryBB = QueryInst->getParent();
290    DirtyBlocks.append(pred_begin(QueryBB), pred_end(QueryBB));
291    NumUncacheNonLocal++;
292  }
293
294  // Visited checked first, vector in sorted order.
295  SmallPtrSet<BasicBlock*, 64> Visited;
296
297  unsigned NumSortedEntries = Cache.size();
298
299  // Iterate while we still have blocks to update.
300  while (!DirtyBlocks.empty()) {
301    BasicBlock *DirtyBB = DirtyBlocks.back();
302    DirtyBlocks.pop_back();
303
304    // Already processed this block?
305    if (!Visited.insert(DirtyBB))
306      continue;
307
308    // Do a binary search to see if we already have an entry for this block in
309    // the cache set.  If so, find it.
310    NonLocalDepInfo::iterator Entry =
311      std::upper_bound(Cache.begin(), Cache.begin()+NumSortedEntries,
312                       std::make_pair(DirtyBB, MemDepResult()));
313    if (Entry != Cache.begin() && (&*Entry)[-1].first == DirtyBB)
314      --Entry;
315
316    MemDepResult *ExistingResult = 0;
317    if (Entry != Cache.begin()+NumSortedEntries &&
318        Entry->first == DirtyBB) {
319      // If we already have an entry, and if it isn't already dirty, the block
320      // is done.
321      if (!Entry->second.isDirty())
322        continue;
323
324      // Otherwise, remember this slot so we can update the value.
325      ExistingResult = &Entry->second;
326    }
327
328    // If the dirty entry has a pointer, start scanning from it so we don't have
329    // to rescan the entire block.
330    BasicBlock::iterator ScanPos = DirtyBB->end();
331    if (ExistingResult) {
332      if (Instruction *Inst = ExistingResult->getInst()) {
333        ScanPos = Inst;
334
335        // We're removing QueryInst's use of Inst.
336        SmallPtrSet<Instruction*, 4> &InstMap = ReverseNonLocalDeps[Inst];
337        InstMap.erase(QueryInst);
338        if (InstMap.empty()) ReverseNonLocalDeps.erase(Inst);
339      }
340    }
341
342    // Find out if this block has a local dependency for QueryInst.
343    MemDepResult Dep = getDependencyFrom(QueryInst, ScanPos, DirtyBB);
344
345    // If we had a dirty entry for the block, update it.  Otherwise, just add
346    // a new entry.
347    if (ExistingResult)
348      *ExistingResult = Dep;
349    else
350      Cache.push_back(std::make_pair(DirtyBB, Dep));
351
352    // If the block has a dependency (i.e. it isn't completely transparent to
353    // the value), remember the association!
354    if (!Dep.isNonLocal()) {
355      // Keep the ReverseNonLocalDeps map up to date so we can efficiently
356      // update this when we remove instructions.
357      if (Instruction *Inst = Dep.getInst())
358        ReverseNonLocalDeps[Inst].insert(QueryInst);
359    } else {
360
361      // If the block *is* completely transparent to the load, we need to check
362      // the predecessors of this block.  Add them to our worklist.
363      DirtyBlocks.append(pred_begin(DirtyBB), pred_end(DirtyBB));
364    }
365  }
366
367  return Cache;
368}
369
370/// removeInstruction - Remove an instruction from the dependence analysis,
371/// updating the dependence of instructions that previously depended on it.
372/// This method attempts to keep the cache coherent using the reverse map.
373void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) {
374  // Walk through the Non-local dependencies, removing this one as the value
375  // for any cached queries.
376  NonLocalDepMapType::iterator NLDI = NonLocalDeps.find(RemInst);
377  if (NLDI != NonLocalDeps.end()) {
378    NonLocalDepInfo &BlockMap = NLDI->second.first;
379    for (NonLocalDepInfo::iterator DI = BlockMap.begin(), DE = BlockMap.end();
380         DI != DE; ++DI)
381      if (Instruction *Inst = DI->second.getInst())
382        ReverseNonLocalDeps[Inst].erase(RemInst);
383    NonLocalDeps.erase(NLDI);
384  }
385
386  // If we have a cached local dependence query for this instruction, remove it.
387  //
388  LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst);
389  if (LocalDepEntry != LocalDeps.end()) {
390    // Remove us from DepInst's reverse set now that the local dep info is gone.
391    if (Instruction *Inst = LocalDepEntry->second.getInst()) {
392      SmallPtrSet<Instruction*, 4> &RLD = ReverseLocalDeps[Inst];
393      RLD.erase(RemInst);
394      if (RLD.empty())
395        ReverseLocalDeps.erase(Inst);
396    }
397
398    // Remove this local dependency info.
399    LocalDeps.erase(LocalDepEntry);
400  }
401
402  // Loop over all of the things that depend on the instruction we're removing.
403  //
404  SmallVector<std::pair<Instruction*, Instruction*>, 8> ReverseDepsToAdd;
405
406  ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst);
407  if (ReverseDepIt != ReverseLocalDeps.end()) {
408    SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second;
409    // RemInst can't be the terminator if it has stuff depending on it.
410    assert(!ReverseDeps.empty() && !isa<TerminatorInst>(RemInst) &&
411           "Nothing can locally depend on a terminator");
412
413    // Anything that was locally dependent on RemInst is now going to be
414    // dependent on the instruction after RemInst.  It will have the dirty flag
415    // set so it will rescan.  This saves having to scan the entire block to get
416    // to this point.
417    Instruction *NewDepInst = next(BasicBlock::iterator(RemInst));
418
419    for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(),
420         E = ReverseDeps.end(); I != E; ++I) {
421      Instruction *InstDependingOnRemInst = *I;
422      assert(InstDependingOnRemInst != RemInst &&
423             "Already removed our local dep info");
424
425      LocalDeps[InstDependingOnRemInst] = MemDepResult::getDirty(NewDepInst);
426
427      // Make sure to remember that new things depend on NewDepInst.
428      ReverseDepsToAdd.push_back(std::make_pair(NewDepInst,
429                                                InstDependingOnRemInst));
430    }
431
432    ReverseLocalDeps.erase(ReverseDepIt);
433
434    // Add new reverse deps after scanning the set, to avoid invalidating the
435    // 'ReverseDeps' reference.
436    while (!ReverseDepsToAdd.empty()) {
437      ReverseLocalDeps[ReverseDepsToAdd.back().first]
438        .insert(ReverseDepsToAdd.back().second);
439      ReverseDepsToAdd.pop_back();
440    }
441  }
442
443  ReverseDepIt = ReverseNonLocalDeps.find(RemInst);
444  if (ReverseDepIt != ReverseNonLocalDeps.end()) {
445    SmallPtrSet<Instruction*, 4>& set = ReverseDepIt->second;
446    for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
447         I != E; ++I) {
448      assert(*I != RemInst && "Already removed NonLocalDep info for RemInst");
449
450      PerInstNLInfo &INLD = NonLocalDeps[*I];
451      // The information is now dirty!
452      INLD.second = true;
453
454      for (NonLocalDepInfo::iterator DI = INLD.first.begin(),
455           DE = INLD.first.end(); DI != DE; ++DI) {
456        if (DI->second.getInst() != RemInst) continue;
457
458        // Convert to a dirty entry for the subsequent instruction.
459        Instruction *NextI = 0;
460        if (!RemInst->isTerminator()) {
461          NextI = next(BasicBlock::iterator(RemInst));
462          ReverseDepsToAdd.push_back(std::make_pair(NextI, *I));
463        }
464        DI->second = MemDepResult::getDirty(NextI);
465      }
466    }
467
468    ReverseNonLocalDeps.erase(ReverseDepIt);
469
470    // Add new reverse deps after scanning the set, to avoid invalidating 'Set'
471    while (!ReverseDepsToAdd.empty()) {
472      ReverseNonLocalDeps[ReverseDepsToAdd.back().first]
473        .insert(ReverseDepsToAdd.back().second);
474      ReverseDepsToAdd.pop_back();
475    }
476  }
477
478  assert(!NonLocalDeps.count(RemInst) && "RemInst got reinserted?");
479  AA->deleteValue(RemInst);
480  DEBUG(verifyRemoved(RemInst));
481}
482
483/// verifyRemoved - Verify that the specified instruction does not occur
484/// in our internal data structures.
485void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const {
486  for (LocalDepMapType::const_iterator I = LocalDeps.begin(),
487       E = LocalDeps.end(); I != E; ++I) {
488    assert(I->first != D && "Inst occurs in data structures");
489    assert(I->second.getInst() != D &&
490           "Inst occurs in data structures");
491  }
492
493  for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(),
494       E = NonLocalDeps.end(); I != E; ++I) {
495    assert(I->first != D && "Inst occurs in data structures");
496    const PerInstNLInfo &INLD = I->second;
497    for (NonLocalDepInfo::const_iterator II = INLD.first.begin(),
498         EE = INLD.first.end(); II  != EE; ++II)
499      assert(II->second.getInst() != D && "Inst occurs in data structures");
500  }
501
502  for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(),
503       E = ReverseLocalDeps.end(); I != E; ++I) {
504    assert(I->first != D && "Inst occurs in data structures");
505    for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
506         EE = I->second.end(); II != EE; ++II)
507      assert(*II != D && "Inst occurs in data structures");
508  }
509
510  for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(),
511       E = ReverseNonLocalDeps.end();
512       I != E; ++I) {
513    assert(I->first != D && "Inst occurs in data structures");
514    for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
515         EE = I->second.end(); II != EE; ++II)
516      assert(*II != D && "Inst occurs in data structures");
517  }
518}
519