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 morph negation transforms #55145

Merged
merged 5 commits into from
Jul 5, 2021
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
12 changes: 5 additions & 7 deletions src/coreclr/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13955,8 +13955,7 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac)

// Skip optimization if non-NEG operand is constant.
// Both op1 and op2 are not constant because it was already checked above.
if (opts.OptimizationEnabled() && fgGlobalMorph &&
(((op1->gtFlags & GTF_EXCEPT) == 0) || ((op2->gtFlags & GTF_EXCEPT) == 0)))
if (opts.OptimizationEnabled() && fgGlobalMorph)
{
// a - -b = > a + b
// SUB(a, (NEG(b)) => ADD(a, b)
Expand All @@ -13981,7 +13980,7 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac)
// -a - -b = > b - a
// SUB(NEG(a), (NEG(b)) => SUB(b, a)

if (op1->OperIs(GT_NEG) && op2->OperIs(GT_NEG))
if (op1->OperIs(GT_NEG) && op2->OperIs(GT_NEG) && gtCanSwapOrder(op1, op2))
{
// tree: SUB
// op1: NEG
Expand Down Expand Up @@ -14180,15 +14179,14 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac)
}
}

if (opts.OptimizationEnabled() && fgGlobalMorph &&
(((op1->gtFlags & GTF_EXCEPT) == 0) || ((op2->gtFlags & GTF_EXCEPT) == 0)))
if (opts.OptimizationEnabled() && fgGlobalMorph)
{
// - a + b = > b - a
// ADD((NEG(a), b) => SUB(b, a)

// Skip optimization if non-NEG operand is constant.
if (op1->OperIs(GT_NEG) && !op2->OperIs(GT_NEG) &&
!(op2->IsCnsIntOrI() && varTypeIsIntegralOrI(typ)))
if (op1->OperIs(GT_NEG) && !op2->OperIs(GT_NEG) && !op2->IsIntegralConst() &&
gtCanSwapOrder(op1, op2))
{
// tree: ADD
// op1: NEG
Expand Down
43 changes: 43 additions & 0 deletions src/tests/JIT/Regression/JitBlue/Runtime_55140/Runtime_55140.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Runtime.CompilerServices;

public class Runtime_55140
{
private static int _value;

public static int Main()
{
_value = 100;
if (TestSubNegNeg() is not 1 and var subNegNeg)
{
Console.WriteLine($"TestSubNegNeg returned: {subNegNeg}. Expected: 1");
return 101;
}

_value = 100;
if (TestAddNeg() is not 1 and var addNeg)
{
Console.WriteLine($"TestAddNeg returned: {addNeg}. Expected: 1");
return 102;
}

return 100;
}

// Test that the ADD(NEG(a), b) => SUB(b, a) transform does not reorder persistent side effects.
[MethodImpl(MethodImplOptions.NoInlining)]
private static int TestAddNeg()
{
return -Increment() + _value;
}

// Test that the SUB(NEG(a), NEG(b)) => SUB(b, a) transform does not reorder persistent side effects.
[MethodImpl(MethodImplOptions.NoInlining)]
private static int TestSubNegNeg()
{
return -Increment() - -_value;
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static int Increment() => _value++;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
</PropertyGroup>
<PropertyGroup>
<DebugType>None</DebugType>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
</Project>