Skip to content
Merged
Show file tree
Hide file tree
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
36 changes: 36 additions & 0 deletions src/coreclr/jit/rangecheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,42 @@ Range RangeCheck::GetRangeFromAssertionsWorker(
}

result = RangeOps::EvalRelop(cmpOper, isUnsigned, r1, r2);

// Example: "(uint)(length - 4) > (uint)length" folds to false when
// length >= 4 (the typical Slice(length - cns) bounds check).
if (!result.IsSingleValueConstant())
Comment thread
EgorBo marked this conversation as resolved.
{
ValueNum op1VN = funcApp.m_args[0];
ValueNum op2VN = funcApp.m_args[1];
ValueNum addOpVN;
int addCns;

if (comp->vnStore->IsVNBinFuncWithConst(op1VN, VNF_ADD, &addOpVN, &addCns) &&
(addOpVN == op2VN) && (addCns < 0) && (addCns > INT32_MIN))
{
if (r2.LowerLimit().IsConstant() && (r2.LowerLimit().GetConstant() >= -addCns))
{
// ADD(A, K) < A is proven (both signed and unsigned).
switch (cmpOper)
{
case GT_LT:
case GT_LE:
case GT_NE:
result = Range(Limit(Limit::keConstant, 1));
break;

case GT_GT:
case GT_GE:
case GT_EQ:
result = Range(Limit(Limit::keConstant, 0));
break;

default:
break;
}
}
}
}
}
break;
}
Expand Down
22 changes: 19 additions & 3 deletions src/coreclr/jit/valuenum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5372,10 +5372,26 @@ ValueNum ValueNumStore::EvalUsingMathIdentity(var_types typ, VNFunc func, ValueN

if (!ovf)
{
// x - (x + a) == -a
// x - (a + x) == -a
ValueNum arg1Op1;
ValueNum arg1Op2;
if (IsVNBinFunc(arg1VN, VNF_ADD, &arg1Op1, &arg1Op2))
{
if (arg1Op1 == arg0VN)
{
return VNForFunc(typ, VNF_NEG, arg1Op2);
}
if (arg1Op2 == arg0VN)
{
return VNForFunc(typ, VNF_NEG, arg1Op1);
}
}

// (x + a) - x == a
// (a + x) - x == a
VNFuncApp add;
if (GetVNFunc(arg0VN, &add) && (add.m_func == (VNFunc)GT_ADD))
if (GetVNFunc(arg0VN, &add) && (add.m_func == VNF_ADD))
{
if (add.m_args[0] == arg1VN)
return add.m_args[1];
Expand All @@ -5387,15 +5403,15 @@ ValueNum ValueNumStore::EvalUsingMathIdentity(var_types typ, VNFunc func, ValueN
// (x + a) - (b + x) == a - b
// (a + x) - (b + x) == a - b
VNFuncApp add2;
if (GetVNFunc(arg1VN, &add2) && (add2.m_func == (VNFunc)GT_ADD))
if (GetVNFunc(arg1VN, &add2) && (add2.m_func == VNF_ADD))
{
for (int a = 0; a < 2; a++)
{
for (int b = 0; b < 2; b++)
{
if (add.m_args[a] == add2.m_args[b])
{
return VNForFunc(typ, (VNFunc)GT_SUB, add.m_args[1 - a], add2.m_args[1 - b]);
return VNForFunc(typ, VNF_SUB, add.m_args[1 - a], add2.m_args[1 - b]);
}
}
}
Expand Down
Loading