Skip to content

JIT: Narrow long comparisons of sign-extended ints to int in VN#129148

Open
EgorBo wants to merge 1 commit into
dotnet:mainfrom
EgorBo:egorbo/jit-narrow-long-cmp
Open

JIT: Narrow long comparisons of sign-extended ints to int in VN#129148
EgorBo wants to merge 1 commit into
dotnet:mainfrom
EgorBo:egorbo/jit-narrow-long-cmp

Conversation

@EgorBo

@EgorBo EgorBo commented Jun 9, 2026

Copy link
Copy Markdown
Member

Simplify some relops from LONG relop LONG to INT relop INT. Also, enable unsgined for IgnoreVNIntToLong

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>
Copilot AI review requested due to automatic review settings June 9, 2026 01:30
@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 Jun 9, 2026

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

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 VNIgnoreIntToLongCast to also look through zero-extending int→(u)long casts when the source is proven never-negative, and to treat “(u)long-like” cast targets via genActualType.
  • Add a folding step in EvalUsingMathIdentity to narrow long comparisons to int comparisons when both operands can be reduced to TYP_INT.

Comment thread src/coreclr/jit/valuenum.cpp
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.

2 participants