RFR: 8273021: C2: Improve Add and Xor ideal optimizations [v3]

Tobias Hartmann thartmann at openjdk.java.net
Thu Sep 9 06:46:10 UTC 2021


On Wed, 8 Sep 2021 05:56:42 GMT, Yi Yang <yyang at openjdk.org> wrote:

>> Greetings. This patch adds the following identical equations for Add and Xor node, respectively, which probably drives further optimizations.
>> 
>> 
>> ~(x-1) => -x
>> ~x + 1 => -x
>> 
>> 
>> 
>> Verified by generated opto assembly, maybe an IR verification test can be added later.
>> 
>> Compiled method (c2)      71    1             compiler.c2.TestAddXorIdeal::test1 (6 bytes)
>>   0x00007f9e11514800:   sub    $0x18,%rsp
>>   0x00007f9e11514807:   mov    %rbp,0x10(%rsp)              ;*synchronization entry
>>                                                             ; - compiler.c2.TestAddXorIdeal::test1 at -1 (line 39)
>>   0x00007f9e1151480c:   mov    %esi,%eax
>>   0x00007f9e1151480e:   neg    %eax                         ;*iadd {reexecute=0 rethrow=0 return_oop=0}
>>                                                             ; - compiler.c2.TestAddXorIdeal::test1 at 4 (line 39)
>>   0x00007f9e11514810:   add    $0x10,%rsp
>>   0x00007f9e11514814:   pop    %rbp
>>   0x00007f9e11514815:   cmp    0x338(%r15),%rsp             ;   {poll_return}
>>   0x00007f9e1151481c:   ja     0x00007f9e11514823
>>   0x00007f9e11514822:   retq   
>> 
>> Compiled method (c2)      73    2             compiler.c2.TestAddXorIdeal::test2 (6 bytes)
>>   0x00007f9e11512480:   sub    $0x18,%rsp
>>   0x00007f9e11512487:   mov    %rbp,0x10(%rsp)              ;*synchronization entry
>>                                                             ; - compiler.c2.TestAddXorIdeal::test2 at -1 (line 43)
>>   0x00007f9e1151248c:   mov    %esi,%eax
>>   0x00007f9e1151248e:   neg    %eax                         ;*ixor {reexecute=0 rethrow=0 return_oop=0}
>>                                                             ; - compiler.c2.TestAddXorIdeal::test2 at 4 (line 43)
>>   0x00007f9e11512490:   add    $0x10,%rsp
>>   0x00007f9e11512494:   pop    %rbp
>>   0x00007f9e11512495:   cmp    0x338(%r15),%rsp             ;   {poll_return}
>>   0x00007f9e1151249c:   ja     0x00007f9e115124a3
>>   0x00007f9e115124a2:   retq 
>> 
>> Compiled method (c2)      72    3             compiler.c2.TestAddXorIdeal::test3 (8 bytes)
>>   0x00007f9e11514b00:   sub    $0x18,%rsp
>>   0x00007f9e11514b07:   mov    %rbp,0x10(%rsp)              ;*synchronization entry
>>                                                             ; - compiler.c2.TestAddXorIdeal::test3 at -1 (line 47)
>>   0x00007f9e11514b0c:   mov    %rsi,%rax
>>   0x00007f9e11514b0f:   neg    %rax                         ;*ladd {reexecute=0 rethrow=0 return_oop=0}
>>                                                             ; - compiler.c2.TestAddXorIdeal::test3 at 6 (line 47)
>>   0x00007f9e11514b12:   add    $0x10,%rsp
>>   0x00007f9e11514b16:   pop    %rbp
>>   0x00007f9e11514b17:   cmp    0x338(%r15),%rsp             ;   {poll_return}
>>   0x00007f9e11514b1e:   ja     0x00007f9e11514b25
>>   0x00007f9e11514b24:   retq  
>> 
>> Compiled method (c2)      72    4             compiler.c2.TestAddXorIdeal::test4 (8 bytes)
>>   0x00007f9e11514500:   sub    $0x18,%rsp
>>   0x00007f9e11514507:   mov    %rbp,0x10(%rsp)              ;*synchronization entry
>>                                                             ; - compiler.c2.TestAddXorIdeal::test4 at -1 (line 51)
>>   0x00007f9e1151450c:   mov    %rsi,%rax
>>   0x00007f9e1151450f:   neg    %rax                         ;*lxor {reexecute=0 rethrow=0 return_oop=0}
>>                                                             ; - compiler.c2.TestAddXorIdeal::test4 at 6 (line 51)
>>   0x00007f9e11514512:   add    $0x10,%rsp
>>   0x00007f9e11514516:   pop    %rbp
>>   0x00007f9e11514517:   cmp    0x338(%r15),%rsp             ;   {poll_return}
>>   0x00007f9e1151451e:   ja     0x00007f9e11514525
>>   0x00007f9e11514524:   retq  
>> 
>> Compiled method (c2)      72    5             compiler.c2.TestAddXorIdeal::test5 (6 bytes)
>>   0x00007f9e11518500:   sub    $0x18,%rsp
>>   0x00007f9e11518507:   mov    %rbp,0x10(%rsp)              ;*synchronization entry
>>                                                             ; - compiler.c2.TestAddXorIdeal::test5 at -1 (line 55)
>>   0x00007f9e1151850c:   mov    %esi,%eax
>>   0x00007f9e1151850e:   neg    %eax                         ;*iadd {reexecute=0 rethrow=0 return_oop=0}
>>                                                             ; - compiler.c2.TestAddXorIdeal::test5 at 4 (line 55)
>>   0x00007f9e11518510:   add    $0x10,%rsp
>>   0x00007f9e11518514:   pop    %rbp
>>   0x00007f9e11518515:   cmp    0x338(%r15),%rsp             ;   {poll_return}
>>   0x00007f9e1151851c:   ja     0x00007f9e11518523
>>   0x00007f9e11518522:   retq  
>> 
>> Compiled method (c2)      74    6             compiler.c2.TestAddXorIdeal::test6 (6 bytes)
>>   0x00007f9e11512180:   sub    $0x18,%rsp
>>   0x00007f9e11512187:   mov    %rbp,0x10(%rsp)              ;*synchronization entry
>>                                                             ; - compiler.c2.TestAddXorIdeal::test6 at -1 (line 59)
>>   0x00007f9e1151218c:   mov    %esi,%eax
>>   0x00007f9e1151218e:   neg    %eax                         ;*ixor {reexecute=0 rethrow=0 return_oop=0}
>>                                                             ; - compiler.c2.TestAddXorIdeal::test6 at 4 (line 59)
>>   0x00007f9e11512190:   add    $0x10,%rsp
>>   0x00007f9e11512194:   pop    %rbp
>>   0x00007f9e11512195:   cmp    0x338(%r15),%rsp             ;   {poll_return}
>>   0x00007f9e1151219c:   ja     0x00007f9e115121a3
>>   0x00007f9e115121a2:   retq  
>> 
>> Compiled method (c2)      74    7             compiler.c2.TestAddXorIdeal::test7 (8 bytes)
>>   0x00007f9e11511e80:   sub    $0x18,%rsp
>>   0x00007f9e11511e87:   mov    %rbp,0x10(%rsp)              ;*synchronization entry
>>                                                             ; - compiler.c2.TestAddXorIdeal::test7 at -1 (line 63)
>>   0x00007f9e11511e8c:   mov    %rsi,%rax
>>   0x00007f9e11511e8f:   neg    %rax                         ;*ladd {reexecute=0 rethrow=0 return_oop=0}
>>                                                             ; - compiler.c2.TestAddXorIdeal::test7 at 6 (line 63)
>>   0x00007f9e11511e92:   add    $0x10,%rsp
>>   0x00007f9e11511e96:   pop    %rbp
>>   0x00007f9e11511e97:   cmp    0x338(%r15),%rsp             ;   {poll_return}
>>   0x00007f9e11511e9e:   ja     0x00007f9e11511ea5
>>   0x00007f9e11511ea4:   retq 
>> 
>> 
>> Compiled method (c2)      73    8             compiler.c2.TestAddXorIdeal::test8 (10 bytes)
>>   0x00007f9e11512780:   sub    $0x18,%rsp
>>   0x00007f9e11512787:   mov    %rbp,0x10(%rsp)              ;*synchronization entry
>>                                                             ; - compiler.c2.TestAddXorIdeal::test8 at -1 (line 67)
>>   0x00007f9e1151278c:   mov    %rsi,%rax
>>   0x00007f9e1151278f:   neg    %rax                         ;*lxor {reexecute=0 rethrow=0 return_oop=0}
>>                                                             ; - compiler.c2.TestAddXorIdeal::test8 at 8 (line 67)
>>   0x00007f9e11512792:   add    $0x10,%rsp
>>   0x00007f9e11512796:   pop    %rbp
>>   0x00007f9e11512797:   cmp    0x338(%r15),%rsp             ;   {poll_return}
>>   0x00007f9e1151279e:   ja     0x00007f9e115127a5
>>   0x00007f9e115127a4:   retq
>
> Yi Yang has updated the pull request incrementally with two additional commits since the last revision:
> 
>  - dontinline
>  - more random

