1 /*
   2  * Copyright (c) 1997, 2018, 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 "compiler/compileLog.hpp"
  27 #include "gc/shared/barrierSet.hpp"
  28 #include "gc/shared/c2/barrierSetC2.hpp"
  29 #include "memory/allocation.inline.hpp"
  30 #include "opto/addnode.hpp"
  31 #include "opto/callnode.hpp"
  32 #include "opto/cfgnode.hpp"
  33 #include "opto/loopnode.hpp"
  34 #include "opto/matcher.hpp"
  35 #include "opto/movenode.hpp"
  36 #include "opto/mulnode.hpp"
  37 #include "opto/opcodes.hpp"
  38 #include "opto/phaseX.hpp"
  39 #include "opto/subnode.hpp"
  40 #include "runtime/sharedRuntime.hpp"
  41 
  42 // Portions of code courtesy of Clifford Click
  43 
  44 // Optimization - Graph Style
  45 
  46 #include "math.h"
  47 
  48 //=============================================================================
  49 //------------------------------Identity---------------------------------------
  50 // If right input is a constant 0, return the left input.
  51 Node* SubNode::Identity(PhaseGVN* phase) {
  52   assert(in(1) != this, "Must already have called Value");
  53   assert(in(2) != this, "Must already have called Value");
  54 
  55   // Remove double negation
  56   const Type *zero = add_id();
  57   if( phase->type( in(1) )->higher_equal( zero ) &&
  58       in(2)->Opcode() == Opcode() &&
  59       phase->type( in(2)->in(1) )->higher_equal( zero ) ) {
  60     return in(2)->in(2);
  61   }
  62 
  63   // Convert "(X+Y) - Y" into X and "(X+Y) - X" into Y
  64   if( in(1)->Opcode() == Op_AddI ) {
  65     if( phase->eqv(in(1)->in(2),in(2)) )
  66       return in(1)->in(1);
  67     if (phase->eqv(in(1)->in(1),in(2)))
  68       return in(1)->in(2);
  69 
  70     // Also catch: "(X + Opaque2(Y)) - Y".  In this case, 'Y' is a loop-varying
  71     // trip counter and X is likely to be loop-invariant (that's how O2 Nodes
  72     // are originally used, although the optimizer sometimes jiggers things).
  73     // This folding through an O2 removes a loop-exit use of a loop-varying
  74     // value and generally lowers register pressure in and around the loop.
  75     if( in(1)->in(2)->Opcode() == Op_Opaque2 &&
  76         phase->eqv(in(1)->in(2)->in(1),in(2)) )
  77       return in(1)->in(1);
  78   }
  79 
  80   return ( phase->type( in(2) )->higher_equal( zero ) ) ? in(1) : this;
  81 }
  82 
  83 //------------------------------Value------------------------------------------
  84 // A subtract node differences it's two inputs.
  85 const Type* SubNode::Value_common(PhaseTransform *phase) const {
  86   const Node* in1 = in(1);
  87   const Node* in2 = in(2);
  88   // Either input is TOP ==> the result is TOP
  89   const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
  90   if( t1 == Type::TOP ) return Type::TOP;
  91   const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
  92   if( t2 == Type::TOP ) return Type::TOP;
  93 
  94   // Not correct for SubFnode and AddFNode (must check for infinity)
  95   // Equal?  Subtract is zero
  96   if (in1->eqv_uncast(in2))  return add_id();
  97 
  98   // Either input is BOTTOM ==> the result is the local BOTTOM
  99   if( t1 == Type::BOTTOM || t2 == Type::BOTTOM )
 100     return bottom_type();
 101 
 102   return NULL;
 103 }
 104 
 105 const Type* SubNode::Value(PhaseGVN* phase) const {
 106   const Type* t = Value_common(phase);
 107   if (t != NULL) {
 108     return t;
 109   }
 110   const Type* t1 = phase->type(in(1));
 111   const Type* t2 = phase->type(in(2));
 112   return sub(t1,t2);            // Local flavor of type subtraction
 113 
 114 }
 115 
 116 //=============================================================================
 117 
 118 //------------------------------Helper function--------------------------------
 119 static bool ok_to_convert(Node* inc, Node* iv) {
 120     // Do not collapse (x+c0)-y if "+" is a loop increment, because the
 121     // "-" is loop invariant and collapsing extends the live-range of "x"
 122     // to overlap with the "+", forcing another register to be used in
 123     // the loop.
 124     // This test will be clearer with '&&' (apply DeMorgan's rule)
 125     // but I like the early cutouts that happen here.
 126     const PhiNode *phi;
 127     if( ( !inc->in(1)->is_Phi() ||
 128           !(phi=inc->in(1)->as_Phi()) ||
 129           phi->is_copy() ||
 130           !phi->region()->is_CountedLoop() ||
 131           inc != phi->region()->as_CountedLoop()->incr() )
 132        &&
 133         // Do not collapse (x+c0)-iv if "iv" is a loop induction variable,
 134         // because "x" maybe invariant.
 135         ( !iv->is_loop_iv() )
 136       ) {
 137       return true;
 138     } else {
 139       return false;
 140     }
 141 }
 142 //------------------------------Ideal------------------------------------------
 143 Node *SubINode::Ideal(PhaseGVN *phase, bool can_reshape){
 144   Node *in1 = in(1);
 145   Node *in2 = in(2);
 146   uint op1 = in1->Opcode();
 147   uint op2 = in2->Opcode();
 148 
 149 #ifdef ASSERT
 150   // Check for dead loop
 151   if( phase->eqv( in1, this ) || phase->eqv( in2, this ) ||
 152       ( ( op1 == Op_AddI || op1 == Op_SubI ) &&
 153         ( phase->eqv( in1->in(1), this ) || phase->eqv( in1->in(2), this ) ||
 154           phase->eqv( in1->in(1), in1  ) || phase->eqv( in1->in(2), in1 ) ) ) )
 155     assert(false, "dead loop in SubINode::Ideal");
 156 #endif
 157 
 158   const Type *t2 = phase->type( in2 );
 159   if( t2 == Type::TOP ) return NULL;
 160   // Convert "x-c0" into "x+ -c0".
 161   if( t2->base() == Type::Int ){        // Might be bottom or top...
 162     const TypeInt *i = t2->is_int();
 163     if( i->is_con() )
 164       return new AddINode(in1, phase->intcon(-i->get_con()));
 165   }
 166 
 167   // Convert "(x+c0) - y" into (x-y) + c0"
 168   // Do not collapse (x+c0)-y if "+" is a loop increment or
 169   // if "y" is a loop induction variable.
 170   if( op1 == Op_AddI && ok_to_convert(in1, in2) ) {
 171     const Type *tadd = phase->type( in1->in(2) );
 172     if( tadd->singleton() && tadd != Type::TOP ) {
 173       Node *sub2 = phase->transform( new SubINode( in1->in(1), in2 ));
 174       return new AddINode( sub2, in1->in(2) );
 175     }
 176   }
 177 
 178 
 179   // Convert "x - (y+c0)" into "(x-y) - c0"
 180   // Need the same check as in above optimization but reversed.
 181   if (op2 == Op_AddI && ok_to_convert(in2, in1)) {
 182     Node* in21 = in2->in(1);
 183     Node* in22 = in2->in(2);
 184     const TypeInt* tcon = phase->type(in22)->isa_int();
 185     if (tcon != NULL && tcon->is_con()) {
 186       Node* sub2 = phase->transform( new SubINode(in1, in21) );
 187       Node* neg_c0 = phase->intcon(- tcon->get_con());
 188       return new AddINode(sub2, neg_c0);
 189     }
 190   }
 191 
 192   const Type *t1 = phase->type( in1 );
 193   if( t1 == Type::TOP ) return NULL;
 194 
 195 #ifdef ASSERT
 196   // Check for dead loop
 197   if( ( op2 == Op_AddI || op2 == Op_SubI ) &&
 198       ( phase->eqv( in2->in(1), this ) || phase->eqv( in2->in(2), this ) ||
 199         phase->eqv( in2->in(1), in2  ) || phase->eqv( in2->in(2), in2  ) ) )
 200     assert(false, "dead loop in SubINode::Ideal");
 201 #endif
 202 
 203   // Convert "x - (x+y)" into "-y"
 204   if( op2 == Op_AddI &&
 205       phase->eqv( in1, in2->in(1) ) )
 206     return new SubINode( phase->intcon(0),in2->in(2));
 207   // Convert "(x-y) - x" into "-y"
 208   if( op1 == Op_SubI &&
 209       phase->eqv( in1->in(1), in2 ) )
 210     return new SubINode( phase->intcon(0),in1->in(2));
 211   // Convert "x - (y+x)" into "-y"
 212   if( op2 == Op_AddI &&
 213       phase->eqv( in1, in2->in(2) ) )
 214     return new SubINode( phase->intcon(0),in2->in(1));
 215 
 216   // Convert "0 - (x-y)" into "y-x"
 217   if( t1 == TypeInt::ZERO && op2 == Op_SubI )
 218     return new SubINode( in2->in(2), in2->in(1) );
 219 
 220   // Convert "0 - (x+con)" into "-con-x"
 221   jint con;
 222   if( t1 == TypeInt::ZERO && op2 == Op_AddI &&
 223       (con = in2->in(2)->find_int_con(0)) != 0 )
 224     return new SubINode( phase->intcon(-con), in2->in(1) );
 225 
 226   // Convert "(X+A) - (X+B)" into "A - B"
 227   if( op1 == Op_AddI && op2 == Op_AddI && in1->in(1) == in2->in(1) )
 228     return new SubINode( in1->in(2), in2->in(2) );
 229 
 230   // Convert "(A+X) - (B+X)" into "A - B"
 231   if( op1 == Op_AddI && op2 == Op_AddI && in1->in(2) == in2->in(2) )
 232     return new SubINode( in1->in(1), in2->in(1) );
 233 
 234   // Convert "(A+X) - (X+B)" into "A - B"
 235   if( op1 == Op_AddI && op2 == Op_AddI && in1->in(2) == in2->in(1) )
 236     return new SubINode( in1->in(1), in2->in(2) );
 237 
 238   // Convert "(X+A) - (B+X)" into "A - B"
 239   if( op1 == Op_AddI && op2 == Op_AddI && in1->in(1) == in2->in(2) )
 240     return new SubINode( in1->in(2), in2->in(1) );
 241 
 242   // Convert "A-(B-C)" into (A+C)-B", since add is commutative and generally
 243   // nicer to optimize than subtract.
 244   if( op2 == Op_SubI && in2->outcnt() == 1) {
 245     Node *add1 = phase->transform( new AddINode( in1, in2->in(2) ) );
 246     return new SubINode( add1, in2->in(1) );
 247   }
 248 
 249   return NULL;
 250 }
 251 
 252 //------------------------------sub--------------------------------------------
 253 // A subtract node differences it's two inputs.
 254 const Type *SubINode::sub( const Type *t1, const Type *t2 ) const {
 255   const TypeInt *r0 = t1->is_int(); // Handy access
 256   const TypeInt *r1 = t2->is_int();
 257   int32_t lo = java_subtract(r0->_lo, r1->_hi);
 258   int32_t hi = java_subtract(r0->_hi, r1->_lo);
 259 
 260   // We next check for 32-bit overflow.
 261   // If that happens, we just assume all integers are possible.
 262   if( (((r0->_lo ^ r1->_hi) >= 0) ||    // lo ends have same signs OR
 263        ((r0->_lo ^      lo) >= 0)) &&   // lo results have same signs AND
 264       (((r0->_hi ^ r1->_lo) >= 0) ||    // hi ends have same signs OR
 265        ((r0->_hi ^      hi) >= 0)) )    // hi results have same signs
 266     return TypeInt::make(lo,hi,MAX2(r0->_widen,r1->_widen));
 267   else                          // Overflow; assume all integers
 268     return TypeInt::INT;
 269 }
 270 
 271 //=============================================================================
 272 //------------------------------Ideal------------------------------------------
 273 Node *SubLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 274   Node *in1 = in(1);
 275   Node *in2 = in(2);
 276   uint op1 = in1->Opcode();
 277   uint op2 = in2->Opcode();
 278 
 279 #ifdef ASSERT
 280   // Check for dead loop
 281   if( phase->eqv( in1, this ) || phase->eqv( in2, this ) ||
 282       ( ( op1 == Op_AddL || op1 == Op_SubL ) &&
 283         ( phase->eqv( in1->in(1), this ) || phase->eqv( in1->in(2), this ) ||
 284           phase->eqv( in1->in(1), in1  ) || phase->eqv( in1->in(2), in1  ) ) ) )
 285     assert(false, "dead loop in SubLNode::Ideal");
 286 #endif
 287 
 288   if( phase->type( in2 ) == Type::TOP ) return NULL;
 289   const TypeLong *i = phase->type( in2 )->isa_long();
 290   // Convert "x-c0" into "x+ -c0".
 291   if( i &&                      // Might be bottom or top...
 292       i->is_con() )
 293     return new AddLNode(in1, phase->longcon(-i->get_con()));
 294 
 295   // Convert "(x+c0) - y" into (x-y) + c0"
 296   // Do not collapse (x+c0)-y if "+" is a loop increment or
 297   // if "y" is a loop induction variable.
 298   if( op1 == Op_AddL && ok_to_convert(in1, in2) ) {
 299     Node *in11 = in1->in(1);
 300     const Type *tadd = phase->type( in1->in(2) );
 301     if( tadd->singleton() && tadd != Type::TOP ) {
 302       Node *sub2 = phase->transform( new SubLNode( in11, in2 ));
 303       return new AddLNode( sub2, in1->in(2) );
 304     }
 305   }
 306 
 307   // Convert "x - (y+c0)" into "(x-y) - c0"
 308   // Need the same check as in above optimization but reversed.
 309   if (op2 == Op_AddL && ok_to_convert(in2, in1)) {
 310     Node* in21 = in2->in(1);
 311     Node* in22 = in2->in(2);
 312     const TypeLong* tcon = phase->type(in22)->isa_long();
 313     if (tcon != NULL && tcon->is_con()) {
 314       Node* sub2 = phase->transform( new SubLNode(in1, in21) );
 315       Node* neg_c0 = phase->longcon(- tcon->get_con());
 316       return new AddLNode(sub2, neg_c0);
 317     }
 318   }
 319 
 320   const Type *t1 = phase->type( in1 );
 321   if( t1 == Type::TOP ) return NULL;
 322 
 323 #ifdef ASSERT
 324   // Check for dead loop
 325   if( ( op2 == Op_AddL || op2 == Op_SubL ) &&
 326       ( phase->eqv( in2->in(1), this ) || phase->eqv( in2->in(2), this ) ||
 327         phase->eqv( in2->in(1), in2  ) || phase->eqv( in2->in(2), in2  ) ) )
 328     assert(false, "dead loop in SubLNode::Ideal");
 329 #endif
 330 
 331   // Convert "x - (x+y)" into "-y"
 332   if( op2 == Op_AddL &&
 333       phase->eqv( in1, in2->in(1) ) )
 334     return new SubLNode( phase->makecon(TypeLong::ZERO), in2->in(2));
 335   // Convert "x - (y+x)" into "-y"
 336   if( op2 == Op_AddL &&
 337       phase->eqv( in1, in2->in(2) ) )
 338     return new SubLNode( phase->makecon(TypeLong::ZERO),in2->in(1));
 339 
 340   // Convert "0 - (x-y)" into "y-x"
 341   if( phase->type( in1 ) == TypeLong::ZERO && op2 == Op_SubL )
 342     return new SubLNode( in2->in(2), in2->in(1) );
 343 
 344   // Convert "(X+A) - (X+B)" into "A - B"
 345   if( op1 == Op_AddL && op2 == Op_AddL && in1->in(1) == in2->in(1) )
 346     return new SubLNode( in1->in(2), in2->in(2) );
 347 
 348   // Convert "(A+X) - (B+X)" into "A - B"
 349   if( op1 == Op_AddL && op2 == Op_AddL && in1->in(2) == in2->in(2) )
 350     return new SubLNode( in1->in(1), in2->in(1) );
 351 
 352   // Convert "A-(B-C)" into (A+C)-B"
 353   if( op2 == Op_SubL && in2->outcnt() == 1) {
 354     Node *add1 = phase->transform( new AddLNode( in1, in2->in(2) ) );
 355     return new SubLNode( add1, in2->in(1) );
 356   }
 357 
 358   return NULL;
 359 }
 360 
 361 //------------------------------sub--------------------------------------------
 362 // A subtract node differences it's two inputs.
 363 const Type *SubLNode::sub( const Type *t1, const Type *t2 ) const {
 364   const TypeLong *r0 = t1->is_long(); // Handy access
 365   const TypeLong *r1 = t2->is_long();
 366   jlong lo = java_subtract(r0->_lo, r1->_hi);
 367   jlong hi = java_subtract(r0->_hi, r1->_lo);
 368 
 369   // We next check for 32-bit overflow.
 370   // If that happens, we just assume all integers are possible.
 371   if( (((r0->_lo ^ r1->_hi) >= 0) ||    // lo ends have same signs OR
 372        ((r0->_lo ^      lo) >= 0)) &&   // lo results have same signs AND
 373       (((r0->_hi ^ r1->_lo) >= 0) ||    // hi ends have same signs OR
 374        ((r0->_hi ^      hi) >= 0)) )    // hi results have same signs
 375     return TypeLong::make(lo,hi,MAX2(r0->_widen,r1->_widen));
 376   else                          // Overflow; assume all integers
 377     return TypeLong::LONG;
 378 }
 379 
 380 //=============================================================================
 381 //------------------------------Value------------------------------------------
 382 // A subtract node differences its two inputs.
 383 const Type* SubFPNode::Value(PhaseGVN* phase) const {
 384   const Node* in1 = in(1);
 385   const Node* in2 = in(2);
 386   // Either input is TOP ==> the result is TOP
 387   const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
 388   if( t1 == Type::TOP ) return Type::TOP;
 389   const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
 390   if( t2 == Type::TOP ) return Type::TOP;
 391 
 392   // if both operands are infinity of same sign, the result is NaN; do
 393   // not replace with zero
 394   if( (t1->is_finite() && t2->is_finite()) ) {
 395     if( phase->eqv(in1, in2) ) return add_id();
 396   }
 397 
 398   // Either input is BOTTOM ==> the result is the local BOTTOM
 399   const Type *bot = bottom_type();
 400   if( (t1 == bot) || (t2 == bot) ||
 401       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 402     return bot;
 403 
 404   return sub(t1,t2);            // Local flavor of type subtraction
 405 }
 406 
 407 
 408 //=============================================================================
 409 //------------------------------Ideal------------------------------------------
 410 Node *SubFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 411   const Type *t2 = phase->type( in(2) );
 412   // Convert "x-c0" into "x+ -c0".
 413   if( t2->base() == Type::FloatCon ) {  // Might be bottom or top...
 414     // return new (phase->C, 3) AddFNode(in(1), phase->makecon( TypeF::make(-t2->getf()) ) );
 415   }
 416 
 417   // Not associative because of boundary conditions (infinity)
 418   if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
 419     // Convert "x - (x+y)" into "-y"
 420     if( in(2)->is_Add() &&
 421         phase->eqv(in(1),in(2)->in(1) ) )
 422       return new SubFNode( phase->makecon(TypeF::ZERO),in(2)->in(2));
 423   }
 424 
 425   // Cannot replace 0.0-X with -X because a 'fsub' bytecode computes
 426   // 0.0-0.0 as +0.0, while a 'fneg' bytecode computes -0.0.
 427   //if( phase->type(in(1)) == TypeF::ZERO )
 428   //return new (phase->C, 2) NegFNode(in(2));
 429 
 430   return NULL;
 431 }
 432 
 433 //------------------------------sub--------------------------------------------
 434 // A subtract node differences its two inputs.
 435 const Type *SubFNode::sub( const Type *t1, const Type *t2 ) const {
 436   // no folding if one of operands is infinity or NaN, do not do constant folding
 437   if( g_isfinite(t1->getf()) && g_isfinite(t2->getf()) ) {
 438     return TypeF::make( t1->getf() - t2->getf() );
 439   }
 440   else if( g_isnan(t1->getf()) ) {
 441     return t1;
 442   }
 443   else if( g_isnan(t2->getf()) ) {
 444     return t2;
 445   }
 446   else {
 447     return Type::FLOAT;
 448   }
 449 }
 450 
 451 //=============================================================================
 452 //------------------------------Ideal------------------------------------------
 453 Node *SubDNode::Ideal(PhaseGVN *phase, bool can_reshape){
 454   const Type *t2 = phase->type( in(2) );
 455   // Convert "x-c0" into "x+ -c0".
 456   if( t2->base() == Type::DoubleCon ) { // Might be bottom or top...
 457     // return new (phase->C, 3) AddDNode(in(1), phase->makecon( TypeD::make(-t2->getd()) ) );
 458   }
 459 
 460   // Not associative because of boundary conditions (infinity)
 461   if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
 462     // Convert "x - (x+y)" into "-y"
 463     if( in(2)->is_Add() &&
 464         phase->eqv(in(1),in(2)->in(1) ) )
 465       return new SubDNode( phase->makecon(TypeD::ZERO),in(2)->in(2));
 466   }
 467 
 468   // Cannot replace 0.0-X with -X because a 'dsub' bytecode computes
 469   // 0.0-0.0 as +0.0, while a 'dneg' bytecode computes -0.0.
 470   //if( phase->type(in(1)) == TypeD::ZERO )
 471   //return new (phase->C, 2) NegDNode(in(2));
 472 
 473   return NULL;
 474 }
 475 
 476 //------------------------------sub--------------------------------------------
 477 // A subtract node differences its two inputs.
 478 const Type *SubDNode::sub( const Type *t1, const Type *t2 ) const {
 479   // no folding if one of operands is infinity or NaN, do not do constant folding
 480   if( g_isfinite(t1->getd()) && g_isfinite(t2->getd()) ) {
 481     return TypeD::make( t1->getd() - t2->getd() );
 482   }
 483   else if( g_isnan(t1->getd()) ) {
 484     return t1;
 485   }
 486   else if( g_isnan(t2->getd()) ) {
 487     return t2;
 488   }
 489   else {
 490     return Type::DOUBLE;
 491   }
 492 }
 493 
 494 //=============================================================================
 495 //------------------------------Idealize---------------------------------------
 496 // Unlike SubNodes, compare must still flatten return value to the
 497 // range -1, 0, 1.
 498 // And optimizations like those for (X + Y) - X fail if overflow happens.
 499 Node* CmpNode::Identity(PhaseGVN* phase) {
 500   return this;
 501 }
 502 
 503 #ifndef PRODUCT
 504 //----------------------------related------------------------------------------
 505 // Related nodes of comparison nodes include all data inputs (until hitting a
 506 // control boundary) as well as all outputs until and including control nodes
 507 // as well as their projections. In compact mode, data inputs till depth 1 and
 508 // all outputs till depth 1 are considered.
 509 void CmpNode::related(GrowableArray<Node*> *in_rel, GrowableArray<Node*> *out_rel, bool compact) const {
 510   if (compact) {
 511     this->collect_nodes(in_rel, 1, false, true);
 512     this->collect_nodes(out_rel, -1, false, false);
 513   } else {
 514     this->collect_nodes_in_all_data(in_rel, false);
 515     this->collect_nodes_out_all_ctrl_boundary(out_rel);
 516     // Now, find all control nodes in out_rel, and include their projections
 517     // and projection targets (if any) in the result.
 518     GrowableArray<Node*> proj(Compile::current()->unique());
 519     for (GrowableArrayIterator<Node*> it = out_rel->begin(); it != out_rel->end(); ++it) {
 520       Node* n = *it;
 521       if (n->is_CFG() && !n->is_Proj()) {
 522         // Assume projections and projection targets are found at levels 1 and 2.
 523         n->collect_nodes(&proj, -2, false, false);
 524         for (GrowableArrayIterator<Node*> p = proj.begin(); p != proj.end(); ++p) {
 525           out_rel->append_if_missing(*p);
 526         }
 527         proj.clear();
 528       }
 529     }
 530   }
 531 }
 532 #endif
 533 
 534 //=============================================================================
 535 //------------------------------cmp--------------------------------------------
 536 // Simplify a CmpI (compare 2 integers) node, based on local information.
 537 // If both inputs are constants, compare them.
 538 const Type *CmpINode::sub( const Type *t1, const Type *t2 ) const {
 539   const TypeInt *r0 = t1->is_int(); // Handy access
 540   const TypeInt *r1 = t2->is_int();
 541 
 542   if( r0->_hi < r1->_lo )       // Range is always low?
 543     return TypeInt::CC_LT;
 544   else if( r0->_lo > r1->_hi )  // Range is always high?
 545     return TypeInt::CC_GT;
 546 
 547   else if( r0->is_con() && r1->is_con() ) { // comparing constants?
 548     assert(r0->get_con() == r1->get_con(), "must be equal");
 549     return TypeInt::CC_EQ;      // Equal results.
 550   } else if( r0->_hi == r1->_lo ) // Range is never high?
 551     return TypeInt::CC_LE;
 552   else if( r0->_lo == r1->_hi ) // Range is never low?
 553     return TypeInt::CC_GE;
 554   return TypeInt::CC;           // else use worst case results
 555 }
 556 
 557 // Simplify a CmpU (compare 2 integers) node, based on local information.
 558 // If both inputs are constants, compare them.
 559 const Type *CmpUNode::sub( const Type *t1, const Type *t2 ) const {
 560   assert(!t1->isa_ptr(), "obsolete usage of CmpU");
 561 
 562   // comparing two unsigned ints
 563   const TypeInt *r0 = t1->is_int();   // Handy access
 564   const TypeInt *r1 = t2->is_int();
 565 
 566   // Current installed version
 567   // Compare ranges for non-overlap
 568   juint lo0 = r0->_lo;
 569   juint hi0 = r0->_hi;
 570   juint lo1 = r1->_lo;
 571   juint hi1 = r1->_hi;
 572 
 573   // If either one has both negative and positive values,
 574   // it therefore contains both 0 and -1, and since [0..-1] is the
 575   // full unsigned range, the type must act as an unsigned bottom.
 576   bool bot0 = ((jint)(lo0 ^ hi0) < 0);
 577   bool bot1 = ((jint)(lo1 ^ hi1) < 0);
 578 
 579   if (bot0 || bot1) {
 580     // All unsigned values are LE -1 and GE 0.
 581     if (lo0 == 0 && hi0 == 0) {
 582       return TypeInt::CC_LE;            //   0 <= bot
 583     } else if ((jint)lo0 == -1 && (jint)hi0 == -1) {
 584       return TypeInt::CC_GE;            // -1 >= bot
 585     } else if (lo1 == 0 && hi1 == 0) {
 586       return TypeInt::CC_GE;            // bot >= 0
 587     } else if ((jint)lo1 == -1 && (jint)hi1 == -1) {
 588       return TypeInt::CC_LE;            // bot <= -1
 589     }
 590   } else {
 591     // We can use ranges of the form [lo..hi] if signs are the same.
 592     assert(lo0 <= hi0 && lo1 <= hi1, "unsigned ranges are valid");
 593     // results are reversed, '-' > '+' for unsigned compare
 594     if (hi0 < lo1) {
 595       return TypeInt::CC_LT;            // smaller
 596     } else if (lo0 > hi1) {
 597       return TypeInt::CC_GT;            // greater
 598     } else if (hi0 == lo1 && lo0 == hi1) {
 599       return TypeInt::CC_EQ;            // Equal results
 600     } else if (lo0 >= hi1) {
 601       return TypeInt::CC_GE;
 602     } else if (hi0 <= lo1) {
 603       // Check for special case in Hashtable::get.  (See below.)
 604       if ((jint)lo0 >= 0 && (jint)lo1 >= 0 && is_index_range_check())
 605         return TypeInt::CC_LT;
 606       return TypeInt::CC_LE;
 607     }
 608   }
 609   // Check for special case in Hashtable::get - the hash index is
 610   // mod'ed to the table size so the following range check is useless.
 611   // Check for: (X Mod Y) CmpU Y, where the mod result and Y both have
 612   // to be positive.
 613   // (This is a gross hack, since the sub method never
 614   // looks at the structure of the node in any other case.)
 615   if ((jint)lo0 >= 0 && (jint)lo1 >= 0 && is_index_range_check())
 616     return TypeInt::CC_LT;
 617   return TypeInt::CC;                   // else use worst case results
 618 }
 619 
 620 const Type* CmpUNode::Value(PhaseGVN* phase) const {
 621   const Type* t = SubNode::Value_common(phase);
 622   if (t != NULL) {
 623     return t;
 624   }
 625   const Node* in1 = in(1);
 626   const Node* in2 = in(2);
 627   const Type* t1 = phase->type(in1);
 628   const Type* t2 = phase->type(in2);
 629   assert(t1->isa_int(), "CmpU has only Int type inputs");
 630   if (t2 == TypeInt::INT) { // Compare to bottom?
 631     return bottom_type();
 632   }
 633   uint in1_op = in1->Opcode();
 634   if (in1_op == Op_AddI || in1_op == Op_SubI) {
 635     // The problem rise when result of AddI(SubI) may overflow
 636     // signed integer value. Let say the input type is
 637     // [256, maxint] then +128 will create 2 ranges due to
 638     // overflow: [minint, minint+127] and [384, maxint].
 639     // But C2 type system keep only 1 type range and as result
 640     // it use general [minint, maxint] for this case which we
 641     // can't optimize.
 642     //
 643     // Make 2 separate type ranges based on types of AddI(SubI) inputs
 644     // and compare results of their compare. If results are the same
 645     // CmpU node can be optimized.
 646     const Node* in11 = in1->in(1);
 647     const Node* in12 = in1->in(2);
 648     const Type* t11 = (in11 == in1) ? Type::TOP : phase->type(in11);
 649     const Type* t12 = (in12 == in1) ? Type::TOP : phase->type(in12);
 650     // Skip cases when input types are top or bottom.
 651     if ((t11 != Type::TOP) && (t11 != TypeInt::INT) &&
 652         (t12 != Type::TOP) && (t12 != TypeInt::INT)) {
 653       const TypeInt *r0 = t11->is_int();
 654       const TypeInt *r1 = t12->is_int();
 655       jlong lo_r0 = r0->_lo;
 656       jlong hi_r0 = r0->_hi;
 657       jlong lo_r1 = r1->_lo;
 658       jlong hi_r1 = r1->_hi;
 659       if (in1_op == Op_SubI) {
 660         jlong tmp = hi_r1;
 661         hi_r1 = -lo_r1;
 662         lo_r1 = -tmp;
 663         // Note, for substructing [minint,x] type range
 664         // long arithmetic provides correct overflow answer.
 665         // The confusion come from the fact that in 32-bit
 666         // -minint == minint but in 64-bit -minint == maxint+1.
 667       }
 668       jlong lo_long = lo_r0 + lo_r1;
 669       jlong hi_long = hi_r0 + hi_r1;
 670       int lo_tr1 = min_jint;
 671       int hi_tr1 = (int)hi_long;
 672       int lo_tr2 = (int)lo_long;
 673       int hi_tr2 = max_jint;
 674       bool underflow = lo_long != (jlong)lo_tr2;
 675       bool overflow  = hi_long != (jlong)hi_tr1;
 676       // Use sub(t1, t2) when there is no overflow (one type range)
 677       // or when both overflow and underflow (too complex).
 678       if ((underflow != overflow) && (hi_tr1 < lo_tr2)) {
 679         // Overflow only on one boundary, compare 2 separate type ranges.
 680         int w = MAX2(r0->_widen, r1->_widen); // _widen does not matter here
 681         const TypeInt* tr1 = TypeInt::make(lo_tr1, hi_tr1, w);
 682         const TypeInt* tr2 = TypeInt::make(lo_tr2, hi_tr2, w);
 683         const Type* cmp1 = sub(tr1, t2);
 684         const Type* cmp2 = sub(tr2, t2);
 685         if (cmp1 == cmp2) {
 686           return cmp1; // Hit!
 687         }
 688       }
 689     }
 690   }
 691 
 692   return sub(t1, t2);            // Local flavor of type subtraction
 693 }
 694 
 695 bool CmpUNode::is_index_range_check() const {
 696   // Check for the "(X ModI Y) CmpU Y" shape
 697   return (in(1)->Opcode() == Op_ModI &&
 698           in(1)->in(2)->eqv_uncast(in(2)));
 699 }
 700 
 701 //------------------------------Idealize---------------------------------------
 702 Node *CmpINode::Ideal( PhaseGVN *phase, bool can_reshape ) {
 703   if (phase->type(in(2))->higher_equal(TypeInt::ZERO)) {
 704     switch (in(1)->Opcode()) {
 705     case Op_CmpL3:              // Collapse a CmpL3/CmpI into a CmpL
 706       return new CmpLNode(in(1)->in(1),in(1)->in(2));
 707     case Op_CmpF3:              // Collapse a CmpF3/CmpI into a CmpF
 708       return new CmpFNode(in(1)->in(1),in(1)->in(2));
 709     case Op_CmpD3:              // Collapse a CmpD3/CmpI into a CmpD
 710       return new CmpDNode(in(1)->in(1),in(1)->in(2));
 711     //case Op_SubI:
 712       // If (x - y) cannot overflow, then ((x - y) <?> 0)
 713       // can be turned into (x <?> y).
 714       // This is handled (with more general cases) by Ideal_sub_algebra.
 715     }
 716   }
 717   return NULL;                  // No change
 718 }
 719 
 720 //------------------------------Ideal------------------------------------------
 721 Node* CmpLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
 722   Node* a = NULL;
 723   Node* b = NULL;
 724   if (is_double_null_check(phase, a, b) && (phase->type(a)->is_zero_type() || phase->type(b)->is_zero_type())) {
 725     // Degraded to a simple null check, use old acmp
 726     return new CmpPNode(a, b);
 727   }
 728   return NULL;
 729 }
 730 
 731 // Match double null check emitted by Compile::optimize_acmp()
 732 bool CmpLNode::is_double_null_check(PhaseGVN* phase, Node*& a, Node*& b) const {
 733   if (in(1)->Opcode() == Op_OrL &&
 734       in(1)->in(1)->Opcode() == Op_CastP2X &&
 735       in(1)->in(2)->Opcode() == Op_CastP2X &&
 736       in(2)->bottom_type()->is_zero_type()) {
 737     assert(EnableValhalla, "unexpected double null check");
 738     a = in(1)->in(1)->in(1);
 739     b = in(1)->in(2)->in(1);
 740     return true;
 741   }
 742   return false;
 743 }
 744 
 745 //------------------------------Value------------------------------------------
 746 const Type* CmpLNode::Value(PhaseGVN* phase) const {
 747   Node* a = NULL;
 748   Node* b = NULL;
 749   if (is_double_null_check(phase, a, b) && (!phase->type(a)->maybe_null() || !phase->type(b)->maybe_null())) {
 750     // One operand is never NULL, emit constant false
 751     return TypeInt::CC_GT;
 752   }
 753   return SubNode::Value(phase);
 754 }
 755 
 756 //=============================================================================
 757 // Simplify a CmpL (compare 2 longs ) node, based on local information.
 758 // If both inputs are constants, compare them.
 759 const Type *CmpLNode::sub( const Type *t1, const Type *t2 ) const {
 760   const TypeLong *r0 = t1->is_long(); // Handy access
 761   const TypeLong *r1 = t2->is_long();
 762 
 763   if( r0->_hi < r1->_lo )       // Range is always low?
 764     return TypeInt::CC_LT;
 765   else if( r0->_lo > r1->_hi )  // Range is always high?
 766     return TypeInt::CC_GT;
 767 
 768   else if( r0->is_con() && r1->is_con() ) { // comparing constants?
 769     assert(r0->get_con() == r1->get_con(), "must be equal");
 770     return TypeInt::CC_EQ;      // Equal results.
 771   } else if( r0->_hi == r1->_lo ) // Range is never high?
 772     return TypeInt::CC_LE;
 773   else if( r0->_lo == r1->_hi ) // Range is never low?
 774     return TypeInt::CC_GE;
 775   return TypeInt::CC;           // else use worst case results
 776 }
 777 
 778 
 779 // Simplify a CmpUL (compare 2 unsigned longs) node, based on local information.
 780 // If both inputs are constants, compare them.
 781 const Type* CmpULNode::sub(const Type* t1, const Type* t2) const {
 782   assert(!t1->isa_ptr(), "obsolete usage of CmpUL");
 783 
 784   // comparing two unsigned longs
 785   const TypeLong* r0 = t1->is_long();   // Handy access
 786   const TypeLong* r1 = t2->is_long();
 787 
 788   // Current installed version
 789   // Compare ranges for non-overlap
 790   julong lo0 = r0->_lo;
 791   julong hi0 = r0->_hi;
 792   julong lo1 = r1->_lo;
 793   julong hi1 = r1->_hi;
 794 
 795   // If either one has both negative and positive values,
 796   // it therefore contains both 0 and -1, and since [0..-1] is the
 797   // full unsigned range, the type must act as an unsigned bottom.
 798   bool bot0 = ((jlong)(lo0 ^ hi0) < 0);
 799   bool bot1 = ((jlong)(lo1 ^ hi1) < 0);
 800 
 801   if (bot0 || bot1) {
 802     // All unsigned values are LE -1 and GE 0.
 803     if (lo0 == 0 && hi0 == 0) {
 804       return TypeInt::CC_LE;            //   0 <= bot
 805     } else if ((jlong)lo0 == -1 && (jlong)hi0 == -1) {
 806       return TypeInt::CC_GE;            // -1 >= bot
 807     } else if (lo1 == 0 && hi1 == 0) {
 808       return TypeInt::CC_GE;            // bot >= 0
 809     } else if ((jlong)lo1 == -1 && (jlong)hi1 == -1) {
 810       return TypeInt::CC_LE;            // bot <= -1
 811     }
 812   } else {
 813     // We can use ranges of the form [lo..hi] if signs are the same.
 814     assert(lo0 <= hi0 && lo1 <= hi1, "unsigned ranges are valid");
 815     // results are reversed, '-' > '+' for unsigned compare
 816     if (hi0 < lo1) {
 817       return TypeInt::CC_LT;            // smaller
 818     } else if (lo0 > hi1) {
 819       return TypeInt::CC_GT;            // greater
 820     } else if (hi0 == lo1 && lo0 == hi1) {
 821       return TypeInt::CC_EQ;            // Equal results
 822     } else if (lo0 >= hi1) {
 823       return TypeInt::CC_GE;
 824     } else if (hi0 <= lo1) {
 825       return TypeInt::CC_LE;
 826     }
 827   }
 828 
 829   return TypeInt::CC;                   // else use worst case results
 830 }
 831 
 832 //=============================================================================
 833 //------------------------------sub--------------------------------------------
 834 // Simplify an CmpP (compare 2 pointers) node, based on local information.
 835 // If both inputs are constants, compare them.
 836 const Type *CmpPNode::sub( const Type *t1, const Type *t2 ) const {
 837   if (ACmpOnValues != 3 &&
 838       (t1->isa_valuetype() || t2->isa_valuetype() ||
 839        ((t1->is_valuetypeptr() || t2->is_valuetypeptr()) &&
 840         (!t1->maybe_null() || !t2->maybe_null())))) {
 841     // One operand is a value type and one operand is never null, fold to constant false
 842     return TypeInt::CC_GT;
 843   }
 844 
 845   const TypePtr *r0 = t1->is_ptr(); // Handy access
 846   const TypePtr *r1 = t2->is_ptr();
 847 
 848   // Undefined inputs makes for an undefined result
 849   if( TypePtr::above_centerline(r0->_ptr) ||
 850       TypePtr::above_centerline(r1->_ptr) )
 851     return Type::TOP;
 852 
 853   if (r0 == r1 && r0->singleton()) {
 854     // Equal pointer constants (klasses, nulls, etc.)
 855     return TypeInt::CC_EQ;
 856   }
 857 
 858   // See if it is 2 unrelated classes.
 859   const TypeOopPtr* p0 = r0->isa_oopptr();
 860   const TypeOopPtr* p1 = r1->isa_oopptr();
 861   if (p0 && p1) {
 862     Node* in1 = in(1)->uncast();
 863     Node* in2 = in(2)->uncast();
 864     AllocateNode* alloc1 = AllocateNode::Ideal_allocation(in1, NULL);
 865     AllocateNode* alloc2 = AllocateNode::Ideal_allocation(in2, NULL);
 866     if (MemNode::detect_ptr_independence(in1, alloc1, in2, alloc2, NULL)) {
 867       return TypeInt::CC_GT;  // different pointers
 868     }
 869     ciKlass* klass0 = p0->klass();
 870     bool    xklass0 = p0->klass_is_exact();
 871     ciKlass* klass1 = p1->klass();
 872     bool    xklass1 = p1->klass_is_exact();
 873     int kps = (p0->isa_klassptr()?1:0) + (p1->isa_klassptr()?1:0);
 874     if (klass0 && klass1 &&
 875         kps != 1 &&             // both or neither are klass pointers
 876         klass0->is_loaded() && !klass0->is_interface() && // do not trust interfaces
 877         klass1->is_loaded() && !klass1->is_interface() &&
 878         (!klass0->is_obj_array_klass() ||
 879          !klass0->as_obj_array_klass()->base_element_klass()->is_interface()) &&
 880         (!klass1->is_obj_array_klass() ||
 881          !klass1->as_obj_array_klass()->base_element_klass()->is_interface())) {
 882       bool unrelated_classes = false;
 883       // See if neither subclasses the other, or if the class on top
 884       // is precise.  In either of these cases, the compare is known
 885       // to fail if at least one of the pointers is provably not null.
 886       if (klass0->equals(klass1)) {  // if types are unequal but klasses are equal
 887         // Do nothing; we know nothing for imprecise types
 888       } else if (klass0->is_subtype_of(klass1)) {
 889         // If klass1's type is PRECISE, then classes are unrelated.
 890         unrelated_classes = xklass1;
 891       } else if (klass1->is_subtype_of(klass0)) {
 892         // If klass0's type is PRECISE, then classes are unrelated.
 893         unrelated_classes = xklass0;
 894       } else {                  // Neither subtypes the other
 895         unrelated_classes = true;
 896       }
 897       if (unrelated_classes) {
 898         // The oops classes are known to be unrelated. If the joined PTRs of
 899         // two oops is not Null and not Bottom, then we are sure that one
 900         // of the two oops is non-null, and the comparison will always fail.
 901         TypePtr::PTR jp = r0->join_ptr(r1->_ptr);
 902         if (jp != TypePtr::Null && jp != TypePtr::BotPTR) {
 903           return TypeInt::CC_GT;
 904         }
 905       }
 906     }
 907   }
 908 
 909   // Known constants can be compared exactly
 910   // Null can be distinguished from any NotNull pointers
 911   // Unknown inputs makes an unknown result
 912   if( r0->singleton() ) {
 913     intptr_t bits0 = r0->get_con();
 914     if( r1->singleton() )
 915       return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT;
 916     return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC;
 917   } else if( r1->singleton() ) {
 918     intptr_t bits1 = r1->get_con();
 919     return ( r0->_ptr == TypePtr::NotNull && bits1==0 ) ? TypeInt::CC_GT : TypeInt::CC;
 920   } else
 921     return TypeInt::CC;
 922 }
 923 
 924 static inline Node* isa_java_mirror_load(PhaseGVN* phase, Node* n) {
 925   // Return the klass node for (indirect load from OopHandle)
 926   //   LoadBarrier?(LoadP(LoadP(AddP(foo:Klass, #java_mirror))))
 927   //   or NULL if not matching.
 928   BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 929     n = bs->step_over_gc_barrier(n);
 930 
 931   if (n->Opcode() != Op_LoadP) return NULL;
 932 
 933   const TypeInstPtr* tp = phase->type(n)->isa_instptr();
 934   if (!tp || tp->klass() != phase->C->env()->Class_klass()) return NULL;
 935 
 936   Node* adr = n->in(MemNode::Address);
 937   // First load from OopHandle: ((OopHandle)mirror)->resolve(); may need barrier.
 938   if (adr->Opcode() != Op_LoadP || !phase->type(adr)->isa_rawptr()) return NULL;
 939   adr = adr->in(MemNode::Address);
 940 
 941   intptr_t off = 0;
 942   Node* k = AddPNode::Ideal_base_and_offset(adr, phase, off);
 943   if (k == NULL)  return NULL;
 944   const TypeKlassPtr* tkp = phase->type(k)->isa_klassptr();
 945   if (!tkp || off != in_bytes(Klass::java_mirror_offset())) return NULL;
 946 
 947   // We've found the klass node of a Java mirror load.
 948   return k;
 949 }
 950 
 951 static inline Node* isa_const_java_mirror(PhaseGVN* phase, Node* n) {
 952   // for ConP(Foo.class) return ConP(Foo.klass)
 953   // otherwise return NULL
 954   if (!n->is_Con()) return NULL;
 955 
 956   const TypeInstPtr* tp = phase->type(n)->isa_instptr();
 957   if (!tp) return NULL;
 958 
 959   ciType* mirror_type = tp->java_mirror_type();
 960   // TypeInstPtr::java_mirror_type() returns non-NULL for compile-
 961   // time Class constants only.
 962   if (!mirror_type) return NULL;
 963 
 964   // x.getClass() == int.class can never be true (for all primitive types)
 965   // Return a ConP(NULL) node for this case.
 966   if (mirror_type->is_classless()) {
 967     return phase->makecon(TypePtr::NULL_PTR);
 968   }
 969 
 970   // return the ConP(Foo.klass)
 971   assert(mirror_type->is_klass(), "mirror_type should represent a Klass*");
 972   return phase->makecon(TypeKlassPtr::make(mirror_type->as_klass()));
 973 }
 974 
 975 //------------------------------Ideal------------------------------------------
 976 // Normalize comparisons between Java mirror loads to compare the klass instead.
 977 //
 978 // Also check for the case of comparing an unknown klass loaded from the primary
 979 // super-type array vs a known klass with no subtypes.  This amounts to
 980 // checking to see an unknown klass subtypes a known klass with no subtypes;
 981 // this only happens on an exact match.  We can shorten this test by 1 load.
 982 Node* CmpPNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 983   Node* pert = has_perturbed_operand();
 984   if (pert != NULL) {
 985     // Optimize new acmp
 986     Node* a = pert->in(AddPNode::Base); // unperturbed a
 987     Node* b = in(2);
 988     Node* cmp = phase->C->optimize_acmp(phase, a, b);
 989     if (cmp != NULL) {
 990       return cmp;
 991     }
 992   }
 993 
 994   // Normalize comparisons between Java mirrors into comparisons of the low-
 995   // level klass, where a dependent load could be shortened.
 996   //
 997   // The new pattern has a nice effect of matching the same pattern used in the
 998   // fast path of instanceof/checkcast/Class.isInstance(), which allows
 999   // redundant exact type check be optimized away by GVN.
1000   // For example, in
1001   //   if (x.getClass() == Foo.class) {
1002   //     Foo foo = (Foo) x;
1003   //     // ... use a ...
1004   //   }
1005   // a CmpPNode could be shared between if_acmpne and checkcast
1006   {
1007     Node* k1 = isa_java_mirror_load(phase, in(1));
1008     Node* k2 = isa_java_mirror_load(phase, in(2));
1009     Node* conk2 = isa_const_java_mirror(phase, in(2));
1010 
1011     if (k1 && (k2 || conk2)) {
1012       Node* lhs = k1;
1013       Node* rhs = (k2 != NULL) ? k2 : conk2;
1014       PhaseIterGVN* igvn = phase->is_IterGVN();
1015       if (igvn != NULL) {
1016         set_req_X(1, lhs, igvn);
1017         set_req_X(2, rhs, igvn);
1018       } else {
1019         set_req(1, lhs);
1020         set_req(2, rhs);
1021       }
1022       return this;
1023     }
1024   }
1025 
1026   // Constant pointer on right?
1027   const TypeKlassPtr* t2 = phase->type(in(2))->isa_klassptr();
1028   if (t2 == NULL || !t2->klass_is_exact())
1029     return NULL;
1030   // Get the constant klass we are comparing to.
1031   ciKlass* superklass = t2->klass();
1032 
1033   // Now check for LoadKlass on left.
1034   Node* ldk1 = in(1);
1035   if (ldk1->is_DecodeNKlass()) {
1036     ldk1 = ldk1->in(1);
1037     if (ldk1->Opcode() != Op_LoadNKlass )
1038       return NULL;
1039   } else if (ldk1->Opcode() != Op_LoadKlass )
1040     return NULL;
1041   // Take apart the address of the LoadKlass:
1042   Node* adr1 = ldk1->in(MemNode::Address);
1043   intptr_t con2 = 0;
1044   Node* ldk2 = AddPNode::Ideal_base_and_offset(adr1, phase, con2);
1045   if (ldk2 == NULL)
1046     return NULL;
1047   if (con2 == oopDesc::klass_offset_in_bytes()) {
1048     // We are inspecting an object's concrete class.
1049     // Short-circuit the check if the query is abstract.
1050     if (superklass->is_interface() ||
1051         superklass->is_abstract()) {
1052       // Make it come out always false:
1053       this->set_req(2, phase->makecon(TypePtr::NULL_PTR));
1054       return this;
1055     }
1056   }
1057 
1058   // Check for a LoadKlass from primary supertype array.
1059   // Any nested loadklass from loadklass+con must be from the p.s. array.
1060   if (ldk2->is_DecodeNKlass()) {
1061     // Keep ldk2 as DecodeN since it could be used in CmpP below.
1062     if (ldk2->in(1)->Opcode() != Op_LoadNKlass )
1063       return NULL;
1064   } else if (ldk2->Opcode() != Op_LoadKlass)
1065     return NULL;
1066 
1067   // Verify that we understand the situation
1068   if (con2 != (intptr_t) superklass->super_check_offset())
1069     return NULL;                // Might be element-klass loading from array klass
1070 
1071   // If 'superklass' has no subklasses and is not an interface, then we are
1072   // assured that the only input which will pass the type check is
1073   // 'superklass' itself.
1074   //
1075   // We could be more liberal here, and allow the optimization on interfaces
1076   // which have a single implementor.  This would require us to increase the
1077   // expressiveness of the add_dependency() mechanism.
1078   // %%% Do this after we fix TypeOopPtr:  Deps are expressive enough now.
1079 
1080   // Object arrays must have their base element have no subtypes
1081   while (superklass->is_obj_array_klass()) {
1082     ciType* elem = superklass->as_obj_array_klass()->element_type();
1083     superklass = elem->as_klass();
1084   }
1085   if (superklass->is_instance_klass()) {
1086     ciInstanceKlass* ik = superklass->as_instance_klass();
1087     if (ik->has_subklass() || ik->is_interface())  return NULL;
1088     // Add a dependency if there is a chance that a subclass will be added later.
1089     if (!ik->is_final()) {
1090       phase->C->dependencies()->assert_leaf_type(ik);
1091     }
1092   }
1093 
1094   // Bypass the dependent load, and compare directly
1095   this->set_req(1,ldk2);
1096 
1097   return this;
1098 }
1099 
1100 // Checks if one operand is perturbed and returns it
1101 Node* CmpPNode::has_perturbed_operand() const {
1102   // We always perturbe the first operand
1103   AddPNode* addP = in(1)->isa_AddP();
1104   if (addP != NULL) {
1105     Node* base = addP->in(AddPNode::Base);
1106     if (base->is_top()) {
1107       // RawPtr comparison
1108       return NULL;
1109     }
1110     assert(EnableValhalla && ACmpOnValues == 1, "unexpected perturbed oop");
1111     return in(1);
1112   }
1113   return NULL;
1114 }
1115 
1116 //=============================================================================
1117 //------------------------------sub--------------------------------------------
1118 // Simplify an CmpN (compare 2 pointers) node, based on local information.
1119 // If both inputs are constants, compare them.
1120 const Type *CmpNNode::sub( const Type *t1, const Type *t2 ) const {
1121   const TypePtr *r0 = t1->make_ptr(); // Handy access
1122   const TypePtr *r1 = t2->make_ptr();
1123 
1124   // Undefined inputs makes for an undefined result
1125   if ((r0 == NULL) || (r1 == NULL) ||
1126       TypePtr::above_centerline(r0->_ptr) ||
1127       TypePtr::above_centerline(r1->_ptr)) {
1128     return Type::TOP;
1129   }
1130   if (r0 == r1 && r0->singleton()) {
1131     // Equal pointer constants (klasses, nulls, etc.)
1132     return TypeInt::CC_EQ;
1133   }
1134 
1135   // See if it is 2 unrelated classes.
1136   const TypeOopPtr* p0 = r0->isa_oopptr();
1137   const TypeOopPtr* p1 = r1->isa_oopptr();
1138   if (p0 && p1) {
1139     ciKlass* klass0 = p0->klass();
1140     bool    xklass0 = p0->klass_is_exact();
1141     ciKlass* klass1 = p1->klass();
1142     bool    xklass1 = p1->klass_is_exact();
1143     int kps = (p0->isa_klassptr()?1:0) + (p1->isa_klassptr()?1:0);
1144     if (klass0 && klass1 &&
1145         kps != 1 &&             // both or neither are klass pointers
1146         !klass0->is_interface() && // do not trust interfaces
1147         !klass1->is_interface()) {
1148       bool unrelated_classes = false;
1149       // See if neither subclasses the other, or if the class on top
1150       // is precise.  In either of these cases, the compare is known
1151       // to fail if at least one of the pointers is provably not null.
1152       if (klass0->equals(klass1)) { // if types are unequal but klasses are equal
1153         // Do nothing; we know nothing for imprecise types
1154       } else if (klass0->is_subtype_of(klass1)) {
1155         // If klass1's type is PRECISE, then classes are unrelated.
1156         unrelated_classes = xklass1;
1157       } else if (klass1->is_subtype_of(klass0)) {
1158         // If klass0's type is PRECISE, then classes are unrelated.
1159         unrelated_classes = xklass0;
1160       } else {                  // Neither subtypes the other
1161         unrelated_classes = true;
1162       }
1163       if (unrelated_classes) {
1164         // The oops classes are known to be unrelated. If the joined PTRs of
1165         // two oops is not Null and not Bottom, then we are sure that one
1166         // of the two oops is non-null, and the comparison will always fail.
1167         TypePtr::PTR jp = r0->join_ptr(r1->_ptr);
1168         if (jp != TypePtr::Null && jp != TypePtr::BotPTR) {
1169           return TypeInt::CC_GT;
1170         }
1171       }
1172     }
1173   }
1174 
1175   // Known constants can be compared exactly
1176   // Null can be distinguished from any NotNull pointers
1177   // Unknown inputs makes an unknown result
1178   if( r0->singleton() ) {
1179     intptr_t bits0 = r0->get_con();
1180     if( r1->singleton() )
1181       return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT;
1182     return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC;
1183   } else if( r1->singleton() ) {
1184     intptr_t bits1 = r1->get_con();
1185     return ( r0->_ptr == TypePtr::NotNull && bits1==0 ) ? TypeInt::CC_GT : TypeInt::CC;
1186   } else
1187     return TypeInt::CC;
1188 }
1189 
1190 //------------------------------Ideal------------------------------------------
1191 Node *CmpNNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
1192   return NULL;
1193 }
1194 
1195 //=============================================================================
1196 //------------------------------Value------------------------------------------
1197 // Simplify an CmpF (compare 2 floats ) node, based on local information.
1198 // If both inputs are constants, compare them.
1199 const Type* CmpFNode::Value(PhaseGVN* phase) const {
1200   const Node* in1 = in(1);
1201   const Node* in2 = in(2);
1202   // Either input is TOP ==> the result is TOP
1203   const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
1204   if( t1 == Type::TOP ) return Type::TOP;
1205   const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
1206   if( t2 == Type::TOP ) return Type::TOP;
1207 
1208   // Not constants?  Don't know squat - even if they are the same
1209   // value!  If they are NaN's they compare to LT instead of EQ.
1210   const TypeF *tf1 = t1->isa_float_constant();
1211   const TypeF *tf2 = t2->isa_float_constant();
1212   if( !tf1 || !tf2 ) return TypeInt::CC;
1213 
1214   // This implements the Java bytecode fcmpl, so unordered returns -1.
1215   if( tf1->is_nan() || tf2->is_nan() )
1216     return TypeInt::CC_LT;
1217 
1218   if( tf1->_f < tf2->_f ) return TypeInt::CC_LT;
1219   if( tf1->_f > tf2->_f ) return TypeInt::CC_GT;
1220   assert( tf1->_f == tf2->_f, "do not understand FP behavior" );
1221   return TypeInt::CC_EQ;
1222 }
1223 
1224 
1225 //=============================================================================
1226 //------------------------------Value------------------------------------------
1227 // Simplify an CmpD (compare 2 doubles ) node, based on local information.
1228 // If both inputs are constants, compare them.
1229 const Type* CmpDNode::Value(PhaseGVN* phase) const {
1230   const Node* in1 = in(1);
1231   const Node* in2 = in(2);
1232   // Either input is TOP ==> the result is TOP
1233   const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
1234   if( t1 == Type::TOP ) return Type::TOP;
1235   const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
1236   if( t2 == Type::TOP ) return Type::TOP;
1237 
1238   // Not constants?  Don't know squat - even if they are the same
1239   // value!  If they are NaN's they compare to LT instead of EQ.
1240   const TypeD *td1 = t1->isa_double_constant();
1241   const TypeD *td2 = t2->isa_double_constant();
1242   if( !td1 || !td2 ) return TypeInt::CC;
1243 
1244   // This implements the Java bytecode dcmpl, so unordered returns -1.
1245   if( td1->is_nan() || td2->is_nan() )
1246     return TypeInt::CC_LT;
1247 
1248   if( td1->_d < td2->_d ) return TypeInt::CC_LT;
1249   if( td1->_d > td2->_d ) return TypeInt::CC_GT;
1250   assert( td1->_d == td2->_d, "do not understand FP behavior" );
1251   return TypeInt::CC_EQ;
1252 }
1253 
1254 //------------------------------Ideal------------------------------------------
1255 Node *CmpDNode::Ideal(PhaseGVN *phase, bool can_reshape){
1256   // Check if we can change this to a CmpF and remove a ConvD2F operation.
1257   // Change  (CMPD (F2D (float)) (ConD value))
1258   // To      (CMPF      (float)  (ConF value))
1259   // Valid when 'value' does not lose precision as a float.
1260   // Benefits: eliminates conversion, does not require 24-bit mode
1261 
1262   // NaNs prevent commuting operands.  This transform works regardless of the
1263   // order of ConD and ConvF2D inputs by preserving the original order.
1264   int idx_f2d = 1;              // ConvF2D on left side?
1265   if( in(idx_f2d)->Opcode() != Op_ConvF2D )
1266     idx_f2d = 2;                // No, swap to check for reversed args
1267   int idx_con = 3-idx_f2d;      // Check for the constant on other input
1268 
1269   if( ConvertCmpD2CmpF &&
1270       in(idx_f2d)->Opcode() == Op_ConvF2D &&
1271       in(idx_con)->Opcode() == Op_ConD ) {
1272     const TypeD *t2 = in(idx_con)->bottom_type()->is_double_constant();
1273     double t2_value_as_double = t2->_d;
1274     float  t2_value_as_float  = (float)t2_value_as_double;
1275     if( t2_value_as_double == (double)t2_value_as_float ) {
1276       // Test value can be represented as a float
1277       // Eliminate the conversion to double and create new comparison
1278       Node *new_in1 = in(idx_f2d)->in(1);
1279       Node *new_in2 = phase->makecon( TypeF::make(t2_value_as_float) );
1280       if( idx_f2d != 1 ) {      // Must flip args to match original order
1281         Node *tmp = new_in1;
1282         new_in1 = new_in2;
1283         new_in2 = tmp;
1284       }
1285       CmpFNode *new_cmp = (Opcode() == Op_CmpD3)
1286         ? new CmpF3Node( new_in1, new_in2 )
1287         : new CmpFNode ( new_in1, new_in2 ) ;
1288       return new_cmp;           // Changed to CmpFNode
1289     }
1290     // Testing value required the precision of a double
1291   }
1292   return NULL;                  // No change
1293 }
1294 
1295 
1296 //=============================================================================
1297 //------------------------------cc2logical-------------------------------------
1298 // Convert a condition code type to a logical type
1299 const Type *BoolTest::cc2logical( const Type *CC ) const {
1300   if( CC == Type::TOP ) return Type::TOP;
1301   if( CC->base() != Type::Int ) return TypeInt::BOOL; // Bottom or worse
1302   const TypeInt *ti = CC->is_int();
1303   if( ti->is_con() ) {          // Only 1 kind of condition codes set?
1304     // Match low order 2 bits
1305     int tmp = ((ti->get_con()&3) == (_test&3)) ? 1 : 0;
1306     if( _test & 4 ) tmp = 1-tmp;     // Optionally complement result
1307     return TypeInt::make(tmp);       // Boolean result
1308   }
1309 
1310   if( CC == TypeInt::CC_GE ) {
1311     if( _test == ge ) return TypeInt::ONE;
1312     if( _test == lt ) return TypeInt::ZERO;
1313   }
1314   if( CC == TypeInt::CC_LE ) {
1315     if( _test == le ) return TypeInt::ONE;
1316     if( _test == gt ) return TypeInt::ZERO;
1317   }
1318 
1319   return TypeInt::BOOL;
1320 }
1321 
1322 //------------------------------dump_spec-------------------------------------
1323 // Print special per-node info
1324 void BoolTest::dump_on(outputStream *st) const {
1325   const char *msg[] = {"eq","gt","of","lt","ne","le","nof","ge"};
1326   st->print("%s", msg[_test]);
1327 }
1328 
1329 // Returns the logical AND of two tests (or 'never' if both tests can never be true).
1330 // For example, a test for 'le' followed by a test for 'lt' is equivalent with 'lt'.
1331 BoolTest::mask BoolTest::merge(BoolTest other) const {
1332   const mask res[illegal+1][illegal+1] = {
1333     // eq,      gt,      of,      lt,      ne,      le,      nof,     ge,      never,   illegal
1334       {eq,      never,   illegal, never,   never,   eq,      illegal, eq,      never,   illegal},  // eq
1335       {never,   gt,      illegal, never,   gt,      never,   illegal, gt,      never,   illegal},  // gt
1336       {illegal, illegal, illegal, illegal, illegal, illegal, illegal, illegal, never,   illegal},  // of
1337       {never,   never,   illegal, lt,      lt,      lt,      illegal, never,   never,   illegal},  // lt
1338       {never,   gt,      illegal, lt,      ne,      lt,      illegal, gt,      never,   illegal},  // ne
1339       {eq,      never,   illegal, lt,      lt,      le,      illegal, eq,      never,   illegal},  // le
1340       {illegal, illegal, illegal, illegal, illegal, illegal, illegal, illegal, never,   illegal},  // nof
1341       {eq,      gt,      illegal, never,   gt,      eq,      illegal, ge,      never,   illegal},  // ge
1342       {never,   never,   never,   never,   never,   never,   never,   never,   never,   illegal},  // never
1343       {illegal, illegal, illegal, illegal, illegal, illegal, illegal, illegal, illegal, illegal}}; // illegal
1344   return res[_test][other._test];
1345 }
1346 
1347 //=============================================================================
1348 uint BoolNode::hash() const { return (Node::hash() << 3)|(_test._test+1); }
1349 uint BoolNode::size_of() const { return sizeof(BoolNode); }
1350 
1351 //------------------------------operator==-------------------------------------
1352 bool BoolNode::cmp( const Node &n ) const {
1353   const BoolNode *b = (const BoolNode *)&n; // Cast up
1354   return (_test._test == b->_test._test);
1355 }
1356 
1357 //-------------------------------make_predicate--------------------------------
1358 Node* BoolNode::make_predicate(Node* test_value, PhaseGVN* phase) {
1359   if (test_value->is_Con())   return test_value;
1360   if (test_value->is_Bool())  return test_value;
1361   if (test_value->is_CMove() &&
1362       test_value->in(CMoveNode::Condition)->is_Bool()) {
1363     BoolNode*   bol   = test_value->in(CMoveNode::Condition)->as_Bool();
1364     const Type* ftype = phase->type(test_value->in(CMoveNode::IfFalse));
1365     const Type* ttype = phase->type(test_value->in(CMoveNode::IfTrue));
1366     if (ftype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ttype)) {
1367       return bol;
1368     } else if (ttype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ftype)) {
1369       return phase->transform( bol->negate(phase) );
1370     }
1371     // Else fall through.  The CMove gets in the way of the test.
1372     // It should be the case that make_predicate(bol->as_int_value()) == bol.
1373   }
1374   Node* cmp = new CmpINode(test_value, phase->intcon(0));
1375   cmp = phase->transform(cmp);
1376   Node* bol = new BoolNode(cmp, BoolTest::ne);
1377   return phase->transform(bol);
1378 }
1379 
1380 //--------------------------------as_int_value---------------------------------
1381 Node* BoolNode::as_int_value(PhaseGVN* phase) {
1382   // Inverse to make_predicate.  The CMove probably boils down to a Conv2B.
1383   Node* cmov = CMoveNode::make(NULL, this,
1384                                phase->intcon(0), phase->intcon(1),
1385                                TypeInt::BOOL);
1386   return phase->transform(cmov);
1387 }
1388 
1389 //----------------------------------negate-------------------------------------
1390 BoolNode* BoolNode::negate(PhaseGVN* phase) {
1391   return new BoolNode(in(1), _test.negate());
1392 }
1393 
1394 // Change "bool eq/ne (cmp (add/sub A B) C)" into false/true if add/sub
1395 // overflows and we can prove that C is not in the two resulting ranges.
1396 // This optimization is similar to the one performed by CmpUNode::Value().
1397 Node* BoolNode::fold_cmpI(PhaseGVN* phase, SubNode* cmp, Node* cmp1, int cmp_op,
1398                           int cmp1_op, const TypeInt* cmp2_type) {
1399   // Only optimize eq/ne integer comparison of add/sub
1400   if((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1401      (cmp_op == Op_CmpI) && (cmp1_op == Op_AddI || cmp1_op == Op_SubI)) {
1402     // Skip cases were inputs of add/sub are not integers or of bottom type
1403     const TypeInt* r0 = phase->type(cmp1->in(1))->isa_int();
1404     const TypeInt* r1 = phase->type(cmp1->in(2))->isa_int();
1405     if ((r0 != NULL) && (r0 != TypeInt::INT) &&
1406         (r1 != NULL) && (r1 != TypeInt::INT) &&
1407         (cmp2_type != TypeInt::INT)) {
1408       // Compute exact (long) type range of add/sub result
1409       jlong lo_long = r0->_lo;
1410       jlong hi_long = r0->_hi;
1411       if (cmp1_op == Op_AddI) {
1412         lo_long += r1->_lo;
1413         hi_long += r1->_hi;
1414       } else {
1415         lo_long -= r1->_hi;
1416         hi_long -= r1->_lo;
1417       }
1418       // Check for over-/underflow by casting to integer
1419       int lo_int = (int)lo_long;
1420       int hi_int = (int)hi_long;
1421       bool underflow = lo_long != (jlong)lo_int;
1422       bool overflow  = hi_long != (jlong)hi_int;
1423       if ((underflow != overflow) && (hi_int < lo_int)) {
1424         // Overflow on one boundary, compute resulting type ranges:
1425         // tr1 [MIN_INT, hi_int] and tr2 [lo_int, MAX_INT]
1426         int w = MAX2(r0->_widen, r1->_widen); // _widen does not matter here
1427         const TypeInt* tr1 = TypeInt::make(min_jint, hi_int, w);
1428         const TypeInt* tr2 = TypeInt::make(lo_int, max_jint, w);
1429         // Compare second input of cmp to both type ranges
1430         const Type* sub_tr1 = cmp->sub(tr1, cmp2_type);
1431         const Type* sub_tr2 = cmp->sub(tr2, cmp2_type);
1432         if (sub_tr1 == TypeInt::CC_LT && sub_tr2 == TypeInt::CC_GT) {
1433           // The result of the add/sub will never equal cmp2. Replace BoolNode
1434           // by false (0) if it tests for equality and by true (1) otherwise.
1435           return ConINode::make((_test._test == BoolTest::eq) ? 0 : 1);
1436         }
1437       }
1438     }
1439   }
1440   return NULL;
1441 }
1442 
1443 static bool is_counted_loop_cmp(Node *cmp) {
1444   Node *n = cmp->in(1)->in(1);
1445   return n != NULL &&
1446          n->is_Phi() &&
1447          n->in(0) != NULL &&
1448          n->in(0)->is_CountedLoop() &&
1449          n->in(0)->as_CountedLoop()->phi() == n;
1450 }
1451 
1452 //------------------------------Ideal------------------------------------------
1453 Node *BoolNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1454   // Change "bool tst (cmp con x)" into "bool ~tst (cmp x con)".
1455   // This moves the constant to the right.  Helps value-numbering.
1456   Node *cmp = in(1);
1457   if( !cmp->is_Sub() ) return NULL;
1458   int cop = cmp->Opcode();
1459   if( cop == Op_FastLock || cop == Op_FastUnlock) return NULL;
1460   Node *cmp1 = cmp->in(1);
1461   Node *cmp2 = cmp->in(2);
1462   if( !cmp1 ) return NULL;
1463 
1464   if (_test._test == BoolTest::overflow || _test._test == BoolTest::no_overflow) {
1465     return NULL;
1466   }
1467 
1468   // Constant on left?
1469   Node *con = cmp1;
1470   uint op2 = cmp2->Opcode();
1471   // Move constants to the right of compare's to canonicalize.
1472   // Do not muck with Opaque1 nodes, as this indicates a loop
1473   // guard that cannot change shape.
1474   if( con->is_Con() && !cmp2->is_Con() && op2 != Op_Opaque1 &&
1475       // Because of NaN's, CmpD and CmpF are not commutative
1476       cop != Op_CmpD && cop != Op_CmpF &&
1477       // Protect against swapping inputs to a compare when it is used by a
1478       // counted loop exit, which requires maintaining the loop-limit as in(2)
1479       !is_counted_loop_exit_test() ) {
1480     // Ok, commute the constant to the right of the cmp node.
1481     // Clone the Node, getting a new Node of the same class
1482     cmp = cmp->clone();
1483     // Swap inputs to the clone
1484     cmp->swap_edges(1, 2);
1485     cmp = phase->transform( cmp );
1486     return new BoolNode( cmp, _test.commute() );
1487   }
1488 
1489   // Change "bool eq/ne (cmp (xor X 1) 0)" into "bool ne/eq (cmp X 0)".
1490   // The XOR-1 is an idiom used to flip the sense of a bool.  We flip the
1491   // test instead.
1492   int cmp1_op = cmp1->Opcode();
1493   const TypeInt* cmp2_type = phase->type(cmp2)->isa_int();
1494   if (cmp2_type == NULL)  return NULL;
1495   Node* j_xor = cmp1;
1496   if( cmp2_type == TypeInt::ZERO &&
1497       cmp1_op == Op_XorI &&
1498       j_xor->in(1) != j_xor &&          // An xor of itself is dead
1499       phase->type( j_xor->in(1) ) == TypeInt::BOOL &&
1500       phase->type( j_xor->in(2) ) == TypeInt::ONE &&
1501       (_test._test == BoolTest::eq ||
1502        _test._test == BoolTest::ne) ) {
1503     Node *ncmp = phase->transform(new CmpINode(j_xor->in(1),cmp2));
1504     return new BoolNode( ncmp, _test.negate() );
1505   }
1506 
1507   // Change ((x & m) u<= m) or ((m & x) u<= m) to always true
1508   // Same with ((x & m) u< m+1) and ((m & x) u< m+1)
1509   if (cop == Op_CmpU &&
1510       cmp1_op == Op_AndI) {
1511     Node* bound = NULL;
1512     if (_test._test == BoolTest::le) {
1513       bound = cmp2;
1514     } else if (_test._test == BoolTest::lt &&
1515                cmp2->Opcode() == Op_AddI &&
1516                cmp2->in(2)->find_int_con(0) == 1) {
1517       bound = cmp2->in(1);
1518     }
1519     if (cmp1->in(2) == bound || cmp1->in(1) == bound) {
1520       return ConINode::make(1);
1521     }
1522   }
1523 
1524   // Change ((x & (m - 1)) u< m) into (m > 0)
1525   // This is the off-by-one variant of the above
1526   if (cop == Op_CmpU &&
1527       _test._test == BoolTest::lt &&
1528       cmp1_op == Op_AndI) {
1529     Node* l = cmp1->in(1);
1530     Node* r = cmp1->in(2);
1531     for (int repeat = 0; repeat < 2; repeat++) {
1532       bool match = r->Opcode() == Op_AddI && r->in(2)->find_int_con(0) == -1 &&
1533                    r->in(1) == cmp2;
1534       if (match) {
1535         // arraylength known to be non-negative, so a (arraylength != 0) is sufficient,
1536         // but to be compatible with the array range check pattern, use (arraylength u> 0)
1537         Node* ncmp = cmp2->Opcode() == Op_LoadRange
1538                      ? phase->transform(new CmpUNode(cmp2, phase->intcon(0)))
1539                      : phase->transform(new CmpINode(cmp2, phase->intcon(0)));
1540         return new BoolNode(ncmp, BoolTest::gt);
1541       } else {
1542         // commute and try again
1543         l = cmp1->in(2);
1544         r = cmp1->in(1);
1545       }
1546     }
1547   }
1548 
1549   // Change x u< 1 or x u<= 0 to x == 0
1550   if (cop == Op_CmpU &&
1551       cmp1_op != Op_LoadRange &&
1552       ((_test._test == BoolTest::lt &&
1553         cmp2->find_int_con(-1) == 1) ||
1554        (_test._test == BoolTest::le &&
1555         cmp2->find_int_con(-1) == 0))) {
1556     Node* ncmp = phase->transform(new CmpINode(cmp1, phase->intcon(0)));
1557     return new BoolNode(ncmp, BoolTest::eq);
1558   }
1559 
1560   // Change (arraylength <= 0) or (arraylength == 0)
1561   //   into (arraylength u<= 0)
1562   // Also change (arraylength != 0) into (arraylength u> 0)
1563   // The latter version matches the code pattern generated for
1564   // array range checks, which will more likely be optimized later.
1565   if (cop == Op_CmpI &&
1566       cmp1_op == Op_LoadRange &&
1567       cmp2->find_int_con(-1) == 0) {
1568     if (_test._test == BoolTest::le || _test._test == BoolTest::eq) {
1569       Node* ncmp = phase->transform(new CmpUNode(cmp1, cmp2));
1570       return new BoolNode(ncmp, BoolTest::le);
1571     } else if (_test._test == BoolTest::ne) {
1572       Node* ncmp = phase->transform(new CmpUNode(cmp1, cmp2));
1573       return new BoolNode(ncmp, BoolTest::gt);
1574     }
1575   }
1576 
1577   // Change "bool eq/ne (cmp (Conv2B X) 0)" into "bool eq/ne (cmp X 0)".
1578   // This is a standard idiom for branching on a boolean value.
1579   Node *c2b = cmp1;
1580   if( cmp2_type == TypeInt::ZERO &&
1581       cmp1_op == Op_Conv2B &&
1582       (_test._test == BoolTest::eq ||
1583        _test._test == BoolTest::ne) ) {
1584     Node *ncmp = phase->transform(phase->type(c2b->in(1))->isa_int()
1585        ? (Node*)new CmpINode(c2b->in(1),cmp2)
1586        : (Node*)new CmpPNode(c2b->in(1),phase->makecon(TypePtr::NULL_PTR))
1587     );
1588     return new BoolNode( ncmp, _test._test );
1589   }
1590 
1591   // Comparing a SubI against a zero is equal to comparing the SubI
1592   // arguments directly.  This only works for eq and ne comparisons
1593   // due to possible integer overflow.
1594   if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1595         (cop == Op_CmpI) &&
1596         (cmp1_op == Op_SubI) &&
1597         ( cmp2_type == TypeInt::ZERO ) ) {
1598     Node *ncmp = phase->transform( new CmpINode(cmp1->in(1),cmp1->in(2)));
1599     return new BoolNode( ncmp, _test._test );
1600   }
1601 
1602   // Same as above but with and AddI of a constant
1603   if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1604       cop == Op_CmpI &&
1605       cmp1_op == Op_AddI &&
1606       cmp1->in(2) != NULL &&
1607       phase->type(cmp1->in(2))->isa_int() &&
1608       phase->type(cmp1->in(2))->is_int()->is_con() &&
1609       cmp2_type == TypeInt::ZERO &&
1610       !is_counted_loop_cmp(cmp) // modifying the exit test of a counted loop messes the counted loop shape
1611       ) {
1612     const TypeInt* cmp1_in2 = phase->type(cmp1->in(2))->is_int();
1613     Node *ncmp = phase->transform( new CmpINode(cmp1->in(1),phase->intcon(-cmp1_in2->_hi)));
1614     return new BoolNode( ncmp, _test._test );
1615   }
1616 
1617   // Change "bool eq/ne (cmp (phi (X -X) 0))" into "bool eq/ne (cmp X 0)"
1618   // since zero check of conditional negation of an integer is equal to
1619   // zero check of the integer directly.
1620   if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1621       (cop == Op_CmpI) &&
1622       (cmp2_type == TypeInt::ZERO) &&
1623       (cmp1_op == Op_Phi)) {
1624     // There should be a diamond phi with true path at index 1 or 2
1625     PhiNode *phi = cmp1->as_Phi();
1626     int idx_true = phi->is_diamond_phi();
1627     if (idx_true != 0) {
1628       // True input is in(idx_true) while false input is in(3 - idx_true)
1629       Node *tin = phi->in(idx_true);
1630       Node *fin = phi->in(3 - idx_true);
1631       if ((tin->Opcode() == Op_SubI) &&
1632           (phase->type(tin->in(1)) == TypeInt::ZERO) &&
1633           (tin->in(2) == fin)) {
1634         // Found conditional negation at true path, create a new CmpINode without that
1635         Node *ncmp = phase->transform(new CmpINode(fin, cmp2));
1636         return new BoolNode(ncmp, _test._test);
1637       }
1638       if ((fin->Opcode() == Op_SubI) &&
1639           (phase->type(fin->in(1)) == TypeInt::ZERO) &&
1640           (fin->in(2) == tin)) {
1641         // Found conditional negation at false path, create a new CmpINode without that
1642         Node *ncmp = phase->transform(new CmpINode(tin, cmp2));
1643         return new BoolNode(ncmp, _test._test);
1644       }
1645     }
1646   }
1647 
1648   // Change (-A vs 0) into (A vs 0) by commuting the test.  Disallow in the
1649   // most general case because negating 0x80000000 does nothing.  Needed for
1650   // the CmpF3/SubI/CmpI idiom.
1651   if( cop == Op_CmpI &&
1652       cmp1_op == Op_SubI &&
1653       cmp2_type == TypeInt::ZERO &&
1654       phase->type( cmp1->in(1) ) == TypeInt::ZERO &&
1655       phase->type( cmp1->in(2) )->higher_equal(TypeInt::SYMINT) ) {
1656     Node *ncmp = phase->transform( new CmpINode(cmp1->in(2),cmp2));
1657     return new BoolNode( ncmp, _test.commute() );
1658   }
1659 
1660   // Try to optimize signed integer comparison
1661   return fold_cmpI(phase, cmp->as_Sub(), cmp1, cop, cmp1_op, cmp2_type);
1662 
1663   //  The transformation below is not valid for either signed or unsigned
1664   //  comparisons due to wraparound concerns at MAX_VALUE and MIN_VALUE.
1665   //  This transformation can be resurrected when we are able to
1666   //  make inferences about the range of values being subtracted from
1667   //  (or added to) relative to the wraparound point.
1668   //
1669   //    // Remove +/-1's if possible.
1670   //    // "X <= Y-1" becomes "X <  Y"
1671   //    // "X+1 <= Y" becomes "X <  Y"
1672   //    // "X <  Y+1" becomes "X <= Y"
1673   //    // "X-1 <  Y" becomes "X <= Y"
1674   //    // Do not this to compares off of the counted-loop-end.  These guys are
1675   //    // checking the trip counter and they want to use the post-incremented
1676   //    // counter.  If they use the PRE-incremented counter, then the counter has
1677   //    // to be incremented in a private block on a loop backedge.
1678   //    if( du && du->cnt(this) && du->out(this)[0]->Opcode() == Op_CountedLoopEnd )
1679   //      return NULL;
1680   //  #ifndef PRODUCT
1681   //    // Do not do this in a wash GVN pass during verification.
1682   //    // Gets triggered by too many simple optimizations to be bothered with
1683   //    // re-trying it again and again.
1684   //    if( !phase->allow_progress() ) return NULL;
1685   //  #endif
1686   //    // Not valid for unsigned compare because of corner cases in involving zero.
1687   //    // For example, replacing "X-1 <u Y" with "X <=u Y" fails to throw an
1688   //    // exception in case X is 0 (because 0-1 turns into 4billion unsigned but
1689   //    // "0 <=u Y" is always true).
1690   //    if( cmp->Opcode() == Op_CmpU ) return NULL;
1691   //    int cmp2_op = cmp2->Opcode();
1692   //    if( _test._test == BoolTest::le ) {
1693   //      if( cmp1_op == Op_AddI &&
1694   //          phase->type( cmp1->in(2) ) == TypeInt::ONE )
1695   //        return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::lt );
1696   //      else if( cmp2_op == Op_AddI &&
1697   //         phase->type( cmp2->in(2) ) == TypeInt::MINUS_1 )
1698   //        return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::lt );
1699   //    } else if( _test._test == BoolTest::lt ) {
1700   //      if( cmp1_op == Op_AddI &&
1701   //          phase->type( cmp1->in(2) ) == TypeInt::MINUS_1 )
1702   //        return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::le );
1703   //      else if( cmp2_op == Op_AddI &&
1704   //         phase->type( cmp2->in(2) ) == TypeInt::ONE )
1705   //        return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::le );
1706   //    }
1707 }
1708 
1709 //------------------------------Value------------------------------------------
1710 // Simplify a Bool (convert condition codes to boolean (1 or 0)) node,
1711 // based on local information.   If the input is constant, do it.
1712 const Type* BoolNode::Value(PhaseGVN* phase) const {
1713   return _test.cc2logical( phase->type( in(1) ) );
1714 }
1715 
1716 #ifndef PRODUCT
1717 //------------------------------dump_spec--------------------------------------
1718 // Dump special per-node info
1719 void BoolNode::dump_spec(outputStream *st) const {
1720   st->print("[");
1721   _test.dump_on(st);
1722   st->print("]");
1723 }
1724 
1725 //-------------------------------related---------------------------------------
1726 // A BoolNode's related nodes are all of its data inputs, and all of its
1727 // outputs until control nodes are hit, which are included. In compact
1728 // representation, inputs till level 3 and immediate outputs are included.
1729 void BoolNode::related(GrowableArray<Node*> *in_rel, GrowableArray<Node*> *out_rel, bool compact) const {
1730   if (compact) {
1731     this->collect_nodes(in_rel, 3, false, true);
1732     this->collect_nodes(out_rel, -1, false, false);
1733   } else {
1734     this->collect_nodes_in_all_data(in_rel, false);
1735     this->collect_nodes_out_all_ctrl_boundary(out_rel);
1736   }
1737 }
1738 #endif
1739 
1740 //----------------------is_counted_loop_exit_test------------------------------
1741 // Returns true if node is used by a counted loop node.
1742 bool BoolNode::is_counted_loop_exit_test() {
1743   for( DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++ ) {
1744     Node* use = fast_out(i);
1745     if (use->is_CountedLoopEnd()) {
1746       return true;
1747     }
1748   }
1749   return false;
1750 }
1751 
1752 //=============================================================================
1753 //------------------------------Value------------------------------------------
1754 // Compute sqrt
1755 const Type* SqrtDNode::Value(PhaseGVN* phase) const {
1756   const Type *t1 = phase->type( in(1) );
1757   if( t1 == Type::TOP ) return Type::TOP;
1758   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1759   double d = t1->getd();
1760   if( d < 0.0 ) return Type::DOUBLE;
1761   return TypeD::make( sqrt( d ) );
1762 }
1763 
1764 const Type* SqrtFNode::Value(PhaseGVN* phase) const {
1765   const Type *t1 = phase->type( in(1) );
1766   if( t1 == Type::TOP ) return Type::TOP;
1767   if( t1->base() != Type::FloatCon ) return Type::FLOAT;
1768   float f = t1->getf();
1769   if( f < 0.0f ) return Type::FLOAT;
1770   return TypeF::make( (float)sqrt( (double)f ) );
1771 }