[llvm] [DA] Rewrite BanerjeeMIV test with safe APInt interval arithmetic (PR #207662)

Ruoyu Qiu via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 16 01:53:44 PDT 2026


https://github.com/cabbaken updated https://github.com/llvm/llvm-project/pull/207662

>From 770ccb319ea4b607a5977f5980e0f7861f137a14 Mon Sep 17 00:00:00 2001
From: Ruoyu Qiu <cabbaken at outlook.com>
Date: Mon, 6 Jul 2026 14:12:57 +0800
Subject: [PATCH] [DA] Rewrite BanerjeeMIV test with safe APInt interval
 arithmetic

The old banerjeeMIVtest computed inequality bounds using SCEV
arithmetic on 64-bit integers. Intermediate operations like
$(A^{-} - B^{+}) \times Iterations$ could overflow i64 even when all individual
coefficients and loop bounds fit, producing unsound results.

Replace the symbolic bound machinery with a self-contained APInt
interval arithmetic implementation. Key design decisions:

- BanerjeeInterval holds [Lower, Upper] signed-inclusive bounds.
  Operations use APInt arithmetic at WideBits, chosen to guarantee no
  intermediate overflow:
  $$
    WideBits = max(8, 2 \times BaseBits + MaxLevels + 8)
  $$
  This is provably sufficient, each term product needs at most `2 \times BaseBits + 1`
  bits, and summing across MaxLevels terms needs at most ceil($\log_2 (MaxLevels)$)
  extra bits.

- Only constant affine expressions are handled. `CollectConstantAffine`
  requires nsw on every SCEVAddRecExpr and a SCEVConstant for both the
  step recurrence and the backedge-taken count.
  Symbolic cases bail out conservatively.

Fixes the FIXME in gcdmiv_delta_ovfl2.ll: the old code could not
detect the dependence because intermediate SCEV subtraction of
coefficients near INT64_MIN and INT64_MAX overflowed i64. The new code
computes correct bounds at WideBits >= 138.

Adds a test in banerjee-overflow.ll for single-iteration loops and
updates PR51512.ll for the more precise direction refinement.

Signed-off-by: Ruoyu Qiu <cabbaken at outlook.com>
---
 .../llvm/Analysis/DependenceAnalysis.h        |  76 +-
 llvm/lib/Analysis/DependenceAnalysis.cpp      | 817 +++++++++---------
 .../Analysis/DependenceAnalysis/PR51512.ll    |   2 +-
 .../DependenceAnalysis/banerjee-overflow.ll   | 107 ++-
 .../DependenceAnalysis/gcd-miv-overflow.ll    |   6 +-
 5 files changed, 515 insertions(+), 493 deletions(-)

diff --git a/llvm/include/llvm/Analysis/DependenceAnalysis.h b/llvm/include/llvm/Analysis/DependenceAnalysis.h
index 490fd4520746f..632f94231d0da 100644
--- a/llvm/include/llvm/Analysis/DependenceAnalysis.h
+++ b/llvm/include/llvm/Analysis/DependenceAnalysis.h
@@ -348,16 +348,13 @@ class DependenceInfo {
   };
 
   struct CoefficientInfo {
-    const SCEV *Coeff;
-    const SCEV *PosPart;
-    const SCEV *NegPart;
-    const SCEV *Iterations;
+    APInt Coeff;
+    std::optional<APInt> MaxIterIndex;
   };
 
   struct BoundInfo {
-    const SCEV *Iterations;
-    const SCEV *Upper[8];
-    const SCEV *Lower[8];
+    std::optional<APInt> Upper[8];
+    std::optional<APInt> Lower[8];
     unsigned char Direction;
     unsigned char DirSet;
   };
@@ -609,12 +606,11 @@ class DependenceInfo {
                        const SmallBitVector &Loops,
                        FullDependence &Result) const;
 
-  /// collectCoeffInfo - Walks through the subscript, collecting each
-  /// coefficient, the associated loop bounds, and recording its positive and
-  /// negative parts for later use.
-  void collectCoeffInfo(const SCEV *Subscript, bool SrcFlag,
-                        const SCEV *&Constant,
-                        SmallVectorImpl<CoefficientInfo> &CI) const;
+  /// Walks through the subscript, collecting each constant coefficient and
+  /// the associated maximum iteration index.
+  const SCEV *collectCoeffInfo(const SCEV *Subscript, bool SrcFlag,
+                               unsigned BaseBits, unsigned WideBits,
+                               MutableArrayRef<CoefficientInfo> CI) const;
 
   /// Given \p Expr of the form
   ///
@@ -635,54 +631,28 @@ class DependenceInfo {
                                         const SCEV *&CurLoopCoeff,
                                         APInt &RunningGCD) const;
 
-  /// getPositivePart - X^+ = max(X, 0).
-  const SCEV *getPositivePart(const SCEV *X) const;
-
-  /// getNegativePart - X^- = min(X, 0).
-  const SCEV *getNegativePart(const SCEV *X) const;
-
-  /// getLowerBound - Looks through all the bounds info and
-  /// computes the lower bound given the current direction settings
-  /// at each level.
-  const SCEV *getLowerBound(ArrayRef<BoundInfo> Bound) const;
-
-  /// getUpperBound - Looks through all the bounds info and
-  /// computes the upper bound given the current direction settings
-  /// at each level.
-  const SCEV *getUpperBound(ArrayRef<BoundInfo> Bound) const;
-
-  /// exploreDirections - Hierarchically expands the direction vector
-  /// search space, combining the directions of discovered dependences
-  /// in the DirSet field of Bound. Returns the number of distinct
-  /// dependences discovered. If the dependence is disproved,
-  /// it will return 0.
-  unsigned exploreDirections(unsigned Level, ArrayRef<CoefficientInfo> A,
-                             ArrayRef<CoefficientInfo> B,
-                             MutableArrayRef<BoundInfo> Bound,
-                             const SmallBitVector &Loops,
-                             unsigned &DepthExpanded, const SCEV *Delta) const;
-
-  /// testBounds - Returns true iff the current bounds are plausible.
+  /// Looks through all the bounds info and computes the selected lower bound.
+  std::optional<APInt> getLowerBound(ArrayRef<BoundInfo> Bound) const;
+
+  /// Looks through all the bounds info and computes the selected upper bound.
+  std::optional<APInt> getUpperBound(ArrayRef<BoundInfo> Bound) const;
+
+  /// Hierarchically expands the direction-vector search space.
+  unsigned exploreDirections(unsigned Level, MutableArrayRef<BoundInfo> Bound,
+                             const SmallBitVector &Loops, const APInt &Delta,
+                             const FullDependence &Result) const;
+
+  /// Returns true iff the current bounds are plausible.
   bool testBounds(unsigned char DirKind, unsigned Level,
-                  MutableArrayRef<BoundInfo> Bound, const SCEV *Delta) const;
+                  MutableArrayRef<BoundInfo> Bound, const APInt &Delta) const;
 
-  /// findBoundsALL - Computes the upper and lower bounds for level K
-  /// using the * direction. Records them in Bound.
+  /// Computes bounds for level K using the indicated direction.
   void findBoundsALL(ArrayRef<CoefficientInfo> A, ArrayRef<CoefficientInfo> B,
                      MutableArrayRef<BoundInfo> Bound, unsigned K) const;
-
-  /// findBoundsLT - Computes the upper and lower bounds for level K
-  /// using the < direction. Records them in Bound.
   void findBoundsLT(ArrayRef<CoefficientInfo> A, ArrayRef<CoefficientInfo> B,
                     MutableArrayRef<BoundInfo> Bound, unsigned K) const;
-
-  /// findBoundsGT - Computes the upper and lower bounds for level K
-  /// using the > direction. Records them in Bound.
   void findBoundsGT(ArrayRef<CoefficientInfo> A, ArrayRef<CoefficientInfo> B,
                     MutableArrayRef<BoundInfo> Bound, unsigned K) const;
