1 /*
   2  * Copyright (c) 2000, 2022, 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 can't 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) != igvn->C->top() &&
 737     ctrl->in(0)->in(1)->in(1)->in(1) == in(1)->in(1)->in(1);
 738 }
 739 
 740 // Do this If and the dominating If share a region?
 741 bool IfNode::has_shared_region(ProjNode* proj, ProjNode*& success, ProjNode*& fail) {
 742   ProjNode* otherproj = proj->other_if_proj();
 743   Node* otherproj_ctrl_use = otherproj->unique_ctrl_out_or_null();
 744   RegionNode* region = (otherproj_ctrl_use != NULL && otherproj_ctrl_use->is_Region()) ? otherproj_ctrl_use->as_Region() : NULL;
 745   success = NULL;
 746   fail = NULL;
 747 
 748   if (otherproj->outcnt() == 1 && region != NULL && !region->has_phi()) {
 749     for (int i = 0; i < 2; i++) {
 750       ProjNode* proj = proj_out(i);
 751       if (success == NULL && proj->outcnt() == 1 && proj->unique_out() == region) {
 752         success = proj;
 753       } else if (fail == NULL) {
 754         fail = proj;
 755       } else {
 756         success = fail = NULL;
 757       }
 758     }
 759   }
 760   return success != NULL && fail != NULL;
 761 }
 762 
 763 bool IfNode::is_dominator_unc(CallStaticJavaNode* dom_unc, CallStaticJavaNode* unc) {
 764   // Different methods and methods containing jsrs are not supported.
 765   ciMethod* method = unc->jvms()->method();
 766   ciMethod* dom_method = dom_unc->jvms()->method();
 767   if (method != dom_method || method->has_jsrs()) {
 768     return false;
 769   }
 770   // Check that both traps are in the same activation of the method (instead
 771   // of two activations being inlined through different call sites) by verifying
 772   // that the call stacks are equal for both JVMStates.
 773   JVMState* dom_caller = dom_unc->jvms()->caller();
 774   JVMState* caller = unc->jvms()->caller();
 775   if ((dom_caller == NULL) != (caller == NULL)) {
 776     // The current method must either be inlined into both dom_caller and
 777     // caller or must not be inlined at all (top method). Bail out otherwise.
 778     return false;
 779   } else if (dom_caller != NULL && !dom_caller->same_calls_as(caller)) {
 780     return false;
 781   }
 782   // Check that the bci of the dominating uncommon trap dominates the bci
 783   // of the dominated uncommon trap. Otherwise we may not re-execute
 784   // the dominated check after deoptimization from the merged uncommon trap.
 785   ciTypeFlow* flow = dom_method->get_flow_analysis();
 786   int bci = unc->jvms()->bci();
 787   int dom_bci = dom_unc->jvms()->bci();
 788   if (!flow->is_dominated_by(bci, dom_bci)) {
 789     return false;
 790   }
 791 
 792   return true;
 793 }
 794 
 795 // Return projection that leads to an uncommon trap if any
 796 ProjNode* IfNode::uncommon_trap_proj(CallStaticJavaNode*& call) const {
 797   for (int i = 0; i < 2; i++) {
 798     call = proj_out(i)->is_uncommon_trap_proj(Deoptimization::Reason_none);
 799     if (call != NULL) {
 800       return proj_out(i);
 801     }
 802   }
 803   return NULL;
 804 }
 805 
 806 // Do this If and the dominating If both branch out to an uncommon trap
 807 bool IfNode::has_only_uncommon_traps(ProjNode* proj, ProjNode*& success, ProjNode*& fail, PhaseIterGVN* igvn) {
 808   ProjNode* otherproj = proj->other_if_proj();
 809   CallStaticJavaNode* dom_unc = otherproj->is_uncommon_trap_proj(Deoptimization::Reason_none);
 810 
 811   if (otherproj->outcnt() == 1 && dom_unc != NULL) {
 812     // We need to re-execute the folded Ifs after deoptimization from the merged traps
 813     if (!dom_unc->jvms()->should_reexecute()) {
 814       return false;
 815     }
 816 
 817     CallStaticJavaNode* unc = NULL;
 818     ProjNode* unc_proj = uncommon_trap_proj(unc);
 819     if (unc_proj != NULL && unc_proj->outcnt() == 1) {
 820       if (dom_unc == unc) {
 821         // Allow the uncommon trap to be shared through a region
 822         RegionNode* r = unc->in(0)->as_Region();
 823         if (r->outcnt() != 2 || r->req() != 3 || r->find_edge(otherproj) == -1 || r->find_edge(unc_proj) == -1) {
 824           return false;
 825         }
 826         assert(r->has_phi() == NULL, "simple region shouldn't have a phi");
 827       } else if (dom_unc->in(0) != otherproj || unc->in(0) != unc_proj) {
 828         return false;
 829       }
 830 
 831       if (!is_dominator_unc(dom_unc, unc)) {
 832         return false;
 833       }
 834 
 835       // See merge_uncommon_traps: the reason of the uncommon trap
 836       // will be changed and the state of the dominating If will be
 837       // used. Checked that we didn't apply this transformation in a
 838       // previous compilation and it didn't cause too many traps
 839       ciMethod* dom_method = dom_unc->jvms()->method();
 840       int dom_bci = dom_unc->jvms()->bci();
 841       if (!igvn->C->too_many_traps(dom_method, dom_bci, Deoptimization::Reason_unstable_fused_if) &&
 842           !igvn->C->too_many_traps(dom_method, dom_bci, Deoptimization::Reason_range_check) &&
 843           // Return true if c2 manages to reconcile with UnstableIf optimization. See the comments for it.
 844           igvn->C->remove_unstable_if_trap(dom_unc, true/*yield*/)) {
 845         success = unc_proj;
 846         fail = unc_proj->other_if_proj();
 847         return true;
 848       }
 849     }
 850   }
 851   return false;
 852 }
 853 
 854 // Check that the 2 CmpI can be folded into as single CmpU and proceed with the folding
 855 bool IfNode::fold_compares_helper(ProjNode* proj, ProjNode* success, ProjNode* fail, PhaseIterGVN* igvn) {
 856   Node* this_cmp = in(1)->in(1);
 857   BoolNode* this_bool = in(1)->as_Bool();
 858   IfNode* dom_iff = proj->in(0)->as_If();
 859   BoolNode* dom_bool = dom_iff->in(1)->as_Bool();
 860   Node* lo = dom_iff->in(1)->in(1)->in(2);
 861   Node* hi = this_cmp->in(2);
 862   Node* n = this_cmp->in(1);
 863   ProjNode* otherproj = proj->other_if_proj();
 864 
 865   const TypeInt* lo_type = IfNode::filtered_int_type(igvn, n, otherproj);
 866   const TypeInt* hi_type = IfNode::filtered_int_type(igvn, n, success);
 867 
 868   BoolTest::mask lo_test = dom_bool->_test._test;
 869   BoolTest::mask hi_test = this_bool->_test._test;
 870   BoolTest::mask cond = hi_test;
 871 
 872   // convert:
 873   //
 874   //          dom_bool = x {<,<=,>,>=} a
 875   //                           / \
 876   //     proj = {True,False}  /   \ otherproj = {False,True}
 877   //                         /
 878   //        this_bool = x {<,<=} b
 879   //                       / \
 880   //  fail = {True,False} /   \ success = {False,True}
 881   //                     /
 882   //
 883   // (Second test guaranteed canonicalized, first one may not have
 884   // been canonicalized yet)
 885   //
 886   // into:
 887   //
 888   // cond = (x - lo) {<u,<=u,>u,>=u} adjusted_lim
 889   //                       / \
 890   //                 fail /   \ success
 891   //                     /
 892   //
 893 
 894   // Figure out which of the two tests sets the upper bound and which
 895   // sets the lower bound if any.
 896   Node* adjusted_lim = NULL;
 897   if (lo_type != NULL && hi_type != NULL && hi_type->_lo > lo_type->_hi &&
 898       hi_type->_hi == max_jint && lo_type->_lo == min_jint && lo_test != BoolTest::ne) {
 899     assert((dom_bool->_test.is_less() && !proj->_con) ||
 900            (dom_bool->_test.is_greater() && proj->_con), "incorrect test");
 901 
 902     // this_bool = <
 903     //   dom_bool = >= (proj = True) or dom_bool = < (proj = False)
 904     //     x in [a, b[ on the fail (= True) projection, b > a-1 (because of hi_type->_lo > lo_type->_hi test above):
 905     //     lo = a, hi = b, adjusted_lim = b-a, cond = <u
 906     //   dom_bool = > (proj = True) or dom_bool = <= (proj = False)
 907     //     x in ]a, b[ on the fail (= True) projection, b > a:
 908     //     lo = a+1, hi = b, adjusted_lim = b-a-1, cond = <u
 909     // this_bool = <=
 910     //   dom_bool = >= (proj = True) or dom_bool = < (proj = False)
 911     //     x in [a, b] on the fail (= True) projection, b+1 > a-1:
 912     //     lo = a, hi = b, adjusted_lim = b-a+1, cond = <u
 913     //     lo = a, hi = b, adjusted_lim = b-a, cond = <=u doesn't work because b = a - 1 is possible, then b-a = -1
 914     //   dom_bool = > (proj = True) or dom_bool = <= (proj = False)
 915     //     x in ]a, b] on the fail (= True) projection b+1 > a:
 916     //     lo = a+1, hi = b, adjusted_lim = b-a, cond = <u
 917     //     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
 918 
 919     if (hi_test == BoolTest::lt) {
 920       if (lo_test == BoolTest::gt || lo_test == BoolTest::le) {
 921         lo = igvn->transform(new AddINode(lo, igvn->intcon(1)));
 922       }
 923     } else if (hi_test == BoolTest::le) {
 924       if (lo_test == BoolTest::ge || lo_test == BoolTest::lt) {
 925         adjusted_lim = igvn->transform(new SubINode(hi, lo));
 926         adjusted_lim = igvn->transform(new AddINode(adjusted_lim, igvn->intcon(1)));
 927         cond = BoolTest::lt;
 928       } else if (lo_test == BoolTest::gt || lo_test == BoolTest::le) {
 929         adjusted_lim = igvn->transform(new SubINode(hi, lo));
 930         lo = igvn->transform(new AddINode(lo, igvn->intcon(1)));
 931         cond = BoolTest::lt;
 932       } else {
 933         assert(false, "unhandled lo_test: %d", lo_test);
 934         return false;
 935       }
 936     } else {
 937       assert(igvn->_worklist.member(in(1)) && in(1)->Value(igvn) != igvn->type(in(1)), "unhandled hi_test: %d", hi_test);
 938       return false;
 939     }
 940     // this test was canonicalized
 941     assert(this_bool->_test.is_less() && fail->_con, "incorrect test");
 942   } else if (lo_type != NULL && hi_type != NULL && lo_type->_lo > hi_type->_hi &&
 943              lo_type->_hi == max_jint && hi_type->_lo == min_jint && lo_test != BoolTest::ne) {
 944 
 945     // this_bool = <
 946     //   dom_bool = < (proj = True) or dom_bool = >= (proj = False)
 947     //     x in [b, a[ on the fail (= False) projection, a > b-1 (because of lo_type->_lo > hi_type->_hi above):
 948     //     lo = b, hi = a, adjusted_lim = a-b, cond = >=u
 949     //   dom_bool = <= (proj = True) or dom_bool = > (proj = False)
 950     //     x in [b, a] on the fail (= False) projection, a+1 > b-1:
 951     //     lo = b, hi = a, adjusted_lim = a-b+1, cond = >=u
 952     //     lo = b, hi = a, adjusted_lim = a-b, cond = >u doesn't work because a = b - 1 is possible, then b-a = -1
 953     // this_bool = <=
 954     //   dom_bool = < (proj = True) or dom_bool = >= (proj = False)
 955     //     x in ]b, a[ on the fail (= False) projection, a > b:
 956     //     lo = b+1, hi = a, adjusted_lim = a-b-1, cond = >=u
 957     //   dom_bool = <= (proj = True) or dom_bool = > (proj = False)
 958     //     x in ]b, a] on the fail (= False) projection, a+1 > b:
 959     //     lo = b+1, hi = a, adjusted_lim = a-b, cond = >=u
 960     //     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
 961 
 962     swap(lo, hi);
 963     swap(lo_type, hi_type);
 964     swap(lo_test, hi_test);
 965 
 966     assert((dom_bool->_test.is_less() && proj->_con) ||
 967            (dom_bool->_test.is_greater() && !proj->_con), "incorrect test");
 968 
 969     cond = (hi_test == BoolTest::le || hi_test == BoolTest::gt) ? BoolTest::gt : BoolTest::ge;
 970 
 971     if (lo_test == BoolTest::lt) {
 972       if (hi_test == BoolTest::lt || hi_test == BoolTest::ge) {
 973         cond = BoolTest::ge;
 974       } else if (hi_test == BoolTest::le || hi_test == BoolTest::gt) {
 975         adjusted_lim = igvn->transform(new SubINode(hi, lo));
 976         adjusted_lim = igvn->transform(new AddINode(adjusted_lim, igvn->intcon(1)));
 977         cond = BoolTest::ge;
 978       } else {
 979         assert(false, "unhandled hi_test: %d", hi_test);
 980         return false;
 981       }
 982     } else if (lo_test == BoolTest::le) {
 983       if (hi_test == BoolTest::lt || hi_test == BoolTest::ge) {
 984         lo = igvn->transform(new AddINode(lo, igvn->intcon(1)));
 985         cond = BoolTest::ge;
 986       } else if (hi_test == BoolTest::le || hi_test == BoolTest::gt) {
 987         adjusted_lim = igvn->transform(new SubINode(hi, lo));
 988         lo = igvn->transform(new AddINode(lo, igvn->intcon(1)));
 989         cond = BoolTest::ge;
 990       } else {
 991         assert(false, "unhandled hi_test: %d", hi_test);
 992         return false;
 993       }
 994     } else {
 995       assert(igvn->_worklist.member(in(1)) && in(1)->Value(igvn) != igvn->type(in(1)), "unhandled lo_test: %d", lo_test);
 996       return false;
 997     }
 998     // this test was canonicalized
 999     assert(this_bool->_test.is_less() && !fail->_con, "incorrect test");
1000   } else {
1001     const TypeInt* failtype = filtered_int_type(igvn, n, proj);
1002     if (failtype != NULL) {
1003       const TypeInt* type2 = filtered_int_type(igvn, n, fail);
1004       if (type2 != NULL) {
1005         failtype = failtype->join(type2)->is_int();
1006         if (failtype->_lo > failtype->_hi) {
1007           // previous if determines the result of this if so
1008           // replace Bool with constant
1009           igvn->replace_input_of(this, 1, igvn->intcon(success->_con));
1010           return true;
1011         }
1012       }
1013     }
1014     lo = NULL;
1015     hi = NULL;
1016   }
1017 
1018   if (lo && hi) {
1019     Node* hook = new Node(1);
1020     hook->init_req(0, lo); // Add a use to lo to prevent him from dying
1021     // Merge the two compares into a single unsigned compare by building (CmpU (n - lo) (hi - lo))
1022     Node* adjusted_val = igvn->transform(new SubINode(n,  lo));
1023     if (adjusted_lim == NULL) {
1024       adjusted_lim = igvn->transform(new SubINode(hi, lo));
1025     }
1026     hook->destruct(igvn);
1027 
1028     int lo = igvn->type(adjusted_lim)->is_int()->_lo;
1029     if (lo < 0) {
1030       // If range check elimination applies to this comparison, it includes code to protect from overflows that may
1031       // cause the main loop to be skipped entirely. Delay this transformation.
1032       // Example:
1033       // for (int i = 0; i < limit; i++) {
1034       //   if (i < max_jint && i > min_jint) {...
1035       // }
1036       // Comparisons folded as:
1037       // i - min_jint - 1 <u -2
1038       // when RC applies, main loop limit becomes:
1039       // min(limit, max(-2 + min_jint + 1, min_jint))
1040       // = min(limit, min_jint)
1041       // = min_jint
1042       if (!igvn->C->post_loop_opts_phase()) {
1043         if (adjusted_val->outcnt() == 0) {
1044           igvn->remove_dead_node(adjusted_val);
1045         }
1046         if (adjusted_lim->outcnt() == 0) {
1047           igvn->remove_dead_node(adjusted_lim);
1048         }
1049         igvn->C->record_for_post_loop_opts_igvn(this);
1050         return false;
1051       }
1052     }
1053 
1054     Node* newcmp = igvn->transform(new CmpUNode(adjusted_val, adjusted_lim));
1055     Node* newbool = igvn->transform(new BoolNode(newcmp, cond));
1056 
1057     igvn->replace_input_of(dom_iff, 1, igvn->intcon(proj->_con));
1058     igvn->replace_input_of(this, 1, newbool);
1059 
1060     return true;
1061   }
1062   return false;
1063 }
1064 
1065 // Merge the branches that trap for this If and the dominating If into
1066 // a single region that branches to the uncommon trap for the
1067 // dominating If
1068 Node* IfNode::merge_uncommon_traps(ProjNode* proj, ProjNode* success, ProjNode* fail, PhaseIterGVN* igvn) {
1069   Node* res = this;
1070   assert(success->in(0) == this, "bad projection");
1071 
1072   ProjNode* otherproj = proj->other_if_proj();
1073 
1074   CallStaticJavaNode* unc = success->is_uncommon_trap_proj(Deoptimization::Reason_none);
1075   CallStaticJavaNode* dom_unc = otherproj->is_uncommon_trap_proj(Deoptimization::Reason_none);
1076 
1077   if (unc != dom_unc) {
1078     Node* r = new RegionNode(3);
1079 
1080     r->set_req(1, otherproj);
1081     r->set_req(2, success);
1082     r = igvn->transform(r);
1083     assert(r->is_Region(), "can't go away");
1084 
1085     // Make both If trap at the state of the first If: once the CmpI
1086     // nodes are merged, if we trap we don't know which of the CmpI
1087     // nodes would have caused the trap so we have to restart
1088     // execution at the first one
1089     igvn->replace_input_of(dom_unc, 0, r);
1090     igvn->replace_input_of(unc, 0, igvn->C->top());
1091   }
1092   int trap_request = dom_unc->uncommon_trap_request();
1093   Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(trap_request);
1094   Deoptimization::DeoptAction action = Deoptimization::trap_request_action(trap_request);
1095 
1096   int flip_test = 0;
1097   Node* l = NULL;
1098   Node* r = NULL;
1099 
1100   if (success->in(0)->as_If()->range_check_trap_proj(flip_test, l, r) != NULL) {
1101     // If this looks like a range check, change the trap to
1102     // Reason_range_check so the compiler recognizes it as a range
1103     // check and applies the corresponding optimizations
1104     trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_range_check, action);
1105 
1106     improve_address_types(l, r, fail, igvn);
1107 
1108     res = igvn->transform(new RangeCheckNode(in(0), in(1), _prob, _fcnt));
1109   } else if (unc != dom_unc) {
1110     // If we trap we won't know what CmpI would have caused the trap
1111     // so use a special trap reason to mark this pair of CmpI nodes as
1112     // bad candidate for folding. On recompilation we won't fold them
1113     // and we may trap again but this time we'll know what branch
1114     // traps
1115     trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_unstable_fused_if, action);
1116   }
1117   igvn->replace_input_of(dom_unc, TypeFunc::Parms, igvn->intcon(trap_request));
1118   return res;
1119 }
1120 
1121 // If we are turning 2 CmpI nodes into a CmpU that follows the pattern
1122 // of a rangecheck on index i, on 64 bit the compares may be followed
1123 // by memory accesses using i as index. In that case, the CmpU tells
1124 // us something about the values taken by i that can help the compiler
1125 // (see Compile::conv_I2X_index())
1126 void IfNode::improve_address_types(Node* l, Node* r, ProjNode* fail, PhaseIterGVN* igvn) {
1127 #ifdef _LP64
1128   ResourceMark rm;
1129   Node_Stack stack(2);
1130 
1131   assert(r->Opcode() == Op_LoadRange, "unexpected range check");
1132   const TypeInt* array_size = igvn->type(r)->is_int();
1133 
1134   stack.push(l, 0);
1135 
1136   while(stack.size() > 0) {
1137     Node* n = stack.node();
1138     uint start = stack.index();
1139 
1140     uint i = start;
1141     for (; i < n->outcnt(); i++) {
1142       Node* use = n->raw_out(i);
1143       if (stack.size() == 1) {
1144         if (use->Opcode() == Op_ConvI2L) {
1145           const TypeLong* bounds = use->as_Type()->type()->is_long();
1146           if (bounds->_lo <= array_size->_lo && bounds->_hi >= array_size->_hi &&
1147               (bounds->_lo != array_size->_lo || bounds->_hi != array_size->_hi)) {
1148             stack.set_index(i+1);
1149             stack.push(use, 0);
1150             break;
1151           }
1152         }
1153       } else if (use->is_Mem()) {
1154         Node* ctrl = use->in(0);
1155         for (int i = 0; i < 10 && ctrl != NULL && ctrl != fail; i++) {
1156           ctrl = up_one_dom(ctrl);
1157         }
1158         if (ctrl == fail) {
1159           Node* init_n = stack.node_at(1);
1160           assert(init_n->Opcode() == Op_ConvI2L, "unexpected first node");
1161           // Create a new narrow ConvI2L node that is dependent on the range check
1162           Node* new_n = igvn->C->conv_I2X_index(igvn, l, array_size, fail);
1163 
1164           // The type of the ConvI2L may be widen and so the new
1165           // ConvI2L may not be better than an existing ConvI2L
1166           if (new_n != init_n) {
1167             for (uint j = 2; j < stack.size(); j++) {
1168               Node* n = stack.node_at(j);
1169               Node* clone = n->clone();
1170               int rep = clone->replace_edge(init_n, new_n, igvn);
1171               assert(rep > 0, "can't find expected node?");
1172               clone = igvn->transform(clone);
1173               init_n = n;
1174               new_n = clone;
1175             }
1176             igvn->hash_delete(use);
1177             int rep = use->replace_edge(init_n, new_n, igvn);
1178             assert(rep > 0, "can't find expected node?");
1179             igvn->transform(use);
1180             if (init_n->outcnt() == 0) {
1181               igvn->_worklist.push(init_n);
1182             }
1183           }
1184         }
1185       } else if (use->in(0) == NULL && (igvn->type(use)->isa_long() ||
1186                                         igvn->type(use)->isa_ptr())) {
1187         stack.set_index(i+1);
1188         stack.push(use, 0);
1189         break;
1190       }
1191     }
1192     if (i == n->outcnt()) {
1193       stack.pop();
1194     }
1195   }
1196 #endif
1197 }
1198 
1199 bool IfNode::is_cmp_with_loadrange(ProjNode* proj) {
1200   if (in(1) != NULL &&
1201       in(1)->in(1) != NULL &&
1202       in(1)->in(1)->in(2) != NULL) {
1203     Node* other = in(1)->in(1)->in(2);
1204     if (other->Opcode() == Op_LoadRange &&
1205         ((other->in(0) != NULL && other->in(0) == proj) ||
1206          (other->in(0) == NULL &&
1207           other->in(2) != NULL &&
1208           other->in(2)->is_AddP() &&
1209           other->in(2)->in(1) != NULL &&
1210           other->in(2)->in(1)->Opcode() == Op_CastPP &&
1211           other->in(2)->in(1)->in(0) == proj))) {
1212       return true;
1213     }
1214   }
1215   return false;
1216 }
1217 
1218 bool IfNode::is_null_check(ProjNode* proj, PhaseIterGVN* igvn) {
1219   Node* other = in(1)->in(1)->in(2);
1220   if (other->in(MemNode::Address) != NULL &&
1221       proj->in(0)->in(1) != NULL &&
1222       proj->in(0)->in(1)->is_Bool() &&
1223       proj->in(0)->in(1)->in(1) != NULL &&
1224       proj->in(0)->in(1)->in(1)->Opcode() == Op_CmpP &&
1225       proj->in(0)->in(1)->in(1)->in(2) != NULL &&
1226       proj->in(0)->in(1)->in(1)->in(1) == other->in(MemNode::Address)->in(AddPNode::Address)->uncast() &&
1227       igvn->type(proj->in(0)->in(1)->in(1)->in(2)) == TypePtr::NULL_PTR) {
1228     return true;
1229   }
1230   return false;
1231 }
1232 
1233 // Returns true if this IfNode belongs to a flat array check
1234 // and returns the corresponding array in the 'array' parameter.
1235 bool IfNode::is_flat_array_check(PhaseTransform* phase, Node** array) {
1236   Node* bol = in(1);
1237   if (!bol->is_Bool()) {
1238     return false;
1239   }
1240   Node* cmp = bol->in(1);
1241   if (cmp->isa_FlatArrayCheck()) {
1242     if (array != NULL) {
1243       *array = cmp->in(FlatArrayCheckNode::ArrayOrKlass);
1244     }
1245     return true;
1246   }
1247   return false;
1248 }
1249 
1250 // Check that the If that is in between the 2 integer comparisons has
1251 // no side effect
1252 bool IfNode::is_side_effect_free_test(ProjNode* proj, PhaseIterGVN* igvn) {
1253   if (proj == NULL) {
1254     return false;
1255   }
1256   CallStaticJavaNode* unc = proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none);
1257   if (unc != NULL && proj->outcnt() <= 2) {
1258     if (proj->outcnt() == 1 ||
1259         // Allow simple null check from LoadRange
1260         (is_cmp_with_loadrange(proj) && is_null_check(proj, igvn))) {
1261       CallStaticJavaNode* unc = proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none);
1262       CallStaticJavaNode* dom_unc = proj->in(0)->in(0)->as_Proj()->is_uncommon_trap_if_pattern(Deoptimization::Reason_none);
1263       assert(dom_unc != NULL, "is_uncommon_trap_if_pattern returned NULL");
1264 
1265       // reroute_side_effect_free_unc changes the state of this
1266       // uncommon trap to restart execution at the previous
1267       // CmpI. Check that this change in a previous compilation didn't
1268       // cause too many traps.
1269       int trap_request = unc->uncommon_trap_request();
1270       Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(trap_request);
1271 
1272       if (igvn->C->too_many_traps(dom_unc->jvms()->method(), dom_unc->jvms()->bci(), reason)) {
1273         return false;
1274       }
1275 
1276       if (!is_dominator_unc(dom_unc, unc)) {
1277         return false;
1278       }
1279 
1280       return true;
1281     }
1282   }
1283   return false;
1284 }
1285 
1286 // Make the If between the 2 integer comparisons trap at the state of
1287 // the first If: the last CmpI is the one replaced by a CmpU and the
1288 // first CmpI is eliminated, so the test between the 2 CmpI nodes
1289 // won't be guarded by the first CmpI anymore. It can trap in cases
1290 // where the first CmpI would have prevented it from executing: on a
1291 // trap, we need to restart execution at the state of the first CmpI
1292 void IfNode::reroute_side_effect_free_unc(ProjNode* proj, ProjNode* dom_proj, PhaseIterGVN* igvn) {
1293   CallStaticJavaNode* dom_unc = dom_proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none);
1294   ProjNode* otherproj = proj->other_if_proj();
1295   CallStaticJavaNode* unc = proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none);
1296   Node* call_proj = dom_unc->unique_ctrl_out();
1297   Node* halt = call_proj->unique_ctrl_out();
1298 
1299   Node* new_unc = dom_unc->clone();
1300   call_proj = call_proj->clone();
1301   halt = halt->clone();
1302   Node* c = otherproj->clone();
1303 
1304   c = igvn->transform(c);
1305   new_unc->set_req(TypeFunc::Parms, unc->in(TypeFunc::Parms));
1306   new_unc->set_req(0, c);
1307   new_unc = igvn->transform(new_unc);
1308   call_proj->set_req(0, new_unc);
1309   call_proj = igvn->transform(call_proj);
1310   halt->set_req(0, call_proj);
1311   halt = igvn->transform(halt);
1312 
1313   igvn->replace_node(otherproj, igvn->C->top());
1314   igvn->C->root()->add_req(halt);
1315 }
1316 
1317 Node* IfNode::fold_compares(PhaseIterGVN* igvn) {
1318   if (Opcode() != Op_If) return NULL;
1319 
1320   if (cmpi_folds(igvn)) {
1321     Node* ctrl = in(0);
1322     if (is_ctrl_folds(ctrl, igvn) && ctrl->outcnt() == 1) {
1323       // A integer comparison immediately dominated by another integer
1324       // comparison
1325       ProjNode* success = NULL;
1326       ProjNode* fail = NULL;
1327       ProjNode* dom_cmp = ctrl->as_Proj();
1328       if (has_shared_region(dom_cmp, success, fail) &&
1329           // Next call modifies graph so must be last
1330           fold_compares_helper(dom_cmp, success, fail, igvn)) {
1331         return this;
1332       }
1333       if (has_only_uncommon_traps(dom_cmp, success, fail, igvn) &&
1334           // Next call modifies graph so must be last
1335           fold_compares_helper(dom_cmp, success, fail, igvn)) {
1336         return merge_uncommon_traps(dom_cmp, success, fail, igvn);
1337       }
1338       return NULL;
1339     } else if (ctrl->in(0) != NULL &&
1340                ctrl->in(0)->in(0) != NULL) {
1341       ProjNode* success = NULL;
1342       ProjNode* fail = NULL;
1343       Node* dom = ctrl->in(0)->in(0);
1344       ProjNode* dom_cmp = dom->isa_Proj();
1345       ProjNode* other_cmp = ctrl->isa_Proj();
1346 
1347       // Check if it's an integer comparison dominated by another
1348       // integer comparison with another test in between
1349       if (is_ctrl_folds(dom, igvn) &&
1350           has_only_uncommon_traps(dom_cmp, success, fail, igvn) &&
1351           is_side_effect_free_test(other_cmp, igvn) &&
1352           // Next call modifies graph so must be last
1353           fold_compares_helper(dom_cmp, success, fail, igvn)) {
1354         reroute_side_effect_free_unc(other_cmp, dom_cmp, igvn);
1355         return merge_uncommon_traps(dom_cmp, success, fail, igvn);
1356       }
1357     }
1358   }
1359   return NULL;
1360 }
1361 
1362 //------------------------------remove_useless_bool----------------------------
1363 // Check for people making a useless boolean: things like
1364 // if( (x < y ? true : false) ) { ... }
1365 // Replace with if( x < y ) { ... }
1366 static Node *remove_useless_bool(IfNode *iff, PhaseGVN *phase) {
1367   Node *i1 = iff->in(1);
1368   if( !i1->is_Bool() ) return NULL;
1369   BoolNode *bol = i1->as_Bool();
1370 
1371   Node *cmp = bol->in(1);
1372   if( cmp->Opcode() != Op_CmpI ) return NULL;
1373 
1374   // Must be comparing against a bool
1375   const Type *cmp2_t = phase->type( cmp->in(2) );
1376   if( cmp2_t != TypeInt::ZERO &&
1377       cmp2_t != TypeInt::ONE )
1378     return NULL;
1379 
1380   // Find a prior merge point merging the boolean
1381   i1 = cmp->in(1);
1382   if( !i1->is_Phi() ) return NULL;
1383   PhiNode *phi = i1->as_Phi();
1384   if( phase->type( phi ) != TypeInt::BOOL )
1385     return NULL;
1386 
1387   // Check for diamond pattern
1388   int true_path = phi->is_diamond_phi();
1389   if( true_path == 0 ) return NULL;
1390 
1391   // Make sure that iff and the control of the phi are different. This
1392   // should really only happen for dead control flow since it requires
1393   // an illegal cycle.
1394   if (phi->in(0)->in(1)->in(0) == iff) return NULL;
1395 
1396   // phi->region->if_proj->ifnode->bool->cmp
1397   BoolNode *bol2 = phi->in(0)->in(1)->in(0)->in(1)->as_Bool();
1398 
1399   // Now get the 'sense' of the test correct so we can plug in
1400   // either iff2->in(1) or its complement.
1401   int flip = 0;
1402   if( bol->_test._test == BoolTest::ne ) flip = 1-flip;
1403   else if( bol->_test._test != BoolTest::eq ) return NULL;
1404   if( cmp2_t == TypeInt::ZERO ) flip = 1-flip;
1405 
1406   const Type *phi1_t = phase->type( phi->in(1) );
1407   const Type *phi2_t = phase->type( phi->in(2) );
1408   // Check for Phi(0,1) and flip
1409   if( phi1_t == TypeInt::ZERO ) {
1410     if( phi2_t != TypeInt::ONE ) return NULL;
1411     flip = 1-flip;
1412   } else {
1413     // Check for Phi(1,0)
1414     if( phi1_t != TypeInt::ONE  ) return NULL;
1415     if( phi2_t != TypeInt::ZERO ) return NULL;
1416   }
1417   if( true_path == 2 ) {
1418     flip = 1-flip;
1419   }
1420 
1421   Node* new_bol = (flip ? phase->transform( bol2->negate(phase) ) : bol2);
1422   assert(new_bol != iff->in(1), "must make progress");
1423   iff->set_req_X(1, new_bol, phase);
1424   // Intervening diamond probably goes dead
1425   phase->C->set_major_progress();
1426   return iff;
1427 }
1428 
1429 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff);
1430 
1431 struct RangeCheck {
1432   Node* ctl;
1433   jint off;
1434 };
1435 
1436 Node* IfNode::Ideal_common(PhaseGVN *phase, bool can_reshape) {
1437   if (remove_dead_region(phase, can_reshape))  return this;
1438   // No Def-Use info?
1439   if (!can_reshape)  return NULL;
1440 
1441   // Don't bother trying to transform a dead if
1442   if (in(0)->is_top())  return NULL;
1443   // Don't bother trying to transform an if with a dead test
1444   if (in(1)->is_top())  return NULL;
1445   // Another variation of a dead test
1446   if (in(1)->is_Con())  return NULL;
1447   // Another variation of a dead if
1448   if (outcnt() < 2)  return NULL;
1449 
1450   // Canonicalize the test.
1451   Node* idt_if = idealize_test(phase, this);
1452   if (idt_if != NULL)  return idt_if;
1453 
1454   // Try to split the IF
1455   PhaseIterGVN *igvn = phase->is_IterGVN();
1456   Node *s = split_if(this, igvn);
1457   if (s != NULL)  return s;
1458 
1459   return NodeSentinel;
1460 }
1461 
1462 //------------------------------Ideal------------------------------------------
1463 // Return a node which is more "ideal" than the current node.  Strip out
1464 // control copies
1465 Node* IfNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1466   Node* res = Ideal_common(phase, can_reshape);
1467   if (res != NodeSentinel) {
1468     return res;
1469   }
1470 
1471   // Check for people making a useless boolean: things like
1472   // if( (x < y ? true : false) ) { ... }
1473   // Replace with if( x < y ) { ... }
1474   Node* bol2 = remove_useless_bool(this, phase);
1475   if (bol2) return bol2;
1476 
1477   if (in(0) == NULL) return NULL;     // Dead loop?
1478 
1479   PhaseIterGVN* igvn = phase->is_IterGVN();
1480   Node* result = fold_compares(igvn);
1481   if (result != NULL) {
1482     return result;
1483   }
1484 
1485   // Scan for an equivalent test
1486   int dist = 4;               // Cutoff limit for search
1487   if (is_If() && in(1)->is_Bool()) {
1488     Node* cmp = in(1)->in(1);
1489     if (cmp->Opcode() == Op_CmpP &&
1490         cmp->in(2) != NULL && // make sure cmp is not already dead
1491         cmp->in(2)->bottom_type() == TypePtr::NULL_PTR) {
1492       dist = 64;              // Limit for null-pointer scans
1493     }
1494   }
1495 
1496   Node* prev_dom = search_identical(dist);
1497 
1498   if (prev_dom != NULL) {
1499     // Replace dominated IfNode
1500     return dominated_by(prev_dom, igvn);
1501   }
1502 
1503   return simple_subsuming(igvn);
1504 }
1505 
1506 //------------------------------dominated_by-----------------------------------
1507 Node* IfNode::dominated_by(Node* prev_dom, PhaseIterGVN *igvn) {
1508 #ifndef PRODUCT
1509   if (TraceIterativeGVN) {
1510     tty->print("   Removing IfNode: "); this->dump();
1511   }
1512 #endif
1513 
1514   igvn->hash_delete(this);      // Remove self to prevent spurious V-N
1515   Node *idom = in(0);
1516   // Need opcode to decide which way 'this' test goes
1517   int prev_op = prev_dom->Opcode();
1518   Node *top = igvn->C->top(); // Shortcut to top
1519 
1520   // Loop predicates may have depending checks which should not
1521   // be skipped. For example, range check predicate has two checks
1522   // for lower and upper bounds.
1523   ProjNode* unc_proj = proj_out(1 - prev_dom->as_Proj()->_con)->as_Proj();
1524   if (unc_proj->is_uncommon_trap_proj(Deoptimization::Reason_predicate) != NULL ||
1525       unc_proj->is_uncommon_trap_proj(Deoptimization::Reason_profile_predicate) != NULL) {
1526     prev_dom = idom;
1527   }
1528 
1529   // Now walk the current IfNode's projections.
1530   // Loop ends when 'this' has no more uses.
1531   for (DUIterator_Last imin, i = last_outs(imin); i >= imin; --i) {
1532     Node *ifp = last_out(i);     // Get IfTrue/IfFalse
1533     igvn->add_users_to_worklist(ifp);
1534     // Check which projection it is and set target.
1535     // Data-target is either the dominating projection of the same type
1536     // or TOP if the dominating projection is of opposite type.
1537     // Data-target will be used as the new control edge for the non-CFG
1538     // nodes like Casts and Loads.
1539     Node *data_target = (ifp->Opcode() == prev_op) ? prev_dom : top;
1540     // Control-target is just the If's immediate dominator or TOP.
1541     Node *ctrl_target = (ifp->Opcode() == prev_op) ?     idom : top;
1542 
1543     // For each child of an IfTrue/IfFalse projection, reroute.
1544     // Loop ends when projection has no more uses.
1545     for (DUIterator_Last jmin, j = ifp->last_outs(jmin); j >= jmin; --j) {
1546       Node* s = ifp->last_out(j);   // Get child of IfTrue/IfFalse
1547       if (s->depends_only_on_test() && igvn->no_dependent_zero_check(s)) {
1548         // For control producers.
1549         // Do not rewire Div and Mod nodes which could have a zero divisor to avoid skipping their zero check.
1550         igvn->replace_input_of(s, 0, data_target); // Move child to data-target
1551       } else {
1552         // Find the control input matching this def-use edge.
1553         // For Regions it may not be in slot 0.
1554         uint l;
1555         for (l = 0; s->in(l) != ifp; l++) { }
1556         igvn->replace_input_of(s, l, ctrl_target);
1557       }
1558     } // End for each child of a projection
1559 
1560     igvn->remove_dead_node(ifp);
1561   } // End for each IfTrue/IfFalse child of If
1562 
1563   // Kill the IfNode
1564   igvn->remove_dead_node(this);
1565 
1566   // Must return either the original node (now dead) or a new node
1567   // (Do not return a top here, since that would break the uniqueness of top.)
1568   return new ConINode(TypeInt::ZERO);
1569 }
1570 
1571 Node* IfNode::search_identical(int dist) {
1572   // Setup to scan up the CFG looking for a dominating test
1573   Node* dom = in(0);
1574   Node* prev_dom = this;
1575   int op = Opcode();
1576   // Search up the dominator tree for an If with an identical test
1577   while (dom->Opcode() != op    ||  // Not same opcode?
1578          dom->in(1)    != in(1) ||  // Not same input 1?
1579          prev_dom->in(0) != dom) {  // One path of test does not dominate?
1580     if (dist < 0) return NULL;
1581 
1582     dist--;
1583     prev_dom = dom;
1584     dom = up_one_dom(dom);
1585     if (!dom) return NULL;
1586   }
1587 
1588   // Check that we did not follow a loop back to ourselves
1589   if (this == dom) {
1590     return NULL;
1591   }
1592 
1593 #ifndef PRODUCT
1594   if (dist > 2) { // Add to count of NULL checks elided
1595     explicit_null_checks_elided++;
1596   }
1597 #endif
1598 
1599   return prev_dom;
1600 }
1601 
1602 
1603 static int subsuming_bool_test_encode(Node*);
1604 
1605 // Check if dominating test is subsuming 'this' one.
1606 //
1607 //              cmp
1608 //              / \
1609 //     (r1)  bool  \
1610 //            /    bool (r2)
1611 //    (dom) if       \
1612 //            \       )
1613 //    (pre)  if[TF]  /
1614 //               \  /
1615 //                if (this)
1616 //   \r1
1617 //  r2\  eqT  eqF  neT  neF  ltT  ltF  leT  leF  gtT  gtF  geT  geF
1618 //  eq    t    f    f    t    f    -    -    f    f    -    -    f
1619 //  ne    f    t    t    f    t    -    -    t    t    -    -    t
1620 //  lt    f    -    -    f    t    f    -    f    f    -    f    t
1621 //  le    t    -    -    t    t    -    t    f    f    t    -    t
1622 //  gt    f    -    -    f    f    -    f    t    t    f    -    f
1623 //  ge    t    -    -    t    f    t    -    t    t    -    t    f
1624 //
1625 Node* IfNode::simple_subsuming(PhaseIterGVN* igvn) {
1626   // Table encoding: N/A (na), True-branch (tb), False-branch (fb).
1627   static enum { na, tb, fb } s_short_circuit_map[6][12] = {
1628   /*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*/
1629   /*eq*/{ tb,  fb,  fb,  tb,  fb,  na,  na,  fb,  fb,  na,  na,  fb },
1630   /*ne*/{ fb,  tb,  tb,  fb,  tb,  na,  na,  tb,  tb,  na,  na,  tb },
1631   /*lt*/{ fb,  na,  na,  fb,  tb,  fb,  na,  fb,  fb,  na,  fb,  tb },
1632   /*le*/{ tb,  na,  na,  tb,  tb,  na,  tb,  fb,  fb,  tb,  na,  tb },
1633   /*gt*/{ fb,  na,  na,  fb,  fb,  na,  fb,  tb,  tb,  fb,  na,  fb },
1634   /*ge*/{ tb,  na,  na,  tb,  fb,  tb,  na,  tb,  tb,  na,  tb,  fb }};
1635 
1636   Node* pre = in(0);
1637   if (!pre->is_IfTrue() && !pre->is_IfFalse()) {
1638     return NULL;
1639   }
1640   Node* dom = pre->in(0);
1641   if (!dom->is_If()) {
1642     return NULL;
1643   }
1644   Node* bol = in(1);
1645   if (!bol->is_Bool()) {
1646     return NULL;
1647   }
1648   Node* cmp = in(1)->in(1);
1649   if (!cmp->is_Cmp()) {
1650     return NULL;
1651   }
1652 
1653   if (!dom->in(1)->is_Bool()) {
1654     return NULL;
1655   }
1656   if (dom->in(1)->in(1) != cmp) {  // Not same cond?
1657     return NULL;
1658   }
1659 
1660   int drel = subsuming_bool_test_encode(dom->in(1));
1661   int trel = subsuming_bool_test_encode(bol);
1662   int bout = pre->is_IfFalse() ? 1 : 0;
1663 
1664   if (drel < 0 || trel < 0) {
1665     return NULL;
1666   }
1667   int br = s_short_circuit_map[trel][2*drel+bout];
1668   if (br == na) {
1669     return NULL;
1670   }
1671 #ifndef PRODUCT
1672   if (TraceIterativeGVN) {
1673     tty->print("   Subsumed IfNode: "); dump();
1674   }
1675 #endif
1676   // Replace condition with constant True(1)/False(0).
1677   bool is_always_true = br == tb;
1678   set_req(1, igvn->intcon(is_always_true ? 1 : 0));
1679 
1680   // Update any data dependencies to the directly dominating test. This subsumed test is not immediately removed by igvn
1681   // and therefore subsequent optimizations might miss these data dependencies otherwise. There might be a dead loop
1682   // ('always_taken_proj' == 'pre') that is cleaned up later. Skip this case to make the iterator work properly.
1683   Node* always_taken_proj = proj_out(is_always_true);
1684   if (always_taken_proj != pre) {
1685     for (DUIterator_Fast imax, i = always_taken_proj->fast_outs(imax); i < imax; i++) {
1686       Node* u = always_taken_proj->fast_out(i);
1687       if (!u->is_CFG()) {
1688         igvn->replace_input_of(u, 0, pre);
1689         --i;
1690         --imax;
1691       }
1692     }
1693   }
1694 
1695   if (bol->outcnt() == 0) {
1696     igvn->remove_dead_node(bol);    // Kill the BoolNode.
1697   }
1698   return this;
1699 }
1700 
1701 // Map BoolTest to local table encoding. The BoolTest (e)numerals
1702 //   { eq = 0, ne = 4, le = 5, ge = 7, lt = 3, gt = 1 }
1703 // are mapped to table indices, while the remaining (e)numerals in BoolTest
1704 //   { overflow = 2, no_overflow = 6, never = 8, illegal = 9 }
1705 // are ignored (these are not modeled in the table).
1706 //
1707 static int subsuming_bool_test_encode(Node* node) {
1708   precond(node->is_Bool());
1709   BoolTest::mask x = node->as_Bool()->_test._test;
1710   switch (x) {
1711     case BoolTest::eq: return 0;
1712     case BoolTest::ne: return 1;
1713     case BoolTest::lt: return 2;
1714     case BoolTest::le: return 3;
1715     case BoolTest::gt: return 4;
1716     case BoolTest::ge: return 5;
1717     case BoolTest::overflow:
1718     case BoolTest::no_overflow:
1719     case BoolTest::never:
1720     case BoolTest::illegal:
1721     default:
1722       return -1;
1723   }
1724 }
1725 
1726 //------------------------------Identity---------------------------------------
1727 // If the test is constant & we match, then we are the input Control
1728 Node* IfProjNode::Identity(PhaseGVN* phase) {
1729   // Can only optimize if cannot go the other way
1730   const TypeTuple *t = phase->type(in(0))->is_tuple();
1731   if (t == TypeTuple::IFNEITHER || (always_taken(t) &&
1732        // During parsing (GVN) we don't remove dead code aggressively.
1733        // Cut off dead branch and let PhaseRemoveUseless take care of it.
1734       (!phase->is_IterGVN() ||
1735        // During IGVN, first wait for the dead branch to be killed.
1736        // Otherwise, the IfNode's control will have two control uses (the IfNode
1737        // that doesn't go away because it still has uses and this branch of the
1738        // If) which breaks other optimizations. Node::has_special_unique_user()
1739        // will cause this node to be reprocessed once the dead branch is killed.
1740        in(0)->outcnt() == 1))) {
1741     // IfNode control
1742     if (in(0)->is_BaseCountedLoopEnd()) {
1743       // CountedLoopEndNode may be eliminated by if subsuming, replace CountedLoopNode with LoopNode to
1744       // avoid mismatching between CountedLoopNode and CountedLoopEndNode in the following optimization.
1745       Node* head = unique_ctrl_out_or_null();
1746       if (head != NULL && head->is_BaseCountedLoop() && head->in(LoopNode::LoopBackControl) == this) {
1747         Node* new_head = new LoopNode(head->in(LoopNode::EntryControl), this);
1748         phase->is_IterGVN()->register_new_node_with_optimizer(new_head);
1749         phase->is_IterGVN()->replace_node(head, new_head);
1750       }
1751     }
1752     return in(0)->in(0);
1753   }
1754   // no progress
1755   return this;
1756 }
1757 
1758 #ifndef PRODUCT
1759 //------------------------------dump_spec--------------------------------------
1760 void IfNode::dump_spec(outputStream *st) const {
1761   st->print("P=%f, C=%f",_prob,_fcnt);
1762 }
1763 #endif
1764 
1765 //------------------------------idealize_test----------------------------------
1766 // Try to canonicalize tests better.  Peek at the Cmp/Bool/If sequence and
1767 // come up with a canonical sequence.  Bools getting 'eq', 'gt' and 'ge' forms
1768 // converted to 'ne', 'le' and 'lt' forms.  IfTrue/IfFalse get swapped as
1769 // needed.
1770 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff) {
1771   assert(iff->in(0) != NULL, "If must be live");
1772 
1773   if (iff->outcnt() != 2)  return NULL; // Malformed projections.
1774   Node* old_if_f = iff->proj_out(false);
1775   Node* old_if_t = iff->proj_out(true);
1776 
1777   // CountedLoopEnds want the back-control test to be TRUE, regardless of
1778   // whether they are testing a 'gt' or 'lt' condition.  The 'gt' condition
1779   // happens in count-down loops
1780   if (iff->is_BaseCountedLoopEnd())  return NULL;
1781   if (!iff->in(1)->is_Bool())  return NULL; // Happens for partially optimized IF tests
1782   BoolNode *b = iff->in(1)->as_Bool();
1783   BoolTest bt = b->_test;
1784   // Test already in good order?
1785   if( bt.is_canonical() )
1786     return NULL;
1787 
1788   // Flip test to be canonical.  Requires flipping the IfFalse/IfTrue and
1789   // cloning the IfNode.
1790   Node* new_b = phase->transform( new BoolNode(b->in(1), bt.negate()) );
1791   if( !new_b->is_Bool() ) return NULL;
1792   b = new_b->as_Bool();
1793 
1794   PhaseIterGVN *igvn = phase->is_IterGVN();
1795   assert( igvn, "Test is not canonical in parser?" );
1796 
1797   // The IF node never really changes, but it needs to be cloned
1798   iff = iff->clone()->as_If();
1799   iff->set_req(1, b);
1800   iff->_prob = 1.0-iff->_prob;
1801 
1802   Node *prior = igvn->hash_find_insert(iff);
1803   if( prior ) {
1804     igvn->remove_dead_node(iff);
1805     iff = (IfNode*)prior;
1806   } else {
1807     // Cannot call transform on it just yet
1808     igvn->set_type_bottom(iff);
1809   }
1810   igvn->_worklist.push(iff);
1811 
1812   // Now handle projections.  Cloning not required.
1813   Node* new_if_f = (Node*)(new IfFalseNode( iff ));
1814   Node* new_if_t = (Node*)(new IfTrueNode ( iff ));
1815 
1816   igvn->register_new_node_with_optimizer(new_if_f);
1817   igvn->register_new_node_with_optimizer(new_if_t);
1818   // Flip test, so flip trailing control
1819   igvn->replace_node(old_if_f, new_if_t);
1820   igvn->replace_node(old_if_t, new_if_f);
1821 
1822   // Progress
1823   return iff;
1824 }
1825 
1826 Node* RangeCheckNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1827   Node* res = Ideal_common(phase, can_reshape);
1828   if (res != NodeSentinel) {
1829     return res;
1830   }
1831 
1832   PhaseIterGVN *igvn = phase->is_IterGVN();
1833   // Setup to scan up the CFG looking for a dominating test
1834   Node* prev_dom = this;
1835 
1836   // Check for range-check vs other kinds of tests
1837   Node* index1;
1838   Node* range1;
1839   jint offset1;
1840   int flip1 = is_range_check(range1, index1, offset1);
1841   if (flip1) {
1842     Node* dom = in(0);
1843     // Try to remove extra range checks.  All 'up_one_dom' gives up at merges
1844     // so all checks we inspect post-dominate the top-most check we find.
1845     // If we are going to fail the current check and we reach the top check
1846     // then we are guaranteed to fail, so just start interpreting there.
1847     // We 'expand' the top 3 range checks to include all post-dominating
1848     // checks.
1849 
1850     // The top 3 range checks seen
1851     const int NRC = 3;
1852     RangeCheck prev_checks[NRC];
1853     int nb_checks = 0;
1854 
1855     // Low and high offsets seen so far
1856     jint off_lo = offset1;
1857     jint off_hi = offset1;
1858 
1859     bool found_immediate_dominator = false;
1860 
1861     // Scan for the top checks and collect range of offsets
1862     for (int dist = 0; dist < 999; dist++) { // Range-Check scan limit
1863       if (dom->Opcode() == Op_RangeCheck &&  // Not same opcode?
1864           prev_dom->in(0) == dom) { // One path of test does dominate?
1865         if (dom == this) return NULL; // dead loop
1866         // See if this is a range check
1867         Node* index2;
1868         Node* range2;
1869         jint offset2;
1870         int flip2 = dom->as_RangeCheck()->is_range_check(range2, index2, offset2);
1871         // See if this is a _matching_ range check, checking against
1872         // the same array bounds.
1873         if (flip2 == flip1 && range2 == range1 && index2 == index1 &&
1874             dom->outcnt() == 2) {
1875           if (nb_checks == 0 && dom->in(1) == in(1)) {
1876             // Found an immediately dominating test at the same offset.
1877             // This kind of back-to-back test can be eliminated locally,
1878             // and there is no need to search further for dominating tests.
1879             assert(offset2 == offset1, "Same test but different offsets");
1880             found_immediate_dominator = true;
1881             break;
1882           }
1883           // Gather expanded bounds
1884           off_lo = MIN2(off_lo,offset2);
1885           off_hi = MAX2(off_hi,offset2);
1886           // Record top NRC range checks
1887           prev_checks[nb_checks%NRC].ctl = prev_dom;
1888           prev_checks[nb_checks%NRC].off = offset2;
1889           nb_checks++;
1890         }
1891       }
1892       prev_dom = dom;
1893       dom = up_one_dom(dom);
1894       if (!dom) break;
1895     }
1896 
1897     if (!found_immediate_dominator) {
1898       // Attempt to widen the dominating range check to cover some later
1899       // ones.  Since range checks "fail" by uncommon-trapping to the
1900       // interpreter, widening a check can make us speculatively enter
1901       // the interpreter.  If we see range-check deopt's, do not widen!
1902       if (!phase->C->allow_range_check_smearing())  return NULL;
1903 
1904       // Didn't find prior covering check, so cannot remove anything.
1905       if (nb_checks == 0) {
1906         return NULL;
1907       }
1908       // Constant indices only need to check the upper bound.
1909       // Non-constant indices must check both low and high.
1910       int chk0 = (nb_checks - 1) % NRC;
1911       if (index1) {
1912         if (nb_checks == 1) {
1913           return NULL;
1914         } else {
1915           // If the top range check's constant is the min or max of
1916           // all constants we widen the next one to cover the whole
1917           // range of constants.
1918           RangeCheck rc0 = prev_checks[chk0];
1919           int chk1 = (nb_checks - 2) % NRC;
1920           RangeCheck rc1 = prev_checks[chk1];
1921           if (rc0.off == off_lo) {
1922             adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn);
1923             prev_dom = rc1.ctl;
1924           } else if (rc0.off == off_hi) {
1925             adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn);
1926             prev_dom = rc1.ctl;
1927           } else {
1928             // If the top test's constant is not the min or max of all
1929             // constants, we need 3 range checks. We must leave the
1930             // top test unchanged because widening it would allow the
1931             // accesses it protects to successfully read/write out of
1932             // bounds.
1933             if (nb_checks == 2) {
1934               return NULL;
1935             }
1936             int chk2 = (nb_checks - 3) % NRC;
1937             RangeCheck rc2 = prev_checks[chk2];
1938             // The top range check a+i covers interval: -a <= i < length-a
1939             // The second range check b+i covers interval: -b <= i < length-b
1940             if (rc1.off <= rc0.off) {
1941               // if b <= a, we change the second range check to:
1942               // -min_of_all_constants <= i < length-min_of_all_constants
1943               // Together top and second range checks now cover:
1944               // -min_of_all_constants <= i < length-a
1945               // which is more restrictive than -b <= i < length-b:
1946               // -b <= -min_of_all_constants <= i < length-a <= length-b
1947               // The third check is then changed to:
1948               // -max_of_all_constants <= i < length-max_of_all_constants
1949               // so 2nd and 3rd checks restrict allowed values of i to:
1950               // -min_of_all_constants <= i < length-max_of_all_constants
1951               adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn);
1952               adjust_check(rc2.ctl, range1, index1, flip1, off_hi, igvn);
1953             } else {
1954               // if b > a, we change the second range check to:
1955               // -max_of_all_constants <= i < length-max_of_all_constants
1956               // Together top and second range checks now cover:
1957               // -a <= i < length-max_of_all_constants
1958               // which is more restrictive than -b <= i < length-b:
1959               // -b < -a <= i < length-max_of_all_constants <= length-b
1960               // The third check is then changed to:
1961               // -max_of_all_constants <= i < length-max_of_all_constants
1962               // so 2nd and 3rd checks restrict allowed values of i to:
1963               // -min_of_all_constants <= i < length-max_of_all_constants
1964               adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn);
1965               adjust_check(rc2.ctl, range1, index1, flip1, off_lo, igvn);
1966             }
1967             prev_dom = rc2.ctl;
1968           }
1969         }
1970       } else {
1971         RangeCheck rc0 = prev_checks[chk0];
1972         // 'Widen' the offset of the 1st and only covering check
1973         adjust_check(rc0.ctl, range1, index1, flip1, off_hi, igvn);
1974         // Test is now covered by prior checks, dominate it out
1975         prev_dom = rc0.ctl;
1976       }
1977     }
1978   } else {
1979     prev_dom = search_identical(4);
1980 
1981     if (prev_dom == NULL) {
1982       return NULL;
1983     }
1984   }
1985 
1986   // Replace dominated IfNode
1987   return dominated_by(prev_dom, igvn);
1988 }