RFR: 8323220: Reassociate loop invariants involved in Cmps and Add/Subs

Emanuel Peter epeter at openjdk.org
Tue Jan 16 17:44:27 UTC 2024


On Thu, 11 Jan 2024 17:41:53 GMT, Joshua Cao <duke at openjdk.org> wrote:

> // inv1 == (x + inv2) => ( inv1 - inv2 ) == x
> // inv1 == (x - inv2) => ( inv1 + inv2 ) == x
> // inv1 == (inv2 - x) => (-inv1 + inv2 ) == x
> 
> 
> For example,
> 
> 
> fn(inv1, inv2)
>   while(...)
>       x = foobar()
>       if inv1 == x + inv2
>            blackhole()
> 
> 
> We can transform this into
> 
> 
> fn(inv1, inv2)
>   t = inv1 - inv2
>   while(...)
>       x = foobar()
>       if t == x
>            blackhole()
> 
> 
> Here is an example: https://github.com/openjdk/jdk/blob/b78896b9aafcb15f453eaed6e154a5461581407b/src/java.base/share/classes/java/lang/invoke/LambdaFormEditor.java#L910. LHS `1` and RHS `pos` are both loop invariant
> 
> Passes tier1 locally on Linux machine. Passes GHA on my fork.

Nice idea, thanks for the work!

Can you also create a regression test that has edge-case values and random values, to check that the correctness ok? This would also experimentally rule out overflow issues.

src/hotspot/share/opto/loopTransform.cpp line 276:

> 274:   }
> 275:   for (DUIterator i = n->outs(); n->has_out(i); i++) {
> 276:     BoolNode *boolOut = n->out(i)->isa_Bool();

Suggestion:

    BoolNode* boolOut = n->out(i)->isa_Bool();

src/hotspot/share/opto/loopTransform.cpp line 277:

> 275:   for (DUIterator i = n->outs(); n->has_out(i); i++) {
> 276:     BoolNode *boolOut = n->out(i)->isa_Bool();
> 277:     if (!boolOut || !(boolOut->_test._test == BoolTest::eq ||

Suggestion:

    if (boolOut != nullptr || !(boolOut->_test._test == BoolTest::eq ||

src/hotspot/share/opto/loopTransform.cpp line 333:

> 331: // inv1 == (inv2 - x) => (-inv1 + inv2 ) == x
> 332: //
> 333: Node* IdealLoopTree::reassociate_add_sub_cmp(Node* n1, int inv1_idx, int inv2_idx, PhaseIdealLoop *phase) {

Suggestion:

Node* IdealLoopTree::reassociate_add_sub_cmp(Node* n1, int inv1_idx, int inv2_idx, PhaseIdealLoop* phase) {

src/hotspot/share/opto/loopTransform.cpp line 345:

> 343:   bool neg_inv1 = (n1->is_Sub() && !n1->is_Cmp() && inv1_idx == 2) ||
> 344:                   (n1->is_Cmp() && inv2_idx == 1 && n2->is_Sub());
> 345:   if (n1->is_Sub() && !n1->is_Cmp() && inv1_idx == 1) {

Would you mind adding some comments for this logic?

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

Changes requested by epeter (Reviewer).

PR Review: https://git.openjdk.org/jdk/pull/17375#pullrequestreview-1824310859
PR Review Comment: https://git.openjdk.org/jdk/pull/17375#discussion_r1453743203
PR Review Comment: https://git.openjdk.org/jdk/pull/17375#discussion_r1453744396
PR Review Comment: https://git.openjdk.org/jdk/pull/17375#discussion_r1453751200
PR Review Comment: https://git.openjdk.org/jdk/pull/17375#discussion_r1453761329


More information about the hotspot-compiler-dev mailing list