-
-  /// findBoundsEQ - Computes the upper and lower bounds for level K
-  /// using the = direction. Records them in Bound.
   void findBoundsEQ(ArrayRef<CoefficientInfo> A, ArrayRef<CoefficientInfo> B,
                     MutableArrayRef<BoundInfo> Bound, unsigned K) const;
 
diff --git a/llvm/lib/Analysis/DependenceAnalysis.cpp b/llvm/lib/Analysis/DependenceAnalysis.cpp
index 9d5a555fb8998..4abaea0c98542 100644
--- a/llvm/lib/Analysis/DependenceAnalysis.cpp
+++ b/llvm/lib/Analysis/DependenceAnalysis.cpp
@@ -1922,36 +1922,144 @@ bool DependenceInfo::gcdMIVtest(const SCEV *Src, const SCEV *Dst,
 }
 
 //===----------------------------------------------------------------------===//
+
+namespace {
+struct BanerjeeInterval {
+  std::optional<APInt> Lower;
+  std::optional<APInt> Upper;
+
+  BanerjeeInterval(std::optional<APInt> Lower, std::optional<APInt> Upper)
+      : Lower(std::move(Lower)), Upper(std::move(Upper)) {}
+};
+} // namespace
+
+static APInt signedMin(const APInt &A, const APInt &B) {
+  return A.slt(B) ? A : B;
+}
+
+static APInt signedMax(const APInt &A, const APInt &B) {
+  return A.sgt(B) ? A : B;
+}
+
+static BanerjeeInterval addIntervals(const BanerjeeInterval &A,
+                                     const BanerjeeInterval &B) {
+  std::optional<APInt> Lower;
+  std::optional<APInt> Upper;
+  if (A.Lower && B.Lower)
+    Lower = *A.Lower + *B.Lower;
+  if (A.Upper && B.Upper)
+    Upper = *A.Upper + *B.Upper;
+  return BanerjeeInterval(std::move(Lower), std::move(Upper));
+}
+
+static BanerjeeInterval constantInterval(const APInt &C) {
+  return BanerjeeInterval(C, C);
+}
+
+static BanerjeeInterval emptyInterval(unsigned Bits) {
+  return BanerjeeInterval(APInt(Bits, 1, true), APInt(Bits, 0, true));
+}
+
+static bool isEmptyInterval(const BanerjeeInterval &Interval) {
+  return Interval.Lower && Interval.Upper &&
+         Interval.Lower->sgt(*Interval.Upper);
+}
+
+static BanerjeeInterval signedRangeInterval(const APInt &Coeff,
+                                            const APInt &Lower,
+                                            const APInt &Upper) {
+  APInt LowerValue = Coeff * Lower;
+  APInt UpperValue = Coeff * Upper;
+  return BanerjeeInterval(signedMin(LowerValue, UpperValue),
+                          signedMax(LowerValue, UpperValue));
+}
+
+static BanerjeeInterval variableInterval(const APInt &Coeff,
+                                         const std::optional<APInt> &Upper) {
+  APInt Zero(Coeff.getBitWidth(), 0, true);
+  if (Coeff.isZero())
+    return constantInterval(Zero);
+  if (!Upper) {
+    if (Coeff.isNegative())
+      return BanerjeeInterval(std::nullopt, Zero);
+    return BanerjeeInterval(Zero, std::nullopt);
+  }
+  return signedRangeInterval(Coeff, Zero, *Upper);
+}
+
+static BanerjeeInterval
+unboundedStrictDirectionInterval(const APInt &ACoeff, const APInt &BCoeff,
+                                 unsigned char Direction) {
+  APInt DeltaCoeff = ACoeff - BCoeff;
+
+  switch (Direction) {
+  case Dependence::DVEntry::LT: {
+    APInt Boundary = -BCoeff;
+    std::optional<APInt> Lower;
+    std::optional<APInt> Upper;
+    if (DeltaCoeff.isNonNegative() && BCoeff.isNonPositive())
+      Lower = Boundary;
+    if (DeltaCoeff.isNonPositive() && BCoeff.isNonNegative())
+      Upper = Boundary;
+    return BanerjeeInterval(std::move(Lower), std::move(Upper));
+  }
+  case Dependence::DVEntry::GT: {
+    std::optional<APInt> Lower;
+    std::optional<APInt> Upper;
+    if (ACoeff.isNonNegative() && DeltaCoeff.isNonNegative())
+      Lower = ACoeff;
+    if (ACoeff.isNonPositive() && DeltaCoeff.isNonPositive())
+      Upper = ACoeff;
+    return BanerjeeInterval(std::move(Lower), std::move(Upper));
+  }
+  default:
+    llvm_unreachable("unexpected direction");
+  }
+}
+
+static BanerjeeInterval intervalFromValues(ArrayRef<APInt> Values) {
+  assert(!Values.empty() && "expected at least one value");
+  APInt Lower = Values.front();
+  APInt Upper = Values.front();
+  for (const APInt &Value : Values.drop_front()) {
+    Lower = signedMin(Lower, Value);
+    Upper = signedMax(Upper, Value);
+  }
+  return BanerjeeInterval(std::move(Lower), std::move(Upper));
+}
+
+static APInt evaluateBanerjeeTerm(const APInt &A, const APInt &SrcIndex,
+                                  const APInt &B, const APInt &DstIndex) {
+  return A * SrcIndex - B * DstIndex;
+}
+
+static std::optional<APInt>
+getBanerjeeConstant(const SCEV *Expr, unsigned BaseBits, unsigned WideBits) {
+  const SCEVConstant *C = dyn_cast<SCEVConstant>(Expr);
+  if (!C)
+    return std::nullopt;
+  APInt Value = C->getAPInt();
+  if (!Value.isSignedIntN(BaseBits))
+    return std::nullopt;
+  return Value.sextOrTrunc(WideBits);
+}
+
 // banerjeeMIVtest -
 // Use Banerjee's Inequalities to test an MIV subscript pair.
-// (Wolfe, in the race-car book, calls this the Extreme Value Test.)
-// Generally follows the discussion in Section 2.5.2 of
-//
-//    Optimizing Supercompilers for Supercomputers
-//    Michael Wolfe
+// (Wolfe calls this the Extreme Value Test; see Section 2.5.2 of
+// Optimizing Supercompilers for Supercomputers, Michael Wolfe.)
 //
-// The inequalities given on page 25 are simplified in that loops are
-// normalized so that the lower bound is always 0 and the stride is always 1.
-// For example, Wolfe gives
+// This implementation handles only constant affine expressions and constant
+// loop bounds. The original Wolfe formulae are algebraically simplified for
+// normalized loops (L_k=0, N_k=1); we instead evaluate the subscript
+// difference directly at the vertices of the constraint polytope for each
+// direction (e.g., (0,1), (0,U), (U-1,U) for <). This eliminates the
+// intermediate SCEV arithmetic whose overflow behaviour makes the legacy
+// symbolic bound formulae unsound for LLVM IR.
 //
