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