Skip to content

Commit b0d6c84

Browse files
committed
8316396: Endless loop in C2 compilation triggered by AddNode::IdealIL
Reviewed-by: thartmann, kvn
1 parent a8549b6 commit b0d6c84

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

src/hotspot/share/opto/addnode.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,26 @@ Node* AddNode::IdealIL(PhaseGVN* phase, bool can_reshape, BasicType bt) {
280280
assert( in1->in(2) != this && in2->in(2) != this,
281281
"dead loop in AddINode::Ideal" );
282282
Node* sub = SubNode::make(nullptr, nullptr, bt);
283-
sub->init_req(1, phase->transform(AddNode::make(in1->in(1), in2->in(1), bt)));
284-
sub->init_req(2, phase->transform(AddNode::make(in1->in(2), in2->in(2), bt)));
283+
Node* sub_in1;
284+
PhaseIterGVN* igvn = phase->is_IterGVN();
285+
// During IGVN, if both inputs of the new AddNode are a tree of SubNodes, this same transformation will be applied
286+
// to every node of the tree. Calling transform() causes the transformation to be applied recursively, once per
287+
// tree node whether some subtrees are identical or not. Pushing to the IGVN worklist instead, causes the transform
288+
// to be applied once per unique subtrees (because all uses of a subtree are updated with the result of the
289+
// transformation). In case of a large tree, this can make a difference in compilation time.
290+
if (igvn != nullptr) {
291+
sub_in1 = igvn->register_new_node_with_optimizer(AddNode::make(in1->in(1), in2->in(1), bt));
292+
} else {
293+
sub_in1 = phase->transform(AddNode::make(in1->in(1), in2->in(1), bt));
294+
}
295+
Node* sub_in2;
296+
if (igvn != nullptr) {
297+
sub_in2 = igvn->register_new_node_with_optimizer(AddNode::make(in1->in(2), in2->in(2), bt));
298+
} else {
299+
sub_in2 = phase->transform(AddNode::make(in1->in(2), in2->in(2), bt));
300+
}
301+
sub->init_req(1, sub_in1);
302+
sub->init_req(2, sub_in2);
285303
return sub;
286304
}
287305
// Convert "(a-b)+(b+c)" into "(a+c)"
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2023, Red Hat, Inc. 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 8316396
27+
* @summary Endless loop in C2 compilation triggered by AddNode::IdealIL
28+
* @run main/othervm -XX:CompileCommand=compileonly,*TestLargeTreeOfSubNodes*::test -XX:-TieredCompilation -Xcomp TestLargeTreeOfSubNodes
29+
*/
30+
31+
public class TestLargeTreeOfSubNodes {
32+
public static long res = 0;
33+
34+
public static void test() {
35+
int a = -1, b = 0;
36+
for (int i = 0; i < 100; ++i) {
37+
for (int j = 0; j < 10; ++j) {
38+
for (int k = 0; k < 1; ++k) {
39+
}
40+
b -= a;
41+
a += b;
42+
}
43+
}
44+
res = a;
45+
}
46+
47+
public static void main(String[] args) {
48+
test();
49+
}
50+
}

0 commit comments

Comments
 (0)