1 /*
   2  * Copyright (c) 2000, 2013, 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/cfgnode.hpp"
  29 #include "opto/connode.hpp"
  30 #include "opto/loopnode.hpp"
  31 #include "opto/phaseX.hpp"
  32 #include "opto/runtime.hpp"
  33 #include "opto/subnode.hpp"
  34 
  35 // Portions of code courtesy of Clifford Click
  36 
  37 // Optimization - Graph Style
  38 
  39 
  40 extern int explicit_null_checks_elided;
  41 
  42 //=============================================================================
  43 //------------------------------Value------------------------------------------
  44 // Return a tuple for whichever arm of the IF is reachable
  45 const Type *IfNode::Value( PhaseTransform *phase ) const {
  46   if( !in(0) ) return Type::TOP;
  47   if( phase->type(in(0)) == Type::TOP )
  48     return Type::TOP;
  49   const Type *t = phase->type(in(1));
  50   if( t == Type::TOP )          // data is undefined
  51     return TypeTuple::IFNEITHER; // unreachable altogether
  52   if( t == TypeInt::ZERO )      // zero, or false
  53     return TypeTuple::IFFALSE;  // only false branch is reachable
  54   if( t == TypeInt::ONE )       // 1, or true
  55     return TypeTuple::IFTRUE;   // only true branch is reachable
  56   assert( t == TypeInt::BOOL, "expected boolean type" );
  57 
  58   return TypeTuple::IFBOTH;     // No progress
  59 }
  60 
  61 const RegMask &IfNode::out_RegMask() const {
  62   return RegMask::Empty;
  63 }
  64 
  65 //------------------------------split_if---------------------------------------
  66 // Look for places where we merge constants, then test on the merged value.
  67 // If the IF test will be constant folded on the path with the constant, we
  68 // win by splitting the IF to before the merge point.
  69 static Node* split_if(IfNode *iff, PhaseIterGVN *igvn) {
  70   // I could be a lot more general here, but I'm trying to squeeze this
  71   // in before the Christmas '98 break so I'm gonna be kinda restrictive
  72   // on the patterns I accept.  CNC
  73 
  74   // Look for a compare of a constant and a merged value
  75   Node *i1 = iff->in(1);
  76   if( !i1->is_Bool() ) return NULL;
  77   BoolNode *b = i1->as_Bool();
  78   Node *cmp = b->in(1);
  79   if( !cmp->is_Cmp() ) return NULL;
  80   i1 = cmp->in(1);
  81   if( i1 == NULL || !i1->is_Phi() ) return NULL;
  82   PhiNode *phi = i1->as_Phi();
  83   if( phi->is_copy() ) return NULL;
  84   Node *con2 = cmp->in(2);
  85   if( !con2->is_Con() ) return NULL;
  86   // See that the merge point contains some constants
  87   Node *con1=NULL;
  88   uint i4;
  89   for( i4 = 1; i4 < phi->req(); i4++ ) {
  90     con1 = phi->in(i4);
  91     if( !con1 ) return NULL;    // Do not optimize partially collapsed merges
  92     if( con1->is_Con() ) break; // Found a constant
  93     // Also allow null-vs-not-null checks
  94     const TypePtr *tp = igvn->type(con1)->isa_ptr();
  95     if( tp && tp->_ptr == TypePtr::NotNull )
  96       break;
  97   }
  98   if( i4 >= phi->req() ) return NULL; // Found no constants
  99 
 100   igvn->C->set_has_split_ifs(true); // Has chance for split-if
 101 
 102   // Make sure that the compare can be constant folded away
 103   Node *cmp2 = cmp->clone();
 104   cmp2->set_req(1,con1);
 105   cmp2->set_req(2,con2);
 106   const Type *t = cmp2->Value(igvn);
 107   // This compare is dead, so whack it!
 108   igvn->remove_dead_node(cmp2);
 109   if( !t->singleton() ) return NULL;
 110 
 111   // No intervening control, like a simple Call
 112   Node *r = iff->in(0);
 113   if( !r->is_Region() ) return NULL;
 114   if( phi->region() != r ) return NULL;
 115   // No other users of the cmp/bool
 116   if (b->outcnt() != 1 || cmp->outcnt() != 1) {
 117     //tty->print_cr("many users of cmp/bool");
 118     return NULL;
 119   }
 120 
 121   // Make sure we can determine where all the uses of merged values go
 122   for (DUIterator_Fast jmax, j = r->fast_outs(jmax); j < jmax; j++) {
 123     Node* u = r->fast_out(j);
 124     if( u == r ) continue;
 125     if( u == iff ) continue;
 126     if( u->outcnt() == 0 ) continue; // use is dead & ignorable
 127     if( !u->is_Phi() ) {
 128       /*
 129       if( u->is_Start() ) {
 130         tty->print_cr("Region has inlined start use");
 131       } else {
 132         tty->print_cr("Region has odd use");
 133         u->dump(2);
 134       }*/
 135       return NULL;
 136     }
 137     if( u != phi ) {
 138       // CNC - do not allow any other merged value
 139       //tty->print_cr("Merging another value");
 140       //u->dump(2);
 141       return NULL;
 142     }
 143     // Make sure we can account for all Phi uses
 144     for (DUIterator_Fast kmax, k = u->fast_outs(kmax); k < kmax; k++) {
 145       Node* v = u->fast_out(k); // User of the phi
 146       // CNC - Allow only really simple patterns.
 147       // In particular I disallow AddP of the Phi, a fairly common pattern
 148       if( v == cmp ) continue;  // The compare is OK
 149       if( (v->is_ConstraintCast()) &&
 150           v->in(0)->in(0) == iff )
 151         continue;               // CastPP/II of the IfNode is OK
 152       // Disabled following code because I cannot tell if exactly one
 153       // path dominates without a real dominator check. CNC 9/9/1999
 154       //uint vop = v->Opcode();
 155       //if( vop == Op_Phi ) {     // Phi from another merge point might be OK
 156       //  Node *r = v->in(0);     // Get controlling point
 157       //  if( !r ) return NULL;   // Degraded to a copy
 158       //  // Find exactly one path in (either True or False doms, but not IFF)
 159       //  int cnt = 0;
 160       //  for( uint i = 1; i < r->req(); i++ )
 161       //    if( r->in(i) && r->in(i)->in(0) == iff )
 162       //      cnt++;
 163       //  if( cnt == 1 ) continue; // Exactly one of True or False guards Phi
 164       //}
 165       if( !v->is_Call() ) {
 166         /*
 167         if( v->Opcode() == Op_AddP ) {
 168           tty->print_cr("Phi has AddP use");
 169         } else if( v->Opcode() == Op_CastPP ) {
 170           tty->print_cr("Phi has CastPP use");
 171         } else if( v->Opcode() == Op_CastII ) {
 172           tty->print_cr("Phi has CastII use");
 173         } else {
 174           tty->print_cr("Phi has use I cant be bothered with");
 175         }
 176         */
 177       }
 178       return NULL;
 179 
 180       /* CNC - Cut out all the fancy acceptance tests
 181       // Can we clone this use when doing the transformation?
 182       // If all uses are from Phis at this merge or constants, then YES.
 183       if( !v->in(0) && v != cmp ) {
 184         tty->print_cr("Phi has free-floating use");
 185         v->dump(2);
 186         return NULL;
 187       }
 188       for( uint l = 1; l < v->req(); l++ ) {
 189         if( (!v->in(l)->is_Phi() || v->in(l)->in(0) != r) &&
 190             !v->in(l)->is_Con() ) {
 191           tty->print_cr("Phi has use");
 192           v->dump(2);
 193           return NULL;
 194         } // End of if Phi-use input is neither Phi nor Constant
 195       } // End of for all inputs to Phi-use
 196       */
 197     } // End of for all uses of Phi
 198   } // End of for all uses of Region
 199 
 200   // Only do this if the IF node is in a sane state
 201   if (iff->outcnt() != 2)
 202     return NULL;
 203 
 204   // Got a hit!  Do the Mondo Hack!
 205   //
 206   //ABC  a1c   def   ghi            B     1     e     h   A C   a c   d f   g i
 207   // R - Phi - Phi - Phi            Rc - Phi - Phi - Phi   Rx - Phi - Phi - Phi
 208   //     cmp - 2                         cmp - 2               cmp - 2
 209   //       bool                            bool_c                bool_x
 210   //       if                               if_c                  if_x
 211   //      T  F                              T  F                  T  F
 212   // ..s..    ..t ..                   ..s..    ..t..        ..s..    ..t..
 213   //
 214   // Split the paths coming into the merge point into 2 separate groups of
 215   // merges.  On the left will be all the paths feeding constants into the
 216   // Cmp's Phi.  On the right will be the remaining paths.  The Cmp's Phi
 217   // will fold up into a constant; this will let the Cmp fold up as well as
 218   // all the control flow.  Below the original IF we have 2 control
 219   // dependent regions, 's' and 't'.  Now we will merge the two paths
 220   // just prior to 's' and 't' from the two IFs.  At least 1 path (and quite
 221   // likely 2 or more) will promptly constant fold away.
 222   PhaseGVN *phase = igvn;
 223 
 224   // Make a region merging constants and a region merging the rest
 225   uint req_c = 0;
 226   Node* predicate_proj = NULL;
 227   for (uint ii = 1; ii < r->req(); ii++) {
 228     if (phi->in(ii) == con1) {
 229       req_c++;
 230     }
 231     Node* proj = PhaseIdealLoop::find_predicate(r->in(ii));
 232     if (proj != NULL) {
 233       assert(predicate_proj == NULL, "only one predicate entry expected");
 234       predicate_proj = proj;
 235     }
 236   }
 237 
 238   // If all the defs of the phi are the same constant, we already have the desired end state.
 239   // Skip the split that would create empty phi and region nodes.
 240   if((r->req() - req_c) == 1) {
 241     return NULL;
 242   }
 243 
 244   Node* predicate_c = NULL;
 245   Node* predicate_x = NULL;
 246   bool counted_loop = r->is_CountedLoop();
 247 
 248   Node *region_c = new (igvn->C) RegionNode(req_c + 1);
 249   Node *phi_c    = con1;
 250   uint  len      = r->req();
 251   Node *region_x = new (igvn->C) RegionNode(len - req_c);
 252   Node *phi_x    = PhiNode::make_blank(region_x, phi);
 253   for (uint i = 1, i_c = 1, i_x = 1; i < len; i++) {
 254     if (phi->in(i) == con1) {
 255       region_c->init_req( i_c++, r  ->in(i) );
 256       if (r->in(i) == predicate_proj)
 257         predicate_c = predicate_proj;
 258     } else {
 259       region_x->init_req( i_x,   r  ->in(i) );
 260       phi_x   ->init_req( i_x++, phi->in(i) );
 261       if (r->in(i) == predicate_proj)
 262         predicate_x = predicate_proj;
 263     }
 264   }
 265   if (predicate_c != NULL && (req_c > 1)) {
 266     assert(predicate_x == NULL, "only one predicate entry expected");
 267     predicate_c = NULL; // Do not clone predicate below merge point
 268   }
 269   if (predicate_x != NULL && ((len - req_c) > 2)) {
 270     assert(predicate_c == NULL, "only one predicate entry expected");
 271     predicate_x = NULL; // Do not clone predicate below merge point
 272   }
 273 
 274   // Register the new RegionNodes but do not transform them.  Cannot
 275   // transform until the entire Region/Phi conglomerate has been hacked
 276   // as a single huge transform.
 277   igvn->register_new_node_with_optimizer( region_c );
 278   igvn->register_new_node_with_optimizer( region_x );
 279   // Prevent the untimely death of phi_x.  Currently he has no uses.  He is
 280   // about to get one.  If this only use goes away, then phi_x will look dead.
 281   // However, he will be picking up some more uses down below.
 282   Node *hook = new (igvn->C) Node(4);
 283   hook->init_req(0, phi_x);
 284   hook->init_req(1, phi_c);
 285   phi_x = phase->transform( phi_x );
 286 
 287   // Make the compare
 288   Node *cmp_c = phase->makecon(t);
 289   Node *cmp_x = cmp->clone();
 290   cmp_x->set_req(1,phi_x);
 291   cmp_x->set_req(2,con2);
 292   cmp_x = phase->transform(cmp_x);
 293   // Make the bool
 294   Node *b_c = phase->transform(new (igvn->C) BoolNode(cmp_c,b->_test._test));
 295   Node *b_x = phase->transform(new (igvn->C) BoolNode(cmp_x,b->_test._test));
 296   // Make the IfNode
 297   IfNode *iff_c = new (igvn->C) IfNode(region_c,b_c,iff->_prob,iff->_fcnt);
 298   igvn->set_type_bottom(iff_c);
 299   igvn->_worklist.push(iff_c);
 300   hook->init_req(2, iff_c);
 301 
 302   IfNode *iff_x = new (igvn->C) IfNode(region_x,b_x,iff->_prob, iff->_fcnt);
 303   igvn->set_type_bottom(iff_x);
 304   igvn->_worklist.push(iff_x);
 305   hook->init_req(3, iff_x);
 306 
 307   // Make the true/false arms
 308   Node *iff_c_t = phase->transform(new (igvn->C) IfTrueNode (iff_c));
 309   Node *iff_c_f = phase->transform(new (igvn->C) IfFalseNode(iff_c));
 310   if (predicate_c != NULL) {
 311     assert(predicate_x == NULL, "only one predicate entry expected");
 312     // Clone loop predicates to each path
 313     iff_c_t = igvn->clone_loop_predicates(predicate_c, iff_c_t, !counted_loop);
 314     iff_c_f = igvn->clone_loop_predicates(predicate_c, iff_c_f, !counted_loop);
 315   }
 316   Node *iff_x_t = phase->transform(new (igvn->C) IfTrueNode (iff_x));
 317   Node *iff_x_f = phase->transform(new (igvn->C) IfFalseNode(iff_x));
 318   if (predicate_x != NULL) {
 319     assert(predicate_c == NULL, "only one predicate entry expected");
 320     // Clone loop predicates to each path
 321     iff_x_t = igvn->clone_loop_predicates(predicate_x, iff_x_t, !counted_loop);
 322     iff_x_f = igvn->clone_loop_predicates(predicate_x, iff_x_f, !counted_loop);
 323   }
 324 
 325   // Merge the TRUE paths
 326   Node *region_s = new (igvn->C) RegionNode(3);
 327   igvn->_worklist.push(region_s);
 328   region_s->init_req(1, iff_c_t);
 329   region_s->init_req(2, iff_x_t);
 330   igvn->register_new_node_with_optimizer( region_s );
 331 
 332   // Merge the FALSE paths
 333   Node *region_f = new (igvn->C) RegionNode(3);
 334   igvn->_worklist.push(region_f);
 335   region_f->init_req(1, iff_c_f);
 336   region_f->init_req(2, iff_x_f);
 337   igvn->register_new_node_with_optimizer( region_f );
 338 
 339   igvn->hash_delete(cmp);// Remove soon-to-be-dead node from hash table.
 340   cmp->set_req(1,NULL);  // Whack the inputs to cmp because it will be dead
 341   cmp->set_req(2,NULL);
 342   // Check for all uses of the Phi and give them a new home.
 343   // The 'cmp' got cloned, but CastPP/IIs need to be moved.
 344   Node *phi_s = NULL;     // do not construct unless needed
 345   Node *phi_f = NULL;     // do not construct unless needed
 346   for (DUIterator_Last i2min, i2 = phi->last_outs(i2min); i2 >= i2min; --i2) {
 347     Node* v = phi->last_out(i2);// User of the phi
 348     igvn->rehash_node_delayed(v); // Have to fixup other Phi users
 349     uint vop = v->Opcode();
 350     Node *proj = NULL;
 351     if( vop == Op_Phi ) {       // Remote merge point
 352       Node *r = v->in(0);
 353       for (uint i3 = 1; i3 < r->req(); i3++)
 354         if (r->in(i3) && r->in(i3)->in(0) == iff) {
 355           proj = r->in(i3);
 356           break;
 357         }
 358     } else if( v->is_ConstraintCast() ) {
 359       proj = v->in(0);          // Controlling projection
 360     } else {
 361       assert( 0, "do not know how to handle this guy" );
 362     }
 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 (igvn->C) ConINode(TypeInt::ZERO);
 449 }
 450 
 451 //------------------------------is_range_check---------------------------------
 452 // Return 0 if not a range check.  Return 1 if a range check and set index and
 453 // offset.  Return 2 if we had to negate the test.  Index is NULL if the check
 454 // is versus a constant.
 455 int IfNode::is_range_check(Node* &range, Node* &index, jint &offset) {
 456   if (outcnt() != 2) {
 457     return 0;
 458   }
 459   Node* b = in(1);
 460   if (b == NULL || !b->is_Bool())  return 0;
 461   BoolNode* bn = b->as_Bool();
 462   Node* cmp = bn->in(1);
 463   if (cmp == NULL)  return 0;
 464   if (cmp->Opcode() != Op_CmpU)  return 0;
 465 
 466   Node* l = cmp->in(1);
 467   Node* r = cmp->in(2);
 468   int flip_test = 1;
 469   if (bn->_test._test == BoolTest::le) {
 470     l = cmp->in(2);
 471     r = cmp->in(1);
 472     flip_test = 2;
 473   } else if (bn->_test._test != BoolTest::lt) {
 474     return 0;
 475   }
 476   if (l->is_top())  return 0;   // Top input means dead test
 477   if (r->Opcode() != Op_LoadRange)  return 0;
 478 
 479   // We have recognized one of these forms:
 480   //  Flip 1:  If (Bool[<] CmpU(l, LoadRange)) ...
 481   //  Flip 2:  If (Bool[<=] CmpU(LoadRange, l)) ...
 482 
 483   // Make sure it's a real range check by requiring an uncommon trap
 484   // along the OOB path.  Otherwise, it's possible that the user wrote
 485   // something which optimized to look like a range check but behaves
 486   // in some other way.
 487   Node* iftrap = proj_out(flip_test == 2 ? true : false);
 488   bool found_trap = false;
 489   if (iftrap != NULL) {
 490     Node* u = iftrap->unique_ctrl_out();
 491     if (u != NULL) {
 492       // It could be a merge point (Region) for uncommon trap.
 493       if (u->is_Region()) {
 494         Node* c = u->unique_ctrl_out();
 495         if (c != NULL) {
 496           iftrap = u;
 497           u = c;
 498         }
 499       }
 500       if (u->in(0) == iftrap && u->is_CallStaticJava()) {
 501         int req = u->as_CallStaticJava()->uncommon_trap_request();
 502         if (Deoptimization::trap_request_reason(req) ==
 503             Deoptimization::Reason_range_check) {
 504           found_trap = true;
 505         }
 506       }
 507     }
 508   }
 509   if (!found_trap)  return 0;   // sorry, no cigar
 510 
 511   // Look for index+offset form
 512   Node* ind = l;
 513   jint  off = 0;
 514   if (l->is_top()) {
 515     return 0;
 516   } else if (l->Opcode() == Op_AddI) {
 517     if ((off = l->in(1)->find_int_con(0)) != 0) {
 518       ind = l->in(2);
 519     } else if ((off = l->in(2)->find_int_con(0)) != 0) {
 520       ind = l->in(1);
 521     }
 522   } else if ((off = l->find_int_con(-1)) >= 0) {
 523     // constant offset with no variable index
 524     ind = NULL;
 525   } else {
 526     // variable index with no constant offset (or dead negative index)
 527     off = 0;
 528   }
 529 
 530   // Return all the values:
 531   index  = ind;
 532   offset = off;
 533   range  = r;
 534   return flip_test;
 535 }
 536 
 537 //------------------------------adjust_check-----------------------------------
 538 // Adjust (widen) a prior range check
 539 static void adjust_check(Node* proj, Node* range, Node* index,
 540                          int flip, jint off_lo, PhaseIterGVN* igvn) {
 541   PhaseGVN *gvn = igvn;
 542   // Break apart the old check
 543   Node *iff = proj->in(0);
 544   Node *bol = iff->in(1);
 545   if( bol->is_top() ) return;   // In case a partially dead range check appears
 546   // bail (or bomb[ASSERT/DEBUG]) if NOT projection-->IfNode-->BoolNode
 547   DEBUG_ONLY( if( !bol->is_Bool() ) { proj->dump(3); fatal("Expect projection-->IfNode-->BoolNode"); } )
 548   if( !bol->is_Bool() ) return;
 549 
 550   Node *cmp = bol->in(1);
 551   // Compute a new check
 552   Node *new_add = gvn->intcon(off_lo);
 553   if( index ) {
 554     new_add = off_lo ? gvn->transform(new (gvn->C) AddINode( index, new_add )) : index;
 555   }
 556   Node *new_cmp = (flip == 1)
 557     ? new (gvn->C) CmpUNode( new_add, range )
 558     : new (gvn->C) CmpUNode( range, new_add );
 559   new_cmp = gvn->transform(new_cmp);
 560   // See if no need to adjust the existing check
 561   if( new_cmp == cmp ) return;
 562   // Else, adjust existing check
 563   Node *new_bol = gvn->transform( new (gvn->C) BoolNode( new_cmp, bol->as_Bool()->_test._test ) );
 564   igvn->rehash_node_delayed( iff );
 565   iff->set_req_X( 1, new_bol, igvn );
 566 }
 567 
 568 //------------------------------up_one_dom-------------------------------------
 569 // Walk up the dominator tree one step.  Return NULL at root or true
 570 // complex merges.  Skips through small diamonds.
 571 Node* IfNode::up_one_dom(Node *curr, bool linear_only) {
 572   Node *dom = curr->in(0);
 573   if( !dom )                    // Found a Region degraded to a copy?
 574     return curr->nonnull_req(); // Skip thru it
 575 
 576   if( curr != dom )             // Normal walk up one step?
 577     return dom;
 578 
 579   // Use linear_only if we are still parsing, since we cannot
 580   // trust the regions to be fully filled in.
 581   if (linear_only)
 582     return NULL;
 583 
 584   if( dom->is_Root() )
 585     return NULL;
 586 
 587   // Else hit a Region.  Check for a loop header
 588   if( dom->is_Loop() )
 589     return dom->in(1);          // Skip up thru loops
 590 
 591   // Check for small diamonds
 592   Node *din1, *din2, *din3, *din4;
 593   if( dom->req() == 3 &&        // 2-path merge point
 594       (din1 = dom ->in(1)) &&   // Left  path exists
 595       (din2 = dom ->in(2)) &&   // Right path exists
 596       (din3 = din1->in(0)) &&   // Left  path up one
 597       (din4 = din2->in(0)) ) {  // Right path up one
 598     if( din3->is_Call() &&      // Handle a slow-path call on either arm
 599         (din3 = din3->in(0)) )
 600       din3 = din3->in(0);
 601     if( din4->is_Call() &&      // Handle a slow-path call on either arm
 602         (din4 = din4->in(0)) )
 603       din4 = din4->in(0);
 604     if( din3 == din4 && din3->is_If() )
 605       return din3;              // Skip around diamonds
 606   }
 607 
 608   // Give up the search at true merges
 609   return NULL;                  // Dead loop?  Or hit root?
 610 }
 611 
 612 
 613 //------------------------------filtered_int_type--------------------------------
 614 // Return a possibly more restrictive type for val based on condition control flow for an if
 615 const TypeInt* IfNode::filtered_int_type(PhaseGVN* gvn, Node *val, Node* if_proj) {
 616   assert(if_proj &&
 617          (if_proj->Opcode() == Op_IfTrue || if_proj->Opcode() == Op_IfFalse), "expecting an if projection");
 618   if (if_proj->in(0) && if_proj->in(0)->is_If()) {
 619     IfNode* iff = if_proj->in(0)->as_If();
 620     if (iff->in(1) && iff->in(1)->is_Bool()) {
 621       BoolNode* bol = iff->in(1)->as_Bool();
 622       if (bol->in(1) && bol->in(1)->is_Cmp()) {
 623         const CmpNode* cmp  = bol->in(1)->as_Cmp();
 624         if (cmp->in(1) == val) {
 625           const TypeInt* cmp2_t = gvn->type(cmp->in(2))->isa_int();
 626           if (cmp2_t != NULL) {
 627             jint lo = cmp2_t->_lo;
 628             jint hi = cmp2_t->_hi;
 629             BoolTest::mask msk = if_proj->Opcode() == Op_IfTrue ? bol->_test._test : bol->_test.negate();
 630             switch (msk) {
 631             case BoolTest::ne:
 632               // Can't refine type
 633               return NULL;
 634             case BoolTest::eq:
 635               return cmp2_t;
 636             case BoolTest::lt:
 637               lo = TypeInt::INT->_lo;
 638               if (hi - 1 < hi) {
 639                 hi = hi - 1;
 640               }
 641               break;
 642             case BoolTest::le:
 643               lo = TypeInt::INT->_lo;
 644               break;
 645             case BoolTest::gt:
 646               if (lo + 1 > lo) {
 647                 lo = lo + 1;
 648               }
 649               hi = TypeInt::INT->_hi;
 650               break;
 651             case BoolTest::ge:
 652               // lo unchanged
 653               hi = TypeInt::INT->_hi;
 654               break;
 655             }
 656             const TypeInt* rtn_t = TypeInt::make(lo, hi, cmp2_t->_widen);
 657             return rtn_t;
 658           }
 659         }
 660       }
 661     }
 662   }
 663   return NULL;
 664 }
 665 
 666 //------------------------------fold_compares----------------------------
 667 // See if a pair of CmpIs can be converted into a CmpU.  In some cases
 668 // the direction of this if is determined by the preceding if so it
 669 // can be eliminate entirely.  Given an if testing (CmpI n c) check
 670 // for an immediately control dependent if that is testing (CmpI n c2)
 671 // and has one projection leading to this if and the other projection
 672 // leading to a region that merges one of this ifs control
 673 // projections.
 674 //
 675 //                   If
 676 //                  / |
 677 //                 /  |
 678 //                /   |
 679 //              If    |
 680 //              /\    |
 681 //             /  \   |
 682 //            /    \  |
 683 //           /    Region
 684 //
 685 Node* IfNode::fold_compares(PhaseGVN* phase) {
 686   if (Opcode() != Op_If) return NULL;
 687 
 688   Node* this_cmp = in(1)->in(1);
 689   if (this_cmp != NULL && this_cmp->Opcode() == Op_CmpI &&
 690       this_cmp->in(2)->is_Con() && this_cmp->in(2) != phase->C->top()) {
 691     Node* ctrl = in(0);
 692     BoolNode* this_bool = in(1)->as_Bool();
 693     Node* n = this_cmp->in(1);
 694     int hi = this_cmp->in(2)->get_int();
 695     if (ctrl != NULL && ctrl->is_Proj() && ctrl->outcnt() == 1 &&
 696         ctrl->in(0)->is_If() &&
 697         ctrl->in(0)->outcnt() == 2 &&
 698         ctrl->in(0)->in(1)->is_Bool() &&
 699         ctrl->in(0)->in(1)->in(1)->Opcode() == Op_CmpI &&
 700         ctrl->in(0)->in(1)->in(1)->in(2)->is_Con() &&
 701         ctrl->in(0)->in(1)->in(1)->in(2) != phase->C->top() &&
 702         ctrl->in(0)->in(1)->in(1)->in(1) == n) {
 703       IfNode* dom_iff = ctrl->in(0)->as_If();
 704       Node* otherproj = dom_iff->proj_out(!ctrl->as_Proj()->_con);
 705       if (otherproj->outcnt() == 1 && otherproj->unique_out()->is_Region() &&
 706           this_bool->_test._test != BoolTest::ne && this_bool->_test._test != BoolTest::eq) {
 707         // Identify which proj goes to the region and which continues on
 708         RegionNode* region = otherproj->unique_out()->as_Region();
 709         Node* success = NULL;
 710         Node* fail = NULL;
 711         for (int i = 0; i < 2; i++) {
 712           Node* proj = proj_out(i);
 713           if (success == NULL && proj->outcnt() == 1 && proj->unique_out() == region) {
 714             success = proj;
 715           } else if (fail == NULL) {
 716             fail = proj;
 717           } else {
 718             success = fail = NULL;
 719           }
 720         }
 721         if (success != NULL && fail != NULL && !region->has_phi()) {
 722           int lo = dom_iff->in(1)->in(1)->in(2)->get_int();
 723           BoolNode* dom_bool = dom_iff->in(1)->as_Bool();
 724           Node* dom_cmp =  dom_bool->in(1);
 725           const TypeInt* failtype  = filtered_int_type(phase, n, ctrl);
 726           if (failtype != NULL) {
 727             const TypeInt* type2 = filtered_int_type(phase, n, fail);
 728             if (type2 != NULL) {
 729               failtype = failtype->join(type2)->is_int();
 730             } else {
 731               failtype = NULL;
 732             }
 733           }
 734 
 735           if (failtype != NULL &&
 736               dom_bool->_test._test != BoolTest::ne && dom_bool->_test._test != BoolTest::eq) {
 737             int bound = failtype->_hi - failtype->_lo + 1;
 738             if (failtype->_hi != max_jint && failtype->_lo != min_jint && bound > 1) {
 739               // Merge the two compares into a single unsigned compare by building  (CmpU (n - lo) hi)
 740               BoolTest::mask cond = fail->as_Proj()->_con ? BoolTest::lt : BoolTest::ge;
 741               Node* adjusted = phase->transform(new (phase->C) SubINode(n, phase->intcon(failtype->_lo)));
 742               Node* newcmp = phase->transform(new (phase->C) CmpUNode(adjusted, phase->intcon(bound)));
 743               Node* newbool = phase->transform(new (phase->C) BoolNode(newcmp, cond));
 744               phase->is_IterGVN()->replace_input_of(dom_iff, 1, phase->intcon(ctrl->as_Proj()->_con));
 745               phase->hash_delete(this);
 746               set_req(1, newbool);
 747               return this;
 748             }
 749             if (failtype->_lo > failtype->_hi) {
 750               // previous if determines the result of this if so
 751               // replace Bool with constant
 752               phase->hash_delete(this);
 753               set_req(1, phase->intcon(success->as_Proj()->_con));
 754               return this;
 755             }
 756           }
 757         }
 758       }
 759     }
 760   }
 761   return NULL;
 762 }
 763 
 764 //------------------------------remove_useless_bool----------------------------
 765 // Check for people making a useless boolean: things like
 766 // if( (x < y ? true : false) ) { ... }
 767 // Replace with if( x < y ) { ... }
 768 static Node *remove_useless_bool(IfNode *iff, PhaseGVN *phase) {
 769   Node *i1 = iff->in(1);
 770   if( !i1->is_Bool() ) return NULL;
 771   BoolNode *bol = i1->as_Bool();
 772 
 773   Node *cmp = bol->in(1);
 774   if( cmp->Opcode() != Op_CmpI ) return NULL;
 775 
 776   // Must be comparing against a bool
 777   const Type *cmp2_t = phase->type( cmp->in(2) );
 778   if( cmp2_t != TypeInt::ZERO &&
 779       cmp2_t != TypeInt::ONE )
 780     return NULL;
 781 
 782   // Find a prior merge point merging the boolean
 783   i1 = cmp->in(1);
 784   if( !i1->is_Phi() ) return NULL;
 785   PhiNode *phi = i1->as_Phi();
 786   if( phase->type( phi ) != TypeInt::BOOL )
 787     return NULL;
 788 
 789   // Check for diamond pattern
 790   int true_path = phi->is_diamond_phi();
 791   if( true_path == 0 ) return NULL;
 792 
 793   // Make sure that iff and the control of the phi are different. This
 794   // should really only happen for dead control flow since it requires
 795   // an illegal cycle.
 796   if (phi->in(0)->in(1)->in(0) == iff) return NULL;
 797 
 798   // phi->region->if_proj->ifnode->bool->cmp
 799   BoolNode *bol2 = phi->in(0)->in(1)->in(0)->in(1)->as_Bool();
 800 
 801   // Now get the 'sense' of the test correct so we can plug in
 802   // either iff2->in(1) or its complement.
 803   int flip = 0;
 804   if( bol->_test._test == BoolTest::ne ) flip = 1-flip;
 805   else if( bol->_test._test != BoolTest::eq ) return NULL;
 806   if( cmp2_t == TypeInt::ZERO ) flip = 1-flip;
 807 
 808   const Type *phi1_t = phase->type( phi->in(1) );
 809   const Type *phi2_t = phase->type( phi->in(2) );
 810   // Check for Phi(0,1) and flip
 811   if( phi1_t == TypeInt::ZERO ) {
 812     if( phi2_t != TypeInt::ONE ) return NULL;
 813     flip = 1-flip;
 814   } else {
 815     // Check for Phi(1,0)
 816     if( phi1_t != TypeInt::ONE  ) return NULL;
 817     if( phi2_t != TypeInt::ZERO ) return NULL;
 818   }
 819   if( true_path == 2 ) {
 820     flip = 1-flip;
 821   }
 822 
 823   Node* new_bol = (flip ? phase->transform( bol2->negate(phase) ) : bol2);
 824   assert(new_bol != iff->in(1), "must make progress");
 825   iff->set_req(1, new_bol);
 826   // Intervening diamond probably goes dead
 827   phase->C->set_major_progress();
 828   return iff;
 829 }
 830 
 831 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff);
 832 
 833 struct RangeCheck {
 834   Node* ctl;
 835   jint off;
 836 };
 837 
 838 //------------------------------Ideal------------------------------------------
 839 // Return a node which is more "ideal" than the current node.  Strip out
 840 // control copies
 841 Node *IfNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 842   if (remove_dead_region(phase, can_reshape))  return this;
 843   // No Def-Use info?
 844   if (!can_reshape)  return NULL;
 845   PhaseIterGVN *igvn = phase->is_IterGVN();
 846 
 847   // Don't bother trying to transform a dead if
 848   if (in(0)->is_top())  return NULL;
 849   // Don't bother trying to transform an if with a dead test
 850   if (in(1)->is_top())  return NULL;
 851   // Another variation of a dead test
 852   if (in(1)->is_Con())  return NULL;
 853   // Another variation of a dead if
 854   if (outcnt() < 2)  return NULL;
 855 
 856   // Canonicalize the test.
 857   Node* idt_if = idealize_test(phase, this);
 858   if (idt_if != NULL)  return idt_if;
 859 
 860   // Try to split the IF
 861   Node *s = split_if(this, igvn);
 862   if (s != NULL)  return s;
 863 
 864   // Check for people making a useless boolean: things like
 865   // if( (x < y ? true : false) ) { ... }
 866   // Replace with if( x < y ) { ... }
 867   Node *bol2 = remove_useless_bool(this, phase);
 868   if( bol2 ) return bol2;
 869 
 870   // Setup to scan up the CFG looking for a dominating test
 871   Node *dom = in(0);
 872   Node *prev_dom = this;
 873 
 874   // Check for range-check vs other kinds of tests
 875   Node *index1, *range1;
 876   jint offset1;
 877   int flip1 = is_range_check(range1, index1, offset1);
 878   if( flip1 ) {
 879     // Try to remove extra range checks.  All 'up_one_dom' gives up at merges
 880     // so all checks we inspect post-dominate the top-most check we find.
 881     // If we are going to fail the current check and we reach the top check
 882     // then we are guaranteed to fail, so just start interpreting there.
 883     // We 'expand' the top 3 range checks to include all post-dominating
 884     // checks.
 885 
 886     // The top 3 range checks seen
 887     const int NRC =3;
 888     RangeCheck prev_checks[NRC];
 889     int nb_checks = 0;
 890 
 891     // Low and high offsets seen so far
 892     jint off_lo = offset1;
 893     jint off_hi = offset1;
 894 
 895     bool found_immediate_dominator = false;
 896 
 897     // Scan for the top checks and collect range of offsets
 898     for (int dist = 0; dist < 999; dist++) { // Range-Check scan limit
 899       if (dom->Opcode() == Op_If &&  // Not same opcode?
 900           prev_dom->in(0) == dom) { // One path of test does dominate?
 901         if (dom == this) return NULL; // dead loop
 902         // See if this is a range check
 903         Node *index2, *range2;
 904         jint offset2;
 905         int flip2 = dom->as_If()->is_range_check(range2, index2, offset2);
 906         // See if this is a _matching_ range check, checking against
 907         // the same array bounds.
 908         if (flip2 == flip1 && range2 == range1 && index2 == index1 &&
 909             dom->outcnt() == 2) {
 910           if (nb_checks == 0 && dom->in(1) == in(1)) {
 911             // Found an immediately dominating test at the same offset.
 912             // This kind of back-to-back test can be eliminated locally,
 913             // and there is no need to search further for dominating tests.
 914             assert(offset2 == offset1, "Same test but different offsets");
 915             found_immediate_dominator = true;
 916             break;
 917           }
 918           // Gather expanded bounds
 919           off_lo = MIN2(off_lo,offset2);
 920           off_hi = MAX2(off_hi,offset2);
 921           // Record top NRC range checks
 922           prev_checks[nb_checks%NRC].ctl = prev_dom;
 923           prev_checks[nb_checks%NRC].off = offset2;
 924           nb_checks++;
 925         }
 926       }
 927       prev_dom = dom;
 928       dom = up_one_dom(dom);
 929       if (!dom) break;
 930     }
 931 
 932     if (!found_immediate_dominator) {
 933       // Attempt to widen the dominating range check to cover some later
 934       // ones.  Since range checks "fail" by uncommon-trapping to the
 935       // interpreter, widening a check can make us speculatively enter
 936       // the interpreter.  If we see range-check deopt's, do not widen!
 937       if (!phase->C->allow_range_check_smearing())  return NULL;
 938 
 939       // Didn't find prior covering check, so cannot remove anything.
 940       if (nb_checks == 0) {
 941         return NULL;
 942       }
 943       // Constant indices only need to check the upper bound.
 944       // Non-constant indices must check both low and high.
 945       int chk0 = (nb_checks - 1) % NRC;
 946       if (index1) {
 947         if (nb_checks == 1) {
 948           return NULL;
 949         } else {
 950           // If the top range check's constant is the min or max of
 951           // all constants we widen the next one to cover the whole
 952           // range of constants.
 953           RangeCheck rc0 = prev_checks[chk0];
 954           int chk1 = (nb_checks - 2) % NRC;
 955           RangeCheck rc1 = prev_checks[chk1];
 956           if (rc0.off == off_lo) {
 957             adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn);
 958             prev_dom = rc1.ctl;
 959           } else if (rc0.off == off_hi) {
 960             adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn);
 961             prev_dom = rc1.ctl;
 962           } else {
 963             // If the top test's constant is not the min or max of all
 964             // constants, we need 3 range checks. We must leave the
 965             // top test unchanged because widening it would allow the
 966             // accesses it protects to successfully read/write out of
 967             // bounds.
 968             if (nb_checks == 2) {
 969               return NULL;
 970             }
 971             int chk2 = (nb_checks - 3) % NRC;
 972             RangeCheck rc2 = prev_checks[chk2];
 973             // The top range check a+i covers interval: -a <= i < length-a
 974             // The second range check b+i covers interval: -b <= i < length-b
 975             if (rc1.off <= rc0.off) {
 976               // if b <= a, we change the second range check to:
 977               // -min_of_all_constants <= i < length-min_of_all_constants
 978               // Together top and second range checks now cover:
 979               // -min_of_all_constants <= i < length-a
 980               // which is more restrictive than -b <= i < length-b:
 981               // -b <= -min_of_all_constants <= i < length-a <= length-b
 982               // The third check is then changed to:
 983               // -max_of_all_constants <= i < length-max_of_all_constants
 984               // so 2nd and 3rd checks restrict allowed values of i to:
 985               // -min_of_all_constants <= i < length-max_of_all_constants
 986               adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn);
 987               adjust_check(rc2.ctl, range1, index1, flip1, off_hi, igvn);
 988             } else {
 989               // if b > a, we change the second range check to:
 990               // -max_of_all_constants <= i < length-max_of_all_constants
 991               // Together top and second range checks now cover:
 992               // -a <= i < length-max_of_all_constants
 993               // which is more restrictive than -b <= i < length-b:
 994               // -b < -a <= i < length-max_of_all_constants <= length-b
 995               // The third check is then changed to:
 996               // -max_of_all_constants <= i < length-max_of_all_constants
 997               // so 2nd and 3rd checks restrict allowed values of i to:
 998               // -min_of_all_constants <= i < length-max_of_all_constants
 999               adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn);
1000               adjust_check(rc2.ctl, range1, index1, flip1, off_lo, igvn);
1001             }
1002             prev_dom = rc2.ctl;
1003           }
1004         }
1005       } else {
1006         RangeCheck rc0 = prev_checks[chk0];
1007         // 'Widen' the offset of the 1st and only covering check
1008         adjust_check(rc0.ctl, range1, index1, flip1, off_hi, igvn);
1009         // Test is now covered by prior checks, dominate it out
1010         prev_dom = rc0.ctl;
1011       }
1012     }
1013 
1014   } else {                      // Scan for an equivalent test
1015 
1016     Node *cmp;
1017     int dist = 0;               // Cutoff limit for search
1018     int op = Opcode();
1019     if( op == Op_If &&
1020         (cmp=in(1)->in(1))->Opcode() == Op_CmpP ) {
1021       if( cmp->in(2) != NULL && // make sure cmp is not already dead
1022           cmp->in(2)->bottom_type() == TypePtr::NULL_PTR ) {
1023         dist = 64;              // Limit for null-pointer scans
1024       } else {
1025         dist = 4;               // Do not bother for random pointer tests
1026       }
1027     } else {
1028       dist = 4;                 // Limit for random junky scans
1029     }
1030 
1031     // Normal equivalent-test check.
1032     if( !dom ) return NULL;     // Dead loop?
1033 
1034     Node* result = fold_compares(phase);
1035     if (result != NULL) {
1036       return result;
1037     }
1038 
1039     // Search up the dominator tree for an If with an identical test
1040     while( dom->Opcode() != op    ||  // Not same opcode?
1041            dom->in(1)    != in(1) ||  // Not same input 1?
1042            (req() == 3 && dom->in(2) != in(2)) || // Not same input 2?
1043            prev_dom->in(0) != dom ) { // One path of test does not dominate?
1044       if( dist < 0 ) return NULL;
1045 
1046       dist--;
1047       prev_dom = dom;
1048       dom = up_one_dom( dom );
1049       if( !dom ) return NULL;
1050     }
1051 
1052     // Check that we did not follow a loop back to ourselves
1053     if( this == dom )
1054       return NULL;
1055 
1056     if( dist > 2 )              // Add to count of NULL checks elided
1057       explicit_null_checks_elided++;
1058 
1059   } // End of Else scan for an equivalent test
1060 
1061   // Hit!  Remove this IF
1062 #ifndef PRODUCT
1063   if( TraceIterativeGVN ) {
1064     tty->print("   Removing IfNode: "); this->dump();
1065   }
1066   if( VerifyOpto && !phase->allow_progress() ) {
1067     // Found an equivalent dominating test,
1068     // we can not guarantee reaching a fix-point for these during iterativeGVN
1069     // since intervening nodes may not change.
1070     return NULL;
1071   }
1072 #endif
1073 
1074   // Replace dominated IfNode
1075   dominated_by( prev_dom, igvn );
1076 
1077   // Must return either the original node (now dead) or a new node
1078   // (Do not return a top here, since that would break the uniqueness of top.)
1079   return new (phase->C) ConINode(TypeInt::ZERO);
1080 }
1081 
1082 //------------------------------dominated_by-----------------------------------
1083 void IfNode::dominated_by( Node *prev_dom, PhaseIterGVN *igvn ) {
1084   igvn->hash_delete(this);      // Remove self to prevent spurious V-N
1085   Node *idom = in(0);
1086   // Need opcode to decide which way 'this' test goes
1087   int prev_op = prev_dom->Opcode();
1088   Node *top = igvn->C->top(); // Shortcut to top
1089 
1090   // Loop predicates may have depending checks which should not
1091   // be skipped. For example, range check predicate has two checks
1092   // for lower and upper bounds.
1093   ProjNode* unc_proj = proj_out(1 - prev_dom->as_Proj()->_con)->as_Proj();
1094   if ((unc_proj != NULL) && (unc_proj->is_uncommon_trap_proj(Deoptimization::Reason_predicate))) {
1095     prev_dom = idom;
1096   }
1097 
1098   // Now walk the current IfNode's projections.
1099   // Loop ends when 'this' has no more uses.
1100   for (DUIterator_Last imin, i = last_outs(imin); i >= imin; --i) {
1101     Node *ifp = last_out(i);     // Get IfTrue/IfFalse
1102     igvn->add_users_to_worklist(ifp);
1103     // Check which projection it is and set target.
1104     // Data-target is either the dominating projection of the same type
1105     // or TOP if the dominating projection is of opposite type.
1106     // Data-target will be used as the new control edge for the non-CFG
1107     // nodes like Casts and Loads.
1108     Node *data_target = (ifp->Opcode() == prev_op) ? prev_dom : top;
1109     // Control-target is just the If's immediate dominator or TOP.
1110     Node *ctrl_target = (ifp->Opcode() == prev_op) ?     idom : top;
1111 
1112     // For each child of an IfTrue/IfFalse projection, reroute.
1113     // Loop ends when projection has no more uses.
1114     for (DUIterator_Last jmin, j = ifp->last_outs(jmin); j >= jmin; --j) {
1115       Node* s = ifp->last_out(j);   // Get child of IfTrue/IfFalse
1116       if( !s->depends_only_on_test() ) {
1117         // Find the control input matching this def-use edge.
1118         // For Regions it may not be in slot 0.
1119         uint l;
1120         for( l = 0; s->in(l) != ifp; l++ ) { }
1121         igvn->replace_input_of(s, l, ctrl_target);
1122       } else {                      // Else, for control producers,
1123         igvn->replace_input_of(s, 0, data_target); // Move child to data-target
1124       }
1125     } // End for each child of a projection
1126 
1127     igvn->remove_dead_node(ifp);
1128   } // End for each IfTrue/IfFalse child of If
1129 
1130   // Kill the IfNode
1131   igvn->remove_dead_node(this);
1132 }
1133 
1134 //------------------------------Identity---------------------------------------
1135 // If the test is constant & we match, then we are the input Control
1136 Node *IfTrueNode::Identity( PhaseTransform *phase ) {
1137   // Can only optimize if cannot go the other way
1138   const TypeTuple *t = phase->type(in(0))->is_tuple();
1139   return ( t == TypeTuple::IFNEITHER || t == TypeTuple::IFTRUE )
1140     ? in(0)->in(0)              // IfNode control
1141     : this;                     // no progress
1142 }
1143 
1144 //------------------------------dump_spec--------------------------------------
1145 #ifndef PRODUCT
1146 void IfNode::dump_spec(outputStream *st) const {
1147   st->print("P=%f, C=%f",_prob,_fcnt);
1148 }
1149 #endif
1150 
1151 //------------------------------idealize_test----------------------------------
1152 // Try to canonicalize tests better.  Peek at the Cmp/Bool/If sequence and
1153 // come up with a canonical sequence.  Bools getting 'eq', 'gt' and 'ge' forms
1154 // converted to 'ne', 'le' and 'lt' forms.  IfTrue/IfFalse get swapped as
1155 // needed.
1156 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff) {
1157   assert(iff->in(0) != NULL, "If must be live");
1158 
1159   if (iff->outcnt() != 2)  return NULL; // Malformed projections.
1160   Node* old_if_f = iff->proj_out(false);
1161   Node* old_if_t = iff->proj_out(true);
1162 
1163   // CountedLoopEnds want the back-control test to be TRUE, irregardless of
1164   // whether they are testing a 'gt' or 'lt' condition.  The 'gt' condition
1165   // happens in count-down loops
1166   if (iff->is_CountedLoopEnd())  return NULL;
1167   if (!iff->in(1)->is_Bool())  return NULL; // Happens for partially optimized IF tests
1168   BoolNode *b = iff->in(1)->as_Bool();
1169   BoolTest bt = b->_test;
1170   // Test already in good order?
1171   if( bt.is_canonical() )
1172     return NULL;
1173 
1174   // Flip test to be canonical.  Requires flipping the IfFalse/IfTrue and
1175   // cloning the IfNode.
1176   Node* new_b = phase->transform( new (phase->C) BoolNode(b->in(1), bt.negate()) );
1177   if( !new_b->is_Bool() ) return NULL;
1178   b = new_b->as_Bool();
1179 
1180   PhaseIterGVN *igvn = phase->is_IterGVN();
1181   assert( igvn, "Test is not canonical in parser?" );
1182 
1183   // The IF node never really changes, but it needs to be cloned
1184   iff = new (phase->C) IfNode( iff->in(0), b, 1.0-iff->_prob, iff->_fcnt);
1185 
1186   Node *prior = igvn->hash_find_insert(iff);
1187   if( prior ) {
1188     igvn->remove_dead_node(iff);
1189     iff = (IfNode*)prior;
1190   } else {
1191     // Cannot call transform on it just yet
1192     igvn->set_type_bottom(iff);
1193   }
1194   igvn->_worklist.push(iff);
1195 
1196   // Now handle projections.  Cloning not required.
1197   Node* new_if_f = (Node*)(new (phase->C) IfFalseNode( iff ));
1198   Node* new_if_t = (Node*)(new (phase->C) IfTrueNode ( iff ));
1199 
1200   igvn->register_new_node_with_optimizer(new_if_f);
1201   igvn->register_new_node_with_optimizer(new_if_t);
1202   // Flip test, so flip trailing control
1203   igvn->replace_node(old_if_f, new_if_t);
1204   igvn->replace_node(old_if_t, new_if_f);
1205 
1206   // Progress
1207   return iff;
1208 }
1209 
1210 //------------------------------Identity---------------------------------------
1211 // If the test is constant & we match, then we are the input Control
1212 Node *IfFalseNode::Identity( PhaseTransform *phase ) {
1213   // Can only optimize if cannot go the other way
1214   const TypeTuple *t = phase->type(in(0))->is_tuple();
1215   return ( t == TypeTuple::IFNEITHER || t == TypeTuple::IFFALSE )
1216     ? in(0)->in(0)              // IfNode control
1217     : this;                     // no progress
1218 }