1 /* 2 * Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "ci/ciTypeFlow.hpp" 27 #include "memory/allocation.inline.hpp" 28 #include "memory/resourceArea.hpp" 29 #include "opto/addnode.hpp" 30 #include "opto/castnode.hpp" 31 #include "opto/cfgnode.hpp" 32 #include "opto/connode.hpp" 33 #include "opto/loopnode.hpp" 34 #include "opto/phaseX.hpp" 35 #include "opto/predicates.hpp" 36 #include "opto/runtime.hpp" 37 #include "opto/rootnode.hpp" 38 #include "opto/subnode.hpp" 39 #include "opto/subtypenode.hpp" 40 41 // Portions of code courtesy of Clifford Click 42 43 // Optimization - Graph Style 44 45 46 #ifndef PRODUCT 47 extern uint explicit_null_checks_elided; 48 #endif 49 50 //============================================================================= 51 //------------------------------Value------------------------------------------ 52 // Return a tuple for whichever arm of the IF is reachable 53 const Type* IfNode::Value(PhaseGVN* phase) const { 54 if( !in(0) ) return Type::TOP; 55 if( phase->type(in(0)) == Type::TOP ) 56 return Type::TOP; 57 const Type *t = phase->type(in(1)); 58 if( t == Type::TOP ) // data is undefined 59 return TypeTuple::IFNEITHER; // unreachable altogether 60 if( t == TypeInt::ZERO ) // zero, or false 61 return TypeTuple::IFFALSE; // only false branch is reachable 62 if( t == TypeInt::ONE ) // 1, or true 63 return TypeTuple::IFTRUE; // only true branch is reachable 64 assert( t == TypeInt::BOOL, "expected boolean type" ); 65 66 return TypeTuple::IFBOTH; // No progress 67 } 68 69 const RegMask &IfNode::out_RegMask() const { 70 return RegMask::Empty; 71 } 72 73 //------------------------------split_if--------------------------------------- 74 // Look for places where we merge constants, then test on the merged value. 75 // If the IF test will be constant folded on the path with the constant, we 76 // win by splitting the IF to before the merge point. 77 static Node* split_if(IfNode *iff, PhaseIterGVN *igvn) { 78 // I could be a lot more general here, but I'm trying to squeeze this 79 // in before the Christmas '98 break so I'm gonna be kinda restrictive 80 // on the patterns I accept. CNC 81 82 // Look for a compare of a constant and a merged value 83 Node *i1 = iff->in(1); 84 if( !i1->is_Bool() ) return nullptr; 85 BoolNode *b = i1->as_Bool(); 86 Node *cmp = b->in(1); 87 if( !cmp->is_Cmp() ) return nullptr; 88 i1 = cmp->in(1); 89 if( i1 == nullptr || !i1->is_Phi() ) return nullptr; 90 PhiNode *phi = i1->as_Phi(); 91 Node *con2 = cmp->in(2); 92 if( !con2->is_Con() ) return nullptr; 93 // See that the merge point contains some constants 94 Node *con1=nullptr; 95 uint i4; 96 RegionNode* phi_region = phi->region(); 97 for (i4 = 1; i4 < phi->req(); i4++ ) { 98 con1 = phi->in(i4); 99 // Do not optimize partially collapsed merges 100 if (con1 == nullptr || phi_region->in(i4) == nullptr || igvn->type(phi_region->in(i4)) == Type::TOP) { 101 igvn->_worklist.push(iff); 102 return nullptr; 103 } 104 if( con1->is_Con() ) break; // Found a constant 105 // Also allow null-vs-not-null checks 106 const TypePtr *tp = igvn->type(con1)->isa_ptr(); 107 if( tp && tp->_ptr == TypePtr::NotNull ) 108 break; 109 } 110 if( i4 >= phi->req() ) return nullptr; // Found no constants 111 112 igvn->C->set_has_split_ifs(true); // Has chance for split-if 113 114 // Make sure that the compare can be constant folded away 115 Node *cmp2 = cmp->clone(); 116 cmp2->set_req(1,con1); 117 cmp2->set_req(2,con2); 118 const Type *t = cmp2->Value(igvn); 119 // This compare is dead, so whack it! 120 igvn->remove_dead_node(cmp2); 121 if( !t->singleton() ) return nullptr; 122 123 // No intervening control, like a simple Call 124 Node* r = iff->in(0); 125 if (!r->is_Region() || r->is_Loop() || phi_region != r || r->as_Region()->is_copy()) { 126 return nullptr; 127 } 128 129 // No other users of the cmp/bool 130 if (b->outcnt() != 1 || cmp->outcnt() != 1) { 131 //tty->print_cr("many users of cmp/bool"); 132 return nullptr; 133 } 134 135 // Make sure we can determine where all the uses of merged values go 136 for (DUIterator_Fast jmax, j = r->fast_outs(jmax); j < jmax; j++) { 137 Node* u = r->fast_out(j); 138 if( u == r ) continue; 139 if( u == iff ) continue; 140 if( u->outcnt() == 0 ) continue; // use is dead & ignorable 141 if( !u->is_Phi() ) { 142 /* 143 if( u->is_Start() ) { 144 tty->print_cr("Region has inlined start use"); 145 } else { 146 tty->print_cr("Region has odd use"); 147 u->dump(2); 148 }*/ 149 return nullptr; 150 } 151 if( u != phi ) { 152 // CNC - do not allow any other merged value 153 //tty->print_cr("Merging another value"); 154 //u->dump(2); 155 return nullptr; 156 } 157 // Make sure we can account for all Phi uses 158 for (DUIterator_Fast kmax, k = u->fast_outs(kmax); k < kmax; k++) { 159 Node* v = u->fast_out(k); // User of the phi 160 // CNC - Allow only really simple patterns. 161 // In particular I disallow AddP of the Phi, a fairly common pattern 162 if (v == cmp) continue; // The compare is OK 163 if (v->is_ConstraintCast()) { 164 // If the cast is derived from data flow edges, it may not have a control edge. 165 // If so, it should be safe to split. But follow-up code can not deal with 166 // this (l. 359). So skip. 167 if (v->in(0) == nullptr) { 168 return nullptr; 169 } 170 if (v->in(0)->in(0) == iff) { 171 continue; // CastPP/II of the IfNode is OK 172 } 173 } 174 // Disabled following code because I cannot tell if exactly one 175 // path dominates without a real dominator check. CNC 9/9/1999 176 //uint vop = v->Opcode(); 177 //if( vop == Op_Phi ) { // Phi from another merge point might be OK 178 // Node *r = v->in(0); // Get controlling point 179 // if( !r ) return nullptr; // Degraded to a copy 180 // // Find exactly one path in (either True or False doms, but not IFF) 181 // int cnt = 0; 182 // for( uint i = 1; i < r->req(); i++ ) 183 // if( r->in(i) && r->in(i)->in(0) == iff ) 184 // cnt++; 185 // if( cnt == 1 ) continue; // Exactly one of True or False guards Phi 186 //} 187 if( !v->is_Call() ) { 188 /* 189 if( v->Opcode() == Op_AddP ) { 190 tty->print_cr("Phi has AddP use"); 191 } else if( v->Opcode() == Op_CastPP ) { 192 tty->print_cr("Phi has CastPP use"); 193 } else if( v->Opcode() == Op_CastII ) { 194 tty->print_cr("Phi has CastII use"); 195 } else { 196 tty->print_cr("Phi has use I can't be bothered with"); 197 } 198 */ 199 } 200 return nullptr; 201 202 /* CNC - Cut out all the fancy acceptance tests 203 // Can we clone this use when doing the transformation? 204 // If all uses are from Phis at this merge or constants, then YES. 205 if( !v->in(0) && v != cmp ) { 206 tty->print_cr("Phi has free-floating use"); 207 v->dump(2); 208 return nullptr; 209 } 210 for( uint l = 1; l < v->req(); l++ ) { 211 if( (!v->in(l)->is_Phi() || v->in(l)->in(0) != r) && 212 !v->in(l)->is_Con() ) { 213 tty->print_cr("Phi has use"); 214 v->dump(2); 215 return nullptr; 216 } // End of if Phi-use input is neither Phi nor Constant 217 } // End of for all inputs to Phi-use 218 */ 219 } // End of for all uses of Phi 220 } // End of for all uses of Region 221 222 // Only do this if the IF node is in a sane state 223 if (iff->outcnt() != 2) 224 return nullptr; 225 226 // Got a hit! Do the Mondo Hack! 227 // 228 //ABC a1c def ghi B 1 e h A C a c d f g i 229 // R - Phi - Phi - Phi Rc - Phi - Phi - Phi Rx - Phi - Phi - Phi 230 // cmp - 2 cmp - 2 cmp - 2 231 // bool bool_c bool_x 232 // if if_c if_x 233 // T F T F T F 234 // ..s.. ..t .. ..s.. ..t.. ..s.. ..t.. 235 // 236 // Split the paths coming into the merge point into 2 separate groups of 237 // merges. On the left will be all the paths feeding constants into the 238 // Cmp's Phi. On the right will be the remaining paths. The Cmp's Phi 239 // will fold up into a constant; this will let the Cmp fold up as well as 240 // all the control flow. Below the original IF we have 2 control 241 // dependent regions, 's' and 't'. Now we will merge the two paths 242 // just prior to 's' and 't' from the two IFs. At least 1 path (and quite 243 // likely 2 or more) will promptly constant fold away. 244 PhaseGVN *phase = igvn; 245 246 // Make a region merging constants and a region merging the rest 247 uint req_c = 0; 248 for (uint ii = 1; ii < r->req(); ii++) { 249 if (phi->in(ii) == con1) { 250 req_c++; 251 } 252 if (Node::may_be_loop_entry(r->in(ii))) { 253 // Bail out if splitting through a region with a Parse Predicate input (could 254 // also be a loop header before loop opts creates a LoopNode for it). 255 return nullptr; 256 } 257 } 258 259 // If all the defs of the phi are the same constant, we already have the desired end state. 260 // Skip the split that would create empty phi and region nodes. 261 if ((r->req() - req_c) == 1) { 262 return nullptr; 263 } 264 265 // At this point we know that we can apply the split if optimization. If the region is still on the worklist, 266 // we should wait until it is processed. The region might be removed which makes this optimization redundant. 267 // This also avoids the creation of dead data loops when rewiring data nodes below when a region is dying. 268 if (igvn->_worklist.member(r)) { 269 igvn->_worklist.push(iff); // retry split if later again 270 return nullptr; 271 } 272 273 Node *region_c = new RegionNode(req_c + 1); 274 Node *phi_c = con1; 275 uint len = r->req(); 276 Node *region_x = new RegionNode(len - req_c); 277 Node *phi_x = PhiNode::make_blank(region_x, phi); 278 for (uint i = 1, i_c = 1, i_x = 1; i < len; i++) { 279 if (phi->in(i) == con1) { 280 region_c->init_req( i_c++, r ->in(i) ); 281 } else { 282 region_x->init_req( i_x, r ->in(i) ); 283 phi_x ->init_req( i_x++, phi->in(i) ); 284 } 285 } 286 287 // Register the new RegionNodes but do not transform them. Cannot 288 // transform until the entire Region/Phi conglomerate has been hacked 289 // as a single huge transform. 290 igvn->register_new_node_with_optimizer( region_c ); 291 igvn->register_new_node_with_optimizer( region_x ); 292 // Prevent the untimely death of phi_x. Currently he has no uses. He is 293 // about to get one. If this only use goes away, then phi_x will look dead. 294 // However, he will be picking up some more uses down below. 295 Node *hook = new Node(4); 296 hook->init_req(0, phi_x); 297 hook->init_req(1, phi_c); 298 phi_x = phase->transform( phi_x ); 299 300 // Make the compare 301 Node *cmp_c = phase->makecon(t); 302 Node *cmp_x = cmp->clone(); 303 cmp_x->set_req(1,phi_x); 304 cmp_x->set_req(2,con2); 305 cmp_x = phase->transform(cmp_x); 306 // Make the bool 307 Node *b_c = phase->transform(new BoolNode(cmp_c,b->_test._test)); 308 Node *b_x = phase->transform(new BoolNode(cmp_x,b->_test._test)); 309 // Make the IfNode 310 IfNode* iff_c = iff->clone()->as_If(); 311 iff_c->set_req(0, region_c); 312 iff_c->set_req(1, b_c); 313 igvn->set_type_bottom(iff_c); 314 igvn->_worklist.push(iff_c); 315 hook->init_req(2, iff_c); 316 317 IfNode* iff_x = iff->clone()->as_If(); 318 iff_x->set_req(0, region_x); 319 iff_x->set_req(1, b_x); 320 igvn->set_type_bottom(iff_x); 321 igvn->_worklist.push(iff_x); 322 hook->init_req(3, iff_x); 323 324 // Make the true/false arms 325 Node *iff_c_t = phase->transform(new IfTrueNode (iff_c)); 326 Node *iff_c_f = phase->transform(new IfFalseNode(iff_c)); 327 Node *iff_x_t = phase->transform(new IfTrueNode (iff_x)); 328 Node *iff_x_f = phase->transform(new IfFalseNode(iff_x)); 329 330 // Merge the TRUE paths 331 Node *region_s = new RegionNode(3); 332 igvn->_worklist.push(region_s); 333 region_s->init_req(1, iff_c_t); 334 region_s->init_req(2, iff_x_t); 335 igvn->register_new_node_with_optimizer( region_s ); 336 337 // Merge the FALSE paths 338 Node *region_f = new RegionNode(3); 339 igvn->_worklist.push(region_f); 340 region_f->init_req(1, iff_c_f); 341 region_f->init_req(2, iff_x_f); 342 igvn->register_new_node_with_optimizer( region_f ); 343 344 igvn->hash_delete(cmp);// Remove soon-to-be-dead node from hash table. 345 cmp->set_req(1,nullptr); // Whack the inputs to cmp because it will be dead 346 cmp->set_req(2,nullptr); 347 // Check for all uses of the Phi and give them a new home. 348 // The 'cmp' got cloned, but CastPP/IIs need to be moved. 349 Node *phi_s = nullptr; // do not construct unless needed 350 Node *phi_f = nullptr; // do not construct unless needed 351 for (DUIterator_Last i2min, i2 = phi->last_outs(i2min); i2 >= i2min; --i2) { 352 Node* v = phi->last_out(i2);// User of the phi 353 igvn->rehash_node_delayed(v); // Have to fixup other Phi users 354 uint vop = v->Opcode(); 355 Node *proj = nullptr; 356 if( vop == Op_Phi ) { // Remote merge point 357 Node *r = v->in(0); 358 for (uint i3 = 1; i3 < r->req(); i3++) 359 if (r->in(i3) && r->in(i3)->in(0) == iff) { 360 proj = r->in(i3); 361 break; 362 } 363 } else if( v->is_ConstraintCast() ) { 364 proj = v->in(0); // Controlling projection 365 } else { 366 assert( 0, "do not know how to handle this guy" ); 367 } 368 guarantee(proj != nullptr, "sanity"); 369 370 Node *proj_path_data, *proj_path_ctrl; 371 if( proj->Opcode() == Op_IfTrue ) { 372 if( phi_s == nullptr ) { 373 // Only construct phi_s if needed, otherwise provides 374 // interfering use. 375 phi_s = PhiNode::make_blank(region_s,phi); 376 phi_s->init_req( 1, phi_c ); 377 phi_s->init_req( 2, phi_x ); 378 hook->add_req(phi_s); 379 phi_s = phase->transform(phi_s); 380 } 381 proj_path_data = phi_s; 382 proj_path_ctrl = region_s; 383 } else { 384 if( phi_f == nullptr ) { 385 // Only construct phi_f if needed, otherwise provides 386 // interfering use. 387 phi_f = PhiNode::make_blank(region_f,phi); 388 phi_f->init_req( 1, phi_c ); 389 phi_f->init_req( 2, phi_x ); 390 hook->add_req(phi_f); 391 phi_f = phase->transform(phi_f); 392 } 393 proj_path_data = phi_f; 394 proj_path_ctrl = region_f; 395 } 396 397 // Fixup 'v' for for the split 398 if( vop == Op_Phi ) { // Remote merge point 399 uint i; 400 for( i = 1; i < v->req(); i++ ) 401 if( v->in(i) == phi ) 402 break; 403 v->set_req(i, proj_path_data ); 404 } else if( v->is_ConstraintCast() ) { 405 v->set_req(0, proj_path_ctrl ); 406 v->set_req(1, proj_path_data ); 407 } else 408 ShouldNotReachHere(); 409 } 410 411 // Now replace the original iff's True/False with region_s/region_t. 412 // This makes the original iff go dead. 413 for (DUIterator_Last i3min, i3 = iff->last_outs(i3min); i3 >= i3min; --i3) { 414 Node* p = iff->last_out(i3); 415 assert( p->Opcode() == Op_IfTrue || p->Opcode() == Op_IfFalse, "" ); 416 Node *u = (p->Opcode() == Op_IfTrue) ? region_s : region_f; 417 // Replace p with u 418 igvn->add_users_to_worklist(p); 419 for (DUIterator_Last lmin, l = p->last_outs(lmin); l >= lmin;) { 420 Node* x = p->last_out(l); 421 igvn->hash_delete(x); 422 uint uses_found = 0; 423 for( uint j = 0; j < x->req(); j++ ) { 424 if( x->in(j) == p ) { 425 x->set_req(j, u); 426 uses_found++; 427 } 428 } 429 l -= uses_found; // we deleted 1 or more copies of this edge 430 } 431 igvn->remove_dead_node(p); 432 } 433 434 // Force the original merge dead 435 igvn->hash_delete(r); 436 // First, remove region's dead users. 437 for (DUIterator_Last lmin, l = r->last_outs(lmin); l >= lmin;) { 438 Node* u = r->last_out(l); 439 if( u == r ) { 440 r->set_req(0, nullptr); 441 } else { 442 assert(u->outcnt() == 0, "only dead users"); 443 igvn->remove_dead_node(u); 444 } 445 l -= 1; 446 } 447 igvn->remove_dead_node(r); 448 449 // Now remove the bogus extra edges used to keep things alive 450 igvn->remove_dead_node( hook ); 451 452 // Must return either the original node (now dead) or a new node 453 // (Do not return a top here, since that would break the uniqueness of top.) 454 return new ConINode(TypeInt::ZERO); 455 } 456 457 IfNode* IfNode::make_with_same_profile(IfNode* if_node_profile, Node* ctrl, BoolNode* bol) { 458 // Assert here that we only try to create a clone from an If node with the same profiling if that actually makes sense. 459 // Some If node subtypes should not be cloned in this way. In theory, we should not clone BaseCountedLoopEndNodes. 460 // But they can end up being used as normal If nodes when peeling a loop - they serve as zero-trip guard. 461 // Allow them as well. 462 assert(if_node_profile->Opcode() == Op_If || if_node_profile->is_RangeCheck() 463 || if_node_profile->is_BaseCountedLoopEnd(), "should not clone other nodes"); 464 if (if_node_profile->is_RangeCheck()) { 465 // RangeCheck nodes could be further optimized. 466 return new RangeCheckNode(ctrl, bol, if_node_profile->_prob, if_node_profile->_fcnt); 467 } else { 468 // Not a RangeCheckNode? Fall back to IfNode. 469 return new IfNode(ctrl, bol, if_node_profile->_prob, if_node_profile->_fcnt); 470 } 471 } 472 473 // if this IfNode follows a range check pattern return the projection 474 // for the failed path 475 ProjNode* IfNode::range_check_trap_proj(int& flip_test, Node*& l, Node*& r) { 476 if (outcnt() != 2) { 477 return nullptr; 478 } 479 Node* b = in(1); 480 if (b == nullptr || !b->is_Bool()) return nullptr; 481 BoolNode* bn = b->as_Bool(); 482 Node* cmp = bn->in(1); 483 if (cmp == nullptr) return nullptr; 484 if (cmp->Opcode() != Op_CmpU) return nullptr; 485 486 l = cmp->in(1); 487 r = cmp->in(2); 488 flip_test = 1; 489 if (bn->_test._test == BoolTest::le) { 490 l = cmp->in(2); 491 r = cmp->in(1); 492 flip_test = 2; 493 } else if (bn->_test._test != BoolTest::lt) { 494 return nullptr; 495 } 496 if (l->is_top()) return nullptr; // Top input means dead test 497 if (r->Opcode() != Op_LoadRange && !is_RangeCheck()) return nullptr; 498 499 // We have recognized one of these forms: 500 // Flip 1: If (Bool[<] CmpU(l, LoadRange)) ... 501 // Flip 2: If (Bool[<=] CmpU(LoadRange, l)) ... 502 503 ProjNode* iftrap = proj_out_or_null(flip_test == 2 ? true : false); 504 return iftrap; 505 } 506 507 508 //------------------------------is_range_check--------------------------------- 509 // Return 0 if not a range check. Return 1 if a range check and set index and 510 // offset. Return 2 if we had to negate the test. Index is null if the check 511 // is versus a constant. 512 int RangeCheckNode::is_range_check(Node* &range, Node* &index, jint &offset) { 513 int flip_test = 0; 514 Node* l = nullptr; 515 Node* r = nullptr; 516 ProjNode* iftrap = range_check_trap_proj(flip_test, l, r); 517 518 if (iftrap == nullptr) { 519 return 0; 520 } 521 522 // Make sure it's a real range check by requiring an uncommon trap 523 // along the OOB path. Otherwise, it's possible that the user wrote 524 // something which optimized to look like a range check but behaves 525 // in some other way. 526 if (iftrap->is_uncommon_trap_proj(Deoptimization::Reason_range_check) == nullptr) { 527 return 0; 528 } 529 530 // Look for index+offset form 531 Node* ind = l; 532 jint off = 0; 533 if (l->is_top()) { 534 return 0; 535 } else if (l->Opcode() == Op_AddI) { 536 if ((off = l->in(1)->find_int_con(0)) != 0) { 537 ind = l->in(2)->uncast(); 538 } else if ((off = l->in(2)->find_int_con(0)) != 0) { 539 ind = l->in(1)->uncast(); 540 } 541 } else if ((off = l->find_int_con(-1)) >= 0) { 542 // constant offset with no variable index 543 ind = nullptr; 544 } else { 545 // variable index with no constant offset (or dead negative index) 546 off = 0; 547 } 548 549 // Return all the values: 550 index = ind; 551 offset = off; 552 range = r; 553 return flip_test; 554 } 555 556 //------------------------------adjust_check----------------------------------- 557 // Adjust (widen) a prior range check 558 static void adjust_check(IfProjNode* proj, Node* range, Node* index, 559 int flip, jint off_lo, PhaseIterGVN* igvn) { 560 PhaseGVN *gvn = igvn; 561 // Break apart the old check 562 Node *iff = proj->in(0); 563 Node *bol = iff->in(1); 564 if( bol->is_top() ) return; // In case a partially dead range check appears 565 // bail (or bomb[ASSERT/DEBUG]) if NOT projection-->IfNode-->BoolNode 566 DEBUG_ONLY( if (!bol->is_Bool()) { proj->dump(3); fatal("Expect projection-->IfNode-->BoolNode"); } ) 567 if (!bol->is_Bool()) return; 568 569 Node *cmp = bol->in(1); 570 // Compute a new check 571 Node *new_add = gvn->intcon(off_lo); 572 if (index) { 573 new_add = off_lo ? gvn->transform(new AddINode(index, new_add)) : index; 574 } 575 Node *new_cmp = (flip == 1) 576 ? new CmpUNode(new_add, range) 577 : new CmpUNode(range, new_add); 578 new_cmp = gvn->transform(new_cmp); 579 // See if no need to adjust the existing check 580 if (new_cmp == cmp) return; 581 // Else, adjust existing check 582 Node* new_bol = gvn->transform(new BoolNode(new_cmp, bol->as_Bool()->_test._test)); 583 igvn->rehash_node_delayed(iff); 584 iff->set_req_X(1, new_bol, igvn); 585 // As part of range check smearing, this range check is widened. Loads and range check Cast nodes that are control 586 // dependent on this range check now depend on multiple dominating range checks. These control dependent nodes end up 587 // at the lowest/nearest dominating check in the graph. To ensure that these Loads/Casts do not float above any of the 588 // dominating checks (even when the lowest dominating check is later replaced by yet another dominating check), we 589 // need to pin them at the lowest dominating check. 590 proj->pin_array_access_nodes(igvn); 591 } 592 593 //------------------------------up_one_dom------------------------------------- 594 // Walk up the dominator tree one step. Return null at root or true 595 // complex merges. Skips through small diamonds. 596 Node* IfNode::up_one_dom(Node *curr, bool linear_only) { 597 Node *dom = curr->in(0); 598 if( !dom ) // Found a Region degraded to a copy? 599 return curr->nonnull_req(); // Skip thru it 600 601 if( curr != dom ) // Normal walk up one step? 602 return dom; 603 604 // Use linear_only if we are still parsing, since we cannot 605 // trust the regions to be fully filled in. 606 if (linear_only) 607 return nullptr; 608 609 if( dom->is_Root() ) 610 return nullptr; 611 612 // Else hit a Region. Check for a loop header 613 if( dom->is_Loop() ) 614 return dom->in(1); // Skip up thru loops 615 616 // Check for small diamonds 617 Node *din1, *din2, *din3, *din4; 618 if( dom->req() == 3 && // 2-path merge point 619 (din1 = dom ->in(1)) && // Left path exists 620 (din2 = dom ->in(2)) && // Right path exists 621 (din3 = din1->in(0)) && // Left path up one 622 (din4 = din2->in(0)) ) { // Right path up one 623 if( din3->is_Call() && // Handle a slow-path call on either arm 624 (din3 = din3->in(0)) ) 625 din3 = din3->in(0); 626 if( din4->is_Call() && // Handle a slow-path call on either arm 627 (din4 = din4->in(0)) ) 628 din4 = din4->in(0); 629 if (din3 != nullptr && din3 == din4 && din3->is_If()) // Regions not degraded to a copy 630 return din3; // Skip around diamonds 631 } 632 633 // Give up the search at true merges 634 return nullptr; // Dead loop? Or hit root? 635 } 636 637 638 //------------------------------filtered_int_type-------------------------------- 639 // Return a possibly more restrictive type for val based on condition control flow for an if 640 const TypeInt* IfNode::filtered_int_type(PhaseGVN* gvn, Node* val, Node* if_proj) { 641 assert(if_proj && 642 (if_proj->Opcode() == Op_IfTrue || if_proj->Opcode() == Op_IfFalse), "expecting an if projection"); 643 if (if_proj->in(0) && if_proj->in(0)->is_If()) { 644 IfNode* iff = if_proj->in(0)->as_If(); 645 if (iff->in(1) && iff->in(1)->is_Bool()) { 646 BoolNode* bol = iff->in(1)->as_Bool(); 647 if (bol->in(1) && bol->in(1)->is_Cmp()) { 648 const CmpNode* cmp = bol->in(1)->as_Cmp(); 649 if (cmp->in(1) == val) { 650 const TypeInt* cmp2_t = gvn->type(cmp->in(2))->isa_int(); 651 if (cmp2_t != nullptr) { 652 jint lo = cmp2_t->_lo; 653 jint hi = cmp2_t->_hi; 654 BoolTest::mask msk = if_proj->Opcode() == Op_IfTrue ? bol->_test._test : bol->_test.negate(); 655 switch (msk) { 656 case BoolTest::ne: { 657 // If val is compared to its lower or upper bound, we can narrow the type 658 const TypeInt* val_t = gvn->type(val)->isa_int(); 659 if (val_t != nullptr && !val_t->singleton() && cmp2_t->is_con()) { 660 if (val_t->_lo == lo) { 661 return TypeInt::make(val_t->_lo + 1, val_t->_hi, val_t->_widen); 662 } else if (val_t->_hi == hi) { 663 return TypeInt::make(val_t->_lo, val_t->_hi - 1, val_t->_widen); 664 } 665 } 666 // Can't refine type 667 return nullptr; 668 } 669 case BoolTest::eq: 670 return cmp2_t; 671 case BoolTest::lt: 672 lo = TypeInt::INT->_lo; 673 if (hi != min_jint) { 674 hi = hi - 1; 675 } 676 break; 677 case BoolTest::le: 678 lo = TypeInt::INT->_lo; 679 break; 680 case BoolTest::gt: 681 if (lo != max_jint) { 682 lo = lo + 1; 683 } 684 hi = TypeInt::INT->_hi; 685 break; 686 case BoolTest::ge: 687 // lo unchanged 688 hi = TypeInt::INT->_hi; 689 break; 690 default: 691 break; 692 } 693 const TypeInt* rtn_t = TypeInt::make(lo, hi, cmp2_t->_widen); 694 return rtn_t; 695 } 696 } 697 } 698 } 699 } 700 return nullptr; 701 } 702 703 //------------------------------fold_compares---------------------------- 704 // See if a pair of CmpIs can be converted into a CmpU. In some cases 705 // the direction of this if is determined by the preceding if so it 706 // can be eliminate entirely. 707 // 708 // Given an if testing (CmpI n v) check for an immediately control 709 // dependent if that is testing (CmpI n v2) and has one projection 710 // leading to this if and the other projection leading to a region 711 // that merges one of this ifs control projections. 712 // 713 // If 714 // / | 715 // / | 716 // / | 717 // If | 718 // /\ | 719 // / \ | 720 // / \ | 721 // / Region 722 // 723 // Or given an if testing (CmpI n v) check for a dominating if that is 724 // testing (CmpI n v2), both having one projection leading to an 725 // uncommon trap. Allow Another independent guard in between to cover 726 // an explicit range check: 727 // if (index < 0 || index >= array.length) { 728 // which may need a null check to guard the LoadRange 729 // 730 // If 731 // / \ 732 // / \ 733 // / \ 734 // If unc 735 // /\ 736 // / \ 737 // / \ 738 // / unc 739 // 740 741 // Is the comparison for this If suitable for folding? 742 bool IfNode::cmpi_folds(PhaseIterGVN* igvn, bool fold_ne) { 743 return in(1) != nullptr && 744 in(1)->is_Bool() && 745 in(1)->in(1) != nullptr && 746 in(1)->in(1)->Opcode() == Op_CmpI && 747 in(1)->in(1)->in(2) != nullptr && 748 in(1)->in(1)->in(2) != igvn->C->top() && 749 (in(1)->as_Bool()->_test.is_less() || 750 in(1)->as_Bool()->_test.is_greater() || 751 (fold_ne && in(1)->as_Bool()->_test._test == BoolTest::ne)); 752 } 753 754 // Is a dominating control suitable for folding with this if? 755 bool IfNode::is_ctrl_folds(Node* ctrl, PhaseIterGVN* igvn) { 756 return ctrl != nullptr && 757 ctrl->is_Proj() && 758 ctrl->in(0) != nullptr && 759 ctrl->in(0)->Opcode() == Op_If && 760 ctrl->in(0)->outcnt() == 2 && 761 ctrl->in(0)->as_If()->cmpi_folds(igvn, true) && 762 // Must compare same value 763 ctrl->in(0)->in(1)->in(1)->in(1) != nullptr && 764 ctrl->in(0)->in(1)->in(1)->in(1) != igvn->C->top() && 765 ctrl->in(0)->in(1)->in(1)->in(1) == in(1)->in(1)->in(1); 766 } 767 768 // Do this If and the dominating If share a region? 769 bool IfNode::has_shared_region(ProjNode* proj, ProjNode*& success, ProjNode*& fail) { 770 ProjNode* otherproj = proj->other_if_proj(); 771 Node* otherproj_ctrl_use = otherproj->unique_ctrl_out_or_null(); 772 RegionNode* region = (otherproj_ctrl_use != nullptr && otherproj_ctrl_use->is_Region()) ? otherproj_ctrl_use->as_Region() : nullptr; 773 success = nullptr; 774 fail = nullptr; 775 776 if (otherproj->outcnt() == 1 && region != nullptr && !region->has_phi()) { 777 for (int i = 0; i < 2; i++) { 778 ProjNode* proj = proj_out(i); 779 if (success == nullptr && proj->outcnt() == 1 && proj->unique_out() == region) { 780 success = proj; 781 } else if (fail == nullptr) { 782 fail = proj; 783 } else { 784 success = fail = nullptr; 785 } 786 } 787 } 788 return success != nullptr && fail != nullptr; 789 } 790 791 bool IfNode::is_dominator_unc(CallStaticJavaNode* dom_unc, CallStaticJavaNode* unc) { 792 // Different methods and methods containing jsrs are not supported. 793 ciMethod* method = unc->jvms()->method(); 794 ciMethod* dom_method = dom_unc->jvms()->method(); 795 if (method != dom_method || method->has_jsrs()) { 796 return false; 797 } 798 // Check that both traps are in the same activation of the method (instead 799 // of two activations being inlined through different call sites) by verifying 800 // that the call stacks are equal for both JVMStates. 801 JVMState* dom_caller = dom_unc->jvms()->caller(); 802 JVMState* caller = unc->jvms()->caller(); 803 if ((dom_caller == nullptr) != (caller == nullptr)) { 804 // The current method must either be inlined into both dom_caller and 805 // caller or must not be inlined at all (top method). Bail out otherwise. 806 return false; 807 } else if (dom_caller != nullptr && !dom_caller->same_calls_as(caller)) { 808 return false; 809 } 810 // Check that the bci of the dominating uncommon trap dominates the bci 811 // of the dominated uncommon trap. Otherwise we may not re-execute 812 // the dominated check after deoptimization from the merged uncommon trap. 813 ciTypeFlow* flow = dom_method->get_flow_analysis(); 814 int bci = unc->jvms()->bci(); 815 int dom_bci = dom_unc->jvms()->bci(); 816 if (!flow->is_dominated_by(bci, dom_bci)) { 817 return false; 818 } 819 820 return true; 821 } 822 823 // Return projection that leads to an uncommon trap if any 824 ProjNode* IfNode::uncommon_trap_proj(CallStaticJavaNode*& call) const { 825 for (int i = 0; i < 2; i++) { 826 call = proj_out(i)->is_uncommon_trap_proj(); 827 if (call != nullptr) { 828 return proj_out(i); 829 } 830 } 831 return nullptr; 832 } 833 834 // Do this If and the dominating If both branch out to an uncommon trap 835 bool IfNode::has_only_uncommon_traps(ProjNode* proj, ProjNode*& success, ProjNode*& fail, PhaseIterGVN* igvn) { 836 ProjNode* otherproj = proj->other_if_proj(); 837 CallStaticJavaNode* dom_unc = otherproj->is_uncommon_trap_proj(); 838 839 if (otherproj->outcnt() == 1 && dom_unc != nullptr) { 840 // We need to re-execute the folded Ifs after deoptimization from the merged traps 841 if (!dom_unc->jvms()->should_reexecute()) { 842 return false; 843 } 844 845 CallStaticJavaNode* unc = nullptr; 846 ProjNode* unc_proj = uncommon_trap_proj(unc); 847 if (unc_proj != nullptr && unc_proj->outcnt() == 1) { 848 if (dom_unc == unc) { 849 // Allow the uncommon trap to be shared through a region 850 RegionNode* r = unc->in(0)->as_Region(); 851 if (r->outcnt() != 2 || r->req() != 3 || r->find_edge(otherproj) == -1 || r->find_edge(unc_proj) == -1) { 852 return false; 853 } 854 assert(r->has_phi() == nullptr, "simple region shouldn't have a phi"); 855 } else if (dom_unc->in(0) != otherproj || unc->in(0) != unc_proj) { 856 return false; 857 } 858 859 if (!is_dominator_unc(dom_unc, unc)) { 860 return false; 861 } 862 863 // See merge_uncommon_traps: the reason of the uncommon trap 864 // will be changed and the state of the dominating If will be 865 // used. Checked that we didn't apply this transformation in a 866 // previous compilation and it didn't cause too many traps 867 ciMethod* dom_method = dom_unc->jvms()->method(); 868 int dom_bci = dom_unc->jvms()->bci(); 869 if (!igvn->C->too_many_traps(dom_method, dom_bci, Deoptimization::Reason_unstable_fused_if) && 870 !igvn->C->too_many_traps(dom_method, dom_bci, Deoptimization::Reason_range_check) && 871 // Return true if c2 manages to reconcile with UnstableIf optimization. See the comments for it. 872 igvn->C->remove_unstable_if_trap(dom_unc, true/*yield*/)) { 873 success = unc_proj; 874 fail = unc_proj->other_if_proj(); 875 return true; 876 } 877 } 878 } 879 return false; 880 } 881 882 // Check that the 2 CmpI can be folded into as single CmpU and proceed with the folding 883 bool IfNode::fold_compares_helper(ProjNode* proj, ProjNode* success, ProjNode* fail, PhaseIterGVN* igvn) { 884 Node* this_cmp = in(1)->in(1); 885 BoolNode* this_bool = in(1)->as_Bool(); 886 IfNode* dom_iff = proj->in(0)->as_If(); 887 BoolNode* dom_bool = dom_iff->in(1)->as_Bool(); 888 Node* lo = dom_iff->in(1)->in(1)->in(2); 889 Node* hi = this_cmp->in(2); 890 Node* n = this_cmp->in(1); 891 ProjNode* otherproj = proj->other_if_proj(); 892 893 const TypeInt* lo_type = IfNode::filtered_int_type(igvn, n, otherproj); 894 const TypeInt* hi_type = IfNode::filtered_int_type(igvn, n, success); 895 896 BoolTest::mask lo_test = dom_bool->_test._test; 897 BoolTest::mask hi_test = this_bool->_test._test; 898 BoolTest::mask cond = hi_test; 899 900 // convert: 901 // 902 // dom_bool = x {<,<=,>,>=} a 903 // / \ 904 // proj = {True,False} / \ otherproj = {False,True} 905 // / 906 // this_bool = x {<,<=} b 907 // / \ 908 // fail = {True,False} / \ success = {False,True} 909 // / 910 // 911 // (Second test guaranteed canonicalized, first one may not have 912 // been canonicalized yet) 913 // 914 // into: 915 // 916 // cond = (x - lo) {<u,<=u,>u,>=u} adjusted_lim 917 // / \ 918 // fail / \ success 919 // / 920 // 921 922 // Figure out which of the two tests sets the upper bound and which 923 // sets the lower bound if any. 924 Node* adjusted_lim = nullptr; 925 if (lo_type != nullptr && hi_type != nullptr && hi_type->_lo > lo_type->_hi && 926 hi_type->_hi == max_jint && lo_type->_lo == min_jint && lo_test != BoolTest::ne) { 927 assert((dom_bool->_test.is_less() && !proj->_con) || 928 (dom_bool->_test.is_greater() && proj->_con), "incorrect test"); 929 930 // this_bool = < 931 // dom_bool = >= (proj = True) or dom_bool = < (proj = False) 932 // x in [a, b[ on the fail (= True) projection, b > a-1 (because of hi_type->_lo > lo_type->_hi test above): 933 // lo = a, hi = b, adjusted_lim = b-a, cond = <u 934 // dom_bool = > (proj = True) or dom_bool = <= (proj = False) 935 // x in ]a, b[ on the fail (= True) projection, b > a: 936 // lo = a+1, hi = b, adjusted_lim = b-a-1, cond = <u 937 // this_bool = <= 938 // dom_bool = >= (proj = True) or dom_bool = < (proj = False) 939 // x in [a, b] on the fail (= True) projection, b+1 > a-1: 940 // lo = a, hi = b, adjusted_lim = b-a+1, cond = <u 941 // lo = a, hi = b, adjusted_lim = b-a, cond = <=u doesn't work because b = a - 1 is possible, then b-a = -1 942 // dom_bool = > (proj = True) or dom_bool = <= (proj = False) 943 // x in ]a, b] on the fail (= True) projection b+1 > a: 944 // lo = a+1, hi = b, adjusted_lim = b-a, cond = <u 945 // lo = a+1, hi = b, adjusted_lim = b-a-1, cond = <=u doesn't work because a = b is possible, then b-a-1 = -1 946 947 if (hi_test == BoolTest::lt) { 948 if (lo_test == BoolTest::gt || lo_test == BoolTest::le) { 949 lo = igvn->transform(new AddINode(lo, igvn->intcon(1))); 950 } 951 } else if (hi_test == BoolTest::le) { 952 if (lo_test == BoolTest::ge || lo_test == BoolTest::lt) { 953 adjusted_lim = igvn->transform(new SubINode(hi, lo)); 954 adjusted_lim = igvn->transform(new AddINode(adjusted_lim, igvn->intcon(1))); 955 cond = BoolTest::lt; 956 } else if (lo_test == BoolTest::gt || lo_test == BoolTest::le) { 957 adjusted_lim = igvn->transform(new SubINode(hi, lo)); 958 lo = igvn->transform(new AddINode(lo, igvn->intcon(1))); 959 cond = BoolTest::lt; 960 } else { 961 assert(false, "unhandled lo_test: %d", lo_test); 962 return false; 963 } 964 } else { 965 assert(igvn->_worklist.member(in(1)) && in(1)->Value(igvn) != igvn->type(in(1)), "unhandled hi_test: %d", hi_test); 966 return false; 967 } 968 // this test was canonicalized 969 assert(this_bool->_test.is_less() && fail->_con, "incorrect test"); 970 } else if (lo_type != nullptr && hi_type != nullptr && lo_type->_lo > hi_type->_hi && 971 lo_type->_hi == max_jint && hi_type->_lo == min_jint && lo_test != BoolTest::ne) { 972 973 // this_bool = < 974 // dom_bool = < (proj = True) or dom_bool = >= (proj = False) 975 // x in [b, a[ on the fail (= False) projection, a > b-1 (because of lo_type->_lo > hi_type->_hi above): 976 // lo = b, hi = a, adjusted_lim = a-b, cond = >=u 977 // dom_bool = <= (proj = True) or dom_bool = > (proj = False) 978 // x in [b, a] on the fail (= False) projection, a+1 > b-1: 979 // lo = b, hi = a, adjusted_lim = a-b+1, cond = >=u 980 // lo = b, hi = a, adjusted_lim = a-b, cond = >u doesn't work because a = b - 1 is possible, then b-a = -1 981 // this_bool = <= 982 // dom_bool = < (proj = True) or dom_bool = >= (proj = False) 983 // x in ]b, a[ on the fail (= False) projection, a > b: 984 // lo = b+1, hi = a, adjusted_lim = a-b-1, cond = >=u 985 // dom_bool = <= (proj = True) or dom_bool = > (proj = False) 986 // x in ]b, a] on the fail (= False) projection, a+1 > b: 987 // lo = b+1, hi = a, adjusted_lim = a-b, cond = >=u 988 // lo = b+1, hi = a, adjusted_lim = a-b-1, cond = >u doesn't work because a = b is possible, then b-a-1 = -1 989 990 swap(lo, hi); 991 swap(lo_type, hi_type); 992 swap(lo_test, hi_test); 993 994 assert((dom_bool->_test.is_less() && proj->_con) || 995 (dom_bool->_test.is_greater() && !proj->_con), "incorrect test"); 996 997 cond = (hi_test == BoolTest::le || hi_test == BoolTest::gt) ? BoolTest::gt : BoolTest::ge; 998 999 if (lo_test == BoolTest::lt) { 1000 if (hi_test == BoolTest::lt || hi_test == BoolTest::ge) { 1001 cond = BoolTest::ge; 1002 } else if (hi_test == BoolTest::le || hi_test == BoolTest::gt) { 1003 adjusted_lim = igvn->transform(new SubINode(hi, lo)); 1004 adjusted_lim = igvn->transform(new AddINode(adjusted_lim, igvn->intcon(1))); 1005 cond = BoolTest::ge; 1006 } else { 1007 assert(false, "unhandled hi_test: %d", hi_test); 1008 return false; 1009 } 1010 } else if (lo_test == BoolTest::le) { 1011 if (hi_test == BoolTest::lt || hi_test == BoolTest::ge) { 1012 lo = igvn->transform(new AddINode(lo, igvn->intcon(1))); 1013 cond = BoolTest::ge; 1014 } else if (hi_test == BoolTest::le || hi_test == BoolTest::gt) { 1015 adjusted_lim = igvn->transform(new SubINode(hi, lo)); 1016 lo = igvn->transform(new AddINode(lo, igvn->intcon(1))); 1017 cond = BoolTest::ge; 1018 } else { 1019 assert(false, "unhandled hi_test: %d", hi_test); 1020 return false; 1021 } 1022 } else { 1023 assert(igvn->_worklist.member(in(1)) && in(1)->Value(igvn) != igvn->type(in(1)), "unhandled lo_test: %d", lo_test); 1024 return false; 1025 } 1026 // this test was canonicalized 1027 assert(this_bool->_test.is_less() && !fail->_con, "incorrect test"); 1028 } else { 1029 const TypeInt* failtype = filtered_int_type(igvn, n, proj); 1030 if (failtype != nullptr) { 1031 const TypeInt* type2 = filtered_int_type(igvn, n, fail); 1032 if (type2 != nullptr) { 1033 failtype = failtype->join(type2)->is_int(); 1034 if (failtype->empty()) { 1035 // previous if determines the result of this if so 1036 // replace Bool with constant 1037 igvn->replace_input_of(this, 1, igvn->intcon(success->_con)); 1038 return true; 1039 } 1040 } 1041 } 1042 return false; 1043 } 1044 1045 assert(lo != nullptr && hi != nullptr, "sanity"); 1046 Node* hook = new Node(lo); // Add a use to lo to prevent him from dying 1047 // Merge the two compares into a single unsigned compare by building (CmpU (n - lo) (hi - lo)) 1048 Node* adjusted_val = igvn->transform(new SubINode(n, lo)); 1049 if (adjusted_lim == nullptr) { 1050 adjusted_lim = igvn->transform(new SubINode(hi, lo)); 1051 } 1052 hook->destruct(igvn); 1053 1054 if (adjusted_val->is_top() || adjusted_lim->is_top()) { 1055 return false; 1056 } 1057 1058 if (igvn->type(adjusted_lim)->is_int()->_lo < 0 && 1059 !igvn->C->post_loop_opts_phase()) { 1060 // If range check elimination applies to this comparison, it includes code to protect from overflows that may 1061 // cause the main loop to be skipped entirely. Delay this transformation. 1062 // Example: 1063 // for (int i = 0; i < limit; i++) { 1064 // if (i < max_jint && i > min_jint) {... 1065 // } 1066 // Comparisons folded as: 1067 // i - min_jint - 1 <u -2 1068 // when RC applies, main loop limit becomes: 1069 // min(limit, max(-2 + min_jint + 1, min_jint)) 1070 // = min(limit, min_jint) 1071 // = min_jint 1072 if (adjusted_val->outcnt() == 0) { 1073 igvn->remove_dead_node(adjusted_val); 1074 } 1075 if (adjusted_lim->outcnt() == 0) { 1076 igvn->remove_dead_node(adjusted_lim); 1077 } 1078 igvn->C->record_for_post_loop_opts_igvn(this); 1079 return false; 1080 } 1081 1082 Node* newcmp = igvn->transform(new CmpUNode(adjusted_val, adjusted_lim)); 1083 Node* newbool = igvn->transform(new BoolNode(newcmp, cond)); 1084 1085 igvn->replace_input_of(dom_iff, 1, igvn->intcon(proj->_con)); 1086 igvn->replace_input_of(this, 1, newbool); 1087 1088 return true; 1089 } 1090 1091 // Merge the branches that trap for this If and the dominating If into 1092 // a single region that branches to the uncommon trap for the 1093 // dominating If 1094 Node* IfNode::merge_uncommon_traps(ProjNode* proj, ProjNode* success, ProjNode* fail, PhaseIterGVN* igvn) { 1095 Node* res = this; 1096 assert(success->in(0) == this, "bad projection"); 1097 1098 ProjNode* otherproj = proj->other_if_proj(); 1099 1100 CallStaticJavaNode* unc = success->is_uncommon_trap_proj(); 1101 CallStaticJavaNode* dom_unc = otherproj->is_uncommon_trap_proj(); 1102 1103 if (unc != dom_unc) { 1104 Node* r = new RegionNode(3); 1105 1106 r->set_req(1, otherproj); 1107 r->set_req(2, success); 1108 r = igvn->transform(r); 1109 assert(r->is_Region(), "can't go away"); 1110 1111 // Make both If trap at the state of the first If: once the CmpI 1112 // nodes are merged, if we trap we don't know which of the CmpI 1113 // nodes would have caused the trap so we have to restart 1114 // execution at the first one 1115 igvn->replace_input_of(dom_unc, 0, r); 1116 igvn->replace_input_of(unc, 0, igvn->C->top()); 1117 } 1118 int trap_request = dom_unc->uncommon_trap_request(); 1119 Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(trap_request); 1120 Deoptimization::DeoptAction action = Deoptimization::trap_request_action(trap_request); 1121 1122 int flip_test = 0; 1123 Node* l = nullptr; 1124 Node* r = nullptr; 1125 1126 if (success->in(0)->as_If()->range_check_trap_proj(flip_test, l, r) != nullptr) { 1127 // If this looks like a range check, change the trap to 1128 // Reason_range_check so the compiler recognizes it as a range 1129 // check and applies the corresponding optimizations 1130 trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_range_check, action); 1131 1132 improve_address_types(l, r, fail, igvn); 1133 1134 res = igvn->transform(new RangeCheckNode(in(0), in(1), _prob, _fcnt)); 1135 } else if (unc != dom_unc) { 1136 // If we trap we won't know what CmpI would have caused the trap 1137 // so use a special trap reason to mark this pair of CmpI nodes as 1138 // bad candidate for folding. On recompilation we won't fold them 1139 // and we may trap again but this time we'll know what branch 1140 // traps 1141 trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_unstable_fused_if, action); 1142 } 1143 igvn->replace_input_of(dom_unc, TypeFunc::Parms, igvn->intcon(trap_request)); 1144 return res; 1145 } 1146 1147 // If we are turning 2 CmpI nodes into a CmpU that follows the pattern 1148 // of a rangecheck on index i, on 64 bit the compares may be followed 1149 // by memory accesses using i as index. In that case, the CmpU tells 1150 // us something about the values taken by i that can help the compiler 1151 // (see Compile::conv_I2X_index()) 1152 void IfNode::improve_address_types(Node* l, Node* r, ProjNode* fail, PhaseIterGVN* igvn) { 1153 #ifdef _LP64 1154 ResourceMark rm; 1155 Node_Stack stack(2); 1156 1157 assert(r->Opcode() == Op_LoadRange, "unexpected range check"); 1158 const TypeInt* array_size = igvn->type(r)->is_int(); 1159 1160 stack.push(l, 0); 1161 1162 while(stack.size() > 0) { 1163 Node* n = stack.node(); 1164 uint start = stack.index(); 1165 1166 uint i = start; 1167 for (; i < n->outcnt(); i++) { 1168 Node* use = n->raw_out(i); 1169 if (stack.size() == 1) { 1170 if (use->Opcode() == Op_ConvI2L) { 1171 const TypeLong* bounds = use->as_Type()->type()->is_long(); 1172 if (bounds->_lo <= array_size->_lo && bounds->_hi >= array_size->_hi && 1173 (bounds->_lo != array_size->_lo || bounds->_hi != array_size->_hi)) { 1174 stack.set_index(i+1); 1175 stack.push(use, 0); 1176 break; 1177 } 1178 } 1179 } else if (use->is_Mem()) { 1180 Node* ctrl = use->in(0); 1181 for (int i = 0; i < 10 && ctrl != nullptr && ctrl != fail; i++) { 1182 ctrl = up_one_dom(ctrl); 1183 } 1184 if (ctrl == fail) { 1185 Node* init_n = stack.node_at(1); 1186 assert(init_n->Opcode() == Op_ConvI2L, "unexpected first node"); 1187 // Create a new narrow ConvI2L node that is dependent on the range check 1188 Node* new_n = igvn->C->conv_I2X_index(igvn, l, array_size, fail); 1189 1190 // The type of the ConvI2L may be widen and so the new 1191 // ConvI2L may not be better than an existing ConvI2L 1192 if (new_n != init_n) { 1193 for (uint j = 2; j < stack.size(); j++) { 1194 Node* n = stack.node_at(j); 1195 Node* clone = n->clone(); 1196 int rep = clone->replace_edge(init_n, new_n, igvn); 1197 assert(rep > 0, "can't find expected node?"); 1198 clone = igvn->transform(clone); 1199 init_n = n; 1200 new_n = clone; 1201 } 1202 igvn->hash_delete(use); 1203 int rep = use->replace_edge(init_n, new_n, igvn); 1204 assert(rep > 0, "can't find expected node?"); 1205 igvn->transform(use); 1206 if (init_n->outcnt() == 0) { 1207 igvn->_worklist.push(init_n); 1208 } 1209 } 1210 } 1211 } else if (use->in(0) == nullptr && (igvn->type(use)->isa_long() || 1212 igvn->type(use)->isa_ptr())) { 1213 stack.set_index(i+1); 1214 stack.push(use, 0); 1215 break; 1216 } 1217 } 1218 if (i == n->outcnt()) { 1219 stack.pop(); 1220 } 1221 } 1222 #endif 1223 } 1224 1225 bool IfNode::is_cmp_with_loadrange(ProjNode* proj) { 1226 if (in(1) != nullptr && 1227 in(1)->in(1) != nullptr && 1228 in(1)->in(1)->in(2) != nullptr) { 1229 Node* other = in(1)->in(1)->in(2); 1230 if (other->Opcode() == Op_LoadRange && 1231 ((other->in(0) != nullptr && other->in(0) == proj) || 1232 (other->in(0) == nullptr && 1233 other->in(2) != nullptr && 1234 other->in(2)->is_AddP() && 1235 other->in(2)->in(1) != nullptr && 1236 other->in(2)->in(1)->Opcode() == Op_CastPP && 1237 other->in(2)->in(1)->in(0) == proj))) { 1238 return true; 1239 } 1240 } 1241 return false; 1242 } 1243 1244 bool IfNode::is_null_check(ProjNode* proj, PhaseIterGVN* igvn) { 1245 Node* other = in(1)->in(1)->in(2); 1246 if (other->in(MemNode::Address) != nullptr && 1247 proj->in(0)->in(1) != nullptr && 1248 proj->in(0)->in(1)->is_Bool() && 1249 proj->in(0)->in(1)->in(1) != nullptr && 1250 proj->in(0)->in(1)->in(1)->Opcode() == Op_CmpP && 1251 proj->in(0)->in(1)->in(1)->in(2) != nullptr && 1252 proj->in(0)->in(1)->in(1)->in(1) == other->in(MemNode::Address)->in(AddPNode::Address)->uncast() && 1253 igvn->type(proj->in(0)->in(1)->in(1)->in(2)) == TypePtr::NULL_PTR) { 1254 return true; 1255 } 1256 return false; 1257 } 1258 1259 // Check that the If that is in between the 2 integer comparisons has 1260 // no side effect 1261 bool IfNode::is_side_effect_free_test(ProjNode* proj, PhaseIterGVN* igvn) { 1262 if (proj == nullptr) { 1263 return false; 1264 } 1265 CallStaticJavaNode* unc = proj->is_uncommon_trap_if_pattern(); 1266 if (unc != nullptr && proj->outcnt() <= 2) { 1267 if (proj->outcnt() == 1 || 1268 // Allow simple null check from LoadRange 1269 (is_cmp_with_loadrange(proj) && is_null_check(proj, igvn))) { 1270 CallStaticJavaNode* unc = proj->is_uncommon_trap_if_pattern(); 1271 CallStaticJavaNode* dom_unc = proj->in(0)->in(0)->as_Proj()->is_uncommon_trap_if_pattern(); 1272 assert(dom_unc != nullptr, "is_uncommon_trap_if_pattern returned null"); 1273 1274 // reroute_side_effect_free_unc changes the state of this 1275 // uncommon trap to restart execution at the previous 1276 // CmpI. Check that this change in a previous compilation didn't 1277 // cause too many traps. 1278 int trap_request = unc->uncommon_trap_request(); 1279 Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(trap_request); 1280 1281 if (igvn->C->too_many_traps(dom_unc->jvms()->method(), dom_unc->jvms()->bci(), reason)) { 1282 return false; 1283 } 1284 1285 if (!is_dominator_unc(dom_unc, unc)) { 1286 return false; 1287 } 1288 1289 return true; 1290 } 1291 } 1292 return false; 1293 } 1294 1295 // Make the If between the 2 integer comparisons trap at the state of 1296 // the first If: the last CmpI is the one replaced by a CmpU and the 1297 // first CmpI is eliminated, so the test between the 2 CmpI nodes 1298 // won't be guarded by the first CmpI anymore. It can trap in cases 1299 // where the first CmpI would have prevented it from executing: on a 1300 // trap, we need to restart execution at the state of the first CmpI 1301 void IfNode::reroute_side_effect_free_unc(ProjNode* proj, ProjNode* dom_proj, PhaseIterGVN* igvn) { 1302 CallStaticJavaNode* dom_unc = dom_proj->is_uncommon_trap_if_pattern(); 1303 ProjNode* otherproj = proj->other_if_proj(); 1304 CallStaticJavaNode* unc = proj->is_uncommon_trap_if_pattern(); 1305 Node* call_proj = dom_unc->unique_ctrl_out(); 1306 Node* halt = call_proj->unique_ctrl_out(); 1307 1308 Node* new_unc = dom_unc->clone(); 1309 call_proj = call_proj->clone(); 1310 halt = halt->clone(); 1311 Node* c = otherproj->clone(); 1312 1313 c = igvn->transform(c); 1314 new_unc->set_req(TypeFunc::Parms, unc->in(TypeFunc::Parms)); 1315 new_unc->set_req(0, c); 1316 new_unc = igvn->transform(new_unc); 1317 call_proj->set_req(0, new_unc); 1318 call_proj = igvn->transform(call_proj); 1319 halt->set_req(0, call_proj); 1320 halt = igvn->transform(halt); 1321 1322 igvn->replace_node(otherproj, igvn->C->top()); 1323 igvn->C->root()->add_req(halt); 1324 } 1325 1326 Node* IfNode::fold_compares(PhaseIterGVN* igvn) { 1327 if (Opcode() != Op_If) return nullptr; 1328 1329 if (cmpi_folds(igvn)) { 1330 Node* ctrl = in(0); 1331 if (is_ctrl_folds(ctrl, igvn) && ctrl->outcnt() == 1) { 1332 // A integer comparison immediately dominated by another integer 1333 // comparison 1334 ProjNode* success = nullptr; 1335 ProjNode* fail = nullptr; 1336 ProjNode* dom_cmp = ctrl->as_Proj(); 1337 if (has_shared_region(dom_cmp, success, fail) && 1338 // Next call modifies graph so must be last 1339 fold_compares_helper(dom_cmp, success, fail, igvn)) { 1340 return this; 1341 } 1342 if (has_only_uncommon_traps(dom_cmp, success, fail, igvn) && 1343 // Next call modifies graph so must be last 1344 fold_compares_helper(dom_cmp, success, fail, igvn)) { 1345 return merge_uncommon_traps(dom_cmp, success, fail, igvn); 1346 } 1347 return nullptr; 1348 } else if (ctrl->in(0) != nullptr && 1349 ctrl->in(0)->in(0) != nullptr) { 1350 ProjNode* success = nullptr; 1351 ProjNode* fail = nullptr; 1352 Node* dom = ctrl->in(0)->in(0); 1353 ProjNode* dom_cmp = dom->isa_Proj(); 1354 ProjNode* other_cmp = ctrl->isa_Proj(); 1355 1356 // Check if it's an integer comparison dominated by another 1357 // integer comparison with another test in between 1358 if (is_ctrl_folds(dom, igvn) && 1359 has_only_uncommon_traps(dom_cmp, success, fail, igvn) && 1360 is_side_effect_free_test(other_cmp, igvn) && 1361 // Next call modifies graph so must be last 1362 fold_compares_helper(dom_cmp, success, fail, igvn)) { 1363 reroute_side_effect_free_unc(other_cmp, dom_cmp, igvn); 1364 return merge_uncommon_traps(dom_cmp, success, fail, igvn); 1365 } 1366 } 1367 } 1368 return nullptr; 1369 } 1370 1371 //------------------------------remove_useless_bool---------------------------- 1372 // Check for people making a useless boolean: things like 1373 // if( (x < y ? true : false) ) { ... } 1374 // Replace with if( x < y ) { ... } 1375 static Node *remove_useless_bool(IfNode *iff, PhaseGVN *phase) { 1376 Node *i1 = iff->in(1); 1377 if( !i1->is_Bool() ) return nullptr; 1378 BoolNode *bol = i1->as_Bool(); 1379 1380 Node *cmp = bol->in(1); 1381 if( cmp->Opcode() != Op_CmpI ) return nullptr; 1382 1383 // Must be comparing against a bool 1384 const Type *cmp2_t = phase->type( cmp->in(2) ); 1385 if( cmp2_t != TypeInt::ZERO && 1386 cmp2_t != TypeInt::ONE ) 1387 return nullptr; 1388 1389 // Find a prior merge point merging the boolean 1390 i1 = cmp->in(1); 1391 if( !i1->is_Phi() ) return nullptr; 1392 PhiNode *phi = i1->as_Phi(); 1393 if( phase->type( phi ) != TypeInt::BOOL ) 1394 return nullptr; 1395 1396 // Check for diamond pattern 1397 int true_path = phi->is_diamond_phi(); 1398 if( true_path == 0 ) return nullptr; 1399 1400 // Make sure that iff and the control of the phi are different. This 1401 // should really only happen for dead control flow since it requires 1402 // an illegal cycle. 1403 if (phi->in(0)->in(1)->in(0) == iff) return nullptr; 1404 1405 // phi->region->if_proj->ifnode->bool->cmp 1406 BoolNode *bol2 = phi->in(0)->in(1)->in(0)->in(1)->as_Bool(); 1407 1408 // Now get the 'sense' of the test correct so we can plug in 1409 // either iff2->in(1) or its complement. 1410 int flip = 0; 1411 if( bol->_test._test == BoolTest::ne ) flip = 1-flip; 1412 else if( bol->_test._test != BoolTest::eq ) return nullptr; 1413 if( cmp2_t == TypeInt::ZERO ) flip = 1-flip; 1414 1415 const Type *phi1_t = phase->type( phi->in(1) ); 1416 const Type *phi2_t = phase->type( phi->in(2) ); 1417 // Check for Phi(0,1) and flip 1418 if( phi1_t == TypeInt::ZERO ) { 1419 if( phi2_t != TypeInt::ONE ) return nullptr; 1420 flip = 1-flip; 1421 } else { 1422 // Check for Phi(1,0) 1423 if( phi1_t != TypeInt::ONE ) return nullptr; 1424 if( phi2_t != TypeInt::ZERO ) return nullptr; 1425 } 1426 if( true_path == 2 ) { 1427 flip = 1-flip; 1428 } 1429 1430 Node* new_bol = (flip ? phase->transform( bol2->negate(phase) ) : bol2); 1431 assert(new_bol != iff->in(1), "must make progress"); 1432 iff->set_req_X(1, new_bol, phase); 1433 // Intervening diamond probably goes dead 1434 phase->C->set_major_progress(); 1435 return iff; 1436 } 1437 1438 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff); 1439 1440 struct RangeCheck { 1441 IfProjNode* ctl; 1442 jint off; 1443 }; 1444 1445 Node* IfNode::Ideal_common(PhaseGVN *phase, bool can_reshape) { 1446 if (remove_dead_region(phase, can_reshape)) return this; 1447 // No Def-Use info? 1448 if (!can_reshape) return nullptr; 1449 1450 // Don't bother trying to transform a dead if 1451 if (in(0)->is_top()) return nullptr; 1452 // Don't bother trying to transform an if with a dead test 1453 if (in(1)->is_top()) return nullptr; 1454 // Another variation of a dead test 1455 if (in(1)->is_Con()) return nullptr; 1456 // Another variation of a dead if 1457 if (outcnt() < 2) return nullptr; 1458 1459 // Canonicalize the test. 1460 Node* idt_if = idealize_test(phase, this); 1461 if (idt_if != nullptr) return idt_if; 1462 1463 // Try to split the IF 1464 PhaseIterGVN *igvn = phase->is_IterGVN(); 1465 Node *s = split_if(this, igvn); 1466 if (s != nullptr) return s; 1467 1468 return NodeSentinel; 1469 } 1470 1471 //------------------------------Ideal------------------------------------------ 1472 // Return a node which is more "ideal" than the current node. Strip out 1473 // control copies 1474 Node* IfNode::Ideal(PhaseGVN *phase, bool can_reshape) { 1475 Node* res = Ideal_common(phase, can_reshape); 1476 if (res != NodeSentinel) { 1477 return res; 1478 } 1479 1480 // Check for people making a useless boolean: things like 1481 // if( (x < y ? true : false) ) { ... } 1482 // Replace with if( x < y ) { ... } 1483 Node* bol2 = remove_useless_bool(this, phase); 1484 if (bol2) return bol2; 1485 1486 if (in(0) == nullptr) return nullptr; // Dead loop? 1487 1488 PhaseIterGVN* igvn = phase->is_IterGVN(); 1489 Node* result = fold_compares(igvn); 1490 if (result != nullptr) { 1491 return result; 1492 } 1493 1494 // Scan for an equivalent test 1495 int dist = 4; // Cutoff limit for search 1496 if (is_If() && in(1)->is_Bool()) { 1497 Node* cmp = in(1)->in(1); 1498 if (cmp->Opcode() == Op_CmpP && 1499 cmp->in(2) != nullptr && // make sure cmp is not already dead 1500 cmp->in(2)->bottom_type() == TypePtr::NULL_PTR) { 1501 dist = 64; // Limit for null-pointer scans 1502 } 1503 } 1504 1505 Node* prev_dom = search_identical(dist, igvn); 1506 1507 if (prev_dom != nullptr) { 1508 // Replace dominated IfNode 1509 return dominated_by(prev_dom, igvn, false); 1510 } 1511 1512 return simple_subsuming(igvn); 1513 } 1514 1515 //------------------------------dominated_by----------------------------------- 1516 Node* IfNode::dominated_by(Node* prev_dom, PhaseIterGVN* igvn, bool pin_array_access_nodes) { 1517 #ifndef PRODUCT 1518 if (TraceIterativeGVN) { 1519 tty->print(" Removing IfNode: "); this->dump(); 1520 } 1521 #endif 1522 1523 igvn->hash_delete(this); // Remove self to prevent spurious V-N 1524 Node *idom = in(0); 1525 // Need opcode to decide which way 'this' test goes 1526 int prev_op = prev_dom->Opcode(); 1527 Node *top = igvn->C->top(); // Shortcut to top 1528 1529 // Now walk the current IfNode's projections. 1530 // Loop ends when 'this' has no more uses. 1531 for (DUIterator_Last imin, i = last_outs(imin); i >= imin; --i) { 1532 Node *ifp = last_out(i); // Get IfTrue/IfFalse 1533 igvn->add_users_to_worklist(ifp); 1534 // Check which projection it is and set target. 1535 // Data-target is either the dominating projection of the same type 1536 // or TOP if the dominating projection is of opposite type. 1537 // Data-target will be used as the new control edge for the non-CFG 1538 // nodes like Casts and Loads. 1539 Node *data_target = (ifp->Opcode() == prev_op) ? prev_dom : top; 1540 // Control-target is just the If's immediate dominator or TOP. 1541 Node *ctrl_target = (ifp->Opcode() == prev_op) ? idom : top; 1542 1543 // For each child of an IfTrue/IfFalse projection, reroute. 1544 // Loop ends when projection has no more uses. 1545 for (DUIterator_Last jmin, j = ifp->last_outs(jmin); j >= jmin; --j) { 1546 Node* s = ifp->last_out(j); // Get child of IfTrue/IfFalse 1547 if (s->depends_only_on_test() && igvn->no_dependent_zero_check(s)) { 1548 // For control producers. 1549 // Do not rewire Div and Mod nodes which could have a zero divisor to avoid skipping their zero check. 1550 igvn->replace_input_of(s, 0, data_target); // Move child to data-target 1551 if (pin_array_access_nodes && data_target != top) { 1552 // As a result of range check smearing, Loads and range check Cast nodes that are control dependent on this 1553 // range check (that is about to be removed) now depend on multiple dominating range checks. After the removal 1554 // of this range check, these control dependent nodes end up at the lowest/nearest dominating check in the 1555 // graph. To ensure that these Loads/Casts do not float above any of the dominating checks (even when the 1556 // lowest dominating check is later replaced by yet another dominating check), we need to pin them at the 1557 // lowest dominating check. 1558 Node* clone = s->pin_array_access_node(); 1559 if (clone != nullptr) { 1560 clone = igvn->transform(clone); 1561 igvn->replace_node(s, clone); 1562 } 1563 } 1564 } else { 1565 // Find the control input matching this def-use edge. 1566 // For Regions it may not be in slot 0. 1567 uint l; 1568 for (l = 0; s->in(l) != ifp; l++) { } 1569 igvn->replace_input_of(s, l, ctrl_target); 1570 } 1571 } // End for each child of a projection 1572 1573 igvn->remove_dead_node(ifp); 1574 } // End for each IfTrue/IfFalse child of If 1575 1576 // Kill the IfNode 1577 igvn->remove_dead_node(this); 1578 1579 // Must return either the original node (now dead) or a new node 1580 // (Do not return a top here, since that would break the uniqueness of top.) 1581 return new ConINode(TypeInt::ZERO); 1582 } 1583 1584 Node* IfNode::search_identical(int dist, PhaseIterGVN* igvn) { 1585 // Setup to scan up the CFG looking for a dominating test 1586 Node* dom = in(0); 1587 Node* prev_dom = this; 1588 int op = Opcode(); 1589 // Search up the dominator tree for an If with an identical test 1590 while (dom->Opcode() != op || // Not same opcode? 1591 !same_condition(dom, igvn) || // Not same input 1? 1592 prev_dom->in(0) != dom) { // One path of test does not dominate? 1593 if (dist < 0) return nullptr; 1594 1595 dist--; 1596 prev_dom = dom; 1597 dom = up_one_dom(dom); 1598 if (!dom) return nullptr; 1599 } 1600 1601 // Check that we did not follow a loop back to ourselves 1602 if (this == dom) { 1603 return nullptr; 1604 } 1605 1606 #ifndef PRODUCT 1607 if (dist > 2) { // Add to count of null checks elided 1608 explicit_null_checks_elided++; 1609 } 1610 #endif 1611 1612 return prev_dom; 1613 } 1614 1615 bool IfNode::same_condition(const Node* dom, PhaseIterGVN* igvn) const { 1616 Node* dom_bool = dom->in(1); 1617 Node* this_bool = in(1); 1618 if (dom_bool == this_bool) { 1619 return true; 1620 } 1621 1622 if (dom_bool == nullptr || !dom_bool->is_Bool() || 1623 this_bool == nullptr || !this_bool->is_Bool()) { 1624 return false; 1625 } 1626 Node* dom_cmp = dom_bool->in(1); 1627 Node* this_cmp = this_bool->in(1); 1628 1629 // If the comparison is a subtype check, then SubTypeCheck nodes may have profile data attached to them and may be 1630 // different nodes even-though they perform the same subtype check 1631 if (dom_cmp == nullptr || !dom_cmp->is_SubTypeCheck() || 1632 this_cmp == nullptr || !this_cmp->is_SubTypeCheck()) { 1633 return false; 1634 } 1635 1636 if (dom_cmp->in(1) != this_cmp->in(1) || 1637 dom_cmp->in(2) != this_cmp->in(2) || 1638 dom_bool->as_Bool()->_test._test != this_bool->as_Bool()->_test._test) { 1639 return false; 1640 } 1641 1642 return true; 1643 } 1644 1645 1646 static int subsuming_bool_test_encode(Node*); 1647 1648 // Check if dominating test is subsuming 'this' one. 1649 // 1650 // cmp 1651 // / \ 1652 // (r1) bool \ 1653 // / bool (r2) 1654 // (dom) if \ 1655 // \ ) 1656 // (pre) if[TF] / 1657 // \ / 1658 // if (this) 1659 // \r1 1660 // r2\ eqT eqF neT neF ltT ltF leT leF gtT gtF geT geF 1661 // eq t f f t f - - f f - - f 1662 // ne f t t f t - - t t - - t 1663 // lt f - - f t f - f f - f t 1664 // le t - - t t - t f f t - t 1665 // gt f - - f f - f t t f - f 1666 // ge t - - t f t - t t - t f 1667 // 1668 Node* IfNode::simple_subsuming(PhaseIterGVN* igvn) { 1669 // Table encoding: N/A (na), True-branch (tb), False-branch (fb). 1670 static enum { na, tb, fb } s_short_circuit_map[6][12] = { 1671 /*rel: eq+T eq+F ne+T ne+F lt+T lt+F le+T le+F gt+T gt+F ge+T ge+F*/ 1672 /*eq*/{ tb, fb, fb, tb, fb, na, na, fb, fb, na, na, fb }, 1673 /*ne*/{ fb, tb, tb, fb, tb, na, na, tb, tb, na, na, tb }, 1674 /*lt*/{ fb, na, na, fb, tb, fb, na, fb, fb, na, fb, tb }, 1675 /*le*/{ tb, na, na, tb, tb, na, tb, fb, fb, tb, na, tb }, 1676 /*gt*/{ fb, na, na, fb, fb, na, fb, tb, tb, fb, na, fb }, 1677 /*ge*/{ tb, na, na, tb, fb, tb, na, tb, tb, na, tb, fb }}; 1678 1679 Node* pre = in(0); 1680 if (!pre->is_IfTrue() && !pre->is_IfFalse()) { 1681 return nullptr; 1682 } 1683 Node* dom = pre->in(0); 1684 if (!dom->is_If()) { 1685 return nullptr; 1686 } 1687 Node* bol = in(1); 1688 if (!bol->is_Bool()) { 1689 return nullptr; 1690 } 1691 Node* cmp = in(1)->in(1); 1692 if (!cmp->is_Cmp()) { 1693 return nullptr; 1694 } 1695 1696 if (!dom->in(1)->is_Bool()) { 1697 return nullptr; 1698 } 1699 if (dom->in(1)->in(1) != cmp) { // Not same cond? 1700 return nullptr; 1701 } 1702 1703 int drel = subsuming_bool_test_encode(dom->in(1)); 1704 int trel = subsuming_bool_test_encode(bol); 1705 int bout = pre->is_IfFalse() ? 1 : 0; 1706 1707 if (drel < 0 || trel < 0) { 1708 return nullptr; 1709 } 1710 int br = s_short_circuit_map[trel][2*drel+bout]; 1711 if (br == na) { 1712 return nullptr; 1713 } 1714 #ifndef PRODUCT 1715 if (TraceIterativeGVN) { 1716 tty->print(" Subsumed IfNode: "); dump(); 1717 } 1718 #endif 1719 // Replace condition with constant True(1)/False(0). 1720 bool is_always_true = br == tb; 1721 set_req(1, igvn->intcon(is_always_true ? 1 : 0)); 1722 1723 // Update any data dependencies to the directly dominating test. This subsumed test is not immediately removed by igvn 1724 // and therefore subsequent optimizations might miss these data dependencies otherwise. There might be a dead loop 1725 // ('always_taken_proj' == 'pre') that is cleaned up later. Skip this case to make the iterator work properly. 1726 Node* always_taken_proj = proj_out(is_always_true); 1727 if (always_taken_proj != pre) { 1728 for (DUIterator_Fast imax, i = always_taken_proj->fast_outs(imax); i < imax; i++) { 1729 Node* u = always_taken_proj->fast_out(i); 1730 if (!u->is_CFG()) { 1731 igvn->replace_input_of(u, 0, pre); 1732 --i; 1733 --imax; 1734 } 1735 } 1736 } 1737 1738 if (bol->outcnt() == 0) { 1739 igvn->remove_dead_node(bol); // Kill the BoolNode. 1740 } 1741 return this; 1742 } 1743 1744 // Map BoolTest to local table encoding. The BoolTest (e)numerals 1745 // { eq = 0, ne = 4, le = 5, ge = 7, lt = 3, gt = 1 } 1746 // are mapped to table indices, while the remaining (e)numerals in BoolTest 1747 // { overflow = 2, no_overflow = 6, never = 8, illegal = 9 } 1748 // are ignored (these are not modeled in the table). 1749 // 1750 static int subsuming_bool_test_encode(Node* node) { 1751 precond(node->is_Bool()); 1752 BoolTest::mask x = node->as_Bool()->_test._test; 1753 switch (x) { 1754 case BoolTest::eq: return 0; 1755 case BoolTest::ne: return 1; 1756 case BoolTest::lt: return 2; 1757 case BoolTest::le: return 3; 1758 case BoolTest::gt: return 4; 1759 case BoolTest::ge: return 5; 1760 case BoolTest::overflow: 1761 case BoolTest::no_overflow: 1762 case BoolTest::never: 1763 case BoolTest::illegal: 1764 default: 1765 return -1; 1766 } 1767 } 1768 1769 //------------------------------Identity--------------------------------------- 1770 // If the test is constant & we match, then we are the input Control 1771 Node* IfProjNode::Identity(PhaseGVN* phase) { 1772 // Can only optimize if cannot go the other way 1773 const TypeTuple *t = phase->type(in(0))->is_tuple(); 1774 if (t == TypeTuple::IFNEITHER || (always_taken(t) && 1775 // During parsing (GVN) we don't remove dead code aggressively. 1776 // Cut off dead branch and let PhaseRemoveUseless take care of it. 1777 (!phase->is_IterGVN() || 1778 // During IGVN, first wait for the dead branch to be killed. 1779 // Otherwise, the IfNode's control will have two control uses (the IfNode 1780 // that doesn't go away because it still has uses and this branch of the 1781 // If) which breaks other optimizations. Node::has_special_unique_user() 1782 // will cause this node to be reprocessed once the dead branch is killed. 1783 in(0)->outcnt() == 1))) { 1784 // IfNode control 1785 if (in(0)->is_BaseCountedLoopEnd()) { 1786 // CountedLoopEndNode may be eliminated by if subsuming, replace CountedLoopNode with LoopNode to 1787 // avoid mismatching between CountedLoopNode and CountedLoopEndNode in the following optimization. 1788 Node* head = unique_ctrl_out_or_null(); 1789 if (head != nullptr && head->is_BaseCountedLoop() && head->in(LoopNode::LoopBackControl) == this) { 1790 Node* new_head = new LoopNode(head->in(LoopNode::EntryControl), this); 1791 phase->is_IterGVN()->register_new_node_with_optimizer(new_head); 1792 phase->is_IterGVN()->replace_node(head, new_head); 1793 } 1794 } 1795 return in(0)->in(0); 1796 } 1797 // no progress 1798 return this; 1799 } 1800 1801 bool IfNode::is_zero_trip_guard() const { 1802 if (in(1)->is_Bool() && in(1)->in(1)->is_Cmp()) { 1803 return in(1)->in(1)->in(1)->Opcode() == Op_OpaqueZeroTripGuard; 1804 } 1805 return false; 1806 } 1807 1808 void IfProjNode::pin_array_access_nodes(PhaseIterGVN* igvn) { 1809 for (DUIterator i = outs(); has_out(i); i++) { 1810 Node* u = out(i); 1811 if (!u->depends_only_on_test()) { 1812 continue; 1813 } 1814 Node* clone = u->pin_array_access_node(); 1815 if (clone != nullptr) { 1816 clone = igvn->transform(clone); 1817 assert(clone != u, "shouldn't common"); 1818 igvn->replace_node(u, clone); 1819 --i; 1820 } 1821 } 1822 } 1823 1824 #ifndef PRODUCT 1825 //------------------------------dump_spec-------------------------------------- 1826 void IfNode::dump_spec(outputStream *st) const { 1827 st->print("P=%f, C=%f",_prob,_fcnt); 1828 } 1829 #endif 1830 1831 //------------------------------idealize_test---------------------------------- 1832 // Try to canonicalize tests better. Peek at the Cmp/Bool/If sequence and 1833 // come up with a canonical sequence. Bools getting 'eq', 'gt' and 'ge' forms 1834 // converted to 'ne', 'le' and 'lt' forms. IfTrue/IfFalse get swapped as 1835 // needed. 1836 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff) { 1837 assert(iff->in(0) != nullptr, "If must be live"); 1838 1839 if (iff->outcnt() != 2) return nullptr; // Malformed projections. 1840 Node* old_if_f = iff->proj_out(false); 1841 Node* old_if_t = iff->proj_out(true); 1842 1843 // CountedLoopEnds want the back-control test to be TRUE, regardless of 1844 // whether they are testing a 'gt' or 'lt' condition. The 'gt' condition 1845 // happens in count-down loops 1846 if (iff->is_BaseCountedLoopEnd()) return nullptr; 1847 if (!iff->in(1)->is_Bool()) return nullptr; // Happens for partially optimized IF tests 1848 BoolNode *b = iff->in(1)->as_Bool(); 1849 BoolTest bt = b->_test; 1850 // Test already in good order? 1851 if( bt.is_canonical() ) 1852 return nullptr; 1853 1854 // Flip test to be canonical. Requires flipping the IfFalse/IfTrue and 1855 // cloning the IfNode. 1856 Node* new_b = phase->transform( new BoolNode(b->in(1), bt.negate()) ); 1857 if( !new_b->is_Bool() ) return nullptr; 1858 b = new_b->as_Bool(); 1859 1860 PhaseIterGVN *igvn = phase->is_IterGVN(); 1861 assert( igvn, "Test is not canonical in parser?" ); 1862 1863 // The IF node never really changes, but it needs to be cloned 1864 iff = iff->clone()->as_If(); 1865 iff->set_req(1, b); 1866 iff->_prob = 1.0-iff->_prob; 1867 1868 Node *prior = igvn->hash_find_insert(iff); 1869 if( prior ) { 1870 igvn->remove_dead_node(iff); 1871 iff = (IfNode*)prior; 1872 } else { 1873 // Cannot call transform on it just yet 1874 igvn->set_type_bottom(iff); 1875 } 1876 igvn->_worklist.push(iff); 1877 1878 // Now handle projections. Cloning not required. 1879 Node* new_if_f = (Node*)(new IfFalseNode( iff )); 1880 Node* new_if_t = (Node*)(new IfTrueNode ( iff )); 1881 1882 igvn->register_new_node_with_optimizer(new_if_f); 1883 igvn->register_new_node_with_optimizer(new_if_t); 1884 // Flip test, so flip trailing control 1885 igvn->replace_node(old_if_f, new_if_t); 1886 igvn->replace_node(old_if_t, new_if_f); 1887 1888 // Progress 1889 return iff; 1890 } 1891 1892 Node* RangeCheckNode::Ideal(PhaseGVN *phase, bool can_reshape) { 1893 Node* res = Ideal_common(phase, can_reshape); 1894 if (res != NodeSentinel) { 1895 return res; 1896 } 1897 1898 PhaseIterGVN *igvn = phase->is_IterGVN(); 1899 // Setup to scan up the CFG looking for a dominating test 1900 Node* prev_dom = this; 1901 1902 // Check for range-check vs other kinds of tests 1903 Node* index1; 1904 Node* range1; 1905 jint offset1; 1906 int flip1 = is_range_check(range1, index1, offset1); 1907 if (flip1) { 1908 Node* dom = in(0); 1909 // Try to remove extra range checks. All 'up_one_dom' gives up at merges 1910 // so all checks we inspect post-dominate the top-most check we find. 1911 // If we are going to fail the current check and we reach the top check 1912 // then we are guaranteed to fail, so just start interpreting there. 1913 // We 'expand' the top 3 range checks to include all post-dominating 1914 // checks. 1915 // 1916 // Example: 1917 // a[i+x] // (1) 1 < x < 6 1918 // a[i+3] // (2) 1919 // a[i+4] // (3) 1920 // a[i+6] // max = max of all constants 1921 // a[i+2] 1922 // a[i+1] // min = min of all constants 1923 // 1924 // If x < 3: 1925 // (1) a[i+x]: Leave unchanged 1926 // (2) a[i+3]: Replace with a[i+max] = a[i+6]: i+x < i+3 <= i+6 -> (2) is covered 1927 // (3) a[i+4]: Replace with a[i+min] = a[i+1]: i+1 < i+4 <= i+6 -> (3) and all following checks are covered 1928 // Remove all other a[i+c] checks 1929 // 1930 // If x >= 3: 1931 // (1) a[i+x]: Leave unchanged 1932 // (2) a[i+3]: Replace with a[i+min] = a[i+1]: i+1 < i+3 <= i+x -> (2) is covered 1933 // (3) a[i+4]: Replace with a[i+max] = a[i+6]: i+1 < i+4 <= i+6 -> (3) and all following checks are covered 1934 // Remove all other a[i+c] checks 1935 // 1936 // We only need the top 2 range checks if x is the min or max of all constants. 1937 // 1938 // This, however, only works if the interval [i+min,i+max] is not larger than max_int (i.e. abs(max - min) < max_int): 1939 // The theoretical max size of an array is max_int with: 1940 // - Valid index space: [0,max_int-1] 1941 // - Invalid index space: [max_int,-1] // max_int, min_int, min_int - 1 ..., -1 1942 // 1943 // The size of the consecutive valid index space is smaller than the size of the consecutive invalid index space. 1944 // If we choose min and max in such a way that: 1945 // - abs(max - min) < max_int 1946 // - i+max and i+min are inside the valid index space 1947 // then all indices [i+min,i+max] must be in the valid index space. Otherwise, the invalid index space must be 1948 // smaller than the valid index space which is never the case for any array size. 1949 // 1950 // Choosing a smaller array size only makes the valid index space smaller and the invalid index space larger and 1951 // the argument above still holds. 1952 // 1953 // Note that the same optimization with the same maximal accepted interval size can also be found in C1. 1954 const jlong maximum_number_of_min_max_interval_indices = (jlong)max_jint; 1955 1956 // The top 3 range checks seen 1957 const int NRC = 3; 1958 RangeCheck prev_checks[NRC]; 1959 int nb_checks = 0; 1960 1961 // Low and high offsets seen so far 1962 jint off_lo = offset1; 1963 jint off_hi = offset1; 1964 1965 bool found_immediate_dominator = false; 1966 1967 // Scan for the top checks and collect range of offsets 1968 for (int dist = 0; dist < 999; dist++) { // Range-Check scan limit 1969 if (dom->Opcode() == Op_RangeCheck && // Not same opcode? 1970 prev_dom->in(0) == dom) { // One path of test does dominate? 1971 if (dom == this) return nullptr; // dead loop 1972 // See if this is a range check 1973 Node* index2; 1974 Node* range2; 1975 jint offset2; 1976 int flip2 = dom->as_RangeCheck()->is_range_check(range2, index2, offset2); 1977 // See if this is a _matching_ range check, checking against 1978 // the same array bounds. 1979 if (flip2 == flip1 && range2 == range1 && index2 == index1 && 1980 dom->outcnt() == 2) { 1981 if (nb_checks == 0 && dom->in(1) == in(1)) { 1982 // Found an immediately dominating test at the same offset. 1983 // This kind of back-to-back test can be eliminated locally, 1984 // and there is no need to search further for dominating tests. 1985 assert(offset2 == offset1, "Same test but different offsets"); 1986 found_immediate_dominator = true; 1987 break; 1988 } 1989 1990 // "x - y" -> must add one to the difference for number of elements in [x,y] 1991 const jlong diff = (jlong)MIN2(offset2, off_lo) - (jlong)MAX2(offset2, off_hi); 1992 if (ABS(diff) < maximum_number_of_min_max_interval_indices) { 1993 // Gather expanded bounds 1994 off_lo = MIN2(off_lo, offset2); 1995 off_hi = MAX2(off_hi, offset2); 1996 // Record top NRC range checks 1997 prev_checks[nb_checks % NRC].ctl = prev_dom->as_IfProj(); 1998 prev_checks[nb_checks % NRC].off = offset2; 1999 nb_checks++; 2000 } 2001 } 2002 } 2003 prev_dom = dom; 2004 dom = up_one_dom(dom); 2005 if (!dom) break; 2006 } 2007 2008 if (!found_immediate_dominator) { 2009 // Attempt to widen the dominating range check to cover some later 2010 // ones. Since range checks "fail" by uncommon-trapping to the 2011 // interpreter, widening a check can make us speculatively enter 2012 // the interpreter. If we see range-check deopt's, do not widen! 2013 if (!phase->C->allow_range_check_smearing()) return nullptr; 2014 2015 if (can_reshape && !phase->C->post_loop_opts_phase()) { 2016 // We are about to perform range check smearing (i.e. remove this RangeCheck if it is dominated by 2017 // a series of RangeChecks which have a range that covers this RangeCheck). This can cause array access nodes to 2018 // be pinned. We want to avoid that and first allow range check elimination a chance to remove the RangeChecks 2019 // from loops. Hence, we delay range check smearing until after loop opts. 2020 phase->C->record_for_post_loop_opts_igvn(this); 2021 return nullptr; 2022 } 2023 2024 // Didn't find prior covering check, so cannot remove anything. 2025 if (nb_checks == 0) { 2026 return nullptr; 2027 } 2028 // Constant indices only need to check the upper bound. 2029 // Non-constant indices must check both low and high. 2030 int chk0 = (nb_checks - 1) % NRC; 2031 if (index1) { 2032 if (nb_checks == 1) { 2033 return nullptr; 2034 } else { 2035 // If the top range check's constant is the min or max of 2036 // all constants we widen the next one to cover the whole 2037 // range of constants. 2038 RangeCheck rc0 = prev_checks[chk0]; 2039 int chk1 = (nb_checks - 2) % NRC; 2040 RangeCheck rc1 = prev_checks[chk1]; 2041 if (rc0.off == off_lo) { 2042 adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn); 2043 prev_dom = rc1.ctl; 2044 } else if (rc0.off == off_hi) { 2045 adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn); 2046 prev_dom = rc1.ctl; 2047 } else { 2048 // If the top test's constant is not the min or max of all 2049 // constants, we need 3 range checks. We must leave the 2050 // top test unchanged because widening it would allow the 2051 // accesses it protects to successfully read/write out of 2052 // bounds. 2053 if (nb_checks == 2) { 2054 return nullptr; 2055 } 2056 int chk2 = (nb_checks - 3) % NRC; 2057 RangeCheck rc2 = prev_checks[chk2]; 2058 // The top range check a+i covers interval: -a <= i < length-a 2059 // The second range check b+i covers interval: -b <= i < length-b 2060 if (rc1.off <= rc0.off) { 2061 // if b <= a, we change the second range check to: 2062 // -min_of_all_constants <= i < length-min_of_all_constants 2063 // Together top and second range checks now cover: 2064 // -min_of_all_constants <= i < length-a 2065 // which is more restrictive than -b <= i < length-b: 2066 // -b <= -min_of_all_constants <= i < length-a <= length-b 2067 // The third check is then changed to: 2068 // -max_of_all_constants <= i < length-max_of_all_constants 2069 // so 2nd and 3rd checks restrict allowed values of i to: 2070 // -min_of_all_constants <= i < length-max_of_all_constants 2071 adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn); 2072 adjust_check(rc2.ctl, range1, index1, flip1, off_hi, igvn); 2073 } else { 2074 // if b > a, we change the second range check to: 2075 // -max_of_all_constants <= i < length-max_of_all_constants 2076 // Together top and second range checks now cover: 2077 // -a <= i < length-max_of_all_constants 2078 // which is more restrictive than -b <= i < length-b: 2079 // -b < -a <= i < length-max_of_all_constants <= length-b 2080 // The third check is then changed to: 2081 // -max_of_all_constants <= i < length-max_of_all_constants 2082 // so 2nd and 3rd checks restrict allowed values of i to: 2083 // -min_of_all_constants <= i < length-max_of_all_constants 2084 adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn); 2085 adjust_check(rc2.ctl, range1, index1, flip1, off_lo, igvn); 2086 } 2087 prev_dom = rc2.ctl; 2088 } 2089 } 2090 } else { 2091 RangeCheck rc0 = prev_checks[chk0]; 2092 // 'Widen' the offset of the 1st and only covering check 2093 adjust_check(rc0.ctl, range1, index1, flip1, off_hi, igvn); 2094 // Test is now covered by prior checks, dominate it out 2095 prev_dom = rc0.ctl; 2096 } 2097 // The last RangeCheck is found to be redundant with a sequence of n (n >= 2) preceding RangeChecks. 2098 // If an array load is control dependent on the eliminated range check, the array load nodes (CastII and Load) 2099 // become control dependent on the last range check of the sequence, but they are really dependent on the entire 2100 // sequence of RangeChecks. If RangeCheck#n is later replaced by a dominating identical check, the array load 2101 // nodes must not float above the n-1 other RangeCheck in the sequence. We pin the array load nodes here to 2102 // guarantee it doesn't happen. 2103 // 2104 // RangeCheck#1 RangeCheck#1 2105 // | \ | \ 2106 // | uncommon trap | uncommon trap 2107 // .. .. 2108 // RangeCheck#n -> RangeCheck#n 2109 // | \ | \ 2110 // | uncommon trap CastII uncommon trap 2111 // RangeCheck Load 2112 // | \ 2113 // CastII uncommon trap 2114 // Load 2115 2116 return dominated_by(prev_dom, igvn, true); 2117 } 2118 } else { 2119 prev_dom = search_identical(4, igvn); 2120 2121 if (prev_dom == nullptr) { 2122 return nullptr; 2123 } 2124 } 2125 2126 // Replace dominated IfNode 2127 return dominated_by(prev_dom, igvn, false); 2128 } 2129 2130 ParsePredicateNode::ParsePredicateNode(Node* control, Deoptimization::DeoptReason deopt_reason, PhaseGVN* gvn) 2131 : IfNode(control, gvn->intcon(1), PROB_MAX, COUNT_UNKNOWN), 2132 _deopt_reason(deopt_reason), 2133 _useless(false) { 2134 init_class_id(Class_ParsePredicate); 2135 gvn->C->add_parse_predicate(this); 2136 gvn->C->record_for_post_loop_opts_igvn(this); 2137 #ifdef ASSERT 2138 switch (deopt_reason) { 2139 case Deoptimization::Reason_predicate: 2140 case Deoptimization::Reason_profile_predicate: 2141 case Deoptimization::Reason_loop_limit_check: 2142 break; 2143 default: 2144 assert(false, "unsupported deoptimization reason for Parse Predicate"); 2145 } 2146 #endif // ASSERT 2147 } 2148 2149 Node* ParsePredicateNode::uncommon_trap() const { 2150 ParsePredicateUncommonProj* uncommon_proj = proj_out(0)->as_IfFalse(); 2151 Node* uct_region_or_call = uncommon_proj->unique_ctrl_out(); 2152 assert(uct_region_or_call->is_Region() || uct_region_or_call->is_Call(), "must be a region or call uct"); 2153 return uct_region_or_call; 2154 } 2155 2156 // Fold this node away once it becomes useless or at latest in post loop opts IGVN. 2157 const Type* ParsePredicateNode::Value(PhaseGVN* phase) const { 2158 if (phase->type(in(0)) == Type::TOP) { 2159 return Type::TOP; 2160 } 2161 if (_useless || phase->C->post_loop_opts_phase()) { 2162 return TypeTuple::IFTRUE; 2163 } else { 2164 return bottom_type(); 2165 } 2166 } 2167 2168 #ifndef PRODUCT 2169 void ParsePredicateNode::dump_spec(outputStream* st) const { 2170 st->print(" #"); 2171 switch (_deopt_reason) { 2172 case Deoptimization::DeoptReason::Reason_predicate: 2173 st->print("Loop "); 2174 break; 2175 case Deoptimization::DeoptReason::Reason_profile_predicate: 2176 st->print("Profiled_Loop "); 2177 break; 2178 case Deoptimization::DeoptReason::Reason_loop_limit_check: 2179 st->print("Loop_Limit_Check "); 2180 break; 2181 default: 2182 fatal("unknown kind"); 2183 } 2184 } 2185 2186 #endif // NOT PRODUCT