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