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

Reapply "Fold "X relop 0" in assertprop" (#110129) #110142

Merged
merged 1 commit into from
Nov 25, 2024
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
42 changes: 40 additions & 2 deletions src/coreclr/jit/assertionprop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3957,7 +3957,8 @@ void Compiler::optAssertionProp_RangeProperties(ASSERT_VALARG_TP assertions,
}

// First, analyze possible X ==/!= CNS assertions.
if (curAssertion->IsConstantInt32Assertion() && (curAssertion->op1.vn == treeVN))
if (curAssertion->IsConstantInt32Assertion() && (curAssertion->op1.kind == O1K_LCLVAR) &&
(curAssertion->op1.vn == treeVN))
Comment on lines +3960 to +3961
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why must the first operand be a LCLVAR to allow using these assertions?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jakobbotsch because O1K_ARR_BND is a weird assertion (when used with OAK_EQUAL/OAK_NOT_EQUAL). See here

e.g. when we have if (arr.Length == 0) we create an assertion op1 OAK_NOT_EQUAL 0 (inside true edge). To be fair, I think this one should be removed completely and be replaced with O1K_LCLVAR (not sure it works for trees not being GT_LCLVAR).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be fair, I think this one should be removed completely

Yeah, it seems like it's just creating wrong assertions that the rest of assertion prop then needs to compensate for? Definitely looks odd that you have to dig into the operands and then the entire assertion changes meaning depending on what the first operand is.

{
if ((curAssertion->assertionKind == OAK_NOT_EQUAL) && (curAssertion->op2.u1.iconVal == 0))
{
Expand Down Expand Up @@ -4295,6 +4296,44 @@ GenTree* Compiler::optAssertionPropGlobal_RelOp(ASSERT_VALARG_TP assertions, Gen
GenTree* op1 = tree->AsOp()->gtOp1;
GenTree* op2 = tree->AsOp()->gtOp2;

// Can we fold "X relop 0" based on assertions?
if (op2->IsIntegralConst(0) && tree->OperIsCmpCompare())
{
bool isNonZero, isNeverNegative;
optAssertionProp_RangeProperties(assertions, op1, &isNonZero, &isNeverNegative);

if (tree->OperIs(GT_GE, GT_LT) && isNeverNegative)
{
// Assertions: X >= 0
//
// X >= 0 --> true
// X < 0 --> false
newTree = tree->OperIs(GT_GE) ? gtNewTrue() : gtNewFalse();
}
else if (tree->OperIs(GT_GT, GT_LE) && isNeverNegative && isNonZero)
{
// Assertions: X > 0
//
// X > 0 --> true
// X <= 0 --> false
newTree = tree->OperIs(GT_GT) ? gtNewTrue() : gtNewFalse();
}
else if (tree->OperIs(GT_EQ, GT_NE) && isNonZero)
{
// Assertions: X != 0
//
// X != 0 --> true
// X == 0 --> false
newTree = tree->OperIs(GT_NE) ? gtNewTrue() : gtNewFalse();
}

if (newTree != tree)
{
newTree = gtWrapWithSideEffects(newTree, tree, GTF_ALL_EFFECT);
return optAssertionProp_Update(newTree, tree, stmt);
}
}

// Look for assertions of the form (tree EQ/NE 0)
AssertionIndex index = optGlobalAssertionIsEqualOrNotEqualZero(assertions, tree);

Expand All @@ -4315,7 +4354,6 @@ GenTree* Compiler::optAssertionPropGlobal_RelOp(ASSERT_VALARG_TP assertions, Gen

newTree = curAssertion->assertionKind == OAK_EQUAL ? gtNewIconNode(0) : gtNewIconNode(1);
newTree = gtWrapWithSideEffects(newTree, tree, GTF_ALL_EFFECT);
newTree = fgMorphTree(newTree);
DISPTREE(newTree);
return optAssertionProp_Update(newTree, tree, stmt);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>None</ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>
<IlcExportUnmanagedEntrypoints>true</IlcExportUnmanagedEntrypoints>
</PropertyGroup>
<!-- Expose unmanaged entry points from NativeExports -->
<!-- Expose unmanaged entry points from NativeExports -->
<ItemGroup Condition="'$(ReferencesNativeExports)' == 'true'">
<UnmanagedEntryPointsAssembly Include="Microsoft.Interop.Tests.NativeExports" />
<DirectPInvoke Include="Microsoft.Interop.Tests.NativeExportsNE" />
Expand Down
Loading