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