blob: 0dea0696e04578c3dd6aa16854bf250ba1aaba01 [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
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 Anderson4c295472007-07-24 21:52:37 +000022#include "llvm/Support/CFG.h"
Tanya Lattner8edb2b72008-02-06 00:54:55 +000023#include "llvm/Support/CommandLine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024#include "llvm/Target/TargetData.h"
Owen Andersond6c7fea2007-09-09 21:43:49 +000025#include "llvm/ADT/Statistic.h"
26
27#define DEBUG_TYPE "memdep"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028
29using 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
Owen Anderson935e39b2007-08-09 04:42:44 +000043Instruction* const MemoryDependenceAnalysis::NonLocal = (Instruction*)-3;
44Instruction* const MemoryDependenceAnalysis::None = (Instruction*)-4;
Owen Anderson7e447562007-09-19 16:13:57 +000045Instruction* const MemoryDependenceAnalysis::Dirty = (Instruction*)-5;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046
47// Register this pass...
48static RegisterPass<MemoryDependenceAnalysis> X("memdep",
Devang Patelbdfd1862008-03-20 02:25:21 +000049 "Memory Dependence Analysis", false, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050
Chris Lattner48b24692008-11-28 21:42:09 +000051void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const {
52 for (depMapType::const_iterator I = depGraphLocal.begin(),
53 E = depGraphLocal.end(); I != E; ++I) {
Owen Andersonc772be72007-12-08 01:37:09 +000054 assert(I->first != D);
55 assert(I->second.first != D);
56 }
57
Chris Lattner48b24692008-11-28 21:42:09 +000058 for (nonLocalDepMapType::const_iterator I = depGraphNonLocal.begin(),
59 E = depGraphNonLocal.end(); I != E; ++I) {
Owen Andersonc772be72007-12-08 01:37:09 +000060 assert(I->first != D);
Owen Andersonc8f33362008-06-01 20:51:41 +000061 for (DenseMap<BasicBlock*, Value*>::iterator II = I->second.begin(),
62 EE = I->second.end(); II != EE; ++II)
63 assert(II->second != D);
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)
Owen Andersonc772be72007-12-08 01:37:09 +000070 assert(*II != D);
71
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)
Owen Andersonc772be72007-12-08 01:37:09 +000077 assert(*II != D);
78}
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.
Owen Anderson935e39b2007-08-09 04:42:44 +000090Instruction* MemoryDependenceAnalysis::getCallSiteDependency(CallSite C,
Owen Andersonafe840e2007-08-08 22:01:54 +000091 Instruction* start,
92 BasicBlock* block) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093
Owen Andersonc772be72007-12-08 01:37:09 +000094 std::pair<Instruction*, bool>& cachedResult =
95 depGraphLocal[C.getInstruction()];
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 AliasAnalysis& AA = getAnalysis<AliasAnalysis>();
97 TargetData& TD = getAnalysis<TargetData>();
98 BasicBlock::iterator blockBegin = C.getInstruction()->getParent()->begin();
99 BasicBlock::iterator QI = C.getInstruction();
100
Duncan Sands4b01c632008-09-11 19:41:10 +0000101 // If the starting point was specified, use it
Owen Andersone84f4bc2007-08-07 00:33:45 +0000102 if (start) {
103 QI = start;
Dan Gohmand4752682008-03-31 22:08:00 +0000104 blockBegin = start->getParent()->begin();
Owen Anderson3de3c532007-08-08 22:26:03 +0000105 // If the starting point wasn't specified, but the block was, use it
Owen Andersone84f4bc2007-08-07 00:33:45 +0000106 } else if (!start && block) {
107 QI = block->end();
Dan Gohmand4752682008-03-31 22:08:00 +0000108 blockBegin = block->begin();
Owen Andersone84f4bc2007-08-07 00:33:45 +0000109 }
110
Owen Anderson3de3c532007-08-08 22:26:03 +0000111 // Walk backwards through the block, looking for dependencies
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 while (QI != blockBegin) {
113 --QI;
114
115 // If this inst is a memory op, get the pointer it accessed
116 Value* pointer = 0;
117 uint64_t pointerSize = 0;
118 if (StoreInst* S = dyn_cast<StoreInst>(QI)) {
119 pointer = S->getPointerOperand();
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000120 pointerSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 } else if (AllocationInst* AI = dyn_cast<AllocationInst>(QI)) {
122 pointer = AI;
123 if (ConstantInt* C = dyn_cast<ConstantInt>(AI->getArraySize()))
Chris Lattner39bc72f2008-11-28 21:16:44 +0000124 pointerSize = C->getZExtValue() *
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000125 TD.getABITypeSize(AI->getAllocatedType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 else
127 pointerSize = ~0UL;
128 } else if (VAArgInst* V = dyn_cast<VAArgInst>(QI)) {
129 pointer = V->getOperand(0);
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000130 pointerSize = TD.getTypeStoreSize(V->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 } else if (FreeInst* F = dyn_cast<FreeInst>(QI)) {
132 pointer = F->getPointerOperand();
133
134 // FreeInsts erase the entire structure
135 pointerSize = ~0UL;
Owen Andersona10020b2008-05-13 21:25:37 +0000136 } else if (CallSite::get(QI).getInstruction() != 0) {
Owen Anderson8b6f04e2007-11-26 02:26:36 +0000137 AliasAnalysis::ModRefBehavior result =
Duncan Sands00b24b52007-12-01 07:51:45 +0000138 AA.getModRefBehavior(CallSite::get(QI));
Owen Andersona0b2b8e2008-04-17 05:36:50 +0000139 if (result != AliasAnalysis::DoesNotAccessMemory) {
Owen Andersone84f4bc2007-08-07 00:33:45 +0000140 if (!start && !block) {
Owen Andersonc772be72007-12-08 01:37:09 +0000141 cachedResult.first = QI;
142 cachedResult.second = true;
Owen Andersone84f4bc2007-08-07 00:33:45 +0000143 reverseDep[QI].insert(C.getInstruction());
144 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 return QI;
146 } else {
147 continue;
148 }
149 } else
150 continue;
151
152 if (AA.getModRefInfo(C, pointer, pointerSize) != AliasAnalysis::NoModRef) {
Owen Andersone84f4bc2007-08-07 00:33:45 +0000153 if (!start && !block) {
Owen Andersonc772be72007-12-08 01:37:09 +0000154 cachedResult.first = QI;
155 cachedResult.second = true;
Owen Andersone84f4bc2007-08-07 00:33:45 +0000156 reverseDep[QI].insert(C.getInstruction());
157 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 return QI;
159 }
160 }
161
162 // No dependence found
Owen Andersonc772be72007-12-08 01:37:09 +0000163 cachedResult.first = NonLocal;
164 cachedResult.second = true;
Owen Andersone84f4bc2007-08-07 00:33:45 +0000165 reverseDep[NonLocal].insert(C.getInstruction());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 return NonLocal;
167}
168
Owen Anderson3de3c532007-08-08 22:26:03 +0000169/// nonLocalHelper - Private helper used to calculate non-local dependencies
170/// by doing DFS on the predecessors of a block to find its dependencies
Owen Anderson3f75d122007-08-01 22:01:54 +0000171void MemoryDependenceAnalysis::nonLocalHelper(Instruction* query,
Owen Anderson5d72a422007-07-25 19:57:03 +0000172 BasicBlock* block,
Owen Andersonafe840e2007-08-08 22:01:54 +0000173 DenseMap<BasicBlock*, Value*>& resp) {
Owen Anderson3de3c532007-08-08 22:26:03 +0000174 // Set of blocks that we've already visited in our DFS
Owen Anderson3f75d122007-08-01 22:01:54 +0000175 SmallPtrSet<BasicBlock*, 4> visited;
Owen Anderson05749072007-09-21 03:53:52 +0000176 // If we're updating a dirtied cache entry, we don't need to reprocess
177 // already computed entries.
178 for (DenseMap<BasicBlock*, Value*>::iterator I = resp.begin(),
179 E = resp.end(); I != E; ++I)
180 if (I->second != Dirty)
181 visited.insert(I->first);
182
Owen Anderson3de3c532007-08-08 22:26:03 +0000183 // Current stack of the DFS
Owen Anderson3f75d122007-08-01 22:01:54 +0000184 SmallVector<BasicBlock*, 4> stack;
Owen Anderson2fecdf12008-04-10 22:13:32 +0000185 for (pred_iterator PI = pred_begin(block), PE = pred_end(block);
186 PI != PE; ++PI)
187 stack.push_back(*PI);
Owen Anderson4c295472007-07-24 21:52:37 +0000188
Owen Anderson3de3c532007-08-08 22:26:03 +0000189 // Do a basic DFS
Owen Anderson3f75d122007-08-01 22:01:54 +0000190 while (!stack.empty()) {
191 BasicBlock* BB = stack.back();
192
Owen Anderson3de3c532007-08-08 22:26:03 +0000193 // If we've already visited this block, no need to revist
Owen Andersonc6a31b92007-08-02 17:56:05 +0000194 if (visited.count(BB)) {
Owen Anderson3f75d122007-08-01 22:01:54 +0000195 stack.pop_back();
196 continue;
197 }
198
Owen Anderson3de3c532007-08-08 22:26:03 +0000199 // If we find a new block with a local dependency for query,
200 // then we insert the new dependency and backtrack.
Owen Anderson3f75d122007-08-01 22:01:54 +0000201 if (BB != block) {
Owen Andersonc6a31b92007-08-02 17:56:05 +0000202 visited.insert(BB);
203
Owen Anderson935e39b2007-08-09 04:42:44 +0000204 Instruction* localDep = getDependency(query, 0, BB);
Owen Anderson3f75d122007-08-01 22:01:54 +0000205 if (localDep != NonLocal) {
Owen Anderson935e39b2007-08-09 04:42:44 +0000206 resp.insert(std::make_pair(BB, localDep));
Owen Andersonc6a31b92007-08-02 17:56:05 +0000207 stack.pop_back();
208
Owen Anderson3f75d122007-08-01 22:01:54 +0000209 continue;
210 }
Owen Anderson3de3c532007-08-08 22:26:03 +0000211 // If we re-encounter the starting block, we still need to search it
212 // because there might be a dependency in the starting block AFTER
213 // the position of the query. This is necessary to get loops right.
Owen Anderson2fecdf12008-04-10 22:13:32 +0000214 } else if (BB == block) {
Owen Andersonc6a31b92007-08-02 17:56:05 +0000215 visited.insert(BB);
216
Owen Anderson935e39b2007-08-09 04:42:44 +0000217 Instruction* localDep = getDependency(query, 0, BB);
Owen Andersonc6a31b92007-08-02 17:56:05 +0000218 if (localDep != query)
Owen Anderson935e39b2007-08-09 04:42:44 +0000219 resp.insert(std::make_pair(BB, localDep));
Owen Andersonc6a31b92007-08-02 17:56:05 +0000220
221 stack.pop_back();
222
223 continue;
Owen Anderson3f75d122007-08-01 22:01:54 +0000224 }
225
Owen Anderson3de3c532007-08-08 22:26:03 +0000226 // If we didn't find anything, recurse on the precessors of this block
Tanya Lattner8edb2b72008-02-06 00:54:55 +0000227 // Only do this for blocks with a small number of predecessors.
Owen Anderson3f75d122007-08-01 22:01:54 +0000228 bool predOnStack = false;
229 bool inserted = false;
Tanya Lattner8edb2b72008-02-06 00:54:55 +0000230 if (std::distance(pred_begin(BB), pred_end(BB)) <= PredLimit) {
231 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
232 PI != PE; ++PI)
233 if (!visited.count(*PI)) {
234 stack.push_back(*PI);
235 inserted = true;
236 } else
237 predOnStack = true;
238 }
Owen Anderson3f75d122007-08-01 22:01:54 +0000239
Owen Anderson3de3c532007-08-08 22:26:03 +0000240 // If we inserted a new predecessor, then we'll come back to this block
Owen Anderson3f75d122007-08-01 22:01:54 +0000241 if (inserted)
242 continue;
Owen Anderson3de3c532007-08-08 22:26:03 +0000243 // If we didn't insert because we have no predecessors, then this
244 // query has no dependency at all.
Owen Anderson3f75d122007-08-01 22:01:54 +0000245 else if (!inserted && !predOnStack) {
Owen Anderson935e39b2007-08-09 04:42:44 +0000246 resp.insert(std::make_pair(BB, None));
Owen Anderson3de3c532007-08-08 22:26:03 +0000247 // If we didn't insert because our predecessors are already on the stack,
248 // then we might still have a dependency, but it will be discovered during
249 // backtracking.
Owen Anderson3f75d122007-08-01 22:01:54 +0000250 } else if (!inserted && predOnStack){
Owen Anderson935e39b2007-08-09 04:42:44 +0000251 resp.insert(std::make_pair(BB, NonLocal));
Owen Anderson3f75d122007-08-01 22:01:54 +0000252 }
253
254 stack.pop_back();
Owen Anderson4c295472007-07-24 21:52:37 +0000255 }
Owen Anderson4c295472007-07-24 21:52:37 +0000256}
257
Owen Andersonafe840e2007-08-08 22:01:54 +0000258/// getNonLocalDependency - Fills the passed-in map with the non-local
259/// dependencies of the queries. The map will contain NonLocal for
260/// blocks between the query and its dependencies.
Owen Anderson3f75d122007-08-01 22:01:54 +0000261void MemoryDependenceAnalysis::getNonLocalDependency(Instruction* query,
Owen Andersonafe840e2007-08-08 22:01:54 +0000262 DenseMap<BasicBlock*, Value*>& resp) {
Owen Anderson2bd46a52007-08-16 21:27:05 +0000263 if (depGraphNonLocal.count(query)) {
Owen Anderson05749072007-09-21 03:53:52 +0000264 DenseMap<BasicBlock*, Value*>& cached = depGraphNonLocal[query];
Owen Andersond6c7fea2007-09-09 21:43:49 +0000265 NumCacheNonlocal++;
Owen Anderson05749072007-09-21 03:53:52 +0000266
267 SmallVector<BasicBlock*, 4> dirtied;
268 for (DenseMap<BasicBlock*, Value*>::iterator I = cached.begin(),
269 E = cached.end(); I != E; ++I)
270 if (I->second == Dirty)
271 dirtied.push_back(I->first);
272
273 for (SmallVector<BasicBlock*, 4>::iterator I = dirtied.begin(),
274 E = dirtied.end(); I != E; ++I) {
275 Instruction* localDep = getDependency(query, 0, *I);
276 if (localDep != NonLocal)
277 cached[*I] = localDep;
278 else {
279 cached.erase(*I);
280 nonLocalHelper(query, *I, cached);
281 }
282 }
283
284 resp = cached;
285
Owen Andersondf3055a2008-06-01 21:03:52 +0000286 // Update the reverse non-local dependency cache
287 for (DenseMap<BasicBlock*, Value*>::iterator I = resp.begin(), E = resp.end();
288 I != E; ++I)
289 reverseDepNonLocal[I->second].insert(query);
290
Owen Anderson2bd46a52007-08-16 21:27:05 +0000291 return;
Owen Andersond6c7fea2007-09-09 21:43:49 +0000292 } else
293 NumUncacheNonlocal++;
Owen Anderson2bd46a52007-08-16 21:27:05 +0000294
Owen Andersond6c7fea2007-09-09 21:43:49 +0000295 // If not, go ahead and search for non-local deps.
Owen Anderson3f75d122007-08-01 22:01:54 +0000296 nonLocalHelper(query, query->getParent(), resp);
Owen Anderson2bd46a52007-08-16 21:27:05 +0000297
298 // Update the non-local dependency cache
299 for (DenseMap<BasicBlock*, Value*>::iterator I = resp.begin(), E = resp.end();
300 I != E; ++I) {
301 depGraphNonLocal[query].insert(*I);
302 reverseDepNonLocal[I->second].insert(query);
303 }
Owen Anderson4c295472007-07-24 21:52:37 +0000304}
305
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306/// getDependency - Return the instruction on which a memory operation
Dan Gohmanf1f99a22008-04-10 23:02:38 +0000307/// depends. The local parameter indicates if the query should only
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308/// evaluate dependencies within the same basic block.
Owen Anderson935e39b2007-08-09 04:42:44 +0000309Instruction* MemoryDependenceAnalysis::getDependency(Instruction* query,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310 Instruction* start,
Owen Anderson4c295472007-07-24 21:52:37 +0000311 BasicBlock* block) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312 // Start looking for dependencies with the queried inst
313 BasicBlock::iterator QI = query;
314
315 // Check for a cached result
Owen Andersonc772be72007-12-08 01:37:09 +0000316 std::pair<Instruction*, bool>& cachedResult = depGraphLocal[query];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000317 // If we have a _confirmed_ cached entry, return it
Owen Andersonc772be72007-12-08 01:37:09 +0000318 if (!block && !start) {
319 if (cachedResult.second)
320 return cachedResult.first;
321 else if (cachedResult.first && cachedResult.first != NonLocal)
322 // If we have an unconfirmed cached entry, we can start our search from there
323 QI = cachedResult.first;
324 }
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))
357 return None;
358 else
359 return None;
360
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) {
Owen Andersonc772be72007-12-08 01:37:09 +0000375 cachedResult.first = S;
376 cachedResult.second = true;
Owen Andersone84f4bc2007-08-07 00:33:45 +0000377 reverseDep[S].insert(query);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378 }
379
380 return S;
381 }
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) {
Owen Andersonc772be72007-12-08 01:37:09 +0000389 cachedResult.first = L;
390 cachedResult.second = true;
Owen Andersone84f4bc2007-08-07 00:33:45 +0000391 reverseDep[L].insert(query);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392 }
393
394 return L;
395 }
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),
417 dependee, dependeeSize);
418
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) {
Owen Andersonc772be72007-12-08 01:37:09 +0000425 cachedResult.first = QI;
426 cachedResult.second = true;
Owen Andersone84f4bc2007-08-07 00:33:45 +0000427 reverseDep[QI].insert(query);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000428 }
429
430 return QI;
431 } else {
432 continue;
433 }
434 }
435
436 // If we found a pointer, check if it could be the same as our pointer
437 if (pointer) {
438 AliasAnalysis::AliasResult R = AA.alias(pointer, pointerSize,
439 dependee, dependeeSize);
440
441 if (R != AliasAnalysis::NoAlias) {
Owen Anderson151f7692007-08-06 23:26:03 +0000442 // May-alias loads don't depend on each other
443 if (isa<LoadInst>(query) && isa<LoadInst>(QI) &&
444 R == AliasAnalysis::MayAlias)
445 continue;
446
Owen Andersone84f4bc2007-08-07 00:33:45 +0000447 if (!start && !block) {
Owen Andersonc772be72007-12-08 01:37:09 +0000448 cachedResult.first = QI;
449 cachedResult.second = true;
Owen Andersone84f4bc2007-08-07 00:33:45 +0000450 reverseDep[QI].insert(query);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451 }
452
453 return QI;
454 }
455 }
456 }
457
458 // If we found nothing, return the non-local flag
Owen Andersone84f4bc2007-08-07 00:33:45 +0000459 if (!start && !block) {
Owen Andersonc772be72007-12-08 01:37:09 +0000460 cachedResult.first = NonLocal;
461 cachedResult.second = true;
Owen Andersone84f4bc2007-08-07 00:33:45 +0000462 reverseDep[NonLocal].insert(query);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000463 }
464
465 return NonLocal;
466}
467
Owen Anderson8d272d52008-02-12 21:15:18 +0000468/// dropInstruction - Remove an instruction from the analysis, making
469/// absolutely conservative assumptions when updating the cache. This is
470/// useful, for example when an instruction is changed rather than removed.
471void MemoryDependenceAnalysis::dropInstruction(Instruction* drop) {
472 depMapType::iterator depGraphEntry = depGraphLocal.find(drop);
473 if (depGraphEntry != depGraphLocal.end())
474 reverseDep[depGraphEntry->second.first].erase(drop);
475
476 // Drop dependency information for things that depended on this instr
477 SmallPtrSet<Instruction*, 4>& set = reverseDep[drop];
478 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
479 I != E; ++I)
480 depGraphLocal.erase(*I);
481
482 depGraphLocal.erase(drop);
483 reverseDep.erase(drop);
484
485 for (DenseMap<BasicBlock*, Value*>::iterator DI =
486 depGraphNonLocal[drop].begin(), DE = depGraphNonLocal[drop].end();
487 DI != DE; ++DI)
488 if (DI->second != None)
489 reverseDepNonLocal[DI->second].erase(drop);
490
491 if (reverseDepNonLocal.count(drop)) {
492 SmallPtrSet<Instruction*, 4>& set = reverseDepNonLocal[drop];
493 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
494 I != E; ++I)
495 for (DenseMap<BasicBlock*, Value*>::iterator DI =
496 depGraphNonLocal[*I].begin(), DE = depGraphNonLocal[*I].end();
497 DI != DE; ++DI)
498 if (DI->second == drop)
499 DI->second = Dirty;
500 }
501
502 reverseDepNonLocal.erase(drop);
503 nonLocalDepMapType::iterator I = depGraphNonLocal.find(drop);
504 if (I != depGraphNonLocal.end())
505 depGraphNonLocal.erase(I);
506}
507
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000508/// removeInstruction - Remove an instruction from the dependence analysis,
509/// updating the dependence of instructions that previously depended on it.
Owen Anderson3de3c532007-08-08 22:26:03 +0000510/// This method attempts to keep the cache coherent using the reverse map.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000511void MemoryDependenceAnalysis::removeInstruction(Instruction* rem) {
512 // Figure out the new dep for things that currently depend on rem
Owen Anderson935e39b2007-08-09 04:42:44 +0000513 Instruction* newDep = NonLocal;
David Greene701171c2007-07-31 20:01:27 +0000514
Owen Andersonc772be72007-12-08 01:37:09 +0000515 for (DenseMap<BasicBlock*, Value*>::iterator DI =
516 depGraphNonLocal[rem].begin(), DE = depGraphNonLocal[rem].end();
517 DI != DE; ++DI)
518 if (DI->second != None)
519 reverseDepNonLocal[DI->second].erase(rem);
520
David Greene701171c2007-07-31 20:01:27 +0000521 depMapType::iterator depGraphEntry = depGraphLocal.find(rem);
David Greene701171c2007-07-31 20:01:27 +0000522
523 if (depGraphEntry != depGraphLocal.end()) {
Owen Anderson8d272d52008-02-12 21:15:18 +0000524 reverseDep[depGraphEntry->second.first].erase(rem);
Owen Anderson25296a22008-01-30 01:24:05 +0000525
David Greene701171c2007-07-31 20:01:27 +0000526 if (depGraphEntry->second.first != NonLocal &&
Owen Anderson6487cf52008-02-05 04:34:03 +0000527 depGraphEntry->second.first != None &&
David Greene701171c2007-07-31 20:01:27 +0000528 depGraphEntry->second.second) {
529 // If we have dep info for rem, set them to it
Owen Anderson935e39b2007-08-09 04:42:44 +0000530 BasicBlock::iterator RI = depGraphEntry->second.first;
David Greene701171c2007-07-31 20:01:27 +0000531 RI++;
Owen Andersonbd9a78d2008-07-28 16:00:58 +0000532
533 // If RI is rem, then we use rem's immediate successor.
534 if (RI == (BasicBlock::iterator)rem) RI++;
535
David Greene701171c2007-07-31 20:01:27 +0000536 newDep = RI;
Chris Lattner39bc72f2008-11-28 21:16:44 +0000537 } else if ((depGraphEntry->second.first == NonLocal ||
538 depGraphEntry->second.first == None) &&
539 depGraphEntry->second.second) {
David Greene701171c2007-07-31 20:01:27 +0000540 // If we have a confirmed non-local flag, use it
Owen Anderson6487cf52008-02-05 04:34:03 +0000541 newDep = depGraphEntry->second.first;
David Greene701171c2007-07-31 20:01:27 +0000542 } else {
543 // Otherwise, use the immediate successor of rem
Owen Andersonafe840e2007-08-08 22:01:54 +0000544 // NOTE: This is because, when getDependence is called, it will first
545 // check the immediate predecessor of what is in the cache.
David Greene701171c2007-07-31 20:01:27 +0000546 BasicBlock::iterator RI = rem;
547 RI++;
548 newDep = RI;
549 }
Owen Anderson6487cf52008-02-05 04:34:03 +0000550 } else {
551 // Otherwise, use the immediate successor of rem
552 // NOTE: This is because, when getDependence is called, it will first
553 // check the immediate predecessor of what is in the cache.
554 BasicBlock::iterator RI = rem;
555 RI++;
556 newDep = RI;
557 }
558
559 SmallPtrSet<Instruction*, 4>& set = reverseDep[rem];
560 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
561 I != E; ++I) {
562 // Insert the new dependencies
563 // Mark it as unconfirmed as long as it is not the non-local flag
564 depGraphLocal[*I] = std::make_pair(newDep, (newDep == NonLocal ||
565 newDep == None));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000566 }
Owen Anderson2bd46a52007-08-16 21:27:05 +0000567
Owen Andersonc772be72007-12-08 01:37:09 +0000568 depGraphLocal.erase(rem);
569 reverseDep.erase(rem);
570
Owen Anderson0ceeca52007-09-11 04:31:00 +0000571 if (reverseDepNonLocal.count(rem)) {
Owen Anderson2bd46a52007-08-16 21:27:05 +0000572 SmallPtrSet<Instruction*, 4>& set = reverseDepNonLocal[rem];
573 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
574 I != E; ++I)
Owen Anderson05749072007-09-21 03:53:52 +0000575 for (DenseMap<BasicBlock*, Value*>::iterator DI =
576 depGraphNonLocal[*I].begin(), DE = depGraphNonLocal[*I].end();
577 DI != DE; ++DI)
578 if (DI->second == rem)
579 DI->second = Dirty;
Owen Anderson2bd46a52007-08-16 21:27:05 +0000580
Owen Anderson2bd46a52007-08-16 21:27:05 +0000581 }
Owen Andersonc772be72007-12-08 01:37:09 +0000582
583 reverseDepNonLocal.erase(rem);
584 nonLocalDepMapType::iterator I = depGraphNonLocal.find(rem);
585 if (I != depGraphNonLocal.end())
586 depGraphNonLocal.erase(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000587
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000588 getAnalysis<AliasAnalysis>().deleteValue(rem);
589}