Make block and function count available via ProfileInfo.
[oota-llvm.git] / lib / Analysis / LoopDependenceAnalysis.cpp
index 51f6c3aafd913f79c0413964ceba7cb4b25740cd..10a6a8eb06f500bdedbf6909a32526ac9d04777f 100644 (file)
@@ -25,6 +25,7 @@
 #include "llvm/Analysis/LoopDependenceAnalysis.h"
 #include "llvm/Analysis/LoopPass.h"
 #include "llvm/Analysis/ScalarEvolution.h"
+#include "llvm/Analysis/ScalarEvolutionExpressions.h"
 #include "llvm/Instructions.h"
 #include "llvm/Operator.h"
 #include "llvm/Support/Allocator.h"
@@ -123,11 +124,54 @@ bool LoopDependenceAnalysis::findOrInsertDependencePair(Value *A,
   return false;
 }
 
+bool LoopDependenceAnalysis::isLoopInvariant(const SCEV *S) const {
+  for (const Loop *L = this->L; L != 0; L = L->getParentLoop())
+    if (!S->isLoopInvariant(L))
+      return false;
+  return true;
+}
+
+bool LoopDependenceAnalysis::isAffine(const SCEV *S) const {
+  const SCEVAddRecExpr *rec = dyn_cast<SCEVAddRecExpr>(S);
+  return isLoopInvariant(S) || (rec && rec->isAffine());
+}
+
+bool LoopDependenceAnalysis::isZIVPair(const SCEV *A, const SCEV *B) const {
+  return isLoopInvariant(A) && isLoopInvariant(B);
+}
+
+LoopDependenceAnalysis::DependenceResult
+LoopDependenceAnalysis::analyseZIV(const SCEV *A,
+                                   const SCEV *B,
+                                   Subscript *S) const {
+  assert(isZIVPair(A, B));
+  const SCEV *diff = SE->getMinusSCEV(A, B);
+  return diff->isZero() ? Dependent : Independent;
+}
+
 LoopDependenceAnalysis::DependenceResult
 LoopDependenceAnalysis::analyseSubscript(const SCEV *A,
                                          const SCEV *B,
                                          Subscript *S) const {
-  return Unknown; // TODO: Implement.
+  DEBUG(errs() << "  Testing subscript: " << *A << ", " << *B << "\n");
+
+  if (A == B) {
+    DEBUG(errs() << "  -> [D] same SCEV\n");
+    return Dependent;
+  }
+
+  if (!isAffine(A) || !isAffine(B)) {
+    DEBUG(errs() << "  -> [?] not affine\n");
+    return Unknown;
+  }
+
+  if (isZIVPair(A, B))
+    return analyseZIV(A, B, S);
+
+  // TODO: Implement SIV/MIV testers.
+
+  DEBUG(errs() << "  -> [?] cannot analyse subscript\n");
+  return Unknown;
 }
 
 LoopDependenceAnalysis::DependenceResult
@@ -167,28 +211,44 @@ LoopDependenceAnalysis::analysePair(DependencePair *P) const {
 
   // FIXME: Is filtering coupled subscripts necessary?
 
-  // Analyse indices pairwise (FIXME: use GetGEPOperands from BasicAA), adding
+  // Collect GEP operand pairs (FIXME: use GetGEPOperands from BasicAA), adding
   // trailing zeroes to the smaller GEP, if needed.
-  GEPOperator::const_op_iterator aIdx = aGEP->idx_begin(),
-                                 aEnd = aGEP->idx_end(),
-                                 bIdx = bGEP->idx_begin(),
-                                 bEnd = bGEP->idx_end();
-  while (aIdx != aEnd && bIdx != bEnd) {
+  typedef SmallVector<std::pair<const SCEV*, const SCEV*>, 4> GEPOpdPairsTy;
+  GEPOpdPairsTy opds;
+  for(GEPOperator::const_op_iterator aIdx = aGEP->idx_begin(),
+                                     aEnd = aGEP->idx_end(),
+                                     bIdx = bGEP->idx_begin(),
+                                     bEnd = bGEP->idx_end();
+      aIdx != aEnd && bIdx != bEnd;
+      aIdx += (aIdx != aEnd), bIdx += (bIdx != bEnd)) {
     const SCEV* aSCEV = (aIdx != aEnd) ? SE->getSCEV(*aIdx) : GetZeroSCEV(SE);
     const SCEV* bSCEV = (bIdx != bEnd) ? SE->getSCEV(*bIdx) : GetZeroSCEV(SE);
+    opds.push_back(std::make_pair(aSCEV, bSCEV));
+  }
+
+  if (!opds.empty() && opds[0].first != opds[0].second) {
+    // We cannot (yet) handle arbitrary GEP pointer offsets. By limiting
+    //
+    // TODO: this could be relaxed by adding the size of the underlying object
+    // to the first subscript. If we have e.g. (GEP x,0,i; GEP x,2,-i) and we
+    // know that x is a [100 x i8]*, we could modify the first subscript to be
+    // (i, 200-i) instead of (i, -i).
+    return Unknown;
+  }
+
+  // Now analyse the collected operand pairs (skipping the GEP ptr offsets).
+  for (GEPOpdPairsTy::const_iterator i = opds.begin() + 1, end = opds.end();
+       i != end; ++i) {
     Subscript subscript;
-    DependenceResult result = analyseSubscript(aSCEV, bSCEV, &subscript);
+    DependenceResult result = analyseSubscript(i->first, i->second, &subscript);
     if (result != Dependent) {
       // We either proved independence or failed to analyse this subscript.
       // Further subscripts will not improve the situation, so abort early.
       return result;
     }
     P->Subscripts.push_back(subscript);
-    if (aIdx != aEnd) ++aIdx;
-    if (bIdx != bEnd) ++bIdx;
   }
-  // Either there were no subscripts or all subscripts were analysed to be
-  // dependent; in both cases we know the accesses are dependent.
+  // We successfully analysed all subscripts but failed to prove independence.
   return Dependent;
 }