Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 1 | //===- MemoryDependenceAnalysis.cpp - Mem Deps Implementation --*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 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 |
Owen Anderson | 80b1f09 | 2007-08-08 22:01:54 +0000 | [diff] [blame] | 12 | // alias analysis information, and tries to provide a lazy, caching interface to |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 13 | // a common kind of alias information query. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | 0e575f4 | 2008-11-28 21:45:17 +0000 | [diff] [blame] | 17 | #define DEBUG_TYPE "memdep" |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/MemoryDependenceAnalysis.h" |
Owen Anderson | 7a616a1 | 2007-07-10 17:25:03 +0000 | [diff] [blame] | 19 | #include "llvm/Constants.h" |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 20 | #include "llvm/Instructions.h" |
| 21 | #include "llvm/Function.h" |
| 22 | #include "llvm/Analysis/AliasAnalysis.h" |
Chris Lattner | baad888 | 2008-11-28 22:28:27 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Statistic.h" |
| 24 | #include "llvm/ADT/STLExtras.h" |
Owen Anderson | 4beedbd | 2007-07-24 21:52:37 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CFG.h" |
Tanya Lattner | 63aa160 | 2008-02-06 00:54:55 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 0e575f4 | 2008-11-28 21:45:17 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetData.h" |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Chris Lattner | 0ec48dd | 2008-11-29 22:02:15 +0000 | [diff] [blame] | 31 | STATISTIC(NumCacheNonLocal, "Number of cached non-local responses"); |
| 32 | STATISTIC(NumUncacheNonLocal, "Number of uncached non-local responses"); |
Owen Anderson | 7fad7e3 | 2007-09-09 21:43:49 +0000 | [diff] [blame] | 33 | |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 34 | char MemoryDependenceAnalysis::ID = 0; |
| 35 | |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 36 | // Register this pass... |
Owen Anderson | 776ee1f | 2007-07-10 20:21:08 +0000 | [diff] [blame] | 37 | static RegisterPass<MemoryDependenceAnalysis> X("memdep", |
Chris Lattner | 0e575f4 | 2008-11-28 21:45:17 +0000 | [diff] [blame] | 38 | "Memory Dependence Analysis", false, true); |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 39 | |
| 40 | /// getAnalysisUsage - Does not modify anything. It uses Alias Analysis. |
| 41 | /// |
| 42 | void MemoryDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
| 43 | AU.setPreservesAll(); |
| 44 | AU.addRequiredTransitive<AliasAnalysis>(); |
| 45 | AU.addRequiredTransitive<TargetData>(); |
| 46 | } |
| 47 | |
Owen Anderson | 642a9e3 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 48 | /// getCallSiteDependency - Private helper for finding the local dependencies |
| 49 | /// of a call site. |
Chris Lattner | 4c72400 | 2008-11-29 02:29:27 +0000 | [diff] [blame] | 50 | MemDepResult MemoryDependenceAnalysis:: |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 51 | getCallSiteDependency(CallSite C, BasicBlock::iterator ScanIt, |
| 52 | BasicBlock *BB) { |
Chris Lattner | 7f52422 | 2008-11-29 03:22:12 +0000 | [diff] [blame] | 53 | AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); |
| 54 | TargetData &TD = getAnalysis<TargetData>(); |
Owen Anderson | dbbe816 | 2007-08-07 00:33:45 +0000 | [diff] [blame] | 55 | |
Owen Anderson | 642a9e3 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 56 | // Walk backwards through the block, looking for dependencies |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 57 | while (ScanIt != BB->begin()) { |
| 58 | Instruction *Inst = --ScanIt; |
Owen Anderson | 5f32320 | 2007-07-10 17:59:22 +0000 | [diff] [blame] | 59 | |
| 60 | // If this inst is a memory op, get the pointer it accessed |
Chris Lattner | 00314b3 | 2008-11-29 09:15:21 +0000 | [diff] [blame] | 61 | Value *Pointer = 0; |
| 62 | uint64_t PointerSize = 0; |
| 63 | if (StoreInst *S = dyn_cast<StoreInst>(Inst)) { |
| 64 | Pointer = S->getPointerOperand(); |
| 65 | PointerSize = TD.getTypeStoreSize(S->getOperand(0)->getType()); |
| 66 | } else if (AllocationInst *AI = dyn_cast<AllocationInst>(Inst)) { |
| 67 | Pointer = AI; |
| 68 | if (ConstantInt *C = dyn_cast<ConstantInt>(AI->getArraySize())) |
Chris Lattner | 86b29ef | 2008-11-29 21:22:42 +0000 | [diff] [blame] | 69 | // Use ABI size (size between elements), not store size (size of one |
| 70 | // element without padding). |
Chris Lattner | 00314b3 | 2008-11-29 09:15:21 +0000 | [diff] [blame] | 71 | PointerSize = C->getZExtValue() * |
Chris Lattner | 86b29ef | 2008-11-29 21:22:42 +0000 | [diff] [blame] | 72 | TD.getABITypeSize(AI->getAllocatedType()); |
Owen Anderson | 5f32320 | 2007-07-10 17:59:22 +0000 | [diff] [blame] | 73 | else |
Chris Lattner | 00314b3 | 2008-11-29 09:15:21 +0000 | [diff] [blame] | 74 | PointerSize = ~0UL; |
| 75 | } else if (VAArgInst *V = dyn_cast<VAArgInst>(Inst)) { |
| 76 | Pointer = V->getOperand(0); |
| 77 | PointerSize = TD.getTypeStoreSize(V->getType()); |
| 78 | } else if (FreeInst *F = dyn_cast<FreeInst>(Inst)) { |
| 79 | Pointer = F->getPointerOperand(); |
Owen Anderson | 5f32320 | 2007-07-10 17:59:22 +0000 | [diff] [blame] | 80 | |
| 81 | // FreeInsts erase the entire structure |
Chris Lattner | 00314b3 | 2008-11-29 09:15:21 +0000 | [diff] [blame] | 82 | PointerSize = ~0UL; |
| 83 | } else if (isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) { |
| 84 | if (AA.getModRefBehavior(CallSite::get(Inst)) == |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 85 | AliasAnalysis::DoesNotAccessMemory) |
Chris Lattner | 00314b3 | 2008-11-29 09:15:21 +0000 | [diff] [blame] | 86 | continue; |
| 87 | return MemDepResult::get(Inst); |
Owen Anderson | 202da14 | 2007-07-10 20:39:07 +0000 | [diff] [blame] | 88 | } else |
| 89 | continue; |
Owen Anderson | 5f32320 | 2007-07-10 17:59:22 +0000 | [diff] [blame] | 90 | |
Chris Lattner | 00314b3 | 2008-11-29 09:15:21 +0000 | [diff] [blame] | 91 | if (AA.getModRefInfo(C, Pointer, PointerSize) != AliasAnalysis::NoModRef) |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 92 | return MemDepResult::get(Inst); |
Owen Anderson | 5f32320 | 2007-07-10 17:59:22 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 95 | // No dependence found. |
Chris Lattner | 4c72400 | 2008-11-29 02:29:27 +0000 | [diff] [blame] | 96 | return MemDepResult::getNonLocal(); |
Owen Anderson | 5f32320 | 2007-07-10 17:59:22 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Chris Lattner | 86b29ef | 2008-11-29 21:22:42 +0000 | [diff] [blame] | 99 | /// getNonLocalDependency - Perform a full dependency query for the |
| 100 | /// specified instruction, returning the set of blocks that the value is |
| 101 | /// potentially live across. The returned set of results will include a |
| 102 | /// "NonLocal" result for all blocks where the value is live across. |
| 103 | /// |
| 104 | /// This method assumes the instruction returns a "nonlocal" dependency |
| 105 | /// within its own block. |
| 106 | /// |
Chris Lattner | 396a4a5 | 2008-11-29 21:33:22 +0000 | [diff] [blame] | 107 | void MemoryDependenceAnalysis:: |
| 108 | getNonLocalDependency(Instruction *QueryInst, |
| 109 | SmallVectorImpl<std::pair<BasicBlock*, |
| 110 | MemDepResult> > &Result) { |
Chris Lattner | 86b29ef | 2008-11-29 21:22:42 +0000 | [diff] [blame] | 111 | assert(getDependency(QueryInst).isNonLocal() && |
| 112 | "getNonLocalDependency should only be used on insts with non-local deps!"); |
| 113 | DenseMap<BasicBlock*, DepResultTy> &Cache = NonLocalDeps[QueryInst]; |
Owen Anderson | 4beedbd | 2007-07-24 21:52:37 +0000 | [diff] [blame] | 114 | |
Chris Lattner | 0ec48dd | 2008-11-29 22:02:15 +0000 | [diff] [blame] | 115 | /// DirtyBlocks - This is the set of blocks that need to be recomputed. In |
| 116 | /// the cached case, this can happen due to instructions being deleted etc. In |
| 117 | /// the uncached case, this starts out as the set of predecessors we care |
| 118 | /// about. |
Chris Lattner | 86b29ef | 2008-11-29 21:22:42 +0000 | [diff] [blame] | 119 | SmallVector<BasicBlock*, 32> DirtyBlocks; |
| 120 | |
| 121 | if (!Cache.empty()) { |
| 122 | // If we already have a partially computed set of results, scan them to |
| 123 | // determine what is dirty, seeding our initial DirtyBlocks worklist. |
| 124 | // FIXME: In the "don't need to be updated" case, this is expensive, why not |
| 125 | // have a per-"cache" flag saying it is undirty? |
| 126 | for (DenseMap<BasicBlock*, DepResultTy>::iterator I = Cache.begin(), |
| 127 | E = Cache.end(); I != E; ++I) |
Chris Lattner | 39f372e | 2008-11-29 01:43:36 +0000 | [diff] [blame] | 128 | if (I->second.getInt() == Dirty) |
Chris Lattner | 86b29ef | 2008-11-29 21:22:42 +0000 | [diff] [blame] | 129 | DirtyBlocks.push_back(I->first); |
Owen Anderson | ce4d88a | 2007-09-21 03:53:52 +0000 | [diff] [blame] | 130 | |
Chris Lattner | 0ec48dd | 2008-11-29 22:02:15 +0000 | [diff] [blame] | 131 | NumCacheNonLocal++; |
| 132 | |
| 133 | //cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: " |
| 134 | // << Cache.size() << " cached: " << *QueryInst; |
Chris Lattner | 86b29ef | 2008-11-29 21:22:42 +0000 | [diff] [blame] | 135 | } else { |
| 136 | // Seed DirtyBlocks with each of the preds of QueryInst's block. |
| 137 | BasicBlock *QueryBB = QueryInst->getParent(); |
Chris Lattner | 396a4a5 | 2008-11-29 21:33:22 +0000 | [diff] [blame] | 138 | DirtyBlocks.append(pred_begin(QueryBB), pred_end(QueryBB)); |
Chris Lattner | 0ec48dd | 2008-11-29 22:02:15 +0000 | [diff] [blame] | 139 | NumUncacheNonLocal++; |
Chris Lattner | 4c72400 | 2008-11-29 02:29:27 +0000 | [diff] [blame] | 140 | } |
Chris Lattner | 4f8c18c | 2008-11-29 23:30:39 +0000 | [diff] [blame^] | 141 | |
| 142 | |
Chris Lattner | 86b29ef | 2008-11-29 21:22:42 +0000 | [diff] [blame] | 143 | // Iterate while we still have blocks to update. |
| 144 | while (!DirtyBlocks.empty()) { |
| 145 | BasicBlock *DirtyBB = DirtyBlocks.back(); |
| 146 | DirtyBlocks.pop_back(); |
| 147 | |
| 148 | // Get the entry for this block. Note that this relies on DepResultTy |
| 149 | // default initializing to Dirty. |
| 150 | DepResultTy &DirtyBBEntry = Cache[DirtyBB]; |
Chris Lattner | 4f8c18c | 2008-11-29 23:30:39 +0000 | [diff] [blame^] | 151 | |
Chris Lattner | 86b29ef | 2008-11-29 21:22:42 +0000 | [diff] [blame] | 152 | // If DirtyBBEntry isn't dirty, it ended up on the worklist multiple times. |
| 153 | if (DirtyBBEntry.getInt() != Dirty) continue; |
Chris Lattner | 4f8c18c | 2008-11-29 23:30:39 +0000 | [diff] [blame^] | 154 | |
Chris Lattner | 86b29ef | 2008-11-29 21:22:42 +0000 | [diff] [blame] | 155 | // Find out if this block has a local dependency for QueryInst. |
| 156 | // FIXME: If the dirty entry has an instruction pointer, scan from it! |
| 157 | // FIXME: Don't convert back and forth for MemDepResult <-> DepResultTy. |
Chris Lattner | 0ec48dd | 2008-11-29 22:02:15 +0000 | [diff] [blame] | 158 | |
| 159 | // If the dirty entry has a pointer, start scanning from it so we don't have |
| 160 | // to rescan the entire block. |
| 161 | BasicBlock::iterator ScanPos = DirtyBB->end(); |
| 162 | if (Instruction *Inst = DirtyBBEntry.getPointer()) |
| 163 | ScanPos = Inst; |
| 164 | |
| 165 | DirtyBBEntry = ConvFromResult(getDependencyFrom(QueryInst, ScanPos, |
Chris Lattner | 86b29ef | 2008-11-29 21:22:42 +0000 | [diff] [blame] | 166 | DirtyBB)); |
Chris Lattner | 4f8c18c | 2008-11-29 23:30:39 +0000 | [diff] [blame^] | 167 | |
Chris Lattner | 86b29ef | 2008-11-29 21:22:42 +0000 | [diff] [blame] | 168 | // If the block has a dependency (i.e. it isn't completely transparent to |
| 169 | // the value), remember it! |
| 170 | if (DirtyBBEntry.getInt() != NonLocal) { |
| 171 | // Keep the ReverseNonLocalDeps map up to date so we can efficiently |
Chris Lattner | 4f8c18c | 2008-11-29 23:30:39 +0000 | [diff] [blame^] | 172 | // update this when we remove instructions. |
Chris Lattner | 86b29ef | 2008-11-29 21:22:42 +0000 | [diff] [blame] | 173 | if (Instruction *Inst = DirtyBBEntry.getPointer()) |
| 174 | ReverseNonLocalDeps[Inst].insert(QueryInst); |
| 175 | continue; |
| 176 | } |
| 177 | |
| 178 | // If the block *is* completely transparent to the load, we need to check |
| 179 | // the predecessors of this block. Add them to our worklist. |
Chris Lattner | 4f8c18c | 2008-11-29 23:30:39 +0000 | [diff] [blame^] | 180 | DirtyBlocks.append(pred_begin(DirtyBB), pred_end(DirtyBB)); |
Owen Anderson | 4d13de4 | 2007-08-16 21:27:05 +0000 | [diff] [blame] | 181 | } |
Chris Lattner | 86b29ef | 2008-11-29 21:22:42 +0000 | [diff] [blame] | 182 | |
Chris Lattner | 4f8c18c | 2008-11-29 23:30:39 +0000 | [diff] [blame^] | 183 | |
Chris Lattner | 86b29ef | 2008-11-29 21:22:42 +0000 | [diff] [blame] | 184 | // Copy the result into the output set. |
| 185 | for (DenseMap<BasicBlock*, DepResultTy>::iterator I = Cache.begin(), |
| 186 | E = Cache.end(); I != E; ++I) |
Chris Lattner | 396a4a5 | 2008-11-29 21:33:22 +0000 | [diff] [blame] | 187 | Result.push_back(std::make_pair(I->first, ConvToResult(I->second))); |
Owen Anderson | 4beedbd | 2007-07-24 21:52:37 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 190 | /// getDependency - Return the instruction on which a memory operation |
Dan Gohman | c04575f | 2008-04-10 23:02:38 +0000 | [diff] [blame] | 191 | /// depends. The local parameter indicates if the query should only |
Owen Anderson | 6b278fc | 2007-07-10 17:08:11 +0000 | [diff] [blame] | 192 | /// evaluate dependencies within the same basic block. |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 193 | MemDepResult MemoryDependenceAnalysis:: |
| 194 | getDependencyFrom(Instruction *QueryInst, BasicBlock::iterator ScanIt, |
| 195 | BasicBlock *BB) { |
| 196 | AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); |
| 197 | TargetData &TD = getAnalysis<TargetData>(); |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 198 | |
| 199 | // Get the pointer value for which dependence will be determined |
Chris Lattner | 25a0814 | 2008-11-29 08:51:16 +0000 | [diff] [blame] | 200 | Value *MemPtr = 0; |
| 201 | uint64_t MemSize = 0; |
| 202 | bool MemVolatile = false; |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 203 | |
| 204 | if (StoreInst* S = dyn_cast<StoreInst>(QueryInst)) { |
Chris Lattner | 25a0814 | 2008-11-29 08:51:16 +0000 | [diff] [blame] | 205 | MemPtr = S->getPointerOperand(); |
| 206 | MemSize = TD.getTypeStoreSize(S->getOperand(0)->getType()); |
| 207 | MemVolatile = S->isVolatile(); |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 208 | } else if (LoadInst* L = dyn_cast<LoadInst>(QueryInst)) { |
Chris Lattner | 25a0814 | 2008-11-29 08:51:16 +0000 | [diff] [blame] | 209 | MemPtr = L->getPointerOperand(); |
| 210 | MemSize = TD.getTypeStoreSize(L->getType()); |
| 211 | MemVolatile = L->isVolatile(); |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 212 | } else if (VAArgInst* V = dyn_cast<VAArgInst>(QueryInst)) { |
Chris Lattner | 25a0814 | 2008-11-29 08:51:16 +0000 | [diff] [blame] | 213 | MemPtr = V->getOperand(0); |
| 214 | MemSize = TD.getTypeStoreSize(V->getType()); |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 215 | } else if (FreeInst* F = dyn_cast<FreeInst>(QueryInst)) { |
Chris Lattner | 25a0814 | 2008-11-29 08:51:16 +0000 | [diff] [blame] | 216 | MemPtr = F->getPointerOperand(); |
| 217 | // FreeInsts erase the entire structure, not just a field. |
| 218 | MemSize = ~0UL; |
| 219 | } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst)) |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 220 | return getCallSiteDependency(CallSite::get(QueryInst), ScanIt, BB); |
Chris Lattner | 25a0814 | 2008-11-29 08:51:16 +0000 | [diff] [blame] | 221 | else // Non-memory instructions depend on nothing. |
Chris Lattner | 4c72400 | 2008-11-29 02:29:27 +0000 | [diff] [blame] | 222 | return MemDepResult::getNone(); |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 223 | |
Owen Anderson | 642a9e3 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 224 | // Walk backwards through the basic block, looking for dependencies |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 225 | while (ScanIt != BB->begin()) { |
| 226 | Instruction *Inst = --ScanIt; |
Chris Lattner | a161ab0 | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 227 | |
| 228 | // If the access is volatile and this is a volatile load/store, return a |
| 229 | // dependence. |
| 230 | if (MemVolatile && |
| 231 | ((isa<LoadInst>(Inst) && cast<LoadInst>(Inst)->isVolatile()) || |
| 232 | (isa<StoreInst>(Inst) && cast<StoreInst>(Inst)->isVolatile()))) |
Chris Lattner | 25a0814 | 2008-11-29 08:51:16 +0000 | [diff] [blame] | 233 | return MemDepResult::get(Inst); |
Chris Lattner | a161ab0 | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 234 | |
| 235 | // MemDep is broken w.r.t. loads: it says that two loads of the same pointer |
| 236 | // depend on each other. :( |
| 237 | // FIXME: ELIMINATE THIS! |
| 238 | if (LoadInst *L = dyn_cast<LoadInst>(Inst)) { |
| 239 | Value *Pointer = L->getPointerOperand(); |
| 240 | uint64_t PointerSize = TD.getTypeStoreSize(L->getType()); |
| 241 | |
| 242 | // If we found a pointer, check if it could be the same as our pointer |
| 243 | AliasAnalysis::AliasResult R = |
| 244 | AA.alias(Pointer, PointerSize, MemPtr, MemSize); |
| 245 | |
| 246 | if (R == AliasAnalysis::NoAlias) |
| 247 | continue; |
| 248 | |
| 249 | // May-alias loads don't depend on each other without a dependence. |
| 250 | if (isa<LoadInst>(QueryInst) && R == AliasAnalysis::MayAlias) |
| 251 | continue; |
| 252 | return MemDepResult::get(Inst); |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Chris Lattner | a161ab0 | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 255 | // FIXME: This claims that an access depends on the allocation. This may |
| 256 | // make sense, but is dubious at best. It would be better to fix GVN to |
| 257 | // handle a 'None' Query. |
| 258 | if (AllocationInst *AI = dyn_cast<AllocationInst>(Inst)) { |
| 259 | Value *Pointer = AI; |
| 260 | uint64_t PointerSize; |
| 261 | if (ConstantInt *C = dyn_cast<ConstantInt>(AI->getArraySize())) |
Chris Lattner | 86b29ef | 2008-11-29 21:22:42 +0000 | [diff] [blame] | 262 | // Use ABI size (size between elements), not store size (size of one |
| 263 | // element without padding). |
Chris Lattner | a161ab0 | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 264 | PointerSize = C->getZExtValue() * |
Chris Lattner | 86b29ef | 2008-11-29 21:22:42 +0000 | [diff] [blame] | 265 | TD.getABITypeSize(AI->getAllocatedType()); |
Chris Lattner | a161ab0 | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 266 | else |
| 267 | PointerSize = ~0UL; |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 268 | |
Chris Lattner | a161ab0 | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 269 | AliasAnalysis::AliasResult R = |
| 270 | AA.alias(Pointer, PointerSize, MemPtr, MemSize); |
| 271 | |
| 272 | if (R == AliasAnalysis::NoAlias) |
| 273 | continue; |
| 274 | return MemDepResult::get(Inst); |
| 275 | } |
| 276 | |
| 277 | |
| 278 | // See if this instruction mod/ref's the pointer. |
| 279 | AliasAnalysis::ModRefResult MRR = AA.getModRefInfo(Inst, MemPtr, MemSize); |
| 280 | |
| 281 | if (MRR == AliasAnalysis::NoModRef) |
Chris Lattner | 25a0814 | 2008-11-29 08:51:16 +0000 | [diff] [blame] | 282 | continue; |
| 283 | |
Chris Lattner | a161ab0 | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 284 | // Loads don't depend on read-only instructions. |
| 285 | if (isa<LoadInst>(QueryInst) && MRR == AliasAnalysis::Ref) |
Chris Lattner | 25a0814 | 2008-11-29 08:51:16 +0000 | [diff] [blame] | 286 | continue; |
Chris Lattner | a161ab0 | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 287 | |
| 288 | // Otherwise, there is a dependence. |
Chris Lattner | 25a0814 | 2008-11-29 08:51:16 +0000 | [diff] [blame] | 289 | return MemDepResult::get(Inst); |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 292 | // If we found nothing, return the non-local flag. |
Chris Lattner | 4c72400 | 2008-11-29 02:29:27 +0000 | [diff] [blame] | 293 | return MemDepResult::getNonLocal(); |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 296 | /// getDependency - Return the instruction on which a memory operation |
| 297 | /// depends. |
| 298 | MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) { |
| 299 | Instruction *ScanPos = QueryInst; |
| 300 | |
| 301 | // Check for a cached result |
| 302 | DepResultTy &LocalCache = LocalDeps[QueryInst]; |
| 303 | |
Chris Lattner | 0ec48dd | 2008-11-29 22:02:15 +0000 | [diff] [blame] | 304 | // If the cached entry is non-dirty, just return it. Note that this depends |
| 305 | // on DepResultTy's default constructing to 'dirty'. |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 306 | if (LocalCache.getInt() != Dirty) |
| 307 | return ConvToResult(LocalCache); |
| 308 | |
| 309 | // Otherwise, if we have a dirty entry, we know we can start the scan at that |
| 310 | // instruction, which may save us some work. |
| 311 | if (Instruction *Inst = LocalCache.getPointer()) |
| 312 | ScanPos = Inst; |
| 313 | |
| 314 | // Do the scan. |
| 315 | MemDepResult Res = |
| 316 | getDependencyFrom(QueryInst, ScanPos, QueryInst->getParent()); |
| 317 | |
| 318 | // Remember the result! |
| 319 | // FIXME: Don't convert back and forth! Make a shared helper function. |
| 320 | LocalCache = ConvFromResult(Res); |
| 321 | if (Instruction *I = Res.getInst()) |
Chris Lattner | 8c46527 | 2008-11-29 09:20:15 +0000 | [diff] [blame] | 322 | ReverseLocalDeps[I].insert(QueryInst); |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 323 | |
| 324 | return Res; |
| 325 | } |
| 326 | |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 327 | /// removeInstruction - Remove an instruction from the dependence analysis, |
| 328 | /// updating the dependence of instructions that previously depended on it. |
Owen Anderson | 642a9e3 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 329 | /// This method attempts to keep the cache coherent using the reverse map. |
Chris Lattner | 5f589dc | 2008-11-28 22:04:47 +0000 | [diff] [blame] | 330 | void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) { |
Chris Lattner | 5f589dc | 2008-11-28 22:04:47 +0000 | [diff] [blame] | 331 | // Walk through the Non-local dependencies, removing this one as the value |
| 332 | // for any cached queries. |
Chris Lattner | 39f372e | 2008-11-29 01:43:36 +0000 | [diff] [blame] | 333 | for (DenseMap<BasicBlock*, DepResultTy>::iterator DI = |
Chris Lattner | 8c46527 | 2008-11-29 09:20:15 +0000 | [diff] [blame] | 334 | NonLocalDeps[RemInst].begin(), DE = NonLocalDeps[RemInst].end(); |
Owen Anderson | 5fc4aba | 2007-12-08 01:37:09 +0000 | [diff] [blame] | 335 | DI != DE; ++DI) |
Chris Lattner | 7f52422 | 2008-11-29 03:22:12 +0000 | [diff] [blame] | 336 | if (Instruction *Inst = DI->second.getPointer()) |
Chris Lattner | 8c46527 | 2008-11-29 09:20:15 +0000 | [diff] [blame] | 337 | ReverseNonLocalDeps[Inst].erase(RemInst); |
Owen Anderson | 5fc4aba | 2007-12-08 01:37:09 +0000 | [diff] [blame] | 338 | |
Chris Lattner | baad888 | 2008-11-28 22:28:27 +0000 | [diff] [blame] | 339 | // Shortly after this, we will look for things that depend on RemInst. In |
| 340 | // order to update these, we'll need a new dependency to base them on. We |
| 341 | // could completely delete any entries that depend on this, but it is better |
| 342 | // to make a more accurate approximation where possible. Compute that better |
| 343 | // approximation if we can. |
Chris Lattner | 39f372e | 2008-11-29 01:43:36 +0000 | [diff] [blame] | 344 | DepResultTy NewDependency; |
Chris Lattner | baad888 | 2008-11-28 22:28:27 +0000 | [diff] [blame] | 345 | |
Chris Lattner | 5f589dc | 2008-11-28 22:04:47 +0000 | [diff] [blame] | 346 | // If we have a cached local dependence query for this instruction, remove it. |
Chris Lattner | baad888 | 2008-11-28 22:28:27 +0000 | [diff] [blame] | 347 | // |
Chris Lattner | 39f372e | 2008-11-29 01:43:36 +0000 | [diff] [blame] | 348 | LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst); |
| 349 | if (LocalDepEntry != LocalDeps.end()) { |
Chris Lattner | 7f52422 | 2008-11-29 03:22:12 +0000 | [diff] [blame] | 350 | DepResultTy LocalDep = LocalDepEntry->second; |
Owen Anderson | 9a8ff8c | 2008-01-30 01:24:05 +0000 | [diff] [blame] | 351 | |
Chris Lattner | baad888 | 2008-11-28 22:28:27 +0000 | [diff] [blame] | 352 | // Remove this local dependency info. |
Chris Lattner | 39f372e | 2008-11-29 01:43:36 +0000 | [diff] [blame] | 353 | LocalDeps.erase(LocalDepEntry); |
Chris Lattner | 5f589dc | 2008-11-28 22:04:47 +0000 | [diff] [blame] | 354 | |
Chris Lattner | baad888 | 2008-11-28 22:28:27 +0000 | [diff] [blame] | 355 | // Remove us from DepInst's reverse set now that the local dep info is gone. |
Chris Lattner | 7f52422 | 2008-11-29 03:22:12 +0000 | [diff] [blame] | 356 | if (Instruction *Inst = LocalDep.getPointer()) |
Chris Lattner | 8c46527 | 2008-11-29 09:20:15 +0000 | [diff] [blame] | 357 | ReverseLocalDeps[Inst].erase(RemInst); |
Chris Lattner | baad888 | 2008-11-28 22:28:27 +0000 | [diff] [blame] | 358 | |
| 359 | // If we have unconfirmed info, don't trust it. |
Chris Lattner | 7f52422 | 2008-11-29 03:22:12 +0000 | [diff] [blame] | 360 | if (LocalDep.getInt() != Dirty) { |
Chris Lattner | baad888 | 2008-11-28 22:28:27 +0000 | [diff] [blame] | 361 | // If we have a confirmed non-local flag, use it. |
Chris Lattner | 39f372e | 2008-11-29 01:43:36 +0000 | [diff] [blame] | 362 | if (LocalDep.getInt() == NonLocal || LocalDep.getInt() == None) { |
Chris Lattner | d3d12ec | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 363 | // The only time this dependency is confirmed is if it is non-local. |
Chris Lattner | 39f372e | 2008-11-29 01:43:36 +0000 | [diff] [blame] | 364 | NewDependency = LocalDep; |
Chris Lattner | baad888 | 2008-11-28 22:28:27 +0000 | [diff] [blame] | 365 | } else { |
| 366 | // If we have dep info for RemInst, set them to it. |
Chris Lattner | 39f372e | 2008-11-29 01:43:36 +0000 | [diff] [blame] | 367 | Instruction *NDI = next(BasicBlock::iterator(LocalDep.getPointer())); |
| 368 | if (NDI != RemInst) // Don't use RemInst for the new dependency! |
Chris Lattner | 7f52422 | 2008-11-29 03:22:12 +0000 | [diff] [blame] | 369 | NewDependency = DepResultTy(NDI, Dirty); |
Chris Lattner | baad888 | 2008-11-28 22:28:27 +0000 | [diff] [blame] | 370 | } |
David Greene | df46419 | 2007-07-31 20:01:27 +0000 | [diff] [blame] | 371 | } |
Owen Anderson | a8701a6 | 2008-02-05 04:34:03 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Chris Lattner | baad888 | 2008-11-28 22:28:27 +0000 | [diff] [blame] | 374 | // If we don't already have a local dependency answer for this instruction, |
| 375 | // use the immediate successor of RemInst. We use the successor because |
| 376 | // getDependence starts by checking the immediate predecessor of what is in |
| 377 | // the cache. |
Chris Lattner | 7f52422 | 2008-11-29 03:22:12 +0000 | [diff] [blame] | 378 | if (NewDependency == DepResultTy(0, Dirty)) |
| 379 | NewDependency = DepResultTy(next(BasicBlock::iterator(RemInst)), Dirty); |
Chris Lattner | baad888 | 2008-11-28 22:28:27 +0000 | [diff] [blame] | 380 | |
Chris Lattner | d3d12ec | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 381 | // Loop over all of the things that depend on the instruction we're removing. |
| 382 | // |
Chris Lattner | 4f8c18c | 2008-11-29 23:30:39 +0000 | [diff] [blame^] | 383 | SmallVector<std::pair<Instruction*, Instruction*>, 8> ReverseDepsToAdd; |
| 384 | |
Chris Lattner | 8c46527 | 2008-11-29 09:20:15 +0000 | [diff] [blame] | 385 | ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst); |
| 386 | if (ReverseDepIt != ReverseLocalDeps.end()) { |
Chris Lattner | d3d12ec | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 387 | SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second; |
| 388 | for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(), |
| 389 | E = ReverseDeps.end(); I != E; ++I) { |
| 390 | Instruction *InstDependingOnRemInst = *I; |
| 391 | |
| 392 | // If we thought the instruction depended on itself (possible for |
| 393 | // unconfirmed dependencies) ignore the update. |
| 394 | if (InstDependingOnRemInst == RemInst) continue; |
| 395 | |
| 396 | // Insert the new dependencies. |
Chris Lattner | 4f8c18c | 2008-11-29 23:30:39 +0000 | [diff] [blame^] | 397 | // FIXME: DEPENDENCIES ARE NOT TRANSITIVE! |
| 398 | //cerr << "FOO:\n"; |
| 399 | //RemInst->dump(); |
| 400 | //InstDependingOnRemInst->dump(); |
Chris Lattner | 7f52422 | 2008-11-29 03:22:12 +0000 | [diff] [blame] | 401 | LocalDeps[InstDependingOnRemInst] = NewDependency; |
Chris Lattner | d3d12ec | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 402 | |
| 403 | // If our NewDependency is an instruction, make sure to remember that new |
| 404 | // things depend on it. |
Chris Lattner | 4f8c18c | 2008-11-29 23:30:39 +0000 | [diff] [blame^] | 405 | if (Instruction *Inst = NewDependency.getPointer()) { |
| 406 | assert(Inst != RemInst); |
| 407 | ReverseDepsToAdd.push_back(std::make_pair(Inst, |
| 408 | InstDependingOnRemInst)); |
| 409 | } |
Chris Lattner | d3d12ec | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 410 | } |
Chris Lattner | 4f8c18c | 2008-11-29 23:30:39 +0000 | [diff] [blame^] | 411 | |
| 412 | ReverseLocalDeps.erase(ReverseDepIt); |
| 413 | |
| 414 | // Add new reverse deps after scanning the set, to avoid invalidating the |
| 415 | // 'ReverseDeps' reference. |
| 416 | while (!ReverseDepsToAdd.empty()) { |
| 417 | ReverseLocalDeps[ReverseDepsToAdd.back().first] |
| 418 | .insert(ReverseDepsToAdd.back().second); |
| 419 | ReverseDepsToAdd.pop_back(); |
| 420 | } |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 421 | } |
Owen Anderson | 4d13de4 | 2007-08-16 21:27:05 +0000 | [diff] [blame] | 422 | |
Chris Lattner | 8c46527 | 2008-11-29 09:20:15 +0000 | [diff] [blame] | 423 | ReverseDepIt = ReverseNonLocalDeps.find(RemInst); |
| 424 | if (ReverseDepIt != ReverseNonLocalDeps.end()) { |
Chris Lattner | d3d12ec | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 425 | SmallPtrSet<Instruction*, 4>& set = ReverseDepIt->second; |
Owen Anderson | 4d13de4 | 2007-08-16 21:27:05 +0000 | [diff] [blame] | 426 | for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end(); |
| 427 | I != E; ++I) |
Chris Lattner | 8c46527 | 2008-11-29 09:20:15 +0000 | [diff] [blame] | 428 | for (DenseMap<BasicBlock*, DepResultTy>::iterator |
| 429 | DI = NonLocalDeps[*I].begin(), DE = NonLocalDeps[*I].end(); |
Owen Anderson | ce4d88a | 2007-09-21 03:53:52 +0000 | [diff] [blame] | 430 | DI != DE; ++DI) |
Chris Lattner | 0ec48dd | 2008-11-29 22:02:15 +0000 | [diff] [blame] | 431 | if (DI->second.getPointer() == RemInst) { |
| 432 | // Convert to a dirty entry for the subsequent instruction. |
| 433 | DI->second.setInt(Dirty); |
| 434 | if (RemInst->isTerminator()) |
| 435 | DI->second.setPointer(0); |
| 436 | else { |
| 437 | Instruction *NextI = next(BasicBlock::iterator(RemInst)); |
| 438 | DI->second.setPointer(NextI); |
Chris Lattner | 4f8c18c | 2008-11-29 23:30:39 +0000 | [diff] [blame^] | 439 | assert(NextI != RemInst); |
Chris Lattner | 0ec48dd | 2008-11-29 22:02:15 +0000 | [diff] [blame] | 440 | ReverseDepsToAdd.push_back(std::make_pair(NextI, *I)); |
| 441 | } |
| 442 | } |
Chris Lattner | 4f8c18c | 2008-11-29 23:30:39 +0000 | [diff] [blame^] | 443 | |
| 444 | ReverseNonLocalDeps.erase(ReverseDepIt); |
| 445 | |
Chris Lattner | 0ec48dd | 2008-11-29 22:02:15 +0000 | [diff] [blame] | 446 | // Add new reverse deps after scanning the set, to avoid invalidating 'Set' |
| 447 | while (!ReverseDepsToAdd.empty()) { |
| 448 | ReverseNonLocalDeps[ReverseDepsToAdd.back().first] |
| 449 | .insert(ReverseDepsToAdd.back().second); |
| 450 | ReverseDepsToAdd.pop_back(); |
| 451 | } |
Owen Anderson | 4d13de4 | 2007-08-16 21:27:05 +0000 | [diff] [blame] | 452 | } |
Owen Anderson | 5fc4aba | 2007-12-08 01:37:09 +0000 | [diff] [blame] | 453 | |
Chris Lattner | 8c46527 | 2008-11-29 09:20:15 +0000 | [diff] [blame] | 454 | NonLocalDeps.erase(RemInst); |
Chris Lattner | 5f589dc | 2008-11-28 22:04:47 +0000 | [diff] [blame] | 455 | getAnalysis<AliasAnalysis>().deleteValue(RemInst); |
Chris Lattner | 5f589dc | 2008-11-28 22:04:47 +0000 | [diff] [blame] | 456 | DEBUG(verifyRemoved(RemInst)); |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 457 | } |
Chris Lattner | 729b237 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 458 | |
| 459 | /// verifyRemoved - Verify that the specified instruction does not occur |
| 460 | /// in our internal data structures. |
| 461 | void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const { |
| 462 | for (LocalDepMapType::const_iterator I = LocalDeps.begin(), |
| 463 | E = LocalDeps.end(); I != E; ++I) { |
| 464 | assert(I->first != D && "Inst occurs in data structures"); |
| 465 | assert(I->second.getPointer() != D && |
| 466 | "Inst occurs in data structures"); |
| 467 | } |
| 468 | |
| 469 | for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(), |
| 470 | E = NonLocalDeps.end(); I != E; ++I) { |
| 471 | assert(I->first != D && "Inst occurs in data structures"); |
| 472 | for (DenseMap<BasicBlock*, DepResultTy>::iterator II = I->second.begin(), |
| 473 | EE = I->second.end(); II != EE; ++II) |
| 474 | assert(II->second.getPointer() != D && "Inst occurs in data structures"); |
| 475 | } |
| 476 | |
| 477 | for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(), |
| 478 | E = ReverseLocalDeps.end(); I != E; ++I) |
| 479 | for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(), |
| 480 | EE = I->second.end(); II != EE; ++II) |
| 481 | assert(*II != D && "Inst occurs in data structures"); |
| 482 | |
| 483 | for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(), |
| 484 | E = ReverseNonLocalDeps.end(); |
| 485 | I != E; ++I) |
| 486 | for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(), |
| 487 | EE = I->second.end(); II != EE; ++II) |
| 488 | assert(*II != D && "Inst occurs in data structures"); |
| 489 | } |