1 /* 2 * Copyright (c) 2011, 2014, 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 "opto/loopnode.hpp" 27 #include "opto/addnode.hpp" 28 #include "opto/callnode.hpp" 29 #include "opto/connode.hpp" 30 #include "opto/loopnode.hpp" 31 #include "opto/mulnode.hpp" 32 #include "opto/rootnode.hpp" 33 #include "opto/subnode.hpp" 34 35 /* 36 * The general idea of Loop Predication is to insert a predicate on the entry 37 * path to a loop, and raise a uncommon trap if the check of the condition fails. 38 * The condition checks are promoted from inside the loop body, and thus 39 * the checks inside the loop could be eliminated. Currently, loop predication 40 * optimization has been applied to remove array range check and loop invariant 41 * checks (such as null checks). 42 */ 43 44 //-------------------------------register_control------------------------- 45 void PhaseIdealLoop::register_control(Node* n, IdealLoopTree *loop, Node* pred) { 46 assert(n->is_CFG(), "must be control node"); 47 _igvn.register_new_node_with_optimizer(n); 48 loop->_body.push(n); 49 set_loop(n, loop); 50 // When called from beautify_loops() idom is not constructed yet. 51 if (_idom != NULL) { 52 set_idom(n, pred, dom_depth(pred)); 53 } 54 } 55 56 //------------------------------create_new_if_for_predicate------------------------ 57 // create a new if above the uct_if_pattern for the predicate to be promoted. 58 // 59 // before after 60 // ---------- ---------- 61 // ctrl ctrl 62 // | | 63 // | | 64 // v v 65 // iff new_iff 66 // / \ / \ 67 // / \ / \ 68 // v v v v 69 // uncommon_proj cont_proj if_uct if_cont 70 // \ | | | | 71 // \ | | | | 72 // v v v | v 73 // rgn loop | iff 74 // | | / \ 75 // | | / \ 76 // v | v v 77 // uncommon_trap | uncommon_proj cont_proj 78 // \ \ | | 79 // \ \ | | 80 // v v v v 81 // rgn loop 82 // | 83 // | 84 // v 85 // uncommon_trap 86 // 87 // 88 // We will create a region to guard the uct call if there is no one there. 89 // The true projecttion (if_cont) of the new_iff is returned. 90 // This code is also used to clone predicates to clonned loops. 91 ProjNode* PhaseIdealLoop::create_new_if_for_predicate(ProjNode* cont_proj, Node* new_entry, 92 Deoptimization::DeoptReason reason) { 93 assert(cont_proj->is_uncommon_trap_if_pattern(reason), "must be a uct if pattern!"); 94 IfNode* iff = cont_proj->in(0)->as_If(); 95 96 ProjNode *uncommon_proj = iff->proj_out(1 - cont_proj->_con); 97 Node *rgn = uncommon_proj->unique_ctrl_out(); 98 assert(rgn->is_Region() || rgn->is_Call(), "must be a region or call uct"); 99 100 uint proj_index = 1; // region's edge corresponding to uncommon_proj 101 if (!rgn->is_Region()) { // create a region to guard the call 102 assert(rgn->is_Call(), "must be call uct"); 103 CallNode* call = rgn->as_Call(); 104 IdealLoopTree* loop = get_loop(call); 105 rgn = new (C) RegionNode(1); 106 rgn->add_req(uncommon_proj); 107 register_control(rgn, loop, uncommon_proj); 108 _igvn.hash_delete(call); 109 call->set_req(0, rgn); 110 // When called from beautify_loops() idom is not constructed yet. 111 if (_idom != NULL) { 112 set_idom(call, rgn, dom_depth(rgn)); 113 } 114 } else { 115 // Find region's edge corresponding to uncommon_proj 116 for (; proj_index < rgn->req(); proj_index++) 117 if (rgn->in(proj_index) == uncommon_proj) break; 118 assert(proj_index < rgn->req(), "sanity"); 119 } 120 121 Node* entry = iff->in(0); 122 if (new_entry != NULL) { 123 // Clonning the predicate to new location. 124 entry = new_entry; 125 } 126 // Create new_iff 127 IdealLoopTree* lp = get_loop(entry); 128 IfNode *new_iff = iff->clone()->as_If(); 129 new_iff->set_req(0, entry); 130 register_control(new_iff, lp, entry); 131 Node *if_cont = new (C) IfTrueNode(new_iff); 132 Node *if_uct = new (C) IfFalseNode(new_iff); 133 if (cont_proj->is_IfFalse()) { 134 // Swap 135 Node* tmp = if_uct; if_uct = if_cont; if_cont = tmp; 136 } 137 register_control(if_cont, lp, new_iff); 138 register_control(if_uct, get_loop(rgn), new_iff); 139 140 // if_uct to rgn 141 _igvn.hash_delete(rgn); 142 rgn->add_req(if_uct); 143 // When called from beautify_loops() idom is not constructed yet. 144 if (_idom != NULL) { 145 Node* ridom = idom(rgn); 146 Node* nrdom = dom_lca(ridom, new_iff); 147 set_idom(rgn, nrdom, dom_depth(rgn)); 148 } 149 150 // If rgn has phis add new edges which has the same 151 // value as on original uncommon_proj pass. 152 assert(rgn->in(rgn->req() -1) == if_uct, "new edge should be last"); 153 bool has_phi = false; 154 for (DUIterator_Fast imax, i = rgn->fast_outs(imax); i < imax; i++) { 155 Node* use = rgn->fast_out(i); 156 if (use->is_Phi() && use->outcnt() > 0) { 157 assert(use->in(0) == rgn, ""); 158 _igvn.rehash_node_delayed(use); 159 use->add_req(use->in(proj_index)); 160 has_phi = true; 161 } 162 } 163 assert(!has_phi || rgn->req() > 3, "no phis when region is created"); 164 165 if (new_entry == NULL) { 166 // Attach if_cont to iff 167 _igvn.hash_delete(iff); 168 iff->set_req(0, if_cont); 169 if (_idom != NULL) { 170 set_idom(iff, if_cont, dom_depth(iff)); 171 } 172 } 173 return if_cont->as_Proj(); 174 } 175 176 //------------------------------create_new_if_for_predicate------------------------ 177 // Create a new if below new_entry for the predicate to be cloned (IGVN optimization) 178 ProjNode* PhaseIterGVN::create_new_if_for_predicate(ProjNode* cont_proj, Node* new_entry, 179 Deoptimization::DeoptReason reason) { 180 assert(new_entry != 0, "only used for clone predicate"); 181 assert(cont_proj->is_uncommon_trap_if_pattern(reason), "must be a uct if pattern!"); 182 IfNode* iff = cont_proj->in(0)->as_If(); 183 184 ProjNode *uncommon_proj = iff->proj_out(1 - cont_proj->_con); 185 Node *rgn = uncommon_proj->unique_ctrl_out(); 186 assert(rgn->is_Region() || rgn->is_Call(), "must be a region or call uct"); 187 188 uint proj_index = 1; // region's edge corresponding to uncommon_proj 189 if (!rgn->is_Region()) { // create a region to guard the call 190 assert(rgn->is_Call(), "must be call uct"); 191 CallNode* call = rgn->as_Call(); 192 rgn = new (C) RegionNode(1); 193 register_new_node_with_optimizer(rgn); 194 rgn->add_req(uncommon_proj); 195 hash_delete(call); 196 call->set_req(0, rgn); 197 } else { 198 // Find region's edge corresponding to uncommon_proj 199 for (; proj_index < rgn->req(); proj_index++) 200 if (rgn->in(proj_index) == uncommon_proj) break; 201 assert(proj_index < rgn->req(), "sanity"); 202 } 203 204 // Create new_iff in new location. 205 IfNode *new_iff = iff->clone()->as_If(); 206 new_iff->set_req(0, new_entry); 207 208 register_new_node_with_optimizer(new_iff); 209 Node *if_cont = new (C) IfTrueNode(new_iff); 210 Node *if_uct = new (C) IfFalseNode(new_iff); 211 if (cont_proj->is_IfFalse()) { 212 // Swap 213 Node* tmp = if_uct; if_uct = if_cont; if_cont = tmp; 214 } 215 register_new_node_with_optimizer(if_cont); 216 register_new_node_with_optimizer(if_uct); 217 218 // if_uct to rgn 219 hash_delete(rgn); 220 rgn->add_req(if_uct); 221 222 // If rgn has phis add corresponding new edges which has the same 223 // value as on original uncommon_proj pass. 224 assert(rgn->in(rgn->req() -1) == if_uct, "new edge should be last"); 225 bool has_phi = false; 226 for (DUIterator_Fast imax, i = rgn->fast_outs(imax); i < imax; i++) { 227 Node* use = rgn->fast_out(i); 228 if (use->is_Phi() && use->outcnt() > 0) { 229 rehash_node_delayed(use); 230 use->add_req(use->in(proj_index)); 231 has_phi = true; 232 } 233 } 234 assert(!has_phi || rgn->req() > 3, "no phis when region is created"); 235 236 return if_cont->as_Proj(); 237 } 238 239 //--------------------------clone_predicate----------------------- 240 ProjNode* PhaseIdealLoop::clone_predicate(ProjNode* predicate_proj, Node* new_entry, 241 Deoptimization::DeoptReason reason, 242 PhaseIdealLoop* loop_phase, 243 PhaseIterGVN* igvn) { 244 ProjNode* new_predicate_proj; 245 if (loop_phase != NULL) { 246 new_predicate_proj = loop_phase->create_new_if_for_predicate(predicate_proj, new_entry, reason); 247 } else { 248 new_predicate_proj = igvn->create_new_if_for_predicate(predicate_proj, new_entry, reason); 249 } 250 IfNode* iff = new_predicate_proj->in(0)->as_If(); 251 Node* ctrl = iff->in(0); 252 253 // Match original condition since predicate's projections could be swapped. 254 assert(predicate_proj->in(0)->in(1)->in(1)->Opcode()==Op_Opaque1, "must be"); 255 Node* opq = new (igvn->C) Opaque1Node(igvn->C, predicate_proj->in(0)->in(1)->in(1)->in(1)); 256 igvn->C->add_predicate_opaq(opq); 257 258 Node* bol = new (igvn->C) Conv2BNode(opq); 259 if (loop_phase != NULL) { 260 loop_phase->register_new_node(opq, ctrl); 261 loop_phase->register_new_node(bol, ctrl); 262 } else { 263 igvn->register_new_node_with_optimizer(opq); 264 igvn->register_new_node_with_optimizer(bol); 265 } 266 igvn->hash_delete(iff); 267 iff->set_req(1, bol); 268 return new_predicate_proj; 269 } 270 271 272 //--------------------------clone_loop_predicates----------------------- 273 // Interface from IGVN 274 Node* PhaseIterGVN::clone_loop_predicates(Node* old_entry, Node* new_entry, bool clone_limit_check) { 275 return PhaseIdealLoop::clone_loop_predicates(old_entry, new_entry, clone_limit_check, NULL, this); 276 } 277 278 // Interface from PhaseIdealLoop 279 Node* PhaseIdealLoop::clone_loop_predicates(Node* old_entry, Node* new_entry, bool clone_limit_check) { 280 return clone_loop_predicates(old_entry, new_entry, clone_limit_check, this, &this->_igvn); 281 } 282 283 // Clone loop predicates to cloned loops (peeled, unswitched, split_if). 284 Node* PhaseIdealLoop::clone_loop_predicates(Node* old_entry, Node* new_entry, 285 bool clone_limit_check, 286 PhaseIdealLoop* loop_phase, 287 PhaseIterGVN* igvn) { 288 #ifdef ASSERT 289 if (new_entry == NULL || !(new_entry->is_Proj() || new_entry->is_Region() || new_entry->is_SafePoint())) { 290 if (new_entry != NULL) 291 new_entry->dump(); 292 assert(false, "not IfTrue, IfFalse, Region or SafePoint"); 293 } 294 #endif 295 // Search original predicates 296 Node* entry = old_entry; 297 ProjNode* limit_check_proj = NULL; 298 if (LoopLimitCheck) { 299 limit_check_proj = find_predicate_insertion_point(entry, Deoptimization::Reason_loop_limit_check); 300 if (limit_check_proj != NULL) { 301 entry = entry->in(0)->in(0); 302 } 303 } 304 if (UseLoopPredicate) { 305 ProjNode* predicate_proj = find_predicate_insertion_point(entry, Deoptimization::Reason_predicate); 306 if (predicate_proj != NULL) { // right pattern that can be used by loop predication 307 // clone predicate 308 new_entry = clone_predicate(predicate_proj, new_entry, 309 Deoptimization::Reason_predicate, 310 loop_phase, igvn); 311 assert(new_entry != NULL && new_entry->is_Proj(), "IfTrue or IfFalse after clone predicate"); 312 if (TraceLoopPredicate) { 313 tty->print("Loop Predicate cloned: "); 314 debug_only( new_entry->in(0)->dump(); ) 315 } 316 } 317 } 318 if (limit_check_proj != NULL && clone_limit_check) { 319 // Clone loop limit check last to insert it before loop. 320 // Don't clone a limit check which was already finalized 321 // for this counted loop (only one limit check is needed). 322 new_entry = clone_predicate(limit_check_proj, new_entry, 323 Deoptimization::Reason_loop_limit_check, 324 loop_phase, igvn); 325 assert(new_entry != NULL && new_entry->is_Proj(), "IfTrue or IfFalse after clone limit check"); 326 if (TraceLoopLimitCheck) { 327 tty->print("Loop Limit Check cloned: "); 328 debug_only( new_entry->in(0)->dump(); ) 329 } 330 } 331 return new_entry; 332 } 333 334 //--------------------------skip_loop_predicates------------------------------ 335 // Skip related predicates. 336 Node* PhaseIdealLoop::skip_loop_predicates(Node* entry) { 337 Node* predicate = NULL; 338 if (LoopLimitCheck) { 339 predicate = find_predicate_insertion_point(entry, Deoptimization::Reason_loop_limit_check); 340 if (predicate != NULL) { 341 entry = entry->in(0)->in(0); 342 } 343 } 344 if (UseLoopPredicate) { 345 predicate = find_predicate_insertion_point(entry, Deoptimization::Reason_predicate); 346 if (predicate != NULL) { // right pattern that can be used by loop predication 347 IfNode* iff = entry->in(0)->as_If(); 348 ProjNode* uncommon_proj = iff->proj_out(1 - entry->as_Proj()->_con); 349 Node* rgn = uncommon_proj->unique_ctrl_out(); 350 assert(rgn->is_Region() || rgn->is_Call(), "must be a region or call uct"); 351 entry = entry->in(0)->in(0); 352 while (entry != NULL && entry->is_Proj() && entry->in(0)->is_If()) { 353 uncommon_proj = entry->in(0)->as_If()->proj_out(1 - entry->as_Proj()->_con); 354 if (uncommon_proj->unique_ctrl_out() != rgn) 355 break; 356 entry = entry->in(0)->in(0); 357 } 358 } 359 } 360 return entry; 361 } 362 363 //--------------------------find_predicate_insertion_point------------------- 364 // Find a good location to insert a predicate 365 ProjNode* PhaseIdealLoop::find_predicate_insertion_point(Node* start_c, Deoptimization::DeoptReason reason) { 366 if (start_c == NULL || !start_c->is_Proj()) 367 return NULL; 368 if (start_c->as_Proj()->is_uncommon_trap_if_pattern(reason)) { 369 return start_c->as_Proj(); 370 } 371 return NULL; 372 } 373 374 //--------------------------find_predicate------------------------------------ 375 // Find a predicate 376 Node* PhaseIdealLoop::find_predicate(Node* entry) { 377 Node* predicate = NULL; 378 if (LoopLimitCheck) { 379 predicate = find_predicate_insertion_point(entry, Deoptimization::Reason_loop_limit_check); 380 if (predicate != NULL) { // right pattern that can be used by loop predication 381 return entry; 382 } 383 } 384 if (UseLoopPredicate) { 385 predicate = find_predicate_insertion_point(entry, Deoptimization::Reason_predicate); 386 if (predicate != NULL) { // right pattern that can be used by loop predication 387 return entry; 388 } 389 } 390 return NULL; 391 } 392 393 //------------------------------Invariance----------------------------------- 394 // Helper class for loop_predication_impl to compute invariance on the fly and 395 // clone invariants. 396 class Invariance : public StackObj { 397 VectorSet _visited, _invariant; 398 Node_Stack _stack; 399 VectorSet _clone_visited; 400 Node_List _old_new; // map of old to new (clone) 401 IdealLoopTree* _lpt; 402 PhaseIdealLoop* _phase; 403 404 // Helper function to set up the invariance for invariance computation 405 // If n is a known invariant, set up directly. Otherwise, look up the 406 // the possibility to push n onto the stack for further processing. 407 void visit(Node* use, Node* n) { 408 if (_lpt->is_invariant(n)) { // known invariant 409 _invariant.set(n->_idx); 410 } else if (!n->is_CFG()) { 411 Node *n_ctrl = _phase->ctrl_or_self(n); 412 Node *u_ctrl = _phase->ctrl_or_self(use); // self if use is a CFG 413 if (_phase->is_dominator(n_ctrl, u_ctrl)) { 414 _stack.push(n, n->in(0) == NULL ? 1 : 0); 415 } 416 } 417 } 418 419 // Compute invariance for "the_node" and (possibly) all its inputs recursively 420 // on the fly 421 void compute_invariance(Node* n) { 422 assert(_visited.test(n->_idx), "must be"); 423 visit(n, n); 424 while (_stack.is_nonempty()) { 425 Node* n = _stack.node(); 426 uint idx = _stack.index(); 427 if (idx == n->req()) { // all inputs are processed 428 _stack.pop(); 429 // n is invariant if it's inputs are all invariant 430 bool all_inputs_invariant = true; 431 for (uint i = 0; i < n->req(); i++) { 432 Node* in = n->in(i); 433 if (in == NULL) continue; 434 assert(_visited.test(in->_idx), "must have visited input"); 435 if (!_invariant.test(in->_idx)) { // bad guy 436 all_inputs_invariant = false; 437 break; 438 } 439 } 440 if (all_inputs_invariant) { 441 // If n's control is a predicate that was moved out of the 442 // loop, it was marked invariant but n is only invariant if 443 // it depends only on that test. Otherwise, unless that test 444 // is out of the loop, it's not invariant. 445 if (n->is_CFG() || n->depends_only_on_test() || n->in(0) == NULL || !_phase->is_member(_lpt, n->in(0))) { 446 _invariant.set(n->_idx); // I am a invariant too 447 } 448 } 449 } else { // process next input 450 _stack.set_index(idx + 1); 451 Node* m = n->in(idx); 452 if (m != NULL && !_visited.test_set(m->_idx)) { 453 visit(n, m); 454 } 455 } 456 } 457 } 458 459 // Helper function to set up _old_new map for clone_nodes. 460 // If n is a known invariant, set up directly ("clone" of n == n). 461 // Otherwise, push n onto the stack for real cloning. 462 void clone_visit(Node* n) { 463 assert(_invariant.test(n->_idx), "must be invariant"); 464 if (_lpt->is_invariant(n)) { // known invariant 465 _old_new.map(n->_idx, n); 466 } else { // to be cloned 467 assert(!n->is_CFG(), "should not see CFG here"); 468 _stack.push(n, n->in(0) == NULL ? 1 : 0); 469 } 470 } 471 472 // Clone "n" and (possibly) all its inputs recursively 473 void clone_nodes(Node* n, Node* ctrl) { 474 clone_visit(n); 475 while (_stack.is_nonempty()) { 476 Node* n = _stack.node(); 477 uint idx = _stack.index(); 478 if (idx == n->req()) { // all inputs processed, clone n! 479 _stack.pop(); 480 // clone invariant node 481 Node* n_cl = n->clone(); 482 _old_new.map(n->_idx, n_cl); 483 _phase->register_new_node(n_cl, ctrl); 484 for (uint i = 0; i < n->req(); i++) { 485 Node* in = n_cl->in(i); 486 if (in == NULL) continue; 487 n_cl->set_req(i, _old_new[in->_idx]); 488 } 489 } else { // process next input 490 _stack.set_index(idx + 1); 491 Node* m = n->in(idx); 492 if (m != NULL && !_clone_visited.test_set(m->_idx)) { 493 clone_visit(m); // visit the input 494 } 495 } 496 } 497 } 498 499 public: 500 Invariance(Arena* area, IdealLoopTree* lpt) : 501 _lpt(lpt), _phase(lpt->_phase), 502 _visited(area), _invariant(area), _stack(area, 10 /* guess */), 503 _clone_visited(area), _old_new(area) 504 {} 505 506 // Map old to n for invariance computation and clone 507 void map_ctrl(Node* old, Node* n) { 508 assert(old->is_CFG() && n->is_CFG(), "must be"); 509 _old_new.map(old->_idx, n); // "clone" of old is n 510 _invariant.set(old->_idx); // old is invariant 511 _clone_visited.set(old->_idx); 512 } 513 514 // Driver function to compute invariance 515 bool is_invariant(Node* n) { 516 if (!_visited.test_set(n->_idx)) 517 compute_invariance(n); 518 return (_invariant.test(n->_idx) != 0); 519 } 520 521 // Driver function to clone invariant 522 Node* clone(Node* n, Node* ctrl) { 523 assert(ctrl->is_CFG(), "must be"); 524 assert(_invariant.test(n->_idx), "must be an invariant"); 525 if (!_clone_visited.test(n->_idx)) 526 clone_nodes(n, ctrl); 527 return _old_new[n->_idx]; 528 } 529 }; 530 531 //------------------------------is_range_check_if ----------------------------------- 532 // Returns true if the predicate of iff is in "scale*iv + offset u< load_range(ptr)" format 533 // Note: this function is particularly designed for loop predication. We require load_range 534 // and offset to be loop invariant computed on the fly by "invar" 535 bool IdealLoopTree::is_range_check_if(IfNode *iff, PhaseIdealLoop *phase, Invariance& invar) const { 536 if (!is_loop_exit(iff)) { 537 return false; 538 } 539 if (!iff->in(1)->is_Bool()) { 540 return false; 541 } 542 const BoolNode *bol = iff->in(1)->as_Bool(); 543 if (bol->_test._test != BoolTest::lt) { 544 return false; 545 } 546 if (!bol->in(1)->is_Cmp()) { 547 return false; 548 } 549 const CmpNode *cmp = bol->in(1)->as_Cmp(); 550 if (cmp->Opcode() != Op_CmpU) { 551 return false; 552 } 553 Node* range = cmp->in(2); 554 if (range->Opcode() != Op_LoadRange) { 555 const TypeInt* tint = phase->_igvn.type(range)->isa_int(); 556 if (tint == NULL || tint->empty() || tint->_lo < 0) { 557 // Allow predication on positive values that aren't LoadRanges. 558 // This allows optimization of loops where the length of the 559 // array is a known value and doesn't need to be loaded back 560 // from the array. 561 return false; 562 } 563 } 564 if (!invar.is_invariant(range)) { 565 return false; 566 } 567 Node *iv = _head->as_CountedLoop()->phi(); 568 int scale = 0; 569 Node *offset = NULL; 570 if (!phase->is_scaled_iv_plus_offset(cmp->in(1), iv, &scale, &offset)) { 571 return false; 572 } 573 if (offset && !invar.is_invariant(offset)) { // offset must be invariant 574 return false; 575 } 576 return true; 577 } 578 579 //------------------------------rc_predicate----------------------------------- 580 // Create a range check predicate 581 // 582 // for (i = init; i < limit; i += stride) { 583 // a[scale*i+offset] 584 // } 585 // 586 // Compute max(scale*i + offset) for init <= i < limit and build the predicate 587 // as "max(scale*i + offset) u< a.length". 588 // 589 // There are two cases for max(scale*i + offset): 590 // (1) stride*scale > 0 591 // max(scale*i + offset) = scale*(limit-stride) + offset 592 // (2) stride*scale < 0 593 // max(scale*i + offset) = scale*init + offset 594 BoolNode* PhaseIdealLoop::rc_predicate(IdealLoopTree *loop, Node* ctrl, 595 int scale, Node* offset, 596 Node* init, Node* limit, Node* stride, 597 Node* range, bool upper) { 598 stringStream* predString = NULL; 599 if (TraceLoopPredicate) { 600 predString = new stringStream(); 601 predString->print("rc_predicate "); 602 } 603 604 Node* max_idx_expr = init; 605 int stride_con = stride->get_int(); 606 if ((stride_con > 0) == (scale > 0) == upper) { 607 if (LoopLimitCheck) { 608 // With LoopLimitCheck limit is not exact. 609 // Calculate exact limit here. 610 // Note, counted loop's test is '<' or '>'. 611 limit = exact_limit(loop); 612 max_idx_expr = new (C) SubINode(limit, stride); 613 register_new_node(max_idx_expr, ctrl); 614 if (TraceLoopPredicate) predString->print("(limit - stride) "); 615 } else { 616 max_idx_expr = new (C) SubINode(limit, stride); 617 register_new_node(max_idx_expr, ctrl); 618 if (TraceLoopPredicate) predString->print("(limit - stride) "); 619 } 620 } else { 621 if (TraceLoopPredicate) predString->print("init "); 622 } 623 624 if (scale != 1) { 625 ConNode* con_scale = _igvn.intcon(scale); 626 max_idx_expr = new (C) MulINode(max_idx_expr, con_scale); 627 register_new_node(max_idx_expr, ctrl); 628 if (TraceLoopPredicate) predString->print("* %d ", scale); 629 } 630 631 if (offset && (!offset->is_Con() || offset->get_int() != 0)){ 632 max_idx_expr = new (C) AddINode(max_idx_expr, offset); 633 register_new_node(max_idx_expr, ctrl); 634 if (TraceLoopPredicate) 635 if (offset->is_Con()) predString->print("+ %d ", offset->get_int()); 636 else predString->print("+ offset "); 637 } 638 639 CmpUNode* cmp = new (C) CmpUNode(max_idx_expr, range); 640 register_new_node(cmp, ctrl); 641 BoolNode* bol = new (C) BoolNode(cmp, BoolTest::lt); 642 register_new_node(bol, ctrl); 643 644 if (TraceLoopPredicate) { 645 predString->print_cr("<u range"); 646 tty->print("%s", predString->as_string()); 647 } 648 return bol; 649 } 650 651 //------------------------------ loop_predication_impl-------------------------- 652 // Insert loop predicates for null checks and range checks 653 bool PhaseIdealLoop::loop_predication_impl(IdealLoopTree *loop) { 654 if (!UseLoopPredicate) return false; 655 656 if (!loop->_head->is_Loop()) { 657 // Could be a simple region when irreducible loops are present. 658 return false; 659 } 660 LoopNode* head = loop->_head->as_Loop(); 661 662 if (head->unique_ctrl_out()->Opcode() == Op_NeverBranch) { 663 // do nothing for infinite loops 664 return false; 665 } 666 667 CountedLoopNode *cl = NULL; 668 if (head->is_valid_counted_loop()) { 669 cl = head->as_CountedLoop(); 670 // do nothing for iteration-splitted loops 671 if (!cl->is_normal_loop()) return false; 672 // Avoid RCE if Counted loop's test is '!='. 673 BoolTest::mask bt = cl->loopexit()->test_trip(); 674 if (bt != BoolTest::lt && bt != BoolTest::gt) 675 cl = NULL; 676 } 677 678 Node* entry = head->in(LoopNode::EntryControl); 679 ProjNode *predicate_proj = NULL; 680 // Loop limit check predicate should be near the loop. 681 if (LoopLimitCheck) { 682 predicate_proj = find_predicate_insertion_point(entry, Deoptimization::Reason_loop_limit_check); 683 if (predicate_proj != NULL) 684 entry = predicate_proj->in(0)->in(0); 685 } 686 687 predicate_proj = find_predicate_insertion_point(entry, Deoptimization::Reason_predicate); 688 if (!predicate_proj) { 689 #ifndef PRODUCT 690 if (TraceLoopPredicate) { 691 tty->print("missing predicate:"); 692 loop->dump_head(); 693 head->dump(1); 694 } 695 #endif 696 return false; 697 } 698 ConNode* zero = _igvn.intcon(0); 699 set_ctrl(zero, C->root()); 700 701 ResourceArea *area = Thread::current()->resource_area(); 702 Invariance invar(area, loop); 703 704 // Create list of if-projs such that a newer proj dominates all older 705 // projs in the list, and they all dominate loop->tail() 706 Node_List if_proj_list(area); 707 Node *current_proj = loop->tail(); //start from tail 708 while (current_proj != head) { 709 if (loop == get_loop(current_proj) && // still in the loop ? 710 current_proj->is_Proj() && // is a projection ? 711 current_proj->in(0)->Opcode() == Op_If) { // is a if projection ? 712 if_proj_list.push(current_proj); 713 } 714 current_proj = idom(current_proj); 715 } 716 717 bool hoisted = false; // true if at least one proj is promoted 718 while (if_proj_list.size() > 0) { 719 // Following are changed to nonnull when a predicate can be hoisted 720 ProjNode* new_predicate_proj = NULL; 721 722 ProjNode* proj = if_proj_list.pop()->as_Proj(); 723 IfNode* iff = proj->in(0)->as_If(); 724 725 if (!proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none)) { 726 if (loop->is_loop_exit(iff)) { 727 // stop processing the remaining projs in the list because the execution of them 728 // depends on the condition of "iff" (iff->in(1)). 729 break; 730 } else { 731 // Both arms are inside the loop. There are two cases: 732 // (1) there is one backward branch. In this case, any remaining proj 733 // in the if_proj list post-dominates "iff". So, the condition of "iff" 734 // does not determine the execution the remining projs directly, and we 735 // can safely continue. 736 // (2) both arms are forwarded, i.e. a diamond shape. In this case, "proj" 737 // does not dominate loop->tail(), so it can not be in the if_proj list. 738 continue; 739 } 740 } 741 742 Node* test = iff->in(1); 743 if (!test->is_Bool()){ //Conv2B, ... 744 continue; 745 } 746 BoolNode* bol = test->as_Bool(); 747 if (invar.is_invariant(bol)) { 748 // Invariant test 749 new_predicate_proj = create_new_if_for_predicate(predicate_proj, NULL, 750 Deoptimization::Reason_predicate); 751 Node* ctrl = new_predicate_proj->in(0)->as_If()->in(0); 752 BoolNode* new_predicate_bol = invar.clone(bol, ctrl)->as_Bool(); 753 754 // Negate test if necessary 755 bool negated = false; 756 if (proj->_con != predicate_proj->_con) { 757 new_predicate_bol = new (C) BoolNode(new_predicate_bol->in(1), new_predicate_bol->_test.negate()); 758 register_new_node(new_predicate_bol, ctrl); 759 negated = true; 760 } 761 IfNode* new_predicate_iff = new_predicate_proj->in(0)->as_If(); 762 _igvn.hash_delete(new_predicate_iff); 763 new_predicate_iff->set_req(1, new_predicate_bol); 764 #ifndef PRODUCT 765 if (TraceLoopPredicate) { 766 tty->print("Predicate invariant if%s: %d ", negated ? " negated" : "", new_predicate_iff->_idx); 767 loop->dump_head(); 768 } else if (TraceLoopOpts) { 769 tty->print("Predicate IC "); 770 loop->dump_head(); 771 } 772 #endif 773 } else if ((cl != NULL) && (proj->_con == predicate_proj->_con) && 774 loop->is_range_check_if(iff, this, invar)) { 775 776 // Range check for counted loops 777 const Node* cmp = bol->in(1)->as_Cmp(); 778 Node* idx = cmp->in(1); 779 assert(!invar.is_invariant(idx), "index is variant"); 780 Node* rng = cmp->in(2); 781 assert(rng->Opcode() == Op_LoadRange || _igvn.type(rng)->is_int() >= 0, "must be"); 782 assert(invar.is_invariant(rng), "range must be invariant"); 783 int scale = 1; 784 Node* offset = zero; 785 bool ok = is_scaled_iv_plus_offset(idx, cl->phi(), &scale, &offset); 786 assert(ok, "must be index expression"); 787 788 Node* init = cl->init_trip(); 789 Node* limit = cl->limit(); 790 Node* stride = cl->stride(); 791 792 // Build if's for the upper and lower bound tests. The 793 // lower_bound test will dominate the upper bound test and all 794 // cloned or created nodes will use the lower bound test as 795 // their declared control. 796 ProjNode* lower_bound_proj = create_new_if_for_predicate(predicate_proj, NULL, Deoptimization::Reason_predicate); 797 ProjNode* upper_bound_proj = create_new_if_for_predicate(predicate_proj, NULL, Deoptimization::Reason_predicate); 798 assert(upper_bound_proj->in(0)->as_If()->in(0) == lower_bound_proj, "should dominate"); 799 Node *ctrl = lower_bound_proj->in(0)->as_If()->in(0); 800 801 // Perform cloning to keep Invariance state correct since the 802 // late schedule will place invariant things in the loop. 803 rng = invar.clone(rng, ctrl); 804 if (offset && offset != zero) { 805 assert(invar.is_invariant(offset), "offset must be loop invariant"); 806 offset = invar.clone(offset, ctrl); 807 } 808 809 // Test the lower bound 810 Node* lower_bound_bol = rc_predicate(loop, ctrl, scale, offset, init, limit, stride, rng, false); 811 IfNode* lower_bound_iff = lower_bound_proj->in(0)->as_If(); 812 _igvn.hash_delete(lower_bound_iff); 813 lower_bound_iff->set_req(1, lower_bound_bol); 814 if (TraceLoopPredicate) tty->print_cr("lower bound check if: %d", lower_bound_iff->_idx); 815 816 // Test the upper bound 817 Node* upper_bound_bol = rc_predicate(loop, lower_bound_proj, scale, offset, init, limit, stride, rng, true); 818 IfNode* upper_bound_iff = upper_bound_proj->in(0)->as_If(); 819 _igvn.hash_delete(upper_bound_iff); 820 upper_bound_iff->set_req(1, upper_bound_bol); 821 if (TraceLoopPredicate) tty->print_cr("upper bound check if: %d", lower_bound_iff->_idx); 822 823 // Fall through into rest of the clean up code which will move 824 // any dependent nodes onto the upper bound test. 825 new_predicate_proj = upper_bound_proj; 826 827 #ifndef PRODUCT 828 if (TraceLoopOpts && !TraceLoopPredicate) { 829 tty->print("Predicate RC "); 830 loop->dump_head(); 831 } 832 #endif 833 } else { 834 // Loop variant check (for example, range check in non-counted loop) 835 // with uncommon trap. 836 continue; 837 } 838 assert(new_predicate_proj != NULL, "sanity"); 839 // Success - attach condition (new_predicate_bol) to predicate if 840 invar.map_ctrl(proj, new_predicate_proj); // so that invariance test can be appropriate 841 842 // Eliminate the old If in the loop body 843 dominated_by( new_predicate_proj, iff, proj->_con != new_predicate_proj->_con ); 844 845 hoisted = true; 846 C->set_major_progress(); 847 } // end while 848 849 #ifndef PRODUCT 850 // report that the loop predication has been actually performed 851 // for this loop 852 if (TraceLoopPredicate && hoisted) { 853 tty->print("Loop Predication Performed:"); 854 loop->dump_head(); 855 } 856 #endif 857 858 return hoisted; 859 } 860 861 //------------------------------loop_predication-------------------------------- 862 // driver routine for loop predication optimization 863 bool IdealLoopTree::loop_predication( PhaseIdealLoop *phase) { 864 bool hoisted = false; 865 // Recursively promote predicates 866 if (_child) { 867 hoisted = _child->loop_predication( phase); 868 } 869 870 // self 871 if (!_irreducible && !tail()->is_top()) { 872 hoisted |= phase->loop_predication_impl(this); 873 } 874 875 if (_next) { //sibling 876 hoisted |= _next->loop_predication( phase); 877 } 878 879 return hoisted; 880 }