diff --git a/src/hotspot/share/opto/loopTransform.cpp b/src/hotspot/share/opto/loopTransform.cpp index b9e14f595c2..31ef298d3af 100644 --- a/src/hotspot/share/opto/loopTransform.cpp +++ b/src/hotspot/share/opto/loopTransform.cpp @@ -3260,7 +3260,6 @@ void IdealLoopTree::adjust_loop_exit_prob(PhaseIdealLoop *phase) { } } -#ifdef ASSERT static CountedLoopNode* locate_pre_from_main(CountedLoopNode* main_loop) { assert(!main_loop->is_main_no_pre_loop(), "Does not have a pre loop"); Node* ctrl = main_loop->skip_assertion_predicates_with_halt(); @@ -3273,7 +3272,6 @@ static CountedLoopNode* locate_pre_from_main(CountedLoopNode* main_loop) { assert(pre_loop->is_pre_loop(), "No pre loop found"); return pre_loop; } -#endif // Remove the main and post loops and make the pre loop execute all // iterations. Useful when the pre loop is found empty. @@ -3301,7 +3299,11 @@ void IdealLoopTree::remove_main_post_loops(CountedLoopNode *cl, PhaseIdealLoop * return; } - assert(locate_pre_from_main(main_head) == cl, "bad main loop"); + // We found a main-loop after this pre-loop, but they might not belong together. + if (locate_pre_from_main(main_head) != cl) { + return; + } + Node* main_iff = main_head->skip_assertion_predicates_with_halt()->in(0); // Remove the Opaque1Node of the pre loop and make it execute all iterations diff --git a/test/hotspot/jtreg/compiler/loopopts/TestEmptyPreLoopForDifferentMainLoop.java b/test/hotspot/jtreg/compiler/loopopts/TestEmptyPreLoopForDifferentMainLoop.java new file mode 100644 index 00000000000..1843742da26 --- /dev/null +++ b/test/hotspot/jtreg/compiler/loopopts/TestEmptyPreLoopForDifferentMainLoop.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2024, 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 8327423 + * @summary Test empty loop removal of pre-loop, with different main-loop after it. + * @run main/othervm -Xcomp + * -XX:CompileCommand=compileonly,compiler.loopopts.TestEmptyPreLoopForDifferentMainLoop::test + * compiler.loopopts.TestEmptyPreLoopForDifferentMainLoop + * @run main compiler.loopopts.TestEmptyPreLoopForDifferentMainLoop + */ + +package compiler.loopopts; + +public class TestEmptyPreLoopForDifferentMainLoop { + static int sink; + + public static void main(String args[]) { + test(false); + } + + static void test(boolean flag) { + int x = 8; + for (int j = 0; j < 100; j++) { + for (int k = 0; k < 100; k++) { + if (flag) { + x += k; + sink = 42; + } + } + if (flag) { + break; + } + } + } +}