roland@7041: /* roland@7041: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. roland@7041: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. roland@7041: * roland@7041: * This code is free software; you can redistribute it and/or modify it roland@7041: * under the terms of the GNU General Public License version 2 only, as roland@7041: * published by the Free Software Foundation. roland@7041: * roland@7041: * This code is distributed in the hope that it will be useful, but WITHOUT roland@7041: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or roland@7041: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License roland@7041: * version 2 for more details (a copy is included in the LICENSE file that roland@7041: * accompanied this code). roland@7041: * roland@7041: * You should have received a copy of the GNU General Public License version roland@7041: * 2 along with this work; if not, write to the Free Software Foundation, roland@7041: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. roland@7041: * roland@7041: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA roland@7041: * or visit www.oracle.com if you need additional information or have any roland@7041: * questions. roland@7041: * roland@7041: */ roland@7041: roland@7041: #include "precompiled.hpp" roland@7041: #include "opto/cfgnode.hpp" roland@7041: #include "opto/phaseX.hpp" roland@7041: #include "opto/replacednodes.hpp" roland@7041: roland@7041: void ReplacedNodes::allocate_if_necessary() { roland@7041: if (_replaced_nodes == NULL) { roland@7041: _replaced_nodes = new GrowableArray(); roland@7041: } roland@7041: } roland@7041: roland@7041: bool ReplacedNodes::is_empty() const { roland@7041: return _replaced_nodes == NULL || _replaced_nodes->length() == 0; roland@7041: } roland@7041: roland@7041: bool ReplacedNodes::has_node(const ReplacedNode& r) const { roland@7041: return _replaced_nodes->find(r) != -1; roland@7041: } roland@7041: roland@7041: bool ReplacedNodes::has_target_node(Node* n) const { roland@7041: for (int i = 0; i < _replaced_nodes->length(); i++) { roland@7041: if (_replaced_nodes->at(i).improved() == n) { roland@7041: return true; roland@7041: } roland@7041: } roland@7041: return false; roland@7041: } roland@7041: roland@7041: // Record replaced node if not seen before roland@7041: void ReplacedNodes::record(Node* initial, Node* improved) { roland@7041: allocate_if_necessary(); roland@7041: ReplacedNode r(initial, improved); roland@7041: if (!has_node(r)) { roland@7041: _replaced_nodes->push(r); roland@7041: } roland@7041: } roland@7041: roland@7041: // Copy replaced nodes from one map to another. idx is used to roland@7041: // identify nodes that are too new to be of interest in the target roland@7041: // node list. roland@7041: void ReplacedNodes::transfer_from(const ReplacedNodes& other, uint idx) { roland@7041: if (other.is_empty()) { roland@7041: return; roland@7041: } roland@7041: allocate_if_necessary(); roland@7041: for (int i = 0; i < other._replaced_nodes->length(); i++) { roland@7041: ReplacedNode replaced = other._replaced_nodes->at(i); roland@7041: // Only transfer the nodes that can actually be useful roland@7041: if (!has_node(replaced) && (replaced.initial()->_idx < idx || has_target_node(replaced.initial()))) { roland@7041: _replaced_nodes->push(replaced); roland@7041: } roland@7041: } roland@7041: } roland@7041: roland@7041: void ReplacedNodes::clone() { roland@7041: if (_replaced_nodes != NULL) { roland@7041: GrowableArray* replaced_nodes_clone = new GrowableArray(); roland@7041: replaced_nodes_clone->appendAll(_replaced_nodes); roland@7041: _replaced_nodes = replaced_nodes_clone; roland@7041: } roland@7041: } roland@7041: roland@7041: void ReplacedNodes::reset() { roland@7041: if (_replaced_nodes != NULL) { roland@7041: _replaced_nodes->clear(); roland@7041: } roland@7041: } roland@7041: roland@7041: // Perfom node replacement (used when returning to caller) roland@7041: void ReplacedNodes::apply(Node* n) { roland@7041: if (is_empty()) { roland@7041: return; roland@7041: } roland@7041: for (int i = 0; i < _replaced_nodes->length(); i++) { roland@7041: ReplacedNode replaced = _replaced_nodes->at(i); roland@7041: n->replace_edge(replaced.initial(), replaced.improved()); roland@7041: } roland@7041: } roland@7041: roland@7041: static void enqueue_use(Node* n, Node* use, Unique_Node_List& work) { roland@7041: if (use->is_Phi()) { roland@7041: Node* r = use->in(0); roland@7041: assert(r->is_Region(), "Phi should have Region"); roland@7041: for (uint i = 1; i < use->req(); i++) { roland@7041: if (use->in(i) == n) { roland@7041: work.push(r->in(i)); roland@7041: } roland@7041: } roland@7041: } else { roland@7041: work.push(use); roland@7041: } roland@7041: } roland@7041: roland@7041: // Perfom node replacement following late inlining roland@7041: void ReplacedNodes::apply(Compile* C, Node* ctl) { roland@7041: // ctl is the control on exit of the method that was late inlined roland@7041: if (is_empty()) { roland@7041: return; roland@7041: } roland@7041: for (int i = 0; i < _replaced_nodes->length(); i++) { roland@7041: ReplacedNode replaced = _replaced_nodes->at(i); roland@7041: Node* initial = replaced.initial(); roland@7041: Node* improved = replaced.improved(); roland@7041: assert (ctl != NULL && !ctl->is_top(), "replaced node should have actual control"); roland@7041: roland@7041: ResourceMark rm; roland@7041: Unique_Node_List work; roland@7041: // Go over all the uses of the node that is considered for replacement... roland@7041: for (DUIterator j = initial->outs(); initial->has_out(j); j++) { roland@7041: Node* use = initial->out(j); roland@7041: roland@7041: if (use == improved || use->outcnt() == 0) { roland@7041: continue; roland@7041: } roland@7041: work.clear(); roland@7041: enqueue_use(initial, use, work); roland@7041: bool replace = true; roland@7041: // Check that this use is dominated by ctl. Go ahead with the roland@7041: // replacement if it is. roland@7041: while (work.size() != 0 && replace) { roland@7041: Node* n = work.pop(); roland@7041: if (use->outcnt() == 0) { roland@7041: continue; roland@7041: } roland@7041: if (n->is_CFG() || (n->in(0) != NULL && !n->in(0)->is_top())) { roland@7041: int depth = 0; roland@7041: Node *m = n; roland@7041: if (!n->is_CFG()) { roland@7041: n = n->in(0); roland@7041: } roland@7041: assert(n->is_CFG(), "should be CFG now"); roland@7041: while(n != ctl) { roland@7041: n = IfNode::up_one_dom(n); roland@7041: depth++; roland@7041: // limit search depth roland@7041: if (depth >= 100 || n == NULL) { roland@7041: replace = false; roland@7041: break; roland@7041: } roland@7041: } roland@7041: } else { roland@7041: for (DUIterator k = n->outs(); n->has_out(k); k++) { roland@7041: enqueue_use(n, n->out(k), work); roland@7041: } roland@7041: } roland@7041: } roland@7041: if (replace) { roland@7041: bool is_in_table = C->initial_gvn()->hash_delete(use); roland@7041: int replaced = use->replace_edge(initial, improved); roland@7041: if (is_in_table) { roland@7041: C->initial_gvn()->hash_find_insert(use); roland@7041: } roland@7041: C->record_for_igvn(use); roland@7041: roland@7041: assert(replaced > 0, "inconsistent"); roland@7041: --j; roland@7041: } roland@7041: } roland@7041: } roland@7041: } roland@7041: roland@7041: void ReplacedNodes::dump(outputStream *st) const { roland@7041: if (!is_empty()) { roland@7041: tty->print("replaced nodes: "); roland@7041: for (int i = 0; i < _replaced_nodes->length(); i++) { roland@7041: tty->print("%d->%d", _replaced_nodes->at(i).initial()->_idx, _replaced_nodes->at(i).improved()->_idx); roland@7041: if (i < _replaced_nodes->length()-1) { roland@7041: tty->print(","); roland@7041: } roland@7041: } roland@7041: } roland@7041: } roland@7041: roland@7041: // Merge 2 list of replaced node at a point where control flow paths merge roland@7041: void ReplacedNodes::merge_with(const ReplacedNodes& other) { roland@7041: if (is_empty()) { roland@7041: return; roland@7041: } roland@7041: if (other.is_empty()) { roland@7041: reset(); roland@7041: return; roland@7041: } roland@7041: int shift = 0; roland@7041: int len = _replaced_nodes->length(); roland@7041: for (int i = 0; i < len; i++) { roland@7041: if (!other.has_node(_replaced_nodes->at(i))) { roland@7041: shift++; roland@7041: } else if (shift > 0) { roland@7041: _replaced_nodes->at_put(i-shift, _replaced_nodes->at(i)); roland@7041: } roland@7041: } roland@7041: if (shift > 0) { roland@7041: _replaced_nodes->trunc_to(len - shift); roland@7041: } roland@7041: }