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(PhaseGVN* phase, Node* n) {
 863   // Return the klass node for
 864   //   LoadP(AddP(foo:Klass, #java_mirror))
 865   //   or NULL if not matching.
 866 
 867 #if INCLUDE_ALL_GCS
 868   n = ShenandoahBarrierNode::skip_through_barrier(n);
 869 #endif
 870 
 871   if (n->Opcode() != Op_LoadP) return NULL;
 872   const TypeInstPtr* tp = phase->type(n)->isa_instptr();
 873   if (!tp || tp->klass() != phase->C->env()->Class_klass()) return NULL;
 874 
 875   Node* adr = n->in(MemNode::Address);
 876   intptr_t off = 0;
 877   Node* k = AddPNode::Ideal_base_and_offset(adr, phase, off);
 878   if (k == NULL)  return NULL;
 879   const TypeKlassPtr* tkp = phase->type(k)->isa_klassptr();
 880   if (!tkp || off != in_bytes(Klass::java_mirror_offset())) return NULL;
 881 
 882   // We've found the klass node of a Java mirror load.
 883   return k;
 884 }
 885 
 886 static inline Node* isa_const_java_mirror(PhaseGVN* phase, Node* n) {
 887   // for ConP(Foo.class) return ConP(Foo.klass)
 888   // otherwise return NULL
 889   if (!n->is_Con()) return NULL;
 890 
 891   const TypeInstPtr* tp = phase->type(n)->isa_instptr();
 892   if (!tp) return NULL;
 893 
 894   ciType* mirror_type = tp->java_mirror_type();
 895   // TypeInstPtr::java_mirror_type() returns non-NULL for compile-
 896   // time Class constants only.
 897   if (!mirror_type) return NULL;
 898 
 899   // x.getClass() == int.class can never be true (for all primitive types)
 900   // Return a ConP(NULL) node for this case.
 901   if (mirror_type->is_classless()) {
 902     return phase->makecon(TypePtr::NULL_PTR);
 903   }
 904 
 905   // return the ConP(Foo.klass)
 906   assert(mirror_type->is_klass(), "mirror_type should represent a Klass*");
 907   return phase->makecon(TypeKlassPtr::make(mirror_type->as_klass()));
 908 }
 909 
 910 //------------------------------Ideal------------------------------------------
 911 // Normalize comparisons between Java mirror loads to compare the klass instead.
 912 //
 913 // Also check for the case of comparing an unknown klass loaded from the primary
 914 // super-type array vs a known klass with no subtypes.  This amounts to
 915 // checking to see an unknown klass subtypes a known klass with no subtypes;
 916 // this only happens on an exact match.  We can shorten this test by 1 load.
 917 Node *CmpPNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
 918   if (UseShenandoahGC) {
 919     Node* in1 = in(1);
 920     Node* in2 = in(2);
 921     if (in1->bottom_type() == TypePtr::NULL_PTR) {
 922       in2 = ShenandoahBarrierNode::skip_through_barrier(in2);
 923     }
 924     if (in2->bottom_type() == TypePtr::NULL_PTR) {
 925       in1 = ShenandoahBarrierNode::skip_through_barrier(in1);
 926     }
 927     PhaseIterGVN* igvn = phase->is_IterGVN();
 928     if (in1 != in(1)) {
 929       if (igvn != NULL) {
 930         set_req_X(1, in1, igvn);
 931       } else {
 932         set_req(1, in1);
 933       }
 934       assert(in2 == in(2), "only one change");
 935       return this;
 936     }
 937     if (in2 != in(2)) {
 938       if (igvn != NULL) {
 939         set_req_X(2, in2, igvn);
 940       } else {
 941         set_req(2, in2);
 942       }
 943       return this;
 944     }
 945   }
 946 
 947   // Normalize comparisons between Java mirrors into comparisons of the low-
 948   // level klass, where a dependent load could be shortened.
 949   //
 950   // The new pattern has a nice effect of matching the same pattern used in the
 951   // fast path of instanceof/checkcast/Class.isInstance(), which allows
 952   // redundant exact type check be optimized away by GVN.
 953   // For example, in
 954   //   if (x.getClass() == Foo.class) {
 955   //     Foo foo = (Foo) x;
 956   //     // ... use a ...
 957   //   }
 958   // a CmpPNode could be shared between if_acmpne and checkcast
 959   {
 960     Node* k1 = isa_java_mirror_load(phase, in(1));
 961     Node* k2 = isa_java_mirror_load(phase, in(2));
 962     Node* conk2 = isa_const_java_mirror(phase, in(2));
 963 
 964     if (k1 && (k2 || conk2)) {
 965       Node* lhs = k1;
 966       Node* rhs = (k2 != NULL) ? k2 : conk2;
 967 #if INCLUDE_ALL_GCS
 968       PhaseIterGVN* igvn = phase->is_IterGVN();
 969       if (igvn != NULL) {
 970         set_req_X(1, lhs, igvn);
 971         set_req_X(2, rhs, igvn);
 972       } else
 973 #endif
 974       {
 975         set_req(1, lhs);
 976         set_req(2, rhs);
 977       }
 978       return this;
 979     }
 980   }
 981 
 982   // Constant pointer on right?
 983   const TypeKlassPtr* t2 = phase->type(in(2))->isa_klassptr();
 984   if (t2 == NULL || !t2->klass_is_exact())
 985     return NULL;
 986   // Get the constant klass we are comparing to.
 987   ciKlass* superklass = t2->klass();
 988 
 989   // Now check for LoadKlass on left.
 990   Node* ldk1 = in(1);
 991   if (ldk1->is_DecodeNKlass()) {
 992     ldk1 = ldk1->in(1);
 993     if (ldk1->Opcode() != Op_LoadNKlass )
 994       return NULL;
 995   } else if (ldk1->Opcode() != Op_LoadKlass )
 996     return NULL;
 997   // Take apart the address of the LoadKlass:
 998   Node* adr1 = ldk1->in(MemNode::Address);
 999   intptr_t con2 = 0;
