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