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   // Figure out which of the two tests sets the upper bound and which
 821   // sets the lower bound if any.
 822   if (hi_type->_lo > lo_type->_hi && hi_type->_hi == max_jint && lo_type->_lo == min_jint) {
 823 
 824     assert((dom_bool->_test.is_less() && !proj->_con) ||
 825            (dom_bool->_test.is_greater() && proj->_con), "incorrect test");
 826     // this test was canonicalized
 827     assert(this_bool->_test.is_less() && fail->_con, "incorrect test");
 828 
 829     if (lo_test == BoolTest::gt || lo_test == BoolTest::le) {
 830       lo = igvn->transform(new AddINode(lo, igvn->intcon(1)));
 831     }
 832   } else if (lo_type->_lo > hi_type->_hi && lo_type->_hi == max_jint && hi_type->_lo == min_jint) {
 833     swap(lo, hi);
 834     swap(lo_type, hi_type);
 835     swap(lo_test, hi_test);
 836 
 837     assert((this_bool->_test.is_less() && proj->_con) ||
 838            (this_bool->_test.is_greater() && !proj->_con), "incorrect test");
 839     // this test was canonicalized
 840     assert(dom_bool->_test.is_less() && !fail->_con, "incorrect test");
 841 
 842     cond = (hi_test == BoolTest::le || hi_test == BoolTest::gt) ? BoolTest::gt : BoolTest::ge;
 843 
 844     if (lo_test == BoolTest::le) {
 845       lo = igvn->transform(new AddINode(lo, igvn->intcon(1)));
 846     }
 847 
 848   } else {
 849     const TypeInt* failtype  = filtered_int_type(igvn, n, proj);
 850     if (failtype != NULL) {
 851       const TypeInt* type2 = filtered_int_type(igvn, n, fail);
 852       if (type2 != NULL) {
 853         failtype = failtype->join(type2)->is_int();
 854         if (failtype->_lo > failtype->_hi) {
 855           // previous if determines the result of this if so
 856           // replace Bool with constant
 857           igvn->hash_delete(this);
 858           set_req(1, igvn->intcon(success->_con));
 859           return true;
 860         }
 861       }
 862     }
 863 
 864     lo = NULL;
 865     hi = NULL;
 866   }
 867 
 868   if (lo && hi) {
 869     // Merge the two compares into a single unsigned compare by building (CmpU (n - lo) (hi - lo))
 870     Node* adjusted_val = igvn->transform(new SubINode(n,  lo));
 871     Node* adjusted_lim = igvn->transform(new SubINode(hi, lo));
 872     Node* newcmp = igvn->transform(new CmpUNode(adjusted_val, adjusted_lim));
 873     Node* newbool = igvn->transform(new BoolNode(newcmp, cond));
 874 
 875     igvn->is_IterGVN()->replace_input_of(dom_iff, 1, igvn->intcon(proj->_con));
 876     igvn->hash_delete(this);
 877     set_req(1, newbool);
 878 
 879     return true;
 880   }
 881   return false;
 882 }
 883 
 884 // Merge the branches that trap for this If and the dominating If into
 885 // a single region that branches to the uncommon trap for the
 886 // dominating If
 887 void IfNode::merge_uncommon_traps(ProjNode* proj, ProjNode* success, ProjNode* fail, PhaseIterGVN* igvn) {
 888   ProjNode* otherproj = proj->other_if_proj();
 889 
 890   CallStaticJavaNode* unc = success->is_uncommon_trap_proj(Deoptimization::Reason_none);
 891   CallStaticJavaNode* dom_unc = otherproj->is_uncommon_trap_proj(Deoptimization::Reason_none);
 892 
 893   if (unc != dom_unc) {
 894     Node* r = new RegionNode(3);
 895 
 896     r->set_req(1, otherproj);
 897     r->set_req(2, success);
 898     r = igvn->transform(r);
 899     assert(r->is_Region(), "can't go away");
 900 
 901     // Make both If trap at the state of the first If: once the CmpI
 902     // nodes are merged, if we trap we don't know which of the CmpI
 903     // nodes would have caused the trap so we have to restart
 904     // execution at the first one
 905     igvn->replace_input_of(dom_unc, 0, r);
 906     igvn->replace_input_of(unc, 0, igvn->C->top());
 907   }
 908   int trap_request = dom_unc->uncommon_trap_request();
 909   Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(trap_request);
 910   Deoptimization::DeoptAction action = Deoptimization::trap_request_action(trap_request);
 911 
 912   int flip_test = 0;
 913   Node* l = NULL;
 914   Node* r = NULL;
 915 
 916   if (success->in(0)->as_If()->range_check_trap_proj(flip_test, l, r) != NULL) {
 917     // If this looks like a range check, change the trap to
 918     // Reason_range_check so the compiler recognizes it as a range
 919     // check and applies the corresponding optimizations
 920     trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_range_check, action);
 921 
 922     improve_address_types(l, r, fail, igvn);
 923   } else if (unc != dom_unc) {
 924     // If we trap we won't know what CmpI would have caused the trap
 925     // so use a special trap reason to mark this pair of CmpI nodes as
 926     // bad candidate for folding. On recompilation we won't fold them
 927     // and we may trap again but this time we'll know what branch
 928     // traps
 929     trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_unstable_fused_if, action);
 930   }
 931   igvn->replace_input_of(dom_unc, TypeFunc::Parms, igvn->intcon(trap_request));
 932 }
 933 
 934 // If we are turning 2 CmpI nodes into a CmpU that follows the pattern
 935 // of a rangecheck on index i, on 64 bit the compares may be followed
 936 // by memory accesses using i as index. In that case, the CmpU tells
 937 // us something about the values taken by i that can help the compiler
 938 // (see Compile::conv_I2X_index())
 939 void IfNode::improve_address_types(Node* l, Node* r, ProjNode* fail, PhaseIterGVN* igvn) {
 940 #ifdef _LP64
 941   ResourceMark rm;
 942   Node_Stack stack(2);
 943 
 944   assert(r->Opcode() == Op_LoadRange, "unexpected range check");
 945   const TypeInt* array_size = igvn->type(r)->is_int();
 946 
 947   stack.push(l, 0);
 948 
 949   while(stack.size() > 0) {
 950     Node* n = stack.node();
 951     uint start = stack.index();
 952 
 953     uint i = start;
 954     for (; i < n->outcnt(); i++) {
 955       Node* use = n->raw_out(i);
 956       if (stack.size() == 1) {
 957         if (use->Opcode() == Op_ConvI2L) {
 958           const TypeLong* bounds = use->as_Type()->type()->is_long();
 959           if (bounds->_lo <= array_size->_lo && bounds->_hi >= array_size->_hi &&
 960               (bounds->_lo != array_size->_lo || bounds->_hi != array_size->_hi)) {
 961             stack.set_index(i+1);
 962             stack.push(use, 0);
 963             break;
 964           }
 965         }
 966       } else if (use->is_Mem()) {
 967         Node* ctrl = use->in(0);
 968         for (int i = 0; i < 10 && ctrl != NULL && ctrl != fail; i++) {
 969           ctrl = up_one_dom(ctrl);
 970         }
 971         if (ctrl == fail) {
 972           Node* init_n = stack.node_at(1);
 973           assert(init_n->Opcode() == Op_ConvI2L, "unexpected first node");
 974           Node* new_n = igvn->C->conv_I2X_index(igvn, l, array_size);
 975 
 976           for (uint j = 2; j < stack.size(); j++) {
 977             Node* n = stack.node_at(j);
 978             Node* clone = n->clone();
 979             int rep = clone->replace_edge(init_n, new_n);
 980             assert(rep > 0, "can't find expected node?");
 981             clone = igvn->transform(clone);
 982             init_n = n;
 983             new_n = clone;
 984           }
 985           igvn->hash_delete(use);
 986           int rep = use->replace_edge(init_n, new_n);
 987           assert(rep > 0, "can't find expected node?");
 988           igvn->transform(use);
 989           if (init_n->outcnt() == 0) {
 990             igvn->_worklist.push(init_n);
 991           }
 992         }
 993       } else if (use->in(0) == NULL && (igvn->type(use)->isa_long() ||
 994                                         igvn->type(use)->isa_ptr())) {
 995         stack.set_index(i+1);
 996         stack.push(use, 0);
 997         break;
 998       }
 999     }
1000     if (i == n->outcnt()) {
1001       stack.pop();
1002     }
1003   }
1004 #endif
1005 }
1006 
1007 bool IfNode::is_cmp_with_loadrange(ProjNode* proj) {
1008   if (in(1) != NULL &&
1009       in(1)->in(1) != NULL &&
1010       in(1)->in(1)->in(2) != NULL) {
1011     Node* other = in(1)->in(1)->in(2);
1012     if (other->Opcode() == Op_LoadRange &&
1013         ((other->in(0) != NULL && other->in(0) == proj) ||
1014          (other->in(0) == NULL &&
1015           other->in(2) != NULL &&
1016           other->in(2)->is_AddP() &&
1017           other->in(2)->in(1) != NULL &&
1018           other->in(2)->in(1)->Opcode() == Op_CastPP &&
1019           other->in(2)->in(1)->in(0) == proj))) {
1020       return true;
1021     }
1022   }
1023   return false;
1024 }
1025 
1026 bool IfNode::is_null_check(ProjNode* proj, PhaseIterGVN* igvn) {
1027   Node* other = in(1)->in(1)->in(2);
1028   if (other->in(MemNode::Address) != NULL &&
1029       proj->in(0)->in(1) != NULL &&
1030       proj->in(0)->in(1)->is_Bool() &&
1031       proj->in(0)->in(1)->in(1) != NULL &&
1032       proj->in(0)->in(1)->in(1)->Opcode() == Op_CmpP &&
1033       proj->in(0)->in(1)->in(1)->in(2) != NULL &&
1034       proj->in(0)->in(1)->in(1)->in(1) == other->in(MemNode::Address)->in(AddPNode::Address)->uncast() &&
1035       igvn->type(proj->in(0)->in(1)->in(1)->in(2)) == TypePtr::NULL_PTR) {
1036     return true;
1037   }
1038   return false;
1039 }
1040 
1041 // Check that the If that is in between the 2 integer comparisons has
1042 // no side effect
1043 bool IfNode::is_side_effect_free_test(ProjNode* proj, PhaseIterGVN* igvn) {
1044   if (proj != NULL &&
1045       proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none) &&
1046       proj->outcnt() <= 2) {
1047     if (proj->outcnt() == 1 ||
1048         // Allow simple null check from LoadRange
1049         (is_cmp_with_loadrange(proj) && is_null_check(proj, igvn))) {
1050       CallStaticJavaNode* unc = proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none);
1051       CallStaticJavaNode* dom_unc = proj->in(0)->in(0)->as_Proj()->is_uncommon_trap_if_pattern(Deoptimization::Reason_none);
1052 
1053       // reroute_side_effect_free_unc changes the state of this
1054       // uncommon trap to restart execution at the previous
1055       // CmpI. Check that this change in a previous compilation didn't
1056       // cause too many traps.
1057       int trap_request = unc->uncommon_trap_request();
1058       Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(trap_request);
1059 
1060       if (igvn->C->too_many_traps(dom_unc->jvms()->method(), dom_unc->jvms()->bci(), reason)) {
1061         return false;
1062       }
1063 
1064       return true;
1065     }
1066   }
1067   return false;
1068 }
1069 
1070 // Make the If between the 2 integer comparisons trap at the state of
1071 // the first If: the last CmpI is the one replaced by a CmpU and the
1072 // first CmpI is eliminated, so the test between the 2 CmpI nodes
1073 // won't be guarded by the first CmpI anymore. It can trap in cases
1074 // where the first CmpI would have prevented it from executing: on a
1075 // trap, we need to restart execution at the state of the first CmpI
1076 void IfNode::reroute_side_effect_free_unc(ProjNode* proj, ProjNode* dom_proj, PhaseIterGVN* igvn) {
1077   CallStaticJavaNode* dom_unc = dom_proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none);
1078   ProjNode* otherproj = proj->other_if_proj();
1079   CallStaticJavaNode* unc = proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none);
1080   Node* call_proj = dom_unc->unique_ctrl_out();
1081   Node* halt = call_proj->unique_ctrl_out();
1082 
1083   Node* new_unc = dom_unc->clone();
1084   call_proj = call_proj->clone();
1085   halt = halt->clone();
1086   Node* c = otherproj->clone();
1087 
1088   c = igvn->transform(c);
1089   new_unc->set_req(TypeFunc::Parms, unc->in(TypeFunc::Parms));
1090   new_unc->set_req(0, c);
1091   new_unc = igvn->transform(new_unc);
1092   call_proj->set_req(0, new_unc);
1093   call_proj = igvn->transform(call_proj);
1094   halt->set_req(0, call_proj);
1095   halt = igvn->transform(halt);
1096 
1097   igvn->replace_node(otherproj, igvn->C->top());
1098   igvn->C->root()->add_req(halt);
1099 }
1100 
1101 Node* IfNode::fold_compares(PhaseIterGVN* igvn) {
1102   if (Opcode() != Op_If) return NULL;
1103 
1104   if (cmpi_folds(igvn)) {
1105     Node* ctrl = in(0);
1106     if (is_ctrl_folds(ctrl, igvn) &&
1107         ctrl->outcnt() == 1) {
1108       // A integer comparison immediately dominated by another integer
1109       // comparison
1110       ProjNode* success = NULL;
1111       ProjNode* fail = NULL;
1112       ProjNode* dom_cmp = ctrl->as_Proj();
1113       if (has_shared_region(dom_cmp, success, fail) &&
1114           // Next call modifies graph so must be last
1115           fold_compares_helper(dom_cmp, success, fail, igvn)) {
1116         return this;
1117       }
1118       if (has_only_uncommon_traps(dom_cmp, success, fail, igvn) &&
1119           // Next call modifies graph so must be last
1120           fold_compares_helper(dom_cmp, success, fail, igvn)) {
1121         merge_uncommon_traps(dom_cmp, success, fail, igvn);
1122         return this;
1123       }
1124       return NULL;
1125     } else if (ctrl->in(0) != NULL &&
1126                ctrl->in(0)->in(0) != NULL) {
1127       ProjNode* success = NULL;
1128       ProjNode* fail = NULL;
1129       Node* dom = ctrl->in(0)->in(0);
1130       ProjNode* dom_cmp = dom->isa_Proj();
1131       ProjNode* other_cmp = ctrl->isa_Proj();
1132 
1133       // Check if it's an integer comparison dominated by another
1134       // integer comparison with another test in between
1135       if (is_ctrl_folds(dom, igvn) &&
1136           has_only_uncommon_traps(dom_cmp, success, fail, igvn) &&
1137           is_side_effect_free_test(other_cmp, igvn) &&
1138           // Next call modifies graph so must be last
1139           fold_compares_helper(dom_cmp, success, fail, igvn)) {
1140         reroute_side_effect_free_unc(other_cmp, dom_cmp, igvn);
1141         merge_uncommon_traps(dom_cmp, success, fail, igvn);
1142         return this;
1143       }
1144     }
1145   }
1146   return NULL;
1147 }
1148 
1149 //------------------------------remove_useless_bool----------------------------
1150 // Check for people making a useless boolean: things like
1151 // if( (x < y ? true : false) ) { ... }
1152 // Replace with if( x < y ) { ... }
1153 static Node *remove_useless_bool(IfNode *iff, PhaseGVN *phase) {
1154   Node *i1 = iff->in(1);
1155   if( !i1->is_Bool() ) return NULL;
1156   BoolNode *bol = i1->as_Bool();
1157 
1158   Node *cmp = bol->in(1);
1159   if( cmp->Opcode() != Op_CmpI ) return NULL;
1160 
1161   // Must be comparing against a bool
1162   const Type *cmp2_t = phase->type( cmp->in(2) );
1163   if( cmp2_t != TypeInt::ZERO &&
1164       cmp2_t != TypeInt::ONE )
1165     return NULL;
1166 
1167   // Find a prior merge point merging the boolean
1168   i1 = cmp->in(1);
1169   if( !i1->is_Phi() ) return NULL;
1170   PhiNode *phi = i1->as_Phi();
1171   if( phase->type( phi ) != TypeInt::BOOL )
1172     return NULL;
1173 
1174   // Check for diamond pattern
1175   int true_path = phi->is_diamond_phi();
1176   if( true_path == 0 ) return NULL;
1177 
1178   // Make sure that iff and the control of the phi are different. This
1179   // should really only happen for dead control flow since it requires
1180   // an illegal cycle.
1181   if (phi->in(0)->in(1)->in(0) == iff) return NULL;
1182 
1183   // phi->region->if_proj->ifnode->bool->cmp
1184   BoolNode *bol2 = phi->in(0)->in(1)->in(0)->in(1)->as_Bool();
1185 
1186   // Now get the 'sense' of the test correct so we can plug in
1187   // either iff2->in(1) or its complement.
1188   int flip = 0;
1189   if( bol->_test._test == BoolTest::ne ) flip = 1-flip;
1190   else if( bol->_test._test != BoolTest::eq ) return NULL;
1191   if( cmp2_t == TypeInt::ZERO ) flip = 1-flip;
1192 
1193   const Type *phi1_t = phase->type( phi->in(1) );
1194   const Type *phi2_t = phase->type( phi->in(2) );
1195   // Check for Phi(0,1) and flip
1196   if( phi1_t == TypeInt::ZERO ) {
1197     if( phi2_t != TypeInt::ONE ) return NULL;
1198     flip = 1-flip;
1199   } else {
1200     // Check for Phi(1,0)
1201     if( phi1_t != TypeInt::ONE  ) return NULL;
1202     if( phi2_t != TypeInt::ZERO ) return NULL;
1203   }
1204   if( true_path == 2 ) {
1205     flip = 1-flip;
1206   }
1207 
1208   Node* new_bol = (flip ? phase->transform( bol2->negate(phase) ) : bol2);
1209   assert(new_bol != iff->in(1), "must make progress");
1210   iff->set_req(1, new_bol);
1211   // Intervening diamond probably goes dead
1212   phase->C->set_major_progress();
1213   return iff;
1214 }
1215 
1216 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff);
1217 
1218 struct RangeCheck {
1219   Node* ctl;
1220   jint off;
1221 };
1222 
1223 //------------------------------Ideal------------------------------------------
1224 // Return a node which is more "ideal" than the current node.  Strip out
1225 // control copies
1226 Node *IfNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1227   if (remove_dead_region(phase, can_reshape))  return this;
1228   // No Def-Use info?
1229   if (!can_reshape)  return NULL;
1230   PhaseIterGVN *igvn = phase->is_IterGVN();
1231 
1232   // Don't bother trying to transform a dead if
1233   if (in(0)->is_top())  return NULL;
1234   // Don't bother trying to transform an if with a dead test
1235   if (in(1)->is_top())  return NULL;
1236   // Another variation of a dead test
1237   if (in(1)->is_Con())  return NULL;
1238   // Another variation of a dead if
1239   if (outcnt() < 2)  return NULL;
1240 
1241   // Canonicalize the test.
1242   Node* idt_if = idealize_test(phase, this);
1243   if (idt_if != NULL)  return idt_if;
1244 
1245   // Try to split the IF
1246   Node *s = split_if(this, igvn);
1247   if (s != NULL)  return s;
1248 
1249   // Check for people making a useless boolean: things like
1250   // if( (x < y ? true : false) ) { ... }
1251   // Replace with if( x < y ) { ... }
1252   Node *bol2 = remove_useless_bool(this, phase);
1253   if( bol2 ) return bol2;
1254 
1255   // Setup to scan up the CFG looking for a dominating test
1256   Node *dom = in(0);
1257   Node *prev_dom = this;
1258 
1259   // Check for range-check vs other kinds of tests
1260   Node *index1, *range1;
1261   jint offset1;
1262   int flip1 = is_range_check(range1, index1, offset1);
1263   if( flip1 ) {
1264     // Try to remove extra range checks.  All 'up_one_dom' gives up at merges
1265     // so all checks we inspect post-dominate the top-most check we find.
1266     // If we are going to fail the current check and we reach the top check
1267     // then we are guaranteed to fail, so just start interpreting there.
1268     // We 'expand' the top 3 range checks to include all post-dominating
1269     // checks.
1270 
1271     // The top 3 range checks seen
1272     const int NRC =3;
1273     RangeCheck prev_checks[NRC];
1274     int nb_checks = 0;
1275 
1276     // Low and high offsets seen so far
1277     jint off_lo = offset1;
1278     jint off_hi = offset1;
1279 
1280     bool found_immediate_dominator = false;
1281 
1282     // Scan for the top checks and collect range of offsets
1283     for (int dist = 0; dist < 999; dist++) { // Range-Check scan limit
1284       if (dom->Opcode() == Op_If &&  // Not same opcode?
1285           prev_dom->in(0) == dom) { // One path of test does dominate?
1286         if (dom == this) return NULL; // dead loop
1287         // See if this is a range check
1288         Node *index2, *range2;
1289         jint offset2;
1290         int flip2 = dom->as_If()->is_range_check(range2, index2, offset2);
1291         // See if this is a _matching_ range check, checking against
1292         // the same array bounds.
1293         if (flip2 == flip1 && range2 == range1 && index2 == index1 &&
1294             dom->outcnt() == 2) {
1295           if (nb_checks == 0 && dom->in(1) == in(1)) {
1296             // Found an immediately dominating test at the same offset.
1297             // This kind of back-to-back test can be eliminated locally,
1298             // and there is no need to search further for dominating tests.
1299             assert(offset2 == offset1, "Same test but different offsets");
1300             found_immediate_dominator = true;
1301             break;
1302           }
1303           // Gather expanded bounds
1304           off_lo = MIN2(off_lo,offset2);
1305           off_hi = MAX2(off_hi,offset2);
1306           // Record top NRC range checks
1307           prev_checks[nb_checks%NRC].ctl = prev_dom;
1308           prev_checks[nb_checks%NRC].off = offset2;
1309           nb_checks++;
1310         }
1311       }
1312       prev_dom = dom;
1313       dom = up_one_dom(dom);
1314       if (!dom) break;
1315     }
1316 
1317     if (!found_immediate_dominator) {
1318       // Attempt to widen the dominating range check to cover some later
1319       // ones.  Since range checks "fail" by uncommon-trapping to the
1320       // interpreter, widening a check can make us speculatively enter
1321       // the interpreter.  If we see range-check deopt's, do not widen!
1322       if (!phase->C->allow_range_check_smearing())  return NULL;
1323 
1324       // Didn't find prior covering check, so cannot remove anything.
1325       if (nb_checks == 0) {
1326         return NULL;
1327       }
1328       // Constant indices only need to check the upper bound.
1329       // Non-constant indices must check both low and high.
1330       int chk0 = (nb_checks - 1) % NRC;
1331       if (index1) {
1332         if (nb_checks == 1) {
1333           return NULL;
1334         } else {
1335           // If the top range check's constant is the min or max of
1336           // all constants we widen the next one to cover the whole
1337           // range of constants.
1338           RangeCheck rc0 = prev_checks[chk0];
1339           int chk1 = (nb_checks - 2) % NRC;
1340           RangeCheck rc1 = prev_checks[chk1];
1341           if (rc0.off == off_lo) {
1342             adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn);
1343             prev_dom = rc1.ctl;
1344           } else if (rc0.off == off_hi) {
1345             adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn);
1346             prev_dom = rc1.ctl;
1347           } else {
1348             // If the top test's constant is not the min or max of all
1349             // constants, we need 3 range checks. We must leave the
1350             // top test unchanged because widening it would allow the
1351             // accesses it protects to successfully read/write out of
1352             // bounds.
1353             if (nb_checks == 2) {
1354               return NULL;
1355             }
1356             int chk2 = (nb_checks - 3) % NRC;
1357             RangeCheck rc2 = prev_checks[chk2];
1358             // The top range check a+i covers interval: -a <= i < length-a
1359             // The second range check b+i covers interval: -b <= i < length-b
1360             if (rc1.off <= rc0.off) {
1361               // if b <= a, we change the second range check to:
1362               // -min_of_all_constants <= i < length-min_of_all_constants
1363               // Together top and second range checks now cover:
1364               // -min_of_all_constants <= i < length-a
1365               // which is more restrictive than -b <= i < length-b:
1366               // -b <= -min_of_all_constants <= i < length-a <= length-b
1367               // The third check is then changed to:
1368               // -max_of_all_constants <= i < length-max_of_all_constants
1369               // so 2nd and 3rd checks restrict allowed values of i to:
1370               // -min_of_all_constants <= i < length-max_of_all_constants
1371               adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn);
1372               adjust_check(rc2.ctl, range1, index1, flip1, off_hi, igvn);
1373             } else {
1374               // if b > a, we change the second range check to:
1375               // -max_of_all_constants <= i < length-max_of_all_constants
1376               // Together top and second range checks now cover:
1377               // -a <= i < length-max_of_all_constants
1378               // which is more restrictive than -b <= i < length-b:
1379               // -b < -a <= i < length-max_of_all_constants <= length-b
1380               // The third check is then changed to:
1381               // -max_of_all_constants <= i < length-max_of_all_constants
1382               // so 2nd and 3rd checks restrict allowed values of i to:
1383               // -min_of_all_constants <= i < length-max_of_all_constants
1384               adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn);
1385               adjust_check(rc2.ctl, range1, index1, flip1, off_lo, igvn);
1386             }
1387             prev_dom = rc2.ctl;
1388           }
1389         }
1390       } else {
1391         RangeCheck rc0 = prev_checks[chk0];
1392         // 'Widen' the offset of the 1st and only covering check
1393         adjust_check(rc0.ctl, range1, index1, flip1, off_hi, igvn);
1394         // Test is now covered by prior checks, dominate it out
1395         prev_dom = rc0.ctl;
1396       }
1397     }
1398 
1399   } else {                      // Scan for an equivalent test
1400 
1401     Node *cmp;
1402     int dist = 0;               // Cutoff limit for search
1403     int op = Opcode();
1404     if( op == Op_If &&
1405         (cmp=in(1)->in(1))->Opcode() == Op_CmpP ) {
1406       if( cmp->in(2) != NULL && // make sure cmp is not already dead
1407           cmp->in(2)->bottom_type() == TypePtr::NULL_PTR ) {
1408         dist = 64;              // Limit for null-pointer scans
1409       } else {
1410         dist = 4;               // Do not bother for random pointer tests
1411       }
1412     } else {
1413       dist = 4;                 // Limit for random junky scans
1414     }
1415 
1416     // Normal equivalent-test check.
1417     if( !dom ) return NULL;     // Dead loop?
1418 
1419     Node* result = fold_compares(igvn);
1420     if (result != NULL) {
1421       return result;
1422     }
1423 
1424     // Search up the dominator tree for an If with an identical test
1425     while( dom->Opcode() != op    ||  // Not same opcode?
1426            dom->in(1)    != in(1) ||  // Not same input 1?
1427            (req() == 3 && dom->in(2) != in(2)) || // Not same input 2?
1428            prev_dom->in(0) != dom ) { // One path of test does not dominate?
1429       if( dist < 0 ) return NULL;
1430 
1431       dist--;
1432       prev_dom = dom;
1433       dom = up_one_dom( dom );
1434       if( !dom ) return NULL;
1435     }
1436 
1437     // Check that we did not follow a loop back to ourselves
1438     if( this == dom )
1439       return NULL;
1440 
1441     if( dist > 2 )              // Add to count of NULL checks elided
1442       explicit_null_checks_elided++;
1443 
1444   } // End of Else scan for an equivalent test
1445 
1446   // Hit!  Remove this IF
1447 #ifndef PRODUCT
1448   if( TraceIterativeGVN ) {
1449     tty->print("   Removing IfNode: "); this->dump();
1450   }
1451   if( VerifyOpto && !phase->allow_progress() ) {
1452     // Found an equivalent dominating test,
1453     // we can not guarantee reaching a fix-point for these during iterativeGVN
1454     // since intervening nodes may not change.
1455     return NULL;
1456   }
1457 #endif
1458 
1459   // Replace dominated IfNode
1460   dominated_by( prev_dom, igvn );
1461 
1462   // Must return either the original node (now dead) or a new node
1463   // (Do not return a top here, since that would break the uniqueness of top.)
1464   return new ConINode(TypeInt::ZERO);
1465 }
1466 
1467 //------------------------------dominated_by-----------------------------------
1468 void IfNode::dominated_by( Node *prev_dom, PhaseIterGVN *igvn ) {
1469   igvn->hash_delete(this);      // Remove self to prevent spurious V-N
1470   Node *idom = in(0);
1471   // Need opcode to decide which way 'this' test goes
1472   int prev_op = prev_dom->Opcode();
1473   Node *top = igvn->C->top(); // Shortcut to top
1474 
1475   // Loop predicates may have depending checks which should not
1476   // be skipped. For example, range check predicate has two checks
1477   // for lower and upper bounds.
1478   ProjNode* unc_proj = proj_out(1 - prev_dom->as_Proj()->_con)->as_Proj();
1479   if (unc_proj->is_uncommon_trap_proj(Deoptimization::Reason_predicate) != NULL)
1480    prev_dom = idom;
1481 
1482   // Now walk the current IfNode's projections.
1483   // Loop ends when 'this' has no more uses.
1484   for (DUIterator_Last imin, i = last_outs(imin); i >= imin; --i) {
1485     Node *ifp = last_out(i);     // Get IfTrue/IfFalse
1486     igvn->add_users_to_worklist(ifp);
1487     // Check which projection it is and set target.
1488     // Data-target is either the dominating projection of the same type
1489     // or TOP if the dominating projection is of opposite type.
1490     // Data-target will be used as the new control edge for the non-CFG
1491     // nodes like Casts and Loads.
1492     Node *data_target = (ifp->Opcode() == prev_op) ? prev_dom : top;
1493     // Control-target is just the If's immediate dominator or TOP.
1494     Node *ctrl_target = (ifp->Opcode() == prev_op) ?     idom : top;
1495 
1496     // For each child of an IfTrue/IfFalse projection, reroute.
1497     // Loop ends when projection has no more uses.
1498     for (DUIterator_Last jmin, j = ifp->last_outs(jmin); j >= jmin; --j) {
1499       Node* s = ifp->last_out(j);   // Get child of IfTrue/IfFalse
1500       if( !s->depends_only_on_test() ) {
1501         // Find the control input matching this def-use edge.
1502         // For Regions it may not be in slot 0.
1503         uint l;
1504         for( l = 0; s->in(l) != ifp; l++ ) { }
1505         igvn->replace_input_of(s, l, ctrl_target);
1506       } else {                      // Else, for control producers,
1507         igvn->replace_input_of(s, 0, data_target); // Move child to data-target
1508       }
1509     } // End for each child of a projection
1510 
1511     igvn->remove_dead_node(ifp);
1512   } // End for each IfTrue/IfFalse child of If
1513 
1514   // Kill the IfNode
1515   igvn->remove_dead_node(this);
1516 }
1517 
1518 //------------------------------Identity---------------------------------------
1519 // If the test is constant & we match, then we are the input Control
1520 Node *IfProjNode::Identity(PhaseTransform *phase) {
1521   // Can only optimize if cannot go the other way
1522   const TypeTuple *t = phase->type(in(0))->is_tuple();
1523   if (t == TypeTuple::IFNEITHER ||
1524       // kill dead branch first otherwise the IfNode's control will
1525       // have 2 control uses (the IfNode that doesn't go away because
1526       // it still has uses and this branch of the
1527       // If). Node::has_special_unique_user() will cause this node to
1528       // be reprocessed once the dead branch is killed.
1529       (always_taken(t) && in(0)->outcnt() == 1)) {
1530     // IfNode control
1531     return in(0)->in(0);
1532   }
1533   // no progress
1534   return this;
1535 }
1536 
1537 //------------------------------dump_spec--------------------------------------
1538 #ifndef PRODUCT
1539 void IfNode::dump_spec(outputStream *st) const {
1540   st->print("P=%f, C=%f",_prob,_fcnt);
1541 }
1542 #endif
1543 
1544 //------------------------------idealize_test----------------------------------
1545 // Try to canonicalize tests better.  Peek at the Cmp/Bool/If sequence and
1546 // come up with a canonical sequence.  Bools getting 'eq', 'gt' and 'ge' forms
1547 // converted to 'ne', 'le' and 'lt' forms.  IfTrue/IfFalse get swapped as
1548 // needed.
1549 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff) {
1550   assert(iff->in(0) != NULL, "If must be live");
1551 
1552   if (iff->outcnt() != 2)  return NULL; // Malformed projections.
1553   Node* old_if_f = iff->proj_out(false);
1554   Node* old_if_t = iff->proj_out(true);
1555 
1556   // CountedLoopEnds want the back-control test to be TRUE, irregardless of
1557   // whether they are testing a 'gt' or 'lt' condition.  The 'gt' condition
1558   // happens in count-down loops
1559   if (iff->is_CountedLoopEnd())  return NULL;
1560   if (!iff->in(1)->is_Bool())  return NULL; // Happens for partially optimized IF tests
1561   BoolNode *b = iff->in(1)->as_Bool();
1562   BoolTest bt = b->_test;
1563   // Test already in good order?
1564   if( bt.is_canonical() )
1565     return NULL;
1566 
1567   // Flip test to be canonical.  Requires flipping the IfFalse/IfTrue and
1568   // cloning the IfNode.
1569   Node* new_b = phase->transform( new BoolNode(b->in(1), bt.negate()) );
1570   if( !new_b->is_Bool() ) return NULL;
1571   b = new_b->as_Bool();
1572 
1573   PhaseIterGVN *igvn = phase->is_IterGVN();
1574   assert( igvn, "Test is not canonical in parser?" );
1575 
1576   // The IF node never really changes, but it needs to be cloned
1577   iff = new IfNode( iff->in(0), b, 1.0-iff->_prob, iff->_fcnt);
1578 
1579   Node *prior = igvn->hash_find_insert(iff);
1580   if( prior ) {
1581     igvn->remove_dead_node(iff);
1582     iff = (IfNode*)prior;
1583   } else {
1584     // Cannot call transform on it just yet
1585     igvn->set_type_bottom(iff);
1586   }
1587   igvn->_worklist.push(iff);
1588 
1589   // Now handle projections.  Cloning not required.
1590   Node* new_if_f = (Node*)(new IfFalseNode( iff ));
1591   Node* new_if_t = (Node*)(new IfTrueNode ( iff ));
1592 
1593   igvn->register_new_node_with_optimizer(new_if_f);
1594   igvn->register_new_node_with_optimizer(new_if_t);
1595   // Flip test, so flip trailing control
1596   igvn->replace_node(old_if_f, new_if_t);
1597   igvn->replace_node(old_if_t, new_if_f);
1598 
1599   // Progress
1600   return iff;
1601 }