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