blob: eb8ea0ebdb0b1cdc8bafd2fdad00d6e858a7efb8 [file] [log] [blame]
Andreas Bolka306b5b22009-06-24 21:29:13 +00001//===- LoopDependenceAnalysis.cpp - LDA Implementation ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This is the (beginning) of an implementation of a loop dependence analysis
11// framework, which is used to detect dependences in memory accesses in loops.
12//
13// Please note that this is work in progress and the interface is subject to
14// change.
15//
16// TODO: adapt as implementation progresses.
17//
Andreas Bolka80478de2009-07-29 05:35:53 +000018// TODO: document lingo (pair, subscript, index)
19//
Andreas Bolka306b5b22009-06-24 21:29:13 +000020//===----------------------------------------------------------------------===//
21
22#define DEBUG_TYPE "lda"
Andreas Bolka43aa1e02009-07-28 19:49:49 +000023#include "llvm/ADT/Statistic.h"
Andreas Bolka8f9cd692009-07-01 21:45:23 +000024#include "llvm/Analysis/AliasAnalysis.h"
Andreas Bolka306b5b22009-06-24 21:29:13 +000025#include "llvm/Analysis/LoopDependenceAnalysis.h"
26#include "llvm/Analysis/LoopPass.h"
27#include "llvm/Analysis/ScalarEvolution.h"
Andreas Bolka3bbe7de2009-08-03 01:03:48 +000028#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +000029#include "llvm/Instructions.h"
Andreas Bolka80478de2009-07-29 05:35:53 +000030#include "llvm/Operator.h"
Andreas Bolka127c2e72009-07-24 23:19:28 +000031#include "llvm/Support/Allocator.h"
Andreas Bolka5771ed92009-06-30 02:12:10 +000032#include "llvm/Support/Debug.h"
Edwin Török675d5622009-07-11 20:10:48 +000033#include "llvm/Support/ErrorHandling.h"
Andreas Bolka127c2e72009-07-24 23:19:28 +000034#include "llvm/Support/raw_ostream.h"
Andreas Bolka8f9cd692009-07-01 21:45:23 +000035#include "llvm/Target/TargetData.h"
Andreas Bolka306b5b22009-06-24 21:29:13 +000036using namespace llvm;
37
Andreas Bolka43aa1e02009-07-28 19:49:49 +000038STATISTIC(NumAnswered, "Number of dependence queries answered");
39STATISTIC(NumAnalysed, "Number of distinct dependence pairs analysed");
40STATISTIC(NumDependent, "Number of pairs with dependent accesses");
41STATISTIC(NumIndependent, "Number of pairs with independent accesses");
42STATISTIC(NumUnknown, "Number of pairs with unknown accesses");
43
Andreas Bolka306b5b22009-06-24 21:29:13 +000044LoopPass *llvm::createLoopDependenceAnalysisPass() {
45 return new LoopDependenceAnalysis();
46}
47
48static RegisterPass<LoopDependenceAnalysis>
49R("lda", "Loop Dependence Analysis", false, true);
50char LoopDependenceAnalysis::ID = 0;
51
52//===----------------------------------------------------------------------===//
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +000053// Utility Functions
54//===----------------------------------------------------------------------===//
55
Andreas Bolkafc8a04a2009-06-29 18:51:11 +000056static inline bool IsMemRefInstr(const Value *V) {
57 const Instruction *I = dyn_cast<const Instruction>(V);
58 return I && (I->mayReadFromMemory() || I->mayWriteToMemory());
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +000059}
60
Andreas Bolkad14a5eb2009-07-23 01:57:06 +000061static void GetMemRefInstrs(const Loop *L,
62 SmallVectorImpl<Instruction*> &Memrefs) {
Andreas Bolka158c5902009-06-28 00:35:22 +000063 for (Loop::block_iterator b = L->block_begin(), be = L->block_end();
Andreas Bolkae00b4942009-07-28 19:49:25 +000064 b != be; ++b)
Andreas Bolka158c5902009-06-28 00:35:22 +000065 for (BasicBlock::iterator i = (*b)->begin(), ie = (*b)->end();
Andreas Bolkae00b4942009-07-28 19:49:25 +000066 i != ie; ++i)
Andreas Bolka88a17f02009-06-29 00:50:26 +000067 if (IsMemRefInstr(i))
Andreas Bolkad14a5eb2009-07-23 01:57:06 +000068 Memrefs.push_back(i);
Andreas Bolka158c5902009-06-28 00:35:22 +000069}
70
Andreas Bolka5771ed92009-06-30 02:12:10 +000071static bool IsLoadOrStoreInst(Value *I) {
72 return isa<LoadInst>(I) || isa<StoreInst>(I);
73}
74
75static Value *GetPointerOperand(Value *I) {
76 if (LoadInst *i = dyn_cast<LoadInst>(I))
77 return i->getPointerOperand();
78 if (StoreInst *i = dyn_cast<StoreInst>(I))
79 return i->getPointerOperand();
Edwin Törökbd448e32009-07-14 16:55:14 +000080 llvm_unreachable("Value is no load or store instruction!");
Andreas Bolka5771ed92009-06-30 02:12:10 +000081 // Never reached.
82 return 0;
83}
84
Andreas Bolkae00b4942009-07-28 19:49:25 +000085static AliasAnalysis::AliasResult UnderlyingObjectsAlias(AliasAnalysis *AA,
86 const Value *A,
87 const Value *B) {
88 const Value *aObj = A->getUnderlyingObject();
89 const Value *bObj = B->getUnderlyingObject();
90 return AA->alias(aObj, AA->getTypeStoreSize(aObj->getType()),
91 bObj, AA->getTypeStoreSize(bObj->getType()));
92}
93
Andreas Bolka80478de2009-07-29 05:35:53 +000094static inline const SCEV *GetZeroSCEV(ScalarEvolution *SE) {
95 return SE->getConstant(Type::Int32Ty, 0L);
96}
97
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +000098//===----------------------------------------------------------------------===//
99// Dependence Testing
100//===----------------------------------------------------------------------===//
101
Andreas Bolkad14a5eb2009-07-23 01:57:06 +0000102bool LoopDependenceAnalysis::isDependencePair(const Value *A,
103 const Value *B) const {
104 return IsMemRefInstr(A) &&
105 IsMemRefInstr(B) &&
106 (cast<const Instruction>(A)->mayWriteToMemory() ||
107 cast<const Instruction>(B)->mayWriteToMemory());
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +0000108}
109
Andreas Bolkae00b4942009-07-28 19:49:25 +0000110bool LoopDependenceAnalysis::findOrInsertDependencePair(Value *A,
111 Value *B,
Andreas Bolka0a528a02009-07-23 14:32:46 +0000112 DependencePair *&P) {
113 void *insertPos = 0;
114 FoldingSetNodeID id;
Andreas Bolkae00b4942009-07-28 19:49:25 +0000115 id.AddPointer(A);
116 id.AddPointer(B);
Andreas Bolka5771ed92009-06-30 02:12:10 +0000117
Andreas Bolka0a528a02009-07-23 14:32:46 +0000118 P = Pairs.FindNodeOrInsertPos(id, insertPos);
119 if (P) return true;
Andreas Bolka5771ed92009-06-30 02:12:10 +0000120
Andreas Bolka0a528a02009-07-23 14:32:46 +0000121 P = PairAllocator.Allocate<DependencePair>();
Andreas Bolkae00b4942009-07-28 19:49:25 +0000122 new (P) DependencePair(id, A, B);
Andreas Bolka0a528a02009-07-23 14:32:46 +0000123 Pairs.InsertNode(P, insertPos);
124 return false;
125}
126
Andreas Bolka3bbe7de2009-08-03 01:03:48 +0000127bool LoopDependenceAnalysis::isLoopInvariant(const SCEV *S) const {
128 for (const Loop *L = this->L; L != 0; L = L->getParentLoop())
129 if (!S->isLoopInvariant(L))
130 return false;
131 return true;
132}
133
134bool LoopDependenceAnalysis::isAffine(const SCEV *S) const {
135 const SCEVAddRecExpr *rec = dyn_cast<SCEVAddRecExpr>(S);
136 return isLoopInvariant(S) || (rec && rec->isAffine());
137}
138
Andreas Bolkad2e1dd02009-08-05 04:26:05 +0000139bool LoopDependenceAnalysis::isZIVPair(const SCEV *A, const SCEV *B) const {
140 return isLoopInvariant(A) && isLoopInvariant(B);
141}
142
143LoopDependenceAnalysis::DependenceResult
144LoopDependenceAnalysis::analyseZIV(const SCEV *A,
145 const SCEV *B,
146 Subscript *S) const {
Andreas Bolka17722c42009-08-06 03:10:33 +0000147 assert(isZIVPair(A, B) && "Attempted to ZIV-test non-ZIV SCEVs!");
148 return A == B ? Dependent : Independent;
Andreas Bolkad2e1dd02009-08-05 04:26:05 +0000149}
150
Andreas Bolka4e661252009-07-28 19:50:13 +0000151LoopDependenceAnalysis::DependenceResult
Andreas Bolka80478de2009-07-29 05:35:53 +0000152LoopDependenceAnalysis::analyseSubscript(const SCEV *A,
153 const SCEV *B,
154 Subscript *S) const {
Andreas Bolkaa89b3b32009-07-30 02:26:01 +0000155 DEBUG(errs() << " Testing subscript: " << *A << ", " << *B << "\n");
156
157 if (A == B) {
158 DEBUG(errs() << " -> [D] same SCEV\n");
159 return Dependent;
160 }
161
Andreas Bolka3bbe7de2009-08-03 01:03:48 +0000162 if (!isAffine(A) || !isAffine(B)) {
163 DEBUG(errs() << " -> [?] not affine\n");
164 return Unknown;
165 }
166
Andreas Bolkad2e1dd02009-08-05 04:26:05 +0000167 if (isZIVPair(A, B))
168 return analyseZIV(A, B, S);
169
170 // TODO: Implement SIV/MIV testers.
Andreas Bolkaa89b3b32009-07-30 02:26:01 +0000171
172 DEBUG(errs() << " -> [?] cannot analyse subscript\n");
173 return Unknown;
Andreas Bolka80478de2009-07-29 05:35:53 +0000174}
175
176LoopDependenceAnalysis::DependenceResult
Andreas Bolka4e661252009-07-28 19:50:13 +0000177LoopDependenceAnalysis::analysePair(DependencePair *P) const {
Andreas Bolka89569882009-07-25 12:19:58 +0000178 DEBUG(errs() << "Analysing:\n" << *P->A << "\n" << *P->B << "\n");
Andreas Bolka0a528a02009-07-23 14:32:46 +0000179
Andreas Bolka0a528a02009-07-23 14:32:46 +0000180 // We only analyse loads and stores but no possible memory accesses by e.g.
181 // free, call, or invoke instructions.
182 if (!IsLoadOrStoreInst(P->A) || !IsLoadOrStoreInst(P->B)) {
Andreas Bolka89569882009-07-25 12:19:58 +0000183 DEBUG(errs() << "--> [?] no load/store\n");
Andreas Bolka4e661252009-07-28 19:50:13 +0000184 return Unknown;
Andreas Bolka0a528a02009-07-23 14:32:46 +0000185 }
186
Andreas Bolkae00b4942009-07-28 19:49:25 +0000187 Value *aPtr = GetPointerOperand(P->A);
188 Value *bPtr = GetPointerOperand(P->B);
Andreas Bolka5771ed92009-06-30 02:12:10 +0000189
Andreas Bolkae00b4942009-07-28 19:49:25 +0000190 switch (UnderlyingObjectsAlias(AA, aPtr, bPtr)) {
191 case AliasAnalysis::MayAlias:
192 // We can not analyse objects if we do not know about their aliasing.
Andreas Bolka89569882009-07-25 12:19:58 +0000193 DEBUG(errs() << "---> [?] may alias\n");
Andreas Bolka4e661252009-07-28 19:50:13 +0000194 return Unknown;
Andreas Bolka5771ed92009-06-30 02:12:10 +0000195
Andreas Bolkae00b4942009-07-28 19:49:25 +0000196 case AliasAnalysis::NoAlias:
197 // If the objects noalias, they are distinct, accesses are independent.
Andreas Bolka89569882009-07-25 12:19:58 +0000198 DEBUG(errs() << "---> [I] no alias\n");
Andreas Bolka4e661252009-07-28 19:50:13 +0000199 return Independent;
Andreas Bolka5771ed92009-06-30 02:12:10 +0000200
Andreas Bolkae00b4942009-07-28 19:49:25 +0000201 case AliasAnalysis::MustAlias:
202 break; // The underlying objects alias, test accesses for dependence.
203 }
Andreas Bolka8f9cd692009-07-01 21:45:23 +0000204
Andreas Bolka80478de2009-07-29 05:35:53 +0000205 const GEPOperator *aGEP = dyn_cast<GEPOperator>(aPtr);
206 const GEPOperator *bGEP = dyn_cast<GEPOperator>(bPtr);
207
208 if (!aGEP || !bGEP)
209 return Unknown;
210
211 // FIXME: Is filtering coupled subscripts necessary?
212
Andreas Bolka5b414bb2009-08-05 04:13:41 +0000213 // Collect GEP operand pairs (FIXME: use GetGEPOperands from BasicAA), adding
Andreas Bolka80478de2009-07-29 05:35:53 +0000214 // trailing zeroes to the smaller GEP, if needed.
Andreas Bolka5b414bb2009-08-05 04:13:41 +0000215 typedef SmallVector<std::pair<const SCEV*, const SCEV*>, 4> GEPOpdPairsTy;
216 GEPOpdPairsTy opds;
217 for(GEPOperator::const_op_iterator aIdx = aGEP->idx_begin(),
218 aEnd = aGEP->idx_end(),
219 bIdx = bGEP->idx_begin(),
220 bEnd = bGEP->idx_end();
221 aIdx != aEnd && bIdx != bEnd;
222 aIdx += (aIdx != aEnd), bIdx += (bIdx != bEnd)) {
Andreas Bolka80478de2009-07-29 05:35:53 +0000223 const SCEV* aSCEV = (aIdx != aEnd) ? SE->getSCEV(*aIdx) : GetZeroSCEV(SE);
224 const SCEV* bSCEV = (bIdx != bEnd) ? SE->getSCEV(*bIdx) : GetZeroSCEV(SE);
Andreas Bolka5b414bb2009-08-05 04:13:41 +0000225 opds.push_back(std::make_pair(aSCEV, bSCEV));
226 }
227
228 if (!opds.empty() && opds[0].first != opds[0].second) {
229 // We cannot (yet) handle arbitrary GEP pointer offsets. By limiting
230 //
231 // TODO: this could be relaxed by adding the size of the underlying object
232 // to the first subscript. If we have e.g. (GEP x,0,i; GEP x,2,-i) and we
233 // know that x is a [100 x i8]*, we could modify the first subscript to be
234 // (i, 200-i) instead of (i, -i).
235 return Unknown;
236 }
237
238 // Now analyse the collected operand pairs (skipping the GEP ptr offsets).
239 for (GEPOpdPairsTy::const_iterator i = opds.begin() + 1, end = opds.end();
240 i != end; ++i) {
Andreas Bolka80478de2009-07-29 05:35:53 +0000241 Subscript subscript;
Andreas Bolka5b414bb2009-08-05 04:13:41 +0000242 DependenceResult result = analyseSubscript(i->first, i->second, &subscript);
Andreas Bolka80478de2009-07-29 05:35:53 +0000243 if (result != Dependent) {
244 // We either proved independence or failed to analyse this subscript.
245 // Further subscripts will not improve the situation, so abort early.
246 return result;
247 }
248 P->Subscripts.push_back(subscript);
Andreas Bolka80478de2009-07-29 05:35:53 +0000249 }
Andreas Bolka5b414bb2009-08-05 04:13:41 +0000250 // We successfully analysed all subscripts but failed to prove independence.
Andreas Bolka80478de2009-07-29 05:35:53 +0000251 return Dependent;
Andreas Bolka0a528a02009-07-23 14:32:46 +0000252}
253
254bool LoopDependenceAnalysis::depends(Value *A, Value *B) {
255 assert(isDependencePair(A, B) && "Values form no dependence pair!");
Andreas Bolka43aa1e02009-07-28 19:49:49 +0000256 ++NumAnswered;
Andreas Bolka0a528a02009-07-23 14:32:46 +0000257
258 DependencePair *p;
259 if (!findOrInsertDependencePair(A, B, p)) {
260 // The pair is not cached, so analyse it.
Andreas Bolka43aa1e02009-07-28 19:49:49 +0000261 ++NumAnalysed;
Andreas Bolka4e661252009-07-28 19:50:13 +0000262 switch (p->Result = analysePair(p)) {
Andreas Bolka43aa1e02009-07-28 19:49:49 +0000263 case Dependent: ++NumDependent; break;
264 case Independent: ++NumIndependent; break;
265 case Unknown: ++NumUnknown; break;
266 }
Andreas Bolka0a528a02009-07-23 14:32:46 +0000267 }
268 return p->Result != Independent;
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +0000269}
270
271//===----------------------------------------------------------------------===//
Andreas Bolka306b5b22009-06-24 21:29:13 +0000272// LoopDependenceAnalysis Implementation
273//===----------------------------------------------------------------------===//
274
275bool LoopDependenceAnalysis::runOnLoop(Loop *L, LPPassManager &) {
276 this->L = L;
Andreas Bolka8f9cd692009-07-01 21:45:23 +0000277 AA = &getAnalysis<AliasAnalysis>();
Andreas Bolka306b5b22009-06-24 21:29:13 +0000278 SE = &getAnalysis<ScalarEvolution>();
279 return false;
280}
281
Andreas Bolka0a528a02009-07-23 14:32:46 +0000282void LoopDependenceAnalysis::releaseMemory() {
283 Pairs.clear();
284 PairAllocator.Reset();
285}
286
Andreas Bolka306b5b22009-06-24 21:29:13 +0000287void LoopDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
288 AU.setPreservesAll();
Andreas Bolka8f9cd692009-07-01 21:45:23 +0000289 AU.addRequiredTransitive<AliasAnalysis>();
Andreas Bolkab88ee912009-06-28 00:16:08 +0000290 AU.addRequiredTransitive<ScalarEvolution>();
291}
292
Andreas Bolkad14a5eb2009-07-23 01:57:06 +0000293static void PrintLoopInfo(raw_ostream &OS,
294 LoopDependenceAnalysis *LDA, const Loop *L) {
Andreas Bolkab88ee912009-06-28 00:16:08 +0000295 if (!L->empty()) return; // ignore non-innermost loops
296
Andreas Bolkab512fb02009-07-03 01:42:52 +0000297 SmallVector<Instruction*, 8> memrefs;
298 GetMemRefInstrs(L, memrefs);
299
Andreas Bolkab88ee912009-06-28 00:16:08 +0000300 OS << "Loop at depth " << L->getLoopDepth() << ", header block: ";
301 WriteAsOperand(OS, L->getHeader(), false);
302 OS << "\n";
Andreas Bolka158c5902009-06-28 00:35:22 +0000303
Andreas Bolka158c5902009-06-28 00:35:22 +0000304 OS << " Load/store instructions: " << memrefs.size() << "\n";
Andreas Bolkab512fb02009-07-03 01:42:52 +0000305 for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(),
Andreas Bolkae00b4942009-07-28 19:49:25 +0000306 end = memrefs.end(); x != end; ++x)
Andreas Bolkad14a5eb2009-07-23 01:57:06 +0000307 OS << "\t" << (x - memrefs.begin()) << ": " << **x << "\n";
Andreas Bolkab512fb02009-07-03 01:42:52 +0000308
Andreas Bolka158c5902009-06-28 00:35:22 +0000309 OS << " Pairwise dependence results:\n";
310 for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(),
Andreas Bolkae00b4942009-07-28 19:49:25 +0000311 end = memrefs.end(); x != end; ++x)
Andreas Bolka158c5902009-06-28 00:35:22 +0000312 for (SmallVector<Instruction*, 8>::const_iterator y = x + 1;
Andreas Bolkae00b4942009-07-28 19:49:25 +0000313 y != end; ++y)
Andreas Bolka158c5902009-06-28 00:35:22 +0000314 if (LDA->isDependencePair(*x, *y))
315 OS << "\t" << (x - memrefs.begin()) << "," << (y - memrefs.begin())
316 << ": " << (LDA->depends(*x, *y) ? "dependent" : "independent")
317 << "\n";
Andreas Bolkab88ee912009-06-28 00:16:08 +0000318}
319
320void LoopDependenceAnalysis::print(raw_ostream &OS, const Module*) const {
Andreas Bolka158c5902009-06-28 00:35:22 +0000321 // TODO: doc why const_cast is safe
322 PrintLoopInfo(OS, const_cast<LoopDependenceAnalysis*>(this), this->L);
Andreas Bolkab88ee912009-06-28 00:16:08 +0000323}
324
325void LoopDependenceAnalysis::print(std::ostream &OS, const Module *M) const {
326 raw_os_ostream os(OS);
327 print(os, M);
Andreas Bolka306b5b22009-06-24 21:29:13 +0000328}