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