RuyJIT: Array.Length / cns can be converted to unsigned div (mod)#32368
Merged
sandreenko merged 4 commits intodotnet:masterfrom May 27, 2020
Merged
RuyJIT: Array.Length / cns can be converted to unsigned div (mod)#32368sandreenko merged 4 commits intodotnet:masterfrom
Array.Length / cns can be converted to unsigned div (mod)#32368sandreenko merged 4 commits intodotnet:masterfrom
Conversation
ahsonkhan
reviewed
Feb 19, 2020
src/coreclr/tests/src/JIT/opt/InstructionCombining/ArrayLengthArithmetic.cs
Show resolved
Hide resolved
ahsonkhan
reviewed
Feb 19, 2020
src/coreclr/tests/src/JIT/opt/InstructionCombining/ArrayLengthArithmetic.cs
Outdated
Show resolved
Hide resolved
Member
Author
|
@dotnet/jit-contrib Please take a look, the change is pretty harmless, isn't it? |
| [MethodImpl(MethodImplOptions.NoInlining)] private static int ArrayLengthDiv_cnsMaxValue(int[] array) => array.Length / int.MaxValue; | ||
| [MethodImpl(MethodImplOptions.NoInlining)] private static int ArrayLengthDiv_cnsn1(int[] array) => array.Length / -1; | ||
| [MethodImpl(MethodImplOptions.NoInlining)] private static int ArrayLengthDiv_cnsn2(int[] array) => array.Length / -2; | ||
| [MethodImpl(MethodImplOptions.NoInlining)] private static int ArrayLengthDiv_cnsMinValue(int[] array) => array.Length / int.MinValue; |
Contributor
There was a problem hiding this comment.
consider adding few cases for long.
| } | ||
|
|
||
| // Array.Length / cns | ||
| [MethodImpl(MethodImplOptions.NoInlining)] private static int ArrayLengthDiv_cns0(int[] array) => array.Length / 0; |
Contributor
There was a problem hiding this comment.
ArrayLengthDiv_cns0 [](start = 66, length = 19)
should we add cases where the operation could become CSE candidate?
.. = array.Length / 3;
...
.. = array.Length / 3;
Contributor
There was a problem hiding this comment.
I do not see any potential issues here, because we don't change tree->gtType, so I can't construct a test where this transformation can lead to add new CSE candidates or reject an existing.
It would be nice to improve the test even more, but I would not block the PR because of that.
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 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.
Array.Lengthis a signed integer but since it should not be negative we can optimize some arithmetic operations, e.g. division (which is faster for unsigned), e.g.:Current codegen:
New codegen:
(non-power of two constants are also optimized better than for signed div)
Jit-diff (-f -pmi)
Full jit-diff: https://gist.github.com/EgorBo/4e784cb90b1222bc928b1a27ccea7efa