Skip to content

Commit f0af830

Browse files
committed
8335747: C2: fix overflow case for LoopLimit with constant inputs
Reviewed-by: kvn, qamai
1 parent fec769b commit f0af830

File tree

2 files changed

+82
-9
lines changed

2 files changed

+82
-9
lines changed

src/hotspot/share/opto/loopnode.cpp

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -2552,11 +2552,15 @@ const Type* LoopLimitNode::Value(PhaseGVN* phase) const {
25522552
jlong trip_count = (limit_con - init_con + stride_m)/stride_con;
25532553
jlong final_con = init_con + stride_con*trip_count;
25542554
int final_int = (int)final_con;
2555-
// The final value should be in integer range since the loop
2556-
// is counted and the limit was checked for overflow.
2557-
// Assert checks for overflow only if all input nodes are ConINodes, as during CCP
2558-
// there might be a temporary overflow from PhiNodes see JDK-8309266
2559-
assert((in(Init)->is_ConI() && in(Limit)->is_ConI() && in(Stride)->is_ConI()) ? final_con == (jlong)final_int : true, "final value should be integer");
2555+
// The final value should be in integer range in almost all cases,
2556+
// since the loop is counted and the limit was checked for overflow.
2557+
// There some exceptions, for example:
2558+
// - During CCP, there might be a temporary overflow from PhiNodes, see JDK-8309266.
2559+
// - During PhaseIdealLoop::split_thru_phi, the LoopLimitNode floats possibly far above
2560+
// the loop and its predicates, and we might get constants on one side of the phi that
2561+
// would lead to overflows. Such a code path would never lead us to enter the loop
2562+
// because of the loop limit overflow check that happens after the LoopLimitNode
2563+
// computation with overflow, but before we enter the loop, see JDK-8335747.
25602564
if (final_con == (jlong)final_int) {
25612565
return TypeInt::make(final_int);
25622566
} else {
@@ -2579,12 +2583,10 @@ Node *LoopLimitNode::Ideal(PhaseGVN *phase, bool can_reshape) {
25792583
if (stride_con == 1)
25802584
return nullptr; // Identity
25812585

2582-
if (in(Init)->is_Con() && in(Limit)->is_Con())
2583-
return nullptr; // Value
2584-
25852586
// Delay following optimizations until all loop optimizations
25862587
// done to keep Ideal graph simple.
25872588
if (!can_reshape || !phase->C->post_loop_opts_phase()) {
2589+
phase->C->record_for_post_loop_opts_igvn(this);
25882590
return nullptr;
25892591
}
25902592

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) 2025, 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 8335747
27+
* @summary Integer overflow in LoopLimit::Value during PhaseIdealLoop::split_thru_phi
28+
* @run main/othervm -Xbatch -XX:CompileCommand=compileonly,TestLoopLimitOverflowDuringSplitThruPhi::test
29+
* compiler.loopopts.TestLoopLimitOverflowDuringSplitThruPhi
30+
* @run driver compiler.loopopts.TestLoopLimitOverflowDuringSplitThruPhi
31+
*/
32+
33+
package compiler.loopopts;
34+
35+
public class TestLoopLimitOverflowDuringSplitThruPhi {
36+
public static void main(String[] args) {
37+
int[] a = new int[1005];
38+
for (int i = 0; i < 20_000; i++) {
39+
test(i % 2 == 0, a);
40+
}
41+
}
42+
43+
static void test(boolean flag, int[] a) {
44+
int x = flag ? 1000 : 2147483647;
45+
// Creates a Phi(1000, 2147483647)
46+
47+
// We do loop-predication, and add a
48+
// LoopLimitNode(init=0, limit=x, stride=4)
49+
//
50+
// Later, we find try to PhaseIdealLoop::split_thru_phi
51+
// the LoopLimitNode, through the Phi(1000, 2147483647).
52+
//
53+
// This creates a temporary
54+
// LoopLimitNode(init=0, limit=2147483647, stride=4)
55+
//
56+
// And then we get this:
57+
// init_con 0
58+
// limit_con 2147483647
59+
// stride_con 4
60+
// trip_count 536870912
61+
// final_int -2147483648
62+
// final_con 2147483648
63+
//
64+
for (int i = 0; i < x; i+=4 /* works for at least 2..64 */) {
65+
// Break before going out of bounds
66+
// but with quadratic check to not affect limit.
67+
if (i * i > 1000_000) { return; }
68+
a[i] = 34;
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)