1 /* 2 * Copyright (c) 1999, 2024, 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 "gc/shared/barrierSet.hpp" 27 #include "gc/shared/c2/barrierSetC2.hpp" 28 #include "memory/allocation.inline.hpp" 29 #include "memory/resourceArea.hpp" 30 #include "opto/addnode.hpp" 31 #include "opto/callnode.hpp" 32 #include "opto/castnode.hpp" 33 #include "opto/connode.hpp" 34 #include "opto/castnode.hpp" 35 #include "opto/divnode.hpp" 36 #include "opto/inlinetypenode.hpp" 37 #include "opto/loopnode.hpp" 38 #include "opto/matcher.hpp" 39 #include "opto/mulnode.hpp" 40 #include "opto/movenode.hpp" 41 #include "opto/opaquenode.hpp" 42 #include "opto/rootnode.hpp" 43 #include "opto/subnode.hpp" 44 #include "opto/subtypenode.hpp" 45 #include "opto/superword.hpp" 46 #include "opto/vectornode.hpp" 47 #include "utilities/macros.hpp" 48 49 //============================================================================= 50 //------------------------------split_thru_phi--------------------------------- 51 // Split Node 'n' through merge point if there is enough win. 52 Node* PhaseIdealLoop::split_thru_phi(Node* n, Node* region, int policy) { 53 if (n->Opcode() == Op_ConvI2L && n->bottom_type() != TypeLong::LONG) { 54 // ConvI2L may have type information on it which is unsafe to push up 55 // so disable this for now 56 return nullptr; 57 } 58 59 // Splitting range check CastIIs through a loop induction Phi can 60 // cause new Phis to be created that are left unrelated to the loop 61 // induction Phi and prevent optimizations (vectorization) 62 if (n->Opcode() == Op_CastII && region->is_CountedLoop() && 63 n->in(1) == region->as_CountedLoop()->phi()) { 64 return nullptr; 65 } 66 67 // Inline types should not be split through Phis because they cannot be merged 68 // through Phi nodes but each value input needs to be merged individually. 69 if (n->is_InlineType()) { 70 return nullptr; 71 } 72 73 if (cannot_split_division(n, region)) { 74 return nullptr; 75 } 76 77 int wins = 0; 78 assert(!n->is_CFG(), ""); 79 assert(region->is_Region(), ""); 80 81 const Type* type = n->bottom_type(); 82 const TypeOopPtr* t_oop = _igvn.type(n)->isa_oopptr(); 83 Node* phi; 84 if (t_oop != nullptr && t_oop->is_known_instance_field()) { 85 int iid = t_oop->instance_id(); 86 int index = C->get_alias_index(t_oop); 87 int offset = t_oop->offset(); 88 phi = new PhiNode(region, type, nullptr, iid, index, offset); 89 } else { 90 phi = PhiNode::make_blank(region, n); 91 } 92 uint old_unique = C->unique(); 93 for (uint i = 1; i < region->req(); i++) { 94 Node* x; 95 Node* the_clone = nullptr; 96 if (region->in(i) == C->top()) { 97 x = C->top(); // Dead path? Use a dead data op 98 } else { 99 x = n->clone(); // Else clone up the data op 100 the_clone = x; // Remember for possible deletion. 101 // Alter data node to use pre-phi inputs 102 if (n->in(0) == region) 103 x->set_req( 0, region->in(i) ); 104 for (uint j = 1; j < n->req(); j++) { 105 Node* in = n->in(j); 106 if (in->is_Phi() && in->in(0) == region) 107 x->set_req(j, in->in(i)); // Use pre-Phi input for the clone 108 } 109 } 110 // Check for a 'win' on some paths 111 const Type* t = x->Value(&_igvn); 112 113 bool singleton = t->singleton(); 114 115 // A TOP singleton indicates that there are no possible values incoming 116 // along a particular edge. In most cases, this is OK, and the Phi will 117 // be eliminated later in an Ideal call. However, we can't allow this to 118 // happen if the singleton occurs on loop entry, as the elimination of 119 // the PhiNode may cause the resulting node to migrate back to a previous 120 // loop iteration. 121 if (singleton && t == Type::TOP) { 122 // Is_Loop() == false does not confirm the absence of a loop (e.g., an 123 // irreducible loop may not be indicated by an affirmative is_Loop()); 124 // therefore, the only top we can split thru a phi is on a backedge of 125 // a loop. 126 singleton &= region->is_Loop() && (i != LoopNode::EntryControl); 127 } 128 129 if (singleton) { 130 wins++; 131 x = ((PhaseGVN&)_igvn).makecon(t); 132 } else { 133 // We now call Identity to try to simplify the cloned node. 134 // Note that some Identity methods call phase->type(this). 135 // Make sure that the type array is big enough for 136 // our new node, even though we may throw the node away. 137 // (Note: This tweaking with igvn only works because x is a new node.) 138 _igvn.set_type(x, t); 139 // If x is a TypeNode, capture any more-precise type permanently into Node 140 // otherwise it will be not updated during igvn->transform since 141 // igvn->type(x) is set to x->Value() already. 142 x->raise_bottom_type(t); 143 Node* y = x->Identity(&_igvn); 144 if (y != x) { 145 wins++; 146 x = y; 147 } else { 148 y = _igvn.hash_find(x); 149 if (y == nullptr) { 150 y = similar_subtype_check(x, region->in(i)); 151 } 152 if (y) { 153 wins++; 154 x = y; 155 } else { 156 // Else x is a new node we are keeping 157 // We do not need register_new_node_with_optimizer 158 // because set_type has already been called. 159 _igvn._worklist.push(x); 160 } 161 } 162 } 163 164 phi->set_req( i, x ); 165 166 if (the_clone == nullptr) { 167 continue; 168 } 169 170 if (the_clone != x) { 171 _igvn.remove_dead_node(the_clone); 172 } else if (region->is_Loop() && i == LoopNode::LoopBackControl && 173 n->is_Load() && can_move_to_inner_loop(n, region->as_Loop(), x)) { 174 // it is not a win if 'x' moved from an outer to an inner loop 175 // this edge case can only happen for Load nodes 176 wins = 0; 177 break; 178 } 179 } 180 // Too few wins? 181 if (wins <= policy) { 182 _igvn.remove_dead_node(phi); 183 return nullptr; 184 } 185 186 // Record Phi 187 register_new_node( phi, region ); 188 189 for (uint i2 = 1; i2 < phi->req(); i2++) { 190 Node *x = phi->in(i2); 191 // If we commoned up the cloned 'x' with another existing Node, 192 // the existing Node picks up a new use. We need to make the 193 // existing Node occur higher up so it dominates its uses. 194 Node *old_ctrl; 195 IdealLoopTree *old_loop; 196 197 if (x->is_Con()) { 198 // Constant's control is always root. 199 set_ctrl(x, C->root()); 200 continue; 201 } 202 // The occasional new node 203 if (x->_idx >= old_unique) { // Found a new, unplaced node? 204 old_ctrl = nullptr; 205 old_loop = nullptr; // Not in any prior loop 206 } else { 207 old_ctrl = get_ctrl(x); 208 old_loop = get_loop(old_ctrl); // Get prior loop 209 } 210 // New late point must dominate new use 211 Node *new_ctrl = dom_lca(old_ctrl, region->in(i2)); 212 if (new_ctrl == old_ctrl) // Nothing is changed 213 continue; 214 215 IdealLoopTree *new_loop = get_loop(new_ctrl); 216 217 // Don't move x into a loop if its uses are 218 // outside of loop. Otherwise x will be cloned 219 // for each use outside of this loop. 220 IdealLoopTree *use_loop = get_loop(region); 221 if (!new_loop->is_member(use_loop) && 222 (old_loop == nullptr || !new_loop->is_member(old_loop))) { 223 // Take early control, later control will be recalculated 224 // during next iteration of loop optimizations. 225 new_ctrl = get_early_ctrl(x); 226 new_loop = get_loop(new_ctrl); 227 } 228 // Set new location 229 set_ctrl(x, new_ctrl); 230 // If changing loop bodies, see if we need to collect into new body 231 if (old_loop != new_loop) { 232 if (old_loop && !old_loop->_child) 233 old_loop->_body.yank(x); 234 if (!new_loop->_child) 235 new_loop->_body.push(x); // Collect body info 236 } 237 } 238 239 return phi; 240 } 241 242 // Test whether node 'x' can move into an inner loop relative to node 'n'. 243 // Note: The test is not exact. Returns true if 'x' COULD end up in an inner loop, 244 // BUT it can also return true and 'x' is in the outer loop 245 bool PhaseIdealLoop::can_move_to_inner_loop(Node* n, LoopNode* n_loop, Node* x) { 246 IdealLoopTree* n_loop_tree = get_loop(n_loop); 247 IdealLoopTree* x_loop_tree = get_loop(get_early_ctrl(x)); 248 // x_loop_tree should be outer or same loop as n_loop_tree 249 return !x_loop_tree->is_member(n_loop_tree); 250 } 251 252 // Subtype checks that carry profile data don't common so look for a replacement by following edges 253 Node* PhaseIdealLoop::similar_subtype_check(const Node* x, Node* r_in) { 254 if (x->is_SubTypeCheck()) { 255 Node* in1 = x->in(1); 256 for (DUIterator_Fast imax, i = in1->fast_outs(imax); i < imax; i++) { 257 Node* u = in1->fast_out(i); 258 if (u != x && u->is_SubTypeCheck() && u->in(1) == x->in(1) && u->in(2) == x->in(2)) { 259 for (DUIterator_Fast jmax, j = u->fast_outs(jmax); j < jmax; j++) { 260 Node* bol = u->fast_out(j); 261 for (DUIterator_Fast kmax, k = bol->fast_outs(kmax); k < kmax; k++) { 262 Node* iff = bol->fast_out(k); 263 // Only dominating subtype checks are interesting: otherwise we risk replacing a subtype check by another with 264 // unrelated profile 265 if (iff->is_If() && is_dominator(iff, r_in)) { 266 return u; 267 } 268 } 269 } 270 } 271 } 272 } 273 return nullptr; 274 } 275 276 // Return true if 'n' is a Div or Mod node (without zero check If node which was removed earlier) with a loop phi divisor 277 // of a trip-counted (integer or long) loop with a backedge input that could be zero (include zero in its type range). In 278 // this case, we cannot split the division to the backedge as it could freely float above the loop exit check resulting in 279 // a division by zero. This situation is possible because the type of an increment node of an iv phi (trip-counter) could 280 // include zero while the iv phi does not (see PhiNode::Value() for trip-counted loops where we improve types of iv phis). 281 // We also need to check other loop phis as they could have been created in the same split-if pass when applying 282 // PhaseIdealLoop::split_thru_phi() to split nodes through an iv phi. 283 bool PhaseIdealLoop::cannot_split_division(const Node* n, const Node* region) const { 284 const Type* zero; 285 switch (n->Opcode()) { 286 case Op_DivI: 287 case Op_ModI: 288 zero = TypeInt::ZERO; 289 break; 290 case Op_DivL: 291 case Op_ModL: 292 zero = TypeLong::ZERO; 293 break; 294 default: 295 return false; 296 } 297 298 if (n->in(0) != nullptr) { 299 // Cannot split through phi if Div or Mod node has a control dependency to a zero check. 300 return true; 301 } 302 303 Node* divisor = n->in(2); 304 return is_divisor_counted_loop_phi(divisor, region) && 305 loop_phi_backedge_type_contains_zero(divisor, zero); 306 } 307 308 bool PhaseIdealLoop::is_divisor_counted_loop_phi(const Node* divisor, const Node* loop) { 309 return loop->is_BaseCountedLoop() && divisor->is_Phi() && divisor->in(0) == loop; 310 } 311 312 bool PhaseIdealLoop::loop_phi_backedge_type_contains_zero(const Node* phi_divisor, const Type* zero) const { 313 return _igvn.type(phi_divisor->in(LoopNode::LoopBackControl))->filter_speculative(zero) != Type::TOP; 314 } 315 316 //------------------------------dominated_by------------------------------------ 317 // Replace the dominated test with an obvious true or false. Place it on the 318 // IGVN worklist for later cleanup. Move control-dependent data Nodes on the 319 // live path up to the dominating control. 320 void PhaseIdealLoop::dominated_by(IfProjNode* prevdom, IfNode* iff, bool flip, bool pin_array_access_nodes) { 321 if (VerifyLoopOptimizations && PrintOpto) { tty->print_cr("dominating test"); } 322 323 // prevdom is the dominating projection of the dominating test. 324 assert(iff->Opcode() == Op_If || 325 iff->Opcode() == Op_CountedLoopEnd || 326 iff->Opcode() == Op_LongCountedLoopEnd || 327 iff->Opcode() == Op_RangeCheck || 328 iff->Opcode() == Op_ParsePredicate, 329 "Check this code when new subtype is added"); 330 331 int pop = prevdom->Opcode(); 332 assert( pop == Op_IfFalse || pop == Op_IfTrue, "" ); 333 if (flip) { 334 if (pop == Op_IfTrue) 335 pop = Op_IfFalse; 336 else 337 pop = Op_IfTrue; 338 } 339 // 'con' is set to true or false to kill the dominated test. 340 Node *con = _igvn.makecon(pop == Op_IfTrue ? TypeInt::ONE : TypeInt::ZERO); 341 set_ctrl(con, C->root()); // Constant gets a new use 342 // Hack the dominated test 343 _igvn.replace_input_of(iff, 1, con); 344 345 // If I don't have a reachable TRUE and FALSE path following the IfNode then 346 // I can assume this path reaches an infinite loop. In this case it's not 347 // important to optimize the data Nodes - either the whole compilation will 348 // be tossed or this path (and all data Nodes) will go dead. 349 if (iff->outcnt() != 2) return; 350 351 // Make control-dependent data Nodes on the live path (path that will remain 352 // once the dominated IF is removed) become control-dependent on the 353 // dominating projection. 354 Node* dp = iff->proj_out_or_null(pop == Op_IfTrue); 355 356 if (dp == nullptr) 357 return; 358 359 IdealLoopTree* old_loop = get_loop(dp); 360 361 for (DUIterator_Fast imax, i = dp->fast_outs(imax); i < imax; i++) { 362 Node* cd = dp->fast_out(i); // Control-dependent node 363 // Do not rewire Div and Mod nodes which could have a zero divisor to avoid skipping their zero check. 364 if (cd->depends_only_on_test() && _igvn.no_dependent_zero_check(cd)) { 365 assert(cd->in(0) == dp, ""); 366 _igvn.replace_input_of(cd, 0, prevdom); 367 if (pin_array_access_nodes) { 368 // Because of Loop Predication, Loads and range check Cast nodes that are control dependent on this range 369 // check (that is about to be removed) now depend on multiple dominating Hoisted Check Predicates. After the 370 // removal of this range check, these control dependent nodes end up at the lowest/nearest dominating predicate 371 // in the graph. To ensure that these Loads/Casts do not float above any of the dominating checks (even when the 372 // lowest dominating check is later replaced by yet another dominating check), we need to pin them at the lowest 373 // dominating check. 374 Node* clone = cd->pin_array_access_node(); 375 if (clone != nullptr) { 376 clone = _igvn.register_new_node_with_optimizer(clone, cd); 377 _igvn.replace_node(cd, clone); 378 cd = clone; 379 } 380 } 381 set_early_ctrl(cd, false); 382 IdealLoopTree* new_loop = get_loop(get_ctrl(cd)); 383 if (old_loop != new_loop) { 384 if (!old_loop->_child) { 385 old_loop->_body.yank(cd); 386 } 387 if (!new_loop->_child) { 388 new_loop->_body.push(cd); 389 } 390 } 391 --i; 392 --imax; 393 } 394 } 395 } 396 397 //------------------------------has_local_phi_input---------------------------- 398 // Return TRUE if 'n' has Phi inputs from its local block and no other 399 // block-local inputs (all non-local-phi inputs come from earlier blocks) 400 Node *PhaseIdealLoop::has_local_phi_input( Node *n ) { 401 Node *n_ctrl = get_ctrl(n); 402 // See if some inputs come from a Phi in this block, or from before 403 // this block. 404 uint i; 405 for( i = 1; i < n->req(); i++ ) { 406 Node *phi = n->in(i); 407 if( phi->is_Phi() && phi->in(0) == n_ctrl ) 408 break; 409 } 410 if( i >= n->req() ) 411 return nullptr; // No Phi inputs; nowhere to clone thru 412 413 // Check for inputs created between 'n' and the Phi input. These 414 // must split as well; they have already been given the chance 415 // (courtesy of a post-order visit) and since they did not we must 416 // recover the 'cost' of splitting them by being very profitable 417 // when splitting 'n'. Since this is unlikely we simply give up. 418 for( i = 1; i < n->req(); i++ ) { 419 Node *m = n->in(i); 420 if( get_ctrl(m) == n_ctrl && !m->is_Phi() ) { 421 // We allow the special case of AddP's with no local inputs. 422 // This allows us to split-up address expressions. 423 if (m->is_AddP() && 424 get_ctrl(m->in(AddPNode::Base)) != n_ctrl && 425 get_ctrl(m->in(AddPNode::Address)) != n_ctrl && 426 get_ctrl(m->in(AddPNode::Offset)) != n_ctrl) { 427 // Move the AddP up to the dominating point. That's fine because control of m's inputs 428 // must dominate get_ctrl(m) == n_ctrl and we just checked that the input controls are != n_ctrl. 429 Node* c = find_non_split_ctrl(idom(n_ctrl)); 430 if (c->is_OuterStripMinedLoop()) { 431 c->as_Loop()->verify_strip_mined(1); 432 c = c->in(LoopNode::EntryControl); 433 } 434 set_ctrl_and_loop(m, c); 435 continue; 436 } 437 return nullptr; 438 } 439 assert(n->is_Phi() || m->is_Phi() || is_dominator(get_ctrl(m), n_ctrl), "m has strange control"); 440 } 441 442 return n_ctrl; 443 } 444 445 // Replace expressions like ((V+I) << 2) with (V<<2 + I<<2). 446 Node* PhaseIdealLoop::remix_address_expressions_add_left_shift(Node* n, IdealLoopTree* n_loop, Node* n_ctrl, BasicType bt) { 447 assert(bt == T_INT || bt == T_LONG, "only for integers"); 448 int n_op = n->Opcode(); 449 450 if (n_op == Op_LShift(bt)) { 451 // Scale is loop invariant 452 Node* scale = n->in(2); 453 Node* scale_ctrl = get_ctrl(scale); 454 IdealLoopTree* scale_loop = get_loop(scale_ctrl); 455 if (n_loop == scale_loop || !scale_loop->is_member(n_loop)) { 456 return nullptr; 457 } 458 const TypeInt* scale_t = scale->bottom_type()->isa_int(); 459 if (scale_t != nullptr && scale_t->is_con() && scale_t->get_con() >= 16) { 460 return nullptr; // Dont bother with byte/short masking 461 } 462 // Add must vary with loop (else shift would be loop-invariant) 463 Node* add = n->in(1); 464 Node* add_ctrl = get_ctrl(add); 465 IdealLoopTree* add_loop = get_loop(add_ctrl); 466 if (n_loop != add_loop) { 467 return nullptr; // happens w/ evil ZKM loops 468 } 469 470 // Convert I-V into I+ (0-V); same for V-I 471 if (add->Opcode() == Op_Sub(bt) && 472 _igvn.type(add->in(1)) != TypeInteger::zero(bt)) { 473 assert(add->Opcode() == Op_SubI || add->Opcode() == Op_SubL, ""); 474 Node* zero = _igvn.integercon(0, bt); 475 set_ctrl(zero, C->root()); 476 Node* neg = SubNode::make(zero, add->in(2), bt); 477 register_new_node(neg, get_ctrl(add->in(2))); 478 add = AddNode::make(add->in(1), neg, bt); 479 register_new_node(add, add_ctrl); 480 } 481 if (add->Opcode() != Op_Add(bt)) return nullptr; 482 assert(add->Opcode() == Op_AddI || add->Opcode() == Op_AddL, ""); 483 // See if one add input is loop invariant 484 Node* add_var = add->in(1); 485 Node* add_var_ctrl = get_ctrl(add_var); 486 IdealLoopTree* add_var_loop = get_loop(add_var_ctrl); 487 Node* add_invar = add->in(2); 488 Node* add_invar_ctrl = get_ctrl(add_invar); 489 IdealLoopTree* add_invar_loop = get_loop(add_invar_ctrl); 490 if (add_invar_loop == n_loop) { 491 // Swap to find the invariant part 492 add_invar = add_var; 493 add_invar_ctrl = add_var_ctrl; 494 add_invar_loop = add_var_loop; 495 add_var = add->in(2); 496 } else if (add_var_loop != n_loop) { // Else neither input is loop invariant 497 return nullptr; 498 } 499 if (n_loop == add_invar_loop || !add_invar_loop->is_member(n_loop)) { 500 return nullptr; // No invariant part of the add? 501 } 502 503 // Yes! Reshape address expression! 504 Node* inv_scale = LShiftNode::make(add_invar, scale, bt); 505 Node* inv_scale_ctrl = 506 dom_depth(add_invar_ctrl) > dom_depth(scale_ctrl) ? 507 add_invar_ctrl : scale_ctrl; 508 register_new_node(inv_scale, inv_scale_ctrl); 509 Node* var_scale = LShiftNode::make(add_var, scale, bt); 510 register_new_node(var_scale, n_ctrl); 511 Node* var_add = AddNode::make(var_scale, inv_scale, bt); 512 register_new_node(var_add, n_ctrl); 513 _igvn.replace_node(n, var_add); 514 return var_add; 515 } 516 return nullptr; 517 } 518 519 //------------------------------remix_address_expressions---------------------- 520 // Rework addressing expressions to get the most loop-invariant stuff 521 // moved out. We'd like to do all associative operators, but it's especially 522 // important (common) to do address expressions. 523 Node* PhaseIdealLoop::remix_address_expressions(Node* n) { 524 if (!has_ctrl(n)) return nullptr; 525 Node* n_ctrl = get_ctrl(n); 526 IdealLoopTree* n_loop = get_loop(n_ctrl); 527 528 // See if 'n' mixes loop-varying and loop-invariant inputs and 529 // itself is loop-varying. 530 531 // Only interested in binary ops (and AddP) 532 if (n->req() < 3 || n->req() > 4) return nullptr; 533 534 Node* n1_ctrl = get_ctrl(n->in( 1)); 535 Node* n2_ctrl = get_ctrl(n->in( 2)); 536 Node* n3_ctrl = get_ctrl(n->in(n->req() == 3 ? 2 : 3)); 537 IdealLoopTree* n1_loop = get_loop(n1_ctrl); 538 IdealLoopTree* n2_loop = get_loop(n2_ctrl); 539 IdealLoopTree* n3_loop = get_loop(n3_ctrl); 540 541 // Does one of my inputs spin in a tighter loop than self? 542 if ((n_loop->is_member(n1_loop) && n_loop != n1_loop) || 543 (n_loop->is_member(n2_loop) && n_loop != n2_loop) || 544 (n_loop->is_member(n3_loop) && n_loop != n3_loop)) { 545 return nullptr; // Leave well enough alone 546 } 547 548 // Is at least one of my inputs loop-invariant? 549 if (n1_loop == n_loop && 550 n2_loop == n_loop && 551 n3_loop == n_loop) { 552 return nullptr; // No loop-invariant inputs 553 } 554 555 Node* res = remix_address_expressions_add_left_shift(n, n_loop, n_ctrl, T_INT); 556 if (res != nullptr) { 557 return res; 558 } 559 res = remix_address_expressions_add_left_shift(n, n_loop, n_ctrl, T_LONG); 560 if (res != nullptr) { 561 return res; 562 } 563 564 int n_op = n->Opcode(); 565 // Replace (I+V) with (V+I) 566 if (n_op == Op_AddI || 567 n_op == Op_AddL || 568 n_op == Op_AddF || 569 n_op == Op_AddD || 570 n_op == Op_MulI || 571 n_op == Op_MulL || 572 n_op == Op_MulF || 573 n_op == Op_MulD) { 574 if (n2_loop == n_loop) { 575 assert(n1_loop != n_loop, ""); 576 n->swap_edges(1, 2); 577 } 578 } 579 580 // Replace ((I1 +p V) +p I2) with ((I1 +p I2) +p V), 581 // but not if I2 is a constant. Skip for irreducible loops. 582 if (n_op == Op_AddP && n_loop->_head->is_Loop()) { 583 if (n2_loop == n_loop && n3_loop != n_loop) { 584 if (n->in(2)->Opcode() == Op_AddP && !n->in(3)->is_Con()) { 585 Node* n22_ctrl = get_ctrl(n->in(2)->in(2)); 586 Node* n23_ctrl = get_ctrl(n->in(2)->in(3)); 587 IdealLoopTree* n22loop = get_loop(n22_ctrl); 588 IdealLoopTree* n23_loop = get_loop(n23_ctrl); 589 if (n22loop != n_loop && n22loop->is_member(n_loop) && 590 n23_loop == n_loop) { 591 Node* add1 = new AddPNode(n->in(1), n->in(2)->in(2), n->in(3)); 592 // Stuff new AddP in the loop preheader 593 register_new_node(add1, n_loop->_head->as_Loop()->skip_strip_mined(1)->in(LoopNode::EntryControl)); 594 Node* add2 = new AddPNode(n->in(1), add1, n->in(2)->in(3)); 595 register_new_node(add2, n_ctrl); 596 _igvn.replace_node(n, add2); 597 return add2; 598 } 599 } 600 } 601 602 // Replace (I1 +p (I2 + V)) with ((I1 +p I2) +p V) 603 if (n2_loop != n_loop && n3_loop == n_loop) { 604 if (n->in(3)->Opcode() == Op_AddX) { 605 Node* V = n->in(3)->in(1); 606 Node* I = n->in(3)->in(2); 607 if (is_member(n_loop,get_ctrl(V))) { 608 } else { 609 Node *tmp = V; V = I; I = tmp; 610 } 611 if (!is_member(n_loop,get_ctrl(I))) { 612 Node* add1 = new AddPNode(n->in(1), n->in(2), I); 613 // Stuff new AddP in the loop preheader 614 register_new_node(add1, n_loop->_head->as_Loop()->skip_strip_mined(1)->in(LoopNode::EntryControl)); 615 Node* add2 = new AddPNode(n->in(1), add1, V); 616 register_new_node(add2, n_ctrl); 617 _igvn.replace_node(n, add2); 618 return add2; 619 } 620 } 621 } 622 } 623 624 return nullptr; 625 } 626 627 // Optimize ((in1[2*i] * in2[2*i]) + (in1[2*i+1] * in2[2*i+1])) 628 Node *PhaseIdealLoop::convert_add_to_muladd(Node* n) { 629 assert(n->Opcode() == Op_AddI, "sanity"); 630 Node * nn = nullptr; 631 Node * in1 = n->in(1); 632 Node * in2 = n->in(2); 633 if (in1->Opcode() == Op_MulI && in2->Opcode() == Op_MulI) { 634 IdealLoopTree* loop_n = get_loop(get_ctrl(n)); 635 if (loop_n->is_counted() && 636 loop_n->_head->as_Loop()->is_valid_counted_loop(T_INT) && 637 Matcher::match_rule_supported(Op_MulAddVS2VI) && 638 Matcher::match_rule_supported(Op_MulAddS2I)) { 639 Node* mul_in1 = in1->in(1); 640 Node* mul_in2 = in1->in(2); 641 Node* mul_in3 = in2->in(1); 642 Node* mul_in4 = in2->in(2); 643 if (mul_in1->Opcode() == Op_LoadS && 644 mul_in2->Opcode() == Op_LoadS && 645 mul_in3->Opcode() == Op_LoadS && 646 mul_in4->Opcode() == Op_LoadS) { 647 IdealLoopTree* loop1 = get_loop(get_ctrl(mul_in1)); 648 IdealLoopTree* loop2 = get_loop(get_ctrl(mul_in2)); 649 IdealLoopTree* loop3 = get_loop(get_ctrl(mul_in3)); 650 IdealLoopTree* loop4 = get_loop(get_ctrl(mul_in4)); 651 IdealLoopTree* loop5 = get_loop(get_ctrl(in1)); 652 IdealLoopTree* loop6 = get_loop(get_ctrl(in2)); 653 // All nodes should be in the same counted loop. 654 if (loop_n == loop1 && loop_n == loop2 && loop_n == loop3 && 655 loop_n == loop4 && loop_n == loop5 && loop_n == loop6) { 656 Node* adr1 = mul_in1->in(MemNode::Address); 657 Node* adr2 = mul_in2->in(MemNode::Address); 658 Node* adr3 = mul_in3->in(MemNode::Address); 659 Node* adr4 = mul_in4->in(MemNode::Address); 660 if (adr1->is_AddP() && adr2->is_AddP() && adr3->is_AddP() && adr4->is_AddP()) { 661 if ((adr1->in(AddPNode::Base) == adr3->in(AddPNode::Base)) && 662 (adr2->in(AddPNode::Base) == adr4->in(AddPNode::Base))) { 663 nn = new MulAddS2INode(mul_in1, mul_in2, mul_in3, mul_in4); 664 register_new_node(nn, get_ctrl(n)); 665 _igvn.replace_node(n, nn); 666 return nn; 667 } else if ((adr1->in(AddPNode::Base) == adr4->in(AddPNode::Base)) && 668 (adr2->in(AddPNode::Base) == adr3->in(AddPNode::Base))) { 669 nn = new MulAddS2INode(mul_in1, mul_in2, mul_in4, mul_in3); 670 register_new_node(nn, get_ctrl(n)); 671 _igvn.replace_node(n, nn); 672 return nn; 673 } 674 } 675 } 676 } 677 } 678 } 679 return nn; 680 } 681 682 //------------------------------conditional_move------------------------------- 683 // Attempt to replace a Phi with a conditional move. We have some pretty 684 // strict profitability requirements. All Phis at the merge point must 685 // be converted, so we can remove the control flow. We need to limit the 686 // number of c-moves to a small handful. All code that was in the side-arms 687 // of the CFG diamond is now speculatively executed. This code has to be 688 // "cheap enough". We are pretty much limited to CFG diamonds that merge 689 // 1 or 2 items with a total of 1 or 2 ops executed speculatively. 690 Node *PhaseIdealLoop::conditional_move( Node *region ) { 691 692 assert(region->is_Region(), "sanity check"); 693 if (region->req() != 3) return nullptr; 694 695 // Check for CFG diamond 696 Node *lp = region->in(1); 697 Node *rp = region->in(2); 698 if (!lp || !rp) return nullptr; 699 Node *lp_c = lp->in(0); 700 if (lp_c == nullptr || lp_c != rp->in(0) || !lp_c->is_If()) return nullptr; 701 IfNode *iff = lp_c->as_If(); 702 703 // Check for ops pinned in an arm of the diamond. 704 // Can't remove the control flow in this case 705 if (lp->outcnt() > 1) return nullptr; 706 if (rp->outcnt() > 1) return nullptr; 707 708 IdealLoopTree* r_loop = get_loop(region); 709 assert(r_loop == get_loop(iff), "sanity"); 710 // Always convert to CMOVE if all results are used only outside this loop. 711 bool used_inside_loop = (r_loop == _ltree_root); 712 713 // Check profitability 714 int cost = 0; 715 int phis = 0; 716 for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) { 717 Node *out = region->fast_out(i); 718 if (!out->is_Phi()) continue; // Ignore other control edges, etc 719 phis++; 720 PhiNode* phi = out->as_Phi(); 721 BasicType bt = phi->type()->basic_type(); 722 switch (bt) { 723 case T_DOUBLE: 724 case T_FLOAT: 725 if (C->use_cmove()) { 726 continue; //TODO: maybe we want to add some cost 727 } 728 cost += Matcher::float_cmove_cost(); // Could be very expensive 729 break; 730 case T_LONG: { 731 cost += Matcher::long_cmove_cost(); // May encodes as 2 CMOV's 732 } 733 case T_INT: // These all CMOV fine 734 case T_ADDRESS: { // (RawPtr) 735 cost++; 736 break; 737 } 738 case T_NARROWOOP: // Fall through 739 case T_OBJECT: { // Base oops are OK, but not derived oops 740 const TypeOopPtr *tp = phi->type()->make_ptr()->isa_oopptr(); 741 // Derived pointers are Bad (tm): what's the Base (for GC purposes) of a 742 // CMOVE'd derived pointer? It's a CMOVE'd derived base. Thus 743 // CMOVE'ing a derived pointer requires we also CMOVE the base. If we 744 // have a Phi for the base here that we convert to a CMOVE all is well 745 // and good. But if the base is dead, we'll not make a CMOVE. Later 746 // the allocator will have to produce a base by creating a CMOVE of the 747 // relevant bases. This puts the allocator in the business of 748 // manufacturing expensive instructions, generally a bad plan. 749 // Just Say No to Conditionally-Moved Derived Pointers. 750 if (tp && tp->offset() != 0) 751 return nullptr; 752 cost++; 753 break; 754 } 755 default: 756 return nullptr; // In particular, can't do memory or I/O 757 } 758 // Add in cost any speculative ops 759 for (uint j = 1; j < region->req(); j++) { 760 Node *proj = region->in(j); 761 Node *inp = phi->in(j); 762 if (inp->isa_InlineType()) { 763 // TODO 8302217 This prevents PhiNode::push_inline_types_through 764 return nullptr; 765 } 766 if (get_ctrl(inp) == proj) { // Found local op 767 cost++; 768 // Check for a chain of dependent ops; these will all become 769 // speculative in a CMOV. 770 for (uint k = 1; k < inp->req(); k++) 771 if (get_ctrl(inp->in(k)) == proj) 772 cost += ConditionalMoveLimit; // Too much speculative goo 773 } 774 } 775 // See if the Phi is used by a Cmp or Narrow oop Decode/Encode. 776 // This will likely Split-If, a higher-payoff operation. 777 for (DUIterator_Fast kmax, k = phi->fast_outs(kmax); k < kmax; k++) { 778 Node* use = phi->fast_out(k); 779 if (use->is_Cmp() || use->is_DecodeNarrowPtr() || use->is_EncodeNarrowPtr()) 780 cost += ConditionalMoveLimit; 781 // Is there a use inside the loop? 782 // Note: check only basic types since CMoveP is pinned. 783 if (!used_inside_loop && is_java_primitive(bt)) { 784 IdealLoopTree* u_loop = get_loop(has_ctrl(use) ? get_ctrl(use) : use); 785 if (r_loop == u_loop || r_loop->is_member(u_loop)) { 786 used_inside_loop = true; 787 } 788 } 789 } 790 }//for 791 Node* bol = iff->in(1); 792 if (bol->Opcode() == Op_Opaque4) { 793 return nullptr; // Ignore loop predicate checks (the Opaque4 ensures they will go away) 794 } 795 assert(bol->Opcode() == Op_Bool, "Unexpected node"); 796 int cmp_op = bol->in(1)->Opcode(); 797 if (cmp_op == Op_SubTypeCheck) { // SubTypeCheck expansion expects an IfNode 798 return nullptr; 799 } 800 // It is expensive to generate flags from a float compare. 801 // Avoid duplicated float compare. 802 if (phis > 1 && (cmp_op == Op_CmpF || cmp_op == Op_CmpD)) return nullptr; 803 804 // Ignore cost if CMOVE can be moved outside the loop. 805 if (used_inside_loop && cost >= ConditionalMoveLimit) { 806 return nullptr; 807 } 808 // Check for highly predictable branch. No point in CMOV'ing if 809 // we are going to predict accurately all the time. 810 constexpr float infrequent_prob = PROB_UNLIKELY_MAG(2); 811 if (C->use_cmove() && (cmp_op == Op_CmpF || cmp_op == Op_CmpD)) { 812 //keep going 813 } else if (iff->_prob < infrequent_prob || iff->_prob > (1.0f - infrequent_prob)) { 814 return nullptr; 815 } 816 817 // -------------- 818 // Now replace all Phis with CMOV's 819 Node *cmov_ctrl = iff->in(0); 820 uint flip = (lp->Opcode() == Op_IfTrue); 821 Node_List wq; 822 while (1) { 823 PhiNode* phi = nullptr; 824 for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) { 825 Node *out = region->fast_out(i); 826 if (out->is_Phi()) { 827 phi = out->as_Phi(); 828 break; 829 } 830 } 831 if (phi == nullptr || _igvn.type(phi) == Type::TOP) { 832 break; 833 } 834 if (PrintOpto && VerifyLoopOptimizations) { tty->print_cr("CMOV"); } 835 // Move speculative ops 836 wq.push(phi); 837 while (wq.size() > 0) { 838 Node *n = wq.pop(); 839 for (uint j = 1; j < n->req(); j++) { 840 Node* m = n->in(j); 841 if (m != nullptr && !is_dominator(get_ctrl(m), cmov_ctrl)) { 842 #ifndef PRODUCT 843 if (PrintOpto && VerifyLoopOptimizations) { 844 tty->print(" speculate: "); 845 m->dump(); 846 } 847 #endif 848 set_ctrl(m, cmov_ctrl); 849 wq.push(m); 850 } 851 } 852 } 853 Node *cmov = CMoveNode::make(cmov_ctrl, iff->in(1), phi->in(1+flip), phi->in(2-flip), _igvn.type(phi)); 854 register_new_node( cmov, cmov_ctrl ); 855 _igvn.replace_node( phi, cmov ); 856 #ifndef PRODUCT 857 if (TraceLoopOpts) { 858 tty->print("CMOV "); 859 r_loop->dump_head(); 860 if (Verbose) { 861 bol->in(1)->dump(1); 862 cmov->dump(1); 863 } 864 } 865 DEBUG_ONLY( if (VerifyLoopOptimizations) { verify(); } ); 866 #endif 867 } 868 869 // The useless CFG diamond will fold up later; see the optimization in 870 // RegionNode::Ideal. 871 _igvn._worklist.push(region); 872 873 return iff->in(1); 874 } 875 876 static void enqueue_cfg_uses(Node* m, Unique_Node_List& wq) { 877 for (DUIterator_Fast imax, i = m->fast_outs(imax); i < imax; i++) { 878 Node* u = m->fast_out(i); 879 if (u->is_CFG()) { 880 if (u->is_NeverBranch()) { 881 u = u->as_NeverBranch()->proj_out(0); 882 enqueue_cfg_uses(u, wq); 883 } else { 884 wq.push(u); 885 } 886 } 887 } 888 } 889 890 // Try moving a store out of a loop, right before the loop 891 Node* PhaseIdealLoop::try_move_store_before_loop(Node* n, Node *n_ctrl) { 892 // Store has to be first in the loop body 893 IdealLoopTree *n_loop = get_loop(n_ctrl); 894 if (n->is_Store() && n_loop != _ltree_root && 895 n_loop->is_loop() && n_loop->_head->is_Loop() && 896 n->in(0) != nullptr) { 897 Node* address = n->in(MemNode::Address); 898 Node* value = n->in(MemNode::ValueIn); 899 Node* mem = n->in(MemNode::Memory); 900 IdealLoopTree* address_loop = get_loop(get_ctrl(address)); 901 IdealLoopTree* value_loop = get_loop(get_ctrl(value)); 902 903 // - address and value must be loop invariant 904 // - memory must be a memory Phi for the loop 905 // - Store must be the only store on this memory slice in the 906 // loop: if there's another store following this one then value 907 // written at iteration i by the second store could be overwritten 908 // at iteration i+n by the first store: it's not safe to move the 909 // first store out of the loop 910 // - nothing must observe the memory Phi: it guarantees no read 911 // before the store, we are also guaranteed the store post 912 // dominates the loop head (ignoring a possible early 913 // exit). Otherwise there would be extra Phi involved between the 914 // loop's Phi and the store. 915 // - there must be no early exit from the loop before the Store 916 // (such an exit most of the time would be an extra use of the 917 // memory Phi but sometimes is a bottom memory Phi that takes the 918 // store as input). 919 920 if (!n_loop->is_member(address_loop) && 921 !n_loop->is_member(value_loop) && 922 mem->is_Phi() && mem->in(0) == n_loop->_head && 923 mem->outcnt() == 1 && 924 mem->in(LoopNode::LoopBackControl) == n) { 925 926 assert(n_loop->_tail != nullptr, "need a tail"); 927 assert(is_dominator(n_ctrl, n_loop->_tail), "store control must not be in a branch in the loop"); 928 929 // Verify that there's no early exit of the loop before the store. 930 bool ctrl_ok = false; 931 { 932 // Follow control from loop head until n, we exit the loop or 933 // we reach the tail 934 ResourceMark rm; 935 Unique_Node_List wq; 936 wq.push(n_loop->_head); 937 938 for (uint next = 0; next < wq.size(); ++next) { 939 Node *m = wq.at(next); 940 if (m == n->in(0)) { 941 ctrl_ok = true; 942 continue; 943 } 944 assert(!has_ctrl(m), "should be CFG"); 945 if (!n_loop->is_member(get_loop(m)) || m == n_loop->_tail) { 946 ctrl_ok = false; 947 break; 948 } 949 enqueue_cfg_uses(m, wq); 950 if (wq.size() > 10) { 951 ctrl_ok = false; 952 break; 953 } 954 } 955 } 956 if (ctrl_ok) { 957 // move the Store 958 _igvn.replace_input_of(mem, LoopNode::LoopBackControl, mem); 959 _igvn.replace_input_of(n, 0, n_loop->_head->as_Loop()->skip_strip_mined()->in(LoopNode::EntryControl)); 960 _igvn.replace_input_of(n, MemNode::Memory, mem->in(LoopNode::EntryControl)); 961 // Disconnect the phi now. An empty phi can confuse other 962 // optimizations in this pass of loop opts. 963 _igvn.replace_node(mem, mem->in(LoopNode::EntryControl)); 964 n_loop->_body.yank(mem); 965 966 set_ctrl_and_loop(n, n->in(0)); 967 968 return n; 969 } 970 } 971 } 972 return nullptr; 973 } 974 975 // Try moving a store out of a loop, right after the loop 976 void PhaseIdealLoop::try_move_store_after_loop(Node* n) { 977 if (n->is_Store() && n->in(0) != nullptr) { 978 Node *n_ctrl = get_ctrl(n); 979 IdealLoopTree *n_loop = get_loop(n_ctrl); 980 // Store must be in a loop 981 if (n_loop != _ltree_root && !n_loop->_irreducible) { 982 Node* address = n->in(MemNode::Address); 983 Node* value = n->in(MemNode::ValueIn); 984 IdealLoopTree* address_loop = get_loop(get_ctrl(address)); 985 // address must be loop invariant 986 if (!n_loop->is_member(address_loop)) { 987 // Store must be last on this memory slice in the loop and 988 // nothing in the loop must observe it 989 Node* phi = nullptr; 990 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 991 Node* u = n->fast_out(i); 992 if (has_ctrl(u)) { // control use? 993 IdealLoopTree *u_loop = get_loop(get_ctrl(u)); 994 if (!n_loop->is_member(u_loop)) { 995 continue; 996 } 997 if (u->is_Phi() && u->in(0) == n_loop->_head) { 998 assert(_igvn.type(u) == Type::MEMORY, "bad phi"); 999 // multiple phis on the same slice are possible 1000 if (phi != nullptr) { 1001 return; 1002 } 1003 phi = u; 1004 continue; 1005 } 1006 } 1007 return; 1008 } 1009 if (phi != nullptr) { 1010 // Nothing in the loop before the store (next iteration) 1011 // must observe the stored value 1012 bool mem_ok = true; 1013 { 1014 ResourceMark rm; 1015 Unique_Node_List wq; 1016 wq.push(phi); 1017 for (uint next = 0; next < wq.size() && mem_ok; ++next) { 1018 Node *m = wq.at(next); 1019 for (DUIterator_Fast imax, i = m->fast_outs(imax); i < imax && mem_ok; i++) { 1020 Node* u = m->fast_out(i); 1021 if (u->is_Store() || u->is_Phi()) { 1022 if (u != n) { 1023 wq.push(u); 1024 mem_ok = (wq.size() <= 10); 1025 } 1026 } else { 1027 mem_ok = false; 1028 break; 1029 } 1030 } 1031 } 1032 } 1033 if (mem_ok) { 1034 // Move the store out of the loop if the LCA of all 1035 // users (except for the phi) is outside the loop. 1036 Node* hook = new Node(1); 1037 hook->init_req(0, n_ctrl); // Add an input to prevent hook from being dead 1038 _igvn.rehash_node_delayed(phi); 1039 int count = phi->replace_edge(n, hook, &_igvn); 1040 assert(count > 0, "inconsistent phi"); 1041 1042 // Compute latest point this store can go 1043 Node* lca = get_late_ctrl(n, get_ctrl(n)); 1044 if (lca->is_OuterStripMinedLoop()) { 1045 lca = lca->in(LoopNode::EntryControl); 1046 } 1047 if (n_loop->is_member(get_loop(lca))) { 1048 // LCA is in the loop - bail out 1049 _igvn.replace_node(hook, n); 1050 return; 1051 } 1052 #ifdef ASSERT 1053 if (n_loop->_head->is_Loop() && n_loop->_head->as_Loop()->is_strip_mined()) { 1054 assert(n_loop->_head->Opcode() == Op_CountedLoop, "outer loop is a strip mined"); 1055 n_loop->_head->as_Loop()->verify_strip_mined(1); 1056 Node* outer = n_loop->_head->as_CountedLoop()->outer_loop(); 1057 IdealLoopTree* outer_loop = get_loop(outer); 1058 assert(n_loop->_parent == outer_loop, "broken loop tree"); 1059 assert(get_loop(lca) == outer_loop, "safepoint in outer loop consume all memory state"); 1060 } 1061 #endif 1062 lca = place_outside_loop(lca, n_loop); 1063 assert(!n_loop->is_member(get_loop(lca)), "control must not be back in the loop"); 1064 assert(get_loop(lca)->_nest < n_loop->_nest || lca->in(0)->is_NeverBranch(), "must not be moved into inner loop"); 1065 1066 // Move store out of the loop 1067 _igvn.replace_node(hook, n->in(MemNode::Memory)); 1068 _igvn.replace_input_of(n, 0, lca); 1069 set_ctrl_and_loop(n, lca); 1070 1071 // Disconnect the phi now. An empty phi can confuse other 1072 // optimizations in this pass of loop opts.. 1073 if (phi->in(LoopNode::LoopBackControl) == phi) { 1074 _igvn.replace_node(phi, phi->in(LoopNode::EntryControl)); 1075 n_loop->_body.yank(phi); 1076 } 1077 } 1078 } 1079 } 1080 } 1081 } 1082 } 1083 1084 // We can't use immutable memory for the flat array check because we are loading the mark word which is 1085 // mutable. Although the bits we are interested in are immutable (we check for markWord::unlocked_value), 1086 // we need to use raw memory to not break anti dependency analysis. Below code will attempt to still move 1087 // flat array checks out of loops, mainly to enable loop unswitching. 1088 void PhaseIdealLoop::move_flat_array_check_out_of_loop(Node* n) { 1089 // Skip checks for more than one array 1090 if (n->req() > 3) { 1091 return; 1092 } 1093 Node* mem = n->in(FlatArrayCheckNode::Memory); 1094 Node* array = n->in(FlatArrayCheckNode::ArrayOrKlass)->uncast(); 1095 IdealLoopTree* check_loop = get_loop(get_ctrl(n)); 1096 IdealLoopTree* ary_loop = get_loop(get_ctrl(array)); 1097 1098 // Check if array is loop invariant 1099 if (!check_loop->is_member(ary_loop)) { 1100 // Walk up memory graph from the check until we leave the loop 1101 VectorSet wq; 1102 wq.set(mem->_idx); 1103 while (check_loop->is_member(get_loop(ctrl_or_self(mem)))) { 1104 if (mem->is_Phi()) { 1105 mem = mem->in(1); 1106 } else if (mem->is_MergeMem()) { 1107 mem = mem->as_MergeMem()->memory_at(Compile::AliasIdxRaw); 1108 } else if (mem->is_Proj()) { 1109 mem = mem->in(0); 1110 } else if (mem->is_MemBar() || mem->is_SafePoint()) { 1111 mem = mem->in(TypeFunc::Memory); 1112 } else if (mem->is_Store() || mem->is_LoadStore() || mem->is_ClearArray()) { 1113 mem = mem->in(MemNode::Memory); 1114 } else { 1115 #ifdef ASSERT 1116 mem->dump(); 1117 #endif 1118 ShouldNotReachHere(); 1119 } 1120 if (wq.test_set(mem->_idx)) { 1121 return; 1122 } 1123 } 1124 // Replace memory input and re-compute ctrl to move the check out of the loop 1125 _igvn.replace_input_of(n, 1, mem); 1126 set_ctrl_and_loop(n, get_early_ctrl(n)); 1127 Node* bol = n->unique_out(); 1128 set_ctrl_and_loop(bol, get_early_ctrl(bol)); 1129 } 1130 } 1131 1132 //------------------------------split_if_with_blocks_pre----------------------- 1133 // Do the real work in a non-recursive function. Data nodes want to be 1134 // cloned in the pre-order so they can feed each other nicely. 1135 Node *PhaseIdealLoop::split_if_with_blocks_pre( Node *n ) { 1136 // Cloning these guys is unlikely to win 1137 int n_op = n->Opcode(); 1138 if (n_op == Op_MergeMem) { 1139 return n; 1140 } 1141 if (n->is_Proj()) { 1142 return n; 1143 } 1144 1145 if (n->isa_FlatArrayCheck()) { 1146 move_flat_array_check_out_of_loop(n); 1147 return n; 1148 } 1149 1150 // Do not clone-up CmpFXXX variations, as these are always 1151 // followed by a CmpI 1152 if (n->is_Cmp()) { 1153 return n; 1154 } 1155 // Attempt to use a conditional move instead of a phi/branch 1156 if (ConditionalMoveLimit > 0 && n_op == Op_Region) { 1157 Node *cmov = conditional_move( n ); 1158 if (cmov) { 1159 return cmov; 1160 } 1161 } 1162 if (n->is_CFG() || n->is_LoadStore()) { 1163 return n; 1164 } 1165 if (n->is_Opaque1()) { // Opaque nodes cannot be mod'd 1166 if (!C->major_progress()) { // If chance of no more loop opts... 1167 _igvn._worklist.push(n); // maybe we'll remove them 1168 } 1169 return n; 1170 } 1171 1172 if (n->is_Con()) { 1173 return n; // No cloning for Con nodes 1174 } 1175 1176 Node *n_ctrl = get_ctrl(n); 1177 if (!n_ctrl) { 1178 return n; // Dead node 1179 } 1180 1181 Node* res = try_move_store_before_loop(n, n_ctrl); 1182 if (res != nullptr) { 1183 return n; 1184 } 1185 1186 // Attempt to remix address expressions for loop invariants 1187 Node *m = remix_address_expressions( n ); 1188 if( m ) return m; 1189 1190 if (n_op == Op_AddI) { 1191 Node *nn = convert_add_to_muladd( n ); 1192 if ( nn ) return nn; 1193 } 1194 1195 if (n->is_ConstraintCast()) { 1196 Node* dom_cast = n->as_ConstraintCast()->dominating_cast(&_igvn, this); 1197 // ConstraintCastNode::dominating_cast() uses node control input to determine domination. 1198 // Node control inputs don't necessarily agree with loop control info (due to 1199 // transformations happened in between), thus additional dominance check is needed 1200 // to keep loop info valid. 1201 if (dom_cast != nullptr && is_dominator(get_ctrl(dom_cast), get_ctrl(n))) { 1202 _igvn.replace_node(n, dom_cast); 1203 return dom_cast; 1204 } 1205 } 1206 1207 // Determine if the Node has inputs from some local Phi. 1208 // Returns the block to clone thru. 1209 Node *n_blk = has_local_phi_input( n ); 1210 if( !n_blk ) return n; 1211 1212 // Do not clone the trip counter through on a CountedLoop 1213 // (messes up the canonical shape). 1214 if (((n_blk->is_CountedLoop() || (n_blk->is_Loop() && n_blk->as_Loop()->is_loop_nest_inner_loop())) && n->Opcode() == Op_AddI) || 1215 (n_blk->is_LongCountedLoop() && n->Opcode() == Op_AddL)) { 1216 return n; 1217 } 1218 // Pushing a shift through the iv Phi can get in the way of addressing optimizations or range check elimination 1219 if (n_blk->is_BaseCountedLoop() && n->Opcode() == Op_LShift(n_blk->as_BaseCountedLoop()->bt()) && 1220 n->in(1) == n_blk->as_BaseCountedLoop()->phi()) { 1221 return n; 1222 } 1223 1224 // Check for having no control input; not pinned. Allow 1225 // dominating control. 1226 if (n->in(0)) { 1227 Node *dom = idom(n_blk); 1228 if (dom_lca(n->in(0), dom) != n->in(0)) { 1229 return n; 1230 } 1231 } 1232 // Policy: when is it profitable. You must get more wins than 1233 // policy before it is considered profitable. Policy is usually 0, 1234 // so 1 win is considered profitable. Big merges will require big 1235 // cloning, so get a larger policy. 1236 int policy = n_blk->req() >> 2; 1237 1238 // If the loop is a candidate for range check elimination, 1239 // delay splitting through it's phi until a later loop optimization 1240 if (n_blk->is_BaseCountedLoop()) { 1241 IdealLoopTree *lp = get_loop(n_blk); 1242 if (lp && lp->_rce_candidate) { 1243 return n; 1244 } 1245 } 1246 1247 if (must_throttle_split_if()) return n; 1248 1249 // Split 'n' through the merge point if it is profitable 1250 Node *phi = split_thru_phi( n, n_blk, policy ); 1251 if (!phi) return n; 1252 1253 // Found a Phi to split thru! 1254 // Replace 'n' with the new phi 1255 _igvn.replace_node( n, phi ); 1256 // Moved a load around the loop, 'en-registering' something. 1257 if (n_blk->is_Loop() && n->is_Load() && 1258 !phi->in(LoopNode::LoopBackControl)->is_Load()) 1259 C->set_major_progress(); 1260 1261 return phi; 1262 } 1263 1264 static bool merge_point_too_heavy(Compile* C, Node* region) { 1265 // Bail out if the region and its phis have too many users. 1266 int weight = 0; 1267 for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) { 1268 weight += region->fast_out(i)->outcnt(); 1269 } 1270 int nodes_left = C->max_node_limit() - C->live_nodes(); 1271 if (weight * 8 > nodes_left) { 1272 if (PrintOpto) { 1273 tty->print_cr("*** Split-if bails out: %d nodes, region weight %d", C->unique(), weight); 1274 } 1275 return true; 1276 } else { 1277 return false; 1278 } 1279 } 1280 1281 static bool merge_point_safe(Node* region) { 1282 // 4799512: Stop split_if_with_blocks from splitting a block with a ConvI2LNode 1283 // having a PhiNode input. This sidesteps the dangerous case where the split 1284 // ConvI2LNode may become TOP if the input Value() does not 1285 // overlap the ConvI2L range, leaving a node which may not dominate its 1286 // uses. 1287 // A better fix for this problem can be found in the BugTraq entry, but 1288 // expediency for Mantis demands this hack. 1289 #ifdef _LP64 1290 for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) { 1291 Node* n = region->fast_out(i); 1292 if (n->is_Phi()) { 1293 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) { 1294 Node* m = n->fast_out(j); 1295 if (m->Opcode() == Op_ConvI2L) 1296 return false; 1297 if (m->is_CastII()) { 1298 return false; 1299 } 1300 } 1301 } 1302 } 1303 #endif 1304 return true; 1305 } 1306 1307 1308 //------------------------------place_outside_loop--------------------------------- 1309 // Place some computation outside of this loop on the path to the use passed as argument 1310 Node* PhaseIdealLoop::place_outside_loop(Node* useblock, IdealLoopTree* loop) const { 1311 Node* head = loop->_head; 1312 assert(!loop->is_member(get_loop(useblock)), "must be outside loop"); 1313 if (head->is_Loop() && head->as_Loop()->is_strip_mined()) { 1314 loop = loop->_parent; 1315 assert(loop->_head->is_OuterStripMinedLoop(), "malformed strip mined loop"); 1316 } 1317 1318 // Pick control right outside the loop 1319 for (;;) { 1320 Node* dom = idom(useblock); 1321 if (loop->is_member(get_loop(dom)) || 1322 // NeverBranch nodes are not assigned to the loop when constructed 1323 (dom->is_NeverBranch() && loop->is_member(get_loop(dom->in(0))))) { 1324 break; 1325 } 1326 useblock = dom; 1327 } 1328 assert(find_non_split_ctrl(useblock) == useblock, "should be non split control"); 1329 return useblock; 1330 } 1331 1332 1333 bool PhaseIdealLoop::identical_backtoback_ifs(Node *n) { 1334 if (!n->is_If() || n->is_BaseCountedLoopEnd()) { 1335 return false; 1336 } 1337 if (!n->in(0)->is_Region()) { 1338 return false; 1339 } 1340 1341 Node* region = n->in(0); 1342 Node* dom = idom(region); 1343 if (!dom->is_If() || !n->as_If()->same_condition(dom, &_igvn)) { 1344 return false; 1345 } 1346 IfNode* dom_if = dom->as_If(); 1347 Node* proj_true = dom_if->proj_out(1); 1348 Node* proj_false = dom_if->proj_out(0); 1349 1350 for (uint i = 1; i < region->req(); i++) { 1351 if (is_dominator(proj_true, region->in(i))) { 1352 continue; 1353 } 1354 if (is_dominator(proj_false, region->in(i))) { 1355 continue; 1356 } 1357 return false; 1358 } 1359 1360 return true; 1361 } 1362 1363 1364 bool PhaseIdealLoop::can_split_if(Node* n_ctrl) { 1365 if (must_throttle_split_if()) { 1366 return false; 1367 } 1368 1369 // Do not do 'split-if' if irreducible loops are present. 1370 if (_has_irreducible_loops) { 1371 return false; 1372 } 1373 1374 if (merge_point_too_heavy(C, n_ctrl)) { 1375 return false; 1376 } 1377 1378 // Do not do 'split-if' if some paths are dead. First do dead code 1379 // elimination and then see if its still profitable. 1380 for (uint i = 1; i < n_ctrl->req(); i++) { 1381 if (n_ctrl->in(i) == C->top()) { 1382 return false; 1383 } 1384 } 1385 1386 // If trying to do a 'Split-If' at the loop head, it is only 1387 // profitable if the cmp folds up on BOTH paths. Otherwise we 1388 // risk peeling a loop forever. 1389 1390 // CNC - Disabled for now. Requires careful handling of loop 1391 // body selection for the cloned code. Also, make sure we check 1392 // for any input path not being in the same loop as n_ctrl. For 1393 // irreducible loops we cannot check for 'n_ctrl->is_Loop()' 1394 // because the alternative loop entry points won't be converted 1395 // into LoopNodes. 1396 IdealLoopTree *n_loop = get_loop(n_ctrl); 1397 for (uint j = 1; j < n_ctrl->req(); j++) { 1398 if (get_loop(n_ctrl->in(j)) != n_loop) { 1399 return false; 1400 } 1401 } 1402 1403 // Check for safety of the merge point. 1404 if (!merge_point_safe(n_ctrl)) { 1405 return false; 1406 } 1407 1408 return true; 1409 } 1410 1411 // Detect if the node is the inner strip-mined loop 1412 // Return: null if it's not the case, or the exit of outer strip-mined loop 1413 static Node* is_inner_of_stripmined_loop(const Node* out) { 1414 Node* out_le = nullptr; 1415 1416 if (out->is_CountedLoopEnd()) { 1417 const CountedLoopNode* loop = out->as_CountedLoopEnd()->loopnode(); 1418 1419 if (loop != nullptr && loop->is_strip_mined()) { 1420 out_le = loop->in(LoopNode::EntryControl)->as_OuterStripMinedLoop()->outer_loop_exit(); 1421 } 1422 } 1423 1424 return out_le; 1425 } 1426 1427 bool PhaseIdealLoop::flat_array_element_type_check(Node *n) { 1428 // If the CmpP is a subtype check for a value that has just been 1429 // loaded from an array, the subtype check guarantees the value 1430 // can't be stored in a flat array and the load of the value 1431 // happens with a flat array check then: push the type check 1432 // through the phi of the flat array check. This needs special 1433 // logic because the subtype check's input is not a phi but a 1434 // LoadKlass that must first be cloned through the phi. 1435 if (n->Opcode() != Op_CmpP) { 1436 return false; 1437 } 1438 1439 Node* klassptr = n->in(1); 1440 Node* klasscon = n->in(2); 1441 1442 if (klassptr->is_DecodeNarrowPtr()) { 1443 klassptr = klassptr->in(1); 1444 } 1445 1446 if (klassptr->Opcode() != Op_LoadKlass && klassptr->Opcode() != Op_LoadNKlass) { 1447 return false; 1448 } 1449 1450 if (!klasscon->is_Con()) { 1451 return false; 1452 } 1453 1454 Node* addr = klassptr->in(MemNode::Address); 1455 1456 if (!addr->is_AddP()) { 1457 return false; 1458 } 1459 1460 intptr_t offset; 1461 Node* obj = AddPNode::Ideal_base_and_offset(addr, &_igvn, offset); 1462 1463 if (obj == nullptr) { 1464 return false; 1465 } 1466 1467 assert(obj != nullptr && addr->in(AddPNode::Base) == addr->in(AddPNode::Address), "malformed AddP?"); 1468 if (obj->Opcode() == Op_CastPP) { 1469 obj = obj->in(1); 1470 } 1471 1472 if (!obj->is_Phi()) { 1473 return false; 1474 } 1475 1476 Node* region = obj->in(0); 1477 1478 Node* phi = PhiNode::make_blank(region, n->in(1)); 1479 for (uint i = 1; i < region->req(); i++) { 1480 Node* in = obj->in(i); 1481 Node* ctrl = region->in(i); 1482 if (addr->in(AddPNode::Base) != obj) { 1483 Node* cast = addr->in(AddPNode::Base); 1484 assert(cast->Opcode() == Op_CastPP && cast->in(0) != nullptr, "inconsistent subgraph"); 1485 Node* cast_clone = cast->clone(); 1486 cast_clone->set_req(0, ctrl); 1487 cast_clone->set_req(1, in); 1488 register_new_node(cast_clone, ctrl); 1489 const Type* tcast = cast_clone->Value(&_igvn); 1490 _igvn.set_type(cast_clone, tcast); 1491 cast_clone->as_Type()->set_type(tcast); 1492 in = cast_clone; 1493 } 1494 Node* addr_clone = addr->clone(); 1495 addr_clone->set_req(AddPNode::Base, in); 1496 addr_clone->set_req(AddPNode::Address, in); 1497 register_new_node(addr_clone, ctrl); 1498 _igvn.set_type(addr_clone, addr_clone->Value(&_igvn)); 1499 Node* klassptr_clone = klassptr->clone(); 1500 klassptr_clone->set_req(2, addr_clone); 1501 register_new_node(klassptr_clone, ctrl); 1502 _igvn.set_type(klassptr_clone, klassptr_clone->Value(&_igvn)); 1503 if (klassptr != n->in(1)) { 1504 Node* decode = n->in(1); 1505 assert(decode->is_DecodeNarrowPtr(), "inconsistent subgraph"); 1506 Node* decode_clone = decode->clone(); 1507 decode_clone->set_req(1, klassptr_clone); 1508 register_new_node(decode_clone, ctrl); 1509 _igvn.set_type(decode_clone, decode_clone->Value(&_igvn)); 1510 klassptr_clone = decode_clone; 1511 } 1512 phi->set_req(i, klassptr_clone); 1513 } 1514 register_new_node(phi, region); 1515 Node* orig = n->in(1); 1516 _igvn.replace_input_of(n, 1, phi); 1517 split_if_with_blocks_post(n); 1518 if (n->outcnt() != 0) { 1519 _igvn.replace_input_of(n, 1, orig); 1520 _igvn.remove_dead_node(phi); 1521 } 1522 return true; 1523 } 1524 1525 //------------------------------split_if_with_blocks_post---------------------- 1526 // Do the real work in a non-recursive function. CFG hackery wants to be 1527 // in the post-order, so it can dirty the I-DOM info and not use the dirtied 1528 // info. 1529 void PhaseIdealLoop::split_if_with_blocks_post(Node *n) { 1530 1531 if (flat_array_element_type_check(n)) { 1532 return; 1533 } 1534 1535 // Cloning Cmp through Phi's involves the split-if transform. 1536 // FastLock is not used by an If 1537 if (n->is_Cmp() && !n->is_FastLock()) { 1538 Node *n_ctrl = get_ctrl(n); 1539 // Determine if the Node has inputs from some local Phi. 1540 // Returns the block to clone thru. 1541 Node *n_blk = has_local_phi_input(n); 1542 if (n_blk != n_ctrl) { 1543 return; 1544 } 1545 1546 if (!can_split_if(n_ctrl)) { 1547 return; 1548 } 1549 1550 if (n->outcnt() != 1) { 1551 return; // Multiple bool's from 1 compare? 1552 } 1553 Node *bol = n->unique_out(); 1554 assert(bol->is_Bool(), "expect a bool here"); 1555 if (bol->outcnt() != 1) { 1556 return;// Multiple branches from 1 compare? 1557 } 1558 Node *iff = bol->unique_out(); 1559 1560 // Check some safety conditions 1561 if (iff->is_If()) { // Classic split-if? 1562 if (iff->in(0) != n_ctrl) { 1563 return; // Compare must be in same blk as if 1564 } 1565 } else if (iff->is_CMove()) { // Trying to split-up a CMOVE 1566 // Can't split CMove with different control. 1567 if (get_ctrl(iff) != n_ctrl) { 1568 return; 1569 } 1570 if (get_ctrl(iff->in(2)) == n_ctrl || 1571 get_ctrl(iff->in(3)) == n_ctrl) { 1572 return; // Inputs not yet split-up 1573 } 1574 if (get_loop(n_ctrl) != get_loop(get_ctrl(iff))) { 1575 return; // Loop-invar test gates loop-varying CMOVE 1576 } 1577 } else { 1578 return; // some other kind of node, such as an Allocate 1579 } 1580 1581 // When is split-if profitable? Every 'win' on means some control flow 1582 // goes dead, so it's almost always a win. 1583 int policy = 0; 1584 // Split compare 'n' through the merge point if it is profitable 1585 Node *phi = split_thru_phi( n, n_ctrl, policy); 1586 if (!phi) { 1587 return; 1588 } 1589 1590 // Found a Phi to split thru! 1591 // Replace 'n' with the new phi 1592 _igvn.replace_node(n, phi); 1593 1594 // Now split the bool up thru the phi 1595 Node *bolphi = split_thru_phi(bol, n_ctrl, -1); 1596 guarantee(bolphi != nullptr, "null boolean phi node"); 1597 1598 _igvn.replace_node(bol, bolphi); 1599 assert(iff->in(1) == bolphi, ""); 1600 1601 if (bolphi->Value(&_igvn)->singleton()) { 1602 return; 1603 } 1604 1605 // Conditional-move? Must split up now 1606 if (!iff->is_If()) { 1607 Node *cmovphi = split_thru_phi(iff, n_ctrl, -1); 1608 _igvn.replace_node(iff, cmovphi); 1609 return; 1610 } 1611 1612 // Now split the IF 1613 C->print_method(PHASE_BEFORE_SPLIT_IF, 4, iff); 1614 if ((PrintOpto && VerifyLoopOptimizations) || TraceLoopOpts) { 1615 tty->print_cr("Split-If"); 1616 } 1617 do_split_if(iff); 1618 C->print_method(PHASE_AFTER_SPLIT_IF, 4, iff); 1619 return; 1620 } 1621 1622 // Two identical ifs back to back can be merged 1623 if (try_merge_identical_ifs(n)) { 1624 return; 1625 } 1626 1627 // Check for an IF ready to split; one that has its 1628 // condition codes input coming from a Phi at the block start. 1629 int n_op = n->Opcode(); 1630 1631 // Check for an IF being dominated by another IF same test 1632 if (n_op == Op_If || 1633 n_op == Op_RangeCheck) { 1634 Node *bol = n->in(1); 1635 uint max = bol->outcnt(); 1636 // Check for same test used more than once? 1637 if (bol->is_Bool() && (max > 1 || bol->in(1)->is_SubTypeCheck())) { 1638 // Search up IDOMs to see if this IF is dominated. 1639 Node* cmp = bol->in(1); 1640 Node *cutoff = cmp->is_SubTypeCheck() ? dom_lca(get_ctrl(cmp->in(1)), get_ctrl(cmp->in(2))) : get_ctrl(bol); 1641 1642 // Now search up IDOMs till cutoff, looking for a dominating test 1643 Node *prevdom = n; 1644 Node *dom = idom(prevdom); 1645 while (dom != cutoff) { 1646 if (dom->req() > 1 && n->as_If()->same_condition(dom, &_igvn) && prevdom->in(0) == dom && 1647 safe_for_if_replacement(dom)) { 1648 // It's invalid to move control dependent data nodes in the inner 1649 // strip-mined loop, because: 1650 // 1) break validation of LoopNode::verify_strip_mined() 1651 // 2) move code with side-effect in strip-mined loop 1652 // Move to the exit of outer strip-mined loop in that case. 1653 Node* out_le = is_inner_of_stripmined_loop(dom); 1654 if (out_le != nullptr) { 1655 prevdom = out_le; 1656 } 1657 // Replace the dominated test with an obvious true or false. 1658 // Place it on the IGVN worklist for later cleanup. 1659 C->set_major_progress(); 1660 // Split if: pin array accesses that are control dependent on a range check and moved to a regular if, 1661 // to prevent an array load from floating above its range check. There are three cases: 1662 // 1. Move from RangeCheck "a" to RangeCheck "b": don't need to pin. If we ever remove b, then we pin 1663 // all its array accesses at that point. 1664 // 2. We move from RangeCheck "a" to regular if "b": need to pin. If we ever remove b, then its array 1665 // accesses would start to float, since we don't pin at that point. 1666 // 3. If we move from regular if: don't pin. All array accesses are already assumed to be pinned. 1667 bool pin_array_access_nodes = n->Opcode() == Op_RangeCheck && 1668 prevdom->in(0)->Opcode() != Op_RangeCheck; 1669 dominated_by(prevdom->as_IfProj(), n->as_If(), false, pin_array_access_nodes); 1670 DEBUG_ONLY( if (VerifyLoopOptimizations) { verify(); } ); 1671 return; 1672 } 1673 prevdom = dom; 1674 dom = idom(prevdom); 1675 } 1676 } 1677 } 1678 1679 try_sink_out_of_loop(n); 1680 1681 try_move_store_after_loop(n); 1682 1683 // Remove multiple allocations of the same inline type 1684 if (n->is_InlineType()) { 1685 n->as_InlineType()->remove_redundant_allocations(this); 1686 } 1687 } 1688 1689 // Transform: 1690 // 1691 // if (some_condition) { 1692 // // body 1 1693 // } else { 1694 // // body 2 1695 // } 1696 // if (some_condition) { 1697 // // body 3 1698 // } else { 1699 // // body 4 1700 // } 1701 // 1702 // into: 1703 // 1704 // 1705 // if (some_condition) { 1706 // // body 1 1707 // // body 3 1708 // } else { 1709 // // body 2 1710 // // body 4 1711 // } 1712 bool PhaseIdealLoop::try_merge_identical_ifs(Node* n) { 1713 if (identical_backtoback_ifs(n) && can_split_if(n->in(0))) { 1714 Node *n_ctrl = n->in(0); 1715 IfNode* dom_if = idom(n_ctrl)->as_If(); 1716 if (n->in(1) != dom_if->in(1)) { 1717 assert(n->in(1)->in(1)->is_SubTypeCheck() && 1718 (n->in(1)->in(1)->as_SubTypeCheck()->method() != nullptr || 1719 dom_if->in(1)->in(1)->as_SubTypeCheck()->method() != nullptr), "only for subtype checks with profile data attached"); 1720 _igvn.replace_input_of(n, 1, dom_if->in(1)); 1721 } 1722 ProjNode* dom_proj_true = dom_if->proj_out(1); 1723 ProjNode* dom_proj_false = dom_if->proj_out(0); 1724 1725 // Now split the IF 1726 RegionNode* new_false_region; 1727 RegionNode* new_true_region; 1728 do_split_if(n, &new_false_region, &new_true_region); 1729 assert(new_false_region->req() == new_true_region->req(), ""); 1730 #ifdef ASSERT 1731 for (uint i = 1; i < new_false_region->req(); ++i) { 1732 assert(new_false_region->in(i)->in(0) == new_true_region->in(i)->in(0), "unexpected shape following split if"); 1733 assert(i == new_false_region->req() - 1 || new_false_region->in(i)->in(0)->in(1) == new_false_region->in(i + 1)->in(0)->in(1), "unexpected shape following split if"); 1734 } 1735 #endif 1736 assert(new_false_region->in(1)->in(0)->in(1) == dom_if->in(1), "dominating if and dominated if after split must share test"); 1737 1738 // We now have: 1739 // if (some_condition) { 1740 // // body 1 1741 // if (some_condition) { 1742 // body3: // new_true_region 1743 // // body3 1744 // } else { 1745 // goto body4; 1746 // } 1747 // } else { 1748 // // body 2 1749 // if (some_condition) { 1750 // goto body3; 1751 // } else { 1752 // body4: // new_false_region 1753 // // body4; 1754 // } 1755 // } 1756 // 1757 1758 // clone pinned nodes thru the resulting regions 1759 push_pinned_nodes_thru_region(dom_if, new_true_region); 1760 push_pinned_nodes_thru_region(dom_if, new_false_region); 1761 1762 // Optimize out the cloned ifs. Because pinned nodes were cloned, this also allows a CastPP that would be dependent 1763 // on a projection of n to have the dom_if as a control dependency. We don't want the CastPP to end up with an 1764 // unrelated control dependency. 1765 for (uint i = 1; i < new_false_region->req(); i++) { 1766 if (is_dominator(dom_proj_true, new_false_region->in(i))) { 1767 dominated_by(dom_proj_true->as_IfProj(), new_false_region->in(i)->in(0)->as_If()); 1768 } else { 1769 assert(is_dominator(dom_proj_false, new_false_region->in(i)), "bad if"); 1770 dominated_by(dom_proj_false->as_IfProj(), new_false_region->in(i)->in(0)->as_If()); 1771 } 1772 } 1773 return true; 1774 } 1775 return false; 1776 } 1777 1778 void PhaseIdealLoop::push_pinned_nodes_thru_region(IfNode* dom_if, Node* region) { 1779 for (DUIterator i = region->outs(); region->has_out(i); i++) { 1780 Node* u = region->out(i); 1781 if (!has_ctrl(u) || u->is_Phi() || !u->depends_only_on_test() || !_igvn.no_dependent_zero_check(u)) { 1782 continue; 1783 } 1784 assert(u->in(0) == region, "not a control dependent node?"); 1785 uint j = 1; 1786 for (; j < u->req(); ++j) { 1787 Node* in = u->in(j); 1788 if (!is_dominator(ctrl_or_self(in), dom_if)) { 1789 break; 1790 } 1791 } 1792 if (j == u->req()) { 1793 Node *phi = PhiNode::make_blank(region, u); 1794 for (uint k = 1; k < region->req(); ++k) { 1795 Node* clone = u->clone(); 1796 clone->set_req(0, region->in(k)); 1797 register_new_node(clone, region->in(k)); 1798 phi->init_req(k, clone); 1799 } 1800 register_new_node(phi, region); 1801 _igvn.replace_node(u, phi); 1802 --i; 1803 } 1804 } 1805 } 1806 1807 bool PhaseIdealLoop::safe_for_if_replacement(const Node* dom) const { 1808 if (!dom->is_CountedLoopEnd()) { 1809 return true; 1810 } 1811 CountedLoopEndNode* le = dom->as_CountedLoopEnd(); 1812 CountedLoopNode* cl = le->loopnode(); 1813 if (cl == nullptr) { 1814 return true; 1815 } 1816 if (!cl->is_main_loop()) { 1817 return true; 1818 } 1819 if (cl->is_canonical_loop_entry() == nullptr) { 1820 return true; 1821 } 1822 // Further unrolling is possible so loop exit condition might change 1823 return false; 1824 } 1825 1826 // See if a shared loop-varying computation has no loop-varying uses. 1827 // Happens if something is only used for JVM state in uncommon trap exits, 1828 // like various versions of induction variable+offset. Clone the 1829 // computation per usage to allow it to sink out of the loop. 1830 void PhaseIdealLoop::try_sink_out_of_loop(Node* n) { 1831 if (has_ctrl(n) && 1832 !n->is_Phi() && 1833 !n->is_Bool() && 1834 !n->is_Proj() && 1835 !n->is_MergeMem() && 1836 !n->is_CMove() && 1837 n->Opcode() != Op_Opaque4 && 1838 !n->is_Type()) { 1839 Node *n_ctrl = get_ctrl(n); 1840 IdealLoopTree *n_loop = get_loop(n_ctrl); 1841 1842 if (n->in(0) != nullptr) { 1843 IdealLoopTree* loop_ctrl = get_loop(n->in(0)); 1844 if (n_loop != loop_ctrl && n_loop->is_member(loop_ctrl)) { 1845 // n has a control input inside a loop but get_ctrl() is member of an outer loop. This could happen, for example, 1846 // for Div nodes inside a loop (control input inside loop) without a use except for an UCT (outside the loop). 1847 // Rewire control of n to right outside of the loop, regardless if its input(s) are later sunk or not. 1848 Node* maybe_pinned_n = n; 1849 Node* outside_ctrl = place_outside_loop(n_ctrl, loop_ctrl); 1850 if (n->depends_only_on_test()) { 1851 Node* pinned_clone = n->pin_array_access_node(); 1852 if (pinned_clone != nullptr) { 1853 // Pin array access nodes: if this is an array load, it's going to be dependent on a condition that's not a 1854 // range check for that access. If that condition is replaced by an identical dominating one, then an 1855 // unpinned load would risk floating above its range check. 1856 register_new_node(pinned_clone, n_ctrl); 1857 maybe_pinned_n = pinned_clone; 1858 _igvn.replace_node(n, pinned_clone); 1859 } 1860 } 1861 _igvn.replace_input_of(maybe_pinned_n, 0, outside_ctrl); 1862 } 1863 } 1864 if (n_loop != _ltree_root && n->outcnt() > 1) { 1865 // Compute early control: needed for anti-dependence analysis. It's also possible that as a result of 1866 // previous transformations in this loop opts round, the node can be hoisted now: early control will tell us. 1867 Node* early_ctrl = compute_early_ctrl(n, n_ctrl); 1868 if (n_loop->is_member(get_loop(early_ctrl)) && // check that this one can't be hoisted now 1869 ctrl_of_all_uses_out_of_loop(n, early_ctrl, n_loop)) { // All uses in outer loops! 1870 assert(!n->is_Store() && !n->is_LoadStore(), "no node with a side effect"); 1871 Node* outer_loop_clone = nullptr; 1872 for (DUIterator_Last jmin, j = n->last_outs(jmin); j >= jmin;) { 1873 Node* u = n->last_out(j); // Clone private computation per use 1874 _igvn.rehash_node_delayed(u); 1875 Node* x = nullptr; 1876 if (n->depends_only_on_test()) { 1877 // Pin array access nodes: if this is an array load, it's going to be dependent on a condition that's not a 1878 // range check for that access. If that condition is replaced by an identical dominating one, then an 1879 // unpinned load would risk floating above its range check. 1880 x = n->pin_array_access_node(); 1881 } 1882 if (x == nullptr) { 1883 x = n->clone(); 1884 } 1885 Node* x_ctrl = nullptr; 1886 if (u->is_Phi()) { 1887 // Replace all uses of normal nodes. Replace Phi uses 1888 // individually, so the separate Nodes can sink down 1889 // different paths. 1890 uint k = 1; 1891 while (u->in(k) != n) k++; 1892 u->set_req(k, x); 1893 // x goes next to Phi input path 1894 x_ctrl = u->in(0)->in(k); 1895 // Find control for 'x' next to use but not inside inner loops. 1896 x_ctrl = place_outside_loop(x_ctrl, n_loop); 1897 --j; 1898 } else { // Normal use 1899 if (has_ctrl(u)) { 1900 x_ctrl = get_ctrl(u); 1901 } else { 1902 x_ctrl = u->in(0); 1903 } 1904 // Find control for 'x' next to use but not inside inner loops. 1905 x_ctrl = place_outside_loop(x_ctrl, n_loop); 1906 // Replace all uses 1907 if (u->is_ConstraintCast() && _igvn.type(n)->higher_equal(u->bottom_type()) && u->in(0) == x_ctrl) { 1908 // If we're sinking a chain of data nodes, we might have inserted a cast to pin the use which is not necessary 1909 // anymore now that we're going to pin n as well 1910 _igvn.replace_node(u, x); 1911 --j; 1912 } else { 1913 int nb = u->replace_edge(n, x, &_igvn); 1914 j -= nb; 1915 } 1916 } 1917 1918 if (n->is_Load()) { 1919 // For loads, add a control edge to a CFG node outside of the loop 1920 // to force them to not combine and return back inside the loop 1921 // during GVN optimization (4641526). 1922 assert(x_ctrl == get_late_ctrl_with_anti_dep(x->as_Load(), early_ctrl, x_ctrl), "anti-dependences were already checked"); 1923 1924 IdealLoopTree* x_loop = get_loop(x_ctrl); 1925 Node* x_head = x_loop->_head; 1926 if (x_head->is_Loop() && x_head->is_OuterStripMinedLoop()) { 1927 // Do not add duplicate LoadNodes to the outer strip mined loop 1928 if (outer_loop_clone != nullptr) { 1929 _igvn.replace_node(x, outer_loop_clone); 1930 continue; 1931 } 1932 outer_loop_clone = x; 1933 } 1934 x->set_req(0, x_ctrl); 1935 } else if (n->in(0) != nullptr){ 1936 x->set_req(0, x_ctrl); 1937 } 1938 assert(dom_depth(n_ctrl) <= dom_depth(x_ctrl), "n is later than its clone"); 1939 assert(!n_loop->is_member(get_loop(x_ctrl)), "should have moved out of loop"); 1940 register_new_node(x, x_ctrl); 1941 1942 // Chain of AddP nodes: (AddP base (AddP base (AddP base ))) 1943 // All AddP nodes must keep the same base after sinking so: 1944 // 1- We don't add a CastPP here until the last one of the chain is sunk: if part of the chain is not sunk, 1945 // their bases remain the same. 1946 // (see 2- below) 1947 assert(!x->is_AddP() || !x->in(AddPNode::Address)->is_AddP() || 1948 x->in(AddPNode::Address)->in(AddPNode::Base) == x->in(AddPNode::Base) || 1949 !x->in(AddPNode::Address)->in(AddPNode::Base)->eqv_uncast(x->in(AddPNode::Base)), "unexpected AddP shape"); 1950 if (x->in(0) == nullptr && !x->is_DecodeNarrowPtr() && 1951 !(x->is_AddP() && x->in(AddPNode::Address)->is_AddP() && x->in(AddPNode::Address)->in(AddPNode::Base) == x->in(AddPNode::Base))) { 1952 assert(!x->is_Load(), "load should be pinned"); 1953 // Use a cast node to pin clone out of loop 1954 Node* cast = nullptr; 1955 for (uint k = 0; k < x->req(); k++) { 1956 Node* in = x->in(k); 1957 if (in != nullptr && n_loop->is_member(get_loop(get_ctrl(in)))) { 1958 const Type* in_t = _igvn.type(in); 1959 cast = ConstraintCastNode::make_cast_for_type(x_ctrl, in, in_t, 1960 ConstraintCastNode::UnconditionalDependency, nullptr); 1961 } 1962 if (cast != nullptr) { 1963 Node* prev = _igvn.hash_find_insert(cast); 1964 if (prev != nullptr && get_ctrl(prev) == x_ctrl) { 1965 cast->destruct(&_igvn); 1966 cast = prev; 1967 } else { 1968 register_new_node(cast, x_ctrl); 1969 } 1970 x->replace_edge(in, cast); 1971 // Chain of AddP nodes: 1972 // 2- A CastPP of the base is only added now that all AddP nodes are sunk 1973 if (x->is_AddP() && k == AddPNode::Base) { 1974 update_addp_chain_base(x, n->in(AddPNode::Base), cast); 1975 } 1976 break; 1977 } 1978 } 1979 assert(cast != nullptr, "must have added a cast to pin the node"); 1980 } 1981 } 1982 _igvn.remove_dead_node(n); 1983 } 1984 _dom_lca_tags_round = 0; 1985 } 1986 } 1987 } 1988 1989 void PhaseIdealLoop::update_addp_chain_base(Node* x, Node* old_base, Node* new_base) { 1990 ResourceMark rm; 1991 Node_List wq; 1992 wq.push(x); 1993 while (wq.size() != 0) { 1994 Node* n = wq.pop(); 1995 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 1996 Node* u = n->fast_out(i); 1997 if (u->is_AddP() && u->in(AddPNode::Base) == old_base) { 1998 _igvn.replace_input_of(u, AddPNode::Base, new_base); 1999 wq.push(u); 2000 } 2001 } 2002 } 2003 } 2004 2005 // Compute the early control of a node by following its inputs until we reach 2006 // nodes that are pinned. Then compute the LCA of the control of all pinned nodes. 2007 Node* PhaseIdealLoop::compute_early_ctrl(Node* n, Node* n_ctrl) { 2008 Node* early_ctrl = nullptr; 2009 ResourceMark rm; 2010 Unique_Node_List wq; 2011 wq.push(n); 2012 for (uint i = 0; i < wq.size(); i++) { 2013 Node* m = wq.at(i); 2014 Node* c = nullptr; 2015 if (m->is_CFG()) { 2016 c = m; 2017 } else if (m->pinned()) { 2018 c = m->in(0); 2019 } else { 2020 for (uint j = 0; j < m->req(); j++) { 2021 Node* in = m->in(j); 2022 if (in != nullptr) { 2023 wq.push(in); 2024 } 2025 } 2026 } 2027 if (c != nullptr) { 2028 assert(is_dominator(c, n_ctrl), "control input must dominate current control"); 2029 if (early_ctrl == nullptr || is_dominator(early_ctrl, c)) { 2030 early_ctrl = c; 2031 } 2032 } 2033 } 2034 assert(is_dominator(early_ctrl, n_ctrl), "early control must dominate current control"); 2035 return early_ctrl; 2036 } 2037 2038 bool PhaseIdealLoop::ctrl_of_all_uses_out_of_loop(const Node* n, Node* n_ctrl, IdealLoopTree* n_loop) { 2039 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 2040 Node* u = n->fast_out(i); 2041 if (u->is_Opaque1()) { 2042 return false; // Found loop limit, bugfix for 4677003 2043 } 2044 // We can't reuse tags in PhaseIdealLoop::dom_lca_for_get_late_ctrl_internal() so make sure calls to 2045 // get_late_ctrl_with_anti_dep() use their own tag 2046 _dom_lca_tags_round++; 2047 assert(_dom_lca_tags_round != 0, "shouldn't wrap around"); 2048 2049 if (u->is_Phi()) { 2050 for (uint j = 1; j < u->req(); ++j) { 2051 if (u->in(j) == n && !ctrl_of_use_out_of_loop(n, n_ctrl, n_loop, u->in(0)->in(j))) { 2052 return false; 2053 } 2054 } 2055 } else { 2056 Node* ctrl = has_ctrl(u) ? get_ctrl(u) : u->in(0); 2057 if (!ctrl_of_use_out_of_loop(n, n_ctrl, n_loop, ctrl)) { 2058 return false; 2059 } 2060 } 2061 } 2062 return true; 2063 } 2064 2065 bool PhaseIdealLoop::ctrl_of_use_out_of_loop(const Node* n, Node* n_ctrl, IdealLoopTree* n_loop, Node* ctrl) { 2066 if (n->is_Load()) { 2067 ctrl = get_late_ctrl_with_anti_dep(n->as_Load(), n_ctrl, ctrl); 2068 } 2069 IdealLoopTree *u_loop = get_loop(ctrl); 2070 if (u_loop == n_loop) { 2071 return false; // Found loop-varying use 2072 } 2073 if (n_loop->is_member(u_loop)) { 2074 return false; // Found use in inner loop 2075 } 2076 // Sinking a node from a pre loop to its main loop pins the node between the pre and main loops. If that node is input 2077 // to a check that's eliminated by range check elimination, it becomes input to an expression that feeds into the exit 2078 // test of the pre loop above the point in the graph where it's pinned. 2079 if (n_loop->_head->is_CountedLoop() && n_loop->_head->as_CountedLoop()->is_pre_loop() && 2080 u_loop->_head->is_CountedLoop() && u_loop->_head->as_CountedLoop()->is_main_loop() && 2081 n_loop->_next == get_loop(u_loop->_head->as_CountedLoop()->skip_strip_mined())) { 2082 return false; 2083 } 2084 return true; 2085 } 2086 2087 //------------------------------split_if_with_blocks--------------------------- 2088 // Check for aggressive application of 'split-if' optimization, 2089 // using basic block level info. 2090 void PhaseIdealLoop::split_if_with_blocks(VectorSet &visited, Node_Stack &nstack) { 2091 Node* root = C->root(); 2092 visited.set(root->_idx); // first, mark root as visited 2093 // Do pre-visit work for root 2094 Node* n = split_if_with_blocks_pre(root); 2095 uint cnt = n->outcnt(); 2096 uint i = 0; 2097 2098 while (true) { 2099 // Visit all children 2100 if (i < cnt) { 2101 Node* use = n->raw_out(i); 2102 ++i; 2103 if (use->outcnt() != 0 && !visited.test_set(use->_idx)) { 2104 // Now do pre-visit work for this use 2105 use = split_if_with_blocks_pre(use); 2106 nstack.push(n, i); // Save parent and next use's index. 2107 n = use; // Process all children of current use. 2108 cnt = use->outcnt(); 2109 i = 0; 2110 } 2111 } 2112 else { 2113 // All of n's children have been processed, complete post-processing. 2114 if (cnt != 0 && !n->is_Con()) { 2115 assert(has_node(n), "no dead nodes"); 2116 split_if_with_blocks_post(n); 2117 } 2118 if (must_throttle_split_if()) { 2119 nstack.clear(); 2120 } 2121 if (nstack.is_empty()) { 2122 // Finished all nodes on stack. 2123 break; 2124 } 2125 // Get saved parent node and next use's index. Visit the rest of uses. 2126 n = nstack.node(); 2127 cnt = n->outcnt(); 2128 i = nstack.index(); 2129 nstack.pop(); 2130 } 2131 } 2132 } 2133 2134 2135 //============================================================================= 2136 // 2137 // C L O N E A L O O P B O D Y 2138 // 2139 2140 //------------------------------clone_iff-------------------------------------- 2141 // Passed in a Phi merging (recursively) some nearly equivalent Bool/Cmps. 2142 // "Nearly" because all Nodes have been cloned from the original in the loop, 2143 // but the fall-in edges to the Cmp are different. Clone bool/Cmp pairs 2144 // through the Phi recursively, and return a Bool. 2145 Node* PhaseIdealLoop::clone_iff(PhiNode* phi) { 2146 2147 // Convert this Phi into a Phi merging Bools 2148 uint i; 2149 for (i = 1; i < phi->req(); i++) { 2150 Node *b = phi->in(i); 2151 if (b->is_Phi()) { 2152 _igvn.replace_input_of(phi, i, clone_iff(b->as_Phi())); 2153 } else { 2154 assert(b->is_Bool() || b->Opcode() == Op_Opaque4, ""); 2155 } 2156 } 2157 2158 Node* n = phi->in(1); 2159 Node* sample_opaque = nullptr; 2160 Node *sample_bool = nullptr; 2161 if (n->Opcode() == Op_Opaque4) { 2162 sample_opaque = n; 2163 sample_bool = n->in(1); 2164 assert(sample_bool->is_Bool(), "wrong type"); 2165 } else { 2166 sample_bool = n; 2167 } 2168 Node* sample_cmp = sample_bool->in(1); 2169 const Type* t = Type::TOP; 2170 const TypePtr* at = nullptr; 2171 if (sample_cmp->is_FlatArrayCheck()) { 2172 // Left input of a FlatArrayCheckNode is memory, set the (adr) type of the phi accordingly 2173 assert(sample_cmp->in(1)->bottom_type() == Type::MEMORY, "unexpected input type"); 2174 t = Type::MEMORY; 2175 at = TypeRawPtr::BOTTOM; 2176 } 2177 2178 // Make Phis to merge the Cmp's inputs. 2179 PhiNode *phi1 = new PhiNode(phi->in(0), t, at); 2180 PhiNode *phi2 = new PhiNode(phi->in(0), Type::TOP); 2181 for (i = 1; i < phi->req(); i++) { 2182 Node *n1 = sample_opaque == nullptr ? phi->in(i)->in(1)->in(1) : phi->in(i)->in(1)->in(1)->in(1); 2183 Node *n2 = sample_opaque == nullptr ? phi->in(i)->in(1)->in(2) : phi->in(i)->in(1)->in(1)->in(2); 2184 phi1->set_req(i, n1); 2185 phi2->set_req(i, n2); 2186 phi1->set_type(phi1->type()->meet_speculative(n1->bottom_type())); 2187 phi2->set_type(phi2->type()->meet_speculative(n2->bottom_type())); 2188 } 2189 // See if these Phis have been made before. 2190 // Register with optimizer 2191 Node *hit1 = _igvn.hash_find_insert(phi1); 2192 if (hit1) { // Hit, toss just made Phi 2193 _igvn.remove_dead_node(phi1); // Remove new phi 2194 assert(hit1->is_Phi(), "" ); 2195 phi1 = (PhiNode*)hit1; // Use existing phi 2196 } else { // Miss 2197 _igvn.register_new_node_with_optimizer(phi1); 2198 } 2199 Node *hit2 = _igvn.hash_find_insert(phi2); 2200 if (hit2) { // Hit, toss just made Phi 2201 _igvn.remove_dead_node(phi2); // Remove new phi 2202 assert(hit2->is_Phi(), "" ); 2203 phi2 = (PhiNode*)hit2; // Use existing phi 2204 } else { // Miss 2205 _igvn.register_new_node_with_optimizer(phi2); 2206 } 2207 // Register Phis with loop/block info 2208 set_ctrl(phi1, phi->in(0)); 2209 set_ctrl(phi2, phi->in(0)); 2210 // Make a new Cmp 2211 Node *cmp = sample_cmp->clone(); 2212 cmp->set_req(1, phi1); 2213 cmp->set_req(2, phi2); 2214 _igvn.register_new_node_with_optimizer(cmp); 2215 set_ctrl(cmp, phi->in(0)); 2216 2217 // Make a new Bool 2218 Node *b = sample_bool->clone(); 2219 b->set_req(1,cmp); 2220 _igvn.register_new_node_with_optimizer(b); 2221 set_ctrl(b, phi->in(0)); 2222 2223 if (sample_opaque != nullptr) { 2224 Node* opaque = sample_opaque->clone(); 2225 opaque->set_req(1, b); 2226 _igvn.register_new_node_with_optimizer(opaque); 2227 set_ctrl(opaque, phi->in(0)); 2228 return opaque; 2229 } 2230 2231 assert(b->is_Bool(), ""); 2232 return b; 2233 } 2234 2235 //------------------------------clone_bool------------------------------------- 2236 // Passed in a Phi merging (recursively) some nearly equivalent Bool/Cmps. 2237 // "Nearly" because all Nodes have been cloned from the original in the loop, 2238 // but the fall-in edges to the Cmp are different. Clone bool/Cmp pairs 2239 // through the Phi recursively, and return a Bool. 2240 CmpNode*PhaseIdealLoop::clone_bool(PhiNode* phi) { 2241 uint i; 2242 // Convert this Phi into a Phi merging Bools 2243 for( i = 1; i < phi->req(); i++ ) { 2244 Node *b = phi->in(i); 2245 if( b->is_Phi() ) { 2246 _igvn.replace_input_of(phi, i, clone_bool(b->as_Phi())); 2247 } else { 2248 assert( b->is_Cmp() || b->is_top(), "inputs are all Cmp or TOP" ); 2249 } 2250 } 2251 2252 Node *sample_cmp = phi->in(1); 2253 2254 // Make Phis to merge the Cmp's inputs. 2255 PhiNode *phi1 = new PhiNode( phi->in(0), Type::TOP ); 2256 PhiNode *phi2 = new PhiNode( phi->in(0), Type::TOP ); 2257 for( uint j = 1; j < phi->req(); j++ ) { 2258 Node *cmp_top = phi->in(j); // Inputs are all Cmp or TOP 2259 Node *n1, *n2; 2260 if( cmp_top->is_Cmp() ) { 2261 n1 = cmp_top->in(1); 2262 n2 = cmp_top->in(2); 2263 } else { 2264 n1 = n2 = cmp_top; 2265 } 2266 phi1->set_req( j, n1 ); 2267 phi2->set_req( j, n2 ); 2268 phi1->set_type(phi1->type()->meet_speculative(n1->bottom_type())); 2269 phi2->set_type(phi2->type()->meet_speculative(n2->bottom_type())); 2270 } 2271 2272 // See if these Phis have been made before. 2273 // Register with optimizer 2274 Node *hit1 = _igvn.hash_find_insert(phi1); 2275 if( hit1 ) { // Hit, toss just made Phi 2276 _igvn.remove_dead_node(phi1); // Remove new phi 2277 assert( hit1->is_Phi(), "" ); 2278 phi1 = (PhiNode*)hit1; // Use existing phi 2279 } else { // Miss 2280 _igvn.register_new_node_with_optimizer(phi1); 2281 } 2282 Node *hit2 = _igvn.hash_find_insert(phi2); 2283 if( hit2 ) { // Hit, toss just made Phi 2284 _igvn.remove_dead_node(phi2); // Remove new phi 2285 assert( hit2->is_Phi(), "" ); 2286 phi2 = (PhiNode*)hit2; // Use existing phi 2287 } else { // Miss 2288 _igvn.register_new_node_with_optimizer(phi2); 2289 } 2290 // Register Phis with loop/block info 2291 set_ctrl(phi1, phi->in(0)); 2292 set_ctrl(phi2, phi->in(0)); 2293 // Make a new Cmp 2294 Node *cmp = sample_cmp->clone(); 2295 cmp->set_req( 1, phi1 ); 2296 cmp->set_req( 2, phi2 ); 2297 _igvn.register_new_node_with_optimizer(cmp); 2298 set_ctrl(cmp, phi->in(0)); 2299 2300 assert( cmp->is_Cmp(), "" ); 2301 return (CmpNode*)cmp; 2302 } 2303 2304 void PhaseIdealLoop::clone_loop_handle_data_uses(Node* old, Node_List &old_new, 2305 IdealLoopTree* loop, IdealLoopTree* outer_loop, 2306 Node_List*& split_if_set, Node_List*& split_bool_set, 2307 Node_List*& split_cex_set, Node_List& worklist, 2308 uint new_counter, CloneLoopMode mode) { 2309 Node* nnn = old_new[old->_idx]; 2310 // Copy uses to a worklist, so I can munge the def-use info 2311 // with impunity. 2312 for (DUIterator_Fast jmax, j = old->fast_outs(jmax); j < jmax; j++) 2313 worklist.push(old->fast_out(j)); 2314 2315 while( worklist.size() ) { 2316 Node *use = worklist.pop(); 2317 if (!has_node(use)) continue; // Ignore dead nodes 2318 if (use->in(0) == C->top()) continue; 2319 IdealLoopTree *use_loop = get_loop( has_ctrl(use) ? get_ctrl(use) : use ); 2320 // Check for data-use outside of loop - at least one of OLD or USE 2321 // must not be a CFG node. 2322 #ifdef ASSERT 2323 if (loop->_head->as_Loop()->is_strip_mined() && outer_loop->is_member(use_loop) && !loop->is_member(use_loop) && old_new[use->_idx] == nullptr) { 2324 Node* sfpt = loop->_head->as_CountedLoop()->outer_safepoint(); 2325 assert(mode != IgnoreStripMined, "incorrect cloning mode"); 2326 assert((mode == ControlAroundStripMined && use == sfpt) || !use->is_reachable_from_root(), "missed a node"); 2327 } 2328 #endif 2329 if (!loop->is_member(use_loop) && !outer_loop->is_member(use_loop) && (!old->is_CFG() || !use->is_CFG())) { 2330 2331 // If the Data use is an IF, that means we have an IF outside of the 2332 // loop that is switching on a condition that is set inside of the 2333 // loop. Happens if people set a loop-exit flag; then test the flag 2334 // in the loop to break the loop, then test is again outside of the 2335 // loop to determine which way the loop exited. 2336 // Loop predicate If node connects to Bool node through Opaque1 node. 2337 // 2338 // If the use is an AllocateArray through its ValidLengthTest input, 2339 // make sure the Bool/Cmp input is cloned down to avoid a Phi between 2340 // the AllocateArray node and its ValidLengthTest input that could cause 2341 // split if to break. 2342 if (use->is_If() || use->is_CMove() || use->Opcode() == Op_Opaque4 || 2343 (use->Opcode() == Op_AllocateArray && use->in(AllocateNode::ValidLengthTest) == old)) { 2344 // Since this code is highly unlikely, we lazily build the worklist 2345 // of such Nodes to go split. 2346 if (!split_if_set) { 2347 split_if_set = new Node_List(); 2348 } 2349 split_if_set->push(use); 2350 } 2351 if (use->is_Bool()) { 2352 if (!split_bool_set) { 2353 split_bool_set = new Node_List(); 2354 } 2355 split_bool_set->push(use); 2356 } 2357 if (use->Opcode() == Op_CreateEx) { 2358 if (!split_cex_set) { 2359 split_cex_set = new Node_List(); 2360 } 2361 split_cex_set->push(use); 2362 } 2363 2364 2365 // Get "block" use is in 2366 uint idx = 0; 2367 while( use->in(idx) != old ) idx++; 2368 Node *prev = use->is_CFG() ? use : get_ctrl(use); 2369 assert(!loop->is_member(get_loop(prev)) && !outer_loop->is_member(get_loop(prev)), "" ); 2370 Node* cfg = (prev->_idx >= new_counter && prev->is_Region()) 2371 ? prev->in(2) 2372 : idom(prev); 2373 if( use->is_Phi() ) // Phi use is in prior block 2374 cfg = prev->in(idx); // NOT in block of Phi itself 2375 if (cfg->is_top()) { // Use is dead? 2376 _igvn.replace_input_of(use, idx, C->top()); 2377 continue; 2378 } 2379 2380 // If use is referenced through control edge... (idx == 0) 2381 if (mode == IgnoreStripMined && idx == 0) { 2382 LoopNode *head = loop->_head->as_Loop(); 2383 if (head->is_strip_mined() && is_dominator(head->outer_loop_exit(), prev)) { 2384 // That node is outside the inner loop, leave it outside the 2385 // outer loop as well to not confuse verification code. 2386 assert(!loop->_parent->is_member(use_loop), "should be out of the outer loop"); 2387 _igvn.replace_input_of(use, 0, head->outer_loop_exit()); 2388 continue; 2389 } 2390 } 2391 2392 while(!outer_loop->is_member(get_loop(cfg))) { 2393 prev = cfg; 2394 cfg = (cfg->_idx >= new_counter && cfg->is_Region()) ? cfg->in(2) : idom(cfg); 2395 } 2396 // If the use occurs after merging several exits from the loop, then 2397 // old value must have dominated all those exits. Since the same old 2398 // value was used on all those exits we did not need a Phi at this 2399 // merge point. NOW we do need a Phi here. Each loop exit value 2400 // is now merged with the peeled body exit; each exit gets its own 2401 // private Phi and those Phis need to be merged here. 2402 Node *phi; 2403 if( prev->is_Region() ) { 2404 if( idx == 0 ) { // Updating control edge? 2405 phi = prev; // Just use existing control 2406 } else { // Else need a new Phi 2407 phi = PhiNode::make( prev, old ); 2408 // Now recursively fix up the new uses of old! 2409 for( uint i = 1; i < prev->req(); i++ ) { 2410 worklist.push(phi); // Onto worklist once for each 'old' input 2411 } 2412 } 2413 } else { 2414 // Get new RegionNode merging old and new loop exits 2415 prev = old_new[prev->_idx]; 2416 assert( prev, "just made this in step 7" ); 2417 if( idx == 0) { // Updating control edge? 2418 phi = prev; // Just use existing control 2419 } else { // Else need a new Phi 2420 // Make a new Phi merging data values properly 2421 phi = PhiNode::make( prev, old ); 2422 phi->set_req( 1, nnn ); 2423 } 2424 } 2425 // If inserting a new Phi, check for prior hits 2426 if( idx != 0 ) { 2427 Node *hit = _igvn.hash_find_insert(phi); 2428 if( hit == nullptr ) { 2429 _igvn.register_new_node_with_optimizer(phi); // Register new phi 2430 } else { // or 2431 // Remove the new phi from the graph and use the hit 2432 _igvn.remove_dead_node(phi); 2433 phi = hit; // Use existing phi 2434 } 2435 set_ctrl(phi, prev); 2436 } 2437 // Make 'use' use the Phi instead of the old loop body exit value 2438 assert(use->in(idx) == old, "old is still input of use"); 2439 // We notify all uses of old, including use, and the indirect uses, 2440 // that may now be optimized because we have replaced old with phi. 2441 _igvn.add_users_to_worklist(old); 2442 if (idx == 0 && 2443 use->depends_only_on_test()) { 2444 Node* pinned_clone = use->pin_array_access_node(); 2445 if (pinned_clone != nullptr) { 2446 // Pin array access nodes: control is updated here to a region. If, after some transformations, only one path 2447 // into the region is left, an array load could become dependent on a condition that's not a range check for 2448 // that access. If that condition is replaced by an identical dominating one, then an unpinned load would risk 2449 // floating above its range check. 2450 pinned_clone->set_req(0, phi); 2451 register_new_node(pinned_clone, get_ctrl(use)); 2452 _igvn.replace_node(use, pinned_clone); 2453 continue; 2454 } 2455 } 2456 _igvn.replace_input_of(use, idx, phi); 2457 if( use->_idx >= new_counter ) { // If updating new phis 2458 // Not needed for correctness, but prevents a weak assert 2459 // in AddPNode from tripping (when we end up with different 2460 // base & derived Phis that will become the same after 2461 // IGVN does CSE). 2462 Node *hit = _igvn.hash_find_insert(use); 2463 if( hit ) // Go ahead and re-hash for hits. 2464 _igvn.replace_node( use, hit ); 2465 } 2466 } 2467 } 2468 } 2469 2470 static void collect_nodes_in_outer_loop_not_reachable_from_sfpt(Node* n, const IdealLoopTree *loop, const IdealLoopTree* outer_loop, 2471 const Node_List &old_new, Unique_Node_List& wq, PhaseIdealLoop* phase, 2472 bool check_old_new) { 2473 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) { 2474 Node* u = n->fast_out(j); 2475 assert(check_old_new || old_new[u->_idx] == nullptr, "shouldn't have been cloned"); 2476 if (!u->is_CFG() && (!check_old_new || old_new[u->_idx] == nullptr)) { 2477 Node* c = phase->get_ctrl(u); 2478 IdealLoopTree* u_loop = phase->get_loop(c); 2479 assert(!loop->is_member(u_loop) || !loop->_body.contains(u), "can be in outer loop or out of both loops only"); 2480 if (!loop->is_member(u_loop)) { 2481 if (outer_loop->is_member(u_loop)) { 2482 wq.push(u); 2483 } else { 2484 // nodes pinned with control in the outer loop but not referenced from the safepoint must be moved out of 2485 // the outer loop too 2486 Node* u_c = u->in(0); 2487 if (u_c != nullptr) { 2488 IdealLoopTree* u_c_loop = phase->get_loop(u_c); 2489 if (outer_loop->is_member(u_c_loop) && !loop->is_member(u_c_loop)) { 2490 wq.push(u); 2491 } 2492 } 2493 } 2494 } 2495 } 2496 } 2497 } 2498 2499 void PhaseIdealLoop::clone_outer_loop(LoopNode* head, CloneLoopMode mode, IdealLoopTree *loop, 2500 IdealLoopTree* outer_loop, int dd, Node_List &old_new, 2501 Node_List& extra_data_nodes) { 2502 if (head->is_strip_mined() && mode != IgnoreStripMined) { 2503 CountedLoopNode* cl = head->as_CountedLoop(); 2504 Node* l = cl->outer_loop(); 2505 Node* tail = cl->outer_loop_tail(); 2506 IfNode* le = cl->outer_loop_end(); 2507 Node* sfpt = cl->outer_safepoint(); 2508 CountedLoopEndNode* cle = cl->loopexit(); 2509 CountedLoopNode* new_cl = old_new[cl->_idx]->as_CountedLoop(); 2510 CountedLoopEndNode* new_cle = new_cl->as_CountedLoop()->loopexit_or_null(); 2511 Node* cle_out = cle->proj_out(false); 2512 2513 Node* new_sfpt = nullptr; 2514 Node* new_cle_out = cle_out->clone(); 2515 old_new.map(cle_out->_idx, new_cle_out); 2516 if (mode == CloneIncludesStripMined) { 2517 // clone outer loop body 2518 Node* new_l = l->clone(); 2519 Node* new_tail = tail->clone(); 2520 IfNode* new_le = le->clone()->as_If(); 2521 new_sfpt = sfpt->clone(); 2522 2523 set_loop(new_l, outer_loop->_parent); 2524 set_idom(new_l, new_l->in(LoopNode::EntryControl), dd); 2525 set_loop(new_cle_out, outer_loop->_parent); 2526 set_idom(new_cle_out, new_cle, dd); 2527 set_loop(new_sfpt, outer_loop->_parent); 2528 set_idom(new_sfpt, new_cle_out, dd); 2529 set_loop(new_le, outer_loop->_parent); 2530 set_idom(new_le, new_sfpt, dd); 2531 set_loop(new_tail, outer_loop->_parent); 2532 set_idom(new_tail, new_le, dd); 2533 set_idom(new_cl, new_l, dd); 2534 2535 old_new.map(l->_idx, new_l); 2536 old_new.map(tail->_idx, new_tail); 2537 old_new.map(le->_idx, new_le); 2538 old_new.map(sfpt->_idx, new_sfpt); 2539 2540 new_l->set_req(LoopNode::LoopBackControl, new_tail); 2541 new_l->set_req(0, new_l); 2542 new_tail->set_req(0, new_le); 2543 new_le->set_req(0, new_sfpt); 2544 new_sfpt->set_req(0, new_cle_out); 2545 new_cle_out->set_req(0, new_cle); 2546 new_cl->set_req(LoopNode::EntryControl, new_l); 2547 2548 _igvn.register_new_node_with_optimizer(new_l); 2549 _igvn.register_new_node_with_optimizer(new_tail); 2550 _igvn.register_new_node_with_optimizer(new_le); 2551 } else { 2552 Node *newhead = old_new[loop->_head->_idx]; 2553 newhead->as_Loop()->clear_strip_mined(); 2554 _igvn.replace_input_of(newhead, LoopNode::EntryControl, newhead->in(LoopNode::EntryControl)->in(LoopNode::EntryControl)); 2555 set_idom(newhead, newhead->in(LoopNode::EntryControl), dd); 2556 } 2557 // Look at data node that were assigned a control in the outer 2558 // loop: they are kept in the outer loop by the safepoint so start 2559 // from the safepoint node's inputs. 2560 IdealLoopTree* outer_loop = get_loop(l); 2561 Node_Stack stack(2); 2562 stack.push(sfpt, 1); 2563 uint new_counter = C->unique(); 2564 while (stack.size() > 0) { 2565 Node* n = stack.node(); 2566 uint i = stack.index(); 2567 while (i < n->req() && 2568 (n->in(i) == nullptr || 2569 !has_ctrl(n->in(i)) || 2570 get_loop(get_ctrl(n->in(i))) != outer_loop || 2571 (old_new[n->in(i)->_idx] != nullptr && old_new[n->in(i)->_idx]->_idx >= new_counter))) { 2572 i++; 2573 } 2574 if (i < n->req()) { 2575 stack.set_index(i+1); 2576 stack.push(n->in(i), 0); 2577 } else { 2578 assert(old_new[n->_idx] == nullptr || n == sfpt || old_new[n->_idx]->_idx < new_counter, "no clone yet"); 2579 Node* m = n == sfpt ? new_sfpt : n->clone(); 2580 if (m != nullptr) { 2581 for (uint i = 0; i < n->req(); i++) { 2582 if (m->in(i) != nullptr && old_new[m->in(i)->_idx] != nullptr) { 2583 m->set_req(i, old_new[m->in(i)->_idx]); 2584 } 2585 } 2586 } else { 2587 assert(n == sfpt && mode != CloneIncludesStripMined, "where's the safepoint clone?"); 2588 } 2589 if (n != sfpt) { 2590 extra_data_nodes.push(n); 2591 _igvn.register_new_node_with_optimizer(m); 2592 assert(get_ctrl(n) == cle_out, "what other control?"); 2593 set_ctrl(m, new_cle_out); 2594 old_new.map(n->_idx, m); 2595 } 2596 stack.pop(); 2597 } 2598 } 2599 if (mode == CloneIncludesStripMined) { 2600 _igvn.register_new_node_with_optimizer(new_sfpt); 2601 _igvn.register_new_node_with_optimizer(new_cle_out); 2602 } 2603 // Some other transformation may have pessimistically assigned some 2604 // data nodes to the outer loop. Set their control so they are out 2605 // of the outer loop. 2606 ResourceMark rm; 2607 Unique_Node_List wq; 2608 for (uint i = 0; i < extra_data_nodes.size(); i++) { 2609 Node* old = extra_data_nodes.at(i); 2610 collect_nodes_in_outer_loop_not_reachable_from_sfpt(old, loop, outer_loop, old_new, wq, this, true); 2611 } 2612 2613 for (uint i = 0; i < loop->_body.size(); i++) { 2614 Node* old = loop->_body.at(i); 2615 collect_nodes_in_outer_loop_not_reachable_from_sfpt(old, loop, outer_loop, old_new, wq, this, true); 2616 } 2617 2618 Node* inner_out = sfpt->in(0); 2619 if (inner_out->outcnt() > 1) { 2620 collect_nodes_in_outer_loop_not_reachable_from_sfpt(inner_out, loop, outer_loop, old_new, wq, this, true); 2621 } 2622 2623 Node* new_ctrl = cl->outer_loop_exit(); 2624 assert(get_loop(new_ctrl) != outer_loop, "must be out of the loop nest"); 2625 for (uint i = 0; i < wq.size(); i++) { 2626 Node* n = wq.at(i); 2627 set_ctrl(n, new_ctrl); 2628 if (n->in(0) != nullptr) { 2629 _igvn.replace_input_of(n, 0, new_ctrl); 2630 } 2631 collect_nodes_in_outer_loop_not_reachable_from_sfpt(n, loop, outer_loop, old_new, wq, this, false); 2632 } 2633 } else { 2634 Node *newhead = old_new[loop->_head->_idx]; 2635 set_idom(newhead, newhead->in(LoopNode::EntryControl), dd); 2636 } 2637 } 2638 2639 //------------------------------clone_loop------------------------------------- 2640 // 2641 // C L O N E A L O O P B O D Y 2642 // 2643 // This is the basic building block of the loop optimizations. It clones an 2644 // entire loop body. It makes an old_new loop body mapping; with this mapping 2645 // you can find the new-loop equivalent to an old-loop node. All new-loop 2646 // nodes are exactly equal to their old-loop counterparts, all edges are the 2647 // same. All exits from the old-loop now have a RegionNode that merges the 2648 // equivalent new-loop path. This is true even for the normal "loop-exit" 2649 // condition. All uses of loop-invariant old-loop values now come from (one 2650 // or more) Phis that merge their new-loop equivalents. 2651 // 2652 // This operation leaves the graph in an illegal state: there are two valid 2653 // control edges coming from the loop pre-header to both loop bodies. I'll 2654 // definitely have to hack the graph after running this transform. 2655 // 2656 // From this building block I will further edit edges to perform loop peeling 2657 // or loop unrolling or iteration splitting (Range-Check-Elimination), etc. 2658 // 2659 // Parameter side_by_size_idom: 2660 // When side_by_size_idom is null, the dominator tree is constructed for 2661 // the clone loop to dominate the original. Used in construction of 2662 // pre-main-post loop sequence. 2663 // When nonnull, the clone and original are side-by-side, both are 2664 // dominated by the side_by_side_idom node. Used in construction of 2665 // unswitched loops. 2666 void PhaseIdealLoop::clone_loop( IdealLoopTree *loop, Node_List &old_new, int dd, 2667 CloneLoopMode mode, Node* side_by_side_idom) { 2668 2669 LoopNode* head = loop->_head->as_Loop(); 2670 head->verify_strip_mined(1); 2671 2672 if (C->do_vector_loop() && PrintOpto) { 2673 const char* mname = C->method()->name()->as_quoted_ascii(); 2674 if (mname != nullptr) { 2675 tty->print("PhaseIdealLoop::clone_loop: for vectorize method %s\n", mname); 2676 } 2677 } 2678 2679 CloneMap& cm = C->clone_map(); 2680 if (C->do_vector_loop()) { 2681 cm.set_clone_idx(cm.max_gen()+1); 2682 #ifndef PRODUCT 2683 if (PrintOpto) { 2684 tty->print_cr("PhaseIdealLoop::clone_loop: _clone_idx %d", cm.clone_idx()); 2685 loop->dump_head(); 2686 } 2687 #endif 2688 } 2689 2690 // Step 1: Clone the loop body. Make the old->new mapping. 2691 clone_loop_body(loop->_body, old_new, &cm); 2692 2693 IdealLoopTree* outer_loop = (head->is_strip_mined() && mode != IgnoreStripMined) ? get_loop(head->as_CountedLoop()->outer_loop()) : loop; 2694 2695 // Step 2: Fix the edges in the new body. If the old input is outside the 2696 // loop use it. If the old input is INside the loop, use the corresponding 2697 // new node instead. 2698 fix_body_edges(loop->_body, loop, old_new, dd, outer_loop->_parent, false); 2699 2700 Node_List extra_data_nodes; // data nodes in the outer strip mined loop 2701 clone_outer_loop(head, mode, loop, outer_loop, dd, old_new, extra_data_nodes); 2702 2703 // Step 3: Now fix control uses. Loop varying control uses have already 2704 // been fixed up (as part of all input edges in Step 2). Loop invariant 2705 // control uses must be either an IfFalse or an IfTrue. Make a merge 2706 // point to merge the old and new IfFalse/IfTrue nodes; make the use 2707 // refer to this. 2708 Node_List worklist; 2709 uint new_counter = C->unique(); 2710 fix_ctrl_uses(loop->_body, loop, old_new, mode, side_by_side_idom, &cm, worklist); 2711 2712 // Step 4: If loop-invariant use is not control, it must be dominated by a 2713 // loop exit IfFalse/IfTrue. Find "proper" loop exit. Make a Region 2714 // there if needed. Make a Phi there merging old and new used values. 2715 Node_List *split_if_set = nullptr; 2716 Node_List *split_bool_set = nullptr; 2717 Node_List *split_cex_set = nullptr; 2718 fix_data_uses(loop->_body, loop, mode, outer_loop, new_counter, old_new, worklist, split_if_set, split_bool_set, split_cex_set); 2719 2720 for (uint i = 0; i < extra_data_nodes.size(); i++) { 2721 Node* old = extra_data_nodes.at(i); 2722 clone_loop_handle_data_uses(old, old_new, loop, outer_loop, split_if_set, 2723 split_bool_set, split_cex_set, worklist, new_counter, 2724 mode); 2725 } 2726 2727 // Check for IFs that need splitting/cloning. Happens if an IF outside of 2728 // the loop uses a condition set in the loop. The original IF probably 2729 // takes control from one or more OLD Regions (which in turn get from NEW 2730 // Regions). In any case, there will be a set of Phis for each merge point 2731 // from the IF up to where the original BOOL def exists the loop. 2732 finish_clone_loop(split_if_set, split_bool_set, split_cex_set); 2733 2734 } 2735 2736 void PhaseIdealLoop::finish_clone_loop(Node_List* split_if_set, Node_List* split_bool_set, Node_List* split_cex_set) { 2737 if (split_if_set) { 2738 while (split_if_set->size()) { 2739 Node *iff = split_if_set->pop(); 2740 uint input = iff->Opcode() == Op_AllocateArray ? AllocateNode::ValidLengthTest : 1; 2741 if (iff->in(input)->is_Phi()) { 2742 Node *b = clone_iff(iff->in(input)->as_Phi()); 2743 _igvn.replace_input_of(iff, input, b); 2744 } 2745 } 2746 } 2747 if (split_bool_set) { 2748 while (split_bool_set->size()) { 2749 Node *b = split_bool_set->pop(); 2750 Node *phi = b->in(1); 2751 assert(phi->is_Phi(), ""); 2752 CmpNode *cmp = clone_bool((PhiNode*) phi); 2753 _igvn.replace_input_of(b, 1, cmp); 2754 } 2755 } 2756 if (split_cex_set) { 2757 while (split_cex_set->size()) { 2758 Node *b = split_cex_set->pop(); 2759 assert(b->in(0)->is_Region(), ""); 2760 assert(b->in(1)->is_Phi(), ""); 2761 assert(b->in(0)->in(0) == b->in(1)->in(0), ""); 2762 split_up(b, b->in(0), nullptr); 2763 } 2764 } 2765 } 2766 2767 void PhaseIdealLoop::fix_data_uses(Node_List& body, IdealLoopTree* loop, CloneLoopMode mode, IdealLoopTree* outer_loop, 2768 uint new_counter, Node_List &old_new, Node_List &worklist, Node_List*& split_if_set, 2769 Node_List*& split_bool_set, Node_List*& split_cex_set) { 2770 for(uint i = 0; i < body.size(); i++ ) { 2771 Node* old = body.at(i); 2772 clone_loop_handle_data_uses(old, old_new, loop, outer_loop, split_if_set, 2773 split_bool_set, split_cex_set, worklist, new_counter, 2774 mode); 2775 } 2776 } 2777 2778 void PhaseIdealLoop::fix_ctrl_uses(const Node_List& body, const IdealLoopTree* loop, Node_List &old_new, CloneLoopMode mode, 2779 Node* side_by_side_idom, CloneMap* cm, Node_List &worklist) { 2780 LoopNode* head = loop->_head->as_Loop(); 2781 for(uint i = 0; i < body.size(); i++ ) { 2782 Node* old = body.at(i); 2783 if( !old->is_CFG() ) continue; 2784 2785 // Copy uses to a worklist, so I can munge the def-use info 2786 // with impunity. 2787 for (DUIterator_Fast jmax, j = old->fast_outs(jmax); j < jmax; j++) { 2788 worklist.push(old->fast_out(j)); 2789 } 2790 2791 while (worklist.size()) { // Visit all uses 2792 Node *use = worklist.pop(); 2793 if (!has_node(use)) continue; // Ignore dead nodes 2794 IdealLoopTree *use_loop = get_loop(has_ctrl(use) ? get_ctrl(use) : use ); 2795 if (!loop->is_member(use_loop) && use->is_CFG()) { 2796 // Both OLD and USE are CFG nodes here. 2797 assert(use->is_Proj(), "" ); 2798 Node* nnn = old_new[old->_idx]; 2799 2800 Node* newuse = nullptr; 2801 if (head->is_strip_mined() && mode != IgnoreStripMined) { 2802 CountedLoopNode* cl = head->as_CountedLoop(); 2803 CountedLoopEndNode* cle = cl->loopexit(); 2804 Node* cle_out = cle->proj_out_or_null(false); 2805 if (use == cle_out) { 2806 IfNode* le = cl->outer_loop_end(); 2807 use = le->proj_out(false); 2808 use_loop = get_loop(use); 2809 if (mode == CloneIncludesStripMined) { 2810 nnn = old_new[le->_idx]; 2811 } else { 2812 newuse = old_new[cle_out->_idx]; 2813 } 2814 } 2815 } 2816 if (newuse == nullptr) { 2817 newuse = use->clone(); 2818 } 2819 2820 // Clone the loop exit control projection 2821 if (C->do_vector_loop() && cm != nullptr) { 2822 cm->verify_insert_and_clone(use, newuse, cm->clone_idx()); 2823 } 2824 newuse->set_req(0,nnn); 2825 _igvn.register_new_node_with_optimizer(newuse); 2826 set_loop(newuse, use_loop); 2827 set_idom(newuse, nnn, dom_depth(nnn) + 1 ); 2828 2829 // We need a Region to merge the exit from the peeled body and the 2830 // exit from the old loop body. 2831 RegionNode *r = new RegionNode(3); 2832 uint dd_r = MIN2(dom_depth(newuse), dom_depth(use)); 2833 assert(dd_r >= dom_depth(dom_lca(newuse, use)), "" ); 2834 2835 // The original user of 'use' uses 'r' instead. 2836 for (DUIterator_Last lmin, l = use->last_outs(lmin); l >= lmin;) { 2837 Node* useuse = use->last_out(l); 2838 _igvn.rehash_node_delayed(useuse); 2839 uint uses_found = 0; 2840 if (useuse->in(0) == use) { 2841 useuse->set_req(0, r); 2842 uses_found++; 2843 if (useuse->is_CFG()) { 2844 // This is not a dom_depth > dd_r because when new 2845 // control flow is constructed by a loop opt, a node and 2846 // its dominator can end up at the same dom_depth 2847 assert(dom_depth(useuse) >= dd_r, ""); 2848 set_idom(useuse, r, dom_depth(useuse)); 2849 } 2850 } 2851 for (uint k = 1; k < useuse->req(); k++) { 2852 if( useuse->in(k) == use ) { 2853 useuse->set_req(k, r); 2854 uses_found++; 2855 if (useuse->is_Loop() && k == LoopNode::EntryControl) { 2856 // This is not a dom_depth > dd_r because when new 2857 // control flow is constructed by a loop opt, a node 2858 // and its dominator can end up at the same dom_depth 2859 assert(dom_depth(useuse) >= dd_r , ""); 2860 set_idom(useuse, r, dom_depth(useuse)); 2861 } 2862 } 2863 } 2864 l -= uses_found; // we deleted 1 or more copies of this edge 2865 } 2866 2867 assert(use->is_Proj(), "loop exit should be projection"); 2868 // lazy_replace() below moves all nodes that are: 2869 // - control dependent on the loop exit or 2870 // - have control set to the loop exit 2871 // below the post-loop merge point. lazy_replace() takes a dead control as first input. To make it 2872 // possible to use it, the loop exit projection is cloned and becomes the new exit projection. The initial one 2873 // becomes dead and is "replaced" by the region. 2874 Node* use_clone = use->clone(); 2875 register_control(use_clone, use_loop, idom(use), dom_depth(use)); 2876 // Now finish up 'r' 2877 r->set_req(1, newuse); 2878 r->set_req(2, use_clone); 2879 _igvn.register_new_node_with_optimizer(r); 2880 set_loop(r, use_loop); 2881 set_idom(r, (side_by_side_idom == nullptr) ? newuse->in(0) : side_by_side_idom, dd_r); 2882 lazy_replace(use, r); 2883 // Map the (cloned) old use to the new merge point 2884 old_new.map(use_clone->_idx, r); 2885 } // End of if a loop-exit test 2886 } 2887 } 2888 } 2889 2890 void PhaseIdealLoop::fix_body_edges(const Node_List &body, IdealLoopTree* loop, const Node_List &old_new, int dd, 2891 IdealLoopTree* parent, bool partial) { 2892 for(uint i = 0; i < body.size(); i++ ) { 2893 Node *old = body.at(i); 2894 Node *nnn = old_new[old->_idx]; 2895 // Fix CFG/Loop controlling the new node 2896 if (has_ctrl(old)) { 2897 set_ctrl(nnn, old_new[get_ctrl(old)->_idx]); 2898 } else { 2899 set_loop(nnn, parent); 2900 if (old->outcnt() > 0) { 2901 Node* dom = idom(old); 2902 if (old_new[dom->_idx] != nullptr) { 2903 dom = old_new[dom->_idx]; 2904 set_idom(nnn, dom, dd ); 2905 } 2906 } 2907 } 2908 // Correct edges to the new node 2909 for (uint j = 0; j < nnn->req(); j++) { 2910 Node *n = nnn->in(j); 2911 if (n != nullptr) { 2912 IdealLoopTree *old_in_loop = get_loop(has_ctrl(n) ? get_ctrl(n) : n); 2913 if (loop->is_member(old_in_loop)) { 2914 if (old_new[n->_idx] != nullptr) { 2915 nnn->set_req(j, old_new[n->_idx]); 2916 } else { 2917 assert(!body.contains(n), ""); 2918 assert(partial, "node not cloned"); 2919 } 2920 } 2921 } 2922 } 2923 _igvn.hash_find_insert(nnn); 2924 } 2925 } 2926 2927 void PhaseIdealLoop::clone_loop_body(const Node_List& body, Node_List &old_new, CloneMap* cm) { 2928 for (uint i = 0; i < body.size(); i++) { 2929 Node* old = body.at(i); 2930 Node* nnn = old->clone(); 2931 old_new.map(old->_idx, nnn); 2932 if (C->do_vector_loop() && cm != nullptr) { 2933 cm->verify_insert_and_clone(old, nnn, cm->clone_idx()); 2934 } 2935 _igvn.register_new_node_with_optimizer(nnn); 2936 } 2937 } 2938 2939 2940 //---------------------- stride_of_possible_iv ------------------------------------- 2941 // Looks for an iff/bool/comp with one operand of the compare 2942 // being a cycle involving an add and a phi, 2943 // with an optional truncation (left-shift followed by a right-shift) 2944 // of the add. Returns zero if not an iv. 2945 int PhaseIdealLoop::stride_of_possible_iv(Node* iff) { 2946 Node* trunc1 = nullptr; 2947 Node* trunc2 = nullptr; 2948 const TypeInteger* ttype = nullptr; 2949 if (!iff->is_If() || iff->in(1) == nullptr || !iff->in(1)->is_Bool()) { 2950 return 0; 2951 } 2952 BoolNode* bl = iff->in(1)->as_Bool(); 2953 Node* cmp = bl->in(1); 2954 if (!cmp || (cmp->Opcode() != Op_CmpI && cmp->Opcode() != Op_CmpU)) { 2955 return 0; 2956 } 2957 // Must have an invariant operand 2958 if (is_member(get_loop(iff), get_ctrl(cmp->in(2)))) { 2959 return 0; 2960 } 2961 Node* add2 = nullptr; 2962 Node* cmp1 = cmp->in(1); 2963 if (cmp1->is_Phi()) { 2964 // (If (Bool (CmpX phi:(Phi ...(Optional-trunc(AddI phi add2))) ))) 2965 Node* phi = cmp1; 2966 for (uint i = 1; i < phi->req(); i++) { 2967 Node* in = phi->in(i); 2968 Node* add = CountedLoopNode::match_incr_with_optional_truncation(in, 2969 &trunc1, &trunc2, &ttype, T_INT); 2970 if (add && add->in(1) == phi) { 2971 add2 = add->in(2); 2972 break; 2973 } 2974 } 2975 } else { 2976 // (If (Bool (CmpX addtrunc:(Optional-trunc((AddI (Phi ...addtrunc...) add2)) ))) 2977 Node* addtrunc = cmp1; 2978 Node* add = CountedLoopNode::match_incr_with_optional_truncation(addtrunc, 2979 &trunc1, &trunc2, &ttype, T_INT); 2980 if (add && add->in(1)->is_Phi()) { 2981 Node* phi = add->in(1); 2982 for (uint i = 1; i < phi->req(); i++) { 2983 if (phi->in(i) == addtrunc) { 2984 add2 = add->in(2); 2985 break; 2986 } 2987 } 2988 } 2989 } 2990 if (add2 != nullptr) { 2991 const TypeInt* add2t = _igvn.type(add2)->is_int(); 2992 if (add2t->is_con()) { 2993 return add2t->get_con(); 2994 } 2995 } 2996 return 0; 2997 } 2998 2999 3000 //---------------------- stay_in_loop ------------------------------------- 3001 // Return the (unique) control output node that's in the loop (if it exists.) 3002 Node* PhaseIdealLoop::stay_in_loop( Node* n, IdealLoopTree *loop) { 3003 Node* unique = nullptr; 3004 if (!n) return nullptr; 3005 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 3006 Node* use = n->fast_out(i); 3007 if (!has_ctrl(use) && loop->is_member(get_loop(use))) { 3008 if (unique != nullptr) { 3009 return nullptr; 3010 } 3011 unique = use; 3012 } 3013 } 3014 return unique; 3015 } 3016 3017 //------------------------------ register_node ------------------------------------- 3018 // Utility to register node "n" with PhaseIdealLoop 3019 void PhaseIdealLoop::register_node(Node* n, IdealLoopTree* loop, Node* pred, uint ddepth) { 3020 _igvn.register_new_node_with_optimizer(n); 3021 loop->_body.push(n); 3022 if (n->is_CFG()) { 3023 set_loop(n, loop); 3024 set_idom(n, pred, ddepth); 3025 } else { 3026 set_ctrl(n, pred); 3027 } 3028 } 3029 3030 //------------------------------ proj_clone ------------------------------------- 3031 // Utility to create an if-projection 3032 ProjNode* PhaseIdealLoop::proj_clone(ProjNode* p, IfNode* iff) { 3033 ProjNode* c = p->clone()->as_Proj(); 3034 c->set_req(0, iff); 3035 return c; 3036 } 3037 3038 //------------------------------ short_circuit_if ------------------------------------- 3039 // Force the iff control output to be the live_proj 3040 Node* PhaseIdealLoop::short_circuit_if(IfNode* iff, ProjNode* live_proj) { 3041 guarantee(live_proj != nullptr, "null projection"); 3042 int proj_con = live_proj->_con; 3043 assert(proj_con == 0 || proj_con == 1, "false or true projection"); 3044 Node *con = _igvn.intcon(proj_con); 3045 set_ctrl(con, C->root()); 3046 if (iff) { 3047 iff->set_req(1, con); 3048 } 3049 return con; 3050 } 3051 3052 //------------------------------ insert_if_before_proj ------------------------------------- 3053 // Insert a new if before an if projection (* - new node) 3054 // 3055 // before 3056 // if(test) 3057 // / \ 3058 // v v 3059 // other-proj proj (arg) 3060 // 3061 // after 3062 // if(test) 3063 // / \ 3064 // / v 3065 // | * proj-clone 3066 // v | 3067 // other-proj v 3068 // * new_if(relop(cmp[IU](left,right))) 3069 // / \ 3070 // v v 3071 // * new-proj proj 3072 // (returned) 3073 // 3074 ProjNode* PhaseIdealLoop::insert_if_before_proj(Node* left, bool Signed, BoolTest::mask relop, Node* right, ProjNode* proj) { 3075 IfNode* iff = proj->in(0)->as_If(); 3076 IdealLoopTree *loop = get_loop(proj); 3077 ProjNode *other_proj = iff->proj_out(!proj->is_IfTrue())->as_Proj(); 3078 uint ddepth = dom_depth(proj); 3079 3080 _igvn.rehash_node_delayed(iff); 3081 _igvn.rehash_node_delayed(proj); 3082 3083 proj->set_req(0, nullptr); // temporary disconnect 3084 ProjNode* proj2 = proj_clone(proj, iff); 3085 register_node(proj2, loop, iff, ddepth); 3086 3087 Node* cmp = Signed ? (Node*) new CmpINode(left, right) : (Node*) new CmpUNode(left, right); 3088 register_node(cmp, loop, proj2, ddepth); 3089 3090 BoolNode* bol = new BoolNode(cmp, relop); 3091 register_node(bol, loop, proj2, ddepth); 3092 3093 int opcode = iff->Opcode(); 3094 assert(opcode == Op_If || opcode == Op_RangeCheck, "unexpected opcode"); 3095 IfNode* new_if = IfNode::make_with_same_profile(iff, proj2, bol); 3096 register_node(new_if, loop, proj2, ddepth); 3097 3098 proj->set_req(0, new_if); // reattach 3099 set_idom(proj, new_if, ddepth); 3100 3101 ProjNode* new_exit = proj_clone(other_proj, new_if)->as_Proj(); 3102 guarantee(new_exit != nullptr, "null exit node"); 3103 register_node(new_exit, get_loop(other_proj), new_if, ddepth); 3104 3105 return new_exit; 3106 } 3107 3108 //------------------------------ insert_region_before_proj ------------------------------------- 3109 // Insert a region before an if projection (* - new node) 3110 // 3111 // before 3112 // if(test) 3113 // / | 3114 // v | 3115 // proj v 3116 // other-proj 3117 // 3118 // after 3119 // if(test) 3120 // / | 3121 // v | 3122 // * proj-clone v 3123 // | other-proj 3124 // v 3125 // * new-region 3126 // | 3127 // v 3128 // * dum_if 3129 // / \ 3130 // v \ 3131 // * dum-proj v 3132 // proj 3133 // 3134 RegionNode* PhaseIdealLoop::insert_region_before_proj(ProjNode* proj) { 3135 IfNode* iff = proj->in(0)->as_If(); 3136 IdealLoopTree *loop = get_loop(proj); 3137 ProjNode *other_proj = iff->proj_out(!proj->is_IfTrue())->as_Proj(); 3138 uint ddepth = dom_depth(proj); 3139 3140 _igvn.rehash_node_delayed(iff); 3141 _igvn.rehash_node_delayed(proj); 3142 3143 proj->set_req(0, nullptr); // temporary disconnect 3144 ProjNode* proj2 = proj_clone(proj, iff); 3145 register_node(proj2, loop, iff, ddepth); 3146 3147 RegionNode* reg = new RegionNode(2); 3148 reg->set_req(1, proj2); 3149 register_node(reg, loop, iff, ddepth); 3150 3151 IfNode* dum_if = new IfNode(reg, short_circuit_if(nullptr, proj), iff->_prob, iff->_fcnt); 3152 register_node(dum_if, loop, reg, ddepth); 3153 3154 proj->set_req(0, dum_if); // reattach 3155 set_idom(proj, dum_if, ddepth); 3156 3157 ProjNode* dum_proj = proj_clone(other_proj, dum_if); 3158 register_node(dum_proj, loop, dum_if, ddepth); 3159 3160 return reg; 3161 } 3162 3163 //------------------------------ insert_cmpi_loop_exit ------------------------------------- 3164 // Clone a signed compare loop exit from an unsigned compare and 3165 // insert it before the unsigned cmp on the stay-in-loop path. 3166 // All new nodes inserted in the dominator tree between the original 3167 // if and it's projections. The original if test is replaced with 3168 // a constant to force the stay-in-loop path. 3169 // 3170 // This is done to make sure that the original if and it's projections 3171 // still dominate the same set of control nodes, that the ctrl() relation 3172 // from data nodes to them is preserved, and that their loop nesting is 3173 // preserved. 3174 // 3175 // before 3176 // if(i <u limit) unsigned compare loop exit 3177 // / | 3178 // v v 3179 // exit-proj stay-in-loop-proj 3180 // 3181 // after 3182 // if(stay-in-loop-const) original if 3183 // / | 3184 // / v 3185 // / if(i < limit) new signed test 3186 // / / | 3187 // / / v 3188 // / / if(i <u limit) new cloned unsigned test 3189 // / / / | 3190 // v v v | 3191 // region | 3192 // | | 3193 // dum-if | 3194 // / | | 3195 // ether | | 3196 // v v 3197 // exit-proj stay-in-loop-proj 3198 // 3199 IfNode* PhaseIdealLoop::insert_cmpi_loop_exit(IfNode* if_cmpu, IdealLoopTree *loop) { 3200 const bool Signed = true; 3201 const bool Unsigned = false; 3202 3203 BoolNode* bol = if_cmpu->in(1)->as_Bool(); 3204 if (bol->_test._test != BoolTest::lt) return nullptr; 3205 CmpNode* cmpu = bol->in(1)->as_Cmp(); 3206 if (cmpu->Opcode() != Op_CmpU) return nullptr; 3207 int stride = stride_of_possible_iv(if_cmpu); 3208 if (stride == 0) return nullptr; 3209 3210 Node* lp_proj = stay_in_loop(if_cmpu, loop); 3211 guarantee(lp_proj != nullptr, "null loop node"); 3212 3213 ProjNode* lp_continue = lp_proj->as_Proj(); 3214 ProjNode* lp_exit = if_cmpu->proj_out(!lp_continue->is_IfTrue())->as_Proj(); 3215 if (!lp_exit->is_IfFalse()) { 3216 // The loop exit condition is (i <u limit) ==> (i >= 0 && i < limit). 3217 // We therefore can't add a single exit condition. 3218 return nullptr; 3219 } 3220 // The loop exit condition is !(i <u limit) ==> (i < 0 || i >= limit). 3221 // Split out the exit condition (i < 0) for stride < 0 or (i >= limit) for stride > 0. 3222 Node* limit = nullptr; 3223 if (stride > 0) { 3224 limit = cmpu->in(2); 3225 } else { 3226 limit = _igvn.makecon(TypeInt::ZERO); 3227 set_ctrl(limit, C->root()); 3228 } 3229 // Create a new region on the exit path 3230 RegionNode* reg = insert_region_before_proj(lp_exit); 3231 guarantee(reg != nullptr, "null region node"); 3232 3233 // Clone the if-cmpu-true-false using a signed compare 3234 BoolTest::mask rel_i = stride > 0 ? bol->_test._test : BoolTest::ge; 3235 ProjNode* cmpi_exit = insert_if_before_proj(cmpu->in(1), Signed, rel_i, limit, lp_continue); 3236 reg->add_req(cmpi_exit); 3237 3238 // Clone the if-cmpu-true-false 3239 BoolTest::mask rel_u = bol->_test._test; 3240 ProjNode* cmpu_exit = insert_if_before_proj(cmpu->in(1), Unsigned, rel_u, cmpu->in(2), lp_continue); 3241 reg->add_req(cmpu_exit); 3242 3243 // Force original if to stay in loop. 3244 short_circuit_if(if_cmpu, lp_continue); 3245 3246 return cmpi_exit->in(0)->as_If(); 3247 } 3248 3249 //------------------------------ remove_cmpi_loop_exit ------------------------------------- 3250 // Remove a previously inserted signed compare loop exit. 3251 void PhaseIdealLoop::remove_cmpi_loop_exit(IfNode* if_cmp, IdealLoopTree *loop) { 3252 Node* lp_proj = stay_in_loop(if_cmp, loop); 3253 assert(if_cmp->in(1)->in(1)->Opcode() == Op_CmpI && 3254 stay_in_loop(lp_proj, loop)->is_If() && 3255 stay_in_loop(lp_proj, loop)->in(1)->in(1)->Opcode() == Op_CmpU, "inserted cmpi before cmpu"); 3256 Node *con = _igvn.makecon(lp_proj->is_IfTrue() ? TypeInt::ONE : TypeInt::ZERO); 3257 set_ctrl(con, C->root()); 3258 if_cmp->set_req(1, con); 3259 } 3260 3261 //------------------------------ scheduled_nodelist ------------------------------------- 3262 // Create a post order schedule of nodes that are in the 3263 // "member" set. The list is returned in "sched". 3264 // The first node in "sched" is the loop head, followed by 3265 // nodes which have no inputs in the "member" set, and then 3266 // followed by the nodes that have an immediate input dependence 3267 // on a node in "sched". 3268 void PhaseIdealLoop::scheduled_nodelist( IdealLoopTree *loop, VectorSet& member, Node_List &sched ) { 3269 3270 assert(member.test(loop->_head->_idx), "loop head must be in member set"); 3271 VectorSet visited; 3272 Node_Stack nstack(loop->_body.size()); 3273 3274 Node* n = loop->_head; // top of stack is cached in "n" 3275 uint idx = 0; 3276 visited.set(n->_idx); 3277 3278 // Initially push all with no inputs from within member set 3279 for(uint i = 0; i < loop->_body.size(); i++ ) { 3280 Node *elt = loop->_body.at(i); 3281 if (member.test(elt->_idx)) { 3282 bool found = false; 3283 for (uint j = 0; j < elt->req(); j++) { 3284 Node* def = elt->in(j); 3285 if (def && member.test(def->_idx) && def != elt) { 3286 found = true; 3287 break; 3288 } 3289 } 3290 if (!found && elt != loop->_head) { 3291 nstack.push(n, idx); 3292 n = elt; 3293 assert(!visited.test(n->_idx), "not seen yet"); 3294 visited.set(n->_idx); 3295 } 3296 } 3297 } 3298 3299 // traverse out's that are in the member set 3300 while (true) { 3301 if (idx < n->outcnt()) { 3302 Node* use = n->raw_out(idx); 3303 idx++; 3304 if (!visited.test_set(use->_idx)) { 3305 if (member.test(use->_idx)) { 3306 nstack.push(n, idx); 3307 n = use; 3308 idx = 0; 3309 } 3310 } 3311 } else { 3312 // All outputs processed 3313 sched.push(n); 3314 if (nstack.is_empty()) break; 3315 n = nstack.node(); 3316 idx = nstack.index(); 3317 nstack.pop(); 3318 } 3319 } 3320 } 3321 3322 3323 //------------------------------ has_use_in_set ------------------------------------- 3324 // Has a use in the vector set 3325 bool PhaseIdealLoop::has_use_in_set( Node* n, VectorSet& vset ) { 3326 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) { 3327 Node* use = n->fast_out(j); 3328 if (vset.test(use->_idx)) { 3329 return true; 3330 } 3331 } 3332 return false; 3333 } 3334 3335 3336 //------------------------------ has_use_internal_to_set ------------------------------------- 3337 // Has use internal to the vector set (ie. not in a phi at the loop head) 3338 bool PhaseIdealLoop::has_use_internal_to_set( Node* n, VectorSet& vset, IdealLoopTree *loop ) { 3339 Node* head = loop->_head; 3340 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) { 3341 Node* use = n->fast_out(j); 3342 if (vset.test(use->_idx) && !(use->is_Phi() && use->in(0) == head)) { 3343 return true; 3344 } 3345 } 3346 return false; 3347 } 3348 3349 3350 //------------------------------ clone_for_use_outside_loop ------------------------------------- 3351 // clone "n" for uses that are outside of loop 3352 int PhaseIdealLoop::clone_for_use_outside_loop( IdealLoopTree *loop, Node* n, Node_List& worklist ) { 3353 int cloned = 0; 3354 assert(worklist.size() == 0, "should be empty"); 3355 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) { 3356 Node* use = n->fast_out(j); 3357 if( !loop->is_member(get_loop(has_ctrl(use) ? get_ctrl(use) : use)) ) { 3358 worklist.push(use); 3359 } 3360 } 3361 3362 if (C->check_node_count(worklist.size() + NodeLimitFudgeFactor, 3363 "Too many clones required in clone_for_use_outside_loop in partial peeling")) { 3364 return -1; 3365 } 3366 3367 while( worklist.size() ) { 3368 Node *use = worklist.pop(); 3369 if (!has_node(use) || use->in(0) == C->top()) continue; 3370 uint j; 3371 for (j = 0; j < use->req(); j++) { 3372 if (use->in(j) == n) break; 3373 } 3374 assert(j < use->req(), "must be there"); 3375 3376 // clone "n" and insert it between the inputs of "n" and the use outside the loop 3377 Node* n_clone = n->clone(); 3378 _igvn.replace_input_of(use, j, n_clone); 3379 cloned++; 3380 Node* use_c; 3381 if (!use->is_Phi()) { 3382 use_c = has_ctrl(use) ? get_ctrl(use) : use->in(0); 3383 } else { 3384 // Use in a phi is considered a use in the associated predecessor block 3385 use_c = use->in(0)->in(j); 3386 } 3387 set_ctrl(n_clone, use_c); 3388 assert(!loop->is_member(get_loop(use_c)), "should be outside loop"); 3389 get_loop(use_c)->_body.push(n_clone); 3390 _igvn.register_new_node_with_optimizer(n_clone); 3391 #ifndef PRODUCT 3392 if (TracePartialPeeling) { 3393 tty->print_cr("loop exit cloning old: %d new: %d newbb: %d", n->_idx, n_clone->_idx, get_ctrl(n_clone)->_idx); 3394 } 3395 #endif 3396 } 3397 return cloned; 3398 } 3399 3400 3401 //------------------------------ clone_for_special_use_inside_loop ------------------------------------- 3402 // clone "n" for special uses that are in the not_peeled region. 3403 // If these def-uses occur in separate blocks, the code generator 3404 // marks the method as not compilable. For example, if a "BoolNode" 3405 // is in a different basic block than the "IfNode" that uses it, then 3406 // the compilation is aborted in the code generator. 3407 void PhaseIdealLoop::clone_for_special_use_inside_loop( IdealLoopTree *loop, Node* n, 3408 VectorSet& not_peel, Node_List& sink_list, Node_List& worklist ) { 3409 if (n->is_Phi() || n->is_Load()) { 3410 return; 3411 } 3412 assert(worklist.size() == 0, "should be empty"); 3413 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) { 3414 Node* use = n->fast_out(j); 3415 if ( not_peel.test(use->_idx) && 3416 (use->is_If() || use->is_CMove() || use->is_Bool()) && 3417 use->in(1) == n) { 3418 worklist.push(use); 3419 } 3420 } 3421 if (worklist.size() > 0) { 3422 // clone "n" and insert it between inputs of "n" and the use 3423 Node* n_clone = n->clone(); 3424 loop->_body.push(n_clone); 3425 _igvn.register_new_node_with_optimizer(n_clone); 3426 set_ctrl(n_clone, get_ctrl(n)); 3427 sink_list.push(n_clone); 3428 not_peel.set(n_clone->_idx); 3429 #ifndef PRODUCT 3430 if (TracePartialPeeling) { 3431 tty->print_cr("special not_peeled cloning old: %d new: %d", n->_idx, n_clone->_idx); 3432 } 3433 #endif 3434 while( worklist.size() ) { 3435 Node *use = worklist.pop(); 3436 _igvn.rehash_node_delayed(use); 3437 for (uint j = 1; j < use->req(); j++) { 3438 if (use->in(j) == n) { 3439 use->set_req(j, n_clone); 3440 } 3441 } 3442 } 3443 } 3444 } 3445 3446 3447 //------------------------------ insert_phi_for_loop ------------------------------------- 3448 // Insert phi(lp_entry_val, back_edge_val) at use->in(idx) for loop lp if phi does not already exist 3449 void PhaseIdealLoop::insert_phi_for_loop( Node* use, uint idx, Node* lp_entry_val, Node* back_edge_val, LoopNode* lp ) { 3450 Node *phi = PhiNode::make(lp, back_edge_val); 3451 phi->set_req(LoopNode::EntryControl, lp_entry_val); 3452 // Use existing phi if it already exists 3453 Node *hit = _igvn.hash_find_insert(phi); 3454 if( hit == nullptr ) { 3455 _igvn.register_new_node_with_optimizer(phi); 3456 set_ctrl(phi, lp); 3457 } else { 3458 // Remove the new phi from the graph and use the hit 3459 _igvn.remove_dead_node(phi); 3460 phi = hit; 3461 } 3462 _igvn.replace_input_of(use, idx, phi); 3463 } 3464 3465 #ifdef ASSERT 3466 //------------------------------ is_valid_loop_partition ------------------------------------- 3467 // Validate the loop partition sets: peel and not_peel 3468 bool PhaseIdealLoop::is_valid_loop_partition( IdealLoopTree *loop, VectorSet& peel, Node_List& peel_list, 3469 VectorSet& not_peel ) { 3470 uint i; 3471 // Check that peel_list entries are in the peel set 3472 for (i = 0; i < peel_list.size(); i++) { 3473 if (!peel.test(peel_list.at(i)->_idx)) { 3474 return false; 3475 } 3476 } 3477 // Check at loop members are in one of peel set or not_peel set 3478 for (i = 0; i < loop->_body.size(); i++ ) { 3479 Node *def = loop->_body.at(i); 3480 uint di = def->_idx; 3481 // Check that peel set elements are in peel_list 3482 if (peel.test(di)) { 3483 if (not_peel.test(di)) { 3484 return false; 3485 } 3486 // Must be in peel_list also 3487 bool found = false; 3488 for (uint j = 0; j < peel_list.size(); j++) { 3489 if (peel_list.at(j)->_idx == di) { 3490 found = true; 3491 break; 3492 } 3493 } 3494 if (!found) { 3495 return false; 3496 } 3497 } else if (not_peel.test(di)) { 3498 if (peel.test(di)) { 3499 return false; 3500 } 3501 } else { 3502 return false; 3503 } 3504 } 3505 return true; 3506 } 3507 3508 //------------------------------ is_valid_clone_loop_exit_use ------------------------------------- 3509 // Ensure a use outside of loop is of the right form 3510 bool PhaseIdealLoop::is_valid_clone_loop_exit_use( IdealLoopTree *loop, Node* use, uint exit_idx) { 3511 Node *use_c = has_ctrl(use) ? get_ctrl(use) : use; 3512 return (use->is_Phi() && 3513 use_c->is_Region() && use_c->req() == 3 && 3514 (use_c->in(exit_idx)->Opcode() == Op_IfTrue || 3515 use_c->in(exit_idx)->Opcode() == Op_IfFalse || 3516 use_c->in(exit_idx)->Opcode() == Op_JumpProj) && 3517 loop->is_member( get_loop( use_c->in(exit_idx)->in(0) ) ) ); 3518 } 3519 3520 //------------------------------ is_valid_clone_loop_form ------------------------------------- 3521 // Ensure that all uses outside of loop are of the right form 3522 bool PhaseIdealLoop::is_valid_clone_loop_form( IdealLoopTree *loop, Node_List& peel_list, 3523 uint orig_exit_idx, uint clone_exit_idx) { 3524 uint len = peel_list.size(); 3525 for (uint i = 0; i < len; i++) { 3526 Node *def = peel_list.at(i); 3527 3528 for (DUIterator_Fast jmax, j = def->fast_outs(jmax); j < jmax; j++) { 3529 Node *use = def->fast_out(j); 3530 Node *use_c = has_ctrl(use) ? get_ctrl(use) : use; 3531 if (!loop->is_member(get_loop(use_c))) { 3532 // use is not in the loop, check for correct structure 3533 if (use->in(0) == def) { 3534 // Okay 3535 } else if (!is_valid_clone_loop_exit_use(loop, use, orig_exit_idx)) { 3536 return false; 3537 } 3538 } 3539 } 3540 } 3541 return true; 3542 } 3543 #endif 3544 3545 //------------------------------ partial_peel ------------------------------------- 3546 // Partially peel (aka loop rotation) the top portion of a loop (called 3547 // the peel section below) by cloning it and placing one copy just before 3548 // the new loop head and the other copy at the bottom of the new loop. 3549 // 3550 // before after where it came from 3551 // 3552 // stmt1 stmt1 3553 // loop: stmt2 clone 3554 // stmt2 if condA goto exitA clone 3555 // if condA goto exitA new_loop: new 3556 // stmt3 stmt3 clone 3557 // if !condB goto loop if condB goto exitB clone 3558 // exitB: stmt2 orig 3559 // stmt4 if !condA goto new_loop orig 3560 // exitA: goto exitA 3561 // exitB: 3562 // stmt4 3563 // exitA: 3564 // 3565 // Step 1: find the cut point: an exit test on probable 3566 // induction variable. 3567 // Step 2: schedule (with cloning) operations in the peel 3568 // section that can be executed after the cut into 3569 // the section that is not peeled. This may need 3570 // to clone operations into exit blocks. For 3571 // instance, a reference to A[i] in the not-peel 3572 // section and a reference to B[i] in an exit block 3573 // may cause a left-shift of i by 2 to be placed 3574 // in the peel block. This step will clone the left 3575 // shift into the exit block and sink the left shift 3576 // from the peel to the not-peel section. 3577 // Step 3: clone the loop, retarget the control, and insert 3578 // phis for values that are live across the new loop 3579 // head. This is very dependent on the graph structure 3580 // from clone_loop. It creates region nodes for 3581 // exit control and associated phi nodes for values 3582 // flow out of the loop through that exit. The region 3583 // node is dominated by the clone's control projection. 3584 // So the clone's peel section is placed before the 3585 // new loop head, and the clone's not-peel section is 3586 // forms the top part of the new loop. The original 3587 // peel section forms the tail of the new loop. 3588 // Step 4: update the dominator tree and recompute the 3589 // dominator depth. 3590 // 3591 // orig 3592 // 3593 // stmt1 3594 // | 3595 // v 3596 // predicates 3597 // | 3598 // v 3599 // loop<----+ 3600 // | | 3601 // stmt2 | 3602 // | | 3603 // v | 3604 // ifA | 3605 // / | | 3606 // v v | 3607 // false true ^ <-- last_peel 3608 // / | | 3609 // / ===|==cut | 3610 // / stmt3 | <-- first_not_peel 3611 // / | | 3612 // | v | 3613 // v ifB | 3614 // exitA: / \ | 3615 // / \ | 3616 // v v | 3617 // false true | 3618 // / \ | 3619 // / ----+ 3620 // | 3621 // v 3622 // exitB: 3623 // stmt4 3624 // 3625 // 3626 // after clone loop 3627 // 3628 // stmt1 3629 // | 3630 // v 3631 // predicates 3632 // / \ 3633 // clone / \ orig 3634 // / \ 3635 // / \ 3636 // v v 3637 // +---->loop loop<----+ 3638 // | | | | 3639 // | stmt2 stmt2 | 3640 // | | | | 3641 // | v v | 3642 // | ifA ifA | 3643 // | | \ / | | 3644 // | v v v v | 3645 // ^ true false false true ^ <-- last_peel 3646 // | | ^ \ / | | 3647 // | cut==|== \ \ / ===|==cut | 3648 // | stmt3 \ \ / stmt3 | <-- first_not_peel 3649 // | | dom | | | | 3650 // | v \ 1v v2 v | 3651 // | ifB regionA ifB | 3652 // | / \ | / \ | 3653 // | / \ v / \ | 3654 // | v v exitA: v v | 3655 // | true false false true | 3656 // | / ^ \ / \ | 3657 // +---- \ \ / ----+ 3658 // dom \ / 3659 // \ 1v v2 3660 // regionB 3661 // | 3662 // v 3663 // exitB: 3664 // stmt4 3665 // 3666 // 3667 // after partial peel 3668 // 3669 // stmt1 3670 // | 3671 // v 3672 // predicates 3673 // / 3674 // clone / orig 3675 // / TOP 3676 // / \ 3677 // v v 3678 // TOP->loop loop----+ 3679 // | | | 3680 // stmt2 stmt2 | 3681 // | | | 3682 // v v | 3683 // ifA ifA | 3684 // | \ / | | 3685 // v v v v | 3686 // true false false true | <-- last_peel 3687 // | ^ \ / +------|---+ 3688 // +->newloop \ \ / === ==cut | | 3689 // | stmt3 \ \ / TOP | | 3690 // | | dom | | stmt3 | | <-- first_not_peel 3691 // | v \ 1v v2 v | | 3692 // | ifB regionA ifB ^ v 3693 // | / \ | / \ | | 3694 // | / \ v / \ | | 3695 // | v v exitA: v v | | 3696 // | true false false true | | 3697 // | / ^ \ / \ | | 3698 // | | \ \ / v | | 3699 // | | dom \ / TOP | | 3700 // | | \ 1v v2 | | 3701 // ^ v regionB | | 3702 // | | | | | 3703 // | | v ^ v 3704 // | | exitB: | | 3705 // | | stmt4 | | 3706 // | +------------>-----------------+ | 3707 // | | 3708 // +-----------------<---------------------+ 3709 // 3710 // 3711 // final graph 3712 // 3713 // stmt1 3714 // | 3715 // v 3716 // predicates 3717 // | 3718 // v 3719 // stmt2 clone 3720 // | 3721 // v 3722 // ........> ifA clone 3723 // : / | 3724 // dom / | 3725 // : v v 3726 // : false true 3727 // : | | 3728 // : | v 3729 // : | newloop<-----+ 3730 // : | | | 3731 // : | stmt3 clone | 3732 // : | | | 3733 // : | v | 3734 // : | ifB | 3735 // : | / \ | 3736 // : | v v | 3737 // : | false true | 3738 // : | | | | 3739 // : | v stmt2 | 3740 // : | exitB: | | 3741 // : | stmt4 v | 3742 // : | ifA orig | 3743 // : | / \ | 3744 // : | / \ | 3745 // : | v v | 3746 // : | false true | 3747 // : | / \ | 3748 // : v v -----+ 3749 // RegionA 3750 // | 3751 // v 3752 // exitA 3753 // 3754 bool PhaseIdealLoop::partial_peel( IdealLoopTree *loop, Node_List &old_new ) { 3755 3756 assert(!loop->_head->is_CountedLoop(), "Non-counted loop only"); 3757 if (!loop->_head->is_Loop()) { 3758 return false; 3759 } 3760 LoopNode *head = loop->_head->as_Loop(); 3761 3762 if (head->is_partial_peel_loop() || head->partial_peel_has_failed()) { 3763 return false; 3764 } 3765 3766 // Check for complex exit control 3767 for (uint ii = 0; ii < loop->_body.size(); ii++) { 3768 Node *n = loop->_body.at(ii); 3769 int opc = n->Opcode(); 3770 if (n->is_Call() || 3771 opc == Op_Catch || 3772 opc == Op_CatchProj || 3773 opc == Op_Jump || 3774 opc == Op_JumpProj) { 3775 #ifndef PRODUCT 3776 if (TracePartialPeeling) { 3777 tty->print_cr("\nExit control too complex: lp: %d", head->_idx); 3778 } 3779 #endif 3780 return false; 3781 } 3782 } 3783 3784 int dd = dom_depth(head); 3785 3786 // Step 1: find cut point 3787 3788 // Walk up dominators to loop head looking for first loop exit 3789 // which is executed on every path thru loop. 3790 IfNode *peel_if = nullptr; 3791 IfNode *peel_if_cmpu = nullptr; 3792 3793 Node *iff = loop->tail(); 3794 while (iff != head) { 3795 if (iff->is_If()) { 3796 Node *ctrl = get_ctrl(iff->in(1)); 3797 if (ctrl->is_top()) return false; // Dead test on live IF. 3798 // If loop-varying exit-test, check for induction variable 3799 if (loop->is_member(get_loop(ctrl)) && 3800 loop->is_loop_exit(iff) && 3801 is_possible_iv_test(iff)) { 3802 Node* cmp = iff->in(1)->in(1); 3803 if (cmp->Opcode() == Op_CmpI) { 3804 peel_if = iff->as_If(); 3805 } else { 3806 assert(cmp->Opcode() == Op_CmpU, "must be CmpI or CmpU"); 3807 peel_if_cmpu = iff->as_If(); 3808 } 3809 } 3810 } 3811 iff = idom(iff); 3812 } 3813 3814 // Prefer signed compare over unsigned compare. 3815 IfNode* new_peel_if = nullptr; 3816 if (peel_if == nullptr) { 3817 if (!PartialPeelAtUnsignedTests || peel_if_cmpu == nullptr) { 3818 return false; // No peel point found 3819 } 3820 new_peel_if = insert_cmpi_loop_exit(peel_if_cmpu, loop); 3821 if (new_peel_if == nullptr) { 3822 return false; // No peel point found 3823 } 3824 peel_if = new_peel_if; 3825 } 3826 Node* last_peel = stay_in_loop(peel_if, loop); 3827 Node* first_not_peeled = stay_in_loop(last_peel, loop); 3828 if (first_not_peeled == nullptr || first_not_peeled == head) { 3829 return false; 3830 } 3831 3832 #ifndef PRODUCT 3833 if (TraceLoopOpts) { 3834 tty->print("PartialPeel "); 3835 loop->dump_head(); 3836 } 3837 3838 if (TracePartialPeeling) { 3839 tty->print_cr("before partial peel one iteration"); 3840 Node_List wl; 3841 Node* t = head->in(2); 3842 while (true) { 3843 wl.push(t); 3844 if (t == head) break; 3845 t = idom(t); 3846 } 3847 while (wl.size() > 0) { 3848 Node* tt = wl.pop(); 3849 tt->dump(); 3850 if (tt == last_peel) tty->print_cr("-- cut --"); 3851 } 3852 } 3853 #endif 3854 3855 C->print_method(PHASE_BEFORE_PARTIAL_PEELING, 4, head); 3856 3857 VectorSet peel; 3858 VectorSet not_peel; 3859 Node_List peel_list; 3860 Node_List worklist; 3861 Node_List sink_list; 3862 3863 uint estimate = loop->est_loop_clone_sz(1); 3864 if (exceeding_node_budget(estimate)) { 3865 return false; 3866 } 3867 3868 // Set of cfg nodes to peel are those that are executable from 3869 // the head through last_peel. 3870 assert(worklist.size() == 0, "should be empty"); 3871 worklist.push(head); 3872 peel.set(head->_idx); 3873 while (worklist.size() > 0) { 3874 Node *n = worklist.pop(); 3875 if (n != last_peel) { 3876 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) { 3877 Node* use = n->fast_out(j); 3878 if (use->is_CFG() && 3879 loop->is_member(get_loop(use)) && 3880 !peel.test_set(use->_idx)) { 3881 worklist.push(use); 3882 } 3883 } 3884 } 3885 } 3886 3887 // Set of non-cfg nodes to peel are those that are control 3888 // dependent on the cfg nodes. 3889 for (uint i = 0; i < loop->_body.size(); i++) { 3890 Node *n = loop->_body.at(i); 3891 Node *n_c = has_ctrl(n) ? get_ctrl(n) : n; 3892 if (peel.test(n_c->_idx)) { 3893 peel.set(n->_idx); 3894 } else { 3895 not_peel.set(n->_idx); 3896 } 3897 } 3898 3899 // Step 2: move operations from the peeled section down into the 3900 // not-peeled section 3901 3902 // Get a post order schedule of nodes in the peel region 3903 // Result in right-most operand. 3904 scheduled_nodelist(loop, peel, peel_list); 3905 3906 assert(is_valid_loop_partition(loop, peel, peel_list, not_peel), "bad partition"); 3907 3908 // For future check for too many new phis 3909 uint old_phi_cnt = 0; 3910 for (DUIterator_Fast jmax, j = head->fast_outs(jmax); j < jmax; j++) { 3911 Node* use = head->fast_out(j); 3912 if (use->is_Phi()) old_phi_cnt++; 3913 } 3914 3915 #ifndef PRODUCT 3916 if (TracePartialPeeling) { 3917 tty->print_cr("\npeeled list"); 3918 } 3919 #endif 3920 3921 // Evacuate nodes in peel region into the not_peeled region if possible 3922 bool too_many_clones = false; 3923 uint new_phi_cnt = 0; 3924 uint cloned_for_outside_use = 0; 3925 for (uint i = 0; i < peel_list.size();) { 3926 Node* n = peel_list.at(i); 3927 #ifndef PRODUCT 3928 if (TracePartialPeeling) n->dump(); 3929 #endif 3930 bool incr = true; 3931 if (!n->is_CFG()) { 3932 if (has_use_in_set(n, not_peel)) { 3933 // If not used internal to the peeled region, 3934 // move "n" from peeled to not_peeled region. 3935 if (!has_use_internal_to_set(n, peel, loop)) { 3936 // if not pinned and not a load (which maybe anti-dependent on a store) 3937 // and not a CMove (Matcher expects only bool->cmove). 3938 if (n->in(0) == nullptr && !n->is_Load() && !n->is_CMove()) { 3939 int new_clones = clone_for_use_outside_loop(loop, n, worklist); 3940 if (C->failing()) return false; 3941 if (new_clones == -1) { 3942 too_many_clones = true; 3943 break; 3944 } 3945 cloned_for_outside_use += new_clones; 3946 sink_list.push(n); 3947 peel.remove(n->_idx); 3948 not_peel.set(n->_idx); 3949 peel_list.remove(i); 3950 incr = false; 3951 #ifndef PRODUCT 3952 if (TracePartialPeeling) { 3953 tty->print_cr("sink to not_peeled region: %d newbb: %d", 3954 n->_idx, get_ctrl(n)->_idx); 3955 } 3956 #endif 3957 } 3958 } else { 3959 // Otherwise check for special def-use cases that span 3960 // the peel/not_peel boundary such as bool->if 3961 clone_for_special_use_inside_loop(loop, n, not_peel, sink_list, worklist); 3962 new_phi_cnt++; 3963 } 3964 } 3965 } 3966 if (incr) i++; 3967 } 3968 3969 estimate += cloned_for_outside_use + new_phi_cnt; 3970 bool exceed_node_budget = !may_require_nodes(estimate); 3971 bool exceed_phi_limit = new_phi_cnt > old_phi_cnt + PartialPeelNewPhiDelta; 3972 3973 if (too_many_clones || exceed_node_budget || exceed_phi_limit) { 3974 #ifndef PRODUCT 3975 if (TracePartialPeeling && exceed_phi_limit) { 3976 tty->print_cr("\nToo many new phis: %d old %d new cmpi: %c", 3977 new_phi_cnt, old_phi_cnt, new_peel_if != nullptr?'T':'F'); 3978 } 3979 #endif 3980 if (new_peel_if != nullptr) { 3981 remove_cmpi_loop_exit(new_peel_if, loop); 3982 } 3983 // Inhibit more partial peeling on this loop 3984 assert(!head->is_partial_peel_loop(), "not partial peeled"); 3985 head->mark_partial_peel_failed(); 3986 if (cloned_for_outside_use > 0) { 3987 // Terminate this round of loop opts because 3988 // the graph outside this loop was changed. 3989 C->set_major_progress(); 3990 return true; 3991 } 3992 return false; 3993 } 3994 3995 // Step 3: clone loop, retarget control, and insert new phis 3996 3997 // Create new loop head for new phis and to hang 3998 // the nodes being moved (sinked) from the peel region. 3999 LoopNode* new_head = new LoopNode(last_peel, last_peel); 4000 new_head->set_unswitch_count(head->unswitch_count()); // Preserve 4001 _igvn.register_new_node_with_optimizer(new_head); 4002 assert(first_not_peeled->in(0) == last_peel, "last_peel <- first_not_peeled"); 4003 _igvn.replace_input_of(first_not_peeled, 0, new_head); 4004 set_loop(new_head, loop); 4005 loop->_body.push(new_head); 4006 not_peel.set(new_head->_idx); 4007 set_idom(new_head, last_peel, dom_depth(first_not_peeled)); 4008 set_idom(first_not_peeled, new_head, dom_depth(first_not_peeled)); 4009 4010 while (sink_list.size() > 0) { 4011 Node* n = sink_list.pop(); 4012 set_ctrl(n, new_head); 4013 } 4014 4015 assert(is_valid_loop_partition(loop, peel, peel_list, not_peel), "bad partition"); 4016 4017 clone_loop(loop, old_new, dd, IgnoreStripMined); 4018 4019 const uint clone_exit_idx = 1; 4020 const uint orig_exit_idx = 2; 4021 assert(is_valid_clone_loop_form(loop, peel_list, orig_exit_idx, clone_exit_idx), "bad clone loop"); 4022 4023 Node* head_clone = old_new[head->_idx]; 4024 LoopNode* new_head_clone = old_new[new_head->_idx]->as_Loop(); 4025 Node* orig_tail_clone = head_clone->in(2); 4026 4027 // Add phi if "def" node is in peel set and "use" is not 4028 4029 for (uint i = 0; i < peel_list.size(); i++) { 4030 Node *def = peel_list.at(i); 4031 if (!def->is_CFG()) { 4032 for (DUIterator_Fast jmax, j = def->fast_outs(jmax); j < jmax; j++) { 4033 Node *use = def->fast_out(j); 4034 if (has_node(use) && use->in(0) != C->top() && 4035 (!peel.test(use->_idx) || 4036 (use->is_Phi() && use->in(0) == head)) ) { 4037 worklist.push(use); 4038 } 4039 } 4040 while( worklist.size() ) { 4041 Node *use = worklist.pop(); 4042 for (uint j = 1; j < use->req(); j++) { 4043 Node* n = use->in(j); 4044 if (n == def) { 4045 4046 // "def" is in peel set, "use" is not in peel set 4047 // or "use" is in the entry boundary (a phi) of the peel set 4048 4049 Node* use_c = has_ctrl(use) ? get_ctrl(use) : use; 4050 4051 if ( loop->is_member(get_loop( use_c )) ) { 4052 // use is in loop 4053 if (old_new[use->_idx] != nullptr) { // null for dead code 4054 Node* use_clone = old_new[use->_idx]; 4055 _igvn.replace_input_of(use, j, C->top()); 4056 insert_phi_for_loop( use_clone, j, old_new[def->_idx], def, new_head_clone ); 4057 } 4058 } else { 4059 assert(is_valid_clone_loop_exit_use(loop, use, orig_exit_idx), "clone loop format"); 4060 // use is not in the loop, check if the live range includes the cut 4061 Node* lp_if = use_c->in(orig_exit_idx)->in(0); 4062 if (not_peel.test(lp_if->_idx)) { 4063 assert(j == orig_exit_idx, "use from original loop"); 4064 insert_phi_for_loop( use, clone_exit_idx, old_new[def->_idx], def, new_head_clone ); 4065 } 4066 } 4067 } 4068 } 4069 } 4070 } 4071 } 4072 4073 // Step 3b: retarget control 4074 4075 // Redirect control to the new loop head if a cloned node in 4076 // the not_peeled region has control that points into the peeled region. 4077 // This necessary because the cloned peeled region will be outside 4078 // the loop. 4079 // from to 4080 // cloned-peeled <---+ 4081 // new_head_clone: | <--+ 4082 // cloned-not_peeled in(0) in(0) 4083 // orig-peeled 4084 4085 for (uint i = 0; i < loop->_body.size(); i++) { 4086 Node *n = loop->_body.at(i); 4087 if (!n->is_CFG() && n->in(0) != nullptr && 4088 not_peel.test(n->_idx) && peel.test(n->in(0)->_idx)) { 4089 Node* n_clone = old_new[n->_idx]; 4090 if (n_clone->depends_only_on_test()) { 4091 // Pin array access nodes: control is updated here to the loop head. If, after some transformations, the 4092 // backedge is removed, an array load could become dependent on a condition that's not a range check for that 4093 // access. If that condition is replaced by an identical dominating one, then an unpinned load would risk 4094 // floating above its range check. 4095 Node* pinned_clone = n_clone->pin_array_access_node(); 4096 if (pinned_clone != nullptr) { 4097 register_new_node(pinned_clone, get_ctrl(n_clone)); 4098 old_new.map(n->_idx, pinned_clone); 4099 _igvn.replace_node(n_clone, pinned_clone); 4100 n_clone = pinned_clone; 4101 } 4102 } 4103 _igvn.replace_input_of(n_clone, 0, new_head_clone); 4104 } 4105 } 4106 4107 // Backedge of the surviving new_head (the clone) is original last_peel 4108 _igvn.replace_input_of(new_head_clone, LoopNode::LoopBackControl, last_peel); 4109 4110 // Cut first node in original not_peel set 4111 _igvn.rehash_node_delayed(new_head); // Multiple edge updates: 4112 new_head->set_req(LoopNode::EntryControl, C->top()); // use rehash_node_delayed / set_req instead of 4113 new_head->set_req(LoopNode::LoopBackControl, C->top()); // multiple replace_input_of calls 4114 4115 // Copy head_clone back-branch info to original head 4116 // and remove original head's loop entry and 4117 // clone head's back-branch 4118 _igvn.rehash_node_delayed(head); // Multiple edge updates 4119 head->set_req(LoopNode::EntryControl, head_clone->in(LoopNode::LoopBackControl)); 4120 head->set_req(LoopNode::LoopBackControl, C->top()); 4121 _igvn.replace_input_of(head_clone, LoopNode::LoopBackControl, C->top()); 4122 4123 // Similarly modify the phis 4124 for (DUIterator_Fast kmax, k = head->fast_outs(kmax); k < kmax; k++) { 4125 Node* use = head->fast_out(k); 4126 if (use->is_Phi() && use->outcnt() > 0) { 4127 Node* use_clone = old_new[use->_idx]; 4128 _igvn.rehash_node_delayed(use); // Multiple edge updates 4129 use->set_req(LoopNode::EntryControl, use_clone->in(LoopNode::LoopBackControl)); 4130 use->set_req(LoopNode::LoopBackControl, C->top()); 4131 _igvn.replace_input_of(use_clone, LoopNode::LoopBackControl, C->top()); 4132 } 4133 } 4134 4135 // Step 4: update dominator tree and dominator depth 4136 4137 set_idom(head, orig_tail_clone, dd); 4138 recompute_dom_depth(); 4139 4140 // Inhibit more partial peeling on this loop 4141 new_head_clone->set_partial_peel_loop(); 4142 C->set_major_progress(); 4143 loop->record_for_igvn(); 4144 4145 #ifndef PRODUCT 4146 if (TracePartialPeeling) { 4147 tty->print_cr("\nafter partial peel one iteration"); 4148 Node_List wl; 4149 Node* t = last_peel; 4150 while (true) { 4151 wl.push(t); 4152 if (t == head_clone) break; 4153 t = idom(t); 4154 } 4155 while (wl.size() > 0) { 4156 Node* tt = wl.pop(); 4157 if (tt == head) tty->print_cr("orig head"); 4158 else if (tt == new_head_clone) tty->print_cr("new head"); 4159 else if (tt == head_clone) tty->print_cr("clone head"); 4160 tt->dump(); 4161 } 4162 } 4163 #endif 4164 4165 C->print_method(PHASE_AFTER_PARTIAL_PEELING, 4, new_head_clone); 4166 4167 return true; 4168 } 4169 4170 // Transform: 4171 // 4172 // loop<-----------------+ 4173 // | | 4174 // stmt1 stmt2 .. stmtn | 4175 // | | | | 4176 // \ | / | 4177 // v v v | 4178 // region | 4179 // | | 4180 // shared_stmt | 4181 // | | 4182 // v | 4183 // if | 4184 // / \ | 4185 // | -----------+ 4186 // v 4187 // 4188 // into: 4189 // 4190 // loop<-------------------+ 4191 // | | 4192 // v | 4193 // +->loop | 4194 // | | | 4195 // | stmt1 stmt2 .. stmtn | 4196 // | | | | | 4197 // | | \ / | 4198 // | | v v | 4199 // | | region1 | 4200 // | | | | 4201 // | shared_stmt shared_stmt | 4202 // | | | | 4203 // | v v | 4204 // | if if | 4205 // | /\ / \ | 4206 // +-- | | -------+ 4207 // \ / 4208 // v v 4209 // region2 4210 // 4211 // (region2 is shown to merge mirrored projections of the loop exit 4212 // ifs to make the diagram clearer but they really merge the same 4213 // projection) 4214 // 4215 // Conditions for this transformation to trigger: 4216 // - the path through stmt1 is frequent enough 4217 // - the inner loop will be turned into a counted loop after transformation 4218 bool PhaseIdealLoop::duplicate_loop_backedge(IdealLoopTree *loop, Node_List &old_new) { 4219 if (!DuplicateBackedge) { 4220 return false; 4221 } 4222 assert(!loop->_head->is_CountedLoop() || StressDuplicateBackedge, "Non-counted loop only"); 4223 if (!loop->_head->is_Loop()) { 4224 return false; 4225 } 4226 4227 uint estimate = loop->est_loop_clone_sz(1); 4228 if (exceeding_node_budget(estimate)) { 4229 return false; 4230 } 4231 4232 LoopNode *head = loop->_head->as_Loop(); 4233 4234 Node* region = nullptr; 4235 IfNode* exit_test = nullptr; 4236 uint inner; 4237 float f; 4238 if (StressDuplicateBackedge) { 4239 if (head->is_strip_mined()) { 4240 return false; 4241 } 4242 Node* c = head->in(LoopNode::LoopBackControl); 4243 4244 while (c != head) { 4245 if (c->is_Region()) { 4246 region = c; 4247 } 4248 c = idom(c); 4249 } 4250 4251 if (region == nullptr) { 4252 return false; 4253 } 4254 4255 inner = 1; 4256 } else { 4257 // Is the shape of the loop that of a counted loop... 4258 Node* back_control = loop_exit_control(head, loop); 4259 if (back_control == nullptr) { 4260 return false; 4261 } 4262 4263 BoolTest::mask bt = BoolTest::illegal; 4264 float cl_prob = 0; 4265 Node* incr = nullptr; 4266 Node* limit = nullptr; 4267 Node* cmp = loop_exit_test(back_control, loop, incr, limit, bt, cl_prob); 4268 if (cmp == nullptr || cmp->Opcode() != Op_CmpI) { 4269 return false; 4270 } 4271 4272 // With an extra phi for the candidate iv? 4273 // Or the region node is the loop head 4274 if (!incr->is_Phi() || incr->in(0) == head) { 4275 return false; 4276 } 4277 4278 PathFrequency pf(head, this); 4279 region = incr->in(0); 4280 4281 // Go over all paths for the extra phi's region and see if that 4282 // path is frequent enough and would match the expected iv shape 4283 // if the extra phi is removed 4284 inner = 0; 4285 for (uint i = 1; i < incr->req(); ++i) { 4286 Node* in = incr->in(i); 4287 Node* trunc1 = nullptr; 4288 Node* trunc2 = nullptr; 4289 const TypeInteger* iv_trunc_t = nullptr; 4290 Node* orig_in = in; 4291 if (!(in = CountedLoopNode::match_incr_with_optional_truncation(in, &trunc1, &trunc2, &iv_trunc_t, T_INT))) { 4292 continue; 4293 } 4294 assert(in->Opcode() == Op_AddI, "wrong increment code"); 4295 Node* xphi = nullptr; 4296 Node* stride = loop_iv_stride(in, loop, xphi); 4297 4298 if (stride == nullptr) { 4299 continue; 4300 } 4301 4302 PhiNode* phi = loop_iv_phi(xphi, nullptr, head, loop); 4303 if (phi == nullptr || 4304 (trunc1 == nullptr && phi->in(LoopNode::LoopBackControl) != incr) || 4305 (trunc1 != nullptr && phi->in(LoopNode::LoopBackControl) != trunc1)) { 4306 return false; 4307 } 4308 4309 f = pf.to(region->in(i)); 4310 if (f > 0.5) { 4311 inner = i; 4312 break; 4313 } 4314 } 4315 4316 if (inner == 0) { 4317 return false; 4318 } 4319 4320 exit_test = back_control->in(0)->as_If(); 4321 } 4322 4323 if (idom(region)->is_Catch()) { 4324 return false; 4325 } 4326 4327 // Collect all control nodes that need to be cloned (shared_stmt in the diagram) 4328 Unique_Node_List wq; 4329 wq.push(head->in(LoopNode::LoopBackControl)); 4330 for (uint i = 0; i < wq.size(); i++) { 4331 Node* c = wq.at(i); 4332 assert(get_loop(c) == loop, "not in the right loop?"); 4333 if (c->is_Region()) { 4334 if (c != region) { 4335 for (uint j = 1; j < c->req(); ++j) { 4336 wq.push(c->in(j)); 4337 } 4338 } 4339 } else { 4340 wq.push(c->in(0)); 4341 } 4342 assert(!is_dominator(c, region) || c == region, "shouldn't go above region"); 4343 } 4344 4345 Node* region_dom = idom(region); 4346 4347 // Can't do the transformation if this would cause a membar pair to 4348 // be split 4349 for (uint i = 0; i < wq.size(); i++) { 4350 Node* c = wq.at(i); 4351 if (c->is_MemBar() && (c->as_MemBar()->trailing_store() || c->as_MemBar()->trailing_load_store())) { 4352 assert(c->as_MemBar()->leading_membar()->trailing_membar() == c, "bad membar pair"); 4353 if (!wq.member(c->as_MemBar()->leading_membar())) { 4354 return false; 4355 } 4356 } 4357 } 4358 4359 // Collect data nodes that need to be clones as well 4360 int dd = dom_depth(head); 4361 4362 for (uint i = 0; i < loop->_body.size(); ++i) { 4363 Node* n = loop->_body.at(i); 4364 if (has_ctrl(n)) { 4365 Node* c = get_ctrl(n); 4366 if (wq.member(c)) { 4367 wq.push(n); 4368 } 4369 } else { 4370 set_idom(n, idom(n), dd); 4371 } 4372 } 4373 4374 // clone shared_stmt 4375 clone_loop_body(wq, old_new, nullptr); 4376 4377 Node* region_clone = old_new[region->_idx]; 4378 region_clone->set_req(inner, C->top()); 4379 set_idom(region, region->in(inner), dd); 4380 4381 // Prepare the outer loop 4382 Node* outer_head = new LoopNode(head->in(LoopNode::EntryControl), old_new[head->in(LoopNode::LoopBackControl)->_idx]); 4383 register_control(outer_head, loop->_parent, outer_head->in(LoopNode::EntryControl)); 4384 _igvn.replace_input_of(head, LoopNode::EntryControl, outer_head); 4385 set_idom(head, outer_head, dd); 4386 4387 fix_body_edges(wq, loop, old_new, dd, loop->_parent, true); 4388 4389 // Make one of the shared_stmt copies only reachable from stmt1, the 4390 // other only from stmt2..stmtn. 4391 Node* dom = nullptr; 4392 for (uint i = 1; i < region->req(); ++i) { 4393 if (i != inner) { 4394 _igvn.replace_input_of(region, i, C->top()); 4395 } 4396 Node* in = region_clone->in(i); 4397 if (in->is_top()) { 4398 continue; 4399 } 4400 if (dom == nullptr) { 4401 dom = in; 4402 } else { 4403 dom = dom_lca(dom, in); 4404 } 4405 } 4406 4407 set_idom(region_clone, dom, dd); 4408 4409 // Set up the outer loop 4410 for (uint i = 0; i < head->outcnt(); i++) { 4411 Node* u = head->raw_out(i); 4412 if (u->is_Phi()) { 4413 Node* outer_phi = u->clone(); 4414 outer_phi->set_req(0, outer_head); 4415 Node* backedge = old_new[u->in(LoopNode::LoopBackControl)->_idx]; 4416 if (backedge == nullptr) { 4417 backedge = u->in(LoopNode::LoopBackControl); 4418 } 4419 outer_phi->set_req(LoopNode::LoopBackControl, backedge); 4420 register_new_node(outer_phi, outer_head); 4421 _igvn.replace_input_of(u, LoopNode::EntryControl, outer_phi); 4422 } 4423 } 4424 4425 // create control and data nodes for out of loop uses (including region2) 4426 Node_List worklist; 4427 uint new_counter = C->unique(); 4428 fix_ctrl_uses(wq, loop, old_new, ControlAroundStripMined, outer_head, nullptr, worklist); 4429 4430 Node_List *split_if_set = nullptr; 4431 Node_List *split_bool_set = nullptr; 4432 Node_List *split_cex_set = nullptr; 4433 fix_data_uses(wq, loop, ControlAroundStripMined, loop->skip_strip_mined(), new_counter, old_new, worklist, 4434 split_if_set, split_bool_set, split_cex_set); 4435 4436 finish_clone_loop(split_if_set, split_bool_set, split_cex_set); 4437 4438 if (exit_test != nullptr) { 4439 float cnt = exit_test->_fcnt; 4440 if (cnt != COUNT_UNKNOWN) { 4441 exit_test->_fcnt = cnt * f; 4442 old_new[exit_test->_idx]->as_If()->_fcnt = cnt * (1 - f); 4443 } 4444 } 4445 4446 C->set_major_progress(); 4447 4448 return true; 4449 } 4450 4451 // AutoVectorize the loop: replace scalar ops with vector ops. 4452 PhaseIdealLoop::AutoVectorizeStatus 4453 PhaseIdealLoop::auto_vectorize(IdealLoopTree* lpt, VSharedData &vshared) { 4454 // Counted loop only 4455 if (!lpt->is_counted()) { 4456 return AutoVectorizeStatus::Impossible; 4457 } 4458 4459 // Main-loop only 4460 CountedLoopNode* cl = lpt->_head->as_CountedLoop(); 4461 if (!cl->is_main_loop()) { 4462 return AutoVectorizeStatus::Impossible; 4463 } 4464 4465 VLoop vloop(lpt, false); 4466 if (!vloop.check_preconditions()) { 4467 return AutoVectorizeStatus::TriedAndFailed; 4468 } 4469 4470 // Ensure the shared data is cleared before each use 4471 vshared.clear(); 4472 4473 const VLoopAnalyzer vloop_analyzer(vloop, vshared); 4474 if (!vloop_analyzer.success()) { 4475 return AutoVectorizeStatus::TriedAndFailed; 4476 } 4477 4478 SuperWord sw(vloop_analyzer); 4479 if (!sw.transform_loop()) { 4480 return AutoVectorizeStatus::TriedAndFailed; 4481 } 4482 4483 return AutoVectorizeStatus::Success; 4484 } 4485 4486 // Having ReductionNodes in the loop is expensive. They need to recursively 4487 // fold together the vector values, for every vectorized loop iteration. If 4488 // we encounter the following pattern, we can vector accumulate the values 4489 // inside the loop, and only have a single UnorderedReduction after the loop. 4490 // 4491 // CountedLoop init 4492 // | | 4493 // +------+ | +-----------------------+ 4494 // | | | | 4495 // PhiNode (s) | 4496 // | | 4497 // | Vector | 4498 // | | | 4499 // UnorderedReduction (first_ur) | 4500 // | | 4501 // ... Vector | 4502 // | | | 4503 // UnorderedReduction (last_ur) | 4504 // | | 4505 // +---------------------+ 4506 // 4507 // We patch the graph to look like this: 4508 // 4509 // CountedLoop identity_vector 4510 // | | 4511 // +-------+ | +---------------+ 4512 // | | | | 4513 // PhiNode (v) | 4514 // | | 4515 // | Vector | 4516 // | | | 4517 // VectorAccumulator | 4518 // | | 4519 // ... Vector | 4520 // | | | 4521 // init VectorAccumulator | 4522 // | | | | 4523 // UnorderedReduction +-----------+ 4524 // 4525 // We turned the scalar (s) Phi into a vectorized one (v). In the loop, we 4526 // use vector_accumulators, which do the same reductions, but only element 4527 // wise. This is a single operation per vector_accumulator, rather than many 4528 // for a UnorderedReduction. We can then reduce the last vector_accumulator 4529 // after the loop, and also reduce the init value into it. 4530 // We can not do this with all reductions. Some reductions do not allow the 4531 // reordering of operations (for example float addition). 4532 void PhaseIdealLoop::move_unordered_reduction_out_of_loop(IdealLoopTree* loop) { 4533 assert(!C->major_progress() && loop->is_counted() && loop->is_innermost(), "sanity"); 4534 4535 // Find all Phi nodes with UnorderedReduction on backedge. 4536 CountedLoopNode* cl = loop->_head->as_CountedLoop(); 4537 for (DUIterator_Fast jmax, j = cl->fast_outs(jmax); j < jmax; j++) { 4538 Node* phi = cl->fast_out(j); 4539 // We have a phi with a single use, and a UnorderedReduction on the backedge. 4540 if (!phi->is_Phi() || phi->outcnt() != 1 || !phi->in(2)->is_UnorderedReduction()) { 4541 continue; 4542 } 4543 4544 UnorderedReductionNode* last_ur = phi->in(2)->as_UnorderedReduction(); 4545 4546 // Determine types 4547 const TypeVect* vec_t = last_ur->vect_type(); 4548 uint vector_length = vec_t->length(); 4549 BasicType bt = vec_t->element_basic_type(); 4550 const Type* bt_t = Type::get_const_basic_type(bt); 4551 4552 // Convert opcode from vector-reduction -> scalar -> normal-vector-op 4553 const int sopc = VectorNode::scalar_opcode(last_ur->Opcode(), bt); 4554 const int vopc = VectorNode::opcode(sopc, bt); 4555 if (!Matcher::match_rule_supported_vector(vopc, vector_length, bt)) { 4556 DEBUG_ONLY( last_ur->dump(); ) 4557 assert(false, "do not have normal vector op for this reduction"); 4558 continue; // not implemented -> fails 4559 } 4560 4561 // Traverse up the chain of UnorderedReductions, checking that it loops back to 4562 // the phi. Check that all UnorderedReductions only have a single use, except for 4563 // the last (last_ur), which only has phi as a use in the loop, and all other uses 4564 // are outside the loop. 4565 UnorderedReductionNode* current = last_ur; 4566 UnorderedReductionNode* first_ur = nullptr; 4567 while (true) { 4568 assert(current->is_UnorderedReduction(), "sanity"); 4569 4570 // Expect no ctrl and a vector_input from within the loop. 4571 Node* ctrl = current->in(0); 4572 Node* vector_input = current->in(2); 4573 if (ctrl != nullptr || get_ctrl(vector_input) != cl) { 4574 DEBUG_ONLY( current->dump(1); ) 4575 assert(false, "reduction has ctrl or bad vector_input"); 4576 break; // Chain traversal fails. 4577 } 4578 4579 assert(current->vect_type() != nullptr, "must have vector type"); 4580 if (current->vect_type() != last_ur->vect_type()) { 4581 // Reductions do not have the same vector type (length and element type). 4582 break; // Chain traversal fails. 4583 } 4584 4585 // Expect single use of UnorderedReduction, except for last_ur. 4586 if (current == last_ur) { 4587 // Expect all uses to be outside the loop, except phi. 4588 for (DUIterator_Fast kmax, k = current->fast_outs(kmax); k < kmax; k++) { 4589 Node* use = current->fast_out(k); 4590 if (use != phi && ctrl_or_self(use) == cl) { 4591 DEBUG_ONLY( current->dump(-1); ) 4592 assert(false, "reduction has use inside loop"); 4593 // Should not be allowed by SuperWord::mark_reductions 4594 return; // bail out of optimization 4595 } 4596 } 4597 } else { 4598 if (current->outcnt() != 1) { 4599 break; // Chain traversal fails. 4600 } 4601 } 4602 4603 // Expect another UnorderedReduction or phi as the scalar input. 4604 Node* scalar_input = current->in(1); 4605 if (scalar_input->is_UnorderedReduction() && 4606 scalar_input->Opcode() == current->Opcode()) { 4607 // Move up the UnorderedReduction chain. 4608 current = scalar_input->as_UnorderedReduction(); 4609 } else if (scalar_input == phi) { 4610 // Chain terminates at phi. 4611 first_ur = current; 4612 current = nullptr; 4613 break; // Success. 4614 } else { 4615 // scalar_input is neither phi nor a matching reduction 4616 // Can for example be scalar reduction when we have 4617 // partial vectorization. 4618 break; // Chain traversal fails. 4619 } 4620 } 4621 if (current != nullptr) { 4622 // Chain traversal was not successful. 4623 continue; 4624 } 4625 assert(first_ur != nullptr, "must have successfully terminated chain traversal"); 4626 4627 Node* identity_scalar = ReductionNode::make_identity_con_scalar(_igvn, sopc, bt); 4628 set_ctrl(identity_scalar, C->root()); 4629 VectorNode* identity_vector = VectorNode::scalar2vector(identity_scalar, vector_length, bt_t); 4630 register_new_node(identity_vector, C->root()); 4631 assert(vec_t == identity_vector->vect_type(), "matching vector type"); 4632 VectorNode::trace_new_vector(identity_vector, "UnorderedReduction"); 4633 4634 // Turn the scalar phi into a vector phi. 4635 _igvn.rehash_node_delayed(phi); 4636 Node* init = phi->in(1); // Remember init before replacing it. 4637 phi->set_req_X(1, identity_vector, &_igvn); 4638 phi->as_Type()->set_type(vec_t); 4639 _igvn.set_type(phi, vec_t); 4640 4641 // Traverse down the chain of UnorderedReductions, and replace them with vector_accumulators. 4642 current = first_ur; 4643 while (true) { 4644 // Create vector_accumulator to replace current. 4645 Node* last_vector_accumulator = current->in(1); 4646 Node* vector_input = current->in(2); 4647 VectorNode* vector_accumulator = VectorNode::make(vopc, last_vector_accumulator, vector_input, vec_t); 4648 register_new_node(vector_accumulator, cl); 4649 _igvn.replace_node(current, vector_accumulator); 4650 VectorNode::trace_new_vector(vector_accumulator, "UnorderedReduction"); 4651 if (current == last_ur) { 4652 break; 4653 } 4654 current = vector_accumulator->unique_out()->as_UnorderedReduction(); 4655 } 4656 4657 // Create post-loop reduction. 4658 Node* last_accumulator = phi->in(2); 4659 Node* post_loop_reduction = ReductionNode::make(sopc, nullptr, init, last_accumulator, bt); 4660 4661 // Take over uses of last_accumulator that are not in the loop. 4662 for (DUIterator i = last_accumulator->outs(); last_accumulator->has_out(i); i++) { 4663 Node* use = last_accumulator->out(i); 4664 if (use != phi && use != post_loop_reduction) { 4665 assert(ctrl_or_self(use) != cl, "use must be outside loop"); 4666 use->replace_edge(last_accumulator, post_loop_reduction, &_igvn); 4667 --i; 4668 } 4669 } 4670 register_new_node(post_loop_reduction, get_late_ctrl(post_loop_reduction, cl)); 4671 VectorNode::trace_new_vector(post_loop_reduction, "UnorderedReduction"); 4672 4673 assert(last_accumulator->outcnt() == 2, "last_accumulator has 2 uses: phi and post_loop_reduction"); 4674 assert(post_loop_reduction->outcnt() > 0, "should have taken over all non loop uses of last_accumulator"); 4675 assert(phi->outcnt() == 1, "accumulator is the only use of phi"); 4676 } 4677 } 4678 4679 void DataNodeGraph::clone_data_nodes(Node* new_ctrl) { 4680 for (uint i = 0; i < _data_nodes.size(); i++) { 4681 clone(_data_nodes[i], new_ctrl); 4682 } 4683 } 4684 4685 // Clone the given node and set it up properly. Set 'new_ctrl' as ctrl. 4686 void DataNodeGraph::clone(Node* node, Node* new_ctrl) { 4687 Node* clone = node->clone(); 4688 _phase->igvn().register_new_node_with_optimizer(clone); 4689 _orig_to_new.put(node, clone); 4690 _phase->set_ctrl(clone, new_ctrl); 4691 } 4692 4693 // Rewire the data inputs of all (unprocessed) cloned nodes, whose inputs are still pointing to the same inputs as their 4694 // corresponding orig nodes, to the newly cloned inputs to create a separate cloned graph. 4695 void DataNodeGraph::rewire_clones_to_cloned_inputs() { 4696 _orig_to_new.iterate_all([&](Node* node, Node* clone) { 4697 for (uint i = 1; i < node->req(); i++) { 4698 Node** cloned_input = _orig_to_new.get(node->in(i)); 4699 if (cloned_input != nullptr) { 4700 // Input was also cloned -> rewire clone to the cloned input. 4701 _phase->igvn().replace_input_of(clone, i, *cloned_input); 4702 } 4703 } 4704 }); 4705 } 4706 4707 // Clone all non-OpaqueLoop* nodes and apply the provided transformation strategy for OpaqueLoop* nodes. 4708 // Set 'new_ctrl' as ctrl for all cloned non-OpaqueLoop* nodes. 4709 void DataNodeGraph::clone_data_nodes_and_transform_opaque_loop_nodes( 4710 const TransformStrategyForOpaqueLoopNodes& transform_strategy, 4711 Node* new_ctrl) { 4712 for (uint i = 0; i < _data_nodes.size(); i++) { 4713 Node* data_node = _data_nodes[i]; 4714 if (data_node->is_Opaque1()) { 4715 transform_opaque_node(transform_strategy, data_node); 4716 } else { 4717 clone(data_node, new_ctrl); 4718 } 4719 } 4720 } 4721 4722 void DataNodeGraph::transform_opaque_node(const TransformStrategyForOpaqueLoopNodes& transform_strategy, Node* node) { 4723 Node* transformed_node; 4724 if (node->is_OpaqueLoopInit()) { 4725 transformed_node = transform_strategy.transform_opaque_init(node->as_OpaqueLoopInit()); 4726 } else { 4727 assert(node->is_OpaqueLoopStride(), "must be OpaqueLoopStrideNode"); 4728 transformed_node = transform_strategy.transform_opaque_stride(node->as_OpaqueLoopStride()); 4729 } 4730 // Add an orig->new mapping to correctly update the inputs of the copied graph in rewire_clones_to_cloned_inputs(). 4731 _orig_to_new.put(node, transformed_node); 4732 }