1 /*
   2  * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "memory/allocation.inline.hpp"
  26 #include "opto/addnode.hpp"
  27 #include "opto/connode.hpp"
  28 #include "opto/convertnode.hpp"
  29 #include "opto/memnode.hpp"
  30 #include "opto/mulnode.hpp"
  31 #include "opto/phaseX.hpp"
  32 #include "opto/subnode.hpp"
  33 #include "utilities/powerOfTwo.hpp"
  34 
  35 // Portions of code courtesy of Clifford Click
  36 
  37 
  38 //=============================================================================
  39 //------------------------------hash-------------------------------------------
  40 // Hash function over MulNodes.  Needs to be commutative; i.e., I swap
  41 // (commute) inputs to MulNodes willy-nilly so the hash function must return
  42 // the same value in the presence of edge swapping.
  43 uint MulNode::hash() const {
  44   return (uintptr_t)in(1) + (uintptr_t)in(2) + Opcode();
  45 }
  46 
  47 //------------------------------Identity---------------------------------------
  48 // Multiplying a one preserves the other argument
  49 Node* MulNode::Identity(PhaseGVN* phase) {
  50   const Type *one = mul_id();  // The multiplicative identity
  51   if( phase->type( in(1) )->higher_equal( one ) ) return in(2);
  52   if( phase->type( in(2) )->higher_equal( one ) ) return in(1);
  53 
  54   return this;
  55 }
  56 
  57 //------------------------------Ideal------------------------------------------
  58 // We also canonicalize the Node, moving constants to the right input,
  59 // and flatten expressions (so that 1+x+2 becomes x+3).
  60 Node *MulNode::Ideal(PhaseGVN *phase, bool can_reshape) {
  61   Node* in1 = in(1);
  62   Node* in2 = in(2);
  63   Node* progress = nullptr;        // Progress flag
  64 
  65   // This code is used by And nodes too, but some conversions are
  66   // only valid for the actual Mul nodes.
  67   uint op = Opcode();
  68   bool real_mul = (op == Op_MulI) || (op == Op_MulL) ||
  69                   (op == Op_MulF) || (op == Op_MulD) ||
  70                   (op == Op_MulHF);
  71 
  72   // Convert "(-a)*(-b)" into "a*b".
  73   if (real_mul && in1->is_Sub() && in2->is_Sub()) {
  74     if (phase->type(in1->in(1))->is_zero_type() &&
  75         phase->type(in2->in(1))->is_zero_type()) {
  76       set_req_X(1, in1->in(2), phase);
  77       set_req_X(2, in2->in(2), phase);
  78       in1 = in(1);
  79       in2 = in(2);
  80       progress = this;
  81     }
  82   }
  83 
  84   // convert "max(a,b) * min(a,b)" into "a*b".
  85   if ((in(1)->Opcode() == max_opcode() && in(2)->Opcode() == min_opcode())
  86       || (in(1)->Opcode() == min_opcode() && in(2)->Opcode() == max_opcode())) {
  87     Node *in11 = in(1)->in(1);
  88     Node *in12 = in(1)->in(2);
  89 
  90     Node *in21 = in(2)->in(1);
  91     Node *in22 = in(2)->in(2);
  92 
  93     if ((in11 == in21 && in12 == in22) ||
  94         (in11 == in22 && in12 == in21)) {
  95       set_req_X(1, in11, phase);
  96       set_req_X(2, in12, phase);
  97       in1 = in(1);
  98       in2 = in(2);
  99       progress = this;
 100     }
 101   }
 102 
 103   const Type* t1 = phase->type(in1);
 104   const Type* t2 = phase->type(in2);
 105 
 106   // We are OK if right is a constant, or right is a load and
 107   // left is a non-constant.
 108   if( !(t2->singleton() ||
 109         (in(2)->is_Load() && !(t1->singleton() || in(1)->is_Load())) ) ) {
 110     if( t1->singleton() ||       // Left input is a constant?
 111         // Otherwise, sort inputs (commutativity) to help value numbering.
 112         (in(1)->_idx > in(2)->_idx) ) {
 113       swap_edges(1, 2);
 114       const Type *t = t1;
 115       t1 = t2;
 116       t2 = t;
 117       progress = this;            // Made progress
 118     }
 119   }
 120 
 121   // If the right input is a constant, and the left input is a product of a
 122   // constant, flatten the expression tree.
 123   if( t2->singleton() &&        // Right input is a constant?
 124       op != Op_MulF &&          // Float & double cannot reassociate
 125       op != Op_MulD &&
 126       op != Op_MulHF) {
 127     if( t2 == Type::TOP ) return nullptr;
 128     Node *mul1 = in(1);
 129 #ifdef ASSERT
 130     // Check for dead loop
 131     int op1 = mul1->Opcode();
 132     if ((mul1 == this) || (in(2) == this) ||
 133         ((op1 == mul_opcode() || op1 == add_opcode()) &&
 134          ((mul1->in(1) == this) || (mul1->in(2) == this) ||
 135           (mul1->in(1) == mul1) || (mul1->in(2) == mul1)))) {
 136       assert(false, "dead loop in MulNode::Ideal");
 137     }
 138 #endif
 139 
 140     if( mul1->Opcode() == mul_opcode() ) {  // Left input is a multiply?
 141       // Mul of a constant?
 142       const Type *t12 = phase->type( mul1->in(2) );
 143       if( t12->singleton() && t12 != Type::TOP) { // Left input is an add of a constant?
 144         // Compute new constant; check for overflow
 145         const Type *tcon01 = ((MulNode*)mul1)->mul_ring(t2,t12);
 146         if( tcon01->singleton() ) {
 147           // The Mul of the flattened expression
 148           set_req_X(1, mul1->in(1), phase);
 149           set_req_X(2, phase->makecon(tcon01), phase);
 150           t2 = tcon01;
 151           progress = this;      // Made progress
 152         }
 153       }
 154     }
 155     // If the right input is a constant, and the left input is an add of a
 156     // constant, flatten the tree: (X+con1)*con0 ==> X*con0 + con1*con0
 157     const Node *add1 = in(1);
 158     if( add1->Opcode() == add_opcode() ) {      // Left input is an add?
 159       // Add of a constant?
 160       const Type *t12 = phase->type( add1->in(2) );
 161       if( t12->singleton() && t12 != Type::TOP ) { // Left input is an add of a constant?
 162         assert( add1->in(1) != add1, "dead loop in MulNode::Ideal" );
 163         // Compute new constant; check for overflow
 164         const Type *tcon01 = mul_ring(t2,t12);
 165         if( tcon01->singleton() ) {
 166 
 167         // Convert (X+con1)*con0 into X*con0
 168           Node *mul = clone();    // mul = ()*con0
 169           mul->set_req(1,add1->in(1));  // mul = X*con0
 170           mul = phase->transform(mul);
 171 
 172           Node *add2 = add1->clone();
 173           add2->set_req(1, mul);        // X*con0 + con0*con1
 174           add2->set_req(2, phase->makecon(tcon01) );
 175           progress = add2;
 176         }
 177       }
 178     } // End of is left input an add
 179   } // End of is right input a Mul
 180 
 181   return progress;
 182 }
 183 
 184 //------------------------------Value-----------------------------------------
 185 const Type* MulNode::Value(PhaseGVN* phase) const {
 186   const Type *t1 = phase->type( in(1) );
 187   const Type *t2 = phase->type( in(2) );
 188   // Either input is TOP ==> the result is TOP
 189   if( t1 == Type::TOP ) return Type::TOP;
 190   if( t2 == Type::TOP ) return Type::TOP;
 191 
 192   // Either input is ZERO ==> the result is ZERO.
 193   // Not valid for floats or doubles since +0.0 * -0.0 --> +0.0
 194   int op = Opcode();
 195   if( op == Op_MulI || op == Op_AndI || op == Op_MulL || op == Op_AndL ) {
 196     const Type *zero = add_id();        // The multiplicative zero
 197     if( t1->higher_equal( zero ) ) return zero;
 198     if( t2->higher_equal( zero ) ) return zero;
 199   }
 200 
 201   // Code pattern on return from a call that returns an __Value.  Can
 202   // be optimized away if the return value turns out to be an oop.
 203   if (op == Op_AndX &&
 204       in(1) != nullptr &&
 205       in(1)->Opcode() == Op_CastP2X &&
 206       in(1)->in(1) != nullptr &&
 207       phase->type(in(1)->in(1))->isa_oopptr() &&
 208       t2->isa_intptr_t()->_lo >= 0 &&
 209       t2->isa_intptr_t()->_hi <= MinObjAlignmentInBytesMask) {
 210     return add_id();
 211   }
 212 
 213   // Either input is BOTTOM ==> the result is the local BOTTOM
 214   if( t1 == Type::BOTTOM || t2 == Type::BOTTOM )
 215     return bottom_type();
 216 
 217 #if defined(IA32)
 218   // Can't trust native compilers to properly fold strict double
 219   // multiplication with round-to-zero on this platform.
 220   if (op == Op_MulD) {
 221     return TypeD::DOUBLE;
 222   }
 223 #endif
 224 
 225   return mul_ring(t1,t2);            // Local flavor of type multiplication
 226 }
 227 
 228 MulNode* MulNode::make(Node* in1, Node* in2, BasicType bt) {
 229   switch (bt) {
 230     case T_INT:
 231       return new MulINode(in1, in2);
 232     case T_LONG:
 233       return new MulLNode(in1, in2);
 234     default:
 235       fatal("Not implemented for %s", type2name(bt));
 236   }
 237   return nullptr;
 238 }
 239 
 240 MulNode* MulNode::make_and(Node* in1, Node* in2, BasicType bt) {
 241   switch (bt) {
 242     case T_INT:
 243       return new AndINode(in1, in2);
 244     case T_LONG:
 245       return new AndLNode(in1, in2);
 246     default:
 247       fatal("Not implemented for %s", type2name(bt));
 248   }
 249   return nullptr;
 250 }
 251 
 252 
 253 //=============================================================================
 254 //------------------------------Ideal------------------------------------------
 255 // Check for power-of-2 multiply, then try the regular MulNode::Ideal
 256 Node *MulINode::Ideal(PhaseGVN *phase, bool can_reshape) {
 257   const jint con = in(2)->find_int_con(0);
 258   if (con == 0) {
 259     // If in(2) is not a constant, call Ideal() of the parent class to
 260     // try to move constant to the right side.
 261     return MulNode::Ideal(phase, can_reshape);
 262   }
 263 
 264   // Now we have a constant Node on the right and the constant in con.
 265   if (con == 1) {
 266     // By one is handled by Identity call
 267     return nullptr;
 268   }
 269 
 270   // Check for negative constant; if so negate the final result
 271   bool sign_flip = false;
 272 
 273   unsigned int abs_con = uabs(con);
 274   if (abs_con != (unsigned int)con) {
 275     sign_flip = true;
 276   }
 277 
 278   // Get low bit; check for being the only bit
 279   Node *res = nullptr;
 280   unsigned int bit1 = submultiple_power_of_2(abs_con);
 281   if (bit1 == abs_con) {           // Found a power of 2?
 282     res = new LShiftINode(in(1), phase->intcon(log2i_exact(bit1)));
 283   } else {
 284     // Check for constant with 2 bits set
 285     unsigned int bit2 = abs_con - bit1;
 286     bit2 = bit2 & (0 - bit2);          // Extract 2nd bit
 287     if (bit2 + bit1 == abs_con) {    // Found all bits in con?
 288       Node *n1 = phase->transform(new LShiftINode(in(1), phase->intcon(log2i_exact(bit1))));
 289       Node *n2 = phase->transform(new LShiftINode(in(1), phase->intcon(log2i_exact(bit2))));
 290       res = new AddINode(n2, n1);
 291     } else if (is_power_of_2(abs_con + 1)) {
 292       // Sleezy: power-of-2 - 1.  Next time be generic.
 293       unsigned int temp = abs_con + 1;
 294       Node *n1 = phase->transform(new LShiftINode(in(1), phase->intcon(log2i_exact(temp))));
 295       res = new SubINode(n1, in(1));
 296     } else {
 297       return MulNode::Ideal(phase, can_reshape);
 298     }
 299   }
 300 
 301   if (sign_flip) {             // Need to negate result?
 302     res = phase->transform(res);// Transform, before making the zero con
 303     res = new SubINode(phase->intcon(0),res);
 304   }
 305 
 306   return res;                   // Return final result
 307 }
 308 
 309 // This template class performs type multiplication for MulI/MulLNode. NativeType is either jint or jlong.
 310 // In this class, the inputs of the MulNodes are named left and right with types [left_lo,left_hi] and [right_lo,right_hi].
 311 //
 312 // In general, the multiplication of two x-bit values could produce a result that consumes up to 2x bits if there is
 313 // enough space to hold them all. We can therefore distinguish the following two cases for the product:
 314 // - no overflow (i.e. product fits into x bits)
 315 // - overflow (i.e. product does not fit into x bits)
 316 //
 317 // When multiplying the two x-bit inputs 'left' and 'right' with their x-bit types [left_lo,left_hi] and [right_lo,right_hi]
 318 // we need to find the minimum and maximum of all possible products to define a new type. To do that, we compute the
 319 // cross product of [left_lo,left_hi] and [right_lo,right_hi] in 2x-bit space where no over- or underflow can happen.
 320 // The cross product consists of the following four multiplications with 2x-bit results:
 321 // (1) left_lo * right_lo
 322 // (2) left_lo * right_hi
 323 // (3) left_hi * right_lo
 324 // (4) left_hi * right_hi
 325 //
 326 // Let's define the following two functions:
 327 // - Lx(i): Returns the lower x bits of the 2x-bit number i.
 328 // - Ux(i): Returns the upper x bits of the 2x-bit number i.
 329 //
 330 // Let's first assume all products are positive where only overflows are possible but no underflows. If there is no
 331 // overflow for a product p, then the upper x bits of the 2x-bit result p are all zero:
 332 //     Ux(p) = 0
 333 //     Lx(p) = p
 334 //
 335 // If none of the multiplications (1)-(4) overflow, we can truncate the upper x bits and use the following result type
 336 // with x bits:
 337 //      [result_lo,result_hi] = [MIN(Lx(1),Lx(2),Lx(3),Lx(4)),MAX(Lx(1),Lx(2),Lx(3),Lx(4))]
 338 //
 339 // If any of these multiplications overflows, we could pessimistically take the bottom type for the x bit result
 340 // (i.e. all values in the x-bit space could be possible):
 341 //      [result_lo,result_hi] = [NativeType_min,NativeType_max]
 342 //
 343 // However, in case of any overflow, we can do better by analyzing the upper x bits of all multiplications (1)-(4) with
 344 // 2x-bit results. The upper x bits tell us something about how many times a multiplication has overflown the lower
 345 // x bits. If the upper x bits of (1)-(4) are all equal, then we know that all of these multiplications overflowed
 346 // the lower x bits the same number of times:
 347 //     Ux((1)) = Ux((2)) = Ux((3)) = Ux((4))
 348 //
 349 // If all upper x bits are equal, we can conclude:
 350 //     Lx(MIN((1),(2),(3),(4))) = MIN(Lx(1),Lx(2),Lx(3),Lx(4)))
 351 //     Lx(MAX((1),(2),(3),(4))) = MAX(Lx(1),Lx(2),Lx(3),Lx(4)))
 352 //
 353 // Therefore, we can use the same precise x-bit result type as for the no-overflow case:
 354 //     [result_lo,result_hi] = [(MIN(Lx(1),Lx(2),Lx(3),Lx(4))),MAX(Lx(1),Lx(2),Lx(3),Lx(4)))]
 355 //
 356 //
 357 // Now let's assume that (1)-(4) are signed multiplications where over- and underflow could occur:
 358 // Negative numbers are all sign extend with ones. Therefore, if a negative product does not underflow, then the
 359 // upper x bits of the 2x-bit result are all set to ones which is minus one in two's complement. If there is an underflow,
 360 // the upper x bits are decremented by the number of times an underflow occurred. The smallest possible negative product
 361 // is NativeType_min*NativeType_max, where the upper x bits are set to NativeType_min / 2 (b11...0). It is therefore
 362 // impossible to underflow the upper x bits. Thus, when having all ones (i.e. minus one) in the upper x bits, we know
 363 // that there is no underflow.
 364 //
 365 // To be able to compare the number of over-/underflows of positive and negative products, respectively, we normalize
 366 // the upper x bits of negative 2x-bit products by adding one. This way a product has no over- or underflow if the
 367 // normalized upper x bits are zero. Now we can use the same improved type as for strictly positive products because we
 368 // can compare the upper x bits in a unified way with N() being the normalization function:
 369 //     N(Ux((1))) = N(Ux((2))) = N(Ux((3)) = N(Ux((4)))
 370 template<typename NativeType>
 371 class IntegerTypeMultiplication {
 372 
 373   NativeType _lo_left;
 374   NativeType _lo_right;
 375   NativeType _hi_left;
 376   NativeType _hi_right;
 377   short _widen_left;
 378   short _widen_right;
 379 
 380   static const Type* overflow_type();
 381   static NativeType multiply_high(NativeType x, NativeType y);
 382   const Type* create_type(NativeType lo, NativeType hi) const;
 383 
 384   static NativeType multiply_high_signed_overflow_value(NativeType x, NativeType y) {
 385     return normalize_overflow_value(x, y, multiply_high(x, y));
 386   }
 387 
 388   bool cross_product_not_same_overflow_value() const {
 389     const NativeType lo_lo_high_product = multiply_high_signed_overflow_value(_lo_left, _lo_right);
 390     const NativeType lo_hi_high_product = multiply_high_signed_overflow_value(_lo_left, _hi_right);
 391     const NativeType hi_lo_high_product = multiply_high_signed_overflow_value(_hi_left, _lo_right);
 392     const NativeType hi_hi_high_product = multiply_high_signed_overflow_value(_hi_left, _hi_right);
 393     return lo_lo_high_product != lo_hi_high_product ||
 394            lo_hi_high_product != hi_lo_high_product ||
 395            hi_lo_high_product != hi_hi_high_product;
 396   }
 397 
 398   bool does_product_overflow(NativeType x, NativeType y) const {
 399     return multiply_high_signed_overflow_value(x, y) != 0;
 400   }
 401 
 402   static NativeType normalize_overflow_value(const NativeType x, const NativeType y, NativeType result) {
 403     return java_multiply(x, y) < 0 ? result + 1 : result;
 404   }
 405 
 406  public:
 407   template<class IntegerType>
 408   IntegerTypeMultiplication(const IntegerType* left, const IntegerType* right)
 409       : _lo_left(left->_lo), _lo_right(right->_lo),
 410         _hi_left(left->_hi), _hi_right(right->_hi),
 411         _widen_left(left->_widen), _widen_right(right->_widen)  {}
 412 
 413   // Compute the product type by multiplying the two input type ranges. We take the minimum and maximum of all possible
 414   // values (requires 4 multiplications of all possible combinations of the two range boundary values). If any of these
 415   // multiplications overflows/underflows, we need to make sure that they all have the same number of overflows/underflows
 416   // If that is not the case, we return the bottom type to cover all values due to the inconsistent overflows/underflows).
 417   const Type* compute() const {
 418     if (cross_product_not_same_overflow_value()) {
 419       return overflow_type();
 420     }
 421 
 422     NativeType lo_lo_product = java_multiply(_lo_left, _lo_right);
 423     NativeType lo_hi_product = java_multiply(_lo_left, _hi_right);
 424     NativeType hi_lo_product = java_multiply(_hi_left, _lo_right);
 425     NativeType hi_hi_product = java_multiply(_hi_left, _hi_right);
 426     const NativeType min = MIN4(lo_lo_product, lo_hi_product, hi_lo_product, hi_hi_product);
 427     const NativeType max = MAX4(lo_lo_product, lo_hi_product, hi_lo_product, hi_hi_product);
 428     return create_type(min, max);
 429   }
 430 
 431   bool does_overflow() const {
 432     return does_product_overflow(_lo_left, _lo_right) ||
 433            does_product_overflow(_lo_left, _hi_right) ||
 434            does_product_overflow(_hi_left, _lo_right) ||
 435            does_product_overflow(_hi_left, _hi_right);
 436   }
 437 };
 438 
 439 template <>
 440 const Type* IntegerTypeMultiplication<jint>::overflow_type() {
 441   return TypeInt::INT;
 442 }
 443 
 444 template <>
 445 jint IntegerTypeMultiplication<jint>::multiply_high(const jint x, const jint y) {
 446   const jlong x_64 = x;
 447   const jlong y_64 = y;
 448   const jlong product = x_64 * y_64;
 449   return (jint)((uint64_t)product >> 32u);
 450 }
 451 
 452 template <>
 453 const Type* IntegerTypeMultiplication<jint>::create_type(jint lo, jint hi) const {
 454   return TypeInt::make(lo, hi, MAX2(_widen_left, _widen_right));
 455 }
 456 
 457 template <>
 458 const Type* IntegerTypeMultiplication<jlong>::overflow_type() {
 459   return TypeLong::LONG;
 460 }
 461 
 462 template <>
 463 jlong IntegerTypeMultiplication<jlong>::multiply_high(const jlong x, const jlong y) {
 464   return multiply_high_signed(x, y);
 465 }
 466 
 467 template <>
 468 const Type* IntegerTypeMultiplication<jlong>::create_type(jlong lo, jlong hi) const {
 469   return TypeLong::make(lo, hi, MAX2(_widen_left, _widen_right));
 470 }
 471 
 472 // Compute the product type of two integer ranges into this node.
 473 const Type* MulINode::mul_ring(const Type* type_left, const Type* type_right) const {
 474   const IntegerTypeMultiplication<jint> integer_multiplication(type_left->is_int(), type_right->is_int());
 475   return integer_multiplication.compute();
 476 }
 477 
 478 bool MulINode::does_overflow(const TypeInt* type_left, const TypeInt* type_right) {
 479   const IntegerTypeMultiplication<jint> integer_multiplication(type_left, type_right);
 480   return integer_multiplication.does_overflow();
 481 }
 482 
 483 // Compute the product type of two long ranges into this node.
 484 const Type* MulLNode::mul_ring(const Type* type_left, const Type* type_right) const {
 485   const IntegerTypeMultiplication<jlong> integer_multiplication(type_left->is_long(), type_right->is_long());
 486   return integer_multiplication.compute();
 487 }
 488 
 489 //=============================================================================
 490 //------------------------------Ideal------------------------------------------
 491 // Check for power-of-2 multiply, then try the regular MulNode::Ideal
 492 Node *MulLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 493   const jlong con = in(2)->find_long_con(0);
 494   if (con == 0) {
 495     // If in(2) is not a constant, call Ideal() of the parent class to
 496     // try to move constant to the right side.
 497     return MulNode::Ideal(phase, can_reshape);
 498   }
 499 
 500   // Now we have a constant Node on the right and the constant in con.
 501   if (con == 1) {
 502     // By one is handled by Identity call
 503     return nullptr;
 504   }
 505 
 506   // Check for negative constant; if so negate the final result
 507   bool sign_flip = false;
 508   julong abs_con = uabs(con);
 509   if (abs_con != (julong)con) {
 510     sign_flip = true;
 511   }
 512 
 513   // Get low bit; check for being the only bit
 514   Node *res = nullptr;
 515   julong bit1 = submultiple_power_of_2(abs_con);
 516   if (bit1 == abs_con) {           // Found a power of 2?
 517     res = new LShiftLNode(in(1), phase->intcon(log2i_exact(bit1)));
 518   } else {
 519 
 520     // Check for constant with 2 bits set
 521     julong bit2 = abs_con-bit1;
 522     bit2 = bit2 & (0-bit2);          // Extract 2nd bit
 523     if (bit2 + bit1 == abs_con) {    // Found all bits in con?
 524       Node *n1 = phase->transform(new LShiftLNode(in(1), phase->intcon(log2i_exact(bit1))));
 525       Node *n2 = phase->transform(new LShiftLNode(in(1), phase->intcon(log2i_exact(bit2))));
 526       res = new AddLNode(n2, n1);
 527 
 528     } else if (is_power_of_2(abs_con+1)) {
 529       // Sleezy: power-of-2 -1.  Next time be generic.
 530       julong temp = abs_con + 1;
 531       Node *n1 = phase->transform( new LShiftLNode(in(1), phase->intcon(log2i_exact(temp))));
 532       res = new SubLNode(n1, in(1));
 533     } else {
 534       return MulNode::Ideal(phase, can_reshape);
 535     }
 536   }
 537 
 538   if (sign_flip) {             // Need to negate result?
 539     res = phase->transform(res);// Transform, before making the zero con
 540     res = new SubLNode(phase->longcon(0),res);
 541   }
 542 
 543   return res;                   // Return final result
 544 }
 545 
 546 //=============================================================================
 547 //------------------------------mul_ring---------------------------------------
 548 // Compute the product type of two double ranges into this node.
 549 const Type *MulFNode::mul_ring(const Type *t0, const Type *t1) const {
 550   if( t0 == Type::FLOAT || t1 == Type::FLOAT ) return Type::FLOAT;
 551   return TypeF::make( t0->getf() * t1->getf() );
 552 }
 553 
 554 //------------------------------Ideal---------------------------------------
 555 // Check to see if we are multiplying by a constant 2 and convert to add, then try the regular MulNode::Ideal
 556 Node* MulFNode::Ideal(PhaseGVN* phase, bool can_reshape) {
 557   const TypeF *t2 = phase->type(in(2))->isa_float_constant();
 558 
 559   // x * 2 -> x + x
 560   if (t2 != nullptr && t2->getf() == 2) {
 561     Node* base = in(1);
 562     return new AddFNode(base, base);
 563   }
 564   return MulNode::Ideal(phase, can_reshape);
 565 }
 566 
 567 //=============================================================================
 568 //------------------------------Ideal------------------------------------------
 569 // Check to see if we are multiplying by a constant 2 and convert to add, then try the regular MulNode::Ideal
 570 Node* MulHFNode::Ideal(PhaseGVN* phase, bool can_reshape) {
 571   const TypeH* t2 = phase->type(in(2))->isa_half_float_constant();
 572 
 573   // x * 2 -> x + x
 574   if (t2 != nullptr && t2->getf() == 2) {
 575     Node* base = in(1);
 576     return new AddHFNode(base, base);
 577   }
 578   return MulNode::Ideal(phase, can_reshape);
 579 }
 580 
 581 // Compute the product type of two half float ranges into this node.
 582 const Type* MulHFNode::mul_ring(const Type* t0, const Type* t1) const {
 583   if (t0 == Type::HALF_FLOAT || t1 == Type::HALF_FLOAT) {
 584     return Type::HALF_FLOAT;
 585   }
 586   return TypeH::make(t0->getf() * t1->getf());
 587 }
 588 
 589 //=============================================================================
 590 //------------------------------mul_ring---------------------------------------
 591 // Compute the product type of two double ranges into this node.
 592 const Type *MulDNode::mul_ring(const Type *t0, const Type *t1) const {
 593   if( t0 == Type::DOUBLE || t1 == Type::DOUBLE ) return Type::DOUBLE;
 594   // We must be multiplying 2 double constants.
 595   return TypeD::make( t0->getd() * t1->getd() );
 596 }
 597 
 598 //------------------------------Ideal---------------------------------------
 599 // Check to see if we are multiplying by a constant 2 and convert to add, then try the regular MulNode::Ideal
 600 Node* MulDNode::Ideal(PhaseGVN* phase, bool can_reshape) {
 601   const TypeD *t2 = phase->type(in(2))->isa_double_constant();
 602 
 603   // x * 2 -> x + x
 604   if (t2 != nullptr && t2->getd() == 2) {
 605     Node* base = in(1);
 606     return new AddDNode(base, base);
 607   }
 608 
 609   return MulNode::Ideal(phase, can_reshape);
 610 }
 611 
 612 //=============================================================================
 613 //------------------------------Value------------------------------------------
 614 const Type* MulHiLNode::Value(PhaseGVN* phase) const {
 615   const Type *t1 = phase->type( in(1) );
 616   const Type *t2 = phase->type( in(2) );
 617   const Type *bot = bottom_type();
 618   return MulHiValue(t1, t2, bot);
 619 }
 620 
 621 const Type* UMulHiLNode::Value(PhaseGVN* phase) const {
 622   const Type *t1 = phase->type( in(1) );
 623   const Type *t2 = phase->type( in(2) );
 624   const Type *bot = bottom_type();
 625   return MulHiValue(t1, t2, bot);
 626 }
 627 
 628 // A common routine used by UMulHiLNode and MulHiLNode
 629 const Type* MulHiValue(const Type *t1, const Type *t2, const Type *bot) {
 630   // Either input is TOP ==> the result is TOP
 631   if( t1 == Type::TOP ) return Type::TOP;
 632   if( t2 == Type::TOP ) return Type::TOP;
 633 
 634   // Either input is BOTTOM ==> the result is the local BOTTOM
 635   if( (t1 == bot) || (t2 == bot) ||
 636       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 637     return bot;
 638 
 639   // It is not worth trying to constant fold this stuff!
 640   return TypeLong::LONG;
 641 }
 642 
 643 template<typename IntegerType>
 644 static const IntegerType* and_value(const IntegerType* r0, const IntegerType* r1) {
 645   typedef typename IntegerType::NativeType NativeType;
 646   static_assert(std::is_signed<NativeType>::value, "Native type of IntegerType must be signed!");
 647 
 648   int widen = MAX2(r0->_widen, r1->_widen);
 649 
 650   // If both types are constants, we can calculate a constant result.
 651   if (r0->is_con() && r1->is_con()) {
 652     return IntegerType::make(r0->get_con() & r1->get_con());
 653   }
 654 
 655   // If both ranges are positive, the result will range from 0 up to the hi value of the smaller range. The minimum
 656   // of the two constrains the upper bound because any higher value in the other range will see all zeroes, so it will be masked out.
 657   if (r0->_lo >= 0 && r1->_lo >= 0) {
 658     return IntegerType::make(0, MIN2(r0->_hi, r1->_hi), widen);
 659   }
 660 
 661   // If only one range is positive, the result will range from 0 up to that range's maximum value.
 662   // For the operation 'x & C' where C is a positive constant, the result will be in the range [0..C]. With that observation,
 663   // we can say that for any integer c such that 0 <= c <= C will also be in the range [0..C]. Therefore, 'x & [c..C]'
 664   // where c >= 0 will be in the range [0..C].
 665   if (r0->_lo >= 0) {
 666     return IntegerType::make(0, r0->_hi, widen);
 667   }
 668 
 669   if (r1->_lo >= 0) {
 670     return IntegerType::make(0, r1->_hi, widen);
 671   }
 672 
 673   // At this point, all positive ranges will have already been handled, so the only remaining cases will be negative ranges
 674   // and constants.
 675 
 676   assert(r0->_lo < 0 && r1->_lo < 0, "positive ranges should already be handled!");
 677 
 678   // As two's complement means that both numbers will start with leading 1s, the lower bound of both ranges will contain
 679   // the common leading 1s of both minimum values. In order to count them with count_leading_zeros, the bits are inverted.
 680   NativeType sel_val = ~MIN2(r0->_lo, r1->_lo);
 681 
 682   NativeType min;
 683   if (sel_val == 0) {
 684     // Since count_leading_zeros is undefined at 0, we short-circuit the condition where both ranges have a minimum of -1.
 685     min = -1;
 686   } else {
 687     // To get the number of bits to shift, we count the leading 0-bits and then subtract one, as the sign bit is already set.
 688     int shift_bits = count_leading_zeros(sel_val) - 1;
 689     min = std::numeric_limits<NativeType>::min() >> shift_bits;
 690   }
 691 
 692   NativeType max;
 693   if (r0->_hi < 0 && r1->_hi < 0) {
 694     // If both ranges are negative, then the same optimization as both positive ranges will apply, and the smaller hi
 695     // value will mask off any bits set by higher values.
 696     max = MIN2(r0->_hi, r1->_hi);
 697   } else {
 698     // In the case of ranges that cross zero, negative values can cause the higher order bits to be set, so the maximum
 699     // positive value can be as high as the larger hi value.
 700     max = MAX2(r0->_hi, r1->_hi);
 701   }
 702 
 703   return IntegerType::make(min, max, widen);
 704 }
 705 
 706 //=============================================================================
 707 //------------------------------mul_ring---------------------------------------
 708 // Supplied function returns the product of the inputs IN THE CURRENT RING.
 709 // For the logical operations the ring's MUL is really a logical AND function.
 710 // This also type-checks the inputs for sanity.  Guaranteed never to
 711 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 712 const Type *AndINode::mul_ring( const Type *t0, const Type *t1 ) const {
 713   const TypeInt* r0 = t0->is_int();
 714   const TypeInt* r1 = t1->is_int();
 715 
 716   return and_value<TypeInt>(r0, r1);
 717 }
 718 
 719 static bool AndIL_is_zero_element_under_mask(const PhaseGVN* phase, const Node* expr, const Node* mask, BasicType bt);
 720 
 721 const Type* AndINode::Value(PhaseGVN* phase) const {
 722   if (AndIL_is_zero_element_under_mask(phase, in(1), in(2), T_INT) ||
 723       AndIL_is_zero_element_under_mask(phase, in(2), in(1), T_INT)) {
 724     return TypeInt::ZERO;
 725   }
 726 
 727   return MulNode::Value(phase);
 728 }
 729 
 730 //------------------------------Identity---------------------------------------
 731 // Masking off the high bits of an unsigned load is not required
 732 Node* AndINode::Identity(PhaseGVN* phase) {
 733 
 734   // x & x => x
 735   if (in(1) == in(2)) {
 736     return in(1);
 737   }
 738 
 739   Node* in1 = in(1);
 740   uint op = in1->Opcode();
 741   const TypeInt* t2 = phase->type(in(2))->isa_int();
 742   if (t2 && t2->is_con()) {
 743     int con = t2->get_con();
 744     // Masking off high bits which are always zero is useless.
 745     const TypeInt* t1 = phase->type(in(1))->isa_int();
 746     if (t1 != nullptr && t1->_lo >= 0) {
 747       jint t1_support = right_n_bits(1 + log2i_graceful(t1->_hi));
 748       if ((t1_support & con) == t1_support)
 749         return in1;
 750     }
 751     // Masking off the high bits of a unsigned-shift-right is not
 752     // needed either.
 753     if (op == Op_URShiftI) {
 754       const TypeInt* t12 = phase->type(in1->in(2))->isa_int();
 755       if (t12 && t12->is_con()) {  // Shift is by a constant
 756         int shift = t12->get_con();
 757         shift &= BitsPerJavaInteger - 1;  // semantics of Java shifts
 758         int mask = max_juint >> shift;
 759         if ((mask & con) == mask)  // If AND is useless, skip it
 760           return in1;
 761       }
 762     }
 763   }
 764   return MulNode::Identity(phase);
 765 }
 766 
 767 //------------------------------Ideal------------------------------------------
 768 Node *AndINode::Ideal(PhaseGVN *phase, bool can_reshape) {
 769   // Simplify (v1 + v2) & mask to v1 & mask or v2 & mask when possible.
 770   Node* progress = AndIL_sum_and_mask(phase, T_INT);
 771   if (progress != nullptr) {
 772     return progress;
 773   }
 774 
 775   // Convert "(~a) & (~b)" into "~(a | b)"
 776   if (AddNode::is_not(phase, in(1), T_INT) && AddNode::is_not(phase, in(2), T_INT)) {
 777     Node* or_a_b = new OrINode(in(1)->in(1), in(2)->in(1));
 778     Node* tn = phase->transform(or_a_b);
 779     return AddNode::make_not(phase, tn, T_INT);
 780   }
 781 
 782   // Special case constant AND mask
 783   const TypeInt *t2 = phase->type( in(2) )->isa_int();
 784   if( !t2 || !t2->is_con() ) return MulNode::Ideal(phase, can_reshape);
 785   const int mask = t2->get_con();
 786   Node *load = in(1);
 787   uint lop = load->Opcode();
 788 
 789   // Masking bits off of a Character?  Hi bits are already zero.
 790   if( lop == Op_LoadUS &&
 791       (mask & 0xFFFF0000) )     // Can we make a smaller mask?
 792     return new AndINode(load,phase->intcon(mask&0xFFFF));
 793 
 794   // Masking bits off of a Short?  Loading a Character does some masking
 795   if (can_reshape &&
 796       load->outcnt() == 1 && load->unique_out() == this) {
 797     if (lop == Op_LoadS && (mask & 0xFFFF0000) == 0 ) {
 798       Node* ldus = load->as_Load()->convert_to_unsigned_load(*phase);
 799       ldus = phase->transform(ldus);
 800       return new AndINode(ldus, phase->intcon(mask & 0xFFFF));
 801     }
 802 
 803     // Masking sign bits off of a Byte?  Do an unsigned byte load plus
 804     // an and.
 805     if (lop == Op_LoadB && (mask & 0xFFFFFF00) == 0) {
 806       Node* ldub = load->as_Load()->convert_to_unsigned_load(*phase);
 807       ldub = phase->transform(ldub);
 808       return new AndINode(ldub, phase->intcon(mask));
 809     }
 810   }
 811 
 812   // Masking off sign bits?  Dont make them!
 813   if( lop == Op_RShiftI ) {
 814     const TypeInt *t12 = phase->type(load->in(2))->isa_int();
 815     if( t12 && t12->is_con() ) { // Shift is by a constant
 816       int shift = t12->get_con();
 817       shift &= BitsPerJavaInteger-1;  // semantics of Java shifts
 818       const int sign_bits_mask = ~right_n_bits(BitsPerJavaInteger - shift);
 819       // If the AND'ing of the 2 masks has no bits, then only original shifted
 820       // bits survive.  NO sign-extension bits survive the maskings.
 821       if( (sign_bits_mask & mask) == 0 ) {
 822         // Use zero-fill shift instead
 823         Node *zshift = phase->transform(new URShiftINode(load->in(1),load->in(2)));
 824         return new AndINode( zshift, in(2) );
 825       }
 826     }
 827   }
 828 
 829   // Check for 'negate/and-1', a pattern emitted when someone asks for
 830   // 'mod 2'.  Negate leaves the low order bit unchanged (think: complement
 831   // plus 1) and the mask is of the low order bit.  Skip the negate.
 832   if( lop == Op_SubI && mask == 1 && load->in(1) &&
 833       phase->type(load->in(1)) == TypeInt::ZERO )
 834     return new AndINode( load->in(2), in(2) );
 835 
 836   return MulNode::Ideal(phase, can_reshape);
 837 }
 838 
 839 //=============================================================================
 840 //------------------------------mul_ring---------------------------------------
 841 // Supplied function returns the product of the inputs IN THE CURRENT RING.
 842 // For the logical operations the ring's MUL is really a logical AND function.
 843 // This also type-checks the inputs for sanity.  Guaranteed never to
 844 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 845 const Type *AndLNode::mul_ring( const Type *t0, const Type *t1 ) const {
 846   const TypeLong* r0 = t0->is_long();
 847   const TypeLong* r1 = t1->is_long();
 848 
 849   return and_value<TypeLong>(r0, r1);
 850 }
 851 
 852 const Type* AndLNode::Value(PhaseGVN* phase) const {
 853   if (AndIL_is_zero_element_under_mask(phase, in(1), in(2), T_LONG) ||
 854       AndIL_is_zero_element_under_mask(phase, in(2), in(1), T_LONG)) {
 855     return TypeLong::ZERO;
 856   }
 857 
 858   return MulNode::Value(phase);
 859 }
 860 
 861 //------------------------------Identity---------------------------------------
 862 // Masking off the high bits of an unsigned load is not required
 863 Node* AndLNode::Identity(PhaseGVN* phase) {
 864 
 865   // x & x => x
 866   if (in(1) == in(2)) {
 867     return in(1);
 868   }
 869 
 870   Node *usr = in(1);
 871   const TypeLong *t2 = phase->type( in(2) )->isa_long();
 872   if( t2 && t2->is_con() ) {
 873     jlong con = t2->get_con();
 874     // Masking off high bits which are always zero is useless.
 875     const TypeLong* t1 = phase->type( in(1) )->isa_long();
 876     if (t1 != nullptr && t1->_lo >= 0) {
 877       int bit_count = log2i_graceful(t1->_hi) + 1;
 878       jlong t1_support = jlong(max_julong >> (BitsPerJavaLong - bit_count));
 879       if ((t1_support & con) == t1_support)
 880         return usr;
 881     }
 882     uint lop = usr->Opcode();
 883     // Masking off the high bits of a unsigned-shift-right is not
 884     // needed either.
 885     if( lop == Op_URShiftL ) {
 886       const TypeInt *t12 = phase->type( usr->in(2) )->isa_int();
 887       if( t12 && t12->is_con() ) {  // Shift is by a constant
 888         int shift = t12->get_con();
 889         shift &= BitsPerJavaLong - 1;  // semantics of Java shifts
 890         jlong mask = max_julong >> shift;
 891         if( (mask&con) == mask )  // If AND is useless, skip it
 892           return usr;
 893       }
 894     }
 895   }
 896   return MulNode::Identity(phase);
 897 }
 898 
 899 //------------------------------Ideal------------------------------------------
 900 Node *AndLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 901   // Simplify (v1 + v2) & mask to v1 & mask or v2 & mask when possible.
 902   Node* progress = AndIL_sum_and_mask(phase, T_LONG);
 903   if (progress != nullptr) {
 904     return progress;
 905   }
 906 
 907   // Convert "(~a) & (~b)" into "~(a | b)"
 908   if (AddNode::is_not(phase, in(1), T_LONG) && AddNode::is_not(phase, in(2), T_LONG)) {
 909     Node* or_a_b = new OrLNode(in(1)->in(1), in(2)->in(1));
 910     Node* tn = phase->transform(or_a_b);
 911     return AddNode::make_not(phase, tn, T_LONG);
 912   }
 913 
 914   // Special case constant AND mask
 915   const TypeLong *t2 = phase->type( in(2) )->isa_long();
 916   if( !t2 || !t2->is_con() ) return MulNode::Ideal(phase, can_reshape);
 917   const jlong mask = t2->get_con();
 918 
 919   Node* in1 = in(1);
 920   int op = in1->Opcode();
 921 
 922   // Are we masking a long that was converted from an int with a mask
 923   // that fits in 32-bits?  Commute them and use an AndINode.  Don't
 924   // convert masks which would cause a sign extension of the integer
 925   // value.  This check includes UI2L masks (0x00000000FFFFFFFF) which
 926   // would be optimized away later in Identity.
 927   if (op == Op_ConvI2L && (mask & UCONST64(0xFFFFFFFF80000000)) == 0) {
 928     Node* andi = new AndINode(in1->in(1), phase->intcon(mask));
 929     andi = phase->transform(andi);
 930     return new ConvI2LNode(andi);
 931   }
 932 
 933   // Masking off sign bits?  Dont make them!
 934   if (op == Op_RShiftL) {
 935     const TypeInt* t12 = phase->type(in1->in(2))->isa_int();
 936     if( t12 && t12->is_con() ) { // Shift is by a constant
 937       int shift = t12->get_con();
 938       shift &= BitsPerJavaLong - 1;  // semantics of Java shifts
 939       const julong sign_bits_mask = ~(((julong)CONST64(1) << (julong)(BitsPerJavaLong - shift)) -1);
 940       // If the AND'ing of the 2 masks has no bits, then only original shifted
 941       // bits survive.  NO sign-extension bits survive the maskings.
 942       if( (sign_bits_mask & mask) == 0 ) {
 943         // Use zero-fill shift instead
 944         Node *zshift = phase->transform(new URShiftLNode(in1->in(1), in1->in(2)));
 945         return new AndLNode(zshift, in(2));
 946       }
 947     }
 948   }
 949 
 950   // Search for GraphKit::mark_word_test patterns and fold the test if the result is statically known
 951   Node* load1 = in(1);
 952   Node* load2 = nullptr;
 953   if (load1->is_Phi() && phase->type(load1)->isa_long()) {
 954     load1 = in(1)->in(1);
 955     load2 = in(1)->in(2);
 956   }
 957   if (load1 != nullptr && load1->is_Load() && phase->type(load1)->isa_long() &&
 958       (load2 == nullptr || (load2->is_Load() && phase->type(load2)->isa_long()))) {
 959     const TypePtr* adr_t1 = phase->type(load1->in(MemNode::Address))->isa_ptr();
 960     const TypePtr* adr_t2 = (load2 != nullptr) ? phase->type(load2->in(MemNode::Address))->isa_ptr() : nullptr;
 961     if (adr_t1 != nullptr && adr_t1->offset() == oopDesc::mark_offset_in_bytes() &&
 962         (load2 == nullptr || (adr_t2 != nullptr && adr_t2->offset() == in_bytes(Klass::prototype_header_offset())))) {
 963       if (mask == markWord::inline_type_pattern) {
 964         if (adr_t1->is_inlinetypeptr()) {
 965           set_req_X(1, in(2), phase);
 966           return this;
 967         } else if (!adr_t1->can_be_inline_type()) {
 968           set_req_X(1, phase->longcon(0), phase);
 969           return this;
 970         }
 971       } else if (mask == markWord::null_free_array_bit_in_place) {
 972         if (adr_t1->is_null_free()) {
 973           set_req_X(1, in(2), phase);
 974           return this;
 975         } else if (adr_t1->is_not_null_free()) {
 976           set_req_X(1, phase->longcon(0), phase);
 977           return this;
 978         }
 979       } else if (mask == markWord::flat_array_bit_in_place) {
 980         if (adr_t1->is_flat()) {
 981           set_req_X(1, in(2), phase);
 982           return this;
 983         } else if (adr_t1->is_not_flat()) {
 984           set_req_X(1, phase->longcon(0), phase);
 985           return this;
 986         }
 987       }
 988     }
 989   }
 990 
 991   return MulNode::Ideal(phase, can_reshape);
 992 }
 993 
 994 LShiftNode* LShiftNode::make(Node* in1, Node* in2, BasicType bt) {
 995   switch (bt) {
 996     case T_INT:
 997       return new LShiftINode(in1, in2);
 998     case T_LONG:
 999       return new LShiftLNode(in1, in2);
1000     default:
1001       fatal("Not implemented for %s", type2name(bt));
1002   }
1003   return nullptr;
1004 }
1005 
1006 // Returns whether the shift amount is constant. If so, sets count.
1007 static bool const_shift_count(PhaseGVN* phase, const Node* shift_node, int* count) {
1008   const TypeInt* tcount = phase->type(shift_node->in(2))->isa_int();
1009   if (tcount != nullptr && tcount->is_con()) {
1010     *count = tcount->get_con();
1011     return true;
1012   }
1013   return false;
1014 }
1015 
1016 // Returns whether the shift amount is constant. If so, sets real_shift and masked_shift.
1017 static bool mask_shift_amount(PhaseGVN* phase, const Node* shift_node, uint nBits, int& real_shift, int& masked_shift) {
1018   if (const_shift_count(phase, shift_node, &real_shift)) {
1019     masked_shift = real_shift & (nBits - 1);
1020     return true;
1021   }
1022   return false;
1023 }
1024 
1025 // Convenience for when we don't care about the real amount
1026 static bool mask_shift_amount(PhaseGVN* phase, const Node* shift_node, uint nBits, int& masked_shift) {
1027   int real_shift;
1028   return mask_shift_amount(phase, shift_node, nBits, real_shift, masked_shift);
1029 }
1030 
1031 // Use this in ::Ideal only with shiftNode == this!
1032 // Returns the masked shift amount if constant or 0 if not constant.
1033 static int mask_and_replace_shift_amount(PhaseGVN* phase, Node* shift_node, uint nBits) {
1034   int real_shift;
1035   int masked_shift;
1036   if (mask_shift_amount(phase, shift_node, nBits, real_shift, masked_shift)) {
1037     if (masked_shift == 0) {
1038       // Let Identity() handle 0 shift count.
1039       return 0;
1040     }
1041 
1042     if (real_shift != masked_shift) {
1043       PhaseIterGVN* igvn = phase->is_IterGVN();
1044       if (igvn != nullptr) {
1045         igvn->_worklist.push(shift_node);
1046       }
1047       shift_node->set_req(2, phase->intcon(masked_shift)); // Replace shift count with masked value.
1048     }
1049     return masked_shift;
1050   }
1051   // Not a shift by a constant.
1052   return 0;
1053 }
1054 
1055 // Called with
1056 //   outer_shift = (_ << rhs_outer)
1057 // We are looking for the pattern:
1058 //   outer_shift = ((X << rhs_inner) << rhs_outer)
1059 //   where rhs_outer and rhs_inner are constant
1060 //   we denote inner_shift the nested expression (X << rhs_inner)
1061 //   con_inner = rhs_inner % nbits and con_outer = rhs_outer % nbits
1062 //   where nbits is the number of bits of the shifts
1063 //
1064 // There are 2 cases:
1065 // if con_outer + con_inner >= nbits => 0
1066 // if con_outer + con_inner < nbits => X << (con_outer + con_inner)
1067 static Node* collapse_nested_shift_left(PhaseGVN* phase, const Node* outer_shift, int con_outer, BasicType bt) {
1068   assert(bt == T_LONG || bt == T_INT, "Unexpected type");
1069   const Node* inner_shift = outer_shift->in(1);
1070   if (inner_shift->Opcode() != Op_LShift(bt)) {
1071     return nullptr;
1072   }
1073 
1074   int nbits = static_cast<int>(bits_per_java_integer(bt));
1075   int con_inner;
1076   if (!mask_shift_amount(phase, inner_shift, nbits, con_inner)) {
1077     return nullptr;
1078   }
1079 
1080   if (con_inner == 0) {
1081     // We let the Identity() of the inner shift do its job.
1082     return nullptr;
1083   }
1084 
1085   if (con_outer + con_inner >= nbits) {
1086     // While it might be tempting to use
1087     // phase->zerocon(bt);
1088     // it would be incorrect: zerocon caches nodes, while Ideal is only allowed
1089     // to return a new node, this or nullptr, but not an old (cached) node.
1090     return ConNode::make(TypeInteger::zero(bt));
1091   }
1092 
1093   // con0 + con1 < nbits ==> actual shift happens now
1094   Node* con0_plus_con1 = phase->intcon(con_outer + con_inner);
1095   return LShiftNode::make(inner_shift->in(1), con0_plus_con1, bt);
1096 }
1097 
1098 //------------------------------Identity---------------------------------------
1099 Node* LShiftINode::Identity(PhaseGVN* phase) {
1100   int count = 0;
1101   if (const_shift_count(phase, this, &count) && (count & (BitsPerJavaInteger - 1)) == 0) {
1102     // Shift by a multiple of 32 does nothing
1103     return in(1);
1104   }
1105   return this;
1106 }
1107 
1108 //------------------------------Ideal------------------------------------------
1109 // If the right input is a constant, and the left input is an add of a
1110 // constant, flatten the tree: (X+con1)<<con0 ==> X<<con0 + con1<<con0
1111 //
1112 // Also collapse nested left-shifts with constant rhs:
1113 // (X << con1) << con2 ==> X << (con1 + con2)
1114 Node *LShiftINode::Ideal(PhaseGVN *phase, bool can_reshape) {
1115   int con = mask_and_replace_shift_amount(phase, this, BitsPerJavaInteger);
1116   if (con == 0) {
1117     return nullptr;
1118   }
1119 
1120   // Left input is an add?
1121   Node *add1 = in(1);
1122   int add1_op = add1->Opcode();
1123   if( add1_op == Op_AddI ) {    // Left input is an add?
1124     assert( add1 != add1->in(1), "dead loop in LShiftINode::Ideal" );
1125 
1126     // Transform is legal, but check for profit.  Avoid breaking 'i2s'
1127     // and 'i2b' patterns which typically fold into 'StoreC/StoreB'.
1128     if( con < 16 ) {
1129       // Left input is an add of the same number?
1130       if (add1->in(1) == add1->in(2)) {
1131         // Convert "(x + x) << c0" into "x << (c0 + 1)"
1132         // In general, this optimization cannot be applied for c0 == 31 since
1133         // 2x << 31 != x << 32 = x << 0 = x (e.g. x = 1: 2 << 31 = 0 != 1)
1134         return new LShiftINode(add1->in(1), phase->intcon(con + 1));
1135       }
1136 
1137       // Left input is an add of a constant?
1138       const TypeInt *t12 = phase->type(add1->in(2))->isa_int();
1139       if( t12 && t12->is_con() ){ // Left input is an add of a con?
1140         // Compute X << con0
1141         Node *lsh = phase->transform( new LShiftINode( add1->in(1), in(2) ) );
1142         // Compute X<<con0 + (con1<<con0)
1143         return new AddINode( lsh, phase->intcon(t12->get_con() << con));
1144       }
1145     }
1146   }
1147 
1148   // Check for "(x >> C1) << C2"
1149   if (add1_op == Op_RShiftI || add1_op == Op_URShiftI) {
1150     int add1Con = 0;
1151     const_shift_count(phase, add1, &add1Con);
1152 
1153     // Special case C1 == C2, which just masks off low bits
1154     if (add1Con > 0 && con == add1Con) {
1155       // Convert to "(x & -(1 << C2))"
1156       return new AndINode(add1->in(1), phase->intcon(java_negate(jint(1 << con))));
1157     } else {
1158       // Wait until the right shift has been sharpened to the correct count
1159       if (add1Con > 0 && add1Con < BitsPerJavaInteger) {
1160         // As loop parsing can produce LShiftI nodes, we should wait until the graph is fully formed
1161         // to apply optimizations, otherwise we can inadvertently stop vectorization opportunities.
1162         if (phase->is_IterGVN()) {
1163           if (con > add1Con) {
1164             // Creates "(x << (C2 - C1)) & -(1 << C2)"
1165             Node* lshift = phase->transform(new LShiftINode(add1->in(1), phase->intcon(con - add1Con)));
1166             return new AndINode(lshift, phase->intcon(java_negate(jint(1 << con))));
1167           } else {
1168             assert(con < add1Con, "must be (%d < %d)", con, add1Con);
1169             // Creates "(x >> (C1 - C2)) & -(1 << C2)"
1170 
1171             // Handle logical and arithmetic shifts
1172             Node* rshift;
1173             if (add1_op == Op_RShiftI) {
1174               rshift = phase->transform(new RShiftINode(add1->in(1), phase->intcon(add1Con - con)));
1175             } else {
1176               rshift = phase->transform(new URShiftINode(add1->in(1), phase->intcon(add1Con - con)));
1177             }
1178 
1179             return new AndINode(rshift, phase->intcon(java_negate(jint(1 << con))));
1180           }
1181         } else {
1182           phase->record_for_igvn(this);
1183         }
1184       }
1185     }
1186   }
1187 
1188   // Check for "((x >> C1) & Y) << C2"
1189   if (add1_op == Op_AndI) {
1190     Node *add2 = add1->in(1);
1191     int add2_op = add2->Opcode();
1192     if (add2_op == Op_RShiftI || add2_op == Op_URShiftI) {
1193       // Special case C1 == C2, which just masks off low bits
1194       if (add2->in(2) == in(2)) {
1195         // Convert to "(x & (Y << C2))"
1196         Node* y_sh = phase->transform(new LShiftINode(add1->in(2), phase->intcon(con)));
1197         return new AndINode(add2->in(1), y_sh);
1198       }
1199 
1200       int add2Con = 0;
1201       const_shift_count(phase, add2, &add2Con);
1202       if (add2Con > 0 && add2Con < BitsPerJavaInteger) {
1203         if (phase->is_IterGVN()) {
1204           // Convert to "((x >> C1) << C2) & (Y << C2)"
1205 
1206           // Make "(x >> C1) << C2", which will get folded away by the rule above
1207           Node* x_sh = phase->transform(new LShiftINode(add2, phase->intcon(con)));
1208           // Make "Y << C2", which will simplify when Y is a constant
1209           Node* y_sh = phase->transform(new LShiftINode(add1->in(2), phase->intcon(con)));
1210 
1211           return new AndINode(x_sh, y_sh);
1212         } else {
1213           phase->record_for_igvn(this);
1214         }
1215       }
1216     }
1217   }
1218 
1219   // Check for ((x & ((1<<(32-c0))-1)) << c0) which ANDs off high bits
1220   // before shifting them away.
1221   const jint bits_mask = right_n_bits(BitsPerJavaInteger-con);
1222   if( add1_op == Op_AndI &&
1223       phase->type(add1->in(2)) == TypeInt::make( bits_mask ) )
1224     return new LShiftINode( add1->in(1), in(2) );
1225 
1226   // Performs:
1227   // (X << con1) << con2 ==> X << (con1 + con2)
1228   Node* doubleShift = collapse_nested_shift_left(phase, this, con, T_INT);
1229   if (doubleShift != nullptr) {
1230     return doubleShift;
1231   }
1232 
1233   return nullptr;
1234 }
1235 
1236 //------------------------------Value------------------------------------------
1237 // A LShiftINode shifts its input2 left by input1 amount.
1238 const Type* LShiftINode::Value(PhaseGVN* phase) const {
1239   const Type *t1 = phase->type( in(1) );
1240   const Type *t2 = phase->type( in(2) );
1241   // Either input is TOP ==> the result is TOP
1242   if( t1 == Type::TOP ) return Type::TOP;
1243   if( t2 == Type::TOP ) return Type::TOP;
1244 
1245   // Left input is ZERO ==> the result is ZERO.
1246   if( t1 == TypeInt::ZERO ) return TypeInt::ZERO;
1247   // Shift by zero does nothing
1248   if( t2 == TypeInt::ZERO ) return t1;
1249 
1250   // Either input is BOTTOM ==> the result is BOTTOM
1251   if( (t1 == TypeInt::INT) || (t2 == TypeInt::INT) ||
1252       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
1253     return TypeInt::INT;
1254 
1255   const TypeInt *r1 = t1->is_int(); // Handy access
1256   const TypeInt *r2 = t2->is_int(); // Handy access
1257 
1258   if (!r2->is_con())
1259     return TypeInt::INT;
1260 
1261   uint shift = r2->get_con();
1262   shift &= BitsPerJavaInteger-1;  // semantics of Java shifts
1263   // Shift by a multiple of 32 does nothing:
1264   if (shift == 0)  return t1;
1265 
1266   // If the shift is a constant, shift the bounds of the type,
1267   // unless this could lead to an overflow.
1268   if (!r1->is_con()) {
1269     jint lo = r1->_lo, hi = r1->_hi;
1270     if (((lo << shift) >> shift) == lo &&
1271         ((hi << shift) >> shift) == hi) {
1272       // No overflow.  The range shifts up cleanly.
1273       return TypeInt::make((jint)lo << (jint)shift,
1274                            (jint)hi << (jint)shift,
1275                            MAX2(r1->_widen,r2->_widen));
1276     }
1277     return TypeInt::INT;
1278   }
1279 
1280   return TypeInt::make( (jint)r1->get_con() << (jint)shift );
1281 }
1282 
1283 //=============================================================================
1284 //------------------------------Identity---------------------------------------
1285 Node* LShiftLNode::Identity(PhaseGVN* phase) {
1286   int count = 0;
1287   if (const_shift_count(phase, this, &count) && (count & (BitsPerJavaLong - 1)) == 0) {
1288     // Shift by a multiple of 64 does nothing
1289     return in(1);
1290   }
1291   return this;
1292 }
1293 
1294 //------------------------------Ideal------------------------------------------
1295 // If the right input is a constant, and the left input is an add of a
1296 // constant, flatten the tree: (X+con1)<<con0 ==> X<<con0 + con1<<con0
1297 //
1298 // Also collapse nested left-shifts with constant rhs:
1299 // (X << con1) << con2 ==> X << (con1 + con2)
1300 Node *LShiftLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1301   int con = mask_and_replace_shift_amount(phase, this, BitsPerJavaLong);
1302   if (con == 0) {
1303     return nullptr;
1304   }
1305 
1306   // Left input is an add?
1307   Node *add1 = in(1);
1308   int add1_op = add1->Opcode();
1309   if( add1_op == Op_AddL ) {    // Left input is an add?
1310     // Avoid dead data cycles from dead loops
1311     assert( add1 != add1->in(1), "dead loop in LShiftLNode::Ideal" );
1312 
1313     // Left input is an add of the same number?
1314     if (con != (BitsPerJavaLong - 1) && add1->in(1) == add1->in(2)) {
1315       // Convert "(x + x) << c0" into "x << (c0 + 1)"
1316       // Can only be applied if c0 != 63 because:
1317       // (x + x) << 63 = 2x << 63, while
1318       // (x + x) << 63 --transform--> x << 64 = x << 0 = x (!= 2x << 63, for example for x = 1)
1319       // According to the Java spec, chapter 15.19, we only consider the six lowest-order bits of the right-hand operand
1320       // (i.e. "right-hand operand" & 0b111111). Therefore, x << 64 is the same as x << 0 (64 = 0b10000000 & 0b0111111 = 0).
1321       return new LShiftLNode(add1->in(1), phase->intcon(con + 1));
1322     }
1323 
1324     // Left input is an add of a constant?
1325     const TypeLong *t12 = phase->type(add1->in(2))->isa_long();
1326     if( t12 && t12->is_con() ){ // Left input is an add of a con?
1327       // Compute X << con0
1328       Node *lsh = phase->transform( new LShiftLNode( add1->in(1), in(2) ) );
1329       // Compute X<<con0 + (con1<<con0)
1330       return new AddLNode( lsh, phase->longcon(t12->get_con() << con));
1331     }
1332   }
1333 
1334   // Check for "(x >> C1) << C2"
1335   if (add1_op == Op_RShiftL || add1_op == Op_URShiftL) {
1336     int add1Con = 0;
1337     const_shift_count(phase, add1, &add1Con);
1338 
1339     // Special case C1 == C2, which just masks off low bits
1340     if (add1Con > 0 && con == add1Con) {
1341       // Convert to "(x & -(1 << C2))"
1342       return new AndLNode(add1->in(1), phase->longcon(java_negate(jlong(CONST64(1) << con))));
1343     } else {
1344       // Wait until the right shift has been sharpened to the correct count
1345       if (add1Con > 0 && add1Con < BitsPerJavaLong) {
1346         // As loop parsing can produce LShiftI nodes, we should wait until the graph is fully formed
1347         // to apply optimizations, otherwise we can inadvertently stop vectorization opportunities.
1348         if (phase->is_IterGVN()) {
1349           if (con > add1Con) {
1350             // Creates "(x << (C2 - C1)) & -(1 << C2)"
1351             Node* lshift = phase->transform(new LShiftLNode(add1->in(1), phase->intcon(con - add1Con)));
1352             return new AndLNode(lshift, phase->longcon(java_negate(jlong(CONST64(1) << con))));
1353           } else {
1354             assert(con < add1Con, "must be (%d < %d)", con, add1Con);
1355             // Creates "(x >> (C1 - C2)) & -(1 << C2)"
1356 
1357             // Handle logical and arithmetic shifts
1358             Node* rshift;
1359             if (add1_op == Op_RShiftL) {
1360               rshift = phase->transform(new RShiftLNode(add1->in(1), phase->intcon(add1Con - con)));
1361             } else {
1362               rshift = phase->transform(new URShiftLNode(add1->in(1), phase->intcon(add1Con - con)));
1363             }
1364 
1365             return new AndLNode(rshift, phase->longcon(java_negate(jlong(CONST64(1) << con))));
1366           }
1367         } else {
1368           phase->record_for_igvn(this);
1369         }
1370       }
1371     }
1372   }
1373 
1374   // Check for "((x >> C1) & Y) << C2"
1375   if (add1_op == Op_AndL) {
1376     Node* add2 = add1->in(1);
1377     int add2_op = add2->Opcode();
1378     if (add2_op == Op_RShiftL || add2_op == Op_URShiftL) {
1379       // Special case C1 == C2, which just masks off low bits
1380       if (add2->in(2) == in(2)) {
1381         // Convert to "(x & (Y << C2))"
1382         Node* y_sh = phase->transform(new LShiftLNode(add1->in(2), phase->intcon(con)));
1383         return new AndLNode(add2->in(1), y_sh);
1384       }
1385 
1386       int add2Con = 0;
1387       const_shift_count(phase, add2, &add2Con);
1388       if (add2Con > 0 && add2Con < BitsPerJavaLong) {
1389         if (phase->is_IterGVN()) {
1390           // Convert to "((x >> C1) << C2) & (Y << C2)"
1391 
1392           // Make "(x >> C1) << C2", which will get folded away by the rule above
1393           Node* x_sh = phase->transform(new LShiftLNode(add2, phase->intcon(con)));
1394           // Make "Y << C2", which will simplify when Y is a constant
1395           Node* y_sh = phase->transform(new LShiftLNode(add1->in(2), phase->intcon(con)));
1396 
1397           return new AndLNode(x_sh, y_sh);
1398         } else {
1399           phase->record_for_igvn(this);
1400         }
1401       }
1402     }
1403   }
1404 
1405   // Check for ((x & ((CONST64(1)<<(64-c0))-1)) << c0) which ANDs off high bits
1406   // before shifting them away.
1407   const jlong bits_mask = jlong(max_julong >> con);
1408   if( add1_op == Op_AndL &&
1409       phase->type(add1->in(2)) == TypeLong::make( bits_mask ) )
1410     return new LShiftLNode( add1->in(1), in(2) );
1411 
1412   // Performs:
1413   // (X << con1) << con2 ==> X << (con1 + con2)
1414   Node* doubleShift = collapse_nested_shift_left(phase, this, con, T_LONG);
1415   if (doubleShift != nullptr) {
1416     return doubleShift;
1417   }
1418 
1419   return nullptr;
1420 }
1421 
1422 //------------------------------Value------------------------------------------
1423 // A LShiftLNode shifts its input2 left by input1 amount.
1424 const Type* LShiftLNode::Value(PhaseGVN* phase) const {
1425   const Type *t1 = phase->type( in(1) );
1426   const Type *t2 = phase->type( in(2) );
1427   // Either input is TOP ==> the result is TOP
1428   if( t1 == Type::TOP ) return Type::TOP;
1429   if( t2 == Type::TOP ) return Type::TOP;
1430 
1431   // Left input is ZERO ==> the result is ZERO.
1432   if( t1 == TypeLong::ZERO ) return TypeLong::ZERO;
1433   // Shift by zero does nothing
1434   if( t2 == TypeInt::ZERO ) return t1;
1435 
1436   // Either input is BOTTOM ==> the result is BOTTOM
1437   if( (t1 == TypeLong::LONG) || (t2 == TypeInt::INT) ||
1438       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
1439     return TypeLong::LONG;
1440 
1441   const TypeLong *r1 = t1->is_long(); // Handy access
1442   const TypeInt  *r2 = t2->is_int();  // Handy access
1443 
1444   if (!r2->is_con())
1445     return TypeLong::LONG;
1446 
1447   uint shift = r2->get_con();
1448   shift &= BitsPerJavaLong - 1;  // semantics of Java shifts
1449   // Shift by a multiple of 64 does nothing:
1450   if (shift == 0)  return t1;
1451 
1452   // If the shift is a constant, shift the bounds of the type,
1453   // unless this could lead to an overflow.
1454   if (!r1->is_con()) {
1455     jlong lo = r1->_lo, hi = r1->_hi;
1456     if (((lo << shift) >> shift) == lo &&
1457         ((hi << shift) >> shift) == hi) {
1458       // No overflow.  The range shifts up cleanly.
1459       return TypeLong::make((jlong)lo << (jint)shift,
1460                             (jlong)hi << (jint)shift,
1461                             MAX2(r1->_widen,r2->_widen));
1462     }
1463     return TypeLong::LONG;
1464   }
1465 
1466   return TypeLong::make( (jlong)r1->get_con() << (jint)shift );
1467 }
1468 
1469 RShiftNode* RShiftNode::make(Node* in1, Node* in2, BasicType bt) {
1470   switch (bt) {
1471     case T_INT:
1472       return new RShiftINode(in1, in2);
1473     case T_LONG:
1474       return new RShiftLNode(in1, in2);
1475     default:
1476       fatal("Not implemented for %s", type2name(bt));
1477   }
1478   return nullptr;
1479 }
1480 
1481 
1482 //=============================================================================
1483 //------------------------------Identity---------------------------------------
1484 Node* RShiftNode::IdentityIL(PhaseGVN* phase, BasicType bt) {
1485   int count = 0;
1486   if (const_shift_count(phase, this, &count)) {
1487     if ((count & (bits_per_java_integer(bt) - 1)) == 0) {
1488       // Shift by a multiple of 32/64 does nothing
1489       return in(1);
1490     }
1491     // Check for useless sign-masking
1492     if (in(1)->Opcode() == Op_LShift(bt) &&
1493         in(1)->req() == 3 &&
1494         in(1)->in(2) == in(2)) {
1495       count &= bits_per_java_integer(bt) - 1; // semantics of Java shifts
1496       // Compute masks for which this shifting doesn't change
1497       jlong lo = (CONST64(-1) << (bits_per_java_integer(bt) - ((uint)count)-1)); // FFFF8000
1498       jlong hi = ~lo;                                                            // 00007FFF
1499       const TypeInteger* t11 = phase->type(in(1)->in(1))->isa_integer(bt);
1500       if (t11 == nullptr) {
1501         return this;
1502       }
1503       // Does actual value fit inside of mask?
1504       if (lo <= t11->lo_as_long() && t11->hi_as_long() <= hi) {
1505         return in(1)->in(1);      // Then shifting is a nop
1506       }
1507     }
1508   }
1509   return this;
1510 }
1511 
1512 Node* RShiftINode::Identity(PhaseGVN* phase) {
1513   return IdentityIL(phase, T_INT);
1514 }
1515 
1516 Node* RShiftNode::IdealIL(PhaseGVN* phase, bool can_reshape, BasicType bt) {
1517   // Inputs may be TOP if they are dead.
1518   const TypeInteger* t1 = phase->type(in(1))->isa_integer(bt);
1519   if (t1 == nullptr) {
1520     return NodeSentinel;        // Left input is an integer
1521   }
1522   int shift = mask_and_replace_shift_amount(phase, this, bits_per_java_integer(bt));
1523   if (shift == 0) {
1524     return NodeSentinel;
1525   }
1526 
1527   // Check for (x & 0xFF000000) >> 24, whose mask can be made smaller.
1528   // and convert to (x >> 24) & (0xFF000000 >> 24) = x >> 24
1529   // Such expressions arise normally from shift chains like (byte)(x >> 24).
1530   const Node* and_node = in(1);
1531   if (and_node->Opcode() != Op_And(bt)) {
1532     return nullptr;
1533   }
1534   const TypeInteger* mask_t = phase->type(and_node->in(2))->isa_integer(bt);
1535   if (mask_t != nullptr && mask_t->is_con()) {
1536     jlong maskbits = mask_t->get_con_as_long(bt);
1537     // Convert to "(x >> shift) & (mask >> shift)"
1538     Node* shr_nomask = phase->transform(RShiftNode::make(and_node->in(1), in(2), bt));
1539     return MulNode::make_and(shr_nomask, phase->integercon(maskbits >> shift, bt), bt);
1540   }
1541   return nullptr;
1542 }
1543 
1544 Node* RShiftINode::Ideal(PhaseGVN* phase, bool can_reshape) {
1545   Node* progress = IdealIL(phase, can_reshape, T_INT);
1546   if (progress == NodeSentinel) {
1547     return nullptr;
1548   }
1549   if (progress != nullptr) {
1550     return progress;
1551   }
1552   int shift = mask_and_replace_shift_amount(phase, this, BitsPerJavaInteger);
1553   assert(shift != 0, "handled by IdealIL");
1554 
1555   // Check for "(short[i] <<16)>>16" which simply sign-extends
1556   const Node *shl = in(1);
1557   if (shl->Opcode() != Op_LShiftI) {
1558     return nullptr;
1559   }
1560 
1561   const TypeInt* left_shift_t = phase->type(shl->in(2))->isa_int();
1562   if (left_shift_t == nullptr) {
1563     return nullptr;
1564   }
1565   if (shift == 16 && left_shift_t->is_con(16)) {
1566     Node *ld = shl->in(1);
1567     if (ld->Opcode() == Op_LoadS) {
1568       // Sign extension is just useless here.  Return a RShiftI of zero instead
1569       // returning 'ld' directly.  We cannot return an old Node directly as
1570       // that is the job of 'Identity' calls and Identity calls only work on
1571       // direct inputs ('ld' is an extra Node removed from 'this').  The
1572       // combined optimization requires Identity only return direct inputs.
1573       set_req_X(1, ld, phase);
1574       set_req_X(2, phase->intcon(0), phase);
1575       return this;
1576     }
1577     else if (can_reshape &&
1578              ld->Opcode() == Op_LoadUS &&
1579              ld->outcnt() == 1 && ld->unique_out() == shl)
1580       // Replace zero-extension-load with sign-extension-load
1581       return ld->as_Load()->convert_to_signed_load(*phase);
1582   }
1583 
1584   // Check for "(byte[i] <<24)>>24" which simply sign-extends
1585   if (shift == 24 && left_shift_t->is_con(24)) {
1586     Node *ld = shl->in(1);
1587     if (ld->Opcode() == Op_LoadB) {
1588       // Sign extension is just useless here
1589       set_req_X(1, ld, phase);
1590       set_req_X(2, phase->intcon(0), phase);
1591       return this;
1592     }
1593   }
1594 
1595   return nullptr;
1596 }
1597 
1598 const Type* RShiftNode::ValueIL(PhaseGVN* phase, BasicType bt) const {
1599   const Type* t1 = phase->type(in(1));
1600   const Type* t2 = phase->type(in(2));
1601   // Either input is TOP ==> the result is TOP
1602   if (t1 == Type::TOP) {
1603     return Type::TOP;
1604   }
1605   if (t2 == Type::TOP) {
1606     return Type::TOP;
1607   }
1608 
1609   // Left input is ZERO ==> the result is ZERO.
1610   if (t1 == TypeInteger::zero(bt)) {
1611     return TypeInteger::zero(bt);
1612   }
1613   // Shift by zero does nothing
1614   if (t2 == TypeInt::ZERO) {
1615     return t1;
1616   }
1617 
1618   // Either input is BOTTOM ==> the result is BOTTOM
1619   if (t1 == Type::BOTTOM || t2 == Type::BOTTOM) {
1620     return TypeInteger::bottom(bt);
1621   }
1622 
1623   const TypeInteger* r1 = t1->isa_integer(bt);
1624   const TypeInt* r2 = t2->isa_int();
1625 
1626   // If the shift is a constant, just shift the bounds of the type.
1627   // For example, if the shift is 31/63, we just propagate sign bits.
1628   if (!r1->is_con() && r2->is_con()) {
1629     uint shift = r2->get_con();
1630     shift &= bits_per_java_integer(bt) - 1;  // semantics of Java shifts
1631     // Shift by a multiple of 32/64 does nothing:
1632     if (shift == 0) {
1633       return t1;
1634     }
1635     // Calculate reasonably aggressive bounds for the result.
1636     // This is necessary if we are to correctly type things
1637     // like (x<<24>>24) == ((byte)x).
1638     jlong lo = r1->lo_as_long() >> (jint)shift;
1639     jlong hi = r1->hi_as_long() >> (jint)shift;
1640     assert(lo <= hi, "must have valid bounds");
1641 #ifdef ASSERT
1642    if (bt == T_INT) {
1643      jint lo_verify = checked_cast<jint>(r1->lo_as_long()) >> (jint)shift;
1644      jint hi_verify = checked_cast<jint>(r1->hi_as_long()) >> (jint)shift;
1645      assert((checked_cast<jint>(lo) == lo_verify) && (checked_cast<jint>(hi) == hi_verify), "inconsistent");
1646    }
1647 #endif
1648     const TypeInteger* ti = TypeInteger::make(lo, hi, MAX2(r1->_widen,r2->_widen), bt);
1649 #ifdef ASSERT
1650     // Make sure we get the sign-capture idiom correct.
1651     if (shift == bits_per_java_integer(bt) - 1) {
1652       if (r1->lo_as_long() >= 0) {
1653         assert(ti == TypeInteger::zero(bt),    ">>31/63 of + is  0");
1654       }
1655       if (r1->hi_as_long() <  0) {
1656         assert(ti == TypeInteger::minus_1(bt), ">>31/63 of - is -1");
1657       }
1658     }
1659 #endif
1660     return ti;
1661   }
1662 
1663   if (!r1->is_con() || !r2->is_con()) {
1664     // If the left input is non-negative the result must also be non-negative, regardless of what the right input is.
1665     if (r1->lo_as_long() >= 0) {
1666       return TypeInteger::make(0, r1->hi_as_long(), MAX2(r1->_widen, r2->_widen), bt);
1667     }
1668 
1669     // Conversely, if the left input is negative then the result must be negative.
1670     if (r1->hi_as_long() <= -1) {
1671       return TypeInteger::make(r1->lo_as_long(), -1, MAX2(r1->_widen, r2->_widen), bt);
1672     }
1673 
1674     return TypeInteger::bottom(bt);
1675   }
1676 
1677   // Signed shift right
1678   return TypeInteger::make(r1->get_con_as_long(bt) >> (r2->get_con() & (bits_per_java_integer(bt) - 1)), bt);
1679 }
1680 
1681 const Type* RShiftINode::Value(PhaseGVN* phase) const {
1682   return ValueIL(phase, T_INT);
1683 }
1684 
1685 //=============================================================================
1686 //------------------------------Identity---------------------------------------
1687 Node* RShiftLNode::Identity(PhaseGVN* phase) {
1688   return IdentityIL(phase, T_LONG);
1689 }
1690 
1691 Node* RShiftLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1692   Node* progress = IdealIL(phase, can_reshape, T_LONG);
1693   if (progress == NodeSentinel) {
1694     return nullptr;
1695   }
1696   return progress;
1697 }
1698 
1699 const Type* RShiftLNode::Value(PhaseGVN* phase) const {
1700   return ValueIL(phase, T_LONG);
1701 }
1702 
1703 //=============================================================================
1704 //------------------------------Identity---------------------------------------
1705 Node* URShiftINode::Identity(PhaseGVN* phase) {
1706   int count = 0;
1707   if (const_shift_count(phase, this, &count) && (count & (BitsPerJavaInteger - 1)) == 0) {
1708     // Shift by a multiple of 32 does nothing
1709     return in(1);
1710   }
1711 
1712   // Check for "((x << LogBytesPerWord) + (wordSize-1)) >> LogBytesPerWord" which is just "x".
1713   // Happens during new-array length computation.
1714   // Safe if 'x' is in the range [0..(max_int>>LogBytesPerWord)]
1715   Node *add = in(1);
1716   if (add->Opcode() == Op_AddI) {
1717     const TypeInt *t2 = phase->type(add->in(2))->isa_int();
1718     if (t2 && t2->is_con(wordSize - 1) &&
1719         add->in(1)->Opcode() == Op_LShiftI) {
1720       // Check that shift_counts are LogBytesPerWord.
1721       Node          *lshift_count   = add->in(1)->in(2);
1722       const TypeInt *t_lshift_count = phase->type(lshift_count)->isa_int();
1723       if (t_lshift_count && t_lshift_count->is_con(LogBytesPerWord) &&
1724           t_lshift_count == phase->type(in(2))) {
1725         Node          *x   = add->in(1)->in(1);
1726         const TypeInt *t_x = phase->type(x)->isa_int();
1727         if (t_x != nullptr && 0 <= t_x->_lo && t_x->_hi <= (max_jint>>LogBytesPerWord)) {
1728           return x;
1729         }
1730       }
1731     }
1732   }
1733 
1734   return (phase->type(in(2))->higher_equal(TypeInt::ZERO)) ? in(1) : this;
1735 }
1736 
1737 //------------------------------Ideal------------------------------------------
1738 Node *URShiftINode::Ideal(PhaseGVN *phase, bool can_reshape) {
1739   int con = mask_and_replace_shift_amount(phase, this, BitsPerJavaInteger);
1740   if (con == 0) {
1741     return nullptr;
1742   }
1743 
1744   // We'll be wanting the right-shift amount as a mask of that many bits
1745   const int mask = right_n_bits(BitsPerJavaInteger - con);
1746 
1747   int in1_op = in(1)->Opcode();
1748 
1749   // Check for ((x>>>a)>>>b) and replace with (x>>>(a+b)) when a+b < 32
1750   if( in1_op == Op_URShiftI ) {
1751     const TypeInt *t12 = phase->type( in(1)->in(2) )->isa_int();
1752     if( t12 && t12->is_con() ) { // Right input is a constant
1753       assert( in(1) != in(1)->in(1), "dead loop in URShiftINode::Ideal" );
1754       const int con2 = t12->get_con() & 31; // Shift count is always masked
1755       const int con3 = con+con2;
1756       if( con3 < 32 )           // Only merge shifts if total is < 32
1757         return new URShiftINode( in(1)->in(1), phase->intcon(con3) );
1758     }
1759   }
1760 
1761   // Check for ((x << z) + Y) >>> z.  Replace with x + con>>>z
1762   // The idiom for rounding to a power of 2 is "(Q+(2^z-1)) >>> z".
1763   // If Q is "X << z" the rounding is useless.  Look for patterns like
1764   // ((X<<Z) + Y) >>> Z  and replace with (X + Y>>>Z) & Z-mask.
1765   Node *add = in(1);
1766   const TypeInt *t2 = phase->type(in(2))->isa_int();
1767   if (in1_op == Op_AddI) {
1768     Node *lshl = add->in(1);
1769     if( lshl->Opcode() == Op_LShiftI &&
1770         phase->type(lshl->in(2)) == t2 ) {
1771       Node *y_z = phase->transform( new URShiftINode(add->in(2),in(2)) );
1772       Node *sum = phase->transform( new AddINode( lshl->in(1), y_z ) );
1773       return new AndINode( sum, phase->intcon(mask) );
1774     }
1775   }
1776 
1777   // Check for (x & mask) >>> z.  Replace with (x >>> z) & (mask >>> z)
1778   // This shortens the mask.  Also, if we are extracting a high byte and
1779   // storing it to a buffer, the mask will be removed completely.
1780   Node *andi = in(1);
1781   if( in1_op == Op_AndI ) {
1782     const TypeInt *t3 = phase->type( andi->in(2) )->isa_int();
1783     if( t3 && t3->is_con() ) { // Right input is a constant
1784       jint mask2 = t3->get_con();
1785       mask2 >>= con;  // *signed* shift downward (high-order zeroes do not help)
1786       Node *newshr = phase->transform( new URShiftINode(andi->in(1), in(2)) );
1787       return new AndINode(newshr, phase->intcon(mask2));
1788       // The negative values are easier to materialize than positive ones.
1789       // A typical case from address arithmetic is ((x & ~15) >> 4).
1790       // It's better to change that to ((x >> 4) & ~0) versus
1791       // ((x >> 4) & 0x0FFFFFFF).  The difference is greatest in LP64.
1792     }
1793   }
1794 
1795   // Check for "(X << z ) >>> z" which simply zero-extends
1796   Node *shl = in(1);
1797   if( in1_op == Op_LShiftI &&
1798       phase->type(shl->in(2)) == t2 )
1799     return new AndINode( shl->in(1), phase->intcon(mask) );
1800 
1801   // Check for (x >> n) >>> 31. Replace with (x >>> 31)
1802   Node *shr = in(1);
1803   if ( in1_op == Op_RShiftI ) {
1804     Node *in11 = shr->in(1);
1805     Node *in12 = shr->in(2);
1806     const TypeInt *t11 = phase->type(in11)->isa_int();
1807     const TypeInt *t12 = phase->type(in12)->isa_int();
1808     if ( t11 && t2 && t2->is_con(31) && t12 && t12->is_con() ) {
1809       return new URShiftINode(in11, phase->intcon(31));
1810     }
1811   }
1812 
1813   return nullptr;
1814 }
1815 
1816 //------------------------------Value------------------------------------------
1817 // A URShiftINode shifts its input2 right by input1 amount.
1818 const Type* URShiftINode::Value(PhaseGVN* phase) const {
1819   // (This is a near clone of RShiftINode::Value.)
1820   const Type *t1 = phase->type( in(1) );
1821   const Type *t2 = phase->type( in(2) );
1822   // Either input is TOP ==> the result is TOP
1823   if( t1 == Type::TOP ) return Type::TOP;
1824   if( t2 == Type::TOP ) return Type::TOP;
1825 
1826   // Left input is ZERO ==> the result is ZERO.
1827   if( t1 == TypeInt::ZERO ) return TypeInt::ZERO;
1828   // Shift by zero does nothing
1829   if( t2 == TypeInt::ZERO ) return t1;
1830 
1831   // Either input is BOTTOM ==> the result is BOTTOM
1832   if (t1 == Type::BOTTOM || t2 == Type::BOTTOM)
1833     return TypeInt::INT;
1834 
1835   if (t2 == TypeInt::INT)
1836     return TypeInt::INT;
1837 
1838   const TypeInt *r1 = t1->is_int();     // Handy access
1839   const TypeInt *r2 = t2->is_int();     // Handy access
1840 
1841   if (r2->is_con()) {
1842     uint shift = r2->get_con();
1843     shift &= BitsPerJavaInteger-1;  // semantics of Java shifts
1844     // Shift by a multiple of 32 does nothing:
1845     if (shift == 0)  return t1;
1846     // Calculate reasonably aggressive bounds for the result.
1847     jint lo = (juint)r1->_lo >> (juint)shift;
1848     jint hi = (juint)r1->_hi >> (juint)shift;
1849     if (r1->_hi >= 0 && r1->_lo < 0) {
1850       // If the type has both negative and positive values,
1851       // there are two separate sub-domains to worry about:
1852       // The positive half and the negative half.
1853       jint neg_lo = lo;
1854       jint neg_hi = (juint)-1 >> (juint)shift;
1855       jint pos_lo = (juint) 0 >> (juint)shift;
1856       jint pos_hi = hi;
1857       lo = MIN2(neg_lo, pos_lo);  // == 0
1858       hi = MAX2(neg_hi, pos_hi);  // == -1 >>> shift;
1859     }
1860     assert(lo <= hi, "must have valid bounds");
1861     const TypeInt* ti = TypeInt::make(lo, hi, MAX2(r1->_widen,r2->_widen));
1862     #ifdef ASSERT
1863     // Make sure we get the sign-capture idiom correct.
1864     if (shift == BitsPerJavaInteger-1) {
1865       if (r1->_lo >= 0) assert(ti == TypeInt::ZERO, ">>>31 of + is 0");
1866       if (r1->_hi < 0)  assert(ti == TypeInt::ONE,  ">>>31 of - is +1");
1867     }
1868     #endif
1869     return ti;
1870   }
1871 
1872   //
1873   // Do not support shifted oops in info for GC
1874   //
1875   // else if( t1->base() == Type::InstPtr ) {
1876   //
1877   //   const TypeInstPtr *o = t1->is_instptr();
1878   //   if( t1->singleton() )
1879   //     return TypeInt::make( ((uint32_t)o->const_oop() + o->_offset) >> shift );
1880   // }
1881   // else if( t1->base() == Type::KlassPtr ) {
1882   //   const TypeKlassPtr *o = t1->is_klassptr();
1883   //   if( t1->singleton() )
1884   //     return TypeInt::make( ((uint32_t)o->const_oop() + o->_offset) >> shift );
1885   // }
1886 
1887   return TypeInt::INT;
1888 }
1889 
1890 //=============================================================================
1891 //------------------------------Identity---------------------------------------
1892 Node* URShiftLNode::Identity(PhaseGVN* phase) {
1893   int count = 0;
1894   if (const_shift_count(phase, this, &count) && (count & (BitsPerJavaLong - 1)) == 0) {
1895     // Shift by a multiple of 64 does nothing
1896     return in(1);
1897   }
1898   return this;
1899 }
1900 
1901 //------------------------------Ideal------------------------------------------
1902 Node *URShiftLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1903   int con = mask_and_replace_shift_amount(phase, this, BitsPerJavaLong);
1904   if (con == 0) {
1905     return nullptr;
1906   }
1907 
1908   // We'll be wanting the right-shift amount as a mask of that many bits
1909   const jlong mask = jlong(max_julong >> con);
1910 
1911   // Check for ((x << z) + Y) >>> z.  Replace with x + con>>>z
1912   // The idiom for rounding to a power of 2 is "(Q+(2^z-1)) >>> z".
1913   // If Q is "X << z" the rounding is useless.  Look for patterns like
1914   // ((X<<Z) + Y) >>> Z  and replace with (X + Y>>>Z) & Z-mask.
1915   Node *add = in(1);
1916   const TypeInt *t2 = phase->type(in(2))->isa_int();
1917   if (add->Opcode() == Op_AddL) {
1918     Node *lshl = add->in(1);
1919     if( lshl->Opcode() == Op_LShiftL &&
1920         phase->type(lshl->in(2)) == t2 ) {
1921       Node *y_z = phase->transform( new URShiftLNode(add->in(2),in(2)) );
1922       Node *sum = phase->transform( new AddLNode( lshl->in(1), y_z ) );
1923       return new AndLNode( sum, phase->longcon(mask) );
1924     }
1925   }
1926 
1927   // Check for (x & mask) >>> z.  Replace with (x >>> z) & (mask >>> z)
1928   // This shortens the mask.  Also, if we are extracting a high byte and
1929   // storing it to a buffer, the mask will be removed completely.
1930   Node *andi = in(1);
1931   if( andi->Opcode() == Op_AndL ) {
1932     const TypeLong *t3 = phase->type( andi->in(2) )->isa_long();
1933     if( t3 && t3->is_con() ) { // Right input is a constant
1934       jlong mask2 = t3->get_con();
1935       mask2 >>= con;  // *signed* shift downward (high-order zeroes do not help)
1936       Node *newshr = phase->transform( new URShiftLNode(andi->in(1), in(2)) );
1937       return new AndLNode(newshr, phase->longcon(mask2));
1938     }
1939   }
1940 
1941   // Check for "(X << z ) >>> z" which simply zero-extends
1942   Node *shl = in(1);
1943   if( shl->Opcode() == Op_LShiftL &&
1944       phase->type(shl->in(2)) == t2 )
1945     return new AndLNode( shl->in(1), phase->longcon(mask) );
1946 
1947   // Check for (x >> n) >>> 63. Replace with (x >>> 63)
1948   Node *shr = in(1);
1949   if ( shr->Opcode() == Op_RShiftL ) {
1950     Node *in11 = shr->in(1);
1951     Node *in12 = shr->in(2);
1952     const TypeLong *t11 = phase->type(in11)->isa_long();
1953     const TypeInt *t12 = phase->type(in12)->isa_int();
1954     if ( t11 && t2 && t2->is_con(63) && t12 && t12->is_con() ) {
1955       return new URShiftLNode(in11, phase->intcon(63));
1956     }
1957   }
1958   return nullptr;
1959 }
1960 
1961 //------------------------------Value------------------------------------------
1962 // A URShiftINode shifts its input2 right by input1 amount.
1963 const Type* URShiftLNode::Value(PhaseGVN* phase) const {
1964   // (This is a near clone of RShiftLNode::Value.)
1965   const Type *t1 = phase->type( in(1) );
1966   const Type *t2 = phase->type( in(2) );
1967   // Either input is TOP ==> the result is TOP
1968   if( t1 == Type::TOP ) return Type::TOP;
1969   if( t2 == Type::TOP ) return Type::TOP;
1970 
1971   // Left input is ZERO ==> the result is ZERO.
1972   if( t1 == TypeLong::ZERO ) return TypeLong::ZERO;
1973   // Shift by zero does nothing
1974   if( t2 == TypeInt::ZERO ) return t1;
1975 
1976   // Either input is BOTTOM ==> the result is BOTTOM
1977   if (t1 == Type::BOTTOM || t2 == Type::BOTTOM)
1978     return TypeLong::LONG;
1979 
1980   if (t2 == TypeInt::INT)
1981     return TypeLong::LONG;
1982 
1983   const TypeLong *r1 = t1->is_long(); // Handy access
1984   const TypeInt  *r2 = t2->is_int (); // Handy access
1985 
1986   if (r2->is_con()) {
1987     uint shift = r2->get_con();
1988     shift &= BitsPerJavaLong - 1;  // semantics of Java shifts
1989     // Shift by a multiple of 64 does nothing:
1990     if (shift == 0)  return t1;
1991     // Calculate reasonably aggressive bounds for the result.
1992     jlong lo = (julong)r1->_lo >> (juint)shift;
1993     jlong hi = (julong)r1->_hi >> (juint)shift;
1994     if (r1->_hi >= 0 && r1->_lo < 0) {
1995       // If the type has both negative and positive values,
1996       // there are two separate sub-domains to worry about:
1997       // The positive half and the negative half.
1998       jlong neg_lo = lo;
1999       jlong neg_hi = (julong)-1 >> (juint)shift;
2000       jlong pos_lo = (julong) 0 >> (juint)shift;
2001       jlong pos_hi = hi;
2002       //lo = MIN2(neg_lo, pos_lo);  // == 0
2003       lo = neg_lo < pos_lo ? neg_lo : pos_lo;
2004       //hi = MAX2(neg_hi, pos_hi);  // == -1 >>> shift;
2005       hi = neg_hi > pos_hi ? neg_hi : pos_hi;
2006     }
2007     assert(lo <= hi, "must have valid bounds");
2008     const TypeLong* tl = TypeLong::make(lo, hi, MAX2(r1->_widen,r2->_widen));
2009     #ifdef ASSERT
2010     // Make sure we get the sign-capture idiom correct.
2011     if (shift == BitsPerJavaLong - 1) {
2012       if (r1->_lo >= 0) assert(tl == TypeLong::ZERO, ">>>63 of + is 0");
2013       if (r1->_hi < 0)  assert(tl == TypeLong::ONE,  ">>>63 of - is +1");
2014     }
2015     #endif
2016     return tl;
2017   }
2018 
2019   return TypeLong::LONG;                // Give up
2020 }
2021 
2022 //=============================================================================
2023 //------------------------------Ideal------------------------------------------
2024 Node* FmaNode::Ideal(PhaseGVN* phase, bool can_reshape) {
2025   // We canonicalize the node by converting "(-a)*b+c" into "b*(-a)+c"
2026   // This reduces the number of rules in the matcher, as we only need to check
2027   // for negations on the second argument, and not the symmetric case where
2028   // the first argument is negated.
2029   if (in(1)->is_Neg() && !in(2)->is_Neg()) {
2030     swap_edges(1, 2);
2031     return this;
2032   }
2033   return nullptr;
2034 }
2035 
2036 //=============================================================================
2037 //------------------------------Value------------------------------------------
2038 const Type* FmaDNode::Value(PhaseGVN* phase) const {
2039   const Type *t1 = phase->type(in(1));
2040   if (t1 == Type::TOP) return Type::TOP;
2041   if (t1->base() != Type::DoubleCon) return Type::DOUBLE;
2042   const Type *t2 = phase->type(in(2));
2043   if (t2 == Type::TOP) return Type::TOP;
2044   if (t2->base() != Type::DoubleCon) return Type::DOUBLE;
2045   const Type *t3 = phase->type(in(3));
2046   if (t3 == Type::TOP) return Type::TOP;
2047   if (t3->base() != Type::DoubleCon) return Type::DOUBLE;
2048 #ifndef __STDC_IEC_559__
2049   return Type::DOUBLE;
2050 #else
2051   double d1 = t1->getd();
2052   double d2 = t2->getd();
2053   double d3 = t3->getd();
2054   return TypeD::make(fma(d1, d2, d3));
2055 #endif
2056 }
2057 
2058 //=============================================================================
2059 //------------------------------Value------------------------------------------
2060 const Type* FmaFNode::Value(PhaseGVN* phase) const {
2061   const Type *t1 = phase->type(in(1));
2062   if (t1 == Type::TOP) return Type::TOP;
2063   if (t1->base() != Type::FloatCon) return Type::FLOAT;
2064   const Type *t2 = phase->type(in(2));
2065   if (t2 == Type::TOP) return Type::TOP;
2066   if (t2->base() != Type::FloatCon) return Type::FLOAT;
2067   const Type *t3 = phase->type(in(3));
2068   if (t3 == Type::TOP) return Type::TOP;
2069   if (t3->base() != Type::FloatCon) return Type::FLOAT;
2070 #ifndef __STDC_IEC_559__
2071   return Type::FLOAT;
2072 #else
2073   float f1 = t1->getf();
2074   float f2 = t2->getf();
2075   float f3 = t3->getf();
2076   return TypeF::make(fma(f1, f2, f3));
2077 #endif
2078 }
2079 
2080 //=============================================================================
2081 //------------------------------Value------------------------------------------
2082 const Type* FmaHFNode::Value(PhaseGVN* phase) const {
2083   const Type* t1 = phase->type(in(1));
2084   if (t1 == Type::TOP) { return Type::TOP; }
2085   if (t1->base() != Type::HalfFloatCon) { return Type::HALF_FLOAT; }
2086   const Type* t2 = phase->type(in(2));
2087   if (t2 == Type::TOP) { return Type::TOP; }
2088   if (t2->base() != Type::HalfFloatCon) { return Type::HALF_FLOAT; }
2089   const Type* t3 = phase->type(in(3));
2090   if (t3 == Type::TOP) { return Type::TOP; }
2091   if (t3->base() != Type::HalfFloatCon) { return Type::HALF_FLOAT; }
2092 #ifndef __STDC_IEC_559__
2093   return Type::HALF_FLOAT;
2094 #else
2095   float f1 = t1->getf();
2096   float f2 = t2->getf();
2097   float f3 = t3->getf();
2098   return TypeH::make(fma(f1, f2, f3));
2099 #endif
2100 }
2101 
2102 //=============================================================================
2103 //------------------------------hash-------------------------------------------
2104 // Hash function for MulAddS2INode.  Operation is commutative with commutative pairs.
2105 // The hash function must return the same value when edge swapping is performed.
2106 uint MulAddS2INode::hash() const {
2107   return (uintptr_t)in(1) + (uintptr_t)in(2) + (uintptr_t)in(3) + (uintptr_t)in(4) + Opcode();
2108 }
2109 
2110 //------------------------------Rotate Operations ------------------------------
2111 
2112 Node* RotateLeftNode::Identity(PhaseGVN* phase) {
2113   const Type* t1 = phase->type(in(1));
2114   if (t1 == Type::TOP) {
2115     return this;
2116   }
2117   int count = 0;
2118   assert(t1->isa_int() || t1->isa_long(), "Unexpected type");
2119   int mask = (t1->isa_int() ? BitsPerJavaInteger : BitsPerJavaLong) - 1;
2120   if (const_shift_count(phase, this, &count) && (count & mask) == 0) {
2121     // Rotate by a multiple of 32/64 does nothing
2122     return in(1);
2123   }
2124   return this;
2125 }
2126 
2127 const Type* RotateLeftNode::Value(PhaseGVN* phase) const {
2128   const Type* t1 = phase->type(in(1));
2129   const Type* t2 = phase->type(in(2));
2130   // Either input is TOP ==> the result is TOP
2131   if (t1 == Type::TOP || t2 == Type::TOP) {
2132     return Type::TOP;
2133   }
2134 
2135   if (t1->isa_int()) {
2136     const TypeInt* r1 = t1->is_int();
2137     const TypeInt* r2 = t2->is_int();
2138 
2139     // Left input is ZERO ==> the result is ZERO.
2140     if (r1 == TypeInt::ZERO) {
2141       return TypeInt::ZERO;
2142     }
2143     // Rotate by zero does nothing
2144     if (r2 == TypeInt::ZERO) {
2145       return r1;
2146     }
2147     if (r1->is_con() && r2->is_con()) {
2148       juint r1_con = (juint)r1->get_con();
2149       juint shift = (juint)(r2->get_con()) & (juint)(BitsPerJavaInteger - 1); // semantics of Java shifts
2150       return TypeInt::make((r1_con << shift) | (r1_con >> (32 - shift)));
2151     }
2152     return TypeInt::INT;
2153   } else {
2154     assert(t1->isa_long(), "Type must be a long");
2155     const TypeLong* r1 = t1->is_long();
2156     const TypeInt*  r2 = t2->is_int();
2157 
2158     // Left input is ZERO ==> the result is ZERO.
2159     if (r1 == TypeLong::ZERO) {
2160       return TypeLong::ZERO;
2161     }
2162     // Rotate by zero does nothing
2163     if (r2 == TypeInt::ZERO) {
2164       return r1;
2165     }
2166     if (r1->is_con() && r2->is_con()) {
2167       julong r1_con = (julong)r1->get_con();
2168       julong shift = (julong)(r2->get_con()) & (julong)(BitsPerJavaLong - 1); // semantics of Java shifts
2169       return TypeLong::make((r1_con << shift) | (r1_con >> (64 - shift)));
2170     }
2171     return TypeLong::LONG;
2172   }
2173 }
2174 
2175 Node* RotateLeftNode::Ideal(PhaseGVN *phase, bool can_reshape) {
2176   const Type* t1 = phase->type(in(1));
2177   const Type* t2 = phase->type(in(2));
2178   if (t2->isa_int() && t2->is_int()->is_con()) {
2179     if (t1->isa_int()) {
2180       int lshift = t2->is_int()->get_con() & 31;
2181       return new RotateRightNode(in(1), phase->intcon(32 - (lshift & 31)), TypeInt::INT);
2182     } else if (t1 != Type::TOP) {
2183       assert(t1->isa_long(), "Type must be a long");
2184       int lshift = t2->is_int()->get_con() & 63;
2185       return new RotateRightNode(in(1), phase->intcon(64 - (lshift & 63)), TypeLong::LONG);
2186     }
2187   }
2188   return nullptr;
2189 }
2190 
2191 Node* RotateRightNode::Identity(PhaseGVN* phase) {
2192   const Type* t1 = phase->type(in(1));
2193   if (t1 == Type::TOP) {
2194     return this;
2195   }
2196   int count = 0;
2197   assert(t1->isa_int() || t1->isa_long(), "Unexpected type");
2198   int mask = (t1->isa_int() ? BitsPerJavaInteger : BitsPerJavaLong) - 1;
2199   if (const_shift_count(phase, this, &count) && (count & mask) == 0) {
2200     // Rotate by a multiple of 32/64 does nothing
2201     return in(1);
2202   }
2203   return this;
2204 }
2205 
2206 const Type* RotateRightNode::Value(PhaseGVN* phase) const {
2207   const Type* t1 = phase->type(in(1));
2208   const Type* t2 = phase->type(in(2));
2209   // Either input is TOP ==> the result is TOP
2210   if (t1 == Type::TOP || t2 == Type::TOP) {
2211     return Type::TOP;
2212   }
2213 
2214   if (t1->isa_int()) {
2215     const TypeInt* r1 = t1->is_int();
2216     const TypeInt* r2 = t2->is_int();
2217 
2218     // Left input is ZERO ==> the result is ZERO.
2219     if (r1 == TypeInt::ZERO) {
2220       return TypeInt::ZERO;
2221     }
2222     // Rotate by zero does nothing
2223     if (r2 == TypeInt::ZERO) {
2224       return r1;
2225     }
2226     if (r1->is_con() && r2->is_con()) {
2227       juint r1_con = (juint)r1->get_con();
2228       juint shift = (juint)(r2->get_con()) & (juint)(BitsPerJavaInteger - 1); // semantics of Java shifts
2229       return TypeInt::make((r1_con >> shift) | (r1_con << (32 - shift)));
2230     }
2231     return TypeInt::INT;
2232   } else {
2233     assert(t1->isa_long(), "Type must be a long");
2234     const TypeLong* r1 = t1->is_long();
2235     const TypeInt*  r2 = t2->is_int();
2236     // Left input is ZERO ==> the result is ZERO.
2237     if (r1 == TypeLong::ZERO) {
2238       return TypeLong::ZERO;
2239     }
2240     // Rotate by zero does nothing
2241     if (r2 == TypeInt::ZERO) {
2242       return r1;
2243     }
2244     if (r1->is_con() && r2->is_con()) {
2245       julong r1_con = (julong)r1->get_con();
2246       julong shift = (julong)(r2->get_con()) & (julong)(BitsPerJavaLong - 1); // semantics of Java shifts
2247       return TypeLong::make((r1_con >> shift) | (r1_con << (64 - shift)));
2248     }
2249     return TypeLong::LONG;
2250   }
2251 }
2252 
2253 //------------------------------ Sum & Mask ------------------------------
2254 
2255 // Returns a lower bound on the number of trailing zeros in expr.
2256 static jint AndIL_min_trailing_zeros(const PhaseGVN* phase, const Node* expr, BasicType bt) {
2257   const TypeInteger* type = phase->type(expr)->isa_integer(bt);
2258   if (type == nullptr) {
2259     return 0;
2260   }
2261 
2262   expr = expr->uncast();
2263   type = phase->type(expr)->isa_integer(bt);
2264   if (type == nullptr) {
2265     return 0;
2266   }
2267 
2268   if (type->is_con()) {
2269     jlong con = type->get_con_as_long(bt);
2270     return con == 0L ? (type2aelembytes(bt) * BitsPerByte) : count_trailing_zeros(con);
2271   }
2272 
2273   if (expr->Opcode() == Op_ConvI2L) {
2274     expr = expr->in(1)->uncast();
2275     bt = T_INT;
2276     type = phase->type(expr)->isa_int();
2277   }
2278 
2279   // Pattern: expr = (x << shift)
2280   if (expr->Opcode() == Op_LShift(bt)) {
2281     const TypeInt* shift_t = phase->type(expr->in(2))->isa_int();
2282     if (shift_t == nullptr || !shift_t->is_con()) {
2283       return 0;
2284     }
2285     // We need to truncate the shift, as it may not have been canonicalized yet.
2286     // T_INT:  0..31 -> shift_mask = 4 * 8 - 1 = 31
2287     // T_LONG: 0..63 -> shift_mask = 8 * 8 - 1 = 63
2288     // (JLS: "Shift Operators")
2289     jint shift_mask = type2aelembytes(bt) * BitsPerByte - 1;
2290     return shift_t->get_con() & shift_mask;
2291   }
2292 
2293   return 0;
2294 }
2295 
2296 // Checks whether expr is neutral additive element (zero) under mask,
2297 // i.e. whether an expression of the form:
2298 //   (AndX (AddX (expr addend) mask)
2299 //   (expr + addend) & mask
2300 // is equivalent to
2301 //   (AndX addend mask)
2302 //   addend & mask
2303 // for any addend.
2304 // (The X in AndX must be I or L, depending on bt).
2305 //
2306 // We check for the sufficient condition when the lowest set bit in expr is higher than
2307 // the highest set bit in mask, i.e.:
2308 // expr: eeeeee0000000000000
2309 // mask: 000000mmmmmmmmmmmmm
2310 //             <--w bits--->
2311 // We do not test for other cases.
2312 //
2313 // Correctness:
2314 //   Given "expr" with at least "w" trailing zeros,
2315 //   let "mod = 2^w", "suffix_mask = mod - 1"
2316 //
2317 //   Since "mask" only has bits set where "suffix_mask" does, we have:
2318 //     mask = suffix_mask & mask     (SUFFIX_MASK)
2319 //
2320 //   And since expr only has bits set above w, and suffix_mask only below:
2321 //     expr & suffix_mask == 0     (NO_BIT_OVERLAP)
2322 //
2323 //   From unsigned modular arithmetic (with unsigned modulo %), and since mod is
2324 //   a power of 2, and we are computing in a ring of powers of 2, we know that
2325 //     (x + y) % mod         = (x % mod         + y) % mod
2326 //     (x + y) & suffix_mask = (x & suffix_mask + y) & suffix_mask       (MOD_ARITH)
2327 //
2328 //   We can now prove the equality:
2329 //     (expr               + addend)               & mask
2330 //   = (expr               + addend) & suffix_mask & mask    (SUFFIX_MASK)
2331 //   = (expr & suffix_mask + addend) & suffix_mask & mask    (MOD_ARITH)
2332 //   = (0                  + addend) & suffix_mask & mask    (NO_BIT_OVERLAP)
2333 //   =                       addend                & mask    (SUFFIX_MASK)
2334 //
2335 // Hence, an expr with at least w trailing zeros is a neutral additive element under any mask with bit width w.
2336 static bool AndIL_is_zero_element_under_mask(const PhaseGVN* phase, const Node* expr, const Node* mask, BasicType bt) {
2337   // When the mask is negative, it has the most significant bit set.
2338   const TypeInteger* mask_t = phase->type(mask)->isa_integer(bt);
2339   if (mask_t == nullptr || mask_t->lo_as_long() < 0) {
2340     return false;
2341   }
2342 
2343   // When the mask is constant zero, we defer to MulNode::Value to eliminate the entire AndX operation.
2344   if (mask_t->hi_as_long() == 0) {
2345     assert(mask_t->lo_as_long() == 0, "checked earlier");
2346     return false;
2347   }
2348 
2349   jint mask_bit_width = BitsPerLong - count_leading_zeros(mask_t->hi_as_long());
2350   jint expr_trailing_zeros = AndIL_min_trailing_zeros(phase, expr, bt);
2351   return expr_trailing_zeros >= mask_bit_width;
2352 }
2353 
2354 // Reduces the pattern:
2355 //   (AndX (AddX add1 add2) mask)
2356 // to
2357 //   (AndX add1 mask), if add2 is neutral wrt mask (see above), and vice versa.
2358 Node* MulNode::AndIL_sum_and_mask(PhaseGVN* phase, BasicType bt) {
2359   Node* add = in(1);
2360   Node* mask = in(2);
2361   int addidx = 0;
2362   if (add->Opcode() == Op_Add(bt)) {
2363     addidx = 1;
2364   } else if (mask->Opcode() == Op_Add(bt)) {
2365     mask = add;
2366     addidx = 2;
2367     add = in(addidx);
2368   }
2369   if (addidx > 0) {
2370     Node* add1 = add->in(1);
2371     Node* add2 = add->in(2);
2372     if (AndIL_is_zero_element_under_mask(phase, add1, mask, bt)) {
2373       set_req_X(addidx, add2, phase);
2374       return this;
2375     } else if (AndIL_is_zero_element_under_mask(phase, add2, mask, bt)) {
2376       set_req_X(addidx, add1, phase);
2377       return this;
2378     }
2379   }
2380   return nullptr;
2381 }