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