1 //===- MemoryDependenceAnalysis.cpp - Mem Deps Implementation --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the Owen Anderson and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements an analysis that determines, for a given memory
11 // operation, what preceding memory operations it depends on. It builds on
12 // alias analysis information, and tries to provide a lazy, caching interface to
13 // a common kind of alias information query.
15 //===----------------------------------------------------------------------===//
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"
22 #include "llvm/Target/TargetData.h"
26 char MemoryDependenceAnalysis::ID = 0;
28 Instruction* MemoryDependenceAnalysis::NonLocal = (Instruction*)0;
29 Instruction* MemoryDependenceAnalysis::None = (Instruction*)~0;
31 // Register this pass...
32 static RegisterPass<MemoryDependenceAnalysis> X("memdep",
33 "Memory Dependence Analysis");
35 /// getAnalysisUsage - Does not modify anything. It uses Alias Analysis.
37 void MemoryDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
39 AU.addRequiredTransitive<AliasAnalysis>();
40 AU.addRequiredTransitive<TargetData>();
43 // Find the dependency of a CallSite
44 Instruction* MemoryDependenceAnalysis::getCallSiteDependency(CallSite C, Instruction* start,
46 assert(local && "Non-local memory dependence analysis not yet implemented");
48 AliasAnalysis& AA = getAnalysis<AliasAnalysis>();
49 TargetData& TD = getAnalysis<TargetData>();
50 BasicBlock::iterator blockBegin = C.getInstruction()->getParent()->begin();
51 BasicBlock::iterator QI = C.getInstruction();
53 while (QI != blockBegin) {
56 // If this inst is a memory op, get the pointer it accessed
58 uint64_t pointerSize = 0;
59 if (StoreInst* S = dyn_cast<StoreInst>(QI)) {
60 pointer = S->getPointerOperand();
61 pointerSize = TD.getTypeSize(S->getOperand(0)->getType());
62 } else if (LoadInst* L = dyn_cast<LoadInst>(QI)) {
63 pointer = L->getPointerOperand();
64 pointerSize = TD.getTypeSize(L->getType());
65 } else if (AllocationInst* AI = dyn_cast<AllocationInst>(QI)) {
67 if (ConstantInt* C = dyn_cast<ConstantInt>(AI->getArraySize()))
68 pointerSize = C->getZExtValue() * TD.getTypeSize(AI->getAllocatedType());
71 } else if (VAArgInst* V = dyn_cast<VAArgInst>(QI)) {
72 pointer = V->getOperand(0);
73 pointerSize = TD.getTypeSize(V->getType());
74 } else if (FreeInst* F = dyn_cast<FreeInst>(QI)) {
75 pointer = F->getPointerOperand();
77 // FreeInsts erase the entire structure
79 } else if (CallSite::get(QI).getInstruction() != 0) {
80 if (AA.getModRefInfo(C, CallSite::get(QI)) != AliasAnalysis::NoModRef) {
81 depGraphLocal.insert(std::make_pair(C.getInstruction(), std::make_pair(QI, true)));
82 reverseDep.insert(std::make_pair(QI, C.getInstruction()));
90 if (AA.getModRefInfo(C, pointer, pointerSize) != AliasAnalysis::NoModRef) {
91 depGraphLocal.insert(std::make_pair(C.getInstruction(), std::make_pair(QI, true)));
92 reverseDep.insert(std::make_pair(QI, C.getInstruction()));
97 // No dependence found
98 depGraphLocal.insert(std::make_pair(C.getInstruction(), std::make_pair(NonLocal, true)));
99 reverseDep.insert(std::make_pair(NonLocal, C.getInstruction()));
103 /// getDependency - Return the instruction on which a memory operation
104 /// depends. The local paramter indicates if the query should only
105 /// evaluate dependencies within the same basic block.
106 Instruction* MemoryDependenceAnalysis::getDependency(Instruction* query,
110 assert(0 && "Non-local memory dependence is not yet supported.");
112 // Start looking for dependencies with the queried inst
113 BasicBlock::iterator QI = query;
115 // Check for a cached result
116 std::pair<Instruction*, bool> cachedResult = depGraphLocal[query];
117 // If we have a _confirmed_ cached entry, return it
118 if (cachedResult.second)
119 return cachedResult.first;
120 else if (cachedResult.first != NonLocal)
121 // If we have an unconfirmed cached entry, we can start our search from there
122 QI = cachedResult.first;
127 AliasAnalysis& AA = getAnalysis<AliasAnalysis>();
128 TargetData& TD = getAnalysis<TargetData>();
130 // Get the pointer value for which dependence will be determined
132 uint64_t dependeeSize = 0;
133 bool queryIsVolatile = false;
134 if (StoreInst* S = dyn_cast<StoreInst>(query)) {
135 dependee = S->getPointerOperand();
136 dependeeSize = TD.getTypeSize(S->getOperand(0)->getType());
137 queryIsVolatile = S->isVolatile();
138 } else if (LoadInst* L = dyn_cast<LoadInst>(query)) {
139 dependee = L->getPointerOperand();
140 dependeeSize = TD.getTypeSize(L->getType());
141 queryIsVolatile = L->isVolatile();
142 } else if (VAArgInst* V = dyn_cast<VAArgInst>(query)) {
143 dependee = V->getOperand(0);
144 dependeeSize = TD.getTypeSize(V->getType());
145 } else if (FreeInst* F = dyn_cast<FreeInst>(query)) {
146 dependee = F->getPointerOperand();
148 // FreeInsts erase the entire structure, not just a field
150 } else if (CallSite::get(query).getInstruction() != 0)
151 return getCallSiteDependency(CallSite::get(query), start);
152 else if (isa<AllocationInst>(query))
157 BasicBlock::iterator blockBegin = query->getParent()->begin();
159 while (QI != blockBegin) {
162 // If this inst is a memory op, get the pointer it accessed
164 uint64_t pointerSize = 0;
165 if (StoreInst* S = dyn_cast<StoreInst>(QI)) {
166 // All volatile loads/stores depend on each other
167 if (queryIsVolatile && S->isVolatile()) {
169 depGraphLocal.insert(std::make_pair(query, std::make_pair(S, true)));
170 reverseDep.insert(std::make_pair(S, query));
176 pointer = S->getPointerOperand();
177 pointerSize = TD.getTypeSize(S->getOperand(0)->getType());
178 } else if (LoadInst* L = dyn_cast<LoadInst>(QI)) {
179 // All volatile loads/stores depend on each other
180 if (queryIsVolatile && L->isVolatile()) {
182 depGraphLocal.insert(std::make_pair(query, std::make_pair(L, true)));
183 reverseDep.insert(std::make_pair(L, query));
189 pointer = L->getPointerOperand();
190 pointerSize = TD.getTypeSize(L->getType());
191 } else if (AllocationInst* AI = dyn_cast<AllocationInst>(QI)) {
193 if (ConstantInt* C = dyn_cast<ConstantInt>(AI->getArraySize()))
194 pointerSize = C->getZExtValue() * TD.getTypeSize(AI->getAllocatedType());
197 } else if (VAArgInst* V = dyn_cast<VAArgInst>(QI)) {
198 pointer = V->getOperand(0);
199 pointerSize = TD.getTypeSize(V->getType());
200 } else if (FreeInst* F = dyn_cast<FreeInst>(QI)) {
201 pointer = F->getPointerOperand();
203 // FreeInsts erase the entire structure
205 } else if (CallSite::get(QI).getInstruction() != 0) {
206 // Call insts need special handling. Check is they can modify our pointer
207 if (AA.getModRefInfo(CallSite::get(QI), dependee, dependeeSize) !=
208 AliasAnalysis::NoModRef) {
210 depGraphLocal.insert(std::make_pair(query, std::make_pair(QI, true)));
211 reverseDep.insert(std::make_pair(QI, query));
220 // If we found a pointer, check if it could be the same as our pointer
222 AliasAnalysis::AliasResult R = AA.alias(pointer, pointerSize,
223 dependee, dependeeSize);
225 if (R != AliasAnalysis::NoAlias) {
227 depGraphLocal.insert(std::make_pair(query, std::make_pair(QI, true)));
228 reverseDep.insert(std::make_pair(QI, query));
236 // If we found nothing, return the non-local flag
238 depGraphLocal.insert(std::make_pair(query,
239 std::make_pair(NonLocal, true)));
240 reverseDep.insert(std::make_pair(NonLocal, query));
246 /// removeInstruction - Remove an instruction from the dependence analysis,
247 /// updating the dependence of instructions that previously depended on it.
248 void MemoryDependenceAnalysis::removeInstruction(Instruction* rem) {
249 // Figure out the new dep for things that currently depend on rem
250 Instruction* newDep = NonLocal;
251 if (depGraphLocal[rem].first != NonLocal &&
252 depGraphLocal[rem].second) {
253 // If we have dep info for rem, set them to it
254 BasicBlock::iterator RI = depGraphLocal[rem].first;
257 } else if (depGraphLocal[rem].first == NonLocal &&
258 depGraphLocal[rem].second ) {
259 // If we have a confirmed non-local flag, use it
262 // Otherwise, use the immediate successor of rem
263 // NOTE: This is because, when getDependence is called, it will first check
264 // the immediate predecessor of what is in the cache.
265 BasicBlock::iterator RI = rem;
270 std::multimap<Instruction*, Instruction*>::iterator I = reverseDep.find(rem);
271 while (I->first == rem) {
272 // Insert the new dependencies
273 // Mark it as unconfirmed as long as it is not the non-local flag
274 depGraphLocal[I->second] = std::make_pair(newDep, !newDep);
276 I = reverseDep.find(rem);
279 getAnalysis<AliasAnalysis>().deleteValue(rem);