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 // if this IfNode follows a range check pattern return the projection
 458 // for the failed path
 459 ProjNode* IfNode::range_check_trap_proj(int& flip_test, Node*& l, Node*& r) {
 460   if (outcnt() != 2) {
 461     return nullptr;
 462   }
 463   Node* b = in(1);
 464   if (b == nullptr || !b->is_Bool())  return nullptr;
 465   BoolNode* bn = b->as_Bool();
 466   Node* cmp = bn->in(1);
 467   if (cmp == nullptr)  return nullptr;
 468   if (cmp->Opcode() != Op_CmpU)  return nullptr;
 469 
 470   l = cmp->in(1);
 471   r = cmp->in(2);
 472   flip_test = 1;
 473   if (bn->_test._test == BoolTest::le) {
 474     l = cmp->in(2);
 475     r = cmp->in(1);
 476     flip_test = 2;
 477   } else if (bn->_test._test != BoolTest::lt) {
 478     return nullptr;
 479   }
 480   if (l->is_top())  return nullptr;   // Top input means dead test
 481   if (r->Opcode() != Op_LoadRange && !is_RangeCheck())  return nullptr;
 482 
 483   // We have recognized one of these forms:
 484   //  Flip 1:  If (Bool[<] CmpU(l, LoadRange)) ...
 485   //  Flip 2:  If (Bool[<=] CmpU(LoadRange, l)) ...
 486 
 487   ProjNode* iftrap = proj_out_or_null(flip_test == 2 ? true : false);
 488   return iftrap;
 489 }
 490 
 491 
 492 //------------------------------is_range_check---------------------------------
 493 // Return 0 if not a range check.  Return 1 if a range check and set index and
 494 // offset.  Return 2 if we had to negate the test.  Index is null if the check
 495 // is versus a constant.
 496 int RangeCheckNode::is_range_check(Node* &range, Node* &index, jint &offset) {
 497   int flip_test = 0;
 498   Node* l = nullptr;
 499   Node* r = nullptr;
 500   ProjNode* iftrap = range_check_trap_proj(flip_test, l, r);
 501 
 502   if (iftrap == nullptr) {
 503     return 0;
 504   }
 505 
 506   // Make sure it's a real range check by requiring an uncommon trap
 507   // along the OOB path.  Otherwise, it's possible that the user wrote
 508   // something which optimized to look like a range check but behaves
 509   // in some other way.
 510   if (iftrap->is_uncommon_trap_proj(Deoptimization::Reason_range_check) == nullptr) {
 511     return 0;
 512   }
 513 
 514   // Look for index+offset form
 515   Node* ind = l;
 516   jint  off = 0;
 517   if (l->is_top()) {
 518     return 0;
 519   } else if (l->Opcode() == Op_AddI) {
 520     if ((off = l->in(1)->find_int_con(0)) != 0) {
 521       ind = l->in(2)->uncast();
 522     } else if ((off = l->in(2)->find_int_con(0)) != 0) {
 523       ind = l->in(1)->uncast();
 524     }
 525   } else if ((off = l->find_int_con(-1)) >= 0) {
 526     // constant offset with no variable index
 527     ind = nullptr;
 528   } else {
 529     // variable index with no constant offset (or dead negative index)
 530     off = 0;
 531   }
 532 
 533   // Return all the values:
 534   index  = ind;
 535   offset = off;
 536   range  = r;
 537   return flip_test;
 538 }
 539 
 540 //------------------------------adjust_check-----------------------------------
 541 // Adjust (widen) a prior range check
 542 static void adjust_check(IfProjNode* proj, Node* range, Node* index,
 543                          int flip, jint off_lo, PhaseIterGVN* igvn) {
 544   PhaseGVN *gvn = igvn;
 545   // Break apart the old check
 546   Node *iff = proj->in(0);
 547   Node *bol = iff->in(1);
 548   if( bol->is_top() ) return;   // In case a partially dead range check appears
 549   // bail (or bomb[ASSERT/DEBUG]) if NOT projection-->IfNode-->BoolNode
 550   DEBUG_ONLY( if (!bol->is_Bool()) { proj->dump(3); fatal("Expect projection-->IfNode-->BoolNode"); } )
 551   if (!bol->is_Bool()) return;
 552 
 553   Node *cmp = bol->in(1);
 554   // Compute a new check
 555   Node *new_add = gvn->intcon(off_lo);
 556   if (index) {
 557     new_add = off_lo ? gvn->transform(new AddINode(index, new_add)) : index;
 558   }
 559   Node *new_cmp = (flip == 1)
 560     ? new CmpUNode(new_add, range)
 561     : new CmpUNode(range, new_add);
 562   new_cmp = gvn->transform(new_cmp);
 563   // See if no need to adjust the existing check
 564   if (new_cmp == cmp) return;
 565   // Else, adjust existing check
 566   Node* new_bol = gvn->transform(new BoolNode(new_cmp, bol->as_Bool()->_test._test));
 567   igvn->rehash_node_delayed(iff);
 568   iff->set_req_X(1, new_bol, igvn);
 569   // As part of range check smearing, this range check is widened. Loads and range check Cast nodes that are control
 570   // dependent on this range check now depend on multiple dominating range checks. These control dependent nodes end up
 571   // at the lowest/nearest dominating check in the graph. To ensure that these Loads/Casts do not float above any of the
 572   // dominating checks (even when the lowest dominating check is later replaced by yet another dominating check), we
 573   // need to pin them at the lowest dominating check.
 574   proj->pin_array_access_nodes(igvn);
 575 }
 576 
 577 //------------------------------up_one_dom-------------------------------------
 578 // Walk up the dominator tree one step.  Return null at root or true
 579 // complex merges.  Skips through small diamonds.
 580 Node* IfNode::up_one_dom(Node *curr, bool linear_only) {
 581   Node *dom = curr->in(0);
 582   if( !dom )                    // Found a Region degraded to a copy?
 583     return curr->nonnull_req(); // Skip thru it
 584 
 585   if( curr != dom )             // Normal walk up one step?
 586     return dom;
 587 
 588   // Use linear_only if we are still parsing, since we cannot
 589   // trust the regions to be fully filled in.
 590   if (linear_only)
 591     return nullptr;
 592 
 593   if( dom->is_Root() )
 594     return nullptr;
 595 
 596   // Else hit a Region.  Check for a loop header
 597   if( dom->is_Loop() )
 598     return dom->in(1);          // Skip up thru loops
 599 
 600   // Check for small diamonds
 601   Node *din1, *din2, *din3, *din4;
 602   if( dom->req() == 3 &&        // 2-path merge point
 603       (din1 = dom ->in(1)) &&   // Left  path exists
 604       (din2 = dom ->in(2)) &&   // Right path exists
 605       (din3 = din1->in(0)) &&   // Left  path up one
 606       (din4 = din2->in(0)) ) {  // Right path up one
 607     if( din3->is_Call() &&      // Handle a slow-path call on either arm
 608         (din3 = din3->in(0)) )
 609       din3 = din3->in(0);
 610     if( din4->is_Call() &&      // Handle a slow-path call on either arm
 611         (din4 = din4->in(0)) )
 612       din4 = din4->in(0);
 613     if (din3 != nullptr && din3 == din4 && din3->is_If()) // Regions not degraded to a copy
 614       return din3;              // Skip around diamonds
 615   }
 616 
 617   // Give up the search at true merges
 618   return nullptr;                  // Dead loop?  Or hit root?
 619 }
 620 
 621 
 622 //------------------------------filtered_int_type--------------------------------
 623 // Return a possibly more restrictive type for val based on condition control flow for an if
 624 const TypeInt* IfNode::filtered_int_type(PhaseGVN* gvn, Node* val, Node* if_proj) {
 625   assert(if_proj &&
 626          (if_proj->Opcode() == Op_IfTrue || if_proj->Opcode() == Op_IfFalse), "expecting an if projection");
 627   if (if_proj->in(0) && if_proj->in(0)->is_If()) {
 628     IfNode* iff = if_proj->in(0)->as_If();
 629     if (iff->in(1) && iff->in(1)->is_Bool()) {
 630       BoolNode* bol = iff->in(1)->as_Bool();
 631       if (bol->in(1) && bol->in(1)->is_Cmp()) {
 632         const CmpNode* cmp  = bol->in(1)->as_Cmp();
 633         if (cmp->in(1) == val) {
 634           const TypeInt* cmp2_t = gvn->type(cmp->in(2))->isa_int();
 635           if (cmp2_t != nullptr) {
 636             jint lo = cmp2_t->_lo;
 637             jint hi = cmp2_t->_hi;
 638             BoolTest::mask msk = if_proj->Opcode() == Op_IfTrue ? bol->_test._test : bol->_test.negate();
 639             switch (msk) {
 640             case BoolTest::ne: {
 641               // If val is compared to its lower or upper bound, we can narrow the type
 642               const TypeInt* val_t = gvn->type(val)->isa_int();
 643               if (val_t != nullptr && !val_t->singleton() && cmp2_t->is_con()) {
 644                 if (val_t->_lo == lo) {
 645                   return TypeInt::make(val_t->_lo + 1, val_t->_hi, val_t->_widen);
 646                 } else if (val_t->_hi == hi) {
 647                   return TypeInt::make(val_t->_lo, val_t->_hi - 1, val_t->_widen);
 648                 }
 649               }
 650               // Can't refine type
 651               return nullptr;
 652             }
 653             case BoolTest::eq:
 654               return cmp2_t;
 655             case BoolTest::lt:
 656               lo = TypeInt::INT->_lo;
 657               if (hi != min_jint) {
 658                 hi = hi - 1;
 659               }
 660               break;
 661             case BoolTest::le:
 662               lo = TypeInt::INT->_lo;
 663               break;
 664             case BoolTest::gt:
 665               if (lo != max_jint) {
 666                 lo = lo + 1;
 667               }
 668               hi = TypeInt::INT->_hi;
 669               break;
 670             case BoolTest::ge:
 671               // lo unchanged
 672               hi = TypeInt::INT->_hi;
 673               break;
 674             default:
 675               break;
 676             }
 677             const TypeInt* rtn_t = TypeInt::make(lo, hi, cmp2_t->_widen);
 678             return rtn_t;
 679           }
 680         }
 681       }
 682     }
 683   }
 684   return nullptr;
 685 }
 686 
 687 //------------------------------fold_compares----------------------------
 688 // See if a pair of CmpIs can be converted into a CmpU.  In some cases
 689 // the direction of this if is determined by the preceding if so it
 690 // can be eliminate entirely.
 691 //
 692 // Given an if testing (CmpI n v) check for an immediately control
 693 // dependent if that is testing (CmpI n v2) and has one projection
 694 // leading to this if and the other projection leading to a region
 695 // that merges one of this ifs control projections.
 696 //
 697 //                   If
 698 //                  / |
 699 //                 /  |
 700 //                /   |
 701 //              If    |
 702 //              /\    |
 703 //             /  \   |
 704 //            /    \  |
 705 //           /    Region
 706 //
 707 // Or given an if testing (CmpI n v) check for a dominating if that is
 708 // testing (CmpI n v2), both having one projection leading to an
 709 // uncommon trap. Allow Another independent guard in between to cover
 710 // an explicit range check:
 711 // if (index < 0 || index >= array.length) {
 712 // which may need a null check to guard the LoadRange
 713 //
 714 //                   If
 715 //                  / \
 716 //                 /   \
 717 //                /     \
 718 //              If      unc
 719 //              /\
 720 //             /  \
 721 //            /    \
 722 //           /      unc
 723 //
 724 
 725 // Is the comparison for this If suitable for folding?
 726 bool IfNode::cmpi_folds(PhaseIterGVN* igvn, bool fold_ne) {
 727   return in(1) != nullptr &&
 728     in(1)->is_Bool() &&
 729     in(1)->in(1) != nullptr &&
 730     in(1)->in(1)->Opcode() == Op_CmpI &&
 731     in(1)->in(1)->in(2) != nullptr &&
 732     in(1)->in(1)->in(2) != igvn->C->top() &&
 733     (in(1)->as_Bool()->_test.is_less() ||
 734      in(1)->as_Bool()->_test.is_greater() ||
 735      (fold_ne && in(1)->as_Bool()->_test._test == BoolTest::ne));
 736 }
 737 
 738 // Is a dominating control suitable for folding with this if?
 739 bool IfNode::is_ctrl_folds(Node* ctrl, PhaseIterGVN* igvn) {
 740   return ctrl != nullptr &&
 741     ctrl->is_Proj() &&
 742     ctrl->in(0) != nullptr &&
 743     ctrl->in(0)->Opcode() == Op_If &&
 744     ctrl->in(0)->outcnt() == 2 &&
 745     ctrl->in(0)->as_If()->cmpi_folds(igvn, true) &&
 746     // Must compare same value
 747     ctrl->in(0)->in(1)->in(1)->in(1) != nullptr &&
 748     ctrl->in(0)->in(1)->in(1)->in(1) != igvn->C->top() &&
 749     ctrl->in(0)->in(1)->in(1)->in(1) == in(1)->in(1)->in(1);
 750 }
 751 
 752 // Do this If and the dominating If share a region?
 753 bool IfNode::has_shared_region(ProjNode* proj, ProjNode*& success, ProjNode*& fail) {
 754   ProjNode* otherproj = proj->other_if_proj();
 755   Node* otherproj_ctrl_use = otherproj->unique_ctrl_out_or_null();
 756   RegionNode* region = (otherproj_ctrl_use != nullptr && otherproj_ctrl_use->is_Region()) ? otherproj_ctrl_use->as_Region() : nullptr;
 757   success = nullptr;
 758   fail = nullptr;
 759 
 760   if (otherproj->outcnt() == 1 && region != nullptr && !region->has_phi()) {
 761     for (int i = 0; i < 2; i++) {
 762       ProjNode* proj = proj_out(i);
 763       if (success == nullptr && proj->outcnt() == 1 && proj->unique_out() == region) {
 764         success = proj;
 765       } else if (fail == nullptr) {
 766         fail = proj;
 767       } else {
 768         success = fail = nullptr;
 769       }
 770     }
 771   }
 772   return success != nullptr && fail != nullptr;
 773 }
 774 
 775 bool IfNode::is_dominator_unc(CallStaticJavaNode* dom_unc, CallStaticJavaNode* unc) {
 776   // Different methods and methods containing jsrs are not supported.
 777   ciMethod* method = unc->jvms()->method();
 778   ciMethod* dom_method = dom_unc->jvms()->method();
 779   if (method != dom_method || method->has_jsrs()) {
 780     return false;
 781   }
 782   // Check that both traps are in the same activation of the method (instead
 783   // of two activations being inlined through different call sites) by verifying
 784   // that the call stacks are equal for both JVMStates.
 785   JVMState* dom_caller = dom_unc->jvms()->caller();
 786   JVMState* caller = unc->jvms()->caller();
 787   if ((dom_caller == nullptr) != (caller == nullptr)) {
 788     // The current method must either be inlined into both dom_caller and
 789     // caller or must not be inlined at all (top method). Bail out otherwise.
 790     return false;
 791   } else if (dom_caller != nullptr && !dom_caller->same_calls_as(caller)) {
 792     return false;
 793   }
 794   // Check that the bci of the dominating uncommon trap dominates the bci
 795   // of the dominated uncommon trap. Otherwise we may not re-execute
 796   // the dominated check after deoptimization from the merged uncommon trap.
 797   ciTypeFlow* flow = dom_method->get_flow_analysis();
 798   int bci = unc->jvms()->bci();
 799   int dom_bci = dom_unc->jvms()->bci();
 800   if (!flow->is_dominated_by(bci, dom_bci)) {
 801     return false;
 802   }
 803 
 804   return true;
 805 }
 806 
 807 // Return projection that leads to an uncommon trap if any
 808 ProjNode* IfNode::uncommon_trap_proj(CallStaticJavaNode*& call) const {
 809   for (int i = 0; i < 2; i++) {
 810     call = proj_out(i)->is_uncommon_trap_proj();
 811     if (call != nullptr) {
 812       return proj_out(i);
 813     }
 814   }
 815   return nullptr;
 816 }
 817 
 818 // Do this If and the dominating If both branch out to an uncommon trap
 819 bool IfNode::has_only_uncommon_traps(ProjNode* proj, ProjNode*& success, ProjNode*& fail, PhaseIterGVN* igvn) {
 820   ProjNode* otherproj = proj->other_if_proj();
 821   CallStaticJavaNode* dom_unc = otherproj->is_uncommon_trap_proj();
 822 
 823   if (otherproj->outcnt() == 1 && dom_unc != nullptr) {
 824     // We need to re-execute the folded Ifs after deoptimization from the merged traps
 825     if (!dom_unc->jvms()->should_reexecute()) {
 826       return false;
 827     }
 828 
 829     CallStaticJavaNode* unc = nullptr;
 830     ProjNode* unc_proj = uncommon_trap_proj(unc);
 831     if (unc_proj != nullptr && unc_proj->outcnt() == 1) {
 832       if (dom_unc == unc) {
 833         // Allow the uncommon trap to be shared through a region
 834         RegionNode* r = unc->in(0)->as_Region();
 835         if (r->outcnt() != 2 || r->req() != 3 || r->find_edge(otherproj) == -1 || r->find_edge(unc_proj) == -1) {
 836           return false;
 837         }
 838         assert(r->has_phi() == nullptr, "simple region shouldn't have a phi");
 839       } else if (dom_unc->in(0) != otherproj || unc->in(0) != unc_proj) {
 840         return false;
 841       }
 842 
 843       if (!is_dominator_unc(dom_unc, unc)) {
 844         return false;
 845       }
 846 
 847       // See merge_uncommon_traps: the reason of the uncommon trap
 848       // will be changed and the state of the dominating If will be
 849       // used. Checked that we didn't apply this transformation in a
 850       // previous compilation and it didn't cause too many traps
 851       ciMethod* dom_method = dom_unc->jvms()->method();
 852       int dom_bci = dom_unc->jvms()->bci();
 853       if (!igvn->C->too_many_traps(dom_method, dom_bci, Deoptimization::Reason_unstable_fused_if) &&
 854           !igvn->C->too_many_traps(dom_method, dom_bci, Deoptimization::Reason_range_check) &&
 855           // Return true if c2 manages to reconcile with UnstableIf optimization. See the comments for it.
 856           igvn->C->remove_unstable_if_trap(dom_unc, true/*yield*/)) {
 857         success = unc_proj;
 858         fail = unc_proj->other_if_proj();
 859         return true;
 860       }
 861     }
 862   }
 863   return false;
 864 }
 865 
 866 // Check that the 2 CmpI can be folded into as single CmpU and proceed with the folding
 867 bool IfNode::fold_compares_helper(ProjNode* proj, ProjNode* success, ProjNode* fail, PhaseIterGVN* igvn) {
 868   Node* this_cmp = in(1)->in(1);
 869   BoolNode* this_bool = in(1)->as_Bool();
 870   IfNode* dom_iff = proj->in(0)->as_If();
 871   BoolNode* dom_bool = dom_iff->in(1)->as_Bool();
 872   Node* lo = dom_iff->in(1)->in(1)->in(2);
 873   Node* hi = this_cmp->in(2);
 874   Node* n = this_cmp->in(1);
 875   ProjNode* otherproj = proj->other_if_proj();
 876 
 877   const TypeInt* lo_type = IfNode::filtered_int_type(igvn, n, otherproj);
 878   const TypeInt* hi_type = IfNode::filtered_int_type(igvn, n, success);
 879 
 880   BoolTest::mask lo_test = dom_bool->_test._test;
 881   BoolTest::mask hi_test = this_bool->_test._test;
 882   BoolTest::mask cond = hi_test;
 883 
 884   // convert:
 885   //
 886   //          dom_bool = x {<,<=,>,>=} a
 887   //                           / \
 888   //     proj = {True,False}  /   \ otherproj = {False,True}
 889   //                         /
 890   //        this_bool = x {<,<=} b
 891   //                       / \
 892   //  fail = {True,False} /   \ success = {False,True}
 893   //                     /
 894   //
 895   // (Second test guaranteed canonicalized, first one may not have
 896   // been canonicalized yet)
 897   //
 898   // into:
 899   //
 900   // cond = (x - lo) {<u,<=u,>u,>=u} adjusted_lim
 901   //                       / \
 902   //                 fail /   \ success
 903   //                     /
 904   //
 905 
 906   // Figure out which of the two tests sets the upper bound and which
 907   // sets the lower bound if any.
 908   Node* adjusted_lim = nullptr;
 909   if (lo_type != nullptr && hi_type != nullptr && hi_type->_lo > lo_type->_hi &&
 910       hi_type->_hi == max_jint && lo_type->_lo == min_jint && lo_test != BoolTest::ne) {
 911     assert((dom_bool->_test.is_less() && !proj->_con) ||
 912            (dom_bool->_test.is_greater() && proj->_con), "incorrect test");
 913 
 914     // this_bool = <
 915     //   dom_bool = >= (proj = True) or dom_bool = < (proj = False)
 916     //     x in [a, b[ on the fail (= True) projection, b > a-1 (because of hi_type->_lo > lo_type->_hi test above):
 917     //     lo = a, hi = b, adjusted_lim = b-a, cond = <u
 918     //   dom_bool = > (proj = True) or dom_bool = <= (proj = False)
 919     //     x in ]a, b[ on the fail (= True) projection, b > a:
 920     //     lo = a+1, hi = b, adjusted_lim = b-a-1, cond = <u
 921     // this_bool = <=
 922     //   dom_bool = >= (proj = True) or dom_bool = < (proj = False)
 923     //     x in [a, b] on the fail (= True) projection, b+1 > a-1:
 924     //     lo = a, hi = b, adjusted_lim = b-a+1, cond = <u
 925     //     lo = a, hi = b, adjusted_lim = b-a, cond = <=u doesn't work because b = a - 1 is possible, then b-a = -1
 926     //   dom_bool = > (proj = True) or dom_bool = <= (proj = False)
 927     //     x in ]a, b] on the fail (= True) projection b+1 > a:
 928     //     lo = a+1, hi = b, adjusted_lim = b-a, cond = <u
 929     //     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
 930 
 931     if (hi_test == BoolTest::lt) {
 932       if (lo_test == BoolTest::gt || lo_test == BoolTest::le) {
 933         lo = igvn->transform(new AddINode(lo, igvn->intcon(1)));
 934       }
 935     } else if (hi_test == BoolTest::le) {
 936       if (lo_test == BoolTest::ge || lo_test == BoolTest::lt) {
 937         adjusted_lim = igvn->transform(new SubINode(hi, lo));
 938         adjusted_lim = igvn->transform(new AddINode(adjusted_lim, igvn->intcon(1)));
 939         cond = BoolTest::lt;
 940       } else if (lo_test == BoolTest::gt || lo_test == BoolTest::le) {
 941         adjusted_lim = igvn->transform(new SubINode(hi, lo));
 942         lo = igvn->transform(new AddINode(lo, igvn->intcon(1)));
 943         cond = BoolTest::lt;
 944       } else {
 945         assert(false, "unhandled lo_test: %d", lo_test);
 946         return false;
 947       }
 948     } else {
 949       assert(igvn->_worklist.member(in(1)) && in(1)->Value(igvn) != igvn->type(in(1)), "unhandled hi_test: %d", hi_test);
 950       return false;
 951     }
 952     // this test was canonicalized
 953     assert(this_bool->_test.is_less() && fail->_con, "incorrect test");
 954   } else if (lo_type != nullptr && hi_type != nullptr && lo_type->_lo > hi_type->_hi &&
 955              lo_type->_hi == max_jint && hi_type->_lo == min_jint && lo_test != BoolTest::ne) {
 956 
 957     // this_bool = <
 958     //   dom_bool = < (proj = True) or dom_bool = >= (proj = False)
 959     //     x in [b, a[ on the fail (= False) projection, a > b-1 (because of lo_type->_lo > hi_type->_hi above):
 960     //     lo = b, hi = a, adjusted_lim = a-b, cond = >=u
 961     //   dom_bool = <= (proj = True) or dom_bool = > (proj = False)
 962     //     x in [b, a] on the fail (= False) projection, a+1 > b-1:
 963     //     lo = b, hi = a, adjusted_lim = a-b+1, cond = >=u
 964     //     lo = b, hi = a, adjusted_lim = a-b, cond = >u doesn't work because a = b - 1 is possible, then b-a = -1
 965     // this_bool = <=
 966     //   dom_bool = < (proj = True) or dom_bool = >= (proj = False)
 967     //     x in ]b, a[ on the fail (= False) projection, a > b:
 968     //     lo = b+1, hi = a, adjusted_lim = a-b-1, cond = >=u
 969     //   dom_bool = <= (proj = True) or dom_bool = > (proj = False)
 970     //     x in ]b, a] on the fail (= False) projection, a+1 > b:
 971     //     lo = b+1, hi = a, adjusted_lim = a-b, cond = >=u
 972     //     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
 973 
 974     swap(lo, hi);
 975     swap(lo_type, hi_type);
 976     swap(lo_test, hi_test);
 977 
 978     assert((dom_bool->_test.is_less() && proj->_con) ||
 979            (dom_bool->_test.is_greater() && !proj->_con), "incorrect test");
 980 
 981     cond = (hi_test == BoolTest::le || hi_test == BoolTest::gt) ? BoolTest::gt : BoolTest::ge;
 982 
 983     if (lo_test == BoolTest::lt) {
 984       if (hi_test == BoolTest::lt || hi_test == BoolTest::ge) {
 985         cond = BoolTest::ge;
 986       } else if (hi_test == BoolTest::le || hi_test == BoolTest::gt) {
 987         adjusted_lim = igvn->transform(new SubINode(hi, lo));
 988         adjusted_lim = igvn->transform(new AddINode(adjusted_lim, igvn->intcon(1)));
 989         cond = BoolTest::ge;
 990       } else {
 991         assert(false, "unhandled hi_test: %d", hi_test);
 992         return false;
 993       }
 994     } else if (lo_test == BoolTest::le) {
 995       if (hi_test == BoolTest::lt || hi_test == BoolTest::ge) {
 996         lo = igvn->transform(new AddINode(lo, igvn->intcon(1)));
 997         cond = BoolTest::ge;
 998       } else if (hi_test == BoolTest::le || hi_test == BoolTest::gt) {
 999         adjusted_lim = igvn->transform(new SubINode(hi, lo));
1000         lo = igvn->transform(new AddINode(lo, igvn->intcon(1)));
1001         cond = BoolTest::ge;
1002       } else {
1003         assert(false, "unhandled hi_test: %d", hi_test);
1004         return false;
1005       }
1006     } else {
1007       assert(igvn->_worklist.member(in(1)) && in(1)->Value(igvn) != igvn->type(in(1)), "unhandled lo_test: %d", lo_test);
1008       return false;
1009     }
1010     // this test was canonicalized
1011     assert(this_bool->_test.is_less() && !fail->_con, "incorrect test");
1012   } else {
1013     const TypeInt* failtype = filtered_int_type(igvn, n, proj);
1014     if (failtype != nullptr) {
1015       const TypeInt* type2 = filtered_int_type(igvn, n, fail);
1016       if (type2 != nullptr) {
1017         failtype = failtype->join(type2)->is_int();
1018         if (failtype->_lo > failtype->_hi) {
1019           // previous if determines the result of this if so
1020           // replace Bool with constant
1021           igvn->replace_input_of(this, 1, igvn->intcon(success->_con));
1022           return true;
1023         }
1024       }
1025     }
1026     lo = nullptr;
1027     hi = nullptr;
1028   }
1029 
1030   if (lo && hi) {
1031     Node* hook = new Node(1);
1032     hook->init_req(0, lo); // Add a use to lo to prevent him from dying
1033     // Merge the two compares into a single unsigned compare by building (CmpU (n - lo) (hi - lo))
1034     Node* adjusted_val = igvn->transform(new SubINode(n,  lo));
1035     if (adjusted_lim == nullptr) {
1036       adjusted_lim = igvn->transform(new SubINode(hi, lo));
1037     }
1038     hook->destruct(igvn);
1039 
1040     int lo = igvn->type(adjusted_lim)->is_int()->_lo;
1041     if (lo < 0) {
1042       // If range check elimination applies to this comparison, it includes code to protect from overflows that may
1043       // cause the main loop to be skipped entirely. Delay this transformation.
1044       // Example:
1045       // for (int i = 0; i < limit; i++) {
1046       //   if (i < max_jint && i > min_jint) {...
1047       // }
1048       // Comparisons folded as:
1049       // i - min_jint - 1 <u -2
1050       // when RC applies, main loop limit becomes:
1051       // min(limit, max(-2 + min_jint + 1, min_jint))
1052       // = min(limit, min_jint)
1053       // = min_jint
1054       if (!igvn->C->post_loop_opts_phase()) {
1055         if (adjusted_val->outcnt() == 0) {
1056           igvn->remove_dead_node(adjusted_val);
1057         }
1058         if (adjusted_lim->outcnt() == 0) {
1059           igvn->remove_dead_node(adjusted_lim);
1060         }
1061         igvn->C->record_for_post_loop_opts_igvn(this);
1062         return false;
1063       }
1064     }
1065 
1066     Node* newcmp = igvn->transform(new CmpUNode(adjusted_val, adjusted_lim));
1067     Node* newbool = igvn->transform(new BoolNode(newcmp, cond));
1068 
1069     igvn->replace_input_of(dom_iff, 1, igvn->intcon(proj->_con));
1070     igvn->replace_input_of(this, 1, newbool);
1071 
1072     return true;
1073   }
1074   return false;
1075 }
1076 
1077 // Merge the branches that trap for this If and the dominating If into
1078 // a single region that branches to the uncommon trap for the
1079 // dominating If
1080 Node* IfNode::merge_uncommon_traps(ProjNode* proj, ProjNode* success, ProjNode* fail, PhaseIterGVN* igvn) {
1081   Node* res = this;
1082   assert(success->in(0) == this, "bad projection");
1083 
1084   ProjNode* otherproj = proj->other_if_proj();
1085 
1086   CallStaticJavaNode* unc = success->is_uncommon_trap_proj();
1087   CallStaticJavaNode* dom_unc = otherproj->is_uncommon_trap_proj();
1088 
1089   if (unc != dom_unc) {
1090     Node* r = new RegionNode(3);
1091 
1092     r->set_req(1, otherproj);
1093     r->set_req(2, success);
1094     r = igvn->transform(r);
1095     assert(r->is_Region(), "can't go away");
1096 
1097     // Make both If trap at the state of the first If: once the CmpI
1098     // nodes are merged, if we trap we don't know which of the CmpI
1099     // nodes would have caused the trap so we have to restart
1100     // execution at the first one
1101     igvn->replace_input_of(dom_unc, 0, r);
1102     igvn->replace_input_of(unc, 0, igvn->C->top());
1103   }
1104   int trap_request = dom_unc->uncommon_trap_request();
1105   Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(trap_request);
1106   Deoptimization::DeoptAction action = Deoptimization::trap_request_action(trap_request);
1107 
1108   int flip_test = 0;
1109   Node* l = nullptr;
1110   Node* r = nullptr;
1111 
1112   if (success->in(0)->as_If()->range_check_trap_proj(flip_test, l, r) != nullptr) {
1113     // If this looks like a range check, change the trap to
1114     // Reason_range_check so the compiler recognizes it as a range
1115     // check and applies the corresponding optimizations
1116     trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_range_check, action);
1117 
1118     improve_address_types(l, r, fail, igvn);
1119 
1120     res = igvn->transform(new RangeCheckNode(in(0), in(1), _prob, _fcnt));
1121   } else if (unc != dom_unc) {
1122     // If we trap we won't know what CmpI would have caused the trap
1123     // so use a special trap reason to mark this pair of CmpI nodes as
1124     // bad candidate for folding. On recompilation we won't fold them
1125     // and we may trap again but this time we'll know what branch
1126     // traps
1127     trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_unstable_fused_if, action);
1128   }
1129   igvn->replace_input_of(dom_unc, TypeFunc::Parms, igvn->intcon(trap_request));
1130   return res;
1131 }
1132 
1133 // If we are turning 2 CmpI nodes into a CmpU that follows the pattern
1134 // of a rangecheck on index i, on 64 bit the compares may be followed
1135 // by memory accesses using i as index. In that case, the CmpU tells
1136 // us something about the values taken by i that can help the compiler
1137 // (see Compile::conv_I2X_index())
1138 void IfNode::improve_address_types(Node* l, Node* r, ProjNode* fail, PhaseIterGVN* igvn) {
1139 #ifdef _LP64
1140   ResourceMark rm;
1141   Node_Stack stack(2);
1142 
1143   assert(r->Opcode() == Op_LoadRange, "unexpected range check");
1144   const TypeInt* array_size = igvn->type(r)->is_int();
1145 
1146   stack.push(l, 0);
1147 
1148   while(stack.size() > 0) {
1149     Node* n = stack.node();
1150     uint start = stack.index();
1151 
1152     uint i = start;
1153     for (; i < n->outcnt(); i++) {
1154       Node* use = n->raw_out(i);
1155       if (stack.size() == 1) {
1156         if (use->Opcode() == Op_ConvI2L) {
1157           const TypeLong* bounds = use->as_Type()->type()->is_long();
1158           if (bounds->_lo <= array_size->_lo && bounds->_hi >= array_size->_hi &&
1159               (bounds->_lo != array_size->_lo || bounds->_hi != array_size->_hi)) {
1160             stack.set_index(i+1);
1161             stack.push(use, 0);
1162             break;
1163           }
1164         }
1165       } else if (use->is_Mem()) {
1166         Node* ctrl = use->in(0);
1167         for (int i = 0; i < 10 && ctrl != nullptr && ctrl != fail; i++) {
1168           ctrl = up_one_dom(ctrl);
1169         }
1170         if (ctrl == fail) {
1171           Node* init_n = stack.node_at(1);
1172           assert(init_n->Opcode() == Op_ConvI2L, "unexpected first node");
1173           // Create a new narrow ConvI2L node that is dependent on the range check
1174           Node* new_n = igvn->C->conv_I2X_index(igvn, l, array_size, fail);
1175 
1176           // The type of the ConvI2L may be widen and so the new
1177           // ConvI2L may not be better than an existing ConvI2L
1178           if (new_n != init_n) {
1179             for (uint j = 2; j < stack.size(); j++) {
1180               Node* n = stack.node_at(j);
1181               Node* clone = n->clone();
1182               int rep = clone->replace_edge(init_n, new_n, igvn);
1183               assert(rep > 0, "can't find expected node?");
1184               clone = igvn->transform(clone);
1185               init_n = n;
1186               new_n = clone;
1187             }
1188             igvn->hash_delete(use);
1189             int rep = use->replace_edge(init_n, new_n, igvn);
1190             assert(rep > 0, "can't find expected node?");
1191             igvn->transform(use);
1192             if (init_n->outcnt() == 0) {
1193               igvn->_worklist.push(init_n);
1194             }
1195           }
1196         }
1197       } else if (use->in(0) == nullptr && (igvn->type(use)->isa_long() ||
1198                                         igvn->type(use)->isa_ptr())) {
1199         stack.set_index(i+1);
1200         stack.push(use, 0);
1201         break;
1202       }
1203     }
1204     if (i == n->outcnt()) {
1205       stack.pop();
1206     }
1207   }
1208 #endif
1209 }
1210 
1211 bool IfNode::is_cmp_with_loadrange(ProjNode* proj) {
1212   if (in(1) != nullptr &&
1213       in(1)->in(1) != nullptr &&
1214       in(1)->in(1)->in(2) != nullptr) {
1215     Node* other = in(1)->in(1)->in(2);
1216     if (other->Opcode() == Op_LoadRange &&
1217         ((other->in(0) != nullptr && other->in(0) == proj) ||
1218          (other->in(0) == nullptr &&
1219           other->in(2) != nullptr &&
1220           other->in(2)->is_AddP() &&
1221           other->in(2)->in(1) != nullptr &&
1222           other->in(2)->in(1)->Opcode() == Op_CastPP &&
1223           other->in(2)->in(1)->in(0) == proj))) {
1224       return true;
1225     }
1226   }
1227   return false;
1228 }
1229 
1230 bool IfNode::is_null_check(ProjNode* proj, PhaseIterGVN* igvn) {
1231   Node* other = in(1)->in(1)->in(2);
1232   if (other->in(MemNode::Address) != nullptr &&
1233       proj->in(0)->in(1) != nullptr &&
1234       proj->in(0)->in(1)->is_Bool() &&
1235       proj->in(0)->in(1)->in(1) != nullptr &&
1236       proj->in(0)->in(1)->in(1)->Opcode() == Op_CmpP &&
1237       proj->in(0)->in(1)->in(1)->in(2) != nullptr &&
1238       proj->in(0)->in(1)->in(1)->in(1) == other->in(MemNode::Address)->in(AddPNode::Address)->uncast() &&
1239       igvn->type(proj->in(0)->in(1)->in(1)->in(2)) == TypePtr::NULL_PTR) {
1240     return true;
1241   }
1242   return false;
1243 }
1244 
1245 // Check that the If that is in between the 2 integer comparisons has
1246 // no side effect
1247 bool IfNode::is_side_effect_free_test(ProjNode* proj, PhaseIterGVN* igvn) {
1248   if (proj == nullptr) {
1249     return false;
1250   }
1251   CallStaticJavaNode* unc = proj->is_uncommon_trap_if_pattern();
1252   if (unc != nullptr && proj->outcnt() <= 2) {
1253     if (proj->outcnt() == 1 ||
1254         // Allow simple null check from LoadRange
1255         (is_cmp_with_loadrange(proj) && is_null_check(proj, igvn))) {
1256       CallStaticJavaNode* unc = proj->is_uncommon_trap_if_pattern();
1257       CallStaticJavaNode* dom_unc = proj->in(0)->in(0)->as_Proj()->is_uncommon_trap_if_pattern();
1258       assert(dom_unc != nullptr, "is_uncommon_trap_if_pattern returned null");
1259 
1260       // reroute_side_effect_free_unc changes the state of this
1261       // uncommon trap to restart execution at the previous
1262       // CmpI. Check that this change in a previous compilation didn't
1263       // cause too many traps.
1264       int trap_request = unc->uncommon_trap_request();
1265       Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(trap_request);
1266 
1267       if (igvn->C->too_many_traps(dom_unc->jvms()->method(), dom_unc->jvms()->bci(), reason)) {
1268         return false;
1269       }
1270 
1271       if (!is_dominator_unc(dom_unc, unc)) {
1272         return false;
1273       }
1274 
1275       return true;
1276     }
1277   }
1278   return false;
1279 }
1280 
1281 // Make the If between the 2 integer comparisons trap at the state of
1282 // the first If: the last CmpI is the one replaced by a CmpU and the
1283 // first CmpI is eliminated, so the test between the 2 CmpI nodes
1284 // won't be guarded by the first CmpI anymore. It can trap in cases
1285 // where the first CmpI would have prevented it from executing: on a
1286 // trap, we need to restart execution at the state of the first CmpI
1287 void IfNode::reroute_side_effect_free_unc(ProjNode* proj, ProjNode* dom_proj, PhaseIterGVN* igvn) {
1288   CallStaticJavaNode* dom_unc = dom_proj->is_uncommon_trap_if_pattern();
1289   ProjNode* otherproj = proj->other_if_proj();
1290   CallStaticJavaNode* unc = proj->is_uncommon_trap_if_pattern();
1291   Node* call_proj = dom_unc->unique_ctrl_out();
1292   Node* halt = call_proj->unique_ctrl_out();
1293 
1294   Node* new_unc = dom_unc->clone();
1295   call_proj = call_proj->clone();
1296   halt = halt->clone();
1297   Node* c = otherproj->clone();
1298 
1299   c = igvn->transform(c);
1300   new_unc->set_req(TypeFunc::Parms, unc->in(TypeFunc::Parms));
1301   new_unc->set_req(0, c);
1302   new_unc = igvn->transform(new_unc);
1303   call_proj->set_req(0, new_unc);
1304   call_proj = igvn->transform(call_proj);
1305   halt->set_req(0, call_proj);
1306   halt = igvn->transform(halt);
1307 
1308   igvn->replace_node(otherproj, igvn->C->top());
1309   igvn->C->root()->add_req(halt);
1310 }
1311 
1312 Node* IfNode::fold_compares(PhaseIterGVN* igvn) {
1313   if (Opcode() != Op_If) return nullptr;
1314 
1315   if (cmpi_folds(igvn)) {
1316     Node* ctrl = in(0);
1317     if (is_ctrl_folds(ctrl, igvn) && ctrl->outcnt() == 1) {
1318       // A integer comparison immediately dominated by another integer
1319       // comparison
1320       ProjNode* success = nullptr;
1321       ProjNode* fail = nullptr;
1322       ProjNode* dom_cmp = ctrl->as_Proj();
1323       if (has_shared_region(dom_cmp, success, fail) &&
1324           // Next call modifies graph so must be last
1325           fold_compares_helper(dom_cmp, success, fail, igvn)) {
1326         return this;
1327       }
1328       if (has_only_uncommon_traps(dom_cmp, success, fail, igvn) &&
1329           // Next call modifies graph so must be last
1330           fold_compares_helper(dom_cmp, success, fail, igvn)) {
1331         return merge_uncommon_traps(dom_cmp, success, fail, igvn);
1332       }
1333       return nullptr;
1334     } else if (ctrl->in(0) != nullptr &&
1335                ctrl->in(0)->in(0) != nullptr) {
1336       ProjNode* success = nullptr;
1337       ProjNode* fail = nullptr;
1338       Node* dom = ctrl->in(0)->in(0);
1339       ProjNode* dom_cmp = dom->isa_Proj();
1340       ProjNode* other_cmp = ctrl->isa_Proj();
1341 
1342       // Check if it's an integer comparison dominated by another
1343       // integer comparison with another test in between
1344       if (is_ctrl_folds(dom, igvn) &&
1345           has_only_uncommon_traps(dom_cmp, success, fail, igvn) &&
1346           is_side_effect_free_test(other_cmp, igvn) &&
1347           // Next call modifies graph so must be last
1348           fold_compares_helper(dom_cmp, success, fail, igvn)) {
1349         reroute_side_effect_free_unc(other_cmp, dom_cmp, igvn);
1350         return merge_uncommon_traps(dom_cmp, success, fail, igvn);
1351       }
1352     }
1353   }
1354   return nullptr;
1355 }
1356 
1357 //------------------------------remove_useless_bool----------------------------
1358 // Check for people making a useless boolean: things like
1359 // if( (x < y ? true : false) ) { ... }
1360 // Replace with if( x < y ) { ... }
1361 static Node *remove_useless_bool(IfNode *iff, PhaseGVN *phase) {
1362   Node *i1 = iff->in(1);
1363   if( !i1->is_Bool() ) return nullptr;
1364   BoolNode *bol = i1->as_Bool();
1365 
1366   Node *cmp = bol->in(1);
1367   if( cmp->Opcode() != Op_CmpI ) return nullptr;
1368 
1369   // Must be comparing against a bool
1370   const Type *cmp2_t = phase->type( cmp->in(2) );
1371   if( cmp2_t != TypeInt::ZERO &&
1372       cmp2_t != TypeInt::ONE )
1373     return nullptr;
1374 
1375   // Find a prior merge point merging the boolean
1376   i1 = cmp->in(1);
1377   if( !i1->is_Phi() ) return nullptr;
1378   PhiNode *phi = i1->as_Phi();
1379   if( phase->type( phi ) != TypeInt::BOOL )
1380     return nullptr;
1381 
1382   // Check for diamond pattern
1383   int true_path = phi->is_diamond_phi();
1384   if( true_path == 0 ) return nullptr;
1385 
1386   // Make sure that iff and the control of the phi are different. This
1387   // should really only happen for dead control flow since it requires
1388   // an illegal cycle.
1389   if (phi->in(0)->in(1)->in(0) == iff) return nullptr;
1390 
1391   // phi->region->if_proj->ifnode->bool->cmp
1392   BoolNode *bol2 = phi->in(0)->in(1)->in(0)->in(1)->as_Bool();
1393 
1394   // Now get the 'sense' of the test correct so we can plug in
1395   // either iff2->in(1) or its complement.
1396   int flip = 0;
1397   if( bol->_test._test == BoolTest::ne ) flip = 1-flip;
1398   else if( bol->_test._test != BoolTest::eq ) return nullptr;
1399   if( cmp2_t == TypeInt::ZERO ) flip = 1-flip;
1400 
1401   const Type *phi1_t = phase->type( phi->in(1) );
1402   const Type *phi2_t = phase->type( phi->in(2) );
1403   // Check for Phi(0,1) and flip
1404   if( phi1_t == TypeInt::ZERO ) {
1405     if( phi2_t != TypeInt::ONE ) return nullptr;
1406     flip = 1-flip;
1407   } else {
1408     // Check for Phi(1,0)
1409     if( phi1_t != TypeInt::ONE  ) return nullptr;
1410     if( phi2_t != TypeInt::ZERO ) return nullptr;
1411   }
1412   if( true_path == 2 ) {
1413     flip = 1-flip;
1414   }
1415 
1416   Node* new_bol = (flip ? phase->transform( bol2->negate(phase) ) : bol2);
1417   assert(new_bol != iff->in(1), "must make progress");
1418   iff->set_req_X(1, new_bol, phase);
1419   // Intervening diamond probably goes dead
1420   phase->C->set_major_progress();
1421   return iff;
1422 }
1423 
1424 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff);
1425 
1426 struct RangeCheck {
1427   IfProjNode* ctl;
1428   jint off;
1429 };
1430 
1431 Node* IfNode::Ideal_common(PhaseGVN *phase, bool can_reshape) {
1432   if (remove_dead_region(phase, can_reshape))  return this;
1433   // No Def-Use info?
1434   if (!can_reshape)  return nullptr;
1435 
1436   // Don't bother trying to transform a dead if
1437   if (in(0)->is_top())  return nullptr;
1438   // Don't bother trying to transform an if with a dead test
1439   if (in(1)->is_top())  return nullptr;
1440   // Another variation of a dead test
1441   if (in(1)->is_Con())  return nullptr;
1442   // Another variation of a dead if
1443   if (outcnt() < 2)  return nullptr;
1444 
1445   // Canonicalize the test.
1446   Node* idt_if = idealize_test(phase, this);
1447   if (idt_if != nullptr)  return idt_if;
1448 
1449   // Try to split the IF
1450   PhaseIterGVN *igvn = phase->is_IterGVN();
1451   Node *s = split_if(this, igvn);
1452   if (s != nullptr)  return s;
1453 
1454   return NodeSentinel;
1455 }
1456 
1457 //------------------------------Ideal------------------------------------------
1458 // Return a node which is more "ideal" than the current node.  Strip out
1459 // control copies
1460 Node* IfNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1461   Node* res = Ideal_common(phase, can_reshape);
1462   if (res != NodeSentinel) {
1463     return res;
1464   }
1465 
1466   // Check for people making a useless boolean: things like
1467   // if( (x < y ? true : false) ) { ... }
1468   // Replace with if( x < y ) { ... }
1469   Node* bol2 = remove_useless_bool(this, phase);
1470   if (bol2) return bol2;
1471 
1472   if (in(0) == nullptr) return nullptr;     // Dead loop?
1473 
1474   PhaseIterGVN* igvn = phase->is_IterGVN();
1475   Node* result = fold_compares(igvn);
1476   if (result != nullptr) {
1477     return result;
1478   }
1479 
1480   // Scan for an equivalent test
1481   int dist = 4;               // Cutoff limit for search
1482   if (is_If() && in(1)->is_Bool()) {
1483     Node* cmp = in(1)->in(1);
1484     if (cmp->Opcode() == Op_CmpP &&
1485         cmp->in(2) != nullptr && // make sure cmp is not already dead
1486         cmp->in(2)->bottom_type() == TypePtr::NULL_PTR) {
1487       dist = 64;              // Limit for null-pointer scans
1488     }
1489   }
1490 
1491   Node* prev_dom = search_identical(dist, igvn);
1492 
1493   if (prev_dom != nullptr) {
1494     // Replace dominated IfNode
1495     return dominated_by(prev_dom, igvn, false);
1496   }
1497 
1498   return simple_subsuming(igvn);
1499 }
1500 
1501 //------------------------------dominated_by-----------------------------------
1502 Node* IfNode::dominated_by(Node* prev_dom, PhaseIterGVN* igvn, bool pin_array_access_nodes) {
1503 #ifndef PRODUCT
1504   if (TraceIterativeGVN) {
1505     tty->print("   Removing IfNode: "); this->dump();
1506   }
1507 #endif
1508 
1509   igvn->hash_delete(this);      // Remove self to prevent spurious V-N
1510   Node *idom = in(0);
1511   // Need opcode to decide which way 'this' test goes
1512   int prev_op = prev_dom->Opcode();
1513   Node *top = igvn->C->top(); // Shortcut to top
1514 
1515   // Now walk the current IfNode's projections.
1516   // Loop ends when 'this' has no more uses.
1517   for (DUIterator_Last imin, i = last_outs(imin); i >= imin; --i) {
1518     Node *ifp = last_out(i);     // Get IfTrue/IfFalse
1519     igvn->add_users_to_worklist(ifp);
1520     // Check which projection it is and set target.
1521     // Data-target is either the dominating projection of the same type
1522     // or TOP if the dominating projection is of opposite type.
1523     // Data-target will be used as the new control edge for the non-CFG
1524     // nodes like Casts and Loads.
1525     Node *data_target = (ifp->Opcode() == prev_op) ? prev_dom : top;
1526     // Control-target is just the If's immediate dominator or TOP.
1527     Node *ctrl_target = (ifp->Opcode() == prev_op) ?     idom : top;
1528 
1529     // For each child of an IfTrue/IfFalse projection, reroute.
1530     // Loop ends when projection has no more uses.
1531     for (DUIterator_Last jmin, j = ifp->last_outs(jmin); j >= jmin; --j) {
1532       Node* s = ifp->last_out(j);   // Get child of IfTrue/IfFalse
1533       if (s->depends_only_on_test() && igvn->no_dependent_zero_check(s)) {
1534         // For control producers.
1535         // Do not rewire Div and Mod nodes which could have a zero divisor to avoid skipping their zero check.
1536         igvn->replace_input_of(s, 0, data_target); // Move child to data-target
1537         if (pin_array_access_nodes && data_target != top) {
1538           // As a result of range check smearing, Loads and range check Cast nodes that are control dependent on this
1539           // range check (that is about to be removed) now depend on multiple dominating range checks. After the removal
1540           // of this range check, these control dependent nodes end up at the lowest/nearest dominating check in the
1541           // graph. To ensure that these Loads/Casts do not float above any of the dominating checks (even when the
1542           // lowest dominating check is later replaced by yet another dominating check), we need to pin them at the
1543           // lowest dominating check.
1544           Node* clone = s->pin_array_access_node();
1545           if (clone != nullptr) {
1546             clone = igvn->transform(clone);
1547             igvn->replace_node(s, clone);
1548           }
1549         }
1550       } else {
1551         // Find the control input matching this def-use edge.
1552         // For Regions it may not be in slot 0.
1553         uint l;
1554         for (l = 0; s->in(l) != ifp; l++) { }
1555         igvn->replace_input_of(s, l, ctrl_target);
1556       }
1557     } // End for each child of a projection
1558 
1559     igvn->remove_dead_node(ifp);
1560   } // End for each IfTrue/IfFalse child of If
1561 
1562   // Kill the IfNode
1563   igvn->remove_dead_node(this);
1564 
1565   // Must return either the original node (now dead) or a new node
1566   // (Do not return a top here, since that would break the uniqueness of top.)
1567   return new ConINode(TypeInt::ZERO);
1568 }
1569 
1570 Node* IfNode::search_identical(int dist, PhaseIterGVN* igvn) {
1571   // Setup to scan up the CFG looking for a dominating test
1572   Node* dom = in(0);
1573   Node* prev_dom = this;
1574   int op = Opcode();
1575   // Search up the dominator tree for an If with an identical test
1576   while (dom->Opcode() != op ||  // Not same opcode?
1577          !same_condition(dom, igvn) ||  // Not same input 1?
1578          prev_dom->in(0) != dom) {  // One path of test does not dominate?
1579     if (dist < 0) return nullptr;
1580 
1581     dist--;
1582     prev_dom = dom;
1583     dom = up_one_dom(dom);
1584     if (!dom) return nullptr;
1585   }
1586 
1587   // Check that we did not follow a loop back to ourselves
1588   if (this == dom) {
1589     return nullptr;
1590   }
1591 
1592 #ifndef PRODUCT
1593   if (dist > 2) { // Add to count of null checks elided
1594     explicit_null_checks_elided++;
1595   }
1596 #endif
1597 
1598   return prev_dom;
1599 }
1600 
1601 bool IfNode::same_condition(const Node* dom, PhaseIterGVN* igvn) const {
1602   Node* dom_bool = dom->in(1);
1603   Node* this_bool = in(1);
1604   if (dom_bool == this_bool) {
1605     return true;
1606   }
1607 
1608   if (dom_bool == nullptr || !dom_bool->is_Bool() ||
1609       this_bool == nullptr || !this_bool->is_Bool()) {
1610     return false;
1611   }
1612   Node* dom_cmp = dom_bool->in(1);
1613   Node* this_cmp = this_bool->in(1);
1614 
1615   // If the comparison is a subtype check, then SubTypeCheck nodes may have profile data attached to them and may be
1616   // different nodes even-though they perform the same subtype check
1617   if (dom_cmp == nullptr || !dom_cmp->is_SubTypeCheck() ||
1618       this_cmp == nullptr || !this_cmp->is_SubTypeCheck()) {
1619     return false;
1620   }
1621 
1622   if (dom_cmp->in(1) != this_cmp->in(1) ||
1623       dom_cmp->in(2) != this_cmp->in(2) ||
1624       dom_bool->as_Bool()->_test._test != this_bool->as_Bool()->_test._test) {
1625     return false;
1626   }
1627 
1628   return true;
1629 }
1630 
1631 
1632 static int subsuming_bool_test_encode(Node*);
1633 
1634 // Check if dominating test is subsuming 'this' one.
1635 //
1636 //              cmp
1637 //              / \
1638 //     (r1)  bool  \
1639 //            /    bool (r2)
1640 //    (dom) if       \
1641 //            \       )
1642 //    (pre)  if[TF]  /
1643 //               \  /
1644 //                if (this)
1645 //   \r1
1646 //  r2\  eqT  eqF  neT  neF  ltT  ltF  leT  leF  gtT  gtF  geT  geF
1647 //  eq    t    f    f    t    f    -    -    f    f    -    -    f
1648 //  ne    f    t    t    f    t    -    -    t    t    -    -    t
1649 //  lt    f    -    -    f    t    f    -    f    f    -    f    t
1650 //  le    t    -    -    t    t    -    t    f    f    t    -    t
1651 //  gt    f    -    -    f    f    -    f    t    t    f    -    f
1652 //  ge    t    -    -    t    f    t    -    t    t    -    t    f
1653 //
1654 Node* IfNode::simple_subsuming(PhaseIterGVN* igvn) {
1655   // Table encoding: N/A (na), True-branch (tb), False-branch (fb).
1656   static enum { na, tb, fb } s_short_circuit_map[6][12] = {
1657   /*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*/
1658   /*eq*/{ tb,  fb,  fb,  tb,  fb,  na,  na,  fb,  fb,  na,  na,  fb },
1659   /*ne*/{ fb,  tb,  tb,  fb,  tb,  na,  na,  tb,  tb,  na,  na,  tb },
1660   /*lt*/{ fb,  na,  na,  fb,  tb,  fb,  na,  fb,  fb,  na,  fb,  tb },
1661   /*le*/{ tb,  na,  na,  tb,  tb,  na,  tb,  fb,  fb,  tb,  na,  tb },
1662   /*gt*/{ fb,  na,  na,  fb,  fb,  na,  fb,  tb,  tb,  fb,  na,  fb },
1663   /*ge*/{ tb,  na,  na,  tb,  fb,  tb,  na,  tb,  tb,  na,  tb,  fb }};
1664 
1665   Node* pre = in(0);
1666   if (!pre->is_IfTrue() && !pre->is_IfFalse()) {
1667     return nullptr;
1668   }
1669   Node* dom = pre->in(0);
1670   if (!dom->is_If()) {
1671     return nullptr;
1672   }
1673   Node* bol = in(1);
1674   if (!bol->is_Bool()) {
1675     return nullptr;
1676   }
1677   Node* cmp = in(1)->in(1);
1678   if (!cmp->is_Cmp()) {
1679     return nullptr;
1680   }
1681 
1682   if (!dom->in(1)->is_Bool()) {
1683     return nullptr;
1684   }
1685   if (dom->in(1)->in(1) != cmp) {  // Not same cond?
1686     return nullptr;
1687   }
1688 
1689   int drel = subsuming_bool_test_encode(dom->in(1));
1690   int trel = subsuming_bool_test_encode(bol);
1691   int bout = pre->is_IfFalse() ? 1 : 0;
1692 
1693   if (drel < 0 || trel < 0) {
1694     return nullptr;
1695   }
1696   int br = s_short_circuit_map[trel][2*drel+bout];
1697   if (br == na) {
1698     return nullptr;
1699   }
1700 #ifndef PRODUCT
1701   if (TraceIterativeGVN) {
1702     tty->print("   Subsumed IfNode: "); dump();
1703   }
1704 #endif
1705   // Replace condition with constant True(1)/False(0).
1706   bool is_always_true = br == tb;
1707   set_req(1, igvn->intcon(is_always_true ? 1 : 0));
1708 
1709   // Update any data dependencies to the directly dominating test. This subsumed test is not immediately removed by igvn
1710   // and therefore subsequent optimizations might miss these data dependencies otherwise. There might be a dead loop
1711   // ('always_taken_proj' == 'pre') that is cleaned up later. Skip this case to make the iterator work properly.
1712   Node* always_taken_proj = proj_out(is_always_true);
1713   if (always_taken_proj != pre) {
1714     for (DUIterator_Fast imax, i = always_taken_proj->fast_outs(imax); i < imax; i++) {
1715       Node* u = always_taken_proj->fast_out(i);
1716       if (!u->is_CFG()) {
1717         igvn->replace_input_of(u, 0, pre);
1718         --i;
1719         --imax;
1720       }
1721     }
1722   }
1723 
1724   if (bol->outcnt() == 0) {
1725     igvn->remove_dead_node(bol);    // Kill the BoolNode.
1726   }
1727   return this;
1728 }
1729 
1730 // Map BoolTest to local table encoding. The BoolTest (e)numerals
1731 //   { eq = 0, ne = 4, le = 5, ge = 7, lt = 3, gt = 1 }
1732 // are mapped to table indices, while the remaining (e)numerals in BoolTest
1733 //   { overflow = 2, no_overflow = 6, never = 8, illegal = 9 }
1734 // are ignored (these are not modeled in the table).
1735 //
1736 static int subsuming_bool_test_encode(Node* node) {
1737   precond(node->is_Bool());
1738   BoolTest::mask x = node->as_Bool()->_test._test;
1739   switch (x) {
1740     case BoolTest::eq: return 0;
1741     case BoolTest::ne: return 1;
1742     case BoolTest::lt: return 2;
1743     case BoolTest::le: return 3;
1744     case BoolTest::gt: return 4;
1745     case BoolTest::ge: return 5;
1746     case BoolTest::overflow:
1747     case BoolTest::no_overflow:
1748     case BoolTest::never:
1749     case BoolTest::illegal:
1750     default:
1751       return -1;
1752   }
1753 }
1754 
1755 //------------------------------Identity---------------------------------------
1756 // If the test is constant & we match, then we are the input Control
1757 Node* IfProjNode::Identity(PhaseGVN* phase) {
1758   // Can only optimize if cannot go the other way
1759   const TypeTuple *t = phase->type(in(0))->is_tuple();
1760   if (t == TypeTuple::IFNEITHER || (always_taken(t) &&
1761        // During parsing (GVN) we don't remove dead code aggressively.
1762        // Cut off dead branch and let PhaseRemoveUseless take care of it.
1763       (!phase->is_IterGVN() ||
1764        // During IGVN, first wait for the dead branch to be killed.
1765        // Otherwise, the IfNode's control will have two control uses (the IfNode
1766        // that doesn't go away because it still has uses and this branch of the
1767        // If) which breaks other optimizations. Node::has_special_unique_user()
1768        // will cause this node to be reprocessed once the dead branch is killed.
1769        in(0)->outcnt() == 1))) {
1770     // IfNode control
1771     if (in(0)->is_BaseCountedLoopEnd()) {
1772       // CountedLoopEndNode may be eliminated by if subsuming, replace CountedLoopNode with LoopNode to
1773       // avoid mismatching between CountedLoopNode and CountedLoopEndNode in the following optimization.
1774       Node* head = unique_ctrl_out_or_null();
1775       if (head != nullptr && head->is_BaseCountedLoop() && head->in(LoopNode::LoopBackControl) == this) {
1776         Node* new_head = new LoopNode(head->in(LoopNode::EntryControl), this);
1777         phase->is_IterGVN()->register_new_node_with_optimizer(new_head);
1778         phase->is_IterGVN()->replace_node(head, new_head);
1779       }
1780     }
1781     return in(0)->in(0);
1782   }
1783   // no progress
1784   return this;
1785 }
1786 
1787 bool IfNode::is_zero_trip_guard() const {
1788   if (in(1)->is_Bool() && in(1)->in(1)->is_Cmp()) {
1789     return in(1)->in(1)->in(1)->Opcode() == Op_OpaqueZeroTripGuard;
1790   }
1791   return false;
1792 }
1793 
1794 void IfProjNode::pin_array_access_nodes(PhaseIterGVN* igvn) {
1795   for (DUIterator i = outs(); has_out(i); i++) {
1796     Node* u = out(i);
1797     if (!u->depends_only_on_test()) {
1798       continue;
1799     }
1800     Node* clone = u->pin_array_access_node();
1801     if (clone != nullptr) {
1802       clone = igvn->transform(clone);
1803       assert(clone != u, "shouldn't common");
1804       igvn->replace_node(u, clone);
1805       --i;
1806     }
1807   }
1808 }
1809 
1810 #ifndef PRODUCT
1811 //------------------------------dump_spec--------------------------------------
1812 void IfNode::dump_spec(outputStream *st) const {
1813   st->print("P=%f, C=%f",_prob,_fcnt);
1814 }
1815 #endif
1816 
1817 //------------------------------idealize_test----------------------------------
1818 // Try to canonicalize tests better.  Peek at the Cmp/Bool/If sequence and
1819 // come up with a canonical sequence.  Bools getting 'eq', 'gt' and 'ge' forms
1820 // converted to 'ne', 'le' and 'lt' forms.  IfTrue/IfFalse get swapped as
1821 // needed.
1822 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff) {
1823   assert(iff->in(0) != nullptr, "If must be live");
1824 
1825   if (iff->outcnt() != 2)  return nullptr; // Malformed projections.
1826   Node* old_if_f = iff->proj_out(false);
1827   Node* old_if_t = iff->proj_out(true);
1828 
1829   // CountedLoopEnds want the back-control test to be TRUE, regardless of
1830   // whether they are testing a 'gt' or 'lt' condition.  The 'gt' condition
1831   // happens in count-down loops
1832   if (iff->is_BaseCountedLoopEnd())  return nullptr;
1833   if (!iff->in(1)->is_Bool())  return nullptr; // Happens for partially optimized IF tests
1834   BoolNode *b = iff->in(1)->as_Bool();
1835   BoolTest bt = b->_test;
1836   // Test already in good order?
1837   if( bt.is_canonical() )
1838     return nullptr;
1839 
1840   // Flip test to be canonical.  Requires flipping the IfFalse/IfTrue and
1841   // cloning the IfNode.
1842   Node* new_b = phase->transform( new BoolNode(b->in(1), bt.negate()) );
1843   if( !new_b->is_Bool() ) return nullptr;
1844   b = new_b->as_Bool();
1845 
1846   PhaseIterGVN *igvn = phase->is_IterGVN();
1847   assert( igvn, "Test is not canonical in parser?" );
1848 
1849   // The IF node never really changes, but it needs to be cloned
1850   iff = iff->clone()->as_If();
1851   iff->set_req(1, b);
1852   iff->_prob = 1.0-iff->_prob;
1853 
1854   Node *prior = igvn->hash_find_insert(iff);
1855   if( prior ) {
1856     igvn->remove_dead_node(iff);
1857     iff = (IfNode*)prior;
1858   } else {
1859     // Cannot call transform on it just yet
1860     igvn->set_type_bottom(iff);
1861   }
1862   igvn->_worklist.push(iff);
1863 
1864   // Now handle projections.  Cloning not required.
1865   Node* new_if_f = (Node*)(new IfFalseNode( iff ));
1866   Node* new_if_t = (Node*)(new IfTrueNode ( iff ));
1867 
1868   igvn->register_new_node_with_optimizer(new_if_f);
1869   igvn->register_new_node_with_optimizer(new_if_t);
1870   // Flip test, so flip trailing control
1871   igvn->replace_node(old_if_f, new_if_t);
1872   igvn->replace_node(old_if_t, new_if_f);
1873 
1874   // Progress
1875   return iff;
1876 }
1877 
1878 Node* RangeCheckNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1879   Node* res = Ideal_common(phase, can_reshape);
1880   if (res != NodeSentinel) {
1881     return res;
1882   }
1883 
1884   PhaseIterGVN *igvn = phase->is_IterGVN();
1885   // Setup to scan up the CFG looking for a dominating test
1886   Node* prev_dom = this;
1887 
1888   // Check for range-check vs other kinds of tests
1889   Node* index1;
1890   Node* range1;
1891   jint offset1;
1892   int flip1 = is_range_check(range1, index1, offset1);
1893   if (flip1) {
1894     Node* dom = in(0);
1895     // Try to remove extra range checks.  All 'up_one_dom' gives up at merges
1896     // so all checks we inspect post-dominate the top-most check we find.
1897     // If we are going to fail the current check and we reach the top check
1898     // then we are guaranteed to fail, so just start interpreting there.
1899     // We 'expand' the top 3 range checks to include all post-dominating
1900     // checks.
1901     //
1902     // Example:
1903     // a[i+x] // (1) 1 < x < 6
1904     // a[i+3] // (2)
1905     // a[i+4] // (3)
1906     // a[i+6] // max = max of all constants
1907     // a[i+2]
1908     // a[i+1] // min = min of all constants
1909     //
1910     // If x < 3:
1911     //   (1) a[i+x]: Leave unchanged
1912     //   (2) a[i+3]: Replace with a[i+max] = a[i+6]: i+x < i+3 <= i+6  -> (2) is covered
1913     //   (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
1914     //   Remove all other a[i+c] checks
1915     //
1916     // If x >= 3:
1917     //   (1) a[i+x]: Leave unchanged
1918     //   (2) a[i+3]: Replace with a[i+min] = a[i+1]: i+1 < i+3 <= i+x  -> (2) is covered
1919     //   (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
1920     //   Remove all other a[i+c] checks
1921     //
1922     // We only need the top 2 range checks if x is the min or max of all constants.
1923     //
1924     // This, however, only works if the interval [i+min,i+max] is not larger than max_int (i.e. abs(max - min) < max_int):
1925     // The theoretical max size of an array is max_int with:
1926     // - Valid index space: [0,max_int-1]
1927     // - Invalid index space: [max_int,-1] // max_int, min_int, min_int - 1 ..., -1
1928     //
1929     // The size of the consecutive valid index space is smaller than the size of the consecutive invalid index space.
1930     // If we choose min and max in such a way that:
1931     // - abs(max - min) < max_int
1932     // - i+max and i+min are inside the valid index space
1933     // then all indices [i+min,i+max] must be in the valid index space. Otherwise, the invalid index space must be
1934     // smaller than the valid index space which is never the case for any array size.
1935     //
1936     // Choosing a smaller array size only makes the valid index space smaller and the invalid index space larger and
1937     // the argument above still holds.
1938     //
1939     // Note that the same optimization with the same maximal accepted interval size can also be found in C1.
1940     const jlong maximum_number_of_min_max_interval_indices = (jlong)max_jint;
1941 
1942     // The top 3 range checks seen
1943     const int NRC = 3;
1944     RangeCheck prev_checks[NRC];
1945     int nb_checks = 0;
1946 
1947     // Low and high offsets seen so far
1948     jint off_lo = offset1;
1949     jint off_hi = offset1;
1950 
1951     bool found_immediate_dominator = false;
1952 
1953     // Scan for the top checks and collect range of offsets
1954     for (int dist = 0; dist < 999; dist++) { // Range-Check scan limit
1955       if (dom->Opcode() == Op_RangeCheck &&  // Not same opcode?
1956           prev_dom->in(0) == dom) { // One path of test does dominate?
1957         if (dom == this) return nullptr; // dead loop
1958         // See if this is a range check
1959         Node* index2;
1960         Node* range2;
1961         jint offset2;
1962         int flip2 = dom->as_RangeCheck()->is_range_check(range2, index2, offset2);
1963         // See if this is a _matching_ range check, checking against
1964         // the same array bounds.
1965         if (flip2 == flip1 && range2 == range1 && index2 == index1 &&
1966             dom->outcnt() == 2) {
1967           if (nb_checks == 0 && dom->in(1) == in(1)) {
1968             // Found an immediately dominating test at the same offset.
1969             // This kind of back-to-back test can be eliminated locally,
1970             // and there is no need to search further for dominating tests.
1971             assert(offset2 == offset1, "Same test but different offsets");
1972             found_immediate_dominator = true;
1973             break;
1974           }
1975 
1976           // "x - y" -> must add one to the difference for number of elements in [x,y]
1977           const jlong diff = (jlong)MIN2(offset2, off_lo) - (jlong)MAX2(offset2, off_hi);
1978           if (ABS(diff) < maximum_number_of_min_max_interval_indices) {
1979             // Gather expanded bounds
1980             off_lo = MIN2(off_lo, offset2);
1981             off_hi = MAX2(off_hi, offset2);
1982             // Record top NRC range checks
1983             prev_checks[nb_checks % NRC].ctl = prev_dom->as_IfProj();
1984             prev_checks[nb_checks % NRC].off = offset2;
1985             nb_checks++;
1986           }
1987         }
1988       }
1989       prev_dom = dom;
1990       dom = up_one_dom(dom);
1991       if (!dom) break;
1992     }
1993 
1994     if (!found_immediate_dominator) {
1995       // Attempt to widen the dominating range check to cover some later
1996       // ones.  Since range checks "fail" by uncommon-trapping to the
1997       // interpreter, widening a check can make us speculatively enter
1998       // the interpreter.  If we see range-check deopt's, do not widen!
1999       if (!phase->C->allow_range_check_smearing())  return nullptr;
2000 
2001       if (can_reshape && !phase->C->post_loop_opts_phase()) {
2002         // We are about to perform range check smearing (i.e. remove this RangeCheck if it is dominated by
2003         // a series of RangeChecks which have a range that covers this RangeCheck). This can cause array access nodes to
2004         // be pinned. We want to avoid that and first allow range check elimination a chance to remove the RangeChecks
2005         // from loops. Hence, we delay range check smearing until after loop opts.
2006         phase->C->record_for_post_loop_opts_igvn(this);
2007         return nullptr;
2008       }
2009 
2010       // Didn't find prior covering check, so cannot remove anything.
2011       if (nb_checks == 0) {
2012         return nullptr;
2013       }
2014       // Constant indices only need to check the upper bound.
2015       // Non-constant indices must check both low and high.
2016       int chk0 = (nb_checks - 1) % NRC;
2017       if (index1) {
2018         if (nb_checks == 1) {
2019           return nullptr;
2020         } else {
2021           // If the top range check's constant is the min or max of
2022           // all constants we widen the next one to cover the whole
2023           // range of constants.
2024           RangeCheck rc0 = prev_checks[chk0];
2025           int chk1 = (nb_checks - 2) % NRC;
2026           RangeCheck rc1 = prev_checks[chk1];
2027           if (rc0.off == off_lo) {
2028             adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn);
2029             prev_dom = rc1.ctl;
2030           } else if (rc0.off == off_hi) {
2031             adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn);
2032             prev_dom = rc1.ctl;
2033           } else {
2034             // If the top test's constant is not the min or max of all
2035             // constants, we need 3 range checks. We must leave the
2036             // top test unchanged because widening it would allow the
2037             // accesses it protects to successfully read/write out of
2038             // bounds.
2039             if (nb_checks == 2) {
2040               return nullptr;
2041             }
2042             int chk2 = (nb_checks - 3) % NRC;
2043             RangeCheck rc2 = prev_checks[chk2];
2044             // The top range check a+i covers interval: -a <= i < length-a
2045             // The second range check b+i covers interval: -b <= i < length-b
2046             if (rc1.off <= rc0.off) {
2047               // if b <= a, we change the second range check to:
2048               // -min_of_all_constants <= i < length-min_of_all_constants
2049               // Together top and second range checks now cover:
2050               // -min_of_all_constants <= i < length-a
2051               // which is more restrictive than -b <= i < length-b:
2052               // -b <= -min_of_all_constants <= i < length-a <= length-b
2053               // The third check is then changed to:
2054               // -max_of_all_constants <= i < length-max_of_all_constants
2055               // so 2nd and 3rd checks restrict allowed values of i to:
2056               // -min_of_all_constants <= i < length-max_of_all_constants
2057               adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn);
2058               adjust_check(rc2.ctl, range1, index1, flip1, off_hi, igvn);
2059             } else {
2060               // if b > a, we change the second range check to:
2061               // -max_of_all_constants <= i < length-max_of_all_constants
2062               // Together top and second range checks now cover:
2063               // -a <= i < length-max_of_all_constants
2064               // which is more restrictive than -b <= i < length-b:
2065               // -b < -a <= i < length-max_of_all_constants <= length-b
2066               // The third check is then changed to:
2067               // -max_of_all_constants <= i < length-max_of_all_constants
2068               // so 2nd and 3rd checks restrict allowed values of i to:
2069               // -min_of_all_constants <= i < length-max_of_all_constants
2070               adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn);
2071               adjust_check(rc2.ctl, range1, index1, flip1, off_lo, igvn);
2072             }
2073             prev_dom = rc2.ctl;
2074           }
2075         }
2076       } else {
2077         RangeCheck rc0 = prev_checks[chk0];
2078         // 'Widen' the offset of the 1st and only covering check
2079         adjust_check(rc0.ctl, range1, index1, flip1, off_hi, igvn);
2080         // Test is now covered by prior checks, dominate it out
2081         prev_dom = rc0.ctl;
2082       }
2083       // The last RangeCheck is found to be redundant with a sequence of n (n >= 2) preceding RangeChecks.
2084       // If an array load is control dependent on the eliminated range check, the array load nodes (CastII and Load)
2085       // become control dependent on the last range check of the sequence, but they are really dependent on the entire
2086       // sequence of RangeChecks. If RangeCheck#n is later replaced by a dominating identical check, the array load
2087       // nodes must not float above the n-1 other RangeCheck in the sequence. We pin the array load nodes here to
2088       // guarantee it doesn't happen.
2089       //
2090       // RangeCheck#1                 RangeCheck#1
2091       //    |      \                     |      \
2092       //    |      uncommon trap         |      uncommon trap
2093       //    ..                           ..
2094       // RangeCheck#n              -> RangeCheck#n
2095       //    |      \                     |      \
2096       //    |      uncommon trap        CastII  uncommon trap
2097       // RangeCheck                     Load
2098       //    |      \
2099       //   CastII  uncommon trap
2100       //   Load
2101 
2102       return dominated_by(prev_dom, igvn, true);
2103     }
2104   } else {
2105     prev_dom = search_identical(4, igvn);
2106 
2107     if (prev_dom == nullptr) {
2108       return nullptr;
2109     }
2110   }
2111 
2112   // Replace dominated IfNode
2113   return dominated_by(prev_dom, igvn, false);
2114 }
2115 
2116 ParsePredicateNode::ParsePredicateNode(Node* control, Deoptimization::DeoptReason deopt_reason, PhaseGVN* gvn)
2117     : IfNode(control, gvn->intcon(1), PROB_MAX, COUNT_UNKNOWN),
2118       _deopt_reason(deopt_reason),
2119       _useless(false) {
2120   init_class_id(Class_ParsePredicate);
2121   gvn->C->add_parse_predicate(this);
2122   gvn->C->record_for_post_loop_opts_igvn(this);
2123 #ifdef ASSERT
2124   switch (deopt_reason) {
2125     case Deoptimization::Reason_predicate:
2126     case Deoptimization::Reason_profile_predicate:
2127     case Deoptimization::Reason_loop_limit_check:
2128       break;
2129     default:
2130       assert(false, "unsupported deoptimization reason for Parse Predicate");
2131   }
2132 #endif // ASSERT
2133 }
2134 
2135 Node* ParsePredicateNode::uncommon_trap() const {
2136   ParsePredicateUncommonProj* uncommon_proj = proj_out(0)->as_IfFalse();
2137   Node* uct_region_or_call = uncommon_proj->unique_ctrl_out();
2138   assert(uct_region_or_call->is_Region() || uct_region_or_call->is_Call(), "must be a region or call uct");
2139   return uct_region_or_call;
2140 }
2141 
2142 // Fold this node away once it becomes useless or at latest in post loop opts IGVN.
2143 const Type* ParsePredicateNode::Value(PhaseGVN* phase) const {
2144   if (phase->type(in(0)) == Type::TOP) {
2145     return Type::TOP;
2146   }
2147   if (_useless || phase->C->post_loop_opts_phase()) {
2148     return TypeTuple::IFTRUE;
2149   } else {
2150     return bottom_type();
2151   }
2152 }
2153 
2154 #ifndef PRODUCT
2155 void ParsePredicateNode::dump_spec(outputStream* st) const {
2156   st->print(" #");
2157   switch (_deopt_reason) {
2158     case Deoptimization::DeoptReason::Reason_predicate:
2159       st->print("Loop ");
2160       break;
2161     case Deoptimization::DeoptReason::Reason_profile_predicate:
2162       st->print("Profiled_Loop ");
2163       break;
2164     case Deoptimization::DeoptReason::Reason_loop_limit_check:
2165       st->print("Loop_Limit_Check ");
2166       break;
2167     default:
2168       fatal("unknown kind");
2169   }
2170 }
2171 
2172 #endif // NOT PRODUCT