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