Fix undefined behavior in the Mips backend.
[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/Analysis/ValueTracking.h"
31 #include "llvm/Assembly/Writer.h"
32 #include "llvm/Instructions.h"
33 #include "llvm/Operator.h"
34 #include "llvm/Support/Allocator.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/raw_ostream.h"
38 #include "llvm/Target/TargetData.h"
39 using namespace llvm;
40
41 STATISTIC(NumAnswered,    "Number of dependence queries answered");
42 STATISTIC(NumAnalysed,    "Number of distinct dependence pairs analysed");
43 STATISTIC(NumDependent,   "Number of pairs with dependent accesses");
44 STATISTIC(NumIndependent, "Number of pairs with independent accesses");
45 STATISTIC(NumUnknown,     "Number of pairs with unknown accesses");
46
47 LoopPass *llvm::createLoopDependenceAnalysisPass() {
48   return new LoopDependenceAnalysis();
49 }
50
51 INITIALIZE_PASS_BEGIN(LoopDependenceAnalysis, "lda",
52                 "Loop Dependence Analysis", false, true)
53 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
54 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
55 INITIALIZE_PASS_END(LoopDependenceAnalysis, "lda",
56                 "Loop Dependence Analysis", false, true)
57 char LoopDependenceAnalysis::ID = 0;
58
59 //===----------------------------------------------------------------------===//
60 //                             Utility Functions
61 //===----------------------------------------------------------------------===//
62
63 static inline bool IsMemRefInstr(const Value *V) {
64   const Instruction *I = dyn_cast<const Instruction>(V);
65   return I && (I->mayReadFromMemory() || I->mayWriteToMemory());
66 }
67
68 static void GetMemRefInstrs(const Loop *L,
69                             SmallVectorImpl<Instruction*> &Memrefs) {
70   for (Loop::block_iterator b = L->block_begin(), be = L->block_end();
71        b != be; ++b)
72     for (BasicBlock::iterator i = (*b)->begin(), ie = (*b)->end();
73          i != ie; ++i)
74       if (IsMemRefInstr(i))
75         Memrefs.push_back(i);
76 }
77
78 static bool IsLoadOrStoreInst(Value *I) {
79   // Returns true if the load or store can be analyzed. Atomic and volatile
80   // operations have properties which this analysis does not understand.
81   if (LoadInst *LI = dyn_cast<LoadInst>(I))
82     return LI->isUnordered();
83   else if (StoreInst *SI = dyn_cast<StoreInst>(I))
84     return SI->isUnordered();
85   return false;
86 }
87
88 static Value *GetPointerOperand(Value *I) {
89   if (LoadInst *i = dyn_cast<LoadInst>(I))
90     return i->getPointerOperand();
91   if (StoreInst *i = dyn_cast<StoreInst>(I))
92     return i->getPointerOperand();
93   llvm_unreachable("Value is no load or store instruction!");
94 }
95
96 static AliasAnalysis::AliasResult UnderlyingObjectsAlias(AliasAnalysis *AA,
97                                                          const Value *A,
98                                                          const Value *B) {
99   const Value *aObj = GetUnderlyingObject(A);
100   const Value *bObj = GetUnderlyingObject(B);
101   return AA->alias(aObj, AA->getTypeStoreSize(aObj->getType()),
102                    bObj, AA->getTypeStoreSize(bObj->getType()));
103 }
104
105 static inline const SCEV *GetZeroSCEV(ScalarEvolution *SE) {
106   return SE->getConstant(Type::getInt32Ty(SE->getContext()), 0L);
107 }
108
109 //===----------------------------------------------------------------------===//
110 //                             Dependence Testing
111 //===----------------------------------------------------------------------===//
112
113 bool LoopDependenceAnalysis::isDependencePair(const Value *A,
114                                               const Value *B) const {
115   return IsMemRefInstr(A) &&
116          IsMemRefInstr(B) &&
117          (cast<const Instruction>(A)->mayWriteToMemory() ||
118           cast<const Instruction>(B)->mayWriteToMemory());
119 }
120
121 bool LoopDependenceAnalysis::findOrInsertDependencePair(Value *A,
122                                                         Value *B,
123                                                         DependencePair *&P) {
124   void *insertPos = 0;
125   FoldingSetNodeID id;
126   id.AddPointer(A);
127   id.AddPointer(B);
128
129   P = Pairs.FindNodeOrInsertPos(id, insertPos);
130   if (P) return true;
131
132   P = new (PairAllocator) DependencePair(id, A, B);
133   Pairs.InsertNode(P, insertPos);
134   return false;
135 }
136
137 void LoopDependenceAnalysis::getLoops(const SCEV *S,
138                                       DenseSet<const Loop*>* Loops) const {
139   // Refactor this into an SCEVVisitor, if efficiency becomes a concern.
140   for (const Loop *L = this->L; L != 0; L = L->getParentLoop())
141     if (!SE->isLoopInvariant(S, L))
142       Loops->insert(L);
143 }
144
145 bool LoopDependenceAnalysis::isLoopInvariant(const SCEV *S) const {
146   DenseSet<const Loop*> loops;
147   getLoops(S, &loops);
148   return loops.empty();
149 }
150
151 bool LoopDependenceAnalysis::isAffine(const SCEV *S) const {
152   const SCEVAddRecExpr *rec = dyn_cast<SCEVAddRecExpr>(S);
153   return isLoopInvariant(S) || (rec && rec->isAffine());
154 }
155
156 bool LoopDependenceAnalysis::isZIVPair(const SCEV *A, const SCEV *B) const {
157   return isLoopInvariant(A) && isLoopInvariant(B);
158 }
159
160 bool LoopDependenceAnalysis::isSIVPair(const SCEV *A, const SCEV *B) const {
161   DenseSet<const Loop*> loops;
162   getLoops(A, &loops);
163   getLoops(B, &loops);
164   return loops.size() == 1;
165 }
166
167 LoopDependenceAnalysis::DependenceResult
168 LoopDependenceAnalysis::analyseZIV(const SCEV *A,
169                                    const SCEV *B,
170                                    Subscript *S) const {
171   assert(isZIVPair(A, B) && "Attempted to ZIV-test non-ZIV SCEVs!");
172   return A == B ? Dependent : Independent;
173 }
174
175 LoopDependenceAnalysis::DependenceResult
176 LoopDependenceAnalysis::analyseSIV(const SCEV *A,
177                                    const SCEV *B,
178                                    Subscript *S) const {
179   return Unknown; // TODO: Implement.
180 }
181
182 LoopDependenceAnalysis::DependenceResult
183 LoopDependenceAnalysis::analyseMIV(const SCEV *A,
184                                    const SCEV *B,
185                                    Subscript *S) const {
186   return Unknown; // TODO: Implement.
187 }
188
189 LoopDependenceAnalysis::DependenceResult
190 LoopDependenceAnalysis::analyseSubscript(const SCEV *A,
191                                          const SCEV *B,
192                                          Subscript *S) const {
193   DEBUG(dbgs() << "  Testing subscript: " << *A << ", " << *B << "\n");
194
195   if (A == B) {
196     DEBUG(dbgs() << "  -> [D] same SCEV\n");
197     return Dependent;
198   }
199
200   if (!isAffine(A) || !isAffine(B)) {
201     DEBUG(dbgs() << "  -> [?] not affine\n");
202     return Unknown;
203   }
204
205   if (isZIVPair(A, B))
206     return analyseZIV(A, B, S);
207
208   if (isSIVPair(A, B))
209     return analyseSIV(A, B, S);
210
211   return analyseMIV(A, B, S);
212 }
213
214 LoopDependenceAnalysis::DependenceResult
215 LoopDependenceAnalysis::analysePair(DependencePair *P) const {
216   DEBUG(dbgs() << "Analysing:\n" << *P->A << "\n" << *P->B << "\n");
217
218   // We only analyse loads and stores but no possible memory accesses by e.g.
219   // free, call, or invoke instructions.
220   if (!IsLoadOrStoreInst(P->A) || !IsLoadOrStoreInst(P->B)) {
221     DEBUG(dbgs() << "--> [?] no load/store\n");
222     return Unknown;
223   }
224
225   Value *aPtr = GetPointerOperand(P->A);
226   Value *bPtr = GetPointerOperand(P->B);
227
228   switch (UnderlyingObjectsAlias(AA, aPtr, bPtr)) {
229   case AliasAnalysis::MayAlias:
230   case AliasAnalysis::PartialAlias:
231     // We can not analyse objects if we do not know about their aliasing.
232     DEBUG(dbgs() << "---> [?] may alias\n");
233     return Unknown;
234
235   case AliasAnalysis::NoAlias:
236     // If the objects noalias, they are distinct, accesses are independent.
237     DEBUG(dbgs() << "---> [I] no alias\n");
238     return Independent;
239
240   case AliasAnalysis::MustAlias:
241     break; // The underlying objects alias, test accesses for dependence.
242   }
243
244   const GEPOperator *aGEP = dyn_cast<GEPOperator>(aPtr);
245   const GEPOperator *bGEP = dyn_cast<GEPOperator>(bPtr);
246
247   if (!aGEP || !bGEP)
248     return Unknown;
249
250   // FIXME: Is filtering coupled subscripts necessary?
251
252   // Collect GEP operand pairs (FIXME: use GetGEPOperands from BasicAA), adding
253   // trailing zeroes to the smaller GEP, if needed.
254   typedef SmallVector<std::pair<const SCEV*, const SCEV*>, 4> GEPOpdPairsTy;
255   GEPOpdPairsTy opds;
256   for(GEPOperator::const_op_iterator aIdx = aGEP->idx_begin(),
257                                      aEnd = aGEP->idx_end(),
258                                      bIdx = bGEP->idx_begin(),
259                                      bEnd = bGEP->idx_end();
260       aIdx != aEnd && bIdx != bEnd;
261       aIdx += (aIdx != aEnd), bIdx += (bIdx != bEnd)) {
262     const SCEV* aSCEV = (aIdx != aEnd) ? SE->getSCEV(*aIdx) : GetZeroSCEV(SE);
263     const SCEV* bSCEV = (bIdx != bEnd) ? SE->getSCEV(*bIdx) : GetZeroSCEV(SE);
264     opds.push_back(std::make_pair(aSCEV, bSCEV));
265   }
266
267   if (!opds.empty() && opds[0].first != opds[0].second) {
268     // We cannot (yet) handle arbitrary GEP pointer offsets. By limiting
269     //
270     // TODO: this could be relaxed by adding the size of the underlying object
271     // to the first subscript. If we have e.g. (GEP x,0,i; GEP x,2,-i) and we
272     // know that x is a [100 x i8]*, we could modify the first subscript to be
273     // (i, 200-i) instead of (i, -i).
274     return Unknown;
275   }
276
277   // Now analyse the collected operand pairs (skipping the GEP ptr offsets).
278   for (GEPOpdPairsTy::const_iterator i = opds.begin() + 1, end = opds.end();
279        i != end; ++i) {
280     Subscript subscript;
281     DependenceResult result = analyseSubscript(i->first, i->second, &subscript);
282     if (result != Dependent) {
283       // We either proved independence or failed to analyse this subscript.
284       // Further subscripts will not improve the situation, so abort early.
285       return result;
286     }
287     P->Subscripts.push_back(subscript);
288   }
289   // We successfully analysed all subscripts but failed to prove independence.
290   return Dependent;
291 }
292
293 bool LoopDependenceAnalysis::depends(Value *A, Value *B) {
294   assert(isDependencePair(A, B) && "Values form no dependence pair!");
295   ++NumAnswered;
296
297   DependencePair *p;
298   if (!findOrInsertDependencePair(A, B, p)) {
299     // The pair is not cached, so analyse it.
300     ++NumAnalysed;
301     switch (p->Result = analysePair(p)) {
302     case Dependent:   ++NumDependent;   break;
303     case Independent: ++NumIndependent; break;
304     case Unknown:     ++NumUnknown;     break;
305     }
306   }
307   return p->Result != Independent;
308 }
309
310 //===----------------------------------------------------------------------===//
311 //                   LoopDependenceAnalysis Implementation
312 //===----------------------------------------------------------------------===//
313
314 bool LoopDependenceAnalysis::runOnLoop(Loop *L, LPPassManager &) {
315   this->L = L;
316   AA = &getAnalysis<AliasAnalysis>();
317   SE = &getAnalysis<ScalarEvolution>();
318   return false;
319 }
320
321 void LoopDependenceAnalysis::releaseMemory() {
322   Pairs.clear();
323   PairAllocator.Reset();
324 }
325
326 void LoopDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
327   AU.setPreservesAll();
328   AU.addRequiredTransitive<AliasAnalysis>();
329   AU.addRequiredTransitive<ScalarEvolution>();
330 }
331
332 static void PrintLoopInfo(raw_ostream &OS,
333                           LoopDependenceAnalysis *LDA, const Loop *L) {
334   if (!L->empty()) return; // ignore non-innermost loops
335
336   SmallVector<Instruction*, 8> memrefs;
337   GetMemRefInstrs(L, memrefs);
338
339   OS << "Loop at depth " << L->getLoopDepth() << ", header block: ";
340   WriteAsOperand(OS, L->getHeader(), false);
341   OS << "\n";
342
343   OS << "  Load/store instructions: " << memrefs.size() << "\n";
344   for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(),
345        end = memrefs.end(); x != end; ++x)
346     OS << "\t" << (x - memrefs.begin()) << ": " << **x << "\n";
347
348   OS << "  Pairwise dependence results:\n";
349   for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(),
350        end = memrefs.end(); x != end; ++x)
351     for (SmallVector<Instruction*, 8>::const_iterator y = x + 1;
352          y != end; ++y)
353       if (LDA->isDependencePair(*x, *y))
354         OS << "\t" << (x - memrefs.begin()) << "," << (y - memrefs.begin())
355            << ": " << (LDA->depends(*x, *y) ? "dependent" : "independent")
356            << "\n";
357 }
358
359 void LoopDependenceAnalysis::print(raw_ostream &OS, const Module*) const {
360   // TODO: doc why const_cast is safe
361   PrintLoopInfo(OS, const_cast<LoopDependenceAnalysis*>(this), this->L);
362 }