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