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