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