JIT: Narrow long comparisons of sign-extended ints to int in VN#129148
Open
EgorBo wants to merge 1 commit into
Open
JIT: Narrow long comparisons of sign-extended ints to int in VN#129148EgorBo wants to merge 1 commit into
EgorBo wants to merge 1 commit into
Conversation
Integer comparisons whose two TYP_LONG operands are both just sign-extended TYP_INT values (or int-fitting TYP_LONG constants) can be evaluated in TYP_INT instead. Sign-extension is monotonic for both the signed and the unsigned interpretation, so the comparison result is unchanged. Operating in TYP_INT is preferred because assertion prop and bounds-check optimizations only consume TYP_INT comparisons (optCreateJTrueBoundsAssertion bails on wider operands). For example `(nint)arr.Length > 100L` becomes `arr.Length > 100`, which then lets the bounds check on `arr[100]` be eliminated. This is done in EvalUsingMathIdentity by re-issuing the comparison through VNIgnoreIntToLongCast. VNIgnoreIntToLongCast is also extended to look through zero-extending int->long casts of never-negative sources (e.g. the common `(nuint)`/`(uint)arr.Length` widenings) and TYP_ULONG cast targets, since for a non-negative value zero- and sign-extension produce the same bits. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates JIT value numbering to recognize when comparisons between two TYP_LONG operands are effectively comparisons between 32-bit values (due to int-to-long casts / int-fitting long constants), and re-issues those comparisons as TYP_INT so downstream optimizations that only consume int comparisons (notably assertion propagation and bounds-check elimination) can kick in.
Changes:
- Extend
VNIgnoreIntToLongCastto also look through zero-extending int→(u)long casts when the source is proven never-negative, and to treat “(u)long-like” cast targets viagenActualType. - Add a folding step in
EvalUsingMathIdentityto narrow long comparisons to int comparisons when both operands can be reduced toTYP_INT.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Simplify some relops from LONG relop LONG to INT relop INT. Also, enable unsgined for IgnoreVNIntToLong