Skip to content

Commit f021ccf

Browse files
authored
Fix Redundant bound check for span.Length == 0 pattern (#101323)
1 parent 66ce263 commit f021ccf

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

Diff for: src/coreclr/jit/rangecheck.cpp

+30-3
Original file line numberDiff line numberDiff line change
@@ -866,10 +866,37 @@ void RangeCheck::MergeEdgeAssertions(ValueNum normalLclVN, ASSERT_VALARG_TP asse
866866
continue;
867867
}
868868

869-
// Doesn't tighten the current bound. So skip.
870-
if (pRange->uLimit.IsConstant() && limit.vn != arrLenVN)
869+
// Skip if it doesn't tighten the current bound:
870+
if (pRange->uLimit.IsConstant() && ((cmpOper == GT_LE) || (cmpOper == GT_LT)))
871871
{
872-
continue;
872+
if (!limit.IsConstant() && (limit.vn != arrLenVN))
873+
{
874+
// If our new limit is not constant and doesn't represent the array's length - bail out.
875+
// NOTE: it's fine to replace the current constant limit with a non-constant arrLenVN.
876+
continue;
877+
}
878+
if (limit.IsConstant() && (limit.cns > pRange->uLimit.cns))
879+
{
880+
// The new constant limit doesn't tighten the current constant bound.
881+
// E.g. current is "X < 10" and the new one is "X < 100"
882+
continue;
883+
}
884+
}
885+
// Same for the lower bound:
886+
if (pRange->lLimit.IsConstant() && ((cmpOper == GT_GE) || (cmpOper == GT_GT)))
887+
{
888+
if (!limit.IsConstant() && (limit.vn != arrLenVN))
889+
{
890+
// If our new limit is not constant and doesn't represent the array's length - bail out.
891+
// NOTE: it's fine to replace the current constant limit with a non-constant arrLenVN.
892+
continue;
893+
}
894+
if (limit.IsConstant() && (limit.cns < pRange->lLimit.cns))
895+
{
896+
// The new constant limit doesn't tighten the current constant bound.
897+
// E.g. current is "X > 10" and the new one is "X > 5"
898+
continue;
899+
}
873900
}
874901

875902
// Check if the incoming limit from assertions tightens the existing upper limit.

0 commit comments

Comments
 (0)