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" |
| 19 | #include "llvm/Instructions.h" |
Owen Anderson | f6cec85 | 2009-03-09 05:12:38 +0000 | [diff] [blame] | 20 | #include "llvm/IntrinsicInst.h" |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 21 | #include "llvm/Function.h" |
| 22 | #include "llvm/Analysis/AliasAnalysis.h" |
Chris Lattner | 6f7b210 | 2009-11-27 22:05:15 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/Dominators.h" |
Chris Lattner | e19e4ba | 2009-11-27 00:34:38 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/InstructionSimplify.h" |
Victor Hernandez | f006b18 | 2009-10-27 20:05:49 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/MemoryBuiltins.h" |
Chris Lattner | baad888 | 2008-11-28 22:28:27 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/Statistic.h" |
Duncan Sands | 7050f3d | 2008-12-10 09:38:36 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 4012fdd | 2008-12-09 06:28:49 +0000 | [diff] [blame] | 28 | #include "llvm/Support/PredIteratorCache.h" |
Chris Lattner | 0e575f4 | 2008-11-28 21:45:17 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Debug.h" |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 32 | STATISTIC(NumCacheNonLocal, "Number of fully cached non-local responses"); |
| 33 | STATISTIC(NumCacheDirtyNonLocal, "Number of dirty cached non-local responses"); |
Chris Lattner | 0ec48dd | 2008-11-29 22:02:15 +0000 | [diff] [blame] | 34 | STATISTIC(NumUncacheNonLocal, "Number of uncached non-local responses"); |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 35 | |
| 36 | STATISTIC(NumCacheNonLocalPtr, |
| 37 | "Number of fully cached non-local ptr responses"); |
| 38 | STATISTIC(NumCacheDirtyNonLocalPtr, |
| 39 | "Number of cached, but dirty, non-local ptr responses"); |
| 40 | STATISTIC(NumUncacheNonLocalPtr, |
| 41 | "Number of uncached non-local ptr responses"); |
Chris Lattner | 11dcd8d | 2008-12-08 07:31:50 +0000 | [diff] [blame] | 42 | STATISTIC(NumCacheCompleteNonLocalPtr, |
| 43 | "Number of block queries that were completely cached"); |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 44 | |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 45 | char MemoryDependenceAnalysis::ID = 0; |
| 46 | |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 47 | // Register this pass... |
Owen Anderson | 776ee1f | 2007-07-10 20:21:08 +0000 | [diff] [blame] | 48 | static RegisterPass<MemoryDependenceAnalysis> X("memdep", |
Chris Lattner | 0e575f4 | 2008-11-28 21:45:17 +0000 | [diff] [blame] | 49 | "Memory Dependence Analysis", false, true); |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 4012fdd | 2008-12-09 06:28:49 +0000 | [diff] [blame] | 51 | MemoryDependenceAnalysis::MemoryDependenceAnalysis() |
| 52 | : FunctionPass(&ID), PredCache(0) { |
| 53 | } |
| 54 | MemoryDependenceAnalysis::~MemoryDependenceAnalysis() { |
| 55 | } |
| 56 | |
| 57 | /// Clean up memory in between runs |
| 58 | void MemoryDependenceAnalysis::releaseMemory() { |
| 59 | LocalDeps.clear(); |
| 60 | NonLocalDeps.clear(); |
| 61 | NonLocalPointerDeps.clear(); |
| 62 | ReverseLocalDeps.clear(); |
| 63 | ReverseNonLocalDeps.clear(); |
| 64 | ReverseNonLocalPtrDeps.clear(); |
| 65 | PredCache->clear(); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 70 | /// getAnalysisUsage - Does not modify anything. It uses Alias Analysis. |
| 71 | /// |
| 72 | void MemoryDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
| 73 | AU.setPreservesAll(); |
| 74 | AU.addRequiredTransitive<AliasAnalysis>(); |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Chris Lattner | d777d40 | 2008-11-30 19:24:31 +0000 | [diff] [blame] | 77 | bool MemoryDependenceAnalysis::runOnFunction(Function &) { |
| 78 | AA = &getAnalysis<AliasAnalysis>(); |
Chris Lattner | 4012fdd | 2008-12-09 06:28:49 +0000 | [diff] [blame] | 79 | if (PredCache == 0) |
| 80 | PredCache.reset(new PredIteratorCache()); |
Chris Lattner | d777d40 | 2008-11-30 19:24:31 +0000 | [diff] [blame] | 81 | return false; |
| 82 | } |
| 83 | |
Chris Lattner | d44745d | 2008-12-07 18:39:13 +0000 | [diff] [blame] | 84 | /// RemoveFromReverseMap - This is a helper function that removes Val from |
| 85 | /// 'Inst's set in ReverseMap. If the set becomes empty, remove Inst's entry. |
| 86 | template <typename KeyTy> |
| 87 | static void RemoveFromReverseMap(DenseMap<Instruction*, |
Chris Lattner | 6a0dcc1 | 2009-03-29 00:24:04 +0000 | [diff] [blame] | 88 | SmallPtrSet<KeyTy, 4> > &ReverseMap, |
| 89 | Instruction *Inst, KeyTy Val) { |
| 90 | typename DenseMap<Instruction*, SmallPtrSet<KeyTy, 4> >::iterator |
Chris Lattner | d44745d | 2008-12-07 18:39:13 +0000 | [diff] [blame] | 91 | InstIt = ReverseMap.find(Inst); |
| 92 | assert(InstIt != ReverseMap.end() && "Reverse map out of sync?"); |
| 93 | bool Found = InstIt->second.erase(Val); |
| 94 | assert(Found && "Invalid reverse map!"); Found=Found; |
| 95 | if (InstIt->second.empty()) |
| 96 | ReverseMap.erase(InstIt); |
| 97 | } |
| 98 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 99 | |
Chris Lattner | 8ef57c5 | 2008-12-07 00:35:51 +0000 | [diff] [blame] | 100 | /// getCallSiteDependencyFrom - Private helper for finding the local |
| 101 | /// dependencies of a call site. |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 102 | MemDepResult MemoryDependenceAnalysis:: |
Chris Lattner | 20d6f09 | 2008-12-09 21:19:42 +0000 | [diff] [blame] | 103 | getCallSiteDependencyFrom(CallSite CS, bool isReadOnlyCall, |
| 104 | BasicBlock::iterator ScanIt, BasicBlock *BB) { |
Owen Anderson | 642a9e3 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 105 | // Walk backwards through the block, looking for dependencies |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 106 | while (ScanIt != BB->begin()) { |
| 107 | Instruction *Inst = --ScanIt; |
Owen Anderson | 5f32320 | 2007-07-10 17:59:22 +0000 | [diff] [blame] | 108 | |
| 109 | // If this inst is a memory op, get the pointer it accessed |
Chris Lattner | 00314b3 | 2008-11-29 09:15:21 +0000 | [diff] [blame] | 110 | Value *Pointer = 0; |
| 111 | uint64_t PointerSize = 0; |
| 112 | if (StoreInst *S = dyn_cast<StoreInst>(Inst)) { |
| 113 | Pointer = S->getPointerOperand(); |
Dan Gohman | f581213 | 2009-07-31 20:53:12 +0000 | [diff] [blame] | 114 | PointerSize = AA->getTypeStoreSize(S->getOperand(0)->getType()); |
Chris Lattner | 00314b3 | 2008-11-29 09:15:21 +0000 | [diff] [blame] | 115 | } else if (VAArgInst *V = dyn_cast<VAArgInst>(Inst)) { |
| 116 | Pointer = V->getOperand(0); |
Dan Gohman | f581213 | 2009-07-31 20:53:12 +0000 | [diff] [blame] | 117 | PointerSize = AA->getTypeStoreSize(V->getType()); |
Victor Hernandez | 046e78c | 2009-10-26 23:43:48 +0000 | [diff] [blame] | 118 | } else if (isFreeCall(Inst)) { |
| 119 | Pointer = Inst->getOperand(1); |
| 120 | // calls to free() erase the entire structure |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 121 | PointerSize = ~0ULL; |
Chris Lattner | 00314b3 | 2008-11-29 09:15:21 +0000 | [diff] [blame] | 122 | } else if (isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) { |
Owen Anderson | f6cec85 | 2009-03-09 05:12:38 +0000 | [diff] [blame] | 123 | // Debug intrinsics don't cause dependences. |
Dale Johannesen | 497cb6f | 2009-03-11 21:13:01 +0000 | [diff] [blame] | 124 | if (isa<DbgInfoIntrinsic>(Inst)) continue; |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 125 | CallSite InstCS = CallSite::get(Inst); |
| 126 | // If these two calls do not interfere, look past it. |
Chris Lattner | 20d6f09 | 2008-12-09 21:19:42 +0000 | [diff] [blame] | 127 | switch (AA->getModRefInfo(CS, InstCS)) { |
| 128 | case AliasAnalysis::NoModRef: |
| 129 | // If the two calls don't interact (e.g. InstCS is readnone) keep |
| 130 | // scanning. |
Chris Lattner | 00314b3 | 2008-11-29 09:15:21 +0000 | [diff] [blame] | 131 | continue; |
Chris Lattner | 20d6f09 | 2008-12-09 21:19:42 +0000 | [diff] [blame] | 132 | case AliasAnalysis::Ref: |
| 133 | // If the two calls read the same memory locations and CS is a readonly |
| 134 | // function, then we have two cases: 1) the calls may not interfere with |
| 135 | // each other at all. 2) the calls may produce the same value. In case |
| 136 | // #1 we want to ignore the values, in case #2, we want to return Inst |
| 137 | // as a Def dependence. This allows us to CSE in cases like: |
| 138 | // X = strlen(P); |
| 139 | // memchr(...); |
| 140 | // Y = strlen(P); // Y = X |
| 141 | if (isReadOnlyCall) { |
| 142 | if (CS.getCalledFunction() != 0 && |
| 143 | CS.getCalledFunction() == InstCS.getCalledFunction()) |
| 144 | return MemDepResult::getDef(Inst); |
| 145 | // Ignore unrelated read/read call dependences. |
| 146 | continue; |
| 147 | } |
| 148 | // FALL THROUGH |
| 149 | default: |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 150 | return MemDepResult::getClobber(Inst); |
Chris Lattner | 20d6f09 | 2008-12-09 21:19:42 +0000 | [diff] [blame] | 151 | } |
Chris Lattner | cfbb634 | 2008-11-30 01:44:00 +0000 | [diff] [blame] | 152 | } else { |
| 153 | // Non-memory instruction. |
Owen Anderson | 202da14 | 2007-07-10 20:39:07 +0000 | [diff] [blame] | 154 | continue; |
Chris Lattner | cfbb634 | 2008-11-30 01:44:00 +0000 | [diff] [blame] | 155 | } |
Owen Anderson | 5f32320 | 2007-07-10 17:59:22 +0000 | [diff] [blame] | 156 | |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 157 | if (AA->getModRefInfo(CS, Pointer, PointerSize) != AliasAnalysis::NoModRef) |
| 158 | return MemDepResult::getClobber(Inst); |
Owen Anderson | 5f32320 | 2007-07-10 17:59:22 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 161 | // No dependence found. If this is the entry block of the function, it is a |
| 162 | // clobber, otherwise it is non-local. |
| 163 | if (BB != &BB->getParent()->getEntryBlock()) |
| 164 | return MemDepResult::getNonLocal(); |
| 165 | return MemDepResult::getClobber(ScanIt); |
Owen Anderson | 5f32320 | 2007-07-10 17:59:22 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 168 | /// getPointerDependencyFrom - Return the instruction on which a memory |
| 169 | /// location depends. If isLoad is true, this routine ignore may-aliases with |
| 170 | /// read-only operations. |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 171 | MemDepResult MemoryDependenceAnalysis:: |
Owen Anderson | 4bc737c | 2009-10-28 06:18:42 +0000 | [diff] [blame] | 172 | getPointerDependencyFrom(Value *MemPtr, uint64_t MemSize, bool isLoad, |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 173 | BasicBlock::iterator ScanIt, BasicBlock *BB) { |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 174 | |
Chris Lattner | 1e8de49 | 2009-12-01 21:16:01 +0000 | [diff] [blame^] | 175 | Value *InvariantTag = 0; |
Owen Anderson | 4bc737c | 2009-10-28 06:18:42 +0000 | [diff] [blame] | 176 | |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 177 | // Walk backwards through the basic block, looking for dependencies. |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 178 | while (ScanIt != BB->begin()) { |
| 179 | Instruction *Inst = --ScanIt; |
Chris Lattner | a161ab0 | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 180 | |
Owen Anderson | 4bc737c | 2009-10-28 06:18:42 +0000 | [diff] [blame] | 181 | // If we're in an invariant region, no dependencies can be found before |
| 182 | // we pass an invariant-begin marker. |
Chris Lattner | 1e8de49 | 2009-12-01 21:16:01 +0000 | [diff] [blame^] | 183 | if (InvariantTag == Inst) { |
| 184 | InvariantTag = 0; |
Owen Anderson | 4bc737c | 2009-10-28 06:18:42 +0000 | [diff] [blame] | 185 | continue; |
Chris Lattner | 1ffb70f | 2009-12-01 21:15:15 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) { |
Owen Anderson | b62f792 | 2009-10-28 07:05:35 +0000 | [diff] [blame] | 189 | // If we pass an invariant-end marker, then we've just entered an |
| 190 | // invariant region and can start ignoring dependencies. |
Owen Anderson | 4bc737c | 2009-10-28 06:18:42 +0000 | [diff] [blame] | 191 | if (II->getIntrinsicID() == Intrinsic::invariant_end) { |
Chris Lattner | 1e8de49 | 2009-12-01 21:16:01 +0000 | [diff] [blame^] | 192 | uint64_t InvariantSize = ~0ULL; |
Nick Lewycky | f27f115 | 2009-11-22 02:38:11 +0000 | [diff] [blame] | 193 | if (ConstantInt *CI = dyn_cast<ConstantInt>(II->getOperand(2))) |
Chris Lattner | 1e8de49 | 2009-12-01 21:16:01 +0000 | [diff] [blame^] | 194 | InvariantSize = CI->getZExtValue(); |
Owen Anderson | 4bc737c | 2009-10-28 06:18:42 +0000 | [diff] [blame] | 195 | |
| 196 | AliasAnalysis::AliasResult R = |
Chris Lattner | 1e8de49 | 2009-12-01 21:16:01 +0000 | [diff] [blame^] | 197 | AA->alias(II->getOperand(3), InvariantSize, MemPtr, MemSize); |
Owen Anderson | 4bc737c | 2009-10-28 06:18:42 +0000 | [diff] [blame] | 198 | if (R == AliasAnalysis::MustAlias) { |
Chris Lattner | 1e8de49 | 2009-12-01 21:16:01 +0000 | [diff] [blame^] | 199 | InvariantTag = II->getOperand(1); |
Owen Anderson | 4bc737c | 2009-10-28 06:18:42 +0000 | [diff] [blame] | 200 | continue; |
| 201 | } |
Owen Anderson | b62f792 | 2009-10-28 07:05:35 +0000 | [diff] [blame] | 202 | |
| 203 | // If we reach a lifetime begin or end marker, then the query ends here |
| 204 | // because the value is undefined. |
| 205 | } else if (II->getIntrinsicID() == Intrinsic::lifetime_start || |
Nick Lewycky | 2eac949 | 2009-11-29 18:10:39 +0000 | [diff] [blame] | 206 | II->getIntrinsicID() == Intrinsic::lifetime_end) { |
Chris Lattner | 1e8de49 | 2009-12-01 21:16:01 +0000 | [diff] [blame^] | 207 | uint64_t InvariantSize = ~0ULL; |
Nick Lewycky | f27f115 | 2009-11-22 02:38:11 +0000 | [diff] [blame] | 208 | if (ConstantInt *CI = dyn_cast<ConstantInt>(II->getOperand(1))) |
Chris Lattner | 1e8de49 | 2009-12-01 21:16:01 +0000 | [diff] [blame^] | 209 | InvariantSize = CI->getZExtValue(); |
Owen Anderson | b62f792 | 2009-10-28 07:05:35 +0000 | [diff] [blame] | 210 | |
| 211 | AliasAnalysis::AliasResult R = |
Chris Lattner | 1e8de49 | 2009-12-01 21:16:01 +0000 | [diff] [blame^] | 212 | AA->alias(II->getOperand(2), InvariantSize, MemPtr, MemSize); |
Owen Anderson | b62f792 | 2009-10-28 07:05:35 +0000 | [diff] [blame] | 213 | if (R == AliasAnalysis::MustAlias) |
| 214 | return MemDepResult::getDef(II); |
Owen Anderson | 4bc737c | 2009-10-28 06:18:42 +0000 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
| 218 | // If we're querying on a load and we're in an invariant region, we're done |
| 219 | // at this point. Nothing a load depends on can live in an invariant region. |
Chris Lattner | 1e8de49 | 2009-12-01 21:16:01 +0000 | [diff] [blame^] | 220 | if (isLoad && InvariantTag) continue; |
Owen Anderson | 4bc737c | 2009-10-28 06:18:42 +0000 | [diff] [blame] | 221 | |
Owen Anderson | f6cec85 | 2009-03-09 05:12:38 +0000 | [diff] [blame] | 222 | // Debug intrinsics don't cause dependences. |
| 223 | if (isa<DbgInfoIntrinsic>(Inst)) continue; |
| 224 | |
Chris Lattner | cfbb634 | 2008-11-30 01:44:00 +0000 | [diff] [blame] | 225 | // Values depend on loads if the pointers are must aliased. This means that |
| 226 | // a load depends on another must aliased load from the same value. |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 227 | if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) { |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 228 | Value *Pointer = LI->getPointerOperand(); |
Dan Gohman | f581213 | 2009-07-31 20:53:12 +0000 | [diff] [blame] | 229 | uint64_t PointerSize = AA->getTypeStoreSize(LI->getType()); |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 230 | |
| 231 | // If we found a pointer, check if it could be the same as our pointer. |
Chris Lattner | a161ab0 | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 232 | AliasAnalysis::AliasResult R = |
Chris Lattner | d777d40 | 2008-11-30 19:24:31 +0000 | [diff] [blame] | 233 | AA->alias(Pointer, PointerSize, MemPtr, MemSize); |
Chris Lattner | a161ab0 | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 234 | if (R == AliasAnalysis::NoAlias) |
| 235 | continue; |
| 236 | |
| 237 | // May-alias loads don't depend on each other without a dependence. |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 238 | if (isLoad && R == AliasAnalysis::MayAlias) |
Chris Lattner | a161ab0 | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 239 | continue; |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 240 | // Stores depend on may and must aliased loads, loads depend on must-alias |
| 241 | // loads. |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 242 | return MemDepResult::getDef(Inst); |
| 243 | } |
| 244 | |
| 245 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
Owen Anderson | a85a664 | 2009-10-28 06:30:52 +0000 | [diff] [blame] | 246 | // There can't be stores to the value we care about inside an |
| 247 | // invariant region. |
Chris Lattner | 1e8de49 | 2009-12-01 21:16:01 +0000 | [diff] [blame^] | 248 | if (InvariantTag) continue; |
Owen Anderson | a85a664 | 2009-10-28 06:30:52 +0000 | [diff] [blame] | 249 | |
Chris Lattner | ab9cf12 | 2009-05-25 21:28:56 +0000 | [diff] [blame] | 250 | // If alias analysis can tell that this store is guaranteed to not modify |
| 251 | // the query pointer, ignore it. Use getModRefInfo to handle cases where |
| 252 | // the query pointer points to constant memory etc. |
| 253 | if (AA->getModRefInfo(SI, MemPtr, MemSize) == AliasAnalysis::NoModRef) |
| 254 | continue; |
| 255 | |
| 256 | // Ok, this store might clobber the query pointer. Check to see if it is |
| 257 | // a must alias: in this case, we want to return this as a def. |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 258 | Value *Pointer = SI->getPointerOperand(); |
Dan Gohman | f581213 | 2009-07-31 20:53:12 +0000 | [diff] [blame] | 259 | uint64_t PointerSize = AA->getTypeStoreSize(SI->getOperand(0)->getType()); |
Chris Lattner | ab9cf12 | 2009-05-25 21:28:56 +0000 | [diff] [blame] | 260 | |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 261 | // If we found a pointer, check if it could be the same as our pointer. |
| 262 | AliasAnalysis::AliasResult R = |
| 263 | AA->alias(Pointer, PointerSize, MemPtr, MemSize); |
| 264 | |
| 265 | if (R == AliasAnalysis::NoAlias) |
| 266 | continue; |
| 267 | if (R == AliasAnalysis::MayAlias) |
| 268 | return MemDepResult::getClobber(Inst); |
| 269 | return MemDepResult::getDef(Inst); |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 270 | } |
Chris Lattner | 237a828 | 2008-11-30 01:39:32 +0000 | [diff] [blame] | 271 | |
| 272 | // If this is an allocation, and if we know that the accessed pointer is to |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 273 | // the allocation, return Def. This means that there is no dependence and |
Chris Lattner | 237a828 | 2008-11-30 01:39:32 +0000 | [diff] [blame] | 274 | // the access can be optimized based on that. For example, a load could |
| 275 | // turn into undef. |
Victor Hernandez | 5c78736 | 2009-10-13 01:42:53 +0000 | [diff] [blame] | 276 | // Note: Only determine this to be a malloc if Inst is the malloc call, not |
| 277 | // a subsequent bitcast of the malloc call result. There can be stores to |
| 278 | // the malloced memory between the malloc call and its bitcast uses, and we |
| 279 | // need to continue scanning until the malloc call. |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 280 | if (isa<AllocaInst>(Inst) || extractMallocCall(Inst)) { |
Victor Hernandez | 46e8312 | 2009-09-18 21:34:51 +0000 | [diff] [blame] | 281 | Value *AccessPtr = MemPtr->getUnderlyingObject(); |
| 282 | |
| 283 | if (AccessPtr == Inst || |
| 284 | AA->alias(Inst, 1, AccessPtr, 1) == AliasAnalysis::MustAlias) |
| 285 | return MemDepResult::getDef(Inst); |
| 286 | continue; |
| 287 | } |
| 288 | |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 289 | // See if this instruction (e.g. a call or vaarg) mod/ref's the pointer. |
Chris Lattner | 3579e44 | 2008-12-09 19:47:40 +0000 | [diff] [blame] | 290 | switch (AA->getModRefInfo(Inst, MemPtr, MemSize)) { |
| 291 | case AliasAnalysis::NoModRef: |
| 292 | // If the call has no effect on the queried pointer, just ignore it. |
Chris Lattner | 25a0814 | 2008-11-29 08:51:16 +0000 | [diff] [blame] | 293 | continue; |
Owen Anderson | a85a664 | 2009-10-28 06:30:52 +0000 | [diff] [blame] | 294 | case AliasAnalysis::Mod: |
| 295 | // If we're in an invariant region, we can ignore calls that ONLY |
| 296 | // modify the pointer. |
Chris Lattner | 1e8de49 | 2009-12-01 21:16:01 +0000 | [diff] [blame^] | 297 | if (InvariantTag) continue; |
Owen Anderson | a85a664 | 2009-10-28 06:30:52 +0000 | [diff] [blame] | 298 | return MemDepResult::getClobber(Inst); |
Chris Lattner | 3579e44 | 2008-12-09 19:47:40 +0000 | [diff] [blame] | 299 | case AliasAnalysis::Ref: |
| 300 | // If the call is known to never store to the pointer, and if this is a |
| 301 | // load query, we can safely ignore it (scan past it). |
| 302 | if (isLoad) |
| 303 | continue; |
Chris Lattner | 3579e44 | 2008-12-09 19:47:40 +0000 | [diff] [blame] | 304 | default: |
| 305 | // Otherwise, there is a potential dependence. Return a clobber. |
| 306 | return MemDepResult::getClobber(Inst); |
| 307 | } |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 310 | // No dependence found. If this is the entry block of the function, it is a |
| 311 | // clobber, otherwise it is non-local. |
| 312 | if (BB != &BB->getParent()->getEntryBlock()) |
| 313 | return MemDepResult::getNonLocal(); |
| 314 | return MemDepResult::getClobber(ScanIt); |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 317 | /// getDependency - Return the instruction on which a memory operation |
| 318 | /// depends. |
| 319 | MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) { |
| 320 | Instruction *ScanPos = QueryInst; |
| 321 | |
| 322 | // Check for a cached result |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 323 | MemDepResult &LocalCache = LocalDeps[QueryInst]; |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 324 | |
Chris Lattner | 0ec48dd | 2008-11-29 22:02:15 +0000 | [diff] [blame] | 325 | // If the cached entry is non-dirty, just return it. Note that this depends |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 326 | // on MemDepResult's default constructing to 'dirty'. |
| 327 | if (!LocalCache.isDirty()) |
| 328 | return LocalCache; |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 329 | |
| 330 | // Otherwise, if we have a dirty entry, we know we can start the scan at that |
| 331 | // instruction, which may save us some work. |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 332 | if (Instruction *Inst = LocalCache.getInst()) { |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 333 | ScanPos = Inst; |
Chris Lattner | 4a69bad | 2008-11-30 02:52:26 +0000 | [diff] [blame] | 334 | |
Chris Lattner | d44745d | 2008-12-07 18:39:13 +0000 | [diff] [blame] | 335 | RemoveFromReverseMap(ReverseLocalDeps, Inst, QueryInst); |
Chris Lattner | 4a69bad | 2008-11-30 02:52:26 +0000 | [diff] [blame] | 336 | } |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 337 | |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 338 | BasicBlock *QueryParent = QueryInst->getParent(); |
| 339 | |
| 340 | Value *MemPtr = 0; |
| 341 | uint64_t MemSize = 0; |
| 342 | |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 343 | // Do the scan. |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 344 | if (BasicBlock::iterator(QueryInst) == QueryParent->begin()) { |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 345 | // No dependence found. If this is the entry block of the function, it is a |
| 346 | // clobber, otherwise it is non-local. |
| 347 | if (QueryParent != &QueryParent->getParent()->getEntryBlock()) |
| 348 | LocalCache = MemDepResult::getNonLocal(); |
| 349 | else |
| 350 | LocalCache = MemDepResult::getClobber(QueryInst); |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 351 | } else if (StoreInst *SI = dyn_cast<StoreInst>(QueryInst)) { |
| 352 | // If this is a volatile store, don't mess around with it. Just return the |
| 353 | // previous instruction as a clobber. |
| 354 | if (SI->isVolatile()) |
| 355 | LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos)); |
| 356 | else { |
| 357 | MemPtr = SI->getPointerOperand(); |
Dan Gohman | f581213 | 2009-07-31 20:53:12 +0000 | [diff] [blame] | 358 | MemSize = AA->getTypeStoreSize(SI->getOperand(0)->getType()); |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 359 | } |
| 360 | } else if (LoadInst *LI = dyn_cast<LoadInst>(QueryInst)) { |
| 361 | // If this is a volatile load, don't mess around with it. Just return the |
| 362 | // previous instruction as a clobber. |
| 363 | if (LI->isVolatile()) |
| 364 | LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos)); |
| 365 | else { |
| 366 | MemPtr = LI->getPointerOperand(); |
Dan Gohman | f581213 | 2009-07-31 20:53:12 +0000 | [diff] [blame] | 367 | MemSize = AA->getTypeStoreSize(LI->getType()); |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 368 | } |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 369 | } else if (isFreeCall(QueryInst)) { |
Victor Hernandez | 046e78c | 2009-10-26 23:43:48 +0000 | [diff] [blame] | 370 | MemPtr = QueryInst->getOperand(1); |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 371 | // calls to free() erase the entire structure, not just a field. |
| 372 | MemSize = ~0UL; |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 373 | } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst)) { |
Nick Lewycky | d801c10 | 2009-11-28 21:27:49 +0000 | [diff] [blame] | 374 | int IntrinsicID = 0; // Intrinsic IDs start at 1. |
| 375 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(QueryInst)) |
| 376 | IntrinsicID = II->getIntrinsicID(); |
| 377 | |
| 378 | switch (IntrinsicID) { |
| 379 | case Intrinsic::lifetime_start: |
| 380 | case Intrinsic::lifetime_end: |
| 381 | case Intrinsic::invariant_start: |
| 382 | MemPtr = QueryInst->getOperand(2); |
| 383 | MemSize = cast<ConstantInt>(QueryInst->getOperand(1))->getZExtValue(); |
| 384 | break; |
| 385 | case Intrinsic::invariant_end: |
| 386 | MemPtr = QueryInst->getOperand(3); |
| 387 | MemSize = cast<ConstantInt>(QueryInst->getOperand(2))->getZExtValue(); |
| 388 | break; |
| 389 | default: |
| 390 | CallSite QueryCS = CallSite::get(QueryInst); |
| 391 | bool isReadOnly = AA->onlyReadsMemory(QueryCS); |
| 392 | LocalCache = getCallSiteDependencyFrom(QueryCS, isReadOnly, ScanPos, |
| 393 | QueryParent); |
| 394 | } |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 395 | } else { |
| 396 | // Non-memory instruction. |
| 397 | LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos)); |
| 398 | } |
| 399 | |
| 400 | // If we need to do a pointer scan, make it happen. |
Nick Lewycky | d801c10 | 2009-11-28 21:27:49 +0000 | [diff] [blame] | 401 | if (MemPtr) { |
| 402 | bool isLoad = !QueryInst->mayWriteToMemory(); |
| 403 | if (IntrinsicInst *II = dyn_cast<MemoryUseIntrinsic>(QueryInst)) { |
| 404 | isLoad |= II->getIntrinsicID() == Intrinsic::lifetime_end; |
| 405 | } |
| 406 | LocalCache = getPointerDependencyFrom(MemPtr, MemSize, isLoad, ScanPos, |
| 407 | QueryParent); |
| 408 | } |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 409 | |
| 410 | // Remember the result! |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 411 | if (Instruction *I = LocalCache.getInst()) |
Chris Lattner | 8c46527 | 2008-11-29 09:20:15 +0000 | [diff] [blame] | 412 | ReverseLocalDeps[I].insert(QueryInst); |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 413 | |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 414 | return LocalCache; |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Chris Lattner | 12a7db3 | 2009-01-22 07:04:01 +0000 | [diff] [blame] | 417 | #ifndef NDEBUG |
| 418 | /// AssertSorted - This method is used when -debug is specified to verify that |
| 419 | /// cache arrays are properly kept sorted. |
| 420 | static void AssertSorted(MemoryDependenceAnalysis::NonLocalDepInfo &Cache, |
| 421 | int Count = -1) { |
| 422 | if (Count == -1) Count = Cache.size(); |
| 423 | if (Count == 0) return; |
| 424 | |
| 425 | for (unsigned i = 1; i != unsigned(Count); ++i) |
| 426 | assert(Cache[i-1] <= Cache[i] && "Cache isn't sorted!"); |
| 427 | } |
| 428 | #endif |
| 429 | |
Chris Lattner | 1559b36 | 2008-12-09 19:38:05 +0000 | [diff] [blame] | 430 | /// getNonLocalCallDependency - Perform a full dependency query for the |
| 431 | /// specified call, returning the set of blocks that the value is |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 432 | /// potentially live across. The returned set of results will include a |
| 433 | /// "NonLocal" result for all blocks where the value is live across. |
| 434 | /// |
Chris Lattner | 1559b36 | 2008-12-09 19:38:05 +0000 | [diff] [blame] | 435 | /// This method assumes the instruction returns a "NonLocal" dependency |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 436 | /// within its own block. |
| 437 | /// |
Chris Lattner | 1559b36 | 2008-12-09 19:38:05 +0000 | [diff] [blame] | 438 | /// This returns a reference to an internal data structure that may be |
| 439 | /// invalidated on the next non-local query or when an instruction is |
| 440 | /// removed. Clients must copy this data if they want it around longer than |
| 441 | /// that. |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 442 | const MemoryDependenceAnalysis::NonLocalDepInfo & |
Chris Lattner | 1559b36 | 2008-12-09 19:38:05 +0000 | [diff] [blame] | 443 | MemoryDependenceAnalysis::getNonLocalCallDependency(CallSite QueryCS) { |
| 444 | assert(getDependency(QueryCS.getInstruction()).isNonLocal() && |
| 445 | "getNonLocalCallDependency should only be used on calls with non-local deps!"); |
| 446 | PerInstNLInfo &CacheP = NonLocalDeps[QueryCS.getInstruction()]; |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 447 | NonLocalDepInfo &Cache = CacheP.first; |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 448 | |
| 449 | /// DirtyBlocks - This is the set of blocks that need to be recomputed. In |
| 450 | /// the cached case, this can happen due to instructions being deleted etc. In |
| 451 | /// the uncached case, this starts out as the set of predecessors we care |
| 452 | /// about. |
| 453 | SmallVector<BasicBlock*, 32> DirtyBlocks; |
| 454 | |
| 455 | if (!Cache.empty()) { |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 456 | // Okay, we have a cache entry. If we know it is not dirty, just return it |
| 457 | // with no computation. |
| 458 | if (!CacheP.second) { |
| 459 | NumCacheNonLocal++; |
| 460 | return Cache; |
| 461 | } |
| 462 | |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 463 | // If we already have a partially computed set of results, scan them to |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 464 | // determine what is dirty, seeding our initial DirtyBlocks worklist. |
| 465 | for (NonLocalDepInfo::iterator I = Cache.begin(), E = Cache.end(); |
| 466 | I != E; ++I) |
| 467 | if (I->second.isDirty()) |
| 468 | DirtyBlocks.push_back(I->first); |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 469 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 470 | // Sort the cache so that we can do fast binary search lookups below. |
| 471 | std::sort(Cache.begin(), Cache.end()); |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 472 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 473 | ++NumCacheDirtyNonLocal; |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 474 | //cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: " |
| 475 | // << Cache.size() << " cached: " << *QueryInst; |
| 476 | } else { |
| 477 | // Seed DirtyBlocks with each of the preds of QueryInst's block. |
Chris Lattner | 1559b36 | 2008-12-09 19:38:05 +0000 | [diff] [blame] | 478 | BasicBlock *QueryBB = QueryCS.getInstruction()->getParent(); |
Chris Lattner | 511b36c | 2008-12-09 06:44:17 +0000 | [diff] [blame] | 479 | for (BasicBlock **PI = PredCache->GetPreds(QueryBB); *PI; ++PI) |
| 480 | DirtyBlocks.push_back(*PI); |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 481 | NumUncacheNonLocal++; |
| 482 | } |
| 483 | |
Chris Lattner | 20d6f09 | 2008-12-09 21:19:42 +0000 | [diff] [blame] | 484 | // isReadonlyCall - If this is a read-only call, we can be more aggressive. |
| 485 | bool isReadonlyCall = AA->onlyReadsMemory(QueryCS); |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 486 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 487 | SmallPtrSet<BasicBlock*, 64> Visited; |
| 488 | |
| 489 | unsigned NumSortedEntries = Cache.size(); |
Chris Lattner | 12a7db3 | 2009-01-22 07:04:01 +0000 | [diff] [blame] | 490 | DEBUG(AssertSorted(Cache)); |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 491 | |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 492 | // Iterate while we still have blocks to update. |
| 493 | while (!DirtyBlocks.empty()) { |
| 494 | BasicBlock *DirtyBB = DirtyBlocks.back(); |
| 495 | DirtyBlocks.pop_back(); |
| 496 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 497 | // Already processed this block? |
| 498 | if (!Visited.insert(DirtyBB)) |
| 499 | continue; |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 500 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 501 | // Do a binary search to see if we already have an entry for this block in |
| 502 | // the cache set. If so, find it. |
Chris Lattner | 12a7db3 | 2009-01-22 07:04:01 +0000 | [diff] [blame] | 503 | DEBUG(AssertSorted(Cache, NumSortedEntries)); |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 504 | NonLocalDepInfo::iterator Entry = |
| 505 | std::upper_bound(Cache.begin(), Cache.begin()+NumSortedEntries, |
| 506 | std::make_pair(DirtyBB, MemDepResult())); |
Duncan Sands | 7050f3d | 2008-12-10 09:38:36 +0000 | [diff] [blame] | 507 | if (Entry != Cache.begin() && prior(Entry)->first == DirtyBB) |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 508 | --Entry; |
| 509 | |
| 510 | MemDepResult *ExistingResult = 0; |
| 511 | if (Entry != Cache.begin()+NumSortedEntries && |
| 512 | Entry->first == DirtyBB) { |
| 513 | // If we already have an entry, and if it isn't already dirty, the block |
| 514 | // is done. |
| 515 | if (!Entry->second.isDirty()) |
| 516 | continue; |
| 517 | |
| 518 | // Otherwise, remember this slot so we can update the value. |
| 519 | ExistingResult = &Entry->second; |
| 520 | } |
| 521 | |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 522 | // If the dirty entry has a pointer, start scanning from it so we don't have |
| 523 | // to rescan the entire block. |
| 524 | BasicBlock::iterator ScanPos = DirtyBB->end(); |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 525 | if (ExistingResult) { |
| 526 | if (Instruction *Inst = ExistingResult->getInst()) { |
| 527 | ScanPos = Inst; |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 528 | // We're removing QueryInst's use of Inst. |
Chris Lattner | 1559b36 | 2008-12-09 19:38:05 +0000 | [diff] [blame] | 529 | RemoveFromReverseMap(ReverseNonLocalDeps, Inst, |
| 530 | QueryCS.getInstruction()); |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 531 | } |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 532 | } |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 533 | |
Chris Lattner | 73ec3cd | 2008-11-30 01:26:32 +0000 | [diff] [blame] | 534 | // Find out if this block has a local dependency for QueryInst. |
Chris Lattner | d8dd934 | 2008-12-07 01:21:14 +0000 | [diff] [blame] | 535 | MemDepResult Dep; |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 536 | |
Chris Lattner | 1559b36 | 2008-12-09 19:38:05 +0000 | [diff] [blame] | 537 | if (ScanPos != DirtyBB->begin()) { |
Chris Lattner | 20d6f09 | 2008-12-09 21:19:42 +0000 | [diff] [blame] | 538 | Dep = getCallSiteDependencyFrom(QueryCS, isReadonlyCall,ScanPos, DirtyBB); |
Chris Lattner | 1559b36 | 2008-12-09 19:38:05 +0000 | [diff] [blame] | 539 | } else if (DirtyBB != &DirtyBB->getParent()->getEntryBlock()) { |
| 540 | // No dependence found. If this is the entry block of the function, it is |
| 541 | // a clobber, otherwise it is non-local. |
| 542 | Dep = MemDepResult::getNonLocal(); |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 543 | } else { |
Chris Lattner | 1559b36 | 2008-12-09 19:38:05 +0000 | [diff] [blame] | 544 | Dep = MemDepResult::getClobber(ScanPos); |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 547 | // If we had a dirty entry for the block, update it. Otherwise, just add |
| 548 | // a new entry. |
| 549 | if (ExistingResult) |
| 550 | *ExistingResult = Dep; |
| 551 | else |
| 552 | Cache.push_back(std::make_pair(DirtyBB, Dep)); |
| 553 | |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 554 | // If the block has a dependency (i.e. it isn't completely transparent to |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 555 | // the value), remember the association! |
| 556 | if (!Dep.isNonLocal()) { |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 557 | // Keep the ReverseNonLocalDeps map up to date so we can efficiently |
| 558 | // update this when we remove instructions. |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 559 | if (Instruction *Inst = Dep.getInst()) |
Chris Lattner | 1559b36 | 2008-12-09 19:38:05 +0000 | [diff] [blame] | 560 | ReverseNonLocalDeps[Inst].insert(QueryCS.getInstruction()); |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 561 | } else { |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 562 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 563 | // If the block *is* completely transparent to the load, we need to check |
| 564 | // the predecessors of this block. Add them to our worklist. |
Chris Lattner | 511b36c | 2008-12-09 06:44:17 +0000 | [diff] [blame] | 565 | for (BasicBlock **PI = PredCache->GetPreds(DirtyBB); *PI; ++PI) |
| 566 | DirtyBlocks.push_back(*PI); |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 567 | } |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 570 | return Cache; |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 573 | /// getNonLocalPointerDependency - Perform a full dependency query for an |
| 574 | /// access to the specified (non-volatile) memory location, returning the |
| 575 | /// set of instructions that either define or clobber the value. |
| 576 | /// |
| 577 | /// This method assumes the pointer has a "NonLocal" dependency within its |
| 578 | /// own block. |
| 579 | /// |
| 580 | void MemoryDependenceAnalysis:: |
| 581 | getNonLocalPointerDependency(Value *Pointer, bool isLoad, BasicBlock *FromBB, |
| 582 | SmallVectorImpl<NonLocalDepEntry> &Result) { |
Chris Lattner | 3f7eb5b | 2008-12-07 18:45:15 +0000 | [diff] [blame] | 583 | assert(isa<PointerType>(Pointer->getType()) && |
| 584 | "Can't get pointer deps of a non-pointer!"); |
Chris Lattner | 9a193fd | 2008-12-07 02:56:57 +0000 | [diff] [blame] | 585 | Result.clear(); |
| 586 | |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 587 | // We know that the pointer value is live into FromBB find the def/clobbers |
| 588 | // from presecessors. |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 589 | const Type *EltTy = cast<PointerType>(Pointer->getType())->getElementType(); |
Dan Gohman | f581213 | 2009-07-31 20:53:12 +0000 | [diff] [blame] | 590 | uint64_t PointeeSize = AA->getTypeStoreSize(EltTy); |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 591 | |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 592 | // This is the set of blocks we've inspected, and the pointer we consider in |
| 593 | // each block. Because of critical edges, we currently bail out if querying |
| 594 | // a block with multiple different pointers. This can happen during PHI |
| 595 | // translation. |
| 596 | DenseMap<BasicBlock*, Value*> Visited; |
| 597 | if (!getNonLocalPointerDepFromBB(Pointer, PointeeSize, isLoad, FromBB, |
| 598 | Result, Visited, true)) |
| 599 | return; |
Chris Lattner | 3af23f8 | 2008-12-15 04:58:29 +0000 | [diff] [blame] | 600 | Result.clear(); |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 601 | Result.push_back(std::make_pair(FromBB, |
| 602 | MemDepResult::getClobber(FromBB->begin()))); |
Chris Lattner | 9a193fd | 2008-12-07 02:56:57 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Chris Lattner | 9863c3f | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 605 | /// GetNonLocalInfoForBlock - Compute the memdep value for BB with |
| 606 | /// Pointer/PointeeSize using either cached information in Cache or by doing a |
| 607 | /// lookup (which may use dirty cache info if available). If we do a lookup, |
| 608 | /// add the result to the cache. |
| 609 | MemDepResult MemoryDependenceAnalysis:: |
| 610 | GetNonLocalInfoForBlock(Value *Pointer, uint64_t PointeeSize, |
| 611 | bool isLoad, BasicBlock *BB, |
| 612 | NonLocalDepInfo *Cache, unsigned NumSortedEntries) { |
| 613 | |
| 614 | // Do a binary search to see if we already have an entry for this block in |
| 615 | // the cache set. If so, find it. |
| 616 | NonLocalDepInfo::iterator Entry = |
| 617 | std::upper_bound(Cache->begin(), Cache->begin()+NumSortedEntries, |
| 618 | std::make_pair(BB, MemDepResult())); |
Duncan Sands | 7050f3d | 2008-12-10 09:38:36 +0000 | [diff] [blame] | 619 | if (Entry != Cache->begin() && prior(Entry)->first == BB) |
Chris Lattner | 9863c3f | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 620 | --Entry; |
| 621 | |
| 622 | MemDepResult *ExistingResult = 0; |
| 623 | if (Entry != Cache->begin()+NumSortedEntries && Entry->first == BB) |
| 624 | ExistingResult = &Entry->second; |
| 625 | |
| 626 | // If we have a cached entry, and it is non-dirty, use it as the value for |
| 627 | // this dependency. |
| 628 | if (ExistingResult && !ExistingResult->isDirty()) { |
| 629 | ++NumCacheNonLocalPtr; |
| 630 | return *ExistingResult; |
| 631 | } |
| 632 | |
| 633 | // Otherwise, we have to scan for the value. If we have a dirty cache |
| 634 | // entry, start scanning from its position, otherwise we scan from the end |
| 635 | // of the block. |
| 636 | BasicBlock::iterator ScanPos = BB->end(); |
| 637 | if (ExistingResult && ExistingResult->getInst()) { |
| 638 | assert(ExistingResult->getInst()->getParent() == BB && |
| 639 | "Instruction invalidated?"); |
| 640 | ++NumCacheDirtyNonLocalPtr; |
| 641 | ScanPos = ExistingResult->getInst(); |
| 642 | |
| 643 | // Eliminating the dirty entry from 'Cache', so update the reverse info. |
| 644 | ValueIsLoadPair CacheKey(Pointer, isLoad); |
Chris Lattner | 6a0dcc1 | 2009-03-29 00:24:04 +0000 | [diff] [blame] | 645 | RemoveFromReverseMap(ReverseNonLocalPtrDeps, ScanPos, CacheKey); |
Chris Lattner | 9863c3f | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 646 | } else { |
| 647 | ++NumUncacheNonLocalPtr; |
| 648 | } |
| 649 | |
| 650 | // Scan the block for the dependency. |
| 651 | MemDepResult Dep = getPointerDependencyFrom(Pointer, PointeeSize, isLoad, |
| 652 | ScanPos, BB); |
| 653 | |
| 654 | // If we had a dirty entry for the block, update it. Otherwise, just add |
| 655 | // a new entry. |
| 656 | if (ExistingResult) |
| 657 | *ExistingResult = Dep; |
| 658 | else |
| 659 | Cache->push_back(std::make_pair(BB, Dep)); |
| 660 | |
| 661 | // If the block has a dependency (i.e. it isn't completely transparent to |
| 662 | // the value), remember the reverse association because we just added it |
| 663 | // to Cache! |
| 664 | if (Dep.isNonLocal()) |
| 665 | return Dep; |
| 666 | |
| 667 | // Keep the ReverseNonLocalPtrDeps map up to date so we can efficiently |
| 668 | // update MemDep when we remove instructions. |
| 669 | Instruction *Inst = Dep.getInst(); |
| 670 | assert(Inst && "Didn't depend on anything?"); |
| 671 | ValueIsLoadPair CacheKey(Pointer, isLoad); |
Chris Lattner | 6a0dcc1 | 2009-03-29 00:24:04 +0000 | [diff] [blame] | 672 | ReverseNonLocalPtrDeps[Inst].insert(CacheKey); |
Chris Lattner | 9863c3f | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 673 | return Dep; |
| 674 | } |
| 675 | |
Chris Lattner | a2f55dd | 2009-07-13 17:20:05 +0000 | [diff] [blame] | 676 | /// SortNonLocalDepInfoCache - Sort the a NonLocalDepInfo cache, given a certain |
| 677 | /// number of elements in the array that are already properly ordered. This is |
| 678 | /// optimized for the case when only a few entries are added. |
| 679 | static void |
| 680 | SortNonLocalDepInfoCache(MemoryDependenceAnalysis::NonLocalDepInfo &Cache, |
| 681 | unsigned NumSortedEntries) { |
| 682 | switch (Cache.size() - NumSortedEntries) { |
| 683 | case 0: |
| 684 | // done, no new entries. |
| 685 | break; |
| 686 | case 2: { |
| 687 | // Two new entries, insert the last one into place. |
| 688 | MemoryDependenceAnalysis::NonLocalDepEntry Val = Cache.back(); |
| 689 | Cache.pop_back(); |
| 690 | MemoryDependenceAnalysis::NonLocalDepInfo::iterator Entry = |
| 691 | std::upper_bound(Cache.begin(), Cache.end()-1, Val); |
| 692 | Cache.insert(Entry, Val); |
| 693 | // FALL THROUGH. |
| 694 | } |
| 695 | case 1: |
| 696 | // One new entry, Just insert the new value at the appropriate position. |
| 697 | if (Cache.size() != 1) { |
| 698 | MemoryDependenceAnalysis::NonLocalDepEntry Val = Cache.back(); |
| 699 | Cache.pop_back(); |
| 700 | MemoryDependenceAnalysis::NonLocalDepInfo::iterator Entry = |
| 701 | std::upper_bound(Cache.begin(), Cache.end(), Val); |
| 702 | Cache.insert(Entry, Val); |
| 703 | } |
| 704 | break; |
| 705 | default: |
| 706 | // Added many values, do a full scale sort. |
| 707 | std::sort(Cache.begin(), Cache.end()); |
| 708 | break; |
| 709 | } |
| 710 | } |
| 711 | |
Chris Lattner | dc59311 | 2009-11-26 23:18:49 +0000 | [diff] [blame] | 712 | /// isPHITranslatable - Return true if the specified computation is derived from |
| 713 | /// a PHI node in the current block and if it is simple enough for us to handle. |
| 714 | static bool isPHITranslatable(Instruction *Inst) { |
| 715 | if (isa<PHINode>(Inst)) |
| 716 | return true; |
| 717 | |
Chris Lattner | cc3d0eb | 2009-11-26 23:41:07 +0000 | [diff] [blame] | 718 | // We can handle bitcast of a PHI, but the PHI needs to be in the same block |
| 719 | // as the bitcast. |
Chris Lattner | 5141421 | 2009-11-27 20:25:30 +0000 | [diff] [blame] | 720 | if (BitCastInst *BC = dyn_cast<BitCastInst>(Inst)) { |
| 721 | Instruction *OpI = dyn_cast<Instruction>(BC->getOperand(0)); |
| 722 | if (OpI == 0 || OpI->getParent() != Inst->getParent()) |
| 723 | return true; |
| 724 | return isPHITranslatable(OpI); |
| 725 | } |
Chris Lattner | dc59311 | 2009-11-26 23:18:49 +0000 | [diff] [blame] | 726 | |
Chris Lattner | 11c6bab | 2009-11-27 19:11:31 +0000 | [diff] [blame] | 727 | // We can translate a GEP if all of its operands defined in this block are phi |
| 728 | // translatable. |
Chris Lattner | 3040762 | 2009-11-27 00:07:37 +0000 | [diff] [blame] | 729 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) { |
Chris Lattner | 11c6bab | 2009-11-27 19:11:31 +0000 | [diff] [blame] | 730 | for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) { |
Chris Lattner | 5141421 | 2009-11-27 20:25:30 +0000 | [diff] [blame] | 731 | Instruction *OpI = dyn_cast<Instruction>(GEP->getOperand(i)); |
| 732 | if (OpI == 0 || OpI->getParent() != Inst->getParent()) |
Chris Lattner | 11c6bab | 2009-11-27 19:11:31 +0000 | [diff] [blame] | 733 | continue; |
| 734 | |
Chris Lattner | 5141421 | 2009-11-27 20:25:30 +0000 | [diff] [blame] | 735 | if (!isPHITranslatable(OpI)) |
Chris Lattner | 11c6bab | 2009-11-27 19:11:31 +0000 | [diff] [blame] | 736 | return false; |
| 737 | } |
| 738 | return true; |
| 739 | } |
| 740 | |
| 741 | if (Inst->getOpcode() == Instruction::Add && |
| 742 | isa<ConstantInt>(Inst->getOperand(1))) { |
Chris Lattner | 5141421 | 2009-11-27 20:25:30 +0000 | [diff] [blame] | 743 | Instruction *OpI = dyn_cast<Instruction>(Inst->getOperand(0)); |
| 744 | if (OpI == 0 || OpI->getParent() != Inst->getParent()) |
Chris Lattner | 11c6bab | 2009-11-27 19:11:31 +0000 | [diff] [blame] | 745 | return true; |
Chris Lattner | 5141421 | 2009-11-27 20:25:30 +0000 | [diff] [blame] | 746 | return isPHITranslatable(OpI); |
Chris Lattner | 3040762 | 2009-11-27 00:07:37 +0000 | [diff] [blame] | 747 | } |
Chris Lattner | cc3d0eb | 2009-11-26 23:41:07 +0000 | [diff] [blame] | 748 | |
Chris Lattner | dc59311 | 2009-11-26 23:18:49 +0000 | [diff] [blame] | 749 | // cerr << "MEMDEP: Could not PHI translate: " << *Pointer; |
| 750 | // if (isa<BitCastInst>(PtrInst) || isa<GetElementPtrInst>(PtrInst)) |
| 751 | // cerr << "OP:\t\t\t\t" << *PtrInst->getOperand(0); |
| 752 | |
| 753 | return false; |
| 754 | } |
| 755 | |
Chris Lattner | 6f7b210 | 2009-11-27 22:05:15 +0000 | [diff] [blame] | 756 | /// GetPHITranslatedValue - Given a computation that satisfied the |
Chris Lattner | dc59311 | 2009-11-26 23:18:49 +0000 | [diff] [blame] | 757 | /// isPHITranslatable predicate, see if we can translate the computation into |
| 758 | /// the specified predecessor block. If so, return that value. |
Chris Lattner | 62deff0 | 2009-11-27 06:31:14 +0000 | [diff] [blame] | 759 | Value *MemoryDependenceAnalysis:: |
Chris Lattner | 6f7b210 | 2009-11-27 22:05:15 +0000 | [diff] [blame] | 760 | GetPHITranslatedValue(Value *InVal, BasicBlock *CurBB, BasicBlock *Pred, |
| 761 | const TargetData *TD) const { |
Chris Lattner | 62deff0 | 2009-11-27 06:31:14 +0000 | [diff] [blame] | 762 | // If the input value is not an instruction, or if it is not defined in CurBB, |
| 763 | // then we don't need to phi translate it. |
| 764 | Instruction *Inst = dyn_cast<Instruction>(InVal); |
| 765 | if (Inst == 0 || Inst->getParent() != CurBB) |
| 766 | return InVal; |
| 767 | |
Chris Lattner | dc59311 | 2009-11-26 23:18:49 +0000 | [diff] [blame] | 768 | if (PHINode *PN = dyn_cast<PHINode>(Inst)) |
| 769 | return PN->getIncomingValueForBlock(Pred); |
| 770 | |
Chris Lattner | 3040762 | 2009-11-27 00:07:37 +0000 | [diff] [blame] | 771 | // Handle bitcast of PHI. |
Chris Lattner | cc3d0eb | 2009-11-26 23:41:07 +0000 | [diff] [blame] | 772 | if (BitCastInst *BC = dyn_cast<BitCastInst>(Inst)) { |
Chris Lattner | 5141421 | 2009-11-27 20:25:30 +0000 | [diff] [blame] | 773 | // PHI translate the input operand. |
Chris Lattner | 6f7b210 | 2009-11-27 22:05:15 +0000 | [diff] [blame] | 774 | Value *PHIIn = GetPHITranslatedValue(BC->getOperand(0), CurBB, Pred, TD); |
Chris Lattner | 5141421 | 2009-11-27 20:25:30 +0000 | [diff] [blame] | 775 | if (PHIIn == 0) return 0; |
Chris Lattner | cc3d0eb | 2009-11-26 23:41:07 +0000 | [diff] [blame] | 776 | |
| 777 | // Constants are trivial to phi translate. |
| 778 | if (Constant *C = dyn_cast<Constant>(PHIIn)) |
| 779 | return ConstantExpr::getBitCast(C, BC->getType()); |
| 780 | |
| 781 | // Otherwise we have to see if a bitcasted version of the incoming pointer |
| 782 | // is available. If so, we can use it, otherwise we have to fail. |
| 783 | for (Value::use_iterator UI = PHIIn->use_begin(), E = PHIIn->use_end(); |
| 784 | UI != E; ++UI) { |
| 785 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(*UI)) |
| 786 | if (BCI->getType() == BC->getType()) |
| 787 | return BCI; |
| 788 | } |
| 789 | return 0; |
| 790 | } |
| 791 | |
Chris Lattner | 11c6bab | 2009-11-27 19:11:31 +0000 | [diff] [blame] | 792 | // Handle getelementptr with at least one PHI translatable operand. |
Chris Lattner | 3040762 | 2009-11-27 00:07:37 +0000 | [diff] [blame] | 793 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) { |
| 794 | SmallVector<Value*, 8> GEPOps; |
Chris Lattner | cca130b | 2009-11-27 05:19:56 +0000 | [diff] [blame] | 795 | BasicBlock *CurBB = GEP->getParent(); |
Chris Lattner | 3040762 | 2009-11-27 00:07:37 +0000 | [diff] [blame] | 796 | for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) { |
Chris Lattner | 616613d | 2009-11-27 08:25:10 +0000 | [diff] [blame] | 797 | Value *GEPOp = GEP->getOperand(i); |
| 798 | // No PHI translation is needed of operands whose values are live in to |
| 799 | // the predecessor block. |
| 800 | if (!isa<Instruction>(GEPOp) || |
| 801 | cast<Instruction>(GEPOp)->getParent() != CurBB) { |
| 802 | GEPOps.push_back(GEPOp); |
| 803 | continue; |
| 804 | } |
| 805 | |
| 806 | // If the operand is a phi node, do phi translation. |
Chris Lattner | 6f7b210 | 2009-11-27 22:05:15 +0000 | [diff] [blame] | 807 | Value *InOp = GetPHITranslatedValue(GEPOp, CurBB, Pred, TD); |
Chris Lattner | 5141421 | 2009-11-27 20:25:30 +0000 | [diff] [blame] | 808 | if (InOp == 0) return 0; |
Chris Lattner | 616613d | 2009-11-27 08:25:10 +0000 | [diff] [blame] | 809 | |
Chris Lattner | 5141421 | 2009-11-27 20:25:30 +0000 | [diff] [blame] | 810 | GEPOps.push_back(InOp); |
Chris Lattner | 3040762 | 2009-11-27 00:07:37 +0000 | [diff] [blame] | 811 | } |
| 812 | |
Chris Lattner | e19e4ba | 2009-11-27 00:34:38 +0000 | [diff] [blame] | 813 | // Simplify the GEP to handle 'gep x, 0' -> x etc. |
| 814 | if (Value *V = SimplifyGEPInst(&GEPOps[0], GEPOps.size(), TD)) |
| 815 | return V; |
Chris Lattner | 616613d | 2009-11-27 08:25:10 +0000 | [diff] [blame] | 816 | |
Chris Lattner | 3040762 | 2009-11-27 00:07:37 +0000 | [diff] [blame] | 817 | // Scan to see if we have this GEP available. |
Chris Lattner | 616613d | 2009-11-27 08:25:10 +0000 | [diff] [blame] | 818 | Value *APHIOp = GEPOps[0]; |
Chris Lattner | 3040762 | 2009-11-27 00:07:37 +0000 | [diff] [blame] | 819 | for (Value::use_iterator UI = APHIOp->use_begin(), E = APHIOp->use_end(); |
| 820 | UI != E; ++UI) { |
| 821 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(*UI)) |
Chris Lattner | cca130b | 2009-11-27 05:19:56 +0000 | [diff] [blame] | 822 | if (GEPI->getType() == GEP->getType() && |
Chris Lattner | 3040762 | 2009-11-27 00:07:37 +0000 | [diff] [blame] | 823 | GEPI->getNumOperands() == GEPOps.size() && |
Chris Lattner | cca130b | 2009-11-27 05:19:56 +0000 | [diff] [blame] | 824 | GEPI->getParent()->getParent() == CurBB->getParent()) { |
Chris Lattner | 3040762 | 2009-11-27 00:07:37 +0000 | [diff] [blame] | 825 | bool Mismatch = false; |
| 826 | for (unsigned i = 0, e = GEPOps.size(); i != e; ++i) |
| 827 | if (GEPI->getOperand(i) != GEPOps[i]) { |
| 828 | Mismatch = true; |
| 829 | break; |
| 830 | } |
| 831 | if (!Mismatch) |
| 832 | return GEPI; |
| 833 | } |
| 834 | } |
| 835 | return 0; |
| 836 | } |
| 837 | |
Chris Lattner | 11c6bab | 2009-11-27 19:11:31 +0000 | [diff] [blame] | 838 | // Handle add with a constant RHS. |
| 839 | if (Inst->getOpcode() == Instruction::Add && |
| 840 | isa<ConstantInt>(Inst->getOperand(1))) { |
| 841 | // PHI translate the LHS. |
| 842 | Value *LHS; |
| 843 | Constant *RHS = cast<ConstantInt>(Inst->getOperand(1)); |
| 844 | Instruction *OpI = dyn_cast<Instruction>(Inst->getOperand(0)); |
| 845 | bool isNSW = cast<BinaryOperator>(Inst)->hasNoSignedWrap(); |
| 846 | bool isNUW = cast<BinaryOperator>(Inst)->hasNoUnsignedWrap(); |
| 847 | |
| 848 | if (OpI == 0 || OpI->getParent() != Inst->getParent()) |
| 849 | LHS = Inst->getOperand(0); |
| 850 | else { |
Chris Lattner | 6f7b210 | 2009-11-27 22:05:15 +0000 | [diff] [blame] | 851 | LHS = GetPHITranslatedValue(Inst->getOperand(0), CurBB, Pred, TD); |
Chris Lattner | 11c6bab | 2009-11-27 19:11:31 +0000 | [diff] [blame] | 852 | if (LHS == 0) |
| 853 | return 0; |
| 854 | } |
| 855 | |
| 856 | // If the PHI translated LHS is an add of a constant, fold the immediates. |
| 857 | if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(LHS)) |
| 858 | if (BOp->getOpcode() == Instruction::Add) |
| 859 | if (ConstantInt *CI = dyn_cast<ConstantInt>(BOp->getOperand(1))) { |
| 860 | LHS = BOp->getOperand(0); |
| 861 | RHS = ConstantExpr::getAdd(RHS, CI); |
| 862 | isNSW = isNUW = false; |
| 863 | } |
| 864 | |
| 865 | // See if the add simplifies away. |
| 866 | if (Value *Res = SimplifyAddInst(LHS, RHS, isNSW, isNUW, TD)) |
| 867 | return Res; |
| 868 | |
| 869 | // Otherwise, see if we have this add available somewhere. |
| 870 | for (Value::use_iterator UI = LHS->use_begin(), E = LHS->use_end(); |
| 871 | UI != E; ++UI) { |
| 872 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(*UI)) |
| 873 | if (BO->getOperand(0) == LHS && BO->getOperand(1) == RHS && |
| 874 | BO->getParent()->getParent() == CurBB->getParent()) |
| 875 | return BO; |
| 876 | } |
| 877 | |
| 878 | return 0; |
| 879 | } |
| 880 | |
Chris Lattner | dc59311 | 2009-11-26 23:18:49 +0000 | [diff] [blame] | 881 | return 0; |
| 882 | } |
| 883 | |
Chris Lattner | 6f7b210 | 2009-11-27 22:05:15 +0000 | [diff] [blame] | 884 | /// GetAvailablePHITranslatePointer - Return the value computed by |
| 885 | /// PHITranslatePointer if it dominates PredBB, otherwise return null. |
| 886 | Value *MemoryDependenceAnalysis:: |
| 887 | GetAvailablePHITranslatedValue(Value *V, |
| 888 | BasicBlock *CurBB, BasicBlock *PredBB, |
| 889 | const TargetData *TD, |
| 890 | const DominatorTree &DT) const { |
| 891 | // See if PHI translation succeeds. |
| 892 | V = GetPHITranslatedValue(V, CurBB, PredBB, TD); |
| 893 | if (V == 0) return 0; |
| 894 | |
| 895 | // Make sure the value is live in the predecessor. |
| 896 | if (Instruction *Inst = dyn_cast_or_null<Instruction>(V)) |
| 897 | if (!DT.dominates(Inst->getParent(), PredBB)) |
| 898 | return 0; |
| 899 | return V; |
| 900 | } |
| 901 | |
| 902 | |
Chris Lattner | 616613d | 2009-11-27 08:25:10 +0000 | [diff] [blame] | 903 | /// InsertPHITranslatedPointer - Insert a computation of the PHI translated |
| 904 | /// version of 'V' for the edge PredBB->CurBB into the end of the PredBB |
Chris Lattner | dd69605 | 2009-11-28 15:39:14 +0000 | [diff] [blame] | 905 | /// block. All newly created instructions are added to the NewInsts list. |
Chris Lattner | 616613d | 2009-11-27 08:25:10 +0000 | [diff] [blame] | 906 | /// |
Chris Lattner | 616613d | 2009-11-27 08:25:10 +0000 | [diff] [blame] | 907 | Value *MemoryDependenceAnalysis:: |
| 908 | InsertPHITranslatedPointer(Value *InVal, BasicBlock *CurBB, |
Chris Lattner | 6f7b210 | 2009-11-27 22:05:15 +0000 | [diff] [blame] | 909 | BasicBlock *PredBB, const TargetData *TD, |
Chris Lattner | dd69605 | 2009-11-28 15:39:14 +0000 | [diff] [blame] | 910 | const DominatorTree &DT, |
| 911 | SmallVectorImpl<Instruction*> &NewInsts) const { |
Chris Lattner | 6f7b210 | 2009-11-27 22:05:15 +0000 | [diff] [blame] | 912 | // See if we have a version of this value already available and dominating |
| 913 | // PredBB. If so, there is no need to insert a new copy. |
| 914 | if (Value *Res = GetAvailablePHITranslatedValue(InVal, CurBB, PredBB, TD, DT)) |
| 915 | return Res; |
| 916 | |
| 917 | // If we don't have an available version of this value, it must be an |
| 918 | // instruction. |
Chris Lattner | 616613d | 2009-11-27 08:25:10 +0000 | [diff] [blame] | 919 | Instruction *Inst = cast<Instruction>(InVal); |
Chris Lattner | 6f7b210 | 2009-11-27 22:05:15 +0000 | [diff] [blame] | 920 | |
| 921 | // Handle bitcast of PHI translatable value. |
Chris Lattner | 616613d | 2009-11-27 08:25:10 +0000 | [diff] [blame] | 922 | if (BitCastInst *BC = dyn_cast<BitCastInst>(Inst)) { |
Chris Lattner | 6f7b210 | 2009-11-27 22:05:15 +0000 | [diff] [blame] | 923 | Value *OpVal = InsertPHITranslatedPointer(BC->getOperand(0), |
Chris Lattner | dd69605 | 2009-11-28 15:39:14 +0000 | [diff] [blame] | 924 | CurBB, PredBB, TD, DT, NewInsts); |
Chris Lattner | 6f7b210 | 2009-11-27 22:05:15 +0000 | [diff] [blame] | 925 | if (OpVal == 0) return 0; |
| 926 | |
Chris Lattner | 616613d | 2009-11-27 08:25:10 +0000 | [diff] [blame] | 927 | // Otherwise insert a bitcast at the end of PredBB. |
Chris Lattner | dd69605 | 2009-11-28 15:39:14 +0000 | [diff] [blame] | 928 | BitCastInst *New = new BitCastInst(OpVal, InVal->getType(), |
| 929 | InVal->getName()+".phi.trans.insert", |
| 930 | PredBB->getTerminator()); |
| 931 | NewInsts.push_back(New); |
| 932 | return New; |
Chris Lattner | 616613d | 2009-11-27 08:25:10 +0000 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | // Handle getelementptr with at least one PHI operand. |
| 936 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) { |
| 937 | SmallVector<Value*, 8> GEPOps; |
Chris Lattner | 616613d | 2009-11-27 08:25:10 +0000 | [diff] [blame] | 938 | BasicBlock *CurBB = GEP->getParent(); |
| 939 | for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) { |
Chris Lattner | 6f7b210 | 2009-11-27 22:05:15 +0000 | [diff] [blame] | 940 | Value *OpVal = InsertPHITranslatedPointer(GEP->getOperand(i), |
Chris Lattner | dd69605 | 2009-11-28 15:39:14 +0000 | [diff] [blame] | 941 | CurBB, PredBB, TD, DT, NewInsts); |
Chris Lattner | 6f7b210 | 2009-11-27 22:05:15 +0000 | [diff] [blame] | 942 | if (OpVal == 0) return 0; |
| 943 | GEPOps.push_back(OpVal); |
Chris Lattner | 616613d | 2009-11-27 08:25:10 +0000 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | GetElementPtrInst *Result = |
| 947 | GetElementPtrInst::Create(GEPOps[0], GEPOps.begin()+1, GEPOps.end(), |
| 948 | InVal->getName()+".phi.trans.insert", |
| 949 | PredBB->getTerminator()); |
| 950 | Result->setIsInBounds(GEP->isInBounds()); |
Chris Lattner | dd69605 | 2009-11-28 15:39:14 +0000 | [diff] [blame] | 951 | NewInsts.push_back(Result); |
Chris Lattner | 616613d | 2009-11-27 08:25:10 +0000 | [diff] [blame] | 952 | return Result; |
| 953 | } |
| 954 | |
Chris Lattner | 6f7b210 | 2009-11-27 22:05:15 +0000 | [diff] [blame] | 955 | #if 0 |
| 956 | // FIXME: This code works, but it is unclear that we actually want to insert |
| 957 | // a big chain of computation in order to make a value available in a block. |
| 958 | // This needs to be evaluated carefully to consider its cost trade offs. |
| 959 | |
| 960 | // Handle add with a constant RHS. |
| 961 | if (Inst->getOpcode() == Instruction::Add && |
| 962 | isa<ConstantInt>(Inst->getOperand(1))) { |
| 963 | // PHI translate the LHS. |
| 964 | Value *OpVal = InsertPHITranslatedPointer(Inst->getOperand(0), |
Chris Lattner | dd69605 | 2009-11-28 15:39:14 +0000 | [diff] [blame] | 965 | CurBB, PredBB, TD, DT, NewInsts); |
Chris Lattner | 6f7b210 | 2009-11-27 22:05:15 +0000 | [diff] [blame] | 966 | if (OpVal == 0) return 0; |
| 967 | |
| 968 | BinaryOperator *Res = BinaryOperator::CreateAdd(OpVal, Inst->getOperand(1), |
| 969 | InVal->getName()+".phi.trans.insert", |
| 970 | PredBB->getTerminator()); |
| 971 | Res->setHasNoSignedWrap(cast<BinaryOperator>(Inst)->hasNoSignedWrap()); |
| 972 | Res->setHasNoUnsignedWrap(cast<BinaryOperator>(Inst)->hasNoUnsignedWrap()); |
Chris Lattner | dd69605 | 2009-11-28 15:39:14 +0000 | [diff] [blame] | 973 | NewInsts.push_back(Res); |
Chris Lattner | 6f7b210 | 2009-11-27 22:05:15 +0000 | [diff] [blame] | 974 | return Res; |
| 975 | } |
| 976 | #endif |
| 977 | |
Chris Lattner | 616613d | 2009-11-27 08:25:10 +0000 | [diff] [blame] | 978 | return 0; |
| 979 | } |
Chris Lattner | 9863c3f | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 980 | |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 981 | /// getNonLocalPointerDepFromBB - Perform a dependency query based on |
| 982 | /// pointer/pointeesize starting at the end of StartBB. Add any clobber/def |
| 983 | /// results to the results vector and keep track of which blocks are visited in |
| 984 | /// 'Visited'. |
| 985 | /// |
| 986 | /// This has special behavior for the first block queries (when SkipFirstBlock |
| 987 | /// is true). In this special case, it ignores the contents of the specified |
| 988 | /// block and starts returning dependence info for its predecessors. |
| 989 | /// |
| 990 | /// This function returns false on success, or true to indicate that it could |
| 991 | /// not compute dependence information for some reason. This should be treated |
| 992 | /// as a clobber dependence on the first instruction in the predecessor block. |
| 993 | bool MemoryDependenceAnalysis:: |
Chris Lattner | 9863c3f | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 994 | getNonLocalPointerDepFromBB(Value *Pointer, uint64_t PointeeSize, |
| 995 | bool isLoad, BasicBlock *StartBB, |
| 996 | SmallVectorImpl<NonLocalDepEntry> &Result, |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 997 | DenseMap<BasicBlock*, Value*> &Visited, |
| 998 | bool SkipFirstBlock) { |
Chris Lattner | 6636434 | 2009-09-20 22:44:26 +0000 | [diff] [blame] | 999 | |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1000 | // Look up the cached info for Pointer. |
| 1001 | ValueIsLoadPair CacheKey(Pointer, isLoad); |
Chris Lattner | 11dcd8d | 2008-12-08 07:31:50 +0000 | [diff] [blame] | 1002 | |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1003 | std::pair<BBSkipFirstBlockPair, NonLocalDepInfo> *CacheInfo = |
| 1004 | &NonLocalPointerDeps[CacheKey]; |
| 1005 | NonLocalDepInfo *Cache = &CacheInfo->second; |
Chris Lattner | 11dcd8d | 2008-12-08 07:31:50 +0000 | [diff] [blame] | 1006 | |
| 1007 | // If we have valid cached information for exactly the block we are |
| 1008 | // investigating, just return it with no recomputation. |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1009 | if (CacheInfo->first == BBSkipFirstBlockPair(StartBB, SkipFirstBlock)) { |
Chris Lattner | f478951 | 2008-12-16 07:10:09 +0000 | [diff] [blame] | 1010 | // We have a fully cached result for this query then we can just return the |
| 1011 | // cached results and populate the visited set. However, we have to verify |
| 1012 | // that we don't already have conflicting results for these blocks. Check |
| 1013 | // to ensure that if a block in the results set is in the visited set that |
| 1014 | // it was for the same pointer query. |
| 1015 | if (!Visited.empty()) { |
| 1016 | for (NonLocalDepInfo::iterator I = Cache->begin(), E = Cache->end(); |
| 1017 | I != E; ++I) { |
| 1018 | DenseMap<BasicBlock*, Value*>::iterator VI = Visited.find(I->first); |
| 1019 | if (VI == Visited.end() || VI->second == Pointer) continue; |
| 1020 | |
| 1021 | // We have a pointer mismatch in a block. Just return clobber, saying |
| 1022 | // that something was clobbered in this result. We could also do a |
| 1023 | // non-fully cached query, but there is little point in doing this. |
| 1024 | return true; |
| 1025 | } |
| 1026 | } |
| 1027 | |
Chris Lattner | 11dcd8d | 2008-12-08 07:31:50 +0000 | [diff] [blame] | 1028 | for (NonLocalDepInfo::iterator I = Cache->begin(), E = Cache->end(); |
Chris Lattner | f478951 | 2008-12-16 07:10:09 +0000 | [diff] [blame] | 1029 | I != E; ++I) { |
| 1030 | Visited.insert(std::make_pair(I->first, Pointer)); |
Chris Lattner | 11dcd8d | 2008-12-08 07:31:50 +0000 | [diff] [blame] | 1031 | if (!I->second.isNonLocal()) |
| 1032 | Result.push_back(*I); |
Chris Lattner | f478951 | 2008-12-16 07:10:09 +0000 | [diff] [blame] | 1033 | } |
Chris Lattner | 11dcd8d | 2008-12-08 07:31:50 +0000 | [diff] [blame] | 1034 | ++NumCacheCompleteNonLocalPtr; |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1035 | return false; |
Chris Lattner | 11dcd8d | 2008-12-08 07:31:50 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | // Otherwise, either this is a new block, a block with an invalid cache |
| 1039 | // pointer or one that we're about to invalidate by putting more info into it |
| 1040 | // than its valid cache info. If empty, the result will be valid cache info, |
| 1041 | // otherwise it isn't. |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1042 | if (Cache->empty()) |
| 1043 | CacheInfo->first = BBSkipFirstBlockPair(StartBB, SkipFirstBlock); |
| 1044 | else |
| 1045 | CacheInfo->first = BBSkipFirstBlockPair(); |
Chris Lattner | 11dcd8d | 2008-12-08 07:31:50 +0000 | [diff] [blame] | 1046 | |
| 1047 | SmallVector<BasicBlock*, 32> Worklist; |
| 1048 | Worklist.push_back(StartBB); |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1049 | |
| 1050 | // Keep track of the entries that we know are sorted. Previously cached |
| 1051 | // entries will all be sorted. The entries we add we only sort on demand (we |
| 1052 | // don't insert every element into its sorted position). We know that we |
| 1053 | // won't get any reuse from currently inserted values, because we don't |
| 1054 | // revisit blocks after we insert info for them. |
| 1055 | unsigned NumSortedEntries = Cache->size(); |
Chris Lattner | 12a7db3 | 2009-01-22 07:04:01 +0000 | [diff] [blame] | 1056 | DEBUG(AssertSorted(*Cache)); |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1057 | |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 1058 | while (!Worklist.empty()) { |
Chris Lattner | 9a193fd | 2008-12-07 02:56:57 +0000 | [diff] [blame] | 1059 | BasicBlock *BB = Worklist.pop_back_val(); |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 1060 | |
Chris Lattner | 6563371 | 2008-12-09 07:52:59 +0000 | [diff] [blame] | 1061 | // Skip the first block if we have it. |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1062 | if (!SkipFirstBlock) { |
Chris Lattner | 6563371 | 2008-12-09 07:52:59 +0000 | [diff] [blame] | 1063 | // Analyze the dependency of *Pointer in FromBB. See if we already have |
| 1064 | // been here. |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1065 | assert(Visited.count(BB) && "Should check 'visited' before adding to WL"); |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1066 | |
Chris Lattner | 6563371 | 2008-12-09 07:52:59 +0000 | [diff] [blame] | 1067 | // Get the dependency info for Pointer in BB. If we have cached |
| 1068 | // information, we will use it, otherwise we compute it. |
Chris Lattner | 12a7db3 | 2009-01-22 07:04:01 +0000 | [diff] [blame] | 1069 | DEBUG(AssertSorted(*Cache, NumSortedEntries)); |
Chris Lattner | 6563371 | 2008-12-09 07:52:59 +0000 | [diff] [blame] | 1070 | MemDepResult Dep = GetNonLocalInfoForBlock(Pointer, PointeeSize, isLoad, |
| 1071 | BB, Cache, NumSortedEntries); |
| 1072 | |
| 1073 | // If we got a Def or Clobber, add this to the list of results. |
| 1074 | if (!Dep.isNonLocal()) { |
| 1075 | Result.push_back(NonLocalDepEntry(BB, Dep)); |
| 1076 | continue; |
| 1077 | } |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1080 | // If 'Pointer' is an instruction defined in this block, then we need to do |
| 1081 | // phi translation to change it into a value live in the predecessor block. |
| 1082 | // If phi translation fails, then we can't continue dependence analysis. |
| 1083 | Instruction *PtrInst = dyn_cast<Instruction>(Pointer); |
| 1084 | bool NeedsPHITranslation = PtrInst && PtrInst->getParent() == BB; |
| 1085 | |
| 1086 | // If no PHI translation is needed, just add all the predecessors of this |
| 1087 | // block to scan them as well. |
| 1088 | if (!NeedsPHITranslation) { |
| 1089 | SkipFirstBlock = false; |
| 1090 | for (BasicBlock **PI = PredCache->GetPreds(BB); *PI; ++PI) { |
| 1091 | // Verify that we haven't looked at this block yet. |
| 1092 | std::pair<DenseMap<BasicBlock*,Value*>::iterator, bool> |
| 1093 | InsertRes = Visited.insert(std::make_pair(*PI, Pointer)); |
| 1094 | if (InsertRes.second) { |
| 1095 | // First time we've looked at *PI. |
| 1096 | Worklist.push_back(*PI); |
| 1097 | continue; |
| 1098 | } |
| 1099 | |
| 1100 | // If we have seen this block before, but it was with a different |
| 1101 | // pointer then we have a phi translation failure and we have to treat |
| 1102 | // this as a clobber. |
| 1103 | if (InsertRes.first->second != Pointer) |
| 1104 | goto PredTranslationFailure; |
| 1105 | } |
| 1106 | continue; |
| 1107 | } |
| 1108 | |
| 1109 | // If we do need to do phi translation, then there are a bunch of different |
| 1110 | // cases, because we have to find a Value* live in the predecessor block. We |
| 1111 | // know that PtrInst is defined in this block at least. |
Chris Lattner | 6fbc196 | 2009-07-13 17:14:23 +0000 | [diff] [blame] | 1112 | |
| 1113 | // We may have added values to the cache list before this PHI translation. |
| 1114 | // If so, we haven't done anything to ensure that the cache remains sorted. |
| 1115 | // Sort it now (if needed) so that recursive invocations of |
| 1116 | // getNonLocalPointerDepFromBB and other routines that could reuse the cache |
| 1117 | // value will only see properly sorted cache arrays. |
| 1118 | if (Cache && NumSortedEntries != Cache->size()) { |
Chris Lattner | a2f55dd | 2009-07-13 17:20:05 +0000 | [diff] [blame] | 1119 | SortNonLocalDepInfoCache(*Cache, NumSortedEntries); |
Chris Lattner | 6fbc196 | 2009-07-13 17:14:23 +0000 | [diff] [blame] | 1120 | NumSortedEntries = Cache->size(); |
| 1121 | } |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1122 | |
Chris Lattner | dc59311 | 2009-11-26 23:18:49 +0000 | [diff] [blame] | 1123 | // If this is a computation derived from a PHI node, use the suitably |
| 1124 | // translated incoming values for each pred as the phi translated version. |
Chris Lattner | e95035a | 2009-11-27 08:37:22 +0000 | [diff] [blame] | 1125 | if (!isPHITranslatable(PtrInst)) |
| 1126 | goto PredTranslationFailure; |
| 1127 | |
| 1128 | Cache = 0; |
Chris Lattner | 6fbc196 | 2009-07-13 17:14:23 +0000 | [diff] [blame] | 1129 | |
Chris Lattner | e95035a | 2009-11-27 08:37:22 +0000 | [diff] [blame] | 1130 | for (BasicBlock **PI = PredCache->GetPreds(BB); *PI; ++PI) { |
| 1131 | BasicBlock *Pred = *PI; |
Chris Lattner | 6f7b210 | 2009-11-27 22:05:15 +0000 | [diff] [blame] | 1132 | // Get the PHI translated pointer in this predecessor. This can fail and |
| 1133 | // return null if not translatable. |
| 1134 | Value *PredPtr = GetPHITranslatedValue(PtrInst, BB, Pred, TD); |
Chris Lattner | e95035a | 2009-11-27 08:37:22 +0000 | [diff] [blame] | 1135 | |
| 1136 | // Check to see if we have already visited this pred block with another |
| 1137 | // pointer. If so, we can't do this lookup. This failure can occur |
| 1138 | // with PHI translation when a critical edge exists and the PHI node in |
| 1139 | // the successor translates to a pointer value different than the |
| 1140 | // pointer the block was first analyzed with. |
| 1141 | std::pair<DenseMap<BasicBlock*,Value*>::iterator, bool> |
| 1142 | InsertRes = Visited.insert(std::make_pair(Pred, PredPtr)); |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1143 | |
Chris Lattner | e95035a | 2009-11-27 08:37:22 +0000 | [diff] [blame] | 1144 | if (!InsertRes.second) { |
| 1145 | // If the predecessor was visited with PredPtr, then we already did |
| 1146 | // the analysis and can ignore it. |
| 1147 | if (InsertRes.first->second == PredPtr) |
| 1148 | continue; |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1149 | |
Chris Lattner | e95035a | 2009-11-27 08:37:22 +0000 | [diff] [blame] | 1150 | // Otherwise, the block was previously analyzed with a different |
| 1151 | // pointer. We can't represent the result of this case, so we just |
| 1152 | // treat this as a phi translation failure. |
| 1153 | goto PredTranslationFailure; |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1154 | } |
Chris Lattner | 6f7b210 | 2009-11-27 22:05:15 +0000 | [diff] [blame] | 1155 | |
| 1156 | // If PHI translation was unable to find an available pointer in this |
| 1157 | // predecessor, then we have to assume that the pointer is clobbered in |
| 1158 | // that predecessor. We can still do PRE of the load, which would insert |
| 1159 | // a computation of the pointer in this predecessor. |
| 1160 | if (PredPtr == 0) { |
Chris Lattner | 855d9da | 2009-12-01 07:33:32 +0000 | [diff] [blame] | 1161 | // Add the entry to the Result list. |
| 1162 | NonLocalDepEntry Entry(Pred, |
| 1163 | MemDepResult::getClobber(Pred->getTerminator())); |
| 1164 | Result.push_back(Entry); |
| 1165 | |
| 1166 | // Add it to the cache for this CacheKey so that subsequent queries get |
| 1167 | // this result. |
| 1168 | Cache = &NonLocalPointerDeps[CacheKey].second; |
| 1169 | MemoryDependenceAnalysis::NonLocalDepInfo::iterator It = |
| 1170 | std::upper_bound(Cache->begin(), Cache->end(), Entry); |
| 1171 | |
| 1172 | if (It != Cache->begin() && prior(It)->first == Pred) |
| 1173 | --It; |
| 1174 | |
| 1175 | if (It == Cache->end() || It->first != Pred) { |
| 1176 | Cache->insert(It, Entry); |
| 1177 | // Add it to the reverse map. |
| 1178 | ReverseNonLocalPtrDeps[Pred->getTerminator()].insert(CacheKey); |
| 1179 | } else if (!It->second.isDirty()) { |
| 1180 | // noop |
| 1181 | } else if (It->second.getInst() == Pred->getTerminator()) { |
| 1182 | // Same instruction, clear the dirty marker. |
| 1183 | It->second = Entry.second; |
| 1184 | } else if (It->second.getInst() == 0) { |
| 1185 | // Dirty, with no instruction, just add this. |
| 1186 | It->second = Entry.second; |
| 1187 | ReverseNonLocalPtrDeps[Pred->getTerminator()].insert(CacheKey); |
| 1188 | } else { |
| 1189 | // Otherwise, dirty with a different instruction. |
| 1190 | RemoveFromReverseMap(ReverseNonLocalPtrDeps, It->second.getInst(), |
| 1191 | CacheKey); |
| 1192 | It->second = Entry.second; |
| 1193 | ReverseNonLocalPtrDeps[Pred->getTerminator()].insert(CacheKey); |
| 1194 | } |
| 1195 | Cache = 0; |
Chris Lattner | 6f7b210 | 2009-11-27 22:05:15 +0000 | [diff] [blame] | 1196 | continue; |
Chris Lattner | 6f7b210 | 2009-11-27 22:05:15 +0000 | [diff] [blame] | 1197 | } |
Chris Lattner | e95035a | 2009-11-27 08:37:22 +0000 | [diff] [blame] | 1198 | |
| 1199 | // FIXME: it is entirely possible that PHI translating will end up with |
| 1200 | // the same value. Consider PHI translating something like: |
| 1201 | // X = phi [x, bb1], [y, bb2]. PHI translating for bb1 doesn't *need* |
| 1202 | // to recurse here, pedantically speaking. |
Chris Lattner | 6fbc196 | 2009-07-13 17:14:23 +0000 | [diff] [blame] | 1203 | |
Chris Lattner | e95035a | 2009-11-27 08:37:22 +0000 | [diff] [blame] | 1204 | // If we have a problem phi translating, fall through to the code below |
| 1205 | // to handle the failure condition. |
| 1206 | if (getNonLocalPointerDepFromBB(PredPtr, PointeeSize, isLoad, Pred, |
| 1207 | Result, Visited)) |
| 1208 | goto PredTranslationFailure; |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1209 | } |
Chris Lattner | e95035a | 2009-11-27 08:37:22 +0000 | [diff] [blame] | 1210 | |
| 1211 | // Refresh the CacheInfo/Cache pointer so that it isn't invalidated. |
| 1212 | CacheInfo = &NonLocalPointerDeps[CacheKey]; |
| 1213 | Cache = &CacheInfo->second; |
| 1214 | NumSortedEntries = Cache->size(); |
| 1215 | |
| 1216 | // Since we did phi translation, the "Cache" set won't contain all of the |
| 1217 | // results for the query. This is ok (we can still use it to accelerate |
| 1218 | // specific block queries) but we can't do the fastpath "return all |
| 1219 | // results from the set" Clear out the indicator for this. |
| 1220 | CacheInfo->first = BBSkipFirstBlockPair(); |
| 1221 | SkipFirstBlock = false; |
| 1222 | continue; |
Chris Lattner | dc59311 | 2009-11-26 23:18:49 +0000 | [diff] [blame] | 1223 | |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1224 | PredTranslationFailure: |
| 1225 | |
Chris Lattner | 95900f2 | 2009-01-23 07:12:16 +0000 | [diff] [blame] | 1226 | if (Cache == 0) { |
| 1227 | // Refresh the CacheInfo/Cache pointer if it got invalidated. |
| 1228 | CacheInfo = &NonLocalPointerDeps[CacheKey]; |
| 1229 | Cache = &CacheInfo->second; |
| 1230 | NumSortedEntries = Cache->size(); |
Chris Lattner | 95900f2 | 2009-01-23 07:12:16 +0000 | [diff] [blame] | 1231 | } |
Chris Lattner | 6fbc196 | 2009-07-13 17:14:23 +0000 | [diff] [blame] | 1232 | |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1233 | // Since we did phi translation, the "Cache" set won't contain all of the |
| 1234 | // results for the query. This is ok (we can still use it to accelerate |
| 1235 | // specific block queries) but we can't do the fastpath "return all |
| 1236 | // results from the set" Clear out the indicator for this. |
| 1237 | CacheInfo->first = BBSkipFirstBlockPair(); |
| 1238 | |
| 1239 | // If *nothing* works, mark the pointer as being clobbered by the first |
| 1240 | // instruction in this block. |
| 1241 | // |
| 1242 | // If this is the magic first block, return this as a clobber of the whole |
| 1243 | // incoming value. Since we can't phi translate to one of the predecessors, |
| 1244 | // we have to bail out. |
| 1245 | if (SkipFirstBlock) |
| 1246 | return true; |
| 1247 | |
| 1248 | for (NonLocalDepInfo::reverse_iterator I = Cache->rbegin(); ; ++I) { |
| 1249 | assert(I != Cache->rend() && "Didn't find current block??"); |
| 1250 | if (I->first != BB) |
| 1251 | continue; |
| 1252 | |
| 1253 | assert(I->second.isNonLocal() && |
| 1254 | "Should only be here with transparent block"); |
| 1255 | I->second = MemDepResult::getClobber(BB->begin()); |
Chris Lattner | 6a0dcc1 | 2009-03-29 00:24:04 +0000 | [diff] [blame] | 1256 | ReverseNonLocalPtrDeps[BB->begin()].insert(CacheKey); |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1257 | Result.push_back(*I); |
| 1258 | break; |
Chris Lattner | 9a193fd | 2008-12-07 02:56:57 +0000 | [diff] [blame] | 1259 | } |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 1260 | } |
Chris Lattner | 95900f2 | 2009-01-23 07:12:16 +0000 | [diff] [blame] | 1261 | |
Chris Lattner | 9863c3f | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 1262 | // Okay, we're done now. If we added new values to the cache, re-sort it. |
Chris Lattner | a2f55dd | 2009-07-13 17:20:05 +0000 | [diff] [blame] | 1263 | SortNonLocalDepInfoCache(*Cache, NumSortedEntries); |
Chris Lattner | 12a7db3 | 2009-01-22 07:04:01 +0000 | [diff] [blame] | 1264 | DEBUG(AssertSorted(*Cache)); |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1265 | return false; |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | /// RemoveCachedNonLocalPointerDependencies - If P exists in |
| 1269 | /// CachedNonLocalPointerInfo, remove it. |
| 1270 | void MemoryDependenceAnalysis:: |
| 1271 | RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P) { |
| 1272 | CachedNonLocalPointerInfo::iterator It = |
| 1273 | NonLocalPointerDeps.find(P); |
| 1274 | if (It == NonLocalPointerDeps.end()) return; |
| 1275 | |
| 1276 | // Remove all of the entries in the BB->val map. This involves removing |
| 1277 | // instructions from the reverse map. |
Chris Lattner | 11dcd8d | 2008-12-08 07:31:50 +0000 | [diff] [blame] | 1278 | NonLocalDepInfo &PInfo = It->second.second; |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1279 | |
| 1280 | for (unsigned i = 0, e = PInfo.size(); i != e; ++i) { |
| 1281 | Instruction *Target = PInfo[i].second.getInst(); |
| 1282 | if (Target == 0) continue; // Ignore non-local dep results. |
Chris Lattner | 5a45bf1 | 2008-12-09 22:45:32 +0000 | [diff] [blame] | 1283 | assert(Target->getParent() == PInfo[i].first); |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1284 | |
| 1285 | // Eliminating the dirty entry from 'Cache', so update the reverse info. |
Chris Lattner | 6a0dcc1 | 2009-03-29 00:24:04 +0000 | [diff] [blame] | 1286 | RemoveFromReverseMap(ReverseNonLocalPtrDeps, Target, P); |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | // Remove P from NonLocalPointerDeps (which deletes NonLocalDepInfo). |
| 1290 | NonLocalPointerDeps.erase(It); |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | |
Chris Lattner | bc99be1 | 2008-12-09 22:06:23 +0000 | [diff] [blame] | 1294 | /// invalidateCachedPointerInfo - This method is used to invalidate cached |
| 1295 | /// information about the specified pointer, because it may be too |
| 1296 | /// conservative in memdep. This is an optional call that can be used when |
| 1297 | /// the client detects an equivalence between the pointer and some other |
| 1298 | /// value and replaces the other value with ptr. This can make Ptr available |
| 1299 | /// in more places that cached info does not necessarily keep. |
| 1300 | void MemoryDependenceAnalysis::invalidateCachedPointerInfo(Value *Ptr) { |
| 1301 | // If Ptr isn't really a pointer, just ignore it. |
| 1302 | if (!isa<PointerType>(Ptr->getType())) return; |
| 1303 | // Flush store info for the pointer. |
| 1304 | RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, false)); |
| 1305 | // Flush load info for the pointer. |
| 1306 | RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, true)); |
| 1307 | } |
| 1308 | |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 1309 | /// removeInstruction - Remove an instruction from the dependence analysis, |
| 1310 | /// updating the dependence of instructions that previously depended on it. |
Owen Anderson | 642a9e3 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 1311 | /// This method attempts to keep the cache coherent using the reverse map. |
Chris Lattner | 5f589dc | 2008-11-28 22:04:47 +0000 | [diff] [blame] | 1312 | void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) { |
Chris Lattner | 5f589dc | 2008-11-28 22:04:47 +0000 | [diff] [blame] | 1313 | // Walk through the Non-local dependencies, removing this one as the value |
| 1314 | // for any cached queries. |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1315 | NonLocalDepMapType::iterator NLDI = NonLocalDeps.find(RemInst); |
| 1316 | if (NLDI != NonLocalDeps.end()) { |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 1317 | NonLocalDepInfo &BlockMap = NLDI->second.first; |
Chris Lattner | 25f4b2b | 2008-11-30 02:30:50 +0000 | [diff] [blame] | 1318 | for (NonLocalDepInfo::iterator DI = BlockMap.begin(), DE = BlockMap.end(); |
| 1319 | DI != DE; ++DI) |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 1320 | if (Instruction *Inst = DI->second.getInst()) |
Chris Lattner | d44745d | 2008-12-07 18:39:13 +0000 | [diff] [blame] | 1321 | RemoveFromReverseMap(ReverseNonLocalDeps, Inst, RemInst); |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1322 | NonLocalDeps.erase(NLDI); |
| 1323 | } |
Owen Anderson | 5fc4aba | 2007-12-08 01:37:09 +0000 | [diff] [blame] | 1324 | |
Chris Lattner | 5f589dc | 2008-11-28 22:04:47 +0000 | [diff] [blame] | 1325 | // 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] | 1326 | // |
Chris Lattner | 39f372e | 2008-11-29 01:43:36 +0000 | [diff] [blame] | 1327 | LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst); |
| 1328 | if (LocalDepEntry != LocalDeps.end()) { |
Chris Lattner | 125ce36 | 2008-11-30 01:09:30 +0000 | [diff] [blame] | 1329 | // Remove us from DepInst's reverse set now that the local dep info is gone. |
Chris Lattner | d44745d | 2008-12-07 18:39:13 +0000 | [diff] [blame] | 1330 | if (Instruction *Inst = LocalDepEntry->second.getInst()) |
| 1331 | RemoveFromReverseMap(ReverseLocalDeps, Inst, RemInst); |
Chris Lattner | 125ce36 | 2008-11-30 01:09:30 +0000 | [diff] [blame] | 1332 | |
Chris Lattner | baad888 | 2008-11-28 22:28:27 +0000 | [diff] [blame] | 1333 | // Remove this local dependency info. |
Chris Lattner | 39f372e | 2008-11-29 01:43:36 +0000 | [diff] [blame] | 1334 | LocalDeps.erase(LocalDepEntry); |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | // If we have any cached pointer dependencies on this instruction, remove |
| 1338 | // them. If the instruction has non-pointer type, then it can't be a pointer |
| 1339 | // base. |
| 1340 | |
| 1341 | // Remove it from both the load info and the store info. The instruction |
| 1342 | // can't be in either of these maps if it is non-pointer. |
| 1343 | if (isa<PointerType>(RemInst->getType())) { |
| 1344 | RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, false)); |
| 1345 | RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, true)); |
| 1346 | } |
Chris Lattner | baad888 | 2008-11-28 22:28:27 +0000 | [diff] [blame] | 1347 | |
Chris Lattner | d3d12ec | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 1348 | // Loop over all of the things that depend on the instruction we're removing. |
| 1349 | // |
Chris Lattner | 4f8c18c | 2008-11-29 23:30:39 +0000 | [diff] [blame] | 1350 | SmallVector<std::pair<Instruction*, Instruction*>, 8> ReverseDepsToAdd; |
Chris Lattner | 0655f73 | 2008-12-07 18:42:51 +0000 | [diff] [blame] | 1351 | |
| 1352 | // If we find RemInst as a clobber or Def in any of the maps for other values, |
| 1353 | // we need to replace its entry with a dirty version of the instruction after |
| 1354 | // it. If RemInst is a terminator, we use a null dirty value. |
| 1355 | // |
| 1356 | // Using a dirty version of the instruction after RemInst saves having to scan |
| 1357 | // the entire block to get to this point. |
| 1358 | MemDepResult NewDirtyVal; |
| 1359 | if (!RemInst->isTerminator()) |
| 1360 | NewDirtyVal = MemDepResult::getDirty(++BasicBlock::iterator(RemInst)); |
Chris Lattner | 4f8c18c | 2008-11-29 23:30:39 +0000 | [diff] [blame] | 1361 | |
Chris Lattner | 8c46527 | 2008-11-29 09:20:15 +0000 | [diff] [blame] | 1362 | ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst); |
| 1363 | if (ReverseDepIt != ReverseLocalDeps.end()) { |
Chris Lattner | d3d12ec | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 1364 | SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second; |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1365 | // RemInst can't be the terminator if it has local stuff depending on it. |
Chris Lattner | 125ce36 | 2008-11-30 01:09:30 +0000 | [diff] [blame] | 1366 | assert(!ReverseDeps.empty() && !isa<TerminatorInst>(RemInst) && |
| 1367 | "Nothing can locally depend on a terminator"); |
| 1368 | |
Chris Lattner | d3d12ec | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 1369 | for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(), |
| 1370 | E = ReverseDeps.end(); I != E; ++I) { |
| 1371 | Instruction *InstDependingOnRemInst = *I; |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1372 | assert(InstDependingOnRemInst != RemInst && |
| 1373 | "Already removed our local dep info"); |
Chris Lattner | 125ce36 | 2008-11-30 01:09:30 +0000 | [diff] [blame] | 1374 | |
Chris Lattner | 0655f73 | 2008-12-07 18:42:51 +0000 | [diff] [blame] | 1375 | LocalDeps[InstDependingOnRemInst] = NewDirtyVal; |
Chris Lattner | d3d12ec | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 1376 | |
Chris Lattner | 125ce36 | 2008-11-30 01:09:30 +0000 | [diff] [blame] | 1377 | // Make sure to remember that new things depend on NewDepInst. |
Chris Lattner | 0655f73 | 2008-12-07 18:42:51 +0000 | [diff] [blame] | 1378 | assert(NewDirtyVal.getInst() && "There is no way something else can have " |
| 1379 | "a local dep on this if it is a terminator!"); |
| 1380 | ReverseDepsToAdd.push_back(std::make_pair(NewDirtyVal.getInst(), |
Chris Lattner | 125ce36 | 2008-11-30 01:09:30 +0000 | [diff] [blame] | 1381 | InstDependingOnRemInst)); |
Chris Lattner | d3d12ec | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 1382 | } |
Chris Lattner | 4f8c18c | 2008-11-29 23:30:39 +0000 | [diff] [blame] | 1383 | |
| 1384 | ReverseLocalDeps.erase(ReverseDepIt); |
| 1385 | |
| 1386 | // Add new reverse deps after scanning the set, to avoid invalidating the |
| 1387 | // 'ReverseDeps' reference. |
| 1388 | while (!ReverseDepsToAdd.empty()) { |
| 1389 | ReverseLocalDeps[ReverseDepsToAdd.back().first] |
| 1390 | .insert(ReverseDepsToAdd.back().second); |
| 1391 | ReverseDepsToAdd.pop_back(); |
| 1392 | } |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 1393 | } |
Owen Anderson | 4d13de4 | 2007-08-16 21:27:05 +0000 | [diff] [blame] | 1394 | |
Chris Lattner | 8c46527 | 2008-11-29 09:20:15 +0000 | [diff] [blame] | 1395 | ReverseDepIt = ReverseNonLocalDeps.find(RemInst); |
| 1396 | if (ReverseDepIt != ReverseNonLocalDeps.end()) { |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1397 | SmallPtrSet<Instruction*, 4> &Set = ReverseDepIt->second; |
| 1398 | for (SmallPtrSet<Instruction*, 4>::iterator I = Set.begin(), E = Set.end(); |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1399 | I != E; ++I) { |
| 1400 | assert(*I != RemInst && "Already removed NonLocalDep info for RemInst"); |
| 1401 | |
Chris Lattner | 4a69bad | 2008-11-30 02:52:26 +0000 | [diff] [blame] | 1402 | PerInstNLInfo &INLD = NonLocalDeps[*I]; |
Chris Lattner | 4a69bad | 2008-11-30 02:52:26 +0000 | [diff] [blame] | 1403 | // The information is now dirty! |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 1404 | INLD.second = true; |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1405 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 1406 | for (NonLocalDepInfo::iterator DI = INLD.first.begin(), |
| 1407 | DE = INLD.first.end(); DI != DE; ++DI) { |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 1408 | if (DI->second.getInst() != RemInst) continue; |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1409 | |
| 1410 | // Convert to a dirty entry for the subsequent instruction. |
Chris Lattner | 0655f73 | 2008-12-07 18:42:51 +0000 | [diff] [blame] | 1411 | DI->second = NewDirtyVal; |
| 1412 | |
| 1413 | if (Instruction *NextI = NewDirtyVal.getInst()) |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1414 | ReverseDepsToAdd.push_back(std::make_pair(NextI, *I)); |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1415 | } |
| 1416 | } |
Chris Lattner | 4f8c18c | 2008-11-29 23:30:39 +0000 | [diff] [blame] | 1417 | |
| 1418 | ReverseNonLocalDeps.erase(ReverseDepIt); |
| 1419 | |
Chris Lattner | 0ec48dd | 2008-11-29 22:02:15 +0000 | [diff] [blame] | 1420 | // Add new reverse deps after scanning the set, to avoid invalidating 'Set' |
| 1421 | while (!ReverseDepsToAdd.empty()) { |
| 1422 | ReverseNonLocalDeps[ReverseDepsToAdd.back().first] |
| 1423 | .insert(ReverseDepsToAdd.back().second); |
| 1424 | ReverseDepsToAdd.pop_back(); |
| 1425 | } |
Owen Anderson | 4d13de4 | 2007-08-16 21:27:05 +0000 | [diff] [blame] | 1426 | } |
Owen Anderson | 5fc4aba | 2007-12-08 01:37:09 +0000 | [diff] [blame] | 1427 | |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1428 | // If the instruction is in ReverseNonLocalPtrDeps then it appears as a |
| 1429 | // value in the NonLocalPointerDeps info. |
| 1430 | ReverseNonLocalPtrDepTy::iterator ReversePtrDepIt = |
| 1431 | ReverseNonLocalPtrDeps.find(RemInst); |
| 1432 | if (ReversePtrDepIt != ReverseNonLocalPtrDeps.end()) { |
Chris Lattner | 6a0dcc1 | 2009-03-29 00:24:04 +0000 | [diff] [blame] | 1433 | SmallPtrSet<ValueIsLoadPair, 4> &Set = ReversePtrDepIt->second; |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1434 | SmallVector<std::pair<Instruction*, ValueIsLoadPair>,8> ReversePtrDepsToAdd; |
| 1435 | |
Chris Lattner | 6a0dcc1 | 2009-03-29 00:24:04 +0000 | [diff] [blame] | 1436 | for (SmallPtrSet<ValueIsLoadPair, 4>::iterator I = Set.begin(), |
| 1437 | E = Set.end(); I != E; ++I) { |
| 1438 | ValueIsLoadPair P = *I; |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1439 | assert(P.getPointer() != RemInst && |
| 1440 | "Already removed NonLocalPointerDeps info for RemInst"); |
| 1441 | |
Chris Lattner | 11dcd8d | 2008-12-08 07:31:50 +0000 | [diff] [blame] | 1442 | NonLocalDepInfo &NLPDI = NonLocalPointerDeps[P].second; |
| 1443 | |
| 1444 | // The cache is not valid for any specific block anymore. |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1445 | NonLocalPointerDeps[P].first = BBSkipFirstBlockPair(); |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1446 | |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1447 | // Update any entries for RemInst to use the instruction after it. |
| 1448 | for (NonLocalDepInfo::iterator DI = NLPDI.begin(), DE = NLPDI.end(); |
| 1449 | DI != DE; ++DI) { |
| 1450 | if (DI->second.getInst() != RemInst) continue; |
| 1451 | |
| 1452 | // Convert to a dirty entry for the subsequent instruction. |
| 1453 | DI->second = NewDirtyVal; |
| 1454 | |
| 1455 | if (Instruction *NewDirtyInst = NewDirtyVal.getInst()) |
| 1456 | ReversePtrDepsToAdd.push_back(std::make_pair(NewDirtyInst, P)); |
| 1457 | } |
Chris Lattner | 95900f2 | 2009-01-23 07:12:16 +0000 | [diff] [blame] | 1458 | |
| 1459 | // Re-sort the NonLocalDepInfo. Changing the dirty entry to its |
| 1460 | // subsequent value may invalidate the sortedness. |
| 1461 | std::sort(NLPDI.begin(), NLPDI.end()); |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | ReverseNonLocalPtrDeps.erase(ReversePtrDepIt); |
| 1465 | |
| 1466 | while (!ReversePtrDepsToAdd.empty()) { |
| 1467 | ReverseNonLocalPtrDeps[ReversePtrDepsToAdd.back().first] |
Chris Lattner | 6a0dcc1 | 2009-03-29 00:24:04 +0000 | [diff] [blame] | 1468 | .insert(ReversePtrDepsToAdd.back().second); |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1469 | ReversePtrDepsToAdd.pop_back(); |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1474 | assert(!NonLocalDeps.count(RemInst) && "RemInst got reinserted?"); |
Chris Lattner | d777d40 | 2008-11-30 19:24:31 +0000 | [diff] [blame] | 1475 | AA->deleteValue(RemInst); |
Chris Lattner | 5f589dc | 2008-11-28 22:04:47 +0000 | [diff] [blame] | 1476 | DEBUG(verifyRemoved(RemInst)); |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 1477 | } |
Chris Lattner | 729b237 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 1478 | /// verifyRemoved - Verify that the specified instruction does not occur |
| 1479 | /// in our internal data structures. |
| 1480 | void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const { |
| 1481 | for (LocalDepMapType::const_iterator I = LocalDeps.begin(), |
| 1482 | E = LocalDeps.end(); I != E; ++I) { |
| 1483 | assert(I->first != D && "Inst occurs in data structures"); |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 1484 | assert(I->second.getInst() != D && |
Chris Lattner | 729b237 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 1485 | "Inst occurs in data structures"); |
| 1486 | } |
| 1487 | |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1488 | for (CachedNonLocalPointerInfo::const_iterator I =NonLocalPointerDeps.begin(), |
| 1489 | E = NonLocalPointerDeps.end(); I != E; ++I) { |
| 1490 | assert(I->first.getPointer() != D && "Inst occurs in NLPD map key"); |
Chris Lattner | 11dcd8d | 2008-12-08 07:31:50 +0000 | [diff] [blame] | 1491 | const NonLocalDepInfo &Val = I->second.second; |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1492 | for (NonLocalDepInfo::const_iterator II = Val.begin(), E = Val.end(); |
| 1493 | II != E; ++II) |
| 1494 | assert(II->second.getInst() != D && "Inst occurs as NLPD value"); |
| 1495 | } |
| 1496 | |
Chris Lattner | 729b237 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 1497 | for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(), |
| 1498 | E = NonLocalDeps.end(); I != E; ++I) { |
| 1499 | assert(I->first != D && "Inst occurs in data structures"); |
Chris Lattner | 4a69bad | 2008-11-30 02:52:26 +0000 | [diff] [blame] | 1500 | const PerInstNLInfo &INLD = I->second; |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 1501 | for (NonLocalDepInfo::const_iterator II = INLD.first.begin(), |
| 1502 | EE = INLD.first.end(); II != EE; ++II) |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 1503 | assert(II->second.getInst() != D && "Inst occurs in data structures"); |
Chris Lattner | 729b237 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(), |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1507 | E = ReverseLocalDeps.end(); I != E; ++I) { |
| 1508 | assert(I->first != D && "Inst occurs in data structures"); |
Chris Lattner | 729b237 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 1509 | for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(), |
| 1510 | EE = I->second.end(); II != EE; ++II) |
| 1511 | assert(*II != D && "Inst occurs in data structures"); |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1512 | } |
Chris Lattner | 729b237 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 1513 | |
| 1514 | for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(), |
| 1515 | E = ReverseNonLocalDeps.end(); |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1516 | I != E; ++I) { |
| 1517 | assert(I->first != D && "Inst occurs in data structures"); |
Chris Lattner | 729b237 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 1518 | for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(), |
| 1519 | EE = I->second.end(); II != EE; ++II) |
| 1520 | assert(*II != D && "Inst occurs in data structures"); |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1521 | } |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1522 | |
| 1523 | for (ReverseNonLocalPtrDepTy::const_iterator |
| 1524 | I = ReverseNonLocalPtrDeps.begin(), |
| 1525 | E = ReverseNonLocalPtrDeps.end(); I != E; ++I) { |
| 1526 | assert(I->first != D && "Inst occurs in rev NLPD map"); |
| 1527 | |
Chris Lattner | 6a0dcc1 | 2009-03-29 00:24:04 +0000 | [diff] [blame] | 1528 | for (SmallPtrSet<ValueIsLoadPair, 4>::const_iterator II = I->second.begin(), |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1529 | E = I->second.end(); II != E; ++II) |
Chris Lattner | 6a0dcc1 | 2009-03-29 00:24:04 +0000 | [diff] [blame] | 1530 | assert(*II != ValueIsLoadPair(D, false) && |
| 1531 | *II != ValueIsLoadPair(D, true) && |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1532 | "Inst occurs in ReverseNonLocalPtrDeps map"); |
| 1533 | } |
| 1534 | |
Chris Lattner | 729b237 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 1535 | } |