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