Changes requested by thartmann (Reviewer).

test/hotspot/jtreg/compiler/c2/TestAddXorIdeal.java line 28:

> 26:  * @test
> 27:  * @bug 8273021
> 28:  * @summary C2: Improve Add and Xor ideal optimizations

The test needs ` * @key randomness`

test/hotspot/jtreg/compiler/c2/TestAddXorIdeal.java line 30:

> 28:  * @summary C2: Improve Add and Xor ideal optimizations
> 29:  * @library /test/lib
> 30:  * @run main/othervm -XX:-TieredCompilation -XX:TieredStopAtLevel=4

`TieredStopAtLevel` has no effect if Tiered Compilation is turned off. You can remove it.

test/hotspot/jtreg/compiler/c2/TestAddXorIdeal.java line 75:

> 73: 
> 74:     public static void main(String... args) {
> 75:         Random random = new Random();

You should use `Utils.getRandomInstance()` from `import jdk.test.lib.Utils` to ensure that the seed is printed for reproducibility. You can check other tests for an example.

test/hotspot/jtreg/compiler/c2/TestAddXorIdeal.java line 77:

> 75:         Random random = new Random();
> 76:         int n = 0;
> 77:         long n1 = 0;

Should be declared in the loop.

test/hotspot/jtreg/compiler/c2/TestAddXorIdeal.java line 80:

> 78:         for (int i = -5_000; i < 5_000; i++) {
> 79:             n = random.nextInt();
> 80:             Asserts.assertTrue(test1(i + n) == -(i + n));

Now that you are using random numbers, can't you simply check `Asserts.assertTrue(test1(n) == -n)`? And just loop for a fixed number of iterations.

-------------

PR: https://git.openjdk.java.net/jdk/pull/5266


More information about the hotspot-compiler-dev mailing list