1 /*
   2  * Copyright 1997-2007 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       // See if neither subclasses the other, or if the class on top
 637       // is precise.  In either of these cases, the compare must fail.
 638       if (klass0->equals(klass1)   ||   // if types are unequal but klasses are
 639           !klass0->is_java_klass() ||   // types not part of Java language?
 640           !klass1->is_java_klass()) {   // types not part of Java language?
 641         // Do nothing; we know nothing for imprecise types
 642       } else if (klass0->is_subtype_of(klass1)) {
 643         // If klass1's type is PRECISE, then we can fail.
 644         if (xklass1)  return TypeInt::CC_GT;
 645       } else if (klass1->is_subtype_of(klass0)) {
 646         // If klass0's type is PRECISE, then we can fail.
 647         if (xklass0)  return TypeInt::CC_GT;
 648       } else {                  // Neither subtypes the other
 649         return TypeInt::CC_GT;  // ...so always fail
 650       }
 651     }
 652   }
 653 
 654   // Known constants can be compared exactly
 655   // Null can be distinguished from any NotNull pointers
 656   // Unknown inputs makes an unknown result
 657   if( r0->singleton() ) {
 658     intptr_t bits0 = r0->get_con();
 659     if( r1->singleton() )
 660       return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT;
 661     return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC;
 662   } else if( r1->singleton() ) {
 663     intptr_t bits1 = r1->get_con();
 664     return ( r0->_ptr == TypePtr::NotNull && bits1==0 ) ? TypeInt::CC_GT : TypeInt::CC;
 665   } else
 666     return TypeInt::CC;
 667 }
 668 
 669 //------------------------------Ideal------------------------------------------
 670 // Check for the case of comparing an unknown klass loaded from the primary
 671 // super-type array vs a known klass with no subtypes.  This amounts to
 672 // checking to see an unknown klass subtypes a known klass with no subtypes;
 673 // this only happens on an exact match.  We can shorten this test by 1 load.
 674 Node *CmpPNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
 675   // Constant pointer on right?
 676   const TypeKlassPtr* t2 = phase->type(in(2))->isa_klassptr();
 677   if (t2 == NULL || !t2->klass_is_exact())
 678     return NULL;
 679   // Get the constant klass we are comparing to.
 680   ciKlass* superklass = t2->klass();
 681 
 682   // Now check for LoadKlass on left.
 683   Node* ldk1 = in(1);
 684   if (ldk1->Opcode() != Op_LoadKlass)
 685     return NULL;
 686   // Take apart the address of the LoadKlass:
 687   Node* adr1 = ldk1->in(MemNode::Address);
 688   intptr_t con2 = 0;
 689   Node* ldk2 = AddPNode::Ideal_base_and_offset(adr1, phase, con2);
 690   if (ldk2 == NULL)
 691     return NULL;
 692   if (con2 == oopDesc::klass_offset_in_bytes()) {
 693     // We are inspecting an object's concrete class.
 694     // Short-circuit the check if the query is abstract.
 695     if (superklass->is_interface() ||
 696         superklass->is_abstract()) {
 697       // Make it come out always false:
 698       this->set_req(2, phase->makecon(TypePtr::NULL_PTR));
 699       return this;
 700     }
 701   }
 702 
 703   // Check for a LoadKlass from primary supertype array.
 704   // Any nested loadklass from loadklass+con must be from the p.s. array.
 705   if (ldk2->Opcode() != Op_LoadKlass)
 706     return NULL;
 707 
 708   // Verify that we understand the situation
 709   if (con2 != (intptr_t) superklass->super_check_offset())
 710     return NULL;                // Might be element-klass loading from array klass
 711 
 712   // If 'superklass' has no subklasses and is not an interface, then we are
 713   // assured that the only input which will pass the type check is
 714   // 'superklass' itself.
 715   //
 716   // We could be more liberal here, and allow the optimization on interfaces
 717   // which have a single implementor.  This would require us to increase the
 718   // expressiveness of the add_dependency() mechanism.
 719   // %%% Do this after we fix TypeOopPtr:  Deps are expressive enough now.
 720 
 721   // Object arrays must have their base element have no subtypes
 722   while (superklass->is_obj_array_klass()) {
 723     ciType* elem = superklass->as_obj_array_klass()->element_type();
 724     superklass = elem->as_klass();
 725   }
 726   if (superklass->is_instance_klass()) {
 727     ciInstanceKlass* ik = superklass->as_instance_klass();
 728     if (ik->has_subklass() || ik->is_interface())  return NULL;
 729     // Add a dependency if there is a chance that a subclass will be added later.
 730     if (!ik->is_final()) {
 731       phase->C->dependencies()->assert_leaf_type(ik);
 732     }
 733   }
 734 
 735   // Bypass the dependent load, and compare directly
 736   this->set_req(1,ldk2);
 737 
 738   return this;
 739 }
 740 
 741 //=============================================================================
 742 //------------------------------sub--------------------------------------------
 743 // Simplify an CmpN (compare 2 pointers) node, based on local information.
 744 // If both inputs are constants, compare them.
 745 const Type *CmpNNode::sub( const Type *t1, const Type *t2 ) const {
 746   const TypePtr *r0 = t1->is_narrowoop()->make_oopptr(); // Handy access
 747   const TypePtr *r1 = t2->is_narrowoop()->make_oopptr();
 748 
 749   // Undefined inputs makes for an undefined result
 750   if( TypePtr::above_centerline(r0->_ptr) ||
 751       TypePtr::above_centerline(r1->_ptr) )
 752     return Type::TOP;
 753 
 754   if (r0 == r1 && r0->singleton()) {
 755     // Equal pointer constants (klasses, nulls, etc.)
 756     return TypeInt::CC_EQ;
 757   }
 758 
 759   // See if it is 2 unrelated classes.
 760   const TypeOopPtr* p0 = r0->isa_oopptr();
 761   const TypeOopPtr* p1 = r1->isa_oopptr();
 762   if (p0 && p1) {
 763     ciKlass* klass0 = p0->klass();
 764     bool    xklass0 = p0->klass_is_exact();
 765     ciKlass* klass1 = p1->klass();
 766     bool    xklass1 = p1->klass_is_exact();
 767     int kps = (p0->isa_klassptr()?1:0) + (p1->isa_klassptr()?1:0);
 768     if (klass0 && klass1 &&
 769         kps != 1 &&             // both or neither are klass pointers
 770         !klass0->is_interface() && // do not trust interfaces
 771         !klass1->is_interface()) {
 772       // See if neither subclasses the other, or if the class on top
 773       // is precise.  In either of these cases, the compare must fail.
 774       if (klass0->equals(klass1)   ||   // if types are unequal but klasses are
 775           !klass0->is_java_klass() ||   // types not part of Java language?
 776           !klass1->is_java_klass()) {   // types not part of Java language?
 777         // Do nothing; we know nothing for imprecise types
 778       } else if (klass0->is_subtype_of(klass1)) {
 779         // If klass1's type is PRECISE, then we can fail.
 780         if (xklass1)  return TypeInt::CC_GT;
 781       } else if (klass1->is_subtype_of(klass0)) {
 782         // If klass0's type is PRECISE, then we can fail.
 783         if (xklass0)  return TypeInt::CC_GT;
 784       } else {                  // Neither subtypes the other
 785         return TypeInt::CC_GT;  // ...so always fail
 786       }
 787     }
 788   }
 789 
 790   // Known constants can be compared exactly
 791   // Null can be distinguished from any NotNull pointers
 792   // Unknown inputs makes an unknown result
 793   if( r0->singleton() ) {
 794     intptr_t bits0 = r0->get_con();
 795     if( r1->singleton() )
 796       return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT;
 797     return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC;
 798   } else if( r1->singleton() ) {
 799     intptr_t bits1 = r1->get_con();
 800     return ( r0->_ptr == TypePtr::NotNull && bits1==0 ) ? TypeInt::CC_GT : TypeInt::CC;
 801   } else
 802     return TypeInt::CC;
 803 }
 804 
 805 //------------------------------Ideal------------------------------------------
 806 Node *CmpNNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
 807   return NULL;
 808 }
 809 
 810 //=============================================================================
 811 //------------------------------Value------------------------------------------
 812 // Simplify an CmpF (compare 2 floats ) node, based on local information.
 813 // If both inputs are constants, compare them.
 814 const Type *CmpFNode::Value( PhaseTransform *phase ) const {
 815   const Node* in1 = in(1);
 816   const Node* in2 = in(2);
 817   // Either input is TOP ==> the result is TOP
 818   const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
 819   if( t1 == Type::TOP ) return Type::TOP;
 820   const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
 821   if( t2 == Type::TOP ) return Type::TOP;
 822 
 823   // Not constants?  Don't know squat - even if they are the same
 824   // value!  If they are NaN's they compare to LT instead of EQ.
 825   const TypeF *tf1 = t1->isa_float_constant();
 826   const TypeF *tf2 = t2->isa_float_constant();
 827   if( !tf1 || !tf2 ) return TypeInt::CC;
 828 
 829   // This implements the Java bytecode fcmpl, so unordered returns -1.
 830   if( tf1->is_nan() || tf2->is_nan() )
 831     return TypeInt::CC_LT;
 832 
 833   if( tf1->_f < tf2->_f ) return TypeInt::CC_LT;
 834   if( tf1->_f > tf2->_f ) return TypeInt::CC_GT;
 835   assert( tf1->_f == tf2->_f, "do not understand FP behavior" );
 836   return TypeInt::CC_EQ;
 837 }
 838 
 839 
 840 //=============================================================================
 841 //------------------------------Value------------------------------------------
 842 // Simplify an CmpD (compare 2 doubles ) node, based on local information.
 843 // If both inputs are constants, compare them.
 844 const Type *CmpDNode::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 TypeD *td1 = t1->isa_double_constant();
 856   const TypeD *td2 = t2->isa_double_constant();
 857   if( !td1 || !td2 ) return TypeInt::CC;
 858 
 859   // This implements the Java bytecode dcmpl, so unordered returns -1.
 860   if( td1->is_nan() || td2->is_nan() )
 861     return TypeInt::CC_LT;
 862 
 863   if( td1->_d < td2->_d ) return TypeInt::CC_LT;
 864   if( td1->_d > td2->_d ) return TypeInt::CC_GT;
 865   assert( td1->_d == td2->_d, "do not understand FP behavior" );
 866   return TypeInt::CC_EQ;
 867 }
 868 
 869 //------------------------------Ideal------------------------------------------
 870 Node *CmpDNode::Ideal(PhaseGVN *phase, bool can_reshape){
 871   // Check if we can change this to a CmpF and remove a ConvD2F operation.
 872   // Change  (CMPD (F2D (float)) (ConD value))
 873   // To      (CMPF      (float)  (ConF value))
 874   // Valid when 'value' does not lose precision as a float.
 875   // Benefits: eliminates conversion, does not require 24-bit mode
 876 
 877   // NaNs prevent commuting operands.  This transform works regardless of the
 878   // order of ConD and ConvF2D inputs by preserving the original order.
 879   int idx_f2d = 1;              // ConvF2D on left side?
 880   if( in(idx_f2d)->Opcode() != Op_ConvF2D )
 881     idx_f2d = 2;                // No, swap to check for reversed args
 882   int idx_con = 3-idx_f2d;      // Check for the constant on other input
 883 
 884   if( ConvertCmpD2CmpF &&
 885       in(idx_f2d)->Opcode() == Op_ConvF2D &&
 886       in(idx_con)->Opcode() == Op_ConD ) {
 887     const TypeD *t2 = in(idx_con)->bottom_type()->is_double_constant();
 888     double t2_value_as_double = t2->_d;
 889     float  t2_value_as_float  = (float)t2_value_as_double;
 890     if( t2_value_as_double == (double)t2_value_as_float ) {
 891       // Test value can be represented as a float
 892       // Eliminate the conversion to double and create new comparison
 893       Node *new_in1 = in(idx_f2d)->in(1);
 894       Node *new_in2 = phase->makecon( TypeF::make(t2_value_as_float) );
 895       if( idx_f2d != 1 ) {      // Must flip args to match original order
 896         Node *tmp = new_in1;
 897         new_in1 = new_in2;
 898         new_in2 = tmp;
 899       }
 900       CmpFNode *new_cmp = (Opcode() == Op_CmpD3)
 901         ? new (phase->C, 3) CmpF3Node( new_in1, new_in2 )
 902         : new (phase->C, 3) CmpFNode ( new_in1, new_in2 ) ;
 903       return new_cmp;           // Changed to CmpFNode
 904     }
 905     // Testing value required the precision of a double
 906   }
 907   return NULL;                  // No change
 908 }
 909 
 910 
 911 //=============================================================================
 912 //------------------------------cc2logical-------------------------------------
 913 // Convert a condition code type to a logical type
 914 const Type *BoolTest::cc2logical( const Type *CC ) const {
 915   if( CC == Type::TOP ) return Type::TOP;
 916   if( CC->base() != Type::Int ) return TypeInt::BOOL; // Bottom or worse
 917   const TypeInt *ti = CC->is_int();
 918   if( ti->is_con() ) {          // Only 1 kind of condition codes set?
 919     // Match low order 2 bits
 920     int tmp = ((ti->get_con()&3) == (_test&3)) ? 1 : 0;
 921     if( _test & 4 ) tmp = 1-tmp;     // Optionally complement result
 922     return TypeInt::make(tmp);       // Boolean result
 923   }
 924 
 925   if( CC == TypeInt::CC_GE ) {
 926     if( _test == ge ) return TypeInt::ONE;
 927     if( _test == lt ) return TypeInt::ZERO;
 928   }
 929   if( CC == TypeInt::CC_LE ) {
 930     if( _test == le ) return TypeInt::ONE;
 931     if( _test == gt ) return TypeInt::ZERO;
 932   }
 933 
 934   return TypeInt::BOOL;
 935 }
 936 
 937 //------------------------------dump_spec-------------------------------------
 938 // Print special per-node info
 939 #ifndef PRODUCT
 940 void BoolTest::dump_on(outputStream *st) const {
 941   const char *msg[] = {"eq","gt","??","lt","ne","le","??","ge"};
 942   st->print(msg[_test]);
 943 }
 944 #endif
 945 
 946 //=============================================================================
 947 uint BoolNode::hash() const { return (Node::hash() << 3)|(_test._test+1); }
 948 uint BoolNode::size_of() const { return sizeof(BoolNode); }
 949 
 950 //------------------------------operator==-------------------------------------
 951 uint BoolNode::cmp( const Node &n ) const {
 952   const BoolNode *b = (const BoolNode *)&n; // Cast up
 953   return (_test._test == b->_test._test);
 954 }
 955 
 956 //------------------------------clone_cmp--------------------------------------
 957 // Clone a compare/bool tree
 958 static Node *clone_cmp( Node *cmp, Node *cmp1, Node *cmp2, PhaseGVN *gvn, BoolTest::mask test ) {
 959   Node *ncmp = cmp->clone();
 960   ncmp->set_req(1,cmp1);
 961   ncmp->set_req(2,cmp2);
 962   ncmp = gvn->transform( ncmp );
 963   return new (gvn->C, 2) BoolNode( ncmp, test );
 964 }
 965 
 966 //-------------------------------make_predicate--------------------------------
 967 Node* BoolNode::make_predicate(Node* test_value, PhaseGVN* phase) {
 968   if (test_value->is_Con())   return test_value;
 969   if (test_value->is_Bool())  return test_value;
 970   Compile* C = phase->C;
 971   if (test_value->is_CMove() &&
 972       test_value->in(CMoveNode::Condition)->is_Bool()) {
 973     BoolNode*   bol   = test_value->in(CMoveNode::Condition)->as_Bool();
 974     const Type* ftype = phase->type(test_value->in(CMoveNode::IfFalse));
 975     const Type* ttype = phase->type(test_value->in(CMoveNode::IfTrue));
 976     if (ftype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ttype)) {
 977       return bol;
 978     } else if (ttype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ftype)) {
 979       return phase->transform( bol->negate(phase) );
 980     }
 981     // Else fall through.  The CMove gets in the way of the test.
 982     // It should be the case that make_predicate(bol->as_int_value()) == bol.
 983   }
 984   Node* cmp = new (C, 3) CmpINode(test_value, phase->intcon(0));
 985   cmp = phase->transform(cmp);
 986   Node* bol = new (C, 2) BoolNode(cmp, BoolTest::ne);
 987   return phase->transform(bol);
 988 }
 989 
 990 //--------------------------------as_int_value---------------------------------
 991 Node* BoolNode::as_int_value(PhaseGVN* phase) {
 992   // Inverse to make_predicate.  The CMove probably boils down to a Conv2B.
 993   Node* cmov = CMoveNode::make(phase->C, NULL, this,
 994                                phase->intcon(0), phase->intcon(1),
 995                                TypeInt::BOOL);
 996   return phase->transform(cmov);
 997 }
 998 
 999 //----------------------------------negate-------------------------------------
1000 BoolNode* BoolNode::negate(PhaseGVN* phase) {
1001   Compile* C = phase->C;
1002   return new (C, 2) BoolNode(in(1), _test.negate());
1003 }
1004 
1005 
1006 //------------------------------Ideal------------------------------------------
1007 Node *BoolNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1008   // Change "bool tst (cmp con x)" into "bool ~tst (cmp x con)".
1009   // This moves the constant to the right.  Helps value-numbering.
1010   Node *cmp = in(1);
1011   if( !cmp->is_Sub() ) return NULL;
1012   int cop = cmp->Opcode();
1013   if( cop == Op_FastLock || cop == Op_FastUnlock ) return NULL;
1014   Node *cmp1 = cmp->in(1);
1015   Node *cmp2 = cmp->in(2);
1016   if( !cmp1 ) return NULL;
1017 
1018   // Constant on left?
1019   Node *con = cmp1;
1020   uint op2 = cmp2->Opcode();
1021   // Move constants to the right of compare's to canonicalize.
1022   // Do not muck with Opaque1 nodes, as this indicates a loop
1023   // guard that cannot change shape.
1024   if( con->is_Con() && !cmp2->is_Con() && op2 != Op_Opaque1 &&
1025       // Because of NaN's, CmpD and CmpF are not commutative
1026       cop != Op_CmpD && cop != Op_CmpF &&
1027       // Protect against swapping inputs to a compare when it is used by a
1028       // counted loop exit, which requires maintaining the loop-limit as in(2)
1029       !is_counted_loop_exit_test() ) {
1030     // Ok, commute the constant to the right of the cmp node.
1031     // Clone the Node, getting a new Node of the same class
1032     cmp = cmp->clone();
1033     // Swap inputs to the clone
1034     cmp->swap_edges(1, 2);
1035     cmp = phase->transform( cmp );
1036     return new (phase->C, 2) BoolNode( cmp, _test.commute() );
1037   }
1038 
1039   // Change "bool eq/ne (cmp (xor X 1) 0)" into "bool ne/eq (cmp X 0)".
1040   // The XOR-1 is an idiom used to flip the sense of a bool.  We flip the
1041   // test instead.
1042   int cmp1_op = cmp1->Opcode();
1043   const TypeInt* cmp2_type = phase->type(cmp2)->isa_int();
1044   if (cmp2_type == NULL)  return NULL;
1045   Node* j_xor = cmp1;
1046   if( cmp2_type == TypeInt::ZERO &&
1047       cmp1_op == Op_XorI &&
1048       j_xor->in(1) != j_xor &&          // An xor of itself is dead
1049       phase->type( j_xor->in(2) ) == TypeInt::ONE &&
1050       (_test._test == BoolTest::eq ||
1051        _test._test == BoolTest::ne) ) {
1052     Node *ncmp = phase->transform(new (phase->C, 3) CmpINode(j_xor->in(1),cmp2));
1053     return new (phase->C, 2) BoolNode( ncmp, _test.negate() );
1054   }
1055 
1056   // Change "bool eq/ne (cmp (Conv2B X) 0)" into "bool eq/ne (cmp X 0)".
1057   // This is a standard idiom for branching on a boolean value.
1058   Node *c2b = cmp1;
1059   if( cmp2_type == TypeInt::ZERO &&
1060       cmp1_op == Op_Conv2B &&
1061       (_test._test == BoolTest::eq ||
1062        _test._test == BoolTest::ne) ) {
1063     Node *ncmp = phase->transform(phase->type(c2b->in(1))->isa_int()
1064        ? (Node*)new (phase->C, 3) CmpINode(c2b->in(1),cmp2)
1065        : (Node*)new (phase->C, 3) CmpPNode(c2b->in(1),phase->makecon(TypePtr::NULL_PTR))
1066     );
1067     return new (phase->C, 2) BoolNode( ncmp, _test._test );
1068   }
1069 
1070   // Comparing a SubI against a zero is equal to comparing the SubI
1071   // arguments directly.  This only works for eq and ne comparisons
1072   // due to possible integer overflow.
1073   if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1074         (cop == Op_CmpI) &&
1075         (cmp1->Opcode() == Op_SubI) &&
1076         ( cmp2_type == TypeInt::ZERO ) ) {
1077     Node *ncmp = phase->transform( new (phase->C, 3) CmpINode(cmp1->in(1),cmp1->in(2)));
1078     return new (phase->C, 2) BoolNode( ncmp, _test._test );
1079   }
1080 
1081   // Change (-A vs 0) into (A vs 0) by commuting the test.  Disallow in the
1082   // most general case because negating 0x80000000 does nothing.  Needed for
1083   // the CmpF3/SubI/CmpI idiom.
1084   if( cop == Op_CmpI &&
1085       cmp1->Opcode() == Op_SubI &&
1086       cmp2_type == TypeInt::ZERO &&
1087       phase->type( cmp1->in(1) ) == TypeInt::ZERO &&
1088       phase->type( cmp1->in(2) )->higher_equal(TypeInt::SYMINT) ) {
1089     Node *ncmp = phase->transform( new (phase->C, 3) CmpINode(cmp1->in(2),cmp2));
1090     return new (phase->C, 2) BoolNode( ncmp, _test.commute() );
1091   }
1092 
1093   //  The transformation below is not valid for either signed or unsigned
1094   //  comparisons due to wraparound concerns at MAX_VALUE and MIN_VALUE.
1095   //  This transformation can be resurrected when we are able to
1096   //  make inferences about the range of values being subtracted from
1097   //  (or added to) relative to the wraparound point.
1098   //
1099   //    // Remove +/-1's if possible.
1100   //    // "X <= Y-1" becomes "X <  Y"
1101   //    // "X+1 <= Y" becomes "X <  Y"
1102   //    // "X <  Y+1" becomes "X <= Y"
1103   //    // "X-1 <  Y" becomes "X <= Y"
1104   //    // Do not this to compares off of the counted-loop-end.  These guys are
1105   //    // checking the trip counter and they want to use the post-incremented
1106   //    // counter.  If they use the PRE-incremented counter, then the counter has
1107   //    // to be incremented in a private block on a loop backedge.
1108   //    if( du && du->cnt(this) && du->out(this)[0]->Opcode() == Op_CountedLoopEnd )
1109   //      return NULL;
1110   //  #ifndef PRODUCT
1111   //    // Do not do this in a wash GVN pass during verification.
1112   //    // Gets triggered by too many simple optimizations to be bothered with
1113   //    // re-trying it again and again.
1114   //    if( !phase->allow_progress() ) return NULL;
1115   //  #endif
1116   //    // Not valid for unsigned compare because of corner cases in involving zero.
1117   //    // For example, replacing "X-1 <u Y" with "X <=u Y" fails to throw an
1118   //    // exception in case X is 0 (because 0-1 turns into 4billion unsigned but
1119   //    // "0 <=u Y" is always true).
1120   //    if( cmp->Opcode() == Op_CmpU ) return NULL;
1121   //    int cmp2_op = cmp2->Opcode();
1122   //    if( _test._test == BoolTest::le ) {
1123   //      if( cmp1_op == Op_AddI &&
1124   //          phase->type( cmp1->in(2) ) == TypeInt::ONE )
1125   //        return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::lt );
1126   //      else if( cmp2_op == Op_AddI &&
1127   //         phase->type( cmp2->in(2) ) == TypeInt::MINUS_1 )
1128   //        return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::lt );
1129   //    } else if( _test._test == BoolTest::lt ) {
1130   //      if( cmp1_op == Op_AddI &&
1131   //          phase->type( cmp1->in(2) ) == TypeInt::MINUS_1 )
1132   //        return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::le );
1133   //      else if( cmp2_op == Op_AddI &&
1134   //         phase->type( cmp2->in(2) ) == TypeInt::ONE )
1135   //        return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::le );
1136   //    }
1137 
1138   return NULL;
1139 }
1140 
1141 //------------------------------Value------------------------------------------
1142 // Simplify a Bool (convert condition codes to boolean (1 or 0)) node,
1143 // based on local information.   If the input is constant, do it.
1144 const Type *BoolNode::Value( PhaseTransform *phase ) const {
1145   return _test.cc2logical( phase->type( in(1) ) );
1146 }
1147 
1148 //------------------------------dump_spec--------------------------------------
1149 // Dump special per-node info
1150 #ifndef PRODUCT
1151 void BoolNode::dump_spec(outputStream *st) const {
1152   st->print("[");
1153   _test.dump_on(st);
1154   st->print("]");
1155 }
1156 #endif
1157 
1158 //------------------------------is_counted_loop_exit_test--------------------------------------
1159 // Returns true if node is used by a counted loop node.
1160 bool BoolNode::is_counted_loop_exit_test() {
1161   for( DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++ ) {
1162     Node* use = fast_out(i);
1163     if (use->is_CountedLoopEnd()) {
1164       return true;
1165     }
1166   }
1167   return false;
1168 }
1169 
1170 //=============================================================================
1171 //------------------------------NegNode----------------------------------------
1172 Node *NegFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1173   if( in(1)->Opcode() == Op_SubF )
1174     return new (phase->C, 3) SubFNode( in(1)->in(2), in(1)->in(1) );
1175   return NULL;
1176 }
1177 
1178 Node *NegDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1179   if( in(1)->Opcode() == Op_SubD )
1180     return new (phase->C, 3) SubDNode( in(1)->in(2), in(1)->in(1) );
1181   return NULL;
1182 }
1183 
1184 
1185 //=============================================================================
1186 //------------------------------Value------------------------------------------
1187 // Compute sqrt
1188 const Type *SqrtDNode::Value( PhaseTransform *phase ) const {
1189   const Type *t1 = phase->type( in(1) );
1190   if( t1 == Type::TOP ) return Type::TOP;
1191   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1192   double d = t1->getd();
1193   if( d < 0.0 ) return Type::DOUBLE;
1194   return TypeD::make( sqrt( d ) );
1195 }
1196 
1197 //=============================================================================
1198 //------------------------------Value------------------------------------------
1199 // Compute cos
1200 const Type *CosDNode::Value( PhaseTransform *phase ) const {
1201   const Type *t1 = phase->type( in(1) );
1202   if( t1 == Type::TOP ) return Type::TOP;
1203   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1204   double d = t1->getd();
1205   if( d < 0.0 ) return Type::DOUBLE;
1206   return TypeD::make( SharedRuntime::dcos( d ) );
1207 }
1208 
1209 //=============================================================================
1210 //------------------------------Value------------------------------------------
1211 // Compute sin
1212 const Type *SinDNode::Value( PhaseTransform *phase ) const {
1213   const Type *t1 = phase->type( in(1) );
1214   if( t1 == Type::TOP ) return Type::TOP;
1215   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1216   double d = t1->getd();
1217   if( d < 0.0 ) return Type::DOUBLE;
1218   return TypeD::make( SharedRuntime::dsin( d ) );
1219 }
1220 
1221 //=============================================================================
1222 //------------------------------Value------------------------------------------
1223 // Compute tan
1224 const Type *TanDNode::Value( PhaseTransform *phase ) const {
1225   const Type *t1 = phase->type( in(1) );
1226   if( t1 == Type::TOP ) return Type::TOP;
1227   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1228   double d = t1->getd();
1229   if( d < 0.0 ) return Type::DOUBLE;
1230   return TypeD::make( SharedRuntime::dtan( d ) );
1231 }
1232 
1233 //=============================================================================
1234 //------------------------------Value------------------------------------------
1235 // Compute log
1236 const Type *LogDNode::Value( PhaseTransform *phase ) const {
1237   const Type *t1 = phase->type( in(1) );
1238   if( t1 == Type::TOP ) return Type::TOP;
1239   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1240   double d = t1->getd();
1241   if( d < 0.0 ) return Type::DOUBLE;
1242   return TypeD::make( SharedRuntime::dlog( d ) );
1243 }
1244 
1245 //=============================================================================
1246 //------------------------------Value------------------------------------------
1247 // Compute log10
1248 const Type *Log10DNode::Value( PhaseTransform *phase ) const {
1249   const Type *t1 = phase->type( in(1) );
1250   if( t1 == Type::TOP ) return Type::TOP;
1251   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1252   double d = t1->getd();
1253   if( d < 0.0 ) return Type::DOUBLE;
1254   return TypeD::make( SharedRuntime::dlog10( d ) );
1255 }
1256 
1257 //=============================================================================
1258 //------------------------------Value------------------------------------------
1259 // Compute exp
1260 const Type *ExpDNode::Value( PhaseTransform *phase ) const {
1261   const Type *t1 = phase->type( in(1) );
1262   if( t1 == Type::TOP ) return Type::TOP;
1263   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1264   double d = t1->getd();
1265   if( d < 0.0 ) return Type::DOUBLE;
1266   return TypeD::make( SharedRuntime::dexp( d ) );
1267 }
1268 
1269 
1270 //=============================================================================
1271 //------------------------------Value------------------------------------------
1272 // Compute pow
1273 const Type *PowDNode::Value( PhaseTransform *phase ) const {
1274   const Type *t1 = phase->type( in(1) );
1275   if( t1 == Type::TOP ) return Type::TOP;
1276   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1277   const Type *t2 = phase->type( in(2) );
1278   if( t2 == Type::TOP ) return Type::TOP;
1279   if( t2->base() != Type::DoubleCon ) return Type::DOUBLE;
1280   double d1 = t1->getd();
1281   double d2 = t2->getd();
1282   if( d1 < 0.0 ) return Type::DOUBLE;
1283   if( d2 < 0.0 ) return Type::DOUBLE;
1284   return TypeD::make( SharedRuntime::dpow( d1, d2 ) );
1285 }