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