0N/A/*
2273N/A * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "memory/allocation.inline.hpp"
1879N/A#include "opto/addnode.hpp"
1879N/A#include "opto/connode.hpp"
1879N/A#include "opto/divnode.hpp"
1879N/A#include "opto/loopnode.hpp"
2885N/A#include "opto/matcher.hpp"
1879N/A#include "opto/mulnode.hpp"
1879N/A#include "opto/rootnode.hpp"
1879N/A#include "opto/subnode.hpp"
0N/A
0N/A//=============================================================================
0N/A//------------------------------split_thru_phi---------------------------------
0N/A// Split Node 'n' through merge point if there is enough win.
0N/ANode *PhaseIdealLoop::split_thru_phi( Node *n, Node *region, int policy ) {
69N/A if (n->Opcode() == Op_ConvI2L && n->bottom_type() != TypeLong::LONG) {
69N/A // ConvI2L may have type information on it which is unsafe to push up
69N/A // so disable this for now
69N/A return NULL;
69N/A }
0N/A int wins = 0;
2230N/A assert(!n->is_CFG(), "");
2230N/A assert(region->is_Region(), "");
64N/A
64N/A const Type* type = n->bottom_type();
64N/A const TypeOopPtr *t_oop = _igvn.type(n)->isa_oopptr();
64N/A Node *phi;
2230N/A if (t_oop != NULL && t_oop->is_known_instance_field()) {
64N/A int iid = t_oop->instance_id();
64N/A int index = C->get_alias_index(t_oop);
64N/A int offset = t_oop->offset();
4022N/A phi = new (C) PhiNode(region, type, NULL, iid, index, offset);
64N/A } else {
1254N/A phi = PhiNode::make_blank(region, n);
64N/A }
0N/A uint old_unique = C->unique();
2230N/A for (uint i = 1; i < region->req(); i++) {
0N/A Node *x;
0N/A Node* the_clone = NULL;
2230N/A if (region->in(i) == C->top()) {
0N/A x = C->top(); // Dead path? Use a dead data op
0N/A } else {
0N/A x = n->clone(); // Else clone up the data op
0N/A the_clone = x; // Remember for possible deletion.
0N/A // Alter data node to use pre-phi inputs
2230N/A if (n->in(0) == region)
0N/A x->set_req( 0, region->in(i) );
2230N/A for (uint j = 1; j < n->req(); j++) {
0N/A Node *in = n->in(j);
2230N/A if (in->is_Phi() && in->in(0) == region)
0N/A x->set_req( j, in->in(i) ); // Use pre-Phi input for the clone
0N/A }
0N/A }
0N/A // Check for a 'win' on some paths
0N/A const Type *t = x->Value(&_igvn);
0N/A
0N/A bool singleton = t->singleton();
0N/A
0N/A // A TOP singleton indicates that there are no possible values incoming
0N/A // along a particular edge. In most cases, this is OK, and the Phi will
0N/A // be eliminated later in an Ideal call. However, we can't allow this to
0N/A // happen if the singleton occurs on loop entry, as the elimination of
0N/A // the PhiNode may cause the resulting node to migrate back to a previous
0N/A // loop iteration.
2230N/A if (singleton && t == Type::TOP) {
0N/A // Is_Loop() == false does not confirm the absence of a loop (e.g., an
0N/A // irreducible loop may not be indicated by an affirmative is_Loop());
0N/A // therefore, the only top we can split thru a phi is on a backedge of
0N/A // a loop.
0N/A singleton &= region->is_Loop() && (i != LoopNode::EntryControl);
0N/A }
0N/A
2230N/A if (singleton) {
0N/A wins++;
0N/A x = ((PhaseGVN&)_igvn).makecon(t);
0N/A } else {
0N/A // We now call Identity to try to simplify the cloned node.
0N/A // Note that some Identity methods call phase->type(this).
0N/A // Make sure that the type array is big enough for
0N/A // our new node, even though we may throw the node away.
0N/A // (Note: This tweaking with igvn only works because x is a new node.)
0N/A _igvn.set_type(x, t);
293N/A // If x is a TypeNode, capture any more-precise type permanently into Node
605N/A // otherwise it will be not updated during igvn->transform since
293N/A // igvn->type(x) is set to x->Value() already.
293N/A x->raise_bottom_type(t);
0N/A Node *y = x->Identity(&_igvn);
2230N/A if (y != x) {
0N/A wins++;
0N/A x = y;
0N/A } else {
0N/A y = _igvn.hash_find(x);
2230N/A if (y) {
0N/A wins++;
0N/A x = y;
0N/A } else {
0N/A // Else x is a new node we are keeping
0N/A // We do not need register_new_node_with_optimizer
0N/A // because set_type has already been called.
0N/A _igvn._worklist.push(x);
0N/A }
0N/A }
0N/A }
0N/A if (x != the_clone && the_clone != NULL)
0N/A _igvn.remove_dead_node(the_clone);
0N/A phi->set_req( i, x );
0N/A }
0N/A // Too few wins?
2230N/A if (wins <= policy) {
0N/A _igvn.remove_dead_node(phi);
0N/A return NULL;
0N/A }
0N/A
0N/A // Record Phi
0N/A register_new_node( phi, region );
0N/A
2230N/A for (uint i2 = 1; i2 < phi->req(); i2++) {
0N/A Node *x = phi->in(i2);
0N/A // If we commoned up the cloned 'x' with another existing Node,
0N/A // the existing Node picks up a new use. We need to make the
0N/A // existing Node occur higher up so it dominates its uses.
0N/A Node *old_ctrl;
0N/A IdealLoopTree *old_loop;
0N/A
2230N/A if (x->is_Con()) {
2230N/A // Constant's control is always root.
2230N/A set_ctrl(x, C->root());
2230N/A continue;
2230N/A }
0N/A // The occasional new node
2230N/A if (x->_idx >= old_unique) { // Found a new, unplaced node?
2230N/A old_ctrl = NULL;
2230N/A old_loop = NULL; // Not in any prior loop
0N/A } else {
2230N/A old_ctrl = get_ctrl(x);
0N/A old_loop = get_loop(old_ctrl); // Get prior loop
0N/A }
0N/A // New late point must dominate new use
2230N/A Node *new_ctrl = dom_lca(old_ctrl, region->in(i2));
2230N/A if (new_ctrl == old_ctrl) // Nothing is changed
2230N/A continue;
2230N/A
2230N/A IdealLoopTree *new_loop = get_loop(new_ctrl);
2230N/A
2230N/A // Don't move x into a loop if its uses are
2230N/A // outside of loop. Otherwise x will be cloned
2230N/A // for each use outside of this loop.
2230N/A IdealLoopTree *use_loop = get_loop(region);
2230N/A if (!new_loop->is_member(use_loop) &&
2230N/A (old_loop == NULL || !new_loop->is_member(old_loop))) {
2230N/A // Take early control, later control will be recalculated
2230N/A // during next iteration of loop optimizations.
2230N/A new_ctrl = get_early_ctrl(x);
2230N/A new_loop = get_loop(new_ctrl);
2230N/A }
0N/A // Set new location
0N/A set_ctrl(x, new_ctrl);
0N/A // If changing loop bodies, see if we need to collect into new body
2230N/A if (old_loop != new_loop) {
2230N/A if (old_loop && !old_loop->_child)
0N/A old_loop->_body.yank(x);
2230N/A if (!new_loop->_child)
0N/A new_loop->_body.push(x); // Collect body info
0N/A }
0N/A }
0N/A
0N/A return phi;
0N/A}
0N/A
0N/A//------------------------------dominated_by------------------------------------
0N/A// Replace the dominated test with an obvious true or false. Place it on the
0N/A// IGVN worklist for later cleanup. Move control-dependent data Nodes on the
0N/A// live path up to the dominating control.
2665N/Avoid PhaseIdealLoop::dominated_by( Node *prevdom, Node *iff, bool flip, bool exclude_loop_predicate ) {
0N/A#ifndef PRODUCT
2230N/A if (VerifyLoopOptimizations && PrintOpto) tty->print_cr("dominating test");
0N/A#endif
0N/A
0N/A
0N/A // prevdom is the dominating projection of the dominating test.
0N/A assert( iff->is_If(), "" );
0N/A assert( iff->Opcode() == Op_If || iff->Opcode() == Op_CountedLoopEnd, "Check this code when new subtype is added");
0N/A int pop = prevdom->Opcode();
0N/A assert( pop == Op_IfFalse || pop == Op_IfTrue, "" );
2230N/A if (flip) {
2230N/A if (pop == Op_IfTrue)
2230N/A pop = Op_IfFalse;
2230N/A else
2230N/A pop = Op_IfTrue;
2230N/A }
0N/A // 'con' is set to true or false to kill the dominated test.
0N/A Node *con = _igvn.makecon(pop == Op_IfTrue ? TypeInt::ONE : TypeInt::ZERO);
0N/A set_ctrl(con, C->root()); // Constant gets a new use
0N/A // Hack the dominated test
3811N/A _igvn.replace_input_of(iff, 1, con);
0N/A
0N/A // If I dont have a reachable TRUE and FALSE path following the IfNode then
0N/A // I can assume this path reaches an infinite loop. In this case it's not
0N/A // important to optimize the data Nodes - either the whole compilation will
0N/A // be tossed or this path (and all data Nodes) will go dead.
2230N/A if (iff->outcnt() != 2) return;
0N/A
0N/A // Make control-dependent data Nodes on the live path (path that will remain
0N/A // once the dominated IF is removed) become control-dependent on the
0N/A // dominating projection.
2665N/A Node* dp = iff->as_If()->proj_out(pop == Op_IfTrue);
2665N/A
2665N/A // Loop predicates may have depending checks which should not
2665N/A // be skipped. For example, range check predicate has two checks
2665N/A // for lower and upper bounds.
2665N/A ProjNode* unc_proj = iff->as_If()->proj_out(1 - dp->as_Proj()->_con)->as_Proj();
2665N/A if (exclude_loop_predicate &&
2665N/A is_uncommon_trap_proj(unc_proj, Deoptimization::Reason_predicate))
2665N/A return; // Let IGVN transformation change control dependence.
2665N/A
0N/A IdealLoopTree *old_loop = get_loop(dp);
0N/A
0N/A for (DUIterator_Fast imax, i = dp->fast_outs(imax); i < imax; i++) {
0N/A Node* cd = dp->fast_out(i); // Control-dependent node
2230N/A if (cd->depends_only_on_test()) {
2230N/A assert(cd->in(0) == dp, "");
3811N/A _igvn.replace_input_of(cd, 0, prevdom);
2230N/A set_early_ctrl(cd);
0N/A IdealLoopTree *new_loop = get_loop(get_ctrl(cd));
2230N/A if (old_loop != new_loop) {
2230N/A if (!old_loop->_child) old_loop->_body.yank(cd);
2230N/A if (!new_loop->_child) new_loop->_body.push(cd);
0N/A }
0N/A --i;
0N/A --imax;
0N/A }
0N/A }
0N/A}
0N/A
0N/A//------------------------------has_local_phi_input----------------------------
0N/A// Return TRUE if 'n' has Phi inputs from its local block and no other
0N/A// block-local inputs (all non-local-phi inputs come from earlier blocks)
0N/ANode *PhaseIdealLoop::has_local_phi_input( Node *n ) {
0N/A Node *n_ctrl = get_ctrl(n);
0N/A // See if some inputs come from a Phi in this block, or from before
0N/A // this block.
0N/A uint i;
0N/A for( i = 1; i < n->req(); i++ ) {
0N/A Node *phi = n->in(i);
0N/A if( phi->is_Phi() && phi->in(0) == n_ctrl )
0N/A break;
0N/A }
0N/A if( i >= n->req() )
0N/A return NULL; // No Phi inputs; nowhere to clone thru
0N/A
0N/A // Check for inputs created between 'n' and the Phi input. These
0N/A // must split as well; they have already been given the chance
0N/A // (courtesy of a post-order visit) and since they did not we must
0N/A // recover the 'cost' of splitting them by being very profitable
0N/A // when splitting 'n'. Since this is unlikely we simply give up.
0N/A for( i = 1; i < n->req(); i++ ) {
0N/A Node *m = n->in(i);
0N/A if( get_ctrl(m) == n_ctrl && !m->is_Phi() ) {
0N/A // We allow the special case of AddP's with no local inputs.
0N/A // This allows us to split-up address expressions.
0N/A if (m->is_AddP() &&
0N/A get_ctrl(m->in(2)) != n_ctrl &&
0N/A get_ctrl(m->in(3)) != n_ctrl) {
0N/A // Move the AddP up to dominating point
0N/A set_ctrl_and_loop(m, find_non_split_ctrl(idom(n_ctrl)));
0N/A continue;
0N/A }
0N/A return NULL;
0N/A }
0N/A }
0N/A
0N/A return n_ctrl;
0N/A}
0N/A
0N/A//------------------------------remix_address_expressions----------------------
0N/A// Rework addressing expressions to get the most loop-invariant stuff
0N/A// moved out. We'd like to do all associative operators, but it's especially
0N/A// important (common) to do address expressions.
0N/ANode *PhaseIdealLoop::remix_address_expressions( Node *n ) {
0N/A if (!has_ctrl(n)) return NULL;
0N/A Node *n_ctrl = get_ctrl(n);
0N/A IdealLoopTree *n_loop = get_loop(n_ctrl);
0N/A
0N/A // See if 'n' mixes loop-varying and loop-invariant inputs and
0N/A // itself is loop-varying.
0N/A
0N/A // Only interested in binary ops (and AddP)
0N/A if( n->req() < 3 || n->req() > 4 ) return NULL;
0N/A
0N/A Node *n1_ctrl = get_ctrl(n->in( 1));
0N/A Node *n2_ctrl = get_ctrl(n->in( 2));
0N/A Node *n3_ctrl = get_ctrl(n->in(n->req() == 3 ? 2 : 3));
0N/A IdealLoopTree *n1_loop = get_loop( n1_ctrl );
0N/A IdealLoopTree *n2_loop = get_loop( n2_ctrl );
0N/A IdealLoopTree *n3_loop = get_loop( n3_ctrl );
0N/A
0N/A // Does one of my inputs spin in a tighter loop than self?
0N/A if( (n_loop->is_member( n1_loop ) && n_loop != n1_loop) ||
0N/A (n_loop->is_member( n2_loop ) && n_loop != n2_loop) ||
0N/A (n_loop->is_member( n3_loop ) && n_loop != n3_loop) )
0N/A return NULL; // Leave well enough alone
0N/A
0N/A // Is at least one of my inputs loop-invariant?
0N/A if( n1_loop == n_loop &&
0N/A n2_loop == n_loop &&
0N/A n3_loop == n_loop )
0N/A return NULL; // No loop-invariant inputs
0N/A
0N/A
0N/A int n_op = n->Opcode();
0N/A
0N/A // Replace expressions like ((V+I) << 2) with (V<<2 + I<<2).
0N/A if( n_op == Op_LShiftI ) {
0N/A // Scale is loop invariant
0N/A Node *scale = n->in(2);
0N/A Node *scale_ctrl = get_ctrl(scale);
0N/A IdealLoopTree *scale_loop = get_loop(scale_ctrl );
0N/A if( n_loop == scale_loop || !scale_loop->is_member( n_loop ) )
0N/A return NULL;
0N/A const TypeInt *scale_t = scale->bottom_type()->isa_int();
0N/A if( scale_t && scale_t->is_con() && scale_t->get_con() >= 16 )
0N/A return NULL; // Dont bother with byte/short masking
0N/A // Add must vary with loop (else shift would be loop-invariant)
0N/A Node *add = n->in(1);
0N/A Node *add_ctrl = get_ctrl(add);
0N/A IdealLoopTree *add_loop = get_loop(add_ctrl);
0N/A //assert( n_loop == add_loop, "" );
0N/A if( n_loop != add_loop ) return NULL; // happens w/ evil ZKM loops
0N/A
0N/A // Convert I-V into I+ (0-V); same for V-I
0N/A if( add->Opcode() == Op_SubI &&
0N/A _igvn.type( add->in(1) ) != TypeInt::ZERO ) {
0N/A Node *zero = _igvn.intcon(0);
0N/A set_ctrl(zero, C->root());
4022N/A Node *neg = new (C) SubINode( _igvn.intcon(0), add->in(2) );
0N/A register_new_node( neg, get_ctrl(add->in(2) ) );
4022N/A add = new (C) AddINode( add->in(1), neg );
0N/A register_new_node( add, add_ctrl );
0N/A }
0N/A if( add->Opcode() != Op_AddI ) return NULL;
0N/A // See if one add input is loop invariant
0N/A Node *add_var = add->in(1);
0N/A Node *add_var_ctrl = get_ctrl(add_var);
0N/A IdealLoopTree *add_var_loop = get_loop(add_var_ctrl );
0N/A Node *add_invar = add->in(2);
0N/A Node *add_invar_ctrl = get_ctrl(add_invar);
0N/A IdealLoopTree *add_invar_loop = get_loop(add_invar_ctrl );
0N/A if( add_var_loop == n_loop ) {
0N/A } else if( add_invar_loop == n_loop ) {
0N/A // Swap to find the invariant part
0N/A add_invar = add_var;
0N/A add_invar_ctrl = add_var_ctrl;
0N/A add_invar_loop = add_var_loop;
0N/A add_var = add->in(2);
0N/A Node *add_var_ctrl = get_ctrl(add_var);
0N/A IdealLoopTree *add_var_loop = get_loop(add_var_ctrl );
0N/A } else // Else neither input is loop invariant
0N/A return NULL;
0N/A if( n_loop == add_invar_loop || !add_invar_loop->is_member( n_loop ) )
0N/A return NULL; // No invariant part of the add?
0N/A
0N/A // Yes! Reshape address expression!
4022N/A Node *inv_scale = new (C) LShiftINode( add_invar, scale );
850N/A Node *inv_scale_ctrl =
850N/A dom_depth(add_invar_ctrl) > dom_depth(scale_ctrl) ?
850N/A add_invar_ctrl : scale_ctrl;
850N/A register_new_node( inv_scale, inv_scale_ctrl );
4022N/A Node *var_scale = new (C) LShiftINode( add_var, scale );
0N/A register_new_node( var_scale, n_ctrl );
4022N/A Node *var_add = new (C) AddINode( var_scale, inv_scale );
0N/A register_new_node( var_add, n_ctrl );
1541N/A _igvn.replace_node( n, var_add );
0N/A return var_add;
0N/A }
0N/A
0N/A // Replace (I+V) with (V+I)
0N/A if( n_op == Op_AddI ||
0N/A n_op == Op_AddL ||
0N/A n_op == Op_AddF ||
0N/A n_op == Op_AddD ||
0N/A n_op == Op_MulI ||
0N/A n_op == Op_MulL ||
0N/A n_op == Op_MulF ||
0N/A n_op == Op_MulD ) {
0N/A if( n2_loop == n_loop ) {
0N/A assert( n1_loop != n_loop, "" );
0N/A n->swap_edges(1, 2);
0N/A }
0N/A }
0N/A
0N/A // Replace ((I1 +p V) +p I2) with ((I1 +p I2) +p V),
0N/A // but not if I2 is a constant.
0N/A if( n_op == Op_AddP ) {
0N/A if( n2_loop == n_loop && n3_loop != n_loop ) {
0N/A if( n->in(2)->Opcode() == Op_AddP && !n->in(3)->is_Con() ) {
0N/A Node *n22_ctrl = get_ctrl(n->in(2)->in(2));
0N/A Node *n23_ctrl = get_ctrl(n->in(2)->in(3));
0N/A IdealLoopTree *n22loop = get_loop( n22_ctrl );
0N/A IdealLoopTree *n23_loop = get_loop( n23_ctrl );
0N/A if( n22loop != n_loop && n22loop->is_member(n_loop) &&
0N/A n23_loop == n_loop ) {
4022N/A Node *add1 = new (C) AddPNode( n->in(1), n->in(2)->in(2), n->in(3) );
0N/A // Stuff new AddP in the loop preheader
0N/A register_new_node( add1, n_loop->_head->in(LoopNode::EntryControl) );
4022N/A Node *add2 = new (C) AddPNode( n->in(1), add1, n->in(2)->in(3) );
0N/A register_new_node( add2, n_ctrl );
1541N/A _igvn.replace_node( n, add2 );
0N/A return add2;
0N/A }
0N/A }
0N/A }
0N/A
0N/A // Replace (I1 +p (I2 + V)) with ((I1 +p I2) +p V)
0N/A if( n2_loop != n_loop && n3_loop == n_loop ) {
0N/A if( n->in(3)->Opcode() == Op_AddI ) {
0N/A Node *V = n->in(3)->in(1);
0N/A Node *I = n->in(3)->in(2);
0N/A if( is_member(n_loop,get_ctrl(V)) ) {
0N/A } else {
0N/A Node *tmp = V; V = I; I = tmp;
0N/A }
0N/A if( !is_member(n_loop,get_ctrl(I)) ) {
4022N/A Node *add1 = new (C) AddPNode( n->in(1), n->in(2), I );
0N/A // Stuff new AddP in the loop preheader
0N/A register_new_node( add1, n_loop->_head->in(LoopNode::EntryControl) );
4022N/A Node *add2 = new (C) AddPNode( n->in(1), add1, V );
0N/A register_new_node( add2, n_ctrl );
1541N/A _igvn.replace_node( n, add2 );
0N/A return add2;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A return NULL;
0N/A}
0N/A
0N/A//------------------------------conditional_move-------------------------------
0N/A// Attempt to replace a Phi with a conditional move. We have some pretty
0N/A// strict profitability requirements. All Phis at the merge point must
0N/A// be converted, so we can remove the control flow. We need to limit the
0N/A// number of c-moves to a small handful. All code that was in the side-arms
0N/A// of the CFG diamond is now speculatively executed. This code has to be
0N/A// "cheap enough". We are pretty much limited to CFG diamonds that merge
0N/A// 1 or 2 items with a total of 1 or 2 ops executed speculatively.
0N/ANode *PhaseIdealLoop::conditional_move( Node *region ) {
0N/A
2885N/A assert(region->is_Region(), "sanity check");
2885N/A if (region->req() != 3) return NULL;
0N/A
0N/A // Check for CFG diamond
0N/A Node *lp = region->in(1);
0N/A Node *rp = region->in(2);
2885N/A if (!lp || !rp) return NULL;
0N/A Node *lp_c = lp->in(0);
2885N/A if (lp_c == NULL || lp_c != rp->in(0) || !lp_c->is_If()) return NULL;
0N/A IfNode *iff = lp_c->as_If();
0N/A
0N/A // Check for ops pinned in an arm of the diamond.
0N/A // Can't remove the control flow in this case
2885N/A if (lp->outcnt() > 1) return NULL;
2885N/A if (rp->outcnt() > 1) return NULL;
2885N/A
2885N/A IdealLoopTree* r_loop = get_loop(region);
2885N/A assert(r_loop == get_loop(iff), "sanity");
2885N/A // Always convert to CMOVE if all results are used only outside this loop.
2885N/A bool used_inside_loop = (r_loop == _ltree_root);
0N/A
0N/A // Check profitability
0N/A int cost = 0;
35N/A int phis = 0;
0N/A for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) {
0N/A Node *out = region->fast_out(i);
2885N/A if (!out->is_Phi()) continue; // Ignore other control edges, etc
35N/A phis++;
0N/A PhiNode* phi = out->as_Phi();
2885N/A BasicType bt = phi->type()->basic_type();
2885N/A switch (bt) {
2885N/A case T_FLOAT:
2885N/A case T_DOUBLE: {
2885N/A cost += Matcher::float_cmove_cost(); // Could be very expensive
2885N/A break;
2885N/A }
2885N/A case T_LONG: {
2885N/A cost += Matcher::long_cmove_cost(); // May encodes as 2 CMOV's
2885N/A }
0N/A case T_INT: // These all CMOV fine
2885N/A case T_ADDRESS: { // (RawPtr)
0N/A cost++;
0N/A break;
2885N/A }
293N/A case T_NARROWOOP: // Fall through
0N/A case T_OBJECT: { // Base oops are OK, but not derived oops
293N/A const TypeOopPtr *tp = phi->type()->make_ptr()->isa_oopptr();
0N/A // Derived pointers are Bad (tm): what's the Base (for GC purposes) of a
0N/A // CMOVE'd derived pointer? It's a CMOVE'd derived base. Thus
0N/A // CMOVE'ing a derived pointer requires we also CMOVE the base. If we
0N/A // have a Phi for the base here that we convert to a CMOVE all is well
0N/A // and good. But if the base is dead, we'll not make a CMOVE. Later
0N/A // the allocator will have to produce a base by creating a CMOVE of the
0N/A // relevant bases. This puts the allocator in the business of
0N/A // manufacturing expensive instructions, generally a bad plan.
0N/A // Just Say No to Conditionally-Moved Derived Pointers.
2885N/A if (tp && tp->offset() != 0)
0N/A return NULL;
0N/A cost++;
0N/A break;
0N/A }
0N/A default:
0N/A return NULL; // In particular, can't do memory or I/O
0N/A }
0N/A // Add in cost any speculative ops
2885N/A for (uint j = 1; j < region->req(); j++) {
0N/A Node *proj = region->in(j);
0N/A Node *inp = phi->in(j);
0N/A if (get_ctrl(inp) == proj) { // Found local op
0N/A cost++;
0N/A // Check for a chain of dependent ops; these will all become
0N/A // speculative in a CMOV.
2885N/A for (uint k = 1; k < inp->req(); k++)
0N/A if (get_ctrl(inp->in(k)) == proj)
2885N/A cost += ConditionalMoveLimit; // Too much speculative goo
0N/A }
0N/A }
293N/A // See if the Phi is used by a Cmp or Narrow oop Decode/Encode.
293N/A // This will likely Split-If, a higher-payoff operation.
0N/A for (DUIterator_Fast kmax, k = phi->fast_outs(kmax); k < kmax; k++) {
0N/A Node* use = phi->fast_out(k);
2885N/A if (use->is_Cmp() || use->is_DecodeN() || use->is_EncodeP())
2885N/A cost += ConditionalMoveLimit;
2885N/A // Is there a use inside the loop?
2885N/A // Note: check only basic types since CMoveP is pinned.
2885N/A if (!used_inside_loop && is_java_primitive(bt)) {
2885N/A IdealLoopTree* u_loop = get_loop(has_ctrl(use) ? get_ctrl(use) : use);
2885N/A if (r_loop == u_loop || r_loop->is_member(u_loop)) {
2885N/A used_inside_loop = true;
2885N/A }
2885N/A }
0N/A }
0N/A }
35N/A Node* bol = iff->in(1);
2885N/A assert(bol->Opcode() == Op_Bool, "");
35N/A int cmp_op = bol->in(1)->Opcode();
35N/A // It is expensive to generate flags from a float compare.
35N/A // Avoid duplicated float compare.
2885N/A if (phis > 1 && (cmp_op == Op_CmpF || cmp_op == Op_CmpD)) return NULL;
2885N/A
2885N/A float infrequent_prob = PROB_UNLIKELY_MAG(3);
2885N/A // Ignore cost and blocks frequency if CMOVE can be moved outside the loop.
2885N/A if (used_inside_loop) {
2885N/A if (cost >= ConditionalMoveLimit) return NULL; // Too much goo
2885N/A
2885N/A // BlockLayoutByFrequency optimization moves infrequent branch
2885N/A // from hot path. No point in CMOV'ing in such case (110 is used
2885N/A // instead of 100 to take into account not exactness of float value).
2885N/A if (BlockLayoutByFrequency) {
2885N/A infrequent_prob = MAX2(infrequent_prob, (float)BlockLayoutMinDiamondPercentage/110.0f);
2885N/A }
2885N/A }
2885N/A // Check for highly predictable branch. No point in CMOV'ing if
2885N/A // we are going to predict accurately all the time.
2885N/A if (iff->_prob < infrequent_prob ||
2885N/A iff->_prob > (1.0f - infrequent_prob))
2885N/A return NULL;
0N/A
0N/A // --------------
0N/A // Now replace all Phis with CMOV's
0N/A Node *cmov_ctrl = iff->in(0);
0N/A uint flip = (lp->Opcode() == Op_IfTrue);
2885N/A while (1) {
0N/A PhiNode* phi = NULL;
0N/A for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) {
0N/A Node *out = region->fast_out(i);
0N/A if (out->is_Phi()) {
0N/A phi = out->as_Phi();
0N/A break;
0N/A }
0N/A }
0N/A if (phi == NULL) break;
0N/A#ifndef PRODUCT
2885N/A if (PrintOpto && VerifyLoopOptimizations) tty->print_cr("CMOV");
0N/A#endif
0N/A // Move speculative ops
2885N/A for (uint j = 1; j < region->req(); j++) {
0N/A Node *proj = region->in(j);
0N/A Node *inp = phi->in(j);
0N/A if (get_ctrl(inp) == proj) { // Found local op
0N/A#ifndef PRODUCT
2885N/A if (PrintOpto && VerifyLoopOptimizations) {
0N/A tty->print(" speculate: ");
0N/A inp->dump();
0N/A }
0N/A#endif
0N/A set_ctrl(inp, cmov_ctrl);
0N/A }
0N/A }
0N/A Node *cmov = CMoveNode::make( C, cmov_ctrl, iff->in(1), phi->in(1+flip), phi->in(2-flip), _igvn.type(phi) );
0N/A register_new_node( cmov, cmov_ctrl );
1541N/A _igvn.replace_node( phi, cmov );
0N/A#ifndef PRODUCT
2885N/A if (TraceLoopOpts) {
2885N/A tty->print("CMOV ");
2885N/A r_loop->dump_head();
2902N/A if (Verbose) {
2885N/A bol->in(1)->dump(1);
2885N/A cmov->dump(1);
2902N/A }
2885N/A }
2885N/A if (VerifyLoopOptimizations) verify();
0N/A#endif
0N/A }
0N/A
0N/A // The useless CFG diamond will fold up later; see the optimization in
0N/A // RegionNode::Ideal.
0N/A _igvn._worklist.push(region);
0N/A
0N/A return iff->in(1);
0N/A}
0N/A
0N/A//------------------------------split_if_with_blocks_pre-----------------------
0N/A// Do the real work in a non-recursive function. Data nodes want to be
0N/A// cloned in the pre-order so they can feed each other nicely.
0N/ANode *PhaseIdealLoop::split_if_with_blocks_pre( Node *n ) {
0N/A // Cloning these guys is unlikely to win
0N/A int n_op = n->Opcode();
0N/A if( n_op == Op_MergeMem ) return n;
0N/A if( n->is_Proj() ) return n;
0N/A // Do not clone-up CmpFXXX variations, as these are always
0N/A // followed by a CmpI
0N/A if( n->is_Cmp() ) return n;
0N/A // Attempt to use a conditional move instead of a phi/branch
0N/A if( ConditionalMoveLimit > 0 && n_op == Op_Region ) {
0N/A Node *cmov = conditional_move( n );
0N/A if( cmov ) return cmov;
0N/A }
253N/A if( n->is_CFG() || n->is_LoadStore() )
253N/A return n;
0N/A if( n_op == Op_Opaque1 || // Opaque nodes cannot be mod'd
0N/A n_op == Op_Opaque2 ) {
0N/A if( !C->major_progress() ) // If chance of no more loop opts...
0N/A _igvn._worklist.push(n); // maybe we'll remove them
0N/A return n;
0N/A }
0N/A
0N/A if( n->is_Con() ) return n; // No cloning for Con nodes
0N/A
0N/A Node *n_ctrl = get_ctrl(n);
0N/A if( !n_ctrl ) return n; // Dead node
0N/A
0N/A // Attempt to remix address expressions for loop invariants
0N/A Node *m = remix_address_expressions( n );
0N/A if( m ) return m;
0N/A
0N/A // Determine if the Node has inputs from some local Phi.
0N/A // Returns the block to clone thru.
0N/A Node *n_blk = has_local_phi_input( n );
0N/A if( !n_blk ) return n;
0N/A // Do not clone the trip counter through on a CountedLoop
0N/A // (messes up the canonical shape).
0N/A if( n_blk->is_CountedLoop() && n->Opcode() == Op_AddI ) return n;
0N/A
0N/A // Check for having no control input; not pinned. Allow
0N/A // dominating control.
0N/A if( n->in(0) ) {
0N/A Node *dom = idom(n_blk);
0N/A if( dom_lca( n->in(0), dom ) != n->in(0) )
0N/A return n;
0N/A }
0N/A // Policy: when is it profitable. You must get more wins than
0N/A // policy before it is considered profitable. Policy is usually 0,
0N/A // so 1 win is considered profitable. Big merges will require big
0N/A // cloning, so get a larger policy.
0N/A int policy = n_blk->req() >> 2;
0N/A
0N/A // If the loop is a candidate for range check elimination,
0N/A // delay splitting through it's phi until a later loop optimization
0N/A if (n_blk->is_CountedLoop()) {
0N/A IdealLoopTree *lp = get_loop(n_blk);
0N/A if (lp && lp->_rce_candidate) {
0N/A return n;
0N/A }
0N/A }
0N/A
0N/A // Use same limit as split_if_with_blocks_post
0N/A if( C->unique() > 35000 ) return n; // Method too big
0N/A
0N/A // Split 'n' through the merge point if it is profitable
0N/A Node *phi = split_thru_phi( n, n_blk, policy );
2885N/A if (!phi) return n;
0N/A
0N/A // Found a Phi to split thru!
0N/A // Replace 'n' with the new phi
1541N/A _igvn.replace_node( n, phi );
0N/A // Moved a load around the loop, 'en-registering' something.
2885N/A if (n_blk->is_Loop() && n->is_Load() &&
2885N/A !phi->in(LoopNode::LoopBackControl)->is_Load())
0N/A C->set_major_progress();
0N/A
0N/A return phi;
0N/A}
0N/A
0N/Astatic bool merge_point_too_heavy(Compile* C, Node* region) {
0N/A // Bail out if the region and its phis have too many users.
0N/A int weight = 0;
0N/A for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) {
0N/A weight += region->fast_out(i)->outcnt();
0N/A }
4123N/A int nodes_left = MaxNodeLimit - C->live_nodes();
0N/A if (weight * 8 > nodes_left) {
0N/A#ifndef PRODUCT
0N/A if (PrintOpto)
0N/A tty->print_cr("*** Split-if bails out: %d nodes, region weight %d", C->unique(), weight);
0N/A#endif
0N/A return true;
0N/A } else {
0N/A return false;
0N/A }
0N/A}
0N/A
0N/Astatic bool merge_point_safe(Node* region) {
0N/A // 4799512: Stop split_if_with_blocks from splitting a block with a ConvI2LNode
0N/A // having a PhiNode input. This sidesteps the dangerous case where the split
0N/A // ConvI2LNode may become TOP if the input Value() does not
0N/A // overlap the ConvI2L range, leaving a node which may not dominate its
0N/A // uses.
0N/A // A better fix for this problem can be found in the BugTraq entry, but
0N/A // expediency for Mantis demands this hack.
834N/A // 6855164: If the merge point has a FastLockNode with a PhiNode input, we stop
834N/A // split_if_with_blocks from splitting a block because we could not move around
834N/A // the FastLockNode.
0N/A for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) {
0N/A Node* n = region->fast_out(i);
0N/A if (n->is_Phi()) {
0N/A for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) {
0N/A Node* m = n->fast_out(j);
834N/A if (m->is_FastLock())
0N/A return false;
834N/A#ifdef _LP64
834N/A if (m->Opcode() == Op_ConvI2L)
834N/A return false;
834N/A#endif
0N/A }
0N/A }
0N/A }
0N/A return true;
0N/A}
0N/A
0N/A
0N/A//------------------------------place_near_use---------------------------------
0N/A// Place some computation next to use but not inside inner loops.
0N/A// For inner loop uses move it to the preheader area.
0N/ANode *PhaseIdealLoop::place_near_use( Node *useblock ) const {
0N/A IdealLoopTree *u_loop = get_loop( useblock );
0N/A return (u_loop->_irreducible || u_loop->_child)
0N/A ? useblock
0N/A : u_loop->_head->in(LoopNode::EntryControl);
0N/A}
0N/A
0N/A
0N/A//------------------------------split_if_with_blocks_post----------------------
0N/A// Do the real work in a non-recursive function. CFG hackery wants to be
0N/A// in the post-order, so it can dirty the I-DOM info and not use the dirtied
0N/A// info.
0N/Avoid PhaseIdealLoop::split_if_with_blocks_post( Node *n ) {
0N/A
0N/A // Cloning Cmp through Phi's involves the split-if transform.
0N/A // FastLock is not used by an If
0N/A if( n->is_Cmp() && !n->is_FastLock() ) {
0N/A if( C->unique() > 35000 ) return; // Method too big
0N/A
0N/A // Do not do 'split-if' if irreducible loops are present.
0N/A if( _has_irreducible_loops )
0N/A return;
0N/A
0N/A Node *n_ctrl = get_ctrl(n);
0N/A // Determine if the Node has inputs from some local Phi.
0N/A // Returns the block to clone thru.
0N/A Node *n_blk = has_local_phi_input( n );
0N/A if( n_blk != n_ctrl ) return;
0N/A
0N/A if( merge_point_too_heavy(C, n_ctrl) )
0N/A return;
0N/A
0N/A if( n->outcnt() != 1 ) return; // Multiple bool's from 1 compare?
0N/A Node *bol = n->unique_out();
0N/A assert( bol->is_Bool(), "expect a bool here" );
0N/A if( bol->outcnt() != 1 ) return;// Multiple branches from 1 compare?
0N/A Node *iff = bol->unique_out();
0N/A
0N/A // Check some safety conditions
0N/A if( iff->is_If() ) { // Classic split-if?
0N/A if( iff->in(0) != n_ctrl ) return; // Compare must be in same blk as if
0N/A } else if (iff->is_CMove()) { // Trying to split-up a CMOVE
3059N/A // Can't split CMove with different control edge.
3059N/A if (iff->in(0) != NULL && iff->in(0) != n_ctrl ) return;
0N/A if( get_ctrl(iff->in(2)) == n_ctrl ||
0N/A get_ctrl(iff->in(3)) == n_ctrl )
0N/A return; // Inputs not yet split-up
0N/A if ( get_loop(n_ctrl) != get_loop(get_ctrl(iff)) ) {
0N/A return; // Loop-invar test gates loop-varying CMOVE
0N/A }
0N/A } else {
0N/A return; // some other kind of node, such as an Allocate
0N/A }
0N/A
0N/A // Do not do 'split-if' if some paths are dead. First do dead code
0N/A // elimination and then see if its still profitable.
0N/A for( uint i = 1; i < n_ctrl->req(); i++ )
0N/A if( n_ctrl->in(i) == C->top() )
0N/A return;
0N/A
0N/A // When is split-if profitable? Every 'win' on means some control flow
0N/A // goes dead, so it's almost always a win.
0N/A int policy = 0;
0N/A // If trying to do a 'Split-If' at the loop head, it is only
0N/A // profitable if the cmp folds up on BOTH paths. Otherwise we
0N/A // risk peeling a loop forever.
0N/A
0N/A // CNC - Disabled for now. Requires careful handling of loop
0N/A // body selection for the cloned code. Also, make sure we check
0N/A // for any input path not being in the same loop as n_ctrl. For
0N/A // irreducible loops we cannot check for 'n_ctrl->is_Loop()'
0N/A // because the alternative loop entry points won't be converted
0N/A // into LoopNodes.
0N/A IdealLoopTree *n_loop = get_loop(n_ctrl);
0N/A for( uint j = 1; j < n_ctrl->req(); j++ )
0N/A if( get_loop(n_ctrl->in(j)) != n_loop )
0N/A return;
0N/A
0N/A // Check for safety of the merge point.
0N/A if( !merge_point_safe(n_ctrl) ) {
0N/A return;
0N/A }
0N/A
0N/A // Split compare 'n' through the merge point if it is profitable
0N/A Node *phi = split_thru_phi( n, n_ctrl, policy );
0N/A if( !phi ) return;
0N/A
0N/A // Found a Phi to split thru!
0N/A // Replace 'n' with the new phi
1541N/A _igvn.replace_node( n, phi );
0N/A
0N/A // Now split the bool up thru the phi
0N/A Node *bolphi = split_thru_phi( bol, n_ctrl, -1 );
1541N/A _igvn.replace_node( bol, bolphi );
0N/A assert( iff->in(1) == bolphi, "" );
0N/A if( bolphi->Value(&_igvn)->singleton() )
0N/A return;
0N/A
0N/A // Conditional-move? Must split up now
0N/A if( !iff->is_If() ) {
0N/A Node *cmovphi = split_thru_phi( iff, n_ctrl, -1 );
1541N/A _igvn.replace_node( iff, cmovphi );
0N/A return;
0N/A }
0N/A
0N/A // Now split the IF
0N/A do_split_if( iff );
0N/A return;
0N/A }
0N/A
0N/A // Check for an IF ready to split; one that has its
0N/A // condition codes input coming from a Phi at the block start.
0N/A int n_op = n->Opcode();
0N/A
0N/A // Check for an IF being dominated by another IF same test
0N/A if( n_op == Op_If ) {
0N/A Node *bol = n->in(1);
0N/A uint max = bol->outcnt();
0N/A // Check for same test used more than once?
0N/A if( n_op == Op_If && max > 1 && bol->is_Bool() ) {
0N/A // Search up IDOMs to see if this IF is dominated.
0N/A Node *cutoff = get_ctrl(bol);
0N/A
0N/A // Now search up IDOMs till cutoff, looking for a dominating test
0N/A Node *prevdom = n;
0N/A Node *dom = idom(prevdom);
0N/A while( dom != cutoff ) {
0N/A if( dom->req() > 1 && dom->in(1) == bol && prevdom->in(0) == dom ) {
0N/A // Replace the dominated test with an obvious true or false.
0N/A // Place it on the IGVN worklist for later cleanup.
0N/A C->set_major_progress();
2665N/A dominated_by( prevdom, n, false, true );
0N/A#ifndef PRODUCT
0N/A if( VerifyLoopOptimizations ) verify();
0N/A#endif
0N/A return;
0N/A }
0N/A prevdom = dom;
0N/A dom = idom(prevdom);
0N/A }
0N/A }
0N/A }
0N/A
0N/A // See if a shared loop-varying computation has no loop-varying uses.
0N/A // Happens if something is only used for JVM state in uncommon trap exits,
0N/A // like various versions of induction variable+offset. Clone the
0N/A // computation per usage to allow it to sink out of the loop.
0N/A if (has_ctrl(n) && !n->in(0)) {// n not dead and has no control edge (can float about)
0N/A Node *n_ctrl = get_ctrl(n);
0N/A IdealLoopTree *n_loop = get_loop(n_ctrl);
0N/A if( n_loop != _ltree_root ) {
0N/A DUIterator_Fast imax, i = n->fast_outs(imax);
0N/A for (; i < imax; i++) {
0N/A Node* u = n->fast_out(i);
0N/A if( !has_ctrl(u) ) break; // Found control user
0N/A IdealLoopTree *u_loop = get_loop(get_ctrl(u));
0N/A if( u_loop == n_loop ) break; // Found loop-varying use
0N/A if( n_loop->is_member( u_loop ) ) break; // Found use in inner loop
0N/A if( u->Opcode() == Op_Opaque1 ) break; // Found loop limit, bugfix for 4677003
0N/A }
0N/A bool did_break = (i < imax); // Did we break out of the previous loop?
0N/A if (!did_break && n->outcnt() > 1) { // All uses in outer loops!
3059N/A Node *late_load_ctrl = NULL;
0N/A if (n->is_Load()) {
0N/A // If n is a load, get and save the result from get_late_ctrl(),
0N/A // to be later used in calculating the control for n's clones.
0N/A clear_dom_lca_tags();
0N/A late_load_ctrl = get_late_ctrl(n, n_ctrl);
0N/A }
0N/A // If n is a load, and the late control is the same as the current
0N/A // control, then the cloning of n is a pointless exercise, because
0N/A // GVN will ensure that we end up where we started.
0N/A if (!n->is_Load() || late_load_ctrl != n_ctrl) {
0N/A for (DUIterator_Last jmin, j = n->last_outs(jmin); j >= jmin; ) {
0N/A Node *u = n->last_out(j); // Clone private computation per use
3811N/A _igvn.rehash_node_delayed(u);
0N/A Node *x = n->clone(); // Clone computation
0N/A Node *x_ctrl = NULL;
0N/A if( u->is_Phi() ) {
0N/A // Replace all uses of normal nodes. Replace Phi uses
605N/A // individually, so the separate Nodes can sink down
0N/A // different paths.
0N/A uint k = 1;
0N/A while( u->in(k) != n ) k++;
0N/A u->set_req( k, x );
0N/A // x goes next to Phi input path
0N/A x_ctrl = u->in(0)->in(k);
0N/A --j;
0N/A } else { // Normal use
0N/A // Replace all uses
0N/A for( uint k = 0; k < u->req(); k++ ) {
0N/A if( u->in(k) == n ) {
0N/A u->set_req( k, x );
0N/A --j;
0N/A }
0N/A }
0N/A x_ctrl = get_ctrl(u);
0N/A }
0N/A
0N/A // Find control for 'x' next to use but not inside inner loops.
0N/A // For inner loop uses get the preheader area.
0N/A x_ctrl = place_near_use(x_ctrl);
0N/A
0N/A if (n->is_Load()) {
0N/A // For loads, add a control edge to a CFG node outside of the loop
0N/A // to force them to not combine and return back inside the loop
0N/A // during GVN optimization (4641526).
0N/A //
0N/A // Because we are setting the actual control input, factor in
0N/A // the result from get_late_ctrl() so we respect any
0N/A // anti-dependences. (6233005).
0N/A x_ctrl = dom_lca(late_load_ctrl, x_ctrl);
0N/A
0N/A // Don't allow the control input to be a CFG splitting node.
0N/A // Such nodes should only have ProjNodes as outs, e.g. IfNode
0N/A // should only have IfTrueNode and IfFalseNode (4985384).
0N/A x_ctrl = find_non_split_ctrl(x_ctrl);
0N/A assert(dom_depth(n_ctrl) <= dom_depth(x_ctrl), "n is later than its clone");
0N/A
0N/A x->set_req(0, x_ctrl);
0N/A }
0N/A register_new_node(x, x_ctrl);
0N/A
0N/A // Some institutional knowledge is needed here: 'x' is
0N/A // yanked because if the optimizer runs GVN on it all the
0N/A // cloned x's will common up and undo this optimization and
0N/A // be forced back in the loop. This is annoying because it
0N/A // makes +VerifyOpto report false-positives on progress. I
0N/A // tried setting control edges on the x's to force them to
0N/A // not combine, but the matching gets worried when it tries
0N/A // to fold a StoreP and an AddP together (as part of an
0N/A // address expression) and the AddP and StoreP have
0N/A // different controls.
318N/A if( !x->is_Load() && !x->is_DecodeN() ) _igvn._worklist.yank(x);
0N/A }
0N/A _igvn.remove_dead_node(n);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A // Check for Opaque2's who's loop has disappeared - who's input is in the
0N/A // same loop nest as their output. Remove 'em, they are no longer useful.
0N/A if( n_op == Op_Opaque2 &&
0N/A n->in(1) != NULL &&
0N/A get_loop(get_ctrl(n)) == get_loop(get_ctrl(n->in(1))) ) {
1541N/A _igvn.replace_node( n, n->in(1) );
0N/A }
0N/A}
0N/A
0N/A//------------------------------split_if_with_blocks---------------------------
0N/A// Check for aggressive application of 'split-if' optimization,
0N/A// using basic block level info.
0N/Avoid PhaseIdealLoop::split_if_with_blocks( VectorSet &visited, Node_Stack &nstack ) {
0N/A Node *n = C->root();
0N/A visited.set(n->_idx); // first, mark node as visited
0N/A // Do pre-visit work for root
0N/A n = split_if_with_blocks_pre( n );
0N/A uint cnt = n->outcnt();
0N/A uint i = 0;
0N/A while (true) {
0N/A // Visit all children
0N/A if (i < cnt) {
0N/A Node* use = n->raw_out(i);
0N/A ++i;
0N/A if (use->outcnt() != 0 && !visited.test_set(use->_idx)) {
0N/A // Now do pre-visit work for this use
0N/A use = split_if_with_blocks_pre( use );
0N/A nstack.push(n, i); // Save parent and next use's index.
0N/A n = use; // Process all children of current use.
0N/A cnt = use->outcnt();
0N/A i = 0;
0N/A }
0N/A }
0N/A else {
0N/A // All of n's children have been processed, complete post-processing.
0N/A if (cnt != 0 && !n->is_Con()) {
0N/A assert(has_node(n), "no dead nodes");
0N/A split_if_with_blocks_post( n );
0N/A }
0N/A if (nstack.is_empty()) {
0N/A // Finished all nodes on stack.
0N/A break;
0N/A }
0N/A // Get saved parent node and next use's index. Visit the rest of uses.
0N/A n = nstack.node();
0N/A cnt = n->outcnt();
0N/A i = nstack.index();
0N/A nstack.pop();
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A//=============================================================================
0N/A//
0N/A// C L O N E A L O O P B O D Y
0N/A//
0N/A
0N/A//------------------------------clone_iff--------------------------------------
0N/A// Passed in a Phi merging (recursively) some nearly equivalent Bool/Cmps.
0N/A// "Nearly" because all Nodes have been cloned from the original in the loop,
0N/A// but the fall-in edges to the Cmp are different. Clone bool/Cmp pairs
0N/A// through the Phi recursively, and return a Bool.
0N/ABoolNode *PhaseIdealLoop::clone_iff( PhiNode *phi, IdealLoopTree *loop ) {
0N/A
0N/A // Convert this Phi into a Phi merging Bools
0N/A uint i;
0N/A for( i = 1; i < phi->req(); i++ ) {
0N/A Node *b = phi->in(i);
0N/A if( b->is_Phi() ) {
3811N/A _igvn.replace_input_of(phi, i, clone_iff( b->as_Phi(), loop ));
0N/A } else {
0N/A assert( b->is_Bool(), "" );
0N/A }
0N/A }
0N/A
0N/A Node *sample_bool = phi->in(1);
0N/A Node *sample_cmp = sample_bool->in(1);
0N/A
0N/A // Make Phis to merge the Cmp's inputs.
4022N/A PhiNode *phi1 = new (C) PhiNode( phi->in(0), Type::TOP );
4022N/A PhiNode *phi2 = new (C) PhiNode( phi->in(0), Type::TOP );
0N/A for( i = 1; i < phi->req(); i++ ) {
0N/A Node *n1 = phi->in(i)->in(1)->in(1);
0N/A Node *n2 = phi->in(i)->in(1)->in(2);
0N/A phi1->set_req( i, n1 );
0N/A phi2->set_req( i, n2 );
0N/A phi1->set_type( phi1->type()->meet(n1->bottom_type()) );
0N/A phi2->set_type( phi2->type()->meet(n2->bottom_type()) );
0N/A }
0N/A // See if these Phis have been made before.
0N/A // Register with optimizer
0N/A Node *hit1 = _igvn.hash_find_insert(phi1);
0N/A if( hit1 ) { // Hit, toss just made Phi
0N/A _igvn.remove_dead_node(phi1); // Remove new phi
0N/A assert( hit1->is_Phi(), "" );
0N/A phi1 = (PhiNode*)hit1; // Use existing phi
0N/A } else { // Miss
0N/A _igvn.register_new_node_with_optimizer(phi1);
0N/A }
0N/A Node *hit2 = _igvn.hash_find_insert(phi2);
0N/A if( hit2 ) { // Hit, toss just made Phi
0N/A _igvn.remove_dead_node(phi2); // Remove new phi
0N/A assert( hit2->is_Phi(), "" );
0N/A phi2 = (PhiNode*)hit2; // Use existing phi
0N/A } else { // Miss
0N/A _igvn.register_new_node_with_optimizer(phi2);
0N/A }
0N/A // Register Phis with loop/block info
0N/A set_ctrl(phi1, phi->in(0));
0N/A set_ctrl(phi2, phi->in(0));
0N/A // Make a new Cmp
0N/A Node *cmp = sample_cmp->clone();
0N/A cmp->set_req( 1, phi1 );
0N/A cmp->set_req( 2, phi2 );
0N/A _igvn.register_new_node_with_optimizer(cmp);
0N/A set_ctrl(cmp, phi->in(0));
0N/A
0N/A // Make a new Bool
0N/A Node *b = sample_bool->clone();
0N/A b->set_req(1,cmp);
0N/A _igvn.register_new_node_with_optimizer(b);
0N/A set_ctrl(b, phi->in(0));
0N/A
0N/A assert( b->is_Bool(), "" );
0N/A return (BoolNode*)b;
0N/A}
0N/A
0N/A//------------------------------clone_bool-------------------------------------
0N/A// Passed in a Phi merging (recursively) some nearly equivalent Bool/Cmps.
0N/A// "Nearly" because all Nodes have been cloned from the original in the loop,
0N/A// but the fall-in edges to the Cmp are different. Clone bool/Cmp pairs
0N/A// through the Phi recursively, and return a Bool.
0N/ACmpNode *PhaseIdealLoop::clone_bool( PhiNode *phi, IdealLoopTree *loop ) {
0N/A uint i;
0N/A // Convert this Phi into a Phi merging Bools
0N/A for( i = 1; i < phi->req(); i++ ) {
0N/A Node *b = phi->in(i);
0N/A if( b->is_Phi() ) {
3811N/A _igvn.replace_input_of(phi, i, clone_bool( b->as_Phi(), loop ));
0N/A } else {
0N/A assert( b->is_Cmp() || b->is_top(), "inputs are all Cmp or TOP" );
0N/A }
0N/A }
0N/A
0N/A Node *sample_cmp = phi->in(1);
0N/A
0N/A // Make Phis to merge the Cmp's inputs.
4022N/A PhiNode *phi1 = new (C) PhiNode( phi->in(0), Type::TOP );
4022N/A PhiNode *phi2 = new (C) PhiNode( phi->in(0), Type::TOP );
0N/A for( uint j = 1; j < phi->req(); j++ ) {
0N/A Node *cmp_top = phi->in(j); // Inputs are all Cmp or TOP
0N/A Node *n1, *n2;
0N/A if( cmp_top->is_Cmp() ) {
0N/A n1 = cmp_top->in(1);
0N/A n2 = cmp_top->in(2);
0N/A } else {
0N/A n1 = n2 = cmp_top;
0N/A }
0N/A phi1->set_req( j, n1 );
0N/A phi2->set_req( j, n2 );
0N/A phi1->set_type( phi1->type()->meet(n1->bottom_type()) );
0N/A phi2->set_type( phi2->type()->meet(n2->bottom_type()) );
0N/A }
0N/A
0N/A // See if these Phis have been made before.
0N/A // Register with optimizer
0N/A Node *hit1 = _igvn.hash_find_insert(phi1);
0N/A if( hit1 ) { // Hit, toss just made Phi
0N/A _igvn.remove_dead_node(phi1); // Remove new phi
0N/A assert( hit1->is_Phi(), "" );
0N/A phi1 = (PhiNode*)hit1; // Use existing phi
0N/A } else { // Miss
0N/A _igvn.register_new_node_with_optimizer(phi1);
0N/A }
0N/A Node *hit2 = _igvn.hash_find_insert(phi2);
0N/A if( hit2 ) { // Hit, toss just made Phi
0N/A _igvn.remove_dead_node(phi2); // Remove new phi
0N/A assert( hit2->is_Phi(), "" );
0N/A phi2 = (PhiNode*)hit2; // Use existing phi
0N/A } else { // Miss
0N/A _igvn.register_new_node_with_optimizer(phi2);
0N/A }
0N/A // Register Phis with loop/block info
0N/A set_ctrl(phi1, phi->in(0));
0N/A set_ctrl(phi2, phi->in(0));
0N/A // Make a new Cmp
0N/A Node *cmp = sample_cmp->clone();
0N/A cmp->set_req( 1, phi1 );
0N/A cmp->set_req( 2, phi2 );
0N/A _igvn.register_new_node_with_optimizer(cmp);
0N/A set_ctrl(cmp, phi->in(0));
0N/A
0N/A assert( cmp->is_Cmp(), "" );
0N/A return (CmpNode*)cmp;
0N/A}
0N/A
0N/A//------------------------------sink_use---------------------------------------
0N/A// If 'use' was in the loop-exit block, it now needs to be sunk
0N/A// below the post-loop merge point.
0N/Avoid PhaseIdealLoop::sink_use( Node *use, Node *post_loop ) {
0N/A if (!use->is_CFG() && get_ctrl(use) == post_loop->in(2)) {
0N/A set_ctrl(use, post_loop);
0N/A for (DUIterator j = use->outs(); use->has_out(j); j++)
0N/A sink_use(use->out(j), post_loop);
0N/A }
0N/A}
0N/A
0N/A//------------------------------clone_loop-------------------------------------
0N/A//
0N/A// C L O N E A L O O P B O D Y
0N/A//
0N/A// This is the basic building block of the loop optimizations. It clones an
0N/A// entire loop body. It makes an old_new loop body mapping; with this mapping
0N/A// you can find the new-loop equivalent to an old-loop node. All new-loop
0N/A// nodes are exactly equal to their old-loop counterparts, all edges are the
0N/A// same. All exits from the old-loop now have a RegionNode that merges the
0N/A// equivalent new-loop path. This is true even for the normal "loop-exit"
0N/A// condition. All uses of loop-invariant old-loop values now come from (one
0N/A// or more) Phis that merge their new-loop equivalents.
0N/A//
0N/A// This operation leaves the graph in an illegal state: there are two valid
0N/A// control edges coming from the loop pre-header to both loop bodies. I'll
0N/A// definitely have to hack the graph after running this transform.
0N/A//
0N/A// From this building block I will further edit edges to perform loop peeling
0N/A// or loop unrolling or iteration splitting (Range-Check-Elimination), etc.
0N/A//
0N/A// Parameter side_by_size_idom:
0N/A// When side_by_size_idom is NULL, the dominator tree is constructed for
0N/A// the clone loop to dominate the original. Used in construction of
0N/A// pre-main-post loop sequence.
0N/A// When nonnull, the clone and original are side-by-side, both are
0N/A// dominated by the side_by_side_idom node. Used in construction of
0N/A// unswitched loops.
0N/Avoid PhaseIdealLoop::clone_loop( IdealLoopTree *loop, Node_List &old_new, int dd,
0N/A Node* side_by_side_idom) {
0N/A
0N/A // Step 1: Clone the loop body. Make the old->new mapping.
0N/A uint i;
0N/A for( i = 0; i < loop->_body.size(); i++ ) {
0N/A Node *old = loop->_body.at(i);
0N/A Node *nnn = old->clone();
0N/A old_new.map( old->_idx, nnn );
0N/A _igvn.register_new_node_with_optimizer(nnn);
0N/A }
0N/A
0N/A
0N/A // Step 2: Fix the edges in the new body. If the old input is outside the
0N/A // loop use it. If the old input is INside the loop, use the corresponding
0N/A // new node instead.
0N/A for( i = 0; i < loop->_body.size(); i++ ) {
0N/A Node *old = loop->_body.at(i);
0N/A Node *nnn = old_new[old->_idx];
0N/A // Fix CFG/Loop controlling the new node
0N/A if (has_ctrl(old)) {
0N/A set_ctrl(nnn, old_new[get_ctrl(old)->_idx]);
0N/A } else {
0N/A set_loop(nnn, loop->_parent);
0N/A if (old->outcnt() > 0) {
0N/A set_idom( nnn, old_new[idom(old)->_idx], dd );
0N/A }
0N/A }
0N/A // Correct edges to the new node
0N/A for( uint j = 0; j < nnn->req(); j++ ) {
0N/A Node *n = nnn->in(j);
0N/A if( n ) {
0N/A IdealLoopTree *old_in_loop = get_loop( has_ctrl(n) ? get_ctrl(n) : n );
0N/A if( loop->is_member( old_in_loop ) )
0N/A nnn->set_req(j, old_new[n->_idx]);
0N/A }
0N/A }
0N/A _igvn.hash_find_insert(nnn);
0N/A }
0N/A Node *newhead = old_new[loop->_head->_idx];
0N/A set_idom(newhead, newhead->in(LoopNode::EntryControl), dd);
0N/A
0N/A
0N/A // Step 3: Now fix control uses. Loop varying control uses have already
0N/A // been fixed up (as part of all input edges in Step 2). Loop invariant
0N/A // control uses must be either an IfFalse or an IfTrue. Make a merge
0N/A // point to merge the old and new IfFalse/IfTrue nodes; make the use
0N/A // refer to this.
0N/A ResourceArea *area = Thread::current()->resource_area();
0N/A Node_List worklist(area);
0N/A uint new_counter = C->unique();
0N/A for( i = 0; i < loop->_body.size(); i++ ) {
0N/A Node* old = loop->_body.at(i);
0N/A if( !old->is_CFG() ) continue;
0N/A Node* nnn = old_new[old->_idx];
0N/A
0N/A // Copy uses to a worklist, so I can munge the def-use info
0N/A // with impunity.
0N/A for (DUIterator_Fast jmax, j = old->fast_outs(jmax); j < jmax; j++)
0N/A worklist.push(old->fast_out(j));
0N/A
0N/A while( worklist.size() ) { // Visit all uses
0N/A Node *use = worklist.pop();
0N/A if (!has_node(use)) continue; // Ignore dead nodes
0N/A IdealLoopTree *use_loop = get_loop( has_ctrl(use) ? get_ctrl(use) : use );
0N/A if( !loop->is_member( use_loop ) && use->is_CFG() ) {
0N/A // Both OLD and USE are CFG nodes here.
0N/A assert( use->is_Proj(), "" );
0N/A
0N/A // Clone the loop exit control projection
0N/A Node *newuse = use->clone();
0N/A newuse->set_req(0,nnn);
0N/A _igvn.register_new_node_with_optimizer(newuse);
0N/A set_loop(newuse, use_loop);
0N/A set_idom(newuse, nnn, dom_depth(nnn) + 1 );
0N/A
0N/A // We need a Region to merge the exit from the peeled body and the
0N/A // exit from the old loop body.
4022N/A RegionNode *r = new (C) RegionNode(3);
0N/A // Map the old use to the new merge point
0N/A old_new.map( use->_idx, r );
0N/A uint dd_r = MIN2(dom_depth(newuse),dom_depth(use));
0N/A assert( dd_r >= dom_depth(dom_lca(newuse,use)), "" );
0N/A
0N/A // The original user of 'use' uses 'r' instead.
0N/A for (DUIterator_Last lmin, l = use->last_outs(lmin); l >= lmin;) {
0N/A Node* useuse = use->last_out(l);
3811N/A _igvn.rehash_node_delayed(useuse);
0N/A uint uses_found = 0;
0N/A if( useuse->in(0) == use ) {
0N/A useuse->set_req(0, r);
0N/A uses_found++;
0N/A if( useuse->is_CFG() ) {
0N/A assert( dom_depth(useuse) > dd_r, "" );
0N/A set_idom(useuse, r, dom_depth(useuse));
0N/A }
0N/A }
0N/A for( uint k = 1; k < useuse->req(); k++ ) {
0N/A if( useuse->in(k) == use ) {
0N/A useuse->set_req(k, r);
0N/A uses_found++;
0N/A }
0N/A }
0N/A l -= uses_found; // we deleted 1 or more copies of this edge
0N/A }
0N/A
0N/A // Now finish up 'r'
0N/A r->set_req( 1, newuse );
0N/A r->set_req( 2, use );
0N/A _igvn.register_new_node_with_optimizer(r);
0N/A set_loop(r, use_loop);
0N/A set_idom(r, !side_by_side_idom ? newuse->in(0) : side_by_side_idom, dd_r);
0N/A } // End of if a loop-exit test
0N/A }
0N/A }
0N/A
0N/A // Step 4: If loop-invariant use is not control, it must be dominated by a
0N/A // loop exit IfFalse/IfTrue. Find "proper" loop exit. Make a Region
0N/A // there if needed. Make a Phi there merging old and new used values.
0N/A Node_List *split_if_set = NULL;
0N/A Node_List *split_bool_set = NULL;
0N/A Node_List *split_cex_set = NULL;
0N/A for( i = 0; i < loop->_body.size(); i++ ) {
0N/A Node* old = loop->_body.at(i);
0N/A Node* nnn = old_new[old->_idx];
0N/A // Copy uses to a worklist, so I can munge the def-use info
0N/A // with impunity.
0N/A for (DUIterator_Fast jmax, j = old->fast_outs(jmax); j < jmax; j++)
0N/A worklist.push(old->fast_out(j));
0N/A
0N/A while( worklist.size() ) {
0N/A Node *use = worklist.pop();
0N/A if (!has_node(use)) continue; // Ignore dead nodes
0N/A if (use->in(0) == C->top()) continue;
0N/A IdealLoopTree *use_loop = get_loop( has_ctrl(use) ? get_ctrl(use) : use );
0N/A // Check for data-use outside of loop - at least one of OLD or USE
0N/A // must not be a CFG node.
0N/A if( !loop->is_member( use_loop ) && (!old->is_CFG() || !use->is_CFG())) {
0N/A
0N/A // If the Data use is an IF, that means we have an IF outside of the
0N/A // loop that is switching on a condition that is set inside of the
0N/A // loop. Happens if people set a loop-exit flag; then test the flag
0N/A // in the loop to break the loop, then test is again outside of the
0N/A // loop to determine which way the loop exited.
0N/A if( use->is_If() || use->is_CMove() ) {
0N/A // Since this code is highly unlikely, we lazily build the worklist
0N/A // of such Nodes to go split.
0N/A if( !split_if_set )
0N/A split_if_set = new Node_List(area);
0N/A split_if_set->push(use);
0N/A }
0N/A if( use->is_Bool() ) {
0N/A if( !split_bool_set )
0N/A split_bool_set = new Node_List(area);
0N/A split_bool_set->push(use);
0N/A }
0N/A if( use->Opcode() == Op_CreateEx ) {
0N/A if( !split_cex_set )
0N/A split_cex_set = new Node_List(area);
0N/A split_cex_set->push(use);
0N/A }
0N/A
0N/A
0N/A // Get "block" use is in
0N/A uint idx = 0;
0N/A while( use->in(idx) != old ) idx++;
0N/A Node *prev = use->is_CFG() ? use : get_ctrl(use);
0N/A assert( !loop->is_member( get_loop( prev ) ), "" );
0N/A Node *cfg = prev->_idx >= new_counter
0N/A ? prev->in(2)
0N/A : idom(prev);
0N/A if( use->is_Phi() ) // Phi use is in prior block
0N/A cfg = prev->in(idx); // NOT in block of Phi itself
0N/A if (cfg->is_top()) { // Use is dead?
3811N/A _igvn.replace_input_of(use, idx, C->top());
0N/A continue;
0N/A }
0N/A
0N/A while( !loop->is_member( get_loop( cfg ) ) ) {
0N/A prev = cfg;
0N/A cfg = cfg->_idx >= new_counter ? cfg->in(2) : idom(cfg);
0N/A }
0N/A // If the use occurs after merging several exits from the loop, then
0N/A // old value must have dominated all those exits. Since the same old
0N/A // value was used on all those exits we did not need a Phi at this
0N/A // merge point. NOW we do need a Phi here. Each loop exit value
0N/A // is now merged with the peeled body exit; each exit gets its own
0N/A // private Phi and those Phis need to be merged here.
0N/A Node *phi;
0N/A if( prev->is_Region() ) {
0N/A if( idx == 0 ) { // Updating control edge?
0N/A phi = prev; // Just use existing control
0N/A } else { // Else need a new Phi
0N/A phi = PhiNode::make( prev, old );
0N/A // Now recursively fix up the new uses of old!
0N/A for( uint i = 1; i < prev->req(); i++ ) {
0N/A worklist.push(phi); // Onto worklist once for each 'old' input
0N/A }
0N/A }
0N/A } else {
0N/A // Get new RegionNode merging old and new loop exits
0N/A prev = old_new[prev->_idx];
0N/A assert( prev, "just made this in step 7" );
0N/A if( idx == 0 ) { // Updating control edge?
0N/A phi = prev; // Just use existing control
0N/A } else { // Else need a new Phi
0N/A // Make a new Phi merging data values properly
0N/A phi = PhiNode::make( prev, old );
0N/A phi->set_req( 1, nnn );
0N/A }
0N/A }
0N/A // If inserting a new Phi, check for prior hits
0N/A if( idx != 0 ) {
0N/A Node *hit = _igvn.hash_find_insert(phi);
0N/A if( hit == NULL ) {
0N/A _igvn.register_new_node_with_optimizer(phi); // Register new phi
0N/A } else { // or
0N/A // Remove the new phi from the graph and use the hit
0N/A _igvn.remove_dead_node(phi);
0N/A phi = hit; // Use existing phi
0N/A }
0N/A set_ctrl(phi, prev);
0N/A }
0N/A // Make 'use' use the Phi instead of the old loop body exit value
3811N/A _igvn.replace_input_of(use, idx, phi);
0N/A if( use->_idx >= new_counter ) { // If updating new phis
0N/A // Not needed for correctness, but prevents a weak assert
0N/A // in AddPNode from tripping (when we end up with different
0N/A // base & derived Phis that will become the same after
0N/A // IGVN does CSE).
0N/A Node *hit = _igvn.hash_find_insert(use);
0N/A if( hit ) // Go ahead and re-hash for hits.
1541N/A _igvn.replace_node( use, hit );
0N/A }
0N/A
0N/A // If 'use' was in the loop-exit block, it now needs to be sunk
0N/A // below the post-loop merge point.
0N/A sink_use( use, prev );
0N/A }
0N/A }
0N/A }
0N/A
0N/A // Check for IFs that need splitting/cloning. Happens if an IF outside of
0N/A // the loop uses a condition set in the loop. The original IF probably
0N/A // takes control from one or more OLD Regions (which in turn get from NEW
0N/A // Regions). In any case, there will be a set of Phis for each merge point
0N/A // from the IF up to where the original BOOL def exists the loop.
0N/A if( split_if_set ) {
0N/A while( split_if_set->size() ) {
0N/A Node *iff = split_if_set->pop();
0N/A if( iff->in(1)->is_Phi() ) {
0N/A BoolNode *b = clone_iff( iff->in(1)->as_Phi(), loop );
3811N/A _igvn.replace_input_of(iff, 1, b);
0N/A }
0N/A }
0N/A }
0N/A if( split_bool_set ) {
0N/A while( split_bool_set->size() ) {
0N/A Node *b = split_bool_set->pop();
0N/A Node *phi = b->in(1);
0N/A assert( phi->is_Phi(), "" );
0N/A CmpNode *cmp = clone_bool( (PhiNode*)phi, loop );
3811N/A _igvn.replace_input_of(b, 1, cmp);
0N/A }
0N/A }
0N/A if( split_cex_set ) {
0N/A while( split_cex_set->size() ) {
0N/A Node *b = split_cex_set->pop();
0N/A assert( b->in(0)->is_Region(), "" );
0N/A assert( b->in(1)->is_Phi(), "" );
0N/A assert( b->in(0)->in(0) == b->in(1)->in(0), "" );
0N/A split_up( b, b->in(0), NULL );
0N/A }
0N/A }
0N/A
0N/A}
0N/A
0N/A
0N/A//---------------------- stride_of_possible_iv -------------------------------------
0N/A// Looks for an iff/bool/comp with one operand of the compare
0N/A// being a cycle involving an add and a phi,
0N/A// with an optional truncation (left-shift followed by a right-shift)
0N/A// of the add. Returns zero if not an iv.
0N/Aint PhaseIdealLoop::stride_of_possible_iv(Node* iff) {
0N/A Node* trunc1 = NULL;
0N/A Node* trunc2 = NULL;
0N/A const TypeInt* ttype = NULL;
0N/A if (!iff->is_If() || iff->in(1) == NULL || !iff->in(1)->is_Bool()) {
0N/A return 0;
0N/A }
0N/A BoolNode* bl = iff->in(1)->as_Bool();
0N/A Node* cmp = bl->in(1);
0N/A if (!cmp || cmp->Opcode() != Op_CmpI && cmp->Opcode() != Op_CmpU) {
0N/A return 0;
0N/A }
0N/A // Must have an invariant operand
0N/A if (is_member(get_loop(iff), get_ctrl(cmp->in(2)))) {
0N/A return 0;
0N/A }
0N/A Node* add2 = NULL;
0N/A Node* cmp1 = cmp->in(1);
0N/A if (cmp1->is_Phi()) {
0N/A // (If (Bool (CmpX phi:(Phi ...(Optional-trunc(AddI phi add2))) )))
0N/A Node* phi = cmp1;
0N/A for (uint i = 1; i < phi->req(); i++) {
0N/A Node* in = phi->in(i);
0N/A Node* add = CountedLoopNode::match_incr_with_optional_truncation(in,
0N/A &trunc1, &trunc2, &ttype);
0N/A if (add && add->in(1) == phi) {
0N/A add2 = add->in(2);
0N/A break;
0N/A }
0N/A }
0N/A } else {
0N/A // (If (Bool (CmpX addtrunc:(Optional-trunc((AddI (Phi ...addtrunc...) add2)) )))
0N/A Node* addtrunc = cmp1;
0N/A Node* add = CountedLoopNode::match_incr_with_optional_truncation(addtrunc,
0N/A &trunc1, &trunc2, &ttype);
0N/A if (add && add->in(1)->is_Phi()) {
0N/A Node* phi = add->in(1);
0N/A for (uint i = 1; i < phi->req(); i++) {
0N/A if (phi->in(i) == addtrunc) {
0N/A add2 = add->in(2);
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A if (add2 != NULL) {
0N/A const TypeInt* add2t = _igvn.type(add2)->is_int();
0N/A if (add2t->is_con()) {
0N/A return add2t->get_con();
0N/A }
0N/A }
0N/A return 0;
0N/A}
0N/A
0N/A
0N/A//---------------------- stay_in_loop -------------------------------------
0N/A// Return the (unique) control output node that's in the loop (if it exists.)
0N/ANode* PhaseIdealLoop::stay_in_loop( Node* n, IdealLoopTree *loop) {
0N/A Node* unique = NULL;
0N/A if (!n) return NULL;
0N/A for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
0N/A Node* use = n->fast_out(i);
0N/A if (!has_ctrl(use) && loop->is_member(get_loop(use))) {
0N/A if (unique != NULL) {
0N/A return NULL;
0N/A }
0N/A unique = use;
0N/A }
0N/A }
0N/A return unique;
0N/A}
0N/A
0N/A//------------------------------ register_node -------------------------------------
0N/A// Utility to register node "n" with PhaseIdealLoop
0N/Avoid PhaseIdealLoop::register_node(Node* n, IdealLoopTree *loop, Node* pred, int ddepth) {
0N/A _igvn.register_new_node_with_optimizer(n);
0N/A loop->_body.push(n);
0N/A if (n->is_CFG()) {
0N/A set_loop(n, loop);
0N/A set_idom(n, pred, ddepth);
0N/A } else {
0N/A set_ctrl(n, pred);
0N/A }
0N/A}
0N/A
0N/A//------------------------------ proj_clone -------------------------------------
0N/A// Utility to create an if-projection
0N/AProjNode* PhaseIdealLoop::proj_clone(ProjNode* p, IfNode* iff) {
0N/A ProjNode* c = p->clone()->as_Proj();
0N/A c->set_req(0, iff);
0N/A return c;
0N/A}
0N/A
0N/A//------------------------------ short_circuit_if -------------------------------------
0N/A// Force the iff control output to be the live_proj
0N/ANode* PhaseIdealLoop::short_circuit_if(IfNode* iff, ProjNode* live_proj) {
0N/A int proj_con = live_proj->_con;
0N/A assert(proj_con == 0 || proj_con == 1, "false or true projection");
0N/A Node *con = _igvn.intcon(proj_con);
0N/A set_ctrl(con, C->root());
0N/A if (iff) {
0N/A iff->set_req(1, con);
0N/A }
0N/A return con;
0N/A}
0N/A
0N/A//------------------------------ insert_if_before_proj -------------------------------------
0N/A// Insert a new if before an if projection (* - new node)
0N/A//
0N/A// before
0N/A// if(test)
0N/A// / \
0N/A// v v
0N/A// other-proj proj (arg)
0N/A//
0N/A// after
0N/A// if(test)
0N/A// / \
0N/A// / v
0N/A// | * proj-clone
0N/A// v |
0N/A// other-proj v
0N/A// * new_if(relop(cmp[IU](left,right)))
0N/A// / \
0N/A// v v
0N/A// * new-proj proj
0N/A// (returned)
0N/A//
0N/AProjNode* PhaseIdealLoop::insert_if_before_proj(Node* left, bool Signed, BoolTest::mask relop, Node* right, ProjNode* proj) {
0N/A IfNode* iff = proj->in(0)->as_If();
0N/A IdealLoopTree *loop = get_loop(proj);
0N/A ProjNode *other_proj = iff->proj_out(!proj->is_IfTrue())->as_Proj();
0N/A int ddepth = dom_depth(proj);
0N/A
3811N/A _igvn.rehash_node_delayed(iff);
3811N/A _igvn.rehash_node_delayed(proj);
0N/A
0N/A proj->set_req(0, NULL); // temporary disconnect
0N/A ProjNode* proj2 = proj_clone(proj, iff);
0N/A register_node(proj2, loop, iff, ddepth);
0N/A
4022N/A Node* cmp = Signed ? (Node*) new (C)CmpINode(left, right) : (Node*) new (C)CmpUNode(left, right);
0N/A register_node(cmp, loop, proj2, ddepth);
0N/A
4022N/A BoolNode* bol = new (C)BoolNode(cmp, relop);
0N/A register_node(bol, loop, proj2, ddepth);
0N/A
4022N/A IfNode* new_if = new (C)IfNode(proj2, bol, iff->_prob, iff->_fcnt);
0N/A register_node(new_if, loop, proj2, ddepth);
0N/A
0N/A proj->set_req(0, new_if); // reattach
0N/A set_idom(proj, new_if, ddepth);
0N/A
0N/A ProjNode* new_exit = proj_clone(other_proj, new_if)->as_Proj();
0N/A register_node(new_exit, get_loop(other_proj), new_if, ddepth);
0N/A
0N/A return new_exit;
0N/A}
0N/A
0N/A//------------------------------ insert_region_before_proj -------------------------------------
0N/A// Insert a region before an if projection (* - new node)
0N/A//
0N/A// before
0N/A// if(test)
0N/A// / |
0N/A// v |
0N/A// proj v
0N/A// other-proj
0N/A//
0N/A// after
0N/A// if(test)
0N/A// / |
0N/A// v |
0N/A// * proj-clone v
0N/A// | other-proj
0N/A// v
0N/A// * new-region
0N/A// |
0N/A// v
0N/A// * dum_if
0N/A// / \
0N/A// v \
0N/A// * dum-proj v
0N/A// proj
0N/A//
0N/ARegionNode* PhaseIdealLoop::insert_region_before_proj(ProjNode* proj) {
0N/A IfNode* iff = proj->in(0)->as_If();
0N/A IdealLoopTree *loop = get_loop(proj);
0N/A ProjNode *other_proj = iff->proj_out(!proj->is_IfTrue())->as_Proj();
0N/A int ddepth = dom_depth(proj);
0N/A
3811N/A _igvn.rehash_node_delayed(iff);
3811N/A _igvn.rehash_node_delayed(proj);
0N/A
0N/A proj->set_req(0, NULL); // temporary disconnect
0N/A ProjNode* proj2 = proj_clone(proj, iff);
0N/A register_node(proj2, loop, iff, ddepth);
0N/A
4022N/A RegionNode* reg = new (C)RegionNode(2);
0N/A reg->set_req(1, proj2);
0N/A register_node(reg, loop, iff, ddepth);
0N/A
4022N/A IfNode* dum_if = new (C)IfNode(reg, short_circuit_if(NULL, proj), iff->_prob, iff->_fcnt);
0N/A register_node(dum_if, loop, reg, ddepth);
0N/A
0N/A proj->set_req(0, dum_if); // reattach
0N/A set_idom(proj, dum_if, ddepth);
0N/A
0N/A ProjNode* dum_proj = proj_clone(other_proj, dum_if);
0N/A register_node(dum_proj, loop, dum_if, ddepth);
0N/A
0N/A return reg;
0N/A}
0N/A
0N/A//------------------------------ insert_cmpi_loop_exit -------------------------------------
0N/A// Clone a signed compare loop exit from an unsigned compare and
0N/A// insert it before the unsigned cmp on the stay-in-loop path.
0N/A// All new nodes inserted in the dominator tree between the original
0N/A// if and it's projections. The original if test is replaced with
0N/A// a constant to force the stay-in-loop path.
0N/A//
0N/A// This is done to make sure that the original if and it's projections
0N/A// still dominate the same set of control nodes, that the ctrl() relation
0N/A// from data nodes to them is preserved, and that their loop nesting is
0N/A// preserved.
0N/A//
0N/A// before
0N/A// if(i <u limit) unsigned compare loop exit
0N/A// / |
0N/A// v v
0N/A// exit-proj stay-in-loop-proj
0N/A//
0N/A// after
0N/A// if(stay-in-loop-const) original if
0N/A// / |
0N/A// / v
0N/A// / if(i < limit) new signed test
0N/A// / / |
0N/A// / / v
0N/A// / / if(i <u limit) new cloned unsigned test
0N/A// / / / |
0N/A// v v v |
0N/A// region |
0N/A// | |
0N/A// dum-if |
0N/A// / | |
0N/A// ether | |
0N/A// v v
0N/A// exit-proj stay-in-loop-proj
0N/A//
0N/AIfNode* PhaseIdealLoop::insert_cmpi_loop_exit(IfNode* if_cmpu, IdealLoopTree *loop) {
0N/A const bool Signed = true;
0N/A const bool Unsigned = false;
0N/A
0N/A BoolNode* bol = if_cmpu->in(1)->as_Bool();
0N/A if (bol->_test._test != BoolTest::lt) return NULL;
0N/A CmpNode* cmpu = bol->in(1)->as_Cmp();
0N/A if (cmpu->Opcode() != Op_CmpU) return NULL;
0N/A int stride = stride_of_possible_iv(if_cmpu);
0N/A if (stride == 0) return NULL;
0N/A
0N/A ProjNode* lp_continue = stay_in_loop(if_cmpu, loop)->as_Proj();
0N/A ProjNode* lp_exit = if_cmpu->proj_out(!lp_continue->is_IfTrue())->as_Proj();
0N/A
0N/A Node* limit = NULL;
0N/A if (stride > 0) {
0N/A limit = cmpu->in(2);
0N/A } else {
0N/A limit = _igvn.makecon(TypeInt::ZERO);
0N/A set_ctrl(limit, C->root());
0N/A }
0N/A // Create a new region on the exit path
0N/A RegionNode* reg = insert_region_before_proj(lp_exit);
0N/A
0N/A // Clone the if-cmpu-true-false using a signed compare
0N/A BoolTest::mask rel_i = stride > 0 ? bol->_test._test : BoolTest::ge;
0N/A ProjNode* cmpi_exit = insert_if_before_proj(cmpu->in(1), Signed, rel_i, limit, lp_continue);
0N/A reg->add_req(cmpi_exit);
0N/A
0N/A // Clone the if-cmpu-true-false
0N/A BoolTest::mask rel_u = bol->_test._test;
0N/A ProjNode* cmpu_exit = insert_if_before_proj(cmpu->in(1), Unsigned, rel_u, cmpu->in(2), lp_continue);
0N/A reg->add_req(cmpu_exit);
0N/A
0N/A // Force original if to stay in loop.
0N/A short_circuit_if(if_cmpu, lp_continue);
0N/A
0N/A return cmpi_exit->in(0)->as_If();
0N/A}
0N/A
0N/A//------------------------------ remove_cmpi_loop_exit -------------------------------------
0N/A// Remove a previously inserted signed compare loop exit.
0N/Avoid PhaseIdealLoop::remove_cmpi_loop_exit(IfNode* if_cmp, IdealLoopTree *loop) {
0N/A Node* lp_proj = stay_in_loop(if_cmp, loop);
0N/A assert(if_cmp->in(1)->in(1)->Opcode() == Op_CmpI &&
0N/A stay_in_loop(lp_proj, loop)->is_If() &&
0N/A stay_in_loop(lp_proj, loop)->in(1)->in(1)->Opcode() == Op_CmpU, "inserted cmpi before cmpu");
0N/A Node *con = _igvn.makecon(lp_proj->is_IfTrue() ? TypeInt::ONE : TypeInt::ZERO);
0N/A set_ctrl(con, C->root());
0N/A if_cmp->set_req(1, con);
0N/A}
0N/A
0N/A//------------------------------ scheduled_nodelist -------------------------------------
0N/A// Create a post order schedule of nodes that are in the
0N/A// "member" set. The list is returned in "sched".
0N/A// The first node in "sched" is the loop head, followed by
0N/A// nodes which have no inputs in the "member" set, and then
0N/A// followed by the nodes that have an immediate input dependence
0N/A// on a node in "sched".
0N/Avoid PhaseIdealLoop::scheduled_nodelist( IdealLoopTree *loop, VectorSet& member, Node_List &sched ) {
0N/A
0N/A assert(member.test(loop->_head->_idx), "loop head must be in member set");
0N/A Arena *a = Thread::current()->resource_area();
0N/A VectorSet visited(a);
0N/A Node_Stack nstack(a, loop->_body.size());
0N/A
0N/A Node* n = loop->_head; // top of stack is cached in "n"
0N/A uint idx = 0;
0N/A visited.set(n->_idx);
0N/A
0N/A // Initially push all with no inputs from within member set
0N/A for(uint i = 0; i < loop->_body.size(); i++ ) {
0N/A Node *elt = loop->_body.at(i);
0N/A if (member.test(elt->_idx)) {
0N/A bool found = false;
0N/A for (uint j = 0; j < elt->req(); j++) {
0N/A Node* def = elt->in(j);
0N/A if (def && member.test(def->_idx) && def != elt) {
0N/A found = true;
0N/A break;
0N/A }
0N/A }
0N/A if (!found && elt != loop->_head) {
0N/A nstack.push(n, idx);
0N/A n = elt;
0N/A assert(!visited.test(n->_idx), "not seen yet");
0N/A visited.set(n->_idx);
0N/A }
0N/A }
0N/A }
0N/A
0N/A // traverse out's that are in the member set
0N/A while (true) {
0N/A if (idx < n->outcnt()) {
0N/A Node* use = n->raw_out(idx);
0N/A idx++;
0N/A if (!visited.test_set(use->_idx)) {
0N/A if (member.test(use->_idx)) {
0N/A nstack.push(n, idx);
0N/A n = use;
0N/A idx = 0;
0N/A }
0N/A }
0N/A } else {
0N/A // All outputs processed
0N/A sched.push(n);
0N/A if (nstack.is_empty()) break;
0N/A n = nstack.node();
0N/A idx = nstack.index();
0N/A nstack.pop();
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A//------------------------------ has_use_in_set -------------------------------------
0N/A// Has a use in the vector set
0N/Abool PhaseIdealLoop::has_use_in_set( Node* n, VectorSet& vset ) {
0N/A for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) {
0N/A Node* use = n->fast_out(j);
0N/A if (vset.test(use->_idx)) {
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A}
0N/A
0N/A
0N/A//------------------------------ has_use_internal_to_set -------------------------------------
0N/A// Has use internal to the vector set (ie. not in a phi at the loop head)
0N/Abool PhaseIdealLoop::has_use_internal_to_set( Node* n, VectorSet& vset, IdealLoopTree *loop ) {
0N/A Node* head = loop->_head;
0N/A for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) {
0N/A Node* use = n->fast_out(j);
0N/A if (vset.test(use->_idx) && !(use->is_Phi() && use->in(0) == head)) {
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A}
0N/A
0N/A
0N/A//------------------------------ clone_for_use_outside_loop -------------------------------------
0N/A// clone "n" for uses that are outside of loop
4533N/Aint PhaseIdealLoop::clone_for_use_outside_loop( IdealLoopTree *loop, Node* n, Node_List& worklist ) {
4533N/A int cloned = 0;
0N/A assert(worklist.size() == 0, "should be empty");
0N/A for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) {
0N/A Node* use = n->fast_out(j);
0N/A if( !loop->is_member(get_loop(has_ctrl(use) ? get_ctrl(use) : use)) ) {
0N/A worklist.push(use);
0N/A }
0N/A }
0N/A while( worklist.size() ) {
0N/A Node *use = worklist.pop();
0N/A if (!has_node(use) || use->in(0) == C->top()) continue;
0N/A uint j;
0N/A for (j = 0; j < use->req(); j++) {
0N/A if (use->in(j) == n) break;
0N/A }
0N/A assert(j < use->req(), "must be there");
0N/A
0N/A // clone "n" and insert it between the inputs of "n" and the use outside the loop
0N/A Node* n_clone = n->clone();
3811N/A _igvn.replace_input_of(use, j, n_clone);
4533N/A cloned++;
251N/A Node* use_c;
0N/A if (!use->is_Phi()) {
251N/A use_c = has_ctrl(use) ? get_ctrl(use) : use->in(0);
0N/A } else {
0N/A // Use in a phi is considered a use in the associated predecessor block
251N/A use_c = use->in(0)->in(j);
0N/A }
251N/A set_ctrl(n_clone, use_c);
251N/A assert(!loop->is_member(get_loop(use_c)), "should be outside loop");
251N/A get_loop(use_c)->_body.push(n_clone);
0N/A _igvn.register_new_node_with_optimizer(n_clone);
0N/A#if !defined(PRODUCT)
0N/A if (TracePartialPeeling) {
0N/A tty->print_cr("loop exit cloning old: %d new: %d newbb: %d", n->_idx, n_clone->_idx, get_ctrl(n_clone)->_idx);
0N/A }
0N/A#endif
0N/A }
4533N/A return cloned;
0N/A}
0N/A
0N/A
0N/A//------------------------------ clone_for_special_use_inside_loop -------------------------------------
0N/A// clone "n" for special uses that are in the not_peeled region.
0N/A// If these def-uses occur in separate blocks, the code generator
0N/A// marks the method as not compilable. For example, if a "BoolNode"
0N/A// is in a different basic block than the "IfNode" that uses it, then
0N/A// the compilation is aborted in the code generator.
0N/Avoid PhaseIdealLoop::clone_for_special_use_inside_loop( IdealLoopTree *loop, Node* n,
0N/A VectorSet& not_peel, Node_List& sink_list, Node_List& worklist ) {
0N/A if (n->is_Phi() || n->is_Load()) {
0N/A return;
0N/A }
0N/A assert(worklist.size() == 0, "should be empty");
0N/A for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) {
0N/A Node* use = n->fast_out(j);
0N/A if ( not_peel.test(use->_idx) &&
0N/A (use->is_If() || use->is_CMove() || use->is_Bool()) &&
0N/A use->in(1) == n) {
0N/A worklist.push(use);
0N/A }
0N/A }
0N/A if (worklist.size() > 0) {
0N/A // clone "n" and insert it between inputs of "n" and the use
0N/A Node* n_clone = n->clone();
0N/A loop->_body.push(n_clone);
0N/A _igvn.register_new_node_with_optimizer(n_clone);
0N/A set_ctrl(n_clone, get_ctrl(n));
0N/A sink_list.push(n_clone);
0N/A not_peel <<= n_clone->_idx; // add n_clone to not_peel set.
0N/A#if !defined(PRODUCT)
0N/A if (TracePartialPeeling) {
0N/A tty->print_cr("special not_peeled cloning old: %d new: %d", n->_idx, n_clone->_idx);
0N/A }
0N/A#endif
0N/A while( worklist.size() ) {
0N/A Node *use = worklist.pop();
3811N/A _igvn.rehash_node_delayed(use);
0N/A for (uint j = 1; j < use->req(); j++) {
0N/A if (use->in(j) == n) {
0N/A use->set_req(j, n_clone);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A//------------------------------ insert_phi_for_loop -------------------------------------
0N/A// Insert phi(lp_entry_val, back_edge_val) at use->in(idx) for loop lp if phi does not already exist
0N/Avoid PhaseIdealLoop::insert_phi_for_loop( Node* use, uint idx, Node* lp_entry_val, Node* back_edge_val, LoopNode* lp ) {
0N/A Node *phi = PhiNode::make(lp, back_edge_val);
0N/A phi->set_req(LoopNode::EntryControl, lp_entry_val);
0N/A // Use existing phi if it already exists
0N/A Node *hit = _igvn.hash_find_insert(phi);
0N/A if( hit == NULL ) {
0N/A _igvn.register_new_node_with_optimizer(phi);
0N/A set_ctrl(phi, lp);
0N/A } else {
0N/A // Remove the new phi from the graph and use the hit
0N/A _igvn.remove_dead_node(phi);
0N/A phi = hit;
0N/A }
3811N/A _igvn.replace_input_of(use, idx, phi);
0N/A}
0N/A
0N/A#ifdef ASSERT
0N/A//------------------------------ is_valid_loop_partition -------------------------------------
0N/A// Validate the loop partition sets: peel and not_peel
0N/Abool PhaseIdealLoop::is_valid_loop_partition( IdealLoopTree *loop, VectorSet& peel, Node_List& peel_list,
0N/A VectorSet& not_peel ) {
0N/A uint i;
0N/A // Check that peel_list entries are in the peel set
0N/A for (i = 0; i < peel_list.size(); i++) {
0N/A if (!peel.test(peel_list.at(i)->_idx)) {
0N/A return false;
0N/A }
0N/A }
0N/A // Check at loop members are in one of peel set or not_peel set
0N/A for (i = 0; i < loop->_body.size(); i++ ) {
0N/A Node *def = loop->_body.at(i);
0N/A uint di = def->_idx;
0N/A // Check that peel set elements are in peel_list
0N/A if (peel.test(di)) {
0N/A if (not_peel.test(di)) {
0N/A return false;
0N/A }
0N/A // Must be in peel_list also
0N/A bool found = false;
0N/A for (uint j = 0; j < peel_list.size(); j++) {
0N/A if (peel_list.at(j)->_idx == di) {
0N/A found = true;
0N/A break;
0N/A }
0N/A }
0N/A if (!found) {
0N/A return false;
0N/A }
0N/A } else if (not_peel.test(di)) {
0N/A if (peel.test(di)) {
0N/A return false;
0N/A }
0N/A } else {
0N/A return false;
0N/A }
0N/A }
0N/A return true;
0N/A}
0N/A
0N/A//------------------------------ is_valid_clone_loop_exit_use -------------------------------------
0N/A// Ensure a use outside of loop is of the right form
0N/Abool PhaseIdealLoop::is_valid_clone_loop_exit_use( IdealLoopTree *loop, Node* use, uint exit_idx) {
0N/A Node *use_c = has_ctrl(use) ? get_ctrl(use) : use;
0N/A return (use->is_Phi() &&
0N/A use_c->is_Region() && use_c->req() == 3 &&
0N/A (use_c->in(exit_idx)->Opcode() == Op_IfTrue ||
0N/A use_c->in(exit_idx)->Opcode() == Op_IfFalse ||
0N/A use_c->in(exit_idx)->Opcode() == Op_JumpProj) &&
0N/A loop->is_member( get_loop( use_c->in(exit_idx)->in(0) ) ) );
0N/A}
0N/A
0N/A//------------------------------ is_valid_clone_loop_form -------------------------------------
0N/A// Ensure that all uses outside of loop are of the right form
0N/Abool PhaseIdealLoop::is_valid_clone_loop_form( IdealLoopTree *loop, Node_List& peel_list,
0N/A uint orig_exit_idx, uint clone_exit_idx) {
0N/A uint len = peel_list.size();
0N/A for (uint i = 0; i < len; i++) {
0N/A Node *def = peel_list.at(i);
0N/A
0N/A for (DUIterator_Fast jmax, j = def->fast_outs(jmax); j < jmax; j++) {
0N/A Node *use = def->fast_out(j);
0N/A Node *use_c = has_ctrl(use) ? get_ctrl(use) : use;
0N/A if (!loop->is_member(get_loop(use_c))) {
0N/A // use is not in the loop, check for correct structure
0N/A if (use->in(0) == def) {
0N/A // Okay
0N/A } else if (!is_valid_clone_loop_exit_use(loop, use, orig_exit_idx)) {
0N/A return false;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A return true;
0N/A}
0N/A#endif
0N/A
0N/A//------------------------------ partial_peel -------------------------------------
0N/A// Partially peel (aka loop rotation) the top portion of a loop (called
0N/A// the peel section below) by cloning it and placing one copy just before
0N/A// the new loop head and the other copy at the bottom of the new loop.
0N/A//
0N/A// before after where it came from
0N/A//
0N/A// stmt1 stmt1
0N/A// loop: stmt2 clone
0N/A// stmt2 if condA goto exitA clone
0N/A// if condA goto exitA new_loop: new
0N/A// stmt3 stmt3 clone
0N/A// if !condB goto loop if condB goto exitB clone
0N/A// exitB: stmt2 orig
0N/A// stmt4 if !condA goto new_loop orig
0N/A// exitA: goto exitA
0N/A// exitB:
0N/A// stmt4
0N/A// exitA:
0N/A//
0N/A// Step 1: find the cut point: an exit test on probable
0N/A// induction variable.
0N/A// Step 2: schedule (with cloning) operations in the peel
0N/A// section that can be executed after the cut into
0N/A// the section that is not peeled. This may need
0N/A// to clone operations into exit blocks. For
0N/A// instance, a reference to A[i] in the not-peel
0N/A// section and a reference to B[i] in an exit block
0N/A// may cause a left-shift of i by 2 to be placed
0N/A// in the peel block. This step will clone the left
0N/A// shift into the exit block and sink the left shift
0N/A// from the peel to the not-peel section.
0N/A// Step 3: clone the loop, retarget the control, and insert
0N/A// phis for values that are live across the new loop
0N/A// head. This is very dependent on the graph structure
0N/A// from clone_loop. It creates region nodes for
0N/A// exit control and associated phi nodes for values
0N/A// flow out of the loop through that exit. The region
0N/A// node is dominated by the clone's control projection.
0N/A// So the clone's peel section is placed before the
0N/A// new loop head, and the clone's not-peel section is
0N/A// forms the top part of the new loop. The original
0N/A// peel section forms the tail of the new loop.
0N/A// Step 4: update the dominator tree and recompute the
0N/A// dominator depth.
0N/A//
0N/A// orig
0N/A//
2292N/A// stmt1
2292N/A// |
2292N/A// v
2292N/A// loop predicate
2292N/A// |
2292N/A// v
0N/A// loop<----+
0N/A// | |
0N/A// stmt2 |
0N/A// | |
0N/A// v |
0N/A// ifA |
0N/A// / | |
0N/A// v v |
0N/A// false true ^ <-- last_peel
0N/A// / | |
0N/A// / ===|==cut |
0N/A// / stmt3 | <-- first_not_peel
0N/A// / | |
0N/A// | v |
0N/A// v ifB |
0N/A// exitA: / \ |
0N/A// / \ |
0N/A// v v |
0N/A// false true |
0N/A// / \ |
0N/A// / ----+
0N/A// |
0N/A// v
0N/A// exitB:
0N/A// stmt4
0N/A//
0N/A//
0N/A// after clone loop
0N/A//
0N/A// stmt1
2292N/A// |
2292N/A// v
2292N/A// loop predicate
0N/A// / \
0N/A// clone / \ orig
0N/A// / \
0N/A// / \
0N/A// v v
0N/A// +---->loop loop<----+
0N/A// | | | |
0N/A// | stmt2 stmt2 |
0N/A// | | | |
0N/A// | v v |
0N/A// | ifA ifA |
0N/A// | | \ / | |
0N/A// | v v v v |
0N/A// ^ true false false true ^ <-- last_peel
0N/A// | | ^ \ / | |
0N/A// | cut==|== \ \ / ===|==cut |
0N/A// | stmt3 \ \ / stmt3 | <-- first_not_peel
0N/A// | | dom | | | |
0N/A// | v \ 1v v2 v |
0N/A// | ifB regionA ifB |
0N/A// | / \ | / \ |
0N/A// | / \ v / \ |
0N/A// | v v exitA: v v |
0N/A// | true false false true |
0N/A// | / ^ \ / \ |
0N/A// +---- \ \ / ----+
0N/A// dom \ /
0N/A// \ 1v v2
0N/A// regionB
0N/A// |
0N/A// v
0N/A// exitB:
0N/A// stmt4
0N/A//
0N/A//
0N/A// after partial peel
0N/A//
0N/A// stmt1
2292N/A// |
2292N/A// v
2292N/A// loop predicate
0N/A// /
0N/A// clone / orig
0N/A// / TOP
0N/A// / \
0N/A// v v
2292N/A// TOP->loop loop----+
0N/A// | | |
0N/A// stmt2 stmt2 |
0N/A// | | |
0N/A// v v |
0N/A// ifA ifA |
0N/A// | \ / | |
0N/A// v v v v |
0N/A// true false false true | <-- last_peel
0N/A// | ^ \ / +------|---+
0N/A// +->newloop \ \ / === ==cut | |
0N/A// | stmt3 \ \ / TOP | |
0N/A// | | dom | | stmt3 | | <-- first_not_peel
0N/A// | v \ 1v v2 v | |
0N/A// | ifB regionA ifB ^ v
0N/A// | / \ | / \ | |
0N/A// | / \ v / \ | |
0N/A// | v v exitA: v v | |
0N/A// | true false false true | |
0N/A// | / ^ \ / \ | |
0N/A// | | \ \ / v | |
0N/A// | | dom \ / TOP | |
0N/A// | | \ 1v v2 | |
0N/A// ^ v regionB | |
0N/A// | | | | |
0N/A// | | v ^ v
0N/A// | | exitB: | |
0N/A// | | stmt4 | |
0N/A// | +------------>-----------------+ |
0N/A// | |
0N/A// +-----------------<---------------------+
0N/A//
0N/A//
0N/A// final graph
0N/A//
0N/A// stmt1
0N/A// |
0N/A// v
2378N/A// loop predicate
2378N/A// |
2378N/A// v
2292N/A// stmt2 clone
2292N/A// |
2292N/A// v
0N/A// ........> ifA clone
0N/A// : / |
0N/A// dom / |
0N/A// : v v
0N/A// : false true
0N/A// : | |
2292N/A// : | v
0N/A// : | newloop<-----+
0N/A// : | | |
0N/A// : | stmt3 clone |
0N/A// : | | |
0N/A// : | v |
0N/A// : | ifB |
0N/A// : | / \ |
0N/A// : | v v |
0N/A// : | false true |
0N/A// : | | | |
0N/A// : | v stmt2 |
0N/A// : | exitB: | |
0N/A// : | stmt4 v |
0N/A// : | ifA orig |
0N/A// : | / \ |
0N/A// : | / \ |
0N/A// : | v v |
0N/A// : | false true |
0N/A// : | / \ |
0N/A// : v v -----+
0N/A// RegionA
0N/A// |
0N/A// v
0N/A// exitA
0N/A//
0N/Abool PhaseIdealLoop::partial_peel( IdealLoopTree *loop, Node_List &old_new ) {
0N/A
2292N/A assert(!loop->_head->is_CountedLoop(), "Non-counted loop only");
108N/A if (!loop->_head->is_Loop()) {
108N/A return false; }
108N/A
0N/A LoopNode *head = loop->_head->as_Loop();
0N/A
0N/A if (head->is_partial_peel_loop() || head->partial_peel_has_failed()) {
0N/A return false;
0N/A }
0N/A
0N/A // Check for complex exit control
0N/A for(uint ii = 0; ii < loop->_body.size(); ii++ ) {
0N/A Node *n = loop->_body.at(ii);
0N/A int opc = n->Opcode();
0N/A if (n->is_Call() ||
0N/A opc == Op_Catch ||
0N/A opc == Op_CatchProj ||
0N/A opc == Op_Jump ||
0N/A opc == Op_JumpProj) {
0N/A#if !defined(PRODUCT)
0N/A if (TracePartialPeeling) {
0N/A tty->print_cr("\nExit control too complex: lp: %d", head->_idx);
0N/A }
0N/A#endif
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A int dd = dom_depth(head);
0N/A
0N/A // Step 1: find cut point
0N/A
0N/A // Walk up dominators to loop head looking for first loop exit
0N/A // which is executed on every path thru loop.
0N/A IfNode *peel_if = NULL;
0N/A IfNode *peel_if_cmpu = NULL;
0N/A
0N/A Node *iff = loop->tail();
0N/A while( iff != head ) {
0N/A if( iff->is_If() ) {
0N/A Node *ctrl = get_ctrl(iff->in(1));
0N/A if (ctrl->is_top()) return false; // Dead test on live IF.
0N/A // If loop-varying exit-test, check for induction variable
0N/A if( loop->is_member(get_loop(ctrl)) &&
0N/A loop->is_loop_exit(iff) &&
0N/A is_possible_iv_test(iff)) {
0N/A Node* cmp = iff->in(1)->in(1);
0N/A if (cmp->Opcode() == Op_CmpI) {
0N/A peel_if = iff->as_If();
0N/A } else {
0N/A assert(cmp->Opcode() == Op_CmpU, "must be CmpI or CmpU");
0N/A peel_if_cmpu = iff->as_If();
0N/A }
0N/A }
0N/A }
0N/A iff = idom(iff);
0N/A }
0N/A // Prefer signed compare over unsigned compare.
0N/A IfNode* new_peel_if = NULL;
0N/A if (peel_if == NULL) {
0N/A if (!PartialPeelAtUnsignedTests || peel_if_cmpu == NULL) {
0N/A return false; // No peel point found
0N/A }
0N/A new_peel_if = insert_cmpi_loop_exit(peel_if_cmpu, loop);
0N/A if (new_peel_if == NULL) {
0N/A return false; // No peel point found
0N/A }
0N/A peel_if = new_peel_if;
0N/A }
0N/A Node* last_peel = stay_in_loop(peel_if, loop);
0N/A Node* first_not_peeled = stay_in_loop(last_peel, loop);
0N/A if (first_not_peeled == NULL || first_not_peeled == head) {
0N/A return false;
0N/A }
0N/A
0N/A#if !defined(PRODUCT)
2230N/A if (TraceLoopOpts) {
2230N/A tty->print("PartialPeel ");
2230N/A loop->dump_head();
2230N/A }
2230N/A
0N/A if (TracePartialPeeling) {
0N/A tty->print_cr("before partial peel one iteration");
0N/A Node_List wl;
0N/A Node* t = head->in(2);
0N/A while (true) {
0N/A wl.push(t);
0N/A if (t == head) break;
0N/A t = idom(t);
0N/A }
0N/A while (wl.size() > 0) {
0N/A Node* tt = wl.pop();
0N/A tt->dump();
0N/A if (tt == last_peel) tty->print_cr("-- cut --");
0N/A }
0N/A }
0N/A#endif
0N/A ResourceArea *area = Thread::current()->resource_area();
0N/A VectorSet peel(area);
0N/A VectorSet not_peel(area);
0N/A Node_List peel_list(area);
0N/A Node_List worklist(area);
0N/A Node_List sink_list(area);
0N/A
0N/A // Set of cfg nodes to peel are those that are executable from
0N/A // the head through last_peel.
0N/A assert(worklist.size() == 0, "should be empty");
0N/A worklist.push(head);
0N/A peel.set(head->_idx);
0N/A while (worklist.size() > 0) {
0N/A Node *n = worklist.pop();
0N/A if (n != last_peel) {
0N/A for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) {
0N/A Node* use = n->fast_out(j);
0N/A if (use->is_CFG() &&
0N/A loop->is_member(get_loop(use)) &&
0N/A !peel.test_set(use->_idx)) {
0N/A worklist.push(use);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A // Set of non-cfg nodes to peel are those that are control
0N/A // dependent on the cfg nodes.
0N/A uint i;
0N/A for(i = 0; i < loop->_body.size(); i++ ) {
0N/A Node *n = loop->_body.at(i);
0N/A Node *n_c = has_ctrl(n) ? get_ctrl(n) : n;
0N/A if (peel.test(n_c->_idx)) {
0N/A peel.set(n->_idx);
0N/A } else {
0N/A not_peel.set(n->_idx);
0N/A }
0N/A }
0N/A
0N/A // Step 2: move operations from the peeled section down into the
0N/A // not-peeled section
0N/A
0N/A // Get a post order schedule of nodes in the peel region
0N/A // Result in right-most operand.
0N/A scheduled_nodelist(loop, peel, peel_list );
0N/A
0N/A assert(is_valid_loop_partition(loop, peel, peel_list, not_peel), "bad partition");
0N/A
0N/A // For future check for too many new phis
0N/A uint old_phi_cnt = 0;
0N/A for (DUIterator_Fast jmax, j = head->fast_outs(jmax); j < jmax; j++) {
0N/A Node* use = head->fast_out(j);
0N/A if (use->is_Phi()) old_phi_cnt++;
0N/A }
0N/A
0N/A#if !defined(PRODUCT)
0N/A if (TracePartialPeeling) {
0N/A tty->print_cr("\npeeled list");
0N/A }
0N/A#endif
0N/A
0N/A // Evacuate nodes in peel region into the not_peeled region if possible
0N/A uint new_phi_cnt = 0;
4533N/A uint cloned_for_outside_use = 0;
0N/A for (i = 0; i < peel_list.size();) {
0N/A Node* n = peel_list.at(i);
0N/A#if !defined(PRODUCT)
0N/A if (TracePartialPeeling) n->dump();
0N/A#endif
0N/A bool incr = true;
0N/A if ( !n->is_CFG() ) {
0N/A
0N/A if ( has_use_in_set(n, not_peel) ) {
0N/A
0N/A // If not used internal to the peeled region,
0N/A // move "n" from peeled to not_peeled region.
0N/A
0N/A if ( !has_use_internal_to_set(n, peel, loop) ) {
0N/A
0N/A // if not pinned and not a load (which maybe anti-dependent on a store)
0N/A // and not a CMove (Matcher expects only bool->cmove).
0N/A if ( n->in(0) == NULL && !n->is_Load() && !n->is_CMove() ) {
4533N/A cloned_for_outside_use += clone_for_use_outside_loop( loop, n, worklist );
0N/A sink_list.push(n);
0N/A peel >>= n->_idx; // delete n from peel set.
0N/A not_peel <<= n->_idx; // add n to not_peel set.
0N/A peel_list.remove(i);
0N/A incr = false;
0N/A#if !defined(PRODUCT)
0N/A if (TracePartialPeeling) {
0N/A tty->print_cr("sink to not_peeled region: %d newbb: %d",
0N/A n->_idx, get_ctrl(n)->_idx);
0N/A }
0N/A#endif
0N/A }
0N/A } else {
0N/A // Otherwise check for special def-use cases that span
0N/A // the peel/not_peel boundary such as bool->if
0N/A clone_for_special_use_inside_loop( loop, n, not_peel, sink_list, worklist );
0N/A new_phi_cnt++;
0N/A }
0N/A }
0N/A }
0N/A if (incr) i++;
0N/A }
0N/A
0N/A if (new_phi_cnt > old_phi_cnt + PartialPeelNewPhiDelta) {
0N/A#if !defined(PRODUCT)
0N/A if (TracePartialPeeling) {
0N/A tty->print_cr("\nToo many new phis: %d old %d new cmpi: %c",
0N/A new_phi_cnt, old_phi_cnt, new_peel_if != NULL?'T':'F');
0N/A }
0N/A#endif
0N/A if (new_peel_if != NULL) {
0N/A remove_cmpi_loop_exit(new_peel_if, loop);
0N/A }
0N/A // Inhibit more partial peeling on this loop
0N/A assert(!head->is_partial_peel_loop(), "not partial peeled");
0N/A head->mark_partial_peel_failed();
4533N/A if (cloned_for_outside_use > 0) {
4533N/A // Terminate this round of loop opts because
4533N/A // the graph outside this loop was changed.
4533N/A C->set_major_progress();
4533N/A return true;
4533N/A }
0N/A return false;
0N/A }
0N/A
0N/A // Step 3: clone loop, retarget control, and insert new phis
0N/A
0N/A // Create new loop head for new phis and to hang
0N/A // the nodes being moved (sinked) from the peel region.
4022N/A LoopNode* new_head = new (C) LoopNode(last_peel, last_peel);
2230N/A new_head->set_unswitch_count(head->unswitch_count()); // Preserve
0N/A _igvn.register_new_node_with_optimizer(new_head);
0N/A assert(first_not_peeled->in(0) == last_peel, "last_peel <- first_not_peeled");
0N/A first_not_peeled->set_req(0, new_head);
0N/A set_loop(new_head, loop);
0N/A loop->_body.push(new_head);
0N/A not_peel.set(new_head->_idx);
0N/A set_idom(new_head, last_peel, dom_depth(first_not_peeled));
0N/A set_idom(first_not_peeled, new_head, dom_depth(first_not_peeled));
0N/A
0N/A while (sink_list.size() > 0) {
0N/A Node* n = sink_list.pop();
0N/A set_ctrl(n, new_head);
0N/A }
0N/A
0N/A assert(is_valid_loop_partition(loop, peel, peel_list, not_peel), "bad partition");
0N/A
0N/A clone_loop( loop, old_new, dd );
0N/A
0N/A const uint clone_exit_idx = 1;
0N/A const uint orig_exit_idx = 2;
0N/A assert(is_valid_clone_loop_form( loop, peel_list, orig_exit_idx, clone_exit_idx ), "bad clone loop");
0N/A
0N/A Node* head_clone = old_new[head->_idx];
0N/A LoopNode* new_head_clone = old_new[new_head->_idx]->as_Loop();
0N/A Node* orig_tail_clone = head_clone->in(2);
0N/A
0N/A // Add phi if "def" node is in peel set and "use" is not
0N/A
0N/A for(i = 0; i < peel_list.size(); i++ ) {
0N/A Node *def = peel_list.at(i);
0N/A if (!def->is_CFG()) {
0N/A for (DUIterator_Fast jmax, j = def->fast_outs(jmax); j < jmax; j++) {
0N/A Node *use = def->fast_out(j);
0N/A if (has_node(use) && use->in(0) != C->top() &&
0N/A (!peel.test(use->_idx) ||
0N/A (use->is_Phi() && use->in(0) == head)) ) {
0N/A worklist.push(use);
0N/A }
0N/A }
0N/A while( worklist.size() ) {
0N/A Node *use = worklist.pop();
0N/A for (uint j = 1; j < use->req(); j++) {
0N/A Node* n = use->in(j);
0N/A if (n == def) {
0N/A
0N/A // "def" is in peel set, "use" is not in peel set
0N/A // or "use" is in the entry boundary (a phi) of the peel set
0N/A
0N/A Node* use_c = has_ctrl(use) ? get_ctrl(use) : use;
0N/A
0N/A if ( loop->is_member(get_loop( use_c )) ) {
0N/A // use is in loop
0N/A if (old_new[use->_idx] != NULL) { // null for dead code
0N/A Node* use_clone = old_new[use->_idx];
3811N/A _igvn.replace_input_of(use, j, C->top());
0N/A insert_phi_for_loop( use_clone, j, old_new[def->_idx], def, new_head_clone );
0N/A }
0N/A } else {
0N/A assert(is_valid_clone_loop_exit_use(loop, use, orig_exit_idx), "clone loop format");
0N/A // use is not in the loop, check if the live range includes the cut
0N/A Node* lp_if = use_c->in(orig_exit_idx)->in(0);
0N/A if (not_peel.test(lp_if->_idx)) {
0N/A assert(j == orig_exit_idx, "use from original loop");
0N/A insert_phi_for_loop( use, clone_exit_idx, old_new[def->_idx], def, new_head_clone );
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A // Step 3b: retarget control
0N/A
0N/A // Redirect control to the new loop head if a cloned node in
0N/A // the not_peeled region has control that points into the peeled region.
0N/A // This necessary because the cloned peeled region will be outside
0N/A // the loop.
0N/A // from to
0N/A // cloned-peeled <---+
0N/A // new_head_clone: | <--+
0N/A // cloned-not_peeled in(0) in(0)
0N/A // orig-peeled
0N/A
0N/A for(i = 0; i < loop->_body.size(); i++ ) {
0N/A Node *n = loop->_body.at(i);
0N/A if (!n->is_CFG() && n->in(0) != NULL &&
0N/A not_peel.test(n->_idx) && peel.test(n->in(0)->_idx)) {
0N/A Node* n_clone = old_new[n->_idx];
3811N/A _igvn.replace_input_of(n_clone, 0, new_head_clone);
0N/A }
0N/A }
0N/A
0N/A // Backedge of the surviving new_head (the clone) is original last_peel
3811N/A _igvn.replace_input_of(new_head_clone, LoopNode::LoopBackControl, last_peel);
0N/A
0N/A // Cut first node in original not_peel set
3811N/A _igvn.rehash_node_delayed(new_head); // Multiple edge updates:
3811N/A new_head->set_req(LoopNode::EntryControl, C->top()); // use rehash_node_delayed / set_req instead of
3811N/A new_head->set_req(LoopNode::LoopBackControl, C->top()); // multiple replace_input_of calls
0N/A
0N/A // Copy head_clone back-branch info to original head
0N/A // and remove original head's loop entry and
0N/A // clone head's back-branch
3811N/A _igvn.rehash_node_delayed(head); // Multiple edge updates
3811N/A head->set_req(LoopNode::EntryControl, head_clone->in(LoopNode::LoopBackControl));
0N/A head->set_req(LoopNode::LoopBackControl, C->top());
3811N/A _igvn.replace_input_of(head_clone, LoopNode::LoopBackControl, C->top());
0N/A
0N/A // Similarly modify the phis
0N/A for (DUIterator_Fast kmax, k = head->fast_outs(kmax); k < kmax; k++) {
0N/A Node* use = head->fast_out(k);
0N/A if (use->is_Phi() && use->outcnt() > 0) {
0N/A Node* use_clone = old_new[use->_idx];
3811N/A _igvn.rehash_node_delayed(use); // Multiple edge updates
3811N/A use->set_req(LoopNode::EntryControl, use_clone->in(LoopNode::LoopBackControl));
0N/A use->set_req(LoopNode::LoopBackControl, C->top());
3811N/A _igvn.replace_input_of(use_clone, LoopNode::LoopBackControl, C->top());
0N/A }
0N/A }
0N/A
0N/A // Step 4: update dominator tree and dominator depth
0N/A
0N/A set_idom(head, orig_tail_clone, dd);
0N/A recompute_dom_depth();
0N/A
0N/A // Inhibit more partial peeling on this loop
0N/A new_head_clone->set_partial_peel_loop();
0N/A C->set_major_progress();
0N/A
0N/A#if !defined(PRODUCT)
0N/A if (TracePartialPeeling) {
0N/A tty->print_cr("\nafter partial peel one iteration");
0N/A Node_List wl(area);
0N/A Node* t = last_peel;
0N/A while (true) {
0N/A wl.push(t);
0N/A if (t == head_clone) break;
0N/A t = idom(t);
0N/A }
0N/A while (wl.size() > 0) {
0N/A Node* tt = wl.pop();
0N/A if (tt == head) tty->print_cr("orig head");
0N/A else if (tt == new_head_clone) tty->print_cr("new head");
0N/A else if (tt == head_clone) tty->print_cr("clone head");
0N/A tt->dump();
0N/A }
0N/A }
0N/A#endif
0N/A return true;
0N/A}
0N/A
0N/A//------------------------------reorg_offsets----------------------------------
0N/A// Reorganize offset computations to lower register pressure. Mostly
0N/A// prevent loop-fallout uses of the pre-incremented trip counter (which are
0N/A// then alive with the post-incremented trip counter forcing an extra
0N/A// register move)
2230N/Avoid PhaseIdealLoop::reorg_offsets(IdealLoopTree *loop) {
2230N/A // Perform it only for canonical counted loops.
2230N/A // Loop's shape could be messed up by iteration_split_impl.
2230N/A if (!loop->_head->is_CountedLoop())
2230N/A return;
2230N/A if (!loop->_head->as_Loop()->is_valid_counted_loop())
2230N/A return;
0N/A
0N/A CountedLoopNode *cl = loop->_head->as_CountedLoop();
0N/A CountedLoopEndNode *cle = cl->loopexit();
0N/A Node *exit = cle->proj_out(false);
2230N/A Node *phi = cl->phi();
0N/A
0N/A // Check for the special case of folks using the pre-incremented
0N/A // trip-counter on the fall-out path (forces the pre-incremented
0N/A // and post-incremented trip counter to be live at the same time).
0N/A // Fix this by adjusting to use the post-increment trip counter.
367N/A
0N/A bool progress = true;
0N/A while (progress) {
0N/A progress = false;
0N/A for (DUIterator_Fast imax, i = phi->fast_outs(imax); i < imax; i++) {
0N/A Node* use = phi->fast_out(i); // User of trip-counter
0N/A if (!has_ctrl(use)) continue;
0N/A Node *u_ctrl = get_ctrl(use);
2230N/A if (use->is_Phi()) {
0N/A u_ctrl = NULL;
2230N/A for (uint j = 1; j < use->req(); j++)
2230N/A if (use->in(j) == phi)
2230N/A u_ctrl = dom_lca(u_ctrl, use->in(0)->in(j));
0N/A }
0N/A IdealLoopTree *u_loop = get_loop(u_ctrl);
0N/A // Look for loop-invariant use
2230N/A if (u_loop == loop) continue;
2230N/A if (loop->is_member(u_loop)) continue;
0N/A // Check that use is live out the bottom. Assuming the trip-counter
0N/A // update is right at the bottom, uses of of the loop middle are ok.
2230N/A if (dom_lca(exit, u_ctrl) != exit) continue;
0N/A // Hit! Refactor use to use the post-incremented tripcounter.
0N/A // Compute a post-increment tripcounter.
4022N/A Node *opaq = new (C) Opaque2Node( C, cle->incr() );
0N/A register_new_node( opaq, u_ctrl );
0N/A Node *neg_stride = _igvn.intcon(-cle->stride_con());
0N/A set_ctrl(neg_stride, C->root());
4022N/A Node *post = new (C) AddINode( opaq, neg_stride);
0N/A register_new_node( post, u_ctrl );
3811N/A _igvn.rehash_node_delayed(use);
2230N/A for (uint j = 1; j < use->req(); j++) {
2230N/A if (use->in(j) == phi)
0N/A use->set_req(j, post);
2230N/A }
0N/A // Since DU info changed, rerun loop
0N/A progress = true;
0N/A break;
0N/A }
0N/A }
0N/A
0N/A}