@@ -1243,35 +1243,72 @@ bool DependenceInfo::strongSIVtest(const SCEV *Coeff, const SCEV *SrcConst,
12431243 if (const SCEV *UpperBound = collectUpperBound (CurLoop, Delta->getType ())) {
12441244 LLVM_DEBUG (dbgs () << " \t UpperBound = " << *UpperBound);
12451245 LLVM_DEBUG (dbgs () << " , " << *UpperBound->getType () << " \n " );
1246+
1247+ // Handle negative upper bound - loop doesn't execute.
1248+ if (SE->isKnownNegative (UpperBound)) {
1249+ LLVM_DEBUG (dbgs () << " \t Negative upper bound - no dependence\n " );
1250+ ++StrongSIVindependence;
1251+ ++StrongSIVsuccesses;
1252+ return true ;
1253+ }
1254+
12461255 const SCEV *AbsDelta =
12471256 SE->isKnownNonNegative (Delta) ? Delta : SE->getNegativeSCEV (Delta);
12481257 const SCEV *AbsCoeff =
12491258 SE->isKnownNonNegative (Coeff) ? Coeff : SE->getNegativeSCEV (Coeff);
12501259 const SCEV *Product = SE->getMulExpr (UpperBound, AbsCoeff);
1260+
12511261 if (isKnownPredicate (CmpInst::ICMP_SGT, AbsDelta, Product)) {
12521262 // Check if this involves symbolic expressions where we might be too
12531263 // conservative.
12541264 if (isa<SCEVUnknown>(Delta) || isa<SCEVUnknown>(Coeff) ||
12551265 !isa<SCEVConstant>(AbsDelta) || !isa<SCEVConstant>(Product)) {
1256- // For symbolic expressions, add runtime assumption rather than
1257- // rejecting.
1258- const SCEVPredicate *BoundPred =
1259- SE->getComparePredicate (ICmpInst::ICMP_SLE, AbsDelta, Product);
1260- if (UnderRuntimeAssumptions) {
1261- SmallVector<const SCEVPredicate *, 4 > NewPreds (
1262- Assumptions.getPredicates ());
1263- NewPreds.push_back (BoundPred);
1264- const_cast <DependenceInfo *>(this )->Assumptions =
1265- SCEVUnionPredicate (NewPreds, *SE);
1266- LLVM_DEBUG (dbgs () << " \t Added runtime bound assumption\n " );
1266+ // Check if the assumption would be meaningful and not trivially
1267+ // impossible. For relationships like Delta=n-m, UpperBound=m-n-1, the
1268+ // assumption |n-m| <= (m-n-1) can be impossible (e.g., 0 <= -1 when
1269+ // n=m).
1270+ bool MeaningfulAssumption = true ;
1271+
1272+ // Detect cases where Delta and UpperBound have inverse relationships
1273+ // that could lead to impossible assumptions.
1274+ if (isa<SCEVAddExpr>(Delta) && isa<SCEVAddExpr>(UpperBound)) {
1275+ // Look for patterns where UpperBound = -Delta - 1 (or similar).
1276+ const SCEV *NegDelta = SE->getNegativeSCEV (Delta);
1277+ const SCEV *NegDeltaMinusOne =
1278+ SE->getAddExpr (NegDelta, SE->getConstant (Delta->getType (), -1 ));
1279+ if (UpperBound == NegDeltaMinusOne) {
1280+ MeaningfulAssumption = false ;
1281+ LLVM_DEBUG (dbgs () << " \t Detected inverse relationship - "
1282+ " assumption would be impossible\n " );
1283+ }
1284+ }
1285+
1286+ if (MeaningfulAssumption && !SE->isKnownNonPositive (Product)) {
1287+ // For symbolic expressions, add runtime assumption rather than
1288+ // rejecting.
1289+ const SCEVPredicate *BoundPred =
1290+ SE->getComparePredicate (ICmpInst::ICMP_SLE, AbsDelta, Product);
1291+ if (UnderRuntimeAssumptions) {
1292+ SmallVector<const SCEVPredicate *, 4 > NewPreds (
1293+ Assumptions.getPredicates ());
1294+ NewPreds.push_back (BoundPred);
1295+ const_cast <DependenceInfo *>(this )->Assumptions =
1296+ SCEVUnionPredicate (NewPreds, *SE);
1297+ LLVM_DEBUG (dbgs () << " \t Added runtime bound assumption\n " );
1298+ } else {
1299+ // Cannot add runtime assumptions, let more complex tests try.
1300+ LLVM_DEBUG (dbgs () << " \t Would need runtime bound assumption "
1301+ " but not allowed. Failing this test.\n " );
1302+ return false ;
1303+ }
12671304 } else {
1268- // Cannot add runtime assumptions, let more complex tests try.
1269- LLVM_DEBUG ( dbgs () << " \t Would need runtime bound assumption but "
1270- " not allowed. Failing this test. \n " );
1305+ LLVM_DEBUG ( dbgs () << " \t Cannot add meaningful assumption \n " );
1306+ // When bound check fails and we can't add meaningful assumptions,
1307+ // this test cannot handle this case reliably.
12711308 return false ;
12721309 }
12731310 } else {
1274- // Distance definitely greater than trip count - no dependence
1311+ // Distance definitely greater than trip count - no dependence.
12751312 ++StrongSIVindependence;
12761313 ++StrongSIVsuccesses;
12771314 return true ;
@@ -2189,8 +2226,39 @@ bool DependenceInfo::symbolicRDIVtest(const SCEV *A1, const SCEV *A2,
21892226 const SCEV *N2 = collectUpperBound (Loop2, A1->getType ());
21902227 LLVM_DEBUG (if (N1) dbgs () << " \t N1 = " << *N1 << " \n " );
21912228 LLVM_DEBUG (if (N2) dbgs () << " \t N2 = " << *N2 << " \n " );
2229+
21922230 const SCEV *C2_C1 = SE->getMinusSCEV (C2, C1);
21932231 const SCEV *C1_C2 = SE->getMinusSCEV (C1, C2);
2232+
2233+ // Check for negative or problematic upper bounds
2234+ auto CheckUpperBound = [&](const SCEV *N, const SCEV *Delta,
2235+ const char *Name) -> bool {
2236+ if (!N)
2237+ // No bound to check.
2238+ return true ;
2239+
2240+ if (SE->isKnownNegative (N)) {
2241+ LLVM_DEBUG (dbgs () << " \t " << Name
2242+ << " is negative - analysis unreliable\n " );
2243+ return false ;
2244+ }
2245+
2246+ // Check for degenerate cases where upper bounds are too close to the delta
2247+ // values. This can happen when N is an expression like (Delta - 1), leading
2248+ // to trivially true comparisons like Delta > (Delta - 1).
2249+ const SCEV *NPlusOne = SE->getAddExpr (N, SE->getOne (N->getType ()));
2250+ if (Delta == NPlusOne) {
2251+ LLVM_DEBUG (dbgs () << " \t Degenerate case: Delta == " << Name
2252+ << " +1 - analysis unreliable\n " );
2253+ return false ;
2254+ }
2255+ // Bound is OK.
2256+ return true ;
2257+ };
2258+
2259+ if (!CheckUpperBound (N1, C2_C1, " N1" ) || !CheckUpperBound (N2, C1_C2, " N2" ))
2260+ return false ;
2261+
21942262 LLVM_DEBUG (dbgs () << " \t C2 - C1 = " << *C2_C1 << " \n " );
21952263 LLVM_DEBUG (dbgs () << " \t C1 - C2 = " << *C1_C2 << " \n " );
21962264 if (SE->isKnownNonNegative (A1)) {
0 commit comments