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