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