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

Expand "return condition" into "return condition ? true : false" #107499

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Sep 7, 2024

Closes #8363

Various control-flow related optimizations do not kick in for return <condition> patterns properly, because JIT mostly works with BBJ_COND types of blocks. optOptimizeBool is one of few phases which does support BBJ_RETURN but that support takes extra (a lot of) code to support.

Instead, let's just expand all return condition into return condition ? true : false and then convert back to return condition where profitable.

Example:

static bool Test(int x)
{
    return x == 10 || x == 20 || x == 30;
}

Here, "recognize bit-test" optimization (fgRecognizeSwitch) does not kick in due to BBJ_RETURN:

; Assembly listing for method Foo:Test(int):ubyte (FullOpts)
       cmp      ecx, 10
       je       SHORT G_M12679_IG04
       cmp      ecx, 20
       jne      SHORT G_M12679_IG06
G_M12679_IG04:
       mov      eax, 1
       ret      
G_M12679_IG06:
       cmp      ecx, 30
       sete     al
       movzx    rax, al
       ret      
; Total bytes of code 26

This PR:

; Assembly listing for method Foo:Test(int):ubyte (FullOpts)
       cmp      ecx, 30
       ja       SHORT G_M12679_IG06
       mov      eax, 0x3FEFFBFF
       bt       eax, ecx
       jb       SHORT G_M12679_IG06
       mov      eax, 1
       ret      
G_M12679_IG06:
       xor      eax, eax
       ret
; Total bytes of code 24

@stephentoub hit this problem when he was trying to showcase various jit optimizations on simple methods and had to rewrite them into if-else.

As a follow-up I might clean up various phases relying on BBJ_RETURN

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Sep 7, 2024
@EgorBo
Copy link
Member Author

EgorBo commented Sep 8, 2024

@MihuBot

@EgorBo
Copy link
Member Author

EgorBo commented Sep 14, 2024

Will first address various size regressions in separate PRs before moving forward with this.

@xtqqczze
Copy link
Contributor

@EgorBo Should this also close #65370?

@EgorBo
Copy link
Member Author

EgorBo commented Oct 10, 2024

@EgorBo Should this also close #65370?

thanks, indeed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Different codegen for return cond; vs if (cond) return true; return false;
3 participants