blob: dd67407ccdfc7bec811662f419e49e0cac860342 [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"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029using namespace llvm;
30
Dan Gohman089efff2008-05-13 00:00:25 +000031// Control the calculation of non-local dependencies by only examining the
32// predecessors if the basic block has less than X amount (50 by default).
33static cl::opt<int>
34PredLimit("nonlocaldep-threshold", cl::Hidden, cl::init(50),
35 cl::desc("Control the calculation of non-local"
36 "dependencies (default = 50)"));
Tanya Lattner8edb2b72008-02-06 00:54:55 +000037
Owen Andersond6c7fea2007-09-09 21:43:49 +000038STATISTIC(NumCacheNonlocal, "Number of cached non-local responses");
39STATISTIC(NumUncacheNonlocal, "Number of uncached non-local responses");
40
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041char MemoryDependenceAnalysis::ID = 0;
42
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043// Register this pass...
44static RegisterPass<MemoryDependenceAnalysis> X("memdep",
Chris Lattner969470c2008-11-28 21:45:17 +000045 "Memory Dependence Analysis", false, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046
Chris Lattner969470c2008-11-28 21:45:17 +000047/// verifyRemoved - Verify that the specified instruction does not occur
48/// in our internal data structures.
Chris Lattner48b24692008-11-28 21:42:09 +000049void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const {
Chris Lattnerfd9b56d2008-11-29 01:43:36 +000050 for (LocalDepMapType::const_iterator I = LocalDeps.begin(),
51 E = LocalDeps.end(); I != E; ++I) {
Chris Lattner969470c2008-11-28 21:45:17 +000052 assert(I->first != D && "Inst occurs in data structures");
Chris Lattnercb53af02008-11-29 03:22:12 +000053 assert(I->second.getPointer() != D &&
Chris Lattnerfd9b56d2008-11-29 01:43:36 +000054 "Inst occurs in data structures");
Owen Andersonc772be72007-12-08 01:37:09 +000055 }
56
Chris Lattner48b24692008-11-28 21:42:09 +000057 for (nonLocalDepMapType::const_iterator I = depGraphNonLocal.begin(),
58 E = depGraphNonLocal.end(); I != E; ++I) {
Chris Lattner969470c2008-11-28 21:45:17 +000059 assert(I->first != D && "Inst occurs in data structures");
Chris Lattnerfd9b56d2008-11-29 01:43:36 +000060 for (DenseMap<BasicBlock*, DepResultTy>::iterator II = I->second.begin(),
Owen Andersonc8f33362008-06-01 20:51:41 +000061 EE = I->second.end(); II != EE; ++II)
Chris Lattnerfd9b56d2008-11-29 01:43:36 +000062 assert(II->second.getPointer() != D && "Inst occurs in data structures");
Owen Andersonc772be72007-12-08 01:37:09 +000063 }
64
Chris Lattner48b24692008-11-28 21:42:09 +000065 for (reverseDepMapType::const_iterator I = reverseDep.begin(),
66 E = reverseDep.end(); I != E; ++I)
67 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
68 EE = I->second.end(); II != EE; ++II)
Chris Lattner969470c2008-11-28 21:45:17 +000069 assert(*II != D && "Inst occurs in data structures");
Owen Andersonc772be72007-12-08 01:37:09 +000070
Chris Lattner48b24692008-11-28 21:42:09 +000071 for (reverseDepMapType::const_iterator I = reverseDepNonLocal.begin(),
72 E = reverseDepNonLocal.end();
Owen Andersonc772be72007-12-08 01:37:09 +000073 I != E; ++I)
Chris Lattner48b24692008-11-28 21:42:09 +000074 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
75 EE = I->second.end(); II != EE; ++II)
Chris Lattner969470c2008-11-28 21:45:17 +000076 assert(*II != D && "Inst occurs in data structures");
Owen Andersonc772be72007-12-08 01:37:09 +000077}
78
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079/// getAnalysisUsage - Does not modify anything. It uses Alias Analysis.
80///
81void MemoryDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
82 AU.setPreservesAll();
83 AU.addRequiredTransitive<AliasAnalysis>();
84 AU.addRequiredTransitive<TargetData>();
85}
86
Owen Anderson3de3c532007-08-08 22:26:03 +000087/// getCallSiteDependency - Private helper for finding the local dependencies
88/// of a call site.
Chris Lattner12cafbf2008-11-29 02:29:27 +000089MemDepResult MemoryDependenceAnalysis::
Chris Lattnerfd9b56d2008-11-29 01:43:36 +000090getCallSiteDependency(CallSite C, Instruction *start, BasicBlock *block) {
Chris Lattnercb53af02008-11-29 03:22:12 +000091 DepResultTy &cachedResult = LocalDeps[C.getInstruction()];
92 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
93 TargetData &TD = getAnalysis<TargetData>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 BasicBlock::iterator blockBegin = C.getInstruction()->getParent()->begin();
95 BasicBlock::iterator QI = C.getInstruction();
96
Duncan Sands4b01c632008-09-11 19:41:10 +000097 // If the starting point was specified, use it
Owen Andersone84f4bc2007-08-07 00:33:45 +000098 if (start) {
99 QI = start;
Dan Gohmand4752682008-03-31 22:08:00 +0000100 blockBegin = start->getParent()->begin();
Owen Anderson3de3c532007-08-08 22:26:03 +0000101 // If the starting point wasn't specified, but the block was, use it
Owen Andersone84f4bc2007-08-07 00:33:45 +0000102 } else if (!start && block) {
103 QI = block->end();
Dan Gohmand4752682008-03-31 22:08:00 +0000104 blockBegin = block->begin();
Owen Andersone84f4bc2007-08-07 00:33:45 +0000105 }
106
Owen Anderson3de3c532007-08-08 22:26:03 +0000107 // Walk backwards through the block, looking for dependencies
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108 while (QI != blockBegin) {
109 --QI;
110
111 // If this inst is a memory op, get the pointer it accessed
112 Value* pointer = 0;
113 uint64_t pointerSize = 0;
114 if (StoreInst* S = dyn_cast<StoreInst>(QI)) {
115 pointer = S->getPointerOperand();
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000116 pointerSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 } else if (AllocationInst* AI = dyn_cast<AllocationInst>(QI)) {
118 pointer = AI;
119 if (ConstantInt* C = dyn_cast<ConstantInt>(AI->getArraySize()))
Chris Lattner39bc72f2008-11-28 21:16:44 +0000120 pointerSize = C->getZExtValue() *
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000121 TD.getABITypeSize(AI->getAllocatedType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 else
123 pointerSize = ~0UL;
124 } else if (VAArgInst* V = dyn_cast<VAArgInst>(QI)) {
125 pointer = V->getOperand(0);
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000126 pointerSize = TD.getTypeStoreSize(V->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 } else if (FreeInst* F = dyn_cast<FreeInst>(QI)) {
128 pointer = F->getPointerOperand();
129
130 // FreeInsts erase the entire structure
131 pointerSize = ~0UL;
Owen Andersona10020b2008-05-13 21:25:37 +0000132 } else if (CallSite::get(QI).getInstruction() != 0) {
Owen Anderson8b6f04e2007-11-26 02:26:36 +0000133 AliasAnalysis::ModRefBehavior result =
Duncan Sands00b24b52007-12-01 07:51:45 +0000134 AA.getModRefBehavior(CallSite::get(QI));
Owen Andersona0b2b8e2008-04-17 05:36:50 +0000135 if (result != AliasAnalysis::DoesNotAccessMemory) {
Owen Andersone84f4bc2007-08-07 00:33:45 +0000136 if (!start && !block) {
Chris Lattnercb53af02008-11-29 03:22:12 +0000137 cachedResult = DepResultTy(QI, Normal);
138 reverseDep[QI].insert(C.getInstruction());
Owen Andersone84f4bc2007-08-07 00:33:45 +0000139 }
Chris Lattner12cafbf2008-11-29 02:29:27 +0000140 return MemDepResult::get(QI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 } else {
142 continue;
143 }
144 } else
145 continue;
146
147 if (AA.getModRefInfo(C, pointer, pointerSize) != AliasAnalysis::NoModRef) {
Owen Andersone84f4bc2007-08-07 00:33:45 +0000148 if (!start && !block) {
Chris Lattnercb53af02008-11-29 03:22:12 +0000149 cachedResult = DepResultTy(QI, Normal);
150 reverseDep[QI].insert(C.getInstruction());
Owen Andersone84f4bc2007-08-07 00:33:45 +0000151 }
Chris Lattner12cafbf2008-11-29 02:29:27 +0000152 return MemDepResult::get(QI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 }
154 }
155
156 // No dependence found
Chris Lattnercb53af02008-11-29 03:22:12 +0000157 cachedResult = DepResultTy(0, NonLocal);
Chris Lattner12cafbf2008-11-29 02:29:27 +0000158 return MemDepResult::getNonLocal();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159}
160
Owen Anderson3de3c532007-08-08 22:26:03 +0000161/// nonLocalHelper - Private helper used to calculate non-local dependencies
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000162/// by doing DFS on the predecessors of a block to find its dependencies.
Owen Anderson3f75d122007-08-01 22:01:54 +0000163void MemoryDependenceAnalysis::nonLocalHelper(Instruction* query,
Owen Anderson5d72a422007-07-25 19:57:03 +0000164 BasicBlock* block,
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000165 DenseMap<BasicBlock*, DepResultTy> &resp) {
Owen Anderson3de3c532007-08-08 22:26:03 +0000166 // Set of blocks that we've already visited in our DFS
Owen Anderson3f75d122007-08-01 22:01:54 +0000167 SmallPtrSet<BasicBlock*, 4> visited;
Owen Anderson05749072007-09-21 03:53:52 +0000168 // If we're updating a dirtied cache entry, we don't need to reprocess
169 // already computed entries.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000170 for (DenseMap<BasicBlock*, DepResultTy>::iterator I = resp.begin(),
Owen Anderson05749072007-09-21 03:53:52 +0000171 E = resp.end(); I != E; ++I)
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000172 if (I->second.getInt() != Dirty)
Owen Anderson05749072007-09-21 03:53:52 +0000173 visited.insert(I->first);
174
Owen Anderson3de3c532007-08-08 22:26:03 +0000175 // Current stack of the DFS
Owen Anderson3f75d122007-08-01 22:01:54 +0000176 SmallVector<BasicBlock*, 4> stack;
Owen Anderson2fecdf12008-04-10 22:13:32 +0000177 for (pred_iterator PI = pred_begin(block), PE = pred_end(block);
178 PI != PE; ++PI)
179 stack.push_back(*PI);
Owen Anderson4c295472007-07-24 21:52:37 +0000180
Owen Anderson3de3c532007-08-08 22:26:03 +0000181 // Do a basic DFS
Owen Anderson3f75d122007-08-01 22:01:54 +0000182 while (!stack.empty()) {
183 BasicBlock* BB = stack.back();
184
Owen Anderson3de3c532007-08-08 22:26:03 +0000185 // If we've already visited this block, no need to revist
Owen Andersonc6a31b92007-08-02 17:56:05 +0000186 if (visited.count(BB)) {
Owen Anderson3f75d122007-08-01 22:01:54 +0000187 stack.pop_back();
188 continue;
189 }
190
Owen Anderson3de3c532007-08-08 22:26:03 +0000191 // If we find a new block with a local dependency for query,
192 // then we insert the new dependency and backtrack.
Owen Anderson3f75d122007-08-01 22:01:54 +0000193 if (BB != block) {
Owen Andersonc6a31b92007-08-02 17:56:05 +0000194 visited.insert(BB);
195
Chris Lattner12cafbf2008-11-29 02:29:27 +0000196 MemDepResult localDep = getDependency(query, 0, BB);
197 if (!localDep.isNonLocal()) {
198 resp.insert(std::make_pair(BB, ConvFromResult(localDep)));
Owen Andersonc6a31b92007-08-02 17:56:05 +0000199 stack.pop_back();
Owen Anderson3f75d122007-08-01 22:01:54 +0000200 continue;
201 }
Owen Anderson3de3c532007-08-08 22:26:03 +0000202 // If we re-encounter the starting block, we still need to search it
203 // because there might be a dependency in the starting block AFTER
204 // the position of the query. This is necessary to get loops right.
Owen Anderson2fecdf12008-04-10 22:13:32 +0000205 } else if (BB == block) {
Owen Andersonc6a31b92007-08-02 17:56:05 +0000206 visited.insert(BB);
207
Chris Lattner12cafbf2008-11-29 02:29:27 +0000208 MemDepResult localDep = getDependency(query, 0, BB);
209 if (localDep.getInst() != query)
210 resp.insert(std::make_pair(BB, ConvFromResult(localDep)));
Owen Andersonc6a31b92007-08-02 17:56:05 +0000211
212 stack.pop_back();
Owen Andersonc6a31b92007-08-02 17:56:05 +0000213 continue;
Owen Anderson3f75d122007-08-01 22:01:54 +0000214 }
215
Owen Anderson3de3c532007-08-08 22:26:03 +0000216 // If we didn't find anything, recurse on the precessors of this block
Tanya Lattner8edb2b72008-02-06 00:54:55 +0000217 // Only do this for blocks with a small number of predecessors.
Owen Anderson3f75d122007-08-01 22:01:54 +0000218 bool predOnStack = false;
219 bool inserted = false;
Tanya Lattner8edb2b72008-02-06 00:54:55 +0000220 if (std::distance(pred_begin(BB), pred_end(BB)) <= PredLimit) {
221 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
222 PI != PE; ++PI)
223 if (!visited.count(*PI)) {
224 stack.push_back(*PI);
225 inserted = true;
226 } else
227 predOnStack = true;
228 }
Owen Anderson3f75d122007-08-01 22:01:54 +0000229
Owen Anderson3de3c532007-08-08 22:26:03 +0000230 // If we inserted a new predecessor, then we'll come back to this block
Owen Anderson3f75d122007-08-01 22:01:54 +0000231 if (inserted)
232 continue;
Owen Anderson3de3c532007-08-08 22:26:03 +0000233 // If we didn't insert because we have no predecessors, then this
234 // query has no dependency at all.
Owen Anderson3f75d122007-08-01 22:01:54 +0000235 else if (!inserted && !predOnStack) {
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000236 resp.insert(std::make_pair(BB, DepResultTy(0, None)));
Owen Anderson3de3c532007-08-08 22:26:03 +0000237 // If we didn't insert because our predecessors are already on the stack,
238 // then we might still have a dependency, but it will be discovered during
239 // backtracking.
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, NonLocal)));
Owen Anderson3f75d122007-08-01 22:01:54 +0000242 }
243
244 stack.pop_back();
Owen Anderson4c295472007-07-24 21:52:37 +0000245 }
Owen Anderson4c295472007-07-24 21:52:37 +0000246}
247
Owen Andersonafe840e2007-08-08 22:01:54 +0000248/// getNonLocalDependency - Fills the passed-in map with the non-local
249/// dependencies of the queries. The map will contain NonLocal for
250/// blocks between the query and its dependencies.
Owen Anderson3f75d122007-08-01 22:01:54 +0000251void MemoryDependenceAnalysis::getNonLocalDependency(Instruction* query,
Chris Lattner12cafbf2008-11-29 02:29:27 +0000252 DenseMap<BasicBlock*, MemDepResult> &resp) {
Owen Anderson2bd46a52007-08-16 21:27:05 +0000253 if (depGraphNonLocal.count(query)) {
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000254 DenseMap<BasicBlock*, DepResultTy> &cached = depGraphNonLocal[query];
Owen Andersond6c7fea2007-09-09 21:43:49 +0000255 NumCacheNonlocal++;
Owen Anderson05749072007-09-21 03:53:52 +0000256
257 SmallVector<BasicBlock*, 4> dirtied;
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000258 for (DenseMap<BasicBlock*, DepResultTy>::iterator I = cached.begin(),
Owen Anderson05749072007-09-21 03:53:52 +0000259 E = cached.end(); I != E; ++I)
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000260 if (I->second.getInt() == Dirty)
Owen Anderson05749072007-09-21 03:53:52 +0000261 dirtied.push_back(I->first);
262
263 for (SmallVector<BasicBlock*, 4>::iterator I = dirtied.begin(),
264 E = dirtied.end(); I != E; ++I) {
Chris Lattner12cafbf2008-11-29 02:29:27 +0000265 MemDepResult localDep = getDependency(query, 0, *I);
266 if (!localDep.isNonLocal())
267 cached[*I] = ConvFromResult(localDep);
Owen Anderson05749072007-09-21 03:53:52 +0000268 else {
269 cached.erase(*I);
270 nonLocalHelper(query, *I, cached);
271 }
272 }
273
Chris Lattner12cafbf2008-11-29 02:29:27 +0000274 // Update the reverse non-local dependency cache.
275 for (DenseMap<BasicBlock*, DepResultTy>::iterator I = cached.begin(),
276 E = cached.end(); I != E; ++I) {
Chris Lattnercb53af02008-11-29 03:22:12 +0000277 if (Instruction *Inst = I->second.getPointer())
278 reverseDepNonLocal[Inst].insert(query);
Chris Lattner12cafbf2008-11-29 02:29:27 +0000279 resp[I->first] = ConvToResult(I->second);
280 }
Owen Andersondf3055a2008-06-01 21:03:52 +0000281
Owen Anderson2bd46a52007-08-16 21:27:05 +0000282 return;
Chris Lattner12cafbf2008-11-29 02:29:27 +0000283 }
284
285 NumUncacheNonlocal++;
Owen Anderson2bd46a52007-08-16 21:27:05 +0000286
Owen Andersond6c7fea2007-09-09 21:43:49 +0000287 // If not, go ahead and search for non-local deps.
Chris Lattner12cafbf2008-11-29 02:29:27 +0000288 DenseMap<BasicBlock*, DepResultTy> &cached = depGraphNonLocal[query];
289 nonLocalHelper(query, query->getParent(), cached);
290
Owen Anderson2bd46a52007-08-16 21:27:05 +0000291 // Update the non-local dependency cache
Chris Lattner12cafbf2008-11-29 02:29:27 +0000292 for (DenseMap<BasicBlock*, DepResultTy>::iterator I = cached.begin(),
293 E = cached.end(); I != E; ++I) {
294 // FIXME: Merge with the code above!
Chris Lattnercb53af02008-11-29 03:22:12 +0000295 if (Instruction *Inst = I->second.getPointer())
296 reverseDepNonLocal[Inst].insert(query);
Chris Lattner12cafbf2008-11-29 02:29:27 +0000297 resp[I->first] = ConvToResult(I->second);
Owen Anderson2bd46a52007-08-16 21:27:05 +0000298 }
Owen Anderson4c295472007-07-24 21:52:37 +0000299}
300
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301/// getDependency - Return the instruction on which a memory operation
Dan Gohmanf1f99a22008-04-10 23:02:38 +0000302/// depends. The local parameter indicates if the query should only
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000303/// evaluate dependencies within the same basic block.
Chris Lattnercb53af02008-11-29 03:22:12 +0000304/// FIXME: ELIMINATE START/BLOCK and make the caching happen in a higher level
305/// METHOD.
Chris Lattner12cafbf2008-11-29 02:29:27 +0000306MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *query,
307 Instruction *start,
308 BasicBlock *block) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309 // Start looking for dependencies with the queried inst
310 BasicBlock::iterator QI = query;
311
312 // Check for a cached result
Chris Lattnercb53af02008-11-29 03:22:12 +0000313 // FIXME: why do this when Block or Start is specified??
314 DepResultTy &cachedResult = LocalDeps[query];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315
316 if (start)
317 QI = start;
Chris Lattnercb53af02008-11-29 03:22:12 +0000318 else if (block)
Owen Anderson5d72a422007-07-25 19:57:03 +0000319 QI = block->end();
Chris Lattnercb53af02008-11-29 03:22:12 +0000320 else if (cachedResult.getInt() != Dirty) {
321 // If we have a _confirmed_ cached entry, return it.
322 return ConvToResult(cachedResult);
323 } else if (Instruction *Inst = cachedResult.getPointer()) {
324 // If we have an unconfirmed cached entry, we can start our search from it.
325 QI = Inst;
326 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327
328 AliasAnalysis& AA = getAnalysis<AliasAnalysis>();
329 TargetData& TD = getAnalysis<TargetData>();
330
331 // Get the pointer value for which dependence will be determined
332 Value* dependee = 0;
333 uint64_t dependeeSize = 0;
334 bool queryIsVolatile = false;
335 if (StoreInst* S = dyn_cast<StoreInst>(query)) {
336 dependee = S->getPointerOperand();
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000337 dependeeSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338 queryIsVolatile = S->isVolatile();
339 } else if (LoadInst* L = dyn_cast<LoadInst>(query)) {
340 dependee = L->getPointerOperand();
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000341 dependeeSize = TD.getTypeStoreSize(L->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000342 queryIsVolatile = L->isVolatile();
343 } else if (VAArgInst* V = dyn_cast<VAArgInst>(query)) {
344 dependee = V->getOperand(0);
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000345 dependeeSize = TD.getTypeStoreSize(V->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000346 } else if (FreeInst* F = dyn_cast<FreeInst>(query)) {
347 dependee = F->getPointerOperand();
348
349 // FreeInsts erase the entire structure, not just a field
350 dependeeSize = ~0UL;
351 } else if (CallSite::get(query).getInstruction() != 0)
Owen Andersone84f4bc2007-08-07 00:33:45 +0000352 return getCallSiteDependency(CallSite::get(query), start, block);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 else if (isa<AllocationInst>(query))
Chris Lattner12cafbf2008-11-29 02:29:27 +0000354 return MemDepResult::getNone();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000355 else
Chris Lattner12cafbf2008-11-29 02:29:27 +0000356 return MemDepResult::getNone();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000357
Owen Anderson4c295472007-07-24 21:52:37 +0000358 BasicBlock::iterator blockBegin = block ? block->begin()
359 : query->getParent()->begin();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000360
Owen Anderson3de3c532007-08-08 22:26:03 +0000361 // Walk backwards through the basic block, looking for dependencies
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000362 while (QI != blockBegin) {
363 --QI;
364
365 // If this inst is a memory op, get the pointer it accessed
366 Value* pointer = 0;
367 uint64_t pointerSize = 0;
368 if (StoreInst* S = dyn_cast<StoreInst>(QI)) {
369 // All volatile loads/stores depend on each other
370 if (queryIsVolatile && S->isVolatile()) {
Owen Andersone84f4bc2007-08-07 00:33:45 +0000371 if (!start && !block) {
Chris Lattnercb53af02008-11-29 03:22:12 +0000372 cachedResult = DepResultTy(S, Normal);
373 reverseDep[S].insert(query);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374 }
375
Chris Lattner12cafbf2008-11-29 02:29:27 +0000376 return MemDepResult::get(S);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377 }
378
379 pointer = S->getPointerOperand();
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000380 pointerSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000381 } else if (LoadInst* L = dyn_cast<LoadInst>(QI)) {
382 // All volatile loads/stores depend on each other
383 if (queryIsVolatile && L->isVolatile()) {
Owen Andersone84f4bc2007-08-07 00:33:45 +0000384 if (!start && !block) {
Chris Lattnercb53af02008-11-29 03:22:12 +0000385 cachedResult = DepResultTy(L, Normal);
386 reverseDep[L].insert(query);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387 }
388
Chris Lattner12cafbf2008-11-29 02:29:27 +0000389 return MemDepResult::get(L);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000390 }
391
392 pointer = L->getPointerOperand();
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000393 pointerSize = TD.getTypeStoreSize(L->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394 } else if (AllocationInst* AI = dyn_cast<AllocationInst>(QI)) {
395 pointer = AI;
396 if (ConstantInt* C = dyn_cast<ConstantInt>(AI->getArraySize()))
Chris Lattner39bc72f2008-11-28 21:16:44 +0000397 pointerSize = C->getZExtValue() *
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000398 TD.getABITypeSize(AI->getAllocatedType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399 else
400 pointerSize = ~0UL;
401 } else if (VAArgInst* V = dyn_cast<VAArgInst>(QI)) {
402 pointer = V->getOperand(0);
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000403 pointerSize = TD.getTypeStoreSize(V->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404 } else if (FreeInst* F = dyn_cast<FreeInst>(QI)) {
405 pointer = F->getPointerOperand();
406
407 // FreeInsts erase the entire structure
408 pointerSize = ~0UL;
409 } else if (CallSite::get(QI).getInstruction() != 0) {
Owen Andersonafe840e2007-08-08 22:01:54 +0000410 // Call insts need special handling. Check if they can modify our pointer
Owen Anderson151f7692007-08-06 23:26:03 +0000411 AliasAnalysis::ModRefResult MR = AA.getModRefInfo(CallSite::get(QI),
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000412 dependee, dependeeSize);
Owen Anderson151f7692007-08-06 23:26:03 +0000413
414 if (MR != AliasAnalysis::NoModRef) {
415 // Loads don't depend on read-only calls
416 if (isa<LoadInst>(query) && MR == AliasAnalysis::Ref)
417 continue;
418
Owen Andersone84f4bc2007-08-07 00:33:45 +0000419 if (!start && !block) {
Chris Lattnercb53af02008-11-29 03:22:12 +0000420 cachedResult = DepResultTy(QI, Normal);
421 reverseDep[QI].insert(query);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000422 }
Chris Lattner12cafbf2008-11-29 02:29:27 +0000423 return MemDepResult::get(QI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000424 } else {
425 continue;
426 }
427 }
428
429 // If we found a pointer, check if it could be the same as our pointer
430 if (pointer) {
431 AliasAnalysis::AliasResult R = AA.alias(pointer, pointerSize,
432 dependee, dependeeSize);
433
434 if (R != AliasAnalysis::NoAlias) {
Owen Anderson151f7692007-08-06 23:26:03 +0000435 // May-alias loads don't depend on each other
436 if (isa<LoadInst>(query) && isa<LoadInst>(QI) &&
437 R == AliasAnalysis::MayAlias)
438 continue;
439
Owen Andersone84f4bc2007-08-07 00:33:45 +0000440 if (!start && !block) {
Chris Lattnercb53af02008-11-29 03:22:12 +0000441 cachedResult = DepResultTy(QI, Normal);
442 reverseDep[QI].insert(query);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000443 }
444
Chris Lattner12cafbf2008-11-29 02:29:27 +0000445 return MemDepResult::get(QI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000446 }
447 }
448 }
449
450 // If we found nothing, return the non-local flag
Chris Lattnercb53af02008-11-29 03:22:12 +0000451 if (!start && !block)
452 cachedResult = DepResultTy(0, NonLocal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453
Chris Lattner12cafbf2008-11-29 02:29:27 +0000454 return MemDepResult::getNonLocal();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455}
456
Owen Anderson8d272d52008-02-12 21:15:18 +0000457/// dropInstruction - Remove an instruction from the analysis, making
458/// absolutely conservative assumptions when updating the cache. This is
459/// useful, for example when an instruction is changed rather than removed.
460void MemoryDependenceAnalysis::dropInstruction(Instruction* drop) {
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000461 LocalDepMapType::iterator depGraphEntry = LocalDeps.find(drop);
462 if (depGraphEntry != LocalDeps.end())
Chris Lattnercb53af02008-11-29 03:22:12 +0000463 if (Instruction *Inst = depGraphEntry->second.getPointer())
464 reverseDep[Inst].erase(drop);
Owen Anderson8d272d52008-02-12 21:15:18 +0000465
466 // Drop dependency information for things that depended on this instr
Chris Lattnercb53af02008-11-29 03:22:12 +0000467 SmallPtrSet<Instruction*, 4>& set = reverseDep[drop];
Owen Anderson8d272d52008-02-12 21:15:18 +0000468 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
469 I != E; ++I)
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000470 LocalDeps.erase(*I);
Owen Anderson8d272d52008-02-12 21:15:18 +0000471
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000472 LocalDeps.erase(drop);
Chris Lattnercb53af02008-11-29 03:22:12 +0000473 reverseDep.erase(drop);
Owen Anderson8d272d52008-02-12 21:15:18 +0000474
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000475 for (DenseMap<BasicBlock*, DepResultTy>::iterator DI =
476 depGraphNonLocal[drop].begin(), DE = depGraphNonLocal[drop].end();
Owen Anderson8d272d52008-02-12 21:15:18 +0000477 DI != DE; ++DI)
Chris Lattnercb53af02008-11-29 03:22:12 +0000478 if (Instruction *Inst = DI->second.getPointer())
479 reverseDepNonLocal[Inst].erase(drop);
Owen Anderson8d272d52008-02-12 21:15:18 +0000480
Chris Lattnercb53af02008-11-29 03:22:12 +0000481 if (reverseDepNonLocal.count(drop)) {
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000482 SmallPtrSet<Instruction*, 4>& set =
Chris Lattnercb53af02008-11-29 03:22:12 +0000483 reverseDepNonLocal[drop];
Owen Anderson8d272d52008-02-12 21:15:18 +0000484 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
485 I != E; ++I)
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000486 for (DenseMap<BasicBlock*, DepResultTy>::iterator DI =
Owen Anderson8d272d52008-02-12 21:15:18 +0000487 depGraphNonLocal[*I].begin(), DE = depGraphNonLocal[*I].end();
488 DI != DE; ++DI)
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000489 if (DI->second == DepResultTy(drop, Normal))
Chris Lattnercb53af02008-11-29 03:22:12 +0000490 // FIXME: Why not remember the old insertion point??
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000491 DI->second = DepResultTy(0, Dirty);
Owen Anderson8d272d52008-02-12 21:15:18 +0000492 }
493
Chris Lattnercb53af02008-11-29 03:22:12 +0000494 reverseDepNonLocal.erase(drop);
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000495 depGraphNonLocal.erase(drop);
Owen Anderson8d272d52008-02-12 21:15:18 +0000496}
497
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000498/// removeInstruction - Remove an instruction from the dependence analysis,
499/// updating the dependence of instructions that previously depended on it.
Owen Anderson3de3c532007-08-08 22:26:03 +0000500/// This method attempts to keep the cache coherent using the reverse map.
Chris Lattner1b185de2008-11-28 22:04:47 +0000501void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) {
Chris Lattner1b185de2008-11-28 22:04:47 +0000502 // Walk through the Non-local dependencies, removing this one as the value
503 // for any cached queries.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000504 for (DenseMap<BasicBlock*, DepResultTy>::iterator DI =
Chris Lattner1b185de2008-11-28 22:04:47 +0000505 depGraphNonLocal[RemInst].begin(), DE = depGraphNonLocal[RemInst].end();
Owen Andersonc772be72007-12-08 01:37:09 +0000506 DI != DE; ++DI)
Chris Lattnercb53af02008-11-29 03:22:12 +0000507 if (Instruction *Inst = DI->second.getPointer())
508 reverseDepNonLocal[Inst].erase(RemInst);
Owen Andersonc772be72007-12-08 01:37:09 +0000509
Chris Lattner52638032008-11-28 22:28:27 +0000510 // Shortly after this, we will look for things that depend on RemInst. In
511 // order to update these, we'll need a new dependency to base them on. We
512 // could completely delete any entries that depend on this, but it is better
513 // to make a more accurate approximation where possible. Compute that better
514 // approximation if we can.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000515 DepResultTy NewDependency;
Chris Lattner52638032008-11-28 22:28:27 +0000516
Chris Lattner1b185de2008-11-28 22:04:47 +0000517 // If we have a cached local dependence query for this instruction, remove it.
Chris Lattner52638032008-11-28 22:28:27 +0000518 //
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000519 LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst);
520 if (LocalDepEntry != LocalDeps.end()) {
Chris Lattnercb53af02008-11-29 03:22:12 +0000521 DepResultTy LocalDep = LocalDepEntry->second;
Owen Anderson25296a22008-01-30 01:24:05 +0000522
Chris Lattner52638032008-11-28 22:28:27 +0000523 // Remove this local dependency info.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000524 LocalDeps.erase(LocalDepEntry);
Chris Lattner1b185de2008-11-28 22:04:47 +0000525
Chris Lattner52638032008-11-28 22:28:27 +0000526 // Remove us from DepInst's reverse set now that the local dep info is gone.
Chris Lattnercb53af02008-11-29 03:22:12 +0000527 if (Instruction *Inst = LocalDep.getPointer())
528 reverseDep[Inst].erase(RemInst);
Chris Lattner52638032008-11-28 22:28:27 +0000529
530 // If we have unconfirmed info, don't trust it.
Chris Lattnercb53af02008-11-29 03:22:12 +0000531 if (LocalDep.getInt() != Dirty) {
Chris Lattner52638032008-11-28 22:28:27 +0000532 // If we have a confirmed non-local flag, use it.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000533 if (LocalDep.getInt() == NonLocal || LocalDep.getInt() == None) {
Chris Lattner89fbbe72008-11-28 22:51:08 +0000534 // The only time this dependency is confirmed is if it is non-local.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000535 NewDependency = LocalDep;
Chris Lattner52638032008-11-28 22:28:27 +0000536 } else {
537 // If we have dep info for RemInst, set them to it.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000538 Instruction *NDI = next(BasicBlock::iterator(LocalDep.getPointer()));
539 if (NDI != RemInst) // Don't use RemInst for the new dependency!
Chris Lattnercb53af02008-11-29 03:22:12 +0000540 NewDependency = DepResultTy(NDI, Dirty);
Chris Lattner52638032008-11-28 22:28:27 +0000541 }
David Greene701171c2007-07-31 20:01:27 +0000542 }
Owen Anderson6487cf52008-02-05 04:34:03 +0000543 }
544
Chris Lattner52638032008-11-28 22:28:27 +0000545 // If we don't already have a local dependency answer for this instruction,
546 // use the immediate successor of RemInst. We use the successor because
547 // getDependence starts by checking the immediate predecessor of what is in
548 // the cache.
Chris Lattnercb53af02008-11-29 03:22:12 +0000549 if (NewDependency == DepResultTy(0, Dirty))
550 NewDependency = DepResultTy(next(BasicBlock::iterator(RemInst)), Dirty);
Chris Lattner52638032008-11-28 22:28:27 +0000551
Chris Lattner89fbbe72008-11-28 22:51:08 +0000552 // Loop over all of the things that depend on the instruction we're removing.
553 //
Chris Lattnercb53af02008-11-29 03:22:12 +0000554 reverseDepMapType::iterator ReverseDepIt = reverseDep.find(RemInst);
Chris Lattner89fbbe72008-11-28 22:51:08 +0000555 if (ReverseDepIt != reverseDep.end()) {
556 SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second;
557 for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(),
558 E = ReverseDeps.end(); I != E; ++I) {
559 Instruction *InstDependingOnRemInst = *I;
560
561 // If we thought the instruction depended on itself (possible for
562 // unconfirmed dependencies) ignore the update.
563 if (InstDependingOnRemInst == RemInst) continue;
564
565 // Insert the new dependencies.
Chris Lattnercb53af02008-11-29 03:22:12 +0000566 LocalDeps[InstDependingOnRemInst] = NewDependency;
Chris Lattner89fbbe72008-11-28 22:51:08 +0000567
568 // If our NewDependency is an instruction, make sure to remember that new
569 // things depend on it.
Chris Lattnercb53af02008-11-29 03:22:12 +0000570 if (Instruction *Inst = NewDependency.getPointer())
571 reverseDep[Inst].insert(InstDependingOnRemInst);
Chris Lattner89fbbe72008-11-28 22:51:08 +0000572 }
Chris Lattnercb53af02008-11-29 03:22:12 +0000573 reverseDep.erase(RemInst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000574 }
Owen Anderson2bd46a52007-08-16 21:27:05 +0000575
Chris Lattnercb53af02008-11-29 03:22:12 +0000576 ReverseDepIt = reverseDepNonLocal.find(RemInst);
Chris Lattner89fbbe72008-11-28 22:51:08 +0000577 if (ReverseDepIt != reverseDepNonLocal.end()) {
578 SmallPtrSet<Instruction*, 4>& set = ReverseDepIt->second;
Owen Anderson2bd46a52007-08-16 21:27:05 +0000579 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
580 I != E; ++I)
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000581 for (DenseMap<BasicBlock*, DepResultTy>::iterator DI =
Owen Anderson05749072007-09-21 03:53:52 +0000582 depGraphNonLocal[*I].begin(), DE = depGraphNonLocal[*I].end();
583 DI != DE; ++DI)
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000584 if (DI->second == DepResultTy(RemInst, Normal))
Chris Lattnercb53af02008-11-29 03:22:12 +0000585 // FIXME: Why not remember the old insertion point??
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000586 DI->second = DepResultTy(0, Dirty);
587 reverseDepNonLocal.erase(ReverseDepIt);
Owen Anderson2bd46a52007-08-16 21:27:05 +0000588 }
Owen Andersonc772be72007-12-08 01:37:09 +0000589
Chris Lattner1b185de2008-11-28 22:04:47 +0000590 depGraphNonLocal.erase(RemInst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000591
Chris Lattner1b185de2008-11-28 22:04:47 +0000592 getAnalysis<AliasAnalysis>().deleteValue(RemInst);
Chris Lattner969470c2008-11-28 21:45:17 +0000593
Chris Lattner1b185de2008-11-28 22:04:47 +0000594 DEBUG(verifyRemoved(RemInst));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595}