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