kvn@2727: /* drchase@6680: * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. kvn@2727: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. kvn@2727: * kvn@2727: * This code is free software; you can redistribute it and/or modify it kvn@2727: * under the terms of the GNU General Public License version 2 only, as kvn@2727: * published by the Free Software Foundation. kvn@2727: * kvn@2727: * This code is distributed in the hope that it will be useful, but WITHOUT kvn@2727: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or kvn@2727: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License kvn@2727: * version 2 for more details (a copy is included in the LICENSE file that kvn@2727: * accompanied this code). kvn@2727: * kvn@2727: * You should have received a copy of the GNU General Public License version kvn@2727: * 2 along with this work; if not, write to the Free Software Foundation, kvn@2727: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. kvn@2727: * kvn@2727: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA kvn@2727: * or visit www.oracle.com if you need additional information or have any kvn@2727: * questions. kvn@2727: * kvn@2727: */ kvn@2727: kvn@2727: #include "precompiled.hpp" kvn@2727: #include "opto/loopnode.hpp" kvn@2727: #include "opto/addnode.hpp" kvn@2727: #include "opto/callnode.hpp" kvn@2727: #include "opto/connode.hpp" kvn@2727: #include "opto/loopnode.hpp" kvn@2727: #include "opto/mulnode.hpp" kvn@2727: #include "opto/rootnode.hpp" kvn@2727: #include "opto/subnode.hpp" kvn@2727: kvn@2727: /* kvn@2727: * The general idea of Loop Predication is to insert a predicate on the entry kvn@2727: * path to a loop, and raise a uncommon trap if the check of the condition fails. kvn@2727: * The condition checks are promoted from inside the loop body, and thus kvn@2727: * the checks inside the loop could be eliminated. Currently, loop predication kvn@2727: * optimization has been applied to remove array range check and loop invariant kvn@2727: * checks (such as null checks). kvn@2727: */ kvn@2727: kvn@2727: //-------------------------------register_control------------------------- kvn@2727: void PhaseIdealLoop::register_control(Node* n, IdealLoopTree *loop, Node* pred) { kvn@2727: assert(n->is_CFG(), "must be control node"); kvn@2727: _igvn.register_new_node_with_optimizer(n); kvn@2727: loop->_body.push(n); kvn@2727: set_loop(n, loop); kvn@2727: // When called from beautify_loops() idom is not constructed yet. kvn@2727: if (_idom != NULL) { kvn@2727: set_idom(n, pred, dom_depth(pred)); kvn@2727: } kvn@2727: } kvn@2727: kvn@2727: //------------------------------create_new_if_for_predicate------------------------ kvn@2727: // create a new if above the uct_if_pattern for the predicate to be promoted. kvn@2727: // kvn@2727: // before after kvn@2727: // ---------- ---------- kvn@2727: // ctrl ctrl kvn@2727: // | | kvn@2727: // | | kvn@2727: // v v kvn@2727: // iff new_iff kvn@2727: // / \ / \ kvn@2727: // / \ / \ kvn@2727: // v v v v kvn@2727: // uncommon_proj cont_proj if_uct if_cont kvn@2727: // \ | | | | kvn@2727: // \ | | | | kvn@2727: // v v v | v kvn@2727: // rgn loop | iff kvn@2727: // | | / \ kvn@2727: // | | / \ kvn@2727: // v | v v kvn@2727: // uncommon_trap | uncommon_proj cont_proj kvn@2727: // \ \ | | kvn@2727: // \ \ | | kvn@2727: // v v v v kvn@2727: // rgn loop kvn@2727: // | kvn@2727: // | kvn@2727: // v kvn@2727: // uncommon_trap kvn@2727: // kvn@2727: // kvn@2727: // We will create a region to guard the uct call if there is no one there. kvn@2727: // The true projecttion (if_cont) of the new_iff is returned. kvn@2727: // This code is also used to clone predicates to clonned loops. kvn@2727: ProjNode* PhaseIdealLoop::create_new_if_for_predicate(ProjNode* cont_proj, Node* new_entry, kvn@2727: Deoptimization::DeoptReason reason) { roland@5981: assert(cont_proj->is_uncommon_trap_if_pattern(reason), "must be a uct if pattern!"); kvn@2727: IfNode* iff = cont_proj->in(0)->as_If(); kvn@2727: kvn@2727: ProjNode *uncommon_proj = iff->proj_out(1 - cont_proj->_con); kvn@2727: Node *rgn = uncommon_proj->unique_ctrl_out(); kvn@2727: assert(rgn->is_Region() || rgn->is_Call(), "must be a region or call uct"); kvn@2727: kvn@2727: uint proj_index = 1; // region's edge corresponding to uncommon_proj kvn@2727: if (!rgn->is_Region()) { // create a region to guard the call kvn@2727: assert(rgn->is_Call(), "must be call uct"); kvn@2727: CallNode* call = rgn->as_Call(); kvn@2727: IdealLoopTree* loop = get_loop(call); kvn@4115: rgn = new (C) RegionNode(1); kvn@2727: rgn->add_req(uncommon_proj); kvn@2727: register_control(rgn, loop, uncommon_proj); kvn@2727: _igvn.hash_delete(call); kvn@2727: call->set_req(0, rgn); kvn@2727: // When called from beautify_loops() idom is not constructed yet. kvn@2727: if (_idom != NULL) { kvn@2727: set_idom(call, rgn, dom_depth(rgn)); kvn@2727: } kvn@2727: } else { kvn@2727: // Find region's edge corresponding to uncommon_proj kvn@2727: for (; proj_index < rgn->req(); proj_index++) kvn@2727: if (rgn->in(proj_index) == uncommon_proj) break; kvn@2727: assert(proj_index < rgn->req(), "sanity"); kvn@2727: } kvn@2727: kvn@2727: Node* entry = iff->in(0); kvn@2727: if (new_entry != NULL) { kvn@2727: // Clonning the predicate to new location. kvn@2727: entry = new_entry; kvn@2727: } kvn@2727: // Create new_iff kvn@2727: IdealLoopTree* lp = get_loop(entry); kvn@2727: IfNode *new_iff = iff->clone()->as_If(); kvn@2727: new_iff->set_req(0, entry); kvn@2727: register_control(new_iff, lp, entry); kvn@4115: Node *if_cont = new (C) IfTrueNode(new_iff); kvn@4115: Node *if_uct = new (C) IfFalseNode(new_iff); kvn@2727: if (cont_proj->is_IfFalse()) { kvn@2727: // Swap kvn@2727: Node* tmp = if_uct; if_uct = if_cont; if_cont = tmp; kvn@2727: } kvn@2727: register_control(if_cont, lp, new_iff); kvn@2727: register_control(if_uct, get_loop(rgn), new_iff); kvn@2727: kvn@2727: // if_uct to rgn kvn@2727: _igvn.hash_delete(rgn); kvn@2727: rgn->add_req(if_uct); kvn@2727: // When called from beautify_loops() idom is not constructed yet. kvn@2727: if (_idom != NULL) { kvn@2727: Node* ridom = idom(rgn); kvn@2727: Node* nrdom = dom_lca(ridom, new_iff); kvn@2727: set_idom(rgn, nrdom, dom_depth(rgn)); kvn@2727: } kvn@2727: kvn@2727: // If rgn has phis add new edges which has the same kvn@2727: // value as on original uncommon_proj pass. kvn@2727: assert(rgn->in(rgn->req() -1) == if_uct, "new edge should be last"); kvn@2727: bool has_phi = false; kvn@2727: for (DUIterator_Fast imax, i = rgn->fast_outs(imax); i < imax; i++) { kvn@2727: Node* use = rgn->fast_out(i); kvn@2727: if (use->is_Phi() && use->outcnt() > 0) { kvn@2727: assert(use->in(0) == rgn, ""); kvn@3847: _igvn.rehash_node_delayed(use); kvn@2727: use->add_req(use->in(proj_index)); kvn@2727: has_phi = true; kvn@2727: } kvn@2727: } kvn@2727: assert(!has_phi || rgn->req() > 3, "no phis when region is created"); kvn@2727: kvn@2727: if (new_entry == NULL) { kvn@2727: // Attach if_cont to iff kvn@2727: _igvn.hash_delete(iff); kvn@2727: iff->set_req(0, if_cont); kvn@2727: if (_idom != NULL) { kvn@2727: set_idom(iff, if_cont, dom_depth(iff)); kvn@2727: } kvn@2727: } kvn@2727: return if_cont->as_Proj(); kvn@2727: } kvn@2727: kvn@2727: //------------------------------create_new_if_for_predicate------------------------ kvn@2727: // Create a new if below new_entry for the predicate to be cloned (IGVN optimization) kvn@2727: ProjNode* PhaseIterGVN::create_new_if_for_predicate(ProjNode* cont_proj, Node* new_entry, kvn@2727: Deoptimization::DeoptReason reason) { kvn@2727: assert(new_entry != 0, "only used for clone predicate"); roland@5981: assert(cont_proj->is_uncommon_trap_if_pattern(reason), "must be a uct if pattern!"); kvn@2727: IfNode* iff = cont_proj->in(0)->as_If(); kvn@2727: kvn@2727: ProjNode *uncommon_proj = iff->proj_out(1 - cont_proj->_con); kvn@2727: Node *rgn = uncommon_proj->unique_ctrl_out(); kvn@2727: assert(rgn->is_Region() || rgn->is_Call(), "must be a region or call uct"); kvn@2727: kvn@2727: uint proj_index = 1; // region's edge corresponding to uncommon_proj kvn@2727: if (!rgn->is_Region()) { // create a region to guard the call kvn@2727: assert(rgn->is_Call(), "must be call uct"); kvn@2727: CallNode* call = rgn->as_Call(); kvn@4115: rgn = new (C) RegionNode(1); kvn@2727: register_new_node_with_optimizer(rgn); kvn@2727: rgn->add_req(uncommon_proj); kvn@2727: hash_delete(call); kvn@2727: call->set_req(0, rgn); kvn@2727: } else { kvn@2727: // Find region's edge corresponding to uncommon_proj kvn@2727: for (; proj_index < rgn->req(); proj_index++) kvn@2727: if (rgn->in(proj_index) == uncommon_proj) break; kvn@2727: assert(proj_index < rgn->req(), "sanity"); kvn@2727: } kvn@2727: kvn@2727: // Create new_iff in new location. kvn@2727: IfNode *new_iff = iff->clone()->as_If(); kvn@2727: new_iff->set_req(0, new_entry); kvn@2727: kvn@2727: register_new_node_with_optimizer(new_iff); kvn@4115: Node *if_cont = new (C) IfTrueNode(new_iff); kvn@4115: Node *if_uct = new (C) IfFalseNode(new_iff); kvn@2727: if (cont_proj->is_IfFalse()) { kvn@2727: // Swap kvn@2727: Node* tmp = if_uct; if_uct = if_cont; if_cont = tmp; kvn@2727: } kvn@2727: register_new_node_with_optimizer(if_cont); kvn@2727: register_new_node_with_optimizer(if_uct); kvn@2727: kvn@2727: // if_uct to rgn kvn@2727: hash_delete(rgn); kvn@2727: rgn->add_req(if_uct); kvn@2727: kvn@2727: // If rgn has phis add corresponding new edges which has the same kvn@2727: // value as on original uncommon_proj pass. kvn@2727: assert(rgn->in(rgn->req() -1) == if_uct, "new edge should be last"); kvn@2727: bool has_phi = false; kvn@2727: for (DUIterator_Fast imax, i = rgn->fast_outs(imax); i < imax; i++) { kvn@2727: Node* use = rgn->fast_out(i); kvn@2727: if (use->is_Phi() && use->outcnt() > 0) { kvn@3847: rehash_node_delayed(use); kvn@2727: use->add_req(use->in(proj_index)); kvn@2727: has_phi = true; kvn@2727: } kvn@2727: } kvn@2727: assert(!has_phi || rgn->req() > 3, "no phis when region is created"); kvn@2727: kvn@2727: return if_cont->as_Proj(); kvn@2727: } kvn@2727: kvn@2727: //--------------------------clone_predicate----------------------- kvn@2727: ProjNode* PhaseIdealLoop::clone_predicate(ProjNode* predicate_proj, Node* new_entry, kvn@2727: Deoptimization::DeoptReason reason, kvn@2727: PhaseIdealLoop* loop_phase, kvn@2727: PhaseIterGVN* igvn) { kvn@2727: ProjNode* new_predicate_proj; kvn@2727: if (loop_phase != NULL) { kvn@2727: new_predicate_proj = loop_phase->create_new_if_for_predicate(predicate_proj, new_entry, reason); kvn@2727: } else { kvn@2727: new_predicate_proj = igvn->create_new_if_for_predicate(predicate_proj, new_entry, reason); kvn@2727: } kvn@2727: IfNode* iff = new_predicate_proj->in(0)->as_If(); kvn@2727: Node* ctrl = iff->in(0); kvn@2727: kvn@2727: // Match original condition since predicate's projections could be swapped. kvn@2727: assert(predicate_proj->in(0)->in(1)->in(1)->Opcode()==Op_Opaque1, "must be"); kvn@4115: Node* opq = new (igvn->C) Opaque1Node(igvn->C, predicate_proj->in(0)->in(1)->in(1)->in(1)); kvn@2727: igvn->C->add_predicate_opaq(opq); kvn@2727: kvn@4115: Node* bol = new (igvn->C) Conv2BNode(opq); kvn@2727: if (loop_phase != NULL) { kvn@2727: loop_phase->register_new_node(opq, ctrl); kvn@2727: loop_phase->register_new_node(bol, ctrl); kvn@2727: } else { kvn@2727: igvn->register_new_node_with_optimizer(opq); kvn@2727: igvn->register_new_node_with_optimizer(bol); kvn@2727: } kvn@2727: igvn->hash_delete(iff); kvn@2727: iff->set_req(1, bol); kvn@2727: return new_predicate_proj; kvn@2727: } kvn@2727: kvn@2727: kvn@2727: //--------------------------clone_loop_predicates----------------------- kvn@2727: // Interface from IGVN kvn@2877: Node* PhaseIterGVN::clone_loop_predicates(Node* old_entry, Node* new_entry, bool clone_limit_check) { kvn@3043: return PhaseIdealLoop::clone_loop_predicates(old_entry, new_entry, clone_limit_check, NULL, this); kvn@2727: } kvn@2727: kvn@2727: // Interface from PhaseIdealLoop kvn@2877: Node* PhaseIdealLoop::clone_loop_predicates(Node* old_entry, Node* new_entry, bool clone_limit_check) { kvn@3043: return clone_loop_predicates(old_entry, new_entry, clone_limit_check, this, &this->_igvn); kvn@2727: } kvn@2727: kvn@2727: // Clone loop predicates to cloned loops (peeled, unswitched, split_if). kvn@2727: Node* PhaseIdealLoop::clone_loop_predicates(Node* old_entry, Node* new_entry, kvn@2877: bool clone_limit_check, kvn@2727: PhaseIdealLoop* loop_phase, kvn@2727: PhaseIterGVN* igvn) { kvn@2727: #ifdef ASSERT kvn@2727: if (new_entry == NULL || !(new_entry->is_Proj() || new_entry->is_Region() || new_entry->is_SafePoint())) { kvn@2727: if (new_entry != NULL) kvn@2727: new_entry->dump(); kvn@2727: assert(false, "not IfTrue, IfFalse, Region or SafePoint"); kvn@2727: } kvn@2727: #endif kvn@2727: // Search original predicates kvn@2727: Node* entry = old_entry; kvn@2877: ProjNode* limit_check_proj = NULL; kvn@2877: if (LoopLimitCheck) { kvn@2877: limit_check_proj = find_predicate_insertion_point(entry, Deoptimization::Reason_loop_limit_check); kvn@2877: if (limit_check_proj != NULL) { kvn@2877: entry = entry->in(0)->in(0); kvn@2877: } kvn@2877: } kvn@2727: if (UseLoopPredicate) { kvn@2727: ProjNode* predicate_proj = find_predicate_insertion_point(entry, Deoptimization::Reason_predicate); kvn@2727: if (predicate_proj != NULL) { // right pattern that can be used by loop predication kvn@3043: // clone predicate kvn@3043: new_entry = clone_predicate(predicate_proj, new_entry, kvn@3043: Deoptimization::Reason_predicate, kvn@3043: loop_phase, igvn); kvn@3043: assert(new_entry != NULL && new_entry->is_Proj(), "IfTrue or IfFalse after clone predicate"); kvn@2727: if (TraceLoopPredicate) { kvn@3043: tty->print("Loop Predicate cloned: "); kvn@2727: debug_only( new_entry->in(0)->dump(); ) kvn@2727: } kvn@2727: } kvn@2727: } kvn@2877: if (limit_check_proj != NULL && clone_limit_check) { kvn@2877: // Clone loop limit check last to insert it before loop. kvn@2877: // Don't clone a limit check which was already finalized kvn@2877: // for this counted loop (only one limit check is needed). kvn@3043: new_entry = clone_predicate(limit_check_proj, new_entry, kvn@3043: Deoptimization::Reason_loop_limit_check, kvn@3043: loop_phase, igvn); kvn@3043: assert(new_entry != NULL && new_entry->is_Proj(), "IfTrue or IfFalse after clone limit check"); kvn@2877: if (TraceLoopLimitCheck) { kvn@3043: tty->print("Loop Limit Check cloned: "); kvn@2877: debug_only( new_entry->in(0)->dump(); ) kvn@2877: } kvn@2877: } kvn@2727: return new_entry; kvn@2727: } kvn@2727: kvn@2727: //--------------------------skip_loop_predicates------------------------------ kvn@2727: // Skip related predicates. kvn@2727: Node* PhaseIdealLoop::skip_loop_predicates(Node* entry) { kvn@2727: Node* predicate = NULL; kvn@2877: if (LoopLimitCheck) { kvn@2877: predicate = find_predicate_insertion_point(entry, Deoptimization::Reason_loop_limit_check); kvn@2877: if (predicate != NULL) { kvn@2877: entry = entry->in(0)->in(0); kvn@2877: } kvn@2877: } kvn@2727: if (UseLoopPredicate) { kvn@2727: predicate = find_predicate_insertion_point(entry, Deoptimization::Reason_predicate); kvn@2727: if (predicate != NULL) { // right pattern that can be used by loop predication kvn@2727: IfNode* iff = entry->in(0)->as_If(); kvn@2727: ProjNode* uncommon_proj = iff->proj_out(1 - entry->as_Proj()->_con); kvn@2727: Node* rgn = uncommon_proj->unique_ctrl_out(); kvn@2727: assert(rgn->is_Region() || rgn->is_Call(), "must be a region or call uct"); kvn@2727: entry = entry->in(0)->in(0); kvn@2727: while (entry != NULL && entry->is_Proj() && entry->in(0)->is_If()) { kvn@2727: uncommon_proj = entry->in(0)->as_If()->proj_out(1 - entry->as_Proj()->_con); kvn@2727: if (uncommon_proj->unique_ctrl_out() != rgn) kvn@2727: break; kvn@2727: entry = entry->in(0)->in(0); kvn@2727: } kvn@2727: } kvn@2727: } kvn@2727: return entry; kvn@2727: } kvn@2727: kvn@2727: //--------------------------find_predicate_insertion_point------------------- kvn@2727: // Find a good location to insert a predicate kvn@2727: ProjNode* PhaseIdealLoop::find_predicate_insertion_point(Node* start_c, Deoptimization::DeoptReason reason) { kvn@2727: if (start_c == NULL || !start_c->is_Proj()) kvn@2727: return NULL; roland@5981: if (start_c->as_Proj()->is_uncommon_trap_if_pattern(reason)) { kvn@2727: return start_c->as_Proj(); kvn@2727: } kvn@2727: return NULL; kvn@2727: } kvn@2727: kvn@2727: //--------------------------find_predicate------------------------------------ kvn@2727: // Find a predicate kvn@2727: Node* PhaseIdealLoop::find_predicate(Node* entry) { kvn@2727: Node* predicate = NULL; kvn@2877: if (LoopLimitCheck) { kvn@2877: predicate = find_predicate_insertion_point(entry, Deoptimization::Reason_loop_limit_check); kvn@2877: if (predicate != NULL) { // right pattern that can be used by loop predication kvn@2877: return entry; kvn@2877: } kvn@2877: } kvn@2727: if (UseLoopPredicate) { kvn@2727: predicate = find_predicate_insertion_point(entry, Deoptimization::Reason_predicate); kvn@2727: if (predicate != NULL) { // right pattern that can be used by loop predication kvn@2727: return entry; kvn@2727: } kvn@2727: } kvn@2727: return NULL; kvn@2727: } kvn@2727: kvn@2727: //------------------------------Invariance----------------------------------- kvn@2727: // Helper class for loop_predication_impl to compute invariance on the fly and kvn@2727: // clone invariants. kvn@2727: class Invariance : public StackObj { kvn@2727: VectorSet _visited, _invariant; kvn@2727: Node_Stack _stack; kvn@2727: VectorSet _clone_visited; kvn@2727: Node_List _old_new; // map of old to new (clone) kvn@2727: IdealLoopTree* _lpt; kvn@2727: PhaseIdealLoop* _phase; kvn@2727: kvn@2727: // Helper function to set up the invariance for invariance computation kvn@2727: // If n is a known invariant, set up directly. Otherwise, look up the kvn@2727: // the possibility to push n onto the stack for further processing. kvn@2727: void visit(Node* use, Node* n) { kvn@2727: if (_lpt->is_invariant(n)) { // known invariant kvn@2727: _invariant.set(n->_idx); kvn@2727: } else if (!n->is_CFG()) { kvn@2727: Node *n_ctrl = _phase->ctrl_or_self(n); kvn@2727: Node *u_ctrl = _phase->ctrl_or_self(use); // self if use is a CFG kvn@2727: if (_phase->is_dominator(n_ctrl, u_ctrl)) { kvn@2727: _stack.push(n, n->in(0) == NULL ? 1 : 0); kvn@2727: } kvn@2727: } kvn@2727: } kvn@2727: kvn@2727: // Compute invariance for "the_node" and (possibly) all its inputs recursively kvn@2727: // on the fly kvn@2727: void compute_invariance(Node* n) { kvn@2727: assert(_visited.test(n->_idx), "must be"); kvn@2727: visit(n, n); kvn@2727: while (_stack.is_nonempty()) { kvn@2727: Node* n = _stack.node(); kvn@2727: uint idx = _stack.index(); kvn@2727: if (idx == n->req()) { // all inputs are processed kvn@2727: _stack.pop(); kvn@2727: // n is invariant if it's inputs are all invariant kvn@2727: bool all_inputs_invariant = true; kvn@2727: for (uint i = 0; i < n->req(); i++) { kvn@2727: Node* in = n->in(i); kvn@2727: if (in == NULL) continue; kvn@2727: assert(_visited.test(in->_idx), "must have visited input"); kvn@2727: if (!_invariant.test(in->_idx)) { // bad guy kvn@2727: all_inputs_invariant = false; kvn@2727: break; kvn@2727: } kvn@2727: } kvn@2727: if (all_inputs_invariant) { kvn@2727: _invariant.set(n->_idx); // I am a invariant too kvn@2727: } kvn@2727: } else { // process next input kvn@2727: _stack.set_index(idx + 1); kvn@2727: Node* m = n->in(idx); kvn@2727: if (m != NULL && !_visited.test_set(m->_idx)) { kvn@2727: visit(n, m); kvn@2727: } kvn@2727: } kvn@2727: } kvn@2727: } kvn@2727: kvn@2727: // Helper function to set up _old_new map for clone_nodes. kvn@2727: // If n is a known invariant, set up directly ("clone" of n == n). kvn@2727: // Otherwise, push n onto the stack for real cloning. kvn@2727: void clone_visit(Node* n) { kvn@2727: assert(_invariant.test(n->_idx), "must be invariant"); kvn@2727: if (_lpt->is_invariant(n)) { // known invariant kvn@2727: _old_new.map(n->_idx, n); kvn@2727: } else { // to be cloned kvn@2727: assert(!n->is_CFG(), "should not see CFG here"); kvn@2727: _stack.push(n, n->in(0) == NULL ? 1 : 0); kvn@2727: } kvn@2727: } kvn@2727: kvn@2727: // Clone "n" and (possibly) all its inputs recursively kvn@2727: void clone_nodes(Node* n, Node* ctrl) { kvn@2727: clone_visit(n); kvn@2727: while (_stack.is_nonempty()) { kvn@2727: Node* n = _stack.node(); kvn@2727: uint idx = _stack.index(); kvn@2727: if (idx == n->req()) { // all inputs processed, clone n! kvn@2727: _stack.pop(); kvn@2727: // clone invariant node kvn@2727: Node* n_cl = n->clone(); kvn@2727: _old_new.map(n->_idx, n_cl); kvn@2727: _phase->register_new_node(n_cl, ctrl); kvn@2727: for (uint i = 0; i < n->req(); i++) { kvn@2727: Node* in = n_cl->in(i); kvn@2727: if (in == NULL) continue; kvn@2727: n_cl->set_req(i, _old_new[in->_idx]); kvn@2727: } kvn@2727: } else { // process next input kvn@2727: _stack.set_index(idx + 1); kvn@2727: Node* m = n->in(idx); kvn@2727: if (m != NULL && !_clone_visited.test_set(m->_idx)) { kvn@2727: clone_visit(m); // visit the input kvn@2727: } kvn@2727: } kvn@2727: } kvn@2727: } kvn@2727: kvn@2727: public: kvn@2727: Invariance(Arena* area, IdealLoopTree* lpt) : kvn@2727: _lpt(lpt), _phase(lpt->_phase), kvn@2727: _visited(area), _invariant(area), _stack(area, 10 /* guess */), kvn@2727: _clone_visited(area), _old_new(area) kvn@2727: {} kvn@2727: kvn@2727: // Map old to n for invariance computation and clone kvn@2727: void map_ctrl(Node* old, Node* n) { kvn@2727: assert(old->is_CFG() && n->is_CFG(), "must be"); kvn@2727: _old_new.map(old->_idx, n); // "clone" of old is n kvn@2727: _invariant.set(old->_idx); // old is invariant kvn@2727: _clone_visited.set(old->_idx); kvn@2727: } kvn@2727: kvn@2727: // Driver function to compute invariance kvn@2727: bool is_invariant(Node* n) { kvn@2727: if (!_visited.test_set(n->_idx)) kvn@2727: compute_invariance(n); kvn@2727: return (_invariant.test(n->_idx) != 0); kvn@2727: } kvn@2727: kvn@2727: // Driver function to clone invariant kvn@2727: Node* clone(Node* n, Node* ctrl) { kvn@2727: assert(ctrl->is_CFG(), "must be"); kvn@2727: assert(_invariant.test(n->_idx), "must be an invariant"); kvn@2727: if (!_clone_visited.test(n->_idx)) kvn@2727: clone_nodes(n, ctrl); kvn@2727: return _old_new[n->_idx]; kvn@2727: } kvn@2727: }; kvn@2727: kvn@2727: //------------------------------is_range_check_if ----------------------------------- kvn@2727: // Returns true if the predicate of iff is in "scale*iv + offset u< load_range(ptr)" format kvn@2727: // Note: this function is particularly designed for loop predication. We require load_range kvn@2727: // and offset to be loop invariant computed on the fly by "invar" kvn@2727: bool IdealLoopTree::is_range_check_if(IfNode *iff, PhaseIdealLoop *phase, Invariance& invar) const { kvn@2727: if (!is_loop_exit(iff)) { kvn@2727: return false; kvn@2727: } kvn@2727: if (!iff->in(1)->is_Bool()) { kvn@2727: return false; kvn@2727: } kvn@2727: const BoolNode *bol = iff->in(1)->as_Bool(); kvn@2727: if (bol->_test._test != BoolTest::lt) { kvn@2727: return false; kvn@2727: } kvn@2727: if (!bol->in(1)->is_Cmp()) { kvn@2727: return false; kvn@2727: } kvn@2727: const CmpNode *cmp = bol->in(1)->as_Cmp(); kvn@2727: if (cmp->Opcode() != Op_CmpU) { kvn@2727: return false; kvn@2727: } kvn@2727: Node* range = cmp->in(2); kvn@2727: if (range->Opcode() != Op_LoadRange) { kvn@2727: const TypeInt* tint = phase->_igvn.type(range)->isa_int(); kvn@2877: if (tint == NULL || tint->empty() || tint->_lo < 0) { kvn@2727: // Allow predication on positive values that aren't LoadRanges. kvn@2727: // This allows optimization of loops where the length of the kvn@2727: // array is a known value and doesn't need to be loaded back kvn@2727: // from the array. kvn@2727: return false; kvn@2727: } kvn@2727: } kvn@2727: if (!invar.is_invariant(range)) { kvn@2727: return false; kvn@2727: } kvn@2727: Node *iv = _head->as_CountedLoop()->phi(); kvn@2727: int scale = 0; kvn@2727: Node *offset = NULL; kvn@2727: if (!phase->is_scaled_iv_plus_offset(cmp->in(1), iv, &scale, &offset)) { kvn@2727: return false; kvn@2727: } kvn@2727: if (offset && !invar.is_invariant(offset)) { // offset must be invariant kvn@2727: return false; kvn@2727: } kvn@2727: return true; kvn@2727: } kvn@2727: kvn@2727: //------------------------------rc_predicate----------------------------------- kvn@2727: // Create a range check predicate kvn@2727: // kvn@2727: // for (i = init; i < limit; i += stride) { kvn@2727: // a[scale*i+offset] kvn@2727: // } kvn@2727: // kvn@2727: // Compute max(scale*i + offset) for init <= i < limit and build the predicate kvn@2727: // as "max(scale*i + offset) u< a.length". kvn@2727: // kvn@2727: // There are two cases for max(scale*i + offset): kvn@2727: // (1) stride*scale > 0 kvn@2727: // max(scale*i + offset) = scale*(limit-stride) + offset kvn@2727: // (2) stride*scale < 0 kvn@2727: // max(scale*i + offset) = scale*init + offset kvn@2877: BoolNode* PhaseIdealLoop::rc_predicate(IdealLoopTree *loop, Node* ctrl, kvn@2727: int scale, Node* offset, kvn@2727: Node* init, Node* limit, Node* stride, kvn@2727: Node* range, bool upper) { never@2868: stringStream* predString = NULL; never@2868: if (TraceLoopPredicate) { never@2868: predString = new stringStream(); never@2868: predString->print("rc_predicate "); never@2868: } kvn@2727: kvn@2727: Node* max_idx_expr = init; kvn@2727: int stride_con = stride->get_int(); kvn@2727: if ((stride_con > 0) == (scale > 0) == upper) { kvn@2877: if (LoopLimitCheck) { kvn@2877: // With LoopLimitCheck limit is not exact. kvn@2877: // Calculate exact limit here. kvn@2877: // Note, counted loop's test is '<' or '>'. kvn@2877: limit = exact_limit(loop); kvn@4115: max_idx_expr = new (C) SubINode(limit, stride); kvn@2877: register_new_node(max_idx_expr, ctrl); kvn@2877: if (TraceLoopPredicate) predString->print("(limit - stride) "); kvn@2877: } else { kvn@4115: max_idx_expr = new (C) SubINode(limit, stride); kvn@2877: register_new_node(max_idx_expr, ctrl); kvn@2877: if (TraceLoopPredicate) predString->print("(limit - stride) "); kvn@2877: } kvn@2727: } else { never@2868: if (TraceLoopPredicate) predString->print("init "); kvn@2727: } kvn@2727: kvn@2727: if (scale != 1) { kvn@2727: ConNode* con_scale = _igvn.intcon(scale); kvn@4115: max_idx_expr = new (C) MulINode(max_idx_expr, con_scale); kvn@2727: register_new_node(max_idx_expr, ctrl); never@2868: if (TraceLoopPredicate) predString->print("* %d ", scale); kvn@2727: } kvn@2727: kvn@2727: if (offset && (!offset->is_Con() || offset->get_int() != 0)){ kvn@4115: max_idx_expr = new (C) AddINode(max_idx_expr, offset); kvn@2727: register_new_node(max_idx_expr, ctrl); kvn@2727: if (TraceLoopPredicate) never@2868: if (offset->is_Con()) predString->print("+ %d ", offset->get_int()); never@2868: else predString->print("+ offset "); kvn@2727: } kvn@2727: kvn@4115: CmpUNode* cmp = new (C) CmpUNode(max_idx_expr, range); kvn@2727: register_new_node(cmp, ctrl); kvn@4115: BoolNode* bol = new (C) BoolNode(cmp, BoolTest::lt); kvn@2727: register_new_node(bol, ctrl); kvn@2727: never@2868: if (TraceLoopPredicate) { never@2868: predString->print_cr("print("%s", predString->as_string()); never@2868: } kvn@2727: return bol; kvn@2727: } kvn@2727: kvn@2727: //------------------------------ loop_predication_impl-------------------------- kvn@2727: // Insert loop predicates for null checks and range checks kvn@2727: bool PhaseIdealLoop::loop_predication_impl(IdealLoopTree *loop) { kvn@2727: if (!UseLoopPredicate) return false; kvn@2727: kvn@2727: if (!loop->_head->is_Loop()) { kvn@2727: // Could be a simple region when irreducible loops are present. kvn@2727: return false; kvn@2727: } kvn@2877: LoopNode* head = loop->_head->as_Loop(); kvn@2727: kvn@2877: if (head->unique_ctrl_out()->Opcode() == Op_NeverBranch) { kvn@2727: // do nothing for infinite loops kvn@2727: return false; kvn@2727: } kvn@2727: kvn@2727: CountedLoopNode *cl = NULL; kvn@3048: if (head->is_valid_counted_loop()) { kvn@2877: cl = head->as_CountedLoop(); kvn@2727: // do nothing for iteration-splitted loops kvn@2727: if (!cl->is_normal_loop()) return false; kvn@3038: // Avoid RCE if Counted loop's test is '!='. kvn@3038: BoolTest::mask bt = cl->loopexit()->test_trip(); kvn@3038: if (bt != BoolTest::lt && bt != BoolTest::gt) kvn@3038: cl = NULL; kvn@2727: } kvn@2727: kvn@2877: Node* entry = head->in(LoopNode::EntryControl); kvn@2877: ProjNode *predicate_proj = NULL; kvn@2877: // Loop limit check predicate should be near the loop. kvn@2877: if (LoopLimitCheck) { kvn@2877: predicate_proj = find_predicate_insertion_point(entry, Deoptimization::Reason_loop_limit_check); kvn@2877: if (predicate_proj != NULL) kvn@2877: entry = predicate_proj->in(0)->in(0); kvn@2877: } kvn@2727: kvn@2877: predicate_proj = find_predicate_insertion_point(entry, Deoptimization::Reason_predicate); kvn@2727: if (!predicate_proj) { kvn@2727: #ifndef PRODUCT kvn@2727: if (TraceLoopPredicate) { kvn@2727: tty->print("missing predicate:"); kvn@2727: loop->dump_head(); kvn@2877: head->dump(1); kvn@2727: } kvn@2727: #endif kvn@2727: return false; kvn@2727: } kvn@2727: ConNode* zero = _igvn.intcon(0); kvn@2727: set_ctrl(zero, C->root()); kvn@2727: kvn@2727: ResourceArea *area = Thread::current()->resource_area(); kvn@2727: Invariance invar(area, loop); kvn@2727: kvn@2727: // Create list of if-projs such that a newer proj dominates all older kvn@2727: // projs in the list, and they all dominate loop->tail() kvn@2727: Node_List if_proj_list(area); kvn@2727: Node *current_proj = loop->tail(); //start from tail kvn@2727: while (current_proj != head) { kvn@2727: if (loop == get_loop(current_proj) && // still in the loop ? kvn@2727: current_proj->is_Proj() && // is a projection ? kvn@2727: current_proj->in(0)->Opcode() == Op_If) { // is a if projection ? kvn@2727: if_proj_list.push(current_proj); kvn@2727: } kvn@2727: current_proj = idom(current_proj); kvn@2727: } kvn@2727: kvn@2727: bool hoisted = false; // true if at least one proj is promoted kvn@2727: while (if_proj_list.size() > 0) { kvn@2727: // Following are changed to nonnull when a predicate can be hoisted kvn@2727: ProjNode* new_predicate_proj = NULL; kvn@2727: kvn@2727: ProjNode* proj = if_proj_list.pop()->as_Proj(); kvn@2727: IfNode* iff = proj->in(0)->as_If(); kvn@2727: roland@5981: if (!proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none)) { kvn@2727: if (loop->is_loop_exit(iff)) { kvn@2727: // stop processing the remaining projs in the list because the execution of them kvn@2727: // depends on the condition of "iff" (iff->in(1)). kvn@2727: break; kvn@2727: } else { kvn@2727: // Both arms are inside the loop. There are two cases: kvn@2727: // (1) there is one backward branch. In this case, any remaining proj kvn@2727: // in the if_proj list post-dominates "iff". So, the condition of "iff" kvn@2727: // does not determine the execution the remining projs directly, and we kvn@2727: // can safely continue. kvn@2727: // (2) both arms are forwarded, i.e. a diamond shape. In this case, "proj" kvn@2727: // does not dominate loop->tail(), so it can not be in the if_proj list. kvn@2727: continue; kvn@2727: } kvn@2727: } kvn@2727: kvn@2727: Node* test = iff->in(1); kvn@2727: if (!test->is_Bool()){ //Conv2B, ... kvn@2727: continue; kvn@2727: } kvn@2727: BoolNode* bol = test->as_Bool(); kvn@2727: if (invar.is_invariant(bol)) { kvn@2727: // Invariant test kvn@2727: new_predicate_proj = create_new_if_for_predicate(predicate_proj, NULL, kvn@2727: Deoptimization::Reason_predicate); kvn@2727: Node* ctrl = new_predicate_proj->in(0)->as_If()->in(0); kvn@2727: BoolNode* new_predicate_bol = invar.clone(bol, ctrl)->as_Bool(); kvn@2727: kvn@2727: // Negate test if necessary kvn@2727: bool negated = false; kvn@2727: if (proj->_con != predicate_proj->_con) { kvn@4115: new_predicate_bol = new (C) BoolNode(new_predicate_bol->in(1), new_predicate_bol->_test.negate()); kvn@2727: register_new_node(new_predicate_bol, ctrl); kvn@2727: negated = true; kvn@2727: } kvn@2727: IfNode* new_predicate_iff = new_predicate_proj->in(0)->as_If(); kvn@2727: _igvn.hash_delete(new_predicate_iff); kvn@2727: new_predicate_iff->set_req(1, new_predicate_bol); kvn@2727: #ifndef PRODUCT kvn@2727: if (TraceLoopPredicate) { kvn@2727: tty->print("Predicate invariant if%s: %d ", negated ? " negated" : "", new_predicate_iff->_idx); kvn@2727: loop->dump_head(); kvn@2727: } else if (TraceLoopOpts) { kvn@2727: tty->print("Predicate IC "); kvn@2727: loop->dump_head(); kvn@2727: } kvn@2727: #endif kvn@5110: } else if ((cl != NULL) && (proj->_con == predicate_proj->_con) && kvn@5110: loop->is_range_check_if(iff, this, invar)) { kvn@2727: kvn@2727: // Range check for counted loops kvn@2727: const Node* cmp = bol->in(1)->as_Cmp(); kvn@2727: Node* idx = cmp->in(1); kvn@2727: assert(!invar.is_invariant(idx), "index is variant"); kvn@2727: Node* rng = cmp->in(2); kvn@2877: assert(rng->Opcode() == Op_LoadRange || _igvn.type(rng)->is_int() >= 0, "must be"); kvn@2727: assert(invar.is_invariant(rng), "range must be invariant"); kvn@2727: int scale = 1; kvn@2727: Node* offset = zero; kvn@2727: bool ok = is_scaled_iv_plus_offset(idx, cl->phi(), &scale, &offset); kvn@2727: assert(ok, "must be index expression"); kvn@2727: kvn@2727: Node* init = cl->init_trip(); kvn@2727: Node* limit = cl->limit(); kvn@2727: Node* stride = cl->stride(); kvn@2727: kvn@2727: // Build if's for the upper and lower bound tests. The kvn@2727: // lower_bound test will dominate the upper bound test and all kvn@2727: // cloned or created nodes will use the lower bound test as kvn@2727: // their declared control. kvn@2727: ProjNode* lower_bound_proj = create_new_if_for_predicate(predicate_proj, NULL, Deoptimization::Reason_predicate); kvn@2727: ProjNode* upper_bound_proj = create_new_if_for_predicate(predicate_proj, NULL, Deoptimization::Reason_predicate); kvn@2727: assert(upper_bound_proj->in(0)->as_If()->in(0) == lower_bound_proj, "should dominate"); kvn@2727: Node *ctrl = lower_bound_proj->in(0)->as_If()->in(0); kvn@2727: kvn@2727: // Perform cloning to keep Invariance state correct since the kvn@2727: // late schedule will place invariant things in the loop. kvn@2727: rng = invar.clone(rng, ctrl); kvn@2727: if (offset && offset != zero) { kvn@2727: assert(invar.is_invariant(offset), "offset must be loop invariant"); kvn@2727: offset = invar.clone(offset, ctrl); kvn@2727: } kvn@2727: kvn@2727: // Test the lower bound kvn@2877: Node* lower_bound_bol = rc_predicate(loop, ctrl, scale, offset, init, limit, stride, rng, false); kvn@2727: IfNode* lower_bound_iff = lower_bound_proj->in(0)->as_If(); kvn@2727: _igvn.hash_delete(lower_bound_iff); kvn@2727: lower_bound_iff->set_req(1, lower_bound_bol); kvn@2727: if (TraceLoopPredicate) tty->print_cr("lower bound check if: %d", lower_bound_iff->_idx); kvn@2727: kvn@2727: // Test the upper bound kvn@3038: Node* upper_bound_bol = rc_predicate(loop, lower_bound_proj, scale, offset, init, limit, stride, rng, true); kvn@2727: IfNode* upper_bound_iff = upper_bound_proj->in(0)->as_If(); kvn@2727: _igvn.hash_delete(upper_bound_iff); kvn@2727: upper_bound_iff->set_req(1, upper_bound_bol); kvn@2727: if (TraceLoopPredicate) tty->print_cr("upper bound check if: %d", lower_bound_iff->_idx); kvn@2727: kvn@2727: // Fall through into rest of the clean up code which will move kvn@2727: // any dependent nodes onto the upper bound test. kvn@2727: new_predicate_proj = upper_bound_proj; kvn@2727: kvn@2727: #ifndef PRODUCT kvn@2727: if (TraceLoopOpts && !TraceLoopPredicate) { kvn@2727: tty->print("Predicate RC "); kvn@2727: loop->dump_head(); kvn@2727: } kvn@2727: #endif kvn@2727: } else { kvn@2727: // Loop variant check (for example, range check in non-counted loop) kvn@2727: // with uncommon trap. kvn@2727: continue; kvn@2727: } kvn@2727: assert(new_predicate_proj != NULL, "sanity"); kvn@2727: // Success - attach condition (new_predicate_bol) to predicate if kvn@2727: invar.map_ctrl(proj, new_predicate_proj); // so that invariance test can be appropriate kvn@2727: kvn@2727: // Eliminate the old If in the loop body kvn@2727: dominated_by( new_predicate_proj, iff, proj->_con != new_predicate_proj->_con ); kvn@2727: kvn@2727: hoisted = true; kvn@2727: C->set_major_progress(); kvn@2727: } // end while kvn@2727: kvn@2727: #ifndef PRODUCT kvn@2727: // report that the loop predication has been actually performed kvn@2727: // for this loop kvn@2727: if (TraceLoopPredicate && hoisted) { kvn@2727: tty->print("Loop Predication Performed:"); kvn@2727: loop->dump_head(); kvn@2727: } kvn@2727: #endif kvn@2727: kvn@2727: return hoisted; kvn@2727: } kvn@2727: kvn@2727: //------------------------------loop_predication-------------------------------- kvn@2727: // driver routine for loop predication optimization kvn@2727: bool IdealLoopTree::loop_predication( PhaseIdealLoop *phase) { kvn@2727: bool hoisted = false; kvn@2727: // Recursively promote predicates kvn@2727: if (_child) { kvn@2727: hoisted = _child->loop_predication( phase); kvn@2727: } kvn@2727: kvn@2727: // self kvn@2727: if (!_irreducible && !tail()->is_top()) { kvn@2727: hoisted |= phase->loop_predication_impl(this); kvn@2727: } kvn@2727: kvn@2727: if (_next) { //sibling kvn@2727: hoisted |= _next->loop_predication( phase); kvn@2727: } kvn@2727: kvn@2727: return hoisted; kvn@2727: }