Skip to content

Commit a6ef655

Browse files
committedJul 14, 2023
8309119: [17u/11u] Redo JDK-8297951: C2: Create skeleton predicates for all If nodes in loop predication
Reviewed-by: mdoerr
1 parent 3cc7858 commit a6ef655

File tree

2 files changed

+83
-7
lines changed

2 files changed

+83
-7
lines changed
 

‎src/hotspot/share/opto/loopPredicate.cpp

+5-7
Original file line numberDiff line numberDiff line change
@@ -1381,13 +1381,11 @@ bool PhaseIdealLoop::loop_predication_impl_helper(IdealLoopTree *loop, ProjNode*
13811381
upper_bound_iff->set_req(1, upper_bound_bol);
13821382
if (TraceLoopPredicate) tty->print_cr("upper bound check if: %d ", lower_bound_iff->_idx);
13831383

1384-
// Fall through into rest of the clean up code which will move
1385-
// any dependent nodes onto the upper bound test.
1386-
new_predicate_proj = upper_bound_proj;
1387-
1388-
if (iff->is_RangeCheck()) {
1389-
new_predicate_proj = insert_initial_skeleton_predicate(iff, loop, if_success_proj, predicate_proj, upper_bound_proj, scale, offset, init, limit, stride, rng, overflow, reason);
1390-
}
1384+
// Fall through into rest of the cleanup code which will move any dependent nodes to the skeleton predicates of the
1385+
// upper bound test. We always need to create skeleton predicates in order to properly remove dead loops when later
1386+
// splitting the predicated loop into (unreachable) sub-loops (i.e. done by unrolling, peeling, pre/main/post etc.).
1387+
new_predicate_proj = insert_initial_skeleton_predicate(iff, loop, if_success_proj, predicate_proj, upper_bound_proj, scale,
1388+
offset, init, limit, stride, rng, overflow, reason);
13911389

13921390
#ifndef PRODUCT
13931391
if (TraceLoopOpts && !TraceLoopPredicate) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8297951
27+
* @summary Test that crashes because we do not emit skeleton predicates for normal If nodes for which a range check
28+
* predicate is created in loop predication.
29+
* @requires vm.debug == true & vm.compiler2.enabled
30+
* @run main/othervm -XX:-TieredCompilation -Xbatch -XX:-RangeCheckElimination -XX:+BailoutToInterpreterForThrows
31+
compiler.loopopts.TestMissingSkeletonPredicateForIfNode
32+
*/
33+
package compiler.loopopts;
34+
35+
public class TestMissingSkeletonPredicateForIfNode {
36+
static int iFld = 2, x;
37+
static short limit = 10;
38+
39+
public static void main(String[] args) throws Exception {
40+
for (int i = 0; i < 5000; i++) {
41+
try {
42+
test(i % 2 == 0, i % 3);
43+
} catch (Exception e) {
44+
// Expected
45+
}
46+
}
47+
}
48+
49+
public static void test(boolean flag, int arg) throws Exception {
50+
int sum = 1;
51+
int[] iArr2 = new int[4];
52+
RuntimeException r = new RuntimeException();
53+
54+
for (int i = 0; i < limit; i+=2) { // Pre/main/post + Unrolled once. This results in the following type for the iv phi i: [2..SHORT_MAX]
55+
x = 5 / sum;
56+
if (Integer.compareUnsigned(i, iArr2.length) < 0) { // (**) Loop predication creates a RC predicate for this check
57+
// After unrolling, we have:
58+
//
59+
// iArr2[i]
60+
// iArr2[i+2]
61+
//
62+
// The type of iArr2[i+2] is [4..SHORT_MAX+2] (we need limit to be short such that we do not have an integer overflow
63+
// which would set the type to int). However, the type of the CastII node for the index i+2 is [0..3] because its size
64+
// is only 4. Since the type of i+2 is outside the range of the CastII node, the CastII node is replaced by top and
65+
// some of the data nodes and memory nodes die. We are left with a broken graph and later assert because of that.
66+
iFld += iArr2[i]; // RangeCheck node is removed because it shares the same bool as the If (**).
67+
sum += iFld;
68+
} else {
69+
// Emits an UCT with -XX:+BailoutToInterpreterForThrows and therefore the If (**) satisfies the condition of being a
70+
// range check if with one of its blocks being an UCT.
71+
throw r;
72+
}
73+
if (i > 50) {
74+
break;
75+
}
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)