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