From 3baff2af6a30cc6cb2e0d4391db1cf7e61c33f64 Mon Sep 17 00:00:00 2001 From: Roland Westrelin Date: Thu, 25 Jul 2024 09:26:11 +0000 Subject: [PATCH] 8335393: C2: assert(!had_error) failed: bad dominance Reviewed-by: thartmann, chagedorn --- src/hotspot/share/opto/loopTransform.cpp | 31 +++++++- .../rangechecks/TestEmptyLoopDeadCast.java | 72 +++++++++++++++++++ 2 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 test/hotspot/jtreg/compiler/rangechecks/TestEmptyLoopDeadCast.java diff --git a/src/hotspot/share/opto/loopTransform.cpp b/src/hotspot/share/opto/loopTransform.cpp index 1b29d31ba68..e961d5a3949 100644 --- a/src/hotspot/share/opto/loopTransform.cpp +++ b/src/hotspot/share/opto/loopTransform.cpp @@ -2871,6 +2871,7 @@ void PhaseIdealLoop::do_range_check(IdealLoopTree *loop, Node_List &old_new) { // Must know if its a count-up or count-down loop int stride_con = cl->stride_con(); + bool abs_stride_is_one = stride_con == 1 || stride_con == -1; Node* zero = _igvn.longcon(0); Node* one = _igvn.longcon(1); // Use symmetrical int range [-max_jint,max_jint] @@ -2882,6 +2883,15 @@ void PhaseIdealLoop::do_range_check(IdealLoopTree *loop, Node_List &old_new) { Node* loop_entry = cl->skip_strip_mined()->in(LoopNode::EntryControl); assert(loop_entry->is_Proj() && loop_entry->in(0)->is_If(), "if projection only"); + // if abs(stride) == 1, an Assertion Predicate for the final iv value is added. We don't know the final iv value until + // we're done with range check elimination so use a place holder. + Node* final_iv_placeholder = nullptr; + if (abs_stride_is_one) { + final_iv_placeholder = new Node(1); + _igvn.set_type(final_iv_placeholder, TypeInt::INT); + final_iv_placeholder->init_req(0, loop_entry); + } + // Check loop body for tests of trip-counter plus loop-invariant vs loop-variant. for (uint i = 0; i < loop->_body.size(); i++) { Node *iff = loop->_body[i]; @@ -2985,6 +2995,20 @@ void PhaseIdealLoop::do_range_check(IdealLoopTree *loop, Node_List &old_new) { Node* opaque_init = new OpaqueLoopInitNode(C, init); register_new_node(opaque_init, loop_entry); + if (abs_stride_is_one) { + // If the main loop becomes empty and the array access for this range check is sunk out of the loop, the index + // for the array access will be set to the index value of the final iteration which could be out of loop. + // Add an Assertion Predicate for that corner case. The final iv is computed from LoopLimit which is the + // LoopNode::limit() only if abs(stride) == 1 otherwise the computation depends on LoopNode::init_trip() as + // well. When LoopLimit only depends on LoopNode::limit(), there are cases where the zero trip guard for the + // main loop doesn't constant fold after range check elimination but, the array access for the final + // iteration of the main loop is out of bound and the index for that access is out of range for the range + // check CastII. + loop_entry = add_range_check_elimination_assertion_predicate(loop, loop_entry, scale_con, int_offset, + int_limit, stride_con, final_iv_placeholder, false); + assert(!assertion_predicate_has_loop_opaque_node(loop_entry->in(0)->as_If()), "unexpected"); + } + // Initialized Assertion Predicate for the value of the initial main-loop. loop_entry = add_range_check_elimination_assertion_predicate(loop, loop_entry, scale_con, int_offset, int_limit, stride_con, init, false); @@ -3116,11 +3140,14 @@ void PhaseIdealLoop::do_range_check(IdealLoopTree *loop, Node_List &old_new) { register_new_node(main_limit, pre_ctrl); // Hack the now-private loop bounds _igvn.replace_input_of(main_cmp, 2, main_limit); + if (abs_stride_is_one) { + Node* final_iv = new SubINode(main_limit, cl->stride()); + register_new_node(final_iv, loop_entry); + _igvn.replace_node(final_iv_placeholder, final_iv); + } // The OpaqueNode is unshared by design assert(opqzm->outcnt() == 1, "cannot hack shared node"); _igvn.replace_input_of(opqzm, 1, main_limit); - - return; } bool IdealLoopTree::compute_has_range_checks() const { diff --git a/test/hotspot/jtreg/compiler/rangechecks/TestEmptyLoopDeadCast.java b/test/hotspot/jtreg/compiler/rangechecks/TestEmptyLoopDeadCast.java new file mode 100644 index 00000000000..105f297c8cb --- /dev/null +++ b/test/hotspot/jtreg/compiler/rangechecks/TestEmptyLoopDeadCast.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2024, Red Hat 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 8335393 + * @summary C2: assert(!had_error) failed: bad dominance + * @requires vm.compiler2.enabled + * @run main/othervm -XX:-TieredCompilation -XX:-UseOnStackReplacement -XX:-BackgroundCompilation -XX:-UseLoopPredicate + * -XX:LoopMaxUnroll=0 TestEmptyLoopDeadCast + */ + +public class TestEmptyLoopDeadCast { + public static void main(String[] args) { + int[] array = new int[100]; + for (int i = 0; i < 20_000; i++) { + test1Helper(1, 101, array); + test1(0, array); + test2Helper(0, -101, array); + test2(0, array); + } + } + + private static int test1(int start, int[] array) { + return test1Helper(start, 0, array); + } + + private static int test1Helper(int start, int stop, int[] array) { + if (array == null) { + } + int v = 0; + for (int i = start; i < stop; i++) { + v = array[i - 1]; + } + return v; + } + + private static int test2(int start, int[] array) { + return test2Helper(start, -1, array); + } + + private static int test2Helper(int start, int stop, int[] array) { + if (array == null) { + } + int v = 0; + for (int i = start-1; i > stop; i--) { + v = array[-1 - i]; + } + return v; + } + +}