Skip to content

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

Merged
EgorBo merged 1 commit into
dotnet:mainfrom
EgorBo:egorbo/jit-narrow-long-cmp
Jun 9, 2026
Merged

EgorBo merged 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 unsigned for IgnoreVNIntToLong

diffs

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 <[email protected]>
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 Resolved
@EgorBo

EgorBo commented Jun 9, 2026 •

Copy link
Copy Markdown
Member Author

PTAL @jakobbotsch @dotnet/jit-contrib

we have ValueNum VNIgnoreIntToLongCast(ValueNum) function that just ignores sign-extension casts from int to long. It's actually can be extended for unsigned casts as well if the underlying object is known to be never negative (not having the sign bit).

Quite big diffs in libraries_tests.run: diffs

@EgorBo
EgorBo requested a review from jakobbotsch June 9, 2026 12:58
Comment thread src/coreclr/jit/valuenum.cpp Resolved
Comment thread src/coreclr/jit/valuenum.cpp Resolved
Comment thread src/coreclr/jit/valuenum.cpp Resolved
@EgorBo
EgorBo enabled auto-merge (squash) June 9, 2026 22:01
@EgorBo
EgorBo merged commit 63bf37b into dotnet:main Jun 9, 2026
146 checks passed
@EgorBo
EgorBo deleted the egorbo/jit-narrow-long-cmp branch June 9, 2026 22:01
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview6 milestone Jun 17, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 18, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

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.

3 participants