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