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