Skip to content

Remove useless complementary assertions #110171

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

Merged
merged 1 commit into from
Feb 25, 2025
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
47 changes: 40 additions & 7 deletions src/coreclr/jit/assertionprop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,9 @@ void Compiler::optPrintAssertion(AssertionDsc* curAssertion, AssertionIndex asse
}
else if (curAssertion->op1.kind == O1K_VN)
{
printf("[" FMT_VN "]", curAssertion->op1.vn);
printf("[vn: " FMT_VN, curAssertion->op1.vn);
vnStore->vnDump(this, curAssertion->op1.vn);
printf("]");
}
else if (curAssertion->op1.kind == O1K_BOUND_OPER_BND)
{
Expand Down Expand Up @@ -1131,6 +1133,13 @@ AssertionIndex Compiler::optCreateAssertion(GenTree* op1,
assertion.op1.kind = O1K_ARR_BND;
assertion.op1.bnd.vnIdx = optConservativeNormalVN(arrBndsChk->GetIndex());
assertion.op1.bnd.vnLen = optConservativeNormalVN(arrBndsChk->GetArrayLength());

if ((assertion.op1.bnd.vnIdx == ValueNumStore::NoVN) || (assertion.op1.bnd.vnLen == ValueNumStore::NoVN))
{
// Don't make an assertion if one of the operands has no VN
return NO_ASSERTION_INDEX;
}

goto DONE_ASSERTION;
}
}
Expand Down Expand Up @@ -1979,6 +1988,30 @@ void Compiler::optCreateComplementaryAssertion(AssertionIndex assertionIndex,

if (candidateAssertion.assertionKind == OAK_EQUAL)
{
// Don't create useless OAK_NOT_EQUAL assertions

if ((candidateAssertion.op1.kind == O1K_LCLVAR) || (candidateAssertion.op1.kind == O1K_VN))
{
// "LCLVAR != CNS" is not a useful assertion (unless CNS is 0/1)
if (((candidateAssertion.op2.kind == O2K_CONST_INT) || (candidateAssertion.op2.kind == O2K_CONST_LONG)) &&
(candidateAssertion.op2.u1.iconVal != 0) && (candidateAssertion.op2.u1.iconVal != 1))
{
return;
}

// "LCLVAR != LCLVAR_COPY"
if (candidateAssertion.op2.kind == O2K_LCLVAR_COPY)
{
return;
}
}

// "Object is not Class" is also not a useful assertion (at least for now)
if ((candidateAssertion.op1.kind == O1K_EXACT_TYPE) || (candidateAssertion.op1.kind == O1K_SUBTYPE))
{
return;
}

AssertionIndex index = optCreateAssertion(op1, op2, OAK_NOT_EQUAL, helperCallArgs);
optMapComplementary(index, assertionIndex);
}
Expand All @@ -1987,12 +2020,6 @@ void Compiler::optCreateComplementaryAssertion(AssertionIndex assertionIndex,
AssertionIndex index = optCreateAssertion(op1, op2, OAK_EQUAL, helperCallArgs);
optMapComplementary(index, assertionIndex);
}

// Are we making a subtype or exact type assertion?
if ((candidateAssertion.op1.kind == O1K_SUBTYPE) || (candidateAssertion.op1.kind == O1K_EXACT_TYPE))
{
optCreateAssertion(op1, nullptr, OAK_NOT_EQUAL);
}
}

// optAssertionGenCast: Create a tentative subrange assertion for a cast.
Expand Down Expand Up @@ -2154,6 +2181,12 @@ AssertionInfo Compiler::optCreateJTrueBoundsAssertion(GenTree* tree)
dsc.op2.kind = O2K_INVALID;
dsc.op2.vn = ValueNumStore::NoVN;

if ((dsc.op1.bnd.vnIdx == ValueNumStore::NoVN) || (dsc.op1.bnd.vnLen == ValueNumStore::NoVN))
{
// Don't make an assertion if one of the operands has no VN
return NO_ASSERTION_INDEX;
}

AssertionIndex index = optAddAssertion(&dsc);
if (unsignedCompareBnd.cmpOper == VNF_GE_UN)
{
Expand Down
3 changes: 2 additions & 1 deletion src/coreclr/jit/rangecheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,8 @@ void RangeCheck::MergeEdgeAssertions(ValueNum normalLclVN, ASSERT_VALARG_TP asse
continue;
}

int cnstLimit = m_pCompiler->vnStore->CoercedConstantValue<int>(curAssertion->op2.vn);
int cnstLimit = (int)curAssertion->op2.u1.iconVal;
assert(cnstLimit == m_pCompiler->vnStore->CoercedConstantValue<int>(curAssertion->op2.vn));

if ((cnstLimit == 0) && (curAssertion->assertionKind == Compiler::OAK_NOT_EQUAL) &&
m_pCompiler->vnStore->IsVNCheckedBound(curAssertion->op1.vn))
Expand Down
Loading