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