@@ -1170,6 +1170,15 @@ const SCEVConstant *DependenceInfo::collectConstantUpperBound(const Loop *L,
11701170 return nullptr ;
11711171}
11721172
1173+ // / Returns \p A - \p B if it guaranteed not to signed wrap. Otherwise returns
1174+ // / nullptr. \p A and \p B must have the same integer type.
1175+ static const SCEV *minusSCEVNoSignedOverflow (const SCEV *A, const SCEV *B,
1176+ ScalarEvolution &SE) {
1177+ if (SE.willNotOverflow (Instruction::Sub, /* Signed=*/ true , A, B))
1178+ return SE.getMinusSCEV (A, B);
1179+ return nullptr ;
1180+ }
1181+
11731182// testZIV -
11741183// When we have a pair of subscripts of the form [c1] and [c2],
11751184// where c1 and c2 are both loop invariant, we attack it using
@@ -1626,7 +1635,9 @@ bool DependenceInfo::exactSIVtest(const SCEV *SrcCoeff, const SCEV *DstCoeff,
16261635 assert (0 < Level && Level <= CommonLevels && " Level out of range" );
16271636 Level--;
16281637 Result.Consistent = false ;
1629- const SCEV *Delta = SE->getMinusSCEV (DstConst, SrcConst);
1638+ const SCEV *Delta = minusSCEVNoSignedOverflow (DstConst, SrcConst, *SE);
1639+ if (!Delta)
1640+ return false ;
16301641 LLVM_DEBUG (dbgs () << " \t Delta = " << *Delta << " \n " );
16311642 NewConstraint.setLine (SrcCoeff, SE->getNegativeSCEV (DstCoeff), Delta,
16321643 CurLoop);
@@ -1716,6 +1727,7 @@ bool DependenceInfo::exactSIVtest(const SCEV *SrcCoeff, const SCEV *DstCoeff,
17161727 // explore directions
17171728 unsigned NewDirection = Dependence::DVEntry::NONE;
17181729 APInt LowerDistance, UpperDistance;
1730+ // TODO: Overflow check may be needed.
17191731 if (TA.sgt (TB)) {
17201732 LowerDistance = (TY - TX) + (TA - TB) * TL;
17211733 UpperDistance = (TY - TX) + (TA - TB) * TU;
0 commit comments