blob: 0c22eb481399ecc8a80f0eb84ebb4747fb76c229 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- MemoryDependenceAnalysis.cpp - Mem Deps Implementation --*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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 Andersonafe840e2007-08-08 22:01:54 +000012// alias analysis information, and tries to provide a lazy, caching interface to
Dan Gohmanf17a25c2007-07-18 16:29:46 +000013// a common kind of alias information query.
14//
15//===----------------------------------------------------------------------===//
16
Chris Lattner969470c2008-11-28 21:45:17 +000017#define DEBUG_TYPE "memdep"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/Analysis/MemoryDependenceAnalysis.h"
19#include "llvm/Constants.h"
20#include "llvm/Instructions.h"
21#include "llvm/Function.h"
22#include "llvm/Analysis/AliasAnalysis.h"
Chris Lattner52638032008-11-28 22:28:27 +000023#include "llvm/ADT/Statistic.h"
24#include "llvm/ADT/STLExtras.h"
Owen Anderson4c295472007-07-24 21:52:37 +000025#include "llvm/Support/CFG.h"
Tanya Lattner8edb2b72008-02-06 00:54:55 +000026#include "llvm/Support/CommandLine.h"
Chris Lattner969470c2008-11-28 21:45:17 +000027#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/Target/TargetData.h"
Owen Andersond6c7fea2007-09-09 21:43:49 +000029
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030using namespace llvm;
31
Dan Gohman089efff2008-05-13 00:00:25 +000032// Control the calculation of non-local dependencies by only examining the
33// predecessors if the basic block has less than X amount (50 by default).
34static cl::opt<int>
35PredLimit("nonlocaldep-threshold", cl::Hidden, cl::init(50),
36 cl::desc("Control the calculation of non-local"
37 "dependencies (default = 50)"));
Tanya Lattner8edb2b72008-02-06 00:54:55 +000038
Owen Andersond6c7fea2007-09-09 21:43:49 +000039STATISTIC(NumCacheNonlocal, "Number of cached non-local responses");
40STATISTIC(NumUncacheNonlocal, "Number of uncached non-local responses");
41
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042char MemoryDependenceAnalysis::ID = 0;
43
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044// Register this pass...
45static RegisterPass<MemoryDependenceAnalysis> X("memdep",
Chris Lattner969470c2008-11-28 21:45:17 +000046 "Memory Dependence Analysis", false, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047
Chris Lattner969470c2008-11-28 21:45:17 +000048/// verifyRemoved - Verify that the specified instruction does not occur
49/// in our internal data structures.
Chris Lattner48b24692008-11-28 21:42:09 +000050void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const {
Chris Lattnerfd9b56d2008-11-29 01:43:36 +000051 for (LocalDepMapType::const_iterator I = LocalDeps.begin(),
52 E = LocalDeps.end(); I != E; ++I) {
Chris Lattner969470c2008-11-28 21:45:17 +000053 assert(I->first != D && "Inst occurs in data structures");
Chris Lattnerfd9b56d2008-11-29 01:43:36 +000054 assert(I->second.first.getPointer() != D &&
55 "Inst occurs in data structures");
Owen Andersonc772be72007-12-08 01:37:09 +000056 }
57
Chris Lattner48b24692008-11-28 21:42:09 +000058 for (nonLocalDepMapType::const_iterator I = depGraphNonLocal.begin(),
59 E = depGraphNonLocal.end(); I != E; ++I) {
Chris Lattner969470c2008-11-28 21:45:17 +000060 assert(I->first != D && "Inst occurs in data structures");
Chris Lattnerfd9b56d2008-11-29 01:43:36 +000061 for (DenseMap<BasicBlock*, DepResultTy>::iterator II = I->second.begin(),
Owen Andersonc8f33362008-06-01 20:51:41 +000062 EE = I->second.end(); II != EE; ++II)
Chris Lattnerfd9b56d2008-11-29 01:43:36 +000063 assert(II->second.getPointer() != D && "Inst occurs in data structures");
Owen Andersonc772be72007-12-08 01:37:09 +000064 }
65
Chris Lattner48b24692008-11-28 21:42:09 +000066 for (reverseDepMapType::const_iterator I = reverseDep.begin(),
67 E = reverseDep.end(); I != E; ++I)
68 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
69 EE = I->second.end(); II != EE; ++II)
Chris Lattner969470c2008-11-28 21:45:17 +000070 assert(*II != D && "Inst occurs in data structures");
Owen Andersonc772be72007-12-08 01:37:09 +000071
Chris Lattner48b24692008-11-28 21:42:09 +000072 for (reverseDepMapType::const_iterator I = reverseDepNonLocal.begin(),
73 E = reverseDepNonLocal.end();
Owen Andersonc772be72007-12-08 01:37:09 +000074 I != E; ++I)
Chris Lattner48b24692008-11-28 21:42:09 +000075 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
76 EE = I->second.end(); II != EE; ++II)
Chris Lattner969470c2008-11-28 21:45:17 +000077 assert(*II != D && "Inst occurs in data structures");
Owen Andersonc772be72007-12-08 01:37:09 +000078}
79
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080/// getAnalysisUsage - Does not modify anything. It uses Alias Analysis.
81///
82void MemoryDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
83 AU.setPreservesAll();
84 AU.addRequiredTransitive<AliasAnalysis>();
85 AU.addRequiredTransitive<TargetData>();
86}
87
Owen Anderson3de3c532007-08-08 22:26:03 +000088/// getCallSiteDependency - Private helper for finding the local dependencies
89/// of a call site.
Chris Lattner12cafbf2008-11-29 02:29:27 +000090MemDepResult MemoryDependenceAnalysis::
Chris Lattnerfd9b56d2008-11-29 01:43:36 +000091getCallSiteDependency(CallSite C, Instruction *start, BasicBlock *block) {
92 std::pair<DepResultTy, bool> &cachedResult = LocalDeps[C.getInstruction()];
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093 AliasAnalysis& AA = getAnalysis<AliasAnalysis>();
94 TargetData& TD = getAnalysis<TargetData>();
95 BasicBlock::iterator blockBegin = C.getInstruction()->getParent()->begin();
96 BasicBlock::iterator QI = C.getInstruction();
97
Duncan Sands4b01c632008-09-11 19:41:10 +000098 // If the starting point was specified, use it
Owen Andersone84f4bc2007-08-07 00:33:45 +000099 if (start) {
100 QI = start;
Dan Gohmand4752682008-03-31 22:08:00 +0000101 blockBegin = start->getParent()->begin();
Owen Anderson3de3c532007-08-08 22:26:03 +0000102 // If the starting point wasn't specified, but the block was, use it
Owen Andersone84f4bc2007-08-07 00:33:45 +0000103 } else if (!start && block) {
104 QI = block->end();
Dan Gohmand4752682008-03-31 22:08:00 +0000105 blockBegin = block->begin();
Owen Andersone84f4bc2007-08-07 00:33:45 +0000106 }
107
Owen Anderson3de3c532007-08-08 22:26:03 +0000108 // Walk backwards through the block, looking for dependencies
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 while (QI != blockBegin) {
110 --QI;
111
112 // If this inst is a memory op, get the pointer it accessed
113 Value* pointer = 0;
114 uint64_t pointerSize = 0;
115 if (StoreInst* S = dyn_cast<StoreInst>(QI)) {
116 pointer = S->getPointerOperand();
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000117 pointerSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 } else if (AllocationInst* AI = dyn_cast<AllocationInst>(QI)) {
119 pointer = AI;
120 if (ConstantInt* C = dyn_cast<ConstantInt>(AI->getArraySize()))
Chris Lattner39bc72f2008-11-28 21:16:44 +0000121 pointerSize = C->getZExtValue() *
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000122 TD.getABITypeSize(AI->getAllocatedType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 else
124 pointerSize = ~0UL;
125 } else if (VAArgInst* V = dyn_cast<VAArgInst>(QI)) {
126 pointer = V->getOperand(0);
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000127 pointerSize = TD.getTypeStoreSize(V->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128 } else if (FreeInst* F = dyn_cast<FreeInst>(QI)) {
129 pointer = F->getPointerOperand();
130
131 // FreeInsts erase the entire structure
132 pointerSize = ~0UL;
Owen Andersona10020b2008-05-13 21:25:37 +0000133 } else if (CallSite::get(QI).getInstruction() != 0) {
Owen Anderson8b6f04e2007-11-26 02:26:36 +0000134 AliasAnalysis::ModRefBehavior result =
Duncan Sands00b24b52007-12-01 07:51:45 +0000135 AA.getModRefBehavior(CallSite::get(QI));
Owen Andersona0b2b8e2008-04-17 05:36:50 +0000136 if (result != AliasAnalysis::DoesNotAccessMemory) {
Owen Andersone84f4bc2007-08-07 00:33:45 +0000137 if (!start && !block) {
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000138 cachedResult.first = DepResultTy(QI, Normal);
Owen Andersonc772be72007-12-08 01:37:09 +0000139 cachedResult.second = true;
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000140 reverseDep[DepResultTy(QI, Normal)].insert(C.getInstruction());
Owen Andersone84f4bc2007-08-07 00:33:45 +0000141 }
Chris Lattner12cafbf2008-11-29 02:29:27 +0000142 return MemDepResult::get(QI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143 } else {
144 continue;
145 }
146 } else
147 continue;
148
149 if (AA.getModRefInfo(C, pointer, pointerSize) != AliasAnalysis::NoModRef) {
Owen Andersone84f4bc2007-08-07 00:33:45 +0000150 if (!start && !block) {
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000151 cachedResult.first = DepResultTy(QI, Normal);
Owen Andersonc772be72007-12-08 01:37:09 +0000152 cachedResult.second = true;
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000153 reverseDep[DepResultTy(QI, Normal)].insert(C.getInstruction());
Owen Andersone84f4bc2007-08-07 00:33:45 +0000154 }
Chris Lattner12cafbf2008-11-29 02:29:27 +0000155 return MemDepResult::get(QI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 }
157 }
158
159 // No dependence found
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000160 cachedResult.first = DepResultTy(0, NonLocal);
Owen Andersonc772be72007-12-08 01:37:09 +0000161 cachedResult.second = true;
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000162 reverseDep[DepResultTy(0, NonLocal)].insert(C.getInstruction());
Chris Lattner12cafbf2008-11-29 02:29:27 +0000163 return MemDepResult::getNonLocal();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164}
165
Owen Anderson3de3c532007-08-08 22:26:03 +0000166/// nonLocalHelper - Private helper used to calculate non-local dependencies
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000167/// by doing DFS on the predecessors of a block to find its dependencies.
Owen Anderson3f75d122007-08-01 22:01:54 +0000168void MemoryDependenceAnalysis::nonLocalHelper(Instruction* query,
Owen Anderson5d72a422007-07-25 19:57:03 +0000169 BasicBlock* block,
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000170 DenseMap<BasicBlock*, DepResultTy> &resp) {
Owen Anderson3de3c532007-08-08 22:26:03 +0000171 // Set of blocks that we've already visited in our DFS
Owen Anderson3f75d122007-08-01 22:01:54 +0000172 SmallPtrSet<BasicBlock*, 4> visited;
Owen Anderson05749072007-09-21 03:53:52 +0000173 // If we're updating a dirtied cache entry, we don't need to reprocess
174 // already computed entries.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000175 for (DenseMap<BasicBlock*, DepResultTy>::iterator I = resp.begin(),
Owen Anderson05749072007-09-21 03:53:52 +0000176 E = resp.end(); I != E; ++I)
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000177 if (I->second.getInt() != Dirty)
Owen Anderson05749072007-09-21 03:53:52 +0000178 visited.insert(I->first);
179
Owen Anderson3de3c532007-08-08 22:26:03 +0000180 // Current stack of the DFS
Owen Anderson3f75d122007-08-01 22:01:54 +0000181 SmallVector<BasicBlock*, 4> stack;
Owen Anderson2fecdf12008-04-10 22:13:32 +0000182 for (pred_iterator PI = pred_begin(block), PE = pred_end(block);
183 PI != PE; ++PI)
184 stack.push_back(*PI);
Owen Anderson4c295472007-07-24 21:52:37 +0000185
Owen Anderson3de3c532007-08-08 22:26:03 +0000186 // Do a basic DFS
Owen Anderson3f75d122007-08-01 22:01:54 +0000187 while (!stack.empty()) {
188 BasicBlock* BB = stack.back();
189
Owen Anderson3de3c532007-08-08 22:26:03 +0000190 // If we've already visited this block, no need to revist
Owen Andersonc6a31b92007-08-02 17:56:05 +0000191 if (visited.count(BB)) {
Owen Anderson3f75d122007-08-01 22:01:54 +0000192 stack.pop_back();
193 continue;
194 }
195
Owen Anderson3de3c532007-08-08 22:26:03 +0000196 // If we find a new block with a local dependency for query,
197 // then we insert the new dependency and backtrack.
Owen Anderson3f75d122007-08-01 22:01:54 +0000198 if (BB != block) {
Owen Andersonc6a31b92007-08-02 17:56:05 +0000199 visited.insert(BB);
200
Chris Lattner12cafbf2008-11-29 02:29:27 +0000201 MemDepResult localDep = getDependency(query, 0, BB);
202 if (!localDep.isNonLocal()) {
203 resp.insert(std::make_pair(BB, ConvFromResult(localDep)));
Owen Andersonc6a31b92007-08-02 17:56:05 +0000204 stack.pop_back();
Owen Anderson3f75d122007-08-01 22:01:54 +0000205 continue;
206 }
Owen Anderson3de3c532007-08-08 22:26:03 +0000207 // If we re-encounter the starting block, we still need to search it
208 // because there might be a dependency in the starting block AFTER
209 // the position of the query. This is necessary to get loops right.
Owen Anderson2fecdf12008-04-10 22:13:32 +0000210 } else if (BB == block) {
Owen Andersonc6a31b92007-08-02 17:56:05 +0000211 visited.insert(BB);
212
Chris Lattner12cafbf2008-11-29 02:29:27 +0000213 MemDepResult localDep = getDependency(query, 0, BB);
214 if (localDep.getInst() != query)
215 resp.insert(std::make_pair(BB, ConvFromResult(localDep)));
Owen Andersonc6a31b92007-08-02 17:56:05 +0000216
217 stack.pop_back();
Owen Andersonc6a31b92007-08-02 17:56:05 +0000218 continue;
Owen Anderson3f75d122007-08-01 22:01:54 +0000219 }
220
Owen Anderson3de3c532007-08-08 22:26:03 +0000221 // If we didn't find anything, recurse on the precessors of this block
Tanya Lattner8edb2b72008-02-06 00:54:55 +0000222 // Only do this for blocks with a small number of predecessors.
Owen Anderson3f75d122007-08-01 22:01:54 +0000223 bool predOnStack = false;
224 bool inserted = false;
Tanya Lattner8edb2b72008-02-06 00:54:55 +0000225 if (std::distance(pred_begin(BB), pred_end(BB)) <= PredLimit) {
226 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
227 PI != PE; ++PI)
228 if (!visited.count(*PI)) {
229 stack.push_back(*PI);
230 inserted = true;
231 } else
232 predOnStack = true;
233 }
Owen Anderson3f75d122007-08-01 22:01:54 +0000234
Owen Anderson3de3c532007-08-08 22:26:03 +0000235 // If we inserted a new predecessor, then we'll come back to this block
Owen Anderson3f75d122007-08-01 22:01:54 +0000236 if (inserted)
237 continue;
Owen Anderson3de3c532007-08-08 22:26:03 +0000238 // If we didn't insert because we have no predecessors, then this
239 // query has no dependency at all.
Owen Anderson3f75d122007-08-01 22:01:54 +0000240 else if (!inserted && !predOnStack) {
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000241 resp.insert(std::make_pair(BB, DepResultTy(0, None)));
Owen Anderson3de3c532007-08-08 22:26:03 +0000242 // If we didn't insert because our predecessors are already on the stack,
243 // then we might still have a dependency, but it will be discovered during
244 // backtracking.
Owen Anderson3f75d122007-08-01 22:01:54 +0000245 } else if (!inserted && predOnStack){
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000246 resp.insert(std::make_pair(BB, DepResultTy(0, NonLocal)));
Owen Anderson3f75d122007-08-01 22:01:54 +0000247 }
248
249 stack.pop_back();
Owen Anderson4c295472007-07-24 21:52:37 +0000250 }
Owen Anderson4c295472007-07-24 21:52:37 +0000251}
252
Owen Andersonafe840e2007-08-08 22:01:54 +0000253/// getNonLocalDependency - Fills the passed-in map with the non-local
254/// dependencies of the queries. The map will contain NonLocal for
255/// blocks between the query and its dependencies.
Owen Anderson3f75d122007-08-01 22:01:54 +0000256void MemoryDependenceAnalysis::getNonLocalDependency(Instruction* query,
Chris Lattner12cafbf2008-11-29 02:29:27 +0000257 DenseMap<BasicBlock*, MemDepResult> &resp) {
Owen Anderson2bd46a52007-08-16 21:27:05 +0000258 if (depGraphNonLocal.count(query)) {
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000259 DenseMap<BasicBlock*, DepResultTy> &cached = depGraphNonLocal[query];
Owen Andersond6c7fea2007-09-09 21:43:49 +0000260 NumCacheNonlocal++;
Owen Anderson05749072007-09-21 03:53:52 +0000261
262 SmallVector<BasicBlock*, 4> dirtied;
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000263 for (DenseMap<BasicBlock*, DepResultTy>::iterator I = cached.begin(),
Owen Anderson05749072007-09-21 03:53:52 +0000264 E = cached.end(); I != E; ++I)
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000265 if (I->second.getInt() == Dirty)
Owen Anderson05749072007-09-21 03:53:52 +0000266 dirtied.push_back(I->first);
267
268 for (SmallVector<BasicBlock*, 4>::iterator I = dirtied.begin(),
269 E = dirtied.end(); I != E; ++I) {
Chris Lattner12cafbf2008-11-29 02:29:27 +0000270 MemDepResult localDep = getDependency(query, 0, *I);
271 if (!localDep.isNonLocal())
272 cached[*I] = ConvFromResult(localDep);
Owen Anderson05749072007-09-21 03:53:52 +0000273 else {
274 cached.erase(*I);
275 nonLocalHelper(query, *I, cached);
276 }
277 }
278
Chris Lattner12cafbf2008-11-29 02:29:27 +0000279 // Update the reverse non-local dependency cache.
280 for (DenseMap<BasicBlock*, DepResultTy>::iterator I = cached.begin(),
281 E = cached.end(); I != E; ++I) {
Owen Andersondf3055a2008-06-01 21:03:52 +0000282 reverseDepNonLocal[I->second].insert(query);
Chris Lattner12cafbf2008-11-29 02:29:27 +0000283 resp[I->first] = ConvToResult(I->second);
284 }
Owen Andersondf3055a2008-06-01 21:03:52 +0000285
Owen Anderson2bd46a52007-08-16 21:27:05 +0000286 return;
Chris Lattner12cafbf2008-11-29 02:29:27 +0000287 }
288
289 NumUncacheNonlocal++;
Owen Anderson2bd46a52007-08-16 21:27:05 +0000290
Owen Andersond6c7fea2007-09-09 21:43:49 +0000291 // If not, go ahead and search for non-local deps.
Chris Lattner12cafbf2008-11-29 02:29:27 +0000292 DenseMap<BasicBlock*, DepResultTy> &cached = depGraphNonLocal[query];
293 nonLocalHelper(query, query->getParent(), cached);
294
Owen Anderson2bd46a52007-08-16 21:27:05 +0000295 // Update the non-local dependency cache
Chris Lattner12cafbf2008-11-29 02:29:27 +0000296 for (DenseMap<BasicBlock*, DepResultTy>::iterator I = cached.begin(),
297 E = cached.end(); I != E; ++I) {
298 // FIXME: Merge with the code above!
Owen Anderson2bd46a52007-08-16 21:27:05 +0000299 reverseDepNonLocal[I->second].insert(query);
Chris Lattner12cafbf2008-11-29 02:29:27 +0000300 resp[I->first] = ConvToResult(I->second);
Owen Anderson2bd46a52007-08-16 21:27:05 +0000301 }
Owen Anderson4c295472007-07-24 21:52:37 +0000302}
303
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304/// getDependency - Return the instruction on which a memory operation
Dan Gohmanf1f99a22008-04-10 23:02:38 +0000305/// depends. The local parameter indicates if the query should only
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306/// evaluate dependencies within the same basic block.
Chris Lattner12cafbf2008-11-29 02:29:27 +0000307MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *query,
308 Instruction *start,
309 BasicBlock *block) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310 // Start looking for dependencies with the queried inst
311 BasicBlock::iterator QI = query;
312
313 // Check for a cached result
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000314 std::pair<DepResultTy, bool>& cachedResult = LocalDeps[query];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315 // If we have a _confirmed_ cached entry, return it
Owen Andersonc772be72007-12-08 01:37:09 +0000316 if (!block && !start) {
317 if (cachedResult.second)
Chris Lattner12cafbf2008-11-29 02:29:27 +0000318 return ConvToResult(cachedResult.first);
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000319 else if (cachedResult.first.getInt() == Normal &&
320 cachedResult.first.getPointer())
321 // If we have an unconfirmed cached entry, we can start our search from
322 // it.
323 QI = cachedResult.first.getPointer();
Owen Andersonc772be72007-12-08 01:37:09 +0000324 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325
326 if (start)
327 QI = start;
Owen Anderson5d72a422007-07-25 19:57:03 +0000328 else if (!start && block)
329 QI = block->end();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000330
331 AliasAnalysis& AA = getAnalysis<AliasAnalysis>();
332 TargetData& TD = getAnalysis<TargetData>();
333
334 // Get the pointer value for which dependence will be determined
335 Value* dependee = 0;
336 uint64_t dependeeSize = 0;
337 bool queryIsVolatile = false;
338 if (StoreInst* S = dyn_cast<StoreInst>(query)) {
339 dependee = S->getPointerOperand();
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000340 dependeeSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341 queryIsVolatile = S->isVolatile();
342 } else if (LoadInst* L = dyn_cast<LoadInst>(query)) {
343 dependee = L->getPointerOperand();
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000344 dependeeSize = TD.getTypeStoreSize(L->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 queryIsVolatile = L->isVolatile();
346 } else if (VAArgInst* V = dyn_cast<VAArgInst>(query)) {
347 dependee = V->getOperand(0);
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000348 dependeeSize = TD.getTypeStoreSize(V->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349 } else if (FreeInst* F = dyn_cast<FreeInst>(query)) {
350 dependee = F->getPointerOperand();
351
352 // FreeInsts erase the entire structure, not just a field
353 dependeeSize = ~0UL;
354 } else if (CallSite::get(query).getInstruction() != 0)
Owen Andersone84f4bc2007-08-07 00:33:45 +0000355 return getCallSiteDependency(CallSite::get(query), start, block);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356 else if (isa<AllocationInst>(query))
Chris Lattner12cafbf2008-11-29 02:29:27 +0000357 return MemDepResult::getNone();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358 else
Chris Lattner12cafbf2008-11-29 02:29:27 +0000359 return MemDepResult::getNone();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000360
Owen Anderson4c295472007-07-24 21:52:37 +0000361 BasicBlock::iterator blockBegin = block ? block->begin()
362 : query->getParent()->begin();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000363
Owen Anderson3de3c532007-08-08 22:26:03 +0000364 // Walk backwards through the basic block, looking for dependencies
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000365 while (QI != blockBegin) {
366 --QI;
367
368 // If this inst is a memory op, get the pointer it accessed
369 Value* pointer = 0;
370 uint64_t pointerSize = 0;
371 if (StoreInst* S = dyn_cast<StoreInst>(QI)) {
372 // All volatile loads/stores depend on each other
373 if (queryIsVolatile && S->isVolatile()) {
Owen Andersone84f4bc2007-08-07 00:33:45 +0000374 if (!start && !block) {
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000375 cachedResult.first = DepResultTy(S, Normal);
Owen Andersonc772be72007-12-08 01:37:09 +0000376 cachedResult.second = true;
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000377 reverseDep[DepResultTy(S, Normal)].insert(query);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378 }
379
Chris Lattner12cafbf2008-11-29 02:29:27 +0000380 return MemDepResult::get(S);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000381 }
382
383 pointer = S->getPointerOperand();
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000384 pointerSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385 } else if (LoadInst* L = dyn_cast<LoadInst>(QI)) {
386 // All volatile loads/stores depend on each other
387 if (queryIsVolatile && L->isVolatile()) {
Owen Andersone84f4bc2007-08-07 00:33:45 +0000388 if (!start && !block) {
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000389 cachedResult.first = DepResultTy(L, Normal);
Owen Andersonc772be72007-12-08 01:37:09 +0000390 cachedResult.second = true;
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000391 reverseDep[DepResultTy(L, Normal)].insert(query);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392 }
393
Chris Lattner12cafbf2008-11-29 02:29:27 +0000394 return MemDepResult::get(L);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000395 }
396
397 pointer = L->getPointerOperand();
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000398 pointerSize = TD.getTypeStoreSize(L->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399 } else if (AllocationInst* AI = dyn_cast<AllocationInst>(QI)) {
400 pointer = AI;
401 if (ConstantInt* C = dyn_cast<ConstantInt>(AI->getArraySize()))
Chris Lattner39bc72f2008-11-28 21:16:44 +0000402 pointerSize = C->getZExtValue() *
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000403 TD.getABITypeSize(AI->getAllocatedType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404 else
405 pointerSize = ~0UL;
406 } else if (VAArgInst* V = dyn_cast<VAArgInst>(QI)) {
407 pointer = V->getOperand(0);
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000408 pointerSize = TD.getTypeStoreSize(V->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409 } else if (FreeInst* F = dyn_cast<FreeInst>(QI)) {
410 pointer = F->getPointerOperand();
411
412 // FreeInsts erase the entire structure
413 pointerSize = ~0UL;
414 } else if (CallSite::get(QI).getInstruction() != 0) {
Owen Andersonafe840e2007-08-08 22:01:54 +0000415 // Call insts need special handling. Check if they can modify our pointer
Owen Anderson151f7692007-08-06 23:26:03 +0000416 AliasAnalysis::ModRefResult MR = AA.getModRefInfo(CallSite::get(QI),
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000417 dependee, dependeeSize);
Owen Anderson151f7692007-08-06 23:26:03 +0000418
419 if (MR != AliasAnalysis::NoModRef) {
420 // Loads don't depend on read-only calls
421 if (isa<LoadInst>(query) && MR == AliasAnalysis::Ref)
422 continue;
423
Owen Andersone84f4bc2007-08-07 00:33:45 +0000424 if (!start && !block) {
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000425 cachedResult.first = DepResultTy(QI, Normal);
Owen Andersonc772be72007-12-08 01:37:09 +0000426 cachedResult.second = true;
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000427 reverseDep[DepResultTy(QI, Normal)].insert(query);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000428 }
Chris Lattner12cafbf2008-11-29 02:29:27 +0000429 return MemDepResult::get(QI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430 } else {
431 continue;
432 }
433 }
434
435 // If we found a pointer, check if it could be the same as our pointer
436 if (pointer) {
437 AliasAnalysis::AliasResult R = AA.alias(pointer, pointerSize,
438 dependee, dependeeSize);
439
440 if (R != AliasAnalysis::NoAlias) {
Owen Anderson151f7692007-08-06 23:26:03 +0000441 // May-alias loads don't depend on each other
442 if (isa<LoadInst>(query) && isa<LoadInst>(QI) &&
443 R == AliasAnalysis::MayAlias)
444 continue;
445
Owen Andersone84f4bc2007-08-07 00:33:45 +0000446 if (!start && !block) {
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000447 cachedResult.first = DepResultTy(QI, Normal);
Owen Andersonc772be72007-12-08 01:37:09 +0000448 cachedResult.second = true;
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000449 reverseDep[DepResultTy(QI, Normal)].insert(query);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450 }
451
Chris Lattner12cafbf2008-11-29 02:29:27 +0000452 return MemDepResult::get(QI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 }
454 }
455 }
456
457 // If we found nothing, return the non-local flag
Owen Andersone84f4bc2007-08-07 00:33:45 +0000458 if (!start && !block) {
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000459 cachedResult.first = DepResultTy(0, NonLocal);
Owen Andersonc772be72007-12-08 01:37:09 +0000460 cachedResult.second = true;
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000461 reverseDep[DepResultTy(0, NonLocal)].insert(query);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000462 }
463
Chris Lattner12cafbf2008-11-29 02:29:27 +0000464 return MemDepResult::getNonLocal();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465}
466
Owen Anderson8d272d52008-02-12 21:15:18 +0000467/// dropInstruction - Remove an instruction from the analysis, making
468/// absolutely conservative assumptions when updating the cache. This is
469/// useful, for example when an instruction is changed rather than removed.
470void MemoryDependenceAnalysis::dropInstruction(Instruction* drop) {
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000471 LocalDepMapType::iterator depGraphEntry = LocalDeps.find(drop);
472 if (depGraphEntry != LocalDeps.end())
Owen Anderson8d272d52008-02-12 21:15:18 +0000473 reverseDep[depGraphEntry->second.first].erase(drop);
474
475 // Drop dependency information for things that depended on this instr
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000476 SmallPtrSet<Instruction*, 4>& set = reverseDep[DepResultTy(drop, Normal)];
Owen Anderson8d272d52008-02-12 21:15:18 +0000477 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
478 I != E; ++I)
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000479 LocalDeps.erase(*I);
Owen Anderson8d272d52008-02-12 21:15:18 +0000480
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000481 LocalDeps.erase(drop);
482 reverseDep.erase(DepResultTy(drop, Normal));
Owen Anderson8d272d52008-02-12 21:15:18 +0000483
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000484 for (DenseMap<BasicBlock*, DepResultTy>::iterator DI =
485 depGraphNonLocal[drop].begin(), DE = depGraphNonLocal[drop].end();
Owen Anderson8d272d52008-02-12 21:15:18 +0000486 DI != DE; ++DI)
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000487 if (DI->second.getInt() != None)
Owen Anderson8d272d52008-02-12 21:15:18 +0000488 reverseDepNonLocal[DI->second].erase(drop);
489
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000490 if (reverseDepNonLocal.count(DepResultTy(drop, Normal))) {
491 SmallPtrSet<Instruction*, 4>& set =
492 reverseDepNonLocal[DepResultTy(drop, Normal)];
Owen Anderson8d272d52008-02-12 21:15:18 +0000493 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
494 I != E; ++I)
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000495 for (DenseMap<BasicBlock*, DepResultTy>::iterator DI =
Owen Anderson8d272d52008-02-12 21:15:18 +0000496 depGraphNonLocal[*I].begin(), DE = depGraphNonLocal[*I].end();
497 DI != DE; ++DI)
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000498 if (DI->second == DepResultTy(drop, Normal))
499 DI->second = DepResultTy(0, Dirty);
Owen Anderson8d272d52008-02-12 21:15:18 +0000500 }
501
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000502 reverseDepNonLocal.erase(DepResultTy(drop, Normal));
503 depGraphNonLocal.erase(drop);
Owen Anderson8d272d52008-02-12 21:15:18 +0000504}
505
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000506/// removeInstruction - Remove an instruction from the dependence analysis,
507/// updating the dependence of instructions that previously depended on it.
Owen Anderson3de3c532007-08-08 22:26:03 +0000508/// This method attempts to keep the cache coherent using the reverse map.
Chris Lattner1b185de2008-11-28 22:04:47 +0000509void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) {
Chris Lattner1b185de2008-11-28 22:04:47 +0000510 // Walk through the Non-local dependencies, removing this one as the value
511 // for any cached queries.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000512 for (DenseMap<BasicBlock*, DepResultTy>::iterator DI =
Chris Lattner1b185de2008-11-28 22:04:47 +0000513 depGraphNonLocal[RemInst].begin(), DE = depGraphNonLocal[RemInst].end();
Owen Andersonc772be72007-12-08 01:37:09 +0000514 DI != DE; ++DI)
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000515 if (DI->second.getInt() != None)
Chris Lattner1b185de2008-11-28 22:04:47 +0000516 reverseDepNonLocal[DI->second].erase(RemInst);
Owen Andersonc772be72007-12-08 01:37:09 +0000517
Chris Lattner52638032008-11-28 22:28:27 +0000518 // Shortly after this, we will look for things that depend on RemInst. In
519 // order to update these, we'll need a new dependency to base them on. We
520 // could completely delete any entries that depend on this, but it is better
521 // to make a more accurate approximation where possible. Compute that better
522 // approximation if we can.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000523 DepResultTy NewDependency;
Chris Lattner52638032008-11-28 22:28:27 +0000524 bool NewDependencyConfirmed = false;
525
Chris Lattner1b185de2008-11-28 22:04:47 +0000526 // If we have a cached local dependence query for this instruction, remove it.
Chris Lattner52638032008-11-28 22:28:27 +0000527 //
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000528 LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst);
529 if (LocalDepEntry != LocalDeps.end()) {
530 DepResultTy LocalDep = LocalDepEntry->second.first;
Chris Lattner52638032008-11-28 22:28:27 +0000531 bool IsConfirmed = LocalDepEntry->second.second;
Owen Anderson25296a22008-01-30 01:24:05 +0000532
Chris Lattner52638032008-11-28 22:28:27 +0000533 // Remove this local dependency info.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000534 LocalDeps.erase(LocalDepEntry);
Chris Lattner1b185de2008-11-28 22:04:47 +0000535
Chris Lattner52638032008-11-28 22:28:27 +0000536 // Remove us from DepInst's reverse set now that the local dep info is gone.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000537 reverseDep[LocalDep].erase(RemInst);
Chris Lattner52638032008-11-28 22:28:27 +0000538
539 // If we have unconfirmed info, don't trust it.
540 if (IsConfirmed) {
541 // If we have a confirmed non-local flag, use it.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000542 if (LocalDep.getInt() == NonLocal || LocalDep.getInt() == None) {
Chris Lattner89fbbe72008-11-28 22:51:08 +0000543 // The only time this dependency is confirmed is if it is non-local.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000544 NewDependency = LocalDep;
Chris Lattner52638032008-11-28 22:28:27 +0000545 NewDependencyConfirmed = true;
546 } else {
547 // If we have dep info for RemInst, set them to it.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000548 Instruction *NDI = next(BasicBlock::iterator(LocalDep.getPointer()));
549 if (NDI != RemInst) // Don't use RemInst for the new dependency!
550 NewDependency = DepResultTy(NDI, Normal);
Chris Lattner52638032008-11-28 22:28:27 +0000551 }
David Greene701171c2007-07-31 20:01:27 +0000552 }
Owen Anderson6487cf52008-02-05 04:34:03 +0000553 }
554
Chris Lattner52638032008-11-28 22:28:27 +0000555 // If we don't already have a local dependency answer for this instruction,
556 // use the immediate successor of RemInst. We use the successor because
557 // getDependence starts by checking the immediate predecessor of what is in
558 // the cache.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000559 if (NewDependency == DepResultTy(0, Normal))
560 NewDependency = DepResultTy(next(BasicBlock::iterator(RemInst)), Normal);
Chris Lattner52638032008-11-28 22:28:27 +0000561
Chris Lattner89fbbe72008-11-28 22:51:08 +0000562 // Loop over all of the things that depend on the instruction we're removing.
563 //
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000564 reverseDepMapType::iterator ReverseDepIt =
565 reverseDep.find(DepResultTy(RemInst, Normal));
Chris Lattner89fbbe72008-11-28 22:51:08 +0000566 if (ReverseDepIt != reverseDep.end()) {
567 SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second;
568 for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(),
569 E = ReverseDeps.end(); I != E; ++I) {
570 Instruction *InstDependingOnRemInst = *I;
571
572 // If we thought the instruction depended on itself (possible for
573 // unconfirmed dependencies) ignore the update.
574 if (InstDependingOnRemInst == RemInst) continue;
575
576 // Insert the new dependencies.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000577 LocalDeps[InstDependingOnRemInst] =
Chris Lattner89fbbe72008-11-28 22:51:08 +0000578 std::make_pair(NewDependency, NewDependencyConfirmed);
579
580 // If our NewDependency is an instruction, make sure to remember that new
581 // things depend on it.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000582 // FIXME: Just insert all deps!
583 if (NewDependency.getInt() != NonLocal && NewDependency.getInt() != None)
Chris Lattner89fbbe72008-11-28 22:51:08 +0000584 reverseDep[NewDependency].insert(InstDependingOnRemInst);
585 }
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000586 reverseDep.erase(DepResultTy(RemInst, Normal));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000587 }
Owen Anderson2bd46a52007-08-16 21:27:05 +0000588
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000589 ReverseDepIt = reverseDepNonLocal.find(DepResultTy(RemInst, Normal));
Chris Lattner89fbbe72008-11-28 22:51:08 +0000590 if (ReverseDepIt != reverseDepNonLocal.end()) {
591 SmallPtrSet<Instruction*, 4>& set = ReverseDepIt->second;
Owen Anderson2bd46a52007-08-16 21:27:05 +0000592 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
593 I != E; ++I)
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000594 for (DenseMap<BasicBlock*, DepResultTy>::iterator DI =
Owen Anderson05749072007-09-21 03:53:52 +0000595 depGraphNonLocal[*I].begin(), DE = depGraphNonLocal[*I].end();
596 DI != DE; ++DI)
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000597 if (DI->second == DepResultTy(RemInst, Normal))
598 DI->second = DepResultTy(0, Dirty);
599 reverseDepNonLocal.erase(ReverseDepIt);
Owen Anderson2bd46a52007-08-16 21:27:05 +0000600 }
Owen Andersonc772be72007-12-08 01:37:09 +0000601
Chris Lattner1b185de2008-11-28 22:04:47 +0000602 depGraphNonLocal.erase(RemInst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000603
Chris Lattner1b185de2008-11-28 22:04:47 +0000604 getAnalysis<AliasAnalysis>().deleteValue(RemInst);
Chris Lattner969470c2008-11-28 21:45:17 +0000605
Chris Lattner1b185de2008-11-28 22:04:47 +0000606 DEBUG(verifyRemoved(RemInst));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607}