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