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