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