Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Redundant bound check for span.Length == 0 pattern #101323

Merged
merged 3 commits into from
Apr 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions src/coreclr/jit/rangecheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -866,10 +866,37 @@ void RangeCheck::MergeEdgeAssertions(ValueNum normalLclVN, ASSERT_VALARG_TP asse
continue;
}

// Doesn't tighten the current bound. So skip.
if (pRange->uLimit.IsConstant() && limit.vn != arrLenVN)
// Skip if it doesn't tighten the current bound:
if (pRange->uLimit.IsConstant() && ((cmpOper == GT_LE) || (cmpOper == GT_LT)))
{
continue;
if (!limit.IsConstant() && (limit.vn != arrLenVN))
{
// If our new limit is not constant and doesn't represent the array's length - bail out.
// NOTE: it's fine to replace the current constant limit with a non-constant arrLenVN.
continue;
}
if (limit.IsConstant() && (limit.cns > pRange->uLimit.cns))
{
// The new constant limit doesn't tighten the current constant bound.
// E.g. current is "X < 10" and the new one is "X < 100"
continue;
}
}
// Same for the lower bound:
if (pRange->lLimit.IsConstant() && ((cmpOper == GT_GE) || (cmpOper == GT_GT)))
{
if (!limit.IsConstant() && (limit.vn != arrLenVN))
{
// If our new limit is not constant and doesn't represent the array's length - bail out.
// NOTE: it's fine to replace the current constant limit with a non-constant arrLenVN.
continue;
}
if (limit.IsConstant() && (limit.cns < pRange->lLimit.cns))
{
// The new constant limit doesn't tighten the current constant bound.
// E.g. current is "X > 10" and the new one is "X > 5"
continue;
}
}

// Check if the incoming limit from assertions tightens the existing upper limit.
Expand Down
Loading