Add newlines.
[oota-llvm.git] / lib / Analysis / LoopDependenceAnalysis.cpp
1 //===- 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 //
18 // TODO: document lingo (pair, subscript, index)
19 //
20 //===----------------------------------------------------------------------===//
21
22 #define DEBUG_TYPE "lda"
23 #include "llvm/ADT/DenseSet.h"
24 #include "llvm/ADT/Statistic.h"
25 #include "llvm/Analysis/AliasAnalysis.h"
26 #include "llvm/Analysis/LoopDependenceAnalysis.h"
27 #include "llvm/Analysis/LoopPass.h"
28 #include "llvm/Analysis/ScalarEvolution.h"
29 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
30 #include "llvm/Instructions.h"
31 #include "llvm/Operator.h"
32 #include "llvm/Support/Allocator.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/raw_ostream.h"
36 #include "llvm/Target/TargetData.h"
37 using namespace llvm;
38
39 STATISTIC(NumAnswered,    "Number of dependence queries answered");
40 STATISTIC(NumAnalysed,    "Number of distinct dependence pairs analysed");
41 STATISTIC(NumDependent,   "Number of pairs with dependent accesses");
42 STATISTIC(NumIndependent, "Number of pairs with independent accesses");
43 STATISTIC(NumUnknown,     "Number of pairs with unknown accesses");
44
45 LoopPass *llvm::createLoopDependenceAnalysisPass() {
46   return new LoopDependenceAnalysis();
47 }
48
49 static RegisterPass<LoopDependenceAnalysis>
50 R("lda", "Loop Dependence Analysis", false, true);
51 char LoopDependenceAnalysis::ID = 0;
52
53 //===----------------------------------------------------------------------===//
54 //                             Utility Functions
55 //===----------------------------------------------------------------------===//
56
57 static inline bool IsMemRefInstr(const Value *V) {
58   const Instruction *I = dyn_cast<const Instruction>(V);
59   return I && (I->mayReadFromMemory() || I->mayWriteToMemory());
60 }
61
62 static void GetMemRefInstrs(const Loop *L,
63                             SmallVectorImpl<Instruction*> &Memrefs) {
64   for (Loop::block_iterator b = L->block_begin(), be = L->block_end();
65        b != be; ++b)
66     for (BasicBlock::iterator i = (*b)->begin(), ie = (*b)->end();
67          i != ie; ++i)
68       if (IsMemRefInstr(i))
69         Memrefs.push_back(i);
70 }
71
72 static bool IsLoadOrStoreInst(Value *I) {
73   return isa<LoadInst>(I) || isa<StoreInst>(I);
74 }
75
76 static 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();
81   llvm_unreachable("Value is no load or store instruction!");
82   // Never reached.
83   return 0;
84 }
85
86 static 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
95 static inline const SCEV *GetZeroSCEV(ScalarEvolution *SE) {
96   return SE->getConstant(Type::getInt32Ty(SE->getContext()), 0L);
97 }
98
99 //===----------------------------------------------------------------------===//
100 //                             Dependence Testing
101 //===----------------------------------------------------------------------===//
102
103 bool 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());
109 }
110
111 bool LoopDependenceAnalysis::findOrInsertDependencePair(Value *A,
112                                                         Value *B,
113                                                         DependencePair *&P) {
114   void *insertPos = 0;
115   FoldingSetNodeID id;
116   id.AddPointer(A);
117   id.AddPointer(B);
118
119   P = Pairs.FindNodeOrInsertPos(id, insertPos);
120   if (P) return true;
121
122   P = PairAllocator.Allocate<DependencePair>();
123   new (P) DependencePair(id, A, B);
124   Pairs.InsertNode(P, insertPos);
125   return false;
126 }
127
128 void LoopDependenceAnalysis::getLoops(const SCEV *S,
129                                       DenseSet<const Loop*>* Loops) const {
130   // Refactor this into an SCEVVisitor, if efficiency becomes a concern.
131   for (const Loop *L = this->L; L != 0; L = L->getParentLoop())
132     if (!S->isLoopInvariant(L))
133       Loops->insert(L);
134 }
135
136 bool LoopDependenceAnalysis::isLoopInvariant(const SCEV *S) const {
137   DenseSet<const Loop*> loops;
138   getLoops(S, &loops);
139   return loops.empty();
140 }
141
142 bool LoopDependenceAnalysis::isAffine(const SCEV *S) const {
143   const SCEVAddRecExpr *rec = dyn_cast<SCEVAddRecExpr>(S);
144   return isLoopInvariant(S) || (rec && rec->isAffine());
145 }
146
147 bool LoopDependenceAnalysis::isZIVPair(const SCEV *A, const SCEV *B) const {
148   return isLoopInvariant(A) && isLoopInvariant(B);
149 }
150
151 bool 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
158 LoopDependenceAnalysis::DependenceResult
159 LoopDependenceAnalysis::analyseZIV(const SCEV *A,
160                                    const SCEV *B,
161                                    Subscript *S) const {
162   assert(isZIVPair(A, B) && "Attempted to ZIV-test non-ZIV SCEVs!");
163   return A == B ? Dependent : Independent;
164 }
165
166 LoopDependenceAnalysis::DependenceResult
167 LoopDependenceAnalysis::analyseSIV(const SCEV *A,
168                                    const SCEV *B,
169                                    Subscript *S) const {
170   return Unknown; // TODO: Implement.
171 }
172
173 LoopDependenceAnalysis::DependenceResult
174 LoopDependenceAnalysis::analyseMIV(const SCEV *A,
175                                    const SCEV *B,
176                                    Subscript *S) const {
177   return Unknown; // TODO: Implement.
178 }
179
180 LoopDependenceAnalysis::DependenceResult
181 LoopDependenceAnalysis::analyseSubscript(const SCEV *A,
182                                          const SCEV *B,
183                                          Subscript *S) const {
184   DEBUG(errs() << "  Testing subscript: " << *A << ", " << *B << "\n");
185
186   if (A == B) {
187     DEBUG(errs() << "  -> [D] same SCEV\n");
188     return Dependent;
189   }
190
191   if (!isAffine(A) || !isAffine(B)) {
192     DEBUG(errs() << "  -> [?] not affine\n");
193     return Unknown;
194   }
195
196   if (isZIVPair(A, B))
197     return analyseZIV(A, B, S);
198
199   if (isSIVPair(A, B))
200     return analyseSIV(A, B, S);
201
202   return analyseMIV(A, B, S);
203 }
204
205 LoopDependenceAnalysis::DependenceResult
206 LoopDependenceAnalysis::analysePair(DependencePair *P) const {
207   DEBUG(errs() << "Analysing:\n" << *P->A << "\n" << *P->B << "\n");
208
209   // 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)) {
212     DEBUG(errs() << "--> [?] no load/store\n");
213     return Unknown;
214   }
215
216   Value *aPtr = GetPointerOperand(P->A);
217   Value *bPtr = GetPointerOperand(P->B);
218
219   switch (UnderlyingObjectsAlias(AA, aPtr, bPtr)) {
220   case AliasAnalysis::MayAlias:
221     // We can not analyse objects if we do not know about their aliasing.
222     DEBUG(errs() << "---> [?] may alias\n");
223     return Unknown;
224
225   case AliasAnalysis::NoAlias:
226     // If the objects noalias, they are distinct, accesses are independent.
227     DEBUG(errs() << "---> [I] no alias\n");
228     return Independent;
229
230   case AliasAnalysis::MustAlias:
231     break; // The underlying objects alias, test accesses for dependence.
232   }
233
234   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
242   // Collect GEP operand pairs (FIXME: use GetGEPOperands from BasicAA), adding
243   // trailing zeroes to the smaller GEP, if needed.
244   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)) {
252     const SCEV* aSCEV = (aIdx != aEnd) ? SE->getSCEV(*aIdx) : GetZeroSCEV(SE);
253     const SCEV* bSCEV = (bIdx != bEnd) ? SE->getSCEV(*bIdx) : GetZeroSCEV(SE);
254     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) {
270     Subscript subscript;
271     DependenceResult result = analyseSubscript(i->first, i->second, &subscript);
272     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);
278   }
279   // We successfully analysed all subscripts but failed to prove independence.
280   return Dependent;
281 }
282
283 bool LoopDependenceAnalysis::depends(Value *A, Value *B) {
284   assert(isDependencePair(A, B) && "Values form no dependence pair!");
285   ++NumAnswered;
286
287   DependencePair *p;
288   if (!findOrInsertDependencePair(A, B, p)) {
289     // The pair is not cached, so analyse it.
290     ++NumAnalysed;
291     switch (p->Result = analysePair(p)) {
292     case Dependent:   ++NumDependent;   break;
293     case Independent: ++NumIndependent; break;
294     case Unknown:     ++NumUnknown;     break;
295     }
296   }
297   return p->Result != Independent;
298 }
299
300 //===----------------------------------------------------------------------===//
301 //                   LoopDependenceAnalysis Implementation
302 //===----------------------------------------------------------------------===//
303
304 bool LoopDependenceAnalysis::runOnLoop(Loop *L, LPPassManager &) {
305   this->L = L;
306   AA = &getAnalysis<AliasAnalysis>();
307   SE = &getAnalysis<ScalarEvolution>();
308   return false;
309 }
310
311 void LoopDependenceAnalysis::releaseMemory() {
312   Pairs.clear();
313   PairAllocator.Reset();
314 }
315
316 void LoopDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
317   AU.setPreservesAll();
318   AU.addRequiredTransitive<AliasAnalysis>();
319   AU.addRequiredTransitive<ScalarEvolution>();
320 }
321
322 static void PrintLoopInfo(raw_ostream &OS,
323                           LoopDependenceAnalysis *LDA, const Loop *L) {
324   if (!L->empty()) return; // ignore non-innermost loops
325
326   SmallVector<Instruction*, 8> memrefs;
327   GetMemRefInstrs(L, memrefs);
328
329   OS << "Loop at depth " << L->getLoopDepth() << ", header block: ";
330   WriteAsOperand(OS, L->getHeader(), false);
331   OS << "\n";
332
333   OS << "  Load/store instructions: " << memrefs.size() << "\n";
334   for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(),
335        end = memrefs.end(); x != end; ++x)
336     OS << "\t" << (x - memrefs.begin()) << ": " << **x << "\n";
337
338   OS << "  Pairwise dependence results:\n";
339   for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(),
340        end = memrefs.end(); x != end; ++x)
341     for (SmallVector<Instruction*, 8>::const_iterator y = x + 1;
342          y != end; ++y)
343       if (LDA->isDependencePair(*x, *y))
344         OS << "\t" << (x - memrefs.begin()) << "," << (y - memrefs.begin())
345            << ": " << (LDA->depends(*x, *y) ? "dependent" : "independent")
346            << "\n";
347 }
348
349 void LoopDependenceAnalysis::print(raw_ostream &OS, const Module*) const {
350   // TODO: doc why const_cast is safe
351   PrintLoopInfo(OS, const_cast<LoopDependenceAnalysis*>(this), this->L);
352 }