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