1 /* 2 * Copyright (c) 2000, 2017, 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 default: 676 break; 677 } 678 const TypeInt* rtn_t = TypeInt::make(lo, hi, cmp2_t->_widen); 679 return rtn_t; 680 } 681 } 682 } 683 } 684 } 685 return NULL; 686 } 687 688 //------------------------------fold_compares---------------------------- 689 // See if a pair of CmpIs can be converted into a CmpU. In some cases 690 // the direction of this if is determined by the preceding if so it 691 // can be eliminate entirely. 692 // 693 // Given an if testing (CmpI n v) check for an immediately control 694 // dependent if that is testing (CmpI n v2) and has one projection 695 // leading to this if and the other projection leading to a region 696 // that merges one of this ifs control projections. 697 // 698 // If 699 // / | 700 // / | 701 // / | 702 // If | 703 // /\ | 704 // / \ | 705 // / \ | 706 // / Region 707 // 708 // Or given an if testing (CmpI n v) check for a dominating if that is 709 // testing (CmpI n v2), both having one projection leading to an 710 // uncommon trap. Allow Another independent guard in between to cover 711 // an explicit range check: 712 // if (index < 0 || index >= array.length) { 713 // which may need a null check to guard the LoadRange 714 // 715 // If 716 // / \ 717 // / \ 718 // / \ 719 // If unc 720 // /\ 721 // / \ 722 // / \ 723 // / unc 724 // 725 726 // Is the comparison for this If suitable for folding? 727 bool IfNode::cmpi_folds(PhaseIterGVN* igvn) { 728 return in(1) != NULL && 729 in(1)->is_Bool() && 730 in(1)->in(1) != NULL && 731 in(1)->in(1)->Opcode() == Op_CmpI && 732 in(1)->in(1)->in(2) != NULL && 733 in(1)->in(1)->in(2) != igvn->C->top() && 734 (in(1)->as_Bool()->_test.is_less() || 735 in(1)->as_Bool()->_test.is_greater()); 736 } 737 738 // Is a dominating control suitable for folding with this if? 739 bool IfNode::is_ctrl_folds(Node* ctrl, PhaseIterGVN* igvn) { 740 return ctrl != NULL && 741 ctrl->is_Proj() && 742 ctrl->in(0) != NULL && 743 ctrl->in(0)->Opcode() == Op_If && 744 ctrl->in(0)->outcnt() == 2 && 745 ctrl->in(0)->as_If()->cmpi_folds(igvn) && 746 // Must compare same value 747 ctrl->in(0)->in(1)->in(1)->in(1) != NULL && 748 ctrl->in(0)->in(1)->in(1)->in(1) == in(1)->in(1)->in(1); 749 } 750 751 // Do this If and the dominating If share a region? 752 bool IfNode::has_shared_region(ProjNode* proj, ProjNode*& success, ProjNode*& fail) { 753 ProjNode* otherproj = proj->other_if_proj(); 754 Node* otherproj_ctrl_use = otherproj->unique_ctrl_out(); 755 RegionNode* region = (otherproj_ctrl_use != NULL && otherproj_ctrl_use->is_Region()) ? otherproj_ctrl_use->as_Region() : NULL; 756 success = NULL; 757 fail = NULL; 758 759 if (otherproj->outcnt() == 1 && region != NULL && !region->has_phi()) { 760 for (int i = 0; i < 2; i++) { 761 ProjNode* proj = proj_out(i); 762 if (success == NULL && proj->outcnt() == 1 && proj->unique_out() == region) { 763 success = proj; 764 } else if (fail == NULL) { 765 fail = proj; 766 } else { 767 success = fail = NULL; 768 } 769 } 770 } 771 return success != NULL && fail != NULL; 772 } 773 774 // Return projection that leads to an uncommon trap if any 775 ProjNode* IfNode::uncommon_trap_proj(CallStaticJavaNode*& call) const { 776 for (int i = 0; i < 2; i++) { 777 call = proj_out(i)->is_uncommon_trap_proj(Deoptimization::Reason_none); 778 if (call != NULL) { 779 return proj_out(i); 780 } 781 } 782 return NULL; 783 } 784 785 // Do this If and the dominating If both branch out to an uncommon trap 786 bool IfNode::has_only_uncommon_traps(ProjNode* proj, ProjNode*& success, ProjNode*& fail, PhaseIterGVN* igvn) { 787 ProjNode* otherproj = proj->other_if_proj(); 788 CallStaticJavaNode* dom_unc = otherproj->is_uncommon_trap_proj(Deoptimization::Reason_none); 789 790 if (otherproj->outcnt() == 1 && dom_unc != NULL) { 791 // We need to re-execute the folded Ifs after deoptimization from the merged traps 792 if (!dom_unc->jvms()->should_reexecute()) { 793 return false; 794 } 795 796 CallStaticJavaNode* unc = NULL; 797 ProjNode* unc_proj = uncommon_trap_proj(unc); 798 if (unc_proj != NULL && unc_proj->outcnt() == 1) { 799 if (dom_unc == unc) { 800 // Allow the uncommon trap to be shared through a region 801 RegionNode* r = unc->in(0)->as_Region(); 802 if (r->outcnt() != 2 || r->req() != 3 || r->find_edge(otherproj) == -1 || r->find_edge(unc_proj) == -1) { 803 return false; 804 } 805 assert(r->has_phi() == NULL, "simple region shouldn't have a phi"); 806 } else if (dom_unc->in(0) != otherproj || unc->in(0) != unc_proj) { 807 return false; 808 } 809 810 // Different methods and methods containing jsrs are not supported. 811 ciMethod* method = unc->jvms()->method(); 812 ciMethod* dom_method = dom_unc->jvms()->method(); 813 if (method != dom_method || method->has_jsrs()) { 814 return false; 815 } 816 // Check that both traps are in the same activation of the method (instead 817 // of two activations being inlined through different call sites) by verifying 818 // that the call stacks are equal for both JVMStates. 819 JVMState* dom_caller = dom_unc->jvms()->caller(); 820 JVMState* caller = unc->jvms()->caller(); 821 if ((dom_caller == NULL) != (caller == NULL)) { 822 // The current method must either be inlined into both dom_caller and 823 // caller or must not be inlined at all (top method). Bail out otherwise. 824 return false; 825 } else if (dom_caller != NULL && !dom_caller->same_calls_as(caller)) { 826 return false; 827 } 828 // Check that the bci of the dominating uncommon trap dominates the bci 829 // of the dominated uncommon trap. Otherwise we may not re-execute 830 // the dominated check after deoptimization from the merged uncommon trap. 831 ciTypeFlow* flow = dom_method->get_flow_analysis(); 832 int bci = unc->jvms()->bci(); 833 int dom_bci = dom_unc->jvms()->bci(); 834 if (!flow->is_dominated_by(bci, dom_bci)) { 835 return false; 836 } 837 838 // See merge_uncommon_traps: the reason of the uncommon trap 839 // will be changed and the state of the dominating If will be 840 // used. Checked that we didn't apply this transformation in a 841 // previous compilation and it didn't cause too many traps 842 if (!igvn->C->too_many_traps(dom_method, dom_bci, Deoptimization::Reason_unstable_fused_if) && 843 !igvn->C->too_many_traps(dom_method, dom_bci, Deoptimization::Reason_range_check)) { 844 success = unc_proj; 845 fail = unc_proj->other_if_proj(); 846 return true; 847 } 848 } 849 } 850 return false; 851 } 852 853 // Check that the 2 CmpI can be folded into as single CmpU and proceed with the folding 854 bool IfNode::fold_compares_helper(ProjNode* proj, ProjNode* success, ProjNode* fail, PhaseIterGVN* igvn) { 855 Node* this_cmp = in(1)->in(1); 856 BoolNode* this_bool = in(1)->as_Bool(); 857 IfNode* dom_iff = proj->in(0)->as_If(); 858 BoolNode* dom_bool = dom_iff->in(1)->as_Bool(); 859 Node* lo = dom_iff->in(1)->in(1)->in(2); 860 Node* hi = this_cmp->in(2); 861 Node* n = this_cmp->in(1); 862 ProjNode* otherproj = proj->other_if_proj(); 863 864 const TypeInt* lo_type = IfNode::filtered_int_type(igvn, n, otherproj); 865 const TypeInt* hi_type = IfNode::filtered_int_type(igvn, n, success); 866 867 BoolTest::mask lo_test = dom_bool->_test._test; 868 BoolTest::mask hi_test = this_bool->_test._test; 869 BoolTest::mask cond = hi_test; 870 871 // convert: 872 // 873 // dom_bool = x {<,<=,>,>=} a 874 // / \ 875 // proj = {True,False} / \ otherproj = {False,True} 876 // / 877 // this_bool = x {<,<=} b 878 // / \ 879 // fail = {True,False} / \ success = {False,True} 880 // / 881 // 882 // (Second test guaranteed canonicalized, first one may not have 883 // been canonicalized yet) 884 // 885 // into: 886 // 887 // cond = (x - lo) {<u,<=u,>u,>=u} adjusted_lim 888 // / \ 889 // fail / \ success 890 // / 891 // 892 893 // Figure out which of the two tests sets the upper bound and which 894 // sets the lower bound if any. 895 Node* adjusted_lim = NULL; 896 if (hi_type->_lo > lo_type->_hi && hi_type->_hi == max_jint && lo_type->_lo == min_jint) { 897 assert((dom_bool->_test.is_less() && !proj->_con) || 898 (dom_bool->_test.is_greater() && proj->_con), "incorrect test"); 899 // this test was canonicalized 900 assert(this_bool->_test.is_less() && fail->_con, "incorrect test"); 901 902 // this_bool = < 903 // dom_bool = >= (proj = True) or dom_bool = < (proj = False) 904 // x in [a, b[ on the fail (= True) projection, b > a-1 (because of hi_type->_lo > lo_type->_hi test above): 905 // lo = a, hi = b, adjusted_lim = b-a, cond = <u 906 // dom_bool = > (proj = True) or dom_bool = <= (proj = False) 907 // x in ]a, b[ on the fail (= True) projection, b > a: 908 // lo = a+1, hi = b, adjusted_lim = b-a-1, cond = <u 909 // this_bool = <= 910 // dom_bool = >= (proj = True) or dom_bool = < (proj = False) 911 // x in [a, b] on the fail (= True) projection, b+1 > a-1: 912 // lo = a, hi = b, adjusted_lim = b-a+1, cond = <u 913 // lo = a, hi = b, adjusted_lim = b-a, cond = <=u doesn't work because b = a - 1 is possible, then b-a = -1 914 // dom_bool = > (proj = True) or dom_bool = <= (proj = False) 915 // x in ]a, b] on the fail (= True) projection b+1 > a: 916 // lo = a+1, hi = b, adjusted_lim = b-a, cond = <u 917 // 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 918 919 if (hi_test == BoolTest::lt) { 920 if (lo_test == BoolTest::gt || lo_test == BoolTest::le) { 921 lo = igvn->transform(new AddINode(lo, igvn->intcon(1))); 922 } 923 } else { 924 assert(hi_test == BoolTest::le, "bad test"); 925 if (lo_test == BoolTest::ge || lo_test == BoolTest::lt) { 926 adjusted_lim = igvn->transform(new SubINode(hi, lo)); 927 adjusted_lim = igvn->transform(new AddINode(adjusted_lim, igvn->intcon(1))); 928 cond = BoolTest::lt; 929 } else { 930 assert(lo_test == BoolTest::gt || lo_test == BoolTest::le, "bad test"); 931 adjusted_lim = igvn->transform(new SubINode(hi, lo)); 932 lo = igvn->transform(new AddINode(lo, igvn->intcon(1))); 933 cond = BoolTest::lt; 934 } 935 } 936 } else if (lo_type->_lo > hi_type->_hi && lo_type->_hi == max_jint && hi_type->_lo == min_jint) { 937 938 // this_bool = < 939 // dom_bool = < (proj = True) or dom_bool = >= (proj = False) 940 // x in [b, a[ on the fail (= False) projection, a > b-1 (because of lo_type->_lo > hi_type->_hi above): 941 // lo = b, hi = a, adjusted_lim = a-b, cond = >=u 942 // dom_bool = <= (proj = True) or dom_bool = > (proj = False) 943 // x in [b, a] on the fail (= False) projection, a+1 > b-1: 944 // lo = b, hi = a, adjusted_lim = a-b+1, cond = >=u 945 // lo = b, hi = a, adjusted_lim = a-b, cond = >u doesn't work because a = b - 1 is possible, then b-a = -1 946 // this_bool = <= 947 // dom_bool = < (proj = True) or dom_bool = >= (proj = False) 948 // x in ]b, a[ on the fail (= False) projection, a > b: 949 // lo = b+1, hi = a, adjusted_lim = a-b-1, cond = >=u 950 // dom_bool = <= (proj = True) or dom_bool = > (proj = False) 951 // x in ]b, a] on the fail (= False) projection, a+1 > b: 952 // lo = b+1, hi = a, adjusted_lim = a-b, cond = >=u 953 // 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 954 955 swap(lo, hi); 956 swap(lo_type, hi_type); 957 swap(lo_test, hi_test); 958 959 assert((dom_bool->_test.is_less() && proj->_con) || 960 (dom_bool->_test.is_greater() && !proj->_con), "incorrect test"); 961 // this test was canonicalized 962 assert(this_bool->_test.is_less() && !fail->_con, "incorrect test"); 963 964 cond = (hi_test == BoolTest::le || hi_test == BoolTest::gt) ? BoolTest::gt : BoolTest::ge; 965 966 if (lo_test == BoolTest::lt) { 967 if (hi_test == BoolTest::lt || hi_test == BoolTest::ge) { 968 cond = BoolTest::ge; 969 } else { 970 assert(hi_test == BoolTest::le || hi_test == BoolTest::gt, "bad test"); 971 adjusted_lim = igvn->transform(new SubINode(hi, lo)); 972 adjusted_lim = igvn->transform(new AddINode(adjusted_lim, igvn->intcon(1))); 973 cond = BoolTest::ge; 974 } 975 } else if (lo_test == BoolTest::le) { 976 if (hi_test == BoolTest::lt || hi_test == BoolTest::ge) { 977 lo = igvn->transform(new AddINode(lo, igvn->intcon(1))); 978 cond = BoolTest::ge; 979 } else { 980 assert(hi_test == BoolTest::le || hi_test == BoolTest::gt, "bad test"); 981 adjusted_lim = igvn->transform(new SubINode(hi, lo)); 982 lo = igvn->transform(new AddINode(lo, igvn->intcon(1))); 983 cond = BoolTest::ge; 984 } 985 } 986 } else { 987 const TypeInt* failtype = filtered_int_type(igvn, n, proj); 988 if (failtype != NULL) { 989 const TypeInt* type2 = filtered_int_type(igvn, n, fail); 990 if (type2 != NULL) { 991 failtype = failtype->join(type2)->is_int(); 992 if (failtype->_lo > failtype->_hi) { 993 // previous if determines the result of this if so 994 // replace Bool with constant 995 igvn->_worklist.push(in(1)); 996 igvn->replace_input_of(this, 1, igvn->intcon(success->_con)); 997 return true; 998 } 999 } 1000 } 1001 lo = NULL; 1002 hi = NULL; 1003 } 1004 1005 if (lo && hi) { 1006 // Merge the two compares into a single unsigned compare by building (CmpU (n - lo) (hi - lo)) 1007 Node* adjusted_val = igvn->transform(new SubINode(n, lo)); 1008 if (adjusted_lim == NULL) { 1009 adjusted_lim = igvn->transform(new SubINode(hi, lo)); 1010 } 1011 Node* newcmp = igvn->transform(new CmpUNode(adjusted_val, adjusted_lim)); 1012 Node* newbool = igvn->transform(new BoolNode(newcmp, cond)); 1013 1014 igvn->replace_input_of(dom_iff, 1, igvn->intcon(proj->_con)); 1015 igvn->_worklist.push(in(1)); 1016 igvn->replace_input_of(this, 1, newbool); 1017 1018 return true; 1019 } 1020 return false; 1021 } 1022 1023 // Merge the branches that trap for this If and the dominating If into 1024 // a single region that branches to the uncommon trap for the 1025 // dominating If 1026 Node* IfNode::merge_uncommon_traps(ProjNode* proj, ProjNode* success, ProjNode* fail, PhaseIterGVN* igvn) { 1027 Node* res = this; 1028 assert(success->in(0) == this, "bad projection"); 1029 1030 ProjNode* otherproj = proj->other_if_proj(); 1031 1032 CallStaticJavaNode* unc = success->is_uncommon_trap_proj(Deoptimization::Reason_none); 1033 CallStaticJavaNode* dom_unc = otherproj->is_uncommon_trap_proj(Deoptimization::Reason_none); 1034 1035 if (unc != dom_unc) { 1036 Node* r = new RegionNode(3); 1037 1038 r->set_req(1, otherproj); 1039 r->set_req(2, success); 1040 r = igvn->transform(r); 1041 assert(r->is_Region(), "can't go away"); 1042 1043 // Make both If trap at the state of the first If: once the CmpI 1044 // nodes are merged, if we trap we don't know which of the CmpI 1045 // nodes would have caused the trap so we have to restart 1046 // execution at the first one 1047 igvn->replace_input_of(dom_unc, 0, r); 1048 igvn->replace_input_of(unc, 0, igvn->C->top()); 1049 } 1050 int trap_request = dom_unc->uncommon_trap_request(); 1051 Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(trap_request); 1052 Deoptimization::DeoptAction action = Deoptimization::trap_request_action(trap_request); 1053 1054 int flip_test = 0; 1055 Node* l = NULL; 1056 Node* r = NULL; 1057 1058 if (success->in(0)->as_If()->range_check_trap_proj(flip_test, l, r) != NULL) { 1059 // If this looks like a range check, change the trap to 1060 // Reason_range_check so the compiler recognizes it as a range 1061 // check and applies the corresponding optimizations 1062 trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_range_check, action); 1063 1064 improve_address_types(l, r, fail, igvn); 1065 1066 res = igvn->transform(new RangeCheckNode(in(0), in(1), _prob, _fcnt)); 1067 } else if (unc != dom_unc) { 1068 // If we trap we won't know what CmpI would have caused the trap 1069 // so use a special trap reason to mark this pair of CmpI nodes as 1070 // bad candidate for folding. On recompilation we won't fold them 1071 // and we may trap again but this time we'll know what branch 1072 // traps 1073 trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_unstable_fused_if, action); 1074 } 1075 igvn->replace_input_of(dom_unc, TypeFunc::Parms, igvn->intcon(trap_request)); 1076 return res; 1077 } 1078 1079 // If we are turning 2 CmpI nodes into a CmpU that follows the pattern 1080 // of a rangecheck on index i, on 64 bit the compares may be followed 1081 // by memory accesses using i as index. In that case, the CmpU tells 1082 // us something about the values taken by i that can help the compiler 1083 // (see Compile::conv_I2X_index()) 1084 void IfNode::improve_address_types(Node* l, Node* r, ProjNode* fail, PhaseIterGVN* igvn) { 1085 #ifdef _LP64 1086 ResourceMark rm; 1087 Node_Stack stack(2); 1088 1089 assert(r->Opcode() == Op_LoadRange, "unexpected range check"); 1090 const TypeInt* array_size = igvn->type(r)->is_int(); 1091 1092 stack.push(l, 0); 1093 1094 while(stack.size() > 0) { 1095 Node* n = stack.node(); 1096 uint start = stack.index(); 1097 1098 uint i = start; 1099 for (; i < n->outcnt(); i++) { 1100 Node* use = n->raw_out(i); 1101 if (stack.size() == 1) { 1102 if (use->Opcode() == Op_ConvI2L) { 1103 const TypeLong* bounds = use->as_Type()->type()->is_long(); 1104 if (bounds->_lo <= array_size->_lo && bounds->_hi >= array_size->_hi && 1105 (bounds->_lo != array_size->_lo || bounds->_hi != array_size->_hi)) { 1106 stack.set_index(i+1); 1107 stack.push(use, 0); 1108 break; 1109 } 1110 } 1111 } else if (use->is_Mem()) { 1112 Node* ctrl = use->in(0); 1113 for (int i = 0; i < 10 && ctrl != NULL && ctrl != fail; i++) { 1114 ctrl = up_one_dom(ctrl); 1115 } 1116 if (ctrl == fail) { 1117 Node* init_n = stack.node_at(1); 1118 assert(init_n->Opcode() == Op_ConvI2L, "unexpected first node"); 1119 // Create a new narrow ConvI2L node that is dependent on the range check 1120 Node* new_n = igvn->C->conv_I2X_index(igvn, l, array_size, fail); 1121 1122 // The type of the ConvI2L may be widen and so the new 1123 // ConvI2L may not be better than an existing ConvI2L 1124 if (new_n != init_n) { 1125 for (uint j = 2; j < stack.size(); j++) { 1126 Node* n = stack.node_at(j); 1127 Node* clone = n->clone(); 1128 int rep = clone->replace_edge(init_n, new_n); 1129 assert(rep > 0, "can't find expected node?"); 1130 clone = igvn->transform(clone); 1131 init_n = n; 1132 new_n = clone; 1133 } 1134 igvn->hash_delete(use); 1135 int rep = use->replace_edge(init_n, new_n); 1136 assert(rep > 0, "can't find expected node?"); 1137 igvn->transform(use); 1138 if (init_n->outcnt() == 0) { 1139 igvn->_worklist.push(init_n); 1140 } 1141 } 1142 } 1143 } else if (use->in(0) == NULL && (igvn->type(use)->isa_long() || 1144 igvn->type(use)->isa_ptr())) { 1145 stack.set_index(i+1); 1146 stack.push(use, 0); 1147 break; 1148 } 1149 } 1150 if (i == n->outcnt()) { 1151 stack.pop(); 1152 } 1153 } 1154 #endif 1155 } 1156 1157 bool IfNode::is_cmp_with_loadrange(ProjNode* proj) { 1158 if (in(1) != NULL && 1159 in(1)->in(1) != NULL && 1160 in(1)->in(1)->in(2) != NULL) { 1161 Node* other = in(1)->in(1)->in(2); 1162 if (other->Opcode() == Op_LoadRange && 1163 ((other->in(0) != NULL && other->in(0) == proj) || 1164 (other->in(0) == NULL && 1165 other->in(2) != NULL && 1166 other->in(2)->is_AddP() && 1167 other->in(2)->in(1) != NULL && 1168 other->in(2)->in(1)->Opcode() == Op_CastPP && 1169 other->in(2)->in(1)->in(0) == proj))) { 1170 return true; 1171 } 1172 } 1173 return false; 1174 } 1175 1176 bool IfNode::is_null_check(ProjNode* proj, PhaseIterGVN* igvn) { 1177 Node* other = in(1)->in(1)->in(2); 1178 if (other->in(MemNode::Address) != NULL && 1179 proj->in(0)->in(1) != NULL && 1180 proj->in(0)->in(1)->is_Bool() && 1181 proj->in(0)->in(1)->in(1) != NULL && 1182 proj->in(0)->in(1)->in(1)->Opcode() == Op_CmpP && 1183 proj->in(0)->in(1)->in(1)->in(2) != NULL && 1184 proj->in(0)->in(1)->in(1)->in(1) == other->in(MemNode::Address)->in(AddPNode::Address)->uncast() && 1185 igvn->type(proj->in(0)->in(1)->in(1)->in(2)) == TypePtr::NULL_PTR) { 1186 return true; 1187 } 1188 return false; 1189 } 1190 1191 // Check that the If that is in between the 2 integer comparisons has 1192 // no side effect 1193 bool IfNode::is_side_effect_free_test(ProjNode* proj, PhaseIterGVN* igvn) { 1194 if (proj != NULL && 1195 proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none) && 1196 proj->outcnt() <= 2) { 1197 if (proj->outcnt() == 1 || 1198 // Allow simple null check from LoadRange 1199 (is_cmp_with_loadrange(proj) && is_null_check(proj, igvn))) { 1200 CallStaticJavaNode* unc = proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none); 1201 CallStaticJavaNode* dom_unc = proj->in(0)->in(0)->as_Proj()->is_uncommon_trap_if_pattern(Deoptimization::Reason_none); 1202 1203 // reroute_side_effect_free_unc changes the state of this 1204 // uncommon trap to restart execution at the previous 1205 // CmpI. Check that this change in a previous compilation didn't 1206 // cause too many traps. 1207 int trap_request = unc->uncommon_trap_request(); 1208 Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(trap_request); 1209 1210 if (igvn->C->too_many_traps(dom_unc->jvms()->method(), dom_unc->jvms()->bci(), reason)) { 1211 return false; 1212 } 1213 1214 return true; 1215 } 1216 } 1217 return false; 1218 } 1219 1220 // Make the If between the 2 integer comparisons trap at the state of 1221 // the first If: the last CmpI is the one replaced by a CmpU and the 1222 // first CmpI is eliminated, so the test between the 2 CmpI nodes 1223 // won't be guarded by the first CmpI anymore. It can trap in cases 1224 // where the first CmpI would have prevented it from executing: on a 1225 // trap, we need to restart execution at the state of the first CmpI 1226 void IfNode::reroute_side_effect_free_unc(ProjNode* proj, ProjNode* dom_proj, PhaseIterGVN* igvn) { 1227 CallStaticJavaNode* dom_unc = dom_proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none); 1228 ProjNode* otherproj = proj->other_if_proj(); 1229 CallStaticJavaNode* unc = proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none); 1230 Node* call_proj = dom_unc->unique_ctrl_out(); 1231 Node* halt = call_proj->unique_ctrl_out(); 1232 1233 Node* new_unc = dom_unc->clone(); 1234 call_proj = call_proj->clone(); 1235 halt = halt->clone(); 1236 Node* c = otherproj->clone(); 1237 1238 c = igvn->transform(c); 1239 new_unc->set_req(TypeFunc::Parms, unc->in(TypeFunc::Parms)); 1240 new_unc->set_req(0, c); 1241 new_unc = igvn->transform(new_unc); 1242 call_proj->set_req(0, new_unc); 1243 call_proj = igvn->transform(call_proj); 1244 halt->set_req(0, call_proj); 1245 halt = igvn->transform(halt); 1246 1247 igvn->replace_node(otherproj, igvn->C->top()); 1248 igvn->C->root()->add_req(halt); 1249 } 1250 1251 Node* IfNode::fold_compares(PhaseIterGVN* igvn) { 1252 if (Opcode() != Op_If) return NULL; 1253 1254 if (cmpi_folds(igvn)) { 1255 Node* ctrl = in(0); 1256 if (is_ctrl_folds(ctrl, igvn) && 1257 ctrl->outcnt() == 1) { 1258 // A integer comparison immediately dominated by another integer 1259 // comparison 1260 ProjNode* success = NULL; 1261 ProjNode* fail = NULL; 1262 ProjNode* dom_cmp = ctrl->as_Proj(); 1263 if (has_shared_region(dom_cmp, success, fail) && 1264 // Next call modifies graph so must be last 1265 fold_compares_helper(dom_cmp, success, fail, igvn)) { 1266 return this; 1267 } 1268 if (has_only_uncommon_traps(dom_cmp, success, fail, igvn) && 1269 // Next call modifies graph so must be last 1270 fold_compares_helper(dom_cmp, success, fail, igvn)) { 1271 return merge_uncommon_traps(dom_cmp, success, fail, igvn); 1272 } 1273 return NULL; 1274 } else if (ctrl->in(0) != NULL && 1275 ctrl->in(0)->in(0) != NULL) { 1276 ProjNode* success = NULL; 1277 ProjNode* fail = NULL; 1278 Node* dom = ctrl->in(0)->in(0); 1279 ProjNode* dom_cmp = dom->isa_Proj(); 1280 ProjNode* other_cmp = ctrl->isa_Proj(); 1281 1282 // Check if it's an integer comparison dominated by another 1283 // integer comparison with another test in between 1284 if (is_ctrl_folds(dom, igvn) && 1285 has_only_uncommon_traps(dom_cmp, success, fail, igvn) && 1286 is_side_effect_free_test(other_cmp, igvn) && 1287 // Next call modifies graph so must be last 1288 fold_compares_helper(dom_cmp, success, fail, igvn)) { 1289 reroute_side_effect_free_unc(other_cmp, dom_cmp, igvn); 1290 return merge_uncommon_traps(dom_cmp, success, fail, igvn); 1291 } 1292 } 1293 } 1294 return NULL; 1295 } 1296 1297 //------------------------------remove_useless_bool---------------------------- 1298 // Check for people making a useless boolean: things like 1299 // if( (x < y ? true : false) ) { ... } 1300 // Replace with if( x < y ) { ... } 1301 static Node *remove_useless_bool(IfNode *iff, PhaseGVN *phase) { 1302 Node *i1 = iff->in(1); 1303 if( !i1->is_Bool() ) return NULL; 1304 BoolNode *bol = i1->as_Bool(); 1305 1306 Node *cmp = bol->in(1); 1307 if( cmp->Opcode() != Op_CmpI ) return NULL; 1308 1309 // Must be comparing against a bool 1310 const Type *cmp2_t = phase->type( cmp->in(2) ); 1311 if( cmp2_t != TypeInt::ZERO && 1312 cmp2_t != TypeInt::ONE ) 1313 return NULL; 1314 1315 // Find a prior merge point merging the boolean 1316 i1 = cmp->in(1); 1317 if( !i1->is_Phi() ) return NULL; 1318 PhiNode *phi = i1->as_Phi(); 1319 if( phase->type( phi ) != TypeInt::BOOL ) 1320 return NULL; 1321 1322 // Check for diamond pattern 1323 int true_path = phi->is_diamond_phi(); 1324 if( true_path == 0 ) return NULL; 1325 1326 // Make sure that iff and the control of the phi are different. This 1327 // should really only happen for dead control flow since it requires 1328 // an illegal cycle. 1329 if (phi->in(0)->in(1)->in(0) == iff) return NULL; 1330 1331 // phi->region->if_proj->ifnode->bool->cmp 1332 BoolNode *bol2 = phi->in(0)->in(1)->in(0)->in(1)->as_Bool(); 1333 1334 // Now get the 'sense' of the test correct so we can plug in 1335 // either iff2->in(1) or its complement. 1336 int flip = 0; 1337 if( bol->_test._test == BoolTest::ne ) flip = 1-flip; 1338 else if( bol->_test._test != BoolTest::eq ) return NULL; 1339 if( cmp2_t == TypeInt::ZERO ) flip = 1-flip; 1340 1341 const Type *phi1_t = phase->type( phi->in(1) ); 1342 const Type *phi2_t = phase->type( phi->in(2) ); 1343 // Check for Phi(0,1) and flip 1344 if( phi1_t == TypeInt::ZERO ) { 1345 if( phi2_t != TypeInt::ONE ) return NULL; 1346 flip = 1-flip; 1347 } else { 1348 // Check for Phi(1,0) 1349 if( phi1_t != TypeInt::ONE ) return NULL; 1350 if( phi2_t != TypeInt::ZERO ) return NULL; 1351 } 1352 if( true_path == 2 ) { 1353 flip = 1-flip; 1354 } 1355 1356 Node* new_bol = (flip ? phase->transform( bol2->negate(phase) ) : bol2); 1357 assert(new_bol != iff->in(1), "must make progress"); 1358 iff->set_req(1, new_bol); 1359 // Intervening diamond probably goes dead 1360 phase->C->set_major_progress(); 1361 return iff; 1362 } 1363 1364 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff); 1365 1366 struct RangeCheck { 1367 Node* ctl; 1368 jint off; 1369 }; 1370 1371 Node* IfNode::Ideal_common(PhaseGVN *phase, bool can_reshape) { 1372 if (remove_dead_region(phase, can_reshape)) return this; 1373 // No Def-Use info? 1374 if (!can_reshape) return NULL; 1375 1376 // Don't bother trying to transform a dead if 1377 if (in(0)->is_top()) return NULL; 1378 // Don't bother trying to transform an if with a dead test 1379 if (in(1)->is_top()) return NULL; 1380 // Another variation of a dead test 1381 if (in(1)->is_Con()) return NULL; 1382 // Another variation of a dead if 1383 if (outcnt() < 2) return NULL; 1384 1385 // Canonicalize the test. 1386 Node* idt_if = idealize_test(phase, this); 1387 if (idt_if != NULL) return idt_if; 1388 1389 // Try to split the IF 1390 PhaseIterGVN *igvn = phase->is_IterGVN(); 1391 Node *s = split_if(this, igvn); 1392 if (s != NULL) return s; 1393 1394 return NodeSentinel; 1395 } 1396 1397 //------------------------------Ideal------------------------------------------ 1398 // Return a node which is more "ideal" than the current node. Strip out 1399 // control copies 1400 Node* IfNode::Ideal(PhaseGVN *phase, bool can_reshape) { 1401 Node* res = Ideal_common(phase, can_reshape); 1402 if (res != NodeSentinel) { 1403 return res; 1404 } 1405 1406 // Check for people making a useless boolean: things like 1407 // if( (x < y ? true : false) ) { ... } 1408 // Replace with if( x < y ) { ... } 1409 Node *bol2 = remove_useless_bool(this, phase); 1410 if( bol2 ) return bol2; 1411 1412 if (in(0) == NULL) return NULL; // Dead loop? 1413 1414 PhaseIterGVN *igvn = phase->is_IterGVN(); 1415 Node* result = fold_compares(igvn); 1416 if (result != NULL) { 1417 return result; 1418 } 1419 1420 // Scan for an equivalent test 1421 Node *cmp; 1422 int dist = 0; // Cutoff limit for search 1423 int op = Opcode(); 1424 if( op == Op_If && 1425 (cmp=in(1)->in(1))->Opcode() == Op_CmpP ) { 1426 if( cmp->in(2) != NULL && // make sure cmp is not already dead 1427 cmp->in(2)->bottom_type() == TypePtr::NULL_PTR ) { 1428 dist = 64; // Limit for null-pointer scans 1429 } else { 1430 dist = 4; // Do not bother for random pointer tests 1431 } 1432 } else { 1433 dist = 4; // Limit for random junky scans 1434 } 1435 1436 Node* prev_dom = search_identical(dist); 1437 1438 if (prev_dom == NULL) { 1439 return NULL; 1440 } 1441 1442 // Replace dominated IfNode 1443 return dominated_by(prev_dom, igvn); 1444 } 1445 1446 //------------------------------dominated_by----------------------------------- 1447 Node* IfNode::dominated_by(Node* prev_dom, PhaseIterGVN *igvn) { 1448 #ifndef PRODUCT 1449 if (TraceIterativeGVN) { 1450 tty->print(" Removing IfNode: "); this->dump(); 1451 } 1452 if (VerifyOpto && !igvn->allow_progress()) { 1453 // Found an equivalent dominating test, 1454 // we can not guarantee reaching a fix-point for these during iterativeGVN 1455 // since intervening nodes may not change. 1456 return NULL; 1457 } 1458 #endif 1459 1460 igvn->hash_delete(this); // Remove self to prevent spurious V-N 1461 Node *idom = in(0); 1462 // Need opcode to decide which way 'this' test goes 1463 int prev_op = prev_dom->Opcode(); 1464 Node *top = igvn->C->top(); // Shortcut to top 1465 1466 // Loop predicates may have depending checks which should not 1467 // be skipped. For example, range check predicate has two checks 1468 // for lower and upper bounds. 1469 ProjNode* unc_proj = proj_out(1 - prev_dom->as_Proj()->_con)->as_Proj(); 1470 if ((unc_proj != NULL) && (unc_proj->is_uncommon_trap_proj(Deoptimization::Reason_predicate) != NULL)) { 1471 prev_dom = idom; 1472 } 1473 1474 // Now walk the current IfNode's projections. 1475 // Loop ends when 'this' has no more uses. 1476 for (DUIterator_Last imin, i = last_outs(imin); i >= imin; --i) { 1477 Node *ifp = last_out(i); // Get IfTrue/IfFalse 1478 igvn->add_users_to_worklist(ifp); 1479 // Check which projection it is and set target. 1480 // Data-target is either the dominating projection of the same type 1481 // or TOP if the dominating projection is of opposite type. 1482 // Data-target will be used as the new control edge for the non-CFG 1483 // nodes like Casts and Loads. 1484 Node *data_target = (ifp->Opcode() == prev_op) ? prev_dom : top; 1485 // Control-target is just the If's immediate dominator or TOP. 1486 Node *ctrl_target = (ifp->Opcode() == prev_op) ? idom : top; 1487 1488 // For each child of an IfTrue/IfFalse projection, reroute. 1489 // Loop ends when projection has no more uses. 1490 for (DUIterator_Last jmin, j = ifp->last_outs(jmin); j >= jmin; --j) { 1491 Node* s = ifp->last_out(j); // Get child of IfTrue/IfFalse 1492 if( !s->depends_only_on_test() ) { 1493 // Find the control input matching this def-use edge. 1494 // For Regions it may not be in slot 0. 1495 uint l; 1496 for( l = 0; s->in(l) != ifp; l++ ) { } 1497 igvn->replace_input_of(s, l, ctrl_target); 1498 } else { // Else, for control producers, 1499 igvn->replace_input_of(s, 0, data_target); // Move child to data-target 1500 } 1501 } // End for each child of a projection 1502 1503 igvn->remove_dead_node(ifp); 1504 } // End for each IfTrue/IfFalse child of If 1505 1506 // Kill the IfNode 1507 igvn->remove_dead_node(this); 1508 1509 // Must return either the original node (now dead) or a new node 1510 // (Do not return a top here, since that would break the uniqueness of top.) 1511 return new ConINode(TypeInt::ZERO); 1512 } 1513 1514 Node* IfNode::search_identical(int dist) { 1515 // Setup to scan up the CFG looking for a dominating test 1516 Node* dom = in(0); 1517 Node* prev_dom = this; 1518 int op = Opcode(); 1519 // Search up the dominator tree for an If with an identical test 1520 while (dom->Opcode() != op || // Not same opcode? 1521 dom->in(1) != in(1) || // Not same input 1? 1522 (req() == 3 && dom->in(2) != in(2)) || // Not same input 2? 1523 prev_dom->in(0) != dom) { // One path of test does not dominate? 1524 if (dist < 0) return NULL; 1525 1526 dist--; 1527 prev_dom = dom; 1528 dom = up_one_dom(dom); 1529 if (!dom) return NULL; 1530 } 1531 1532 // Check that we did not follow a loop back to ourselves 1533 if (this == dom) { 1534 return NULL; 1535 } 1536 1537 #ifndef PRODUCT 1538 if (dist > 2) { // Add to count of NULL checks elided 1539 explicit_null_checks_elided++; 1540 } 1541 #endif 1542 1543 return prev_dom; 1544 } 1545 1546 //------------------------------Identity--------------------------------------- 1547 // If the test is constant & we match, then we are the input Control 1548 Node* IfProjNode::Identity(PhaseGVN* phase) { 1549 // Can only optimize if cannot go the other way 1550 const TypeTuple *t = phase->type(in(0))->is_tuple(); 1551 if (t == TypeTuple::IFNEITHER || (always_taken(t) && 1552 // During parsing (GVN) we don't remove dead code aggressively. 1553 // Cut off dead branch and let PhaseRemoveUseless take care of it. 1554 (!phase->is_IterGVN() || 1555 // During IGVN, first wait for the dead branch to be killed. 1556 // Otherwise, the IfNode's control will have two control uses (the IfNode 1557 // that doesn't go away because it still has uses and this branch of the 1558 // If) which breaks other optimizations. Node::has_special_unique_user() 1559 // will cause this node to be reprocessed once the dead branch is killed. 1560 in(0)->outcnt() == 1))) { 1561 // IfNode control 1562 return in(0)->in(0); 1563 } 1564 // no progress 1565 return this; 1566 } 1567 1568 #ifndef PRODUCT 1569 //-------------------------------related--------------------------------------- 1570 // An IfProjNode's related node set consists of its input (an IfNode) including 1571 // the IfNode's condition, plus all of its outputs at level 1. In compact mode, 1572 // the restrictions for IfNode apply (see IfNode::rel). 1573 void IfProjNode::related(GrowableArray<Node*> *in_rel, GrowableArray<Node*> *out_rel, bool compact) const { 1574 Node* ifNode = this->in(0); 1575 in_rel->append(ifNode); 1576 if (compact) { 1577 ifNode->collect_nodes(in_rel, 3, false, true); 1578 } else { 1579 ifNode->collect_nodes_in_all_data(in_rel, false); 1580 } 1581 this->collect_nodes(out_rel, -1, false, false); 1582 } 1583 1584 //------------------------------dump_spec-------------------------------------- 1585 void IfNode::dump_spec(outputStream *st) const { 1586 st->print("P=%f, C=%f",_prob,_fcnt); 1587 } 1588 1589 //-------------------------------related--------------------------------------- 1590 // For an IfNode, the set of related output nodes is just the output nodes till 1591 // depth 2, i.e, the IfTrue/IfFalse projection nodes plus the nodes they refer. 1592 // The related input nodes contain no control nodes, but all data nodes 1593 // pertaining to the condition. In compact mode, the input nodes are collected 1594 // up to a depth of 3. 1595 void IfNode::related(GrowableArray <Node *> *in_rel, GrowableArray <Node *> *out_rel, bool compact) const { 1596 if (compact) { 1597 this->collect_nodes(in_rel, 3, false, true); 1598 } else { 1599 this->collect_nodes_in_all_data(in_rel, false); 1600 } 1601 this->collect_nodes(out_rel, -2, false, false); 1602 } 1603 #endif 1604 1605 //------------------------------idealize_test---------------------------------- 1606 // Try to canonicalize tests better. Peek at the Cmp/Bool/If sequence and 1607 // come up with a canonical sequence. Bools getting 'eq', 'gt' and 'ge' forms 1608 // converted to 'ne', 'le' and 'lt' forms. IfTrue/IfFalse get swapped as 1609 // needed. 1610 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff) { 1611 assert(iff->in(0) != NULL, "If must be live"); 1612 1613 if (iff->outcnt() != 2) return NULL; // Malformed projections. 1614 Node* old_if_f = iff->proj_out(false); 1615 Node* old_if_t = iff->proj_out(true); 1616 1617 // CountedLoopEnds want the back-control test to be TRUE, irregardless of 1618 // whether they are testing a 'gt' or 'lt' condition. The 'gt' condition 1619 // happens in count-down loops 1620 if (iff->is_CountedLoopEnd()) return NULL; 1621 if (!iff->in(1)->is_Bool()) return NULL; // Happens for partially optimized IF tests 1622 BoolNode *b = iff->in(1)->as_Bool(); 1623 BoolTest bt = b->_test; 1624 // Test already in good order? 1625 if( bt.is_canonical() ) 1626 return NULL; 1627 1628 // Flip test to be canonical. Requires flipping the IfFalse/IfTrue and 1629 // cloning the IfNode. 1630 Node* new_b = phase->transform( new BoolNode(b->in(1), bt.negate()) ); 1631 if( !new_b->is_Bool() ) return NULL; 1632 b = new_b->as_Bool(); 1633 1634 PhaseIterGVN *igvn = phase->is_IterGVN(); 1635 assert( igvn, "Test is not canonical in parser?" ); 1636 1637 // The IF node never really changes, but it needs to be cloned 1638 iff = iff->clone()->as_If(); 1639 iff->set_req(1, b); 1640 iff->_prob = 1.0-iff->_prob; 1641 1642 Node *prior = igvn->hash_find_insert(iff); 1643 if( prior ) { 1644 igvn->remove_dead_node(iff); 1645 iff = (IfNode*)prior; 1646 } else { 1647 // Cannot call transform on it just yet 1648 igvn->set_type_bottom(iff); 1649 } 1650 igvn->_worklist.push(iff); 1651 1652 // Now handle projections. Cloning not required. 1653 Node* new_if_f = (Node*)(new IfFalseNode( iff )); 1654 Node* new_if_t = (Node*)(new IfTrueNode ( iff )); 1655 1656 igvn->register_new_node_with_optimizer(new_if_f); 1657 igvn->register_new_node_with_optimizer(new_if_t); 1658 // Flip test, so flip trailing control 1659 igvn->replace_node(old_if_f, new_if_t); 1660 igvn->replace_node(old_if_t, new_if_f); 1661 1662 // Progress 1663 return iff; 1664 } 1665 1666 Node* RangeCheckNode::Ideal(PhaseGVN *phase, bool can_reshape) { 1667 Node* res = Ideal_common(phase, can_reshape); 1668 if (res != NodeSentinel) { 1669 return res; 1670 } 1671 1672 PhaseIterGVN *igvn = phase->is_IterGVN(); 1673 // Setup to scan up the CFG looking for a dominating test 1674 Node* prev_dom = this; 1675 1676 // Check for range-check vs other kinds of tests 1677 Node* index1; 1678 Node* range1; 1679 jint offset1; 1680 int flip1 = is_range_check(range1, index1, offset1); 1681 if (flip1) { 1682 Node* dom = in(0); 1683 // Try to remove extra range checks. All 'up_one_dom' gives up at merges 1684 // so all checks we inspect post-dominate the top-most check we find. 1685 // If we are going to fail the current check and we reach the top check 1686 // then we are guaranteed to fail, so just start interpreting there. 1687 // We 'expand' the top 3 range checks to include all post-dominating 1688 // checks. 1689 1690 // The top 3 range checks seen 1691 const int NRC =3; 1692 RangeCheck prev_checks[NRC]; 1693 int nb_checks = 0; 1694 1695 // Low and high offsets seen so far 1696 jint off_lo = offset1; 1697 jint off_hi = offset1; 1698 1699 bool found_immediate_dominator = false; 1700 1701 // Scan for the top checks and collect range of offsets 1702 for (int dist = 0; dist < 999; dist++) { // Range-Check scan limit 1703 if (dom->Opcode() == Op_RangeCheck && // Not same opcode? 1704 prev_dom->in(0) == dom) { // One path of test does dominate? 1705 if (dom == this) return NULL; // dead loop 1706 // See if this is a range check 1707 Node* index2; 1708 Node* range2; 1709 jint offset2; 1710 int flip2 = dom->as_RangeCheck()->is_range_check(range2, index2, offset2); 1711 // See if this is a _matching_ range check, checking against 1712 // the same array bounds. 1713 if (flip2 == flip1 && range2 == range1 && index2 == index1 && 1714 dom->outcnt() == 2) { 1715 if (nb_checks == 0 && dom->in(1) == in(1)) { 1716 // Found an immediately dominating test at the same offset. 1717 // This kind of back-to-back test can be eliminated locally, 1718 // and there is no need to search further for dominating tests. 1719 assert(offset2 == offset1, "Same test but different offsets"); 1720 found_immediate_dominator = true; 1721 break; 1722 } 1723 // Gather expanded bounds 1724 off_lo = MIN2(off_lo,offset2); 1725 off_hi = MAX2(off_hi,offset2); 1726 // Record top NRC range checks 1727 prev_checks[nb_checks%NRC].ctl = prev_dom; 1728 prev_checks[nb_checks%NRC].off = offset2; 1729 nb_checks++; 1730 } 1731 } 1732 prev_dom = dom; 1733 dom = up_one_dom(dom); 1734 if (!dom) break; 1735 } 1736 1737 if (!found_immediate_dominator) { 1738 // Attempt to widen the dominating range check to cover some later 1739 // ones. Since range checks "fail" by uncommon-trapping to the 1740 // interpreter, widening a check can make us speculatively enter 1741 // the interpreter. If we see range-check deopt's, do not widen! 1742 if (!phase->C->allow_range_check_smearing()) return NULL; 1743 1744 // Didn't find prior covering check, so cannot remove anything. 1745 if (nb_checks == 0) { 1746 return NULL; 1747 } 1748 // Constant indices only need to check the upper bound. 1749 // Non-constant indices must check both low and high. 1750 int chk0 = (nb_checks - 1) % NRC; 1751 if (index1) { 1752 if (nb_checks == 1) { 1753 return NULL; 1754 } else { 1755 // If the top range check's constant is the min or max of 1756 // all constants we widen the next one to cover the whole 1757 // range of constants. 1758 RangeCheck rc0 = prev_checks[chk0]; 1759 int chk1 = (nb_checks - 2) % NRC; 1760 RangeCheck rc1 = prev_checks[chk1]; 1761 if (rc0.off == off_lo) { 1762 adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn); 1763 prev_dom = rc1.ctl; 1764 } else if (rc0.off == off_hi) { 1765 adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn); 1766 prev_dom = rc1.ctl; 1767 } else { 1768 // If the top test's constant is not the min or max of all 1769 // constants, we need 3 range checks. We must leave the 1770 // top test unchanged because widening it would allow the 1771 // accesses it protects to successfully read/write out of 1772 // bounds. 1773 if (nb_checks == 2) { 1774 return NULL; 1775 } 1776 int chk2 = (nb_checks - 3) % NRC; 1777 RangeCheck rc2 = prev_checks[chk2]; 1778 // The top range check a+i covers interval: -a <= i < length-a 1779 // The second range check b+i covers interval: -b <= i < length-b 1780 if (rc1.off <= rc0.off) { 1781 // if b <= a, we change the second range check to: 1782 // -min_of_all_constants <= i < length-min_of_all_constants 1783 // Together top and second range checks now cover: 1784 // -min_of_all_constants <= i < length-a 1785 // which is more restrictive than -b <= i < length-b: 1786 // -b <= -min_of_all_constants <= i < length-a <= length-b 1787 // The third check is then changed to: 1788 // -max_of_all_constants <= i < length-max_of_all_constants 1789 // so 2nd and 3rd checks restrict allowed values of i to: 1790 // -min_of_all_constants <= i < length-max_of_all_constants 1791 adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn); 1792 adjust_check(rc2.ctl, range1, index1, flip1, off_hi, igvn); 1793 } else { 1794 // if b > a, we change the second range check to: 1795 // -max_of_all_constants <= i < length-max_of_all_constants 1796 // Together top and second range checks now cover: 1797 // -a <= i < length-max_of_all_constants 1798 // which is more restrictive than -b <= i < length-b: 1799 // -b < -a <= i < length-max_of_all_constants <= length-b 1800 // The third check is then changed to: 1801 // -max_of_all_constants <= i < length-max_of_all_constants 1802 // so 2nd and 3rd checks restrict allowed values of i to: 1803 // -min_of_all_constants <= i < length-max_of_all_constants 1804 adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn); 1805 adjust_check(rc2.ctl, range1, index1, flip1, off_lo, igvn); 1806 } 1807 prev_dom = rc2.ctl; 1808 } 1809 } 1810 } else { 1811 RangeCheck rc0 = prev_checks[chk0]; 1812 // 'Widen' the offset of the 1st and only covering check 1813 adjust_check(rc0.ctl, range1, index1, flip1, off_hi, igvn); 1814 // Test is now covered by prior checks, dominate it out 1815 prev_dom = rc0.ctl; 1816 } 1817 } 1818 } else { 1819 prev_dom = search_identical(4); 1820 1821 if (prev_dom == NULL) { 1822 return NULL; 1823 } 1824 } 1825 1826 // Replace dominated IfNode 1827 return dominated_by(prev_dom, igvn); 1828 }