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