1 /*
   2  * Copyright 1997-2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 // Portions of code courtesy of Clifford Click
  26 
  27 // Optimization - Graph Style
  28 
  29 #include "incls/_precompiled.incl"
  30 #include "incls/_subnode.cpp.incl"
  31 #include "math.h"
  32 
  33 //=============================================================================
  34 //------------------------------Identity---------------------------------------
  35 // If right input is a constant 0, return the left input.
  36 Node *SubNode::Identity( PhaseTransform *phase ) {
  37   assert(in(1) != this, "Must already have called Value");
  38   assert(in(2) != this, "Must already have called Value");
  39 
  40   // Remove double negation
  41   const Type *zero = add_id();
  42   if( phase->type( in(1) )->higher_equal( zero ) &&
  43       in(2)->Opcode() == Opcode() &&
  44       phase->type( in(2)->in(1) )->higher_equal( zero ) ) {
  45     return in(2)->in(2);
  46   }
  47 
  48   // Convert "(X+Y) - Y" into X and "(X+Y) - X" into Y
  49   if( in(1)->Opcode() == Op_AddI ) {
  50     if( phase->eqv(in(1)->in(2),in(2)) )
  51       return in(1)->in(1);
  52     if (phase->eqv(in(1)->in(1),in(2)))
  53       return in(1)->in(2);
  54 
  55     // Also catch: "(X + Opaque2(Y)) - Y".  In this case, 'Y' is a loop-varying
  56     // trip counter and X is likely to be loop-invariant (that's how O2 Nodes
  57     // are originally used, although the optimizer sometimes jiggers things).
  58     // This folding through an O2 removes a loop-exit use of a loop-varying
  59     // value and generally lowers register pressure in and around the loop.
  60     if( in(1)->in(2)->Opcode() == Op_Opaque2 &&
  61         phase->eqv(in(1)->in(2)->in(1),in(2)) )
  62       return in(1)->in(1);
  63   }
  64 
  65   return ( phase->type( in(2) )->higher_equal( zero ) ) ? in(1) : this;
  66 }
  67 
  68 //------------------------------Value------------------------------------------
  69 // A subtract node differences it's two inputs.
  70 const Type *SubNode::Value( PhaseTransform *phase ) const {
  71   const Node* in1 = in(1);
  72   const Node* in2 = in(2);
  73   // Either input is TOP ==> the result is TOP
  74   const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
  75   if( t1 == Type::TOP ) return Type::TOP;
  76   const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
  77   if( t2 == Type::TOP ) return Type::TOP;
  78 
  79   // Not correct for SubFnode and AddFNode (must check for infinity)
  80   // Equal?  Subtract is zero
  81   if (phase->eqv_uncast(in1, in2))  return add_id();
  82 
  83   // Either input is BOTTOM ==> the result is the local BOTTOM
  84   if( t1 == Type::BOTTOM || t2 == Type::BOTTOM )
  85     return bottom_type();
  86 
  87   return sub(t1,t2);            // Local flavor of type subtraction
  88 
  89 }
  90 
  91 //=============================================================================
  92 
  93 //------------------------------Helper function--------------------------------
  94 static bool ok_to_convert(Node* inc, Node* iv) {
  95     // Do not collapse (x+c0)-y if "+" is a loop increment, because the
  96     // "-" is loop invariant and collapsing extends the live-range of "x"
  97     // to overlap with the "+", forcing another register to be used in
  98     // the loop.
  99     // This test will be clearer with '&&' (apply DeMorgan's rule)
 100     // but I like the early cutouts that happen here.
 101     const PhiNode *phi;
 102     if( ( !inc->in(1)->is_Phi() ||
 103           !(phi=inc->in(1)->as_Phi()) ||
 104           phi->is_copy() ||
 105           !phi->region()->is_CountedLoop() ||
 106           inc != phi->region()->as_CountedLoop()->incr() )
 107        &&
 108         // Do not collapse (x+c0)-iv if "iv" is a loop induction variable,
 109         // because "x" maybe invariant.
 110         ( !iv->is_loop_iv() )
 111       ) {
 112       return true;
 113     } else {
 114       return false;
 115     }
 116 }
 117 //------------------------------Ideal------------------------------------------
 118 Node *SubINode::Ideal(PhaseGVN *phase, bool can_reshape){
 119   Node *in1 = in(1);
 120   Node *in2 = in(2);
 121   uint op1 = in1->Opcode();
 122   uint op2 = in2->Opcode();
 123 
 124 #ifdef ASSERT
 125   // Check for dead loop
 126   if( phase->eqv( in1, this ) || phase->eqv( in2, this ) ||
 127       ( op1 == Op_AddI || op1 == Op_SubI ) &&
 128       ( phase->eqv( in1->in(1), this ) || phase->eqv( in1->in(2), this ) ||
 129         phase->eqv( in1->in(1), in1  ) || phase->eqv( in1->in(2), in1 ) ) )
 130     assert(false, "dead loop in SubINode::Ideal");
 131 #endif
 132 
 133   const Type *t2 = phase->type( in2 );
 134   if( t2 == Type::TOP ) return NULL;
 135   // Convert "x-c0" into "x+ -c0".
 136   if( t2->base() == Type::Int ){        // Might be bottom or top...
 137     const TypeInt *i = t2->is_int();
 138     if( i->is_con() )
 139       return new (phase->C, 3) AddINode(in1, phase->intcon(-i->get_con()));
 140   }
 141 
 142   // Convert "(x+c0) - y" into (x-y) + c0"
 143   // Do not collapse (x+c0)-y if "+" is a loop increment or
 144   // if "y" is a loop induction variable.
 145   if( op1 == Op_AddI && ok_to_convert(in1, in2) ) {
 146     const Type *tadd = phase->type( in1->in(2) );
 147     if( tadd->singleton() && tadd != Type::TOP ) {
 148       Node *sub2 = phase->transform( new (phase->C, 3) SubINode( in1->in(1), in2 ));
 149       return new (phase->C, 3) AddINode( sub2, in1->in(2) );
 150     }
 151   }
 152 
 153 
 154   // Convert "x - (y+c0)" into "(x-y) - c0"
 155   // Need the same check as in above optimization but reversed.
 156   if (op2 == Op_AddI && ok_to_convert(in2, in1)) {
 157     Node* in21 = in2->in(1);
 158     Node* in22 = in2->in(2);
 159     const TypeInt* tcon = phase->type(in22)->isa_int();
 160     if (tcon != NULL && tcon->is_con()) {
 161       Node* sub2 = phase->transform( new (phase->C, 3) SubINode(in1, in21) );
 162       Node* neg_c0 = phase->intcon(- tcon->get_con());
 163       return new (phase->C, 3) AddINode(sub2, neg_c0);
 164     }
 165   }
 166 
 167   const Type *t1 = phase->type( in1 );
 168   if( t1 == Type::TOP ) return NULL;
 169 
 170 #ifdef ASSERT
 171   // Check for dead loop
 172   if( ( op2 == Op_AddI || op2 == Op_SubI ) &&
 173       ( phase->eqv( in2->in(1), this ) || phase->eqv( in2->in(2), this ) ||
 174         phase->eqv( in2->in(1), in2  ) || phase->eqv( in2->in(2), in2  ) ) )
 175     assert(false, "dead loop in SubINode::Ideal");
 176 #endif
 177 
 178   // Convert "x - (x+y)" into "-y"
 179   if( op2 == Op_AddI &&
 180       phase->eqv( in1, in2->in(1) ) )
 181     return new (phase->C, 3) SubINode( phase->intcon(0),in2->in(2));
 182   // Convert "(x-y) - x" into "-y"
 183   if( op1 == Op_SubI &&
 184       phase->eqv( in1->in(1), in2 ) )
 185     return new (phase->C, 3) SubINode( phase->intcon(0),in1->in(2));
 186   // Convert "x - (y+x)" into "-y"
 187   if( op2 == Op_AddI &&
 188       phase->eqv( in1, in2->in(2) ) )
 189     return new (phase->C, 3) SubINode( phase->intcon(0),in2->in(1));
 190 
 191   // Convert "0 - (x-y)" into "y-x"
 192   if( t1 == TypeInt::ZERO && op2 == Op_SubI )
 193     return new (phase->C, 3) SubINode( in2->in(2), in2->in(1) );
 194 
 195   // Convert "0 - (x+con)" into "-con-x"
 196   jint con;
 197   if( t1 == TypeInt::ZERO && op2 == Op_AddI &&
 198       (con = in2->in(2)->find_int_con(0)) != 0 )
 199     return new (phase->C, 3) SubINode( phase->intcon(-con), in2->in(1) );
 200 
 201   // Convert "(X+A) - (X+B)" into "A - B"
 202   if( op1 == Op_AddI && op2 == Op_AddI && in1->in(1) == in2->in(1) )
 203     return new (phase->C, 3) SubINode( in1->in(2), in2->in(2) );
 204 
 205   // Convert "(A+X) - (B+X)" into "A - B"
 206   if( op1 == Op_AddI && op2 == Op_AddI && in1->in(2) == in2->in(2) )
 207     return new (phase->C, 3) SubINode( in1->in(1), in2->in(1) );
 208 
 209   // Convert "A-(B-C)" into (A+C)-B", since add is commutative and generally
 210   // nicer to optimize than subtract.
 211   if( op2 == Op_SubI && in2->outcnt() == 1) {
 212     Node *add1 = phase->transform( new (phase->C, 3) AddINode( in1, in2->in(2) ) );
 213     return new (phase->C, 3) SubINode( add1, in2->in(1) );
 214   }
 215 
 216   return NULL;
 217 }
 218 
 219 //------------------------------sub--------------------------------------------
 220 // A subtract node differences it's two inputs.
 221 const Type *SubINode::sub( const Type *t1, const Type *t2 ) const {
 222   const TypeInt *r0 = t1->is_int(); // Handy access
 223   const TypeInt *r1 = t2->is_int();
 224   int32 lo = r0->_lo - r1->_hi;
 225   int32 hi = r0->_hi - r1->_lo;
 226 
 227   // We next check for 32-bit overflow.
 228   // If that happens, we just assume all integers are possible.
 229   if( (((r0->_lo ^ r1->_hi) >= 0) ||    // lo ends have same signs OR
 230        ((r0->_lo ^      lo) >= 0)) &&   // lo results have same signs AND
 231       (((r0->_hi ^ r1->_lo) >= 0) ||    // hi ends have same signs OR
 232        ((r0->_hi ^      hi) >= 0)) )    // hi results have same signs
 233     return TypeInt::make(lo,hi,MAX2(r0->_widen,r1->_widen));
 234   else                          // Overflow; assume all integers
 235     return TypeInt::INT;
 236 }
 237 
 238 //=============================================================================
 239 //------------------------------Ideal------------------------------------------
 240 Node *SubLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 241   Node *in1 = in(1);
 242   Node *in2 = in(2);
 243   uint op1 = in1->Opcode();
 244   uint op2 = in2->Opcode();
 245 
 246 #ifdef ASSERT
 247   // Check for dead loop
 248   if( phase->eqv( in1, this ) || phase->eqv( in2, this ) ||
 249       ( op1 == Op_AddL || op1 == Op_SubL ) &&
 250       ( phase->eqv( in1->in(1), this ) || phase->eqv( in1->in(2), this ) ||
 251         phase->eqv( in1->in(1), in1  ) || phase->eqv( in1->in(2), in1  ) ) )
 252     assert(false, "dead loop in SubLNode::Ideal");
 253 #endif
 254 
 255   if( phase->type( in2 ) == Type::TOP ) return NULL;
 256   const TypeLong *i = phase->type( in2 )->isa_long();
 257   // Convert "x-c0" into "x+ -c0".
 258   if( i &&                      // Might be bottom or top...
 259       i->is_con() )
 260     return new (phase->C, 3) AddLNode(in1, phase->longcon(-i->get_con()));
 261 
 262   // Convert "(x+c0) - y" into (x-y) + c0"
 263   // Do not collapse (x+c0)-y if "+" is a loop increment or
 264   // if "y" is a loop induction variable.
 265   if( op1 == Op_AddL && ok_to_convert(in1, in2) ) {
 266     Node *in11 = in1->in(1);
 267     const Type *tadd = phase->type( in1->in(2) );
 268     if( tadd->singleton() && tadd != Type::TOP ) {
 269       Node *sub2 = phase->transform( new (phase->C, 3) SubLNode( in11, in2 ));
 270       return new (phase->C, 3) AddLNode( sub2, in1->in(2) );
 271     }
 272   }
 273 
 274   // Convert "x - (y+c0)" into "(x-y) - c0"
 275   // Need the same check as in above optimization but reversed.
 276   if (op2 == Op_AddL && ok_to_convert(in2, in1)) {
 277     Node* in21 = in2->in(1);
 278     Node* in22 = in2->in(2);
 279     const TypeLong* tcon = phase->type(in22)->isa_long();
 280     if (tcon != NULL && tcon->is_con()) {
 281       Node* sub2 = phase->transform( new (phase->C, 3) SubLNode(in1, in21) );
 282       Node* neg_c0 = phase->longcon(- tcon->get_con());
 283       return new (phase->C, 3) AddLNode(sub2, neg_c0);
 284     }
 285   }
 286 
 287   const Type *t1 = phase->type( in1 );
 288   if( t1 == Type::TOP ) return NULL;
 289 
 290 #ifdef ASSERT
 291   // Check for dead loop
 292   if( ( op2 == Op_AddL || op2 == Op_SubL ) &&
 293       ( phase->eqv( in2->in(1), this ) || phase->eqv( in2->in(2), this ) ||
 294         phase->eqv( in2->in(1), in2  ) || phase->eqv( in2->in(2), in2  ) ) )
 295     assert(false, "dead loop in SubLNode::Ideal");
 296 #endif
 297 
 298   // Convert "x - (x+y)" into "-y"
 299   if( op2 == Op_AddL &&
 300       phase->eqv( in1, in2->in(1) ) )
 301     return new (phase->C, 3) SubLNode( phase->makecon(TypeLong::ZERO), in2->in(2));
 302   // Convert "x - (y+x)" into "-y"
 303   if( op2 == Op_AddL &&
 304       phase->eqv( in1, in2->in(2) ) )
 305     return new (phase->C, 3) SubLNode( phase->makecon(TypeLong::ZERO),in2->in(1));
 306 
 307   // Convert "0 - (x-y)" into "y-x"
 308   if( phase->type( in1 ) == TypeLong::ZERO && op2 == Op_SubL )
 309     return new (phase->C, 3) SubLNode( in2->in(2), in2->in(1) );
 310 
 311   // Convert "(X+A) - (X+B)" into "A - B"
 312   if( op1 == Op_AddL && op2 == Op_AddL && in1->in(1) == in2->in(1) )
 313     return new (phase->C, 3) SubLNode( in1->in(2), in2->in(2) );
 314 
 315   // Convert "(A+X) - (B+X)" into "A - B"
 316   if( op1 == Op_AddL && op2 == Op_AddL && in1->in(2) == in2->in(2) )
 317     return new (phase->C, 3) SubLNode( in1->in(1), in2->in(1) );
 318 
 319   // Convert "A-(B-C)" into (A+C)-B"
 320   if( op2 == Op_SubL && in2->outcnt() == 1) {
 321     Node *add1 = phase->transform( new (phase->C, 3) AddLNode( in1, in2->in(2) ) );
 322     return new (phase->C, 3) SubLNode( add1, in2->in(1) );
 323   }
 324 
 325   return NULL;
 326 }
 327 
 328 //------------------------------sub--------------------------------------------
 329 // A subtract node differences it's two inputs.
 330 const Type *SubLNode::sub( const Type *t1, const Type *t2 ) const {
 331   const TypeLong *r0 = t1->is_long(); // Handy access
 332   const TypeLong *r1 = t2->is_long();
 333   jlong lo = r0->_lo - r1->_hi;
 334   jlong hi = r0->_hi - r1->_lo;
 335 
 336   // We next check for 32-bit overflow.
 337   // If that happens, we just assume all integers are possible.
 338   if( (((r0->_lo ^ r1->_hi) >= 0) ||    // lo ends have same signs OR
 339        ((r0->_lo ^      lo) >= 0)) &&   // lo results have same signs AND
 340       (((r0->_hi ^ r1->_lo) >= 0) ||    // hi ends have same signs OR
 341        ((r0->_hi ^      hi) >= 0)) )    // hi results have same signs
 342     return TypeLong::make(lo,hi,MAX2(r0->_widen,r1->_widen));
 343   else                          // Overflow; assume all integers
 344     return TypeLong::LONG;
 345 }
 346 
 347 //=============================================================================
 348 //------------------------------Value------------------------------------------
 349 // A subtract node differences its two inputs.
 350 const Type *SubFPNode::Value( PhaseTransform *phase ) const {
 351   const Node* in1 = in(1);
 352   const Node* in2 = in(2);
 353   // Either input is TOP ==> the result is TOP
 354   const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
 355   if( t1 == Type::TOP ) return Type::TOP;
 356   const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
 357   if( t2 == Type::TOP ) return Type::TOP;
 358 
 359   // if both operands are infinity of same sign, the result is NaN; do
 360   // not replace with zero
 361   if( (t1->is_finite() && t2->is_finite()) ) {
 362     if( phase->eqv(in1, in2) ) return add_id();
 363   }
 364 
 365   // Either input is BOTTOM ==> the result is the local BOTTOM
 366   const Type *bot = bottom_type();
 367   if( (t1 == bot) || (t2 == bot) ||
 368       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 369     return bot;
 370 
 371   return sub(t1,t2);            // Local flavor of type subtraction
 372 }
 373 
 374 
 375 //=============================================================================
 376 //------------------------------Ideal------------------------------------------
 377 Node *SubFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 378   const Type *t2 = phase->type( in(2) );
 379   // Convert "x-c0" into "x+ -c0".
 380   if( t2->base() == Type::FloatCon ) {  // Might be bottom or top...
 381     // return new (phase->C, 3) AddFNode(in(1), phase->makecon( TypeF::make(-t2->getf()) ) );
 382   }
 383 
 384   // Not associative because of boundary conditions (infinity)
 385   if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
 386     // Convert "x - (x+y)" into "-y"
 387     if( in(2)->is_Add() &&
 388         phase->eqv(in(1),in(2)->in(1) ) )
 389       return new (phase->C, 3) SubFNode( phase->makecon(TypeF::ZERO),in(2)->in(2));
 390   }
 391 
 392   // Cannot replace 0.0-X with -X because a 'fsub' bytecode computes
 393   // 0.0-0.0 as +0.0, while a 'fneg' bytecode computes -0.0.
 394   //if( phase->type(in(1)) == TypeF::ZERO )
 395   //return new (phase->C, 2) NegFNode(in(2));
 396 
 397   return NULL;
 398 }
 399 
 400 //------------------------------sub--------------------------------------------
 401 // A subtract node differences its two inputs.
 402 const Type *SubFNode::sub( const Type *t1, const Type *t2 ) const {
 403   // no folding if one of operands is infinity or NaN, do not do constant folding
 404   if( g_isfinite(t1->getf()) && g_isfinite(t2->getf()) ) {
 405     return TypeF::make( t1->getf() - t2->getf() );
 406   }
 407   else if( g_isnan(t1->getf()) ) {
 408     return t1;
 409   }
 410   else if( g_isnan(t2->getf()) ) {
 411     return t2;
 412   }
 413   else {
 414     return Type::FLOAT;
 415   }
 416 }
 417 
 418 //=============================================================================
 419 //------------------------------Ideal------------------------------------------
 420 Node *SubDNode::Ideal(PhaseGVN *phase, bool can_reshape){
 421   const Type *t2 = phase->type( in(2) );
 422   // Convert "x-c0" into "x+ -c0".
 423   if( t2->base() == Type::DoubleCon ) { // Might be bottom or top...
 424     // return new (phase->C, 3) AddDNode(in(1), phase->makecon( TypeD::make(-t2->getd()) ) );
 425   }
 426 
 427   // Not associative because of boundary conditions (infinity)
 428   if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
 429     // Convert "x - (x+y)" into "-y"
 430     if( in(2)->is_Add() &&
 431         phase->eqv(in(1),in(2)->in(1) ) )
 432       return new (phase->C, 3) SubDNode( phase->makecon(TypeD::ZERO),in(2)->in(2));
 433   }
 434 
 435   // Cannot replace 0.0-X with -X because a 'dsub' bytecode computes
 436   // 0.0-0.0 as +0.0, while a 'dneg' bytecode computes -0.0.
 437   //if( phase->type(in(1)) == TypeD::ZERO )
 438   //return new (phase->C, 2) NegDNode(in(2));
 439 
 440   return NULL;
 441 }
 442 
 443 //------------------------------sub--------------------------------------------
 444 // A subtract node differences its two inputs.
 445 const Type *SubDNode::sub( const Type *t1, const Type *t2 ) const {
 446   // no folding if one of operands is infinity or NaN, do not do constant folding
 447   if( g_isfinite(t1->getd()) && g_isfinite(t2->getd()) ) {
 448     return TypeD::make( t1->getd() - t2->getd() );
 449   }
 450   else if( g_isnan(t1->getd()) ) {
 451     return t1;
 452   }
 453   else if( g_isnan(t2->getd()) ) {
 454     return t2;
 455   }
 456   else {
 457     return Type::DOUBLE;
 458   }
 459 }
 460 
 461 //=============================================================================
 462 //------------------------------Idealize---------------------------------------
 463 // Unlike SubNodes, compare must still flatten return value to the
 464 // range -1, 0, 1.
 465 // And optimizations like those for (X + Y) - X fail if overflow happens.
 466 Node *CmpNode::Identity( PhaseTransform *phase ) {
 467   return this;
 468 }
 469 
 470 //=============================================================================
 471 //------------------------------cmp--------------------------------------------
 472 // Simplify a CmpI (compare 2 integers) node, based on local information.
 473 // If both inputs are constants, compare them.
 474 const Type *CmpINode::sub( const Type *t1, const Type *t2 ) const {
 475   const TypeInt *r0 = t1->is_int(); // Handy access
 476   const TypeInt *r1 = t2->is_int();
 477 
 478   if( r0->_hi < r1->_lo )       // Range is always low?
 479     return TypeInt::CC_LT;
 480   else if( r0->_lo > r1->_hi )  // Range is always high?
 481     return TypeInt::CC_GT;
 482 
 483   else if( r0->is_con() && r1->is_con() ) { // comparing constants?
 484     assert(r0->get_con() == r1->get_con(), "must be equal");
 485     return TypeInt::CC_EQ;      // Equal results.
 486   } else if( r0->_hi == r1->_lo ) // Range is never high?
 487     return TypeInt::CC_LE;
 488   else if( r0->_lo == r1->_hi ) // Range is never low?
 489     return TypeInt::CC_GE;
 490   return TypeInt::CC;           // else use worst case results
 491 }
 492 
 493 // Simplify a CmpU (compare 2 integers) node, based on local information.
 494 // If both inputs are constants, compare them.
 495 const Type *CmpUNode::sub( const Type *t1, const Type *t2 ) const {
 496   assert(!t1->isa_ptr(), "obsolete usage of CmpU");
 497 
 498   // comparing two unsigned ints
 499   const TypeInt *r0 = t1->is_int();   // Handy access
 500   const TypeInt *r1 = t2->is_int();
 501 
 502   // Current installed version
 503   // Compare ranges for non-overlap
 504   juint lo0 = r0->_lo;
 505   juint hi0 = r0->_hi;
 506   juint lo1 = r1->_lo;
 507   juint hi1 = r1->_hi;
 508 
 509   // If either one has both negative and positive values,
 510   // it therefore contains both 0 and -1, and since [0..-1] is the
 511   // full unsigned range, the type must act as an unsigned bottom.
 512   bool bot0 = ((jint)(lo0 ^ hi0) < 0);
 513   bool bot1 = ((jint)(lo1 ^ hi1) < 0);
 514 
 515   if (bot0 || bot1) {
 516     // All unsigned values are LE -1 and GE 0.
 517     if (lo0 == 0 && hi0 == 0) {
 518       return TypeInt::CC_LE;            //   0 <= bot
 519     } else if (lo1 == 0 && hi1 == 0) {
 520       return TypeInt::CC_GE;            // bot >= 0
 521     }
 522   } else {
 523     // We can use ranges of the form [lo..hi] if signs are the same.
 524     assert(lo0 <= hi0 && lo1 <= hi1, "unsigned ranges are valid");
 525     // results are reversed, '-' > '+' for unsigned compare
 526     if (hi0 < lo1) {
 527       return TypeInt::CC_LT;            // smaller
 528     } else if (lo0 > hi1) {
 529       return TypeInt::CC_GT;            // greater
 530     } else if (hi0 == lo1 && lo0 == hi1) {
 531       return TypeInt::CC_EQ;            // Equal results
 532     } else if (lo0 >= hi1) {
 533       return TypeInt::CC_GE;
 534     } else if (hi0 <= lo1) {
 535       // Check for special case in Hashtable::get.  (See below.)
 536       if ((jint)lo0 >= 0 && (jint)lo1 >= 0 &&
 537           in(1)->Opcode() == Op_ModI &&
 538           in(1)->in(2) == in(2) )
 539         return TypeInt::CC_LT;
 540       return TypeInt::CC_LE;
 541     }
 542   }
 543   // Check for special case in Hashtable::get - the hash index is
 544   // mod'ed to the table size so the following range check is useless.
 545   // Check for: (X Mod Y) CmpU Y, where the mod result and Y both have
 546   // to be positive.
 547   // (This is a gross hack, since the sub method never
 548   // looks at the structure of the node in any other case.)
 549   if ((jint)lo0 >= 0 && (jint)lo1 >= 0 &&
 550       in(1)->Opcode() == Op_ModI &&
 551       in(1)->in(2)->uncast() == in(2)->uncast())
 552     return TypeInt::CC_LT;
 553   return TypeInt::CC;                   // else use worst case results
 554 }
 555 
 556 //------------------------------Idealize---------------------------------------
 557 Node *CmpINode::Ideal( PhaseGVN *phase, bool can_reshape ) {
 558   if (phase->type(in(2))->higher_equal(TypeInt::ZERO)) {
 559     switch (in(1)->Opcode()) {
 560     case Op_CmpL3:              // Collapse a CmpL3/CmpI into a CmpL
 561       return new (phase->C, 3) CmpLNode(in(1)->in(1),in(1)->in(2));
 562     case Op_CmpF3:              // Collapse a CmpF3/CmpI into a CmpF
 563       return new (phase->C, 3) CmpFNode(in(1)->in(1),in(1)->in(2));
 564     case Op_CmpD3:              // Collapse a CmpD3/CmpI into a CmpD
 565       return new (phase->C, 3) CmpDNode(in(1)->in(1),in(1)->in(2));
 566     //case Op_SubI:
 567       // If (x - y) cannot overflow, then ((x - y) <?> 0)
 568       // can be turned into (x <?> y).
 569       // This is handled (with more general cases) by Ideal_sub_algebra.
 570     }
 571   }
 572   return NULL;                  // No change
 573 }
 574 
 575 
 576 //=============================================================================
 577 // Simplify a CmpL (compare 2 longs ) node, based on local information.
 578 // If both inputs are constants, compare them.
 579 const Type *CmpLNode::sub( const Type *t1, const Type *t2 ) const {
 580   const TypeLong *r0 = t1->is_long(); // Handy access
 581   const TypeLong *r1 = t2->is_long();
 582 
 583   if( r0->_hi < r1->_lo )       // Range is always low?
 584     return TypeInt::CC_LT;
 585   else if( r0->_lo > r1->_hi )  // Range is always high?
 586     return TypeInt::CC_GT;
 587 
 588   else if( r0->is_con() && r1->is_con() ) { // comparing constants?
 589     assert(r0->get_con() == r1->get_con(), "must be equal");
 590     return TypeInt::CC_EQ;      // Equal results.
 591   } else if( r0->_hi == r1->_lo ) // Range is never high?
 592     return TypeInt::CC_LE;
 593   else if( r0->_lo == r1->_hi ) // Range is never low?
 594     return TypeInt::CC_GE;
 595   return TypeInt::CC;           // else use worst case results
 596 }
 597 
 598 //=============================================================================
 599 //------------------------------sub--------------------------------------------
 600 // Simplify an CmpP (compare 2 pointers) node, based on local information.
 601 // If both inputs are constants, compare them.
 602 const Type *CmpPNode::sub( const Type *t1, const Type *t2 ) const {
 603   const TypePtr *r0 = t1->is_ptr(); // Handy access
 604   const TypePtr *r1 = t2->is_ptr();
 605 
 606   // Undefined inputs makes for an undefined result
 607   if( TypePtr::above_centerline(r0->_ptr) ||
 608       TypePtr::above_centerline(r1->_ptr) )
 609     return Type::TOP;
 610 
 611   if (r0 == r1 && r0->singleton()) {
 612     // Equal pointer constants (klasses, nulls, etc.)
 613     return TypeInt::CC_EQ;
 614   }
 615 
 616   // See if it is 2 unrelated classes.
 617   const TypeOopPtr* p0 = r0->isa_oopptr();
 618   const TypeOopPtr* p1 = r1->isa_oopptr();
 619   if (p0 && p1) {
 620     Node* in1 = in(1)->uncast();
 621     Node* in2 = in(2)->uncast();
 622     AllocateNode* alloc1 = AllocateNode::Ideal_allocation(in1, NULL);
 623     AllocateNode* alloc2 = AllocateNode::Ideal_allocation(in2, NULL);
 624     if (MemNode::detect_ptr_independence(in1, alloc1, in2, alloc2, NULL)) {
 625       return TypeInt::CC_GT;  // different pointers
 626     }
 627     ciKlass* klass0 = p0->klass();
 628     bool    xklass0 = p0->klass_is_exact();
 629     ciKlass* klass1 = p1->klass();
 630     bool    xklass1 = p1->klass_is_exact();
 631     int kps = (p0->isa_klassptr()?1:0) + (p1->isa_klassptr()?1:0);
 632     if (klass0 && klass1 &&
 633         kps != 1 &&             // both or neither are klass pointers
 634         !klass0->is_interface() && // do not trust interfaces
 635         !klass1->is_interface()) {
 636       bool unrelated_classes = false;
 637       // See if neither subclasses the other, or if the class on top
 638       // is precise.  In either of these cases, the compare is known
 639       // to fail if at least one of the pointers is provably not null.
 640       if (klass0->equals(klass1)   ||   // if types are unequal but klasses are
 641           !klass0->is_java_klass() ||   // types not part of Java language?
 642           !klass1->is_java_klass()) {   // types not part of Java language?
 643         // Do nothing; we know nothing for imprecise types
 644       } else if (klass0->is_subtype_of(klass1)) {
 645         // If klass1's type is PRECISE, then classes are unrelated.
 646         unrelated_classes = xklass1;
 647       } else if (klass1->is_subtype_of(klass0)) {
 648         // If klass0's type is PRECISE, then classes are unrelated.
 649         unrelated_classes = xklass0;
 650       } else {                  // Neither subtypes the other
 651         unrelated_classes = true;
 652       }
 653       if (unrelated_classes) {
 654         // The oops classes are known to be unrelated. If the joined PTRs of
 655         // two oops is not Null and not Bottom, then we are sure that one
 656         // of the two oops is non-null, and the comparison will always fail.
 657         TypePtr::PTR jp = r0->join_ptr(r1->_ptr);
 658         if (jp != TypePtr::Null && jp != TypePtr::BotPTR) {
 659           return TypeInt::CC_GT;
 660         }
 661       }
 662     }
 663   }
 664 
 665   // Known constants can be compared exactly
 666   // Null can be distinguished from any NotNull pointers
 667   // Unknown inputs makes an unknown result
 668   if( r0->singleton() ) {
 669     intptr_t bits0 = r0->get_con();
 670     if( r1->singleton() )
 671       return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT;
 672     return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC;
 673   } else if( r1->singleton() ) {
 674     intptr_t bits1 = r1->get_con();
 675     return ( r0->_ptr == TypePtr::NotNull && bits1==0 ) ? TypeInt::CC_GT : TypeInt::CC;
 676   } else
 677     return TypeInt::CC;
 678 }
 679 
 680 //------------------------------Ideal------------------------------------------
 681 // Check for the case of comparing an unknown klass loaded from the primary
 682 // super-type array vs a known klass with no subtypes.  This amounts to
 683 // checking to see an unknown klass subtypes a known klass with no subtypes;
 684 // this only happens on an exact match.  We can shorten this test by 1 load.
 685 Node *CmpPNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
 686   // Constant pointer on right?
 687   const TypeKlassPtr* t2 = phase->type(in(2))->isa_klassptr();
 688   if (t2 == NULL || !t2->klass_is_exact())
 689     return NULL;
 690   // Get the constant klass we are comparing to.
 691   ciKlass* superklass = t2->klass();
 692 
 693   // Now check for LoadKlass on left.
 694   Node* ldk1 = in(1);
 695   if (ldk1->is_DecodeN()) {
 696     ldk1 = ldk1->in(1);
 697     if (ldk1->Opcode() != Op_LoadNKlass )
 698       return NULL;
 699   } else if (ldk1->Opcode() != Op_LoadKlass )
 700     return NULL;
 701   // Take apart the address of the LoadKlass:
 702   Node* adr1 = ldk1->in(MemNode::Address);
 703   intptr_t con2 = 0;
 704   Node* ldk2 = AddPNode::Ideal_base_and_offset(adr1, phase, con2);
 705   if (ldk2 == NULL)
 706     return NULL;
 707   if (con2 == oopDesc::klass_offset_in_bytes()) {
 708     // We are inspecting an object's concrete class.
 709     // Short-circuit the check if the query is abstract.
 710     if (superklass->is_interface() ||
 711         superklass->is_abstract()) {
 712       // Make it come out always false:
 713       this->set_req(2, phase->makecon(TypePtr::NULL_PTR));
 714       return this;
 715     }
 716   }
 717 
 718   // Check for a LoadKlass from primary supertype array.
 719   // Any nested loadklass from loadklass+con must be from the p.s. array.
 720   if (ldk2->is_DecodeN()) {
 721     // Keep ldk2 as DecodeN since it could be used in CmpP below.
 722     if (ldk2->in(1)->Opcode() != Op_LoadNKlass )
 723       return NULL;
 724   } else if (ldk2->Opcode() != Op_LoadKlass)
 725     return NULL;
 726 
 727   // Verify that we understand the situation
 728   if (con2 != (intptr_t) superklass->super_check_offset())
 729     return NULL;                // Might be element-klass loading from array klass
 730 
 731   // If 'superklass' has no subklasses and is not an interface, then we are
 732   // assured that the only input which will pass the type check is
 733   // 'superklass' itself.
 734   //
 735   // We could be more liberal here, and allow the optimization on interfaces
 736   // which have a single implementor.  This would require us to increase the
 737   // expressiveness of the add_dependency() mechanism.
 738   // %%% Do this after we fix TypeOopPtr:  Deps are expressive enough now.
 739 
 740   // Object arrays must have their base element have no subtypes
 741   while (superklass->is_obj_array_klass()) {
 742     ciType* elem = superklass->as_obj_array_klass()->element_type();
 743     superklass = elem->as_klass();
 744   }
 745   if (superklass->is_instance_klass()) {
 746     ciInstanceKlass* ik = superklass->as_instance_klass();
 747     if (ik->has_subklass() || ik->is_interface())  return NULL;
 748     // Add a dependency if there is a chance that a subclass will be added later.
 749     if (!ik->is_final()) {
 750       phase->C->dependencies()->assert_leaf_type(ik);
 751     }
 752   }
 753 
 754   // Bypass the dependent load, and compare directly
 755   this->set_req(1,ldk2);
 756 
 757   return this;
 758 }
 759 
 760 //=============================================================================
 761 //------------------------------sub--------------------------------------------
 762 // Simplify an CmpN (compare 2 pointers) node, based on local information.
 763 // If both inputs are constants, compare them.
 764 const Type *CmpNNode::sub( const Type *t1, const Type *t2 ) const {
 765   const TypePtr *r0 = t1->make_ptr(); // Handy access
 766   const TypePtr *r1 = t2->make_ptr();
 767 
 768   // Undefined inputs makes for an undefined result
 769   if( TypePtr::above_centerline(r0->_ptr) ||
 770       TypePtr::above_centerline(r1->_ptr) )
 771     return Type::TOP;
 772 
 773   if (r0 == r1 && r0->singleton()) {
 774     // Equal pointer constants (klasses, nulls, etc.)
 775     return TypeInt::CC_EQ;
 776   }
 777 
 778   // See if it is 2 unrelated classes.
 779   const TypeOopPtr* p0 = r0->isa_oopptr();
 780   const TypeOopPtr* p1 = r1->isa_oopptr();
 781   if (p0 && p1) {
 782     ciKlass* klass0 = p0->klass();
 783     bool    xklass0 = p0->klass_is_exact();
 784     ciKlass* klass1 = p1->klass();
 785     bool    xklass1 = p1->klass_is_exact();
 786     int kps = (p0->isa_klassptr()?1:0) + (p1->isa_klassptr()?1:0);
 787     if (klass0 && klass1 &&
 788         kps != 1 &&             // both or neither are klass pointers
 789         !klass0->is_interface() && // do not trust interfaces
 790         !klass1->is_interface()) {
 791       bool unrelated_classes = false;
 792       // See if neither subclasses the other, or if the class on top
 793       // is precise.  In either of these cases, the compare is known
 794       // to fail if at least one of the pointers is provably not null.
 795       if (klass0->equals(klass1)   ||   // if types are unequal but klasses are
 796           !klass0->is_java_klass() ||   // types not part of Java language?
 797           !klass1->is_java_klass()) {   // types not part of Java language?
 798         // Do nothing; we know nothing for imprecise types
 799       } else if (klass0->is_subtype_of(klass1)) {
 800         // If klass1's type is PRECISE, then classes are unrelated.
 801         unrelated_classes = xklass1;
 802       } else if (klass1->is_subtype_of(klass0)) {
 803         // If klass0's type is PRECISE, then classes are unrelated.
 804         unrelated_classes = xklass0;
 805       } else {                  // Neither subtypes the other
 806         unrelated_classes = true;
 807       }
 808       if (unrelated_classes) {
 809         // The oops classes are known to be unrelated. If the joined PTRs of
 810         // two oops is not Null and not Bottom, then we are sure that one
 811         // of the two oops is non-null, and the comparison will always fail.
 812         TypePtr::PTR jp = r0->join_ptr(r1->_ptr);
 813         if (jp != TypePtr::Null && jp != TypePtr::BotPTR) {
 814           return TypeInt::CC_GT;
 815         }
 816       }
 817     }
 818   }
 819 
 820   // Known constants can be compared exactly
 821   // Null can be distinguished from any NotNull pointers
 822   // Unknown inputs makes an unknown result
 823   if( r0->singleton() ) {
 824     intptr_t bits0 = r0->get_con();
 825     if( r1->singleton() )
 826       return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT;
 827     return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC;
 828   } else if( r1->singleton() ) {
 829     intptr_t bits1 = r1->get_con();
 830     return ( r0->_ptr == TypePtr::NotNull && bits1==0 ) ? TypeInt::CC_GT : TypeInt::CC;
 831   } else
 832     return TypeInt::CC;
 833 }
 834 
 835 //------------------------------Ideal------------------------------------------
 836 Node *CmpNNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
 837   return NULL;
 838 }
 839 
 840 //=============================================================================
 841 //------------------------------Value------------------------------------------
 842 // Simplify an CmpF (compare 2 floats ) node, based on local information.
 843 // If both inputs are constants, compare them.
 844 const Type *CmpFNode::Value( PhaseTransform *phase ) const {
 845   const Node* in1 = in(1);
 846   const Node* in2 = in(2);
 847   // Either input is TOP ==> the result is TOP
 848   const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
 849   if( t1 == Type::TOP ) return Type::TOP;
 850   const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
 851   if( t2 == Type::TOP ) return Type::TOP;
 852 
 853   // Not constants?  Don't know squat - even if they are the same
 854   // value!  If they are NaN's they compare to LT instead of EQ.
 855   const TypeF *tf1 = t1->isa_float_constant();
 856   const TypeF *tf2 = t2->isa_float_constant();
 857   if( !tf1 || !tf2 ) return TypeInt::CC;
 858 
 859   // This implements the Java bytecode fcmpl, so unordered returns -1.
 860   if( tf1->is_nan() || tf2->is_nan() )
 861     return TypeInt::CC_LT;
 862 
 863   if( tf1->_f < tf2->_f ) return TypeInt::CC_LT;
 864   if( tf1->_f > tf2->_f ) return TypeInt::CC_GT;
 865   assert( tf1->_f == tf2->_f, "do not understand FP behavior" );
 866   return TypeInt::CC_EQ;
 867 }
 868 
 869 
 870 //=============================================================================
 871 //------------------------------Value------------------------------------------
 872 // Simplify an CmpD (compare 2 doubles ) node, based on local information.
 873 // If both inputs are constants, compare them.
 874 const Type *CmpDNode::Value( PhaseTransform *phase ) const {
 875   const Node* in1 = in(1);
 876   const Node* in2 = in(2);
 877   // Either input is TOP ==> the result is TOP
 878   const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
 879   if( t1 == Type::TOP ) return Type::TOP;
 880   const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
 881   if( t2 == Type::TOP ) return Type::TOP;
 882 
 883   // Not constants?  Don't know squat - even if they are the same
 884   // value!  If they are NaN's they compare to LT instead of EQ.
 885   const TypeD *td1 = t1->isa_double_constant();
 886   const TypeD *td2 = t2->isa_double_constant();
 887   if( !td1 || !td2 ) return TypeInt::CC;
 888 
 889   // This implements the Java bytecode dcmpl, so unordered returns -1.
 890   if( td1->is_nan() || td2->is_nan() )
 891     return TypeInt::CC_LT;
 892 
 893   if( td1->_d < td2->_d ) return TypeInt::CC_LT;
 894   if( td1->_d > td2->_d ) return TypeInt::CC_GT;
 895   assert( td1->_d == td2->_d, "do not understand FP behavior" );
 896   return TypeInt::CC_EQ;
 897 }
 898 
 899 //------------------------------Ideal------------------------------------------
 900 Node *CmpDNode::Ideal(PhaseGVN *phase, bool can_reshape){
 901   // Check if we can change this to a CmpF and remove a ConvD2F operation.
 902   // Change  (CMPD (F2D (float)) (ConD value))
 903   // To      (CMPF      (float)  (ConF value))
 904   // Valid when 'value' does not lose precision as a float.
 905   // Benefits: eliminates conversion, does not require 24-bit mode
 906 
 907   // NaNs prevent commuting operands.  This transform works regardless of the
 908   // order of ConD and ConvF2D inputs by preserving the original order.
 909   int idx_f2d = 1;              // ConvF2D on left side?
 910   if( in(idx_f2d)->Opcode() != Op_ConvF2D )
 911     idx_f2d = 2;                // No, swap to check for reversed args
 912   int idx_con = 3-idx_f2d;      // Check for the constant on other input
 913 
 914   if( ConvertCmpD2CmpF &&
 915       in(idx_f2d)->Opcode() == Op_ConvF2D &&
 916       in(idx_con)->Opcode() == Op_ConD ) {
 917     const TypeD *t2 = in(idx_con)->bottom_type()->is_double_constant();
 918     double t2_value_as_double = t2->_d;
 919     float  t2_value_as_float  = (float)t2_value_as_double;
 920     if( t2_value_as_double == (double)t2_value_as_float ) {
 921       // Test value can be represented as a float
 922       // Eliminate the conversion to double and create new comparison
 923       Node *new_in1 = in(idx_f2d)->in(1);
 924       Node *new_in2 = phase->makecon( TypeF::make(t2_value_as_float) );
 925       if( idx_f2d != 1 ) {      // Must flip args to match original order
 926         Node *tmp = new_in1;
 927         new_in1 = new_in2;
 928         new_in2 = tmp;
 929       }
 930       CmpFNode *new_cmp = (Opcode() == Op_CmpD3)
 931         ? new (phase->C, 3) CmpF3Node( new_in1, new_in2 )
 932         : new (phase->C, 3) CmpFNode ( new_in1, new_in2 ) ;
 933       return new_cmp;           // Changed to CmpFNode
 934     }
 935     // Testing value required the precision of a double
 936   }
 937   return NULL;                  // No change
 938 }
 939 
 940 
 941 //=============================================================================
 942 //------------------------------cc2logical-------------------------------------
 943 // Convert a condition code type to a logical type
 944 const Type *BoolTest::cc2logical( const Type *CC ) const {
 945   if( CC == Type::TOP ) return Type::TOP;
 946   if( CC->base() != Type::Int ) return TypeInt::BOOL; // Bottom or worse
 947   const TypeInt *ti = CC->is_int();
 948   if( ti->is_con() ) {          // Only 1 kind of condition codes set?
 949     // Match low order 2 bits
 950     int tmp = ((ti->get_con()&3) == (_test&3)) ? 1 : 0;
 951     if( _test & 4 ) tmp = 1-tmp;     // Optionally complement result
 952     return TypeInt::make(tmp);       // Boolean result
 953   }
 954 
 955   if( CC == TypeInt::CC_GE ) {
 956     if( _test == ge ) return TypeInt::ONE;
 957     if( _test == lt ) return TypeInt::ZERO;
 958   }
 959   if( CC == TypeInt::CC_LE ) {
 960     if( _test == le ) return TypeInt::ONE;
 961     if( _test == gt ) return TypeInt::ZERO;
 962   }
 963 
 964   return TypeInt::BOOL;
 965 }
 966 
 967 //------------------------------dump_spec-------------------------------------
 968 // Print special per-node info
 969 #ifndef PRODUCT
 970 void BoolTest::dump_on(outputStream *st) const {
 971   const char *msg[] = {"eq","gt","??","lt","ne","le","??","ge"};
 972   st->print(msg[_test]);
 973 }
 974 #endif
 975 
 976 //=============================================================================
 977 uint BoolNode::hash() const { return (Node::hash() << 3)|(_test._test+1); }
 978 uint BoolNode::size_of() const { return sizeof(BoolNode); }
 979 
 980 //------------------------------operator==-------------------------------------
 981 uint BoolNode::cmp( const Node &n ) const {
 982   const BoolNode *b = (const BoolNode *)&n; // Cast up
 983   return (_test._test == b->_test._test);
 984 }
 985 
 986 //------------------------------clone_cmp--------------------------------------
 987 // Clone a compare/bool tree
 988 static Node *clone_cmp( Node *cmp, Node *cmp1, Node *cmp2, PhaseGVN *gvn, BoolTest::mask test ) {
 989   Node *ncmp = cmp->clone();
 990   ncmp->set_req(1,cmp1);
 991   ncmp->set_req(2,cmp2);
 992   ncmp = gvn->transform( ncmp );
 993   return new (gvn->C, 2) BoolNode( ncmp, test );
 994 }
 995 
 996 //-------------------------------make_predicate--------------------------------
 997 Node* BoolNode::make_predicate(Node* test_value, PhaseGVN* phase) {
 998   if (test_value->is_Con())   return test_value;
 999   if (test_value->is_Bool())  return test_value;
1000   Compile* C = phase->C;
1001   if (test_value->is_CMove() &&
1002       test_value->in(CMoveNode::Condition)->is_Bool()) {
1003     BoolNode*   bol   = test_value->in(CMoveNode::Condition)->as_Bool();
1004     const Type* ftype = phase->type(test_value->in(CMoveNode::IfFalse));
1005     const Type* ttype = phase->type(test_value->in(CMoveNode::IfTrue));
1006     if (ftype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ttype)) {
1007       return bol;
1008     } else if (ttype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ftype)) {
1009       return phase->transform( bol->negate(phase) );
1010     }
1011     // Else fall through.  The CMove gets in the way of the test.
1012     // It should be the case that make_predicate(bol->as_int_value()) == bol.
1013   }
1014   Node* cmp = new (C, 3) CmpINode(test_value, phase->intcon(0));
1015   cmp = phase->transform(cmp);
1016   Node* bol = new (C, 2) BoolNode(cmp, BoolTest::ne);
1017   return phase->transform(bol);
1018 }
1019 
1020 //--------------------------------as_int_value---------------------------------
1021 Node* BoolNode::as_int_value(PhaseGVN* phase) {
1022   // Inverse to make_predicate.  The CMove probably boils down to a Conv2B.
1023   Node* cmov = CMoveNode::make(phase->C, NULL, this,
1024                                phase->intcon(0), phase->intcon(1),
1025                                TypeInt::BOOL);
1026   return phase->transform(cmov);
1027 }
1028 
1029 //----------------------------------negate-------------------------------------
1030 BoolNode* BoolNode::negate(PhaseGVN* phase) {
1031   Compile* C = phase->C;
1032   return new (C, 2) BoolNode(in(1), _test.negate());
1033 }
1034 
1035 
1036 //------------------------------Ideal------------------------------------------
1037 Node *BoolNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1038   // Change "bool tst (cmp con x)" into "bool ~tst (cmp x con)".
1039   // This moves the constant to the right.  Helps value-numbering.
1040   Node *cmp = in(1);
1041   if( !cmp->is_Sub() ) return NULL;
1042   int cop = cmp->Opcode();
1043   if( cop == Op_FastLock || cop == Op_FastUnlock ) return NULL;
1044   Node *cmp1 = cmp->in(1);
1045   Node *cmp2 = cmp->in(2);
1046   if( !cmp1 ) return NULL;
1047 
1048   // Constant on left?
1049   Node *con = cmp1;
1050   uint op2 = cmp2->Opcode();
1051   // Move constants to the right of compare's to canonicalize.
1052   // Do not muck with Opaque1 nodes, as this indicates a loop
1053   // guard that cannot change shape.
1054   if( con->is_Con() && !cmp2->is_Con() && op2 != Op_Opaque1 &&
1055       // Because of NaN's, CmpD and CmpF are not commutative
1056       cop != Op_CmpD && cop != Op_CmpF &&
1057       // Protect against swapping inputs to a compare when it is used by a
1058       // counted loop exit, which requires maintaining the loop-limit as in(2)
1059       !is_counted_loop_exit_test() ) {
1060     // Ok, commute the constant to the right of the cmp node.
1061     // Clone the Node, getting a new Node of the same class
1062     cmp = cmp->clone();
1063     // Swap inputs to the clone
1064     cmp->swap_edges(1, 2);
1065     cmp = phase->transform( cmp );
1066     return new (phase->C, 2) BoolNode( cmp, _test.commute() );
1067   }
1068 
1069   // Change "bool eq/ne (cmp (xor X 1) 0)" into "bool ne/eq (cmp X 0)".
1070   // The XOR-1 is an idiom used to flip the sense of a bool.  We flip the
1071   // test instead.
1072   int cmp1_op = cmp1->Opcode();
1073   const TypeInt* cmp2_type = phase->type(cmp2)->isa_int();
1074   if (cmp2_type == NULL)  return NULL;
1075   Node* j_xor = cmp1;
1076   if( cmp2_type == TypeInt::ZERO &&
1077       cmp1_op == Op_XorI &&
1078       j_xor->in(1) != j_xor &&          // An xor of itself is dead
1079       phase->type( j_xor->in(2) ) == TypeInt::ONE &&
1080       (_test._test == BoolTest::eq ||
1081        _test._test == BoolTest::ne) ) {
1082     Node *ncmp = phase->transform(new (phase->C, 3) CmpINode(j_xor->in(1),cmp2));
1083     return new (phase->C, 2) BoolNode( ncmp, _test.negate() );
1084   }
1085 
1086   // Change "bool eq/ne (cmp (Conv2B X) 0)" into "bool eq/ne (cmp X 0)".
1087   // This is a standard idiom for branching on a boolean value.
1088   Node *c2b = cmp1;
1089   if( cmp2_type == TypeInt::ZERO &&
1090       cmp1_op == Op_Conv2B &&
1091       (_test._test == BoolTest::eq ||
1092        _test._test == BoolTest::ne) ) {
1093     Node *ncmp = phase->transform(phase->type(c2b->in(1))->isa_int()
1094        ? (Node*)new (phase->C, 3) CmpINode(c2b->in(1),cmp2)
1095        : (Node*)new (phase->C, 3) CmpPNode(c2b->in(1),phase->makecon(TypePtr::NULL_PTR))
1096     );
1097     return new (phase->C, 2) BoolNode( ncmp, _test._test );
1098   }
1099 
1100   // Comparing a SubI against a zero is equal to comparing the SubI
1101   // arguments directly.  This only works for eq and ne comparisons
1102   // due to possible integer overflow.
1103   if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1104         (cop == Op_CmpI) &&
1105         (cmp1->Opcode() == Op_SubI) &&
1106         ( cmp2_type == TypeInt::ZERO ) ) {
1107     Node *ncmp = phase->transform( new (phase->C, 3) CmpINode(cmp1->in(1),cmp1->in(2)));
1108     return new (phase->C, 2) BoolNode( ncmp, _test._test );
1109   }
1110 
1111   // Change (-A vs 0) into (A vs 0) by commuting the test.  Disallow in the
1112   // most general case because negating 0x80000000 does nothing.  Needed for
1113   // the CmpF3/SubI/CmpI idiom.
1114   if( cop == Op_CmpI &&
1115       cmp1->Opcode() == Op_SubI &&
1116       cmp2_type == TypeInt::ZERO &&
1117       phase->type( cmp1->in(1) ) == TypeInt::ZERO &&
1118       phase->type( cmp1->in(2) )->higher_equal(TypeInt::SYMINT) ) {
1119     Node *ncmp = phase->transform( new (phase->C, 3) CmpINode(cmp1->in(2),cmp2));
1120     return new (phase->C, 2) BoolNode( ncmp, _test.commute() );
1121   }
1122 
1123   //  The transformation below is not valid for either signed or unsigned
1124   //  comparisons due to wraparound concerns at MAX_VALUE and MIN_VALUE.
1125   //  This transformation can be resurrected when we are able to
1126   //  make inferences about the range of values being subtracted from
1127   //  (or added to) relative to the wraparound point.
1128   //
1129   //    // Remove +/-1's if possible.
1130   //    // "X <= Y-1" becomes "X <  Y"
1131   //    // "X+1 <= Y" becomes "X <  Y"
1132   //    // "X <  Y+1" becomes "X <= Y"
1133   //    // "X-1 <  Y" becomes "X <= Y"
1134   //    // Do not this to compares off of the counted-loop-end.  These guys are
1135   //    // checking the trip counter and they want to use the post-incremented
1136   //    // counter.  If they use the PRE-incremented counter, then the counter has
1137   //    // to be incremented in a private block on a loop backedge.
1138   //    if( du && du->cnt(this) && du->out(this)[0]->Opcode() == Op_CountedLoopEnd )
1139   //      return NULL;
1140   //  #ifndef PRODUCT
1141   //    // Do not do this in a wash GVN pass during verification.
1142   //    // Gets triggered by too many simple optimizations to be bothered with
1143   //    // re-trying it again and again.
1144   //    if( !phase->allow_progress() ) return NULL;
1145   //  #endif
1146   //    // Not valid for unsigned compare because of corner cases in involving zero.
1147   //    // For example, replacing "X-1 <u Y" with "X <=u Y" fails to throw an
1148   //    // exception in case X is 0 (because 0-1 turns into 4billion unsigned but
1149   //    // "0 <=u Y" is always true).
1150   //    if( cmp->Opcode() == Op_CmpU ) return NULL;
1151   //    int cmp2_op = cmp2->Opcode();
1152   //    if( _test._test == BoolTest::le ) {
1153   //      if( cmp1_op == Op_AddI &&
1154   //          phase->type( cmp1->in(2) ) == TypeInt::ONE )
1155   //        return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::lt );
1156   //      else if( cmp2_op == Op_AddI &&
1157   //         phase->type( cmp2->in(2) ) == TypeInt::MINUS_1 )
1158   //        return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::lt );
1159   //    } else if( _test._test == BoolTest::lt ) {
1160   //      if( cmp1_op == Op_AddI &&
1161   //          phase->type( cmp1->in(2) ) == TypeInt::MINUS_1 )
1162   //        return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::le );
1163   //      else if( cmp2_op == Op_AddI &&
1164   //         phase->type( cmp2->in(2) ) == TypeInt::ONE )
1165   //        return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::le );
1166   //    }
1167 
1168   return NULL;
1169 }
1170 
1171 //------------------------------Value------------------------------------------
1172 // Simplify a Bool (convert condition codes to boolean (1 or 0)) node,
1173 // based on local information.   If the input is constant, do it.
1174 const Type *BoolNode::Value( PhaseTransform *phase ) const {
1175   return _test.cc2logical( phase->type( in(1) ) );
1176 }
1177 
1178 //------------------------------dump_spec--------------------------------------
1179 // Dump special per-node info
1180 #ifndef PRODUCT
1181 void BoolNode::dump_spec(outputStream *st) const {
1182   st->print("[");
1183   _test.dump_on(st);
1184   st->print("]");
1185 }
1186 #endif
1187 
1188 //------------------------------is_counted_loop_exit_test--------------------------------------
1189 // Returns true if node is used by a counted loop node.
1190 bool BoolNode::is_counted_loop_exit_test() {
1191   for( DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++ ) {
1192     Node* use = fast_out(i);
1193     if (use->is_CountedLoopEnd()) {
1194       return true;
1195     }
1196   }
1197   return false;
1198 }
1199 
1200 //=============================================================================
1201 //------------------------------NegNode----------------------------------------
1202 Node *NegFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1203   if( in(1)->Opcode() == Op_SubF )
1204     return new (phase->C, 3) SubFNode( in(1)->in(2), in(1)->in(1) );
1205   return NULL;
1206 }
1207 
1208 Node *NegDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1209   if( in(1)->Opcode() == Op_SubD )
1210     return new (phase->C, 3) SubDNode( in(1)->in(2), in(1)->in(1) );
1211   return NULL;
1212 }
1213 
1214 
1215 //=============================================================================
1216 //------------------------------Value------------------------------------------
1217 // Compute sqrt
1218 const Type *SqrtDNode::Value( PhaseTransform *phase ) const {
1219   const Type *t1 = phase->type( in(1) );
1220   if( t1 == Type::TOP ) return Type::TOP;
1221   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1222   double d = t1->getd();
1223   if( d < 0.0 ) return Type::DOUBLE;
1224   return TypeD::make( sqrt( d ) );
1225 }
1226 
1227 //=============================================================================
1228 //------------------------------Value------------------------------------------
1229 // Compute cos
1230 const Type *CosDNode::Value( PhaseTransform *phase ) const {
1231   const Type *t1 = phase->type( in(1) );
1232   if( t1 == Type::TOP ) return Type::TOP;
1233   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1234   double d = t1->getd();
1235   if( d < 0.0 ) return Type::DOUBLE;
1236   return TypeD::make( SharedRuntime::dcos( d ) );
1237 }
1238 
1239 //=============================================================================
1240 //------------------------------Value------------------------------------------
1241 // Compute sin
1242 const Type *SinDNode::Value( PhaseTransform *phase ) const {
1243   const Type *t1 = phase->type( in(1) );
1244   if( t1 == Type::TOP ) return Type::TOP;
1245   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1246   double d = t1->getd();
1247   if( d < 0.0 ) return Type::DOUBLE;
1248   return TypeD::make( SharedRuntime::dsin( d ) );
1249 }
1250 
1251 //=============================================================================
1252 //------------------------------Value------------------------------------------
1253 // Compute tan
1254 const Type *TanDNode::Value( PhaseTransform *phase ) const {
1255   const Type *t1 = phase->type( in(1) );
1256   if( t1 == Type::TOP ) return Type::TOP;
1257   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1258   double d = t1->getd();
1259   if( d < 0.0 ) return Type::DOUBLE;
1260   return TypeD::make( SharedRuntime::dtan( d ) );
1261 }
1262 
1263 //=============================================================================
1264 //------------------------------Value------------------------------------------
1265 // Compute log
1266 const Type *LogDNode::Value( PhaseTransform *phase ) const {
1267   const Type *t1 = phase->type( in(1) );
1268   if( t1 == Type::TOP ) return Type::TOP;
1269   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1270   double d = t1->getd();
1271   if( d < 0.0 ) return Type::DOUBLE;
1272   return TypeD::make( SharedRuntime::dlog( d ) );
1273 }
1274 
1275 //=============================================================================
1276 //------------------------------Value------------------------------------------
1277 // Compute log10
1278 const Type *Log10DNode::Value( PhaseTransform *phase ) const {
1279   const Type *t1 = phase->type( in(1) );
1280   if( t1 == Type::TOP ) return Type::TOP;
1281   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1282   double d = t1->getd();
1283   if( d < 0.0 ) return Type::DOUBLE;
1284   return TypeD::make( SharedRuntime::dlog10( d ) );
1285 }
1286 
1287 //=============================================================================
1288 //------------------------------Value------------------------------------------
1289 // Compute exp
1290 const Type *ExpDNode::Value( PhaseTransform *phase ) const {
1291   const Type *t1 = phase->type( in(1) );
1292   if( t1 == Type::TOP ) return Type::TOP;
1293   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1294   double d = t1->getd();
1295   if( d < 0.0 ) return Type::DOUBLE;
1296   return TypeD::make( SharedRuntime::dexp( d ) );
1297 }
1298 
1299 
1300 //=============================================================================
1301 //------------------------------Value------------------------------------------
1302 // Compute pow
1303 const Type *PowDNode::Value( PhaseTransform *phase ) const {
1304   const Type *t1 = phase->type( in(1) );
1305   if( t1 == Type::TOP ) return Type::TOP;
1306   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1307   const Type *t2 = phase->type( in(2) );
1308   if( t2 == Type::TOP ) return Type::TOP;
1309   if( t2->base() != Type::DoubleCon ) return Type::DOUBLE;
1310   double d1 = t1->getd();
1311   double d2 = t2->getd();
1312   if( d1 < 0.0 ) return Type::DOUBLE;
1313   if( d2 < 0.0 ) return Type::DOUBLE;
1314   return TypeD::make( SharedRuntime::dpow( d1, d2 ) );
1315 }