Skip to content

JIT: Compute ranges for RSH, RSZ and UDIV in IntegralRange::ForNode - #134739

Closed
p6laris wants to merge 1 commit into
dotnet:mainfrom
p6laris:jit-never-negative-shifts
Closed

p6laris wants to merge 1 commit into
dotnet:mainfrom
p6laris:jit-never-negative-shifts

Conversation

@p6laris

@p6laris p6laris commented Sep 27, 2026 •

Copy link
Copy Markdown

Fixes #119680

IntegralRange::ForNode didn't handle GT_RSH/GT_RSZ/GT_UDIV, so fgOptimizeCast couldn't tell that e.g. BitOperations.TrailingZeroCount(x) / 2 is non-negative, and the int -> long cast stayed sign-extending.
This adds:

  • RSH/RSZ/UDIV with a non-negative op1 → [0..upper(op1)]
  • RSZ by a constant in [1, bitWidth) → sign bit cleared
  • UDIV by a constant > 1 → sign bit cleared
        xor      eax, eax
        tzcnt    eax, ecx
        shr      eax, 1
-       cdqe
        ret

Local SuperPMI (windows-x64): -4,559 bytes overall, all collections improve. Added JIT/opt/Casts/NeverNegativeCast with disasm checks.

Note: LeadingZeroCount(ulong) >> 1 still sign-extends because the inner CAST(int <- long) loses the range. I think that's closer to #119699 / #132450, so I left it out.

IntegralRange::ForNode did not handle right shifts or unsigned
division, so casts from int to long over expressions such as
`BitOperations.TrailingZeroCount(x) / 2` were not proven non-negative
and kept a sign-extension.

- RSH/RSZ/UDIV with a non-negative op1 produce [0..upper(op1)].
- RSZ by a constant in [1, bitWidth) clears the sign bit.
- UDIV by a constant greater than one clears the sign bit.

Fixes dotnet#119680
@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 Sep 27, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Sep 27, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
11 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@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.

@EgorBo

EgorBo commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

Thanks for the contribution, I don't think th fix belongs to this function, we already do this optimization in the global assertion prop phase with help of VN/assertions, it just needs a small fix in assertionprop (relax a condition in optAssertionProp_RangeProperties), no need in +50 LOC in a function that I would love to remove some day.

@EgorBo EgorBo closed this Oct 16, 2026
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 community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unnecessary sign-extension for some never-negative expressions

2 participants