src/share/vm/opto/loopopts.cpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "memory/allocation.inline.hpp"
aoqi@0 27 #include "opto/addnode.hpp"
aoqi@0 28 #include "opto/connode.hpp"
aoqi@0 29 #include "opto/divnode.hpp"
aoqi@0 30 #include "opto/loopnode.hpp"
aoqi@0 31 #include "opto/matcher.hpp"
aoqi@0 32 #include "opto/mulnode.hpp"
aoqi@0 33 #include "opto/rootnode.hpp"
aoqi@0 34 #include "opto/subnode.hpp"
aoqi@0 35
aoqi@0 36 //=============================================================================
aoqi@0 37 //------------------------------split_thru_phi---------------------------------
aoqi@0 38 // Split Node 'n' through merge point if there is enough win.
aoqi@0 39 Node *PhaseIdealLoop::split_thru_phi( Node *n, Node *region, int policy ) {
aoqi@0 40 if (n->Opcode() == Op_ConvI2L && n->bottom_type() != TypeLong::LONG) {
aoqi@0 41 // ConvI2L may have type information on it which is unsafe to push up
aoqi@0 42 // so disable this for now
aoqi@0 43 return NULL;
aoqi@0 44 }
aoqi@0 45
aoqi@0 46 int wins = 0;
aoqi@0 47 assert(!n->is_CFG(), "");
aoqi@0 48 assert(region->is_Region(), "");
aoqi@0 49
aoqi@0 50 const Type* type = n->bottom_type();
aoqi@0 51 const TypeOopPtr *t_oop = _igvn.type(n)->isa_oopptr();
aoqi@0 52 Node *phi;
aoqi@0 53 if (t_oop != NULL && t_oop->is_known_instance_field()) {
aoqi@0 54 int iid = t_oop->instance_id();
aoqi@0 55 int index = C->get_alias_index(t_oop);
aoqi@0 56 int offset = t_oop->offset();
aoqi@0 57 phi = new (C) PhiNode(region, type, NULL, iid, index, offset);
aoqi@0 58 } else {
aoqi@0 59 phi = PhiNode::make_blank(region, n);
aoqi@0 60 }
aoqi@0 61 uint old_unique = C->unique();
aoqi@0 62 for (uint i = 1; i < region->req(); i++) {
aoqi@0 63 Node *x;
aoqi@0 64 Node* the_clone = NULL;
aoqi@0 65 if (region->in(i) == C->top()) {
aoqi@0 66 x = C->top(); // Dead path? Use a dead data op
aoqi@0 67 } else {
aoqi@0 68 x = n->clone(); // Else clone up the data op
aoqi@0 69 the_clone = x; // Remember for possible deletion.
aoqi@0 70 // Alter data node to use pre-phi inputs
aoqi@0 71 if (n->in(0) == region)
aoqi@0 72 x->set_req( 0, region->in(i) );
aoqi@0 73 for (uint j = 1; j < n->req(); j++) {
aoqi@0 74 Node *in = n->in(j);
aoqi@0 75 if (in->is_Phi() && in->in(0) == region)
aoqi@0 76 x->set_req( j, in->in(i) ); // Use pre-Phi input for the clone
aoqi@0 77 }
aoqi@0 78 }
aoqi@0 79 // Check for a 'win' on some paths
aoqi@0 80 const Type *t = x->Value(&_igvn);
aoqi@0 81
aoqi@0 82 bool singleton = t->singleton();
aoqi@0 83
aoqi@0 84 // A TOP singleton indicates that there are no possible values incoming
aoqi@0 85 // along a particular edge. In most cases, this is OK, and the Phi will
aoqi@0 86 // be eliminated later in an Ideal call. However, we can't allow this to
aoqi@0 87 // happen if the singleton occurs on loop entry, as the elimination of
aoqi@0 88 // the PhiNode may cause the resulting node to migrate back to a previous
aoqi@0 89 // loop iteration.
aoqi@0 90 if (singleton && t == Type::TOP) {
aoqi@0 91 // Is_Loop() == false does not confirm the absence of a loop (e.g., an
aoqi@0 92 // irreducible loop may not be indicated by an affirmative is_Loop());
aoqi@0 93 // therefore, the only top we can split thru a phi is on a backedge of
aoqi@0 94 // a loop.
aoqi@0 95 singleton &= region->is_Loop() && (i != LoopNode::EntryControl);
aoqi@0 96 }
aoqi@0 97
aoqi@0 98 if (singleton) {
aoqi@0 99 wins++;
aoqi@0 100 x = ((PhaseGVN&)_igvn).makecon(t);
aoqi@0 101 } else {
aoqi@0 102 // We now call Identity to try to simplify the cloned node.
aoqi@0 103 // Note that some Identity methods call phase->type(this).
aoqi@0 104 // Make sure that the type array is big enough for
aoqi@0 105 // our new node, even though we may throw the node away.
aoqi@0 106 // (Note: This tweaking with igvn only works because x is a new node.)
aoqi@0 107 _igvn.set_type(x, t);
aoqi@0 108 // If x is a TypeNode, capture any more-precise type permanently into Node
aoqi@0 109 // otherwise it will be not updated during igvn->transform since
aoqi@0 110 // igvn->type(x) is set to x->Value() already.
aoqi@0 111 x->raise_bottom_type(t);
aoqi@0 112 Node *y = x->Identity(&_igvn);
aoqi@0 113 if (y != x) {
aoqi@0 114 wins++;
aoqi@0 115 x = y;
aoqi@0 116 } else {
aoqi@0 117 y = _igvn.hash_find(x);
aoqi@0 118 if (y) {
aoqi@0 119 wins++;
aoqi@0 120 x = y;
aoqi@0 121 } else {
aoqi@0 122 // Else x is a new node we are keeping
aoqi@0 123 // We do not need register_new_node_with_optimizer
aoqi@0 124 // because set_type has already been called.
aoqi@0 125 _igvn._worklist.push(x);
aoqi@0 126 }
aoqi@0 127 }
aoqi@0 128 }
aoqi@0 129 if (x != the_clone && the_clone != NULL)
aoqi@0 130 _igvn.remove_dead_node(the_clone);
aoqi@0 131 phi->set_req( i, x );
aoqi@0 132 }
aoqi@0 133 // Too few wins?
aoqi@0 134 if (wins <= policy) {
aoqi@0 135 _igvn.remove_dead_node(phi);
aoqi@0 136 return NULL;
aoqi@0 137 }
aoqi@0 138
aoqi@0 139 // Record Phi
aoqi@0 140 register_new_node( phi, region );
aoqi@0 141
aoqi@0 142 for (uint i2 = 1; i2 < phi->req(); i2++) {
aoqi@0 143 Node *x = phi->in(i2);
aoqi@0 144 // If we commoned up the cloned 'x' with another existing Node,
aoqi@0 145 // the existing Node picks up a new use. We need to make the
aoqi@0 146 // existing Node occur higher up so it dominates its uses.
aoqi@0 147 Node *old_ctrl;
aoqi@0 148 IdealLoopTree *old_loop;
aoqi@0 149
aoqi@0 150 if (x->is_Con()) {
aoqi@0 151 // Constant's control is always root.
aoqi@0 152 set_ctrl(x, C->root());
aoqi@0 153 continue;
aoqi@0 154 }
aoqi@0 155 // The occasional new node
aoqi@0 156 if (x->_idx >= old_unique) { // Found a new, unplaced node?
aoqi@0 157 old_ctrl = NULL;
aoqi@0 158 old_loop = NULL; // Not in any prior loop
aoqi@0 159 } else {
aoqi@0 160 old_ctrl = get_ctrl(x);
aoqi@0 161 old_loop = get_loop(old_ctrl); // Get prior loop
aoqi@0 162 }
aoqi@0 163 // New late point must dominate new use
aoqi@0 164 Node *new_ctrl = dom_lca(old_ctrl, region->in(i2));
aoqi@0 165 if (new_ctrl == old_ctrl) // Nothing is changed
aoqi@0 166 continue;
aoqi@0 167
aoqi@0 168 IdealLoopTree *new_loop = get_loop(new_ctrl);
aoqi@0 169
aoqi@0 170 // Don't move x into a loop if its uses are
aoqi@0 171 // outside of loop. Otherwise x will be cloned
aoqi@0 172 // for each use outside of this loop.
aoqi@0 173 IdealLoopTree *use_loop = get_loop(region);
aoqi@0 174 if (!new_loop->is_member(use_loop) &&
aoqi@0 175 (old_loop == NULL || !new_loop->is_member(old_loop))) {
aoqi@0 176 // Take early control, later control will be recalculated
aoqi@0 177 // during next iteration of loop optimizations.
aoqi@0 178 new_ctrl = get_early_ctrl(x);
aoqi@0 179 new_loop = get_loop(new_ctrl);
aoqi@0 180 }
aoqi@0 181 // Set new location
aoqi@0 182 set_ctrl(x, new_ctrl);
aoqi@0 183 // If changing loop bodies, see if we need to collect into new body
aoqi@0 184 if (old_loop != new_loop) {
aoqi@0 185 if (old_loop && !old_loop->_child)
aoqi@0 186 old_loop->_body.yank(x);
aoqi@0 187 if (!new_loop->_child)
aoqi@0 188 new_loop->_body.push(x); // Collect body info
aoqi@0 189 }
aoqi@0 190 }
aoqi@0 191
aoqi@0 192 return phi;
aoqi@0 193 }
aoqi@0 194
aoqi@0 195 //------------------------------dominated_by------------------------------------
aoqi@0 196 // Replace the dominated test with an obvious true or false. Place it on the
aoqi@0 197 // IGVN worklist for later cleanup. Move control-dependent data Nodes on the
aoqi@0 198 // live path up to the dominating control.
aoqi@0 199 void PhaseIdealLoop::dominated_by( Node *prevdom, Node *iff, bool flip, bool exclude_loop_predicate ) {
aoqi@0 200 #ifndef PRODUCT
aoqi@0 201 if (VerifyLoopOptimizations && PrintOpto) tty->print_cr("dominating test");
aoqi@0 202 #endif
aoqi@0 203
aoqi@0 204
aoqi@0 205 // prevdom is the dominating projection of the dominating test.
aoqi@0 206 assert( iff->is_If(), "" );
aoqi@0 207 assert( iff->Opcode() == Op_If || iff->Opcode() == Op_CountedLoopEnd, "Check this code when new subtype is added");
aoqi@0 208 int pop = prevdom->Opcode();
aoqi@0 209 assert( pop == Op_IfFalse || pop == Op_IfTrue, "" );
aoqi@0 210 if (flip) {
aoqi@0 211 if (pop == Op_IfTrue)
aoqi@0 212 pop = Op_IfFalse;
aoqi@0 213 else
aoqi@0 214 pop = Op_IfTrue;
aoqi@0 215 }
aoqi@0 216 // 'con' is set to true or false to kill the dominated test.
aoqi@0 217 Node *con = _igvn.makecon(pop == Op_IfTrue ? TypeInt::ONE : TypeInt::ZERO);
aoqi@0 218 set_ctrl(con, C->root()); // Constant gets a new use
aoqi@0 219 // Hack the dominated test
aoqi@0 220 _igvn.replace_input_of(iff, 1, con);
aoqi@0 221
aoqi@0 222 // If I dont have a reachable TRUE and FALSE path following the IfNode then
aoqi@0 223 // I can assume this path reaches an infinite loop. In this case it's not
aoqi@0 224 // important to optimize the data Nodes - either the whole compilation will
aoqi@0 225 // be tossed or this path (and all data Nodes) will go dead.
aoqi@0 226 if (iff->outcnt() != 2) return;
aoqi@0 227
aoqi@0 228 // Make control-dependent data Nodes on the live path (path that will remain
aoqi@0 229 // once the dominated IF is removed) become control-dependent on the
aoqi@0 230 // dominating projection.
aoqi@0 231 Node* dp = iff->as_If()->proj_out(pop == Op_IfTrue);
aoqi@0 232
aoqi@0 233 // Loop predicates may have depending checks which should not
aoqi@0 234 // be skipped. For example, range check predicate has two checks
aoqi@0 235 // for lower and upper bounds.
aoqi@0 236 if (dp == NULL)
aoqi@0 237 return;
aoqi@0 238
aoqi@0 239 ProjNode* dp_proj = dp->as_Proj();
aoqi@0 240 ProjNode* unc_proj = iff->as_If()->proj_out(1 - dp_proj->_con)->as_Proj();
aoqi@0 241 if (exclude_loop_predicate &&
aoqi@0 242 unc_proj->is_uncommon_trap_proj(Deoptimization::Reason_predicate))
aoqi@0 243 return; // Let IGVN transformation change control dependence.
aoqi@0 244
aoqi@0 245 IdealLoopTree *old_loop = get_loop(dp);
aoqi@0 246
aoqi@0 247 for (DUIterator_Fast imax, i = dp->fast_outs(imax); i < imax; i++) {
aoqi@0 248 Node* cd = dp->fast_out(i); // Control-dependent node
aoqi@0 249 if (cd->depends_only_on_test()) {
aoqi@0 250 assert(cd->in(0) == dp, "");
aoqi@0 251 _igvn.replace_input_of(cd, 0, prevdom);
aoqi@0 252 set_early_ctrl(cd);
aoqi@0 253 IdealLoopTree *new_loop = get_loop(get_ctrl(cd));
aoqi@0 254 if (old_loop != new_loop) {
aoqi@0 255 if (!old_loop->_child) old_loop->_body.yank(cd);
aoqi@0 256 if (!new_loop->_child) new_loop->_body.push(cd);
aoqi@0 257 }
aoqi@0 258 --i;
aoqi@0 259 --imax;
aoqi@0 260 }
aoqi@0 261 }
aoqi@0 262 }
aoqi@0 263
aoqi@0 264 //------------------------------has_local_phi_input----------------------------
aoqi@0 265 // Return TRUE if 'n' has Phi inputs from its local block and no other
aoqi@0 266 // block-local inputs (all non-local-phi inputs come from earlier blocks)
aoqi@0 267 Node *PhaseIdealLoop::has_local_phi_input( Node *n ) {
aoqi@0 268 Node *n_ctrl = get_ctrl(n);
aoqi@0 269 // See if some inputs come from a Phi in this block, or from before
aoqi@0 270 // this block.
aoqi@0 271 uint i;
aoqi@0 272 for( i = 1; i < n->req(); i++ ) {
aoqi@0 273 Node *phi = n->in(i);
aoqi@0 274 if( phi->is_Phi() && phi->in(0) == n_ctrl )
aoqi@0 275 break;
aoqi@0 276 }
aoqi@0 277 if( i >= n->req() )
aoqi@0 278 return NULL; // No Phi inputs; nowhere to clone thru
aoqi@0 279
aoqi@0 280 // Check for inputs created between 'n' and the Phi input. These
aoqi@0 281 // must split as well; they have already been given the chance
aoqi@0 282 // (courtesy of a post-order visit) and since they did not we must
aoqi@0 283 // recover the 'cost' of splitting them by being very profitable
aoqi@0 284 // when splitting 'n'. Since this is unlikely we simply give up.
aoqi@0 285 for( i = 1; i < n->req(); i++ ) {
aoqi@0 286 Node *m = n->in(i);
aoqi@0 287 if( get_ctrl(m) == n_ctrl && !m->is_Phi() ) {
aoqi@0 288 // We allow the special case of AddP's with no local inputs.
aoqi@0 289 // This allows us to split-up address expressions.
aoqi@0 290 if (m->is_AddP() &&
aoqi@0 291 get_ctrl(m->in(2)) != n_ctrl &&
aoqi@0 292 get_ctrl(m->in(3)) != n_ctrl) {
aoqi@0 293 // Move the AddP up to dominating point
aoqi@0 294 set_ctrl_and_loop(m, find_non_split_ctrl(idom(n_ctrl)));
aoqi@0 295 continue;
aoqi@0 296 }
aoqi@0 297 return NULL;
aoqi@0 298 }
aoqi@0 299 }
aoqi@0 300
aoqi@0 301 return n_ctrl;
aoqi@0 302 }
aoqi@0 303
aoqi@0 304 //------------------------------remix_address_expressions----------------------
aoqi@0 305 // Rework addressing expressions to get the most loop-invariant stuff
aoqi@0 306 // moved out. We'd like to do all associative operators, but it's especially
aoqi@0 307 // important (common) to do address expressions.
aoqi@0 308 Node *PhaseIdealLoop::remix_address_expressions( Node *n ) {
aoqi@0 309 if (!has_ctrl(n)) return NULL;
aoqi@0 310 Node *n_ctrl = get_ctrl(n);
aoqi@0 311 IdealLoopTree *n_loop = get_loop(n_ctrl);
aoqi@0 312
aoqi@0 313 // See if 'n' mixes loop-varying and loop-invariant inputs and
aoqi@0 314 // itself is loop-varying.
aoqi@0 315
aoqi@0 316 // Only interested in binary ops (and AddP)
aoqi@0 317 if( n->req() < 3 || n->req() > 4 ) return NULL;
aoqi@0 318
aoqi@0 319 Node *n1_ctrl = get_ctrl(n->in( 1));
aoqi@0 320 Node *n2_ctrl = get_ctrl(n->in( 2));
aoqi@0 321 Node *n3_ctrl = get_ctrl(n->in(n->req() == 3 ? 2 : 3));
aoqi@0 322 IdealLoopTree *n1_loop = get_loop( n1_ctrl );
aoqi@0 323 IdealLoopTree *n2_loop = get_loop( n2_ctrl );
aoqi@0 324 IdealLoopTree *n3_loop = get_loop( n3_ctrl );
aoqi@0 325
aoqi@0 326 // Does one of my inputs spin in a tighter loop than self?
aoqi@0 327 if( (n_loop->is_member( n1_loop ) && n_loop != n1_loop) ||
aoqi@0 328 (n_loop->is_member( n2_loop ) && n_loop != n2_loop) ||
aoqi@0 329 (n_loop->is_member( n3_loop ) && n_loop != n3_loop) )
aoqi@0 330 return NULL; // Leave well enough alone
aoqi@0 331
aoqi@0 332 // Is at least one of my inputs loop-invariant?
aoqi@0 333 if( n1_loop == n_loop &&
aoqi@0 334 n2_loop == n_loop &&
aoqi@0 335 n3_loop == n_loop )
aoqi@0 336 return NULL; // No loop-invariant inputs
aoqi@0 337
aoqi@0 338
aoqi@0 339 int n_op = n->Opcode();
aoqi@0 340
aoqi@0 341 // Replace expressions like ((V+I) << 2) with (V<<2 + I<<2).
aoqi@0 342 if( n_op == Op_LShiftI ) {
aoqi@0 343 // Scale is loop invariant
aoqi@0 344 Node *scale = n->in(2);
aoqi@0 345 Node *scale_ctrl = get_ctrl(scale);
aoqi@0 346 IdealLoopTree *scale_loop = get_loop(scale_ctrl );
aoqi@0 347 if( n_loop == scale_loop || !scale_loop->is_member( n_loop ) )
aoqi@0 348 return NULL;
aoqi@0 349 const TypeInt *scale_t = scale->bottom_type()->isa_int();
aoqi@0 350 if( scale_t && scale_t->is_con() && scale_t->get_con() >= 16 )
aoqi@0 351 return NULL; // Dont bother with byte/short masking
aoqi@0 352 // Add must vary with loop (else shift would be loop-invariant)
aoqi@0 353 Node *add = n->in(1);
aoqi@0 354 Node *add_ctrl = get_ctrl(add);
aoqi@0 355 IdealLoopTree *add_loop = get_loop(add_ctrl);
aoqi@0 356 //assert( n_loop == add_loop, "" );
aoqi@0 357 if( n_loop != add_loop ) return NULL; // happens w/ evil ZKM loops
aoqi@0 358
aoqi@0 359 // Convert I-V into I+ (0-V); same for V-I
aoqi@0 360 if( add->Opcode() == Op_SubI &&
aoqi@0 361 _igvn.type( add->in(1) ) != TypeInt::ZERO ) {
aoqi@0 362 Node *zero = _igvn.intcon(0);
aoqi@0 363 set_ctrl(zero, C->root());
aoqi@0 364 Node *neg = new (C) SubINode( _igvn.intcon(0), add->in(2) );
aoqi@0 365 register_new_node( neg, get_ctrl(add->in(2) ) );
aoqi@0 366 add = new (C) AddINode( add->in(1), neg );
aoqi@0 367 register_new_node( add, add_ctrl );
aoqi@0 368 }
aoqi@0 369 if( add->Opcode() != Op_AddI ) return NULL;
aoqi@0 370 // See if one add input is loop invariant
aoqi@0 371 Node *add_var = add->in(1);
aoqi@0 372 Node *add_var_ctrl = get_ctrl(add_var);
aoqi@0 373 IdealLoopTree *add_var_loop = get_loop(add_var_ctrl );
aoqi@0 374 Node *add_invar = add->in(2);
aoqi@0 375 Node *add_invar_ctrl = get_ctrl(add_invar);
aoqi@0 376 IdealLoopTree *add_invar_loop = get_loop(add_invar_ctrl );
aoqi@0 377 if( add_var_loop == n_loop ) {
aoqi@0 378 } else if( add_invar_loop == n_loop ) {
aoqi@0 379 // Swap to find the invariant part
aoqi@0 380 add_invar = add_var;
aoqi@0 381 add_invar_ctrl = add_var_ctrl;
aoqi@0 382 add_invar_loop = add_var_loop;
aoqi@0 383 add_var = add->in(2);
aoqi@0 384 Node *add_var_ctrl = get_ctrl(add_var);
aoqi@0 385 IdealLoopTree *add_var_loop = get_loop(add_var_ctrl );
aoqi@0 386 } else // Else neither input is loop invariant
aoqi@0 387 return NULL;
aoqi@0 388 if( n_loop == add_invar_loop || !add_invar_loop->is_member( n_loop ) )
aoqi@0 389 return NULL; // No invariant part of the add?
aoqi@0 390
aoqi@0 391 // Yes! Reshape address expression!
aoqi@0 392 Node *inv_scale = new (C) LShiftINode( add_invar, scale );
aoqi@0 393 Node *inv_scale_ctrl =
aoqi@0 394 dom_depth(add_invar_ctrl) > dom_depth(scale_ctrl) ?
aoqi@0 395 add_invar_ctrl : scale_ctrl;
aoqi@0 396 register_new_node( inv_scale, inv_scale_ctrl );
aoqi@0 397 Node *var_scale = new (C) LShiftINode( add_var, scale );
aoqi@0 398 register_new_node( var_scale, n_ctrl );
aoqi@0 399 Node *var_add = new (C) AddINode( var_scale, inv_scale );
aoqi@0 400 register_new_node( var_add, n_ctrl );
aoqi@0 401 _igvn.replace_node( n, var_add );
aoqi@0 402 return var_add;
aoqi@0 403 }
aoqi@0 404
aoqi@0 405 // Replace (I+V) with (V+I)
aoqi@0 406 if( n_op == Op_AddI ||
aoqi@0 407 n_op == Op_AddL ||
aoqi@0 408 n_op == Op_AddF ||
aoqi@0 409 n_op == Op_AddD ||
aoqi@0 410 n_op == Op_MulI ||
aoqi@0 411 n_op == Op_MulL ||
aoqi@0 412 n_op == Op_MulF ||
aoqi@0 413 n_op == Op_MulD ) {
aoqi@0 414 if( n2_loop == n_loop ) {
aoqi@0 415 assert( n1_loop != n_loop, "" );
aoqi@0 416 n->swap_edges(1, 2);
aoqi@0 417 }
aoqi@0 418 }
aoqi@0 419
aoqi@0 420 // Replace ((I1 +p V) +p I2) with ((I1 +p I2) +p V),
aoqi@0 421 // but not if I2 is a constant.
aoqi@0 422 if( n_op == Op_AddP ) {
aoqi@0 423 if( n2_loop == n_loop && n3_loop != n_loop ) {
aoqi@0 424 if( n->in(2)->Opcode() == Op_AddP && !n->in(3)->is_Con() ) {
aoqi@0 425 Node *n22_ctrl = get_ctrl(n->in(2)->in(2));
aoqi@0 426 Node *n23_ctrl = get_ctrl(n->in(2)->in(3));
aoqi@0 427 IdealLoopTree *n22loop = get_loop( n22_ctrl );
aoqi@0 428 IdealLoopTree *n23_loop = get_loop( n23_ctrl );
aoqi@0 429 if( n22loop != n_loop && n22loop->is_member(n_loop) &&
aoqi@0 430 n23_loop == n_loop ) {
aoqi@0 431 Node *add1 = new (C) AddPNode( n->in(1), n->in(2)->in(2), n->in(3) );
aoqi@0 432 // Stuff new AddP in the loop preheader
aoqi@0 433 register_new_node( add1, n_loop->_head->in(LoopNode::EntryControl) );
aoqi@0 434 Node *add2 = new (C) AddPNode( n->in(1), add1, n->in(2)->in(3) );
aoqi@0 435 register_new_node( add2, n_ctrl );
aoqi@0 436 _igvn.replace_node( n, add2 );
aoqi@0 437 return add2;
aoqi@0 438 }
aoqi@0 439 }
aoqi@0 440 }
aoqi@0 441
aoqi@0 442 // Replace (I1 +p (I2 + V)) with ((I1 +p I2) +p V)
aoqi@0 443 if( n2_loop != n_loop && n3_loop == n_loop ) {
aoqi@0 444 if( n->in(3)->Opcode() == Op_AddI ) {
aoqi@0 445 Node *V = n->in(3)->in(1);
aoqi@0 446 Node *I = n->in(3)->in(2);
aoqi@0 447 if( is_member(n_loop,get_ctrl(V)) ) {
aoqi@0 448 } else {
aoqi@0 449 Node *tmp = V; V = I; I = tmp;
aoqi@0 450 }
aoqi@0 451 if( !is_member(n_loop,get_ctrl(I)) ) {
aoqi@0 452 Node *add1 = new (C) AddPNode( n->in(1), n->in(2), I );
aoqi@0 453 // Stuff new AddP in the loop preheader
aoqi@0 454 register_new_node( add1, n_loop->_head->in(LoopNode::EntryControl) );
aoqi@0 455 Node *add2 = new (C) AddPNode( n->in(1), add1, V );
aoqi@0 456 register_new_node( add2, n_ctrl );
aoqi@0 457 _igvn.replace_node( n, add2 );
aoqi@0 458 return add2;
aoqi@0 459 }
aoqi@0 460 }
aoqi@0 461 }
aoqi@0 462 }
aoqi@0 463
aoqi@0 464 return NULL;
aoqi@0 465 }
aoqi@0 466
aoqi@0 467 //------------------------------conditional_move-------------------------------
aoqi@0 468 // Attempt to replace a Phi with a conditional move. We have some pretty
aoqi@0 469 // strict profitability requirements. All Phis at the merge point must
aoqi@0 470 // be converted, so we can remove the control flow. We need to limit the
aoqi@0 471 // number of c-moves to a small handful. All code that was in the side-arms
aoqi@0 472 // of the CFG diamond is now speculatively executed. This code has to be
aoqi@0 473 // "cheap enough". We are pretty much limited to CFG diamonds that merge
aoqi@0 474 // 1 or 2 items with a total of 1 or 2 ops executed speculatively.
aoqi@0 475 Node *PhaseIdealLoop::conditional_move( Node *region ) {
aoqi@0 476
aoqi@0 477 assert(region->is_Region(), "sanity check");
aoqi@0 478 if (region->req() != 3) return NULL;
aoqi@0 479
aoqi@0 480 // Check for CFG diamond
aoqi@0 481 Node *lp = region->in(1);
aoqi@0 482 Node *rp = region->in(2);
aoqi@0 483 if (!lp || !rp) return NULL;
aoqi@0 484 Node *lp_c = lp->in(0);
aoqi@0 485 if (lp_c == NULL || lp_c != rp->in(0) || !lp_c->is_If()) return NULL;
aoqi@0 486 IfNode *iff = lp_c->as_If();
aoqi@0 487
aoqi@0 488 // Check for ops pinned in an arm of the diamond.
aoqi@0 489 // Can't remove the control flow in this case
aoqi@0 490 if (lp->outcnt() > 1) return NULL;
aoqi@0 491 if (rp->outcnt() > 1) return NULL;
aoqi@0 492
aoqi@0 493 IdealLoopTree* r_loop = get_loop(region);
aoqi@0 494 assert(r_loop == get_loop(iff), "sanity");
aoqi@0 495 // Always convert to CMOVE if all results are used only outside this loop.
aoqi@0 496 bool used_inside_loop = (r_loop == _ltree_root);
aoqi@0 497
aoqi@0 498 // Check profitability
aoqi@0 499 int cost = 0;
aoqi@0 500 int phis = 0;
aoqi@0 501 for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) {
aoqi@0 502 Node *out = region->fast_out(i);
aoqi@0 503 if (!out->is_Phi()) continue; // Ignore other control edges, etc
aoqi@0 504 phis++;
aoqi@0 505 PhiNode* phi = out->as_Phi();
aoqi@0 506 BasicType bt = phi->type()->basic_type();
aoqi@0 507 switch (bt) {
aoqi@0 508 case T_FLOAT:
aoqi@0 509 case T_DOUBLE: {
aoqi@0 510 cost += Matcher::float_cmove_cost(); // Could be very expensive
aoqi@0 511 break;
aoqi@0 512 }
aoqi@0 513 case T_LONG: {
aoqi@0 514 cost += Matcher::long_cmove_cost(); // May encodes as 2 CMOV's
aoqi@0 515 }
aoqi@0 516 case T_INT: // These all CMOV fine
aoqi@0 517 case T_ADDRESS: { // (RawPtr)
aoqi@0 518 cost++;
aoqi@0 519 break;
aoqi@0 520 }
aoqi@0 521 case T_NARROWOOP: // Fall through
aoqi@0 522 case T_OBJECT: { // Base oops are OK, but not derived oops
aoqi@0 523 const TypeOopPtr *tp = phi->type()->make_ptr()->isa_oopptr();
aoqi@0 524 // Derived pointers are Bad (tm): what's the Base (for GC purposes) of a
aoqi@0 525 // CMOVE'd derived pointer? It's a CMOVE'd derived base. Thus
aoqi@0 526 // CMOVE'ing a derived pointer requires we also CMOVE the base. If we
aoqi@0 527 // have a Phi for the base here that we convert to a CMOVE all is well
aoqi@0 528 // and good. But if the base is dead, we'll not make a CMOVE. Later
aoqi@0 529 // the allocator will have to produce a base by creating a CMOVE of the
aoqi@0 530 // relevant bases. This puts the allocator in the business of
aoqi@0 531 // manufacturing expensive instructions, generally a bad plan.
aoqi@0 532 // Just Say No to Conditionally-Moved Derived Pointers.
aoqi@0 533 if (tp && tp->offset() != 0)
aoqi@0 534 return NULL;
aoqi@0 535 cost++;
aoqi@0 536 break;
aoqi@0 537 }
aoqi@0 538 default:
aoqi@0 539 return NULL; // In particular, can't do memory or I/O
aoqi@0 540 }
aoqi@0 541 // Add in cost any speculative ops
aoqi@0 542 for (uint j = 1; j < region->req(); j++) {
aoqi@0 543 Node *proj = region->in(j);
aoqi@0 544 Node *inp = phi->in(j);
aoqi@0 545 if (get_ctrl(inp) == proj) { // Found local op
aoqi@0 546 cost++;
aoqi@0 547 // Check for a chain of dependent ops; these will all become
aoqi@0 548 // speculative in a CMOV.
aoqi@0 549 for (uint k = 1; k < inp->req(); k++)
aoqi@0 550 if (get_ctrl(inp->in(k)) == proj)
aoqi@0 551 cost += ConditionalMoveLimit; // Too much speculative goo
aoqi@0 552 }
aoqi@0 553 }
aoqi@0 554 // See if the Phi is used by a Cmp or Narrow oop Decode/Encode.
aoqi@0 555 // This will likely Split-If, a higher-payoff operation.
aoqi@0 556 for (DUIterator_Fast kmax, k = phi->fast_outs(kmax); k < kmax; k++) {
aoqi@0 557 Node* use = phi->fast_out(k);
aoqi@0 558 if (use->is_Cmp() || use->is_DecodeNarrowPtr() || use->is_EncodeNarrowPtr())
aoqi@0 559 cost += ConditionalMoveLimit;
aoqi@0 560 // Is there a use inside the loop?
aoqi@0 561 // Note: check only basic types since CMoveP is pinned.
aoqi@0 562 if (!used_inside_loop && is_java_primitive(bt)) {
aoqi@0 563 IdealLoopTree* u_loop = get_loop(has_ctrl(use) ? get_ctrl(use) : use);
aoqi@0 564 if (r_loop == u_loop || r_loop->is_member(u_loop)) {
aoqi@0 565 used_inside_loop = true;
aoqi@0 566 }
aoqi@0 567 }
aoqi@0 568 }
aoqi@0 569 }
aoqi@0 570 Node* bol = iff->in(1);
aoqi@0 571 assert(bol->Opcode() == Op_Bool, "");
aoqi@0 572 int cmp_op = bol->in(1)->Opcode();
aoqi@0 573 // It is expensive to generate flags from a float compare.
aoqi@0 574 // Avoid duplicated float compare.
aoqi@0 575 if (phis > 1 && (cmp_op == Op_CmpF || cmp_op == Op_CmpD)) return NULL;
aoqi@0 576
aoqi@0 577 float infrequent_prob = PROB_UNLIKELY_MAG(3);
aoqi@0 578 // Ignore cost and blocks frequency if CMOVE can be moved outside the loop.
aoqi@0 579 if (used_inside_loop) {
aoqi@0 580 if (cost >= ConditionalMoveLimit) return NULL; // Too much goo
aoqi@0 581
aoqi@0 582 // BlockLayoutByFrequency optimization moves infrequent branch
aoqi@0 583 // from hot path. No point in CMOV'ing in such case (110 is used
aoqi@0 584 // instead of 100 to take into account not exactness of float value).
aoqi@0 585 if (BlockLayoutByFrequency) {
aoqi@0 586 infrequent_prob = MAX2(infrequent_prob, (float)BlockLayoutMinDiamondPercentage/110.0f);
aoqi@0 587 }
aoqi@0 588 }
aoqi@0 589 // Check for highly predictable branch. No point in CMOV'ing if
aoqi@0 590 // we are going to predict accurately all the time.
aoqi@0 591 if (iff->_prob < infrequent_prob ||
aoqi@0 592 iff->_prob > (1.0f - infrequent_prob))
aoqi@0 593 return NULL;
aoqi@0 594
aoqi@0 595 // --------------
aoqi@0 596 // Now replace all Phis with CMOV's
aoqi@0 597 Node *cmov_ctrl = iff->in(0);
aoqi@0 598 uint flip = (lp->Opcode() == Op_IfTrue);
aoqi@0 599 while (1) {
aoqi@0 600 PhiNode* phi = NULL;
aoqi@0 601 for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) {
aoqi@0 602 Node *out = region->fast_out(i);
aoqi@0 603 if (out->is_Phi()) {
aoqi@0 604 phi = out->as_Phi();
aoqi@0 605 break;
aoqi@0 606 }
aoqi@0 607 }
aoqi@0 608 if (phi == NULL) break;
aoqi@0 609 #ifndef PRODUCT
aoqi@0 610 if (PrintOpto && VerifyLoopOptimizations) tty->print_cr("CMOV");
aoqi@0 611 #endif
aoqi@0 612 // Move speculative ops
aoqi@0 613 for (uint j = 1; j < region->req(); j++) {
aoqi@0 614 Node *proj = region->in(j);
aoqi@0 615 Node *inp = phi->in(j);
aoqi@0 616 if (get_ctrl(inp) == proj) { // Found local op
aoqi@0 617 #ifndef PRODUCT
aoqi@0 618 if (PrintOpto && VerifyLoopOptimizations) {
aoqi@0 619 tty->print(" speculate: ");
aoqi@0 620 inp->dump();
aoqi@0 621 }
aoqi@0 622 #endif
aoqi@0 623 set_ctrl(inp, cmov_ctrl);
aoqi@0 624 }
aoqi@0 625 }
aoqi@0 626 Node *cmov = CMoveNode::make( C, cmov_ctrl, iff->in(1), phi->in(1+flip), phi->in(2-flip), _igvn.type(phi) );
aoqi@0 627 register_new_node( cmov, cmov_ctrl );
aoqi@0 628 _igvn.replace_node( phi, cmov );
aoqi@0 629 #ifndef PRODUCT
aoqi@0 630 if (TraceLoopOpts) {
aoqi@0 631 tty->print("CMOV ");
aoqi@0 632 r_loop->dump_head();
aoqi@0 633 if (Verbose) {
aoqi@0 634 bol->in(1)->dump(1);
aoqi@0 635 cmov->dump(1);
aoqi@0 636 }
aoqi@0 637 }
aoqi@0 638 if (VerifyLoopOptimizations) verify();
aoqi@0 639 #endif
aoqi@0 640 }
aoqi@0 641
aoqi@0 642 // The useless CFG diamond will fold up later; see the optimization in
aoqi@0 643 // RegionNode::Ideal.
aoqi@0 644 _igvn._worklist.push(region);
aoqi@0 645
aoqi@0 646 return iff->in(1);
aoqi@0 647 }
aoqi@0 648
aoqi@0 649 //------------------------------split_if_with_blocks_pre-----------------------
aoqi@0 650 // Do the real work in a non-recursive function. Data nodes want to be
aoqi@0 651 // cloned in the pre-order so they can feed each other nicely.
aoqi@0 652 Node *PhaseIdealLoop::split_if_with_blocks_pre( Node *n ) {
aoqi@0 653 // Cloning these guys is unlikely to win
aoqi@0 654 int n_op = n->Opcode();
aoqi@0 655 if( n_op == Op_MergeMem ) return n;
aoqi@0 656 if( n->is_Proj() ) return n;
aoqi@0 657 // Do not clone-up CmpFXXX variations, as these are always
aoqi@0 658 // followed by a CmpI
aoqi@0 659 if( n->is_Cmp() ) return n;
aoqi@0 660 // Attempt to use a conditional move instead of a phi/branch
aoqi@0 661 if( ConditionalMoveLimit > 0 && n_op == Op_Region ) {
aoqi@0 662 Node *cmov = conditional_move( n );
aoqi@0 663 if( cmov ) return cmov;
aoqi@0 664 }
aoqi@0 665 if( n->is_CFG() || n->is_LoadStore() )
aoqi@0 666 return n;
aoqi@0 667 if( n_op == Op_Opaque1 || // Opaque nodes cannot be mod'd
aoqi@0 668 n_op == Op_Opaque2 ) {
aoqi@0 669 if( !C->major_progress() ) // If chance of no more loop opts...
aoqi@0 670 _igvn._worklist.push(n); // maybe we'll remove them
aoqi@0 671 return n;
aoqi@0 672 }
aoqi@0 673
aoqi@0 674 if( n->is_Con() ) return n; // No cloning for Con nodes
aoqi@0 675
aoqi@0 676 Node *n_ctrl = get_ctrl(n);
aoqi@0 677 if( !n_ctrl ) return n; // Dead node
aoqi@0 678
aoqi@0 679 // Attempt to remix address expressions for loop invariants
aoqi@0 680 Node *m = remix_address_expressions( n );
aoqi@0 681 if( m ) return m;
aoqi@0 682
aoqi@0 683 // Determine if the Node has inputs from some local Phi.
aoqi@0 684 // Returns the block to clone thru.
aoqi@0 685 Node *n_blk = has_local_phi_input( n );
aoqi@0 686 if( !n_blk ) return n;
aoqi@0 687 // Do not clone the trip counter through on a CountedLoop
aoqi@0 688 // (messes up the canonical shape).
aoqi@0 689 if( n_blk->is_CountedLoop() && n->Opcode() == Op_AddI ) return n;
aoqi@0 690
aoqi@0 691 // Check for having no control input; not pinned. Allow
aoqi@0 692 // dominating control.
aoqi@0 693 if( n->in(0) ) {
aoqi@0 694 Node *dom = idom(n_blk);
aoqi@0 695 if( dom_lca( n->in(0), dom ) != n->in(0) )
aoqi@0 696 return n;
aoqi@0 697 }
aoqi@0 698 // Policy: when is it profitable. You must get more wins than
aoqi@0 699 // policy before it is considered profitable. Policy is usually 0,
aoqi@0 700 // so 1 win is considered profitable. Big merges will require big
aoqi@0 701 // cloning, so get a larger policy.
aoqi@0 702 int policy = n_blk->req() >> 2;
aoqi@0 703
aoqi@0 704 // If the loop is a candidate for range check elimination,
aoqi@0 705 // delay splitting through it's phi until a later loop optimization
aoqi@0 706 if (n_blk->is_CountedLoop()) {
aoqi@0 707 IdealLoopTree *lp = get_loop(n_blk);
aoqi@0 708 if (lp && lp->_rce_candidate) {
aoqi@0 709 return n;
aoqi@0 710 }
aoqi@0 711 }
aoqi@0 712
aoqi@0 713 // Use same limit as split_if_with_blocks_post
aoqi@0 714 if( C->unique() > 35000 ) return n; // Method too big
aoqi@0 715
aoqi@0 716 // Split 'n' through the merge point if it is profitable
aoqi@0 717 Node *phi = split_thru_phi( n, n_blk, policy );
aoqi@0 718 if (!phi) return n;
aoqi@0 719
aoqi@0 720 // Found a Phi to split thru!
aoqi@0 721 // Replace 'n' with the new phi
aoqi@0 722 _igvn.replace_node( n, phi );
aoqi@0 723 // Moved a load around the loop, 'en-registering' something.
aoqi@0 724 if (n_blk->is_Loop() && n->is_Load() &&
aoqi@0 725 !phi->in(LoopNode::LoopBackControl)->is_Load())
aoqi@0 726 C->set_major_progress();
aoqi@0 727
aoqi@0 728 return phi;
aoqi@0 729 }
aoqi@0 730
aoqi@0 731 static bool merge_point_too_heavy(Compile* C, Node* region) {
aoqi@0 732 // Bail out if the region and its phis have too many users.
aoqi@0 733 int weight = 0;
aoqi@0 734 for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) {
aoqi@0 735 weight += region->fast_out(i)->outcnt();
aoqi@0 736 }
aoqi@0 737 int nodes_left = MaxNodeLimit - C->live_nodes();
aoqi@0 738 if (weight * 8 > nodes_left) {
aoqi@0 739 #ifndef PRODUCT
aoqi@0 740 if (PrintOpto)
aoqi@0 741 tty->print_cr("*** Split-if bails out: %d nodes, region weight %d", C->unique(), weight);
aoqi@0 742 #endif
aoqi@0 743 return true;
aoqi@0 744 } else {
aoqi@0 745 return false;
aoqi@0 746 }
aoqi@0 747 }
aoqi@0 748
aoqi@0 749 static bool merge_point_safe(Node* region) {
aoqi@0 750 // 4799512: Stop split_if_with_blocks from splitting a block with a ConvI2LNode
aoqi@0 751 // having a PhiNode input. This sidesteps the dangerous case where the split
aoqi@0 752 // ConvI2LNode may become TOP if the input Value() does not
aoqi@0 753 // overlap the ConvI2L range, leaving a node which may not dominate its
aoqi@0 754 // uses.
aoqi@0 755 // A better fix for this problem can be found in the BugTraq entry, but
aoqi@0 756 // expediency for Mantis demands this hack.
aoqi@0 757 // 6855164: If the merge point has a FastLockNode with a PhiNode input, we stop
aoqi@0 758 // split_if_with_blocks from splitting a block because we could not move around
aoqi@0 759 // the FastLockNode.
aoqi@0 760 for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) {
aoqi@0 761 Node* n = region->fast_out(i);
aoqi@0 762 if (n->is_Phi()) {
aoqi@0 763 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) {
aoqi@0 764 Node* m = n->fast_out(j);
aoqi@0 765 if (m->is_FastLock())
aoqi@0 766 return false;
aoqi@0 767 #ifdef _LP64
aoqi@0 768 if (m->Opcode() == Op_ConvI2L)
aoqi@0 769 return false;
aoqi@0 770 #endif
aoqi@0 771 }
aoqi@0 772 }
aoqi@0 773 }
aoqi@0 774 return true;
aoqi@0 775 }
aoqi@0 776
aoqi@0 777
aoqi@0 778 //------------------------------place_near_use---------------------------------
aoqi@0 779 // Place some computation next to use but not inside inner loops.
aoqi@0 780 // For inner loop uses move it to the preheader area.
aoqi@0 781 Node *PhaseIdealLoop::place_near_use( Node *useblock ) const {
aoqi@0 782 IdealLoopTree *u_loop = get_loop( useblock );
aoqi@0 783 return (u_loop->_irreducible || u_loop->_child)
aoqi@0 784 ? useblock
aoqi@0 785 : u_loop->_head->in(LoopNode::EntryControl);
aoqi@0 786 }
aoqi@0 787
aoqi@0 788
aoqi@0 789 //------------------------------split_if_with_blocks_post----------------------
aoqi@0 790 // Do the real work in a non-recursive function. CFG hackery wants to be
aoqi@0 791 // in the post-order, so it can dirty the I-DOM info and not use the dirtied
aoqi@0 792 // info.
aoqi@0 793 void PhaseIdealLoop::split_if_with_blocks_post( Node *n ) {
aoqi@0 794
aoqi@0 795 // Cloning Cmp through Phi's involves the split-if transform.
aoqi@0 796 // FastLock is not used by an If
aoqi@0 797 if( n->is_Cmp() && !n->is_FastLock() ) {
aoqi@0 798 if( C->unique() > 35000 ) return; // Method too big
aoqi@0 799
aoqi@0 800 // Do not do 'split-if' if irreducible loops are present.
aoqi@0 801 if( _has_irreducible_loops )
aoqi@0 802 return;
aoqi@0 803
aoqi@0 804 Node *n_ctrl = get_ctrl(n);
aoqi@0 805 // Determine if the Node has inputs from some local Phi.
aoqi@0 806 // Returns the block to clone thru.
aoqi@0 807 Node *n_blk = has_local_phi_input( n );
aoqi@0 808 if( n_blk != n_ctrl ) return;
aoqi@0 809
aoqi@0 810 if( merge_point_too_heavy(C, n_ctrl) )
aoqi@0 811 return;
aoqi@0 812
aoqi@0 813 if( n->outcnt() != 1 ) return; // Multiple bool's from 1 compare?
aoqi@0 814 Node *bol = n->unique_out();
aoqi@0 815 assert( bol->is_Bool(), "expect a bool here" );
aoqi@0 816 if( bol->outcnt() != 1 ) return;// Multiple branches from 1 compare?
aoqi@0 817 Node *iff = bol->unique_out();
aoqi@0 818
aoqi@0 819 // Check some safety conditions
aoqi@0 820 if( iff->is_If() ) { // Classic split-if?
aoqi@0 821 if( iff->in(0) != n_ctrl ) return; // Compare must be in same blk as if
aoqi@0 822 } else if (iff->is_CMove()) { // Trying to split-up a CMOVE
aoqi@0 823 // Can't split CMove with different control edge.
aoqi@0 824 if (iff->in(0) != NULL && iff->in(0) != n_ctrl ) return;
aoqi@0 825 if( get_ctrl(iff->in(2)) == n_ctrl ||
aoqi@0 826 get_ctrl(iff->in(3)) == n_ctrl )
aoqi@0 827 return; // Inputs not yet split-up
aoqi@0 828 if ( get_loop(n_ctrl) != get_loop(get_ctrl(iff)) ) {
aoqi@0 829 return; // Loop-invar test gates loop-varying CMOVE
aoqi@0 830 }
aoqi@0 831 } else {
aoqi@0 832 return; // some other kind of node, such as an Allocate
aoqi@0 833 }
aoqi@0 834
aoqi@0 835 // Do not do 'split-if' if some paths are dead. First do dead code
aoqi@0 836 // elimination and then see if its still profitable.
aoqi@0 837 for( uint i = 1; i < n_ctrl->req(); i++ )
aoqi@0 838 if( n_ctrl->in(i) == C->top() )
aoqi@0 839 return;
aoqi@0 840
aoqi@0 841 // When is split-if profitable? Every 'win' on means some control flow
aoqi@0 842 // goes dead, so it's almost always a win.
aoqi@0 843 int policy = 0;
aoqi@0 844 // If trying to do a 'Split-If' at the loop head, it is only
aoqi@0 845 // profitable if the cmp folds up on BOTH paths. Otherwise we
aoqi@0 846 // risk peeling a loop forever.
aoqi@0 847
aoqi@0 848 // CNC - Disabled for now. Requires careful handling of loop
aoqi@0 849 // body selection for the cloned code. Also, make sure we check
aoqi@0 850 // for any input path not being in the same loop as n_ctrl. For
aoqi@0 851 // irreducible loops we cannot check for 'n_ctrl->is_Loop()'
aoqi@0 852 // because the alternative loop entry points won't be converted
aoqi@0 853 // into LoopNodes.
aoqi@0 854 IdealLoopTree *n_loop = get_loop(n_ctrl);
aoqi@0 855 for( uint j = 1; j < n_ctrl->req(); j++ )
aoqi@0 856 if( get_loop(n_ctrl->in(j)) != n_loop )
aoqi@0 857 return;
aoqi@0 858
aoqi@0 859 // Check for safety of the merge point.
aoqi@0 860 if( !merge_point_safe(n_ctrl) ) {
aoqi@0 861 return;
aoqi@0 862 }
aoqi@0 863
aoqi@0 864 // Split compare 'n' through the merge point if it is profitable
aoqi@0 865 Node *phi = split_thru_phi( n, n_ctrl, policy );
aoqi@0 866 if( !phi ) return;
aoqi@0 867
aoqi@0 868 // Found a Phi to split thru!
aoqi@0 869 // Replace 'n' with the new phi
aoqi@0 870 _igvn.replace_node( n, phi );
aoqi@0 871
aoqi@0 872 // Now split the bool up thru the phi
aoqi@0 873 Node *bolphi = split_thru_phi( bol, n_ctrl, -1 );
aoqi@0 874 guarantee(bolphi != NULL, "null boolean phi node");
aoqi@0 875
aoqi@0 876 _igvn.replace_node( bol, bolphi );
aoqi@0 877 assert( iff->in(1) == bolphi, "" );
aoqi@0 878
aoqi@0 879 if( bolphi->Value(&_igvn)->singleton() )
aoqi@0 880 return;
aoqi@0 881
aoqi@0 882 // Conditional-move? Must split up now
aoqi@0 883 if( !iff->is_If() ) {
aoqi@0 884 Node *cmovphi = split_thru_phi( iff, n_ctrl, -1 );
aoqi@0 885 _igvn.replace_node( iff, cmovphi );
aoqi@0 886 return;
aoqi@0 887 }
aoqi@0 888
aoqi@0 889 // Now split the IF
aoqi@0 890 do_split_if( iff );
aoqi@0 891 return;
aoqi@0 892 }
aoqi@0 893
aoqi@0 894 // Check for an IF ready to split; one that has its
aoqi@0 895 // condition codes input coming from a Phi at the block start.
aoqi@0 896 int n_op = n->Opcode();
aoqi@0 897
aoqi@0 898 // Check for an IF being dominated by another IF same test
aoqi@0 899 if( n_op == Op_If ) {
aoqi@0 900 Node *bol = n->in(1);
aoqi@0 901 uint max = bol->outcnt();
aoqi@0 902 // Check for same test used more than once?
aoqi@0 903 if( n_op == Op_If && max > 1 && bol->is_Bool() ) {
aoqi@0 904 // Search up IDOMs to see if this IF is dominated.
aoqi@0 905 Node *cutoff = get_ctrl(bol);
aoqi@0 906
aoqi@0 907 // Now search up IDOMs till cutoff, looking for a dominating test
aoqi@0 908 Node *prevdom = n;
aoqi@0 909 Node *dom = idom(prevdom);
aoqi@0 910 while( dom != cutoff ) {
aoqi@0 911 if( dom->req() > 1 && dom->in(1) == bol && prevdom->in(0) == dom ) {
aoqi@0 912 // Replace the dominated test with an obvious true or false.
aoqi@0 913 // Place it on the IGVN worklist for later cleanup.
aoqi@0 914 C->set_major_progress();
aoqi@0 915 dominated_by( prevdom, n, false, true );
aoqi@0 916 #ifndef PRODUCT
aoqi@0 917 if( VerifyLoopOptimizations ) verify();
aoqi@0 918 #endif
aoqi@0 919 return;
aoqi@0 920 }
aoqi@0 921 prevdom = dom;
aoqi@0 922 dom = idom(prevdom);
aoqi@0 923 }
aoqi@0 924 }
aoqi@0 925 }
aoqi@0 926
aoqi@0 927 // See if a shared loop-varying computation has no loop-varying uses.
aoqi@0 928 // Happens if something is only used for JVM state in uncommon trap exits,
aoqi@0 929 // like various versions of induction variable+offset. Clone the
aoqi@0 930 // computation per usage to allow it to sink out of the loop.
aoqi@0 931 if (has_ctrl(n) && !n->in(0)) {// n not dead and has no control edge (can float about)
aoqi@0 932 Node *n_ctrl = get_ctrl(n);
aoqi@0 933 IdealLoopTree *n_loop = get_loop(n_ctrl);
aoqi@0 934 if( n_loop != _ltree_root ) {
aoqi@0 935 DUIterator_Fast imax, i = n->fast_outs(imax);
aoqi@0 936 for (; i < imax; i++) {
aoqi@0 937 Node* u = n->fast_out(i);
aoqi@0 938 if( !has_ctrl(u) ) break; // Found control user
aoqi@0 939 IdealLoopTree *u_loop = get_loop(get_ctrl(u));
aoqi@0 940 if( u_loop == n_loop ) break; // Found loop-varying use
aoqi@0 941 if( n_loop->is_member( u_loop ) ) break; // Found use in inner loop
aoqi@0 942 if( u->Opcode() == Op_Opaque1 ) break; // Found loop limit, bugfix for 4677003
aoqi@0 943 }
aoqi@0 944 bool did_break = (i < imax); // Did we break out of the previous loop?
aoqi@0 945 if (!did_break && n->outcnt() > 1) { // All uses in outer loops!
aoqi@0 946 Node *late_load_ctrl = NULL;
aoqi@0 947 if (n->is_Load()) {
aoqi@0 948 // If n is a load, get and save the result from get_late_ctrl(),
aoqi@0 949 // to be later used in calculating the control for n's clones.
aoqi@0 950 clear_dom_lca_tags();
aoqi@0 951 late_load_ctrl = get_late_ctrl(n, n_ctrl);
aoqi@0 952 }
aoqi@0 953 // If n is a load, and the late control is the same as the current
aoqi@0 954 // control, then the cloning of n is a pointless exercise, because
aoqi@0 955 // GVN will ensure that we end up where we started.
aoqi@0 956 if (!n->is_Load() || late_load_ctrl != n_ctrl) {
aoqi@0 957 for (DUIterator_Last jmin, j = n->last_outs(jmin); j >= jmin; ) {
aoqi@0 958 Node *u = n->last_out(j); // Clone private computation per use
aoqi@0 959 _igvn.rehash_node_delayed(u);
aoqi@0 960 Node *x = n->clone(); // Clone computation
aoqi@0 961 Node *x_ctrl = NULL;
aoqi@0 962 if( u->is_Phi() ) {
aoqi@0 963 // Replace all uses of normal nodes. Replace Phi uses
aoqi@0 964 // individually, so the separate Nodes can sink down
aoqi@0 965 // different paths.
aoqi@0 966 uint k = 1;
aoqi@0 967 while( u->in(k) != n ) k++;
aoqi@0 968 u->set_req( k, x );
aoqi@0 969 // x goes next to Phi input path
aoqi@0 970 x_ctrl = u->in(0)->in(k);
aoqi@0 971 --j;
aoqi@0 972 } else { // Normal use
aoqi@0 973 // Replace all uses
aoqi@0 974 for( uint k = 0; k < u->req(); k++ ) {
aoqi@0 975 if( u->in(k) == n ) {
aoqi@0 976 u->set_req( k, x );
aoqi@0 977 --j;
aoqi@0 978 }
aoqi@0 979 }
aoqi@0 980 x_ctrl = get_ctrl(u);
aoqi@0 981 }
aoqi@0 982
aoqi@0 983 // Find control for 'x' next to use but not inside inner loops.
aoqi@0 984 // For inner loop uses get the preheader area.
aoqi@0 985 x_ctrl = place_near_use(x_ctrl);
aoqi@0 986
aoqi@0 987 if (n->is_Load()) {
aoqi@0 988 // For loads, add a control edge to a CFG node outside of the loop
aoqi@0 989 // to force them to not combine and return back inside the loop
aoqi@0 990 // during GVN optimization (4641526).
aoqi@0 991 //
aoqi@0 992 // Because we are setting the actual control input, factor in
aoqi@0 993 // the result from get_late_ctrl() so we respect any
aoqi@0 994 // anti-dependences. (6233005).
aoqi@0 995 x_ctrl = dom_lca(late_load_ctrl, x_ctrl);
aoqi@0 996
aoqi@0 997 // Don't allow the control input to be a CFG splitting node.
aoqi@0 998 // Such nodes should only have ProjNodes as outs, e.g. IfNode
aoqi@0 999 // should only have IfTrueNode and IfFalseNode (4985384).
aoqi@0 1000 x_ctrl = find_non_split_ctrl(x_ctrl);
aoqi@0 1001 assert(dom_depth(n_ctrl) <= dom_depth(x_ctrl), "n is later than its clone");
aoqi@0 1002
aoqi@0 1003 x->set_req(0, x_ctrl);
aoqi@0 1004 }
aoqi@0 1005 register_new_node(x, x_ctrl);
aoqi@0 1006
aoqi@0 1007 // Some institutional knowledge is needed here: 'x' is
aoqi@0 1008 // yanked because if the optimizer runs GVN on it all the
aoqi@0 1009 // cloned x's will common up and undo this optimization and
aoqi@0 1010 // be forced back in the loop. This is annoying because it
aoqi@0 1011 // makes +VerifyOpto report false-positives on progress. I
aoqi@0 1012 // tried setting control edges on the x's to force them to
aoqi@0 1013 // not combine, but the matching gets worried when it tries
aoqi@0 1014 // to fold a StoreP and an AddP together (as part of an
aoqi@0 1015 // address expression) and the AddP and StoreP have
aoqi@0 1016 // different controls.
aoqi@0 1017 if (!x->is_Load() && !x->is_DecodeNarrowPtr()) _igvn._worklist.yank(x);
aoqi@0 1018 }
aoqi@0 1019 _igvn.remove_dead_node(n);
aoqi@0 1020 }
aoqi@0 1021 }
aoqi@0 1022 }
aoqi@0 1023 }
aoqi@0 1024
aoqi@0 1025 // Check for Opaque2's who's loop has disappeared - who's input is in the
aoqi@0 1026 // same loop nest as their output. Remove 'em, they are no longer useful.
aoqi@0 1027 if( n_op == Op_Opaque2 &&
aoqi@0 1028 n->in(1) != NULL &&
aoqi@0 1029 get_loop(get_ctrl(n)) == get_loop(get_ctrl(n->in(1))) ) {
aoqi@0 1030 _igvn.replace_node( n, n->in(1) );
aoqi@0 1031 }
aoqi@0 1032 }
aoqi@0 1033
aoqi@0 1034 //------------------------------split_if_with_blocks---------------------------
aoqi@0 1035 // Check for aggressive application of 'split-if' optimization,
aoqi@0 1036 // using basic block level info.
aoqi@0 1037 void PhaseIdealLoop::split_if_with_blocks( VectorSet &visited, Node_Stack &nstack ) {
aoqi@0 1038 Node *n = C->root();
aoqi@0 1039 visited.set(n->_idx); // first, mark node as visited
aoqi@0 1040 // Do pre-visit work for root
aoqi@0 1041 n = split_if_with_blocks_pre( n );
aoqi@0 1042 uint cnt = n->outcnt();
aoqi@0 1043 uint i = 0;
aoqi@0 1044 while (true) {
aoqi@0 1045 // Visit all children
aoqi@0 1046 if (i < cnt) {
aoqi@0 1047 Node* use = n->raw_out(i);
aoqi@0 1048 ++i;
aoqi@0 1049 if (use->outcnt() != 0 && !visited.test_set(use->_idx)) {
aoqi@0 1050 // Now do pre-visit work for this use
aoqi@0 1051 use = split_if_with_blocks_pre( use );
aoqi@0 1052 nstack.push(n, i); // Save parent and next use's index.
aoqi@0 1053 n = use; // Process all children of current use.
aoqi@0 1054 cnt = use->outcnt();
aoqi@0 1055 i = 0;
aoqi@0 1056 }
aoqi@0 1057 }
aoqi@0 1058 else {
aoqi@0 1059 // All of n's children have been processed, complete post-processing.
aoqi@0 1060 if (cnt != 0 && !n->is_Con()) {
aoqi@0 1061 assert(has_node(n), "no dead nodes");
aoqi@0 1062 split_if_with_blocks_post( n );
aoqi@0 1063 }
aoqi@0 1064 if (nstack.is_empty()) {
aoqi@0 1065 // Finished all nodes on stack.
aoqi@0 1066 break;
aoqi@0 1067 }
aoqi@0 1068 // Get saved parent node and next use's index. Visit the rest of uses.
aoqi@0 1069 n = nstack.node();
aoqi@0 1070 cnt = n->outcnt();
aoqi@0 1071 i = nstack.index();
aoqi@0 1072 nstack.pop();
aoqi@0 1073 }
aoqi@0 1074 }
aoqi@0 1075 }
aoqi@0 1076
aoqi@0 1077
aoqi@0 1078 //=============================================================================
aoqi@0 1079 //
aoqi@0 1080 // C L O N E A L O O P B O D Y
aoqi@0 1081 //
aoqi@0 1082
aoqi@0 1083 //------------------------------clone_iff--------------------------------------
aoqi@0 1084 // Passed in a Phi merging (recursively) some nearly equivalent Bool/Cmps.
aoqi@0 1085 // "Nearly" because all Nodes have been cloned from the original in the loop,
aoqi@0 1086 // but the fall-in edges to the Cmp are different. Clone bool/Cmp pairs
aoqi@0 1087 // through the Phi recursively, and return a Bool.
aoqi@0 1088 BoolNode *PhaseIdealLoop::clone_iff( PhiNode *phi, IdealLoopTree *loop ) {
aoqi@0 1089
aoqi@0 1090 // Convert this Phi into a Phi merging Bools
aoqi@0 1091 uint i;
aoqi@0 1092 for( i = 1; i < phi->req(); i++ ) {
aoqi@0 1093 Node *b = phi->in(i);
aoqi@0 1094 if( b->is_Phi() ) {
aoqi@0 1095 _igvn.replace_input_of(phi, i, clone_iff( b->as_Phi(), loop ));
aoqi@0 1096 } else {
aoqi@0 1097 assert( b->is_Bool(), "" );
aoqi@0 1098 }
aoqi@0 1099 }
aoqi@0 1100
aoqi@0 1101 Node *sample_bool = phi->in(1);
aoqi@0 1102 Node *sample_cmp = sample_bool->in(1);
aoqi@0 1103
aoqi@0 1104 // Make Phis to merge the Cmp's inputs.
aoqi@0 1105 PhiNode *phi1 = new (C) PhiNode( phi->in(0), Type::TOP );
aoqi@0 1106 PhiNode *phi2 = new (C) PhiNode( phi->in(0), Type::TOP );
aoqi@0 1107 for( i = 1; i < phi->req(); i++ ) {
aoqi@0 1108 Node *n1 = phi->in(i)->in(1)->in(1);
aoqi@0 1109 Node *n2 = phi->in(i)->in(1)->in(2);
aoqi@0 1110 phi1->set_req( i, n1 );
aoqi@0 1111 phi2->set_req( i, n2 );
aoqi@0 1112 phi1->set_type( phi1->type()->meet_speculative(n1->bottom_type()));
aoqi@0 1113 phi2->set_type( phi2->type()->meet_speculative(n2->bottom_type()));
aoqi@0 1114 }
aoqi@0 1115 // See if these Phis have been made before.
aoqi@0 1116 // Register with optimizer
aoqi@0 1117 Node *hit1 = _igvn.hash_find_insert(phi1);
aoqi@0 1118 if( hit1 ) { // Hit, toss just made Phi
aoqi@0 1119 _igvn.remove_dead_node(phi1); // Remove new phi
aoqi@0 1120 assert( hit1->is_Phi(), "" );
aoqi@0 1121 phi1 = (PhiNode*)hit1; // Use existing phi
aoqi@0 1122 } else { // Miss
aoqi@0 1123 _igvn.register_new_node_with_optimizer(phi1);
aoqi@0 1124 }
aoqi@0 1125 Node *hit2 = _igvn.hash_find_insert(phi2);
aoqi@0 1126 if( hit2 ) { // Hit, toss just made Phi
aoqi@0 1127 _igvn.remove_dead_node(phi2); // Remove new phi
aoqi@0 1128 assert( hit2->is_Phi(), "" );
aoqi@0 1129 phi2 = (PhiNode*)hit2; // Use existing phi
aoqi@0 1130 } else { // Miss
aoqi@0 1131 _igvn.register_new_node_with_optimizer(phi2);
aoqi@0 1132 }
aoqi@0 1133 // Register Phis with loop/block info
aoqi@0 1134 set_ctrl(phi1, phi->in(0));
aoqi@0 1135 set_ctrl(phi2, phi->in(0));
aoqi@0 1136 // Make a new Cmp
aoqi@0 1137 Node *cmp = sample_cmp->clone();
aoqi@0 1138 cmp->set_req( 1, phi1 );
aoqi@0 1139 cmp->set_req( 2, phi2 );
aoqi@0 1140 _igvn.register_new_node_with_optimizer(cmp);
aoqi@0 1141 set_ctrl(cmp, phi->in(0));
aoqi@0 1142
aoqi@0 1143 // Make a new Bool
aoqi@0 1144 Node *b = sample_bool->clone();
aoqi@0 1145 b->set_req(1,cmp);
aoqi@0 1146 _igvn.register_new_node_with_optimizer(b);
aoqi@0 1147 set_ctrl(b, phi->in(0));
aoqi@0 1148
aoqi@0 1149 assert( b->is_Bool(), "" );
aoqi@0 1150 return (BoolNode*)b;
aoqi@0 1151 }
aoqi@0 1152
aoqi@0 1153 //------------------------------clone_bool-------------------------------------
aoqi@0 1154 // Passed in a Phi merging (recursively) some nearly equivalent Bool/Cmps.
aoqi@0 1155 // "Nearly" because all Nodes have been cloned from the original in the loop,
aoqi@0 1156 // but the fall-in edges to the Cmp are different. Clone bool/Cmp pairs
aoqi@0 1157 // through the Phi recursively, and return a Bool.
aoqi@0 1158 CmpNode *PhaseIdealLoop::clone_bool( PhiNode *phi, IdealLoopTree *loop ) {
aoqi@0 1159 uint i;
aoqi@0 1160 // Convert this Phi into a Phi merging Bools
aoqi@0 1161 for( i = 1; i < phi->req(); i++ ) {
aoqi@0 1162 Node *b = phi->in(i);
aoqi@0 1163 if( b->is_Phi() ) {
aoqi@0 1164 _igvn.replace_input_of(phi, i, clone_bool( b->as_Phi(), loop ));
aoqi@0 1165 } else {
aoqi@0 1166 assert( b->is_Cmp() || b->is_top(), "inputs are all Cmp or TOP" );
aoqi@0 1167 }
aoqi@0 1168 }
aoqi@0 1169
aoqi@0 1170 Node *sample_cmp = phi->in(1);
aoqi@0 1171
aoqi@0 1172 // Make Phis to merge the Cmp's inputs.
aoqi@0 1173 PhiNode *phi1 = new (C) PhiNode( phi->in(0), Type::TOP );
aoqi@0 1174 PhiNode *phi2 = new (C) PhiNode( phi->in(0), Type::TOP );
aoqi@0 1175 for( uint j = 1; j < phi->req(); j++ ) {
aoqi@0 1176 Node *cmp_top = phi->in(j); // Inputs are all Cmp or TOP
aoqi@0 1177 Node *n1, *n2;
aoqi@0 1178 if( cmp_top->is_Cmp() ) {
aoqi@0 1179 n1 = cmp_top->in(1);
aoqi@0 1180 n2 = cmp_top->in(2);
aoqi@0 1181 } else {
aoqi@0 1182 n1 = n2 = cmp_top;
aoqi@0 1183 }
aoqi@0 1184 phi1->set_req( j, n1 );
aoqi@0 1185 phi2->set_req( j, n2 );
aoqi@0 1186 phi1->set_type(phi1->type()->meet_speculative(n1->bottom_type()));
aoqi@0 1187 phi2->set_type(phi2->type()->meet_speculative(n2->bottom_type()));
aoqi@0 1188 }
aoqi@0 1189
aoqi@0 1190 // See if these Phis have been made before.
aoqi@0 1191 // Register with optimizer
aoqi@0 1192 Node *hit1 = _igvn.hash_find_insert(phi1);
aoqi@0 1193 if( hit1 ) { // Hit, toss just made Phi
aoqi@0 1194 _igvn.remove_dead_node(phi1); // Remove new phi
aoqi@0 1195 assert( hit1->is_Phi(), "" );
aoqi@0 1196 phi1 = (PhiNode*)hit1; // Use existing phi
aoqi@0 1197 } else { // Miss
aoqi@0 1198 _igvn.register_new_node_with_optimizer(phi1);
aoqi@0 1199 }
aoqi@0 1200 Node *hit2 = _igvn.hash_find_insert(phi2);
aoqi@0 1201 if( hit2 ) { // Hit, toss just made Phi
aoqi@0 1202 _igvn.remove_dead_node(phi2); // Remove new phi
aoqi@0 1203 assert( hit2->is_Phi(), "" );
aoqi@0 1204 phi2 = (PhiNode*)hit2; // Use existing phi
aoqi@0 1205 } else { // Miss
aoqi@0 1206 _igvn.register_new_node_with_optimizer(phi2);
aoqi@0 1207 }
aoqi@0 1208 // Register Phis with loop/block info
aoqi@0 1209 set_ctrl(phi1, phi->in(0));
aoqi@0 1210 set_ctrl(phi2, phi->in(0));
aoqi@0 1211 // Make a new Cmp
aoqi@0 1212 Node *cmp = sample_cmp->clone();
aoqi@0 1213 cmp->set_req( 1, phi1 );
aoqi@0 1214 cmp->set_req( 2, phi2 );
aoqi@0 1215 _igvn.register_new_node_with_optimizer(cmp);
aoqi@0 1216 set_ctrl(cmp, phi->in(0));
aoqi@0 1217
aoqi@0 1218 assert( cmp->is_Cmp(), "" );
aoqi@0 1219 return (CmpNode*)cmp;
aoqi@0 1220 }
aoqi@0 1221
aoqi@0 1222 //------------------------------sink_use---------------------------------------
aoqi@0 1223 // If 'use' was in the loop-exit block, it now needs to be sunk
aoqi@0 1224 // below the post-loop merge point.
aoqi@0 1225 void PhaseIdealLoop::sink_use( Node *use, Node *post_loop ) {
aoqi@0 1226 if (!use->is_CFG() && get_ctrl(use) == post_loop->in(2)) {
aoqi@0 1227 set_ctrl(use, post_loop);
aoqi@0 1228 for (DUIterator j = use->outs(); use->has_out(j); j++)
aoqi@0 1229 sink_use(use->out(j), post_loop);
aoqi@0 1230 }
aoqi@0 1231 }
aoqi@0 1232
aoqi@0 1233 //------------------------------clone_loop-------------------------------------
aoqi@0 1234 //
aoqi@0 1235 // C L O N E A L O O P B O D Y
aoqi@0 1236 //
aoqi@0 1237 // This is the basic building block of the loop optimizations. It clones an
aoqi@0 1238 // entire loop body. It makes an old_new loop body mapping; with this mapping
aoqi@0 1239 // you can find the new-loop equivalent to an old-loop node. All new-loop
aoqi@0 1240 // nodes are exactly equal to their old-loop counterparts, all edges are the
aoqi@0 1241 // same. All exits from the old-loop now have a RegionNode that merges the
aoqi@0 1242 // equivalent new-loop path. This is true even for the normal "loop-exit"
aoqi@0 1243 // condition. All uses of loop-invariant old-loop values now come from (one
aoqi@0 1244 // or more) Phis that merge their new-loop equivalents.
aoqi@0 1245 //
aoqi@0 1246 // This operation leaves the graph in an illegal state: there are two valid
aoqi@0 1247 // control edges coming from the loop pre-header to both loop bodies. I'll
aoqi@0 1248 // definitely have to hack the graph after running this transform.
aoqi@0 1249 //
aoqi@0 1250 // From this building block I will further edit edges to perform loop peeling
aoqi@0 1251 // or loop unrolling or iteration splitting (Range-Check-Elimination), etc.
aoqi@0 1252 //
aoqi@0 1253 // Parameter side_by_size_idom:
aoqi@0 1254 // When side_by_size_idom is NULL, the dominator tree is constructed for
aoqi@0 1255 // the clone loop to dominate the original. Used in construction of
aoqi@0 1256 // pre-main-post loop sequence.
aoqi@0 1257 // When nonnull, the clone and original are side-by-side, both are
aoqi@0 1258 // dominated by the side_by_side_idom node. Used in construction of
aoqi@0 1259 // unswitched loops.
aoqi@0 1260 void PhaseIdealLoop::clone_loop( IdealLoopTree *loop, Node_List &old_new, int dd,
aoqi@0 1261 Node* side_by_side_idom) {
aoqi@0 1262
aoqi@0 1263 // Step 1: Clone the loop body. Make the old->new mapping.
aoqi@0 1264 uint i;
aoqi@0 1265 for( i = 0; i < loop->_body.size(); i++ ) {
aoqi@0 1266 Node *old = loop->_body.at(i);
aoqi@0 1267 Node *nnn = old->clone();
aoqi@0 1268 old_new.map( old->_idx, nnn );
aoqi@0 1269 _igvn.register_new_node_with_optimizer(nnn);
aoqi@0 1270 }
aoqi@0 1271
aoqi@0 1272
aoqi@0 1273 // Step 2: Fix the edges in the new body. If the old input is outside the
aoqi@0 1274 // loop use it. If the old input is INside the loop, use the corresponding
aoqi@0 1275 // new node instead.
aoqi@0 1276 for( i = 0; i < loop->_body.size(); i++ ) {
aoqi@0 1277 Node *old = loop->_body.at(i);
aoqi@0 1278 Node *nnn = old_new[old->_idx];
aoqi@0 1279 // Fix CFG/Loop controlling the new node
aoqi@0 1280 if (has_ctrl(old)) {
aoqi@0 1281 set_ctrl(nnn, old_new[get_ctrl(old)->_idx]);
aoqi@0 1282 } else {
aoqi@0 1283 set_loop(nnn, loop->_parent);
aoqi@0 1284 if (old->outcnt() > 0) {
aoqi@0 1285 set_idom( nnn, old_new[idom(old)->_idx], dd );
aoqi@0 1286 }
aoqi@0 1287 }
aoqi@0 1288 // Correct edges to the new node
aoqi@0 1289 for( uint j = 0; j < nnn->req(); j++ ) {
aoqi@0 1290 Node *n = nnn->in(j);
aoqi@0 1291 if( n ) {
aoqi@0 1292 IdealLoopTree *old_in_loop = get_loop( has_ctrl(n) ? get_ctrl(n) : n );
aoqi@0 1293 if( loop->is_member( old_in_loop ) )
aoqi@0 1294 nnn->set_req(j, old_new[n->_idx]);
aoqi@0 1295 }
aoqi@0 1296 }
aoqi@0 1297 _igvn.hash_find_insert(nnn);
aoqi@0 1298 }
aoqi@0 1299 Node *newhead = old_new[loop->_head->_idx];
aoqi@0 1300 set_idom(newhead, newhead->in(LoopNode::EntryControl), dd);
aoqi@0 1301
aoqi@0 1302
aoqi@0 1303 // Step 3: Now fix control uses. Loop varying control uses have already
aoqi@0 1304 // been fixed up (as part of all input edges in Step 2). Loop invariant
aoqi@0 1305 // control uses must be either an IfFalse or an IfTrue. Make a merge
aoqi@0 1306 // point to merge the old and new IfFalse/IfTrue nodes; make the use
aoqi@0 1307 // refer to this.
aoqi@0 1308 ResourceArea *area = Thread::current()->resource_area();
aoqi@0 1309 Node_List worklist(area);
aoqi@0 1310 uint new_counter = C->unique();
aoqi@0 1311 for( i = 0; i < loop->_body.size(); i++ ) {
aoqi@0 1312 Node* old = loop->_body.at(i);
aoqi@0 1313 if( !old->is_CFG() ) continue;
aoqi@0 1314 Node* nnn = old_new[old->_idx];
aoqi@0 1315
aoqi@0 1316 // Copy uses to a worklist, so I can munge the def-use info
aoqi@0 1317 // with impunity.
aoqi@0 1318 for (DUIterator_Fast jmax, j = old->fast_outs(jmax); j < jmax; j++)
aoqi@0 1319 worklist.push(old->fast_out(j));
aoqi@0 1320
aoqi@0 1321 while( worklist.size() ) { // Visit all uses
aoqi@0 1322 Node *use = worklist.pop();
aoqi@0 1323 if (!has_node(use)) continue; // Ignore dead nodes
aoqi@0 1324 IdealLoopTree *use_loop = get_loop( has_ctrl(use) ? get_ctrl(use) : use );
aoqi@0 1325 if( !loop->is_member( use_loop ) && use->is_CFG() ) {
aoqi@0 1326 // Both OLD and USE are CFG nodes here.
aoqi@0 1327 assert( use->is_Proj(), "" );
aoqi@0 1328
aoqi@0 1329 // Clone the loop exit control projection
aoqi@0 1330 Node *newuse = use->clone();
aoqi@0 1331 newuse->set_req(0,nnn);
aoqi@0 1332 _igvn.register_new_node_with_optimizer(newuse);
aoqi@0 1333 set_loop(newuse, use_loop);
aoqi@0 1334 set_idom(newuse, nnn, dom_depth(nnn) + 1 );
aoqi@0 1335
aoqi@0 1336 // We need a Region to merge the exit from the peeled body and the
aoqi@0 1337 // exit from the old loop body.
aoqi@0 1338 RegionNode *r = new (C) RegionNode(3);
aoqi@0 1339 // Map the old use to the new merge point
aoqi@0 1340 old_new.map( use->_idx, r );
aoqi@0 1341 uint dd_r = MIN2(dom_depth(newuse),dom_depth(use));
aoqi@0 1342 assert( dd_r >= dom_depth(dom_lca(newuse,use)), "" );
aoqi@0 1343
aoqi@0 1344 // The original user of 'use' uses 'r' instead.
aoqi@0 1345 for (DUIterator_Last lmin, l = use->last_outs(lmin); l >= lmin;) {
aoqi@0 1346 Node* useuse = use->last_out(l);
aoqi@0 1347 _igvn.rehash_node_delayed(useuse);
aoqi@0 1348 uint uses_found = 0;
aoqi@0 1349 if( useuse->in(0) == use ) {
aoqi@0 1350 useuse->set_req(0, r);
aoqi@0 1351 uses_found++;
aoqi@0 1352 if( useuse->is_CFG() ) {
aoqi@0 1353 assert( dom_depth(useuse) > dd_r, "" );
aoqi@0 1354 set_idom(useuse, r, dom_depth(useuse));
aoqi@0 1355 }
aoqi@0 1356 }
aoqi@0 1357 for( uint k = 1; k < useuse->req(); k++ ) {
aoqi@0 1358 if( useuse->in(k) == use ) {
aoqi@0 1359 useuse->set_req(k, r);
aoqi@0 1360 uses_found++;
aoqi@0 1361 }
aoqi@0 1362 }
aoqi@0 1363 l -= uses_found; // we deleted 1 or more copies of this edge
aoqi@0 1364 }
aoqi@0 1365
aoqi@0 1366 // Now finish up 'r'
aoqi@0 1367 r->set_req( 1, newuse );
aoqi@0 1368 r->set_req( 2, use );
aoqi@0 1369 _igvn.register_new_node_with_optimizer(r);
aoqi@0 1370 set_loop(r, use_loop);
aoqi@0 1371 set_idom(r, !side_by_side_idom ? newuse->in(0) : side_by_side_idom, dd_r);
aoqi@0 1372 } // End of if a loop-exit test
aoqi@0 1373 }
aoqi@0 1374 }
aoqi@0 1375
aoqi@0 1376 // Step 4: If loop-invariant use is not control, it must be dominated by a
aoqi@0 1377 // loop exit IfFalse/IfTrue. Find "proper" loop exit. Make a Region
aoqi@0 1378 // there if needed. Make a Phi there merging old and new used values.
aoqi@0 1379 Node_List *split_if_set = NULL;
aoqi@0 1380 Node_List *split_bool_set = NULL;
aoqi@0 1381 Node_List *split_cex_set = NULL;
aoqi@0 1382 for( i = 0; i < loop->_body.size(); i++ ) {
aoqi@0 1383 Node* old = loop->_body.at(i);
aoqi@0 1384 Node* nnn = old_new[old->_idx];
aoqi@0 1385 // Copy uses to a worklist, so I can munge the def-use info
aoqi@0 1386 // with impunity.
aoqi@0 1387 for (DUIterator_Fast jmax, j = old->fast_outs(jmax); j < jmax; j++)
aoqi@0 1388 worklist.push(old->fast_out(j));
aoqi@0 1389
aoqi@0 1390 while( worklist.size() ) {
aoqi@0 1391 Node *use = worklist.pop();
aoqi@0 1392 if (!has_node(use)) continue; // Ignore dead nodes
aoqi@0 1393 if (use->in(0) == C->top()) continue;
aoqi@0 1394 IdealLoopTree *use_loop = get_loop( has_ctrl(use) ? get_ctrl(use) : use );
aoqi@0 1395 // Check for data-use outside of loop - at least one of OLD or USE
aoqi@0 1396 // must not be a CFG node.
aoqi@0 1397 if( !loop->is_member( use_loop ) && (!old->is_CFG() || !use->is_CFG())) {
aoqi@0 1398
aoqi@0 1399 // If the Data use is an IF, that means we have an IF outside of the
aoqi@0 1400 // loop that is switching on a condition that is set inside of the
aoqi@0 1401 // loop. Happens if people set a loop-exit flag; then test the flag
aoqi@0 1402 // in the loop to break the loop, then test is again outside of the
aoqi@0 1403 // loop to determine which way the loop exited.
aoqi@0 1404 // Loop predicate If node connects to Bool node through Opaque1 node.
aoqi@0 1405 if (use->is_If() || use->is_CMove() || C->is_predicate_opaq(use)) {
aoqi@0 1406 // Since this code is highly unlikely, we lazily build the worklist
aoqi@0 1407 // of such Nodes to go split.
aoqi@0 1408 if( !split_if_set )
aoqi@0 1409 split_if_set = new Node_List(area);
aoqi@0 1410 split_if_set->push(use);
aoqi@0 1411 }
aoqi@0 1412 if( use->is_Bool() ) {
aoqi@0 1413 if( !split_bool_set )
aoqi@0 1414 split_bool_set = new Node_List(area);
aoqi@0 1415 split_bool_set->push(use);
aoqi@0 1416 }
aoqi@0 1417 if( use->Opcode() == Op_CreateEx ) {
aoqi@0 1418 if( !split_cex_set )
aoqi@0 1419 split_cex_set = new Node_List(area);
aoqi@0 1420 split_cex_set->push(use);
aoqi@0 1421 }
aoqi@0 1422
aoqi@0 1423
aoqi@0 1424 // Get "block" use is in
aoqi@0 1425 uint idx = 0;
aoqi@0 1426 while( use->in(idx) != old ) idx++;
aoqi@0 1427 Node *prev = use->is_CFG() ? use : get_ctrl(use);
aoqi@0 1428 assert( !loop->is_member( get_loop( prev ) ), "" );
aoqi@0 1429 Node *cfg = prev->_idx >= new_counter
aoqi@0 1430 ? prev->in(2)
aoqi@0 1431 : idom(prev);
aoqi@0 1432 if( use->is_Phi() ) // Phi use is in prior block
aoqi@0 1433 cfg = prev->in(idx); // NOT in block of Phi itself
aoqi@0 1434 if (cfg->is_top()) { // Use is dead?
aoqi@0 1435 _igvn.replace_input_of(use, idx, C->top());
aoqi@0 1436 continue;
aoqi@0 1437 }
aoqi@0 1438
aoqi@0 1439 while( !loop->is_member( get_loop( cfg ) ) ) {
aoqi@0 1440 prev = cfg;
aoqi@0 1441 cfg = cfg->_idx >= new_counter ? cfg->in(2) : idom(cfg);
aoqi@0 1442 }
aoqi@0 1443 // If the use occurs after merging several exits from the loop, then
aoqi@0 1444 // old value must have dominated all those exits. Since the same old
aoqi@0 1445 // value was used on all those exits we did not need a Phi at this
aoqi@0 1446 // merge point. NOW we do need a Phi here. Each loop exit value
aoqi@0 1447 // is now merged with the peeled body exit; each exit gets its own
aoqi@0 1448 // private Phi and those Phis need to be merged here.
aoqi@0 1449 Node *phi;
aoqi@0 1450 if( prev->is_Region() ) {
aoqi@0 1451 if( idx == 0 ) { // Updating control edge?
aoqi@0 1452 phi = prev; // Just use existing control
aoqi@0 1453 } else { // Else need a new Phi
aoqi@0 1454 phi = PhiNode::make( prev, old );
aoqi@0 1455 // Now recursively fix up the new uses of old!
aoqi@0 1456 for( uint i = 1; i < prev->req(); i++ ) {
aoqi@0 1457 worklist.push(phi); // Onto worklist once for each 'old' input
aoqi@0 1458 }
aoqi@0 1459 }
aoqi@0 1460 } else {
aoqi@0 1461 // Get new RegionNode merging old and new loop exits
aoqi@0 1462 prev = old_new[prev->_idx];
aoqi@0 1463 assert( prev, "just made this in step 7" );
aoqi@0 1464 if( idx == 0 ) { // Updating control edge?
aoqi@0 1465 phi = prev; // Just use existing control
aoqi@0 1466 } else { // Else need a new Phi
aoqi@0 1467 // Make a new Phi merging data values properly
aoqi@0 1468 phi = PhiNode::make( prev, old );
aoqi@0 1469 phi->set_req( 1, nnn );
aoqi@0 1470 }
aoqi@0 1471 }
aoqi@0 1472 // If inserting a new Phi, check for prior hits
aoqi@0 1473 if( idx != 0 ) {
aoqi@0 1474 Node *hit = _igvn.hash_find_insert(phi);
aoqi@0 1475 if( hit == NULL ) {
aoqi@0 1476 _igvn.register_new_node_with_optimizer(phi); // Register new phi
aoqi@0 1477 } else { // or
aoqi@0 1478 // Remove the new phi from the graph and use the hit
aoqi@0 1479 _igvn.remove_dead_node(phi);
aoqi@0 1480 phi = hit; // Use existing phi
aoqi@0 1481 }
aoqi@0 1482 set_ctrl(phi, prev);
aoqi@0 1483 }
aoqi@0 1484 // Make 'use' use the Phi instead of the old loop body exit value
aoqi@0 1485 _igvn.replace_input_of(use, idx, phi);
aoqi@0 1486 if( use->_idx >= new_counter ) { // If updating new phis
aoqi@0 1487 // Not needed for correctness, but prevents a weak assert
aoqi@0 1488 // in AddPNode from tripping (when we end up with different
aoqi@0 1489 // base & derived Phis that will become the same after
aoqi@0 1490 // IGVN does CSE).
aoqi@0 1491 Node *hit = _igvn.hash_find_insert(use);
aoqi@0 1492 if( hit ) // Go ahead and re-hash for hits.
aoqi@0 1493 _igvn.replace_node( use, hit );
aoqi@0 1494 }
aoqi@0 1495
aoqi@0 1496 // If 'use' was in the loop-exit block, it now needs to be sunk
aoqi@0 1497 // below the post-loop merge point.
aoqi@0 1498 sink_use( use, prev );
aoqi@0 1499 }
aoqi@0 1500 }
aoqi@0 1501 }
aoqi@0 1502
aoqi@0 1503 // Check for IFs that need splitting/cloning. Happens if an IF outside of
aoqi@0 1504 // the loop uses a condition set in the loop. The original IF probably
aoqi@0 1505 // takes control from one or more OLD Regions (which in turn get from NEW
aoqi@0 1506 // Regions). In any case, there will be a set of Phis for each merge point
aoqi@0 1507 // from the IF up to where the original BOOL def exists the loop.
aoqi@0 1508 if( split_if_set ) {
aoqi@0 1509 while( split_if_set->size() ) {
aoqi@0 1510 Node *iff = split_if_set->pop();
aoqi@0 1511 if( iff->in(1)->is_Phi() ) {
aoqi@0 1512 BoolNode *b = clone_iff( iff->in(1)->as_Phi(), loop );
aoqi@0 1513 _igvn.replace_input_of(iff, 1, b);
aoqi@0 1514 }
aoqi@0 1515 }
aoqi@0 1516 }
aoqi@0 1517 if( split_bool_set ) {
aoqi@0 1518 while( split_bool_set->size() ) {
aoqi@0 1519 Node *b = split_bool_set->pop();
aoqi@0 1520 Node *phi = b->in(1);
aoqi@0 1521 assert( phi->is_Phi(), "" );
aoqi@0 1522 CmpNode *cmp = clone_bool( (PhiNode*)phi, loop );
aoqi@0 1523 _igvn.replace_input_of(b, 1, cmp);
aoqi@0 1524 }
aoqi@0 1525 }
aoqi@0 1526 if( split_cex_set ) {
aoqi@0 1527 while( split_cex_set->size() ) {
aoqi@0 1528 Node *b = split_cex_set->pop();
aoqi@0 1529 assert( b->in(0)->is_Region(), "" );
aoqi@0 1530 assert( b->in(1)->is_Phi(), "" );
aoqi@0 1531 assert( b->in(0)->in(0) == b->in(1)->in(0), "" );
aoqi@0 1532 split_up( b, b->in(0), NULL );
aoqi@0 1533 }
aoqi@0 1534 }
aoqi@0 1535
aoqi@0 1536 }
aoqi@0 1537
aoqi@0 1538
aoqi@0 1539 //---------------------- stride_of_possible_iv -------------------------------------
aoqi@0 1540 // Looks for an iff/bool/comp with one operand of the compare
aoqi@0 1541 // being a cycle involving an add and a phi,
aoqi@0 1542 // with an optional truncation (left-shift followed by a right-shift)
aoqi@0 1543 // of the add. Returns zero if not an iv.
aoqi@0 1544 int PhaseIdealLoop::stride_of_possible_iv(Node* iff) {
aoqi@0 1545 Node* trunc1 = NULL;
aoqi@0 1546 Node* trunc2 = NULL;
aoqi@0 1547 const TypeInt* ttype = NULL;
aoqi@0 1548 if (!iff->is_If() || iff->in(1) == NULL || !iff->in(1)->is_Bool()) {
aoqi@0 1549 return 0;
aoqi@0 1550 }
aoqi@0 1551 BoolNode* bl = iff->in(1)->as_Bool();
aoqi@0 1552 Node* cmp = bl->in(1);
aoqi@0 1553 if (!cmp || cmp->Opcode() != Op_CmpI && cmp->Opcode() != Op_CmpU) {
aoqi@0 1554 return 0;
aoqi@0 1555 }
aoqi@0 1556 // Must have an invariant operand
aoqi@0 1557 if (is_member(get_loop(iff), get_ctrl(cmp->in(2)))) {
aoqi@0 1558 return 0;
aoqi@0 1559 }
aoqi@0 1560 Node* add2 = NULL;
aoqi@0 1561 Node* cmp1 = cmp->in(1);
aoqi@0 1562 if (cmp1->is_Phi()) {
aoqi@0 1563 // (If (Bool (CmpX phi:(Phi ...(Optional-trunc(AddI phi add2))) )))
aoqi@0 1564 Node* phi = cmp1;
aoqi@0 1565 for (uint i = 1; i < phi->req(); i++) {
aoqi@0 1566 Node* in = phi->in(i);
aoqi@0 1567 Node* add = CountedLoopNode::match_incr_with_optional_truncation(in,
aoqi@0 1568 &trunc1, &trunc2, &ttype);
aoqi@0 1569 if (add && add->in(1) == phi) {
aoqi@0 1570 add2 = add->in(2);
aoqi@0 1571 break;
aoqi@0 1572 }
aoqi@0 1573 }
aoqi@0 1574 } else {
aoqi@0 1575 // (If (Bool (CmpX addtrunc:(Optional-trunc((AddI (Phi ...addtrunc...) add2)) )))
aoqi@0 1576 Node* addtrunc = cmp1;
aoqi@0 1577 Node* add = CountedLoopNode::match_incr_with_optional_truncation(addtrunc,
aoqi@0 1578 &trunc1, &trunc2, &ttype);
aoqi@0 1579 if (add && add->in(1)->is_Phi()) {
aoqi@0 1580 Node* phi = add->in(1);
aoqi@0 1581 for (uint i = 1; i < phi->req(); i++) {
aoqi@0 1582 if (phi->in(i) == addtrunc) {
aoqi@0 1583 add2 = add->in(2);
aoqi@0 1584 break;
aoqi@0 1585 }
aoqi@0 1586 }
aoqi@0 1587 }
aoqi@0 1588 }
aoqi@0 1589 if (add2 != NULL) {
aoqi@0 1590 const TypeInt* add2t = _igvn.type(add2)->is_int();
aoqi@0 1591 if (add2t->is_con()) {
aoqi@0 1592 return add2t->get_con();
aoqi@0 1593 }
aoqi@0 1594 }
aoqi@0 1595 return 0;
aoqi@0 1596 }
aoqi@0 1597
aoqi@0 1598
aoqi@0 1599 //---------------------- stay_in_loop -------------------------------------
aoqi@0 1600 // Return the (unique) control output node that's in the loop (if it exists.)
aoqi@0 1601 Node* PhaseIdealLoop::stay_in_loop( Node* n, IdealLoopTree *loop) {
aoqi@0 1602 Node* unique = NULL;
aoqi@0 1603 if (!n) return NULL;
aoqi@0 1604 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
aoqi@0 1605 Node* use = n->fast_out(i);
aoqi@0 1606 if (!has_ctrl(use) && loop->is_member(get_loop(use))) {
aoqi@0 1607 if (unique != NULL) {
aoqi@0 1608 return NULL;
aoqi@0 1609 }
aoqi@0 1610 unique = use;
aoqi@0 1611 }
aoqi@0 1612 }
aoqi@0 1613 return unique;
aoqi@0 1614 }
aoqi@0 1615
aoqi@0 1616 //------------------------------ register_node -------------------------------------
aoqi@0 1617 // Utility to register node "n" with PhaseIdealLoop
aoqi@0 1618 void PhaseIdealLoop::register_node(Node* n, IdealLoopTree *loop, Node* pred, int ddepth) {
aoqi@0 1619 _igvn.register_new_node_with_optimizer(n);
aoqi@0 1620 loop->_body.push(n);
aoqi@0 1621 if (n->is_CFG()) {
aoqi@0 1622 set_loop(n, loop);
aoqi@0 1623 set_idom(n, pred, ddepth);
aoqi@0 1624 } else {
aoqi@0 1625 set_ctrl(n, pred);
aoqi@0 1626 }
aoqi@0 1627 }
aoqi@0 1628
aoqi@0 1629 //------------------------------ proj_clone -------------------------------------
aoqi@0 1630 // Utility to create an if-projection
aoqi@0 1631 ProjNode* PhaseIdealLoop::proj_clone(ProjNode* p, IfNode* iff) {
aoqi@0 1632 ProjNode* c = p->clone()->as_Proj();
aoqi@0 1633 c->set_req(0, iff);
aoqi@0 1634 return c;
aoqi@0 1635 }
aoqi@0 1636
aoqi@0 1637 //------------------------------ short_circuit_if -------------------------------------
aoqi@0 1638 // Force the iff control output to be the live_proj
aoqi@0 1639 Node* PhaseIdealLoop::short_circuit_if(IfNode* iff, ProjNode* live_proj) {
aoqi@0 1640 guarantee(live_proj != NULL, "null projection");
aoqi@0 1641 int proj_con = live_proj->_con;
aoqi@0 1642 assert(proj_con == 0 || proj_con == 1, "false or true projection");
aoqi@0 1643 Node *con = _igvn.intcon(proj_con);
aoqi@0 1644 set_ctrl(con, C->root());
aoqi@0 1645 if (iff) {
aoqi@0 1646 iff->set_req(1, con);
aoqi@0 1647 }
aoqi@0 1648 return con;
aoqi@0 1649 }
aoqi@0 1650
aoqi@0 1651 //------------------------------ insert_if_before_proj -------------------------------------
aoqi@0 1652 // Insert a new if before an if projection (* - new node)
aoqi@0 1653 //
aoqi@0 1654 // before
aoqi@0 1655 // if(test)
aoqi@0 1656 // / \
aoqi@0 1657 // v v
aoqi@0 1658 // other-proj proj (arg)
aoqi@0 1659 //
aoqi@0 1660 // after
aoqi@0 1661 // if(test)
aoqi@0 1662 // / \
aoqi@0 1663 // / v
aoqi@0 1664 // | * proj-clone
aoqi@0 1665 // v |
aoqi@0 1666 // other-proj v
aoqi@0 1667 // * new_if(relop(cmp[IU](left,right)))
aoqi@0 1668 // / \
aoqi@0 1669 // v v
aoqi@0 1670 // * new-proj proj
aoqi@0 1671 // (returned)
aoqi@0 1672 //
aoqi@0 1673 ProjNode* PhaseIdealLoop::insert_if_before_proj(Node* left, bool Signed, BoolTest::mask relop, Node* right, ProjNode* proj) {
aoqi@0 1674 IfNode* iff = proj->in(0)->as_If();
aoqi@0 1675 IdealLoopTree *loop = get_loop(proj);
aoqi@0 1676 ProjNode *other_proj = iff->proj_out(!proj->is_IfTrue())->as_Proj();
aoqi@0 1677 int ddepth = dom_depth(proj);
aoqi@0 1678
aoqi@0 1679 _igvn.rehash_node_delayed(iff);
aoqi@0 1680 _igvn.rehash_node_delayed(proj);
aoqi@0 1681
aoqi@0 1682 proj->set_req(0, NULL); // temporary disconnect
aoqi@0 1683 ProjNode* proj2 = proj_clone(proj, iff);
aoqi@0 1684 register_node(proj2, loop, iff, ddepth);
aoqi@0 1685
aoqi@0 1686 Node* cmp = Signed ? (Node*) new (C)CmpINode(left, right) : (Node*) new (C)CmpUNode(left, right);
aoqi@0 1687 register_node(cmp, loop, proj2, ddepth);
aoqi@0 1688
aoqi@0 1689 BoolNode* bol = new (C)BoolNode(cmp, relop);
aoqi@0 1690 register_node(bol, loop, proj2, ddepth);
aoqi@0 1691
aoqi@0 1692 IfNode* new_if = new (C)IfNode(proj2, bol, iff->_prob, iff->_fcnt);
aoqi@0 1693 register_node(new_if, loop, proj2, ddepth);
aoqi@0 1694
aoqi@0 1695 proj->set_req(0, new_if); // reattach
aoqi@0 1696 set_idom(proj, new_if, ddepth);
aoqi@0 1697
aoqi@0 1698 ProjNode* new_exit = proj_clone(other_proj, new_if)->as_Proj();
aoqi@0 1699 guarantee(new_exit != NULL, "null exit node");
aoqi@0 1700 register_node(new_exit, get_loop(other_proj), new_if, ddepth);
aoqi@0 1701
aoqi@0 1702 return new_exit;
aoqi@0 1703 }
aoqi@0 1704
aoqi@0 1705 //------------------------------ insert_region_before_proj -------------------------------------
aoqi@0 1706 // Insert a region before an if projection (* - new node)
aoqi@0 1707 //
aoqi@0 1708 // before
aoqi@0 1709 // if(test)
aoqi@0 1710 // / |
aoqi@0 1711 // v |
aoqi@0 1712 // proj v
aoqi@0 1713 // other-proj
aoqi@0 1714 //
aoqi@0 1715 // after
aoqi@0 1716 // if(test)
aoqi@0 1717 // / |
aoqi@0 1718 // v |
aoqi@0 1719 // * proj-clone v
aoqi@0 1720 // | other-proj
aoqi@0 1721 // v
aoqi@0 1722 // * new-region
aoqi@0 1723 // |
aoqi@0 1724 // v
aoqi@0 1725 // * dum_if
aoqi@0 1726 // / \
aoqi@0 1727 // v \
aoqi@0 1728 // * dum-proj v
aoqi@0 1729 // proj
aoqi@0 1730 //
aoqi@0 1731 RegionNode* PhaseIdealLoop::insert_region_before_proj(ProjNode* proj) {
aoqi@0 1732 IfNode* iff = proj->in(0)->as_If();
aoqi@0 1733 IdealLoopTree *loop = get_loop(proj);
aoqi@0 1734 ProjNode *other_proj = iff->proj_out(!proj->is_IfTrue())->as_Proj();
aoqi@0 1735 int ddepth = dom_depth(proj);
aoqi@0 1736
aoqi@0 1737 _igvn.rehash_node_delayed(iff);
aoqi@0 1738 _igvn.rehash_node_delayed(proj);
aoqi@0 1739
aoqi@0 1740 proj->set_req(0, NULL); // temporary disconnect
aoqi@0 1741 ProjNode* proj2 = proj_clone(proj, iff);
aoqi@0 1742 register_node(proj2, loop, iff, ddepth);
aoqi@0 1743
aoqi@0 1744 RegionNode* reg = new (C)RegionNode(2);
aoqi@0 1745 reg->set_req(1, proj2);
aoqi@0 1746 register_node(reg, loop, iff, ddepth);
aoqi@0 1747
aoqi@0 1748 IfNode* dum_if = new (C)IfNode(reg, short_circuit_if(NULL, proj), iff->_prob, iff->_fcnt);
aoqi@0 1749 register_node(dum_if, loop, reg, ddepth);
aoqi@0 1750
aoqi@0 1751 proj->set_req(0, dum_if); // reattach
aoqi@0 1752 set_idom(proj, dum_if, ddepth);
aoqi@0 1753
aoqi@0 1754 ProjNode* dum_proj = proj_clone(other_proj, dum_if);
aoqi@0 1755 register_node(dum_proj, loop, dum_if, ddepth);
aoqi@0 1756
aoqi@0 1757 return reg;
aoqi@0 1758 }
aoqi@0 1759
aoqi@0 1760 //------------------------------ insert_cmpi_loop_exit -------------------------------------
aoqi@0 1761 // Clone a signed compare loop exit from an unsigned compare and
aoqi@0 1762 // insert it before the unsigned cmp on the stay-in-loop path.
aoqi@0 1763 // All new nodes inserted in the dominator tree between the original
aoqi@0 1764 // if and it's projections. The original if test is replaced with
aoqi@0 1765 // a constant to force the stay-in-loop path.
aoqi@0 1766 //
aoqi@0 1767 // This is done to make sure that the original if and it's projections
aoqi@0 1768 // still dominate the same set of control nodes, that the ctrl() relation
aoqi@0 1769 // from data nodes to them is preserved, and that their loop nesting is
aoqi@0 1770 // preserved.
aoqi@0 1771 //
aoqi@0 1772 // before
aoqi@0 1773 // if(i <u limit) unsigned compare loop exit
aoqi@0 1774 // / |
aoqi@0 1775 // v v
aoqi@0 1776 // exit-proj stay-in-loop-proj
aoqi@0 1777 //
aoqi@0 1778 // after
aoqi@0 1779 // if(stay-in-loop-const) original if
aoqi@0 1780 // / |
aoqi@0 1781 // / v
aoqi@0 1782 // / if(i < limit) new signed test
aoqi@0 1783 // / / |
aoqi@0 1784 // / / v
aoqi@0 1785 // / / if(i <u limit) new cloned unsigned test
aoqi@0 1786 // / / / |
aoqi@0 1787 // v v v |
aoqi@0 1788 // region |
aoqi@0 1789 // | |
aoqi@0 1790 // dum-if |
aoqi@0 1791 // / | |
aoqi@0 1792 // ether | |
aoqi@0 1793 // v v
aoqi@0 1794 // exit-proj stay-in-loop-proj
aoqi@0 1795 //
aoqi@0 1796 IfNode* PhaseIdealLoop::insert_cmpi_loop_exit(IfNode* if_cmpu, IdealLoopTree *loop) {
aoqi@0 1797 const bool Signed = true;
aoqi@0 1798 const bool Unsigned = false;
aoqi@0 1799
aoqi@0 1800 BoolNode* bol = if_cmpu->in(1)->as_Bool();
aoqi@0 1801 if (bol->_test._test != BoolTest::lt) return NULL;
aoqi@0 1802 CmpNode* cmpu = bol->in(1)->as_Cmp();
aoqi@0 1803 if (cmpu->Opcode() != Op_CmpU) return NULL;
aoqi@0 1804 int stride = stride_of_possible_iv(if_cmpu);
aoqi@0 1805 if (stride == 0) return NULL;
aoqi@0 1806
aoqi@0 1807 Node* lp_proj = stay_in_loop(if_cmpu, loop);
aoqi@0 1808 guarantee(lp_proj != NULL, "null loop node");
aoqi@0 1809
aoqi@0 1810 ProjNode* lp_continue = lp_proj->as_Proj();
aoqi@0 1811 ProjNode* lp_exit = if_cmpu->proj_out(!lp_continue->is_IfTrue())->as_Proj();
aoqi@0 1812
aoqi@0 1813 Node* limit = NULL;
aoqi@0 1814 if (stride > 0) {
aoqi@0 1815 limit = cmpu->in(2);
aoqi@0 1816 } else {
aoqi@0 1817 limit = _igvn.makecon(TypeInt::ZERO);
aoqi@0 1818 set_ctrl(limit, C->root());
aoqi@0 1819 }
aoqi@0 1820 // Create a new region on the exit path
aoqi@0 1821 RegionNode* reg = insert_region_before_proj(lp_exit);
aoqi@0 1822 guarantee(reg != NULL, "null region node");
aoqi@0 1823
aoqi@0 1824 // Clone the if-cmpu-true-false using a signed compare
aoqi@0 1825 BoolTest::mask rel_i = stride > 0 ? bol->_test._test : BoolTest::ge;
aoqi@0 1826 ProjNode* cmpi_exit = insert_if_before_proj(cmpu->in(1), Signed, rel_i, limit, lp_continue);
aoqi@0 1827 reg->add_req(cmpi_exit);
aoqi@0 1828
aoqi@0 1829 // Clone the if-cmpu-true-false
aoqi@0 1830 BoolTest::mask rel_u = bol->_test._test;
aoqi@0 1831 ProjNode* cmpu_exit = insert_if_before_proj(cmpu->in(1), Unsigned, rel_u, cmpu->in(2), lp_continue);
aoqi@0 1832 reg->add_req(cmpu_exit);
aoqi@0 1833
aoqi@0 1834 // Force original if to stay in loop.
aoqi@0 1835 short_circuit_if(if_cmpu, lp_continue);
aoqi@0 1836
aoqi@0 1837 return cmpi_exit->in(0)->as_If();
aoqi@0 1838 }
aoqi@0 1839
aoqi@0 1840 //------------------------------ remove_cmpi_loop_exit -------------------------------------
aoqi@0 1841 // Remove a previously inserted signed compare loop exit.
aoqi@0 1842 void PhaseIdealLoop::remove_cmpi_loop_exit(IfNode* if_cmp, IdealLoopTree *loop) {
aoqi@0 1843 Node* lp_proj = stay_in_loop(if_cmp, loop);
aoqi@0 1844 assert(if_cmp->in(1)->in(1)->Opcode() == Op_CmpI &&
aoqi@0 1845 stay_in_loop(lp_proj, loop)->is_If() &&
aoqi@0 1846 stay_in_loop(lp_proj, loop)->in(1)->in(1)->Opcode() == Op_CmpU, "inserted cmpi before cmpu");
aoqi@0 1847 Node *con = _igvn.makecon(lp_proj->is_IfTrue() ? TypeInt::ONE : TypeInt::ZERO);
aoqi@0 1848 set_ctrl(con, C->root());
aoqi@0 1849 if_cmp->set_req(1, con);
aoqi@0 1850 }
aoqi@0 1851
aoqi@0 1852 //------------------------------ scheduled_nodelist -------------------------------------
aoqi@0 1853 // Create a post order schedule of nodes that are in the
aoqi@0 1854 // "member" set. The list is returned in "sched".
aoqi@0 1855 // The first node in "sched" is the loop head, followed by
aoqi@0 1856 // nodes which have no inputs in the "member" set, and then
aoqi@0 1857 // followed by the nodes that have an immediate input dependence
aoqi@0 1858 // on a node in "sched".
aoqi@0 1859 void PhaseIdealLoop::scheduled_nodelist( IdealLoopTree *loop, VectorSet& member, Node_List &sched ) {
aoqi@0 1860
aoqi@0 1861 assert(member.test(loop->_head->_idx), "loop head must be in member set");
aoqi@0 1862 Arena *a = Thread::current()->resource_area();
aoqi@0 1863 VectorSet visited(a);
aoqi@0 1864 Node_Stack nstack(a, loop->_body.size());
aoqi@0 1865
aoqi@0 1866 Node* n = loop->_head; // top of stack is cached in "n"
aoqi@0 1867 uint idx = 0;
aoqi@0 1868 visited.set(n->_idx);
aoqi@0 1869
aoqi@0 1870 // Initially push all with no inputs from within member set
aoqi@0 1871 for(uint i = 0; i < loop->_body.size(); i++ ) {
aoqi@0 1872 Node *elt = loop->_body.at(i);
aoqi@0 1873 if (member.test(elt->_idx)) {
aoqi@0 1874 bool found = false;
aoqi@0 1875 for (uint j = 0; j < elt->req(); j++) {
aoqi@0 1876 Node* def = elt->in(j);
aoqi@0 1877 if (def && member.test(def->_idx) && def != elt) {
aoqi@0 1878 found = true;
aoqi@0 1879 break;
aoqi@0 1880 }
aoqi@0 1881 }
aoqi@0 1882 if (!found && elt != loop->_head) {
aoqi@0 1883 nstack.push(n, idx);
aoqi@0 1884 n = elt;
aoqi@0 1885 assert(!visited.test(n->_idx), "not seen yet");
aoqi@0 1886 visited.set(n->_idx);
aoqi@0 1887 }
aoqi@0 1888 }
aoqi@0 1889 }
aoqi@0 1890
aoqi@0 1891 // traverse out's that are in the member set
aoqi@0 1892 while (true) {
aoqi@0 1893 if (idx < n->outcnt()) {
aoqi@0 1894 Node* use = n->raw_out(idx);
aoqi@0 1895 idx++;
aoqi@0 1896 if (!visited.test_set(use->_idx)) {
aoqi@0 1897 if (member.test(use->_idx)) {
aoqi@0 1898 nstack.push(n, idx);
aoqi@0 1899 n = use;
aoqi@0 1900 idx = 0;
aoqi@0 1901 }
aoqi@0 1902 }
aoqi@0 1903 } else {
aoqi@0 1904 // All outputs processed
aoqi@0 1905 sched.push(n);
aoqi@0 1906 if (nstack.is_empty()) break;
aoqi@0 1907 n = nstack.node();
aoqi@0 1908 idx = nstack.index();
aoqi@0 1909 nstack.pop();
aoqi@0 1910 }
aoqi@0 1911 }
aoqi@0 1912 }
aoqi@0 1913
aoqi@0 1914
aoqi@0 1915 //------------------------------ has_use_in_set -------------------------------------
aoqi@0 1916 // Has a use in the vector set
aoqi@0 1917 bool PhaseIdealLoop::has_use_in_set( Node* n, VectorSet& vset ) {
aoqi@0 1918 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) {
aoqi@0 1919 Node* use = n->fast_out(j);
aoqi@0 1920 if (vset.test(use->_idx)) {
aoqi@0 1921 return true;
aoqi@0 1922 }
aoqi@0 1923 }
aoqi@0 1924 return false;
aoqi@0 1925 }
aoqi@0 1926
aoqi@0 1927
aoqi@0 1928 //------------------------------ has_use_internal_to_set -------------------------------------
aoqi@0 1929 // Has use internal to the vector set (ie. not in a phi at the loop head)
aoqi@0 1930 bool PhaseIdealLoop::has_use_internal_to_set( Node* n, VectorSet& vset, IdealLoopTree *loop ) {
aoqi@0 1931 Node* head = loop->_head;
aoqi@0 1932 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) {
aoqi@0 1933 Node* use = n->fast_out(j);
aoqi@0 1934 if (vset.test(use->_idx) && !(use->is_Phi() && use->in(0) == head)) {
aoqi@0 1935 return true;
aoqi@0 1936 }
aoqi@0 1937 }
aoqi@0 1938 return false;
aoqi@0 1939 }
aoqi@0 1940
aoqi@0 1941
aoqi@0 1942 //------------------------------ clone_for_use_outside_loop -------------------------------------
aoqi@0 1943 // clone "n" for uses that are outside of loop
aoqi@0 1944 int PhaseIdealLoop::clone_for_use_outside_loop( IdealLoopTree *loop, Node* n, Node_List& worklist ) {
aoqi@0 1945 int cloned = 0;
aoqi@0 1946 assert(worklist.size() == 0, "should be empty");
aoqi@0 1947 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) {
aoqi@0 1948 Node* use = n->fast_out(j);
aoqi@0 1949 if( !loop->is_member(get_loop(has_ctrl(use) ? get_ctrl(use) : use)) ) {
aoqi@0 1950 worklist.push(use);
aoqi@0 1951 }
aoqi@0 1952 }
aoqi@0 1953 while( worklist.size() ) {
aoqi@0 1954 Node *use = worklist.pop();
aoqi@0 1955 if (!has_node(use) || use->in(0) == C->top()) continue;
aoqi@0 1956 uint j;
aoqi@0 1957 for (j = 0; j < use->req(); j++) {
aoqi@0 1958 if (use->in(j) == n) break;
aoqi@0 1959 }
aoqi@0 1960 assert(j < use->req(), "must be there");
aoqi@0 1961
aoqi@0 1962 // clone "n" and insert it between the inputs of "n" and the use outside the loop
aoqi@0 1963 Node* n_clone = n->clone();
aoqi@0 1964 _igvn.replace_input_of(use, j, n_clone);
aoqi@0 1965 cloned++;
aoqi@0 1966 Node* use_c;
aoqi@0 1967 if (!use->is_Phi()) {
aoqi@0 1968 use_c = has_ctrl(use) ? get_ctrl(use) : use->in(0);
aoqi@0 1969 } else {
aoqi@0 1970 // Use in a phi is considered a use in the associated predecessor block
aoqi@0 1971 use_c = use->in(0)->in(j);
aoqi@0 1972 }
aoqi@0 1973 set_ctrl(n_clone, use_c);
aoqi@0 1974 assert(!loop->is_member(get_loop(use_c)), "should be outside loop");
aoqi@0 1975 get_loop(use_c)->_body.push(n_clone);
aoqi@0 1976 _igvn.register_new_node_with_optimizer(n_clone);
aoqi@0 1977 #if !defined(PRODUCT)
aoqi@0 1978 if (TracePartialPeeling) {
aoqi@0 1979 tty->print_cr("loop exit cloning old: %d new: %d newbb: %d", n->_idx, n_clone->_idx, get_ctrl(n_clone)->_idx);
aoqi@0 1980 }
aoqi@0 1981 #endif
aoqi@0 1982 }
aoqi@0 1983 return cloned;
aoqi@0 1984 }
aoqi@0 1985
aoqi@0 1986
aoqi@0 1987 //------------------------------ clone_for_special_use_inside_loop -------------------------------------
aoqi@0 1988 // clone "n" for special uses that are in the not_peeled region.
aoqi@0 1989 // If these def-uses occur in separate blocks, the code generator
aoqi@0 1990 // marks the method as not compilable. For example, if a "BoolNode"
aoqi@0 1991 // is in a different basic block than the "IfNode" that uses it, then
aoqi@0 1992 // the compilation is aborted in the code generator.
aoqi@0 1993 void PhaseIdealLoop::clone_for_special_use_inside_loop( IdealLoopTree *loop, Node* n,
aoqi@0 1994 VectorSet& not_peel, Node_List& sink_list, Node_List& worklist ) {
aoqi@0 1995 if (n->is_Phi() || n->is_Load()) {
aoqi@0 1996 return;
aoqi@0 1997 }
aoqi@0 1998 assert(worklist.size() == 0, "should be empty");
aoqi@0 1999 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) {
aoqi@0 2000 Node* use = n->fast_out(j);
aoqi@0 2001 if ( not_peel.test(use->_idx) &&
aoqi@0 2002 (use->is_If() || use->is_CMove() || use->is_Bool()) &&
aoqi@0 2003 use->in(1) == n) {
aoqi@0 2004 worklist.push(use);
aoqi@0 2005 }
aoqi@0 2006 }
aoqi@0 2007 if (worklist.size() > 0) {
aoqi@0 2008 // clone "n" and insert it between inputs of "n" and the use
aoqi@0 2009 Node* n_clone = n->clone();
aoqi@0 2010 loop->_body.push(n_clone);
aoqi@0 2011 _igvn.register_new_node_with_optimizer(n_clone);
aoqi@0 2012 set_ctrl(n_clone, get_ctrl(n));
aoqi@0 2013 sink_list.push(n_clone);
aoqi@0 2014 not_peel <<= n_clone->_idx; // add n_clone to not_peel set.
aoqi@0 2015 #if !defined(PRODUCT)
aoqi@0 2016 if (TracePartialPeeling) {
aoqi@0 2017 tty->print_cr("special not_peeled cloning old: %d new: %d", n->_idx, n_clone->_idx);
aoqi@0 2018 }
aoqi@0 2019 #endif
aoqi@0 2020 while( worklist.size() ) {
aoqi@0 2021 Node *use = worklist.pop();
aoqi@0 2022 _igvn.rehash_node_delayed(use);
aoqi@0 2023 for (uint j = 1; j < use->req(); j++) {
aoqi@0 2024 if (use->in(j) == n) {
aoqi@0 2025 use->set_req(j, n_clone);
aoqi@0 2026 }
aoqi@0 2027 }
aoqi@0 2028 }
aoqi@0 2029 }
aoqi@0 2030 }
aoqi@0 2031
aoqi@0 2032
aoqi@0 2033 //------------------------------ insert_phi_for_loop -------------------------------------
aoqi@0 2034 // Insert phi(lp_entry_val, back_edge_val) at use->in(idx) for loop lp if phi does not already exist
aoqi@0 2035 void PhaseIdealLoop::insert_phi_for_loop( Node* use, uint idx, Node* lp_entry_val, Node* back_edge_val, LoopNode* lp ) {
aoqi@0 2036 Node *phi = PhiNode::make(lp, back_edge_val);
aoqi@0 2037 phi->set_req(LoopNode::EntryControl, lp_entry_val);
aoqi@0 2038 // Use existing phi if it already exists
aoqi@0 2039 Node *hit = _igvn.hash_find_insert(phi);
aoqi@0 2040 if( hit == NULL ) {
aoqi@0 2041 _igvn.register_new_node_with_optimizer(phi);
aoqi@0 2042 set_ctrl(phi, lp);
aoqi@0 2043 } else {
aoqi@0 2044 // Remove the new phi from the graph and use the hit
aoqi@0 2045 _igvn.remove_dead_node(phi);
aoqi@0 2046 phi = hit;
aoqi@0 2047 }
aoqi@0 2048 _igvn.replace_input_of(use, idx, phi);
aoqi@0 2049 }
aoqi@0 2050
aoqi@0 2051 #ifdef ASSERT
aoqi@0 2052 //------------------------------ is_valid_loop_partition -------------------------------------
aoqi@0 2053 // Validate the loop partition sets: peel and not_peel
aoqi@0 2054 bool PhaseIdealLoop::is_valid_loop_partition( IdealLoopTree *loop, VectorSet& peel, Node_List& peel_list,
aoqi@0 2055 VectorSet& not_peel ) {
aoqi@0 2056 uint i;
aoqi@0 2057 // Check that peel_list entries are in the peel set
aoqi@0 2058 for (i = 0; i < peel_list.size(); i++) {
aoqi@0 2059 if (!peel.test(peel_list.at(i)->_idx)) {
aoqi@0 2060 return false;
aoqi@0 2061 }
aoqi@0 2062 }
aoqi@0 2063 // Check at loop members are in one of peel set or not_peel set
aoqi@0 2064 for (i = 0; i < loop->_body.size(); i++ ) {
aoqi@0 2065 Node *def = loop->_body.at(i);
aoqi@0 2066 uint di = def->_idx;
aoqi@0 2067 // Check that peel set elements are in peel_list
aoqi@0 2068 if (peel.test(di)) {
aoqi@0 2069 if (not_peel.test(di)) {
aoqi@0 2070 return false;
aoqi@0 2071 }
aoqi@0 2072 // Must be in peel_list also
aoqi@0 2073 bool found = false;
aoqi@0 2074 for (uint j = 0; j < peel_list.size(); j++) {
aoqi@0 2075 if (peel_list.at(j)->_idx == di) {
aoqi@0 2076 found = true;
aoqi@0 2077 break;
aoqi@0 2078 }
aoqi@0 2079 }
aoqi@0 2080 if (!found) {
aoqi@0 2081 return false;
aoqi@0 2082 }
aoqi@0 2083 } else if (not_peel.test(di)) {
aoqi@0 2084 if (peel.test(di)) {
aoqi@0 2085 return false;
aoqi@0 2086 }
aoqi@0 2087 } else {
aoqi@0 2088 return false;
aoqi@0 2089 }
aoqi@0 2090 }
aoqi@0 2091 return true;
aoqi@0 2092 }
aoqi@0 2093
aoqi@0 2094 //------------------------------ is_valid_clone_loop_exit_use -------------------------------------
aoqi@0 2095 // Ensure a use outside of loop is of the right form
aoqi@0 2096 bool PhaseIdealLoop::is_valid_clone_loop_exit_use( IdealLoopTree *loop, Node* use, uint exit_idx) {
aoqi@0 2097 Node *use_c = has_ctrl(use) ? get_ctrl(use) : use;
aoqi@0 2098 return (use->is_Phi() &&
aoqi@0 2099 use_c->is_Region() && use_c->req() == 3 &&
aoqi@0 2100 (use_c->in(exit_idx)->Opcode() == Op_IfTrue ||
aoqi@0 2101 use_c->in(exit_idx)->Opcode() == Op_IfFalse ||
aoqi@0 2102 use_c->in(exit_idx)->Opcode() == Op_JumpProj) &&
aoqi@0 2103 loop->is_member( get_loop( use_c->in(exit_idx)->in(0) ) ) );
aoqi@0 2104 }
aoqi@0 2105
aoqi@0 2106 //------------------------------ is_valid_clone_loop_form -------------------------------------
aoqi@0 2107 // Ensure that all uses outside of loop are of the right form
aoqi@0 2108 bool PhaseIdealLoop::is_valid_clone_loop_form( IdealLoopTree *loop, Node_List& peel_list,
aoqi@0 2109 uint orig_exit_idx, uint clone_exit_idx) {
aoqi@0 2110 uint len = peel_list.size();
aoqi@0 2111 for (uint i = 0; i < len; i++) {
aoqi@0 2112 Node *def = peel_list.at(i);
aoqi@0 2113
aoqi@0 2114 for (DUIterator_Fast jmax, j = def->fast_outs(jmax); j < jmax; j++) {
aoqi@0 2115 Node *use = def->fast_out(j);
aoqi@0 2116 Node *use_c = has_ctrl(use) ? get_ctrl(use) : use;
aoqi@0 2117 if (!loop->is_member(get_loop(use_c))) {
aoqi@0 2118 // use is not in the loop, check for correct structure
aoqi@0 2119 if (use->in(0) == def) {
aoqi@0 2120 // Okay
aoqi@0 2121 } else if (!is_valid_clone_loop_exit_use(loop, use, orig_exit_idx)) {
aoqi@0 2122 return false;
aoqi@0 2123 }
aoqi@0 2124 }
aoqi@0 2125 }
aoqi@0 2126 }
aoqi@0 2127 return true;
aoqi@0 2128 }
aoqi@0 2129 #endif
aoqi@0 2130
aoqi@0 2131 //------------------------------ partial_peel -------------------------------------
aoqi@0 2132 // Partially peel (aka loop rotation) the top portion of a loop (called
aoqi@0 2133 // the peel section below) by cloning it and placing one copy just before
aoqi@0 2134 // the new loop head and the other copy at the bottom of the new loop.
aoqi@0 2135 //
aoqi@0 2136 // before after where it came from
aoqi@0 2137 //
aoqi@0 2138 // stmt1 stmt1
aoqi@0 2139 // loop: stmt2 clone
aoqi@0 2140 // stmt2 if condA goto exitA clone
aoqi@0 2141 // if condA goto exitA new_loop: new
aoqi@0 2142 // stmt3 stmt3 clone
aoqi@0 2143 // if !condB goto loop if condB goto exitB clone
aoqi@0 2144 // exitB: stmt2 orig
aoqi@0 2145 // stmt4 if !condA goto new_loop orig
aoqi@0 2146 // exitA: goto exitA
aoqi@0 2147 // exitB:
aoqi@0 2148 // stmt4
aoqi@0 2149 // exitA:
aoqi@0 2150 //
aoqi@0 2151 // Step 1: find the cut point: an exit test on probable
aoqi@0 2152 // induction variable.
aoqi@0 2153 // Step 2: schedule (with cloning) operations in the peel
aoqi@0 2154 // section that can be executed after the cut into
aoqi@0 2155 // the section that is not peeled. This may need
aoqi@0 2156 // to clone operations into exit blocks. For
aoqi@0 2157 // instance, a reference to A[i] in the not-peel
aoqi@0 2158 // section and a reference to B[i] in an exit block
aoqi@0 2159 // may cause a left-shift of i by 2 to be placed
aoqi@0 2160 // in the peel block. This step will clone the left
aoqi@0 2161 // shift into the exit block and sink the left shift
aoqi@0 2162 // from the peel to the not-peel section.
aoqi@0 2163 // Step 3: clone the loop, retarget the control, and insert
aoqi@0 2164 // phis for values that are live across the new loop
aoqi@0 2165 // head. This is very dependent on the graph structure
aoqi@0 2166 // from clone_loop. It creates region nodes for
aoqi@0 2167 // exit control and associated phi nodes for values
aoqi@0 2168 // flow out of the loop through that exit. The region
aoqi@0 2169 // node is dominated by the clone's control projection.
aoqi@0 2170 // So the clone's peel section is placed before the
aoqi@0 2171 // new loop head, and the clone's not-peel section is
aoqi@0 2172 // forms the top part of the new loop. The original
aoqi@0 2173 // peel section forms the tail of the new loop.
aoqi@0 2174 // Step 4: update the dominator tree and recompute the
aoqi@0 2175 // dominator depth.
aoqi@0 2176 //
aoqi@0 2177 // orig
aoqi@0 2178 //
aoqi@0 2179 // stmt1
aoqi@0 2180 // |
aoqi@0 2181 // v
aoqi@0 2182 // loop predicate
aoqi@0 2183 // |
aoqi@0 2184 // v
aoqi@0 2185 // loop<----+
aoqi@0 2186 // | |
aoqi@0 2187 // stmt2 |
aoqi@0 2188 // | |
aoqi@0 2189 // v |
aoqi@0 2190 // ifA |
aoqi@0 2191 // / | |
aoqi@0 2192 // v v |
aoqi@0 2193 // false true ^ <-- last_peel
aoqi@0 2194 // / | |
aoqi@0 2195 // / ===|==cut |
aoqi@0 2196 // / stmt3 | <-- first_not_peel
aoqi@0 2197 // / | |
aoqi@0 2198 // | v |
aoqi@0 2199 // v ifB |
aoqi@0 2200 // exitA: / \ |
aoqi@0 2201 // / \ |
aoqi@0 2202 // v v |
aoqi@0 2203 // false true |
aoqi@0 2204 // / \ |
aoqi@0 2205 // / ----+
aoqi@0 2206 // |
aoqi@0 2207 // v
aoqi@0 2208 // exitB:
aoqi@0 2209 // stmt4
aoqi@0 2210 //
aoqi@0 2211 //
aoqi@0 2212 // after clone loop
aoqi@0 2213 //
aoqi@0 2214 // stmt1
aoqi@0 2215 // |
aoqi@0 2216 // v
aoqi@0 2217 // loop predicate
aoqi@0 2218 // / \
aoqi@0 2219 // clone / \ orig
aoqi@0 2220 // / \
aoqi@0 2221 // / \
aoqi@0 2222 // v v
aoqi@0 2223 // +---->loop loop<----+
aoqi@0 2224 // | | | |
aoqi@0 2225 // | stmt2 stmt2 |
aoqi@0 2226 // | | | |
aoqi@0 2227 // | v v |
aoqi@0 2228 // | ifA ifA |
aoqi@0 2229 // | | \ / | |
aoqi@0 2230 // | v v v v |
aoqi@0 2231 // ^ true false false true ^ <-- last_peel
aoqi@0 2232 // | | ^ \ / | |
aoqi@0 2233 // | cut==|== \ \ / ===|==cut |
aoqi@0 2234 // | stmt3 \ \ / stmt3 | <-- first_not_peel
aoqi@0 2235 // | | dom | | | |
aoqi@0 2236 // | v \ 1v v2 v |
aoqi@0 2237 // | ifB regionA ifB |
aoqi@0 2238 // | / \ | / \ |
aoqi@0 2239 // | / \ v / \ |
aoqi@0 2240 // | v v exitA: v v |
aoqi@0 2241 // | true false false true |
aoqi@0 2242 // | / ^ \ / \ |
aoqi@0 2243 // +---- \ \ / ----+
aoqi@0 2244 // dom \ /
aoqi@0 2245 // \ 1v v2
aoqi@0 2246 // regionB
aoqi@0 2247 // |
aoqi@0 2248 // v
aoqi@0 2249 // exitB:
aoqi@0 2250 // stmt4
aoqi@0 2251 //
aoqi@0 2252 //
aoqi@0 2253 // after partial peel
aoqi@0 2254 //
aoqi@0 2255 // stmt1
aoqi@0 2256 // |
aoqi@0 2257 // v
aoqi@0 2258 // loop predicate
aoqi@0 2259 // /
aoqi@0 2260 // clone / orig
aoqi@0 2261 // / TOP
aoqi@0 2262 // / \
aoqi@0 2263 // v v
aoqi@0 2264 // TOP->loop loop----+
aoqi@0 2265 // | | |
aoqi@0 2266 // stmt2 stmt2 |
aoqi@0 2267 // | | |
aoqi@0 2268 // v v |
aoqi@0 2269 // ifA ifA |
aoqi@0 2270 // | \ / | |
aoqi@0 2271 // v v v v |
aoqi@0 2272 // true false false true | <-- last_peel
aoqi@0 2273 // | ^ \ / +------|---+
aoqi@0 2274 // +->newloop \ \ / === ==cut | |
aoqi@0 2275 // | stmt3 \ \ / TOP | |
aoqi@0 2276 // | | dom | | stmt3 | | <-- first_not_peel
aoqi@0 2277 // | v \ 1v v2 v | |
aoqi@0 2278 // | ifB regionA ifB ^ v
aoqi@0 2279 // | / \ | / \ | |
aoqi@0 2280 // | / \ v / \ | |
aoqi@0 2281 // | v v exitA: v v | |
aoqi@0 2282 // | true false false true | |
aoqi@0 2283 // | / ^ \ / \ | |
aoqi@0 2284 // | | \ \ / v | |
aoqi@0 2285 // | | dom \ / TOP | |
aoqi@0 2286 // | | \ 1v v2 | |
aoqi@0 2287 // ^ v regionB | |
aoqi@0 2288 // | | | | |
aoqi@0 2289 // | | v ^ v
aoqi@0 2290 // | | exitB: | |
aoqi@0 2291 // | | stmt4 | |
aoqi@0 2292 // | +------------>-----------------+ |
aoqi@0 2293 // | |
aoqi@0 2294 // +-----------------<---------------------+
aoqi@0 2295 //
aoqi@0 2296 //
aoqi@0 2297 // final graph
aoqi@0 2298 //
aoqi@0 2299 // stmt1
aoqi@0 2300 // |
aoqi@0 2301 // v
aoqi@0 2302 // loop predicate
aoqi@0 2303 // |
aoqi@0 2304 // v
aoqi@0 2305 // stmt2 clone
aoqi@0 2306 // |
aoqi@0 2307 // v
aoqi@0 2308 // ........> ifA clone
aoqi@0 2309 // : / |
aoqi@0 2310 // dom / |
aoqi@0 2311 // : v v
aoqi@0 2312 // : false true
aoqi@0 2313 // : | |
aoqi@0 2314 // : | v
aoqi@0 2315 // : | newloop<-----+
aoqi@0 2316 // : | | |
aoqi@0 2317 // : | stmt3 clone |
aoqi@0 2318 // : | | |
aoqi@0 2319 // : | v |
aoqi@0 2320 // : | ifB |
aoqi@0 2321 // : | / \ |
aoqi@0 2322 // : | v v |
aoqi@0 2323 // : | false true |
aoqi@0 2324 // : | | | |
aoqi@0 2325 // : | v stmt2 |
aoqi@0 2326 // : | exitB: | |
aoqi@0 2327 // : | stmt4 v |
aoqi@0 2328 // : | ifA orig |
aoqi@0 2329 // : | / \ |
aoqi@0 2330 // : | / \ |
aoqi@0 2331 // : | v v |
aoqi@0 2332 // : | false true |
aoqi@0 2333 // : | / \ |
aoqi@0 2334 // : v v -----+
aoqi@0 2335 // RegionA
aoqi@0 2336 // |
aoqi@0 2337 // v
aoqi@0 2338 // exitA
aoqi@0 2339 //
aoqi@0 2340 bool PhaseIdealLoop::partial_peel( IdealLoopTree *loop, Node_List &old_new ) {
aoqi@0 2341
aoqi@0 2342 assert(!loop->_head->is_CountedLoop(), "Non-counted loop only");
aoqi@0 2343 if (!loop->_head->is_Loop()) {
aoqi@0 2344 return false; }
aoqi@0 2345
aoqi@0 2346 LoopNode *head = loop->_head->as_Loop();
aoqi@0 2347
aoqi@0 2348 if (head->is_partial_peel_loop() || head->partial_peel_has_failed()) {
aoqi@0 2349 return false;
aoqi@0 2350 }
aoqi@0 2351
aoqi@0 2352 // Check for complex exit control
aoqi@0 2353 for(uint ii = 0; ii < loop->_body.size(); ii++ ) {
aoqi@0 2354 Node *n = loop->_body.at(ii);
aoqi@0 2355 int opc = n->Opcode();
aoqi@0 2356 if (n->is_Call() ||
aoqi@0 2357 opc == Op_Catch ||
aoqi@0 2358 opc == Op_CatchProj ||
aoqi@0 2359 opc == Op_Jump ||
aoqi@0 2360 opc == Op_JumpProj) {
aoqi@0 2361 #if !defined(PRODUCT)
aoqi@0 2362 if (TracePartialPeeling) {
aoqi@0 2363 tty->print_cr("\nExit control too complex: lp: %d", head->_idx);
aoqi@0 2364 }
aoqi@0 2365 #endif
aoqi@0 2366 return false;
aoqi@0 2367 }
aoqi@0 2368 }
aoqi@0 2369
aoqi@0 2370 int dd = dom_depth(head);
aoqi@0 2371
aoqi@0 2372 // Step 1: find cut point
aoqi@0 2373
aoqi@0 2374 // Walk up dominators to loop head looking for first loop exit
aoqi@0 2375 // which is executed on every path thru loop.
aoqi@0 2376 IfNode *peel_if = NULL;
aoqi@0 2377 IfNode *peel_if_cmpu = NULL;
aoqi@0 2378
aoqi@0 2379 Node *iff = loop->tail();
aoqi@0 2380 while( iff != head ) {
aoqi@0 2381 if( iff->is_If() ) {
aoqi@0 2382 Node *ctrl = get_ctrl(iff->in(1));
aoqi@0 2383 if (ctrl->is_top()) return false; // Dead test on live IF.
aoqi@0 2384 // If loop-varying exit-test, check for induction variable
aoqi@0 2385 if( loop->is_member(get_loop(ctrl)) &&
aoqi@0 2386 loop->is_loop_exit(iff) &&
aoqi@0 2387 is_possible_iv_test(iff)) {
aoqi@0 2388 Node* cmp = iff->in(1)->in(1);
aoqi@0 2389 if (cmp->Opcode() == Op_CmpI) {
aoqi@0 2390 peel_if = iff->as_If();
aoqi@0 2391 } else {
aoqi@0 2392 assert(cmp->Opcode() == Op_CmpU, "must be CmpI or CmpU");
aoqi@0 2393 peel_if_cmpu = iff->as_If();
aoqi@0 2394 }
aoqi@0 2395 }
aoqi@0 2396 }
aoqi@0 2397 iff = idom(iff);
aoqi@0 2398 }
aoqi@0 2399 // Prefer signed compare over unsigned compare.
aoqi@0 2400 IfNode* new_peel_if = NULL;
aoqi@0 2401 if (peel_if == NULL) {
aoqi@0 2402 if (!PartialPeelAtUnsignedTests || peel_if_cmpu == NULL) {
aoqi@0 2403 return false; // No peel point found
aoqi@0 2404 }
aoqi@0 2405 new_peel_if = insert_cmpi_loop_exit(peel_if_cmpu, loop);
aoqi@0 2406 if (new_peel_if == NULL) {
aoqi@0 2407 return false; // No peel point found
aoqi@0 2408 }
aoqi@0 2409 peel_if = new_peel_if;
aoqi@0 2410 }
aoqi@0 2411 Node* last_peel = stay_in_loop(peel_if, loop);
aoqi@0 2412 Node* first_not_peeled = stay_in_loop(last_peel, loop);
aoqi@0 2413 if (first_not_peeled == NULL || first_not_peeled == head) {
aoqi@0 2414 return false;
aoqi@0 2415 }
aoqi@0 2416
aoqi@0 2417 #if !defined(PRODUCT)
aoqi@0 2418 if (TraceLoopOpts) {
aoqi@0 2419 tty->print("PartialPeel ");
aoqi@0 2420 loop->dump_head();
aoqi@0 2421 }
aoqi@0 2422
aoqi@0 2423 if (TracePartialPeeling) {
aoqi@0 2424 tty->print_cr("before partial peel one iteration");
aoqi@0 2425 Node_List wl;
aoqi@0 2426 Node* t = head->in(2);
aoqi@0 2427 while (true) {
aoqi@0 2428 wl.push(t);
aoqi@0 2429 if (t == head) break;
aoqi@0 2430 t = idom(t);
aoqi@0 2431 }
aoqi@0 2432 while (wl.size() > 0) {
aoqi@0 2433 Node* tt = wl.pop();
aoqi@0 2434 tt->dump();
aoqi@0 2435 if (tt == last_peel) tty->print_cr("-- cut --");
aoqi@0 2436 }
aoqi@0 2437 }
aoqi@0 2438 #endif
aoqi@0 2439 ResourceArea *area = Thread::current()->resource_area();
aoqi@0 2440 VectorSet peel(area);
aoqi@0 2441 VectorSet not_peel(area);
aoqi@0 2442 Node_List peel_list(area);
aoqi@0 2443 Node_List worklist(area);
aoqi@0 2444 Node_List sink_list(area);
aoqi@0 2445
aoqi@0 2446 // Set of cfg nodes to peel are those that are executable from
aoqi@0 2447 // the head through last_peel.
aoqi@0 2448 assert(worklist.size() == 0, "should be empty");
aoqi@0 2449 worklist.push(head);
aoqi@0 2450 peel.set(head->_idx);
aoqi@0 2451 while (worklist.size() > 0) {
aoqi@0 2452 Node *n = worklist.pop();
aoqi@0 2453 if (n != last_peel) {
aoqi@0 2454 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) {
aoqi@0 2455 Node* use = n->fast_out(j);
aoqi@0 2456 if (use->is_CFG() &&
aoqi@0 2457 loop->is_member(get_loop(use)) &&
aoqi@0 2458 !peel.test_set(use->_idx)) {
aoqi@0 2459 worklist.push(use);
aoqi@0 2460 }
aoqi@0 2461 }
aoqi@0 2462 }
aoqi@0 2463 }
aoqi@0 2464
aoqi@0 2465 // Set of non-cfg nodes to peel are those that are control
aoqi@0 2466 // dependent on the cfg nodes.
aoqi@0 2467 uint i;
aoqi@0 2468 for(i = 0; i < loop->_body.size(); i++ ) {
aoqi@0 2469 Node *n = loop->_body.at(i);
aoqi@0 2470 Node *n_c = has_ctrl(n) ? get_ctrl(n) : n;
aoqi@0 2471 if (peel.test(n_c->_idx)) {
aoqi@0 2472 peel.set(n->_idx);
aoqi@0 2473 } else {
aoqi@0 2474 not_peel.set(n->_idx);
aoqi@0 2475 }
aoqi@0 2476 }
aoqi@0 2477
aoqi@0 2478 // Step 2: move operations from the peeled section down into the
aoqi@0 2479 // not-peeled section
aoqi@0 2480
aoqi@0 2481 // Get a post order schedule of nodes in the peel region
aoqi@0 2482 // Result in right-most operand.
aoqi@0 2483 scheduled_nodelist(loop, peel, peel_list );
aoqi@0 2484
aoqi@0 2485 assert(is_valid_loop_partition(loop, peel, peel_list, not_peel), "bad partition");
aoqi@0 2486
aoqi@0 2487 // For future check for too many new phis
aoqi@0 2488 uint old_phi_cnt = 0;
aoqi@0 2489 for (DUIterator_Fast jmax, j = head->fast_outs(jmax); j < jmax; j++) {
aoqi@0 2490 Node* use = head->fast_out(j);
aoqi@0 2491 if (use->is_Phi()) old_phi_cnt++;
aoqi@0 2492 }
aoqi@0 2493
aoqi@0 2494 #if !defined(PRODUCT)
aoqi@0 2495 if (TracePartialPeeling) {
aoqi@0 2496 tty->print_cr("\npeeled list");
aoqi@0 2497 }
aoqi@0 2498 #endif
aoqi@0 2499
aoqi@0 2500 // Evacuate nodes in peel region into the not_peeled region if possible
aoqi@0 2501 uint new_phi_cnt = 0;
aoqi@0 2502 uint cloned_for_outside_use = 0;
aoqi@0 2503 for (i = 0; i < peel_list.size();) {
aoqi@0 2504 Node* n = peel_list.at(i);
aoqi@0 2505 #if !defined(PRODUCT)
aoqi@0 2506 if (TracePartialPeeling) n->dump();
aoqi@0 2507 #endif
aoqi@0 2508 bool incr = true;
aoqi@0 2509 if ( !n->is_CFG() ) {
aoqi@0 2510
aoqi@0 2511 if ( has_use_in_set(n, not_peel) ) {
aoqi@0 2512
aoqi@0 2513 // If not used internal to the peeled region,
aoqi@0 2514 // move "n" from peeled to not_peeled region.
aoqi@0 2515
aoqi@0 2516 if ( !has_use_internal_to_set(n, peel, loop) ) {
aoqi@0 2517
aoqi@0 2518 // if not pinned and not a load (which maybe anti-dependent on a store)
aoqi@0 2519 // and not a CMove (Matcher expects only bool->cmove).
aoqi@0 2520 if ( n->in(0) == NULL && !n->is_Load() && !n->is_CMove() ) {
aoqi@0 2521 cloned_for_outside_use += clone_for_use_outside_loop( loop, n, worklist );
aoqi@0 2522 sink_list.push(n);
aoqi@0 2523 peel >>= n->_idx; // delete n from peel set.
aoqi@0 2524 not_peel <<= n->_idx; // add n to not_peel set.
aoqi@0 2525 peel_list.remove(i);
aoqi@0 2526 incr = false;
aoqi@0 2527 #if !defined(PRODUCT)
aoqi@0 2528 if (TracePartialPeeling) {
aoqi@0 2529 tty->print_cr("sink to not_peeled region: %d newbb: %d",
aoqi@0 2530 n->_idx, get_ctrl(n)->_idx);
aoqi@0 2531 }
aoqi@0 2532 #endif
aoqi@0 2533 }
aoqi@0 2534 } else {
aoqi@0 2535 // Otherwise check for special def-use cases that span
aoqi@0 2536 // the peel/not_peel boundary such as bool->if
aoqi@0 2537 clone_for_special_use_inside_loop( loop, n, not_peel, sink_list, worklist );
aoqi@0 2538 new_phi_cnt++;
aoqi@0 2539 }
aoqi@0 2540 }
aoqi@0 2541 }
aoqi@0 2542 if (incr) i++;
aoqi@0 2543 }
aoqi@0 2544
aoqi@0 2545 if (new_phi_cnt > old_phi_cnt + PartialPeelNewPhiDelta) {
aoqi@0 2546 #if !defined(PRODUCT)
aoqi@0 2547 if (TracePartialPeeling) {
aoqi@0 2548 tty->print_cr("\nToo many new phis: %d old %d new cmpi: %c",
aoqi@0 2549 new_phi_cnt, old_phi_cnt, new_peel_if != NULL?'T':'F');
aoqi@0 2550 }
aoqi@0 2551 #endif
aoqi@0 2552 if (new_peel_if != NULL) {
aoqi@0 2553 remove_cmpi_loop_exit(new_peel_if, loop);
aoqi@0 2554 }
aoqi@0 2555 // Inhibit more partial peeling on this loop
aoqi@0 2556 assert(!head->is_partial_peel_loop(), "not partial peeled");
aoqi@0 2557 head->mark_partial_peel_failed();
aoqi@0 2558 if (cloned_for_outside_use > 0) {
aoqi@0 2559 // Terminate this round of loop opts because
aoqi@0 2560 // the graph outside this loop was changed.
aoqi@0 2561 C->set_major_progress();
aoqi@0 2562 return true;
aoqi@0 2563 }
aoqi@0 2564 return false;
aoqi@0 2565 }
aoqi@0 2566
aoqi@0 2567 // Step 3: clone loop, retarget control, and insert new phis
aoqi@0 2568
aoqi@0 2569 // Create new loop head for new phis and to hang
aoqi@0 2570 // the nodes being moved (sinked) from the peel region.
aoqi@0 2571 LoopNode* new_head = new (C) LoopNode(last_peel, last_peel);
aoqi@0 2572 new_head->set_unswitch_count(head->unswitch_count()); // Preserve
aoqi@0 2573 _igvn.register_new_node_with_optimizer(new_head);
aoqi@0 2574 assert(first_not_peeled->in(0) == last_peel, "last_peel <- first_not_peeled");
aoqi@0 2575 first_not_peeled->set_req(0, new_head);
aoqi@0 2576 set_loop(new_head, loop);
aoqi@0 2577 loop->_body.push(new_head);
aoqi@0 2578 not_peel.set(new_head->_idx);
aoqi@0 2579 set_idom(new_head, last_peel, dom_depth(first_not_peeled));
aoqi@0 2580 set_idom(first_not_peeled, new_head, dom_depth(first_not_peeled));
aoqi@0 2581
aoqi@0 2582 while (sink_list.size() > 0) {
aoqi@0 2583 Node* n = sink_list.pop();
aoqi@0 2584 set_ctrl(n, new_head);
aoqi@0 2585 }
aoqi@0 2586
aoqi@0 2587 assert(is_valid_loop_partition(loop, peel, peel_list, not_peel), "bad partition");
aoqi@0 2588
aoqi@0 2589 clone_loop( loop, old_new, dd );
aoqi@0 2590
aoqi@0 2591 const uint clone_exit_idx = 1;
aoqi@0 2592 const uint orig_exit_idx = 2;
aoqi@0 2593 assert(is_valid_clone_loop_form( loop, peel_list, orig_exit_idx, clone_exit_idx ), "bad clone loop");
aoqi@0 2594
aoqi@0 2595 Node* head_clone = old_new[head->_idx];
aoqi@0 2596 LoopNode* new_head_clone = old_new[new_head->_idx]->as_Loop();
aoqi@0 2597 Node* orig_tail_clone = head_clone->in(2);
aoqi@0 2598
aoqi@0 2599 // Add phi if "def" node is in peel set and "use" is not
aoqi@0 2600
aoqi@0 2601 for(i = 0; i < peel_list.size(); i++ ) {
aoqi@0 2602 Node *def = peel_list.at(i);
aoqi@0 2603 if (!def->is_CFG()) {
aoqi@0 2604 for (DUIterator_Fast jmax, j = def->fast_outs(jmax); j < jmax; j++) {
aoqi@0 2605 Node *use = def->fast_out(j);
aoqi@0 2606 if (has_node(use) && use->in(0) != C->top() &&
aoqi@0 2607 (!peel.test(use->_idx) ||
aoqi@0 2608 (use->is_Phi() && use->in(0) == head)) ) {
aoqi@0 2609 worklist.push(use);
aoqi@0 2610 }
aoqi@0 2611 }
aoqi@0 2612 while( worklist.size() ) {
aoqi@0 2613 Node *use = worklist.pop();
aoqi@0 2614 for (uint j = 1; j < use->req(); j++) {
aoqi@0 2615 Node* n = use->in(j);
aoqi@0 2616 if (n == def) {
aoqi@0 2617
aoqi@0 2618 // "def" is in peel set, "use" is not in peel set
aoqi@0 2619 // or "use" is in the entry boundary (a phi) of the peel set
aoqi@0 2620
aoqi@0 2621 Node* use_c = has_ctrl(use) ? get_ctrl(use) : use;
aoqi@0 2622
aoqi@0 2623 if ( loop->is_member(get_loop( use_c )) ) {
aoqi@0 2624 // use is in loop
aoqi@0 2625 if (old_new[use->_idx] != NULL) { // null for dead code
aoqi@0 2626 Node* use_clone = old_new[use->_idx];
aoqi@0 2627 _igvn.replace_input_of(use, j, C->top());
aoqi@0 2628 insert_phi_for_loop( use_clone, j, old_new[def->_idx], def, new_head_clone );
aoqi@0 2629 }
aoqi@0 2630 } else {
aoqi@0 2631 assert(is_valid_clone_loop_exit_use(loop, use, orig_exit_idx), "clone loop format");
aoqi@0 2632 // use is not in the loop, check if the live range includes the cut
aoqi@0 2633 Node* lp_if = use_c->in(orig_exit_idx)->in(0);
aoqi@0 2634 if (not_peel.test(lp_if->_idx)) {
aoqi@0 2635 assert(j == orig_exit_idx, "use from original loop");
aoqi@0 2636 insert_phi_for_loop( use, clone_exit_idx, old_new[def->_idx], def, new_head_clone );
aoqi@0 2637 }
aoqi@0 2638 }
aoqi@0 2639 }
aoqi@0 2640 }
aoqi@0 2641 }
aoqi@0 2642 }
aoqi@0 2643 }
aoqi@0 2644
aoqi@0 2645 // Step 3b: retarget control
aoqi@0 2646
aoqi@0 2647 // Redirect control to the new loop head if a cloned node in
aoqi@0 2648 // the not_peeled region has control that points into the peeled region.
aoqi@0 2649 // This necessary because the cloned peeled region will be outside
aoqi@0 2650 // the loop.
aoqi@0 2651 // from to
aoqi@0 2652 // cloned-peeled <---+
aoqi@0 2653 // new_head_clone: | <--+
aoqi@0 2654 // cloned-not_peeled in(0) in(0)
aoqi@0 2655 // orig-peeled
aoqi@0 2656
aoqi@0 2657 for(i = 0; i < loop->_body.size(); i++ ) {
aoqi@0 2658 Node *n = loop->_body.at(i);
aoqi@0 2659 if (!n->is_CFG() && n->in(0) != NULL &&
aoqi@0 2660 not_peel.test(n->_idx) && peel.test(n->in(0)->_idx)) {
aoqi@0 2661 Node* n_clone = old_new[n->_idx];
aoqi@0 2662 _igvn.replace_input_of(n_clone, 0, new_head_clone);
aoqi@0 2663 }
aoqi@0 2664 }
aoqi@0 2665
aoqi@0 2666 // Backedge of the surviving new_head (the clone) is original last_peel
aoqi@0 2667 _igvn.replace_input_of(new_head_clone, LoopNode::LoopBackControl, last_peel);
aoqi@0 2668
aoqi@0 2669 // Cut first node in original not_peel set
aoqi@0 2670 _igvn.rehash_node_delayed(new_head); // Multiple edge updates:
aoqi@0 2671 new_head->set_req(LoopNode::EntryControl, C->top()); // use rehash_node_delayed / set_req instead of
aoqi@0 2672 new_head->set_req(LoopNode::LoopBackControl, C->top()); // multiple replace_input_of calls
aoqi@0 2673
aoqi@0 2674 // Copy head_clone back-branch info to original head
aoqi@0 2675 // and remove original head's loop entry and
aoqi@0 2676 // clone head's back-branch
aoqi@0 2677 _igvn.rehash_node_delayed(head); // Multiple edge updates
aoqi@0 2678 head->set_req(LoopNode::EntryControl, head_clone->in(LoopNode::LoopBackControl));
aoqi@0 2679 head->set_req(LoopNode::LoopBackControl, C->top());
aoqi@0 2680 _igvn.replace_input_of(head_clone, LoopNode::LoopBackControl, C->top());
aoqi@0 2681
aoqi@0 2682 // Similarly modify the phis
aoqi@0 2683 for (DUIterator_Fast kmax, k = head->fast_outs(kmax); k < kmax; k++) {
aoqi@0 2684 Node* use = head->fast_out(k);
aoqi@0 2685 if (use->is_Phi() && use->outcnt() > 0) {
aoqi@0 2686 Node* use_clone = old_new[use->_idx];
aoqi@0 2687 _igvn.rehash_node_delayed(use); // Multiple edge updates
aoqi@0 2688 use->set_req(LoopNode::EntryControl, use_clone->in(LoopNode::LoopBackControl));
aoqi@0 2689 use->set_req(LoopNode::LoopBackControl, C->top());
aoqi@0 2690 _igvn.replace_input_of(use_clone, LoopNode::LoopBackControl, C->top());
aoqi@0 2691 }
aoqi@0 2692 }
aoqi@0 2693
aoqi@0 2694 // Step 4: update dominator tree and dominator depth
aoqi@0 2695
aoqi@0 2696 set_idom(head, orig_tail_clone, dd);
aoqi@0 2697 recompute_dom_depth();
aoqi@0 2698
aoqi@0 2699 // Inhibit more partial peeling on this loop
aoqi@0 2700 new_head_clone->set_partial_peel_loop();
aoqi@0 2701 C->set_major_progress();
aoqi@0 2702 loop->record_for_igvn();
aoqi@0 2703
aoqi@0 2704 #if !defined(PRODUCT)
aoqi@0 2705 if (TracePartialPeeling) {
aoqi@0 2706 tty->print_cr("\nafter partial peel one iteration");
aoqi@0 2707 Node_List wl(area);
aoqi@0 2708 Node* t = last_peel;
aoqi@0 2709 while (true) {
aoqi@0 2710 wl.push(t);
aoqi@0 2711 if (t == head_clone) break;
aoqi@0 2712 t = idom(t);
aoqi@0 2713 }
aoqi@0 2714 while (wl.size() > 0) {
aoqi@0 2715 Node* tt = wl.pop();
aoqi@0 2716 if (tt == head) tty->print_cr("orig head");
aoqi@0 2717 else if (tt == new_head_clone) tty->print_cr("new head");
aoqi@0 2718 else if (tt == head_clone) tty->print_cr("clone head");
aoqi@0 2719 tt->dump();
aoqi@0 2720 }
aoqi@0 2721 }
aoqi@0 2722 #endif
aoqi@0 2723 return true;
aoqi@0 2724 }
aoqi@0 2725
aoqi@0 2726 //------------------------------reorg_offsets----------------------------------
aoqi@0 2727 // Reorganize offset computations to lower register pressure. Mostly
aoqi@0 2728 // prevent loop-fallout uses of the pre-incremented trip counter (which are
aoqi@0 2729 // then alive with the post-incremented trip counter forcing an extra
aoqi@0 2730 // register move)
aoqi@0 2731 void PhaseIdealLoop::reorg_offsets(IdealLoopTree *loop) {
aoqi@0 2732 // Perform it only for canonical counted loops.
aoqi@0 2733 // Loop's shape could be messed up by iteration_split_impl.
aoqi@0 2734 if (!loop->_head->is_CountedLoop())
aoqi@0 2735 return;
aoqi@0 2736 if (!loop->_head->as_Loop()->is_valid_counted_loop())
aoqi@0 2737 return;
aoqi@0 2738
aoqi@0 2739 CountedLoopNode *cl = loop->_head->as_CountedLoop();
aoqi@0 2740 CountedLoopEndNode *cle = cl->loopexit();
aoqi@0 2741 Node *exit = cle->proj_out(false);
aoqi@0 2742 Node *phi = cl->phi();
aoqi@0 2743
aoqi@0 2744 // Check for the special case of folks using the pre-incremented
aoqi@0 2745 // trip-counter on the fall-out path (forces the pre-incremented
aoqi@0 2746 // and post-incremented trip counter to be live at the same time).
aoqi@0 2747 // Fix this by adjusting to use the post-increment trip counter.
aoqi@0 2748
aoqi@0 2749 bool progress = true;
aoqi@0 2750 while (progress) {
aoqi@0 2751 progress = false;
aoqi@0 2752 for (DUIterator_Fast imax, i = phi->fast_outs(imax); i < imax; i++) {
aoqi@0 2753 Node* use = phi->fast_out(i); // User of trip-counter
aoqi@0 2754 if (!has_ctrl(use)) continue;
aoqi@0 2755 Node *u_ctrl = get_ctrl(use);
aoqi@0 2756 if (use->is_Phi()) {
aoqi@0 2757 u_ctrl = NULL;
aoqi@0 2758 for (uint j = 1; j < use->req(); j++)
aoqi@0 2759 if (use->in(j) == phi)
aoqi@0 2760 u_ctrl = dom_lca(u_ctrl, use->in(0)->in(j));
aoqi@0 2761 }
aoqi@0 2762 IdealLoopTree *u_loop = get_loop(u_ctrl);
aoqi@0 2763 // Look for loop-invariant use
aoqi@0 2764 if (u_loop == loop) continue;
aoqi@0 2765 if (loop->is_member(u_loop)) continue;
aoqi@0 2766 // Check that use is live out the bottom. Assuming the trip-counter
aoqi@0 2767 // update is right at the bottom, uses of of the loop middle are ok.
aoqi@0 2768 if (dom_lca(exit, u_ctrl) != exit) continue;
aoqi@0 2769 // Hit! Refactor use to use the post-incremented tripcounter.
aoqi@0 2770 // Compute a post-increment tripcounter.
aoqi@0 2771 Node *opaq = new (C) Opaque2Node( C, cle->incr() );
aoqi@0 2772 register_new_node(opaq, exit);
aoqi@0 2773 Node *neg_stride = _igvn.intcon(-cle->stride_con());
aoqi@0 2774 set_ctrl(neg_stride, C->root());
aoqi@0 2775 Node *post = new (C) AddINode( opaq, neg_stride);
aoqi@0 2776 register_new_node(post, exit);
aoqi@0 2777 _igvn.rehash_node_delayed(use);
aoqi@0 2778 for (uint j = 1; j < use->req(); j++) {
aoqi@0 2779 if (use->in(j) == phi)
aoqi@0 2780 use->set_req(j, post);
aoqi@0 2781 }
aoqi@0 2782 // Since DU info changed, rerun loop
aoqi@0 2783 progress = true;
aoqi@0 2784 break;
aoqi@0 2785 }
aoqi@0 2786 }
aoqi@0 2787
aoqi@0 2788 }

mercurial