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