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

Make BSWAP16 nodes normalize upper 16 bits #67903

Merged
merged 3 commits into from
Apr 20, 2022
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
1 change: 1 addition & 0 deletions src/coreclr/jit/codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,7 @@ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
void genCodeForIndir(GenTreeIndir* tree);
void genCodeForNegNot(GenTree* tree);
void genCodeForBswap(GenTree* tree);
bool genCanOmitNormalizationForBswap16(GenTree* tree);
void genCodeForLclVar(GenTreeLclVar* tree);
void genCodeForLclFld(GenTreeLclFld* tree);
void genCodeForStoreLclFld(GenTreeLclFld* tree);
Expand Down
11 changes: 8 additions & 3 deletions src/coreclr/jit/codegenarm64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3071,13 +3071,18 @@ void CodeGen::genCodeForBswap(GenTree* tree)
assert(operand->isUsedFromReg());
regNumber operandReg = genConsumeReg(operand);

if (tree->OperIs(GT_BSWAP16))
if (tree->OperIs(GT_BSWAP))
{
inst_RV_RV(INS_rev16, targetReg, operandReg, targetType);
inst_RV_RV(INS_rev, targetReg, operandReg, targetType);
}
else
{
inst_RV_RV(INS_rev, targetReg, operandReg, targetType);
inst_RV_RV(INS_rev16, targetReg, operandReg, targetType);

if (!genCanOmitNormalizationForBswap16(tree))
{
GetEmitter()->emitIns_Mov(INS_uxth, EA_4BYTE, targetReg, targetReg, /* canSkip */ false);
}
}

genProduceReg(tree);
Expand Down
34 changes: 34 additions & 0 deletions src/coreclr/jit/codegencommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9591,3 +9591,37 @@ void CodeGen::genCodeForBitCast(GenTreeOp* treeNode)
}
genProduceReg(treeNode);
}

//----------------------------------------------------------------------
// genCanOmitNormalizationForBswap16:
// Small peephole to check if a bswap16 node can omit normalization.
//
// Arguments:
// tree - The BSWAP16 node
//
// Remarks:
// BSWAP16 nodes are required to zero extend the upper 16 bits, but since the
// importer always inserts a normalizing cast (either sign or zero extending)
// we almost never need to actually do this.
//
bool CodeGen::genCanOmitNormalizationForBswap16(GenTree* tree)
{
if (compiler->opts.OptimizationDisabled())
{
return false;
}

assert(tree->OperIs(GT_BSWAP16));
if ((tree->gtNext == nullptr) || !tree->gtNext->OperIs(GT_CAST))
{
return false;
}

GenTreeCast* cast = tree->gtNext->AsCast();
if (cast->gtOverflow() || (cast->CastOp() != tree))
{
return false;
}

return (cast->gtCastType == TYP_USHORT) || (cast->gtCastType == TYP_SHORT);
}
5 changes: 5 additions & 0 deletions src/coreclr/jit/codegenxarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,11 @@ void CodeGen::genCodeForBswap(GenTree* tree)
{
// 16-bit byte swaps use "ror reg.16, 8"
inst_RV_IV(INS_ror_N, targetReg, 8 /* val */, emitAttr::EA_2BYTE);

if (!genCanOmitNormalizationForBswap16(tree))
{
GetEmitter()->emitIns_Mov(INS_movzx, EA_2BYTE, targetReg, targetReg, /* canSkip */ false);
}
}

genProduceReg(tree);
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/gtlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ GTNODE(RUNTIMELOOKUP , GenTreeRuntimeLookup, 0,GTK_UNOP|GTK_EXOP|DBK_NOTLIR)
GTNODE(ARR_ADDR , GenTreeArrAddr ,0,GTK_UNOP|GTK_EXOP|DBK_NOTLIR) // Wraps an array address expression

GTNODE(BSWAP , GenTreeOp ,0,GTK_UNOP) // Byte swap (32-bit or 64-bit)
GTNODE(BSWAP16 , GenTreeOp ,0,GTK_UNOP) // Byte swap (16-bit)
GTNODE(BSWAP16 , GenTreeOp ,0,GTK_UNOP) // Byte swap lower 16-bits and zero upper 16 bits

//-----------------------------------------------------------------------------
// Binary operators (2 operands):
Expand Down
14 changes: 14 additions & 0 deletions src/tests/JIT/Regression/JitBlue/Runtime_67723/Runtime_67723.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Buffers.Binary;

public class Runtime_67223
{
public static int Main()
{
short[] foo = { short.MinValue };
int test = BinaryPrimitives.ReverseEndianness(foo[0]);
return test == 0x80 ? 100 : -1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
</Project>