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