RFR: 8270366: C2: Add associative rule to add/sub node [v3]

Vladimir Kozlov kvn at openjdk.java.net
Wed Jul 14 17:52:12 UTC 2021


On Wed, 14 Jul 2021 17:30:42 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 incrementally with one additional commit since the last revision:
> 
>   Add a test

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);
     }

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

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


More information about the hotspot-compiler-dev mailing list