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