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
8270366: C2: Add associative rule to add/sub node #4765
Conversation
|
@zhengyu123 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
|
Hi Zhengyu, It looks like you are missing a case for both long and int. Your code looks for these patterns:
So you are not transforming this this case
|
Can this rule cause the JIT to compute different results than the interpreter if there is integer overflow? Does the spec allow that? |
@jddarcy may know the answer. |
I don't believe so. It should obvious that unsigned arithmetic will always give the same result for any order of computation. Why? Well, if we compute the result for a sequence of n-bit operations in an unlimited bit field we can be sure the results are order independent because of the associativity law. Setting and clearing of higher bits because of overflow beyond n-bits will not change the result that appears in the low n bits. For signed arithmetic all we need to note is that for every signed n-bit operation that overflows (or underflows) there is an equivalent unsigned operation in 2n bits that has the same bottom n-bits plus some extra non-zero bits in the top n bits. So, for any chain of k signed operations there will be an equivalent chain of unsigned operations in at most kn bits which has the same low n bits. Re-ordering the unsigned operations would produce the same unsigned result which guarantees that an equivalent re-ordering of the signed operations will produce the same signed value in the low n bits. |
|
Thanks, Andrew. Updated. |
Based on [https://www.agner.org/optimize/optimizing_cpp.pdf](Optimizing software in C++), this optimization is implemented in all 4 compilers (GNU, Clang, Microsoft and Intel) author investigated. For example:
GNU/Clang:
MSVC:
|
https://docs.oracle.com/javase/specs/jls/se16/html/jls-15.html#jls-15.17.1
I am interpreting this as using long arithmetic for integer overflow and taking low 32-bits of result. In this sense suggested optimization is legal. But we may have bugs in VM. I suggest to add a regression test to compare golden (from Interpreter) values with results from optimized compiled code for all interesting/corner cases. |
This RFE would also be well suited for using the new IR framework to create an IR test to verify the new IR transformations. |
@vnkozlov Thanks for confirmation. I added a test as you suggested, but not sure it covers all interesting/corner cases, please advise. |
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.
Repetitive copy-past code pattern concern me - prone to bugs. Can you consider using next pattern instead:
if (op1 == Op_MulI && op2 == Op_MulI) {
Node* add_in1 = NULL;
Node* add_in2 = NULL;
Node* mul_in = NULL;
if (in1->in(1) == in2->in(1)) {
// Convert "a*b+a*c into a *(b+c)
add_in1 = in1->in(2);
add_in2 = in2->in(2);
mul_in = in1->in(1);
...
if (mul_in != NULL) {
Node* add = phase->transform(new AddINode(add_in1, add_in2));
return new MulINode(mul_in, add);
}
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.
I am fine with your selection of corner cases in the test.
You need second test file for long
values.
// Method should be compiled | ||
private static int addComp(int a, int b, int c) { | ||
return a * b + a * c; | ||
} |
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.
You don't need separate method for compilation. First call will be executed in interpreter.
private static int addInt(int a, int b, int c) { | ||
return a * b + a * c; |
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.
You need tests for all order variations you optimized.
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.
You need second review.
@zhengyu123 This change now passes all automated pre-integration checks. 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 39 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.
|
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.
This is definitely clearer and the test is a good safety net.
/integrate |
Going to push as commit 746fe5d.
Your commit was automatically rebased without conflicts. |
@zhengyu123 Pushed as commit 746fe5d. |
Please review this small patch that add associative rule to add/sub node, that eliminates a multiply instruction.
e.g.
x86_64:
Before:
After:
Progress
Issue
Reviewers
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/4765/head:pull/4765
$ git checkout pull/4765
Update a local copy of the PR:
$ git checkout pull/4765
$ git pull https://git.openjdk.java.net/jdk pull/4765/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 4765
View PR using the GUI difftool:
$ git pr show -t 4765
Using diff file
Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/4765.diff