1 #ifdef USE_PRAGMA_IDENT_SRC 2 #pragma ident "@(#)ifnode.cpp 1.63 07/10/23 13:12:51 JVM" 3 #endif 4 /* 5 * Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved. 6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 7 * 8 * This code is free software; you can redistribute it and/or modify it 9 * under the terms of the GNU General Public License version 2 only, as 10 * published by the Free Software Foundation. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 23 * CA 95054 USA or visit www.sun.com if you need additional information or 24 * have any questions. 25 * 26 */ 27 28 // Portions of code courtesy of Clifford Click 29 30 // Optimization - Graph Style 31 32 #include "incls/_precompiled.incl" 33 #include "incls/_ifnode.cpp.incl" 34 35 36 extern int explicit_null_checks_elided; 37 38 //============================================================================= 39 //------------------------------Value------------------------------------------ 40 // Return a tuple for whichever arm of the IF is reachable 41 const Type *IfNode::Value( PhaseTransform *phase ) const { 42 if( !in(0) ) return Type::TOP; 43 if( phase->type(in(0)) == Type::TOP ) 44 return Type::TOP; 45 const Type *t = phase->type(in(1)); 46 if( t == Type::TOP ) // data is undefined 47 return TypeTuple::IFNEITHER; // unreachable altogether 48 if( t == TypeInt::ZERO ) // zero, or false 49 return TypeTuple::IFFALSE; // only false branch is reachable 50 if( t == TypeInt::ONE ) // 1, or true 51 return TypeTuple::IFTRUE; // only true branch is reachable 52 assert( t == TypeInt::BOOL, "expected boolean type" ); 53 54 return TypeTuple::IFBOTH; // No progress 55 } 56 57 const RegMask &IfNode::out_RegMask() const { 58 return RegMask::Empty; 59 } 60 61 //------------------------------split_if--------------------------------------- 62 // Look for places where we merge constants, then test on the merged value. 63 // If the IF test will be constant folded on the path with the constant, we 64 // win by splitting the IF to before the merge point. 65 static Node* split_if(IfNode *iff, PhaseIterGVN *igvn) { 66 // I could be a lot more general here, but I'm trying to squeeze this 67 // in before the Christmas '98 break so I'm gonna be kinda restrictive 68 // on the patterns I accept. CNC 69 70 // Look for a compare of a constant and a merged value 71 Node *i1 = iff->in(1); 72 if( !i1->is_Bool() ) return NULL; 73 BoolNode *b = i1->as_Bool(); 74 Node *cmp = b->in(1); 75 if( !cmp->is_Cmp() ) return NULL; 76 i1 = cmp->in(1); 77 if( i1 == NULL || !i1->is_Phi() ) return NULL; 78 PhiNode *phi = i1->as_Phi(); 79 if( phi->is_copy() ) return NULL; 80 Node *con2 = cmp->in(2); 81 if( !con2->is_Con() ) return NULL; 82 // See that the merge point contains some constants 83 Node *con1=NULL; 84 uint i4; 85 for( i4 = 1; i4 < phi->req(); i4++ ) { 86 con1 = phi->in(i4); 87 if( !con1 ) return NULL; // Do not optimize partially collaped merges 88 if( con1->is_Con() ) break; // Found a constant 89 // Also allow null-vs-not-null checks 90 const TypePtr *tp = igvn->type(con1)->isa_ptr(); 91 if( tp && tp->_ptr == TypePtr::NotNull ) 92 break; 93 } 94 if( i4 >= phi->req() ) return NULL; // Found no constants 95 96 igvn->C->set_has_split_ifs(true); // Has chance for split-if 97 98 // Make sure that the compare can be constant folded away 99 Node *cmp2 = cmp->clone(); 100 cmp2->set_req(1,con1); 101 cmp2->set_req(2,con2); 102 const Type *t = cmp2->Value(igvn); 103 // This compare is dead, so whack it! 104 igvn->remove_dead_node(cmp2); 105 if( !t->singleton() ) return NULL; 106 107 // No intervening control, like a simple Call 108 Node *r = iff->in(0); 109 if( !r->is_Region() ) return NULL; 110 if( phi->region() != r ) return NULL; 111 // No other users of the cmp/bool 112 if (b->outcnt() != 1 || cmp->outcnt() != 1) { 113 //tty->print_cr("many users of cmp/bool"); 114 return NULL; 115 } 116 117 // Make sure we can determine where all the uses of merged values go 118 for (DUIterator_Fast jmax, j = r->fast_outs(jmax); j < jmax; j++) { 119 Node* u = r->fast_out(j); 120 if( u == r ) continue; 121 if( u == iff ) continue; 122 if( u->outcnt() == 0 ) continue; // use is dead & ignorable 123 if( !u->is_Phi() ) { 124 /* 125 if( u->is_Start() ) { 126 tty->print_cr("Region has inlined start use"); 127 } else { 128 tty->print_cr("Region has odd use"); 129 u->dump(2); 130 }*/ 131 return NULL; 132 } 133 if( u != phi ) { 134 // CNC - do not allow any other merged value 135 //tty->print_cr("Merging another value"); 136 //u->dump(2); 137 return NULL; 138 } 139 // Make sure we can account for all Phi uses 140 for (DUIterator_Fast kmax, k = u->fast_outs(kmax); k < kmax; k++) { 141 Node* v = u->fast_out(k); // User of the phi 142 // CNC - Allow only really simple patterns. 143 // In particular I disallow AddP of the Phi, a fairly common pattern 144 if( v == cmp ) continue; // The compare is OK 145 if( (v->is_ConstraintCast()) && 146 v->in(0)->in(0) == iff ) 147 continue; // CastPP/II of the IfNode is OK 148 // Disabled following code because I cannot tell if exactly one 149 // path dominates without a real dominator check. CNC 9/9/1999 150 //uint vop = v->Opcode(); 151 //if( vop == Op_Phi ) { // Phi from another merge point might be OK 152 // Node *r = v->in(0); // Get controlling point 153 // if( !r ) return NULL; // Degraded to a copy 154 // // Find exactly one path in (either True or False doms, but not IFF) 155 // int cnt = 0; 156 // for( uint i = 1; i < r->req(); i++ ) 157 // if( r->in(i) && r->in(i)->in(0) == iff ) 158 // cnt++; 159 // if( cnt == 1 ) continue; // Exactly one of True or False guards Phi 160 //} 161 if( !v->is_Call() ) { 162 /* 163 if( v->Opcode() == Op_AddP ) { 164 tty->print_cr("Phi has AddP use"); 165 } else if( v->Opcode() == Op_CastPP ) { 166 tty->print_cr("Phi has CastPP use"); 167 } else if( v->Opcode() == Op_CastII ) { 168 tty->print_cr("Phi has CastII use"); 169 } else { 170 tty->print_cr("Phi has use I cant be bothered with"); 171 } 172 */ 173 } 174 return NULL; 175 176 /* CNC - Cut out all the fancy acceptance tests 177 // Can we clone this use when doing the transformation? 178 // If all uses are from Phis at this merge or constants, then YES. 179 if( !v->in(0) && v != cmp ) { 180 tty->print_cr("Phi has free-floating use"); 181 v->dump(2); 182 return NULL; 183 } 184 for( uint l = 1; l < v->req(); l++ ) { 185 if( (!v->in(l)->is_Phi() || v->in(l)->in(0) != r) && 186 !v->in(l)->is_Con() ) { 187 tty->print_cr("Phi has use"); 188 v->dump(2); 189 return NULL; 190 } // End of if Phi-use input is neither Phi nor Constant 191 } // End of for all inputs to Phi-use 192 */ 193 } // End of for all uses of Phi 194 } // End of for all uses of Region 195 196 // Only do this if the IF node is in a sane state 197 if (iff->outcnt() != 2) 198 return NULL; 199 200 // Got a hit! Do the Mondo Hack! 201 // 202 //ABC a1c def ghi B 1 e h A C a c d f g i 203 // R - Phi - Phi - Phi Rc - Phi - Phi - Phi Rx - Phi - Phi - Phi 204 // cmp - 2 cmp - 2 cmp - 2 205 // bool bool_c bool_x 206 // if if_c if_x 207 // T F T F T F 208 // ..s.. ..t .. ..s.. ..t.. ..s.. ..t.. 209 // 210 // Split the paths coming into the merge point into 2 seperate groups of 211 // merges. On the left will be all the paths feeding constants into the 212 // Cmp's Phi. On the right will be the remaining paths. The Cmp's Phi 213 // will fold up into a constant; this will let the Cmp fold up as well as 214 // all the control flow. Below the original IF we have 2 control 215 // dependent regions, 's' and 't'. Now we will merge the two paths 216 // just prior to 's' and 't' from the two IFs. At least 1 path (and quite 217 // likely 2 or more) will promptly constant fold away. 218 PhaseGVN *phase = igvn; 219 220 // Make a region merging constants and a region merging the rest 221 uint req_c = 0; 222 for (uint ii = 1; ii < r->req(); ii++) { 223 if( phi->in(ii) == con1 ) { 224 req_c++; 225 } 226 } 227 Node *region_c = new (igvn->C, req_c + 1) RegionNode(req_c + 1); 228 Node *phi_c = con1; 229 uint len = r->req(); 230 Node *region_x = new (igvn->C, len - req_c + 1) RegionNode(len - req_c + 1); 231 Node *phi_x = PhiNode::make_blank(region_x, phi); 232 for (uint i = 1, i_c = 1, i_x = 1; i < len; i++) { 233 if( phi->in(i) == con1 ) { 234 region_c->init_req( i_c++, r ->in(i) ); 235 } else { 236 region_x->init_req( i_x, r ->in(i) ); 237 phi_x ->init_req( i_x++, phi->in(i) ); 238 } 239 } 240 241 // Register the new RegionNodes but do not transform them. Cannot 242 // transform until the entire Region/Phi conglerate has been hacked 243 // as a single huge transform. 244 igvn->register_new_node_with_optimizer( region_c ); 245 igvn->register_new_node_with_optimizer( region_x ); 246 phi_x = phase->transform( phi_x ); 247 // Prevent the untimely death of phi_x. Currently he has no uses. He is 248 // about to get one. If this only use goes away, then phi_x will look dead. 249 // However, he will be picking up some more uses down below. 250 Node *hook = new (igvn->C, 4) Node(4); 251 hook->init_req(0, phi_x); 252 hook->init_req(1, phi_c); 253 254 // Make the compare 255 Node *cmp_c = phase->makecon(t); 256 Node *cmp_x = cmp->clone(); 257 cmp_x->set_req(1,phi_x); 258 cmp_x->set_req(2,con2); 259 cmp_x = phase->transform(cmp_x); 260 // Make the bool 261 Node *b_c = phase->transform(new (igvn->C, 2) BoolNode(cmp_c,b->_test._test)); 262 Node *b_x = phase->transform(new (igvn->C, 2) BoolNode(cmp_x,b->_test._test)); 263 // Make the IfNode 264 IfNode *iff_c = new (igvn->C, 2) IfNode(region_c,b_c,iff->_prob,iff->_fcnt); 265 igvn->set_type_bottom(iff_c); 266 igvn->_worklist.push(iff_c); 267 hook->init_req(2, iff_c); 268 269 IfNode *iff_x = new (igvn->C, 2) IfNode(region_x,b_x,iff->_prob, iff->_fcnt); 270 igvn->set_type_bottom(iff_x); 271 igvn->_worklist.push(iff_x); 272 hook->init_req(3, iff_x); 273 274 // Make the true/false arms 275 Node *iff_c_t = phase->transform(new (igvn->C, 1) IfTrueNode (iff_c)); 276 Node *iff_c_f = phase->transform(new (igvn->C, 1) IfFalseNode(iff_c)); 277 Node *iff_x_t = phase->transform(new (igvn->C, 1) IfTrueNode (iff_x)); 278 Node *iff_x_f = phase->transform(new (igvn->C, 1) IfFalseNode(iff_x)); 279 280 // Merge the TRUE paths 281 Node *region_s = new (igvn->C, 3) RegionNode(3); 282 igvn->_worklist.push(region_s); 283 region_s->init_req(1, iff_c_t); 284 region_s->init_req(2, iff_x_t); 285 igvn->register_new_node_with_optimizer( region_s ); 286 287 // Merge the FALSE paths 288 Node *region_f = new (igvn->C, 3) RegionNode(3); 289 igvn->_worklist.push(region_f); 290 region_f->init_req(1, iff_c_f); 291 region_f->init_req(2, iff_x_f); 292 igvn->register_new_node_with_optimizer( region_f ); 293 294 igvn->hash_delete(cmp);// Remove soon-to-be-dead node from hash table. 295 cmp->set_req(1,NULL); // Whack the inputs to cmp because it will be dead 296 cmp->set_req(2,NULL); 297 // Check for all uses of the Phi and give them a new home. 298 // The 'cmp' got cloned, but CastPP/IIs need to be moved. 299 Node *phi_s = NULL; // do not construct unless needed 300 Node *phi_f = NULL; // do not construct unless needed 301 for (DUIterator_Last i2min, i2 = phi->last_outs(i2min); i2 >= i2min; --i2) { 302 Node* v = phi->last_out(i2);// User of the phi 303 igvn->hash_delete(v); // Have to fixup other Phi users 304 igvn->_worklist.push(v); 305 uint vop = v->Opcode(); 306 Node *proj = NULL; 307 if( vop == Op_Phi ) { // Remote merge point 308 Node *r = v->in(0); 309 for (uint i3 = 1; i3 < r->req(); i3++) 310 if (r->in(i3) && r->in(i3)->in(0) == iff) { 311 proj = r->in(i3); 312 break; 313 } 314 } else if( v->is_ConstraintCast() ) { 315 proj = v->in(0); // Controlling projection 316 } else { 317 assert( 0, "do not know how to handle this guy" ); 318 } 319 320 Node *proj_path_data, *proj_path_ctrl; 321 if( proj->Opcode() == Op_IfTrue ) { 322 if( phi_s == NULL ) { 323 // Only construct phi_s if needed, otherwise provides 324 // interfering use. 325 phi_s = PhiNode::make_blank(region_s,phi); 326 phi_s->init_req( 1, phi_c ); 327 phi_s->init_req( 2, phi_x ); 328 phi_s = phase->transform(phi_s); 329 } 330 proj_path_data = phi_s; 331 proj_path_ctrl = region_s; 332 } else { 333 if( phi_f == NULL ) { 334 // Only construct phi_f if needed, otherwise provides 335 // interfering use. 336 phi_f = PhiNode::make_blank(region_f,phi); 337 phi_f->init_req( 1, phi_c ); 338 phi_f->init_req( 2, phi_x ); 339 phi_f = phase->transform(phi_f); 340 } 341 proj_path_data = phi_f; 342 proj_path_ctrl = region_f; 343 } 344 345 // Fixup 'v' for for the split 346 if( vop == Op_Phi ) { // Remote merge point 347 uint i; 348 for( i = 1; i < v->req(); i++ ) 349 if( v->in(i) == phi ) 350 break; 351 v->set_req(i, proj_path_data ); 352 } else if( v->is_ConstraintCast() ) { 353 v->set_req(0, proj_path_ctrl ); 354 v->set_req(1, proj_path_data ); 355 } else 356 ShouldNotReachHere(); 357 } 358 359 // Now replace the original iff's True/False with region_s/region_t. 360 // This makes the original iff go dead. 361 for (DUIterator_Last i3min, i3 = iff->last_outs(i3min); i3 >= i3min; --i3) { 362 Node* p = iff->last_out(i3); 363 assert( p->Opcode() == Op_IfTrue || p->Opcode() == Op_IfFalse, "" ); 364 Node *u = (p->Opcode() == Op_IfTrue) ? region_s : region_f; 365 // Replace p with u 366 igvn->add_users_to_worklist(p); 367 for (DUIterator_Last lmin, l = p->last_outs(lmin); l >= lmin;) { 368 Node* x = p->last_out(l); 369 igvn->hash_delete(x); 370 uint uses_found = 0; 371 for( uint j = 0; j < x->req(); j++ ) { 372 if( x->in(j) == p ) { 373 x->set_req(j, u); 374 uses_found++; 375 } 376 } 377 l -= uses_found; // we deleted 1 or more copies of this edge 378 } 379 igvn->remove_dead_node(p); 380 } 381 382 // Force the original merge dead 383 igvn->hash_delete(r); 384 r->set_req_X(0,NULL,igvn); 385 386 // Now remove the bogus extra edges used to keep things alive 387 igvn->remove_dead_node( hook ); 388 389 // Must return either the original node (now dead) or a new node 390 // (Do not return a top here, since that would break the uniqueness of top.) 391 return new (igvn->C, 1) ConINode(TypeInt::ZERO); 392 } 393 394 //------------------------------is_range_check--------------------------------- 395 // Return 0 if not a range check. Return 1 if a range check and set index and 396 // offset. Return 2 if we had to negate the test. Index is NULL if the check 397 // is versus a constant. 398 int IfNode::is_range_check(Node* &range, Node* &index, jint &offset) { 399 Node* b = in(1); 400 if (b == NULL || !b->is_Bool()) return 0; 401 BoolNode* bn = b->as_Bool(); 402 Node* cmp = bn->in(1); 403 if (cmp == NULL) return 0; 404 if (cmp->Opcode() != Op_CmpU) return 0; 405 406 Node* l = cmp->in(1); 407 Node* r = cmp->in(2); 408 int flip_test = 1; 409 if (bn->_test._test == BoolTest::le) { 410 l = cmp->in(2); 411 r = cmp->in(1); 412 flip_test = 2; 413 } else if (bn->_test._test != BoolTest::lt) { 414 return 0; 415 } 416 if (l->is_top()) return 0; // Top input means dead test 417 if (r->Opcode() != Op_LoadRange) return 0; 418 419 // We have recognized one of these forms: 420 // Flip 1: If (Bool[<] CmpU(l, LoadRange)) ... 421 // Flip 2: If (Bool[<=] CmpU(LoadRange, l)) ... 422 423 // Make sure it's a real range check by requiring an uncommon trap 424 // along the OOB path. Otherwise, it's possible that the user wrote 425 // something which optimized to look like a range check but behaves 426 // in some other way. 427 Node* iftrap = proj_out(flip_test == 2 ? true : false); 428 bool found_trap = false; 429 if (iftrap != NULL) { 430 Node* u = iftrap->unique_ctrl_out(); 431 if (u != NULL) { 432 // It could be a merge point (Region) for uncommon trap. 433 if (u->is_Region()) { 434 Node* c = u->unique_ctrl_out(); 435 if (c != NULL) { 436 iftrap = u; 437 u = c; 438 } 439 } 440 if (u->in(0) == iftrap && u->is_CallStaticJava()) { 441 int req = u->as_CallStaticJava()->uncommon_trap_request(); 442 if (Deoptimization::trap_request_reason(req) == 443 Deoptimization::Reason_range_check) { 444 found_trap = true; 445 } 446 } 447 } 448 } 449 if (!found_trap) return 0; // sorry, no cigar 450 451 // Look for index+offset form 452 Node* ind = l; 453 jint off = 0; 454 if (l->is_top()) { 455 return 0; 456 } else if (l->is_Add()) { 457 if ((off = l->in(1)->find_int_con(0)) != 0) { 458 ind = l->in(2); 459 } else if ((off = l->in(2)->find_int_con(0)) != 0) { 460 ind = l->in(1); 461 } 462 } else if ((off = l->find_int_con(-1)) >= 0) { 463 // constant offset with no variable index 464 ind = NULL; 465 } else { 466 // variable index with no constant offset (or dead negative index) 467 off = 0; 468 } 469 470 // Return all the values: 471 index = ind; 472 offset = off; 473 range = r; 474 return flip_test; 475 } 476 477 //------------------------------adjust_check----------------------------------- 478 // Adjust (widen) a prior range check 479 static void adjust_check(Node* proj, Node* range, Node* index, 480 int flip, jint off_lo, PhaseIterGVN* igvn) { 481 PhaseGVN *gvn = igvn; 482 // Break apart the old check 483 Node *iff = proj->in(0); 484 Node *bol = iff->in(1); 485 if( bol->is_top() ) return; // In case a partially dead range check appears 486 // bail (or bomb[ASSERT/DEBUG]) if NOT projection-->IfNode-->BoolNode 487 DEBUG_ONLY( if( !bol->is_Bool() ) { proj->dump(3); fatal("Expect projection-->IfNode-->BoolNode"); } ) 488 if( !bol->is_Bool() ) return; 489 490 Node *cmp = bol->in(1); 491 // Compute a new check 492 Node *new_add = gvn->intcon(off_lo); 493 if( index ) { 494 new_add = off_lo ? gvn->transform(new (gvn->C, 3) AddINode( index, new_add )) : index; 495 } 496 Node *new_cmp = (flip == 1) 497 ? new (gvn->C, 3) CmpUNode( new_add, range ) 498 : new (gvn->C, 3) CmpUNode( range, new_add ); 499 new_cmp = gvn->transform(new_cmp); 500 // See if no need to adjust the existing check 501 if( new_cmp == cmp ) return; 502 // Else, adjust existing check 503 Node *new_bol = gvn->transform( new (gvn->C, 2) BoolNode( new_cmp, bol->as_Bool()->_test._test ) ); 504 igvn->hash_delete( iff ); 505 iff->set_req_X( 1, new_bol, igvn ); 506 } 507 508 //------------------------------up_one_dom------------------------------------- 509 // Walk up the dominator tree one step. Return NULL at root or true 510 // complex merges. Skips through small diamonds. 511 Node* IfNode::up_one_dom(Node *curr, bool linear_only) { 512 Node *dom = curr->in(0); 513 if( !dom ) // Found a Region degraded to a copy? 514 return curr->nonnull_req(); // Skip thru it 515 516 if( curr != dom ) // Normal walk up one step? 517 return dom; 518 519 // Use linear_only if we are still parsing, since we cannot 520 // trust the regions to be fully filled in. 521 if (linear_only) 522 return NULL; 523 524 // Else hit a Region. Check for a loop header 525 if( dom->is_Loop() ) 526 return dom->in(1); // Skip up thru loops 527 528 // Check for small diamonds 529 Node *din1, *din2, *din3, *din4; 530 if( dom->req() == 3 && // 2-path merge point 531 (din1 = dom ->in(1)) && // Left path exists 532 (din2 = dom ->in(2)) && // Right path exists 533 (din3 = din1->in(0)) && // Left path up one 534 (din4 = din2->in(0)) ) { // Right path up one 535 if( din3->is_Call() && // Handle a slow-path call on either arm 536 (din3 = din3->in(0)) ) 537 din3 = din3->in(0); 538 if( din4->is_Call() && // Handle a slow-path call on either arm 539 (din4 = din4->in(0)) ) 540 din4 = din4->in(0); 541 if( din3 == din4 && din3->is_If() ) 542 return din3; // Skip around diamonds 543 } 544 545 // Give up the search at true merges 546 return NULL; // Dead loop? Or hit root? 547 } 548 549 550 //------------------------------filtered_int_type-------------------------------- 551 // Return a possibly more restrictive type for val based on condition control flow for an if 552 const TypeInt* IfNode::filtered_int_type(PhaseGVN* gvn, Node *val, Node* if_proj) { 553 assert(if_proj && 554 (if_proj->Opcode() == Op_IfTrue || if_proj->Opcode() == Op_IfFalse), "expecting an if projection"); 555 if (if_proj->in(0) && if_proj->in(0)->is_If()) { 556 IfNode* iff = if_proj->in(0)->as_If(); 557 if (iff->in(1) && iff->in(1)->is_Bool()) { 558 BoolNode* bol = iff->in(1)->as_Bool(); 559 if (bol->in(1) && bol->in(1)->is_Cmp()) { 560 const CmpNode* cmp = bol->in(1)->as_Cmp(); 561 if (cmp->in(1) == val) { 562 const TypeInt* cmp2_t = gvn->type(cmp->in(2))->isa_int(); 563 if (cmp2_t != NULL) { 564 jint lo = cmp2_t->_lo; 565 jint hi = cmp2_t->_hi; 566 BoolTest::mask msk = if_proj->Opcode() == Op_IfTrue ? bol->_test._test : bol->_test.negate(); 567 switch (msk) { 568 case BoolTest::ne: 569 // Can't refine type 570 return NULL; 571 case BoolTest::eq: 572 return cmp2_t; 573 case BoolTest::lt: 574 lo = TypeInt::INT->_lo; 575 if (hi - 1 < hi) { 576 hi = hi - 1; 577 } 578 break; 579 case BoolTest::le: 580 lo = TypeInt::INT->_lo; 581 break; 582 case BoolTest::gt: 583 if (lo + 1 > lo) { 584 lo = lo + 1; 585 } 586 hi = TypeInt::INT->_hi; 587 break; 588 case BoolTest::ge: 589 // lo unchanged 590 hi = TypeInt::INT->_hi; 591 break; 592 } 593 const TypeInt* rtn_t = TypeInt::make(lo, hi, cmp2_t->_widen); 594 return rtn_t; 595 } 596 } 597 } 598 } 599 } 600 return NULL; 601 } 602 603 //------------------------------fold_compares---------------------------- 604 // See if a pair of CmpIs can be converted into a CmpU. In some cases 605 // the direction of this if is determined by the preciding if so it 606 // can be eliminate entirely. Given an if testing (CmpI n c) check 607 // for an immediately control dependent if that is testing (CmpI n c2) 608 // and has one projection leading to this if and the other projection 609 // leading to a region that merges one of this ifs control 610 // projections. 611 // 612 // If 613 // / | 614 // / | 615 // / | 616 // If | 617 // /\ | 618 // / \ | 619 // / \ | 620 // / Region 621 // 622 Node* IfNode::fold_compares(PhaseGVN* phase) { 623 if (!EliminateAutoBox || Opcode() != Op_If) return NULL; 624 625 Node* this_cmp = in(1)->in(1); 626 if (this_cmp != NULL && this_cmp->Opcode() == Op_CmpI && 627 this_cmp->in(2)->is_Con() && this_cmp->in(2) != phase->C->top()) { 628 Node* ctrl = in(0); 629 BoolNode* this_bool = in(1)->as_Bool(); 630 Node* n = this_cmp->in(1); 631 int hi = this_cmp->in(2)->get_int(); 632 if (ctrl != NULL && ctrl->is_Proj() && ctrl->outcnt() == 1 && 633 ctrl->in(0)->is_If() && 634 ctrl->in(0)->outcnt() == 2 && 635 ctrl->in(0)->in(1)->is_Bool() && 636 ctrl->in(0)->in(1)->in(1)->Opcode() == Op_CmpI && 637 ctrl->in(0)->in(1)->in(1)->in(2)->is_Con() && 638 ctrl->in(0)->in(1)->in(1)->in(1) == n) { 639 IfNode* dom_iff = ctrl->in(0)->as_If(); 640 Node* otherproj = dom_iff->proj_out(!ctrl->as_Proj()->_con); 641 if (otherproj->outcnt() == 1 && otherproj->unique_out()->is_Region() && 642 this_bool->_test._test != BoolTest::ne && this_bool->_test._test != BoolTest::eq) { 643 // Identify which proj goes to the region and which continues on 644 RegionNode* region = otherproj->unique_out()->as_Region(); 645 Node* success = NULL; 646 Node* fail = NULL; 647 for (int i = 0; i < 2; i++) { 648 Node* proj = proj_out(i); 649 if (success == NULL && proj->outcnt() == 1 && proj->unique_out() == region) { 650 success = proj; 651 } else if (fail == NULL) { 652 fail = proj; 653 } else { 654 success = fail = NULL; 655 } 656 } 657 if (success != NULL && fail != NULL && !region->has_phi()) { 658 int lo = dom_iff->in(1)->in(1)->in(2)->get_int(); 659 BoolNode* dom_bool = dom_iff->in(1)->as_Bool(); 660 Node* dom_cmp = dom_bool->in(1); 661 const TypeInt* failtype = filtered_int_type(phase, n, ctrl); 662 if (failtype != NULL) { 663 const TypeInt* type2 = filtered_int_type(phase, n, fail); 664 if (type2 != NULL) { 665 failtype = failtype->join(type2)->is_int(); 666 } else { 667 failtype = NULL; 668 } 669 } 670 671 if (failtype != NULL && 672 dom_bool->_test._test != BoolTest::ne && dom_bool->_test._test != BoolTest::eq) { 673 int bound = failtype->_hi - failtype->_lo + 1; 674 if (failtype->_hi != max_jint && failtype->_lo != min_jint && bound > 1) { 675 // Merge the two compares into a single unsigned compare by building (CmpU (n - lo) hi) 676 BoolTest::mask cond = fail->as_Proj()->_con ? BoolTest::lt : BoolTest::ge; 677 Node* adjusted = phase->transform(new (phase->C, 3) SubINode(n, phase->intcon(failtype->_lo))); 678 Node* newcmp = phase->transform(new (phase->C, 3) CmpUNode(adjusted, phase->intcon(bound))); 679 Node* newbool = phase->transform(new (phase->C, 2) BoolNode(newcmp, cond)); 680 phase->hash_delete(dom_iff); 681 dom_iff->set_req(1, phase->intcon(ctrl->as_Proj()->_con)); 682 phase->is_IterGVN()->_worklist.push(dom_iff); 683 phase->hash_delete(this); 684 set_req(1, newbool); 685 return this; 686 } 687 if (failtype->_lo > failtype->_hi) { 688 // previous if determines the result of this if so 689 // replace Bool with constant 690 phase->hash_delete(this); 691 set_req(1, phase->intcon(success->as_Proj()->_con)); 692 return this; 693 } 694 } 695 } 696 } 697 } 698 } 699 return NULL; 700 } 701 702 //------------------------------remove_useless_bool---------------------------- 703 // Check for people making a useless boolean: things like 704 // if( (x < y ? true : false) ) { ... } 705 // Replace with if( x < y ) { ... } 706 static Node *remove_useless_bool(IfNode *iff, PhaseGVN *phase) { 707 Node *i1 = iff->in(1); 708 if( !i1->is_Bool() ) return NULL; 709 BoolNode *bol = i1->as_Bool(); 710 711 Node *cmp = bol->in(1); 712 if( cmp->Opcode() != Op_CmpI ) return NULL; 713 714 // Must be comparing against a bool 715 const Type *cmp2_t = phase->type( cmp->in(2) ); 716 if( cmp2_t != TypeInt::ZERO && 717 cmp2_t != TypeInt::ONE ) 718 return NULL; 719 720 // Find a prior merge point merging the boolean 721 i1 = cmp->in(1); 722 if( !i1->is_Phi() ) return NULL; 723 PhiNode *phi = i1->as_Phi(); 724 if( phase->type( phi ) != TypeInt::BOOL ) 725 return NULL; 726 727 // Check for diamond pattern 728 int true_path = phi->is_diamond_phi(); 729 if( true_path == 0 ) return NULL; 730 731 // phi->region->if_proj->ifnode->bool->cmp 732 BoolNode *bol2 = phi->in(0)->in(1)->in(0)->in(1)->as_Bool(); 733 734 // Now get the 'sense' of the test correct so we can plug in 735 // either iff2->in(1) or its complement. 736 int flip = 0; 737 if( bol->_test._test == BoolTest::ne ) flip = 1-flip; 738 else if( bol->_test._test != BoolTest::eq ) return NULL; 739 if( cmp2_t == TypeInt::ZERO ) flip = 1-flip; 740 741 const Type *phi1_t = phase->type( phi->in(1) ); 742 const Type *phi2_t = phase->type( phi->in(2) ); 743 // Check for Phi(0,1) and flip 744 if( phi1_t == TypeInt::ZERO ) { 745 if( phi2_t != TypeInt::ONE ) return NULL; 746 flip = 1-flip; 747 } else { 748 // Check for Phi(1,0) 749 if( phi1_t != TypeInt::ONE ) return NULL; 750 if( phi2_t != TypeInt::ZERO ) return NULL; 751 } 752 if( true_path == 2 ) { 753 flip = 1-flip; 754 } 755 756 Node* new_bol = (flip ? phase->transform( bol2->negate(phase) ) : bol2); 757 iff->set_req(1, new_bol); 758 // Intervening diamond probably goes dead 759 phase->C->set_major_progress(); 760 return iff; 761 } 762 763 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff); 764 765 //------------------------------Ideal------------------------------------------ 766 // Return a node which is more "ideal" than the current node. Strip out 767 // control copies 768 Node *IfNode::Ideal(PhaseGVN *phase, bool can_reshape) { 769 if (remove_dead_region(phase, can_reshape)) return this; 770 // No Def-Use info? 771 if (!can_reshape) return NULL; 772 PhaseIterGVN *igvn = phase->is_IterGVN(); 773 774 // Don't bother trying to transform a dead if 775 if (in(0)->is_top()) return NULL; 776 // Don't bother trying to transform an if with a dead test 777 if (in(1)->is_top()) return NULL; 778 // Another variation of a dead test 779 if (in(1)->is_Con()) return NULL; 780 // Another variation of a dead if 781 if (outcnt() < 2) return NULL; 782 783 // Canonicalize the test. 784 Node* idt_if = idealize_test(phase, this); 785 if (idt_if != NULL) return idt_if; 786 787 // Try to split the IF 788 Node *s = split_if(this, igvn); 789 if (s != NULL) return s; 790 791 // Check for people making a useless boolean: things like 792 // if( (x < y ? true : false) ) { ... } 793 // Replace with if( x < y ) { ... } 794 Node *bol2 = remove_useless_bool(this, phase); 795 if( bol2 ) return bol2; 796 797 // Setup to scan up the CFG looking for a dominating test 798 Node *dom = in(0); 799 Node *prev_dom = this; 800 801 // Check for range-check vs other kinds of tests 802 Node *index1, *range1; 803 jint offset1; 804 int flip1 = is_range_check(range1, index1, offset1); 805 if( flip1 ) { 806 Node *first_prev_dom = NULL; 807 808 // Try to remove extra range checks. All 'up_one_dom' gives up at merges 809 // so all checks we inspect post-dominate the top-most check we find. 810 // If we are going to fail the current check and we reach the top check 811 // then we are guarenteed to fail, so just start interpreting there. 812 // We 'expand' the top 2 range checks to include all post-dominating 813 // checks. 814 815 // The top 2 range checks seen 816 Node *prev_chk1 = NULL; 817 Node *prev_chk2 = NULL; 818 // Low and high offsets seen so far 819 jint off_lo = offset1; 820 jint off_hi = offset1; 821 822 // Scan for the top 2 checks and collect range of offsets 823 for( int dist = 0; dist < 999; dist++ ) { // Range-Check scan limit 824 if( dom->Opcode() == Op_If && // Not same opcode? 825 prev_dom->in(0) == dom ) { // One path of test does dominate? 826 if( dom == this ) return NULL; // dead loop 827 // See if this is a range check 828 Node *index2, *range2; 829 jint offset2; 830 int flip2 = dom->as_If()->is_range_check(range2, index2, offset2); 831 // See if this is a _matching_ range check, checking against 832 // the same array bounds. 833 if( flip2 == flip1 && range2 == range1 && index2 == index1 && 834 dom->outcnt() == 2 ) { 835 // Gather expanded bounds 836 off_lo = MIN2(off_lo,offset2); 837 off_hi = MAX2(off_hi,offset2); 838 // Record top 2 range checks 839 prev_chk2 = prev_chk1; 840 prev_chk1 = prev_dom; 841 // If we match the test exactly, then the top test covers 842 // both our lower and upper bounds. 843 if( dom->in(1) == in(1) ) 844 prev_chk2 = prev_chk1; 845 } 846 } 847 prev_dom = dom; 848 dom = up_one_dom( dom ); 849 if( !dom ) break; 850 } 851 852 853 // Attempt to widen the dominating range check to cover some later 854 // ones. Since range checks "fail" by uncommon-trapping to the 855 // interpreter, widening a check can make us speculative enter the 856 // interpreter. If we see range-check deopt's, do not widen! 857 if (!phase->C->allow_range_check_smearing()) return NULL; 858 859 // Constant indices only need to check the upper bound. 860 // Non-constance indices must check both low and high. 861 if( index1 ) { 862 // Didn't find 2 prior covering checks, so cannot remove anything. 863 if( !prev_chk2 ) return NULL; 864 // 'Widen' the offsets of the 1st and 2nd covering check 865 adjust_check( prev_chk1, range1, index1, flip1, off_lo, igvn ); 866 // Do not call adjust_check twice on the same projection 867 // as the first call may have transformed the BoolNode to a ConI 868 if( prev_chk1 != prev_chk2 ) { 869 adjust_check( prev_chk2, range1, index1, flip1, off_hi, igvn ); 870 } 871 // Test is now covered by prior checks, dominate it out 872 prev_dom = prev_chk2; 873 } else { 874 // Didn't find prior covering check, so cannot remove anything. 875 if( !prev_chk1 ) return NULL; 876 // 'Widen' the offset of the 1st and only covering check 877 adjust_check( prev_chk1, range1, index1, flip1, off_hi, igvn ); 878 // Test is now covered by prior checks, dominate it out 879 prev_dom = prev_chk1; 880 } 881 882 883 } else { // Scan for an equivalent test 884 885 Node *cmp; 886 int dist = 0; // Cutoff limit for search 887 int op = Opcode(); 888 if( op == Op_If && 889 (cmp=in(1)->in(1))->Opcode() == Op_CmpP ) { 890 if( cmp->in(2) != NULL && // make sure cmp is not already dead 891 cmp->in(2)->bottom_type() == TypePtr::NULL_PTR ) { 892 dist = 64; // Limit for null-pointer scans 893 } else { 894 dist = 4; // Do not bother for random pointer tests 895 } 896 } else { 897 dist = 4; // Limit for random junky scans 898 } 899 900 // Normal equivalent-test check. 901 if( !dom ) return NULL; // Dead loop? 902 903 Node* result = fold_compares(phase); 904 if (result != NULL) { 905 return result; 906 } 907 908 // Search up the dominator tree for an If with an identical test 909 while( dom->Opcode() != op || // Not same opcode? 910 dom->in(1) != in(1) || // Not same input 1? 911 (req() == 3 && dom->in(2) != in(2)) || // Not same input 2? 912 prev_dom->in(0) != dom ) { // One path of test does not dominate? 913 if( dist < 0 ) return NULL; 914 915 dist--; 916 prev_dom = dom; 917 dom = up_one_dom( dom ); 918 if( !dom ) return NULL; 919 } 920 921 // Check that we did not follow a loop back to ourselves 922 if( this == dom ) 923 return NULL; 924 925 if( dist > 2 ) // Add to count of NULL checks elided 926 explicit_null_checks_elided++; 927 928 } // End of Else scan for an equivalent test 929 930 // Hit! Remove this IF 931 #ifndef PRODUCT 932 if( TraceIterativeGVN ) { 933 tty->print(" Removing IfNode: "); this->dump(); 934 } 935 if( VerifyOpto && !phase->allow_progress() ) { 936 // Found an equivalent dominating test, 937 // we can not guarantee reaching a fix-point for these during iterativeGVN 938 // since intervening nodes may not change. 939 return NULL; 940 } 941 #endif 942 943 // Replace dominated IfNode 944 dominated_by( prev_dom, igvn ); 945 946 // Must return either the original node (now dead) or a new node 947 // (Do not return a top here, since that would break the uniqueness of top.) 948 return new (phase->C, 1) ConINode(TypeInt::ZERO); 949 } 950 951 //------------------------------dominated_by----------------------------------- 952 void IfNode::dominated_by( Node *prev_dom, PhaseIterGVN *igvn ) { 953 igvn->hash_delete(this); // Remove self to prevent spurious V-N 954 Node *idom = in(0); 955 // Need opcode to decide which way 'this' test goes 956 int prev_op = prev_dom->Opcode(); 957 Node *top = igvn->C->top(); // Shortcut to top 958 959 // Now walk the current IfNode's projections. 960 // Loop ends when 'this' has no more uses. 961 for (DUIterator_Last imin, i = last_outs(imin); i >= imin; --i) { 962 Node *ifp = last_out(i); // Get IfTrue/IfFalse 963 igvn->add_users_to_worklist(ifp); 964 // Check which projection it is and set target. 965 // Data-target is either the dominating projection of the same type 966 // or TOP if the dominating projection is of opposite type. 967 // Data-target will be used as the new control edge for the non-CFG 968 // nodes like Casts and Loads. 969 Node *data_target = (ifp->Opcode() == prev_op ) ? prev_dom : top; 970 // Control-target is just the If's immediate dominator or TOP. 971 Node *ctrl_target = (ifp->Opcode() == prev_op ) ? idom : top; 972 973 // For each child of an IfTrue/IfFalse projection, reroute. 974 // Loop ends when projection has no more uses. 975 for (DUIterator_Last jmin, j = ifp->last_outs(jmin); j >= jmin; --j) { 976 Node* s = ifp->last_out(j); // Get child of IfTrue/IfFalse 977 igvn->hash_delete(s); // Yank from hash table before edge hacking 978 if( !s->depends_only_on_test() ) { 979 // Find the control input matching this def-use edge. 980 // For Regions it may not be in slot 0. 981 uint l; 982 for( l = 0; s->in(l) != ifp; l++ ) { } 983 s->set_req(l, ctrl_target); 984 } else { // Else, for control producers, 985 s->set_req(0, data_target); // Move child to data-target 986 } 987 igvn->_worklist.push(s); // Revisit collapsed Phis 988 } // End for each child of a projection 989 990 igvn->remove_dead_node(ifp); 991 } // End for each IfTrue/IfFalse child of If 992 993 // Kill the IfNode 994 igvn->remove_dead_node(this); 995 } 996 997 //------------------------------Identity--------------------------------------- 998 // If the test is constant & we match, then we are the input Control 999 Node *IfTrueNode::Identity( PhaseTransform *phase ) { 1000 // Can only optimize if cannot go the other way 1001 const TypeTuple *t = phase->type(in(0))->is_tuple(); 1002 return ( t == TypeTuple::IFNEITHER || t == TypeTuple::IFTRUE ) 1003 ? in(0)->in(0) // IfNode control 1004 : this; // no progress 1005 } 1006 1007 //------------------------------dump_spec-------------------------------------- 1008 #ifndef PRODUCT 1009 void IfNode::dump_spec(outputStream *st) const { 1010 st->print("P=%f, C=%f",_prob,_fcnt); 1011 } 1012 #endif 1013 1014 //------------------------------idealize_test---------------------------------- 1015 // Try to canonicalize tests better. Peek at the Cmp/Bool/If sequence and 1016 // come up with a canonical sequence. Bools getting 'eq', 'gt' and 'ge' forms 1017 // converted to 'ne', 'le' and 'lt' forms. IfTrue/IfFalse get swapped as 1018 // needed. 1019 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff) { 1020 assert(iff->in(0) != NULL, "If must be live"); 1021 1022 if (iff->outcnt() != 2) return NULL; // Malformed projections. 1023 Node* old_if_f = iff->proj_out(false); 1024 Node* old_if_t = iff->proj_out(true); 1025 1026 // CountedLoopEnds want the back-control test to be TRUE, irregardless of 1027 // whether they are testing a 'gt' or 'lt' condition. The 'gt' condition 1028 // happens in count-down loops 1029 if (iff->is_CountedLoopEnd()) return NULL; 1030 if (!iff->in(1)->is_Bool()) return NULL; // Happens for partially optimized IF tests 1031 BoolNode *b = iff->in(1)->as_Bool(); 1032 BoolTest bt = b->_test; 1033 // Test already in good order? 1034 if( bt.is_canonical() ) 1035 return NULL; 1036 1037 // Flip test to be canonical. Requires flipping the IfFalse/IfTrue and 1038 // cloning the IfNode. 1039 Node* new_b = phase->transform( new (phase->C, 2) BoolNode(b->in(1), bt.negate()) ); 1040 if( !new_b->is_Bool() ) return NULL; 1041 b = new_b->as_Bool(); 1042 1043 PhaseIterGVN *igvn = phase->is_IterGVN(); 1044 assert( igvn, "Test is not canonical in parser?" ); 1045 1046 // The IF node never really changes, but it needs to be cloned 1047 iff = new (phase->C, 2) IfNode( iff->in(0), b, 1.0-iff->_prob, iff->_fcnt); 1048 1049 Node *prior = igvn->hash_find_insert(iff); 1050 if( prior ) { 1051 igvn->remove_dead_node(iff); 1052 iff = (IfNode*)prior; 1053 } else { 1054 // Cannot call transform on it just yet 1055 igvn->set_type_bottom(iff); 1056 } 1057 igvn->_worklist.push(iff); 1058 1059 // Now handle projections. Cloning not required. 1060 Node* new_if_f = (Node*)(new (phase->C, 1) IfFalseNode( iff )); 1061 Node* new_if_t = (Node*)(new (phase->C, 1) IfTrueNode ( iff )); 1062 1063 igvn->register_new_node_with_optimizer(new_if_f); 1064 igvn->register_new_node_with_optimizer(new_if_t); 1065 igvn->hash_delete(old_if_f); 1066 igvn->hash_delete(old_if_t); 1067 // Flip test, so flip trailing control 1068 igvn->subsume_node(old_if_f, new_if_t); 1069 igvn->subsume_node(old_if_t, new_if_f); 1070 1071 // Progress 1072 return iff; 1073 } 1074 1075 //------------------------------Identity--------------------------------------- 1076 // If the test is constant & we match, then we are the input Control 1077 Node *IfFalseNode::Identity( PhaseTransform *phase ) { 1078 // Can only optimize if cannot go the other way 1079 const TypeTuple *t = phase->type(in(0))->is_tuple(); 1080 return ( t == TypeTuple::IFNEITHER || t == TypeTuple::IFFALSE ) 1081 ? in(0)->in(0) // IfNode control 1082 : this; // no progress 1083 } 1084