--- old/src/share/vm/opto/callnode.hpp 2017-02-24 15:14:27.300509784 +0100 +++ new/src/share/vm/opto/callnode.hpp 2017-02-24 15:14:27.243509775 +0100 @@ -449,8 +449,8 @@ void delete_replaced_nodes() { _replaced_nodes.reset(); } - void apply_replaced_nodes() { - _replaced_nodes.apply(this); + void apply_replaced_nodes(uint idx) { + _replaced_nodes.apply(this, idx); } void merge_replaced_nodes_with(SafePointNode* sfpt) { _replaced_nodes.merge_with(sfpt->_replaced_nodes); --- old/src/share/vm/opto/parse1.cpp 2017-02-24 15:14:27.638509835 +0100 +++ new/src/share/vm/opto/parse1.cpp 2017-02-24 15:14:27.579509826 +0100 @@ -1048,7 +1048,7 @@ kit.make_dtrace_method_exit(method()); } if (_replaced_nodes_for_exceptions) { - kit.map()->apply_replaced_nodes(); + kit.map()->apply_replaced_nodes(_new_idx); } // Done with exception-path processing. ex_map = kit.make_exception_state(ex_oop); @@ -1069,7 +1069,7 @@ _exits.add_exception_state(ex_map); } } - _exits.map()->apply_replaced_nodes(); + _exits.map()->apply_replaced_nodes(_new_idx); } //-----------------------------create_entry_map------------------------------- --- old/src/share/vm/opto/replacednodes.cpp 2017-02-24 15:14:27.980509887 +0100 +++ new/src/share/vm/opto/replacednodes.cpp 2017-02-24 15:14:27.921509878 +0100 @@ -91,13 +91,17 @@ } // Perfom node replacement (used when returning to caller) -void ReplacedNodes::apply(Node* n) { +void ReplacedNodes::apply(Node* n, uint idx) { if (is_empty()) { return; } for (int i = 0; i < _replaced_nodes->length(); i++) { ReplacedNode replaced = _replaced_nodes->at(i); - n->replace_edge(replaced.initial(), replaced.improved()); + // Only apply if improved node was created in a callee to avoid + // issues with irreducible loops in the caller + if (replaced.improved()->_idx >= idx) { + n->replace_edge(replaced.initial(), replaced.improved()); + } } } --- old/src/share/vm/opto/replacednodes.hpp 2017-02-24 15:14:28.326509939 +0100 +++ new/src/share/vm/opto/replacednodes.hpp 2017-02-24 15:14:28.268509930 +0100 @@ -71,7 +71,7 @@ void record(Node* initial, Node* improved); void transfer_from(const ReplacedNodes& other, uint idx); void reset(); - void apply(Node* n); + void apply(Node* n, uint idx); void merge_with(const ReplacedNodes& other); bool is_empty() const; void dump(outputStream *st) const; --- /dev/null 2017-02-05 15:04:38.919372554 +0100 +++ new/test/compiler/c2/TestReplacedNodesOSR.java 2017-02-24 15:14:28.600509980 +0100 @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2017, 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 + * @bug 8174164 + * @summary SafePointNode::_replaced_nodes breaks with irreducible loops + * @run main/othervm -XX:-BackgroundCompilation TestReplacedNodesOSR + * + */ + +public class TestReplacedNodesOSR { + + static Object dummy; + + static interface I { + } + + static class A implements I { + } + + static final class MyException extends Exception { + } + + static final A obj = new A(); + static I static_field() { return obj; } + + // When OSR compiled, this method has an irreducible loop + static void test(int v, MyException e) { + int i = 0; + for (;;) { + if (i == 1000) { + break; + } + try { + if ((i%2) == 0) { + int j = 0; + for (;;) { + j++; + if (i+j != v) { + if (j == 1000) { + break; + } + } else { + A a = (A)static_field(); + // replaced node recorded here + throw e; + } + } + } + } catch(MyException ex) { + } + i++; + // replaced node applied on return of the method + // replaced node used here + dummy = static_field(); + } + } + + + static public void main(String[] args) { + for (int i = 0; i < 1000; i++) { + test(1100, new MyException()); + } + } +}