diff --git a/src/hotspot/share/opto/loopTransform.cpp b/src/hotspot/share/opto/loopTransform.cpp index a4486df9afd..235b03b9a5c 100644 --- a/src/hotspot/share/opto/loopTransform.cpp +++ b/src/hotspot/share/opto/loopTransform.cpp @@ -124,7 +124,10 @@ void IdealLoopTree::compute_trip_count(PhaseIdealLoop* phase) { jlong limit_con = (stride_con > 0) ? limit_type->_hi : limit_type->_lo; int stride_m = stride_con - (stride_con > 0 ? 1 : -1); jlong trip_count = (limit_con - init_con + stride_m)/stride_con; - if (trip_count > 0 && (julong)trip_count < (julong)max_juint) { + // The loop body is always executed at least once even if init >= limit (for stride_con > 0) or + // init <= limit (for stride_con < 0). + trip_count = MAX2(trip_count, (jlong)1); + if (trip_count < (jlong)max_juint) { if (init_n->is_Con() && limit_n->is_Con()) { // Set exact trip count. cl->set_exact_trip_count((uint)trip_count); diff --git a/test/hotspot/jtreg/compiler/loopopts/TestCountedLoopZeroIter.java b/test/hotspot/jtreg/compiler/loopopts/TestCountedLoopZeroIter.java new file mode 100644 index 00000000000..86b46a3d098 --- /dev/null +++ b/test/hotspot/jtreg/compiler/loopopts/TestCountedLoopZeroIter.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2021, Red Hat, Inc. 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 8261308 + * @summary C2: assert(inner->is_valid_counted_loop(T_INT) && inner->is_strip_mined()) failed: OuterStripMinedLoop should have been removed + * + * @run main/othervm -Xcomp -XX:CompileOnly=TestCountedLoopZeroIter TestCountedLoopZeroIter + * + */ + + +public class TestCountedLoopZeroIter { + static int N = 400; + static boolean b; + static long lArrFld[] = new long[N]; + static int iArrFld[] = new int[N]; + + static void test() { + int i19 = 9, i21 = 8; + long l1; + do + if (!b) { + iArrFld[1] /= l1 = 1; + do { + int i22 = 1; + do { + iArrFld[0] = (int)l1; + iArrFld[i22 - 1] = i21; + } while (++i22 < 1); + lArrFld[1] = i21; + } while (++l1 < 145); + } + while (++i19 < 173); + System.out.println(); + } + + public static void main(String[] strArr) { + for (int i = 0; i < 10; i++) { + test(); + } + } +}