@@ -1582,6 +1582,23 @@ static const SCEV *minusSCEVNoSignedOverflow(const SCEV *A, const SCEV *B,
15821582 return nullptr ;
15831583}
15841584
1585+ // / Returns the absolute value of \p A. In the context of dependence analysis,
1586+ // / we need an absolute value in a mathematical sense. If \p A is the signed
1587+ // / minimum value, we cannot represent it unless extending the original type.
1588+ // / Thus if we cannot prove that \p A is not the signed minimum value, returns
1589+ // / nullptr.
1590+ static const SCEV *absSCEVNoSignedOverflow (const SCEV *A, ScalarEvolution &SE) {
1591+ IntegerType *Ty = cast<IntegerType>(A->getType ());
1592+ if (!Ty)
1593+ return nullptr ;
1594+
1595+ const SCEV *SMin =
1596+ SE.getConstant (APInt::getSignedMinValue (Ty->getBitWidth ()));
1597+ if (!SE.isKnownPredicate (CmpInst::ICMP_NE, A, SMin))
1598+ return nullptr ;
1599+ return SE.getAbsExpr (A, /* IsNSW=*/ true );
1600+ }
1601+
15851602// / Returns true iff \p Test is enabled.
15861603static bool isDependenceTestEnabled (DependenceTestType Test) {
15871604 if (EnableDependenceTest == DependenceTestType::All)
@@ -1669,21 +1686,25 @@ bool DependenceInfo::strongSIVtest(const SCEV *Coeff, const SCEV *SrcConst,
16691686 LLVM_DEBUG (dbgs () << " , " << *Delta->getType () << " \n " );
16701687
16711688 // check that |Delta| < iteration count
1672- if (const SCEV *UpperBound =
1673- collectUpperBound (CurSrcLoop, Delta->getType ())) {
1689+ bool IsDeltaLarge = [&] {
1690+ const SCEV *UpperBound = collectUpperBound (CurSrcLoop, Delta->getType ());
1691+ if (!UpperBound)
1692+ return false ;
1693+
16741694 LLVM_DEBUG (dbgs () << " \t UpperBound = " << *UpperBound);
16751695 LLVM_DEBUG (dbgs () << " , " << *UpperBound->getType () << " \n " );
1676- const SCEV *AbsDelta =
1677- SE-> isKnownNonNegative (Delta) ? Delta : SE-> getNegativeSCEV (Delta );
1678- const SCEV *AbsCoeff =
1679- SE-> isKnownNonNegative (Coeff) ? Coeff : SE-> getNegativeSCEV (Coeff) ;
1696+ const SCEV *AbsDelta = absSCEVNoSignedOverflow (Delta, *SE);
1697+ const SCEV *AbsCoeff = absSCEVNoSignedOverflow (Coeff, *SE );
1698+ if (!AbsDelta || !AbsCoeff)
1699+ return false ;
16801700 const SCEV *Product = SE->getMulExpr (UpperBound, AbsCoeff);
1681- if (isKnownPredicate (CmpInst::ICMP_SGT, AbsDelta, Product)) {
1682- // Distance greater than trip count - no dependence
1683- ++StrongSIVindependence;
1684- ++StrongSIVsuccesses;
1685- return true ;
1686- }
1701+ return isKnownPredicate (CmpInst::ICMP_SGT, AbsDelta, Product);
1702+ }();
1703+ if (IsDeltaLarge) {
1704+ // Distance greater than trip count - no dependence
1705+ ++StrongSIVindependence;
1706+ ++StrongSIVsuccesses;
1707+ return true ;
16871708 }
16881709
16891710 // Can we compute distance?
@@ -2259,6 +2280,9 @@ bool DependenceInfo::weakZeroSrcSIVtest(
22592280 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(DstCoeff);
22602281 if (!ConstCoeff)
22612282 return false ;
2283+
2284+ // Since ConstCoeff is constant, !isKnownNegative means it's non-negative.
2285+ // TODO: Bail out if it's a signed minimum value.
22622286 const SCEV *AbsCoeff = SE->isKnownNegative (ConstCoeff)
22632287 ? SE->getNegativeSCEV (ConstCoeff)
22642288 : ConstCoeff;
@@ -2369,6 +2393,9 @@ bool DependenceInfo::weakZeroDstSIVtest(
23692393 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(SrcCoeff);
23702394 if (!ConstCoeff)
23712395 return false ;
2396+
2397+ // Since ConstCoeff is constant, !isKnownNegative means it's non-negative.
2398+ // TODO: Bail out if it's a signed minimum value.
23722399 const SCEV *AbsCoeff = SE->isKnownNegative (ConstCoeff)
23732400 ? SE->getNegativeSCEV (ConstCoeff)
23742401 : ConstCoeff;
0 commit comments