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