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