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