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