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