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