Skip to content

JIT: Canonicalize '(A & pow2) == pow2' -> '(A & pow2) != 0'#128533

Merged
EgorBo merged 7 commits into
dotnet:mainfrom
BoyBaykiller:canon-bit-test-to-zero-on-right
Jun 4, 2026
Merged

JIT: Canonicalize '(A & pow2) == pow2' -> '(A & pow2) != 0'#128533
EgorBo merged 7 commits into
dotnet:mainfrom
BoyBaykiller:canon-bit-test-to-zero-on-right

Conversation

@BoyBaykiller

Copy link
Copy Markdown
Contributor

Basically so we always have 0 at the right-side:

  1. '(A & pow2) == pow2' -> '(A & pow2) != 0'
  2. '(A & pow2) != pow2' -> '(A & pow2) == 0'

Tthis currently helps optimizeBools, but I am also adding this with the future in mind.
Here is one example:

static bool Example(int foo)
{
    return ((foo & 4) == 4) || ((foo & 8) == 8);
}
;; ------ BASE
G_M000_IG02:                ;; offset=0x0000
       test     cl, 4
       je       SHORT G_M000_IG05
 
G_M000_IG03:                ;; offset=0x0005
       mov      eax, 1
 
G_M000_IG04:                ;; offset=0x000A
       ret      
 
G_M000_IG05:                ;; offset=0x000B
       test     cl, 8
       setne    al
       movzx    rax, al
 
G_M000_IG06:                ;; offset=0x0014
       ret      

;; ------ DIFF
G_M33938_IG02:  ;; offset=0x0000
       mov      eax, ecx
       and      eax, 4
       and      ecx, 8
       or       eax, ecx
       setne    al
       movzx    rax, al

Note: With #126852 this would then get transformed into just test 12.

* '(A & pow2) != pow2' -> '(A & pow2) == 0'
@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label May 24, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label May 24, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@BoyBaykiller BoyBaykiller marked this pull request as draft May 24, 2026 05:38
@BoyBaykiller BoyBaykiller marked this pull request as ready for review May 24, 2026 05:44
@MichalPetryka

Copy link
Copy Markdown
Contributor

@MihuBot -nuget

Comment thread src/coreclr/jit/morph.cpp Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Canonicalizes (A & pow2) == pow2 to (A & pow2) != 0 (and the inverse for !=) during morph relational-comparison optimization. This puts the comparison into a uniform form with 0 on the right, which immediately helps optimizeBools and sets the stage for further follow-on optimizations (e.g., the distributive transform in #126852).

Changes:

  • Add a canonicalization in fgOptimizeRelationalComparison that, when matching (A & pow2) ==/!= pow2, reverses the relop and bashes the RHS constant to zero, with VN updated accordingly.

@BoyBaykiller

Copy link
Copy Markdown
Contributor Author

@EgorBo I fixed the merge conflicts.

Comment thread src/coreclr/jit/morph.cpp Outdated
@BoyBaykiller BoyBaykiller reopened this Jun 3, 2026
@BoyBaykiller BoyBaykiller requested a review from EgorBo June 3, 2026 23:47
Comment thread src/coreclr/jit/morph.cpp
@BoyBaykiller

Copy link
Copy Markdown
Contributor Author

Are we good to merge :) ? I'd like to continue follow up work @EgorBo

@EgorBo

EgorBo commented Jun 4, 2026

Copy link
Copy Markdown
Member

/ba-g known CSharpMissingShebangInFileBasedProgram failure

@EgorBo EgorBo enabled auto-merge (squash) June 4, 2026 16:08
@EgorBo EgorBo merged commit ebb13c5 into dotnet:main Jun 4, 2026
148 of 151 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview6 milestone Jun 5, 2026
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 community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants