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

Optimize comparison of unknown type with a typeof #31686

Closed
wants to merge 7 commits into from

Commits on Feb 6, 2020

  1. Optimize comparison of unknown type with a typeof

    The JIT currently optimizes `System.Type` comparisons that involve either `typeof` on both sides of the comparison, or `typeof` on one side and `object.GetType` on the other.
    
    This adds handling for "anything" on one side and a `typeof` on the other side. The optimization removes the materialization of the `System.Type` instance from the `typeof`. This is done by calling a new helper that can compare a raw type handle with a `System.Type` instance.
    
    Obligatory before/after:
    
    ```csharp
    class Program
    {
        static Type s_Object = typeof(object);
    
        static int Main()
        {
            if (typeof(object) != s_Object)
                return 1;
            return 100;
        }
    }
    ```
    
    Before:
    
    ```asm
           56                   push     rsi
           4883EC20             sub      rsp, 32
           488B0D00000000       mov      rcx, qword ptr [(reloc)]
           FF1500000000         call     [CORINFO_HELP_TYPEHANDLE_TO_RUNTIMETYPE]
           488BF0               mov      rsi, rax
           FF1500000000         call     [CORINFO_HELP_READYTORUN_STATIC_BASE]
           483B30               cmp      rsi, gword ptr [rax]
           740B                 je       SHORT G_M24376_IG05
           B801000000           mov      eax, 1
           4883C420             add      rsp, 32
           5E                   pop      rsi
           C3                   ret
           B864000000           mov      eax, 100
           4883C420             add      rsp, 32
           5E                   pop      rsi
           C3                   ret
    ```
    
    After:
    
    ```asm
           4883EC28             sub      rsp, 40
           FF1500000000         call     [CORINFO_HELP_READYTORUN_STATIC_BASE]
           488B10               mov      rdx, gword ptr [rax]
           488B0D00000000       mov      rcx, qword ptr [(reloc)]
           FF1500000000         call     [CORINFO_HELP_ARE_TYPEHANDLE_AND_TYPE_EQUIVALENT]
           85C0                 test     eax, eax
           750A                 jne      SHORT G_M24376_IG05
           B801000000           mov      eax, 1
           4883C428             add      rsp, 40
           C3                   ret
           B864000000           mov      eax, 100
           4883C428             add      rsp, 40
           C3                   ret
    ```
    MichalStrehovsky committed Feb 6, 2020
    Configuration menu
    Copy the full SHA
    11f47d1 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    00b8a22 View commit details
    Browse the repository at this point in the history
  3. Review feedback

    MichalStrehovsky committed Feb 6, 2020
    Configuration menu
    Copy the full SHA
    1615ea5 View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    6996492 View commit details
    Browse the repository at this point in the history

Commits on Feb 17, 2020

  1. Configuration menu
    Copy the full SHA
    671fdf0 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    f82a5fc View commit details
    Browse the repository at this point in the history
  3. Formatting

    MichalStrehovsky committed Feb 17, 2020
    Configuration menu
    Copy the full SHA
    665385a View commit details
    Browse the repository at this point in the history