-//     LB^<_k = (A^-_k - B_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
-//
-// where A_k is the coefficient of the kth index in the source subscript,
-// B_k is the coefficient of the kth index in the destination subscript,
-// U_k is the upper bound of the kth index, L_k is the lower bound of the Kth
-// index, and N_k is the stride of the kth index. Since all loops are normalized
-// by the SCEV package, N_k = 1 and L_k = 0, allowing us to simplify the
-// equation to
-//
-//     LB^<_k = (A^-_k - B_k)^- (U_k - 0 - 1) + (A_k - B_k)0 - B_k 1
-//            = (A^-_k - B_k)^- (U_k - 1)  - B_k
-//
-// Similar simplifications are possible for the other equations.
-//
-// When we can't determine the number of iterations for a loop,
-// we use NULL as an indicator for the worst case, infinity.
-// When computing the upper bound, NULL denotes +inf;
-// for the lower bound, NULL denotes -inf.
+// Loop bounds are backedge-taken counts (maximum normalized iteration
+// index). A single-iteration loop has bound 0, making < and > impossible.
+// Symbolic cases bail out conservatively.
 //
 // Return true if dependence disproved.
 bool DependenceInfo::banerjeeMIVtest(const SCEV *Src, const SCEV *Dst,
@@ -1962,464 +2070,309 @@ bool DependenceInfo::banerjeeMIVtest(const SCEV *Src, const SCEV *Dst,
 
   LLVM_DEBUG(dbgs() << "starting Banerjee\n");
   ++BanerjeeApplications;
-  LLVM_DEBUG(dbgs() << "    Src = " << *Src << '\n');
-  const SCEV *A0;
-  SmallVector<CoefficientInfo, 4> A;
-  collectCoeffInfo(Src, true, A0, A);
-  LLVM_DEBUG(dbgs() << "    Dst = " << *Dst << '\n');
-  const SCEV *B0;
-  SmallVector<CoefficientInfo, 4> B;
-  collectCoeffInfo(Dst, false, B0, B);
-  SmallVector<BoundInfo, 4> Bound(MaxLevels + 1);
-  const SCEV *Delta = minusSCEVNoSignedOverflow(B0, A0, *SE);
-  if (!Delta)
+
+  unsigned SrcBits = SE->getTypeSizeInBits(Src->getType());
+  unsigned DstBits = SE->getTypeSizeInBits(Dst->getType());
+  unsigned BaseBits = std::max(SrcBits, DstBits);
+  // Coefficients fit in signed BaseBits and normalized iteration indices fit
+  // in unsigned BaseBits. Thus, the magnitude of a coefficient/index product
+  // is less than 2^(2*BaseBits-1), and the difference of two such products is
+  // less than 2^(2*BaseBits). Since MaxLevels <= 2^MaxLevels, summing that many
+  // terms needs at most MaxLevels more magnitude bits, plus one bit for the
+  // sign. Consequently none of the APInt operations below can wrap at
+  // WideBits.
+  unsigned WideBits = 2 * BaseBits + MaxLevels + 1;
+  APInt Zero(WideBits, 0, true);
+
+  CoefficientInfo EmptyCoeff{Zero, std::nullopt};
+  SmallVector<CoefficientInfo, 4> A(MaxLevels + 1, EmptyCoeff);
+  SmallVector<CoefficientInfo, 4> B(MaxLevels + 1, EmptyCoeff);
+  assert(Loops.size() > MaxLevels && "loop bit vector is too small");
+  assert(Result.Levels >= CommonLevels &&
+         "direction vector is too small for common levels");
+
+  const SCEV *A0 = collectCoeffInfo(Src, true, BaseBits, WideBits, A);
+  if (!A0)
+    return false;
+  const SCEV *B0 = collectCoeffInfo(Dst, false, BaseBits, WideBits, B);
+  if (!B0)
     return false;
-  LLVM_DEBUG(dbgs() << "\tDelta = " << *Delta << '\n');
 
-  // Compute bounds for all the * directions.
-  LLVM_DEBUG(dbgs() << "\tBounds[*]\n");
-  for (unsigned K = 1; K <= MaxLevels; ++K) {
-    Bound[K].Iterations = A[K].Iterations ? A[K].Iterations : B[K].Iterations;
+  std::optional<APInt> DeltaValue;
+  if (A0 == B0) {
+    DeltaValue = Zero;
+  } else {
+    std::optional<APInt> A0Constant =
+        getBanerjeeConstant(A0, BaseBits, WideBits);
+    std::optional<APInt> B0Constant =
+        getBanerjeeConstant(B0, BaseBits, WideBits);
+    if (A0Constant && B0Constant)
+      DeltaValue = *B0Constant - *A0Constant;
+    if (!DeltaValue && A0->getType() == B0->getType()) {
+      if (const SCEV *DeltaSCEV = minusSCEVNoSignedOverflow(B0, A0, *SE))
+        DeltaValue = getBanerjeeConstant(DeltaSCEV, BaseBits, WideBits);
+    }
+  }
+  if (!DeltaValue)
+    return false;
+
+  APInt Delta = *DeltaValue;
+  LLVM_DEBUG(dbgs() << "\tDelta = " << Delta << '\n');
+
+  SmallVector<BoundInfo, 4> Bound(MaxLevels + 1);
+  for (unsigned K = 0; K <= MaxLevels; ++K) {
     Bound[K].Direction = Dependence::DVEntry::ALL;
     Bound[K].DirSet = Dependence::DVEntry::NONE;
+  }
+  for (unsigned K = 1; K <= MaxLevels; ++K) {
     findBoundsALL(A, B, Bound, K);
-#ifndef NDEBUG
-    LLVM_DEBUG(dbgs() << "\t    " << K << '\t');
-    if (Bound[K].Lower[Dependence::DVEntry::ALL])
-      LLVM_DEBUG(dbgs() << *Bound[K].Lower[Dependence::DVEntry::ALL] << '\t');
-    else
-      LLVM_DEBUG(dbgs() << "-inf\t");
-    if (Bound[K].Upper[Dependence::DVEntry::ALL])
-      LLVM_DEBUG(dbgs() << *Bound[K].Upper[Dependence::DVEntry::ALL] << '\n');
-    else
-      LLVM_DEBUG(dbgs() << "+inf\n");
-#endif
+    findBoundsLT(A, B, Bound, K);
+    findBoundsEQ(A, B, Bound, K);
+    findBoundsGT(A, B, Bound, K);
   }
 
-  // Test the *, *, *, ... case.
-  bool Disproved = false;
-  if (testBounds(Dependence::DVEntry::ALL, 0, Bound, Delta)) {
-    // Explore the direction vector hierarchy.
-    unsigned DepthExpanded = 0;
-    unsigned NewDeps =
-        exploreDirections(1, A, B, Bound, Loops, DepthExpanded, Delta);
-    if (NewDeps > 0) {
-      bool Improved = false;
-      for (unsigned K = 1; K <= CommonLevels; ++K) {
-        if (Loops[K]) {
-          unsigned Old = Result.DV[K - 1].Direction;
-          Result.DV[K - 1].Direction = Old & Bound[K].DirSet;
-          Improved |= Old != Result.DV[K - 1].Direction;
-          if (!Result.DV[K - 1].Direction) {
-            Improved = false;
-            Disproved = true;
-            break;
-          }
-        }
-      }
-      if (Improved)
-        ++BanerjeeSuccesses;
-    } else {
-      ++BanerjeeIndependence;
-      Disproved = true;
-    }
-  } else {
+  if (!testBounds(Dependence::DVEntry::ALL, 0, Bound, Delta)) {
     ++BanerjeeIndependence;
-    Disproved = true;
-  }
-  return Disproved;
-}
-
-// Hierarchically expands the direction vector
-// search space, combining the directions of discovered dependences
-// in the DirSet field of Bound. Returns the number of distinct
-// dependences discovered. If the dependence is disproved,
-// it will return 0.
-unsigned DependenceInfo::exploreDirections(
-    unsigned Level, ArrayRef<CoefficientInfo> A, ArrayRef<CoefficientInfo> B,
-    MutableArrayRef<BoundInfo> Bound, const SmallBitVector &Loops,
-    unsigned &DepthExpanded, const SCEV *Delta) const {
-  // This algorithm has worst case complexity of O(3^n), where 'n' is the number
-  // of common loop levels. To avoid excessive compile-time, pessimize all the
-  // results and immediately return when the number of common levels is beyond
-  // the given threshold.
-  if (CommonLevels > MIVMaxLevelThreshold) {
-    LLVM_DEBUG(dbgs() << "Number of common levels exceeded the threshold. MIV "
-                         "direction exploration is terminated.\n");
-    for (unsigned K = 1; K <= CommonLevels; ++K)
-      if (Loops[K])
-        Bound[K].DirSet = Dependence::DVEntry::ALL;
-    return 1;
+    return true;
   }
 
-  if (Level > CommonLevels) {
-    // record result
-    LLVM_DEBUG(dbgs() << "\t[");
-    for (unsigned K = 1; K <= CommonLevels; ++K) {
-      if (Loops[K]) {
-        Bound[K].DirSet |= Bound[K].Direction;
-#ifndef NDEBUG
-        switch (Bound[K].Direction) {
-        case Dependence::DVEntry::LT:
-          LLVM_DEBUG(dbgs() << " <");
-          break;
-        case Dependence::DVEntry::EQ:
-          LLVM_DEBUG(dbgs() << " =");
-          break;
-        case Dependence::DVEntry::GT:
-          LLVM_DEBUG(dbgs() << " >");
-          break;
-        case Dependence::DVEntry::ALL:
-          LLVM_DEBUG(dbgs() << " *");
-          break;
-        default:
-          llvm_unreachable("unexpected Bound[K].Direction");
-        }
-#endif
-      }
-    }
-    LLVM_DEBUG(dbgs() << " ]\n");
-    return 1;
+  unsigned NewDeps = exploreDirections(1, Bound, Loops, Delta, Result);
+  if (NewDeps == 0) {
+    ++BanerjeeIndependence;
+    return true;
   }
-  if (Loops[Level]) {
-    if (Level > DepthExpanded) {
-      DepthExpanded = Level;
-      // compute bounds for <, =, > at current level
-      findBoundsLT(A, B, Bound, Level);
-      findBoundsGT(A, B, Bound, Level);
-      findBoundsEQ(A, B, Bound, Level);
-#ifndef NDEBUG
-      LLVM_DEBUG(dbgs() << "\tBound for level = " << Level << '\n');
-      LLVM_DEBUG(dbgs() << "\t    <\t");
-      if (Bound[Level].Lower[Dependence::DVEntry::LT])
-        LLVM_DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::LT]
-                          << '\t');
-      else
-        LLVM_DEBUG(dbgs() << "-inf\t");
-      if (Bound[Level].Upper[Dependence::DVEntry::LT])
-        LLVM_DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::LT]
-                          << '\n');
-      else
-        LLVM_DEBUG(dbgs() << "+inf\n");
-      LLVM_DEBUG(dbgs() << "\t    =\t");
-      if (Bound[Level].Lower[Dependence::DVEntry::EQ])
-        LLVM_DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::EQ]
-                          << '\t');
-      else
-        LLVM_DEBUG(dbgs() << "-inf\t");
-      if (Bound[Level].Upper[Dependence::DVEntry::EQ])
-        LLVM_DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::EQ]
-                          << '\n');
-      else
-        LLVM_DEBUG(dbgs() << "+inf\n");
-      LLVM_DEBUG(dbgs() << "\t    >\t");
-      if (Bound[Level].Lower[Dependence::DVEntry::GT])
-        LLVM_DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::GT]
-                          << '\t');
-      else
-        LLVM_DEBUG(dbgs() << "-inf\t");
-      if (Bound[Level].Upper[Dependence::DVEntry::GT])
-        LLVM_DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::GT]
-                          << '\n');
-      else
-        LLVM_DEBUG(dbgs() << "+inf\n");
-#endif
+
+  bool Improved = false;
+  for (unsigned K = 1; K <= CommonLevels; ++K) {
+    if (!Loops[K])
+      continue;
+    unsigned Old = Result.DV[K - 1].Direction;
+    Result.DV[K - 1].Direction = Old & Bound[K].DirSet;
+    Improved |= Old != Result.DV[K - 1].Direction;
+    if (!Result.DV[K - 1].Direction) {
+      ++BanerjeeIndependence;
+      return true;
     }
+  }
+
+  if (Improved)
+    ++BanerjeeSuccesses;
+  return false;
+}
 
-    unsigned NewDeps = 0;
+// Walks through the subscript and collects the constant coefficient and
+// maximum iteration index associated with each loop level.
+const SCEV *
+DependenceInfo::collectCoeffInfo(const SCEV *Subscript, bool SrcFlag,
+                                 unsigned BaseBits, unsigned WideBits,
+                                 MutableArrayRef<CoefficientInfo> CI) const {
+  SmallBitVector SeenLevels(MaxLevels + 1);
+  while (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Subscript)) {
+    unsigned K =
+        SrcFlag ? mapSrcLoop(AddRec->getLoop()) : mapDstLoop(AddRec->getLoop());
+    if (K == 0 || K > MaxLevels || SeenLevels[K])
+      return nullptr;
+    SeenLevels.set(K);
 
-    // test bounds for <, *, *, ...
-    if (testBounds(Dependence::DVEntry::LT, Level, Bound, Delta))
-      NewDeps += exploreDirections(Level + 1, A, B, Bound, Loops, DepthExpanded,
-                                   Delta);
+    std::optional<APInt> Step =
+        getBanerjeeConstant(AddRec->getStepRecurrence(*SE), BaseBits, WideBits);
+    if (!Step)
+      return nullptr;
+    CI[K].Coeff = *Step;
 
-    // Test bounds for =, *, *, ...
-    if (testBounds(Dependence::DVEntry::EQ, Level, Bound, Delta))
-      NewDeps += exploreDirections(Level + 1, A, B, Bound, Loops, DepthExpanded,
-                                   Delta);
+    if (std::optional<APInt> MaxIndex = collectNonNegativeConstantUpperBound(
+            AddRec->getLoop(), Subscript->getType()))
+      CI[K].MaxIterIndex = MaxIndex->zextOrTrunc(WideBits);
 
-    // test bounds for >, *, *, ...
-    if (testBounds(Dependence::DVEntry::GT, Level, Bound, Delta))
-      NewDeps += exploreDirections(Level + 1, A, B, Bound, Loops, DepthExpanded,
-                                   Delta);
+    Subscript = AddRec->getStart();
+  }
+  return Subscript;
+}
 
-    Bound[Level].Direction = Dependence::DVEntry::ALL;
-    return NewDeps;
-  } else
-    return exploreDirections(Level + 1, A, B, Bound, Loops, DepthExpanded,
-                             Delta);
+// Looks through all the bounds info and computes the selected lower bound.
+std::optional<APInt>
+DependenceInfo::getLowerBound(ArrayRef<BoundInfo> Bound) const {
+  std::optional<APInt> Sum = Bound[1].Lower[Bound[1].Direction];
+  for (unsigned K = 2; Sum && K <= MaxLevels; ++K) {
+    if (!Bound[K].Lower[Bound[K].Direction])
+      return std::nullopt;
+    *Sum += *Bound[K].Lower[Bound[K].Direction];
+  }
+  return Sum;
+}
+
+// Looks through all the bounds info and computes the selected upper bound.
+std::optional<APInt>
+DependenceInfo::getUpperBound(ArrayRef<BoundInfo> Bound) const {
+  std::optional<APInt> Sum = Bound[1].Upper[Bound[1].Direction];
+  for (unsigned K = 2; Sum && K <= MaxLevels; ++K) {
+    if (!Bound[K].Upper[Bound[K].Direction])
+      return std::nullopt;
+    *Sum += *Bound[K].Upper[Bound[K].Direction];
+  }
+  return Sum;
 }
 
 // Returns true iff the current bounds are plausible.
 bool DependenceInfo::testBounds(unsigned char DirKind, unsigned Level,
                                 MutableArrayRef<BoundInfo> Bound,
-                                const SCEV *Delta) const {
+                                const APInt &Delta) const {
   Bound[Level].Direction = DirKind;
-  if (const SCEV *LowerBound = getLowerBound(Bound))
-    if (SE->isKnownPredicate(CmpInst::ICMP_SGT, LowerBound, Delta))
+  for (unsigned K = 1; K <= MaxLevels; ++K) {
+    unsigned char Direction = Bound[K].Direction;
+    BanerjeeInterval Interval(Bound[K].Lower[Direction],
+                              Bound[K].Upper[Direction]);
+    if (isEmptyInterval(Interval))
+      return false;
+  }
+  if (std::optional<APInt> Lower = getLowerBound(Bound))
+    if (Lower->sgt(Delta))
       return false;
-  if (const SCEV *UpperBound = getUpperBound(Bound))
-    if (SE->isKnownPredicate(CmpInst::ICMP_SGT, Delta, UpperBound))
+  if (std::optional<APInt> Upper = getUpperBound(Bound))
+    if (Delta.sgt(*Upper))
       return false;
   return true;
 }
 
-// Computes the upper and lower bounds for level K
-// using the * direction. Records them in Bound.
-// Wolfe gives the equations
-//
-//    LB^*_k = (A^-_k - B^+_k)(U_k - L_k) + (A_k - B_k)L_k
-//    UB^*_k = (A^+_k - B^-_k)(U_k - L_k) + (A_k - B_k)L_k
-//
-// Since we normalize loops, we can simplify these equations to
-//
-//    LB^*_k = (A^-_k - B^+_k)U_k
-//    UB^*_k = (A^+_k - B^-_k)U_k
-//
-// We must be careful to handle the case where the upper bound is unknown.
-// Note that the lower bound is always <= 0
-// and the upper bound is always >= 0.
+// Hierarchically expands the direction-vector search space.
+unsigned DependenceInfo::exploreDirections(unsigned Level,
+                                           MutableArrayRef<BoundInfo> Bound,
+                                           const SmallBitVector &Loops,
+                                           const APInt &Delta,
+                                           const FullDependence &Result) const {
+  if (CommonLevels > MIVMaxLevelThreshold) {
+    LLVM_DEBUG(dbgs() << "Number of common levels exceeded the threshold. MIV "
+                         "direction exploration is terminated.\n");
+    for (unsigned K = 1; K <= CommonLevels; ++K)
+      if (Loops[K])
+        Bound[K].DirSet = Dependence::DVEntry::ALL;
+    return 1;
+  }
+
+  if (Level > CommonLevels) {
+    for (unsigned K = 1; K <= CommonLevels; ++K)
+      if (Loops[K])
+        Bound[K].DirSet |= Bound[K].Direction;
+    return 1;
+  }
+
+  if (!Loops[Level])
+    return exploreDirections(Level + 1, Bound, Loops, Delta, Result);
+
+  unsigned NewDeps = 0;
+  unsigned OldDirections = Result.DV[Level - 1].Direction;
+  for (unsigned char Dir : {Dependence::DVEntry::LT, Dependence::DVEntry::EQ,
+                            Dependence::DVEntry::GT}) {
+    if (!(OldDirections & Dir))
+      continue;
+    if (testBounds(Dir, Level, Bound, Delta))
+      NewDeps += exploreDirections(Level + 1, Bound, Loops, Delta, Result);
+  }
+  Bound[Level].Direction = Dependence::DVEntry::ALL;
+  return NewDeps;
+}
+
+// Computes the lower and upper bounds for level K using the * direction.
 void DependenceInfo::findBoundsALL(ArrayRef<CoefficientInfo> A,
                                    ArrayRef<CoefficientInfo> B,
                                    MutableArrayRef<BoundInfo> Bound,
                                    unsigned K) const {
-  Bound[K].Lower[Dependence::DVEntry::ALL] =
-      nullptr; // Default value = -infinity.
-  Bound[K].Upper[Dependence::DVEntry::ALL] =
-      nullptr; // Default value = +infinity.
-  if (Bound[K].Iterations) {
-    Bound[K].Lower[Dependence::DVEntry::ALL] = SE->getMulExpr(
-        SE->getMinusSCEV(A[K].NegPart, B[K].PosPart), Bound[K].Iterations);
-    Bound[K].Upper[Dependence::DVEntry::ALL] = SE->getMulExpr(
-        SE->getMinusSCEV(A[K].PosPart, B[K].NegPart), Bound[K].Iterations);
-  } else {
-    // If the difference is 0, we won't need to know the number of iterations.
-    if (SE->isKnownPredicate(CmpInst::ICMP_EQ, A[K].NegPart, B[K].PosPart))
-      Bound[K].Lower[Dependence::DVEntry::ALL] =
-          SE->getZero(A[K].Coeff->getType());
-    if (SE->isKnownPredicate(CmpInst::ICMP_EQ, A[K].PosPart, B[K].NegPart))
-      Bound[K].Upper[Dependence::DVEntry::ALL] =
-          SE->getZero(A[K].Coeff->getType());
-  }
+  BanerjeeInterval SrcInterval =
+      variableInterval(A[K].Coeff, A[K].MaxIterIndex);
+  BanerjeeInterval DstInterval =
+      variableInterval(-B[K].Coeff, B[K].MaxIterIndex);
+  BanerjeeInterval Interval = addIntervals(SrcInterval, DstInterval);
+  Bound[K].Lower[Dependence::DVEntry::ALL] = std::move(Interval.Lower);
+  Bound[K].Upper[Dependence::DVEntry::ALL] = std::move(Interval.Upper);
 }
 
-// Computes the upper and lower bounds for level K
-// using the = direction. Records them in Bound.
-// Wolfe gives the equations
-//
-//    LB^=_k = (A_k - B_k)^- (U_k - L_k) + (A_k - B_k)L_k
-//    UB^=_k = (A_k - B_k)^+ (U_k - L_k) + (A_k - B_k)L_k
-//
-// Since we normalize loops, we can simplify these equations to
-//
-//    LB^=_k = (A_k - B_k)^- U_k
-//    UB^=_k = (A_k - B_k)^+ U_k
-//
-// We must be careful to handle the case where the upper bound is unknown.
-// Note that the lower bound is always <= 0
-// and the upper bound is always >= 0.
+// Computes the lower and upper bounds for level K using the = direction.
 void DependenceInfo::findBoundsEQ(ArrayRef<CoefficientInfo> A,
                                   ArrayRef<CoefficientInfo> B,
                                   MutableArrayRef<BoundInfo> Bound,
                                   unsigned K) const {
-  Bound[K].Lower[Dependence::DVEntry::EQ] =
-      nullptr; // Default value = -infinity.
-  Bound[K].Upper[Dependence::DVEntry::EQ] =
-      nullptr; // Default value = +infinity.
-  if (Bound[K].Iterations) {
-    const SCEV *Delta = SE->getMinusSCEV(A[K].Coeff, B[K].Coeff);
-    const SCEV *NegativePart = getNegativePart(Delta);
-    Bound[K].Lower[Dependence::DVEntry::EQ] =
-        SE->getMulExpr(NegativePart, Bound[K].Iterations);
-    const SCEV *PositivePart = getPositivePart(Delta);
-    Bound[K].Upper[Dependence::DVEntry::EQ] =
-        SE->getMulExpr(PositivePart, Bound[K].Iterations);
-  } else {
-    // If the positive/negative part of the difference is 0,
-    // we won't need to know the number of iterations.
-    const SCEV *Delta = SE->getMinusSCEV(A[K].Coeff, B[K].Coeff);
-    const SCEV *NegativePart = getNegativePart(Delta);
-    if (NegativePart->isZero())
-      Bound[K].Lower[Dependence::DVEntry::EQ] = NegativePart; // Zero
-    const SCEV *PositivePart = getPositivePart(Delta);
-    if (PositivePart->isZero())
-      Bound[K].Upper[Dependence::DVEntry::EQ] = PositivePart; // Zero
-  }
+  std::optional<APInt> MaxIterIndex;
+  if (A[K].MaxIterIndex && B[K].MaxIterIndex)
+    MaxIterIndex = signedMin(*A[K].MaxIterIndex, *B[K].MaxIterIndex);
+  else
+    MaxIterIndex = A[K].MaxIterIndex ? A[K].MaxIterIndex : B[K].MaxIterIndex;
+
+  BanerjeeInterval Interval =
+      variableInterval(A[K].Coeff - B[K].Coeff, MaxIterIndex);
+  Bound[K].Lower[Dependence::DVEntry::EQ] = std::move(Interval.Lower);
+  Bound[K].Upper[Dependence::DVEntry::EQ] = std::move(Interval.Upper);
 }
 
