Skip to content

Merge nested branches into AND test #101820

Open

Description

@reflectronic

Consider:

public static void M1(bool b1, bool b2) 
{
    if (b1)
    {
        if (b2)
        {
            Console.WriteLine(100);
        }
    }
}

public static void M2(bool b1, bool b2) 
{
    if (b1 & b2)
    {
        Console.WriteLine(100);
    }
}

The following code is generated:

;; coreclr trunk-20240502+71f8fb65a5a28018901823c19de57fe9451ab3b1

D:M1(ubyte,ubyte) (FullOpts):
G_M7125_IG01:  ;; offset=0x0000
       push     rbp
       mov      rbp, rsp
G_M7125_IG02:  ;; offset=0x0004
       test     dil, dil
       je       SHORT G_M7125_IG05
G_M7125_IG03:  ;; offset=0x0009
       test     sil, sil
       je       SHORT G_M7125_IG05
       mov      edi, 100
G_M7125_IG04:  ;; offset=0x0013
       pop      rbp
       tail.jmp [System.Console:WriteLine(int)]
G_M7125_IG05:  ;; offset=0x001A
       pop      rbp
       ret      

D:M2(ubyte,ubyte) (FullOpts):
G_M31862_IG01:  ;; offset=0x0000
G_M31862_IG02:  ;; offset=0x0000
       movzx    rdi, dil
       movzx    rax, sil
       test     edi, eax
       je       SHORT G_M31862_IG05
G_M31862_IG03:  ;; offset=0x000C
       mov      edi, 100
G_M31862_IG04:  ;; offset=0x0011
       tail.jmp [System.Console:WriteLine(int)]
G_M31862_IG05:  ;; offset=0x0017
       ret      

Though the two methods behave identically, M1 contains an extra test and branch (and frame, due to more complex control flow). The nested branches could be transformed into one AND test to optimize this automatically.

Activity

  1. added
    tenet-performance
    on May 2, 2024
  2. ghost added
    area-CodeGen-coreclr
    on May 2, 2024
  3. added
    untriaged
    on May 2, 2024
  4. dotnet-policy-service commented on May 2, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
    See info in area-owners.md if you want to be subscribed.

  5. reflectronic commented on May 2, 2024

    @reflectronic
    ContributorAuthor

    Though the two methods behave identically

    Hm. They do not for non-normalized bool values. That makes it more complicated than this (though perhaps still cheaply possible).

  6. added
    help wanted
    and removed
    untriaged
    on Jun 3, 2024
  7. added this to the Future milestone on Jun 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrhelp wantedtenet-performance

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions