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