diff --git a/src/hotspot/share/opto/loopopts.cpp b/src/hotspot/share/opto/loopopts.cpp index 9e3d4681e7c..1855263539b 100644 --- a/src/hotspot/share/opto/loopopts.cpp +++ b/src/hotspot/share/opto/loopopts.cpp @@ -1922,11 +1922,6 @@ bool PhaseIdealLoop::ctrl_of_all_uses_out_of_loop(const Node* n, Node* n_ctrl, I if (u->is_Opaque1()) { return false; // Found loop limit, bugfix for 4677003 } - // We can't reuse tags in PhaseIdealLoop::dom_lca_for_get_late_ctrl_internal() so make sure calls to - // get_late_ctrl_with_anti_dep() use their own tag - _dom_lca_tags_round++; - assert(_dom_lca_tags_round != 0, "shouldn't wrap around"); - if (u->is_Phi()) { for (uint j = 1; j < u->req(); ++j) { if (u->in(j) == n && !ctrl_of_use_out_of_loop(n, n_ctrl, n_loop, u->in(0)->in(j))) { @@ -1960,6 +1955,11 @@ bool PhaseIdealLoop::would_sink_below_pre_loop_exit(IdealLoopTree* n_loop, Node* bool PhaseIdealLoop::ctrl_of_use_out_of_loop(const Node* n, Node* n_ctrl, IdealLoopTree* n_loop, Node* ctrl) { if (n->is_Load()) { + // We can't reuse tags in PhaseIdealLoop::dom_lca_for_get_late_ctrl_internal() so make sure each call to + // get_late_ctrl_with_anti_dep() uses its own tag + _dom_lca_tags_round++; + assert(_dom_lca_tags_round != 0, "shouldn't wrap around"); + ctrl = get_late_ctrl_with_anti_dep(n->as_Load(), n_ctrl, ctrl); } IdealLoopTree *u_loop = get_loop(ctrl); diff --git a/test/hotspot/jtreg/compiler/loopopts/TestSinkingLoadInputOfPhi.java b/test/hotspot/jtreg/compiler/loopopts/TestSinkingLoadInputOfPhi.java new file mode 100644 index 00000000000..cd563e6caf2 --- /dev/null +++ b/test/hotspot/jtreg/compiler/loopopts/TestSinkingLoadInputOfPhi.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2026 IBM Corporation. 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 8374725 + * @summary C2: assert(x_ctrl == get_late_ctrl_with_anti_dep(x->as_Load(), early_ctrl, x_ctrl)) failed: anti-dependences were already checked + * @run main/othervm -XX:CompileCommand=compileonly,*TestSinkingLoadInputOfPhi*::* -XX:-TieredCompilation -Xcomp ${test.main.class} + * @run main ${test.main.class} + */ + +package compiler.loopopts; + +public class TestSinkingLoadInputOfPhi { + + static long lFld; + static int iFld = 55; + static int iFld2 = 10; + static void test() { + int iArr[] = new int[iFld2]; + + for (int i13 : iArr) + switch (iFld) { + case 69: + case 46: + case 65: + lFld = i13; + case 55: // taken + for (int i16 = 1; i16 < 30000; i16++) + ; + case 71: + iArr[iFld2] = 2; // OOB-access + } + } + + public static void main(String[] strArr) { + try { + test(); + } catch (ArrayIndexOutOfBoundsException e) { + // expected + } + } +}