Skip to content

Commit

Permalink
Ensure DecomposeRotate correctly orders parameters. (dotnet#86066)
Browse files Browse the repository at this point in the history
* Add a regression test for dotnet#86207

* Ensure RotateRight and RotateLeft decomposition orders parameters correctly

* Add explicit tests covering the other two DecomposeRotate paths

* Ensure other code paths that create GT_RSH_LO nodes aren't broken
  • Loading branch information
tannergooding authored May 11, 2023
1 parent d4175a8 commit c13325f
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 6 deletions.
13 changes: 9 additions & 4 deletions src/coreclr/jit/codegenxarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4842,17 +4842,22 @@ void CodeGen::genCodeForShiftLong(GenTree* tree)

unsigned int count = (unsigned int)shiftBy->AsIntConCommon()->IconValue();

regNumber regResult = (oper == GT_LSH_HI) ? regHi : regLo;

inst_Mov(targetType, tree->GetRegNum(), regResult, /* canSkip */ true);

if (oper == GT_LSH_HI)
{
regNumber tgtReg = tree->GetRegNum();
assert(regLo != tgtReg);

inst_Mov(targetType, tgtReg, regHi, /* canSkip */ true);
inst_RV_RV_IV(ins, emitTypeSize(targetType), tree->GetRegNum(), regLo, count);
}
else
{
assert(oper == GT_RSH_LO);

regNumber tgtReg = tree->GetRegNum();
assert(regHi != tgtReg);

inst_Mov(targetType, tgtReg, regLo, /* canSkip */ true);
inst_RV_RV_IV(ins, emitTypeSize(targetType), tree->GetRegNum(), regHi, count);
}

Expand Down
9 changes: 7 additions & 2 deletions src/coreclr/jit/decomposelongs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1399,12 +1399,12 @@ GenTree* DecomposeLongs::DecomposeRotate(LIR::Use& use)
// For longs, we need to change rols into two GT_LSH_HIs and rors into two GT_RSH_LOs
// so we will get:
//
// shld lo, hi, rotateAmount
// shld lo, hiCopy, rotateAmount
// shld hi, loCopy, rotateAmount
//
// or:
//
// shrd lo, hi, rotateAmount
// shrd lo, hiCopy, rotateAmount
// shrd hi, loCopy, rotateAmount

if (oper == GT_ROL)
Expand Down Expand Up @@ -1475,6 +1475,11 @@ GenTree* DecomposeLongs::DecomposeRotate(LIR::Use& use)
hiOp1 = RepresentOpAsLocalVar(hiOp1, gtLong, &gtLong->AsOp()->gtOp2);
}

if (oper == GT_RSH_LO)
{
// lsra/codegen expects these operands in the opposite order
std::swap(loOp1, hiOp1);
}
Range().Remove(gtLong);

unsigned loOp1LclNum = loOp1->AsLclVarCommon()->GetLclNum();
Expand Down
87 changes: 87 additions & 0 deletions src/tests/JIT/Regression/JitBlue/Runtime_86027/Runtime_86207.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.CompilerServices;
using Xunit;

public class Runtime_86027
{
[Fact]
public static int TestRotateLeft1()
{
long[] firstOp = new long[] { 7822136809956075968, 1 };

if (long.RotateLeft(firstOp[0], 1) != -2802470453797399680)
{
return 0;
}

return 100;
}

[Fact]
public static int TestRotateLeft32()
{
long[] firstOp = new long[] { 7822136809956075968, 1 };

if (long.RotateLeft(firstOp[0], 32) != 3886722753596608508)
{
return 0;
}

return 100;
}

[Fact]
public static int TestRotateLeft33()
{
long[] firstOp = new long[] { 7822136809956075968, 1 };

if (long.RotateLeft(firstOp[0], 33) != 7773445507193217016)
{
return 0;
}

return 100;
}

[Fact]
public static int TestRotateRight1()
{
long[] firstOp = new long[] { 7822136809956075968, 1 };

if (long.RotateRight(firstOp[0], 1) != 3911068404978037984)
{
return 0;
}

return 100;
}

[Fact]
public static int TestRotateRight32()
{
long[] firstOp = new long[] { 7822136809956075968, 1 };

if (long.RotateRight(firstOp[0], 32) != 3886722753596608508)
{
return 0;
}

return 100;
}

[Fact]
public static int TestRotateRight33()
{
long[] firstOp = new long[] { 7822136809956075968, 1 };

if (long.RotateRight(firstOp[0], 33) != 1943361376798304254)
{
return 0;
}

return 100;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Optimize>True</Optimize>
<DebugType>None</DebugType>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
</Project>

0 comments on commit c13325f

Please sign in to comment.