Fix #74020: Optimize consecutive shifts in JIT Morph#122533
Open
csa7mdm wants to merge 1 commit intodotnet:mainfrom
Open
Fix #74020: Optimize consecutive shifts in JIT Morph#122533csa7mdm wants to merge 1 commit intodotnet:mainfrom
csa7mdm wants to merge 1 commit intodotnet:mainfrom
Conversation
0b10860 to
cca8115
Compare
tannergooding
approved these changes
Dec 18, 2025
Member
tannergooding
left a comment
There was a problem hiding this comment.
LGTM. I do still think this would be better to handle in morph and that it would be good to track RSH(x, n) for 0 < n < bitWidth as being "never negative", but will defer to other input.
CC. @dotnet/jit-contrib for secondary review
Member
@csa7mdm, I also think it would be better to handle it in morph. Do you want to try that? |
Author
|
n workflow(s) awaiting approva |
Author
|
n workflow(s) awaiting approval |
Member
|
Approved workflow to run. |
This was referenced Jan 27, 2026
Open
Contributor
|
@csa7mdm it looks like the latest CI run failed if you want to take a look? |
Member
968d96a to
b9cbf68
Compare
Add consecutive shift folding to fgMorphSmpOpOptional (morph.cpp): - Fold (x shift c1) shift c2 into x shift (c1+c2) for same-op shifts - Handle RSH overshift with saturation to bitWidth-1 - Handle RSZ/LSH overshift by folding to zero constant - Guarded by fgGlobalMorph to run only during morphing phase Per reviewer feedback, this algebraic simplification belongs in Morph (not Lowering) so downstream passes (VN, CSE, LSRA) benefit from the simplified IR. Fixes dotnet#74020
b9cbf68 to
fc342c4
Compare
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.
Description
This PR addresses issue #74020 by adding a peephole optimization in the JIT Morph phase (
fgMorphSmpOpOptional) to combine consecutive right shift operations (GT_RSH/GT_RSZ) with constant shift amounts.Per reviewer feedback from @tannergooding and @JulieLeeMSFT, the optimization was moved from Lowering to Morph so downstream passes (VN, CSE, LSRA) benefit from the simplified IR.
Changes
src/coreclr/jit/morph.cpp- Addedcase GT_RSH/case GT_RSZtofgMorphSmpOpOptional:(x shift c1) shift c2intox shift (c1+c2)for same-operation shiftsbitWidth - 1fgGlobalMorphVerification
x / c1 / c2generates redundantshrinstructions