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