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