1 #ifdef USE_PRAGMA_IDENT_SRC 2 #pragma ident "@(#)loopnode.cpp 1.262 07/10/23 13:12:50 JVM" 3 #endif 4 /* 5 * Copyright 1998-2007 Sun Microsystems, Inc. All Rights Reserved. 6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 7 * 8 * This code is free software; you can redistribute it and/or modify it 9 * under the terms of the GNU General Public License version 2 only, as 10 * published by the Free Software Foundation. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 23 * CA 95054 USA or visit www.sun.com if you need additional information or 24 * have any questions. 25 * 26 */ 27 28 #include "incls/_precompiled.incl" 29 #include "incls/_loopnode.cpp.incl" 30 31 //============================================================================= 32 //------------------------------is_loop_iv------------------------------------- 33 // Determine if a node is Counted loop induction variable. 34 // The method is declared in node.hpp. 35 const Node* Node::is_loop_iv() const { 36 if (this->is_Phi() && !this->as_Phi()->is_copy() && 37 this->as_Phi()->region()->is_CountedLoop() && 38 this->as_Phi()->region()->as_CountedLoop()->phi() == this) { 39 return this; 40 } else { 41 return NULL; 42 } 43 } 44 45 //============================================================================= 46 //------------------------------dump_spec-------------------------------------- 47 // Dump special per-node info 48 #ifndef PRODUCT 49 void LoopNode::dump_spec(outputStream *st) const { 50 if( is_inner_loop () ) st->print( "inner " ); 51 if( is_partial_peel_loop () ) st->print( "partial_peel " ); 52 if( partial_peel_has_failed () ) st->print( "partial_peel_failed " ); 53 } 54 #endif 55 56 //------------------------------get_early_ctrl--------------------------------- 57 // Compute earliest legal control 58 Node *PhaseIdealLoop::get_early_ctrl( Node *n ) { 59 assert( !n->is_Phi() && !n->is_CFG(), "this code only handles data nodes" ); 60 uint i; 61 Node *early; 62 if( n->in(0) ) { 63 early = n->in(0); 64 if( !early->is_CFG() ) // Might be a non-CFG multi-def 65 early = get_ctrl(early); // So treat input as a straight data input 66 i = 1; 67 } else { 68 early = get_ctrl(n->in(1)); 69 i = 2; 70 } 71 uint e_d = dom_depth(early); 72 assert( early, "" ); 73 for( ; i < n->req(); i++ ) { 74 Node *cin = get_ctrl(n->in(i)); 75 assert( cin, "" ); 76 // Keep deepest dominator depth 77 uint c_d = dom_depth(cin); 78 if( c_d > e_d ) { // Deeper guy? 79 early = cin; // Keep deepest found so far 80 e_d = c_d; 81 } else if( c_d == e_d && // Same depth? 82 early != cin ) { // If not equal, must use slower algorithm 83 // If same depth but not equal, one _must_ dominate the other 84 // and we want the deeper (i.e., dominated) guy. 85 Node *n1 = early; 86 Node *n2 = cin; 87 while( 1 ) { 88 n1 = idom(n1); // Walk up until break cycle 89 n2 = idom(n2); 90 if( n1 == cin || // Walked early up to cin 91 dom_depth(n2) < c_d ) 92 break; // early is deeper; keep him 93 if( n2 == early || // Walked cin up to early 94 dom_depth(n1) < c_d ) { 95 early = cin; // cin is deeper; keep him 96 break; 97 } 98 } 99 e_d = dom_depth(early); // Reset depth register cache 100 } 101 } 102 103 // Return earliest legal location 104 assert(early == find_non_split_ctrl(early), "unexpected early control"); 105 106 return early; 107 } 108 109 //------------------------------set_early_ctrl--------------------------------- 110 // Set earliest legal control 111 void PhaseIdealLoop::set_early_ctrl( Node *n ) { 112 Node *early = get_early_ctrl(n); 113 114 // Record earliest legal location 115 set_ctrl(n, early); 116 } 117 118 //------------------------------set_subtree_ctrl------------------------------- 119 // set missing _ctrl entries on new nodes 120 void PhaseIdealLoop::set_subtree_ctrl( Node *n ) { 121 // Already set? Get out. 122 if( _nodes[n->_idx] ) return; 123 // Recursively set _nodes array to indicate where the Node goes 124 uint i; 125 for( i = 0; i < n->req(); ++i ) { 126 Node *m = n->in(i); 127 if( m && m != C->root() ) 128 set_subtree_ctrl( m ); 129 } 130 131 // Fixup self 132 set_early_ctrl( n ); 133 } 134 135 //------------------------------is_counted_loop-------------------------------- 136 Node *PhaseIdealLoop::is_counted_loop( Node *x, IdealLoopTree *loop ) { 137 PhaseGVN *gvn = &_igvn; 138 139 // Counted loop head must be a good RegionNode with only 3 not NULL 140 // control input edges: Self, Entry, LoopBack. 141 if ( x->in(LoopNode::Self) == NULL || x->req() != 3 ) 142 return NULL; 143 144 Node *init_control = x->in(LoopNode::EntryControl); 145 Node *back_control = x->in(LoopNode::LoopBackControl); 146 if( init_control == NULL || back_control == NULL ) // Partially dead 147 return NULL; 148 // Must also check for TOP when looking for a dead loop 149 if( init_control->is_top() || back_control->is_top() ) 150 return NULL; 151 152 // Allow funny placement of Safepoint 153 if( back_control->Opcode() == Op_SafePoint ) 154 back_control = back_control->in(TypeFunc::Control); 155 156 // Controlling test for loop 157 Node *iftrue = back_control; 158 uint iftrue_op = iftrue->Opcode(); 159 if( iftrue_op != Op_IfTrue && 160 iftrue_op != Op_IfFalse ) 161 // I have a weird back-control. Probably the loop-exit test is in 162 // the middle of the loop and I am looking at some trailing control-flow 163 // merge point. To fix this I would have to partially peel the loop. 164 return NULL; // Obscure back-control 165 166 // Get boolean guarding loop-back test 167 Node *iff = iftrue->in(0); 168 if( get_loop(iff) != loop || !iff->in(1)->is_Bool() ) return NULL; 169 BoolNode *test = iff->in(1)->as_Bool(); 170 BoolTest::mask bt = test->_test._test; 171 float cl_prob = iff->as_If()->_prob; 172 if( iftrue_op == Op_IfFalse ) { 173 bt = BoolTest(bt).negate(); 174 cl_prob = 1.0 - cl_prob; 175 } 176 // Get backedge compare 177 Node *cmp = test->in(1); 178 int cmp_op = cmp->Opcode(); 179 if( cmp_op != Op_CmpI ) 180 return NULL; // Avoid pointer & float compares 181 182 // Find the trip-counter increment & limit. Limit must be loop invariant. 183 Node *incr = cmp->in(1); 184 Node *limit = cmp->in(2); 185 186 // --------- 187 // need 'loop()' test to tell if limit is loop invariant 188 // --------- 189 190 if( !is_member( loop, get_ctrl(incr) ) ) { // Swapped trip counter and limit? 191 Node *tmp = incr; // Then reverse order into the CmpI 192 incr = limit; 193 limit = tmp; 194 bt = BoolTest(bt).commute(); // And commute the exit test 195 } 196 if( is_member( loop, get_ctrl(limit) ) ) // Limit must loop-invariant 197 return NULL; 198 199 // Trip-counter increment must be commutative & associative. 200 uint incr_op = incr->Opcode(); 201 if( incr_op == Op_Phi && incr->req() == 3 ) { 202 incr = incr->in(2); // Assume incr is on backedge of Phi 203 incr_op = incr->Opcode(); 204 } 205 Node* trunc1 = NULL; 206 Node* trunc2 = NULL; 207 const TypeInt* iv_trunc_t = NULL; 208 if (!(incr = CountedLoopNode::match_incr_with_optional_truncation(incr, &trunc1, &trunc2, &iv_trunc_t))) { 209 return NULL; // Funny increment opcode 210 } 211 212 // Get merge point 213 Node *xphi = incr->in(1); 214 Node *stride = incr->in(2); 215 if( !stride->is_Con() ) { // Oops, swap these 216 if( !xphi->is_Con() ) // Is the other guy a constant? 217 return NULL; // Nope, unknown stride, bail out 218 Node *tmp = xphi; // 'incr' is commutative, so ok to swap 219 xphi = stride; 220 stride = tmp; 221 } 222 //if( loop(xphi) != l) return NULL;// Merge point is in inner loop?? 223 if( !xphi->is_Phi() ) return NULL; // Too much math on the trip counter 224 PhiNode *phi = xphi->as_Phi(); 225 226 // Stride must be constant 227 const Type *stride_t = stride->bottom_type(); 228 int stride_con = stride_t->is_int()->get_con(); 229 assert( stride_con, "missed some peephole opt" ); 230 231 // Phi must be of loop header; backedge must wrap to increment 232 if( phi->region() != x ) return NULL; 233 if( trunc1 == NULL && phi->in(LoopNode::LoopBackControl) != incr || 234 trunc1 != NULL && phi->in(LoopNode::LoopBackControl) != trunc1 ) { 235 return NULL; 236 } 237 Node *init_trip = phi->in(LoopNode::EntryControl); 238 //if (!init_trip->is_Con()) return NULL; // avoid rolling over MAXINT/MININT 239 240 // If iv trunc type is smaller than int, check for possible wrap. 241 if (!TypeInt::INT->higher_equal(iv_trunc_t)) { 242 assert(trunc1 != NULL, "must have found some truncation"); 243 244 // Get a better type for the phi (filtered thru if's) 245 const TypeInt* phi_ft = filtered_type(phi); 246 247 // Can iv take on a value that will wrap? 248 // 249 // Ensure iv's limit is not within "stride" of the wrap value. 250 // 251 // Example for "short" type 252 // Truncation ensures value is in the range -32768..32767 (iv_trunc_t) 253 // If the stride is +10, then the last value of the induction 254 // variable before the increment (phi_ft->_hi) must be 255 // <= 32767 - 10 and (phi_ft->_lo) must be >= -32768 to 256 // ensure no truncation occurs after the increment. 257 258 if (stride_con > 0) { 259 if (iv_trunc_t->_hi - phi_ft->_hi < stride_con || 260 iv_trunc_t->_lo > phi_ft->_lo) { 261 return NULL; // truncation may occur 262 } 263 } else if (stride_con < 0) { 264 if (iv_trunc_t->_lo - phi_ft->_lo > stride_con || 265 iv_trunc_t->_hi < phi_ft->_hi) { 266 return NULL; // truncation may occur 267 } 268 } 269 // No possibility of wrap so truncation can be discarded 270 // Promote iv type to Int 271 } else { 272 assert(trunc1 == NULL && trunc2 == NULL, "no truncation for int"); 273 } 274 275 // ================================================= 276 // ---- SUCCESS! Found A Trip-Counted Loop! ----- 277 // 278 // Canonicalize the condition on the test. If we can exactly determine 279 // the trip-counter exit value, then set limit to that value and use 280 // a '!=' test. Otherwise use conditon '<' for count-up loops and 281 // '>' for count-down loops. If the condition is inverted and we will 282 // be rolling through MININT to MAXINT, then bail out. 283 284 C->print_method("Before CountedLoop", 3); 285 286 // Check for SafePoint on backedge and remove 287 Node *sfpt = x->in(LoopNode::LoopBackControl); 288 if( sfpt->Opcode() == Op_SafePoint && is_deleteable_safept(sfpt)) { 289 lazy_replace( sfpt, iftrue ); 290 loop->_tail = iftrue; 291 } 292 293 294 // If compare points to incr, we are ok. Otherwise the compare 295 // can directly point to the phi; in this case adjust the compare so that 296 // it points to the incr by adusting the limit. 297 if( cmp->in(1) == phi || cmp->in(2) == phi ) 298 limit = gvn->transform(new (C, 3) AddINode(limit,stride)); 299 300 // trip-count for +-tive stride should be: (limit - init_trip + stride - 1)/stride. 301 // Final value for iterator should be: trip_count * stride + init_trip. 302 const Type *limit_t = limit->bottom_type(); 303 const Type *init_t = init_trip->bottom_type(); 304 Node *one_p = gvn->intcon( 1); 305 Node *one_m = gvn->intcon(-1); 306 307 Node *trip_count = NULL; 308 Node *hook = new (C, 6) Node(6); 309 switch( bt ) { 310 case BoolTest::eq: 311 return NULL; // Bail out, but this loop trips at most twice! 312 case BoolTest::ne: // Ahh, the case we desire 313 if( stride_con == 1 ) 314 trip_count = gvn->transform(new (C, 3) SubINode(limit,init_trip)); 315 else if( stride_con == -1 ) 316 trip_count = gvn->transform(new (C, 3) SubINode(init_trip,limit)); 317 else 318 return NULL; // Odd stride; must prove we hit limit exactly 319 set_subtree_ctrl( trip_count ); 320 //_loop.map(trip_count->_idx,loop(limit)); 321 break; 322 case BoolTest::le: // Maybe convert to '<' case 323 limit = gvn->transform(new (C, 3) AddINode(limit,one_p)); 324 set_subtree_ctrl( limit ); 325 hook->init_req(4, limit); 326 327 bt = BoolTest::lt; 328 // Make the new limit be in the same loop nest as the old limit 329 //_loop.map(limit->_idx,limit_loop); 330 // Fall into next case 331 case BoolTest::lt: { // Maybe convert to '!=' case 332 if( stride_con < 0 ) return NULL; // Count down loop rolls through MAXINT 333 Node *range = gvn->transform(new (C, 3) SubINode(limit,init_trip)); 334 set_subtree_ctrl( range ); 335 hook->init_req(0, range); 336 337 Node *bias = gvn->transform(new (C, 3) AddINode(range,stride)); 338 set_subtree_ctrl( bias ); 339 hook->init_req(1, bias); 340 341 Node *bias1 = gvn->transform(new (C, 3) AddINode(bias,one_m)); 342 set_subtree_ctrl( bias1 ); 343 hook->init_req(2, bias1); 344 345 trip_count = gvn->transform(new (C, 3) DivINode(0,bias1,stride)); 346 set_subtree_ctrl( trip_count ); 347 hook->init_req(3, trip_count); 348 break; 349 } 350 351 case BoolTest::ge: // Maybe convert to '>' case 352 limit = gvn->transform(new (C, 3) AddINode(limit,one_m)); 353 set_subtree_ctrl( limit ); 354 hook->init_req(4 ,limit); 355 356 bt = BoolTest::gt; 357 // Make the new limit be in the same loop nest as the old limit 358 //_loop.map(limit->_idx,limit_loop); 359 // Fall into next case 360 case BoolTest::gt: { // Maybe convert to '!=' case 361 if( stride_con > 0 ) return NULL; // count up loop rolls through MININT 362 Node *range = gvn->transform(new (C, 3) SubINode(limit,init_trip)); 363 set_subtree_ctrl( range ); 364 hook->init_req(0, range); 365 366 Node *bias = gvn->transform(new (C, 3) AddINode(range,stride)); 367 set_subtree_ctrl( bias ); 368 hook->init_req(1, bias); 369 370 Node *bias1 = gvn->transform(new (C, 3) AddINode(bias,one_p)); 371 set_subtree_ctrl( bias1 ); 372 hook->init_req(2, bias1); 373 374 trip_count = gvn->transform(new (C, 3) DivINode(0,bias1,stride)); 375 set_subtree_ctrl( trip_count ); 376 hook->init_req(3, trip_count); 377 break; 378 } 379 } 380 381 Node *span = gvn->transform(new (C, 3) MulINode(trip_count,stride)); 382 set_subtree_ctrl( span ); 383 hook->init_req(5, span); 384 385 limit = gvn->transform(new (C, 3) AddINode(span,init_trip)); 386 set_subtree_ctrl( limit ); 387 388 // Build a canonical trip test. 389 // Clone code, as old values may be in use. 390 incr = incr->clone(); 391 incr->set_req(1,phi); 392 incr->set_req(2,stride); 393 incr = _igvn.register_new_node_with_optimizer(incr); 394 set_early_ctrl( incr ); 395 _igvn.hash_delete(phi); 396 phi->set_req_X( LoopNode::LoopBackControl, incr, &_igvn ); 397 398 // If phi type is more restrictive than Int, raise to 399 // Int to prevent (almost) infinite recursion in igvn 400 // which can only handle integer types for constants or minint..maxint. 401 if (!TypeInt::INT->higher_equal(phi->bottom_type())) { 402 Node* nphi = PhiNode::make(phi->in(0), phi->in(LoopNode::EntryControl), TypeInt::INT); 403 nphi->set_req(LoopNode::LoopBackControl, phi->in(LoopNode::LoopBackControl)); 404 nphi = _igvn.register_new_node_with_optimizer(nphi); 405 set_ctrl(nphi, get_ctrl(phi)); 406 _igvn.subsume_node(phi, nphi); 407 phi = nphi->as_Phi(); 408 } 409 cmp = cmp->clone(); 410 cmp->set_req(1,incr); 411 cmp->set_req(2,limit); 412 cmp = _igvn.register_new_node_with_optimizer(cmp); 413 set_ctrl(cmp, iff->in(0)); 414 415 Node *tmp = test->clone(); 416 assert( tmp->is_Bool(), "" ); 417 test = (BoolNode*)tmp; 418 (*(BoolTest*)&test->_test)._test = bt; //BoolTest::ne; 419 test->set_req(1,cmp); 420 _igvn.register_new_node_with_optimizer(test); 421 set_ctrl(test, iff->in(0)); 422 // If the exit test is dead, STOP! 423 if( test == NULL ) return NULL; 424 _igvn.hash_delete(iff); 425 iff->set_req_X( 1, test, &_igvn ); 426 427 // Replace the old IfNode with a new LoopEndNode 428 Node *lex = _igvn.register_new_node_with_optimizer(new (C, 2) CountedLoopEndNode( iff->in(0), iff->in(1), cl_prob, iff->as_If()->_fcnt )); 429 IfNode *le = lex->as_If(); 430 uint dd = dom_depth(iff); 431 set_idom(le, le->in(0), dd); // Update dominance for loop exit 432 set_loop(le, loop); 433 434 // Get the loop-exit control 435 Node *if_f = iff->as_If()->proj_out(!(iftrue_op == Op_IfTrue)); 436 437 // Need to swap loop-exit and loop-back control? 438 if( iftrue_op == Op_IfFalse ) { 439 Node *ift2=_igvn.register_new_node_with_optimizer(new (C, 1) IfTrueNode (le)); 440 Node *iff2=_igvn.register_new_node_with_optimizer(new (C, 1) IfFalseNode(le)); 441 442 loop->_tail = back_control = ift2; 443 set_loop(ift2, loop); 444 set_loop(iff2, get_loop(if_f)); 445 446 // Lazy update of 'get_ctrl' mechanism. 447 lazy_replace_proj( if_f , iff2 ); 448 lazy_replace_proj( iftrue, ift2 ); 449 450 // Swap names 451 if_f = iff2; 452 iftrue = ift2; 453 } else { 454 _igvn.hash_delete(if_f ); 455 _igvn.hash_delete(iftrue); 456 if_f ->set_req_X( 0, le, &_igvn ); 457 iftrue->set_req_X( 0, le, &_igvn ); 458 } 459 460 set_idom(iftrue, le, dd+1); 461 set_idom(if_f, le, dd+1); 462 463 // Now setup a new CountedLoopNode to replace the existing LoopNode 464 CountedLoopNode *l = new (C, 3) CountedLoopNode(init_control, back_control); 465 // The following assert is approximately true, and defines the intention 466 // of can_be_counted_loop. It fails, however, because phase->type 467 // is not yet initialized for this loop and its parts. 468 //assert(l->can_be_counted_loop(this), "sanity"); 469 _igvn.register_new_node_with_optimizer(l); 470 set_loop(l, loop); 471 loop->_head = l; 472 // Fix all data nodes placed at the old loop head. 473 // Uses the lazy-update mechanism of 'get_ctrl'. 474 lazy_replace( x, l ); 475 set_idom(l, init_control, dom_depth(x)); 476 477 // Check for immediately preceeding SafePoint and remove 478 Node *sfpt2 = le->in(0); 479 if( sfpt2->Opcode() == Op_SafePoint && is_deleteable_safept(sfpt2)) 480 lazy_replace( sfpt2, sfpt2->in(TypeFunc::Control)); 481 482 // Free up intermediate goo 483 _igvn.remove_dead_node(hook); 484 485 C->print_method("After CountedLoop", 3); 486 487 // Return trip counter 488 return trip_count; 489 } 490 491 492 //------------------------------Ideal------------------------------------------ 493 // Return a node which is more "ideal" than the current node. 494 // Attempt to convert into a counted-loop. 495 Node *LoopNode::Ideal(PhaseGVN *phase, bool can_reshape) { 496 if (!can_be_counted_loop(phase)) { 497 phase->C->set_major_progress(); 498 } 499 return RegionNode::Ideal(phase, can_reshape); 500 } 501 502 503 //============================================================================= 504 //------------------------------Ideal------------------------------------------ 505 // Return a node which is more "ideal" than the current node. 506 // Attempt to convert into a counted-loop. 507 Node *CountedLoopNode::Ideal(PhaseGVN *phase, bool can_reshape) { 508 return RegionNode::Ideal(phase, can_reshape); 509 } 510 511 //------------------------------dump_spec-------------------------------------- 512 // Dump special per-node info 513 #ifndef PRODUCT 514 void CountedLoopNode::dump_spec(outputStream *st) const { 515 LoopNode::dump_spec(st); 516 if( stride_is_con() ) { 517 st->print("stride: %d ",stride_con()); 518 } else { 519 st->print("stride: not constant "); 520 } 521 if( is_pre_loop () ) st->print("pre of N%d" , _main_idx ); 522 if( is_main_loop() ) st->print("main of N%d", _idx ); 523 if( is_post_loop() ) st->print("post of N%d", _main_idx ); 524 } 525 #endif 526 527 //============================================================================= 528 int CountedLoopEndNode::stride_con() const { 529 return stride()->bottom_type()->is_int()->get_con(); 530 } 531 532 533 //----------------------match_incr_with_optional_truncation-------------------- 534 // Match increment with optional truncation: 535 // CHAR: (i+1)&0x7fff, BYTE: ((i+1)<<8)>>8, or SHORT: ((i+1)<<16)>>16 536 // Return NULL for failure. Success returns the increment node. 537 Node* CountedLoopNode::match_incr_with_optional_truncation( 538 Node* expr, Node** trunc1, Node** trunc2, const TypeInt** trunc_type) { 539 // Quick cutouts: 540 if (expr == NULL || expr->req() != 3) return false; 541 542 Node *t1 = NULL; 543 Node *t2 = NULL; 544 const TypeInt* trunc_t = TypeInt::INT; 545 Node* n1 = expr; 546 int n1op = n1->Opcode(); 547 548 // Try to strip (n1 & M) or (n1 << N >> N) from n1. 549 if (n1op == Op_AndI && 550 n1->in(2)->is_Con() && 551 n1->in(2)->bottom_type()->is_int()->get_con() == 0x7fff) { 552 // %%% This check should match any mask of 2**K-1. 553 t1 = n1; 554 n1 = t1->in(1); 555 n1op = n1->Opcode(); 556 trunc_t = TypeInt::CHAR; 557 } else if (n1op == Op_RShiftI && 558 n1->in(1) != NULL && 559 n1->in(1)->Opcode() == Op_LShiftI && 560 n1->in(2) == n1->in(1)->in(2) && 561 n1->in(2)->is_Con()) { 562 jint shift = n1->in(2)->bottom_type()->is_int()->get_con(); 563 // %%% This check should match any shift in [1..31]. 564 if (shift == 16 || shift == 8) { 565 t1 = n1; 566 t2 = t1->in(1); 567 n1 = t2->in(1); 568 n1op = n1->Opcode(); 569 if (shift == 16) { 570 trunc_t = TypeInt::SHORT; 571 } else if (shift == 8) { 572 trunc_t = TypeInt::BYTE; 573 } 574 } 575 } 576 577 // If (maybe after stripping) it is an AddI, we won: 578 if (n1op == Op_AddI) { 579 *trunc1 = t1; 580 *trunc2 = t2; 581 *trunc_type = trunc_t; 582 return n1; 583 } 584 585 // failed 586 return NULL; 587 } 588 589 590 //------------------------------filtered_type-------------------------------- 591 // Return a type based on condition control flow 592 // A successful return will be a type that is restricted due 593 // to a series of dominating if-tests, such as: 594 // if (i < 10) { 595 // if (i > 0) { 596 // here: "i" type is [1..10) 597 // } 598 // } 599 // or a control flow merge 600 // if (i < 10) { 601 // do { 602 // phi( , ) -- at top of loop type is [min_int..10) 603 // i = ? 604 // } while ( i < 10) 605 // 606 const TypeInt* PhaseIdealLoop::filtered_type( Node *n, Node* n_ctrl) { 607 assert(n && n->bottom_type()->is_int(), "must be int"); 608 const TypeInt* filtered_t = NULL; 609 if (!n->is_Phi()) { 610 assert(n_ctrl != NULL || n_ctrl == C->top(), "valid control"); 611 filtered_t = filtered_type_from_dominators(n, n_ctrl); 612 613 } else { 614 Node* phi = n->as_Phi(); 615 Node* region = phi->in(0); 616 assert(n_ctrl == NULL || n_ctrl == region, "ctrl parameter must be region"); 617 if (region && region != C->top()) { 618 for (uint i = 1; i < phi->req(); i++) { 619 Node* val = phi->in(i); 620 Node* use_c = region->in(i); 621 const TypeInt* val_t = filtered_type_from_dominators(val, use_c); 622 if (val_t != NULL) { 623 if (filtered_t == NULL) { 624 filtered_t = val_t; 625 } else { 626 filtered_t = filtered_t->meet(val_t)->is_int(); 627 } 628 } 629 } 630 } 631 } 632 const TypeInt* n_t = _igvn.type(n)->is_int(); 633 if (filtered_t != NULL) { 634 n_t = n_t->join(filtered_t)->is_int(); 635 } 636 return n_t; 637 } 638 639 640 //------------------------------filtered_type_from_dominators-------------------------------- 641 // Return a possibly more restrictive type for val based on condition control flow of dominators 642 const TypeInt* PhaseIdealLoop::filtered_type_from_dominators( Node* val, Node *use_ctrl) { 643 if (val->is_Con()) { 644 return val->bottom_type()->is_int(); 645 } 646 uint if_limit = 10; // Max number of dominating if's visited 647 const TypeInt* rtn_t = NULL; 648 649 if (use_ctrl && use_ctrl != C->top()) { 650 Node* val_ctrl = get_ctrl(val); 651 uint val_dom_depth = dom_depth(val_ctrl); 652 Node* pred = use_ctrl; 653 uint if_cnt = 0; 654 while (if_cnt < if_limit) { 655 if ((pred->Opcode() == Op_IfTrue || pred->Opcode() == Op_IfFalse)) { 656 if_cnt++; 657 const TypeInt* if_t = IfNode::filtered_int_type(&_igvn, val, pred); 658 if (if_t != NULL) { 659 if (rtn_t == NULL) { 660 rtn_t = if_t; 661 } else { 662 rtn_t = rtn_t->join(if_t)->is_int(); 663 } 664 } 665 } 666 pred = idom(pred); 667 if (pred == NULL || pred == C->top()) { 668 break; 669 } 670 // Stop if going beyond definition block of val 671 if (dom_depth(pred) < val_dom_depth) { 672 break; 673 } 674 } 675 } 676 return rtn_t; 677 } 678 679 680 //------------------------------dump_spec-------------------------------------- 681 // Dump special per-node info 682 #ifndef PRODUCT 683 void CountedLoopEndNode::dump_spec(outputStream *st) const { 684 if( in(TestValue)->is_Bool() ) { 685 BoolTest bt( test_trip()); // Added this for g++. 686 687 st->print("["); 688 bt.dump_on(st); 689 st->print("]"); 690 } 691 st->print(" "); 692 IfNode::dump_spec(st); 693 } 694 #endif 695 696 //============================================================================= 697 //------------------------------is_member-------------------------------------- 698 // Is 'l' a member of 'this'? 699 int IdealLoopTree::is_member( const IdealLoopTree *l ) const { 700 while( l->_nest > _nest ) l = l->_parent; 701 return l == this; 702 } 703 704 //------------------------------set_nest--------------------------------------- 705 // Set loop tree nesting depth. Accumulate _has_call bits. 706 int IdealLoopTree::set_nest( uint depth ) { 707 _nest = depth; 708 int bits = _has_call; 709 if( _child ) bits |= _child->set_nest(depth+1); 710 if( bits ) _has_call = 1; 711 if( _next ) bits |= _next ->set_nest(depth ); 712 return bits; 713 } 714 715 //------------------------------split_fall_in---------------------------------- 716 // Split out multiple fall-in edges from the loop header. Move them to a 717 // private RegionNode before the loop. This becomes the loop landing pad. 718 void IdealLoopTree::split_fall_in( PhaseIdealLoop *phase, int fall_in_cnt ) { 719 PhaseIterGVN &igvn = phase->_igvn; 720 uint i; 721 722 // Make a new RegionNode to be the landing pad. 723 Node *landing_pad = new (phase->C, fall_in_cnt+1) RegionNode( fall_in_cnt+1 ); 724 phase->set_loop(landing_pad,_parent); 725 // Gather all the fall-in control paths into the landing pad 726 uint icnt = fall_in_cnt; 727 uint oreq = _head->req(); 728 for( i = oreq-1; i>0; i-- ) 729 if( !phase->is_member( this, _head->in(i) ) ) 730 landing_pad->set_req(icnt--,_head->in(i)); 731 732 // Peel off PhiNode edges as well 733 for (DUIterator_Fast jmax, j = _head->fast_outs(jmax); j < jmax; j++) { 734 Node *oj = _head->fast_out(j); 735 if( oj->is_Phi() ) { 736 PhiNode* old_phi = oj->as_Phi(); 737 assert( old_phi->region() == _head, "" ); 738 igvn.hash_delete(old_phi); // Yank from hash before hacking edges 739 Node *p = PhiNode::make_blank(landing_pad, old_phi); 740 uint icnt = fall_in_cnt; 741 for( i = oreq-1; i>0; i-- ) { 742 if( !phase->is_member( this, _head->in(i) ) ) { 743 p->init_req(icnt--, old_phi->in(i)); 744 // Go ahead and clean out old edges from old phi 745 old_phi->del_req(i); 746 } 747 } 748 // Search for CSE's here, because ZKM.jar does a lot of 749 // loop hackery and we need to be a little incremental 750 // with the CSE to avoid O(N^2) node blow-up. 751 Node *p2 = igvn.hash_find_insert(p); // Look for a CSE 752 if( p2 ) { // Found CSE 753 p->destruct(); // Recover useless new node 754 p = p2; // Use old node 755 } else { 756 igvn.register_new_node_with_optimizer(p, old_phi); 757 } 758 // Make old Phi refer to new Phi. 759 old_phi->add_req(p); 760 // Check for the special case of making the old phi useless and 761 // disappear it. In JavaGrande I have a case where this useless 762 // Phi is the loop limit and prevents recognizing a CountedLoop 763 // which in turn prevents removing an empty loop. 764 Node *id_old_phi = old_phi->Identity( &igvn ); 765 if( id_old_phi != old_phi ) { // Found a simple identity? 766 // Note that I cannot call 'subsume_node' here, because 767 // that will yank the edge from old_phi to the Region and 768 // I'm mid-iteration over the Region's uses. 769 for (DUIterator_Last imin, i = old_phi->last_outs(imin); i >= imin; ) { 770 Node* use = old_phi->last_out(i); 771 igvn.hash_delete(use); 772 igvn._worklist.push(use); 773 uint uses_found = 0; 774 for (uint j = 0; j < use->len(); j++) { 775 if (use->in(j) == old_phi) { 776 if (j < use->req()) use->set_req (j, id_old_phi); 777 else use->set_prec(j, id_old_phi); 778 uses_found++; 779 } 780 } 781 i -= uses_found; // we deleted 1 or more copies of this edge 782 } 783 } 784 igvn._worklist.push(old_phi); 785 } 786 } 787 // Finally clean out the fall-in edges from the RegionNode 788 for( i = oreq-1; i>0; i-- ) { 789 if( !phase->is_member( this, _head->in(i) ) ) { 790 _head->del_req(i); 791 } 792 } 793 // Transform landing pad 794 igvn.register_new_node_with_optimizer(landing_pad, _head); 795 // Insert landing pad into the header 796 _head->add_req(landing_pad); 797 } 798 799 //------------------------------split_outer_loop------------------------------- 800 // Split out the outermost loop from this shared header. 801 void IdealLoopTree::split_outer_loop( PhaseIdealLoop *phase ) { 802 PhaseIterGVN &igvn = phase->_igvn; 803 804 // Find index of outermost loop; it should also be my tail. 805 uint outer_idx = 1; 806 while( _head->in(outer_idx) != _tail ) outer_idx++; 807 808 // Make a LoopNode for the outermost loop. 809 Node *ctl = _head->in(LoopNode::EntryControl); 810 Node *outer = new (phase->C, 3) LoopNode( ctl, _head->in(outer_idx) ); 811 outer = igvn.register_new_node_with_optimizer(outer, _head); 812 phase->set_created_loop_node(); 813 // Outermost loop falls into '_head' loop 814 _head->set_req(LoopNode::EntryControl, outer); 815 _head->del_req(outer_idx); 816 // Split all the Phis up between '_head' loop and 'outer' loop. 817 for (DUIterator_Fast jmax, j = _head->fast_outs(jmax); j < jmax; j++) { 818 Node *out = _head->fast_out(j); 819 if( out->is_Phi() ) { 820 PhiNode *old_phi = out->as_Phi(); 821 assert( old_phi->region() == _head, "" ); 822 Node *phi = PhiNode::make_blank(outer, old_phi); 823 phi->init_req(LoopNode::EntryControl, old_phi->in(LoopNode::EntryControl)); 824 phi->init_req(LoopNode::LoopBackControl, old_phi->in(outer_idx)); 825 phi = igvn.register_new_node_with_optimizer(phi, old_phi); 826 // Make old Phi point to new Phi on the fall-in path 827 igvn.hash_delete(old_phi); 828 old_phi->set_req(LoopNode::EntryControl, phi); 829 old_phi->del_req(outer_idx); 830 igvn._worklist.push(old_phi); 831 } 832 } 833 834 // Use the new loop head instead of the old shared one 835 _head = outer; 836 phase->set_loop(_head, this); 837 } 838 839 //------------------------------fix_parent------------------------------------- 840 static void fix_parent( IdealLoopTree *loop, IdealLoopTree *parent ) { 841 loop->_parent = parent; 842 if( loop->_child ) fix_parent( loop->_child, loop ); 843 if( loop->_next ) fix_parent( loop->_next , parent ); 844 } 845 846 //------------------------------estimate_path_freq----------------------------- 847 static float estimate_path_freq( Node *n ) { 848 // Try to extract some path frequency info 849 IfNode *iff; 850 for( int i = 0; i < 50; i++ ) { // Skip through a bunch of uncommon tests 851 uint nop = n->Opcode(); 852 if( nop == Op_SafePoint ) { // Skip any safepoint 853 n = n->in(0); 854 continue; 855 } 856 if( nop == Op_CatchProj ) { // Get count from a prior call 857 // Assume call does not always throw exceptions: means the call-site 858 // count is also the frequency of the fall-through path. 859 assert( n->is_CatchProj(), "" ); 860 if( ((CatchProjNode*)n)->_con != CatchProjNode::fall_through_index ) 861 return 0.0f; // Assume call exception path is rare 862 Node *call = n->in(0)->in(0)->in(0); 863 assert( call->is_Call(), "expect a call here" ); 864 const JVMState *jvms = ((CallNode*)call)->jvms(); 865 ciMethodData* methodData = jvms->method()->method_data(); 866 if (!methodData->is_mature()) return 0.0f; // No call-site data 867 ciProfileData* data = methodData->bci_to_data(jvms->bci()); 868 if ((data == NULL) || !data->is_CounterData()) { 869 // no call profile available, try call's control input 870 n = n->in(0); 871 continue; 872 } 873 return data->as_CounterData()->count()/FreqCountInvocations; 874 } 875 // See if there's a gating IF test 876 Node *n_c = n->in(0); 877 if( !n_c->is_If() ) break; // No estimate available 878 iff = n_c->as_If(); 879 if( iff->_fcnt != COUNT_UNKNOWN ) // Have a valid count? 880 // Compute how much count comes on this path 881 return ((nop == Op_IfTrue) ? iff->_prob : 1.0f - iff->_prob) * iff->_fcnt; 882 // Have no count info. Skip dull uncommon-trap like branches. 883 if( (nop == Op_IfTrue && iff->_prob < PROB_LIKELY_MAG(5)) || 884 (nop == Op_IfFalse && iff->_prob > PROB_UNLIKELY_MAG(5)) ) 885 break; 886 // Skip through never-taken branch; look for a real loop exit. 887 n = iff->in(0); 888 } 889 return 0.0f; // No estimate available 890 } 891 892 //------------------------------merge_many_backedges--------------------------- 893 // Merge all the backedges from the shared header into a private Region. 894 // Feed that region as the one backedge to this loop. 895 void IdealLoopTree::merge_many_backedges( PhaseIdealLoop *phase ) { 896 uint i; 897 898 // Scan for the top 2 hottest backedges 899 float hotcnt = 0.0f; 900 float warmcnt = 0.0f; 901 uint hot_idx = 0; 902 // Loop starts at 2 because slot 1 is the fall-in path 903 for( i = 2; i < _head->req(); i++ ) { 904 float cnt = estimate_path_freq(_head->in(i)); 905 if( cnt > hotcnt ) { // Grab hottest path 906 warmcnt = hotcnt; 907 hotcnt = cnt; 908 hot_idx = i; 909 } else if( cnt > warmcnt ) { // And 2nd hottest path 910 warmcnt = cnt; 911 } 912 } 913 914 // See if the hottest backedge is worthy of being an inner loop 915 // by being much hotter than the next hottest backedge. 916 if( hotcnt <= 0.0001 || 917 hotcnt < 2.0*warmcnt ) hot_idx = 0;// No hot backedge 918 919 // Peel out the backedges into a private merge point; peel 920 // them all except optionally hot_idx. 921 PhaseIterGVN &igvn = phase->_igvn; 922 923 Node *hot_tail = NULL; 924 // Make a Region for the merge point 925 Node *r = new (phase->C, 1) RegionNode(1); 926 for( i = 2; i < _head->req(); i++ ) { 927 if( i != hot_idx ) 928 r->add_req( _head->in(i) ); 929 else hot_tail = _head->in(i); 930 } 931 igvn.register_new_node_with_optimizer(r, _head); 932 // Plug region into end of loop _head, followed by hot_tail 933 while( _head->req() > 3 ) _head->del_req( _head->req()-1 ); 934 _head->set_req(2, r); 935 if( hot_idx ) _head->add_req(hot_tail); 936 937 // Split all the Phis up between '_head' loop and the Region 'r' 938 for (DUIterator_Fast jmax, j = _head->fast_outs(jmax); j < jmax; j++) { 939 Node *out = _head->fast_out(j); 940 if( out->is_Phi() ) { 941 PhiNode* n = out->as_Phi(); 942 igvn.hash_delete(n); // Delete from hash before hacking edges 943 Node *hot_phi = NULL; 944 Node *phi = new (phase->C, r->req()) PhiNode(r, n->type(), n->adr_type()); 945 // Check all inputs for the ones to peel out 946 uint j = 1; 947 for( uint i = 2; i < n->req(); i++ ) { 948 if( i != hot_idx ) 949 phi->set_req( j++, n->in(i) ); 950 else hot_phi = n->in(i); 951 } 952 // Register the phi but do not transform until whole place transforms 953 igvn.register_new_node_with_optimizer(phi, n); 954 // Add the merge phi to the old Phi 955 while( n->req() > 3 ) n->del_req( n->req()-1 ); 956 n->set_req(2, phi); 957 if( hot_idx ) n->add_req(hot_phi); 958 } 959 } 960 961 962 // Insert a new IdealLoopTree inserted below me. Turn it into a clone 963 // of self loop tree. Turn self into a loop headed by _head and with 964 // tail being the new merge point. 965 IdealLoopTree *ilt = new IdealLoopTree( phase, _head, _tail ); 966 phase->set_loop(_tail,ilt); // Adjust tail 967 _tail = r; // Self's tail is new merge point 968 phase->set_loop(r,this); 969 ilt->_child = _child; // New guy has my children 970 _child = ilt; // Self has new guy as only child 971 ilt->_parent = this; // new guy has self for parent 972 ilt->_nest = _nest; // Same nesting depth (for now) 973 974 // Starting with 'ilt', look for child loop trees using the same shared 975 // header. Flatten these out; they will no longer be loops in the end. 976 IdealLoopTree **pilt = &_child; 977 while( ilt ) { 978 if( ilt->_head == _head ) { 979 uint i; 980 for( i = 2; i < _head->req(); i++ ) 981 if( _head->in(i) == ilt->_tail ) 982 break; // Still a loop 983 if( i == _head->req() ) { // No longer a loop 984 // Flatten ilt. Hang ilt's "_next" list from the end of 985 // ilt's '_child' list. Move the ilt's _child up to replace ilt. 986 IdealLoopTree **cp = &ilt->_child; 987 while( *cp ) cp = &(*cp)->_next; // Find end of child list 988 *cp = ilt->_next; // Hang next list at end of child list 989 *pilt = ilt->_child; // Move child up to replace ilt 990 ilt->_head = NULL; // Flag as a loop UNIONED into parent 991 ilt = ilt->_child; // Repeat using new ilt 992 continue; // do not advance over ilt->_child 993 } 994 assert( ilt->_tail == hot_tail, "expected to only find the hot inner loop here" ); 995 phase->set_loop(_head,ilt); 996 } 997 pilt = &ilt->_child; // Advance to next 998 ilt = *pilt; 999 } 1000 1001 if( _child ) fix_parent( _child, this ); 1002 } 1003 1004 //------------------------------beautify_loops--------------------------------- 1005 // Split shared headers and insert loop landing pads. 1006 // Insert a LoopNode to replace the RegionNode. 1007 // Return TRUE if loop tree is structurally changed. 1008 bool IdealLoopTree::beautify_loops( PhaseIdealLoop *phase ) { 1009 bool result = false; 1010 // Cache parts in locals for easy 1011 PhaseIterGVN &igvn = phase->_igvn; 1012 1013 phase->C->print_method("Before beautify loops", 3); 1014 1015 igvn.hash_delete(_head); // Yank from hash before hacking edges 1016 1017 // Check for multiple fall-in paths. Peel off a landing pad if need be. 1018 int fall_in_cnt = 0; 1019 for( uint i = 1; i < _head->req(); i++ ) 1020 if( !phase->is_member( this, _head->in(i) ) ) 1021 fall_in_cnt++; 1022 assert( fall_in_cnt, "at least 1 fall-in path" ); 1023 if( fall_in_cnt > 1 ) // Need a loop landing pad to merge fall-ins 1024 split_fall_in( phase, fall_in_cnt ); 1025 1026 // Swap inputs to the _head and all Phis to move the fall-in edge to 1027 // the left. 1028 fall_in_cnt = 1; 1029 while( phase->is_member( this, _head->in(fall_in_cnt) ) ) 1030 fall_in_cnt++; 1031 if( fall_in_cnt > 1 ) { 1032 // Since I am just swapping inputs I do not need to update def-use info 1033 Node *tmp = _head->in(1); 1034 _head->set_req( 1, _head->in(fall_in_cnt) ); 1035 _head->set_req( fall_in_cnt, tmp ); 1036 // Swap also all Phis 1037 for (DUIterator_Fast imax, i = _head->fast_outs(imax); i < imax; i++) { 1038 Node* phi = _head->fast_out(i); 1039 if( phi->is_Phi() ) { 1040 igvn.hash_delete(phi); // Yank from hash before hacking edges 1041 tmp = phi->in(1); 1042 phi->set_req( 1, phi->in(fall_in_cnt) ); 1043 phi->set_req( fall_in_cnt, tmp ); 1044 } 1045 } 1046 } 1047 assert( !phase->is_member( this, _head->in(1) ), "left edge is fall-in" ); 1048 assert( phase->is_member( this, _head->in(2) ), "right edge is loop" ); 1049 1050 // If I am a shared header (multiple backedges), peel off the many 1051 // backedges into a private merge point and use the merge point as 1052 // the one true backedge. 1053 if( _head->req() > 3 ) { 1054 // Merge the many backedges into a single backedge. 1055 merge_many_backedges( phase ); 1056 result = true; 1057 } 1058 1059 // If I am a shared header (multiple backedges), peel off myself loop. 1060 // I better be the outermost loop. 1061 if( _head->req() > 3 ) { 1062 split_outer_loop( phase ); 1063 result = true; 1064 1065 } else if( !_head->is_Loop() && !_irreducible ) { 1066 // Make a new LoopNode to replace the old loop head 1067 Node *l = new (phase->C, 3) LoopNode( _head->in(1), _head->in(2) ); 1068 l = igvn.register_new_node_with_optimizer(l, _head); 1069 phase->set_created_loop_node(); 1070 // Go ahead and replace _head 1071 phase->_igvn.subsume_node( _head, l ); 1072 _head = l; 1073 phase->set_loop(_head, this); 1074 for (DUIterator_Fast imax, i = l->fast_outs(imax); i < imax; i++) 1075 phase->_igvn.add_users_to_worklist(l->fast_out(i)); 1076 } 1077 1078 phase->C->print_method("After beautify loops", 3); 1079 1080 // Now recursively beautify nested loops 1081 if( _child ) result |= _child->beautify_loops( phase ); 1082 if( _next ) result |= _next ->beautify_loops( phase ); 1083 return result; 1084 } 1085 1086 //------------------------------allpaths_check_safepts---------------------------- 1087 // Allpaths backwards scan from loop tail, terminating each path at first safepoint 1088 // encountered. Helper for check_safepts. 1089 void IdealLoopTree::allpaths_check_safepts(VectorSet &visited, Node_List &stack) { 1090 assert(stack.size() == 0, "empty stack"); 1091 stack.push(_tail); 1092 visited.Clear(); 1093 visited.set(_tail->_idx); 1094 while (stack.size() > 0) { 1095 Node* n = stack.pop(); 1096 if (n->is_Call() && n->as_Call()->guaranteed_safepoint()) { 1097 // Terminate this path 1098 } else if (n->Opcode() == Op_SafePoint) { 1099 if (_phase->get_loop(n) != this) { 1100 if (_required_safept == NULL) _required_safept = new Node_List(); 1101 _required_safept->push(n); // save the one closest to the tail 1102 } 1103 // Terminate this path 1104 } else { 1105 uint start = n->is_Region() ? 1 : 0; 1106 uint end = n->is_Region() && !n->is_Loop() ? n->req() : start + 1; 1107 for (uint i = start; i < end; i++) { 1108 Node* in = n->in(i); 1109 assert(in->is_CFG(), "must be"); 1110 if (!visited.test_set(in->_idx) && is_member(_phase->get_loop(in))) { 1111 stack.push(in); 1112 } 1113 } 1114 } 1115 } 1116 } 1117 1118 //------------------------------check_safepts---------------------------- 1119 // Given dominators, try to find loops with calls that must always be 1120 // executed (call dominates loop tail). These loops do not need non-call 1121 // safepoints (ncsfpt). 1122 // 1123 // A complication is that a safepoint in a inner loop may be needed 1124 // by an outer loop. In the following, the inner loop sees it has a 1125 // call (block 3) on every path from the head (block 2) to the 1126 // backedge (arc 3->2). So it deletes the ncsfpt (non-call safepoint) 1127 // in block 2, _but_ this leaves the outer loop without a safepoint. 1128 // 1129 // entry 0 1130 // | 1131 // v 1132 // outer 1,2 +->1 1133 // | | 1134 // | v 1135 // | 2<---+ ncsfpt in 2 1136 // |_/|\ | 1137 // | v | 1138 // inner 2,3 / 3 | call in 3 1139 // / | | 1140 // v +--+ 1141 // exit 4 1142 // 1143 // 1144 // This method creates a list (_required_safept) of ncsfpt nodes that must 1145 // be protected is created for each loop. When a ncsfpt maybe deleted, it 1146 // is first looked for in the lists for the outer loops of the current loop. 1147 // 1148 // The insights into the problem: 1149 // A) counted loops are okay 1150 // B) innermost loops are okay (only an inner loop can delete 1151 // a ncsfpt needed by an outer loop) 1152 // C) a loop is immune from an inner loop deleting a safepoint 1153 // if the loop has a call on the idom-path 1154 // D) a loop is also immune if it has a ncsfpt (non-call safepoint) on the 1155 // idom-path that is not in a nested loop 1156 // E) otherwise, an ncsfpt on the idom-path that is nested in an inner 1157 // loop needs to be prevented from deletion by an inner loop 1158 // 1159 // There are two analyses: 1160 // 1) The first, and cheaper one, scans the loop body from 1161 // tail to head following the idom (immediate dominator) 1162 // chain, looking for the cases (C,D,E) above. 1163 // Since inner loops are scanned before outer loops, there is summary 1164 // information about inner loops. Inner loops can be skipped over 1165 // when the tail of an inner loop is encountered. 1166 // 1167 // 2) The second, invoked if the first fails to find a call or ncsfpt on 1168 // the idom path (which is rare), scans all predecessor control paths 1169 // from the tail to the head, terminating a path when a call or sfpt 1170 // is encountered, to find the ncsfpt's that are closest to the tail. 1171 // 1172 void IdealLoopTree::check_safepts(VectorSet &visited, Node_List &stack) { 1173 // Bottom up traversal 1174 IdealLoopTree* ch = _child; 1175 while (ch != NULL) { 1176 ch->check_safepts(visited, stack); 1177 ch = ch->_next; 1178 } 1179 1180 if (!_head->is_CountedLoop() && !_has_sfpt && _parent != NULL && !_irreducible) { 1181 bool has_call = false; // call on dom-path 1182 bool has_local_ncsfpt = false; // ncsfpt on dom-path at this loop depth 1183 Node* nonlocal_ncsfpt = NULL; // ncsfpt on dom-path at a deeper depth 1184 // Scan the dom-path nodes from tail to head 1185 for (Node* n = tail(); n != _head; n = _phase->idom(n)) { 1186 if (n->is_Call() && n->as_Call()->guaranteed_safepoint()) { 1187 has_call = true; 1188 _has_sfpt = 1; // Then no need for a safept! 1189 break; 1190 } else if (n->Opcode() == Op_SafePoint) { 1191 if (_phase->get_loop(n) == this) { 1192 has_local_ncsfpt = true; 1193 break; 1194 } 1195 if (nonlocal_ncsfpt == NULL) { 1196 nonlocal_ncsfpt = n; // save the one closest to the tail 1197 } 1198 } else { 1199 IdealLoopTree* nlpt = _phase->get_loop(n); 1200 if (this != nlpt) { 1201 // If at an inner loop tail, see if the inner loop has already 1202 // recorded seeing a call on the dom-path (and stop.) If not, 1203 // jump to the head of the inner loop. 1204 assert(is_member(nlpt), "nested loop"); 1205 Node* tail = nlpt->_tail; 1206 if (tail->in(0)->is_If()) tail = tail->in(0); 1207 if (n == tail) { 1208 // If inner loop has call on dom-path, so does outer loop 1209 if (nlpt->_has_sfpt) { 1210 has_call = true; 1211 _has_sfpt = 1; 1212 break; 1213 } 1214 // Skip to head of inner loop 1215 assert(_phase->is_dominator(_head, nlpt->_head), "inner head dominated by outer head"); 1216 n = nlpt->_head; 1217 } 1218 } 1219 } 1220 } 1221 // Record safept's that this loop needs preserved when an 1222 // inner loop attempts to delete it's safepoints. 1223 if (_child != NULL && !has_call && !has_local_ncsfpt) { 1224 if (nonlocal_ncsfpt != NULL) { 1225 if (_required_safept == NULL) _required_safept = new Node_List(); 1226 _required_safept->push(nonlocal_ncsfpt); 1227 } else { 1228 // Failed to find a suitable safept on the dom-path. Now use 1229 // an all paths walk from tail to head, looking for safepoints to preserve. 1230 allpaths_check_safepts(visited, stack); 1231 } 1232 } 1233 } 1234 } 1235 1236 //---------------------------is_deleteable_safept---------------------------- 1237 // Is safept not required by an outer loop? 1238 bool PhaseIdealLoop::is_deleteable_safept(Node* sfpt) { 1239 assert(sfpt->Opcode() == Op_SafePoint, ""); 1240 IdealLoopTree* lp = get_loop(sfpt)->_parent; 1241 while (lp != NULL) { 1242 Node_List* sfpts = lp->_required_safept; 1243 if (sfpts != NULL) { 1244 for (uint i = 0; i < sfpts->size(); i++) { 1245 if (sfpt == sfpts->at(i)) 1246 return false; 1247 } 1248 } 1249 lp = lp->_parent; 1250 } 1251 return true; 1252 } 1253 1254 //------------------------------counted_loop----------------------------------- 1255 // Convert to counted loops where possible 1256 void IdealLoopTree::counted_loop( PhaseIdealLoop *phase ) { 1257 1258 // For grins, set the inner-loop flag here 1259 if( !_child ) { 1260 if( _head->is_Loop() ) _head->as_Loop()->set_inner_loop(); 1261 } 1262 1263 if( _head->is_CountedLoop() || 1264 phase->is_counted_loop( _head, this ) ) { 1265 _has_sfpt = 1; // Indicate we do not need a safepoint here 1266 1267 // Look for a safepoint to remove 1268 for (Node* n = tail(); n != _head; n = phase->idom(n)) 1269 if (n->Opcode() == Op_SafePoint && phase->get_loop(n) == this && 1270 phase->is_deleteable_safept(n)) 1271 phase->lazy_replace(n,n->in(TypeFunc::Control)); 1272 1273 CountedLoopNode *cl = _head->as_CountedLoop(); 1274 Node *incr = cl->incr(); 1275 if( !incr ) return; // Dead loop? 1276 Node *init = cl->init_trip(); 1277 Node *phi = cl->phi(); 1278 // protect against stride not being a constant 1279 if( !cl->stride_is_con() ) return; 1280 int stride_con = cl->stride_con(); 1281 1282 // Look for induction variables 1283 1284 // Visit all children, looking for Phis 1285 for (DUIterator i = cl->outs(); cl->has_out(i); i++) { 1286 Node *out = cl->out(i); 1287 if (!out->is_Phi()) continue; // Looking for phis 1288 PhiNode* phi2 = out->as_Phi(); 1289 Node *incr2 = phi2->in( LoopNode::LoopBackControl ); 1290 // Look for induction variables of the form: X += constant 1291 if( phi2->region() != _head || 1292 incr2->req() != 3 || 1293 incr2->in(1) != phi2 || 1294 incr2 == incr || 1295 incr2->Opcode() != Op_AddI || 1296 !incr2->in(2)->is_Con() ) 1297 continue; 1298 1299 // Check for parallel induction variable (parallel to trip counter) 1300 // via an affine function. In particular, count-down loops with 1301 // count-up array indices are common. We only RCE references off 1302 // the trip-counter, so we need to convert all these to trip-counter 1303 // expressions. 1304 Node *init2 = phi2->in( LoopNode::EntryControl ); 1305 int stride_con2 = incr2->in(2)->get_int(); 1306 1307 // The general case here gets a little tricky. We want to find the 1308 // GCD of all possible parallel IV's and make a new IV using this 1309 // GCD for the loop. Then all possible IVs are simple multiples of 1310 // the GCD. In practice, this will cover very few extra loops. 1311 // Instead we require 'stride_con2' to be a multiple of 'stride_con', 1312 // where +/-1 is the common case, but other integer multiples are 1313 // also easy to handle. 1314 int ratio_con = stride_con2/stride_con; 1315 1316 if( ratio_con * stride_con == stride_con2 ) { // Check for exact 1317 // Convert to using the trip counter. The parallel induction 1318 // variable differs from the trip counter by a loop-invariant 1319 // amount, the difference between their respective initial values. 1320 // It is scaled by the 'ratio_con'. 1321 Compile* C = phase->C; 1322 Node* ratio = phase->_igvn.intcon(ratio_con); 1323 phase->set_ctrl(ratio, C->root()); 1324 Node* ratio_init = new (C, 3) MulINode(init, ratio); 1325 phase->_igvn.register_new_node_with_optimizer(ratio_init, init); 1326 phase->set_early_ctrl(ratio_init); 1327 Node* diff = new (C, 3) SubINode(init2, ratio_init); 1328 phase->_igvn.register_new_node_with_optimizer(diff, init2); 1329 phase->set_early_ctrl(diff); 1330 Node* ratio_idx = new (C, 3) MulINode(phi, ratio); 1331 phase->_igvn.register_new_node_with_optimizer(ratio_idx, phi); 1332 phase->set_ctrl(ratio_idx, cl); 1333 Node* add = new (C, 3) AddINode(ratio_idx, diff); 1334 phase->_igvn.register_new_node_with_optimizer(add); 1335 phase->set_ctrl(add, cl); 1336 phase->_igvn.hash_delete( phi2 ); 1337 phase->_igvn.subsume_node( phi2, add ); 1338 // Sometimes an induction variable is unused 1339 if (add->outcnt() == 0) { 1340 phase->_igvn.remove_dead_node(add); 1341 } 1342 --i; // deleted this phi; rescan starting with next position 1343 continue; 1344 } 1345 } 1346 } else if (_parent != NULL && !_irreducible) { 1347 // Not a counted loop. 1348 // Look for a safepoint on the idom-path to remove, preserving the first one 1349 bool found = false; 1350 Node* n = tail(); 1351 for (; n != _head && !found; n = phase->idom(n)) { 1352 if (n->Opcode() == Op_SafePoint && phase->get_loop(n) == this) 1353 found = true; // Found one 1354 } 1355 // Skip past it and delete the others 1356 for (; n != _head; n = phase->idom(n)) { 1357 if (n->Opcode() == Op_SafePoint && phase->get_loop(n) == this && 1358 phase->is_deleteable_safept(n)) 1359 phase->lazy_replace(n,n->in(TypeFunc::Control)); 1360 } 1361 } 1362 1363 // Recursively 1364 if( _child ) _child->counted_loop( phase ); 1365 if( _next ) _next ->counted_loop( phase ); 1366 } 1367 1368 #ifndef PRODUCT 1369 //------------------------------dump_head-------------------------------------- 1370 // Dump 1 liner for loop header info 1371 void IdealLoopTree::dump_head( ) const { 1372 for( uint i=0; i<_nest; i++ ) 1373 tty->print(" "); 1374 tty->print("Loop: N%d/N%d ",_head->_idx,_tail->_idx); 1375 if( _irreducible ) tty->print(" IRREDUCIBLE"); 1376 if( _head->is_CountedLoop() ) { 1377 CountedLoopNode *cl = _head->as_CountedLoop(); 1378 tty->print(" counted"); 1379 if( cl->is_pre_loop () ) tty->print(" pre" ); 1380 if( cl->is_main_loop() ) tty->print(" main"); 1381 if( cl->is_post_loop() ) tty->print(" post"); 1382 } 1383 tty->cr(); 1384 } 1385 1386 //------------------------------dump------------------------------------------- 1387 // Dump loops by loop tree 1388 void IdealLoopTree::dump( ) const { 1389 dump_head(); 1390 if( _child ) _child->dump(); 1391 if( _next ) _next ->dump(); 1392 } 1393 1394 #endif 1395 1396 //============================================================================= 1397 //------------------------------PhaseIdealLoop--------------------------------- 1398 // Create a PhaseLoop. Build the ideal Loop tree. Map each Ideal Node to 1399 // its corresponding LoopNode. If 'optimize' is true, do some loop cleanups. 1400 PhaseIdealLoop::PhaseIdealLoop( PhaseIterGVN &igvn, const PhaseIdealLoop *verify_me, bool do_split_ifs ) 1401 : PhaseTransform(Ideal_Loop), 1402 _igvn(igvn), 1403 _dom_lca_tags(C->comp_arena()) { 1404 // Reset major-progress flag for the driver's heuristics 1405 C->clear_major_progress(); 1406 1407 #ifndef PRODUCT 1408 // Capture for later assert 1409 uint unique = C->unique(); 1410 _loop_invokes++; 1411 _loop_work += unique; 1412 #endif 1413 1414 // True if the method has at least 1 irreducible loop 1415 _has_irreducible_loops = false; 1416 1417 _created_loop_node = false; 1418 1419 Arena *a = Thread::current()->resource_area(); 1420 VectorSet visited(a); 1421 // Pre-grow the mapping from Nodes to IdealLoopTrees. 1422 _nodes.map(C->unique(), NULL); 1423 memset(_nodes.adr(), 0, wordSize * C->unique()); 1424 1425 // Pre-build the top-level outermost loop tree entry 1426 _ltree_root = new IdealLoopTree( this, C->root(), C->root() ); 1427 // Do not need a safepoint at the top level 1428 _ltree_root->_has_sfpt = 1; 1429 1430 // Empty pre-order array 1431 allocate_preorders(); 1432 1433 // Build a loop tree on the fly. Build a mapping from CFG nodes to 1434 // IdealLoopTree entries. Data nodes are NOT walked. 1435 build_loop_tree(); 1436 // Check for bailout, and return 1437 if (C->failing()) { 1438 return; 1439 } 1440 1441 // No loops after all 1442 if( !_ltree_root->_child ) C->set_has_loops(false); 1443 1444 // There should always be an outer loop containing the Root and Return nodes. 1445 // If not, we have a degenerate empty program. Bail out in this case. 1446 if (!has_node(C->root())) { 1447 C->clear_major_progress(); 1448 C->record_method_not_compilable("empty program detected during loop optimization"); 1449 return; 1450 } 1451 1452 // Nothing to do, so get out 1453 if( !C->has_loops() && !do_split_ifs && !verify_me) { 1454 _igvn.optimize(); // Cleanup NeverBranches 1455 return; 1456 } 1457 1458 // Set loop nesting depth 1459 _ltree_root->set_nest( 0 ); 1460 1461 // Split shared headers and insert loop landing pads. 1462 // Do not bother doing this on the Root loop of course. 1463 if( !verify_me && _ltree_root->_child ) { 1464 if( _ltree_root->_child->beautify_loops( this ) ) { 1465 // Re-build loop tree! 1466 _ltree_root->_child = NULL; 1467 _nodes.clear(); 1468 reallocate_preorders(); 1469 build_loop_tree(); 1470 // Check for bailout, and return 1471 if (C->failing()) { 1472 return; 1473 } 1474 // Reset loop nesting depth 1475 _ltree_root->set_nest( 0 ); 1476 } 1477 } 1478 1479 // Build Dominators for elision of NULL checks & loop finding. 1480 // Since nodes do not have a slot for immediate dominator, make 1481 // a persistant side array for that info indexed on node->_idx. 1482 _idom_size = C->unique(); 1483 _idom = NEW_RESOURCE_ARRAY( Node*, _idom_size ); 1484 _dom_depth = NEW_RESOURCE_ARRAY( uint, _idom_size ); 1485 _dom_stk = NULL; // Allocated on demand in recompute_dom_depth 1486 memset( _dom_depth, 0, _idom_size * sizeof(uint) ); 1487 1488 Dominators(); 1489 1490 // As a side effect, Dominators removed any unreachable CFG paths 1491 // into RegionNodes. It doesn't do this test against Root, so 1492 // we do it here. 1493 for( uint i = 1; i < C->root()->req(); i++ ) { 1494 if( !_nodes[C->root()->in(i)->_idx] ) { // Dead path into Root? 1495 _igvn.hash_delete(C->root()); 1496 C->root()->del_req(i); 1497 _igvn._worklist.push(C->root()); 1498 i--; // Rerun same iteration on compressed edges 1499 } 1500 } 1501 1502 // Given dominators, try to find inner loops with calls that must 1503 // always be executed (call dominates loop tail). These loops do 1504 // not need a seperate safepoint. 1505 Node_List cisstack(a); 1506 _ltree_root->check_safepts(visited, cisstack); 1507 1508 // Walk the DATA nodes and place into loops. Find earliest control 1509 // node. For CFG nodes, the _nodes array starts out and remains 1510 // holding the associated IdealLoopTree pointer. For DATA nodes, the 1511 // _nodes array holds the earliest legal controlling CFG node. 1512 1513 // Allocate stack with enough space to avoid frequent realloc 1514 int stack_size = (C->unique() >> 1) + 16; // (unique>>1)+16 from Java2D stats 1515 Node_Stack nstack( a, stack_size ); 1516 1517 visited.Clear(); 1518 Node_List worklist(a); 1519 // Don't need C->root() on worklist since 1520 // it will be processed among C->top() inputs 1521 worklist.push( C->top() ); 1522 visited.set( C->top()->_idx ); // Set C->top() as visited now 1523 build_loop_early( visited, worklist, nstack, verify_me ); 1524 1525 // Given early legal placement, try finding counted loops. This placement 1526 // is good enough to discover most loop invariants. 1527 if( !verify_me ) 1528 _ltree_root->counted_loop( this ); 1529 1530 // Find latest loop placement. Find ideal loop placement. 1531 visited.Clear(); 1532 init_dom_lca_tags(); 1533 // Need C->root() on worklist when processing outs 1534 worklist.push( C->root() ); 1535 NOT_PRODUCT( C->verify_graph_edges(); ) 1536 worklist.push( C->top() ); 1537 build_loop_late( visited, worklist, nstack, verify_me ); 1538 1539 // clear out the dead code 1540 while(_deadlist.size()) { 1541 igvn.remove_globally_dead_node(_deadlist.pop()); 1542 } 1543 1544 #ifndef PRODUCT 1545 C->verify_graph_edges(); 1546 if( verify_me ) { // Nested verify pass? 1547 // Check to see if the verify mode is broken 1548 assert(C->unique() == unique, "non-optimize mode made Nodes? ? ?"); 1549 return; 1550 } 1551 if( VerifyLoopOptimizations ) verify(); 1552 #endif 1553 1554 if (ReassociateInvariants) { 1555 // Reassociate invariants and prep for split_thru_phi 1556 for (LoopTreeIterator iter(_ltree_root); !iter.done(); iter.next()) { 1557 IdealLoopTree* lpt = iter.current(); 1558 if (!lpt->is_counted() || !lpt->is_inner()) continue; 1559 1560 lpt->reassociate_invariants(this); 1561 1562 // Because RCE opportunities can be masked by split_thru_phi, 1563 // look for RCE candidates and inhibit split_thru_phi 1564 // on just their loop-phi's for this pass of loop opts 1565 if( SplitIfBlocks && do_split_ifs ) { 1566 if (lpt->policy_range_check(this)) { 1567 lpt->_rce_candidate = true; 1568 } 1569 } 1570 } 1571 } 1572 1573 // Check for aggressive application of split-if and other transforms 1574 // that require basic-block info (like cloning through Phi's) 1575 if( SplitIfBlocks && do_split_ifs ) { 1576 visited.Clear(); 1577 split_if_with_blocks( visited, nstack ); 1578 NOT_PRODUCT( if( VerifyLoopOptimizations ) verify(); ); 1579 } 1580 1581 // Perform iteration-splitting on inner loops. Split iterations to avoid 1582 // range checks or one-shot null checks. 1583 1584 // If split-if's didn't hack the graph too bad (no CFG changes) 1585 // then do loop opts. 1586 if( C->has_loops() && !C->major_progress() ) { 1587 memset( worklist.adr(), 0, worklist.Size()*sizeof(Node*) ); 1588 _ltree_root->_child->iteration_split( this, worklist ); 1589 // No verify after peeling! GCM has hoisted code out of the loop. 1590 // After peeling, the hoisted code could sink inside the peeled area. 1591 // The peeling code does not try to recompute the best location for 1592 // all the code before the peeled area, so the verify pass will always 1593 // complain about it. 1594 } 1595 // Do verify graph edges in any case 1596 NOT_PRODUCT( C->verify_graph_edges(); ); 1597 1598 if( !do_split_ifs ) { 1599 // We saw major progress in Split-If to get here. We forced a 1600 // pass with unrolling and not split-if, however more split-if's 1601 // might make progress. If the unrolling didn't make progress 1602 // then the major-progress flag got cleared and we won't try 1603 // another round of Split-If. In particular the ever-common 1604 // instance-of/check-cast pattern requires at least 2 rounds of 1605 // Split-If to clear out. 1606 C->set_major_progress(); 1607 } 1608 1609 // Repeat loop optimizations if new loops were seen 1610 if (created_loop_node()) { 1611 C->set_major_progress(); 1612 } 1613 1614 // Convert scalar to superword operations 1615 1616 if (UseSuperWord && C->has_loops() && !C->major_progress()) { 1617 // SuperWord transform 1618 SuperWord sw(this); 1619 for (LoopTreeIterator iter(_ltree_root); !iter.done(); iter.next()) { 1620 IdealLoopTree* lpt = iter.current(); 1621 if (lpt->is_counted()) { 1622 sw.transform_loop(lpt); 1623 } 1624 } 1625 } 1626 1627 // Cleanup any modified bits 1628 _igvn.optimize(); 1629 1630 // Do not repeat loop optimizations if irreducible loops are present 1631 // by claiming no-progress. 1632 if( _has_irreducible_loops ) 1633 C->clear_major_progress(); 1634 } 1635 1636 #ifndef PRODUCT 1637 //------------------------------print_statistics------------------------------- 1638 int PhaseIdealLoop::_loop_invokes=0;// Count of PhaseIdealLoop invokes 1639 int PhaseIdealLoop::_loop_work=0; // Sum of PhaseIdealLoop x unique 1640 void PhaseIdealLoop::print_statistics() { 1641 tty->print_cr("PhaseIdealLoop=%d, sum _unique=%d", _loop_invokes, _loop_work); 1642 } 1643 1644 //------------------------------verify----------------------------------------- 1645 // Build a verify-only PhaseIdealLoop, and see that it agrees with me. 1646 static int fail; // debug only, so its multi-thread dont care 1647 void PhaseIdealLoop::verify() const { 1648 int old_progress = C->major_progress(); 1649 ResourceMark rm; 1650 PhaseIdealLoop loop_verify( _igvn, this, false ); 1651 VectorSet visited(Thread::current()->resource_area()); 1652 1653 fail = 0; 1654 verify_compare( C->root(), &loop_verify, visited ); 1655 assert( fail == 0, "verify loops failed" ); 1656 // Verify loop structure is the same 1657 _ltree_root->verify_tree(loop_verify._ltree_root, NULL); 1658 // Reset major-progress. It was cleared by creating a verify version of 1659 // PhaseIdealLoop. 1660 for( int i=0; i<old_progress; i++ ) 1661 C->set_major_progress(); 1662 } 1663 1664 //------------------------------verify_compare--------------------------------- 1665 // Make sure me and the given PhaseIdealLoop agree on key data structures 1666 void PhaseIdealLoop::verify_compare( Node *n, const PhaseIdealLoop *loop_verify, VectorSet &visited ) const { 1667 if( !n ) return; 1668 if( visited.test_set( n->_idx ) ) return; 1669 if( !_nodes[n->_idx] ) { // Unreachable 1670 assert( !loop_verify->_nodes[n->_idx], "both should be unreachable" ); 1671 return; 1672 } 1673 1674 uint i; 1675 for( i = 0; i < n->req(); i++ ) 1676 verify_compare( n->in(i), loop_verify, visited ); 1677 1678 // Check the '_nodes' block/loop structure 1679 i = n->_idx; 1680 if( has_ctrl(n) ) { // We have control; verify has loop or ctrl 1681 if( _nodes[i] != loop_verify->_nodes[i] && 1682 get_ctrl_no_update(n) != loop_verify->get_ctrl_no_update(n) ) { 1683 tty->print("Mismatched control setting for: "); 1684 n->dump(); 1685 if( fail++ > 10 ) return; 1686 Node *c = get_ctrl_no_update(n); 1687 tty->print("We have it as: "); 1688 if( c->in(0) ) c->dump(); 1689 else tty->print_cr("N%d",c->_idx); 1690 tty->print("Verify thinks: "); 1691 if( loop_verify->has_ctrl(n) ) 1692 loop_verify->get_ctrl_no_update(n)->dump(); 1693 else 1694 loop_verify->get_loop_idx(n)->dump(); 1695 tty->cr(); 1696 } 1697 } else { // We have a loop 1698 IdealLoopTree *us = get_loop_idx(n); 1699 if( loop_verify->has_ctrl(n) ) { 1700 tty->print("Mismatched loop setting for: "); 1701 n->dump(); 1702 if( fail++ > 10 ) return; 1703 tty->print("We have it as: "); 1704 us->dump(); 1705 tty->print("Verify thinks: "); 1706 loop_verify->get_ctrl_no_update(n)->dump(); 1707 tty->cr(); 1708 } else if (!C->major_progress()) { 1709 // Loop selection can be messed up if we did a major progress 1710 // operation, like split-if. Do not verify in that case. 1711 IdealLoopTree *them = loop_verify->get_loop_idx(n); 1712 if( us->_head != them->_head || us->_tail != them->_tail ) { 1713 tty->print("Unequals loops for: "); 1714 n->dump(); 1715 if( fail++ > 10 ) return; 1716 tty->print("We have it as: "); 1717 us->dump(); 1718 tty->print("Verify thinks: "); 1719 them->dump(); 1720 tty->cr(); 1721 } 1722 } 1723 } 1724 1725 // Check for immediate dominators being equal 1726 if( i >= _idom_size ) { 1727 if( !n->is_CFG() ) return; 1728 tty->print("CFG Node with no idom: "); 1729 n->dump(); 1730 return; 1731 } 1732 if( !n->is_CFG() ) return; 1733 if( n == C->root() ) return; // No IDOM here 1734 1735 assert(n->_idx == i, "sanity"); 1736 Node *id = idom_no_update(n); 1737 if( id != loop_verify->idom_no_update(n) ) { 1738 tty->print("Unequals idoms for: "); 1739 n->dump(); 1740 if( fail++ > 10 ) return; 1741 tty->print("We have it as: "); 1742 id->dump(); 1743 tty->print("Verify thinks: "); 1744 loop_verify->idom_no_update(n)->dump(); 1745 tty->cr(); 1746 } 1747 1748 } 1749 1750 //------------------------------verify_tree------------------------------------ 1751 // Verify that tree structures match. Because the CFG can change, siblings 1752 // within the loop tree can be reordered. We attempt to deal with that by 1753 // reordering the verify's loop tree if possible. 1754 void IdealLoopTree::verify_tree(IdealLoopTree *loop, const IdealLoopTree *parent) const { 1755 assert( _parent == parent, "Badly formed loop tree" ); 1756 1757 // Siblings not in same order? Attempt to re-order. 1758 if( _head != loop->_head ) { 1759 // Find _next pointer to update 1760 IdealLoopTree **pp = &loop->_parent->_child; 1761 while( *pp != loop ) 1762 pp = &((*pp)->_next); 1763 // Find proper sibling to be next 1764 IdealLoopTree **nn = &loop->_next; 1765 while( (*nn) && (*nn)->_head != _head ) 1766 nn = &((*nn)->_next); 1767 1768 // Check for no match. 1769 if( !(*nn) ) { 1770 // Annoyingly, irreducible loops can pick different headers 1771 // after a major_progress operation, so the rest of the loop 1772 // tree cannot be matched. 1773 if (_irreducible && Compile::current()->major_progress()) return; 1774 assert( 0, "failed to match loop tree" ); 1775 } 1776 1777 // Move (*nn) to (*pp) 1778 IdealLoopTree *hit = *nn; 1779 *nn = hit->_next; 1780 hit->_next = loop; 1781 *pp = loop; 1782 loop = hit; 1783 // Now try again to verify 1784 } 1785 1786 assert( _head == loop->_head , "mismatched loop head" ); 1787 Node *tail = _tail; // Inline a non-updating version of 1788 while( !tail->in(0) ) // the 'tail()' call. 1789 tail = tail->in(1); 1790 assert( tail == loop->_tail, "mismatched loop tail" ); 1791 1792 // Counted loops that are guarded should be able to find their guards 1793 if( _head->is_CountedLoop() && _head->as_CountedLoop()->is_main_loop() ) { 1794 CountedLoopNode *cl = _head->as_CountedLoop(); 1795 Node *init = cl->init_trip(); 1796 Node *ctrl = cl->in(LoopNode::EntryControl); 1797 assert( ctrl->Opcode() == Op_IfTrue || ctrl->Opcode() == Op_IfFalse, "" ); 1798 Node *iff = ctrl->in(0); 1799 assert( iff->Opcode() == Op_If, "" ); 1800 Node *bol = iff->in(1); 1801 assert( bol->Opcode() == Op_Bool, "" ); 1802 Node *cmp = bol->in(1); 1803 assert( cmp->Opcode() == Op_CmpI, "" ); 1804 Node *add = cmp->in(1); 1805 Node *opaq; 1806 if( add->Opcode() == Op_Opaque1 ) { 1807 opaq = add; 1808 } else { 1809 assert( add->Opcode() == Op_AddI || add->Opcode() == Op_ConI , "" ); 1810 assert( add == init, "" ); 1811 opaq = cmp->in(2); 1812 } 1813 assert( opaq->Opcode() == Op_Opaque1, "" ); 1814 1815 } 1816 1817 if (_child != NULL) _child->verify_tree(loop->_child, this); 1818 if (_next != NULL) _next ->verify_tree(loop->_next, parent); 1819 // Innermost loops need to verify loop bodies, 1820 // but only if no 'major_progress' 1821 int fail = 0; 1822 if (!Compile::current()->major_progress() && _child == NULL) { 1823 for( uint i = 0; i < _body.size(); i++ ) { 1824 Node *n = _body.at(i); 1825 if (n->outcnt() == 0) continue; // Ignore dead 1826 uint j; 1827 for( j = 0; j < loop->_body.size(); j++ ) 1828 if( loop->_body.at(j) == n ) 1829 break; 1830 if( j == loop->_body.size() ) { // Not found in loop body 1831 // Last ditch effort to avoid assertion: Its possible that we 1832 // have some users (so outcnt not zero) but are still dead. 1833 // Try to find from root. 1834 if (Compile::current()->root()->find(n->_idx)) { 1835 fail++; 1836 tty->print("We have that verify does not: "); 1837 n->dump(); 1838 } 1839 } 1840 } 1841 for( uint i2 = 0; i2 < loop->_body.size(); i2++ ) { 1842 Node *n = loop->_body.at(i2); 1843 if (n->outcnt() == 0) continue; // Ignore dead 1844 uint j; 1845 for( j = 0; j < _body.size(); j++ ) 1846 if( _body.at(j) == n ) 1847 break; 1848 if( j == _body.size() ) { // Not found in loop body 1849 // Last ditch effort to avoid assertion: Its possible that we 1850 // have some users (so outcnt not zero) but are still dead. 1851 // Try to find from root. 1852 if (Compile::current()->root()->find(n->_idx)) { 1853 fail++; 1854 tty->print("Verify has that we do not: "); 1855 n->dump(); 1856 } 1857 } 1858 } 1859 assert( !fail, "loop body mismatch" ); 1860 } 1861 } 1862 1863 #endif 1864 1865 //------------------------------set_idom--------------------------------------- 1866 void PhaseIdealLoop::set_idom(Node* d, Node* n, uint dom_depth) { 1867 uint idx = d->_idx; 1868 if (idx >= _idom_size) { 1869 uint newsize = _idom_size<<1; 1870 while( idx >= newsize ) { 1871 newsize <<= 1; 1872 } 1873 _idom = REALLOC_RESOURCE_ARRAY( Node*, _idom,_idom_size,newsize); 1874 _dom_depth = REALLOC_RESOURCE_ARRAY( uint, _dom_depth,_idom_size,newsize); 1875 memset( _dom_depth + _idom_size, 0, (newsize - _idom_size) * sizeof(uint) ); 1876 _idom_size = newsize; 1877 } 1878 _idom[idx] = n; 1879 _dom_depth[idx] = dom_depth; 1880 } 1881 1882 //------------------------------recompute_dom_depth--------------------------------------- 1883 // The dominator tree is constructed with only parent pointers. 1884 // This recomputes the depth in the tree by first tagging all 1885 // nodes as "no depth yet" marker. The next pass then runs up 1886 // the dom tree from each node marked "no depth yet", and computes 1887 // the depth on the way back down. 1888 void PhaseIdealLoop::recompute_dom_depth() { 1889 uint no_depth_marker = C->unique(); 1890 uint i; 1891 // Initialize depth to "no depth yet" 1892 for (i = 0; i < _idom_size; i++) { 1893 if (_dom_depth[i] > 0 && _idom[i] != NULL) { 1894 _dom_depth[i] = no_depth_marker; 1895 } 1896 } 1897 if (_dom_stk == NULL) { 1898 uint init_size = C->unique() / 100; // Guess that 1/100 is a reasonable initial size. 1899 if (init_size < 10) init_size = 10; 1900 _dom_stk = new (C->node_arena()) GrowableArray<uint>(C->node_arena(), init_size, 0, 0); 1901 } 1902 // Compute new depth for each node. 1903 for (i = 0; i < _idom_size; i++) { 1904 uint j = i; 1905 // Run up the dom tree to find a node with a depth 1906 while (_dom_depth[j] == no_depth_marker) { 1907 _dom_stk->push(j); 1908 j = _idom[j]->_idx; 1909 } 1910 // Compute the depth on the way back down this tree branch 1911 uint dd = _dom_depth[j] + 1; 1912 while (_dom_stk->length() > 0) { 1913 uint j = _dom_stk->pop(); 1914 _dom_depth[j] = dd; 1915 dd++; 1916 } 1917 } 1918 } 1919 1920 //------------------------------sort------------------------------------------- 1921 // Insert 'loop' into the existing loop tree. 'innermost' is a leaf of the 1922 // loop tree, not the root. 1923 IdealLoopTree *PhaseIdealLoop::sort( IdealLoopTree *loop, IdealLoopTree *innermost ) { 1924 if( !innermost ) return loop; // New innermost loop 1925 1926 int loop_preorder = get_preorder(loop->_head); // Cache pre-order number 1927 assert( loop_preorder, "not yet post-walked loop" ); 1928 IdealLoopTree **pp = &innermost; // Pointer to previous next-pointer 1929 IdealLoopTree *l = *pp; // Do I go before or after 'l'? 1930 1931 // Insert at start of list 1932 while( l ) { // Insertion sort based on pre-order 1933 if( l == loop ) return innermost; // Already on list! 1934 int l_preorder = get_preorder(l->_head); // Cache pre-order number 1935 assert( l_preorder, "not yet post-walked l" ); 1936 // Check header pre-order number to figure proper nesting 1937 if( loop_preorder > l_preorder ) 1938 break; // End of insertion 1939 // If headers tie (e.g., shared headers) check tail pre-order numbers. 1940 // Since I split shared headers, you'd think this could not happen. 1941 // BUT: I must first do the preorder numbering before I can discover I 1942 // have shared headers, so the split headers all get the same preorder 1943 // number as the RegionNode they split from. 1944 if( loop_preorder == l_preorder && 1945 get_preorder(loop->_tail) < get_preorder(l->_tail) ) 1946 break; // Also check for shared headers (same pre#) 1947 pp = &l->_parent; // Chain up list 1948 l = *pp; 1949 } 1950 // Link into list 1951 // Point predecessor to me 1952 *pp = loop; 1953 // Point me to successor 1954 IdealLoopTree *p = loop->_parent; 1955 loop->_parent = l; // Point me to successor 1956 if( p ) sort( p, innermost ); // Insert my parents into list as well 1957 return innermost; 1958 } 1959 1960 //------------------------------build_loop_tree-------------------------------- 1961 // I use a modified Vick/Tarjan algorithm. I need pre- and a post- visit 1962 // bits. The _nodes[] array is mapped by Node index and holds a NULL for 1963 // not-yet-pre-walked, pre-order # for pre-but-not-post-walked and holds the 1964 // tightest enclosing IdealLoopTree for post-walked. 1965 // 1966 // During my forward walk I do a short 1-layer lookahead to see if I can find 1967 // a loop backedge with that doesn't have any work on the backedge. This 1968 // helps me construct nested loops with shared headers better. 1969 // 1970 // Once I've done the forward recursion, I do the post-work. For each child 1971 // I check to see if there is a backedge. Backedges define a loop! I 1972 // insert an IdealLoopTree at the target of the backedge. 1973 // 1974 // During the post-work I also check to see if I have several children 1975 // belonging to different loops. If so, then this Node is a decision point 1976 // where control flow can choose to change loop nests. It is at this 1977 // decision point where I can figure out how loops are nested. At this 1978 // time I can properly order the different loop nests from my children. 1979 // Note that there may not be any backedges at the decision point! 1980 // 1981 // Since the decision point can be far removed from the backedges, I can't 1982 // order my loops at the time I discover them. Thus at the decision point 1983 // I need to inspect loop header pre-order numbers to properly nest my 1984 // loops. This means I need to sort my childrens' loops by pre-order. 1985 // The sort is of size number-of-control-children, which generally limits 1986 // it to size 2 (i.e., I just choose between my 2 target loops). 1987 void PhaseIdealLoop::build_loop_tree() { 1988 // Allocate stack of size C->unique()/2 to avoid frequent realloc 1989 GrowableArray <Node *> bltstack(C->unique() >> 1); 1990 Node *n = C->root(); 1991 bltstack.push(n); 1992 int pre_order = 1; 1993 int stack_size; 1994 1995 while ( ( stack_size = bltstack.length() ) != 0 ) { 1996 n = bltstack.top(); // Leave node on stack 1997 if ( !is_visited(n) ) { 1998 // ---- Pre-pass Work ---- 1999 // Pre-walked but not post-walked nodes need a pre_order number. 2000 2001 set_preorder_visited( n, pre_order ); // set as visited 2002 2003 // ---- Scan over children ---- 2004 // Scan first over control projections that lead to loop headers. 2005 // This helps us find inner-to-outer loops with shared headers better. 2006 2007 // Scan children's children for loop headers. 2008 for ( int i = n->outcnt() - 1; i >= 0; --i ) { 2009 Node* m = n->raw_out(i); // Child 2010 if( m->is_CFG() && !is_visited(m) ) { // Only for CFG children 2011 // Scan over children's children to find loop 2012 for (DUIterator_Fast jmax, j = m->fast_outs(jmax); j < jmax; j++) { 2013 Node* l = m->fast_out(j); 2014 if( is_visited(l) && // Been visited? 2015 !is_postvisited(l) && // But not post-visited 2016 get_preorder(l) < pre_order ) { // And smaller pre-order 2017 // Found! Scan the DFS down this path before doing other paths 2018 bltstack.push(m); 2019 break; 2020 } 2021 } 2022 } 2023 } 2024 pre_order++; 2025 } 2026 else if ( !is_postvisited(n) ) { 2027 // Note: build_loop_tree_impl() adds out edges on rare occasions, 2028 // such as com.sun.rsasign.am::a. 2029 // For non-recursive version, first, process current children. 2030 // On next iteration, check if additional children were added. 2031 for ( int k = n->outcnt() - 1; k >= 0; --k ) { 2032 Node* u = n->raw_out(k); 2033 if ( u->is_CFG() && !is_visited(u) ) { 2034 bltstack.push(u); 2035 } 2036 } 2037 if ( bltstack.length() == stack_size ) { 2038 // There were no additional children, post visit node now 2039 (void)bltstack.pop(); // Remove node from stack 2040 pre_order = build_loop_tree_impl( n, pre_order ); 2041 // Check for bailout 2042 if (C->failing()) { 2043 return; 2044 } 2045 // Check to grow _preorders[] array for the case when 2046 // build_loop_tree_impl() adds new nodes. 2047 check_grow_preorders(); 2048 } 2049 } 2050 else { 2051 (void)bltstack.pop(); // Remove post-visited node from stack 2052 } 2053 } 2054 } 2055 2056 //------------------------------build_loop_tree_impl--------------------------- 2057 int PhaseIdealLoop::build_loop_tree_impl( Node *n, int pre_order ) { 2058 // ---- Post-pass Work ---- 2059 // Pre-walked but not post-walked nodes need a pre_order number. 2060 2061 // Tightest enclosing loop for this Node 2062 IdealLoopTree *innermost = NULL; 2063 2064 // For all children, see if any edge is a backedge. If so, make a loop 2065 // for it. Then find the tightest enclosing loop for the self Node. 2066 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 2067 Node* m = n->fast_out(i); // Child 2068 if( n == m ) continue; // Ignore control self-cycles 2069 if( !m->is_CFG() ) continue;// Ignore non-CFG edges 2070 2071 IdealLoopTree *l; // Child's loop 2072 if( !is_postvisited(m) ) { // Child visited but not post-visited? 2073 // Found a backedge 2074 assert( get_preorder(m) < pre_order, "should be backedge" ); 2075 // Check for the RootNode, which is already a LoopNode and is allowed 2076 // to have multiple "backedges". 2077 if( m == C->root()) { // Found the root? 2078 l = _ltree_root; // Root is the outermost LoopNode 2079 } else { // Else found a nested loop 2080 // Insert a LoopNode to mark this loop. 2081 l = new IdealLoopTree(this, m, n); 2082 } // End of Else found a nested loop 2083 if( !has_loop(m) ) // If 'm' does not already have a loop set 2084 set_loop(m, l); // Set loop header to loop now 2085 2086 } else { // Else not a nested loop 2087 if( !_nodes[m->_idx] ) continue; // Dead code has no loop 2088 l = get_loop(m); // Get previously determined loop 2089 // If successor is header of a loop (nest), move up-loop till it 2090 // is a member of some outer enclosing loop. Since there are no 2091 // shared headers (I've split them already) I only need to go up 2092 // at most 1 level. 2093 while( l && l->_head == m ) // Successor heads loop? 2094 l = l->_parent; // Move up 1 for me 2095 // If this loop is not properly parented, then this loop 2096 // has no exit path out, i.e. its an infinite loop. 2097 if( !l ) { 2098 // Make loop "reachable" from root so the CFG is reachable. Basically 2099 // insert a bogus loop exit that is never taken. 'm', the loop head, 2100 // points to 'n', one (of possibly many) fall-in paths. There may be 2101 // many backedges as well. 2102 2103 // Here I set the loop to be the root loop. I could have, after 2104 // inserting a bogus loop exit, restarted the recursion and found my 2105 // new loop exit. This would make the infinite loop a first-class 2106 // loop and it would then get properly optimized. What's the use of 2107 // optimizing an infinite loop? 2108 l = _ltree_root; // Oops, found infinite loop 2109 2110 // Insert the NeverBranch between 'm' and it's control user. 2111 NeverBranchNode *iff = new (C, 1) NeverBranchNode( m ); 2112 _igvn.register_new_node_with_optimizer(iff); 2113 set_loop(iff, l); 2114 Node *if_t = new (C, 1) CProjNode( iff, 0 ); 2115 _igvn.register_new_node_with_optimizer(if_t); 2116 set_loop(if_t, l); 2117 2118 Node* cfg = NULL; // Find the One True Control User of m 2119 for (DUIterator_Fast jmax, j = m->fast_outs(jmax); j < jmax; j++) { 2120 Node* x = m->fast_out(j); 2121 if (x->is_CFG() && x != m && x != iff) 2122 { cfg = x; break; } 2123 } 2124 assert(cfg != NULL, "must find the control user of m"); 2125 uint k = 0; // Probably cfg->in(0) 2126 while( cfg->in(k) != m ) k++; // But check incase cfg is a Region 2127 cfg->set_req( k, if_t ); // Now point to NeverBranch 2128 2129 // Now create the never-taken loop exit 2130 Node *if_f = new (C, 1) CProjNode( iff, 1 ); 2131 _igvn.register_new_node_with_optimizer(if_f); 2132 set_loop(if_f, l); 2133 // Find frame ptr for Halt. Relies on the optimizer 2134 // V-N'ing. Easier and quicker than searching through 2135 // the program structure. 2136 Node *frame = new (C, 1) ParmNode( C->start(), TypeFunc::FramePtr ); 2137 _igvn.register_new_node_with_optimizer(frame); 2138 // Halt & Catch Fire 2139 Node *halt = new (C, TypeFunc::Parms) HaltNode( if_f, frame ); 2140 _igvn.register_new_node_with_optimizer(halt); 2141 set_loop(halt, l); 2142 C->root()->add_req(halt); 2143 set_loop(C->root(), _ltree_root); 2144 } 2145 } 2146 // Weeny check for irreducible. This child was already visited (this 2147 // IS the post-work phase). Is this child's loop header post-visited 2148 // as well? If so, then I found another entry into the loop. 2149 while( is_postvisited(l->_head) ) { 2150 // found irreducible 2151 l->_irreducible = true; 2152 l = l->_parent; 2153 _has_irreducible_loops = true; 2154 // Check for bad CFG here to prevent crash, and bailout of compile 2155 if (l == NULL) { 2156 C->record_method_not_compilable("unhandled CFG detected during loop optimization"); 2157 return pre_order; 2158 } 2159 } 2160 2161 // This Node might be a decision point for loops. It is only if 2162 // it's children belong to several different loops. The sort call 2163 // does a trivial amount of work if there is only 1 child or all 2164 // children belong to the same loop. If however, the children 2165 // belong to different loops, the sort call will properly set the 2166 // _parent pointers to show how the loops nest. 2167 // 2168 // In any case, it returns the tightest enclosing loop. 2169 innermost = sort( l, innermost ); 2170 } 2171 2172 // Def-use info will have some dead stuff; dead stuff will have no 2173 // loop decided on. 2174 2175 // Am I a loop header? If so fix up my parent's child and next ptrs. 2176 if( innermost && innermost->_head == n ) { 2177 assert( get_loop(n) == innermost, "" ); 2178 IdealLoopTree *p = innermost->_parent; 2179 IdealLoopTree *l = innermost; 2180 while( p && l->_head == n ) { 2181 l->_next = p->_child; // Put self on parents 'next child' 2182 p->_child = l; // Make self as first child of parent 2183 l = p; // Now walk up the parent chain 2184 p = l->_parent; 2185 } 2186 } else { 2187 // Note that it is possible for a LoopNode to reach here, if the 2188 // backedge has been made unreachable (hence the LoopNode no longer 2189 // denotes a Loop, and will eventually be removed). 2190 2191 // Record tightest enclosing loop for self. Mark as post-visited. 2192 set_loop(n, innermost); 2193 // Also record has_call flag early on 2194 if( innermost ) { 2195 if( n->is_Call() && !n->is_CallLeaf() && !n->is_macro() ) { 2196 // Do not count uncommon calls 2197 if( !n->is_CallStaticJava() || !n->as_CallStaticJava()->_name ) { 2198 Node *iff = n->in(0)->in(0); 2199 if( !iff->is_If() || 2200 (n->in(0)->Opcode() == Op_IfFalse && 2201 (1.0 - iff->as_If()->_prob) >= 0.01) || 2202 (iff->as_If()->_prob >= 0.01) ) 2203 innermost->_has_call = 1; 2204 } 2205 } 2206 } 2207 } 2208 2209 // Flag as post-visited now 2210 set_postvisited(n); 2211 return pre_order; 2212 } 2213 2214 2215 //------------------------------build_loop_early------------------------------- 2216 // Put Data nodes into some loop nest, by setting the _nodes[]->loop mapping. 2217 // First pass computes the earliest controlling node possible. This is the 2218 // controlling input with the deepest dominating depth. 2219 void PhaseIdealLoop::build_loop_early( VectorSet &visited, Node_List &worklist, Node_Stack &nstack, const PhaseIdealLoop *verify_me ) { 2220 while (worklist.size() != 0) { 2221 // Use local variables nstack_top_n & nstack_top_i to cache values 2222 // on nstack's top. 2223 Node *nstack_top_n = worklist.pop(); 2224 uint nstack_top_i = 0; 2225 //while_nstack_nonempty: 2226 while (true) { 2227 // Get parent node and next input's index from stack's top. 2228 Node *n = nstack_top_n; 2229 uint i = nstack_top_i; 2230 uint cnt = n->req(); // Count of inputs 2231 if (i == 0) { // Pre-process the node. 2232 if( has_node(n) && // Have either loop or control already? 2233 !has_ctrl(n) ) { // Have loop picked out already? 2234 // During "merge_many_backedges" we fold up several nested loops 2235 // into a single loop. This makes the members of the original 2236 // loop bodies pointing to dead loops; they need to move up 2237 // to the new UNION'd larger loop. I set the _head field of these 2238 // dead loops to NULL and the _parent field points to the owning 2239 // loop. Shades of UNION-FIND algorithm. 2240 IdealLoopTree *ilt; 2241 while( !(ilt = get_loop(n))->_head ) { 2242 // Normally I would use a set_loop here. But in this one special 2243 // case, it is legal (and expected) to change what loop a Node 2244 // belongs to. 2245 _nodes.map(n->_idx, (Node*)(ilt->_parent) ); 2246 } 2247 // Remove safepoints ONLY if I've already seen I don't need one. 2248 // (the old code here would yank a 2nd safepoint after seeing a 2249 // first one, even though the 1st did not dominate in the loop body 2250 // and thus could be avoided indefinitely) 2251 if( !verify_me && ilt->_has_sfpt && n->Opcode() == Op_SafePoint && 2252 is_deleteable_safept(n)) { 2253 Node *in = n->in(TypeFunc::Control); 2254 lazy_replace(n,in); // Pull safepoint now 2255 // Carry on with the recursion "as if" we are walking 2256 // only the control input 2257 if( !visited.test_set( in->_idx ) ) { 2258 worklist.push(in); // Visit this guy later, using worklist 2259 } 2260 // Get next node from nstack: 2261 // - skip n's inputs processing by setting i > cnt; 2262 // - we also will not call set_early_ctrl(n) since 2263 // has_node(n) == true (see the condition above). 2264 i = cnt + 1; 2265 } 2266 } 2267 } // if (i == 0) 2268 2269 // Visit all inputs 2270 bool done = true; // Assume all n's inputs will be processed 2271 while (i < cnt) { 2272 Node *in = n->in(i); 2273 ++i; 2274 if (in == NULL) continue; 2275 if (in->pinned() && !in->is_CFG()) 2276 set_ctrl(in, in->in(0)); 2277 int is_visited = visited.test_set( in->_idx ); 2278 if (!has_node(in)) { // No controlling input yet? 2279 assert( !in->is_CFG(), "CFG Node with no controlling input?" ); 2280 assert( !is_visited, "visit only once" ); 2281 nstack.push(n, i); // Save parent node and next input's index. 2282 nstack_top_n = in; // Process current input now. 2283 nstack_top_i = 0; 2284 done = false; // Not all n's inputs processed. 2285 break; // continue while_nstack_nonempty; 2286 } else if (!is_visited) { 2287 // This guy has a location picked out for him, but has not yet 2288 // been visited. Happens to all CFG nodes, for instance. 2289 // Visit him using the worklist instead of recursion, to break 2290 // cycles. Since he has a location already we do not need to 2291 // find his location before proceeding with the current Node. 2292 worklist.push(in); // Visit this guy later, using worklist 2293 } 2294 } 2295 if (done) { 2296 // All of n's inputs have been processed, complete post-processing. 2297 2298 // Compute earilest point this Node can go. 2299 // CFG, Phi, pinned nodes already know their controlling input. 2300 if (!has_node(n)) { 2301 // Record earliest legal location 2302 set_early_ctrl( n ); 2303 } 2304 if (nstack.is_empty()) { 2305 // Finished all nodes on stack. 2306 // Process next node on the worklist. 2307 break; 2308 } 2309 // Get saved parent node and next input's index. 2310 nstack_top_n = nstack.node(); 2311 nstack_top_i = nstack.index(); 2312 nstack.pop(); 2313 } 2314 } // while (true) 2315 } 2316 } 2317 2318 //------------------------------dom_lca_internal-------------------------------- 2319 // Pair-wise LCA 2320 Node *PhaseIdealLoop::dom_lca_internal( Node *n1, Node *n2 ) const { 2321 if( !n1 ) return n2; // Handle NULL original LCA 2322 assert( n1->is_CFG(), "" ); 2323 assert( n2->is_CFG(), "" ); 2324 // find LCA of all uses 2325 uint d1 = dom_depth(n1); 2326 uint d2 = dom_depth(n2); 2327 while (n1 != n2) { 2328 if (d1 > d2) { 2329 n1 = idom(n1); 2330 d1 = dom_depth(n1); 2331 } else if (d1 < d2) { 2332 n2 = idom(n2); 2333 d2 = dom_depth(n2); 2334 } else { 2335 // Here d1 == d2. Due to edits of the dominator-tree, sections 2336 // of the tree might have the same depth. These sections have 2337 // to be searched more carefully. 2338 2339 // Scan up all the n1's with equal depth, looking for n2. 2340 Node *t1 = idom(n1); 2341 while (dom_depth(t1) == d1) { 2342 if (t1 == n2) return n2; 2343 t1 = idom(t1); 2344 } 2345 // Scan up all the n2's with equal depth, looking for n1. 2346 Node *t2 = idom(n2); 2347 while (dom_depth(t2) == d2) { 2348 if (t2 == n1) return n1; 2349 t2 = idom(t2); 2350 } 2351 // Move up to a new dominator-depth value as well as up the dom-tree. 2352 n1 = t1; 2353 n2 = t2; 2354 d1 = dom_depth(n1); 2355 d2 = dom_depth(n2); 2356 } 2357 } 2358 return n1; 2359 } 2360 2361 //------------------------------compute_idom----------------------------------- 2362 // Locally compute IDOM using dom_lca call. Correct only if the incoming 2363 // IDOMs are correct. 2364 Node *PhaseIdealLoop::compute_idom( Node *region ) const { 2365 assert( region->is_Region(), "" ); 2366 Node *LCA = NULL; 2367 for( uint i = 1; i < region->req(); i++ ) { 2368 if( region->in(i) != C->top() ) 2369 LCA = dom_lca( LCA, region->in(i) ); 2370 } 2371 return LCA; 2372 } 2373 2374 //------------------------------get_late_ctrl---------------------------------- 2375 // Compute latest legal control. 2376 Node *PhaseIdealLoop::get_late_ctrl( Node *n, Node *early ) { 2377 assert(early != NULL, "early control should not be NULL"); 2378 2379 // Compute LCA over list of uses 2380 Node *LCA = NULL; 2381 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax && LCA != early; i++) { 2382 Node* c = n->fast_out(i); 2383 if (_nodes[c->_idx] == NULL) 2384 continue; // Skip the occasional dead node 2385 if( c->is_Phi() ) { // For Phis, we must land above on the path 2386 for( uint j=1; j<c->req(); j++ ) {// For all inputs 2387 if( c->in(j) == n ) { // Found matching input? 2388 Node *use = c->in(0)->in(j); 2389 LCA = dom_lca_for_get_late_ctrl( LCA, use, n ); 2390 } 2391 } 2392 } else { 2393 // For CFG data-users, use is in the block just prior 2394 Node *use = has_ctrl(c) ? get_ctrl(c) : c->in(0); 2395 LCA = dom_lca_for_get_late_ctrl( LCA, use, n ); 2396 } 2397 } 2398 2399 // if this is a load, check for anti-dependent stores 2400 // We use a conservative algorithm to identify potential interfering 2401 // instructions and for rescheduling the load. The users of the memory 2402 // input of this load are examined. Any use which is not a load and is 2403 // dominated by early is considered a potentially interfering store. 2404 // This can produce false positives. 2405 if (n->is_Load() && LCA != early) { 2406 Node_List worklist; 2407 2408 Node *mem = n->in(MemNode::Memory); 2409 for (DUIterator_Fast imax, i = mem->fast_outs(imax); i < imax; i++) { 2410 Node* s = mem->fast_out(i); 2411 worklist.push(s); 2412 } 2413 while(worklist.size() != 0 && LCA != early) { 2414 Node* s = worklist.pop(); 2415 if (s->is_Load()) { 2416 continue; 2417 } else if (s->is_MergeMem()) { 2418 for (DUIterator_Fast imax, i = s->fast_outs(imax); i < imax; i++) { 2419 Node* s1 = s->fast_out(i); 2420 worklist.push(s1); 2421 } 2422 } else { 2423 Node *sctrl = has_ctrl(s) ? get_ctrl(s) : s->in(0); 2424 assert(sctrl != NULL || s->outcnt() == 0, "must have control"); 2425 if (sctrl != NULL && !sctrl->is_top() && is_dominator(early, sctrl)) { 2426 LCA = dom_lca_for_get_late_ctrl(LCA, sctrl, n); 2427 } 2428 } 2429 } 2430 } 2431 2432 assert(LCA == find_non_split_ctrl(LCA), "unexpected late control"); 2433 return LCA; 2434 } 2435 2436 // true if CFG node d dominates CFG node n 2437 bool PhaseIdealLoop::is_dominator(Node *d, Node *n) { 2438 if (d == n) 2439 return true; 2440 assert(d->is_CFG() && n->is_CFG(), "must have CFG nodes"); 2441 uint dd = dom_depth(d); 2442 while (dom_depth(n) >= dd) { 2443 if (n == d) 2444 return true; 2445 n = idom(n); 2446 } 2447 return false; 2448 } 2449 2450 //------------------------------dom_lca_for_get_late_ctrl_internal------------- 2451 // Pair-wise LCA with tags. 2452 // Tag each index with the node 'tag' currently being processed 2453 // before advancing up the dominator chain using idom(). 2454 // Later calls that find a match to 'tag' know that this path has already 2455 // been considered in the current LCA (which is input 'n1' by convention). 2456 // Since get_late_ctrl() is only called once for each node, the tag array 2457 // does not need to be cleared between calls to get_late_ctrl(). 2458 // Algorithm trades a larger constant factor for better asymptotic behavior 2459 // 2460 Node *PhaseIdealLoop::dom_lca_for_get_late_ctrl_internal( Node *n1, Node *n2, Node *tag ) { 2461 uint d1 = dom_depth(n1); 2462 uint d2 = dom_depth(n2); 2463 2464 do { 2465 if (d1 > d2) { 2466 // current lca is deeper than n2 2467 _dom_lca_tags.map(n1->_idx, tag); 2468 n1 = idom(n1); 2469 d1 = dom_depth(n1); 2470 } else if (d1 < d2) { 2471 // n2 is deeper than current lca 2472 Node *memo = _dom_lca_tags[n2->_idx]; 2473 if( memo == tag ) { 2474 return n1; // Return the current LCA 2475 } 2476 _dom_lca_tags.map(n2->_idx, tag); 2477 n2 = idom(n2); 2478 d2 = dom_depth(n2); 2479 } else { 2480 // Here d1 == d2. Due to edits of the dominator-tree, sections 2481 // of the tree might have the same depth. These sections have 2482 // to be searched more carefully. 2483 2484 // Scan up all the n1's with equal depth, looking for n2. 2485 _dom_lca_tags.map(n1->_idx, tag); 2486 Node *t1 = idom(n1); 2487 while (dom_depth(t1) == d1) { 2488 if (t1 == n2) return n2; 2489 _dom_lca_tags.map(t1->_idx, tag); 2490 t1 = idom(t1); 2491 } 2492 // Scan up all the n2's with equal depth, looking for n1. 2493 _dom_lca_tags.map(n2->_idx, tag); 2494 Node *t2 = idom(n2); 2495 while (dom_depth(t2) == d2) { 2496 if (t2 == n1) return n1; 2497 _dom_lca_tags.map(t2->_idx, tag); 2498 t2 = idom(t2); 2499 } 2500 // Move up to a new dominator-depth value as well as up the dom-tree. 2501 n1 = t1; 2502 n2 = t2; 2503 d1 = dom_depth(n1); 2504 d2 = dom_depth(n2); 2505 } 2506 } while (n1 != n2); 2507 return n1; 2508 } 2509 2510 //------------------------------init_dom_lca_tags------------------------------ 2511 // Tag could be a node's integer index, 32bits instead of 64bits in some cases 2512 // Intended use does not involve any growth for the array, so it could 2513 // be of fixed size. 2514 void PhaseIdealLoop::init_dom_lca_tags() { 2515 uint limit = C->unique() + 1; 2516 _dom_lca_tags.map( limit, NULL ); 2517 #ifdef ASSERT 2518 for( uint i = 0; i < limit; ++i ) { 2519 assert(_dom_lca_tags[i] == NULL, "Must be distinct from each node pointer"); 2520 } 2521 #endif // ASSERT 2522 } 2523 2524 //------------------------------clear_dom_lca_tags------------------------------ 2525 // Tag could be a node's integer index, 32bits instead of 64bits in some cases 2526 // Intended use does not involve any growth for the array, so it could 2527 // be of fixed size. 2528 void PhaseIdealLoop::clear_dom_lca_tags() { 2529 uint limit = C->unique() + 1; 2530 _dom_lca_tags.map( limit, NULL ); 2531 _dom_lca_tags.clear(); 2532 #ifdef ASSERT 2533 for( uint i = 0; i < limit; ++i ) { 2534 assert(_dom_lca_tags[i] == NULL, "Must be distinct from each node pointer"); 2535 } 2536 #endif // ASSERT 2537 } 2538 2539 //------------------------------build_loop_late-------------------------------- 2540 // Put Data nodes into some loop nest, by setting the _nodes[]->loop mapping. 2541 // Second pass finds latest legal placement, and ideal loop placement. 2542 void PhaseIdealLoop::build_loop_late( VectorSet &visited, Node_List &worklist, Node_Stack &nstack, const PhaseIdealLoop *verify_me ) { 2543 while (worklist.size() != 0) { 2544 Node *n = worklist.pop(); 2545 // Only visit once 2546 if (visited.test_set(n->_idx)) continue; 2547 uint cnt = n->outcnt(); 2548 uint i = 0; 2549 while (true) { 2550 assert( _nodes[n->_idx], "no dead nodes" ); 2551 // Visit all children 2552 if (i < cnt) { 2553 Node* use = n->raw_out(i); 2554 ++i; 2555 // Check for dead uses. Aggressively prune such junk. It might be 2556 // dead in the global sense, but still have local uses so I cannot 2557 // easily call 'remove_dead_node'. 2558 if( _nodes[use->_idx] != NULL || use->is_top() ) { // Not dead? 2559 // Due to cycles, we might not hit the same fixed point in the verify 2560 // pass as we do in the regular pass. Instead, visit such phis as 2561 // simple uses of the loop head. 2562 if( use->in(0) && (use->is_CFG() || use->is_Phi()) ) { 2563 if( !visited.test(use->_idx) ) 2564 worklist.push(use); 2565 } else if( !visited.test_set(use->_idx) ) { 2566 nstack.push(n, i); // Save parent and next use's index. 2567 n = use; // Process all children of current use. 2568 cnt = use->outcnt(); 2569 i = 0; 2570 } 2571 } else { 2572 // Do not visit around the backedge of loops via data edges. 2573 // push dead code onto a worklist 2574 _deadlist.push(use); 2575 } 2576 } else { 2577 // All of n's children have been processed, complete post-processing. 2578 build_loop_late_post(n, verify_me); 2579 if (nstack.is_empty()) { 2580 // Finished all nodes on stack. 2581 // Process next node on the worklist. 2582 break; 2583 } 2584 // Get saved parent node and next use's index. Visit the rest of uses. 2585 n = nstack.node(); 2586 cnt = n->outcnt(); 2587 i = nstack.index(); 2588 nstack.pop(); 2589 } 2590 } 2591 } 2592 } 2593 2594 //------------------------------build_loop_late_post--------------------------- 2595 // Put Data nodes into some loop nest, by setting the _nodes[]->loop mapping. 2596 // Second pass finds latest legal placement, and ideal loop placement. 2597 void PhaseIdealLoop::build_loop_late_post( Node *n, const PhaseIdealLoop *verify_me ) { 2598 2599 if (n->req() == 2 && n->Opcode() == Op_ConvI2L && !C->major_progress()) { 2600 _igvn._worklist.push(n); // Maybe we'll normalize it, if no more loops. 2601 } 2602 2603 // CFG and pinned nodes already handled 2604 if( n->in(0) ) { 2605 if( n->in(0)->is_top() ) return; // Dead? 2606 2607 // We'd like +VerifyLoopOptimizations to not believe that Mod's/Loads 2608 // _must_ be pinned (they have to observe their control edge of course). 2609 // Unlike Stores (which modify an unallocable resource, the memory 2610 // state), Mods/Loads can float around. So free them up. 2611 bool pinned = true; 2612 switch( n->Opcode() ) { 2613 case Op_DivI: 2614 case Op_DivF: 2615 case Op_DivD: 2616 case Op_ModI: 2617 case Op_ModF: 2618 case Op_ModD: 2619 case Op_LoadB: // Same with Loads; they can sink 2620 case Op_LoadC: // during loop optimizations. 2621 case Op_LoadD: 2622 case Op_LoadF: 2623 case Op_LoadI: 2624 case Op_LoadKlass: 2625 case Op_LoadL: 2626 case Op_LoadS: 2627 case Op_LoadP: 2628 case Op_LoadRange: 2629 case Op_LoadD_unaligned: 2630 case Op_LoadL_unaligned: 2631 case Op_StrComp: // Does a bunch of load-like effects 2632 pinned = false; 2633 } 2634 if( pinned ) { 2635 IdealLoopTree *choosen_loop = get_loop(n->is_CFG() ? n : get_ctrl(n)); 2636 if( !choosen_loop->_child ) // Inner loop? 2637 choosen_loop->_body.push(n); // Collect inner loops 2638 return; 2639 } 2640 } else { // No slot zero 2641 if( n->is_CFG() ) { // CFG with no slot 0 is dead 2642 _nodes.map(n->_idx,0); // No block setting, it's globally dead 2643 return; 2644 } 2645 assert(!n->is_CFG() || n->outcnt() == 0, ""); 2646 } 2647 2648 // Do I have a "safe range" I can select over? 2649 Node *early = get_ctrl(n);// Early location already computed 2650 2651 // Compute latest point this Node can go 2652 Node *LCA = get_late_ctrl( n, early ); 2653 // LCA is NULL due to uses being dead 2654 if( LCA == NULL ) { 2655 #ifdef ASSERT 2656 for (DUIterator i1 = n->outs(); n->has_out(i1); i1++) { 2657 assert( _nodes[n->out(i1)->_idx] == NULL, "all uses must also be dead"); 2658 } 2659 #endif 2660 _nodes.map(n->_idx, 0); // This node is useless 2661 _deadlist.push(n); 2662 return; 2663 } 2664 assert(LCA != NULL && !LCA->is_top(), "no dead nodes"); 2665 2666 Node *legal = LCA; // Walk 'legal' up the IDOM chain 2667 Node *least = legal; // Best legal position so far 2668 while( early != legal ) { // While not at earliest legal 2669 // Find least loop nesting depth 2670 legal = idom(legal); // Bump up the IDOM tree 2671 // Check for lower nesting depth 2672 if( get_loop(legal)->_nest < get_loop(least)->_nest ) 2673 least = legal; 2674 } 2675 2676 // Try not to place code on a loop entry projection 2677 // which can inhibit range check elimination. 2678 if (least != early) { 2679 Node* ctrl_out = least->unique_ctrl_out(); 2680 if (ctrl_out && ctrl_out->is_CountedLoop() && 2681 least == ctrl_out->in(LoopNode::EntryControl)) { 2682 Node* least_dom = idom(least); 2683 if (get_loop(least_dom)->is_member(get_loop(least))) { 2684 least = least_dom; 2685 } 2686 } 2687 } 2688 2689 #ifdef ASSERT 2690 // If verifying, verify that 'verify_me' has a legal location 2691 // and choose it as our location. 2692 if( verify_me ) { 2693 Node *v_ctrl = verify_me->get_ctrl_no_update(n); 2694 Node *legal = LCA; 2695 while( early != legal ) { // While not at earliest legal 2696 if( legal == v_ctrl ) break; // Check for prior good location 2697 legal = idom(legal) ;// Bump up the IDOM tree 2698 } 2699 // Check for prior good location 2700 if( legal == v_ctrl ) least = legal; // Keep prior if found 2701 } 2702 #endif 2703 2704 // Assign discovered "here or above" point 2705 least = find_non_split_ctrl(least); 2706 set_ctrl(n, least); 2707 2708 // Collect inner loop bodies 2709 IdealLoopTree *choosen_loop = get_loop(least); 2710 if( !choosen_loop->_child ) // Inner loop? 2711 choosen_loop->_body.push(n);// Collect inner loops 2712 } 2713 2714 #ifndef PRODUCT 2715 //------------------------------dump------------------------------------------- 2716 void PhaseIdealLoop::dump( ) const { 2717 ResourceMark rm; 2718 Arena* arena = Thread::current()->resource_area(); 2719 Node_Stack stack(arena, C->unique() >> 2); 2720 Node_List rpo_list; 2721 VectorSet visited(arena); 2722 visited.set(C->top()->_idx); 2723 rpo( C->root(), stack, visited, rpo_list ); 2724 // Dump root loop indexed by last element in PO order 2725 dump( _ltree_root, rpo_list.size(), rpo_list ); 2726 } 2727 2728 void PhaseIdealLoop::dump( IdealLoopTree *loop, uint idx, Node_List &rpo_list ) const { 2729 2730 // Indent by loop nesting depth 2731 for( uint x = 0; x < loop->_nest; x++ ) 2732 tty->print(" "); 2733 tty->print_cr("---- Loop N%d-N%d ----", loop->_head->_idx,loop->_tail->_idx); 2734 2735 // Now scan for CFG nodes in the same loop 2736 for( uint j=idx; j > 0; j-- ) { 2737 Node *n = rpo_list[j-1]; 2738 if( !_nodes[n->_idx] ) // Skip dead nodes 2739 continue; 2740 if( get_loop(n) != loop ) { // Wrong loop nest 2741 if( get_loop(n)->_head == n && // Found nested loop? 2742 get_loop(n)->_parent == loop ) 2743 dump(get_loop(n),rpo_list.size(),rpo_list); // Print it nested-ly 2744 continue; 2745 } 2746 2747 // Dump controlling node 2748 for( uint x = 0; x < loop->_nest; x++ ) 2749 tty->print(" "); 2750 tty->print("C"); 2751 if( n == C->root() ) { 2752 n->dump(); 2753 } else { 2754 Node* cached_idom = idom_no_update(n); 2755 Node *computed_idom = n->in(0); 2756 if( n->is_Region() ) { 2757 computed_idom = compute_idom(n); 2758 // computed_idom() will return n->in(0) when idom(n) is an IfNode (or 2759 // any MultiBranch ctrl node), so apply a similar transform to 2760 // the cached idom returned from idom_no_update. 2761 cached_idom = find_non_split_ctrl(cached_idom); 2762 } 2763 tty->print(" ID:%d",computed_idom->_idx); 2764 n->dump(); 2765 if( cached_idom != computed_idom ) { 2766 tty->print_cr("*** BROKEN IDOM! Computed as: %d, cached as: %d", 2767 computed_idom->_idx, cached_idom->_idx); 2768 } 2769 } 2770 // Dump nodes it controls 2771 for( uint k = 0; k < _nodes.Size(); k++ ) { 2772 // (k < C->unique() && get_ctrl(find(k)) == n) 2773 if (k < C->unique() && _nodes[k] == (Node*)((intptr_t)n + 1)) { 2774 Node *m = C->root()->find(k); 2775 if( m && m->outcnt() > 0 ) { 2776 if (!(has_ctrl(m) && get_ctrl_no_update(m) == n)) { 2777 tty->print_cr("*** BROKEN CTRL ACCESSOR! _nodes[k] is %p, ctrl is %p", 2778 _nodes[k], has_ctrl(m) ? get_ctrl_no_update(m) : NULL); 2779 } 2780 for( uint j = 0; j < loop->_nest; j++ ) 2781 tty->print(" "); 2782 tty->print(" "); 2783 m->dump(); 2784 } 2785 } 2786 } 2787 } 2788 } 2789 2790 // Collect a R-P-O for the whole CFG. 2791 // Result list is in post-order (scan backwards for RPO) 2792 void PhaseIdealLoop::rpo( Node *start, Node_Stack &stk, VectorSet &visited, Node_List &rpo_list ) const { 2793 stk.push(start, 0); 2794 visited.set(start->_idx); 2795 2796 while (stk.is_nonempty()) { 2797 Node* m = stk.node(); 2798 uint idx = stk.index(); 2799 if (idx < m->outcnt()) { 2800 stk.set_index(idx + 1); 2801 Node* n = m->raw_out(idx); 2802 if (n->is_CFG() && !visited.test_set(n->_idx)) { 2803 stk.push(n, 0); 2804 } 2805 } else { 2806 rpo_list.push(m); 2807 stk.pop(); 2808 } 2809 } 2810 } 2811 #endif 2812 2813 2814 //============================================================================= 2815 //------------------------------LoopTreeIterator----------------------------------- 2816 2817 // Advance to next loop tree using a preorder, left-to-right traversal. 2818 void LoopTreeIterator::next() { 2819 assert(!done(), "must not be done."); 2820 if (_curnt->_child != NULL) { 2821 _curnt = _curnt->_child; 2822 } else if (_curnt->_next != NULL) { 2823 _curnt = _curnt->_next; 2824 } else { 2825 while (_curnt != _root && _curnt->_next == NULL) { 2826 _curnt = _curnt->_parent; 2827 } 2828 if (_curnt == _root) { 2829 _curnt = NULL; 2830 assert(done(), "must be done."); 2831 } else { 2832 assert(_curnt->_next != NULL, "must be more to do"); 2833 _curnt = _curnt->_next; 2834 } 2835 } 2836 } 2837