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