Skip to content

Commit

Permalink
Fix morph negation transforms (#55145)
Browse files Browse the repository at this point in the history
* Add a test

* Fix the SUB(NEG(a), (NEG(b)) => SUB(b, a) case

Also relax the condition for SUB(a, (NEG(b)) => ADD(a, b).

* Fix the ADD((NEG(a), b) => SUB(b, a) case

Also relax the condition for ADD(a, (NEG(b)) => SUB(a, b).
  • Loading branch information
SingleAccretion authored Jul 5, 2021
1 parent 7fed263 commit 5065d3b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/coreclr/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12661,8 +12661,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 @@ -12687,7 +12686,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 @@ -12886,15 +12885,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>

0 comments on commit 5065d3b

Please sign in to comment.