1 /* 2 * Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "memory/allocation.inline.hpp" 27 #include "opto/mulnode.hpp" 28 #include "opto/addnode.hpp" 29 #include "opto/connode.hpp" 30 #include "opto/convertnode.hpp" 31 #include "opto/loopnode.hpp" 32 #include "opto/opaquenode.hpp" 33 #include "opto/rootnode.hpp" 34 35 //================= Loop Unswitching ===================== 36 // 37 // orig: transformed: 38 // if (invariant-test) then 39 // predicate predicate 40 // loop loop 41 // stmt1 stmt1 42 // if (invariant-test) then stmt2 43 // stmt2 stmt4 44 // else endloop 45 // stmt3 else 46 // endif predicate [clone] 47 // stmt4 loop [clone] 48 // endloop stmt1 [clone] 49 // stmt3 50 // stmt4 [clone] 51 // endloop 52 // endif 53 // 54 // Note: the "else" clause may be empty 55 56 57 //------------------------------policy_unswitching----------------------------- 58 // Return TRUE or FALSE if the loop should be unswitched 59 // (ie. clone loop with an invariant test that does not exit the loop) 60 bool IdealLoopTree::policy_unswitching( PhaseIdealLoop *phase ) const { 61 if (!LoopUnswitching) { 62 return false; 63 } 64 if (!_head->is_Loop()) { 65 return false; 66 } 67 68 // If nodes are depleted, some transform has miscalculated its needs. 69 assert(!phase->exceeding_node_budget(), "sanity"); 70 71 // check for vectorized loops, any unswitching was already applied 72 if (_head->is_CountedLoop() && _head->as_CountedLoop()->is_unroll_only()) { 73 return false; 74 } 75 76 LoopNode* head = _head->as_Loop(); 77 if (head->unswitch_count() + 1 > head->unswitch_max()) { 78 return false; 79 } 80 81 if (head->is_flattened_arrays()) { 82 return false; 83 } 84 85 Node_List flattened_checks; 86 if (phase->find_unswitching_candidate(this, flattened_checks) == NULL && flattened_checks.size() == 0) { 87 return false; 88 } 89 90 // Too speculative if running low on nodes. 91 return phase->may_require_nodes(est_loop_clone_sz(2)); 92 } 93 94 //------------------------------find_unswitching_candidate----------------------------- 95 // Find candidate "if" for unswitching 96 IfNode* PhaseIdealLoop::find_unswitching_candidate(const IdealLoopTree *loop, Node_List& flattened_checks) const { 97 98 // Find first invariant test that doesn't exit the loop 99 LoopNode *head = loop->_head->as_Loop(); 100 IfNode* unswitch_iff = NULL; 101 Node* n = head->in(LoopNode::LoopBackControl); 102 while (n != head) { 103 Node* n_dom = idom(n); 104 if (n->is_Region()) { 105 if (n_dom->is_If()) { 106 IfNode* iff = n_dom->as_If(); 107 if (iff->in(1)->is_Bool()) { 108 BoolNode* bol = iff->in(1)->as_Bool(); 109 if (bol->in(1)->is_Cmp()) { 110 // If condition is invariant and not a loop exit, 111 // then found reason to unswitch. 112 if (loop->is_invariant(bol) && !loop->is_loop_exit(iff)) { 113 unswitch_iff = iff; 114 } 115 } 116 } 117 } 118 } 119 n = n_dom; 120 } 121 122 Node* array; 123 if (unswitch_iff == NULL || unswitch_iff->is_flattened_array_check(&_igvn, array)) { 124 // collect all flattened array checks 125 for (uint i = 0; i < loop->_body.size(); i++) { 126 Node* n = loop->_body.at(i); 127 if (n->is_If() && n->as_If()->is_flattened_array_check(&_igvn, array) && 128 loop->is_invariant(n->in(1)) && 129 !loop->is_loop_exit(n)) { 130 flattened_checks.push(n); 131 } 132 } 133 if (flattened_checks.size() > 1) { 134 unswitch_iff = NULL; 135 } else { 136 flattened_checks.clear(); 137 } 138 } 139 140 return unswitch_iff; 141 } 142 143 //------------------------------do_unswitching----------------------------- 144 // Clone loop with an invariant test (that does not exit) and 145 // insert a clone of the test that selects which version to 146 // execute. 147 void PhaseIdealLoop::do_unswitching(IdealLoopTree *loop, Node_List &old_new) { 148 149 // Find first invariant test that doesn't exit the loop 150 LoopNode *head = loop->_head->as_Loop(); 151 152 Node_List flattened_checks; 153 IfNode* unswitch_iff = find_unswitching_candidate((const IdealLoopTree *)loop, flattened_checks); 154 assert(unswitch_iff != NULL || flattened_checks.size() > 0, "should be at least one"); 155 if (unswitch_iff == NULL) { 156 unswitch_iff = flattened_checks.at(0)->as_If(); 157 } 158 159 #ifndef PRODUCT 160 if (TraceLoopOpts) { 161 tty->print("Unswitch %d ", head->unswitch_count()+1); 162 loop->dump_head(); 163 } 164 #endif 165 166 // Need to revert back to normal loop 167 if (head->is_CountedLoop() && !head->as_CountedLoop()->is_normal_loop()) { 168 head->as_CountedLoop()->set_normal_loop(); 169 } 170 171 ProjNode* proj_true = create_slow_version_of_loop(loop, old_new, unswitch_iff->Opcode(), CloneIncludesStripMined); 172 173 #ifdef ASSERT 174 Node* uniqc = proj_true->unique_ctrl_out(); 175 Node* entry = head->skip_strip_mined()->in(LoopNode::EntryControl); 176 Node* predicate = find_predicate(entry); 177 if (predicate != NULL) { 178 entry = skip_loop_predicates(entry); 179 } 180 if (predicate != NULL && UseLoopPredicate) { 181 // We may have two predicates, find first. 182 Node* n = find_predicate(entry); 183 if (n != NULL) { 184 predicate = n; 185 entry = skip_loop_predicates(entry); 186 } 187 } 188 if (predicate != NULL && UseProfiledLoopPredicate) { 189 entry = find_predicate(entry); 190 if (entry != NULL) predicate = entry; 191 } 192 if (predicate != NULL) predicate = predicate->in(0); 193 assert(proj_true->is_IfTrue() && 194 (predicate == NULL && uniqc == head && !head->is_strip_mined() || 195 predicate == NULL && uniqc == head->in(LoopNode::EntryControl) && head->is_strip_mined() || 196 predicate != NULL && uniqc == predicate), "by construction"); 197 #endif 198 // Increment unswitch count 199 LoopNode* head_clone = old_new[head->_idx]->as_Loop(); 200 int nct = head->unswitch_count() + 1; 201 head->set_unswitch_count(nct); 202 head_clone->set_unswitch_count(nct); 203 if (flattened_checks.size() > 0) { 204 head->mark_flattened_arrays(); 205 } 206 207 // Add test to new "if" outside of loop 208 IfNode* invar_iff = proj_true->in(0)->as_If(); 209 Node* invar_iff_c = invar_iff->in(0); 210 invar_iff->_prob = unswitch_iff->_prob; 211 if (flattened_checks.size() > 0) { 212 // Flattened array checks are used in 213 // Parse::array_store()/Parse::array_load() to switch between a 214 // legacy object array access and a flattened value array 215 // access. We want the performance impact on legacy accesses to be 216 // as small as possible so we make 2 copies of the loops: a fast 217 // one where all accesses are known to be legacy, a slow one where 218 // some accesses are to flattened arrays. Flattened array checks 219 // can be removed from the first one but not from the second one 220 // as it can have a mix of flattened/legacy accesses. 221 BoolNode* bol = unswitch_iff->in(1)->clone()->as_Bool(); 222 register_new_node(bol, invar_iff->in(0)); 223 Node* cmp = bol->in(1)->clone(); 224 register_new_node(cmp, invar_iff->in(0)); 225 bol->set_req(1, cmp); 226 Node* in1 = NULL; 227 for (uint i = 0; i < flattened_checks.size(); i++) { 228 Node* v = flattened_checks.at(i)->in(1)->in(1)->in(1); 229 if (in1 == NULL) { 230 in1 = v; 231 } else { 232 if (cmp->Opcode() == Op_CmpL) { 233 in1 = new OrLNode(in1, v); 234 } else { 235 in1 = new OrINode(in1, v); 236 } 237 register_new_node(in1, invar_iff->in(0)); 238 } 239 } 240 cmp->set_req(1, in1); 241 invar_iff->set_req(1, bol); 242 } else { 243 BoolNode* bol = unswitch_iff->in(1)->as_Bool(); 244 invar_iff->set_req(1, bol); 245 } 246 247 ProjNode* proj_false = invar_iff->proj_out(0)->as_Proj(); 248 249 // Hoist invariant casts out of each loop to the appropriate 250 // control projection. 251 252 Node_List worklist; 253 254 if (flattened_checks.size() > 0) { 255 for (uint i = 0; i < flattened_checks.size(); i++) { 256 IfNode* iff = flattened_checks.at(i)->as_If(); 257 ProjNode* proj= iff->proj_out(0)->as_Proj(); 258 // Copy to a worklist for easier manipulation 259 for (DUIterator_Fast jmax, j = proj->fast_outs(jmax); j < jmax; j++) { 260 Node* use = proj->fast_out(j); 261 if (use->Opcode() == Op_CheckCastPP && loop->is_invariant(use->in(1))) { 262 worklist.push(use); 263 } 264 } 265 ProjNode* invar_proj = invar_iff->proj_out(proj->_con)->as_Proj(); 266 while (worklist.size() > 0) { 267 Node* use = worklist.pop(); 268 Node* nuse = use->clone(); 269 nuse->set_req(0, invar_proj); 270 _igvn.replace_input_of(use, 1, nuse); 271 register_new_node(nuse, invar_proj); 272 // Same for the clone 273 Node* use_clone = old_new[use->_idx]; 274 _igvn.replace_input_of(use_clone, 1, nuse); 275 } 276 } 277 } else { 278 for (DUIterator_Fast imax, i = unswitch_iff->fast_outs(imax); i < imax; i++) { 279 ProjNode* proj= unswitch_iff->fast_out(i)->as_Proj(); 280 // Copy to a worklist for easier manipulation 281 for (DUIterator_Fast jmax, j = proj->fast_outs(jmax); j < jmax; j++) { 282 Node* use = proj->fast_out(j); 283 if (use->Opcode() == Op_CheckCastPP && loop->is_invariant(use->in(1))) { 284 worklist.push(use); 285 } 286 } 287 ProjNode* invar_proj = invar_iff->proj_out(proj->_con)->as_Proj(); 288 while (worklist.size() > 0) { 289 Node* use = worklist.pop(); 290 Node* nuse = use->clone(); 291 nuse->set_req(0, invar_proj); 292 _igvn.replace_input_of(use, 1, nuse); 293 register_new_node(nuse, invar_proj); 294 // Same for the clone 295 Node* use_clone = old_new[use->_idx]; 296 _igvn.replace_input_of(use_clone, 1, nuse); 297 } 298 } 299 } 300 301 IfNode* unswitch_iff_clone = old_new[unswitch_iff->_idx]->as_If(); 302 if (flattened_checks.size() > 0) { 303 for (uint i = 0; i < flattened_checks.size(); i++) { 304 IfNode* iff = flattened_checks.at(i)->as_If(); 305 _igvn.rehash_node_delayed(iff); 306 short_circuit_if(old_new[iff->_idx]->as_If(), proj_false); 307 } 308 } else { 309 // Hardwire the control paths in the loops into if(true) and if(false) 310 _igvn.rehash_node_delayed(unswitch_iff); 311 short_circuit_if(unswitch_iff, proj_true); 312 313 _igvn.rehash_node_delayed(unswitch_iff_clone); 314 short_circuit_if(unswitch_iff_clone, proj_false); 315 } 316 317 // Reoptimize loops 318 loop->record_for_igvn(); 319 for(int i = loop->_body.size() - 1; i >= 0 ; i--) { 320 Node *n = loop->_body[i]; 321 Node *n_clone = old_new[n->_idx]; 322 _igvn._worklist.push(n_clone); 323 } 324 325 #ifndef PRODUCT 326 if (TraceLoopUnswitching) { 327 tty->print_cr("Loop unswitching orig: %d @ %d new: %d @ %d", 328 head->_idx, unswitch_iff->_idx, 329 old_new[head->_idx]->_idx, unswitch_iff_clone->_idx); 330 } 331 #endif 332 333 C->set_major_progress(); 334 } 335 336 //-------------------------create_slow_version_of_loop------------------------ 337 // Create a slow version of the loop by cloning the loop 338 // and inserting an if to select fast-slow versions. 339 // Return control projection of the entry to the fast version. 340 ProjNode* PhaseIdealLoop::create_slow_version_of_loop(IdealLoopTree *loop, 341 Node_List &old_new, 342 int opcode, 343 CloneLoopMode mode) { 344 LoopNode* head = loop->_head->as_Loop(); 345 bool counted_loop = head->is_CountedLoop(); 346 Node* entry = head->skip_strip_mined()->in(LoopNode::EntryControl); 347 _igvn.rehash_node_delayed(entry); 348 IdealLoopTree* outer_loop = loop->_parent; 349 350 head->verify_strip_mined(1); 351 352 Node *cont = _igvn.intcon(1); 353 set_ctrl(cont, C->root()); 354 Node* opq = new Opaque1Node(C, cont); 355 register_node(opq, outer_loop, entry, dom_depth(entry)); 356 Node *bol = new Conv2BNode(opq); 357 register_node(bol, outer_loop, entry, dom_depth(entry)); 358 IfNode* iff = (opcode == Op_RangeCheck) ? new RangeCheckNode(entry, bol, PROB_MAX, COUNT_UNKNOWN) : 359 new IfNode(entry, bol, PROB_MAX, COUNT_UNKNOWN); 360 register_node(iff, outer_loop, entry, dom_depth(entry)); 361 ProjNode* iffast = new IfTrueNode(iff); 362 register_node(iffast, outer_loop, iff, dom_depth(iff)); 363 ProjNode* ifslow = new IfFalseNode(iff); 364 register_node(ifslow, outer_loop, iff, dom_depth(iff)); 365 366 // Clone the loop body. The clone becomes the fast loop. The 367 // original pre-header will (illegally) have 3 control users 368 // (old & new loops & new if). 369 clone_loop(loop, old_new, dom_depth(head->skip_strip_mined()), mode, iff); 370 assert(old_new[head->_idx]->is_Loop(), "" ); 371 372 // Fast (true) control 373 Node* iffast_pred = clone_loop_predicates(entry, iffast, !counted_loop); 374 375 // Slow (false) control 376 Node* ifslow_pred = clone_loop_predicates(entry, ifslow, !counted_loop); 377 378 Node* l = head->skip_strip_mined(); 379 _igvn.replace_input_of(l, LoopNode::EntryControl, iffast_pred); 380 set_idom(l, iffast_pred, dom_depth(l)); 381 LoopNode* slow_l = old_new[head->_idx]->as_Loop()->skip_strip_mined(); 382 _igvn.replace_input_of(slow_l, LoopNode::EntryControl, ifslow_pred); 383 set_idom(slow_l, ifslow_pred, dom_depth(l)); 384 385 recompute_dom_depth(); 386 387 return iffast; 388 } 389 390 LoopNode* PhaseIdealLoop::create_reserve_version_of_loop(IdealLoopTree *loop, CountedLoopReserveKit* lk) { 391 Node_List old_new; 392 LoopNode* head = loop->_head->as_Loop(); 393 bool counted_loop = head->is_CountedLoop(); 394 Node* entry = head->skip_strip_mined()->in(LoopNode::EntryControl); 395 _igvn.rehash_node_delayed(entry); 396 IdealLoopTree* outer_loop = head->is_strip_mined() ? loop->_parent->_parent : loop->_parent; 397 398 ConINode* const_1 = _igvn.intcon(1); 399 set_ctrl(const_1, C->root()); 400 IfNode* iff = new IfNode(entry, const_1, PROB_MAX, COUNT_UNKNOWN); 401 register_node(iff, outer_loop, entry, dom_depth(entry)); 402 ProjNode* iffast = new IfTrueNode(iff); 403 register_node(iffast, outer_loop, iff, dom_depth(iff)); 404 ProjNode* ifslow = new IfFalseNode(iff); 405 register_node(ifslow, outer_loop, iff, dom_depth(iff)); 406 407 // Clone the loop body. The clone becomes the fast loop. The 408 // original pre-header will (illegally) have 3 control users 409 // (old & new loops & new if). 410 clone_loop(loop, old_new, dom_depth(head), CloneIncludesStripMined, iff); 411 assert(old_new[head->_idx]->is_Loop(), "" ); 412 413 LoopNode* slow_head = old_new[head->_idx]->as_Loop(); 414 415 #ifndef PRODUCT 416 if (TraceLoopOpts) { 417 tty->print_cr("PhaseIdealLoop::create_reserve_version_of_loop:"); 418 tty->print("\t iff = %d, ", iff->_idx); iff->dump(); 419 tty->print("\t iffast = %d, ", iffast->_idx); iffast->dump(); 420 tty->print("\t ifslow = %d, ", ifslow->_idx); ifslow->dump(); 421 tty->print("\t before replace_input_of: head = %d, ", head->_idx); head->dump(); 422 tty->print("\t before replace_input_of: slow_head = %d, ", slow_head->_idx); slow_head->dump(); 423 } 424 #endif 425 426 // Fast (true) control 427 _igvn.replace_input_of(head->skip_strip_mined(), LoopNode::EntryControl, iffast); 428 // Slow (false) control 429 _igvn.replace_input_of(slow_head->skip_strip_mined(), LoopNode::EntryControl, ifslow); 430 431 recompute_dom_depth(); 432 433 lk->set_iff(iff); 434 435 #ifndef PRODUCT 436 if (TraceLoopOpts ) { 437 tty->print("\t after replace_input_of: head = %d, ", head->_idx); head->dump(); 438 tty->print("\t after replace_input_of: slow_head = %d, ", slow_head->_idx); slow_head->dump(); 439 } 440 #endif 441 442 return slow_head->as_Loop(); 443 } 444 445 CountedLoopReserveKit::CountedLoopReserveKit(PhaseIdealLoop* phase, IdealLoopTree *loop, bool active = true) : 446 _phase(phase), 447 _lpt(loop), 448 _lp(NULL), 449 _iff(NULL), 450 _lp_reserved(NULL), 451 _has_reserved(false), 452 _use_new(false), 453 _active(active) 454 { 455 create_reserve(); 456 }; 457 458 CountedLoopReserveKit::~CountedLoopReserveKit() { 459 if (!_active) { 460 return; 461 } 462 463 if (_has_reserved && !_use_new) { 464 // intcon(0)->iff-node reverts CF to the reserved copy 465 ConINode* const_0 = _phase->_igvn.intcon(0); 466 _phase->set_ctrl(const_0, _phase->C->root()); 467 _iff->set_req(1, const_0); 468 469 #ifndef PRODUCT 470 if (TraceLoopOpts) { 471 tty->print_cr("CountedLoopReserveKit::~CountedLoopReserveKit()"); 472 tty->print("\t discard loop %d and revert to the reserved loop clone %d: ", _lp->_idx, _lp_reserved->_idx); 473 _lp_reserved->dump(); 474 } 475 #endif 476 } 477 } 478 479 bool CountedLoopReserveKit::create_reserve() { 480 if (!_active) { 481 return false; 482 } 483 484 if(!_lpt->_head->is_CountedLoop()) { 485 if (TraceLoopOpts) { 486 tty->print_cr("CountedLoopReserveKit::create_reserve: %d not counted loop", _lpt->_head->_idx); 487 } 488 return false; 489 } 490 CountedLoopNode *cl = _lpt->_head->as_CountedLoop(); 491 if (!cl->is_valid_counted_loop()) { 492 if (TraceLoopOpts) { 493 tty->print_cr("CountedLoopReserveKit::create_reserve: %d not valid counted loop", cl->_idx); 494 } 495 return false; // skip malformed counted loop 496 } 497 if (!cl->is_main_loop()) { 498 bool loop_not_canonical = true; 499 if (cl->is_post_loop() && (cl->slp_max_unroll() > 0)) { 500 loop_not_canonical = false; 501 } 502 // only reject some loop forms 503 if (loop_not_canonical) { 504 if (TraceLoopOpts) { 505 tty->print_cr("CountedLoopReserveKit::create_reserve: %d not canonical loop", cl->_idx); 506 } 507 return false; // skip normal, pre, and post (conditionally) loops 508 } 509 } 510 511 _lp = _lpt->_head->as_Loop(); 512 _lp_reserved = _phase->create_reserve_version_of_loop(_lpt, this); 513 514 if (!_lp_reserved->is_CountedLoop()) { 515 return false; 516 } 517 518 Node* ifslow_pred = _lp_reserved->skip_strip_mined()->in(LoopNode::EntryControl); 519 520 if (!ifslow_pred->is_IfFalse()) { 521 return false; 522 } 523 524 Node* iff = ifslow_pred->in(0); 525 if (!iff->is_If() || iff != _iff) { 526 return false; 527 } 528 529 if (iff->in(1)->Opcode() != Op_ConI) { 530 return false; 531 } 532 533 return _has_reserved = true; 534 }