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 // Check that the If that is in between the 2 integer comparisons has
1233 // no side effect
1234 bool IfNode::is_side_effect_free_test(ProjNode* proj, PhaseIterGVN* igvn) {
1235   if (proj == NULL) {
1236     return false;
1237   }
1238   CallStaticJavaNode* unc = proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none);
1239   if (unc != NULL && proj->outcnt() <= 2) {
1240     if (proj->outcnt() == 1 ||
1241         // Allow simple null check from LoadRange
1242         (is_cmp_with_loadrange(proj) && is_null_check(proj, igvn))) {
1243       CallStaticJavaNode* unc = proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none);
1244       CallStaticJavaNode* dom_unc = proj->in(0)->in(0)->as_Proj()->is_uncommon_trap_if_pattern(Deoptimization::Reason_none);
1245       assert(dom_unc != NULL, "is_uncommon_trap_if_pattern returned NULL");
1246 
1247       // reroute_side_effect_free_unc changes the state of this
1248       // uncommon trap to restart execution at the previous
1249       // CmpI. Check that this change in a previous compilation didn't
1250       // cause too many traps.
1251       int trap_request = unc->uncommon_trap_request();
1252       Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(trap_request);
1253 
1254       if (igvn->C->too_many_traps(dom_unc->jvms()->method(), dom_unc->jvms()->bci(), reason)) {
1255         return false;
1256       }
1257 
1258       if (!is_dominator_unc(dom_unc, unc)) {
1259         return false;
1260       }
1261 
1262       return true;
1263     }
1264   }
1265   return false;
1266 }
1267 
1268 // Make the If between the 2 integer comparisons trap at the state of
1269 // the first If: the last CmpI is the one replaced by a CmpU and the
1270 // first CmpI is eliminated, so the test between the 2 CmpI nodes
1271 // won't be guarded by the first CmpI anymore. It can trap in cases
1272 // where the first CmpI would have prevented it from executing: on a
1273 // trap, we need to restart execution at the state of the first CmpI
1274 void IfNode::reroute_side_effect_free_unc(ProjNode* proj, ProjNode* dom_proj, PhaseIterGVN* igvn) {
1275   CallStaticJavaNode* dom_unc = dom_proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none);
1276   ProjNode* otherproj = proj->other_if_proj();
1277   CallStaticJavaNode* unc = proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none);
1278   Node* call_proj = dom_unc->unique_ctrl_out();
1279   Node* halt = call_proj->unique_ctrl_out();
1280 
1281   Node* new_unc = dom_unc->clone();
1282   call_proj = call_proj->clone();
1283   halt = halt->clone();
1284   Node* c = otherproj->clone();
1285 
1286   c = igvn->transform(c);
1287   new_unc->set_req(TypeFunc::Parms, unc->in(TypeFunc::Parms));
1288   new_unc->set_req(0, c);
1289   new_unc = igvn->transform(new_unc);
1290   call_proj->set_req(0, new_unc);
1291   call_proj = igvn->transform(call_proj);
1292   halt->set_req(0, call_proj);
1293   halt = igvn->transform(halt);
1294 
1295   igvn->replace_node(otherproj, igvn->C->top());
1296   igvn->C->root()->add_req(halt);
1297 }
1298 
1299 Node* IfNode::fold_compares(PhaseIterGVN* igvn) {
1300   if (Opcode() != Op_If) return NULL;
1301 
1302   if (cmpi_folds(igvn)) {
1303     Node* ctrl = in(0);
1304     if (is_ctrl_folds(ctrl, igvn) && ctrl->outcnt() == 1) {
1305       // A integer comparison immediately dominated by another integer
1306       // comparison
1307       ProjNode* success = NULL;
1308       ProjNode* fail = NULL;
1309       ProjNode* dom_cmp = ctrl->as_Proj();
1310       if (has_shared_region(dom_cmp, success, fail) &&
1311           // Next call modifies graph so must be last
1312           fold_compares_helper(dom_cmp, success, fail, igvn)) {
1313         return this;
1314       }
1315       if (has_only_uncommon_traps(dom_cmp, success, fail, igvn) &&
1316           // Next call modifies graph so must be last
1317           fold_compares_helper(dom_cmp, success, fail, igvn)) {
1318         return merge_uncommon_traps(dom_cmp, success, fail, igvn);
1319       }
1320       return NULL;
1321     } else if (ctrl->in(0) != NULL &&
1322                ctrl->in(0)->in(0) != NULL) {
1323       ProjNode* success = NULL;
1324       ProjNode* fail = NULL;
1325       Node* dom = ctrl->in(0)->in(0);
1326       ProjNode* dom_cmp = dom->isa_Proj();
1327       ProjNode* other_cmp = ctrl->isa_Proj();
1328 
1329       // Check if it's an integer comparison dominated by another
1330       // integer comparison with another test in between
1331       if (is_ctrl_folds(dom, igvn) &&
1332           has_only_uncommon_traps(dom_cmp, success, fail, igvn) &&
1333           is_side_effect_free_test(other_cmp, igvn) &&
1334           // Next call modifies graph so must be last
1335           fold_compares_helper(dom_cmp, success, fail, igvn)) {
1336         reroute_side_effect_free_unc(other_cmp, dom_cmp, igvn);
1337         return merge_uncommon_traps(dom_cmp, success, fail, igvn);
1338       }
1339     }
1340   }
1341   return NULL;
1342 }
1343 
1344 //------------------------------remove_useless_bool----------------------------
1345 // Check for people making a useless boolean: things like
1346 // if( (x < y ? true : false) ) { ... }
1347 // Replace with if( x < y ) { ... }
1348 static Node *remove_useless_bool(IfNode *iff, PhaseGVN *phase) {
1349   Node *i1 = iff->in(1);
1350   if( !i1->is_Bool() ) return NULL;
1351   BoolNode *bol = i1->as_Bool();
1352 
1353   Node *cmp = bol->in(1);
1354   if( cmp->Opcode() != Op_CmpI ) return NULL;
1355 
1356   // Must be comparing against a bool
1357   const Type *cmp2_t = phase->type( cmp->in(2) );
1358   if( cmp2_t != TypeInt::ZERO &&
1359       cmp2_t != TypeInt::ONE )
1360     return NULL;
1361 
1362   // Find a prior merge point merging the boolean
1363   i1 = cmp->in(1);
1364   if( !i1->is_Phi() ) return NULL;
1365   PhiNode *phi = i1->as_Phi();
1366   if( phase->type( phi ) != TypeInt::BOOL )
1367     return NULL;
1368 
1369   // Check for diamond pattern
1370   int true_path = phi->is_diamond_phi();
1371   if( true_path == 0 ) return NULL;
1372 
1373   // Make sure that iff and the control of the phi are different. This
1374   // should really only happen for dead control flow since it requires
1375   // an illegal cycle.
1376   if (phi->in(0)->in(1)->in(0) == iff) return NULL;
1377 
1378   // phi->region->if_proj->ifnode->bool->cmp
1379   BoolNode *bol2 = phi->in(0)->in(1)->in(0)->in(1)->as_Bool();
1380 
1381   // Now get the 'sense' of the test correct so we can plug in
1382   // either iff2->in(1) or its complement.
1383   int flip = 0;
1384   if( bol->_test._test == BoolTest::ne ) flip = 1-flip;
1385   else if( bol->_test._test != BoolTest::eq ) return NULL;
1386   if( cmp2_t == TypeInt::ZERO ) flip = 1-flip;
1387 
1388   const Type *phi1_t = phase->type( phi->in(1) );
1389   const Type *phi2_t = phase->type( phi->in(2) );
1390   // Check for Phi(0,1) and flip
1391   if( phi1_t == TypeInt::ZERO ) {
1392     if( phi2_t != TypeInt::ONE ) return NULL;
1393     flip = 1-flip;
1394   } else {
1395     // Check for Phi(1,0)
1396     if( phi1_t != TypeInt::ONE  ) return NULL;
1397     if( phi2_t != TypeInt::ZERO ) return NULL;
1398   }
1399   if( true_path == 2 ) {
1400     flip = 1-flip;
1401   }
1402 
1403   Node* new_bol = (flip ? phase->transform( bol2->negate(phase) ) : bol2);
1404   assert(new_bol != iff->in(1), "must make progress");
1405   iff->set_req_X(1, new_bol, phase);
1406   // Intervening diamond probably goes dead
1407   phase->C->set_major_progress();
1408   return iff;
1409 }
1410 
1411 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff);
1412 
1413 struct RangeCheck {
1414   Node* ctl;
1415   jint off;
1416 };
1417 
1418 Node* IfNode::Ideal_common(PhaseGVN *phase, bool can_reshape) {
1419   if (remove_dead_region(phase, can_reshape))  return this;
1420   // No Def-Use info?
1421   if (!can_reshape)  return NULL;
1422 
1423   // Don't bother trying to transform a dead if
1424   if (in(0)->is_top())  return NULL;
1425   // Don't bother trying to transform an if with a dead test
1426   if (in(1)->is_top())  return NULL;
1427   // Another variation of a dead test
1428   if (in(1)->is_Con())  return NULL;
1429   // Another variation of a dead if
1430   if (outcnt() < 2)  return NULL;
1431 
1432   // Canonicalize the test.
1433   Node* idt_if = idealize_test(phase, this);
1434   if (idt_if != NULL)  return idt_if;
1435 
1436   // Try to split the IF
1437   PhaseIterGVN *igvn = phase->is_IterGVN();
1438   Node *s = split_if(this, igvn);
1439   if (s != NULL)  return s;
1440 
1441   return NodeSentinel;
1442 }
1443 
1444 //------------------------------Ideal------------------------------------------
1445 // Return a node which is more "ideal" than the current node.  Strip out
1446 // control copies
1447 Node* IfNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1448   Node* res = Ideal_common(phase, can_reshape);
1449   if (res != NodeSentinel) {
1450     return res;
1451   }
1452 
1453   // Check for people making a useless boolean: things like
1454   // if( (x < y ? true : false) ) { ... }
1455   // Replace with if( x < y ) { ... }
1456   Node* bol2 = remove_useless_bool(this, phase);
1457   if (bol2) return bol2;
1458 
1459   if (in(0) == NULL) return NULL;     // Dead loop?
1460 
1461   PhaseIterGVN* igvn = phase->is_IterGVN();
1462   Node* result = fold_compares(igvn);
1463   if (result != NULL) {
1464     return result;
1465   }
1466 
1467   // Scan for an equivalent test
1468   int dist = 4;               // Cutoff limit for search
1469   if (is_If() && in(1)->is_Bool()) {
1470     Node* cmp = in(1)->in(1);
1471     if (cmp->Opcode() == Op_CmpP &&
1472         cmp->in(2) != NULL && // make sure cmp is not already dead
1473         cmp->in(2)->bottom_type() == TypePtr::NULL_PTR) {
1474       dist = 64;              // Limit for null-pointer scans
1475     }
1476   }
1477 
1478   Node* prev_dom = search_identical(dist);
1479 
1480   if (prev_dom != NULL) {
1481     // Replace dominated IfNode
1482     return dominated_by(prev_dom, igvn);
1483   }
1484 
1485   return simple_subsuming(igvn);
1486 }
1487 
1488 //------------------------------dominated_by-----------------------------------
1489 Node* IfNode::dominated_by(Node* prev_dom, PhaseIterGVN *igvn) {
1490 #ifndef PRODUCT
1491   if (TraceIterativeGVN) {
1492     tty->print("   Removing IfNode: "); this->dump();
1493   }
1494 #endif
1495 
1496   igvn->hash_delete(this);      // Remove self to prevent spurious V-N
1497   Node *idom = in(0);
1498   // Need opcode to decide which way 'this' test goes
1499   int prev_op = prev_dom->Opcode();
1500   Node *top = igvn->C->top(); // Shortcut to top
1501 
1502   // Loop predicates may have depending checks which should not
1503   // be skipped. For example, range check predicate has two checks
1504   // for lower and upper bounds.
1505   ProjNode* unc_proj = proj_out(1 - prev_dom->as_Proj()->_con)->as_Proj();
1506   if (unc_proj->is_uncommon_trap_proj(Deoptimization::Reason_predicate) != NULL ||
1507       unc_proj->is_uncommon_trap_proj(Deoptimization::Reason_profile_predicate) != NULL) {
1508     prev_dom = idom;
1509   }
1510 
1511   // Now walk the current IfNode's projections.
1512   // Loop ends when 'this' has no more uses.
1513   for (DUIterator_Last imin, i = last_outs(imin); i >= imin; --i) {
1514     Node *ifp = last_out(i);     // Get IfTrue/IfFalse
1515     igvn->add_users_to_worklist(ifp);
1516     // Check which projection it is and set target.
1517     // Data-target is either the dominating projection of the same type
1518     // or TOP if the dominating projection is of opposite type.
1519     // Data-target will be used as the new control edge for the non-CFG
1520     // nodes like Casts and Loads.
1521     Node *data_target = (ifp->Opcode() == prev_op) ? prev_dom : top;
1522     // Control-target is just the If's immediate dominator or TOP.
1523     Node *ctrl_target = (ifp->Opcode() == prev_op) ?     idom : top;
1524 
1525     // For each child of an IfTrue/IfFalse projection, reroute.
1526     // Loop ends when projection has no more uses.
1527     for (DUIterator_Last jmin, j = ifp->last_outs(jmin); j >= jmin; --j) {
1528       Node* s = ifp->last_out(j);   // Get child of IfTrue/IfFalse
1529       if (s->depends_only_on_test() && igvn->no_dependent_zero_check(s)) {
1530         // For control producers.
1531         // Do not rewire Div and Mod nodes which could have a zero divisor to avoid skipping their zero check.
1532         igvn->replace_input_of(s, 0, data_target); // Move child to data-target
1533       } else {
1534         // Find the control input matching this def-use edge.
1535         // For Regions it may not be in slot 0.
1536         uint l;
1537         for (l = 0; s->in(l) != ifp; l++) { }
1538         igvn->replace_input_of(s, l, ctrl_target);
1539       }
1540     } // End for each child of a projection
1541 
1542     igvn->remove_dead_node(ifp);
1543   } // End for each IfTrue/IfFalse child of If
1544 
1545   // Kill the IfNode
1546   igvn->remove_dead_node(this);
1547 
1548   // Must return either the original node (now dead) or a new node
1549   // (Do not return a top here, since that would break the uniqueness of top.)
1550   return new ConINode(TypeInt::ZERO);
1551 }
1552 
1553 Node* IfNode::search_identical(int dist) {
1554   // Setup to scan up the CFG looking for a dominating test
1555   Node* dom = in(0);
1556   Node* prev_dom = this;
1557   int op = Opcode();
1558   // Search up the dominator tree for an If with an identical test
1559   while (dom->Opcode() != op    ||  // Not same opcode?
1560          dom->in(1)    != in(1) ||  // Not same input 1?
1561          prev_dom->in(0) != dom) {  // One path of test does not dominate?
1562     if (dist < 0) return NULL;
1563 
1564     dist--;
1565     prev_dom = dom;
1566     dom = up_one_dom(dom);
1567     if (!dom) return NULL;
1568   }
1569 
1570   // Check that we did not follow a loop back to ourselves
1571   if (this == dom) {
1572     return NULL;
1573   }
1574 
1575 #ifndef PRODUCT
1576   if (dist > 2) { // Add to count of NULL checks elided
1577     explicit_null_checks_elided++;
1578   }
1579 #endif
1580 
1581   return prev_dom;
1582 }
1583 
1584 
1585 static int subsuming_bool_test_encode(Node*);
1586 
1587 // Check if dominating test is subsuming 'this' one.
1588 //
1589 //              cmp
1590 //              / \
1591 //     (r1)  bool  \
1592 //            /    bool (r2)
1593 //    (dom) if       \
1594 //            \       )
1595 //    (pre)  if[TF]  /
1596 //               \  /
1597 //                if (this)
1598 //   \r1
1599 //  r2\  eqT  eqF  neT  neF  ltT  ltF  leT  leF  gtT  gtF  geT  geF
1600 //  eq    t    f    f    t    f    -    -    f    f    -    -    f
1601 //  ne    f    t    t    f    t    -    -    t    t    -    -    t
1602 //  lt    f    -    -    f    t    f    -    f    f    -    f    t
1603 //  le    t    -    -    t    t    -    t    f    f    t    -    t
1604 //  gt    f    -    -    f    f    -    f    t    t    f    -    f
1605 //  ge    t    -    -    t    f    t    -    t    t    -    t    f
1606 //
1607 Node* IfNode::simple_subsuming(PhaseIterGVN* igvn) {
1608   // Table encoding: N/A (na), True-branch (tb), False-branch (fb).
1609   static enum { na, tb, fb } s_short_circuit_map[6][12] = {
1610   /*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*/
1611   /*eq*/{ tb,  fb,  fb,  tb,  fb,  na,  na,  fb,  fb,  na,  na,  fb },
1612   /*ne*/{ fb,  tb,  tb,  fb,  tb,  na,  na,  tb,  tb,  na,  na,  tb },
1613   /*lt*/{ fb,  na,  na,  fb,  tb,  fb,  na,  fb,  fb,  na,  fb,  tb },
1614   /*le*/{ tb,  na,  na,  tb,  tb,  na,  tb,  fb,  fb,  tb,  na,  tb },
1615   /*gt*/{ fb,  na,  na,  fb,  fb,  na,  fb,  tb,  tb,  fb,  na,  fb },
1616   /*ge*/{ tb,  na,  na,  tb,  fb,  tb,  na,  tb,  tb,  na,  tb,  fb }};
1617 
1618   Node* pre = in(0);
1619   if (!pre->is_IfTrue() && !pre->is_IfFalse()) {
1620     return NULL;
1621   }
1622   Node* dom = pre->in(0);
1623   if (!dom->is_If()) {
1624     return NULL;
1625   }
1626   Node* bol = in(1);
1627   if (!bol->is_Bool()) {
1628     return NULL;
1629   }
1630   Node* cmp = in(1)->in(1);
1631   if (!cmp->is_Cmp()) {
1632     return NULL;
1633   }
1634 
1635   if (!dom->in(1)->is_Bool()) {
1636     return NULL;
1637   }
1638   if (dom->in(1)->in(1) != cmp) {  // Not same cond?
1639     return NULL;
1640   }
1641 
1642   int drel = subsuming_bool_test_encode(dom->in(1));
1643   int trel = subsuming_bool_test_encode(bol);
1644   int bout = pre->is_IfFalse() ? 1 : 0;
1645 
1646   if (drel < 0 || trel < 0) {
1647     return NULL;
1648   }
1649   int br = s_short_circuit_map[trel][2*drel+bout];
1650   if (br == na) {
1651     return NULL;
1652   }
1653 #ifndef PRODUCT
1654   if (TraceIterativeGVN) {
1655     tty->print("   Subsumed IfNode: "); dump();
1656   }
1657 #endif
1658   // Replace condition with constant True(1)/False(0).
1659   bool is_always_true = br == tb;
1660   set_req(1, igvn->intcon(is_always_true ? 1 : 0));
1661 
1662   // Update any data dependencies to the directly dominating test. This subsumed test is not immediately removed by igvn
1663   // and therefore subsequent optimizations might miss these data dependencies otherwise. There might be a dead loop
1664   // ('always_taken_proj' == 'pre') that is cleaned up later. Skip this case to make the iterator work properly.
1665   Node* always_taken_proj = proj_out(is_always_true);
1666   if (always_taken_proj != pre) {
1667     for (DUIterator_Fast imax, i = always_taken_proj->fast_outs(imax); i < imax; i++) {
1668       Node* u = always_taken_proj->fast_out(i);
1669       if (!u->is_CFG()) {
1670         igvn->replace_input_of(u, 0, pre);
1671         --i;
1672         --imax;
1673       }
1674     }
1675   }
1676 
1677   if (bol->outcnt() == 0) {
1678     igvn->remove_dead_node(bol);    // Kill the BoolNode.
1679   }
1680   return this;
1681 }
1682 
1683 // Map BoolTest to local table encoding. The BoolTest (e)numerals
1684 //   { eq = 0, ne = 4, le = 5, ge = 7, lt = 3, gt = 1 }
1685 // are mapped to table indices, while the remaining (e)numerals in BoolTest
1686 //   { overflow = 2, no_overflow = 6, never = 8, illegal = 9 }
1687 // are ignored (these are not modeled in the table).
1688 //
1689 static int subsuming_bool_test_encode(Node* node) {
1690   precond(node->is_Bool());
1691   BoolTest::mask x = node->as_Bool()->_test._test;
1692   switch (x) {
1693     case BoolTest::eq: return 0;
1694     case BoolTest::ne: return 1;
1695     case BoolTest::lt: return 2;
1696     case BoolTest::le: return 3;
1697     case BoolTest::gt: return 4;
1698     case BoolTest::ge: return 5;
1699     case BoolTest::overflow:
1700     case BoolTest::no_overflow:
1701     case BoolTest::never:
1702     case BoolTest::illegal:
1703     default:
1704       return -1;
1705   }
1706 }
1707 
1708 //------------------------------Identity---------------------------------------
1709 // If the test is constant & we match, then we are the input Control
1710 Node* IfProjNode::Identity(PhaseGVN* phase) {
1711   // Can only optimize if cannot go the other way
1712   const TypeTuple *t = phase->type(in(0))->is_tuple();
1713   if (t == TypeTuple::IFNEITHER || (always_taken(t) &&
1714        // During parsing (GVN) we don't remove dead code aggressively.
1715        // Cut off dead branch and let PhaseRemoveUseless take care of it.
1716       (!phase->is_IterGVN() ||
1717        // During IGVN, first wait for the dead branch to be killed.
1718        // Otherwise, the IfNode's control will have two control uses (the IfNode
1719        // that doesn't go away because it still has uses and this branch of the
1720        // If) which breaks other optimizations. Node::has_special_unique_user()
1721        // will cause this node to be reprocessed once the dead branch is killed.
1722        in(0)->outcnt() == 1))) {
1723     // IfNode control
1724     return in(0)->in(0);
1725   }
1726   // no progress
1727   return this;
1728 }
1729 
1730 #ifndef PRODUCT
1731 //-------------------------------related---------------------------------------
1732 // An IfProjNode's related node set consists of its input (an IfNode) including
1733 // the IfNode's condition, plus all of its outputs at level 1. In compact mode,
1734 // the restrictions for IfNode apply (see IfNode::rel).
1735 void IfProjNode::related(GrowableArray<Node*> *in_rel, GrowableArray<Node*> *out_rel, bool compact) const {
1736   Node* ifNode = this->in(0);
1737   in_rel->append(ifNode);
1738   if (compact) {
1739     ifNode->collect_nodes(in_rel, 3, false, true);
1740   } else {
1741     ifNode->collect_nodes_in_all_data(in_rel, false);
1742   }
1743   this->collect_nodes(out_rel, -1, false, false);
1744 }
1745 
1746 //------------------------------dump_spec--------------------------------------
1747 void IfNode::dump_spec(outputStream *st) const {
1748   st->print("P=%f, C=%f",_prob,_fcnt);
1749 }
1750 
1751 //-------------------------------related---------------------------------------
1752 // For an IfNode, the set of related output nodes is just the output nodes till
1753 // depth 2, i.e, the IfTrue/IfFalse projection nodes plus the nodes they refer.
1754 // The related input nodes contain no control nodes, but all data nodes
1755 // pertaining to the condition. In compact mode, the input nodes are collected
1756 // up to a depth of 3.
1757 void IfNode::related(GrowableArray <Node *> *in_rel, GrowableArray <Node *> *out_rel, bool compact) const {
1758   if (compact) {
1759     this->collect_nodes(in_rel, 3, false, true);
1760   } else {
1761     this->collect_nodes_in_all_data(in_rel, false);
1762   }
1763   this->collect_nodes(out_rel, -2, false, false);
1764 }
1765 #endif
1766 
1767 //------------------------------idealize_test----------------------------------
1768 // Try to canonicalize tests better.  Peek at the Cmp/Bool/If sequence and
1769 // come up with a canonical sequence.  Bools getting 'eq', 'gt' and 'ge' forms
1770 // converted to 'ne', 'le' and 'lt' forms.  IfTrue/IfFalse get swapped as
1771 // needed.
1772 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff) {
1773   assert(iff->in(0) != NULL, "If must be live");
1774 
1775   if (iff->outcnt() != 2)  return NULL; // Malformed projections.
1776   Node* old_if_f = iff->proj_out(false);
1777   Node* old_if_t = iff->proj_out(true);
1778 
1779   // CountedLoopEnds want the back-control test to be TRUE, irregardless of
1780   // whether they are testing a 'gt' or 'lt' condition.  The 'gt' condition
1781   // happens in count-down loops
1782   if (iff->is_BaseCountedLoopEnd())  return NULL;
1783   if (!iff->in(1)->is_Bool())  return NULL; // Happens for partially optimized IF tests
1784   BoolNode *b = iff->in(1)->as_Bool();
1785   BoolTest bt = b->_test;
1786   // Test already in good order?
1787   if( bt.is_canonical() )
1788     return NULL;
1789 
1790   // Flip test to be canonical.  Requires flipping the IfFalse/IfTrue and
1791   // cloning the IfNode.
1792   Node* new_b = phase->transform( new BoolNode(b->in(1), bt.negate()) );
1793   if( !new_b->is_Bool() ) return NULL;
1794   b = new_b->as_Bool();
1795 
1796   PhaseIterGVN *igvn = phase->is_IterGVN();
1797   assert( igvn, "Test is not canonical in parser?" );
1798 
1799   // The IF node never really changes, but it needs to be cloned
1800   iff = iff->clone()->as_If();
1801   iff->set_req(1, b);
1802   iff->_prob = 1.0-iff->_prob;
1803 
1804   Node *prior = igvn->hash_find_insert(iff);
1805   if( prior ) {
1806     igvn->remove_dead_node(iff);
1807     iff = (IfNode*)prior;
1808   } else {
1809     // Cannot call transform on it just yet
1810     igvn->set_type_bottom(iff);
1811   }
1812   igvn->_worklist.push(iff);
1813 
1814   // Now handle projections.  Cloning not required.
1815   Node* new_if_f = (Node*)(new IfFalseNode( iff ));
1816   Node* new_if_t = (Node*)(new IfTrueNode ( iff ));
1817 
1818   igvn->register_new_node_with_optimizer(new_if_f);
1819   igvn->register_new_node_with_optimizer(new_if_t);
1820   // Flip test, so flip trailing control
1821   igvn->replace_node(old_if_f, new_if_t);
1822   igvn->replace_node(old_if_t, new_if_f);
1823 
1824   // Progress
1825   return iff;
1826 }
1827 
1828 Node* RangeCheckNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1829   Node* res = Ideal_common(phase, can_reshape);
1830   if (res != NodeSentinel) {
1831     return res;
1832   }
1833 
1834   PhaseIterGVN *igvn = phase->is_IterGVN();
1835   // Setup to scan up the CFG looking for a dominating test
1836   Node* prev_dom = this;
1837 
1838   // Check for range-check vs other kinds of tests
1839   Node* index1;
1840   Node* range1;
1841   jint offset1;
1842   int flip1 = is_range_check(range1, index1, offset1);
1843   if (flip1) {
1844     Node* dom = in(0);
1845     // Try to remove extra range checks.  All 'up_one_dom' gives up at merges
1846     // so all checks we inspect post-dominate the top-most check we find.
1847     // If we are going to fail the current check and we reach the top check
1848     // then we are guaranteed to fail, so just start interpreting there.
1849     // We 'expand' the top 3 range checks to include all post-dominating
1850     // checks.
1851 
1852     // The top 3 range checks seen
1853     const int NRC = 3;
1854     RangeCheck prev_checks[NRC];
1855     int nb_checks = 0;
1856 
1857     // Low and high offsets seen so far
1858     jint off_lo = offset1;
1859     jint off_hi = offset1;
1860 
1861     bool found_immediate_dominator = false;
1862 
1863     // Scan for the top checks and collect range of offsets
1864     for (int dist = 0; dist < 999; dist++) { // Range-Check scan limit
1865       if (dom->Opcode() == Op_RangeCheck &&  // Not same opcode?
1866           prev_dom->in(0) == dom) { // One path of test does dominate?
1867         if (dom == this) return NULL; // dead loop
1868         // See if this is a range check
1869         Node* index2;
1870         Node* range2;
1871         jint offset2;
1872         int flip2 = dom->as_RangeCheck()->is_range_check(range2, index2, offset2);
1873         // See if this is a _matching_ range check, checking against
1874         // the same array bounds.
1875         if (flip2 == flip1 && range2 == range1 && index2 == index1 &&
1876             dom->outcnt() == 2) {
1877           if (nb_checks == 0 && dom->in(1) == in(1)) {
1878             // Found an immediately dominating test at the same offset.
1879             // This kind of back-to-back test can be eliminated locally,
1880             // and there is no need to search further for dominating tests.
1881             assert(offset2 == offset1, "Same test but different offsets");
1882             found_immediate_dominator = true;
1883             break;
1884           }
1885           // Gather expanded bounds
1886           off_lo = MIN2(off_lo,offset2);
1887           off_hi = MAX2(off_hi,offset2);
1888           // Record top NRC range checks
1889           prev_checks[nb_checks%NRC].ctl = prev_dom;
1890           prev_checks[nb_checks%NRC].off = offset2;
1891           nb_checks++;
1892         }
1893       }
1894       prev_dom = dom;
1895       dom = up_one_dom(dom);
1896       if (!dom) break;
1897     }
1898 
1899     if (!found_immediate_dominator) {
1900       // Attempt to widen the dominating range check to cover some later
1901       // ones.  Since range checks "fail" by uncommon-trapping to the
1902       // interpreter, widening a check can make us speculatively enter
1903       // the interpreter.  If we see range-check deopt's, do not widen!
1904       if (!phase->C->allow_range_check_smearing())  return NULL;
1905 
1906       // Didn't find prior covering check, so cannot remove anything.
1907       if (nb_checks == 0) {
1908         return NULL;
1909       }
1910       // Constant indices only need to check the upper bound.
1911       // Non-constant indices must check both low and high.
1912       int chk0 = (nb_checks - 1) % NRC;
1913       if (index1) {
1914         if (nb_checks == 1) {
1915           return NULL;
1916         } else {
1917           // If the top range check's constant is the min or max of
1918           // all constants we widen the next one to cover the whole
1919           // range of constants.
1920           RangeCheck rc0 = prev_checks[chk0];
1921           int chk1 = (nb_checks - 2) % NRC;
1922           RangeCheck rc1 = prev_checks[chk1];
1923           if (rc0.off == off_lo) {
1924             adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn);
1925             prev_dom = rc1.ctl;
1926           } else if (rc0.off == off_hi) {
1927             adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn);
1928             prev_dom = rc1.ctl;
1929           } else {
1930             // If the top test's constant is not the min or max of all
1931             // constants, we need 3 range checks. We must leave the
1932             // top test unchanged because widening it would allow the
1933             // accesses it protects to successfully read/write out of
1934             // bounds.
1935             if (nb_checks == 2) {
1936               return NULL;
1937             }
1938             int chk2 = (nb_checks - 3) % NRC;
1939             RangeCheck rc2 = prev_checks[chk2];
1940             // The top range check a+i covers interval: -a <= i < length-a
1941             // The second range check b+i covers interval: -b <= i < length-b
1942             if (rc1.off <= rc0.off) {
1943               // if b <= a, we change the second range check to:
1944               // -min_of_all_constants <= i < length-min_of_all_constants
1945               // Together top and second range checks now cover:
1946               // -min_of_all_constants <= i < length-a
1947               // which is more restrictive than -b <= i < length-b:
1948               // -b <= -min_of_all_constants <= i < length-a <= length-b
1949               // The third check is then changed to:
1950               // -max_of_all_constants <= i < length-max_of_all_constants
1951               // so 2nd and 3rd checks restrict allowed values of i to:
1952               // -min_of_all_constants <= i < length-max_of_all_constants
1953               adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn);
1954               adjust_check(rc2.ctl, range1, index1, flip1, off_hi, igvn);
1955             } else {
1956               // if b > a, we change the second range check to:
1957               // -max_of_all_constants <= i < length-max_of_all_constants
1958               // Together top and second range checks now cover:
1959               // -a <= i < length-max_of_all_constants
1960               // which is more restrictive than -b <= i < length-b:
1961               // -b < -a <= i < length-max_of_all_constants <= length-b
1962               // The third check is then changed to:
1963               // -max_of_all_constants <= i < length-max_of_all_constants
1964               // so 2nd and 3rd checks restrict allowed values of i to:
1965               // -min_of_all_constants <= i < length-max_of_all_constants
1966               adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn);
1967               adjust_check(rc2.ctl, range1, index1, flip1, off_lo, igvn);
1968             }
1969             prev_dom = rc2.ctl;
1970           }
1971         }
1972       } else {
1973         RangeCheck rc0 = prev_checks[chk0];
1974         // 'Widen' the offset of the 1st and only covering check
1975         adjust_check(rc0.ctl, range1, index1, flip1, off_hi, igvn);
1976         // Test is now covered by prior checks, dominate it out
1977         prev_dom = rc0.ctl;
1978       }
1979     }
1980   } else {
1981     prev_dom = search_identical(4);
1982 
1983     if (prev_dom == NULL) {
1984       return NULL;
1985     }
1986   }
1987 
1988   // Replace dominated IfNode
1989   return dominated_by(prev_dom, igvn);
1990 }