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