1 /* 2 * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "ci/ciTypeFlow.hpp" 27 #include "memory/allocation.inline.hpp" 28 #include "memory/resourceArea.hpp" 29 #include "opto/addnode.hpp" 30 #include "opto/castnode.hpp" 31 #include "opto/cfgnode.hpp" 32 #include "opto/connode.hpp" 33 #include "opto/loopnode.hpp" 34 #include "opto/phaseX.hpp" 35 #include "opto/runtime.hpp" 36 #include "opto/rootnode.hpp" 37 #include "opto/subnode.hpp" 38 39 // Portions of code courtesy of Clifford Click 40 41 // Optimization - Graph Style 42 43 44 #ifndef PRODUCT 45 extern int explicit_null_checks_elided; 46 #endif 47 48 //============================================================================= 49 //------------------------------Value------------------------------------------ 50 // Return a tuple for whichever arm of the IF is reachable 51 const Type* IfNode::Value(PhaseGVN* phase) const { 52 if( !in(0) ) return Type::TOP; 53 if( phase->type(in(0)) == Type::TOP ) 54 return Type::TOP; 55 const Type *t = phase->type(in(1)); 56 if( t == Type::TOP ) // data is undefined 57 return TypeTuple::IFNEITHER; // unreachable altogether 58 if( t == TypeInt::ZERO ) // zero, or false 59 return TypeTuple::IFFALSE; // only false branch is reachable 60 if( t == TypeInt::ONE ) // 1, or true 61 return TypeTuple::IFTRUE; // only true branch is reachable 62 assert( t == TypeInt::BOOL, "expected boolean type" ); 63 64 return TypeTuple::IFBOTH; // No progress 65 } 66 67 const RegMask &IfNode::out_RegMask() const { 68 return RegMask::Empty; 69 } 70 71 //------------------------------split_if--------------------------------------- 72 // Look for places where we merge constants, then test on the merged value. 73 // If the IF test will be constant folded on the path with the constant, we 74 // win by splitting the IF to before the merge point. 75 static Node* split_if(IfNode *iff, PhaseIterGVN *igvn) { 76 // I could be a lot more general here, but I'm trying to squeeze this 77 // in before the Christmas '98 break so I'm gonna be kinda restrictive 78 // on the patterns I accept. CNC 79 80 // Look for a compare of a constant and a merged value 81 Node *i1 = iff->in(1); 82 if( !i1->is_Bool() ) return NULL; 83 BoolNode *b = i1->as_Bool(); 84 Node *cmp = b->in(1); 85 if( !cmp->is_Cmp() ) return NULL; 86 i1 = cmp->in(1); 87 if( i1 == NULL || !i1->is_Phi() ) return NULL; 88 PhiNode *phi = i1->as_Phi(); 89 if( phi->is_copy() ) return NULL; 90 Node *con2 = cmp->in(2); 91 if( !con2->is_Con() ) return NULL; 92 // See that the merge point contains some constants 93 Node *con1=NULL; 94 uint i4; 95 for( i4 = 1; i4 < phi->req(); i4++ ) { 96 con1 = phi->in(i4); 97 if( !con1 ) return NULL; // Do not optimize partially collapsed merges 98 if( con1->is_Con() ) break; // Found a constant 99 // Also allow null-vs-not-null checks 100 const TypePtr *tp = igvn->type(con1)->isa_ptr(); 101 if( tp && tp->_ptr == TypePtr::NotNull ) 102 break; 103 } 104 if( i4 >= phi->req() ) return NULL; // Found no constants 105 106 igvn->C->set_has_split_ifs(true); // Has chance for split-if 107 108 // Make sure that the compare can be constant folded away 109 Node *cmp2 = cmp->clone(); 110 cmp2->set_req(1,con1); 111 cmp2->set_req(2,con2); 112 const Type *t = cmp2->Value(igvn); 113 // This compare is dead, so whack it! 114 igvn->remove_dead_node(cmp2); 115 if( !t->singleton() ) return NULL; 116 117 // No intervening control, like a simple Call 118 Node *r = iff->in(0); 119 if( !r->is_Region() ) return NULL; 120 if (r->is_Loop()) return NULL; 121 if( phi->region() != r ) return NULL; 122 // No other users of the cmp/bool 123 if (b->outcnt() != 1 || cmp->outcnt() != 1) { 124 //tty->print_cr("many users of cmp/bool"); 125 return NULL; 126 } 127 128 // Make sure we can determine where all the uses of merged values go 129 for (DUIterator_Fast jmax, j = r->fast_outs(jmax); j < jmax; j++) { 130 Node* u = r->fast_out(j); 131 if( u == r ) continue; 132 if( u == iff ) continue; 133 if( u->outcnt() == 0 ) continue; // use is dead & ignorable 134 if( !u->is_Phi() ) { 135 /* 136 if( u->is_Start() ) { 137 tty->print_cr("Region has inlined start use"); 138 } else { 139 tty->print_cr("Region has odd use"); 140 u->dump(2); 141 }*/ 142 return NULL; 143 } 144 if( u != phi ) { 145 // CNC - do not allow any other merged value 146 //tty->print_cr("Merging another value"); 147 //u->dump(2); 148 return NULL; 149 } 150 // Make sure we can account for all Phi uses 151 for (DUIterator_Fast kmax, k = u->fast_outs(kmax); k < kmax; k++) { 152 Node* v = u->fast_out(k); // User of the phi 153 // CNC - Allow only really simple patterns. 154 // In particular I disallow AddP of the Phi, a fairly common pattern 155 if (v == cmp) continue; // The compare is OK 156 if (v->is_ConstraintCast()) { 157 // If the cast is derived from data flow edges, it may not have a control edge. 158 // If so, it should be safe to split. But follow-up code can not deal with 159 // this (l. 359). So skip. 160 if (v->in(0) == NULL) { 161 return NULL; 162 } 163 if (v->in(0)->in(0) == iff) { 164 continue; // CastPP/II of the IfNode is OK 165 } 166 } 167 // Disabled following code because I cannot tell if exactly one 168 // path dominates without a real dominator check. CNC 9/9/1999 169 //uint vop = v->Opcode(); 170 //if( vop == Op_Phi ) { // Phi from another merge point might be OK 171 // Node *r = v->in(0); // Get controlling point 172 // if( !r ) return NULL; // Degraded to a copy 173 // // Find exactly one path in (either True or False doms, but not IFF) 174 // int cnt = 0; 175 // for( uint i = 1; i < r->req(); i++ ) 176 // if( r->in(i) && r->in(i)->in(0) == iff ) 177 // cnt++; 178 // if( cnt == 1 ) continue; // Exactly one of True or False guards Phi 179 //} 180 if( !v->is_Call() ) { 181 /* 182 if( v->Opcode() == Op_AddP ) { 183 tty->print_cr("Phi has AddP use"); 184 } else if( v->Opcode() == Op_CastPP ) { 185 tty->print_cr("Phi has CastPP use"); 186 } else if( v->Opcode() == Op_CastII ) { 187 tty->print_cr("Phi has CastII use"); 188 } else { 189 tty->print_cr("Phi has use I cant be bothered with"); 190 } 191 */ 192 } 193 return NULL; 194 195 /* CNC - Cut out all the fancy acceptance tests 196 // Can we clone this use when doing the transformation? 197 // If all uses are from Phis at this merge or constants, then YES. 198 if( !v->in(0) && v != cmp ) { 199 tty->print_cr("Phi has free-floating use"); 200 v->dump(2); 201 return NULL; 202 } 203 for( uint l = 1; l < v->req(); l++ ) { 204 if( (!v->in(l)->is_Phi() || v->in(l)->in(0) != r) && 205 !v->in(l)->is_Con() ) { 206 tty->print_cr("Phi has use"); 207 v->dump(2); 208 return NULL; 209 } // End of if Phi-use input is neither Phi nor Constant 210 } // End of for all inputs to Phi-use 211 */ 212 } // End of for all uses of Phi 213 } // End of for all uses of Region 214 215 // Only do this if the IF node is in a sane state 216 if (iff->outcnt() != 2) 217 return NULL; 218 219 // Got a hit! Do the Mondo Hack! 220 // 221 //ABC a1c def ghi B 1 e h A C a c d f g i 222 // R - Phi - Phi - Phi Rc - Phi - Phi - Phi Rx - Phi - Phi - Phi 223 // cmp - 2 cmp - 2 cmp - 2 224 // bool bool_c bool_x 225 // if if_c if_x 226 // T F T F T F 227 // ..s.. ..t .. ..s.. ..t.. ..s.. ..t.. 228 // 229 // Split the paths coming into the merge point into 2 separate groups of 230 // merges. On the left will be all the paths feeding constants into the 231 // Cmp's Phi. On the right will be the remaining paths. The Cmp's Phi 232 // will fold up into a constant; this will let the Cmp fold up as well as 233 // all the control flow. Below the original IF we have 2 control 234 // dependent regions, 's' and 't'. Now we will merge the two paths 235 // just prior to 's' and 't' from the two IFs. At least 1 path (and quite 236 // likely 2 or more) will promptly constant fold away. 237 PhaseGVN *phase = igvn; 238 239 // Make a region merging constants and a region merging the rest 240 uint req_c = 0; 241 for (uint ii = 1; ii < r->req(); ii++) { 242 if (phi->in(ii) == con1) { 243 req_c++; 244 } 245 Node* proj = PhaseIdealLoop::find_predicate(r->in(ii)); 246 if (proj != NULL) { 247 return NULL; 248 } 249 } 250 251 // If all the defs of the phi are the same constant, we already have the desired end state. 252 // Skip the split that would create empty phi and region nodes. 253 if((r->req() - req_c) == 1) { 254 return NULL; 255 } 256 257 Node *region_c = new RegionNode(req_c + 1); 258 Node *phi_c = con1; 259 uint len = r->req(); 260 Node *region_x = new RegionNode(len - req_c); 261 Node *phi_x = PhiNode::make_blank(region_x, phi); 262 for (uint i = 1, i_c = 1, i_x = 1; i < len; i++) { 263 if (phi->in(i) == con1) { 264 region_c->init_req( i_c++, r ->in(i) ); 265 } else { 266 region_x->init_req( i_x, r ->in(i) ); 267 phi_x ->init_req( i_x++, phi->in(i) ); 268 } 269 } 270 271 // Register the new RegionNodes but do not transform them. Cannot 272 // transform until the entire Region/Phi conglomerate has been hacked 273 // as a single huge transform. 274 igvn->register_new_node_with_optimizer( region_c ); 275 igvn->register_new_node_with_optimizer( region_x ); 276 // Prevent the untimely death of phi_x. Currently he has no uses. He is 277 // about to get one. If this only use goes away, then phi_x will look dead. 278 // However, he will be picking up some more uses down below. 279 Node *hook = new Node(4); 280 hook->init_req(0, phi_x); 281 hook->init_req(1, phi_c); 282 phi_x = phase->transform( phi_x ); 283 284 // Make the compare 285 Node *cmp_c = phase->makecon(t); 286 Node *cmp_x = cmp->clone(); 287 cmp_x->set_req(1,phi_x); 288 cmp_x->set_req(2,con2); 289 cmp_x = phase->transform(cmp_x); 290 // Make the bool 291 Node *b_c = phase->transform(new BoolNode(cmp_c,b->_test._test)); 292 Node *b_x = phase->transform(new BoolNode(cmp_x,b->_test._test)); 293 // Make the IfNode 294 IfNode* iff_c = iff->clone()->as_If(); 295 iff_c->set_req(0, region_c); 296 iff_c->set_req(1, b_c); 297 igvn->set_type_bottom(iff_c); 298 igvn->_worklist.push(iff_c); 299 hook->init_req(2, iff_c); 300 301 IfNode* iff_x = iff->clone()->as_If(); 302 iff_x->set_req(0, region_x); 303 iff_x->set_req(1, b_x); 304 igvn->set_type_bottom(iff_x); 305 igvn->_worklist.push(iff_x); 306 hook->init_req(3, iff_x); 307 308 // Make the true/false arms 309 Node *iff_c_t = phase->transform(new IfTrueNode (iff_c)); 310 Node *iff_c_f = phase->transform(new IfFalseNode(iff_c)); 311 Node *iff_x_t = phase->transform(new IfTrueNode (iff_x)); 312 Node *iff_x_f = phase->transform(new IfFalseNode(iff_x)); 313 314 // Merge the TRUE paths 315 Node *region_s = new RegionNode(3); 316 igvn->_worklist.push(region_s); 317 region_s->init_req(1, iff_c_t); 318 region_s->init_req(2, iff_x_t); 319 igvn->register_new_node_with_optimizer( region_s ); 320 321 // Merge the FALSE paths 322 Node *region_f = new RegionNode(3); 323 igvn->_worklist.push(region_f); 324 region_f->init_req(1, iff_c_f); 325 region_f->init_req(2, iff_x_f); 326 igvn->register_new_node_with_optimizer( region_f ); 327 328 igvn->hash_delete(cmp);// Remove soon-to-be-dead node from hash table. 329 cmp->set_req(1,NULL); // Whack the inputs to cmp because it will be dead 330 cmp->set_req(2,NULL); 331 // Check for all uses of the Phi and give them a new home. 332 // The 'cmp' got cloned, but CastPP/IIs need to be moved. 333 Node *phi_s = NULL; // do not construct unless needed 334 Node *phi_f = NULL; // do not construct unless needed 335 for (DUIterator_Last i2min, i2 = phi->last_outs(i2min); i2 >= i2min; --i2) { 336 Node* v = phi->last_out(i2);// User of the phi 337 igvn->rehash_node_delayed(v); // Have to fixup other Phi users 338 uint vop = v->Opcode(); 339 Node *proj = NULL; 340 if( vop == Op_Phi ) { // Remote merge point 341 Node *r = v->in(0); 342 for (uint i3 = 1; i3 < r->req(); i3++) 343 if (r->in(i3) && r->in(i3)->in(0) == iff) { 344 proj = r->in(i3); 345 break; 346 } 347 } else if( v->is_ConstraintCast() ) { 348 proj = v->in(0); // Controlling projection 349 } else { 350 assert( 0, "do not know how to handle this guy" ); 351 } 352 guarantee(proj != NULL, "sanity"); 353 354 Node *proj_path_data, *proj_path_ctrl; 355 if( proj->Opcode() == Op_IfTrue ) { 356 if( phi_s == NULL ) { 357 // Only construct phi_s if needed, otherwise provides 358 // interfering use. 359 phi_s = PhiNode::make_blank(region_s,phi); 360 phi_s->init_req( 1, phi_c ); 361 phi_s->init_req( 2, phi_x ); 362 hook->add_req(phi_s); 363 phi_s = phase->transform(phi_s); 364 } 365 proj_path_data = phi_s; 366 proj_path_ctrl = region_s; 367 } else { 368 if( phi_f == NULL ) { 369 // Only construct phi_f if needed, otherwise provides 370 // interfering use. 371 phi_f = PhiNode::make_blank(region_f,phi); 372 phi_f->init_req( 1, phi_c ); 373 phi_f->init_req( 2, phi_x ); 374 hook->add_req(phi_f); 375 phi_f = phase->transform(phi_f); 376 } 377 proj_path_data = phi_f; 378 proj_path_ctrl = region_f; 379 } 380 381 // Fixup 'v' for for the split 382 if( vop == Op_Phi ) { // Remote merge point 383 uint i; 384 for( i = 1; i < v->req(); i++ ) 385 if( v->in(i) == phi ) 386 break; 387 v->set_req(i, proj_path_data ); 388 } else if( v->is_ConstraintCast() ) { 389 v->set_req(0, proj_path_ctrl ); 390 v->set_req(1, proj_path_data ); 391 } else 392 ShouldNotReachHere(); 393 } 394 395 // Now replace the original iff's True/False with region_s/region_t. 396 // This makes the original iff go dead. 397 for (DUIterator_Last i3min, i3 = iff->last_outs(i3min); i3 >= i3min; --i3) { 398 Node* p = iff->last_out(i3); 399 assert( p->Opcode() == Op_IfTrue || p->Opcode() == Op_IfFalse, "" ); 400 Node *u = (p->Opcode() == Op_IfTrue) ? region_s : region_f; 401 // Replace p with u 402 igvn->add_users_to_worklist(p); 403 for (DUIterator_Last lmin, l = p->last_outs(lmin); l >= lmin;) { 404 Node* x = p->last_out(l); 405 igvn->hash_delete(x); 406 uint uses_found = 0; 407 for( uint j = 0; j < x->req(); j++ ) { 408 if( x->in(j) == p ) { 409 x->set_req(j, u); 410 uses_found++; 411 } 412 } 413 l -= uses_found; // we deleted 1 or more copies of this edge 414 } 415 igvn->remove_dead_node(p); 416 } 417 418 // Force the original merge dead 419 igvn->hash_delete(r); 420 // First, remove region's dead users. 421 for (DUIterator_Last lmin, l = r->last_outs(lmin); l >= lmin;) { 422 Node* u = r->last_out(l); 423 if( u == r ) { 424 r->set_req(0, NULL); 425 } else { 426 assert(u->outcnt() == 0, "only dead users"); 427 igvn->remove_dead_node(u); 428 } 429 l -= 1; 430 } 431 igvn->remove_dead_node(r); 432 433 // Now remove the bogus extra edges used to keep things alive 434 igvn->remove_dead_node( hook ); 435 436 // Must return either the original node (now dead) or a new node 437 // (Do not return a top here, since that would break the uniqueness of top.) 438 return new ConINode(TypeInt::ZERO); 439 } 440 441 // if this IfNode follows a range check pattern return the projection 442 // for the failed path 443 ProjNode* IfNode::range_check_trap_proj(int& flip_test, Node*& l, Node*& r) { 444 if (outcnt() != 2) { 445 return NULL; 446 } 447 Node* b = in(1); 448 if (b == NULL || !b->is_Bool()) return NULL; 449 BoolNode* bn = b->as_Bool(); 450 Node* cmp = bn->in(1); 451 if (cmp == NULL) return NULL; 452 if (cmp->Opcode() != Op_CmpU) return NULL; 453 454 l = cmp->in(1); 455 r = cmp->in(2); 456 flip_test = 1; 457 if (bn->_test._test == BoolTest::le) { 458 l = cmp->in(2); 459 r = cmp->in(1); 460 flip_test = 2; 461 } else if (bn->_test._test != BoolTest::lt) { 462 return NULL; 463 } 464 if (l->is_top()) return NULL; // Top input means dead test 465 if (r->Opcode() != Op_LoadRange && !is_RangeCheck()) return NULL; 466 467 // We have recognized one of these forms: 468 // Flip 1: If (Bool[<] CmpU(l, LoadRange)) ... 469 // Flip 2: If (Bool[<=] CmpU(LoadRange, l)) ... 470 471 ProjNode* iftrap = proj_out_or_null(flip_test == 2 ? true : false); 472 return iftrap; 473 } 474 475 476 //------------------------------is_range_check--------------------------------- 477 // Return 0 if not a range check. Return 1 if a range check and set index and 478 // offset. Return 2 if we had to negate the test. Index is NULL if the check 479 // is versus a constant. 480 int RangeCheckNode::is_range_check(Node* &range, Node* &index, jint &offset) { 481 int flip_test = 0; 482 Node* l = NULL; 483 Node* r = NULL; 484 ProjNode* iftrap = range_check_trap_proj(flip_test, l, r); 485 486 if (iftrap == NULL) { 487 return 0; 488 } 489 490 // Make sure it's a real range check by requiring an uncommon trap 491 // along the OOB path. Otherwise, it's possible that the user wrote 492 // something which optimized to look like a range check but behaves 493 // in some other way. 494 if (iftrap->is_uncommon_trap_proj(Deoptimization::Reason_range_check) == NULL) { 495 return 0; 496 } 497 498 // Look for index+offset form 499 Node* ind = l; 500 jint off = 0; 501 if (l->is_top()) { 502 return 0; 503 } else if (l->Opcode() == Op_AddI) { 504 if ((off = l->in(1)->find_int_con(0)) != 0) { 505 ind = l->in(2)->uncast(); 506 } else if ((off = l->in(2)->find_int_con(0)) != 0) { 507 ind = l->in(1)->uncast(); 508 } 509 } else if ((off = l->find_int_con(-1)) >= 0) { 510 // constant offset with no variable index 511 ind = NULL; 512 } else { 513 // variable index with no constant offset (or dead negative index) 514 off = 0; 515 } 516 517 // Return all the values: 518 index = ind; 519 offset = off; 520 range = r; 521 return flip_test; 522 } 523 524 //------------------------------adjust_check----------------------------------- 525 // Adjust (widen) a prior range check 526 static void adjust_check(Node* proj, Node* range, Node* index, 527 int flip, jint off_lo, PhaseIterGVN* igvn) { 528 PhaseGVN *gvn = igvn; 529 // Break apart the old check 530 Node *iff = proj->in(0); 531 Node *bol = iff->in(1); 532 if( bol->is_top() ) return; // In case a partially dead range check appears 533 // bail (or bomb[ASSERT/DEBUG]) if NOT projection-->IfNode-->BoolNode 534 DEBUG_ONLY( if( !bol->is_Bool() ) { proj->dump(3); fatal("Expect projection-->IfNode-->BoolNode"); } ) 535 if( !bol->is_Bool() ) return; 536 537 Node *cmp = bol->in(1); 538 // Compute a new check 539 Node *new_add = gvn->intcon(off_lo); 540 if( index ) { 541 new_add = off_lo ? gvn->transform(new AddINode( index, new_add )) : index; 542 } 543 Node *new_cmp = (flip == 1) 544 ? new CmpUNode( new_add, range ) 545 : new CmpUNode( range, new_add ); 546 new_cmp = gvn->transform(new_cmp); 547 // See if no need to adjust the existing check 548 if( new_cmp == cmp ) return; 549 // Else, adjust existing check 550 Node *new_bol = gvn->transform( new BoolNode( new_cmp, bol->as_Bool()->_test._test ) ); 551 igvn->rehash_node_delayed( iff ); 552 iff->set_req_X( 1, new_bol, igvn ); 553 } 554 555 //------------------------------up_one_dom------------------------------------- 556 // Walk up the dominator tree one step. Return NULL at root or true 557 // complex merges. Skips through small diamonds. 558 Node* IfNode::up_one_dom(Node *curr, bool linear_only) { 559 Node *dom = curr->in(0); 560 if( !dom ) // Found a Region degraded to a copy? 561 return curr->nonnull_req(); // Skip thru it 562 563 if( curr != dom ) // Normal walk up one step? 564 return dom; 565 566 // Use linear_only if we are still parsing, since we cannot 567 // trust the regions to be fully filled in. 568 if (linear_only) 569 return NULL; 570 571 if( dom->is_Root() ) 572 return NULL; 573 574 // Else hit a Region. Check for a loop header 575 if( dom->is_Loop() ) 576 return dom->in(1); // Skip up thru loops 577 578 // Check for small diamonds 579 Node *din1, *din2, *din3, *din4; 580 if( dom->req() == 3 && // 2-path merge point 581 (din1 = dom ->in(1)) && // Left path exists 582 (din2 = dom ->in(2)) && // Right path exists 583 (din3 = din1->in(0)) && // Left path up one 584 (din4 = din2->in(0)) ) { // Right path up one 585 if( din3->is_Call() && // Handle a slow-path call on either arm 586 (din3 = din3->in(0)) ) 587 din3 = din3->in(0); 588 if( din4->is_Call() && // Handle a slow-path call on either arm 589 (din4 = din4->in(0)) ) 590 din4 = din4->in(0); 591 if (din3 != NULL && din3 == din4 && din3->is_If()) // Regions not degraded to a copy 592 return din3; // Skip around diamonds 593 } 594 595 // Give up the search at true merges 596 return NULL; // Dead loop? Or hit root? 597 } 598 599 600 //------------------------------filtered_int_type-------------------------------- 601 // Return a possibly more restrictive type for val based on condition control flow for an if 602 const TypeInt* IfNode::filtered_int_type(PhaseGVN* gvn, Node *val, Node* if_proj) { 603 assert(if_proj && 604 (if_proj->Opcode() == Op_IfTrue || if_proj->Opcode() == Op_IfFalse), "expecting an if projection"); 605 if (if_proj->in(0) && if_proj->in(0)->is_If()) { 606 IfNode* iff = if_proj->in(0)->as_If(); 607 if (iff->in(1) && iff->in(1)->is_Bool()) { 608 BoolNode* bol = iff->in(1)->as_Bool(); 609 if (bol->in(1) && bol->in(1)->is_Cmp()) { 610 const CmpNode* cmp = bol->in(1)->as_Cmp(); 611 if (cmp->in(1) == val) { 612 const TypeInt* cmp2_t = gvn->type(cmp->in(2))->isa_int(); 613 if (cmp2_t != NULL) { 614 jint lo = cmp2_t->_lo; 615 jint hi = cmp2_t->_hi; 616 BoolTest::mask msk = if_proj->Opcode() == Op_IfTrue ? bol->_test._test : bol->_test.negate(); 617 switch (msk) { 618 case BoolTest::ne: 619 // Can't refine type 620 return NULL; 621 case BoolTest::eq: 622 return cmp2_t; 623 case BoolTest::lt: 624 lo = TypeInt::INT->_lo; 625 if (hi - 1 < hi) { 626 hi = hi - 1; 627 } 628 break; 629 case BoolTest::le: 630 lo = TypeInt::INT->_lo; 631 break; 632 case BoolTest::gt: 633 if (lo + 1 > lo) { 634 lo = lo + 1; 635 } 636 hi = TypeInt::INT->_hi; 637 break; 638 case BoolTest::ge: 639 // lo unchanged 640 hi = TypeInt::INT->_hi; 641 break; 642 default: 643 break; 644 } 645 const TypeInt* rtn_t = TypeInt::make(lo, hi, cmp2_t->_widen); 646 return rtn_t; 647 } 648 } 649 } 650 } 651 } 652 return NULL; 653 } 654 655 //------------------------------fold_compares---------------------------- 656 // See if a pair of CmpIs can be converted into a CmpU. In some cases 657 // the direction of this if is determined by the preceding if so it 658 // can be eliminate entirely. 659 // 660 // Given an if testing (CmpI n v) check for an immediately control 661 // dependent if that is testing (CmpI n v2) and has one projection 662 // leading to this if and the other projection leading to a region 663 // that merges one of this ifs control projections. 664 // 665 // If 666 // / | 667 // / | 668 // / | 669 // If | 670 // /\ | 671 // / \ | 672 // / \ | 673 // / Region 674 // 675 // Or given an if testing (CmpI n v) check for a dominating if that is 676 // testing (CmpI n v2), both having one projection leading to an 677 // uncommon trap. Allow Another independent guard in between to cover 678 // an explicit range check: 679 // if (index < 0 || index >= array.length) { 680 // which may need a null check to guard the LoadRange 681 // 682 // If 683 // / \ 684 // / \ 685 // / \ 686 // If unc 687 // /\ 688 // / \ 689 // / \ 690 // / unc 691 // 692 693 // Is the comparison for this If suitable for folding? 694 bool IfNode::cmpi_folds(PhaseIterGVN* igvn) { 695 return in(1) != NULL && 696 in(1)->is_Bool() && 697 in(1)->in(1) != NULL && 698 in(1)->in(1)->Opcode() == Op_CmpI && 699 in(1)->in(1)->in(2) != NULL && 700 in(1)->in(1)->in(2) != igvn->C->top() && 701 (in(1)->as_Bool()->_test.is_less() || 702 in(1)->as_Bool()->_test.is_greater()); 703 } 704 705 // Is a dominating control suitable for folding with this if? 706 bool IfNode::is_ctrl_folds(Node* ctrl, PhaseIterGVN* igvn) { 707 return ctrl != NULL && 708 ctrl->is_Proj() && 709 ctrl->in(0) != NULL && 710 ctrl->in(0)->Opcode() == Op_If && 711 ctrl->in(0)->outcnt() == 2 && 712 ctrl->in(0)->as_If()->cmpi_folds(igvn) && 713 // Must compare same value 714 ctrl->in(0)->in(1)->in(1)->in(1) != NULL && 715 ctrl->in(0)->in(1)->in(1)->in(1) == in(1)->in(1)->in(1); 716 } 717 718 // Do this If and the dominating If share a region? 719 bool IfNode::has_shared_region(ProjNode* proj, ProjNode*& success, ProjNode*& fail) { 720 ProjNode* otherproj = proj->other_if_proj(); 721 Node* otherproj_ctrl_use = otherproj->unique_ctrl_out(); 722 RegionNode* region = (otherproj_ctrl_use != NULL && otherproj_ctrl_use->is_Region()) ? otherproj_ctrl_use->as_Region() : NULL; 723 success = NULL; 724 fail = NULL; 725 726 if (otherproj->outcnt() == 1 && region != NULL && !region->has_phi()) { 727 for (int i = 0; i < 2; i++) { 728 ProjNode* proj = proj_out(i); 729 if (success == NULL && proj->outcnt() == 1 && proj->unique_out() == region) { 730 success = proj; 731 } else if (fail == NULL) { 732 fail = proj; 733 } else { 734 success = fail = NULL; 735 } 736 } 737 } 738 return success != NULL && fail != NULL; 739 } 740 741 bool IfNode::is_dominator_unc(CallStaticJavaNode* dom_unc, CallStaticJavaNode* unc) { 742 // Different methods and methods containing jsrs are not supported. 743 ciMethod* method = unc->jvms()->method(); 744 ciMethod* dom_method = dom_unc->jvms()->method(); 745 if (method != dom_method || method->has_jsrs()) { 746 return false; 747 } 748 // Check that both traps are in the same activation of the method (instead 749 // of two activations being inlined through different call sites) by verifying 750 // that the call stacks are equal for both JVMStates. 751 JVMState* dom_caller = dom_unc->jvms()->caller(); 752 JVMState* caller = unc->jvms()->caller(); 753 if ((dom_caller == NULL) != (caller == NULL)) { 754 // The current method must either be inlined into both dom_caller and 755 // caller or must not be inlined at all (top method). Bail out otherwise. 756 return false; 757 } else if (dom_caller != NULL && !dom_caller->same_calls_as(caller)) { 758 return false; 759 } 760 // Check that the bci of the dominating uncommon trap dominates the bci 761 // of the dominated uncommon trap. Otherwise we may not re-execute 762 // the dominated check after deoptimization from the merged uncommon trap. 763 ciTypeFlow* flow = dom_method->get_flow_analysis(); 764 int bci = unc->jvms()->bci(); 765 int dom_bci = dom_unc->jvms()->bci(); 766 if (!flow->is_dominated_by(bci, dom_bci)) { 767 return false; 768 } 769 770 return true; 771 } 772 773 // Return projection that leads to an uncommon trap if any 774 ProjNode* IfNode::uncommon_trap_proj(CallStaticJavaNode*& call) const { 775 for (int i = 0; i < 2; i++) { 776 call = proj_out(i)->is_uncommon_trap_proj(Deoptimization::Reason_none); 777 if (call != NULL) { 778 return proj_out(i); 779 } 780 } 781 return NULL; 782 } 783 784 // Do this If and the dominating If both branch out to an uncommon trap 785 bool IfNode::has_only_uncommon_traps(ProjNode* proj, ProjNode*& success, ProjNode*& fail, PhaseIterGVN* igvn) { 786 ProjNode* otherproj = proj->other_if_proj(); 787 CallStaticJavaNode* dom_unc = otherproj->is_uncommon_trap_proj(Deoptimization::Reason_none); 788 789 if (otherproj->outcnt() == 1 && dom_unc != NULL) { 790 // We need to re-execute the folded Ifs after deoptimization from the merged traps 791 if (!dom_unc->jvms()->should_reexecute()) { 792 return false; 793 } 794 795 CallStaticJavaNode* unc = NULL; 796 ProjNode* unc_proj = uncommon_trap_proj(unc); 797 if (unc_proj != NULL && unc_proj->outcnt() == 1) { 798 if (dom_unc == unc) { 799 // Allow the uncommon trap to be shared through a region 800 RegionNode* r = unc->in(0)->as_Region(); 801 if (r->outcnt() != 2 || r->req() != 3 || r->find_edge(otherproj) == -1 || r->find_edge(unc_proj) == -1) { 802 return false; 803 } 804 assert(r->has_phi() == NULL, "simple region shouldn't have a phi"); 805 } else if (dom_unc->in(0) != otherproj || unc->in(0) != unc_proj) { 806 return false; 807 } 808 809 if (!is_dominator_unc(dom_unc, unc)) { 810 return false; 811 } 812 813 // See merge_uncommon_traps: the reason of the uncommon trap 814 // will be changed and the state of the dominating If will be 815 // used. Checked that we didn't apply this transformation in a 816 // previous compilation and it didn't cause too many traps 817 ciMethod* dom_method = dom_unc->jvms()->method(); 818 int dom_bci = dom_unc->jvms()->bci(); 819 if (!igvn->C->too_many_traps(dom_method, dom_bci, Deoptimization::Reason_unstable_fused_if) && 820 !igvn->C->too_many_traps(dom_method, dom_bci, Deoptimization::Reason_range_check)) { 821 success = unc_proj; 822 fail = unc_proj->other_if_proj(); 823 return true; 824 } 825 } 826 } 827 return false; 828 } 829 830 // Check that the 2 CmpI can be folded into as single CmpU and proceed with the folding 831 bool IfNode::fold_compares_helper(ProjNode* proj, ProjNode* success, ProjNode* fail, PhaseIterGVN* igvn) { 832 Node* this_cmp = in(1)->in(1); 833 BoolNode* this_bool = in(1)->as_Bool(); 834 IfNode* dom_iff = proj->in(0)->as_If(); 835 BoolNode* dom_bool = dom_iff->in(1)->as_Bool(); 836 Node* lo = dom_iff->in(1)->in(1)->in(2); 837 Node* hi = this_cmp->in(2); 838 Node* n = this_cmp->in(1); 839 ProjNode* otherproj = proj->other_if_proj(); 840 841 const TypeInt* lo_type = IfNode::filtered_int_type(igvn, n, otherproj); 842 const TypeInt* hi_type = IfNode::filtered_int_type(igvn, n, success); 843 844 BoolTest::mask lo_test = dom_bool->_test._test; 845 BoolTest::mask hi_test = this_bool->_test._test; 846 BoolTest::mask cond = hi_test; 847 848 // convert: 849 // 850 // dom_bool = x {<,<=,>,>=} a 851 // / \ 852 // proj = {True,False} / \ otherproj = {False,True} 853 // / 854 // this_bool = x {<,<=} b 855 // / \ 856 // fail = {True,False} / \ success = {False,True} 857 // / 858 // 859 // (Second test guaranteed canonicalized, first one may not have 860 // been canonicalized yet) 861 // 862 // into: 863 // 864 // cond = (x - lo) {<u,<=u,>u,>=u} adjusted_lim 865 // / \ 866 // fail / \ success 867 // / 868 // 869 870 // Figure out which of the two tests sets the upper bound and which 871 // sets the lower bound if any. 872 Node* adjusted_lim = NULL; 873 if (lo_type != NULL && hi_type != NULL && hi_type->_lo > lo_type->_hi && 874 hi_type->_hi == max_jint && lo_type->_lo == min_jint) { 875 assert((dom_bool->_test.is_less() && !proj->_con) || 876 (dom_bool->_test.is_greater() && proj->_con), "incorrect test"); 877 // this test was canonicalized 878 assert(this_bool->_test.is_less() && fail->_con, "incorrect test"); 879 880 // this_bool = < 881 // dom_bool = >= (proj = True) or dom_bool = < (proj = False) 882 // x in [a, b[ on the fail (= True) projection, b > a-1 (because of hi_type->_lo > lo_type->_hi test above): 883 // lo = a, hi = b, adjusted_lim = b-a, cond = <u 884 // dom_bool = > (proj = True) or dom_bool = <= (proj = False) 885 // x in ]a, b[ on the fail (= True) projection, b > a: 886 // lo = a+1, hi = b, adjusted_lim = b-a-1, cond = <u 887 // this_bool = <= 888 // dom_bool = >= (proj = True) or dom_bool = < (proj = False) 889 // x in [a, b] on the fail (= True) projection, b+1 > a-1: 890 // lo = a, hi = b, adjusted_lim = b-a+1, cond = <u 891 // lo = a, hi = b, adjusted_lim = b-a, cond = <=u doesn't work because b = a - 1 is possible, then b-a = -1 892 // dom_bool = > (proj = True) or dom_bool = <= (proj = False) 893 // x in ]a, b] on the fail (= True) projection b+1 > a: 894 // lo = a+1, hi = b, adjusted_lim = b-a, cond = <u 895 // lo = a+1, hi = b, adjusted_lim = b-a-1, cond = <=u doesn't work because a = b is possible, then b-a-1 = -1 896 897 if (hi_test == BoolTest::lt) { 898 if (lo_test == BoolTest::gt || lo_test == BoolTest::le) { 899 lo = igvn->transform(new AddINode(lo, igvn->intcon(1))); 900 } 901 } else { 902 assert(hi_test == BoolTest::le, "bad test"); 903 if (lo_test == BoolTest::ge || lo_test == BoolTest::lt) { 904 adjusted_lim = igvn->transform(new SubINode(hi, lo)); 905 adjusted_lim = igvn->transform(new AddINode(adjusted_lim, igvn->intcon(1))); 906 cond = BoolTest::lt; 907 } else { 908 assert(lo_test == BoolTest::gt || lo_test == BoolTest::le, "bad test"); 909 adjusted_lim = igvn->transform(new SubINode(hi, lo)); 910 lo = igvn->transform(new AddINode(lo, igvn->intcon(1))); 911 cond = BoolTest::lt; 912 } 913 } 914 } else if (lo_type != NULL && hi_type != NULL && lo_type->_lo > hi_type->_hi && 915 lo_type->_hi == max_jint && hi_type->_lo == min_jint) { 916 917 // this_bool = < 918 // dom_bool = < (proj = True) or dom_bool = >= (proj = False) 919 // x in [b, a[ on the fail (= False) projection, a > b-1 (because of lo_type->_lo > hi_type->_hi above): 920 // lo = b, hi = a, adjusted_lim = a-b, cond = >=u 921 // dom_bool = <= (proj = True) or dom_bool = > (proj = False) 922 // x in [b, a] on the fail (= False) projection, a+1 > b-1: 923 // lo = b, hi = a, adjusted_lim = a-b+1, cond = >=u 924 // lo = b, hi = a, adjusted_lim = a-b, cond = >u doesn't work because a = b - 1 is possible, then b-a = -1 925 // this_bool = <= 926 // dom_bool = < (proj = True) or dom_bool = >= (proj = False) 927 // x in ]b, a[ on the fail (= False) projection, a > b: 928 // lo = b+1, hi = a, adjusted_lim = a-b-1, cond = >=u 929 // dom_bool = <= (proj = True) or dom_bool = > (proj = False) 930 // x in ]b, a] on the fail (= False) projection, a+1 > b: 931 // lo = b+1, hi = a, adjusted_lim = a-b, cond = >=u 932 // lo = b+1, hi = a, adjusted_lim = a-b-1, cond = >u doesn't work because a = b is possible, then b-a-1 = -1 933 934 swap(lo, hi); 935 swap(lo_type, hi_type); 936 swap(lo_test, hi_test); 937 938 assert((dom_bool->_test.is_less() && proj->_con) || 939 (dom_bool->_test.is_greater() && !proj->_con), "incorrect test"); 940 // this test was canonicalized 941 assert(this_bool->_test.is_less() && !fail->_con, "incorrect test"); 942 943 cond = (hi_test == BoolTest::le || hi_test == BoolTest::gt) ? BoolTest::gt : BoolTest::ge; 944 945 if (lo_test == BoolTest::lt) { 946 if (hi_test == BoolTest::lt || hi_test == BoolTest::ge) { 947 cond = BoolTest::ge; 948 } else { 949 assert(hi_test == BoolTest::le || hi_test == BoolTest::gt, "bad test"); 950 adjusted_lim = igvn->transform(new SubINode(hi, lo)); 951 adjusted_lim = igvn->transform(new AddINode(adjusted_lim, igvn->intcon(1))); 952 cond = BoolTest::ge; 953 } 954 } else if (lo_test == BoolTest::le) { 955 if (hi_test == BoolTest::lt || hi_test == BoolTest::ge) { 956 lo = igvn->transform(new AddINode(lo, igvn->intcon(1))); 957 cond = BoolTest::ge; 958 } else { 959 assert(hi_test == BoolTest::le || hi_test == BoolTest::gt, "bad test"); 960 adjusted_lim = igvn->transform(new SubINode(hi, lo)); 961 lo = igvn->transform(new AddINode(lo, igvn->intcon(1))); 962 cond = BoolTest::ge; 963 } 964 } 965 } else { 966 const TypeInt* failtype = filtered_int_type(igvn, n, proj); 967 if (failtype != NULL) { 968 const TypeInt* type2 = filtered_int_type(igvn, n, fail); 969 if (type2 != NULL) { 970 failtype = failtype->join(type2)->is_int(); 971 if (failtype->_lo > failtype->_hi) { 972 // previous if determines the result of this if so 973 // replace Bool with constant 974 igvn->_worklist.push(in(1)); 975 igvn->replace_input_of(this, 1, igvn->intcon(success->_con)); 976 return true; 977 } 978 } 979 } 980 lo = NULL; 981 hi = NULL; 982 } 983 984 if (lo && hi) { 985 // Merge the two compares into a single unsigned compare by building (CmpU (n - lo) (hi - lo)) 986 Node* adjusted_val = igvn->transform(new SubINode(n, lo)); 987 if (adjusted_lim == NULL) { 988 adjusted_lim = igvn->transform(new SubINode(hi, lo)); 989 } 990 Node* newcmp = igvn->transform(new CmpUNode(adjusted_val, adjusted_lim)); 991 Node* newbool = igvn->transform(new BoolNode(newcmp, cond)); 992 993 igvn->replace_input_of(dom_iff, 1, igvn->intcon(proj->_con)); 994 igvn->_worklist.push(in(1)); 995 igvn->replace_input_of(this, 1, newbool); 996 997 return true; 998 } 999 return false; 1000 } 1001 1002 // Merge the branches that trap for this If and the dominating If into 1003 // a single region that branches to the uncommon trap for the 1004 // dominating If 1005 Node* IfNode::merge_uncommon_traps(ProjNode* proj, ProjNode* success, ProjNode* fail, PhaseIterGVN* igvn) { 1006 Node* res = this; 1007 assert(success->in(0) == this, "bad projection"); 1008 1009 ProjNode* otherproj = proj->other_if_proj(); 1010 1011 CallStaticJavaNode* unc = success->is_uncommon_trap_proj(Deoptimization::Reason_none); 1012 CallStaticJavaNode* dom_unc = otherproj->is_uncommon_trap_proj(Deoptimization::Reason_none); 1013 1014 if (unc != dom_unc) { 1015 Node* r = new RegionNode(3); 1016 1017 r->set_req(1, otherproj); 1018 r->set_req(2, success); 1019 r = igvn->transform(r); 1020 assert(r->is_Region(), "can't go away"); 1021 1022 // Make both If trap at the state of the first If: once the CmpI 1023 // nodes are merged, if we trap we don't know which of the CmpI 1024 // nodes would have caused the trap so we have to restart 1025 // execution at the first one 1026 igvn->replace_input_of(dom_unc, 0, r); 1027 igvn->replace_input_of(unc, 0, igvn->C->top()); 1028 } 1029 int trap_request = dom_unc->uncommon_trap_request(); 1030 Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(trap_request); 1031 Deoptimization::DeoptAction action = Deoptimization::trap_request_action(trap_request); 1032 1033 int flip_test = 0; 1034 Node* l = NULL; 1035 Node* r = NULL; 1036 1037 if (success->in(0)->as_If()->range_check_trap_proj(flip_test, l, r) != NULL) { 1038 // If this looks like a range check, change the trap to 1039 // Reason_range_check so the compiler recognizes it as a range 1040 // check and applies the corresponding optimizations 1041 trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_range_check, action); 1042 1043 improve_address_types(l, r, fail, igvn); 1044 1045 res = igvn->transform(new RangeCheckNode(in(0), in(1), _prob, _fcnt)); 1046 } else if (unc != dom_unc) { 1047 // If we trap we won't know what CmpI would have caused the trap 1048 // so use a special trap reason to mark this pair of CmpI nodes as 1049 // bad candidate for folding. On recompilation we won't fold them 1050 // and we may trap again but this time we'll know what branch 1051 // traps 1052 trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_unstable_fused_if, action); 1053 } 1054 igvn->replace_input_of(dom_unc, TypeFunc::Parms, igvn->intcon(trap_request)); 1055 return res; 1056 } 1057 1058 // If we are turning 2 CmpI nodes into a CmpU that follows the pattern 1059 // of a rangecheck on index i, on 64 bit the compares may be followed 1060 // by memory accesses using i as index. In that case, the CmpU tells 1061 // us something about the values taken by i that can help the compiler 1062 // (see Compile::conv_I2X_index()) 1063 void IfNode::improve_address_types(Node* l, Node* r, ProjNode* fail, PhaseIterGVN* igvn) { 1064 #ifdef _LP64 1065 ResourceMark rm; 1066 Node_Stack stack(2); 1067 1068 assert(r->Opcode() == Op_LoadRange, "unexpected range check"); 1069 const TypeInt* array_size = igvn->type(r)->is_int(); 1070 1071 stack.push(l, 0); 1072 1073 while(stack.size() > 0) { 1074 Node* n = stack.node(); 1075 uint start = stack.index(); 1076 1077 uint i = start; 1078 for (; i < n->outcnt(); i++) { 1079 Node* use = n->raw_out(i); 1080 if (stack.size() == 1) { 1081 if (use->Opcode() == Op_ConvI2L) { 1082 const TypeLong* bounds = use->as_Type()->type()->is_long(); 1083 if (bounds->_lo <= array_size->_lo && bounds->_hi >= array_size->_hi && 1084 (bounds->_lo != array_size->_lo || bounds->_hi != array_size->_hi)) { 1085 stack.set_index(i+1); 1086 stack.push(use, 0); 1087 break; 1088 } 1089 } 1090 } else if (use->is_Mem()) { 1091 Node* ctrl = use->in(0); 1092 for (int i = 0; i < 10 && ctrl != NULL && ctrl != fail; i++) { 1093 ctrl = up_one_dom(ctrl); 1094 } 1095 if (ctrl == fail) { 1096 Node* init_n = stack.node_at(1); 1097 assert(init_n->Opcode() == Op_ConvI2L, "unexpected first node"); 1098 // Create a new narrow ConvI2L node that is dependent on the range check 1099 Node* new_n = igvn->C->conv_I2X_index(igvn, l, array_size, fail); 1100 1101 // The type of the ConvI2L may be widen and so the new 1102 // ConvI2L may not be better than an existing ConvI2L 1103 if (new_n != init_n) { 1104 for (uint j = 2; j < stack.size(); j++) { 1105 Node* n = stack.node_at(j); 1106 Node* clone = n->clone(); 1107 int rep = clone->replace_edge(init_n, new_n); 1108 assert(rep > 0, "can't find expected node?"); 1109 clone = igvn->transform(clone); 1110 init_n = n; 1111 new_n = clone; 1112 } 1113 igvn->hash_delete(use); 1114 int rep = use->replace_edge(init_n, new_n); 1115 assert(rep > 0, "can't find expected node?"); 1116 igvn->transform(use); 1117 if (init_n->outcnt() == 0) { 1118 igvn->_worklist.push(init_n); 1119 } 1120 } 1121 } 1122 } else if (use->in(0) == NULL && (igvn->type(use)->isa_long() || 1123 igvn->type(use)->isa_ptr())) { 1124 stack.set_index(i+1); 1125 stack.push(use, 0); 1126 break; 1127 } 1128 } 1129 if (i == n->outcnt()) { 1130 stack.pop(); 1131 } 1132 } 1133 #endif 1134 } 1135 1136 bool IfNode::is_cmp_with_loadrange(ProjNode* proj) { 1137 if (in(1) != NULL && 1138 in(1)->in(1) != NULL && 1139 in(1)->in(1)->in(2) != NULL) { 1140 Node* other = in(1)->in(1)->in(2); 1141 if (other->Opcode() == Op_LoadRange && 1142 ((other->in(0) != NULL && other->in(0) == proj) || 1143 (other->in(0) == NULL && 1144 other->in(2) != NULL && 1145 other->in(2)->is_AddP() && 1146 other->in(2)->in(1) != NULL && 1147 other->in(2)->in(1)->Opcode() == Op_CastPP && 1148 other->in(2)->in(1)->in(0) == proj))) { 1149 return true; 1150 } 1151 } 1152 return false; 1153 } 1154 1155 bool IfNode::is_null_check(ProjNode* proj, PhaseIterGVN* igvn) { 1156 Node* other = in(1)->in(1)->in(2); 1157 if (other->in(MemNode::Address) != NULL && 1158 proj->in(0)->in(1) != NULL && 1159 proj->in(0)->in(1)->is_Bool() && 1160 proj->in(0)->in(1)->in(1) != NULL && 1161 proj->in(0)->in(1)->in(1)->Opcode() == Op_CmpP && 1162 proj->in(0)->in(1)->in(1)->in(2) != NULL && 1163 proj->in(0)->in(1)->in(1)->in(1) == other->in(MemNode::Address)->in(AddPNode::Address)->uncast() && 1164 igvn->type(proj->in(0)->in(1)->in(1)->in(2)) == TypePtr::NULL_PTR) { 1165 return true; 1166 } 1167 return false; 1168 } 1169 1170 // Check that the If that is in between the 2 integer comparisons has 1171 // no side effect 1172 bool IfNode::is_side_effect_free_test(ProjNode* proj, PhaseIterGVN* igvn) { 1173 if (proj == NULL) { 1174 return false; 1175 } 1176 CallStaticJavaNode* unc = proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none); 1177 if (unc != NULL && proj->outcnt() <= 2) { 1178 if (proj->outcnt() == 1 || 1179 // Allow simple null check from LoadRange 1180 (is_cmp_with_loadrange(proj) && is_null_check(proj, igvn))) { 1181 CallStaticJavaNode* unc = proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none); 1182 CallStaticJavaNode* dom_unc = proj->in(0)->in(0)->as_Proj()->is_uncommon_trap_if_pattern(Deoptimization::Reason_none); 1183 assert(dom_unc != NULL, "is_uncommon_trap_if_pattern returned NULL"); 1184 1185 // reroute_side_effect_free_unc changes the state of this 1186 // uncommon trap to restart execution at the previous 1187 // CmpI. Check that this change in a previous compilation didn't 1188 // cause too many traps. 1189 int trap_request = unc->uncommon_trap_request(); 1190 Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(trap_request); 1191 1192 if (igvn->C->too_many_traps(dom_unc->jvms()->method(), dom_unc->jvms()->bci(), reason)) { 1193 return false; 1194 } 1195 1196 if (!is_dominator_unc(dom_unc, unc)) { 1197 return false; 1198 } 1199 1200 return true; 1201 } 1202 } 1203 return false; 1204 } 1205 1206 // Make the If between the 2 integer comparisons trap at the state of 1207 // the first If: the last CmpI is the one replaced by a CmpU and the 1208 // first CmpI is eliminated, so the test between the 2 CmpI nodes 1209 // won't be guarded by the first CmpI anymore. It can trap in cases 1210 // where the first CmpI would have prevented it from executing: on a 1211 // trap, we need to restart execution at the state of the first CmpI 1212 void IfNode::reroute_side_effect_free_unc(ProjNode* proj, ProjNode* dom_proj, PhaseIterGVN* igvn) { 1213 CallStaticJavaNode* dom_unc = dom_proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none); 1214 ProjNode* otherproj = proj->other_if_proj(); 1215 CallStaticJavaNode* unc = proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none); 1216 Node* call_proj = dom_unc->unique_ctrl_out(); 1217 Node* halt = call_proj->unique_ctrl_out(); 1218 1219 Node* new_unc = dom_unc->clone(); 1220 call_proj = call_proj->clone(); 1221 halt = halt->clone(); 1222 Node* c = otherproj->clone(); 1223 1224 c = igvn->transform(c); 1225 new_unc->set_req(TypeFunc::Parms, unc->in(TypeFunc::Parms)); 1226 new_unc->set_req(0, c); 1227 new_unc = igvn->transform(new_unc); 1228 call_proj->set_req(0, new_unc); 1229 call_proj = igvn->transform(call_proj); 1230 halt->set_req(0, call_proj); 1231 halt = igvn->transform(halt); 1232 1233 igvn->replace_node(otherproj, igvn->C->top()); 1234 igvn->C->root()->add_req(halt); 1235 } 1236 1237 Node* IfNode::fold_compares(PhaseIterGVN* igvn) { 1238 if (Opcode() != Op_If) return NULL; 1239 1240 if (cmpi_folds(igvn)) { 1241 Node* ctrl = in(0); 1242 if (is_ctrl_folds(ctrl, igvn) && 1243 ctrl->outcnt() == 1) { 1244 // A integer comparison immediately dominated by another integer 1245 // comparison 1246 ProjNode* success = NULL; 1247 ProjNode* fail = NULL; 1248 ProjNode* dom_cmp = ctrl->as_Proj(); 1249 if (has_shared_region(dom_cmp, success, fail) && 1250 // Next call modifies graph so must be last 1251 fold_compares_helper(dom_cmp, success, fail, igvn)) { 1252 return this; 1253 } 1254 if (has_only_uncommon_traps(dom_cmp, success, fail, igvn) && 1255 // Next call modifies graph so must be last 1256 fold_compares_helper(dom_cmp, success, fail, igvn)) { 1257 return merge_uncommon_traps(dom_cmp, success, fail, igvn); 1258 } 1259 return NULL; 1260 } else if (ctrl->in(0) != NULL && 1261 ctrl->in(0)->in(0) != NULL) { 1262 ProjNode* success = NULL; 1263 ProjNode* fail = NULL; 1264 Node* dom = ctrl->in(0)->in(0); 1265 ProjNode* dom_cmp = dom->isa_Proj(); 1266 ProjNode* other_cmp = ctrl->isa_Proj(); 1267 1268 // Check if it's an integer comparison dominated by another 1269 // integer comparison with another test in between 1270 if (is_ctrl_folds(dom, igvn) && 1271 has_only_uncommon_traps(dom_cmp, success, fail, igvn) && 1272 is_side_effect_free_test(other_cmp, igvn) && 1273 // Next call modifies graph so must be last 1274 fold_compares_helper(dom_cmp, success, fail, igvn)) { 1275 reroute_side_effect_free_unc(other_cmp, dom_cmp, igvn); 1276 return merge_uncommon_traps(dom_cmp, success, fail, igvn); 1277 } 1278 } 1279 } 1280 return NULL; 1281 } 1282 1283 //------------------------------remove_useless_bool---------------------------- 1284 // Check for people making a useless boolean: things like 1285 // if( (x < y ? true : false) ) { ... } 1286 // Replace with if( x < y ) { ... } 1287 static Node *remove_useless_bool(IfNode *iff, PhaseGVN *phase) { 1288 Node *i1 = iff->in(1); 1289 if( !i1->is_Bool() ) return NULL; 1290 BoolNode *bol = i1->as_Bool(); 1291 1292 Node *cmp = bol->in(1); 1293 if( cmp->Opcode() != Op_CmpI ) return NULL; 1294 1295 // Must be comparing against a bool 1296 const Type *cmp2_t = phase->type( cmp->in(2) ); 1297 if( cmp2_t != TypeInt::ZERO && 1298 cmp2_t != TypeInt::ONE ) 1299 return NULL; 1300 1301 // Find a prior merge point merging the boolean 1302 i1 = cmp->in(1); 1303 if( !i1->is_Phi() ) return NULL; 1304 PhiNode *phi = i1->as_Phi(); 1305 if( phase->type( phi ) != TypeInt::BOOL ) 1306 return NULL; 1307 1308 // Check for diamond pattern 1309 int true_path = phi->is_diamond_phi(); 1310 if( true_path == 0 ) return NULL; 1311 1312 // Make sure that iff and the control of the phi are different. This 1313 // should really only happen for dead control flow since it requires 1314 // an illegal cycle. 1315 if (phi->in(0)->in(1)->in(0) == iff) return NULL; 1316 1317 // phi->region->if_proj->ifnode->bool->cmp 1318 BoolNode *bol2 = phi->in(0)->in(1)->in(0)->in(1)->as_Bool(); 1319 1320 // Now get the 'sense' of the test correct so we can plug in 1321 // either iff2->in(1) or its complement. 1322 int flip = 0; 1323 if( bol->_test._test == BoolTest::ne ) flip = 1-flip; 1324 else if( bol->_test._test != BoolTest::eq ) return NULL; 1325 if( cmp2_t == TypeInt::ZERO ) flip = 1-flip; 1326 1327 const Type *phi1_t = phase->type( phi->in(1) ); 1328 const Type *phi2_t = phase->type( phi->in(2) ); 1329 // Check for Phi(0,1) and flip 1330 if( phi1_t == TypeInt::ZERO ) { 1331 if( phi2_t != TypeInt::ONE ) return NULL; 1332 flip = 1-flip; 1333 } else { 1334 // Check for Phi(1,0) 1335 if( phi1_t != TypeInt::ONE ) return NULL; 1336 if( phi2_t != TypeInt::ZERO ) return NULL; 1337 } 1338 if( true_path == 2 ) { 1339 flip = 1-flip; 1340 } 1341 1342 Node* new_bol = (flip ? phase->transform( bol2->negate(phase) ) : bol2); 1343 assert(new_bol != iff->in(1), "must make progress"); 1344 iff->set_req(1, new_bol); 1345 // Intervening diamond probably goes dead 1346 phase->C->set_major_progress(); 1347 return iff; 1348 } 1349 1350 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff); 1351 1352 struct RangeCheck { 1353 Node* ctl; 1354 jint off; 1355 }; 1356 1357 Node* IfNode::Ideal_common(PhaseGVN *phase, bool can_reshape) { 1358 if (remove_dead_region(phase, can_reshape)) return this; 1359 // No Def-Use info? 1360 if (!can_reshape) return NULL; 1361 1362 // Don't bother trying to transform a dead if 1363 if (in(0)->is_top()) return NULL; 1364 // Don't bother trying to transform an if with a dead test 1365 if (in(1)->is_top()) return NULL; 1366 // Another variation of a dead test 1367 if (in(1)->is_Con()) return NULL; 1368 // Another variation of a dead if 1369 if (outcnt() < 2) return NULL; 1370 1371 // Canonicalize the test. 1372 Node* idt_if = idealize_test(phase, this); 1373 if (idt_if != NULL) return idt_if; 1374 1375 // Try to split the IF 1376 PhaseIterGVN *igvn = phase->is_IterGVN(); 1377 Node *s = split_if(this, igvn); 1378 if (s != NULL) return s; 1379 1380 return NodeSentinel; 1381 } 1382 1383 //------------------------------Ideal------------------------------------------ 1384 // Return a node which is more "ideal" than the current node. Strip out 1385 // control copies 1386 Node* IfNode::Ideal(PhaseGVN *phase, bool can_reshape) { 1387 Node* res = Ideal_common(phase, can_reshape); 1388 if (res != NodeSentinel) { 1389 return res; 1390 } 1391 1392 // Check for people making a useless boolean: things like 1393 // if( (x < y ? true : false) ) { ... } 1394 // Replace with if( x < y ) { ... } 1395 Node *bol2 = remove_useless_bool(this, phase); 1396 if( bol2 ) return bol2; 1397 1398 if (in(0) == NULL) return NULL; // Dead loop? 1399 1400 PhaseIterGVN *igvn = phase->is_IterGVN(); 1401 Node* result = fold_compares(igvn); 1402 if (result != NULL) { 1403 return result; 1404 } 1405 1406 // Scan for an equivalent test 1407 Node *cmp; 1408 int dist = 0; // Cutoff limit for search 1409 int op = Opcode(); 1410 if( op == Op_If && 1411 (cmp=in(1)->in(1))->Opcode() == Op_CmpP ) { 1412 if( cmp->in(2) != NULL && // make sure cmp is not already dead 1413 cmp->in(2)->bottom_type() == TypePtr::NULL_PTR ) { 1414 dist = 64; // Limit for null-pointer scans 1415 } else { 1416 dist = 4; // Do not bother for random pointer tests 1417 } 1418 } else { 1419 dist = 4; // Limit for random junky scans 1420 } 1421 1422 Node* prev_dom = search_identical(dist); 1423 1424 if (prev_dom == NULL) { 1425 return NULL; 1426 } 1427 1428 // Replace dominated IfNode 1429 return dominated_by(prev_dom, igvn); 1430 } 1431 1432 //------------------------------dominated_by----------------------------------- 1433 Node* IfNode::dominated_by(Node* prev_dom, PhaseIterGVN *igvn) { 1434 #ifndef PRODUCT 1435 if (TraceIterativeGVN) { 1436 tty->print(" Removing IfNode: "); this->dump(); 1437 } 1438 if (VerifyOpto && !igvn->allow_progress()) { 1439 // Found an equivalent dominating test, 1440 // we can not guarantee reaching a fix-point for these during iterativeGVN 1441 // since intervening nodes may not change. 1442 return NULL; 1443 } 1444 #endif 1445 1446 igvn->hash_delete(this); // Remove self to prevent spurious V-N 1447 Node *idom = in(0); 1448 // Need opcode to decide which way 'this' test goes 1449 int prev_op = prev_dom->Opcode(); 1450 Node *top = igvn->C->top(); // Shortcut to top 1451 1452 // Loop predicates may have depending checks which should not 1453 // be skipped. For example, range check predicate has two checks 1454 // for lower and upper bounds. 1455 ProjNode* unc_proj = proj_out(1 - prev_dom->as_Proj()->_con)->as_Proj(); 1456 if (unc_proj->is_uncommon_trap_proj(Deoptimization::Reason_predicate) != NULL || 1457 unc_proj->is_uncommon_trap_proj(Deoptimization::Reason_profile_predicate) != NULL) { 1458 prev_dom = idom; 1459 } 1460 1461 // Now walk the current IfNode's projections. 1462 // Loop ends when 'this' has no more uses. 1463 for (DUIterator_Last imin, i = last_outs(imin); i >= imin; --i) { 1464 Node *ifp = last_out(i); // Get IfTrue/IfFalse 1465 igvn->add_users_to_worklist(ifp); 1466 // Check which projection it is and set target. 1467 // Data-target is either the dominating projection of the same type 1468 // or TOP if the dominating projection is of opposite type. 1469 // Data-target will be used as the new control edge for the non-CFG 1470 // nodes like Casts and Loads. 1471 Node *data_target = (ifp->Opcode() == prev_op) ? prev_dom : top; 1472 // Control-target is just the If's immediate dominator or TOP. 1473 Node *ctrl_target = (ifp->Opcode() == prev_op) ? idom : top; 1474 1475 // For each child of an IfTrue/IfFalse projection, reroute. 1476 // Loop ends when projection has no more uses. 1477 for (DUIterator_Last jmin, j = ifp->last_outs(jmin); j >= jmin; --j) { 1478 Node* s = ifp->last_out(j); // Get child of IfTrue/IfFalse 1479 if( !s->depends_only_on_test() ) { 1480 // Find the control input matching this def-use edge. 1481 // For Regions it may not be in slot 0. 1482 uint l; 1483 for( l = 0; s->in(l) != ifp; l++ ) { } 1484 igvn->replace_input_of(s, l, ctrl_target); 1485 } else { // Else, for control producers, 1486 igvn->replace_input_of(s, 0, data_target); // Move child to data-target 1487 } 1488 } // End for each child of a projection 1489 1490 igvn->remove_dead_node(ifp); 1491 } // End for each IfTrue/IfFalse child of If 1492 1493 // Kill the IfNode 1494 igvn->remove_dead_node(this); 1495 1496 // Must return either the original node (now dead) or a new node 1497 // (Do not return a top here, since that would break the uniqueness of top.) 1498 return new ConINode(TypeInt::ZERO); 1499 } 1500 1501 Node* IfNode::search_identical(int dist) { 1502 // Setup to scan up the CFG looking for a dominating test 1503 Node* dom = in(0); 1504 Node* prev_dom = this; 1505 int op = Opcode(); 1506 // Search up the dominator tree for an If with an identical test 1507 while (dom->Opcode() != op || // Not same opcode? 1508 dom->in(1) != in(1) || // Not same input 1? 1509 prev_dom->in(0) != dom) { // One path of test does not dominate? 1510 if (dist < 0) return NULL; 1511 1512 dist--; 1513 prev_dom = dom; 1514 dom = up_one_dom(dom); 1515 if (!dom) return NULL; 1516 } 1517 1518 // Check that we did not follow a loop back to ourselves 1519 if (this == dom) { 1520 return NULL; 1521 } 1522 1523 #ifndef PRODUCT 1524 if (dist > 2) { // Add to count of NULL checks elided 1525 explicit_null_checks_elided++; 1526 } 1527 #endif 1528 1529 return prev_dom; 1530 } 1531 1532 //------------------------------Identity--------------------------------------- 1533 // If the test is constant & we match, then we are the input Control 1534 Node* IfProjNode::Identity(PhaseGVN* phase) { 1535 // Can only optimize if cannot go the other way 1536 const TypeTuple *t = phase->type(in(0))->is_tuple(); 1537 if (t == TypeTuple::IFNEITHER || (always_taken(t) && 1538 // During parsing (GVN) we don't remove dead code aggressively. 1539 // Cut off dead branch and let PhaseRemoveUseless take care of it. 1540 (!phase->is_IterGVN() || 1541 // During IGVN, first wait for the dead branch to be killed. 1542 // Otherwise, the IfNode's control will have two control uses (the IfNode 1543 // that doesn't go away because it still has uses and this branch of the 1544 // If) which breaks other optimizations. Node::has_special_unique_user() 1545 // will cause this node to be reprocessed once the dead branch is killed. 1546 in(0)->outcnt() == 1))) { 1547 // IfNode control 1548 return in(0)->in(0); 1549 } 1550 // no progress 1551 return this; 1552 } 1553 1554 #ifndef PRODUCT 1555 //-------------------------------related--------------------------------------- 1556 // An IfProjNode's related node set consists of its input (an IfNode) including 1557 // the IfNode's condition, plus all of its outputs at level 1. In compact mode, 1558 // the restrictions for IfNode apply (see IfNode::rel). 1559 void IfProjNode::related(GrowableArray<Node*> *in_rel, GrowableArray<Node*> *out_rel, bool compact) const { 1560 Node* ifNode = this->in(0); 1561 in_rel->append(ifNode); 1562 if (compact) { 1563 ifNode->collect_nodes(in_rel, 3, false, true); 1564 } else { 1565 ifNode->collect_nodes_in_all_data(in_rel, false); 1566 } 1567 this->collect_nodes(out_rel, -1, false, false); 1568 } 1569 1570 //------------------------------dump_spec-------------------------------------- 1571 void IfNode::dump_spec(outputStream *st) const { 1572 st->print("P=%f, C=%f",_prob,_fcnt); 1573 } 1574 1575 //-------------------------------related--------------------------------------- 1576 // For an IfNode, the set of related output nodes is just the output nodes till 1577 // depth 2, i.e, the IfTrue/IfFalse projection nodes plus the nodes they refer. 1578 // The related input nodes contain no control nodes, but all data nodes 1579 // pertaining to the condition. In compact mode, the input nodes are collected 1580 // up to a depth of 3. 1581 void IfNode::related(GrowableArray <Node *> *in_rel, GrowableArray <Node *> *out_rel, bool compact) const { 1582 if (compact) { 1583 this->collect_nodes(in_rel, 3, false, true); 1584 } else { 1585 this->collect_nodes_in_all_data(in_rel, false); 1586 } 1587 this->collect_nodes(out_rel, -2, false, false); 1588 } 1589 #endif 1590 1591 //------------------------------idealize_test---------------------------------- 1592 // Try to canonicalize tests better. Peek at the Cmp/Bool/If sequence and 1593 // come up with a canonical sequence. Bools getting 'eq', 'gt' and 'ge' forms 1594 // converted to 'ne', 'le' and 'lt' forms. IfTrue/IfFalse get swapped as 1595 // needed. 1596 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff) { 1597 assert(iff->in(0) != NULL, "If must be live"); 1598 1599 if (iff->outcnt() != 2) return NULL; // Malformed projections. 1600 Node* old_if_f = iff->proj_out(false); 1601 Node* old_if_t = iff->proj_out(true); 1602 1603 // CountedLoopEnds want the back-control test to be TRUE, irregardless of 1604 // whether they are testing a 'gt' or 'lt' condition. The 'gt' condition 1605 // happens in count-down loops 1606 if (iff->is_CountedLoopEnd()) return NULL; 1607 if (!iff->in(1)->is_Bool()) return NULL; // Happens for partially optimized IF tests 1608 BoolNode *b = iff->in(1)->as_Bool(); 1609 BoolTest bt = b->_test; 1610 // Test already in good order? 1611 if( bt.is_canonical() ) 1612 return NULL; 1613 1614 // Flip test to be canonical. Requires flipping the IfFalse/IfTrue and 1615 // cloning the IfNode. 1616 Node* new_b = phase->transform( new BoolNode(b->in(1), bt.negate()) ); 1617 if( !new_b->is_Bool() ) return NULL; 1618 b = new_b->as_Bool(); 1619 1620 PhaseIterGVN *igvn = phase->is_IterGVN(); 1621 assert( igvn, "Test is not canonical in parser?" ); 1622 1623 // The IF node never really changes, but it needs to be cloned 1624 iff = iff->clone()->as_If(); 1625 iff->set_req(1, b); 1626 iff->_prob = 1.0-iff->_prob; 1627 1628 Node *prior = igvn->hash_find_insert(iff); 1629 if( prior ) { 1630 igvn->remove_dead_node(iff); 1631 iff = (IfNode*)prior; 1632 } else { 1633 // Cannot call transform on it just yet 1634 igvn->set_type_bottom(iff); 1635 } 1636 igvn->_worklist.push(iff); 1637 1638 // Now handle projections. Cloning not required. 1639 Node* new_if_f = (Node*)(new IfFalseNode( iff )); 1640 Node* new_if_t = (Node*)(new IfTrueNode ( iff )); 1641 1642 igvn->register_new_node_with_optimizer(new_if_f); 1643 igvn->register_new_node_with_optimizer(new_if_t); 1644 // Flip test, so flip trailing control 1645 igvn->replace_node(old_if_f, new_if_t); 1646 igvn->replace_node(old_if_t, new_if_f); 1647 1648 // Progress 1649 return iff; 1650 } 1651 1652 Node* RangeCheckNode::Ideal(PhaseGVN *phase, bool can_reshape) { 1653 Node* res = Ideal_common(phase, can_reshape); 1654 if (res != NodeSentinel) { 1655 return res; 1656 } 1657 1658 PhaseIterGVN *igvn = phase->is_IterGVN(); 1659 // Setup to scan up the CFG looking for a dominating test 1660 Node* prev_dom = this; 1661 1662 // Check for range-check vs other kinds of tests 1663 Node* index1; 1664 Node* range1; 1665 jint offset1; 1666 int flip1 = is_range_check(range1, index1, offset1); 1667 if (flip1) { 1668 Node* dom = in(0); 1669 // Try to remove extra range checks. All 'up_one_dom' gives up at merges 1670 // so all checks we inspect post-dominate the top-most check we find. 1671 // If we are going to fail the current check and we reach the top check 1672 // then we are guaranteed to fail, so just start interpreting there. 1673 // We 'expand' the top 3 range checks to include all post-dominating 1674 // checks. 1675 1676 // The top 3 range checks seen 1677 const int NRC =3; 1678 RangeCheck prev_checks[NRC]; 1679 int nb_checks = 0; 1680 1681 // Low and high offsets seen so far 1682 jint off_lo = offset1; 1683 jint off_hi = offset1; 1684 1685 bool found_immediate_dominator = false; 1686 1687 // Scan for the top checks and collect range of offsets 1688 for (int dist = 0; dist < 999; dist++) { // Range-Check scan limit 1689 if (dom->Opcode() == Op_RangeCheck && // Not same opcode? 1690 prev_dom->in(0) == dom) { // One path of test does dominate? 1691 if (dom == this) return NULL; // dead loop 1692 // See if this is a range check 1693 Node* index2; 1694 Node* range2; 1695 jint offset2; 1696 int flip2 = dom->as_RangeCheck()->is_range_check(range2, index2, offset2); 1697 // See if this is a _matching_ range check, checking against 1698 // the same array bounds. 1699 if (flip2 == flip1 && range2 == range1 && index2 == index1 && 1700 dom->outcnt() == 2) { 1701 if (nb_checks == 0 && dom->in(1) == in(1)) { 1702 // Found an immediately dominating test at the same offset. 1703 // This kind of back-to-back test can be eliminated locally, 1704 // and there is no need to search further for dominating tests. 1705 assert(offset2 == offset1, "Same test but different offsets"); 1706 found_immediate_dominator = true; 1707 break; 1708 } 1709 // Gather expanded bounds 1710 off_lo = MIN2(off_lo,offset2); 1711 off_hi = MAX2(off_hi,offset2); 1712 // Record top NRC range checks 1713 prev_checks[nb_checks%NRC].ctl = prev_dom; 1714 prev_checks[nb_checks%NRC].off = offset2; 1715 nb_checks++; 1716 } 1717 } 1718 prev_dom = dom; 1719 dom = up_one_dom(dom); 1720 if (!dom) break; 1721 } 1722 1723 if (!found_immediate_dominator) { 1724 // Attempt to widen the dominating range check to cover some later 1725 // ones. Since range checks "fail" by uncommon-trapping to the 1726 // interpreter, widening a check can make us speculatively enter 1727 // the interpreter. If we see range-check deopt's, do not widen! 1728 if (!phase->C->allow_range_check_smearing()) return NULL; 1729 1730 // Didn't find prior covering check, so cannot remove anything. 1731 if (nb_checks == 0) { 1732 return NULL; 1733 } 1734 // Constant indices only need to check the upper bound. 1735 // Non-constant indices must check both low and high. 1736 int chk0 = (nb_checks - 1) % NRC; 1737 if (index1) { 1738 if (nb_checks == 1) { 1739 return NULL; 1740 } else { 1741 // If the top range check's constant is the min or max of 1742 // all constants we widen the next one to cover the whole 1743 // range of constants. 1744 RangeCheck rc0 = prev_checks[chk0]; 1745 int chk1 = (nb_checks - 2) % NRC; 1746 RangeCheck rc1 = prev_checks[chk1]; 1747 if (rc0.off == off_lo) { 1748 adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn); 1749 prev_dom = rc1.ctl; 1750 } else if (rc0.off == off_hi) { 1751 adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn); 1752 prev_dom = rc1.ctl; 1753 } else { 1754 // If the top test's constant is not the min or max of all 1755 // constants, we need 3 range checks. We must leave the 1756 // top test unchanged because widening it would allow the 1757 // accesses it protects to successfully read/write out of 1758 // bounds. 1759 if (nb_checks == 2) { 1760 return NULL; 1761 } 1762 int chk2 = (nb_checks - 3) % NRC; 1763 RangeCheck rc2 = prev_checks[chk2]; 1764 // The top range check a+i covers interval: -a <= i < length-a 1765 // The second range check b+i covers interval: -b <= i < length-b 1766 if (rc1.off <= rc0.off) { 1767 // if b <= a, we change the second range check to: 1768 // -min_of_all_constants <= i < length-min_of_all_constants 1769 // Together top and second range checks now cover: 1770 // -min_of_all_constants <= i < length-a 1771 // which is more restrictive than -b <= i < length-b: 1772 // -b <= -min_of_all_constants <= i < length-a <= length-b 1773 // The third check is then changed to: 1774 // -max_of_all_constants <= i < length-max_of_all_constants 1775 // so 2nd and 3rd checks restrict allowed values of i to: 1776 // -min_of_all_constants <= i < length-max_of_all_constants 1777 adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn); 1778 adjust_check(rc2.ctl, range1, index1, flip1, off_hi, igvn); 1779 } else { 1780 // if b > a, we change the second range check to: 1781 // -max_of_all_constants <= i < length-max_of_all_constants 1782 // Together top and second range checks now cover: 1783 // -a <= i < length-max_of_all_constants 1784 // which is more restrictive than -b <= i < length-b: 1785 // -b < -a <= i < length-max_of_all_constants <= length-b 1786 // The third check is then changed to: 1787 // -max_of_all_constants <= i < length-max_of_all_constants 1788 // so 2nd and 3rd checks restrict allowed values of i to: 1789 // -min_of_all_constants <= i < length-max_of_all_constants 1790 adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn); 1791 adjust_check(rc2.ctl, range1, index1, flip1, off_lo, igvn); 1792 } 1793 prev_dom = rc2.ctl; 1794 } 1795 } 1796 } else { 1797 RangeCheck rc0 = prev_checks[chk0]; 1798 // 'Widen' the offset of the 1st and only covering check 1799 adjust_check(rc0.ctl, range1, index1, flip1, off_hi, igvn); 1800 // Test is now covered by prior checks, dominate it out 1801 prev_dom = rc0.ctl; 1802 } 1803 } 1804 } else { 1805 prev_dom = search_identical(4); 1806 1807 if (prev_dom == NULL) { 1808 return NULL; 1809 } 1810 } 1811 1812 // Replace dominated IfNode 1813 return dominated_by(prev_dom, igvn); 1814 }