1000   Node* ldk2 = AddPNode::Ideal_base_and_offset(adr1, phase, con2);
1001   if (ldk2 == NULL)
1002     return NULL;
1003   if (con2 == oopDesc::klass_offset_in_bytes()) {
1004     // We are inspecting an object's concrete class.
1005     // Short-circuit the check if the query is abstract.
1006     if (superklass->is_interface() ||
1007         superklass->is_abstract()) {
1008       // Make it come out always false:
1009       this->set_req(2, phase->makecon(TypePtr::NULL_PTR));
1010       return this;
1011     }
1012   }
1013 
1014   // Check for a LoadKlass from primary supertype array.
1015   // Any nested loadklass from loadklass+con must be from the p.s. array.
1016   if (ldk2->is_DecodeNKlass()) {
1017     // Keep ldk2 as DecodeN since it could be used in CmpP below.
1018     if (ldk2->in(1)->Opcode() != Op_LoadNKlass )
1019       return NULL;
1020   } else if (ldk2->Opcode() != Op_LoadKlass)
1021     return NULL;
1022 
1023   // Verify that we understand the situation
1024   if (con2 != (intptr_t) superklass->super_check_offset())
1025     return NULL;                // Might be element-klass loading from array klass
1026 
1027   // If 'superklass' has no subklasses and is not an interface, then we are
1028   // assured that the only input which will pass the type check is
1029   // 'superklass' itself.
1030   //
1031   // We could be more liberal here, and allow the optimization on interfaces
1032   // which have a single implementor.  This would require us to increase the
1033   // expressiveness of the add_dependency() mechanism.
1034   // %%% Do this after we fix TypeOopPtr:  Deps are expressive enough now.
1035 
1036   // Object arrays must have their base element have no subtypes
1037   while (superklass->is_obj_array_klass()) {
1038     ciType* elem = superklass->as_obj_array_klass()->element_type();
1039     superklass = elem->as_klass();
1040   }
1041   if (superklass->is_instance_klass()) {
1042     ciInstanceKlass* ik = superklass->as_instance_klass();
1043     if (ik->has_subklass() || ik->is_interface())  return NULL;
1044     // Add a dependency if there is a chance that a subclass will be added later.
1045     if (!ik->is_final()) {
1046       phase->C->dependencies()->assert_leaf_type(ik);
1047     }
1048   }
1049 
1050   // Bypass the dependent load, and compare directly
1051   this->set_req(1,ldk2);
1052 
1053   return this;
1054 }
1055 
1056 //=============================================================================
1057 //------------------------------sub--------------------------------------------
1058 // Simplify an CmpN (compare 2 pointers) node, based on local information.
1059 // If both inputs are constants, compare them.
1060 const Type *CmpNNode::sub( const Type *t1, const Type *t2 ) const {
1061   const TypePtr *r0 = t1->make_ptr(); // Handy access
1062   const TypePtr *r1 = t2->make_ptr();
1063 
1064   // Undefined inputs makes for an undefined result
1065   if ((r0 == NULL) || (r1 == NULL) ||
1066       TypePtr::above_centerline(r0->_ptr) ||
1067       TypePtr::above_centerline(r1->_ptr)) {
1068     return Type::TOP;
1069   }
1070   if (r0 == r1 && r0->singleton()) {
1071     // Equal pointer constants (klasses, nulls, etc.)
1072     return TypeInt::CC_EQ;
1073   }
1074 
1075   // See if it is 2 unrelated classes.
1076   const TypeOopPtr* p0 = r0->isa_oopptr();
1077   const TypeOopPtr* p1 = r1->isa_oopptr();
1078   if (p0 && p1) {
1079     ciKlass* klass0 = p0->klass();
1080     bool    xklass0 = p0->klass_is_exact();
1081     ciKlass* klass1 = p1->klass();
1082     bool    xklass1 = p1->klass_is_exact();
1083     int kps = (p0->isa_klassptr()?1:0) + (p1->isa_klassptr()?1:0);
1084     if (klass0 && klass1 &&
1085         kps != 1 &&             // both or neither are klass pointers
1086         !klass0->is_interface() && // do not trust interfaces
1087         !klass1->is_interface()) {
1088       bool unrelated_classes = false;
1089       // See if neither subclasses the other, or if the class on top
1090       // is precise.  In either of these cases, the compare is known
1091       // to fail if at least one of the pointers is provably not null.
1092       if (klass0->equals(klass1)) { // if types are unequal but klasses are equal
1093         // Do nothing; we know nothing for imprecise types
1094       } else if (klass0->is_subtype_of(klass1)) {
1095         // If klass1's type is PRECISE, then classes are unrelated.
1096         unrelated_classes = xklass1;
1097       } else if (klass1->is_subtype_of(klass0)) {
1098         // If klass0's type is PRECISE, then classes are unrelated.
1099         unrelated_classes = xklass0;
1100       } else {                  // Neither subtypes the other
1101         unrelated_classes = true;
1102       }
1103       if (unrelated_classes) {
1104         // The oops classes are known to be unrelated. If the joined PTRs of
1105         // two oops is not Null and not Bottom, then we are sure that one
1106         // of the two oops is non-null, and the comparison will always fail.
1107         TypePtr::PTR jp = r0->join_ptr(r1->_ptr);
1108         if (jp != TypePtr::Null && jp != TypePtr::BotPTR) {
1109           return TypeInt::CC_GT;
1110         }
1111       }
1112     }
1113   }
1114 
1115   // Known constants can be compared exactly
1116   // Null can be distinguished from any NotNull pointers
1117   // Unknown inputs makes an unknown result
1118   if( r0->singleton() ) {
1119     intptr_t bits0 = r0->get_con();
1120     if( r1->singleton() )
1121       return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT;
1122     return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC;
1123   } else if( r1->singleton() ) {
1124     intptr_t bits1 = r1->get_con();
1125     return ( r0->_ptr == TypePtr::NotNull && bits1==0 ) ? TypeInt::CC_GT : TypeInt::CC;
1126   } else
1127     return TypeInt::CC;
1128 }
1129 
1130 //------------------------------Ideal------------------------------------------
1131 Node *CmpNNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
1132   return NULL;
1133 }
1134 
1135 //=============================================================================
1136 //------------------------------Value------------------------------------------
1137 // Simplify an CmpF (compare 2 floats ) node, based on local information.
1138 // If both inputs are constants, compare them.
1139 const Type *CmpFNode::Value( PhaseTransform *phase ) const {
1140   const Node* in1 = in(1);
1141   const Node* in2 = in(2);
1142   // Either input is TOP ==> the result is TOP
1143   const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
1144   if( t1 == Type::TOP ) return Type::TOP;
1145   const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
1146   if( t2 == Type::TOP ) return Type::TOP;
1147 
1148   // Not constants?  Don't know squat - even if they are the same
1149   // value!  If they are NaN's they compare to LT instead of EQ.
1150   const TypeF *tf1 = t1->isa_float_constant();
1151   const TypeF *tf2 = t2->isa_float_constant();
1152   if( !tf1 || !tf2 ) return TypeInt::CC;
1153 
1154   // This implements the Java bytecode fcmpl, so unordered returns -1.
1155   if( tf1->is_nan() || tf2->is_nan() )
1156     return TypeInt::CC_LT;
1157 
1158   if( tf1->_f < tf2->_f ) return TypeInt::CC_LT;
1159   if( tf1->_f > tf2->_f ) return TypeInt::CC_GT;
1160   assert( tf1->_f == tf2->_f, "do not understand FP behavior" );
1161   return TypeInt::CC_EQ;
1162 }
1163 
1164 
1165 //=============================================================================
1166 //------------------------------Value------------------------------------------
1167 // Simplify an CmpD (compare 2 doubles ) node, based on local information.
1168 // If both inputs are constants, compare them.
1169 const Type *CmpDNode::Value( PhaseTransform *phase ) const {
1170   const Node* in1 = in(1);
1171   const Node* in2 = in(2);
1172   // Either input is TOP ==> the result is TOP
1173   const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
1174   if( t1 == Type::TOP ) return Type::TOP;
1175   const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
1176   if( t2 == Type::TOP ) return Type::TOP;
1177 
1178   // Not constants?  Don't know squat - even if they are the same
1179   // value!  If they are NaN's they compare to LT instead of EQ.
1180   const TypeD *td1 = t1->isa_double_constant();
1181   const TypeD *td2 = t2->isa_double_constant();
1182   if( !td1 || !td2 ) return TypeInt::CC;
1183 
1184   // This implements the Java bytecode dcmpl, so unordered returns -1.
1185   if( td1->is_nan() || td2->is_nan() )
1186     return TypeInt::CC_LT;
1187 
1188   if( td1->_d < td2->_d ) return TypeInt::CC_LT;
1189   if( td1->_d > td2->_d ) return TypeInt::CC_GT;
1190   assert( td1->_d == td2->_d, "do not understand FP behavior" );
1191   return TypeInt::CC_EQ;
1192 }
1193 
1194 //------------------------------Ideal------------------------------------------
1195 Node *CmpDNode::Ideal(PhaseGVN *phase, bool can_reshape){
1196   // Check if we can change this to a CmpF and remove a ConvD2F operation.
1197   // Change  (CMPD (F2D (float)) (ConD value))
1198   // To      (CMPF      (float)  (ConF value))
1199   // Valid when 'value' does not lose precision as a float.
1200   // Benefits: eliminates conversion, does not require 24-bit mode
1201 
1202   // NaNs prevent commuting operands.  This transform works regardless of the
1203   // order of ConD and ConvF2D inputs by preserving the original order.
1204   int idx_f2d = 1;              // ConvF2D on left side?
1205   if( in(idx_f2d)->Opcode() != Op_ConvF2D )
1206     idx_f2d = 2;                // No, swap to check for reversed args
1207   int idx_con = 3-idx_f2d;      // Check for the constant on other input
1208 
1209   if( ConvertCmpD2CmpF &&
1210       in(idx_f2d)->Opcode() == Op_ConvF2D &&
1211       in(idx_con)->Opcode() == Op_ConD ) {
1212     const TypeD *t2 = in(idx_con)->bottom_type()->is_double_constant();
1213     double t2_value_as_double = t2->_d;
1214     float  t2_value_as_float  = (float)t2_value_as_double;
1215     if( t2_value_as_double == (double)t2_value_as_float ) {
1216       // Test value can be represented as a float
1217       // Eliminate the conversion to double and create new comparison
1218       Node *new_in1 = in(idx_f2d)->in(1);
1219       Node *new_in2 = phase->makecon( TypeF::make(t2_value_as_float) );
1220       if( idx_f2d != 1 ) {      // Must flip args to match original order
1221         Node *tmp = new_in1;
1222         new_in1 = new_in2;
1223         new_in2 = tmp;
1224       }
1225       CmpFNode *new_cmp = (Opcode() == Op_CmpD3)
1226         ? new (phase->C) CmpF3Node( new_in1, new_in2 )
1227         : new (phase->C) CmpFNode ( new_in1, new_in2 ) ;
1228       return new_cmp;           // Changed to CmpFNode
1229     }
1230     // Testing value required the precision of a double
1231   }
1232   return NULL;                  // No change
1233 }
1234 
1235 
1236 //=============================================================================
1237 //------------------------------cc2logical-------------------------------------
1238 // Convert a condition code type to a logical type
1239 const Type *BoolTest::cc2logical( const Type *CC ) const {
1240   if( CC == Type::TOP ) return Type::TOP;
1241   if( CC->base() != Type::Int ) return TypeInt::BOOL; // Bottom or worse
1242   const TypeInt *ti = CC->is_int();
1243   if( ti->is_con() ) {          // Only 1 kind of condition codes set?
1244     // Match low order 2 bits
1245     int tmp = ((ti->get_con()&3) == (_test&3)) ? 1 : 0;
1246     if( _test & 4 ) tmp = 1-tmp;     // Optionally complement result
1247     return TypeInt::make(tmp);       // Boolean result
1248   }
1249 
1250   if( CC == TypeInt::CC_GE ) {
1251     if( _test == ge ) return TypeInt::ONE;
1252     if( _test == lt ) return TypeInt::ZERO;
1253   }
1254   if( CC == TypeInt::CC_LE ) {
1255     if( _test == le ) return TypeInt::ONE;
1256     if( _test == gt ) return TypeInt::ZERO;
1257   }
1258 
1259   return TypeInt::BOOL;
1260 }
1261 
1262 //------------------------------dump_spec-------------------------------------
1263 // Print special per-node info
1264 void BoolTest::dump_on(outputStream *st) const {
1265   const char *msg[] = {"eq","gt","of","lt","ne","le","nof","ge"};
1266   st->print("%s", msg[_test]);
1267 }
1268 
1269 //=============================================================================
1270 uint BoolNode::hash() const { return (Node::hash() << 3)|(_test._test+1); }
1271 uint BoolNode::size_of() const { return sizeof(BoolNode); }
1272 
1273 //------------------------------operator==-------------------------------------
1274 uint BoolNode::cmp( const Node &n ) const {
1275   const BoolNode *b = (const BoolNode *)&n; // Cast up
1276   return (_test._test == b->_test._test);
1277 }
1278 
1279 //-------------------------------make_predicate--------------------------------
1280 Node* BoolNode::make_predicate(Node* test_value, PhaseGVN* phase) {
1281   if (test_value->is_Con())   return test_value;
1282   if (test_value->is_Bool())  return test_value;
1283   Compile* C = phase->C;
1284   if (test_value->is_CMove() &&
1285       test_value->in(CMoveNode::Condition)->is_Bool()) {
1286     BoolNode*   bol   = test_value->in(CMoveNode::Condition)->as_Bool();
1287     const Type* ftype = phase->type(test_value->in(CMoveNode::IfFalse));
1288     const Type* ttype = phase->type(test_value->in(CMoveNode::IfTrue));
1289     if (ftype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ttype)) {
1290       return bol;
1291     } else if (ttype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ftype)) {
1292       return phase->transform( bol->negate(phase) );
1293     }
1294     // Else fall through.  The CMove gets in the way of the test.
1295     // It should be the case that make_predicate(bol->as_int_value()) == bol.
1296   }
1297   Node* cmp = new (C) CmpINode(test_value, phase->intcon(0));
1298   cmp = phase->transform(cmp);
1299   Node* bol = new (C) BoolNode(cmp, BoolTest::ne);
1300   return phase->transform(bol);
1301 }
1302 
1303 //--------------------------------as_int_value---------------------------------
1304 Node* BoolNode::as_int_value(PhaseGVN* phase) {
1305   // Inverse to make_predicate.  The CMove probably boils down to a Conv2B.
1306   Node* cmov = CMoveNode::make(phase->C, NULL, this,
1307                                phase->intcon(0), phase->intcon(1),
1308                                TypeInt::BOOL);
1309   return phase->transform(cmov);
1310 }
1311 
1312 //----------------------------------negate-------------------------------------
1313 BoolNode* BoolNode::negate(PhaseGVN* phase) {
1314   Compile* C = phase->C;
1315   return new (C) BoolNode(in(1), _test.negate());
1316 }
1317 
1318 
1319 //------------------------------Ideal------------------------------------------
1320 Node *BoolNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1321   // Change "bool tst (cmp con x)" into "bool ~tst (cmp x con)".
1322   // This moves the constant to the right.  Helps value-numbering.
1323   Node *cmp = in(1);
1324   if( !cmp->is_Sub() ) return NULL;
1325   int cop = cmp->Opcode();
1326   if( cop == Op_FastLock || cop == Op_FastUnlock) return NULL;
1327   Node *cmp1 = cmp->in(1);
1328   Node *cmp2 = cmp->in(2);
1329   if( !cmp1 ) return NULL;
1330 
1331   if (_test._test == BoolTest::overflow || _test._test == BoolTest::no_overflow) {
1332     return NULL;
1333   }
1334 
1335   // Constant on left?
1336   Node *con = cmp1;
1337   uint op2 = cmp2->Opcode();
1338   // Move constants to the right of compare's to canonicalize.
1339   // Do not muck with Opaque1 nodes, as this indicates a loop
1340   // guard that cannot change shape.
1341   if( con->is_Con() && !cmp2->is_Con() && op2 != Op_Opaque1 &&
1342       // Because of NaN's, CmpD and CmpF are not commutative
1343       cop != Op_CmpD && cop != Op_CmpF &&
1344       // Protect against swapping inputs to a compare when it is used by a
1345       // counted loop exit, which requires maintaining the loop-limit as in(2)
1346       !is_counted_loop_exit_test() ) {
1347     // Ok, commute the constant to the right of the cmp node.
1348     // Clone the Node, getting a new Node of the same class
1349     cmp = cmp->clone();
1350     // Swap inputs to the clone
1351     cmp->swap_edges(1, 2);
1352     cmp = phase->transform( cmp );
1353     return new (phase->C) BoolNode( cmp, _test.commute() );
1354   }
1355 
1356   // Change "bool eq/ne (cmp (xor X 1) 0)" into "bool ne/eq (cmp X 0)".
1357   // The XOR-1 is an idiom used to flip the sense of a bool.  We flip the
1358   // test instead.
1359   int cmp1_op = cmp1->Opcode();
1360   const TypeInt* cmp2_type = phase->type(cmp2)->isa_int();
1361   if (cmp2_type == NULL)  return NULL;
1362   Node* j_xor = cmp1;
1363   if( cmp2_type == TypeInt::ZERO &&
1364       cmp1_op == Op_XorI &&
1365       j_xor->in(1) != j_xor &&          // An xor of itself is dead
1366       phase->type( j_xor->in(1) ) == TypeInt::BOOL &&
1367       phase->type( j_xor->in(2) ) == TypeInt::ONE &&
1368       (_test._test == BoolTest::eq ||
1369        _test._test == BoolTest::ne) ) {
1370     Node *ncmp = phase->transform(new (phase->C) CmpINode(j_xor->in(1),cmp2));
1371     return new (phase->C) BoolNode( ncmp, _test.negate() );
1372   }
1373 
1374   // Change "bool eq/ne (cmp (Conv2B X) 0)" into "bool eq/ne (cmp X 0)".
1375   // This is a standard idiom for branching on a boolean value.
1376   Node *c2b = cmp1;
1377   if( cmp2_type == TypeInt::ZERO &&
1378       cmp1_op == Op_Conv2B &&
1379       (_test._test == BoolTest::eq ||
1380        _test._test == BoolTest::ne) ) {
1381     Node *ncmp = phase->transform(phase->type(c2b->in(1))->isa_int()
1382        ? (Node*)new (phase->C) CmpINode(c2b->in(1),cmp2)
1383        : (Node*)new (phase->C) CmpPNode(c2b->in(1),phase->makecon(TypePtr::NULL_PTR))
1384     );
1385     return new (phase->C) BoolNode( ncmp, _test._test );
1386   }
1387 
1388   // Comparing a SubI against a zero is equal to comparing the SubI
1389   // arguments directly.  This only works for eq and ne comparisons
1390   // due to possible integer overflow.
1391   if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1392         (cop == Op_CmpI) &&
1393         (cmp1->Opcode() == Op_SubI) &&
1394         ( cmp2_type == TypeInt::ZERO ) ) {
1395     Node *ncmp = phase->transform( new (phase->C) CmpINode(cmp1->in(1),cmp1->in(2)));
1396     return new (phase->C) BoolNode( ncmp, _test._test );
1397   }
1398 
1399   // Change (-A vs 0) into (A vs 0) by commuting the test.  Disallow in the
1400   // most general case because negating 0x80000000 does nothing.  Needed for
1401   // the CmpF3/SubI/CmpI idiom.
1402   if( cop == Op_CmpI &&
1403       cmp1->Opcode() == Op_SubI &&
1404       cmp2_type == TypeInt::ZERO &&
1405       phase->type( cmp1->in(1) ) == TypeInt::ZERO &&
1406       phase->type( cmp1->in(2) )->higher_equal(TypeInt::SYMINT) ) {
1407     Node *ncmp = phase->transform( new (phase->C) CmpINode(cmp1->in(2),cmp2));
1408     return new (phase->C) BoolNode( ncmp, _test.commute() );
1409   }
1410 
1411   //  The transformation below is not valid for either signed or unsigned
1412   //  comparisons due to wraparound concerns at MAX_VALUE and MIN_VALUE.
1413   //  This transformation can be resurrected when we are able to
1414   //  make inferences about the range of values being subtracted from
1415   //  (or added to) relative to the wraparound point.
1416   //
1417   //    // Remove +/-1's if possible.
1418   //    // "X <= Y-1" becomes "X <  Y"
1419   //    // "X+1 <= Y" becomes "X <  Y"
1420   //    // "X <  Y+1" becomes "X <= Y"
1421   //    // "X-1 <  Y" becomes "X <= Y"
1422   //    // Do not this to compares off of the counted-loop-end.  These guys are
1423   //    // checking the trip counter and they want to use the post-incremented
1424   //    // counter.  If they use the PRE-incremented counter, then the counter has
1425   //    // to be incremented in a private block on a loop backedge.
1426   //    if( du && du->cnt(this) && du->out(this)[0]->Opcode() == Op_CountedLoopEnd )
1427   //      return NULL;
1428   //  #ifndef PRODUCT
1429   //    // Do not do this in a wash GVN pass during verification.
1430   //    // Gets triggered by too many simple optimizations to be bothered with
1431   //    // re-trying it again and again.
1432   //    if( !phase->allow_progress() ) return NULL;
1433   //  #endif
1434   //    // Not valid for unsigned compare because of corner cases in involving zero.
1435   //    // For example, replacing "X-1 <u Y" with "X <=u Y" fails to throw an
1436   //    // exception in case X is 0 (because 0-1 turns into 4billion unsigned but
1437   //    // "0 <=u Y" is always true).
1438   //    if( cmp->Opcode() == Op_CmpU ) return NULL;
1439   //    int cmp2_op = cmp2->Opcode();
1440   //    if( _test._test == BoolTest::le ) {
1441   //      if( cmp1_op == Op_AddI &&
1442   //          phase->type( cmp1->in(2) ) == TypeInt::ONE )
1443   //        return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::lt );
1444   //      else if( cmp2_op == Op_AddI &&
1445   //         phase->type( cmp2->in(2) ) == TypeInt::MINUS_1 )
1446   //        return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::lt );
1447   //    } else if( _test._test == BoolTest::lt ) {
1448   //      if( cmp1_op == Op_AddI &&
1449   //          phase->type( cmp1->in(2) ) == TypeInt::MINUS_1 )
1450   //        return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::le );
1451   //      else if( cmp2_op == Op_AddI &&
1452   //         phase->type( cmp2->in(2) ) == TypeInt::ONE )
1453   //        return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::le );
1454   //    }
1455 
1456   return NULL;
1457 }
1458 
1459 //------------------------------Value------------------------------------------
1460 // Simplify a Bool (convert condition codes to boolean (1 or 0)) node,
1461 // based on local information.   If the input is constant, do it.
1462 const Type *BoolNode::Value( PhaseTransform *phase ) const {
1463   return _test.cc2logical( phase->type( in(1) ) );
1464 }
1465 
1466 //------------------------------dump_spec--------------------------------------
1467 // Dump special per-node info
1468 #ifndef PRODUCT
1469 void BoolNode::dump_spec(outputStream *st) const {
1470   st->print("[");
1471   _test.dump_on(st);
1472   st->print("]");
1473 }
1474 #endif
1475 
1476 //------------------------------is_counted_loop_exit_test--------------------------------------
1477 // Returns true if node is used by a counted loop node.
1478 bool BoolNode::is_counted_loop_exit_test() {
1479   for( DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++ ) {
1480     Node* use = fast_out(i);
1481     if (use->is_CountedLoopEnd()) {
1482       return true;
1483     }
1484   }
1485   return false;
1486 }
1487 
1488 //=============================================================================
1489 //------------------------------Value------------------------------------------
1490 // Compute sqrt
1491 const Type *SqrtDNode::Value( PhaseTransform *phase ) const {
1492   const Type *t1 = phase->type( in(1) );
1493   if( t1 == Type::TOP ) return Type::TOP;
1494   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1495   double d = t1->getd();
1496   if( d < 0.0 ) return Type::DOUBLE;
1497   return TypeD::make( sqrt( d ) );
1498 }
1499 
1500 //=============================================================================
1501 //------------------------------Value------------------------------------------
1502 // Compute cos
1503 const Type *CosDNode::Value( PhaseTransform *phase ) const {
1504   const Type *t1 = phase->type( in(1) );
1505   if( t1 == Type::TOP ) return Type::TOP;
1506   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1507   double d = t1->getd();
1508   return TypeD::make( StubRoutines::intrinsic_cos( d ) );
1509 }
1510 
1511 //=============================================================================
1512 //------------------------------Value------------------------------------------
1513 // Compute sin
1514 const Type *SinDNode::Value( PhaseTransform *phase ) const {
1515   const Type *t1 = phase->type( in(1) );
1516   if( t1 == Type::TOP ) return Type::TOP;
1517   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1518   double d = t1->getd();
1519   return TypeD::make( StubRoutines::intrinsic_sin( d ) );
1520 }
1521 
1522 //=============================================================================
1523 //------------------------------Value------------------------------------------
1524 // Compute tan
1525 const Type *TanDNode::Value( PhaseTransform *phase ) const {
1526   const Type *t1 = phase->type( in(1) );
1527   if( t1 == Type::TOP ) return Type::TOP;
1528   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1529   double d = t1->getd();
1530   return TypeD::make( StubRoutines::intrinsic_tan( d ) );
1531 }
1532 
1533 //=============================================================================
1534 //------------------------------Value------------------------------------------
1535 // Compute log
1536 const Type *LogDNode::Value( PhaseTransform *phase ) const {
1537   const Type *t1 = phase->type( in(1) );
1538   if( t1 == Type::TOP ) return Type::TOP;
1539   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1540   double d = t1->getd();
1541   return TypeD::make( StubRoutines::intrinsic_log( d ) );
1542 }
1543 
1544 //=============================================================================
1545 //------------------------------Value------------------------------------------
1546 // Compute log10
1547 const Type *Log10DNode::Value( PhaseTransform *phase ) const {
1548   const Type *t1 = phase->type( in(1) );
1549   if( t1 == Type::TOP ) return Type::TOP;
1550   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1551   double d = t1->getd();
1552   return TypeD::make( StubRoutines::intrinsic_log10( d ) );
1553 }
1554 
1555 //=============================================================================
1556 //------------------------------Value------------------------------------------
1557 // Compute exp
1558 const Type *ExpDNode::Value( PhaseTransform *phase ) const {
1559   const Type *t1 = phase->type( in(1) );
1560   if( t1 == Type::TOP ) return Type::TOP;
1561   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1562   double d = t1->getd();
1563   return TypeD::make( StubRoutines::intrinsic_exp( d ) );
1564 }
1565 
1566 
1567 //=============================================================================
1568 //------------------------------Value------------------------------------------
1569 // Compute pow
1570 const Type *PowDNode::Value( PhaseTransform *phase ) const {
1571   const Type *t1 = phase->type( in(1) );
1572   if( t1 == Type::TOP ) return Type::TOP;
1573   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1574   const Type *t2 = phase->type( in(2) );
1575   if( t2 == Type::TOP ) return Type::TOP;
1576   if( t2->base() != Type::DoubleCon ) return Type::DOUBLE;
1577   double d1 = t1->getd();
1578   double d2 = t2->getd();
1579   return TypeD::make( StubRoutines::intrinsic_pow( d1, d2 ) );
1580 }