Skip to content

8359603: Missed optimization in PhaseIterGVN for redundant ConvX2Y->ConvY2X->ConvX2Y sequences due to missing notification in PhaseIterGVN::add_users_of_use_to_worklist #26368

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

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/hotspot/share/opto/phaseX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2558,6 +2558,24 @@ void PhaseIterGVN::add_users_of_use_to_worklist(Node* n, Node* use, Unique_Node_
}
}
}
// Check for redundant conversion patterns:
// ConvD2L->ConvL2D->ConvD2L
// ConvF2I->ConvI2F->ConvF2I
// ConvF2L->ConvL2F->ConvF2L
// ConvI2F->ConvF2I->ConvI2F
Comment on lines +2562 to +2565
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another thought: Since this is an incomplete list of variations (especially missing, for example, the I2D version while the I2F version is here), should we leave a comment about not being able to trigger issues with the other versions? Otherwise, it could suggest that it was just forgotten.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The notification issue that is solved here only happens with optimizations that have this chain pattern with three nodes (checking the input of the input) and this is specific to conversions that have a loss of precision. ConvI2D is not here because the ConvI2D->ConvD2I gets optimized to a NOP already (ConvD2INode::Identity). But there are also chains for which there is a known optimization and for which I was not able to trigger a missed optimization, so it would make sense to have mention this in any case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added a short note, let me know what you think!

// Note: there may be other 3-nodes conversion chains that would require to be added here, but these
// are the only ones that are known to trigger missed optimizations otherwise
if ((n->Opcode() == Op_ConvD2L && use_op == Op_ConvL2D) ||
(n->Opcode() == Op_ConvF2I && use_op == Op_ConvI2F) ||
(n->Opcode() == Op_ConvF2L && use_op == Op_ConvL2F) ||
(n->Opcode() == Op_ConvI2F && use_op == Op_ConvF2I)) {
for (DUIterator_Fast i2max, i2 = use->fast_outs(i2max); i2 < i2max; i2++) {
Node* u = use->fast_out(i2);
if (u->Opcode() == n->Opcode()) {
worklist.push(u);
}
}
}
// If changed AddP inputs:
// - check Stores for loop invariant, and
// - if the changed input is the offset, check constant-offset AddP users for
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8359603
* @summary Redundant ConvX2Y->ConvY2X->ConvX2Y sequences should be
* simplified to a single ConvX2Y operation when applicable
* VerifyIterativeGVN checks that this optimization was applied
* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions
* -XX:CompileCommand=compileonly,compiler.c2.TestEliminateRedundantConversionSequences::test*
* -XX:-TieredCompilation -Xbatch -XX:VerifyIterativeGVN=1110 compiler.c2.TestEliminateRedundantConversionSequences
* @run main compiler.c2.TestEliminateRedundantConversionSequences
*
*/

package compiler.c2;

public class TestEliminateRedundantConversionSequences {
static long instanceCountD2L;
static int instanceCoundF2I;
static long instanceCountF2L;
static float instanceCountI2F;

// ConvD2L->ConvL2D->ConvD2L
static void testD2L(double d) {
int i = 1;
int j = 1;
while (++i < 3) {
for (; 8 > j; ++j) {
instanceCountD2L = (long)d;
d = instanceCountD2L;
}
}
}

// ConvF2I->ConvI2F->ConvF2I
static void testF2I(float d) {
int i = 1;
int j = 1;
while (++i < 3) {
for (; 8 > j; ++j) {
instanceCoundF2I = (int)d;
d = instanceCoundF2I;
}
}
}

// ConvF2L->ConvL2F->ConvF2L
static void testF2L(float d) {
int i = 1;
int j = 1;
while (++i < 3) {
for (; 8 > j; ++j) {
instanceCountF2L = (long)d;
d = instanceCountF2L;
}
}
}

// ConvI2F->ConvF2I->ConvI2F
static void testI2F(int d) {
int i = 1;
int j = 1;
while (++i < 3) {
for (; 8 > j; ++j) {
instanceCountI2F = d;
d = (int)instanceCountI2F;
}
}
}

public static void main(String[] strArr) {
for (int i = 0; i < 1550; ++i) {
testD2L(1);
}
for (int i = 0; i < 1550; ++i) {
testF2I(1);
}
for (int i = 0; i < 1550; ++i) {
testF2L(1);
}
for (int i = 0; i < 1550; ++i) {
testI2F(1);
}
}
}