Skip to content

JIT: missing opportunity for Redundant Branch Opt #98227

Closed

Description

@EgorBo

A simplified repro of what I saw in a real code:

void Test(int a)
{
    if (a >= 100)
    {
        if (a <= 100)
        {
            Console.WriteLine(a);
        }
    }
}

Current codegen:

; Method Prog:Test(int):this (FullOpts)
G_M36082_IG01:
       sub      rsp, 40
G_M36082_IG02:
       cmp      edx, 100
       jl       SHORT G_M36082_IG04
G_M36082_IG03:
       cmp      edx, 100
       jg       SHORT G_M36082_IG04
       mov      ecx, edx
       call     [System.Console:WriteLine(int)]
G_M36082_IG04:
       nop      
G_M36082_IG05:
       add      rsp, 40
       ret      
; Total bytes of code: 28

Expected codegen:

; Method Prog:Test(int):this (FullOpts)
G_M36082_IG01:
       sub      rsp, 40
G_M36082_IG02:
       cmp      edx, 100
       jne      SHORT G_M36082_IG04
G_M36082_IG03:
       mov      ecx, 100
       call     [System.Console:WriteLine(int)]
G_M36082_IG04:
       nop      
G_M36082_IG05:
       add      rsp, 40
       ret      
; Total bytes of code: 26

Activity

  1. ghost added
    untriaged
    on Feb 9, 2024
  2. ghost added
    area-CodeGen-coreclr
    on Feb 9, 2024
  3. changed the title [-]JIT: Missing oportunity for Redundant Branch Opt[/-] [+]JIT: missing opportunity for Redundant Branch Opt[/+] on Feb 9, 2024
  4. removed
    untriaged
    on Feb 9, 2024
  5. added this to the Future milestone on Feb 9, 2024
  6. EgorBo commented on Feb 9, 2024

    @EgorBo
    MemberAuthor

    сс @AndyAyersMS I assume this is slightly different from "implied relops" opts we have.
    In this case we can:

    1. Change the 2nd condition to just == 100 (and then assertprop can propagate it as a constant)
    2. Ideally change the dominating comparison if there are no statements between two?
  7. AndyAyersMS commented on Feb 9, 2024

    @AndyAyersMS
    Member

    Looks similar to the case noted in #81220 (comment).

    Linked this example into #48115.

  8. AndyAyersMS commented on Apr 17, 2026

    @AndyAyersMS
    Member

    This looks like something we could handle with a small-ish extension to #126587.

    Given if (pA) if (pB) S;

    In #126587 we simplified flow if we could prove pB ==> pA to if (true) if (pB) S;

    We can also see if pB & pA simplifies to just one relop, and if so, a relop between the values already being used in pB, and if so, we can optimize to if (true) if (pA & pB) S;

  9. added a commit that references this issue on Apr 20, 2026
    fe46b64
  10. added
    in-pr
    on Apr 20, 2026
  11. added a commit that references this issue on Apr 24, 2026
    19f4dfc
  12. locked and limited conversation to collaborators on May 25, 2026
  13. added a commit that references this issue on Jul 15, 2026
    3cf428a
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

    JIT-RBOarea-CodeGen-coreclrin-pr

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions