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