Skip to content

Suboptimal codegen when comparing sbyte and 16-bit values #10337

@GrabYourPitchforks

Description

@GrabYourPitchforks

I have the following two repro methods along with their x64 codegen using the latest RyuJIT.

[MethodImpl(MethodImplOptions.NoInlining)]
public static bool MethodFoo(byte x)
{
    return ((byte)x < 128);
}
ConsoleApp12!ConsoleApp12.Program.MethodFoo(Byte):
00007ffe`2cb01630 80f980          cmp     cl,80h
00007ffe`2cb01633 0f92c0          setb    al
00007ffe`2cb01636 0fb6c0          movzx   eax,al
00007ffe`2cb01639 c3              ret
[MethodImpl(MethodImplOptions.NoInlining)]
public static bool MethodBar(byte x)
{
    return ((sbyte)x < -64);
}
ConsoleApp12!ConsoleApp12.Program.MethodBar(Byte):
00007ffe`2cb01650 0fb6c1          movzx   eax,cl
00007ffe`2cb01653 480fbec0        movsx   rax,al
00007ffe`2cb01657 83f8c0          cmp     eax,0FFFFFFC0h
00007ffe`2cb0165a 0f9cc0          setl    al
00007ffe`2cb0165d 0fb6c0          movzx   eax,al
00007ffe`2cb01660 c3              ret

The Foo method checks to see if the high bit is clear, and the Bar method checks to see if the byte has input pattern 10xxxxxx. These are both common operations when performing UTF-8 text processing. In the examples above, the Bar method has suboptimal codegen. I would instead expect the codegen to be the following.

cmp     cl,C0h
setl    al
movzx   eax,al
ret

category:cq
theme:basic-cq
skill-level:intermediate
cost:small

Metadata

Metadata

Assignees

No one assigned

    Labels

    JitUntriagedCLR JIT issues needing additional triagearea-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIin-prThere is an active PR which will close this issue when it is mergedoptimization

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions