Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- MemoryDependenceAnalysis.cpp - Mem Deps Implementation --*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the Owen Anderson and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements an analysis that determines, for a given memory |
| 11 | // operation, what preceding memory operations it depends on. It builds on |
Owen Anderson | afe840e | 2007-08-08 22:01:54 +0000 | [diff] [blame] | 12 | // alias analysis information, and tries to provide a lazy, caching interface to |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 13 | // a common kind of alias information query. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "llvm/Analysis/MemoryDependenceAnalysis.h" |
| 18 | #include "llvm/Constants.h" |
| 19 | #include "llvm/Instructions.h" |
| 20 | #include "llvm/Function.h" |
| 21 | #include "llvm/Analysis/AliasAnalysis.h" |
Owen Anderson | 4c29547 | 2007-07-24 21:52:37 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CFG.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetData.h" |
Owen Anderson | d6c7fea | 2007-09-09 21:43:49 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/Statistic.h" |
| 25 | |
| 26 | #define DEBUG_TYPE "memdep" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace llvm; |
| 29 | |
Owen Anderson | d6c7fea | 2007-09-09 21:43:49 +0000 | [diff] [blame] | 30 | STATISTIC(NumCacheNonlocal, "Number of cached non-local responses"); |
| 31 | STATISTIC(NumUncacheNonlocal, "Number of uncached non-local responses"); |
| 32 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 33 | char MemoryDependenceAnalysis::ID = 0; |
| 34 | |
Owen Anderson | 935e39b | 2007-08-09 04:42:44 +0000 | [diff] [blame] | 35 | Instruction* const MemoryDependenceAnalysis::NonLocal = (Instruction*)-3; |
| 36 | Instruction* const MemoryDependenceAnalysis::None = (Instruction*)-4; |
Owen Anderson | 7e44756 | 2007-09-19 16:13:57 +0000 | [diff] [blame^] | 37 | Instruction* const MemoryDependenceAnalysis::Dirty = (Instruction*)-5; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 38 | |
| 39 | // Register this pass... |
| 40 | static RegisterPass<MemoryDependenceAnalysis> X("memdep", |
| 41 | "Memory Dependence Analysis"); |
| 42 | |
| 43 | /// getAnalysisUsage - Does not modify anything. It uses Alias Analysis. |
| 44 | /// |
| 45 | void MemoryDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
| 46 | AU.setPreservesAll(); |
| 47 | AU.addRequiredTransitive<AliasAnalysis>(); |
| 48 | AU.addRequiredTransitive<TargetData>(); |
| 49 | } |
| 50 | |
Owen Anderson | 3de3c53 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 51 | /// getCallSiteDependency - Private helper for finding the local dependencies |
| 52 | /// of a call site. |
Owen Anderson | 935e39b | 2007-08-09 04:42:44 +0000 | [diff] [blame] | 53 | Instruction* MemoryDependenceAnalysis::getCallSiteDependency(CallSite C, |
Owen Anderson | afe840e | 2007-08-08 22:01:54 +0000 | [diff] [blame] | 54 | Instruction* start, |
| 55 | BasicBlock* block) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 56 | |
| 57 | AliasAnalysis& AA = getAnalysis<AliasAnalysis>(); |
| 58 | TargetData& TD = getAnalysis<TargetData>(); |
| 59 | BasicBlock::iterator blockBegin = C.getInstruction()->getParent()->begin(); |
| 60 | BasicBlock::iterator QI = C.getInstruction(); |
| 61 | |
Owen Anderson | 3de3c53 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 62 | // If the starting point was specifiy, use it |
Owen Anderson | e84f4bc | 2007-08-07 00:33:45 +0000 | [diff] [blame] | 63 | if (start) { |
| 64 | QI = start; |
| 65 | blockBegin = start->getParent()->end(); |
Owen Anderson | 3de3c53 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 66 | // If the starting point wasn't specified, but the block was, use it |
Owen Anderson | e84f4bc | 2007-08-07 00:33:45 +0000 | [diff] [blame] | 67 | } else if (!start && block) { |
| 68 | QI = block->end(); |
| 69 | blockBegin = block->end(); |
| 70 | } |
| 71 | |
Owen Anderson | 3de3c53 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 72 | // Walk backwards through the block, looking for dependencies |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 73 | while (QI != blockBegin) { |
| 74 | --QI; |
| 75 | |
| 76 | // If this inst is a memory op, get the pointer it accessed |
| 77 | Value* pointer = 0; |
| 78 | uint64_t pointerSize = 0; |
| 79 | if (StoreInst* S = dyn_cast<StoreInst>(QI)) { |
| 80 | pointer = S->getPointerOperand(); |
| 81 | pointerSize = TD.getTypeSize(S->getOperand(0)->getType()); |
| 82 | } else if (LoadInst* L = dyn_cast<LoadInst>(QI)) { |
| 83 | pointer = L->getPointerOperand(); |
| 84 | pointerSize = TD.getTypeSize(L->getType()); |
| 85 | } else if (AllocationInst* AI = dyn_cast<AllocationInst>(QI)) { |
| 86 | pointer = AI; |
| 87 | if (ConstantInt* C = dyn_cast<ConstantInt>(AI->getArraySize())) |
Owen Anderson | afe840e | 2007-08-08 22:01:54 +0000 | [diff] [blame] | 88 | pointerSize = C->getZExtValue() * \ |
| 89 | TD.getTypeSize(AI->getAllocatedType()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 90 | else |
| 91 | pointerSize = ~0UL; |
| 92 | } else if (VAArgInst* V = dyn_cast<VAArgInst>(QI)) { |
| 93 | pointer = V->getOperand(0); |
| 94 | pointerSize = TD.getTypeSize(V->getType()); |
| 95 | } else if (FreeInst* F = dyn_cast<FreeInst>(QI)) { |
| 96 | pointer = F->getPointerOperand(); |
| 97 | |
| 98 | // FreeInsts erase the entire structure |
| 99 | pointerSize = ~0UL; |
| 100 | } else if (CallSite::get(QI).getInstruction() != 0) { |
| 101 | if (AA.getModRefInfo(C, CallSite::get(QI)) != AliasAnalysis::NoModRef) { |
Owen Anderson | e84f4bc | 2007-08-07 00:33:45 +0000 | [diff] [blame] | 102 | if (!start && !block) { |
| 103 | depGraphLocal.insert(std::make_pair(C.getInstruction(), |
| 104 | std::make_pair(QI, true))); |
| 105 | reverseDep[QI].insert(C.getInstruction()); |
| 106 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 107 | return QI; |
| 108 | } else { |
| 109 | continue; |
| 110 | } |
| 111 | } else |
| 112 | continue; |
| 113 | |
| 114 | if (AA.getModRefInfo(C, pointer, pointerSize) != AliasAnalysis::NoModRef) { |
Owen Anderson | e84f4bc | 2007-08-07 00:33:45 +0000 | [diff] [blame] | 115 | if (!start && !block) { |
| 116 | depGraphLocal.insert(std::make_pair(C.getInstruction(), |
| 117 | std::make_pair(QI, true))); |
| 118 | reverseDep[QI].insert(C.getInstruction()); |
| 119 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 120 | return QI; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // No dependence found |
Owen Anderson | afe840e | 2007-08-08 22:01:54 +0000 | [diff] [blame] | 125 | depGraphLocal.insert(std::make_pair(C.getInstruction(), |
| 126 | std::make_pair(NonLocal, true))); |
Owen Anderson | e84f4bc | 2007-08-07 00:33:45 +0000 | [diff] [blame] | 127 | reverseDep[NonLocal].insert(C.getInstruction()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 128 | return NonLocal; |
| 129 | } |
| 130 | |
Owen Anderson | 3de3c53 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 131 | /// nonLocalHelper - Private helper used to calculate non-local dependencies |
| 132 | /// by doing DFS on the predecessors of a block to find its dependencies |
Owen Anderson | 3f75d12 | 2007-08-01 22:01:54 +0000 | [diff] [blame] | 133 | void MemoryDependenceAnalysis::nonLocalHelper(Instruction* query, |
Owen Anderson | 5d72a42 | 2007-07-25 19:57:03 +0000 | [diff] [blame] | 134 | BasicBlock* block, |
Owen Anderson | afe840e | 2007-08-08 22:01:54 +0000 | [diff] [blame] | 135 | DenseMap<BasicBlock*, Value*>& resp) { |
Owen Anderson | 3de3c53 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 136 | // Set of blocks that we've already visited in our DFS |
Owen Anderson | 3f75d12 | 2007-08-01 22:01:54 +0000 | [diff] [blame] | 137 | SmallPtrSet<BasicBlock*, 4> visited; |
Owen Anderson | 3de3c53 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 138 | // Current stack of the DFS |
Owen Anderson | 3f75d12 | 2007-08-01 22:01:54 +0000 | [diff] [blame] | 139 | SmallVector<BasicBlock*, 4> stack; |
| 140 | stack.push_back(block); |
Owen Anderson | 4c29547 | 2007-07-24 21:52:37 +0000 | [diff] [blame] | 141 | |
Owen Anderson | 3de3c53 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 142 | // Do a basic DFS |
Owen Anderson | 3f75d12 | 2007-08-01 22:01:54 +0000 | [diff] [blame] | 143 | while (!stack.empty()) { |
| 144 | BasicBlock* BB = stack.back(); |
| 145 | |
Owen Anderson | 3de3c53 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 146 | // If we've already visited this block, no need to revist |
Owen Anderson | c6a31b9 | 2007-08-02 17:56:05 +0000 | [diff] [blame] | 147 | if (visited.count(BB)) { |
Owen Anderson | 3f75d12 | 2007-08-01 22:01:54 +0000 | [diff] [blame] | 148 | stack.pop_back(); |
| 149 | continue; |
| 150 | } |
| 151 | |
Owen Anderson | 3de3c53 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 152 | // If we find a new block with a local dependency for query, |
| 153 | // then we insert the new dependency and backtrack. |
Owen Anderson | 3f75d12 | 2007-08-01 22:01:54 +0000 | [diff] [blame] | 154 | if (BB != block) { |
Owen Anderson | c6a31b9 | 2007-08-02 17:56:05 +0000 | [diff] [blame] | 155 | visited.insert(BB); |
| 156 | |
Owen Anderson | 935e39b | 2007-08-09 04:42:44 +0000 | [diff] [blame] | 157 | Instruction* localDep = getDependency(query, 0, BB); |
Owen Anderson | 3f75d12 | 2007-08-01 22:01:54 +0000 | [diff] [blame] | 158 | if (localDep != NonLocal) { |
Owen Anderson | 935e39b | 2007-08-09 04:42:44 +0000 | [diff] [blame] | 159 | resp.insert(std::make_pair(BB, localDep)); |
Owen Anderson | c6a31b9 | 2007-08-02 17:56:05 +0000 | [diff] [blame] | 160 | stack.pop_back(); |
| 161 | |
Owen Anderson | 3f75d12 | 2007-08-01 22:01:54 +0000 | [diff] [blame] | 162 | continue; |
| 163 | } |
Owen Anderson | 3de3c53 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 164 | // If we re-encounter the starting block, we still need to search it |
| 165 | // because there might be a dependency in the starting block AFTER |
| 166 | // the position of the query. This is necessary to get loops right. |
Owen Anderson | c6a31b9 | 2007-08-02 17:56:05 +0000 | [diff] [blame] | 167 | } else if (BB == block && stack.size() > 1) { |
| 168 | visited.insert(BB); |
| 169 | |
Owen Anderson | 935e39b | 2007-08-09 04:42:44 +0000 | [diff] [blame] | 170 | Instruction* localDep = getDependency(query, 0, BB); |
Owen Anderson | c6a31b9 | 2007-08-02 17:56:05 +0000 | [diff] [blame] | 171 | if (localDep != query) |
Owen Anderson | 935e39b | 2007-08-09 04:42:44 +0000 | [diff] [blame] | 172 | resp.insert(std::make_pair(BB, localDep)); |
Owen Anderson | c6a31b9 | 2007-08-02 17:56:05 +0000 | [diff] [blame] | 173 | |
| 174 | stack.pop_back(); |
| 175 | |
| 176 | continue; |
Owen Anderson | 3f75d12 | 2007-08-01 22:01:54 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Owen Anderson | 3de3c53 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 179 | // If we didn't find anything, recurse on the precessors of this block |
Owen Anderson | 3f75d12 | 2007-08-01 22:01:54 +0000 | [diff] [blame] | 180 | bool predOnStack = false; |
| 181 | bool inserted = false; |
| 182 | for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); |
| 183 | PI != PE; ++PI) |
| 184 | if (!visited.count(*PI)) { |
| 185 | stack.push_back(*PI); |
| 186 | inserted = true; |
| 187 | } else |
| 188 | predOnStack = true; |
| 189 | |
Owen Anderson | 3de3c53 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 190 | // If we inserted a new predecessor, then we'll come back to this block |
Owen Anderson | 3f75d12 | 2007-08-01 22:01:54 +0000 | [diff] [blame] | 191 | if (inserted) |
| 192 | continue; |
Owen Anderson | 3de3c53 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 193 | // If we didn't insert because we have no predecessors, then this |
| 194 | // query has no dependency at all. |
Owen Anderson | 3f75d12 | 2007-08-01 22:01:54 +0000 | [diff] [blame] | 195 | else if (!inserted && !predOnStack) { |
Owen Anderson | 935e39b | 2007-08-09 04:42:44 +0000 | [diff] [blame] | 196 | resp.insert(std::make_pair(BB, None)); |
Owen Anderson | 3de3c53 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 197 | // If we didn't insert because our predecessors are already on the stack, |
| 198 | // then we might still have a dependency, but it will be discovered during |
| 199 | // backtracking. |
Owen Anderson | 3f75d12 | 2007-08-01 22:01:54 +0000 | [diff] [blame] | 200 | } else if (!inserted && predOnStack){ |
Owen Anderson | 935e39b | 2007-08-09 04:42:44 +0000 | [diff] [blame] | 201 | resp.insert(std::make_pair(BB, NonLocal)); |
Owen Anderson | 3f75d12 | 2007-08-01 22:01:54 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | stack.pop_back(); |
Owen Anderson | 4c29547 | 2007-07-24 21:52:37 +0000 | [diff] [blame] | 205 | } |
Owen Anderson | 4c29547 | 2007-07-24 21:52:37 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Owen Anderson | afe840e | 2007-08-08 22:01:54 +0000 | [diff] [blame] | 208 | /// getNonLocalDependency - Fills the passed-in map with the non-local |
| 209 | /// dependencies of the queries. The map will contain NonLocal for |
| 210 | /// blocks between the query and its dependencies. |
Owen Anderson | 3f75d12 | 2007-08-01 22:01:54 +0000 | [diff] [blame] | 211 | void MemoryDependenceAnalysis::getNonLocalDependency(Instruction* query, |
Owen Anderson | afe840e | 2007-08-08 22:01:54 +0000 | [diff] [blame] | 212 | DenseMap<BasicBlock*, Value*>& resp) { |
Owen Anderson | 2bd46a5 | 2007-08-16 21:27:05 +0000 | [diff] [blame] | 213 | if (depGraphNonLocal.count(query)) { |
| 214 | resp = depGraphNonLocal[query]; |
Owen Anderson | d6c7fea | 2007-09-09 21:43:49 +0000 | [diff] [blame] | 215 | NumCacheNonlocal++; |
Owen Anderson | 2bd46a5 | 2007-08-16 21:27:05 +0000 | [diff] [blame] | 216 | return; |
Owen Anderson | d6c7fea | 2007-09-09 21:43:49 +0000 | [diff] [blame] | 217 | } else |
| 218 | NumUncacheNonlocal++; |
Owen Anderson | 2bd46a5 | 2007-08-16 21:27:05 +0000 | [diff] [blame] | 219 | |
Owen Anderson | d6c7fea | 2007-09-09 21:43:49 +0000 | [diff] [blame] | 220 | // If not, go ahead and search for non-local deps. |
Owen Anderson | 3f75d12 | 2007-08-01 22:01:54 +0000 | [diff] [blame] | 221 | nonLocalHelper(query, query->getParent(), resp); |
Owen Anderson | 2bd46a5 | 2007-08-16 21:27:05 +0000 | [diff] [blame] | 222 | |
| 223 | // Update the non-local dependency cache |
| 224 | for (DenseMap<BasicBlock*, Value*>::iterator I = resp.begin(), E = resp.end(); |
| 225 | I != E; ++I) { |
| 226 | depGraphNonLocal[query].insert(*I); |
| 227 | reverseDepNonLocal[I->second].insert(query); |
| 228 | } |
Owen Anderson | 4c29547 | 2007-07-24 21:52:37 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 231 | /// getDependency - Return the instruction on which a memory operation |
| 232 | /// depends. The local paramter indicates if the query should only |
| 233 | /// evaluate dependencies within the same basic block. |
Owen Anderson | 935e39b | 2007-08-09 04:42:44 +0000 | [diff] [blame] | 234 | Instruction* MemoryDependenceAnalysis::getDependency(Instruction* query, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 235 | Instruction* start, |
Owen Anderson | 4c29547 | 2007-07-24 21:52:37 +0000 | [diff] [blame] | 236 | BasicBlock* block) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 237 | // Start looking for dependencies with the queried inst |
| 238 | BasicBlock::iterator QI = query; |
| 239 | |
| 240 | // Check for a cached result |
Owen Anderson | 935e39b | 2007-08-09 04:42:44 +0000 | [diff] [blame] | 241 | std::pair<Instruction*, bool> cachedResult = depGraphLocal[query]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 242 | // If we have a _confirmed_ cached entry, return it |
| 243 | if (cachedResult.second) |
| 244 | return cachedResult.first; |
Owen Anderson | 5d72a42 | 2007-07-25 19:57:03 +0000 | [diff] [blame] | 245 | else if (cachedResult.first && cachedResult.first != NonLocal) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 246 | // If we have an unconfirmed cached entry, we can start our search from there |
Owen Anderson | 935e39b | 2007-08-09 04:42:44 +0000 | [diff] [blame] | 247 | QI = cachedResult.first; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 248 | |
| 249 | if (start) |
| 250 | QI = start; |
Owen Anderson | 5d72a42 | 2007-07-25 19:57:03 +0000 | [diff] [blame] | 251 | else if (!start && block) |
| 252 | QI = block->end(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 253 | |
| 254 | AliasAnalysis& AA = getAnalysis<AliasAnalysis>(); |
| 255 | TargetData& TD = getAnalysis<TargetData>(); |
| 256 | |
| 257 | // Get the pointer value for which dependence will be determined |
| 258 | Value* dependee = 0; |
| 259 | uint64_t dependeeSize = 0; |
| 260 | bool queryIsVolatile = false; |
| 261 | if (StoreInst* S = dyn_cast<StoreInst>(query)) { |
| 262 | dependee = S->getPointerOperand(); |
| 263 | dependeeSize = TD.getTypeSize(S->getOperand(0)->getType()); |
| 264 | queryIsVolatile = S->isVolatile(); |
| 265 | } else if (LoadInst* L = dyn_cast<LoadInst>(query)) { |
| 266 | dependee = L->getPointerOperand(); |
| 267 | dependeeSize = TD.getTypeSize(L->getType()); |
| 268 | queryIsVolatile = L->isVolatile(); |
| 269 | } else if (VAArgInst* V = dyn_cast<VAArgInst>(query)) { |
| 270 | dependee = V->getOperand(0); |
| 271 | dependeeSize = TD.getTypeSize(V->getType()); |
| 272 | } else if (FreeInst* F = dyn_cast<FreeInst>(query)) { |
| 273 | dependee = F->getPointerOperand(); |
| 274 | |
| 275 | // FreeInsts erase the entire structure, not just a field |
| 276 | dependeeSize = ~0UL; |
| 277 | } else if (CallSite::get(query).getInstruction() != 0) |
Owen Anderson | e84f4bc | 2007-08-07 00:33:45 +0000 | [diff] [blame] | 278 | return getCallSiteDependency(CallSite::get(query), start, block); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 279 | else if (isa<AllocationInst>(query)) |
| 280 | return None; |
| 281 | else |
| 282 | return None; |
| 283 | |
Owen Anderson | 4c29547 | 2007-07-24 21:52:37 +0000 | [diff] [blame] | 284 | BasicBlock::iterator blockBegin = block ? block->begin() |
| 285 | : query->getParent()->begin(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 286 | |
Owen Anderson | 3de3c53 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 287 | // Walk backwards through the basic block, looking for dependencies |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 288 | while (QI != blockBegin) { |
| 289 | --QI; |
| 290 | |
| 291 | // If this inst is a memory op, get the pointer it accessed |
| 292 | Value* pointer = 0; |
| 293 | uint64_t pointerSize = 0; |
| 294 | if (StoreInst* S = dyn_cast<StoreInst>(QI)) { |
| 295 | // All volatile loads/stores depend on each other |
| 296 | if (queryIsVolatile && S->isVolatile()) { |
Owen Anderson | e84f4bc | 2007-08-07 00:33:45 +0000 | [diff] [blame] | 297 | if (!start && !block) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 298 | depGraphLocal.insert(std::make_pair(query, std::make_pair(S, true))); |
Owen Anderson | e84f4bc | 2007-08-07 00:33:45 +0000 | [diff] [blame] | 299 | reverseDep[S].insert(query); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | return S; |
| 303 | } |
| 304 | |
| 305 | pointer = S->getPointerOperand(); |
| 306 | pointerSize = TD.getTypeSize(S->getOperand(0)->getType()); |
| 307 | } else if (LoadInst* L = dyn_cast<LoadInst>(QI)) { |
| 308 | // All volatile loads/stores depend on each other |
| 309 | if (queryIsVolatile && L->isVolatile()) { |
Owen Anderson | e84f4bc | 2007-08-07 00:33:45 +0000 | [diff] [blame] | 310 | if (!start && !block) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 311 | depGraphLocal.insert(std::make_pair(query, std::make_pair(L, true))); |
Owen Anderson | e84f4bc | 2007-08-07 00:33:45 +0000 | [diff] [blame] | 312 | reverseDep[L].insert(query); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | return L; |
| 316 | } |
| 317 | |
| 318 | pointer = L->getPointerOperand(); |
| 319 | pointerSize = TD.getTypeSize(L->getType()); |
| 320 | } else if (AllocationInst* AI = dyn_cast<AllocationInst>(QI)) { |
| 321 | pointer = AI; |
| 322 | if (ConstantInt* C = dyn_cast<ConstantInt>(AI->getArraySize())) |
Owen Anderson | afe840e | 2007-08-08 22:01:54 +0000 | [diff] [blame] | 323 | pointerSize = C->getZExtValue() * \ |
| 324 | TD.getTypeSize(AI->getAllocatedType()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 325 | else |
| 326 | pointerSize = ~0UL; |
| 327 | } else if (VAArgInst* V = dyn_cast<VAArgInst>(QI)) { |
| 328 | pointer = V->getOperand(0); |
| 329 | pointerSize = TD.getTypeSize(V->getType()); |
| 330 | } else if (FreeInst* F = dyn_cast<FreeInst>(QI)) { |
| 331 | pointer = F->getPointerOperand(); |
| 332 | |
| 333 | // FreeInsts erase the entire structure |
| 334 | pointerSize = ~0UL; |
| 335 | } else if (CallSite::get(QI).getInstruction() != 0) { |
Owen Anderson | afe840e | 2007-08-08 22:01:54 +0000 | [diff] [blame] | 336 | // Call insts need special handling. Check if they can modify our pointer |
Owen Anderson | 151f769 | 2007-08-06 23:26:03 +0000 | [diff] [blame] | 337 | AliasAnalysis::ModRefResult MR = AA.getModRefInfo(CallSite::get(QI), |
| 338 | dependee, dependeeSize); |
| 339 | |
| 340 | if (MR != AliasAnalysis::NoModRef) { |
| 341 | // Loads don't depend on read-only calls |
| 342 | if (isa<LoadInst>(query) && MR == AliasAnalysis::Ref) |
| 343 | continue; |
| 344 | |
Owen Anderson | e84f4bc | 2007-08-07 00:33:45 +0000 | [diff] [blame] | 345 | if (!start && !block) { |
Owen Anderson | afe840e | 2007-08-08 22:01:54 +0000 | [diff] [blame] | 346 | depGraphLocal.insert(std::make_pair(query, |
| 347 | std::make_pair(QI, true))); |
Owen Anderson | e84f4bc | 2007-08-07 00:33:45 +0000 | [diff] [blame] | 348 | reverseDep[QI].insert(query); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | return QI; |
| 352 | } else { |
| 353 | continue; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | // If we found a pointer, check if it could be the same as our pointer |
| 358 | if (pointer) { |
| 359 | AliasAnalysis::AliasResult R = AA.alias(pointer, pointerSize, |
| 360 | dependee, dependeeSize); |
| 361 | |
| 362 | if (R != AliasAnalysis::NoAlias) { |
Owen Anderson | 151f769 | 2007-08-06 23:26:03 +0000 | [diff] [blame] | 363 | // May-alias loads don't depend on each other |
| 364 | if (isa<LoadInst>(query) && isa<LoadInst>(QI) && |
| 365 | R == AliasAnalysis::MayAlias) |
| 366 | continue; |
| 367 | |
Owen Anderson | e84f4bc | 2007-08-07 00:33:45 +0000 | [diff] [blame] | 368 | if (!start && !block) { |
Owen Anderson | afe840e | 2007-08-08 22:01:54 +0000 | [diff] [blame] | 369 | depGraphLocal.insert(std::make_pair(query, |
| 370 | std::make_pair(QI, true))); |
Owen Anderson | e84f4bc | 2007-08-07 00:33:45 +0000 | [diff] [blame] | 371 | reverseDep[QI].insert(query); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | return QI; |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | // If we found nothing, return the non-local flag |
Owen Anderson | e84f4bc | 2007-08-07 00:33:45 +0000 | [diff] [blame] | 380 | if (!start && !block) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 381 | depGraphLocal.insert(std::make_pair(query, |
| 382 | std::make_pair(NonLocal, true))); |
Owen Anderson | e84f4bc | 2007-08-07 00:33:45 +0000 | [diff] [blame] | 383 | reverseDep[NonLocal].insert(query); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | return NonLocal; |
| 387 | } |
| 388 | |
| 389 | /// removeInstruction - Remove an instruction from the dependence analysis, |
| 390 | /// updating the dependence of instructions that previously depended on it. |
Owen Anderson | 3de3c53 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 391 | /// This method attempts to keep the cache coherent using the reverse map. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 392 | void MemoryDependenceAnalysis::removeInstruction(Instruction* rem) { |
| 393 | // Figure out the new dep for things that currently depend on rem |
Owen Anderson | 935e39b | 2007-08-09 04:42:44 +0000 | [diff] [blame] | 394 | Instruction* newDep = NonLocal; |
David Greene | 701171c | 2007-07-31 20:01:27 +0000 | [diff] [blame] | 395 | |
| 396 | depMapType::iterator depGraphEntry = depGraphLocal.find(rem); |
David Greene | 701171c | 2007-07-31 20:01:27 +0000 | [diff] [blame] | 397 | |
| 398 | if (depGraphEntry != depGraphLocal.end()) { |
| 399 | if (depGraphEntry->second.first != NonLocal && |
| 400 | depGraphEntry->second.second) { |
| 401 | // If we have dep info for rem, set them to it |
Owen Anderson | 935e39b | 2007-08-09 04:42:44 +0000 | [diff] [blame] | 402 | BasicBlock::iterator RI = depGraphEntry->second.first; |
David Greene | 701171c | 2007-07-31 20:01:27 +0000 | [diff] [blame] | 403 | RI++; |
| 404 | newDep = RI; |
| 405 | } else if (depGraphEntry->second.first == NonLocal && |
| 406 | depGraphEntry->second.second ) { |
| 407 | // If we have a confirmed non-local flag, use it |
| 408 | newDep = NonLocal; |
| 409 | } else { |
| 410 | // Otherwise, use the immediate successor of rem |
Owen Anderson | afe840e | 2007-08-08 22:01:54 +0000 | [diff] [blame] | 411 | // NOTE: This is because, when getDependence is called, it will first |
| 412 | // check the immediate predecessor of what is in the cache. |
David Greene | 701171c | 2007-07-31 20:01:27 +0000 | [diff] [blame] | 413 | BasicBlock::iterator RI = rem; |
| 414 | RI++; |
| 415 | newDep = RI; |
| 416 | } |
| 417 | |
Owen Anderson | e84f4bc | 2007-08-07 00:33:45 +0000 | [diff] [blame] | 418 | SmallPtrSet<Instruction*, 4>& set = reverseDep[rem]; |
| 419 | for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end(); |
| 420 | I != E; ++I) { |
David Greene | 701171c | 2007-07-31 20:01:27 +0000 | [diff] [blame] | 421 | // Insert the new dependencies |
| 422 | // Mark it as unconfirmed as long as it is not the non-local flag |
Owen Anderson | e84f4bc | 2007-08-07 00:33:45 +0000 | [diff] [blame] | 423 | depGraphLocal[*I] = std::make_pair(newDep, !newDep); |
David Greene | 701171c | 2007-07-31 20:01:27 +0000 | [diff] [blame] | 424 | } |
Owen Anderson | 2bd46a5 | 2007-08-16 21:27:05 +0000 | [diff] [blame] | 425 | |
Owen Anderson | e84f4bc | 2007-08-07 00:33:45 +0000 | [diff] [blame] | 426 | reverseDep.erase(rem); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 427 | } |
Owen Anderson | 2bd46a5 | 2007-08-16 21:27:05 +0000 | [diff] [blame] | 428 | |
Owen Anderson | 0ceeca5 | 2007-09-11 04:31:00 +0000 | [diff] [blame] | 429 | if (reverseDepNonLocal.count(rem)) { |
Owen Anderson | 2bd46a5 | 2007-08-16 21:27:05 +0000 | [diff] [blame] | 430 | SmallPtrSet<Instruction*, 4>& set = reverseDepNonLocal[rem]; |
| 431 | for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end(); |
| 432 | I != E; ++I) |
| 433 | depGraphNonLocal.erase(*I); |
| 434 | |
| 435 | reverseDepNonLocal.erase(rem); |
| 436 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 437 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 438 | getAnalysis<AliasAnalysis>().deleteValue(rem); |
| 439 | } |