Skip to content

JIT: Optimize linear mapping of values inside switch #132365

Description

@BoyBaykiller

https://godbolt.org/z/E1j3nM6c6

Something like the following can frequently happen when converting between enums:

static int GetFuncArgumentReg(int argumentId)
{
    if (argumentId == 0)
    {
        return 4;
    }
    if (argumentId == 1)
    {
        return 5;
    }
    if (argumentId == 2)
    {
        return 6;
    }
    if (argumentId == 3)
    {
        return 7;
    }

    throw new Exception();
}

There is a simple linear mapping: argumentId + 4.

We already have an optimization for GT_SWITCH that turns it into a rangecheck if possible. So adding this would be nothing new. However I see that the above code doesn't get switch-recognized, so that'd be the first thing to delve into probably.

Current assembly:

Program:GetFuncArgumentReg(int):int (FullOpts):
       push     rbp
       push     rbx
       push     rax
       lea      rbp, [rsp+0x10]
       test     edi, edi
       je       SHORT G_M29572_IG09
       cmp      edi, 1
       je       SHORT G_M29572_IG07
       cmp      edi, 2
       je       SHORT G_M29572_IG05
       cmp      edi, 3
       jne      SHORT G_M29572_IG11
       mov      eax, 7
       add      rsp, 8
       pop      rbx
       pop      rbp
       ret      
G_M29572_IG05:  ;; offset=0x0027
       mov      eax, 6
       add      rsp, 8
       pop      rbx
       pop      rbp
       ret      
G_M29572_IG07:  ;; offset=0x0033
       mov      eax, 5
       add      rsp, 8
       pop      rbx
       pop      rbp
       ret      
G_M29572_IG09:  ;; offset=0x003F
       mov      eax, 4
       add      rsp, 8
       pop      rbx
       pop      rbp
       ret      
G_M29572_IG11:  ;; offset=0x004B
       mov      rdi, 0x7D7F3F541690      ; System.Exception
       call     CORINFO_HELP_NEWSFAST
       mov      rbx, rax
       mov      rdi, rbx
       call     [System.Exception:.ctor():this]
       mov      rdi, rbx
       call     CORINFO_HELP_THROW
       int3     

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIuntriagedNew issue has not been triaged by the area owner

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions