# HG changeset patch # User andrew # Date 1595594231 -3600 # Node ID 147bfde2dfd4aa6cb7a464ce356e599999ced0f7 # Parent d961c6fee21607969b079c7e6f60a6b1bb1dd83b# Parent d2ec2776ad0cfc481bd3a6a5c3adb2702db8ae8d Merge diff -r d961c6fee216 -r 147bfde2dfd4 src/share/vm/opto/callnode.cpp --- a/src/share/vm/opto/callnode.cpp Fri Jul 24 13:16:58 2020 +0100 +++ b/src/share/vm/opto/callnode.cpp Fri Jul 24 13:37:11 2020 +0100 @@ -1180,6 +1180,14 @@ return (TypeFunc::Parms == idx); } +void SafePointNode::disconnect_from_root(PhaseIterGVN *igvn) { + assert(Opcode() == Op_SafePoint, "only value for safepoint in loops"); + int nb = igvn->C->root()->find_prec_edge(this); + if (nb != -1) { + igvn->C->root()->rm_prec(nb); + } +} + //============== SafePointScalarObjectNode ============== SafePointScalarObjectNode::SafePointScalarObjectNode(const TypeOopPtr* tp, diff -r d961c6fee216 -r 147bfde2dfd4 src/share/vm/opto/callnode.hpp --- a/src/share/vm/opto/callnode.hpp Fri Jul 24 13:16:58 2020 +0100 +++ b/src/share/vm/opto/callnode.hpp Fri Jul 24 13:37:11 2020 +0100 @@ -459,6 +459,8 @@ return !_replaced_nodes.is_empty(); } + void disconnect_from_root(PhaseIterGVN *igvn); + // Standard Node stuff virtual int Opcode() const; virtual bool pinned() const { return true; } diff -r d961c6fee216 -r 147bfde2dfd4 src/share/vm/opto/compile.cpp --- a/src/share/vm/opto/compile.cpp Fri Jul 24 13:16:58 2020 +0100 +++ b/src/share/vm/opto/compile.cpp Fri Jul 24 13:37:11 2020 +0100 @@ -2075,6 +2075,23 @@ } +// Remove edges from "root" to each SafePoint at a backward branch. +// They were inserted during parsing (see add_safepoint()) to make +// infinite loops without calls or exceptions visible to root, i.e., +// useful. +void Compile::remove_root_to_sfpts_edges() { + Node *r = root(); + if (r != NULL) { + for (uint i = r->req(); i < r->len(); ++i) { + Node *n = r->in(i); + if (n != NULL && n->is_SafePoint()) { + r->rm_prec(i); + --i; + } + } + } +} + //------------------------------Optimize--------------------------------------- // Given a graph, optimize it. void Compile::Optimize() { @@ -2130,6 +2147,10 @@ if (failing()) return; } + // Now that all inlining is over, cut edge from root to loop + // safepoints + remove_root_to_sfpts_edges(); + // Remove the speculative part of types and clean up the graph from // the extra CastPP nodes whose only purpose is to carry them. Do // that early so that optimizations are not disrupted by the extra @@ -3012,8 +3033,10 @@ break; } } - assert(proj != NULL, "must be found"); - p->subsume_by(proj, this); + assert(proj != NULL || p->_con == TypeFunc::I_O, "io may be dropped at an infinite loop"); + if (proj != NULL) { + p->subsume_by(proj, this); + } } } break; diff -r d961c6fee216 -r 147bfde2dfd4 src/share/vm/opto/compile.hpp --- a/src/share/vm/opto/compile.hpp Fri Jul 24 13:16:58 2020 +0100 +++ b/src/share/vm/opto/compile.hpp Fri Jul 24 13:37:11 2020 +0100 @@ -959,6 +959,7 @@ void inline_incrementally(PhaseIterGVN& igvn); void inline_string_calls(bool parse_time); void inline_boxing_calls(PhaseIterGVN& igvn); + void remove_root_to_sfpts_edges(); // Matching, CFG layout, allocation, code generation PhaseCFG* cfg() { return _cfg; } diff -r d961c6fee216 -r 147bfde2dfd4 src/share/vm/opto/node.cpp --- a/src/share/vm/opto/node.cpp Fri Jul 24 13:16:58 2020 +0100 +++ b/src/share/vm/opto/node.cpp Fri Jul 24 13:37:11 2020 +0100 @@ -1329,6 +1329,9 @@ while (nstack.size() > 0) { dead = nstack.pop(); + if (dead->Opcode() == Op_SafePoint) { + dead->as_SafePoint()->disconnect_from_root(igvn); + } if (dead->outcnt() > 0) { // Keep dead node on stack until all uses are processed. nstack.push(dead); diff -r d961c6fee216 -r 147bfde2dfd4 src/share/vm/opto/phaseX.cpp --- a/src/share/vm/opto/phaseX.cpp Fri Jul 24 13:16:58 2020 +0100 +++ b/src/share/vm/opto/phaseX.cpp Fri Jul 24 13:37:11 2020 +0100 @@ -419,20 +419,6 @@ // Disconnect 'useless' nodes that are adjacent to useful nodes C->remove_useless_nodes(_useful); - - // Remove edges from "root" to each SafePoint at a backward branch. - // They were inserted during parsing (see add_safepoint()) to make infinite - // loops without calls or exceptions visible to root, i.e., useful. - Node *root = C->root(); - if( root != NULL ) { - for( uint i = root->req(); i < root->len(); ++i ) { - Node *n = root->in(i); - if( n != NULL && n->is_SafePoint() ) { - root->rm_prec(i); - --i; - } - } - } } //============================================================================= @@ -1258,6 +1244,9 @@ while (_stack.is_nonempty()) { dead = _stack.node(); + if (dead->Opcode() == Op_SafePoint) { + dead->as_SafePoint()->disconnect_from_root(this); + } uint progress_state = _stack.index(); assert(dead != C->root(), "killing root, eh?"); assert(!dead->is_top(), "add check for top when pushing"); @@ -1352,6 +1341,9 @@ //------------------------------subsume_node----------------------------------- // Remove users from node 'old' and add them to node 'nn'. void PhaseIterGVN::subsume_node( Node *old, Node *nn ) { + if (old->Opcode() == Op_SafePoint) { + old->as_SafePoint()->disconnect_from_root(this); + } assert( old != hash_find(old), "should already been removed" ); assert( old != C->top(), "cannot subsume top node"); // Copy debug or profile information to the new version: diff -r d961c6fee216 -r 147bfde2dfd4 test/compiler/inlining/StringConcatInfiniteLoop.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/compiler/inlining/StringConcatInfiniteLoop.java Fri Jul 24 13:37:11 2020 +0100 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2018, 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 8214862 + * @summary Multiple passes of PhaseRemoveUseless causes infinite loop to be optimized out + * + * @run main/othervm -XX:-TieredCompilation -Xcomp -XX:CompileOnly=StringConcatInfiniteLoop::test -XX:CompileCommand=dontinline,*StringBuilder::* StringConcatInfiniteLoop + * + */ + +public class StringConcatInfiniteLoop { + public static void main(String[] args) { + StringBuilder sb = new StringBuilder(); + test(sb, "foo", "bar", true); + } + + private static void test(Object v, String s1, String s2, boolean flag) { + if (flag) { + return; + } + int i = 0; + for (; i < 10; i++); + if (i == 10) { + v = null; + } + StringBuilder sb = new StringBuilder(s1); + sb.append(s2); + while (v == null); + } + + private static class A { + } +}