blob: 32d22662c341586e2be6a5a51c8d9a37e17da650 [file] [log] [blame]
Andreas Bolkacb210102009-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 Bolka15f72db2009-07-29 05:35:53 +000018// TODO: document lingo (pair, subscript, index)
19//
Andreas Bolkacb210102009-06-24 21:29:13 +000020//===----------------------------------------------------------------------===//
21
22#define DEBUG_TYPE "lda"
Andreas Bolka699db992009-08-07 18:23:41 +000023#include "llvm/ADT/DenseSet.h"
Andreas Bolka328fb3d2009-07-28 19:49:49 +000024#include "llvm/ADT/Statistic.h"
Andreas Bolkafecbc592009-07-01 21:45:23 +000025#include "llvm/Analysis/AliasAnalysis.h"
Andreas Bolkacb210102009-06-24 21:29:13 +000026#include "llvm/Analysis/LoopDependenceAnalysis.h"
27#include "llvm/Analysis/LoopPass.h"
28#include "llvm/Analysis/ScalarEvolution.h"
Andreas Bolka5eca4522009-08-03 01:03:48 +000029#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Andreas Bolkaf35626d2009-06-28 00:21:21 +000030#include "llvm/Instructions.h"
Andreas Bolka15f72db2009-07-29 05:35:53 +000031#include "llvm/Operator.h"
Andreas Bolka0baa25d2009-07-24 23:19:28 +000032#include "llvm/Support/Allocator.h"
Andreas Bolkae9722fc2009-06-30 02:12:10 +000033#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000034#include "llvm/Support/ErrorHandling.h"
Andreas Bolka0baa25d2009-07-24 23:19:28 +000035#include "llvm/Support/raw_ostream.h"
Andreas Bolkafecbc592009-07-01 21:45:23 +000036#include "llvm/Target/TargetData.h"
Andreas Bolkacb210102009-06-24 21:29:13 +000037using namespace llvm;
38
Andreas Bolka328fb3d2009-07-28 19:49:49 +000039STATISTIC(NumAnswered, "Number of dependence queries answered");
40STATISTIC(NumAnalysed, "Number of distinct dependence pairs analysed");
41STATISTIC(NumDependent, "Number of pairs with dependent accesses");
42STATISTIC(NumIndependent, "Number of pairs with independent accesses");
43STATISTIC(NumUnknown, "Number of pairs with unknown accesses");
44
Andreas Bolkacb210102009-06-24 21:29:13 +000045LoopPass *llvm::createLoopDependenceAnalysisPass() {
46 return new LoopDependenceAnalysis();
47}
48
49static RegisterPass<LoopDependenceAnalysis>
50R("lda", "Loop Dependence Analysis", false, true);
51char LoopDependenceAnalysis::ID = 0;
52
53//===----------------------------------------------------------------------===//
Andreas Bolkaf35626d2009-06-28 00:21:21 +000054// Utility Functions
55//===----------------------------------------------------------------------===//
56
Andreas Bolka2fbb7702009-06-29 18:51:11 +000057static inline bool IsMemRefInstr(const Value *V) {
58 const Instruction *I = dyn_cast<const Instruction>(V);
59 return I && (I->mayReadFromMemory() || I->mayWriteToMemory());
Andreas Bolkaf35626d2009-06-28 00:21:21 +000060}
61
Andreas Bolka3b59dd82009-07-23 01:57:06 +000062static void GetMemRefInstrs(const Loop *L,
63 SmallVectorImpl<Instruction*> &Memrefs) {
Andreas Bolkac6a30302009-06-28 00:35:22 +000064 for (Loop::block_iterator b = L->block_begin(), be = L->block_end();
Andreas Bolka34ce6872009-07-28 19:49:25 +000065 b != be; ++b)
Andreas Bolkac6a30302009-06-28 00:35:22 +000066 for (BasicBlock::iterator i = (*b)->begin(), ie = (*b)->end();
Andreas Bolka34ce6872009-07-28 19:49:25 +000067 i != ie; ++i)
Andreas Bolkaacd6f8d2009-06-29 00:50:26 +000068 if (IsMemRefInstr(i))
Andreas Bolka3b59dd82009-07-23 01:57:06 +000069 Memrefs.push_back(i);
Andreas Bolkac6a30302009-06-28 00:35:22 +000070}
71
Andreas Bolkae9722fc2009-06-30 02:12:10 +000072static bool IsLoadOrStoreInst(Value *I) {
73 return isa<LoadInst>(I) || isa<StoreInst>(I);
74}
75
76static Value *GetPointerOperand(Value *I) {
77 if (LoadInst *i = dyn_cast<LoadInst>(I))
78 return i->getPointerOperand();
79 if (StoreInst *i = dyn_cast<StoreInst>(I))
80 return i->getPointerOperand();
Torok Edwinc23197a2009-07-14 16:55:14 +000081 llvm_unreachable("Value is no load or store instruction!");
Andreas Bolkae9722fc2009-06-30 02:12:10 +000082 // Never reached.
83 return 0;
84}
85
Andreas Bolka34ce6872009-07-28 19:49:25 +000086static AliasAnalysis::AliasResult UnderlyingObjectsAlias(AliasAnalysis *AA,
87 const Value *A,
88 const Value *B) {
89 const Value *aObj = A->getUnderlyingObject();
90 const Value *bObj = B->getUnderlyingObject();
91 return AA->alias(aObj, AA->getTypeStoreSize(aObj->getType()),
92 bObj, AA->getTypeStoreSize(bObj->getType()));
93}
94
Andreas Bolka15f72db2009-07-29 05:35:53 +000095static inline const SCEV *GetZeroSCEV(ScalarEvolution *SE) {
Owen Anderson1d0be152009-08-13 21:58:54 +000096 return SE->getConstant(Type::getInt32Ty(SE->getContext()), 0L);
Andreas Bolka15f72db2009-07-29 05:35:53 +000097}
98
Andreas Bolkaf35626d2009-06-28 00:21:21 +000099//===----------------------------------------------------------------------===//
100// Dependence Testing
101//===----------------------------------------------------------------------===//
102
Andreas Bolka3b59dd82009-07-23 01:57:06 +0000103bool LoopDependenceAnalysis::isDependencePair(const Value *A,
104 const Value *B) const {
105 return IsMemRefInstr(A) &&
106 IsMemRefInstr(B) &&
107 (cast<const Instruction>(A)->mayWriteToMemory() ||
108 cast<const Instruction>(B)->mayWriteToMemory());
Andreas Bolkaf35626d2009-06-28 00:21:21 +0000109}
110
Andreas Bolka34ce6872009-07-28 19:49:25 +0000111bool LoopDependenceAnalysis::findOrInsertDependencePair(Value *A,
112 Value *B,
Andreas Bolkab4c28e92009-07-23 14:32:46 +0000113 DependencePair *&P) {
114 void *insertPos = 0;
115 FoldingSetNodeID id;
Andreas Bolka34ce6872009-07-28 19:49:25 +0000116 id.AddPointer(A);
117 id.AddPointer(B);
Andreas Bolkae9722fc2009-06-30 02:12:10 +0000118
Andreas Bolkab4c28e92009-07-23 14:32:46 +0000119 P = Pairs.FindNodeOrInsertPos(id, insertPos);
120 if (P) return true;
Andreas Bolkae9722fc2009-06-30 02:12:10 +0000121
Andreas Bolkab4c28e92009-07-23 14:32:46 +0000122 P = PairAllocator.Allocate<DependencePair>();
Andreas Bolka34ce6872009-07-28 19:49:25 +0000123 new (P) DependencePair(id, A, B);
Andreas Bolkab4c28e92009-07-23 14:32:46 +0000124 Pairs.InsertNode(P, insertPos);
125 return false;
126}
127
Andreas Bolka699db992009-08-07 18:23:41 +0000128void LoopDependenceAnalysis::getLoops(const SCEV *S,
129 DenseSet<const Loop*>* Loops) const {
130 // Refactor this into an SCEVVisitor, if efficiency becomes a concern.
Andreas Bolka5eca4522009-08-03 01:03:48 +0000131 for (const Loop *L = this->L; L != 0; L = L->getParentLoop())
132 if (!S->isLoopInvariant(L))
Andreas Bolka699db992009-08-07 18:23:41 +0000133 Loops->insert(L);
134}
135
136bool LoopDependenceAnalysis::isLoopInvariant(const SCEV *S) const {
137 DenseSet<const Loop*> loops;
138 getLoops(S, &loops);
139 return loops.empty();
Andreas Bolka5eca4522009-08-03 01:03:48 +0000140}
141
142bool LoopDependenceAnalysis::isAffine(const SCEV *S) const {
143 const SCEVAddRecExpr *rec = dyn_cast<SCEVAddRecExpr>(S);
144 return isLoopInvariant(S) || (rec && rec->isAffine());
145}
146
Andreas Bolka831f6f62009-08-05 04:26:05 +0000147bool LoopDependenceAnalysis::isZIVPair(const SCEV *A, const SCEV *B) const {
148 return isLoopInvariant(A) && isLoopInvariant(B);
149}
150
Andreas Bolka699db992009-08-07 18:23:41 +0000151bool LoopDependenceAnalysis::isSIVPair(const SCEV *A, const SCEV *B) const {
152 DenseSet<const Loop*> loops;
153 getLoops(A, &loops);
154 getLoops(B, &loops);
155 return loops.size() == 1;
156}
157
Andreas Bolka831f6f62009-08-05 04:26:05 +0000158LoopDependenceAnalysis::DependenceResult
159LoopDependenceAnalysis::analyseZIV(const SCEV *A,
160 const SCEV *B,
161 Subscript *S) const {
Andreas Bolka6151f672009-08-06 03:10:33 +0000162 assert(isZIVPair(A, B) && "Attempted to ZIV-test non-ZIV SCEVs!");
163 return A == B ? Dependent : Independent;
Andreas Bolka831f6f62009-08-05 04:26:05 +0000164}
165
Andreas Bolkac3cc45a2009-07-28 19:50:13 +0000166LoopDependenceAnalysis::DependenceResult
Andreas Bolka699db992009-08-07 18:23:41 +0000167LoopDependenceAnalysis::analyseSIV(const SCEV *A,
168 const SCEV *B,
169 Subscript *S) const {
170 return Unknown; // TODO: Implement.
171}
172
173LoopDependenceAnalysis::DependenceResult
174LoopDependenceAnalysis::analyseMIV(const SCEV *A,
175 const SCEV *B,
176 Subscript *S) const {
177 return Unknown; // TODO: Implement.
178}
179
180LoopDependenceAnalysis::DependenceResult
Andreas Bolka15f72db2009-07-29 05:35:53 +0000181LoopDependenceAnalysis::analyseSubscript(const SCEV *A,
182 const SCEV *B,
183 Subscript *S) const {
Andreas Bolka71339592009-07-30 02:26:01 +0000184 DEBUG(errs() << " Testing subscript: " << *A << ", " << *B << "\n");
185
186 if (A == B) {
187 DEBUG(errs() << " -> [D] same SCEV\n");
188 return Dependent;
189 }
190
Andreas Bolka5eca4522009-08-03 01:03:48 +0000191 if (!isAffine(A) || !isAffine(B)) {
192 DEBUG(errs() << " -> [?] not affine\n");
193 return Unknown;
194 }
195
Andreas Bolka831f6f62009-08-05 04:26:05 +0000196 if (isZIVPair(A, B))
197 return analyseZIV(A, B, S);
198
Andreas Bolka699db992009-08-07 18:23:41 +0000199 if (isSIVPair(A, B))
200 return analyseSIV(A, B, S);
Andreas Bolka71339592009-07-30 02:26:01 +0000201
Andreas Bolka699db992009-08-07 18:23:41 +0000202 return analyseMIV(A, B, S);
Andreas Bolka15f72db2009-07-29 05:35:53 +0000203}
204
205LoopDependenceAnalysis::DependenceResult
Andreas Bolkac3cc45a2009-07-28 19:50:13 +0000206LoopDependenceAnalysis::analysePair(DependencePair *P) const {
Andreas Bolka8a8bd3d2009-07-25 12:19:58 +0000207 DEBUG(errs() << "Analysing:\n" << *P->A << "\n" << *P->B << "\n");
Andreas Bolkab4c28e92009-07-23 14:32:46 +0000208
Andreas Bolkab4c28e92009-07-23 14:32:46 +0000209 // We only analyse loads and stores but no possible memory accesses by e.g.
210 // free, call, or invoke instructions.
211 if (!IsLoadOrStoreInst(P->A) || !IsLoadOrStoreInst(P->B)) {
Andreas Bolka8a8bd3d2009-07-25 12:19:58 +0000212 DEBUG(errs() << "--> [?] no load/store\n");
Andreas Bolkac3cc45a2009-07-28 19:50:13 +0000213 return Unknown;
Andreas Bolkab4c28e92009-07-23 14:32:46 +0000214 }
215
Andreas Bolka34ce6872009-07-28 19:49:25 +0000216 Value *aPtr = GetPointerOperand(P->A);
217 Value *bPtr = GetPointerOperand(P->B);
Andreas Bolkae9722fc2009-06-30 02:12:10 +0000218
Andreas Bolka34ce6872009-07-28 19:49:25 +0000219 switch (UnderlyingObjectsAlias(AA, aPtr, bPtr)) {
220 case AliasAnalysis::MayAlias:
221 // We can not analyse objects if we do not know about their aliasing.
Andreas Bolka8a8bd3d2009-07-25 12:19:58 +0000222 DEBUG(errs() << "---> [?] may alias\n");
Andreas Bolkac3cc45a2009-07-28 19:50:13 +0000223 return Unknown;
Andreas Bolkae9722fc2009-06-30 02:12:10 +0000224
Andreas Bolka34ce6872009-07-28 19:49:25 +0000225 case AliasAnalysis::NoAlias:
226 // If the objects noalias, they are distinct, accesses are independent.
Andreas Bolka8a8bd3d2009-07-25 12:19:58 +0000227 DEBUG(errs() << "---> [I] no alias\n");
Andreas Bolkac3cc45a2009-07-28 19:50:13 +0000228 return Independent;
Andreas Bolkae9722fc2009-06-30 02:12:10 +0000229
Andreas Bolka34ce6872009-07-28 19:49:25 +0000230 case AliasAnalysis::MustAlias:
231 break; // The underlying objects alias, test accesses for dependence.
232 }
Andreas Bolkafecbc592009-07-01 21:45:23 +0000233
Andreas Bolka15f72db2009-07-29 05:35:53 +0000234 const GEPOperator *aGEP = dyn_cast<GEPOperator>(aPtr);
235 const GEPOperator *bGEP = dyn_cast<GEPOperator>(bPtr);
236
237 if (!aGEP || !bGEP)
238 return Unknown;
239
240 // FIXME: Is filtering coupled subscripts necessary?
241
Andreas Bolkaa1b78d12009-08-05 04:13:41 +0000242 // Collect GEP operand pairs (FIXME: use GetGEPOperands from BasicAA), adding
Andreas Bolka15f72db2009-07-29 05:35:53 +0000243 // trailing zeroes to the smaller GEP, if needed.
Andreas Bolkaa1b78d12009-08-05 04:13:41 +0000244 typedef SmallVector<std::pair<const SCEV*, const SCEV*>, 4> GEPOpdPairsTy;
245 GEPOpdPairsTy opds;
246 for(GEPOperator::const_op_iterator aIdx = aGEP->idx_begin(),
247 aEnd = aGEP->idx_end(),
248 bIdx = bGEP->idx_begin(),
249 bEnd = bGEP->idx_end();
250 aIdx != aEnd && bIdx != bEnd;
251 aIdx += (aIdx != aEnd), bIdx += (bIdx != bEnd)) {
Andreas Bolka15f72db2009-07-29 05:35:53 +0000252 const SCEV* aSCEV = (aIdx != aEnd) ? SE->getSCEV(*aIdx) : GetZeroSCEV(SE);
253 const SCEV* bSCEV = (bIdx != bEnd) ? SE->getSCEV(*bIdx) : GetZeroSCEV(SE);
Andreas Bolkaa1b78d12009-08-05 04:13:41 +0000254 opds.push_back(std::make_pair(aSCEV, bSCEV));
255 }
256
257 if (!opds.empty() && opds[0].first != opds[0].second) {
258 // We cannot (yet) handle arbitrary GEP pointer offsets. By limiting
259 //
260 // TODO: this could be relaxed by adding the size of the underlying object
261 // to the first subscript. If we have e.g. (GEP x,0,i; GEP x,2,-i) and we
262 // know that x is a [100 x i8]*, we could modify the first subscript to be
263 // (i, 200-i) instead of (i, -i).
264 return Unknown;
265 }
266
267 // Now analyse the collected operand pairs (skipping the GEP ptr offsets).
268 for (GEPOpdPairsTy::const_iterator i = opds.begin() + 1, end = opds.end();
269 i != end; ++i) {
Andreas Bolka15f72db2009-07-29 05:35:53 +0000270 Subscript subscript;
Andreas Bolkaa1b78d12009-08-05 04:13:41 +0000271 DependenceResult result = analyseSubscript(i->first, i->second, &subscript);
Andreas Bolka15f72db2009-07-29 05:35:53 +0000272 if (result != Dependent) {
273 // We either proved independence or failed to analyse this subscript.
274 // Further subscripts will not improve the situation, so abort early.
275 return result;
276 }
277 P->Subscripts.push_back(subscript);
Andreas Bolka15f72db2009-07-29 05:35:53 +0000278 }
Andreas Bolkaa1b78d12009-08-05 04:13:41 +0000279 // We successfully analysed all subscripts but failed to prove independence.
Andreas Bolka15f72db2009-07-29 05:35:53 +0000280 return Dependent;
Andreas Bolkab4c28e92009-07-23 14:32:46 +0000281}
282
283bool LoopDependenceAnalysis::depends(Value *A, Value *B) {
284 assert(isDependencePair(A, B) && "Values form no dependence pair!");
Andreas Bolka328fb3d2009-07-28 19:49:49 +0000285 ++NumAnswered;
Andreas Bolkab4c28e92009-07-23 14:32:46 +0000286
287 DependencePair *p;
288 if (!findOrInsertDependencePair(A, B, p)) {
289 // The pair is not cached, so analyse it.
Andreas Bolka328fb3d2009-07-28 19:49:49 +0000290 ++NumAnalysed;
Andreas Bolkac3cc45a2009-07-28 19:50:13 +0000291 switch (p->Result = analysePair(p)) {
Andreas Bolka328fb3d2009-07-28 19:49:49 +0000292 case Dependent: ++NumDependent; break;
293 case Independent: ++NumIndependent; break;
294 case Unknown: ++NumUnknown; break;
295 }
Andreas Bolkab4c28e92009-07-23 14:32:46 +0000296 }
297 return p->Result != Independent;
Andreas Bolkaf35626d2009-06-28 00:21:21 +0000298}
299
300//===----------------------------------------------------------------------===//
Andreas Bolkacb210102009-06-24 21:29:13 +0000301// LoopDependenceAnalysis Implementation
302//===----------------------------------------------------------------------===//
303
304bool LoopDependenceAnalysis::runOnLoop(Loop *L, LPPassManager &) {
305 this->L = L;
Andreas Bolkafecbc592009-07-01 21:45:23 +0000306 AA = &getAnalysis<AliasAnalysis>();
Andreas Bolkacb210102009-06-24 21:29:13 +0000307 SE = &getAnalysis<ScalarEvolution>();
308 return false;
309}
310
Andreas Bolkab4c28e92009-07-23 14:32:46 +0000311void LoopDependenceAnalysis::releaseMemory() {
312 Pairs.clear();
313 PairAllocator.Reset();
314}
315
Andreas Bolkacb210102009-06-24 21:29:13 +0000316void LoopDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
317 AU.setPreservesAll();
Andreas Bolkafecbc592009-07-01 21:45:23 +0000318 AU.addRequiredTransitive<AliasAnalysis>();
Andreas Bolka707207a2009-06-28 00:16:08 +0000319 AU.addRequiredTransitive<ScalarEvolution>();
320}
321
Andreas Bolka3b59dd82009-07-23 01:57:06 +0000322static void PrintLoopInfo(raw_ostream &OS,
323 LoopDependenceAnalysis *LDA, const Loop *L) {
Andreas Bolka707207a2009-06-28 00:16:08 +0000324 if (!L->empty()) return; // ignore non-innermost loops
325
Andreas Bolka292aef32009-07-03 01:42:52 +0000326 SmallVector<Instruction*, 8> memrefs;
327 GetMemRefInstrs(L, memrefs);
328
Andreas Bolka707207a2009-06-28 00:16:08 +0000329 OS << "Loop at depth " << L->getLoopDepth() << ", header block: ";
330 WriteAsOperand(OS, L->getHeader(), false);
331 OS << "\n";
Andreas Bolkac6a30302009-06-28 00:35:22 +0000332
Andreas Bolkac6a30302009-06-28 00:35:22 +0000333 OS << " Load/store instructions: " << memrefs.size() << "\n";
Andreas Bolka292aef32009-07-03 01:42:52 +0000334 for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(),
Andreas Bolka34ce6872009-07-28 19:49:25 +0000335 end = memrefs.end(); x != end; ++x)
Andreas Bolka3b59dd82009-07-23 01:57:06 +0000336 OS << "\t" << (x - memrefs.begin()) << ": " << **x << "\n";
Andreas Bolka292aef32009-07-03 01:42:52 +0000337
Andreas Bolkac6a30302009-06-28 00:35:22 +0000338 OS << " Pairwise dependence results:\n";
339 for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(),
Andreas Bolka34ce6872009-07-28 19:49:25 +0000340 end = memrefs.end(); x != end; ++x)
Andreas Bolkac6a30302009-06-28 00:35:22 +0000341 for (SmallVector<Instruction*, 8>::const_iterator y = x + 1;
Andreas Bolka34ce6872009-07-28 19:49:25 +0000342 y != end; ++y)
Andreas Bolkac6a30302009-06-28 00:35:22 +0000343 if (LDA->isDependencePair(*x, *y))
344 OS << "\t" << (x - memrefs.begin()) << "," << (y - memrefs.begin())
345 << ": " << (LDA->depends(*x, *y) ? "dependent" : "independent")
346 << "\n";
Andreas Bolka707207a2009-06-28 00:16:08 +0000347}
348
349void LoopDependenceAnalysis::print(raw_ostream &OS, const Module*) const {
Andreas Bolkac6a30302009-06-28 00:35:22 +0000350 // TODO: doc why const_cast is safe
351 PrintLoopInfo(OS, const_cast<LoopDependenceAnalysis*>(this), this->L);
Andreas Bolka707207a2009-06-28 00:16:08 +0000352}