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