Skip to content

Commit e62f2f4

Browse files
committed
8357105: C2: compilation fails with "assert(false) failed: empty program detected during loop optimization"
Backport-of: a300c35
1 parent 432eb57 commit e62f2f4

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

src/hotspot/share/opto/stringopts.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -988,12 +988,21 @@ bool StringConcat::validate_control_flow() {
988988
continue;
989989
}
990990

991-
// A test which leads to an uncommon trap which should be safe.
992-
// Later this trap will be converted into a trap that restarts
991+
// A test which leads to an uncommon trap. It is safe to convert the trap
992+
// into a trap that restarts at the beginning as long as its test does not
993+
// depend on intermediate results of the candidate chain.
993994
// at the beginning.
994995
if (otherproj->outcnt() == 1) {
995996
CallStaticJavaNode* call = otherproj->unique_out()->isa_CallStaticJava();
996997
if (call != nullptr && call->_name != nullptr && strcmp(call->_name, "uncommon_trap") == 0) {
998+
// First check for dependency on a toString that is going away during stacked concats.
999+
if (_multiple &&
1000+
((v1->is_Proj() && is_SB_toString(v1->in(0)) && ctrl_path.member(v1->in(0))) ||
1001+
(v2->is_Proj() && is_SB_toString(v2->in(0)) && ctrl_path.member(v2->in(0))))) {
1002+
// iftrue -> if -> bool -> cmpp -> resproj -> tostring
1003+
fail = true;
1004+
break;
1005+
}
9971006
// control flow leads to uct so should be ok
9981007
_uncommon_traps.push(call);
9991008
ctrl_path.push(call);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 8357105
27+
* @summary Test stacked string concatenations where the toString result
28+
* of the first StringBuilder chain is wired into an uncommon trap
29+
* located in the second one.
30+
* @run main/othervm compiler.stringopts.TestStackedConcatsAppendUncommonTrap
31+
* @run main/othervm -XX:-TieredCompilation -Xbatch
32+
* -XX:CompileOnly=compiler.stringopts.TestStackedConcatsAppendUncommonTrap::*
33+
* compiler.stringopts.TestStackedConcatsAppendUncommonTrap
34+
*/
35+
36+
package compiler.stringopts;
37+
38+
public class TestStackedConcatsAppendUncommonTrap {
39+
40+
public static void main (String... args) {
41+
for (int i = 0; i < 10000; i++) {
42+
String s = f(" ");
43+
if (!s.equals(" ")) {
44+
throw new RuntimeException("wrong result.");
45+
}
46+
}
47+
}
48+
49+
static String f(String c) {
50+
String s = " ";
51+
s = new StringBuilder().append(s).append(s).toString();
52+
s = new StringBuilder().append(s).append(s == c ? s : " ").toString();
53+
return s;
54+
}
55+
}

0 commit comments

Comments
 (0)