--- old/src/hotspot/share/opto/callnode.cpp 2019-01-08 14:06:19.464610483 +0100 +++ new/src/hotspot/share/opto/callnode.cpp 2019-01-08 14:06:13.017664301 +0100 @@ -1271,6 +1271,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, --- old/src/hotspot/share/opto/callnode.hpp 2019-01-08 14:06:26.641550569 +0100 +++ new/src/hotspot/share/opto/callnode.hpp 2019-01-08 14:06:19.553609740 +0100 @@ -462,6 +462,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; } --- old/src/hotspot/share/opto/compile.cpp 2019-01-08 14:06:33.392494212 +0100 +++ new/src/hotspot/share/opto/compile.cpp 2019-01-08 14:06:26.729549835 +0100 @@ -2184,6 +2184,23 @@ return true; } +// 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() { @@ -2244,6 +2261,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 @@ -3248,8 +3269,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; --- old/src/hotspot/share/opto/compile.hpp 2019-01-08 14:06:40.556434409 +0100 +++ new/src/hotspot/share/opto/compile.hpp 2019-01-08 14:06:33.507493252 +0100 @@ -1088,6 +1088,7 @@ void inline_string_calls(bool parse_time); void inline_boxing_calls(PhaseIterGVN& igvn); bool optimize_loops(PhaseIterGVN& igvn, LoopOptsMode mode); + void remove_root_to_sfpts_edges(); // Matching, CFG layout, allocation, code generation PhaseCFG* cfg() { return _cfg; } --- old/src/hotspot/share/opto/node.cpp 2019-01-08 14:06:47.521376268 +0100 +++ new/src/hotspot/share/opto/node.cpp 2019-01-08 14:06:40.642433691 +0100 @@ -37,6 +37,7 @@ #include "opto/node.hpp" #include "opto/opcodes.hpp" #include "opto/regmask.hpp" +#include "opto/rootnode.hpp" #include "opto/type.hpp" #include "utilities/copy.hpp" #include "utilities/macros.hpp" @@ -1310,6 +1311,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); --- old/src/hotspot/share/opto/phaseX.cpp 2019-01-08 14:06:54.675316549 +0100 +++ new/src/hotspot/share/opto/phaseX.cpp 2019-01-08 14:06:47.613375500 +0100 @@ -430,20 +430,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; - } - } - } } //============================================================================= @@ -1354,6 +1340,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"); @@ -1456,6 +1445,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: --- /dev/null 2018-12-19 13:15:18.454299236 +0100 +++ new/test/hotspot/jtreg/compiler/inlining/StringConcatInfiniteLoop.java 2019-01-08 14:06:54.763315814 +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 { + } +}