Skip to content

Ensure we preserve vn and produce a canonical tree#127689

Open
tannergooding wants to merge 2 commits intodotnet:mainfrom
tannergooding:fix-morph
Open

Ensure we preserve vn and produce a canonical tree#127689
tannergooding wants to merge 2 commits intodotnet:mainfrom
tannergooding:fix-morph

Conversation

@tannergooding
Copy link
Copy Markdown
Member

This ensures that we preserve the VN when morphing a particular MOD->AND pattern and ensures that a particular SUB->ADD pattern produces a "canonical" tree with the constant as the second operand.

Copilot AI review requested due to automatic review settings May 2, 2026 22:56
@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 2, 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.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates CoreCLR JIT morphing to (1) preserve value numbering (VN) across a specific MOD -> AND strength-reduction used under ==/!= 0 comparisons, and (2) canonicalize a SUB -> ADD rewrite so the constant ends up as the second operand.

Changes:

  • Preserve VN when rewriting (a % c) ==/!= 0 (power-of-2 c) into a & (c - 1) ==/!= 0.
  • Adjust the cns1 - op2 rewrite to produce ((-op2) + cns1) (constant as operand 2).
  • Update the in-code comment to reflect the intended canonical tree form.

Comment thread src/coreclr/jit/morph.cpp Outdated
Copilot AI review requested due to automatic review settings May 3, 2026 03:18
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comment thread src/coreclr/jit/morph.cpp
@tannergooding
Copy link
Copy Markdown
Member Author

CC. @EgorBo, no diffs but cleans up the pre-existing PreserveVN case you commented on in the other PR

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

/* Check for "cns1 - op2" , we change it to "(cns1 + (-op2))" */
/* Check for "cns1 - op2" , we change it to "((-op2) + cns1)" */
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wonder if this transformation even makes any impact and do we revert it back to cns - op2 near codegen 🤔 otherwise we seems to be introducing a new NEG on top of X

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The impact is that adds are associative, while subtracts are not and we have various opts like (x + cns1) + (y + cns2) -> (x + y) + cns3 or (x + cns1) + cns2 to x + cnx3 which we wouldn't find otherwise.

We also tend to not look at subtracts at all for things like address computation or similar; so x - cns would be skipped over while x + (-cns) would not, and same again for cns - x vs (-x) + cns.

We could of course update all those places to handle subtracts as well, and maybe that's even beneficial, but the canonicalization to an add form simplifies a lot of things (at least when constants are involved)

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.

I wonder if this transformation even makes any impact and do we revert it back to cns - op2 near codegen 🤔 otherwise we seems to be introducing a new NEG on top of X

We don't revert to cns - op2 near codegen. I tried to do so in Lower and it had regressions #126442 (comment). From my understanding problem was register alloc, specifically extra mov.

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.

4 participants