RFR: 8270366: C2: Add associative rule to add/sub node [v2]
Vladimir Kozlov
kvn at openjdk.java.net
Wed Jul 14 15:23:13 UTC 2021
On Wed, 14 Jul 2021 14:04:41 GMT, Zhengyu Gu <zgu at openjdk.org> wrote:
>> Please review this small patch that add associative rule to add/sub node, that eliminates a multiply instruction.
>>
>> e.g.
>>
>> private static int assocInt(int a, int b, int c) {
>> return a * c + b * c;
>> }
>>
>>
>> x86_64:
>> Before:
>>
>> 0x00007fda1506152c: imul %ecx,%esi
>> 0x00007fda1506152f: imul %ecx,%edx
>> 0x00007fda15061532: mov %esi,%eax
>> 0x00007fda15061534: add %edx,%eax ;*iadd {reexecute=0 rethrow=0 return_oop=0}
>> ; - TestAssoc::assocInt at 6 (line 9)
>>
>>
>> After:
>>
>> 0x00007fc1c078d52c: add %edx,%esi
>> 0x00007fc1c078d52e: mov %ecx,%eax
>> 0x00007fc1c078d530: imul %esi,%eax ;*iadd {reexecute=0 rethrow=0 return_oop=0}
>> ; - TestAssoc::assocInt at 6 (line 9)
>
> Zhengyu Gu has updated the pull request with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains four additional commits since the last revision:
>
> - Missing case pointed out by Andrew Dinn
> - Merge branch 'master' into JDK-8270366-associative
> - sub
> - add
https://docs.oracle.com/javase/specs/jls/se16/html/jls-15.html#jls-15.17.1
If an integer multiplication overflows, then the result is the low-order bits of the mathematical product as represented in some sufficiently large two's-complement format. As a result, if overflow occurs, then the sign of the result may not be the same as the sign of the mathematical product of the two operand values.
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.
-------------
PR: https://git.openjdk.java.net/jdk/pull/4765
More information about the hotspot-compiler-dev
mailing list