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