Repository navigation
JIT: Narrow long comparisons of sign-extended ints to int in VN - #129148
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 <[email protected]>
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.
|
PTAL @jakobbotsch @dotnet/jit-contrib we have Quite big diffs in libraries_tests.run: diffs |
) Simplify some relops from LONG relop LONG to INT relop INT. Also, enable unsigned for IgnoreVNIntToLong [diffs](https://dev.azure.com/dnceng-public/public/_build/results?buildId=1455284&view=ms.vss-build-web.run-extensions-tab) Co-authored-by: Copilot <[email protected]>
Simplify some relops from LONG relop LONG to INT relop INT. Also, enable unsigned for IgnoreVNIntToLong
diffs