Skip to content
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
34 changes: 34 additions & 0 deletions src/coreclr/jit/rangecheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,40 @@ void RangeCheck::MergeEdgeAssertions(Compiler* comp,
continue;
}
}
// If we're not allowed to emit keBinOpArray, we still can deduce something useful from
// O2K_CHECKED_BOUND_ADD_CNS for never negative checked bounds
//
// Example: "(uint)normalLclVN < span.Length" means normalLclVN's range is [0..INT32_MAX-1]
// Example: "(uint)normalLclVN <= array.Length" means normalLclVN's range is [0..Array.MaxLength]
//
else if (!canUseCheckedBounds && curAssertion.IsRelop() && (curAssertion.GetOp1().GetVN() == normalLclVN) &&
(curAssertion.GetOp2().KindIs(Compiler::O2K_CHECKED_BOUND_ADD_CNS)) &&
(curAssertion.GetOp2().IsCheckedBoundNeverNegative()) &&
(curAssertion.GetOp2().GetCheckedBoundConstant() == 0))
{
// We can assume that the checked bound is within [0..INT32_MAX] thanks to IsCheckedBoundNeverNegative,
// but let's see if we can do better: we could call GetRangeFromAssertions on the checked bound's VN,
// but that may lead to infinite recursion. Also, the checked bound is usually either arr.Length or
// span.Length anyway.
ValueNum checkedBoundVN = curAssertion.GetOp2().GetCheckedBound();

int maxValue = INT32_MAX;
if (comp->vnStore->IsVNArrLen(checkedBoundVN))
{
maxValue = CORINFO_Array_MaxLength;
}
else
{
int cns;
if (comp->vnStore->IsVNIntegralConstant(checkedBoundVN, &cns) && (cns >= 0))
{
maxValue = cns;
}
}

cmpOper = Compiler::AssertionDsc::ToCompareOper(curAssertion.GetKind(), &isUnsigned);
limit = Limit(Limit::keConstant, maxValue);
}
// Current assertion is of the form "i <relop> (checkedBndVN + cns)"
else if (curAssertion.KindIs(Compiler::OAK_GE, Compiler::OAK_GT, Compiler::OAK_LE, Compiler::OAK_LT) &&
curAssertion.GetOp2().KindIs(Compiler::O2K_CHECKED_BOUND_ADD_CNS))
Expand Down
Loading