-
Notifications
You must be signed in to change notification settings - Fork 6.1k
8261008: Optimize Xor #2776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
8261008: Optimize Xor #2776
Conversation
This patch canonicalizes 'Xor' to constant zero when both inputs are the
same.
It's not quite easy to measure the performance change between 'xor' and
constant zero, the later is typically a single 'mov' in generated code.
But given by this transformation, c2 may perform some other more
powerful optimizations.
This was tested with the micro benchmark below. Loop in this case is
properly removed and the performance increases significantly.
```
public void xorTheSame(MyState s, Blackhole bh) {
int x = s.in1;
int y = s.in2;
for (int i = 0; i < 5000; i++) {
y = x ^ x;
x = y ^ y;
}
bh.consume(x);
}
```
[Test]
All jtreg tests passed without new failure.
Change-Id: I1334199868b07543c4fe004976c26bed9162a993
|
👋 Welcome back theRealELiu! A progress list of the required criteria for merging this PR into |
|
@theRealELiu The following label will be automatically applied to this pull request:
When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command. |
Webrevs
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wondering, why is x ^ x special? Shouldn't we then also optimize x - x, x | x and others?
|
If we agree that this optimization is desired, then it makes sense to also write optimizations for However, how often do these patterns occur in real code? Can you provide some examples (of source code from OSS projects) where |
I found this case by accident and didn't notice that other node might share the same problem. I suppose they would also bring some benifits without much effort. |
Thanks for your feedback. I didn't find them in real cases. This case was discussed in https://mail.openjdk.java.net/pipermail/hotspot-compiler-dev/2020-November/041798.html. To persue the optimal code, it could be done without too much effort. |
|
For |
Change-Id: Ic9fc01375801adc82c0a0289d9a11a5367031eb4
Hi Bernardo, I tried to find this by adding some instruments. The JDK itself exactly has this pattern. I think no one would write those kind of code directly, but GVN, after all, was a general phase which may create new node during several iteration. Besides the inline method may lead to this as well. --Eric |
|
@theRealELiu that's a very interesting find! It makes me wonder if these aren't also warnings analysis tools should provide (if it's caused by optimizations of user code), something like "expression is always 0". |
Thanks for your review. I moved those codes into |
|
I think C2 optimizations could lead to IR shapes containing that pattern even if the Java source code does not. In any case, such simple patterns should be optimized and as Vladimir mentioned, it should be a |
|
Please help to review the new commit. All jtreg tests passed without new failure. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good.
|
@theRealELiu This change now passes all automated pre-integration checks. ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details. After integration, the commit message for the final commit will be: You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed. At the time when this comment was updated there had been 122 new commits pushed to the
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details. As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@TobiHartmann, @vnkozlov) but any other Committer may sponsor as well. ➡️ To flag this PR as ready for integration with the above commit message, type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me too.
|
/integrate |
|
@theRealELiu |
|
/sponsor |
|
@vnkozlov @theRealELiu Since your change was applied there have been 129 commits pushed to the
Your commit was automatically rebased without conflicts. Pushed as commit 23ee60d. 💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored. |
This patch canonicalizes 'Xor' to constant zero when both inputs are the
same.
It's not quite easy to measure the performance change between 'xor' and
constant zero, the later is typically a single 'mov' in generated code.
But given by this transformation, c2 may perform some other more
powerful optimizations.
This was tested with the micro benchmark below. Loop in this case is
properly removed and the performance increases significantly.
[Test]
All jtreg tests passed without new failure.
Progress
Issue
Reviewers
Download
$ git fetch https://git.openjdk.java.net/jdk pull/2776/head:pull/2776$ git checkout pull/2776