-// Computes the upper and lower bounds for level K
-// using the < direction. Records them in Bound.
-// Wolfe gives the equations
-//
-//    LB^<_k = (A^-_k - B_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
-//    UB^<_k = (A^+_k - B_k)^+ (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
-//
-// Since we normalize loops, we can simplify these equations to
-//
-//    LB^<_k = (A^-_k - B_k)^- (U_k - 1) - B_k
-//    UB^<_k = (A^+_k - B_k)^+ (U_k - 1) - B_k
-//
-// We must be careful to handle the case where the upper bound is unknown.
+// Computes the lower and upper bounds for level K using the < direction.
 void DependenceInfo::findBoundsLT(ArrayRef<CoefficientInfo> A,
                                   ArrayRef<CoefficientInfo> B,
                                   MutableArrayRef<BoundInfo> Bound,
                                   unsigned K) const {
-  Bound[K].Lower[Dependence::DVEntry::LT] =
-      nullptr; // Default value = -infinity.
-  Bound[K].Upper[Dependence::DVEntry::LT] =
-      nullptr; // Default value = +infinity.
-  if (Bound[K].Iterations) {
-    const SCEV *Iter_1 = SE->getMinusSCEV(
-        Bound[K].Iterations, SE->getOne(Bound[K].Iterations->getType()));
-    const SCEV *NegPart =
-        getNegativePart(SE->getMinusSCEV(A[K].NegPart, B[K].Coeff));
-    Bound[K].Lower[Dependence::DVEntry::LT] =
-        SE->getMinusSCEV(SE->getMulExpr(NegPart, Iter_1), B[K].Coeff);
-    const SCEV *PosPart =
-        getPositivePart(SE->getMinusSCEV(A[K].PosPart, B[K].Coeff));
-    Bound[K].Upper[Dependence::DVEntry::LT] =
-        SE->getMinusSCEV(SE->getMulExpr(PosPart, Iter_1), B[K].Coeff);
+  const APInt &ACoeff = A[K].Coeff;
+  const APInt &BCoeff = B[K].Coeff;
+  std::optional<APInt> MaxIterIndex;
+  if (A[K].MaxIterIndex && B[K].MaxIterIndex) {
+    if (*A[K].MaxIterIndex == *B[K].MaxIterIndex)
+      MaxIterIndex = A[K].MaxIterIndex;
   } else {
-    // If the positive/negative part of the difference is 0,
-    // we won't need to know the number of iterations.
-    const SCEV *NegPart =
-        getNegativePart(SE->getMinusSCEV(A[K].NegPart, B[K].Coeff));
-    if (NegPart->isZero())
-      Bound[K].Lower[Dependence::DVEntry::LT] = SE->getNegativeSCEV(B[K].Coeff);
-    const SCEV *PosPart =
-        getPositivePart(SE->getMinusSCEV(A[K].PosPart, B[K].Coeff));
-    if (PosPart->isZero())
-      Bound[K].Upper[Dependence::DVEntry::LT] = SE->getNegativeSCEV(B[K].Coeff);
-  }
-}
-
-// Computes the upper and lower bounds for level K
-// using the > direction. Records them in Bound.
-// Wolfe gives the equations
-//
-//    LB^>_k = (A_k - B^+_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k + A_k N_k
-//    UB^>_k = (A_k - B^-_k)^+ (U_k - L_k - N_k) + (A_k - B_k)L_k + A_k N_k
-//
-// Since we normalize loops, we can simplify these equations to
-//
-//    LB^>_k = (A_k - B^+_k)^- (U_k - 1) + A_k
-//    UB^>_k = (A_k - B^-_k)^+ (U_k - 1) + A_k
-//
-// We must be careful to handle the case where the upper bound is unknown.
-void DependenceInfo::findBoundsGT(ArrayRef<CoefficientInfo> A,
-                                  ArrayRef<CoefficientInfo> B,
-                                  MutableArrayRef<BoundInfo> Bound,
-                                  unsigned K) const {
-  Bound[K].Lower[Dependence::DVEntry::GT] =
-      nullptr; // Default value = -infinity.
-  Bound[K].Upper[Dependence::DVEntry::GT] =
-      nullptr; // Default value = +infinity.
-  if (Bound[K].Iterations) {
-    const SCEV *Iter_1 = SE->getMinusSCEV(
-        Bound[K].Iterations, SE->getOne(Bound[K].Iterations->getType()));
-    const SCEV *NegPart =
-        getNegativePart(SE->getMinusSCEV(A[K].Coeff, B[K].PosPart));
-    Bound[K].Lower[Dependence::DVEntry::GT] =
-        SE->getAddExpr(SE->getMulExpr(NegPart, Iter_1), A[K].Coeff);
-    const SCEV *PosPart =
-        getPositivePart(SE->getMinusSCEV(A[K].Coeff, B[K].NegPart));
-    Bound[K].Upper[Dependence::DVEntry::GT] =
-        SE->getAddExpr(SE->getMulExpr(PosPart, Iter_1), A[K].Coeff);
-  } else {
-    // If the positive/negative part of the difference is 0,
-    // we won't need to know the number of iterations.
-    const SCEV *NegPart =
-        getNegativePart(SE->getMinusSCEV(A[K].Coeff, B[K].PosPart));
-    if (NegPart->isZero())
-      Bound[K].Lower[Dependence::DVEntry::GT] = A[K].Coeff;
-    const SCEV *PosPart =
-        getPositivePart(SE->getMinusSCEV(A[K].Coeff, B[K].NegPart));
-    if (PosPart->isZero())
-      Bound[K].Upper[Dependence::DVEntry::GT] = A[K].Coeff;
+    MaxIterIndex = A[K].MaxIterIndex ? A[K].MaxIterIndex : B[K].MaxIterIndex;
   }
-}
-
-// X^+ = max(X, 0)
-const SCEV *DependenceInfo::getPositivePart(const SCEV *X) const {
-  return SE->getSMaxExpr(X, SE->getZero(X->getType()));
-}
-
-// X^- = min(X, 0)
-const SCEV *DependenceInfo::getNegativePart(const SCEV *X) const {
-  return SE->getSMinExpr(X, SE->getZero(X->getType()));
-}
 
-// Walks through the subscript,
-// collecting each coefficient, the associated loop bounds,
-// and recording its positive and negative parts for later use.
-void DependenceInfo::collectCoeffInfo(
-    const SCEV *Subscript, bool SrcFlag, const SCEV *&Constant,
-    SmallVectorImpl<CoefficientInfo> &CI) const {
-  const SCEV *Zero = SE->getZero(Subscript->getType());
-  CI.resize(MaxLevels + 1);
-  for (unsigned K = 1; K <= MaxLevels; ++K) {
-    CI[K].Coeff = Zero;
-    CI[K].PosPart = Zero;
-    CI[K].NegPart = Zero;
-    CI[K].Iterations = nullptr;
-  }
-  while (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Subscript)) {
-    const Loop *L = AddRec->getLoop();
-    unsigned K = SrcFlag ? mapSrcLoop(L) : mapDstLoop(L);
-    CI[K].Coeff = AddRec->getStepRecurrence(*SE);
-    CI[K].PosPart = getPositivePart(CI[K].Coeff);
-    CI[K].NegPart = getNegativePart(CI[K].Coeff);
-    CI[K].Iterations = collectUpperBound(L, Subscript->getType());
-    Subscript = AddRec->getStart();
-  }
-  Constant = Subscript;
-#ifndef NDEBUG
-  LLVM_DEBUG(dbgs() << "\tCoefficient Info\n");
-  for (unsigned K = 1; K <= MaxLevels; ++K) {
-    LLVM_DEBUG(dbgs() << "\t    " << K << "\t" << *CI[K].Coeff);
-    LLVM_DEBUG(dbgs() << "\tPos Part = ");
-    LLVM_DEBUG(dbgs() << *CI[K].PosPart);
-    LLVM_DEBUG(dbgs() << "\tNeg Part = ");
-    LLVM_DEBUG(dbgs() << *CI[K].NegPart);
-    LLVM_DEBUG(dbgs() << "\tUpper Bound = ");
-    if (CI[K].Iterations)
-      LLVM_DEBUG(dbgs() << *CI[K].Iterations);
-    else
-      LLVM_DEBUG(dbgs() << "+inf");
-    LLVM_DEBUG(dbgs() << '\n');
+  BanerjeeInterval Interval(std::nullopt, std::nullopt);
+  if (!MaxIterIndex) {
+    Interval = unboundedStrictDirectionInterval(ACoeff, BCoeff,
+                                                Dependence::DVEntry::LT);
+  } else if (MaxIterIndex->isZero()) {
+    Interval = emptyInterval(ACoeff.getBitWidth());
+  } else {
+    APInt Zero(ACoeff.getBitWidth(), 0, true);
+    APInt One(ACoeff.getBitWidth(), 1, true);
+    APInt MaxMinusOne = *MaxIterIndex - One;
+    SmallVector<APInt, 3> Values;
+    Values.push_back(evaluateBanerjeeTerm(ACoeff, Zero, BCoeff, One));
+    Values.push_back(evaluateBanerjeeTerm(ACoeff, Zero, BCoeff, *MaxIterIndex));
+    Values.push_back(
+        evaluateBanerjeeTerm(ACoeff, MaxMinusOne, BCoeff, *MaxIterIndex));
+    Interval = intervalFromValues(Values);
   }
-  LLVM_DEBUG(dbgs() << "\t    Constant = " << *Subscript << '\n');
-#endif
+  Bound[K].Lower[Dependence::DVEntry::LT] = std::move(Interval.Lower);
+  Bound[K].Upper[Dependence::DVEntry::LT] = std::move(Interval.Upper);
 }
 
-// Looks through all the bounds info and
-// computes the lower bound given the current direction settings
-// at each level. If the lower bound for any level is -inf,
-// the result is -inf.
-const SCEV *DependenceInfo::getLowerBound(ArrayRef<BoundInfo> Bound) const {
-  const SCEV *Sum = Bound[1].Lower[Bound[1].Direction];
-  for (unsigned K = 2; Sum && K <= MaxLevels; ++K) {
-    if (Bound[K].Lower[Bound[K].Direction])
-      Sum = SE->getAddExpr(Sum, Bound[K].Lower[Bound[K].Direction]);
-    else
-      Sum = nullptr;
+// Computes the lower and upper bounds for level K using the > direction.
+void DependenceInfo::findBoundsGT(ArrayRef<CoefficientInfo> A,
+                                  ArrayRef<CoefficientInfo> B,
+                                  MutableArrayRef<BoundInfo> Bound,
+                                  unsigned K) const {
+  const APInt &ACoeff = A[K].Coeff;
+  const APInt &BCoeff = B[K].Coeff;
+  std::optional<APInt> MaxIterIndex;
+  if (A[K].MaxIterIndex && B[K].MaxIterIndex) {
+    if (*A[K].MaxIterIndex == *B[K].MaxIterIndex)
+      MaxIterIndex = A[K].MaxIterIndex;
+  } else {
+    MaxIterIndex = A[K].MaxIterIndex ? A[K].MaxIterIndex : B[K].MaxIterIndex;
   }
-  return Sum;
-}
 
-// Looks through all the bounds info and
-// computes the upper bound given the current direction settings
-// at each level. If the upper bound at any level is +inf,
-// the result is +inf.
-const SCEV *DependenceInfo::getUpperBound(ArrayRef<BoundInfo> Bound) const {
-  const SCEV *Sum = Bound[1].Upper[Bound[1].Direction];
-  for (unsigned K = 2; Sum && K <= MaxLevels; ++K) {
-    if (Bound[K].Upper[Bound[K].Direction])
-      Sum = SE->getAddExpr(Sum, Bound[K].Upper[Bound[K].Direction]);
-    else
-      Sum = nullptr;
-  }
-  return Sum;
+  BanerjeeInterval Interval(std::nullopt, std::nullopt);
+  if (!MaxIterIndex) {
+    Interval = unboundedStrictDirectionInterval(ACoeff, BCoeff,
+                                                Dependence::DVEntry::GT);
+  } else if (MaxIterIndex->isZero()) {
+    Interval = emptyInterval(ACoeff.getBitWidth());
+  } else {
+    APInt Zero(ACoeff.getBitWidth(), 0, true);
+    APInt One(ACoeff.getBitWidth(), 1, true);
+    APInt MaxMinusOne = *MaxIterIndex - One;
+    SmallVector<APInt, 3> Values;
+    Values.push_back(evaluateBanerjeeTerm(ACoeff, One, BCoeff, Zero));
+    Values.push_back(evaluateBanerjeeTerm(ACoeff, *MaxIterIndex, BCoeff, Zero));
+    Values.push_back(
+        evaluateBanerjeeTerm(ACoeff, *MaxIterIndex, BCoeff, MaxMinusOne));
+    Interval = intervalFromValues(Values);
+  }
+  Bound[K].Lower[Dependence::DVEntry::GT] = std::move(Interval.Lower);
+  Bound[K].Upper[Dependence::DVEntry::GT] = std::move(Interval.Upper);
 }
 
 /// Check if we can delinearize the subscripts. If the SCEVs representing the
diff --git a/llvm/test/Analysis/DependenceAnalysis/PR51512.ll b/llvm/test/Analysis/DependenceAnalysis/PR51512.ll
index d55d607827963..c5cf111e52e74 100644
--- a/llvm/test/Analysis/DependenceAnalysis/PR51512.ll
+++ b/llvm/test/Analysis/DependenceAnalysis/PR51512.ll
@@ -10,7 +10,7 @@ define void @foo() {
 ; CHECK-NEXT:  Src: store i32 42, ptr %getelementptr, align 1 --> Dst: store i32 42, ptr %getelementptr, align 1
 ; CHECK-NEXT:    da analyze - output [0 S]!
 ; CHECK-NEXT:  Src: store i32 42, ptr %getelementptr, align 1 --> Dst: store i32 0, ptr %getelementptr5, align 1
-; CHECK-NEXT:    da analyze - output [0 <=|<]!
+; CHECK-NEXT:    da analyze - output [0 0|<]!
 ; CHECK-NEXT:  Src: store i32 0, ptr %getelementptr5, align 1 --> Dst: store i32 0, ptr %getelementptr5, align 1
 ; CHECK-NEXT:    da analyze - none!
 ;
diff --git a/llvm/test/Analysis/DependenceAnalysis/banerjee-overflow.ll b/llvm/test/Analysis/DependenceAnalysis/banerjee-overflow.ll
index 6081b7f18a01e..f934a304d7ee2 100644
--- a/llvm/test/Analysis/DependenceAnalysis/banerjee-overflow.ll
+++ b/llvm/test/Analysis/DependenceAnalysis/banerjee-overflow.ll
@@ -68,6 +68,107 @@ for.inc.i:
 for.end.i:
   ret void
 }
-;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
-; CHECK-ALL: {{.*}}
-; CHECK-BANERJEE-MIV: {{.*}}
+
+;
+; for (int i = 0; i < 1; i++)
+;   for (int j = 0; j < 1; j++) {
+;     A[i + j] = 0;
+;     A[i + j] = 1;
+;   }
+;
+; Backedge taken count is the maximum normalized iteration index. For a
+; single-iteration loop that value is 0, making < and > directions impossible.
+;
+define void @banerjee_single_iteration(ptr %A) {
+; CHECK-ALL-LABEL: 'banerjee_single_iteration'
+; CHECK-ALL-NEXT:  Src: store i8 0, ptr %gep.0, align 1 --> Dst: store i8 0, ptr %gep.0, align 1
+; CHECK-ALL-NEXT:    da analyze - output [* *]!
+; CHECK-ALL-NEXT:  Src: store i8 0, ptr %gep.0, align 1 --> Dst: store i8 1, ptr %gep.1, align 1
+; CHECK-ALL-NEXT:    da analyze - output [* *|<]!
+; CHECK-ALL-NEXT:  Src: store i8 1, ptr %gep.1, align 1 --> Dst: store i8 1, ptr %gep.1, align 1
+; CHECK-ALL-NEXT:    da analyze - output [* *]!
+;
+; CHECK-BANERJEE-MIV-LABEL: 'banerjee_single_iteration'
+; CHECK-BANERJEE-MIV-NEXT:  Src: store i8 0, ptr %gep.0, align 1 --> Dst: store i8 0, ptr %gep.0, align 1
+; CHECK-BANERJEE-MIV-NEXT:    da analyze - none!
+; CHECK-BANERJEE-MIV-NEXT:  Src: store i8 0, ptr %gep.0, align 1 --> Dst: store i8 1, ptr %gep.1, align 1
+; CHECK-BANERJEE-MIV-NEXT:    da analyze - output [0 0|<]!
+; CHECK-BANERJEE-MIV-NEXT:  Src: store i8 1, ptr %gep.1, align 1 --> Dst: store i8 1, ptr %gep.1, align 1
+; CHECK-BANERJEE-MIV-NEXT:    da analyze - none!
+;
+entry:
+  br label %loop.i
+
+loop.i:
+  %i = phi i64 [ 0, %entry ], [ %i.inc, %loop.i.latch ]
+  br label %loop.j
+
+loop.j:
+  %j = phi i64 [ 0, %loop.i ], [ %j.inc, %loop.j ]
+  %offset = add nsw i64 %i, %j
+  %gep.0 = getelementptr i8, ptr %A, i64 %offset
+  store i8 0, ptr %gep.0, align 1
+  %gep.1 = getelementptr i8, ptr %A, i64 %offset
+  store i8 1, ptr %gep.1, align 1
+  %j.inc = add nuw nsw i64 %j, 1
+  %ec.j = icmp eq i64 %j.inc, 1
+  br i1 %ec.j, label %loop.i.latch, label %loop.j
+
+loop.i.latch:
+  %i.inc = add nuw nsw i64 %i, 1
+  %ec.i = icmp eq i64 %i.inc, 1
+  br i1 %ec.i, label %exit, label %loop.i
+
+exit:
+  ret void
+}
+
+;
+; for (int8_t i = 0; i < 2; ++i)
+;   for (int8_t j = 0; j < 2; ++j) {
+;     A[(int8_t)(-128 + i + j)] = 0;
+;     A[(int8_t)(127 + i + j)] = 1;
+;   }
+;
+; These access functions wrap in their original i8 width. Without no-wrap
+; information, dependence analysis must reject the addrecs and remain
+; conservative instead of interpreting their distance in the widened analysis
+; width.
+;
+define void @banerjee_no_nsw(ptr %A) {
+; CHECK-LABEL: 'banerjee_no_nsw'
+; CHECK-NEXT:  Src: store i8 0, ptr %gep.0, align 1 --> Dst: store i8 0, ptr %gep.0, align 1
+; CHECK-NEXT:    da analyze - output [* *]!
+; CHECK-NEXT:  Src: store i8 0, ptr %gep.0, align 1 --> Dst: store i8 1, ptr %gep.1, align 1
+; CHECK-NEXT:    da analyze - output [* *|<]!
+; CHECK-NEXT:  Src: store i8 1, ptr %gep.1, align 1 --> Dst: store i8 1, ptr %gep.1, align 1
+; CHECK-NEXT:    da analyze - output [* *]!
+;
+entry:
+  br label %loop.i
+
+loop.i:
+  %i = phi i8 [ 0, %entry ], [ %i.inc, %loop.i.latch ]
+  br label %loop.j
+
+loop.j:
+  %j = phi i8 [ 0, %loop.i ], [ %j.inc, %loop.j ]
+  %sum = add i8 %i, %j
+  %offset.0 = add i8 %sum, -128
+  %offset.1 = add i8 %sum, 127
+  %gep.0 = getelementptr i8, ptr %A, i8 %offset.0
+  %gep.1 = getelementptr i8, ptr %A, i8 %offset.1
+  store i8 0, ptr %gep.0, align 1
+  store i8 1, ptr %gep.1, align 1
+  %j.inc = add i8 %j, 1
+  %ec.j = icmp eq i8 %j.inc, 2
+  br i1 %ec.j, label %loop.i.latch, label %loop.j
+
+loop.i.latch:
+  %i.inc = add i8 %i, 1
+  %ec.i = icmp eq i8 %i.inc, 2
+  br i1 %ec.i, label %exit, label %loop.i
+
+exit:
+  ret void
+}
diff --git a/llvm/test/Analysis/DependenceAnalysis/gcd-miv-overflow.ll b/llvm/test/Analysis/DependenceAnalysis/gcd-miv-overflow.ll
index dfd3c3bc0f462..7bb97ae82f66f 100644
--- a/llvm/test/Analysis/DependenceAnalysis/gcd-miv-overflow.ll
+++ b/llvm/test/Analysis/DependenceAnalysis/gcd-miv-overflow.ll
@@ -189,8 +189,6 @@ exit:
 ;     if (offset1 == -5) A[offset1] = 0;
 ;   }
 ;
-; FIXME: DependenceAnalysis fails to detect dependency between two stores.
-;
 ; memory accesses                     | (i,j) == (1844674407370955160,1)
 ; ------------------------------------|----------------------------------
 ; A[ 5*i - 9223372036854775805*j]     | A[-5]
@@ -201,9 +199,9 @@ define void @gcdmiv_delta_ovfl2(ptr %A) {
 ; CHECK-ALL-NEXT:  Src: store i8 0, ptr %gep.0, align 1 --> Dst: store i8 0, ptr %gep.0, align 1
 ; CHECK-ALL-NEXT:    da analyze - none!
 ; CHECK-ALL-NEXT:  Src: store i8 0, ptr %gep.0, align 1 --> Dst: store i8 0, ptr %gep.1, align 1
-; CHECK-ALL-NEXT:    da analyze - none!
+; CHECK-ALL-NEXT:    da analyze - output [* *|<]!
 ; CHECK-ALL-NEXT:  Src: store i8 0, ptr %gep.1, align 1 --> Dst: store i8 0, ptr %gep.1, align 1
-; CHECK-ALL-NEXT:    da analyze - none!
+; CHECK-ALL-NEXT:    da analyze - output [* *]!
 ;
 ; CHECK-GCD-MIV-LABEL: 'gcdmiv_delta_ovfl2'
 ; CHECK-GCD-MIV-NEXT:  Src: store i8 0, ptr %gep.0, align 1 --> Dst: store i8 0, ptr %gep.0, align 1



More information about the llvm-commits mailing list