From cd7ce619f558748736a04e050858e09e6515e6dd Mon Sep 17 00:00:00 2001 From: EgorBo Date: Sun, 2 Nov 2025 00:24:44 +0100 Subject: [PATCH 1/2] tighten pRange in MergeEdgeAssertions via X != CNS assertions --- src/coreclr/jit/rangecheck.cpp | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/coreclr/jit/rangecheck.cpp b/src/coreclr/jit/rangecheck.cpp index 23c995db9205a5..df3319c9e453ed 100644 --- a/src/coreclr/jit/rangecheck.cpp +++ b/src/coreclr/jit/rangecheck.cpp @@ -863,8 +863,28 @@ void RangeCheck::MergeEdgeAssertions(Compiler* comp, } else { - // We have a != assertion, but it doesn't tell us much about the interval. So just skip it. - continue; + assert(curAssertion->assertionKind == Compiler::OAK_NOT_EQUAL); + + // We have an assertion of the form "var != cnstLimit". + // Say we have "var != 100" and we already have [100, X] as range for var, + // then we can tighten it to [101, X] + // Similarly, if we have [Y, 100], we can tighten it to [Y, 99]. + + if (pRange->LowerLimit().IsConstant() && pRange->LowerLimit().GetConstant() == cnstLimit) + { + limit = Limit(Limit::keConstant, cnstLimit); + cmpOper = GT_GT; + } + else if (pRange->UpperLimit().IsConstant() && pRange->UpperLimit().GetConstant() == cnstLimit) + { + limit = Limit(Limit::keConstant, cnstLimit); + cmpOper = GT_LT; + } + else + { + // We can't make any useful deduction from this assertion. + continue; + } } isConstantAssertion = true; From c0c8d9b0bf9e7d40fb416167276a9dca1dbfee4e Mon Sep 17 00:00:00 2001 From: Egor Bogatov Date: Sun, 2 Nov 2025 02:30:36 +0100 Subject: [PATCH 2/2] Update rangecheck.cpp --- src/coreclr/jit/rangecheck.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/coreclr/jit/rangecheck.cpp b/src/coreclr/jit/rangecheck.cpp index df3319c9e453ed..de746eb9a36a64 100644 --- a/src/coreclr/jit/rangecheck.cpp +++ b/src/coreclr/jit/rangecheck.cpp @@ -865,10 +865,10 @@ void RangeCheck::MergeEdgeAssertions(Compiler* comp, { assert(curAssertion->assertionKind == Compiler::OAK_NOT_EQUAL); - // We have an assertion of the form "var != cnstLimit". - // Say we have "var != 100" and we already have [100, X] as range for var, - // then we can tighten it to [101, X] - // Similarly, if we have [Y, 100], we can tighten it to [Y, 99]. + // We have an assertion of the form "X != constLimit". + // For example, if the assertion is "X != 100" and the current range for X is [100, X], + // we can tighten the range to [101, X]. + // Similarly, if the range is [Y, 100], we can tighten it to [Y, 99]. if (pRange->LowerLimit().IsConstant() && pRange->LowerLimit().GetConstant() == cnstLimit) {