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