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