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