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 wrong constant folding for bswap16 #67726

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 3 additions & 4 deletions src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13434,15 +13434,14 @@ GenTree* Compiler::gtFoldExprConst(GenTree* tree)
i1 = -i1;
break;

// Note: BSWAP16 is considered to leave upper 16 bits
// undefined as it differs by platform, so we cannot
// constant fold it.
case GT_BSWAP:
i1 = ((i1 >> 24) & 0xFF) | ((i1 >> 8) & 0xFF00) | ((i1 << 8) & 0xFF0000) |
((i1 << 24) & 0xFF000000);
break;

case GT_BSWAP16:
i1 = ((i1 >> 8) & 0xFF) | ((i1 << 8) & 0xFF00);
break;

case GT_CAST:
// assert (genActualType(tree->CastToType()) == tree->TypeGet());

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. The upper 16 bits are undefined.

//-----------------------------------------------------------------------------
// Binary operators (2 operands):
Expand Down
13 changes: 4 additions & 9 deletions src/coreclr/jit/valuenum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -557,14 +557,9 @@ T ValueNumStore::EvalOpSpecialized(VNFunc vnf, T v0)
case GT_NOT:
return ~v0;

case GT_BSWAP16:
{
UINT16 v0_unsigned = UINT16(v0);

v0_unsigned = ((v0_unsigned >> 8) & 0xFF) | ((v0_unsigned << 8) & 0xFF00);
return T(v0_unsigned);
}

// Note: BSWAP16 is considered to leave upper 16 bits
// undefined as it differs by platform, so we cannot
// constant fold it.
case GT_BSWAP:
if (sizeof(T) == 4)
{
Expand Down Expand Up @@ -3355,7 +3350,7 @@ bool ValueNumStore::CanEvalForConstantArgs(VNFunc vnf)
// Unary Ops
case GT_NEG:
case GT_NOT:
case GT_BSWAP16:
// Cannot constant fold BSWAP16 as upper bits are undefined.
case GT_BSWAP:

// Binary Ops
Expand Down
15 changes: 15 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,15 @@
// 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>