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

[JIT] X64/ARM64 - Fold 'x & 255' and 'x & 65535' to a cast #79630

Merged
merged 9 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
18 changes: 18 additions & 0 deletions src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13745,6 +13745,24 @@ GenTree* Compiler::gtFoldExprSpecial(GenTree* tree)
goto DONE_FOLD;
}
}
else if ((val == 0xFF) && !varTypeIsLong(tree))
{
op = gtNewCastNode(tree->TypeGet(), op, false, TYP_UBYTE);
if (fgGlobalMorph)
{
fgMorphTreeDone(op);
}
goto DONE_FOLD;
}
else if ((val == 0xFFFF) && !varTypeIsLong(tree))
{
op = gtNewCastNode(tree->TypeGet(), op, false, TYP_USHORT);
if (fgGlobalMorph)
{
fgMorphTreeDone(op);
}
goto DONE_FOLD;
}
else
{
/* The GTF_BOOLEAN flag is set for nodes that are part
Expand Down
69 changes: 66 additions & 3 deletions src/tests/JIT/opt/Remainder/IntRemainder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
TIHan marked this conversation as resolved.
Show resolved Hide resolved

using System;
using System.Runtime.CompilerServices;

Expand All @@ -9,6 +6,7 @@ namespace CodeGenTests
static class IntRemainder
{
static int _fieldValue = 123;
static uint _fieldValueUnsigned = 123;

[MethodImpl(MethodImplOptions.NoInlining)]
static int Int32_RemainderByOne()
Expand All @@ -32,6 +30,56 @@ static int Int32_RemainderByOneWithValue(int value)
return value % 1;
}

[MethodImpl(MethodImplOptions.NoInlining)]
static byte Byte_RemainderByMaxValuePlusOne(uint value)
{
// X64-NOT: and {{[a-z]+}}

// X64: movzx {{[a-z]+}}, {{[a-z]+}}

return (byte)(value % (Byte.MaxValue + 1));
}

[MethodImpl(MethodImplOptions.NoInlining)]
static ushort UInt16_RemainderByMaxValuePlusOne(uint value)
{
// X64-NOT: and {{[a-z]+}}

// X64: movzx {{[a-z]+}}, {{[a-z]+}}

return (ushort)(value % (UInt16.MaxValue + 1));
}

[MethodImpl(MethodImplOptions.NoInlining)]
static uint Byte_RemainderByMaxValuePlusOne_Return_UInt32(uint value)
{
// X64-NOT: and {{[a-z]+}}

// X64: movzx {{[a-z]+}}, {{[a-z]+}}

return (value % (Byte.MaxValue + 1));
}

[MethodImpl(MethodImplOptions.NoInlining)]
static uint UInt16_RemainderByMaxValuePlusOne_Return_UInt32(uint value)
{
// X64-NOT: and {{[a-z]+}}

// X64: movzx {{[a-z]+}}, {{[a-z]+}}

return (value % (UInt16.MaxValue + 1));
}

[MethodImpl(MethodImplOptions.NoInlining)]
static byte Byte_RemainderByMaxValuePlusOne_WithField()
{
// X64-NOT: and {{[a-z]+}}

// X64: movzx {{[a-z]+}}, {{[a-z]+}}

return (byte)(_fieldValueUnsigned % (Byte.MaxValue + 1));
}

static int Main()
{
if (Int32_RemainderByOne() != 0)
Expand All @@ -40,6 +88,21 @@ static int Main()
if (Int32_RemainderByOneWithValue(-123) != 0)
return 0;

if (Byte_RemainderByMaxValuePlusOne(68000) != 160)
return 0;

if (UInt16_RemainderByMaxValuePlusOne(68000) != 2464)
return 0;

if (Byte_RemainderByMaxValuePlusOne_Return_UInt32(68000) != 160)
return 0;

if (UInt16_RemainderByMaxValuePlusOne_Return_UInt32(68000) != 2464)
return 0;

if (Byte_RemainderByMaxValuePlusOne_WithField() != 123)
return 0;

return 100;
}
}
Expand Down