This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Move magic division optimization from morph to lowering #8106
Merged
Conversation
This file contains 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
mikedn
force-pushed
the
magic-div
branch
7 times, most recently
from
November 14, 2016 22:39
6c74632
to
1a8c826
Compare
@dotnet-bot test Windows_NT x86 legacy_backend Checked Build and Test |
No actual code changes.
Currently this optimization is done during global morph. Global morph takes place very early so cases that rely on constant propagation and folding (e.g "int d = 3; return n / d;") aren't handled. Also, the IR generated during morph is complex and unlikely to enable further optimizations. Quite the contrary, the presence of GT_ASG and GT_MULHI nodes blocks other optimizations (CSE, LICM currently). The generated code is identical to the code generated by the morph implementation with one exception: when 2 shifts are needed (e.g. for x / 7) they are now computed independently: mov eax, edx sar eax, 2 shr edx, 31 add eax, edx instead of: mov eax, edx sar eax, 2 mov edx, eax shr edx, 31 add eax, edx This results in shorter code and avoids creating an additional temp.
This is no longer needed, lowering does this now.
Doing magic division in morph did have one positive effect. For code like `x = a / 10; y = a % 10;` some (not all due to an assignment) of the common code was CSEd. To preserve this optimization we transform `a % b` to `a - (a / b) * b` if we know that lowering will do magic division. If a redundant `a / b` exists then CSE can pick it up.
We only care about the upper 32 bits of the 64 bit result and those are in EDX. It doesn't make sense to set the destination candidate to EAX, most of the time this will result in a useless `mov eax, edx` being generated. Also, genCodeForMulHi needs to check if the implicit operand is in EAX rather than targetReg.
jit-diff summary:
Elimination of useless
|
mikedn
changed the title
[WIP] Move magic division optimization from morph to lowering
Move magic division optimization from morph to lowering
Nov 15, 2016
cc @dotnet/jit-contrib |
LGTM. |
picenka21
pushed a commit
to picenka21/runtime
that referenced
this pull request
Feb 18, 2022
Move magic division optimization from morph to lowering Commit migrated from dotnet/coreclr@d92a1ce
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Fixes #2183
This fixes all issues mentioned in 2183 by moving magic division to lowering and fixing
GT_MULHI
register requirements.As mentioned in the issue doing magic division during global morph has a few problems - increased IR complexity, missed or partial CSE and LICM, missed magic division in cases that depend on constant propagation and folding.
It did, however, had one positive effect. In code like
a = x / 3; b = x % 3;
most of the common division code was CSEed. To preserve this useful side-effect I expandedx % 3
tox - (x / 3) * 3
in morph (there was already code to do this for ARM). This allows CSE to remove a redundantx / 3
(if any).This expansion is currently done only if we know that lower will do magic division because magic division does the expansion anyway. We could do this for mod by power of 2 but the lowering or something else will need to fold
(shl(shr(x, 2), 2)
into a singleand
like the current mod by power of 2 does. But that's stuff for another PR.