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