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