1 /*
   2  * Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "memory/allocation.inline.hpp"
  27 #include "opto/addnode.hpp"
  28 #include "opto/connode.hpp"
  29 #include "opto/convertnode.hpp"
  30 #include "opto/memnode.hpp"
  31 #include "opto/mulnode.hpp"
  32 #include "opto/phaseX.hpp"
  33 #include "opto/subnode.hpp"
  34 #include "utilities/powerOfTwo.hpp"
  35 
  36 // Portions of code courtesy of Clifford Click
  37 
  38 
  39 //=============================================================================
  40 //------------------------------hash-------------------------------------------
  41 // Hash function over MulNodes.  Needs to be commutative; i.e., I swap
  42 // (commute) inputs to MulNodes willy-nilly so the hash function must return
  43 // the same value in the presence of edge swapping.
  44 uint MulNode::hash() const {
  45   return (uintptr_t)in(1) + (uintptr_t)in(2) + Opcode();
  46 }
  47 
  48 //------------------------------Identity---------------------------------------
  49 // Multiplying a one preserves the other argument
  50 Node* MulNode::Identity(PhaseGVN* phase) {
  51   const Type *one = mul_id();  // The multiplicative identity
  52   if( phase->type( in(1) )->higher_equal( one ) ) return in(2);
  53   if( phase->type( in(2) )->higher_equal( one ) ) return in(1);
  54 
  55   return this;
  56 }
  57 
  58 //------------------------------Ideal------------------------------------------
  59 // We also canonicalize the Node, moving constants to the right input,
  60 // and flatten expressions (so that 1+x+2 becomes x+3).
  61 Node *MulNode::Ideal(PhaseGVN *phase, bool can_reshape) {
  62   Node* in1 = in(1);
  63   Node* in2 = in(2);
  64   Node* progress = NULL;        // Progress flag
  65 
  66   // This code is used by And nodes too, but some conversions are
  67   // only valid for the actual Mul nodes.
  68   uint op = Opcode();
  69   bool real_mul = (op == Op_MulI) || (op == Op_MulL) ||
  70                   (op == Op_MulF) || (op == Op_MulD);
  71 
  72   // Convert "(-a)*(-b)" into "a*b".
  73   if (real_mul && in1->is_Sub() && in2->is_Sub()) {
  74     if (phase->type(in1->in(1))->is_zero_type() &&
  75         phase->type(in2->in(1))->is_zero_type()) {
  76       set_req_X(1, in1->in(2), phase);
  77       set_req_X(2, in2->in(2), phase);
  78       in1 = in(1);
  79       in2 = in(2);
  80       progress = this;
  81     }
  82   }
  83 
  84   // convert "max(a,b) * min(a,b)" into "a*b".
  85   if ((in(1)->Opcode() == max_opcode() && in(2)->Opcode() == min_opcode())
  86       || (in(1)->Opcode() == min_opcode() && in(2)->Opcode() == max_opcode())) {
  87     Node *in11 = in(1)->in(1);
  88     Node *in12 = in(1)->in(2);
  89 
  90     Node *in21 = in(2)->in(1);
  91     Node *in22 = in(2)->in(2);
  92 
  93     if ((in11 == in21 && in12 == in22) ||
  94         (in11 == in22 && in12 == in21)) {
  95       set_req_X(1, in11, phase);
  96       set_req_X(2, in12, phase);
  97       in1 = in(1);
  98       in2 = in(2);
  99       progress = this;
 100     }
 101   }
 102 
 103   const Type* t1 = phase->type(in1);
 104   const Type* t2 = phase->type(in2);
 105 
 106   // We are OK if right is a constant, or right is a load and
 107   // left is a non-constant.
 108   if( !(t2->singleton() ||
 109         (in(2)->is_Load() && !(t1->singleton() || in(1)->is_Load())) ) ) {
 110     if( t1->singleton() ||       // Left input is a constant?
 111         // Otherwise, sort inputs (commutativity) to help value numbering.
 112         (in(1)->_idx > in(2)->_idx) ) {
 113       swap_edges(1, 2);
 114       const Type *t = t1;
 115       t1 = t2;
 116       t2 = t;
 117       progress = this;            // Made progress
 118     }
 119   }
 120 
 121   // If the right input is a constant, and the left input is a product of a
 122   // constant, flatten the expression tree.
 123   if( t2->singleton() &&        // Right input is a constant?
 124       op != Op_MulF &&          // Float & double cannot reassociate
 125       op != Op_MulD ) {
 126     if( t2 == Type::TOP ) return NULL;
 127     Node *mul1 = in(1);
 128 #ifdef ASSERT
 129     // Check for dead loop
 130     int op1 = mul1->Opcode();
 131     if ((mul1 == this) || (in(2) == this) ||
 132         ((op1 == mul_opcode() || op1 == add_opcode()) &&
 133          ((mul1->in(1) == this) || (mul1->in(2) == this) ||
 134           (mul1->in(1) == mul1) || (mul1->in(2) == mul1)))) {
 135       assert(false, "dead loop in MulNode::Ideal");
 136     }
 137 #endif
 138 
 139     if( mul1->Opcode() == mul_opcode() ) {  // Left input is a multiply?
 140       // Mul of a constant?
 141       const Type *t12 = phase->type( mul1->in(2) );
 142       if( t12->singleton() && t12 != Type::TOP) { // Left input is an add of a constant?
 143         // Compute new constant; check for overflow
 144         const Type *tcon01 = ((MulNode*)mul1)->mul_ring(t2,t12);
 145         if( tcon01->singleton() ) {
 146           // The Mul of the flattened expression
 147           set_req_X(1, mul1->in(1), phase);
 148           set_req_X(2, phase->makecon(tcon01), phase);
 149           t2 = tcon01;
 150           progress = this;      // Made progress
 151         }
 152       }
 153     }
 154     // If the right input is a constant, and the left input is an add of a
 155     // constant, flatten the tree: (X+con1)*con0 ==> X*con0 + con1*con0
 156     const Node *add1 = in(1);
 157     if( add1->Opcode() == add_opcode() ) {      // Left input is an add?
 158       // Add of a constant?
 159       const Type *t12 = phase->type( add1->in(2) );
 160       if( t12->singleton() && t12 != Type::TOP ) { // Left input is an add of a constant?
 161         assert( add1->in(1) != add1, "dead loop in MulNode::Ideal" );
 162         // Compute new constant; check for overflow
 163         const Type *tcon01 = mul_ring(t2,t12);
 164         if( tcon01->singleton() ) {
 165 
 166         // Convert (X+con1)*con0 into X*con0
 167           Node *mul = clone();    // mul = ()*con0
 168           mul->set_req(1,add1->in(1));  // mul = X*con0
 169           mul = phase->transform(mul);
 170 
 171           Node *add2 = add1->clone();
 172           add2->set_req(1, mul);        // X*con0 + con0*con1
 173           add2->set_req(2, phase->makecon(tcon01) );
 174           progress = add2;
 175         }
 176       }
 177     } // End of is left input an add
 178   } // End of is right input a Mul
 179 
 180   return progress;
 181 }
 182 
 183 //------------------------------Value-----------------------------------------
 184 const Type* MulNode::Value(PhaseGVN* phase) const {
 185   const Type *t1 = phase->type( in(1) );
 186   const Type *t2 = phase->type( in(2) );
 187   // Either input is TOP ==> the result is TOP
 188   if( t1 == Type::TOP ) return Type::TOP;
 189   if( t2 == Type::TOP ) return Type::TOP;
 190 
 191   // Either input is ZERO ==> the result is ZERO.
 192   // Not valid for floats or doubles since +0.0 * -0.0 --> +0.0
 193   int op = Opcode();
 194   if( op == Op_MulI || op == Op_AndI || op == Op_MulL || op == Op_AndL ) {
 195     const Type *zero = add_id();        // The multiplicative zero
 196     if( t1->higher_equal( zero ) ) return zero;
 197     if( t2->higher_equal( zero ) ) return zero;
 198   }
 199 
 200   // Either input is BOTTOM ==> the result is the local BOTTOM
 201   if( t1 == Type::BOTTOM || t2 == Type::BOTTOM )
 202     return bottom_type();
 203 
 204 #if defined(IA32)
 205   // Can't trust native compilers to properly fold strict double
 206   // multiplication with round-to-zero on this platform.
 207   if (op == Op_MulD) {
 208     return TypeD::DOUBLE;
 209   }
 210 #endif
 211 
 212   return mul_ring(t1,t2);            // Local flavor of type multiplication
 213 }
 214 
 215 MulNode* MulNode::make(Node* in1, Node* in2, BasicType bt) {
 216   switch (bt) {
 217     case T_INT:
 218       return new MulINode(in1, in2);
 219     case T_LONG:
 220       return new MulLNode(in1, in2);
 221     default:
 222       fatal("Not implemented for %s", type2name(bt));
 223   }
 224   return NULL;
 225 }
 226 
 227 
 228 //=============================================================================
 229 //------------------------------Ideal------------------------------------------
 230 // Check for power-of-2 multiply, then try the regular MulNode::Ideal
 231 Node *MulINode::Ideal(PhaseGVN *phase, bool can_reshape) {
 232   const jint con = in(2)->find_int_con(0);
 233   if (con == 0) {
 234     // If in(2) is not a constant, call Ideal() of the parent class to
 235     // try to move constant to the right side.
 236     return MulNode::Ideal(phase, can_reshape);
 237   }
 238 
 239   // Now we have a constant Node on the right and the constant in con.
 240   if (con == 1) {
 241     // By one is handled by Identity call
 242     return NULL;
 243   }
 244 
 245   // Check for negative constant; if so negate the final result
 246   bool sign_flip = false;
 247 
 248   unsigned int abs_con = uabs(con);
 249   if (abs_con != (unsigned int)con) {
 250     sign_flip = true;
 251   }
 252 
 253   // Get low bit; check for being the only bit
 254   Node *res = NULL;
 255   unsigned int bit1 = submultiple_power_of_2(abs_con);
 256   if (bit1 == abs_con) {           // Found a power of 2?
 257     res = new LShiftINode(in(1), phase->intcon(log2i_exact(bit1)));
 258   } else {
 259     // Check for constant with 2 bits set
 260     unsigned int bit2 = abs_con - bit1;
 261     bit2 = bit2 & (0 - bit2);          // Extract 2nd bit
 262     if (bit2 + bit1 == abs_con) {    // Found all bits in con?
 263       Node *n1 = phase->transform(new LShiftINode(in(1), phase->intcon(log2i_exact(bit1))));
 264       Node *n2 = phase->transform(new LShiftINode(in(1), phase->intcon(log2i_exact(bit2))));
 265       res = new AddINode(n2, n1);
 266     } else if (is_power_of_2(abs_con + 1)) {
 267       // Sleezy: power-of-2 - 1.  Next time be generic.
 268       unsigned int temp = abs_con + 1;
 269       Node *n1 = phase->transform(new LShiftINode(in(1), phase->intcon(log2i_exact(temp))));
 270       res = new SubINode(n1, in(1));
 271     } else {
 272       return MulNode::Ideal(phase, can_reshape);
 273     }
 274   }
 275 
 276   if (sign_flip) {             // Need to negate result?
 277     res = phase->transform(res);// Transform, before making the zero con
 278     res = new SubINode(phase->intcon(0),res);
 279   }
 280 
 281   return res;                   // Return final result
 282 }
 283 
 284 //------------------------------mul_ring---------------------------------------
 285 // Compute the product type of two integer ranges into this node.
 286 const Type *MulINode::mul_ring(const Type *t0, const Type *t1) const {
 287   const TypeInt *r0 = t0->is_int(); // Handy access
 288   const TypeInt *r1 = t1->is_int();
 289 
 290   // Fetch endpoints of all ranges
 291   jint lo0 = r0->_lo;
 292   double a = (double)lo0;
 293   jint hi0 = r0->_hi;
 294   double b = (double)hi0;
 295   jint lo1 = r1->_lo;
 296   double c = (double)lo1;
 297   jint hi1 = r1->_hi;
 298   double d = (double)hi1;
 299 
 300   // Compute all endpoints & check for overflow
 301   int32_t A = java_multiply(lo0, lo1);
 302   if( (double)A != a*c ) return TypeInt::INT; // Overflow?
 303   int32_t B = java_multiply(lo0, hi1);
 304   if( (double)B != a*d ) return TypeInt::INT; // Overflow?
 305   int32_t C = java_multiply(hi0, lo1);
 306   if( (double)C != b*c ) return TypeInt::INT; // Overflow?
 307   int32_t D = java_multiply(hi0, hi1);
 308   if( (double)D != b*d ) return TypeInt::INT; // Overflow?
 309 
 310   if( A < B ) { lo0 = A; hi0 = B; } // Sort range endpoints
 311   else { lo0 = B; hi0 = A; }
 312   if( C < D ) {
 313     if( C < lo0 ) lo0 = C;
 314     if( D > hi0 ) hi0 = D;
 315   } else {
 316     if( D < lo0 ) lo0 = D;
 317     if( C > hi0 ) hi0 = C;
 318   }
 319   return TypeInt::make(lo0, hi0, MAX2(r0->_widen,r1->_widen));
 320 }
 321 
 322 
 323 //=============================================================================
 324 //------------------------------Ideal------------------------------------------
 325 // Check for power-of-2 multiply, then try the regular MulNode::Ideal
 326 Node *MulLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 327   const jlong con = in(2)->find_long_con(0);
 328   if (con == 0) {
 329     // If in(2) is not a constant, call Ideal() of the parent class to
 330     // try to move constant to the right side.
 331     return MulNode::Ideal(phase, can_reshape);
 332   }
 333 
 334   // Now we have a constant Node on the right and the constant in con.
 335   if (con == 1) {
 336     // By one is handled by Identity call
 337     return NULL;
 338   }
 339 
 340   // Check for negative constant; if so negate the final result
 341   bool sign_flip = false;
 342   julong abs_con = uabs(con);
 343   if (abs_con != (julong)con) {
 344     sign_flip = true;
 345   }
 346 
 347   // Get low bit; check for being the only bit
 348   Node *res = NULL;
 349   julong bit1 = submultiple_power_of_2(abs_con);
 350   if (bit1 == abs_con) {           // Found a power of 2?
 351     res = new LShiftLNode(in(1), phase->intcon(log2i_exact(bit1)));
 352   } else {
 353 
 354     // Check for constant with 2 bits set
 355     julong bit2 = abs_con-bit1;
 356     bit2 = bit2 & (0-bit2);          // Extract 2nd bit
 357     if (bit2 + bit1 == abs_con) {    // Found all bits in con?
 358       Node *n1 = phase->transform(new LShiftLNode(in(1), phase->intcon(log2i_exact(bit1))));
 359       Node *n2 = phase->transform(new LShiftLNode(in(1), phase->intcon(log2i_exact(bit2))));
 360       res = new AddLNode(n2, n1);
 361 
 362     } else if (is_power_of_2(abs_con+1)) {
 363       // Sleezy: power-of-2 -1.  Next time be generic.
 364       julong temp = abs_con + 1;
 365       Node *n1 = phase->transform( new LShiftLNode(in(1), phase->intcon(log2i_exact(temp))));
 366       res = new SubLNode(n1, in(1));
 367     } else {
 368       return MulNode::Ideal(phase, can_reshape);
 369     }
 370   }
 371 
 372   if (sign_flip) {             // Need to negate result?
 373     res = phase->transform(res);// Transform, before making the zero con
 374     res = new SubLNode(phase->longcon(0),res);
 375   }
 376 
 377   return res;                   // Return final result
 378 }
 379 
 380 //------------------------------mul_ring---------------------------------------
 381 // Compute the product type of two integer ranges into this node.
 382 const Type *MulLNode::mul_ring(const Type *t0, const Type *t1) const {
 383   const TypeLong *r0 = t0->is_long(); // Handy access
 384   const TypeLong *r1 = t1->is_long();
 385 
 386   // Fetch endpoints of all ranges
 387   jlong lo0 = r0->_lo;
 388   double a = (double)lo0;
 389   jlong hi0 = r0->_hi;
 390   double b = (double)hi0;
 391   jlong lo1 = r1->_lo;
 392   double c = (double)lo1;
 393   jlong hi1 = r1->_hi;
 394   double d = (double)hi1;
 395 
 396   // Compute all endpoints & check for overflow
 397   jlong A = java_multiply(lo0, lo1);
 398   if( (double)A != a*c ) return TypeLong::LONG; // Overflow?
 399   jlong B = java_multiply(lo0, hi1);
 400   if( (double)B != a*d ) return TypeLong::LONG; // Overflow?
 401   jlong C = java_multiply(hi0, lo1);
 402   if( (double)C != b*c ) return TypeLong::LONG; // Overflow?
 403   jlong D = java_multiply(hi0, hi1);
 404   if( (double)D != b*d ) return TypeLong::LONG; // Overflow?
 405 
 406   if( A < B ) { lo0 = A; hi0 = B; } // Sort range endpoints
 407   else { lo0 = B; hi0 = A; }
 408   if( C < D ) {
 409     if( C < lo0 ) lo0 = C;
 410     if( D > hi0 ) hi0 = D;
 411   } else {
 412     if( D < lo0 ) lo0 = D;
 413     if( C > hi0 ) hi0 = C;
 414   }
 415   return TypeLong::make(lo0, hi0, MAX2(r0->_widen,r1->_widen));
 416 }
 417 
 418 //=============================================================================
 419 //------------------------------mul_ring---------------------------------------
 420 // Compute the product type of two double ranges into this node.
 421 const Type *MulFNode::mul_ring(const Type *t0, const Type *t1) const {
 422   if( t0 == Type::FLOAT || t1 == Type::FLOAT ) return Type::FLOAT;
 423   return TypeF::make( t0->getf() * t1->getf() );
 424 }
 425 
 426 //=============================================================================
 427 //------------------------------mul_ring---------------------------------------
 428 // Compute the product type of two double ranges into this node.
 429 const Type *MulDNode::mul_ring(const Type *t0, const Type *t1) const {
 430   if( t0 == Type::DOUBLE || t1 == Type::DOUBLE ) return Type::DOUBLE;
 431   // We must be multiplying 2 double constants.
 432   return TypeD::make( t0->getd() * t1->getd() );
 433 }
 434 
 435 //=============================================================================
 436 //------------------------------Value------------------------------------------
 437 const Type* MulHiLNode::Value(PhaseGVN* phase) const {
 438   const Type *t1 = phase->type( in(1) );
 439   const Type *t2 = phase->type( in(2) );
 440   const Type *bot = bottom_type();
 441   return MulHiValue(t1, t2, bot);
 442 }
 443 
 444 const Type* UMulHiLNode::Value(PhaseGVN* phase) const {
 445   const Type *t1 = phase->type( in(1) );
 446   const Type *t2 = phase->type( in(2) );
 447   const Type *bot = bottom_type();
 448   return MulHiValue(t1, t2, bot);
 449 }
 450 
 451 // A common routine used by UMulHiLNode and MulHiLNode
 452 const Type* MulHiValue(const Type *t1, const Type *t2, const Type *bot) {
 453   // Either input is TOP ==> the result is TOP
 454   if( t1 == Type::TOP ) return Type::TOP;
 455   if( t2 == Type::TOP ) return Type::TOP;
 456 
 457   // Either input is BOTTOM ==> the result is the local BOTTOM
 458   if( (t1 == bot) || (t2 == bot) ||
 459       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 460     return bot;
 461 
 462   // It is not worth trying to constant fold this stuff!
 463   return TypeLong::LONG;
 464 }
 465 
 466 //=============================================================================
 467 //------------------------------mul_ring---------------------------------------
 468 // Supplied function returns the product of the inputs IN THE CURRENT RING.
 469 // For the logical operations the ring's MUL is really a logical AND function.
 470 // This also type-checks the inputs for sanity.  Guaranteed never to
 471 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 472 const Type *AndINode::mul_ring( const Type *t0, const Type *t1 ) const {
 473   const TypeInt *r0 = t0->is_int(); // Handy access
 474   const TypeInt *r1 = t1->is_int();
 475   int widen = MAX2(r0->_widen,r1->_widen);
 476 
 477   // If either input is a constant, might be able to trim cases
 478   if( !r0->is_con() && !r1->is_con() )
 479     return TypeInt::INT;        // No constants to be had
 480 
 481   // Both constants?  Return bits
 482   if( r0->is_con() && r1->is_con() )
 483     return TypeInt::make( r0->get_con() & r1->get_con() );
 484 
 485   if( r0->is_con() && r0->get_con() > 0 )
 486     return TypeInt::make(0, r0->get_con(), widen);
 487 
 488   if( r1->is_con() && r1->get_con() > 0 )
 489     return TypeInt::make(0, r1->get_con(), widen);
 490 
 491   if( r0 == TypeInt::BOOL || r1 == TypeInt::BOOL ) {
 492     return TypeInt::BOOL;
 493   }
 494 
 495   return TypeInt::INT;          // No constants to be had
 496 }
 497 
 498 const Type* AndINode::Value(PhaseGVN* phase) const {
 499   // patterns similar to (v << 2) & 3
 500   if (AndIL_shift_and_mask_is_always_zero(phase, in(1), in(2), T_INT, true)) {
 501     return TypeInt::ZERO;
 502   }
 503 
 504   return MulNode::Value(phase);
 505 }
 506 
 507 //------------------------------Identity---------------------------------------
 508 // Masking off the high bits of an unsigned load is not required
 509 Node* AndINode::Identity(PhaseGVN* phase) {
 510 
 511   // x & x => x
 512   if (in(1) == in(2)) {
 513     return in(1);
 514   }
 515 
 516   Node* in1 = in(1);
 517   uint op = in1->Opcode();
 518   const TypeInt* t2 = phase->type(in(2))->isa_int();
 519   if (t2 && t2->is_con()) {
 520     int con = t2->get_con();
 521     // Masking off high bits which are always zero is useless.
 522     const TypeInt* t1 = phase->type(in(1))->isa_int();
 523     if (t1 != NULL && t1->_lo >= 0) {
 524       jint t1_support = right_n_bits(1 + log2i_graceful(t1->_hi));
 525       if ((t1_support & con) == t1_support)
 526         return in1;
 527     }
 528     // Masking off the high bits of a unsigned-shift-right is not
 529     // needed either.
 530     if (op == Op_URShiftI) {
 531       const TypeInt* t12 = phase->type(in1->in(2))->isa_int();
 532       if (t12 && t12->is_con()) {  // Shift is by a constant
 533         int shift = t12->get_con();
 534         shift &= BitsPerJavaInteger - 1;  // semantics of Java shifts
 535         int mask = max_juint >> shift;
 536         if ((mask & con) == mask)  // If AND is useless, skip it
 537           return in1;
 538       }
 539     }
 540   }
 541   return MulNode::Identity(phase);
 542 }
 543 
 544 //------------------------------Ideal------------------------------------------
 545 Node *AndINode::Ideal(PhaseGVN *phase, bool can_reshape) {
 546   // pattern similar to (v1 + (v2 << 2)) & 3 transformed to v1 & 3
 547   Node* progress = AndIL_add_shift_and_mask(phase, T_INT);
 548   if (progress != NULL) {
 549     return progress;
 550   }
 551 
 552   // Special case constant AND mask
 553   const TypeInt *t2 = phase->type( in(2) )->isa_int();
 554   if( !t2 || !t2->is_con() ) return MulNode::Ideal(phase, can_reshape);
 555   const int mask = t2->get_con();
 556   Node *load = in(1);
 557   uint lop = load->Opcode();
 558 
 559   // Masking bits off of a Character?  Hi bits are already zero.
 560   if( lop == Op_LoadUS &&
 561       (mask & 0xFFFF0000) )     // Can we make a smaller mask?
 562     return new AndINode(load,phase->intcon(mask&0xFFFF));
 563 
 564   // Masking bits off of a Short?  Loading a Character does some masking
 565   if (can_reshape &&
 566       load->outcnt() == 1 && load->unique_out() == this) {
 567     if (lop == Op_LoadS && (mask & 0xFFFF0000) == 0 ) {
 568       Node* ldus = load->as_Load()->convert_to_unsigned_load(*phase);
 569       ldus = phase->transform(ldus);
 570       return new AndINode(ldus, phase->intcon(mask & 0xFFFF));
 571     }
 572 
 573     // Masking sign bits off of a Byte?  Do an unsigned byte load plus
 574     // an and.
 575     if (lop == Op_LoadB && (mask & 0xFFFFFF00) == 0) {
 576       Node* ldub = load->as_Load()->convert_to_unsigned_load(*phase);
 577       ldub = phase->transform(ldub);
 578       return new AndINode(ldub, phase->intcon(mask));
 579     }
 580   }
 581 
 582   // Masking off sign bits?  Dont make them!
 583   if( lop == Op_RShiftI ) {
 584     const TypeInt *t12 = phase->type(load->in(2))->isa_int();
 585     if( t12 && t12->is_con() ) { // Shift is by a constant
 586       int shift = t12->get_con();
 587       shift &= BitsPerJavaInteger-1;  // semantics of Java shifts
 588       const int sign_bits_mask = ~right_n_bits(BitsPerJavaInteger - shift);
 589       // If the AND'ing of the 2 masks has no bits, then only original shifted
 590       // bits survive.  NO sign-extension bits survive the maskings.
 591       if( (sign_bits_mask & mask) == 0 ) {
 592         // Use zero-fill shift instead
 593         Node *zshift = phase->transform(new URShiftINode(load->in(1),load->in(2)));
 594         return new AndINode( zshift, in(2) );
 595       }
 596     }
 597   }
 598 
 599   // Check for 'negate/and-1', a pattern emitted when someone asks for
 600   // 'mod 2'.  Negate leaves the low order bit unchanged (think: complement
 601   // plus 1) and the mask is of the low order bit.  Skip the negate.
 602   if( lop == Op_SubI && mask == 1 && load->in(1) &&
 603       phase->type(load->in(1)) == TypeInt::ZERO )
 604     return new AndINode( load->in(2), in(2) );
 605 
 606   return MulNode::Ideal(phase, can_reshape);
 607 }
 608 
 609 //=============================================================================
 610 //------------------------------mul_ring---------------------------------------
 611 // Supplied function returns the product of the inputs IN THE CURRENT RING.
 612 // For the logical operations the ring's MUL is really a logical AND function.
 613 // This also type-checks the inputs for sanity.  Guaranteed never to
 614 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 615 const Type *AndLNode::mul_ring( const Type *t0, const Type *t1 ) const {
 616   const TypeLong *r0 = t0->is_long(); // Handy access
 617   const TypeLong *r1 = t1->is_long();
 618   int widen = MAX2(r0->_widen,r1->_widen);
 619 
 620   // If either input is a constant, might be able to trim cases
 621   if( !r0->is_con() && !r1->is_con() )
 622     return TypeLong::LONG;      // No constants to be had
 623 
 624   // Both constants?  Return bits
 625   if( r0->is_con() && r1->is_con() )
 626     return TypeLong::make( r0->get_con() & r1->get_con() );
 627 
 628   if( r0->is_con() && r0->get_con() > 0 )
 629     return TypeLong::make(CONST64(0), r0->get_con(), widen);
 630 
 631   if( r1->is_con() && r1->get_con() > 0 )
 632     return TypeLong::make(CONST64(0), r1->get_con(), widen);
 633 
 634   return TypeLong::LONG;        // No constants to be had
 635 }
 636 
 637 const Type* AndLNode::Value(PhaseGVN* phase) const {
 638   // patterns similar to (v << 2) & 3
 639   if (AndIL_shift_and_mask_is_always_zero(phase, in(1), in(2), T_LONG, true)) {
 640     return TypeLong::ZERO;
 641   }
 642 
 643   return MulNode::Value(phase);
 644 }
 645 
 646 //------------------------------Identity---------------------------------------
 647 // Masking off the high bits of an unsigned load is not required
 648 Node* AndLNode::Identity(PhaseGVN* phase) {
 649 
 650   // x & x => x
 651   if (in(1) == in(2)) {
 652     return in(1);
 653   }
 654 
 655   Node *usr = in(1);
 656   const TypeLong *t2 = phase->type( in(2) )->isa_long();
 657   if( t2 && t2->is_con() ) {
 658     jlong con = t2->get_con();
 659     // Masking off high bits which are always zero is useless.
 660     const TypeLong* t1 = phase->type( in(1) )->isa_long();
 661     if (t1 != NULL && t1->_lo >= 0) {
 662       int bit_count = log2i_graceful(t1->_hi) + 1;
 663       jlong t1_support = jlong(max_julong >> (BitsPerJavaLong - bit_count));
 664       if ((t1_support & con) == t1_support)
 665         return usr;
 666     }
 667     uint lop = usr->Opcode();
 668     // Masking off the high bits of a unsigned-shift-right is not
 669     // needed either.
 670     if( lop == Op_URShiftL ) {
 671       const TypeInt *t12 = phase->type( usr->in(2) )->isa_int();
 672       if( t12 && t12->is_con() ) {  // Shift is by a constant
 673         int shift = t12->get_con();
 674         shift &= BitsPerJavaLong - 1;  // semantics of Java shifts
 675         jlong mask = max_julong >> shift;
 676         if( (mask&con) == mask )  // If AND is useless, skip it
 677           return usr;
 678       }
 679     }
 680   }
 681   return MulNode::Identity(phase);
 682 }
 683 
 684 //------------------------------Ideal------------------------------------------
 685 Node *AndLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 686   // pattern similar to (v1 + (v2 << 2)) & 3 transformed to v1 & 3
 687   Node* progress = AndIL_add_shift_and_mask(phase, T_LONG);
 688   if (progress != NULL) {
 689     return progress;
 690   }
 691 
 692   // Special case constant AND mask
 693   const TypeLong *t2 = phase->type( in(2) )->isa_long();
 694   if( !t2 || !t2->is_con() ) return MulNode::Ideal(phase, can_reshape);
 695   const jlong mask = t2->get_con();
 696 
 697   Node* in1 = in(1);
 698   int op = in1->Opcode();
 699 
 700   // Are we masking a long that was converted from an int with a mask
 701   // that fits in 32-bits?  Commute them and use an AndINode.  Don't
 702   // convert masks which would cause a sign extension of the integer
 703   // value.  This check includes UI2L masks (0x00000000FFFFFFFF) which
 704   // would be optimized away later in Identity.
 705   if (op == Op_ConvI2L && (mask & UCONST64(0xFFFFFFFF80000000)) == 0) {
 706     Node* andi = new AndINode(in1->in(1), phase->intcon(mask));
 707     andi = phase->transform(andi);
 708     return new ConvI2LNode(andi);
 709   }
 710 
 711   // Masking off sign bits?  Dont make them!
 712   if (op == Op_RShiftL) {
 713     const TypeInt* t12 = phase->type(in1->in(2))->isa_int();
 714     if( t12 && t12->is_con() ) { // Shift is by a constant
 715       int shift = t12->get_con();
 716       shift &= BitsPerJavaLong - 1;  // semantics of Java shifts
 717       const jlong sign_bits_mask = ~(((jlong)CONST64(1) << (jlong)(BitsPerJavaLong - shift)) -1);
 718       // If the AND'ing of the 2 masks has no bits, then only original shifted
 719       // bits survive.  NO sign-extension bits survive the maskings.
 720       if( (sign_bits_mask & mask) == 0 ) {
 721         // Use zero-fill shift instead
 722         Node *zshift = phase->transform(new URShiftLNode(in1->in(1), in1->in(2)));
 723         return new AndLNode(zshift, in(2));
 724       }
 725     }
 726   }
 727 
 728   return MulNode::Ideal(phase, can_reshape);
 729 }
 730 
 731 LShiftNode* LShiftNode::make(Node* in1, Node* in2, BasicType bt) {
 732   switch (bt) {
 733     case T_INT:
 734       return new LShiftINode(in1, in2);
 735     case T_LONG:
 736       return new LShiftLNode(in1, in2);
 737     default:
 738       fatal("Not implemented for %s", type2name(bt));
 739   }
 740   return NULL;
 741 }
 742 
 743 //=============================================================================
 744 
 745 static bool const_shift_count(PhaseGVN* phase, Node* shiftNode, int* count) {
 746   const TypeInt* tcount = phase->type(shiftNode->in(2))->isa_int();
 747   if (tcount != NULL && tcount->is_con()) {
 748     *count = tcount->get_con();
 749     return true;
 750   }
 751   return false;
 752 }
 753 
 754 static int maskShiftAmount(PhaseGVN* phase, Node* shiftNode, int nBits) {
 755   int count = 0;
 756   if (const_shift_count(phase, shiftNode, &count)) {
 757     int maskedShift = count & (nBits - 1);
 758     if (maskedShift == 0) {
 759       // Let Identity() handle 0 shift count.
 760       return 0;
 761     }
 762 
 763     if (count != maskedShift) {
 764       shiftNode->set_req(2, phase->intcon(maskedShift)); // Replace shift count with masked value.
 765       PhaseIterGVN* igvn = phase->is_IterGVN();
 766       if (igvn) {
 767         igvn->rehash_node_delayed(shiftNode);
 768       }
 769     }
 770     return maskedShift;
 771   }
 772   return 0;
 773 }
 774 
 775 //------------------------------Identity---------------------------------------
 776 Node* LShiftINode::Identity(PhaseGVN* phase) {
 777   int count = 0;
 778   if (const_shift_count(phase, this, &count) && (count & (BitsPerJavaInteger - 1)) == 0) {
 779     // Shift by a multiple of 32 does nothing
 780     return in(1);
 781   }
 782   return this;
 783 }
 784 
 785 //------------------------------Ideal------------------------------------------
 786 // If the right input is a constant, and the left input is an add of a
 787 // constant, flatten the tree: (X+con1)<<con0 ==> X<<con0 + con1<<con0
 788 Node *LShiftINode::Ideal(PhaseGVN *phase, bool can_reshape) {
 789   int con = maskShiftAmount(phase, this, BitsPerJavaInteger);
 790   if (con == 0) {
 791     return NULL;
 792   }
 793 
 794   // Left input is an add?
 795   Node *add1 = in(1);
 796   int add1_op = add1->Opcode();
 797   if( add1_op == Op_AddI ) {    // Left input is an add?
 798     assert( add1 != add1->in(1), "dead loop in LShiftINode::Ideal" );
 799 
 800     // Transform is legal, but check for profit.  Avoid breaking 'i2s'
 801     // and 'i2b' patterns which typically fold into 'StoreC/StoreB'.
 802     if( con < 16 ) {
 803       // Left input is an add of the same number?
 804       if (add1->in(1) == add1->in(2)) {
 805         // Convert "(x + x) << c0" into "x << (c0 + 1)"
 806         // In general, this optimization cannot be applied for c0 == 31 since
 807         // 2x << 31 != x << 32 = x << 0 = x (e.g. x = 1: 2 << 31 = 0 != 1)
 808         return new LShiftINode(add1->in(1), phase->intcon(con + 1));
 809       }
 810 
 811       // Left input is an add of a constant?
 812       const TypeInt *t12 = phase->type(add1->in(2))->isa_int();
 813       if( t12 && t12->is_con() ){ // Left input is an add of a con?
 814         // Compute X << con0
 815         Node *lsh = phase->transform( new LShiftINode( add1->in(1), in(2) ) );
 816         // Compute X<<con0 + (con1<<con0)
 817         return new AddINode( lsh, phase->intcon(t12->get_con() << con));
 818       }
 819     }
 820   }
 821 
 822   // Check for "(x>>c0)<<c0" which just masks off low bits
 823   if( (add1_op == Op_RShiftI || add1_op == Op_URShiftI ) &&
 824       add1->in(2) == in(2) )
 825     // Convert to "(x & -(1<<c0))"
 826     return new AndINode(add1->in(1),phase->intcon( -(1<<con)));
 827 
 828   // Check for "((x>>c0) & Y)<<c0" which just masks off more low bits
 829   if( add1_op == Op_AndI ) {
 830     Node *add2 = add1->in(1);
 831     int add2_op = add2->Opcode();
 832     if( (add2_op == Op_RShiftI || add2_op == Op_URShiftI ) &&
 833         add2->in(2) == in(2) ) {
 834       // Convert to "(x & (Y<<c0))"
 835       Node *y_sh = phase->transform( new LShiftINode( add1->in(2), in(2) ) );
 836       return new AndINode( add2->in(1), y_sh );
 837     }
 838   }
 839 
 840   // Check for ((x & ((1<<(32-c0))-1)) << c0) which ANDs off high bits
 841   // before shifting them away.
 842   const jint bits_mask = right_n_bits(BitsPerJavaInteger-con);
 843   if( add1_op == Op_AndI &&
 844       phase->type(add1->in(2)) == TypeInt::make( bits_mask ) )
 845     return new LShiftINode( add1->in(1), in(2) );
 846 
 847   return NULL;
 848 }
 849 
 850 //------------------------------Value------------------------------------------
 851 // A LShiftINode shifts its input2 left by input1 amount.
 852 const Type* LShiftINode::Value(PhaseGVN* phase) const {
 853   const Type *t1 = phase->type( in(1) );
 854   const Type *t2 = phase->type( in(2) );
 855   // Either input is TOP ==> the result is TOP
 856   if( t1 == Type::TOP ) return Type::TOP;
 857   if( t2 == Type::TOP ) return Type::TOP;
 858 
 859   // Left input is ZERO ==> the result is ZERO.
 860   if( t1 == TypeInt::ZERO ) return TypeInt::ZERO;
 861   // Shift by zero does nothing
 862   if( t2 == TypeInt::ZERO ) return t1;
 863 
 864   // Either input is BOTTOM ==> the result is BOTTOM
 865   if( (t1 == TypeInt::INT) || (t2 == TypeInt::INT) ||
 866       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 867     return TypeInt::INT;
 868 
 869   const TypeInt *r1 = t1->is_int(); // Handy access
 870   const TypeInt *r2 = t2->is_int(); // Handy access
 871 
 872   if (!r2->is_con())
 873     return TypeInt::INT;
 874 
 875   uint shift = r2->get_con();
 876   shift &= BitsPerJavaInteger-1;  // semantics of Java shifts
 877   // Shift by a multiple of 32 does nothing:
 878   if (shift == 0)  return t1;
 879 
 880   // If the shift is a constant, shift the bounds of the type,
 881   // unless this could lead to an overflow.
 882   if (!r1->is_con()) {
 883     jint lo = r1->_lo, hi = r1->_hi;
 884     if (((lo << shift) >> shift) == lo &&
 885         ((hi << shift) >> shift) == hi) {
 886       // No overflow.  The range shifts up cleanly.
 887       return TypeInt::make((jint)lo << (jint)shift,
 888                            (jint)hi << (jint)shift,
 889                            MAX2(r1->_widen,r2->_widen));
 890     }
 891     return TypeInt::INT;
 892   }
 893 
 894   return TypeInt::make( (jint)r1->get_con() << (jint)shift );
 895 }
 896 
 897 //=============================================================================
 898 //------------------------------Identity---------------------------------------
 899 Node* LShiftLNode::Identity(PhaseGVN* phase) {
 900   int count = 0;
 901   if (const_shift_count(phase, this, &count) && (count & (BitsPerJavaLong - 1)) == 0) {
 902     // Shift by a multiple of 64 does nothing
 903     return in(1);
 904   }
 905   return this;
 906 }
 907 
 908 //------------------------------Ideal------------------------------------------
 909 // If the right input is a constant, and the left input is an add of a
 910 // constant, flatten the tree: (X+con1)<<con0 ==> X<<con0 + con1<<con0
 911 Node *LShiftLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 912   int con = maskShiftAmount(phase, this, BitsPerJavaLong);
 913   if (con == 0) {
 914     return NULL;
 915   }
 916 
 917   // Left input is an add?
 918   Node *add1 = in(1);
 919   int add1_op = add1->Opcode();
 920   if( add1_op == Op_AddL ) {    // Left input is an add?
 921     // Avoid dead data cycles from dead loops
 922     assert( add1 != add1->in(1), "dead loop in LShiftLNode::Ideal" );
 923 
 924     // Left input is an add of the same number?
 925     if (con != (BitsPerJavaLong - 1) && add1->in(1) == add1->in(2)) {
 926       // Convert "(x + x) << c0" into "x << (c0 + 1)"
 927       // Can only be applied if c0 != 63 because:
 928       // (x + x) << 63 = 2x << 63, while
 929       // (x + x) << 63 --transform--> x << 64 = x << 0 = x (!= 2x << 63, for example for x = 1)
 930       // According to the Java spec, chapter 15.19, we only consider the six lowest-order bits of the right-hand operand
 931       // (i.e. "right-hand operand" & 0b111111). Therefore, x << 64 is the same as x << 0 (64 = 0b10000000 & 0b0111111 = 0).
 932       return new LShiftLNode(add1->in(1), phase->intcon(con + 1));
 933     }
 934 
 935     // Left input is an add of a constant?
 936     const TypeLong *t12 = phase->type(add1->in(2))->isa_long();
 937     if( t12 && t12->is_con() ){ // Left input is an add of a con?
 938       // Compute X << con0
 939       Node *lsh = phase->transform( new LShiftLNode( add1->in(1), in(2) ) );
 940       // Compute X<<con0 + (con1<<con0)
 941       return new AddLNode( lsh, phase->longcon(t12->get_con() << con));
 942     }
 943   }
 944 
 945   // Check for "(x>>c0)<<c0" which just masks off low bits
 946   if( (add1_op == Op_RShiftL || add1_op == Op_URShiftL ) &&
 947       add1->in(2) == in(2) )
 948     // Convert to "(x & -(1<<c0))"
 949     return new AndLNode(add1->in(1),phase->longcon( -(CONST64(1)<<con)));
 950 
 951   // Check for "((x>>c0) & Y)<<c0" which just masks off more low bits
 952   if( add1_op == Op_AndL ) {
 953     Node *add2 = add1->in(1);
 954     int add2_op = add2->Opcode();
 955     if( (add2_op == Op_RShiftL || add2_op == Op_URShiftL ) &&
 956         add2->in(2) == in(2) ) {
 957       // Convert to "(x & (Y<<c0))"
 958       Node *y_sh = phase->transform( new LShiftLNode( add1->in(2), in(2) ) );
 959       return new AndLNode( add2->in(1), y_sh );
 960     }
 961   }
 962 
 963   // Check for ((x & ((CONST64(1)<<(64-c0))-1)) << c0) which ANDs off high bits
 964   // before shifting them away.
 965   const jlong bits_mask = jlong(max_julong >> con);
 966   if( add1_op == Op_AndL &&
 967       phase->type(add1->in(2)) == TypeLong::make( bits_mask ) )
 968     return new LShiftLNode( add1->in(1), in(2) );
 969 
 970   return NULL;
 971 }
 972 
 973 //------------------------------Value------------------------------------------
 974 // A LShiftLNode shifts its input2 left by input1 amount.
 975 const Type* LShiftLNode::Value(PhaseGVN* phase) const {
 976   const Type *t1 = phase->type( in(1) );
 977   const Type *t2 = phase->type( in(2) );
 978   // Either input is TOP ==> the result is TOP
 979   if( t1 == Type::TOP ) return Type::TOP;
 980   if( t2 == Type::TOP ) return Type::TOP;
 981 
 982   // Left input is ZERO ==> the result is ZERO.
 983   if( t1 == TypeLong::ZERO ) return TypeLong::ZERO;
 984   // Shift by zero does nothing
 985   if( t2 == TypeInt::ZERO ) return t1;
 986 
 987   // Either input is BOTTOM ==> the result is BOTTOM
 988   if( (t1 == TypeLong::LONG) || (t2 == TypeInt::INT) ||
 989       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 990     return TypeLong::LONG;
 991 
 992   const TypeLong *r1 = t1->is_long(); // Handy access
 993   const TypeInt  *r2 = t2->is_int();  // Handy access
 994 
 995   if (!r2->is_con())
 996     return TypeLong::LONG;
 997 
 998   uint shift = r2->get_con();
 999   shift &= BitsPerJavaLong - 1;  // semantics of Java shifts
1000   // Shift by a multiple of 64 does nothing:
1001   if (shift == 0)  return t1;
1002 
1003   // If the shift is a constant, shift the bounds of the type,
1004   // unless this could lead to an overflow.
1005   if (!r1->is_con()) {
1006     jlong lo = r1->_lo, hi = r1->_hi;
1007     if (((lo << shift) >> shift) == lo &&
1008         ((hi << shift) >> shift) == hi) {
1009       // No overflow.  The range shifts up cleanly.
1010       return TypeLong::make((jlong)lo << (jint)shift,
1011                             (jlong)hi << (jint)shift,
1012                             MAX2(r1->_widen,r2->_widen));
1013     }
1014     return TypeLong::LONG;
1015   }
1016 
1017   return TypeLong::make( (jlong)r1->get_con() << (jint)shift );
1018 }
1019 
1020 //=============================================================================
1021 //------------------------------Identity---------------------------------------
1022 Node* RShiftINode::Identity(PhaseGVN* phase) {
1023   int count = 0;
1024   if (const_shift_count(phase, this, &count)) {
1025     if ((count & (BitsPerJavaInteger - 1)) == 0) {
1026       // Shift by a multiple of 32 does nothing
1027       return in(1);
1028     }
1029     // Check for useless sign-masking
1030     if (in(1)->Opcode() == Op_LShiftI &&
1031         in(1)->req() == 3 &&
1032         in(1)->in(2) == in(2)) {
1033       count &= BitsPerJavaInteger-1; // semantics of Java shifts
1034       // Compute masks for which this shifting doesn't change
1035       int lo = (-1 << (BitsPerJavaInteger - ((uint)count)-1)); // FFFF8000
1036       int hi = ~lo;               // 00007FFF
1037       const TypeInt* t11 = phase->type(in(1)->in(1))->isa_int();
1038       if (t11 == NULL) {
1039         return this;
1040       }
1041       // Does actual value fit inside of mask?
1042       if (lo <= t11->_lo && t11->_hi <= hi) {
1043         return in(1)->in(1);      // Then shifting is a nop
1044       }
1045     }
1046   }
1047   return this;
1048 }
1049 
1050 //------------------------------Ideal------------------------------------------
1051 Node *RShiftINode::Ideal(PhaseGVN *phase, bool can_reshape) {
1052   // Inputs may be TOP if they are dead.
1053   const TypeInt *t1 = phase->type(in(1))->isa_int();
1054   if (!t1) return NULL;        // Left input is an integer
1055   const TypeInt *t3;  // type of in(1).in(2)
1056   int shift = maskShiftAmount(phase, this, BitsPerJavaInteger);
1057   if (shift == 0) {
1058     return NULL;
1059   }
1060 
1061   // Check for (x & 0xFF000000) >> 24, whose mask can be made smaller.
1062   // Such expressions arise normally from shift chains like (byte)(x >> 24).
1063   const Node *mask = in(1);
1064   if( mask->Opcode() == Op_AndI &&
1065       (t3 = phase->type(mask->in(2))->isa_int()) &&
1066       t3->is_con() ) {
1067     Node *x = mask->in(1);
1068     jint maskbits = t3->get_con();
1069     // Convert to "(x >> shift) & (mask >> shift)"
1070     Node *shr_nomask = phase->transform( new RShiftINode(mask->in(1), in(2)) );
1071     return new AndINode(shr_nomask, phase->intcon( maskbits >> shift));
1072   }
1073 
1074   // Check for "(short[i] <<16)>>16" which simply sign-extends
1075   const Node *shl = in(1);
1076   if( shl->Opcode() != Op_LShiftI ) return NULL;
1077 
1078   if( shift == 16 &&
1079       (t3 = phase->type(shl->in(2))->isa_int()) &&
1080       t3->is_con(16) ) {
1081     Node *ld = shl->in(1);
1082     if( ld->Opcode() == Op_LoadS ) {
1083       // Sign extension is just useless here.  Return a RShiftI of zero instead
1084       // returning 'ld' directly.  We cannot return an old Node directly as
1085       // that is the job of 'Identity' calls and Identity calls only work on
1086       // direct inputs ('ld' is an extra Node removed from 'this').  The
1087       // combined optimization requires Identity only return direct inputs.
1088       set_req_X(1, ld, phase);
1089       set_req_X(2, phase->intcon(0), phase);
1090       return this;
1091     }
1092     else if (can_reshape &&
1093              ld->Opcode() == Op_LoadUS &&
1094              ld->outcnt() == 1 && ld->unique_out() == shl)
1095       // Replace zero-extension-load with sign-extension-load
1096       return ld->as_Load()->convert_to_signed_load(*phase);
1097   }
1098 
1099   // Check for "(byte[i] <<24)>>24" which simply sign-extends
1100   if( shift == 24 &&
1101       (t3 = phase->type(shl->in(2))->isa_int()) &&
1102       t3->is_con(24) ) {
1103     Node *ld = shl->in(1);
1104     if (ld->Opcode() == Op_LoadB) {
1105       // Sign extension is just useless here
1106       set_req_X(1, ld, phase);
1107       set_req_X(2, phase->intcon(0), phase);
1108       return this;
1109     }
1110   }
1111 
1112   return NULL;
1113 }
1114 
1115 //------------------------------Value------------------------------------------
1116 // A RShiftINode shifts its input2 right by input1 amount.
1117 const Type* RShiftINode::Value(PhaseGVN* phase) const {
1118   const Type *t1 = phase->type( in(1) );
1119   const Type *t2 = phase->type( in(2) );
1120   // Either input is TOP ==> the result is TOP
1121   if( t1 == Type::TOP ) return Type::TOP;
1122   if( t2 == Type::TOP ) return Type::TOP;
1123 
1124   // Left input is ZERO ==> the result is ZERO.
1125   if( t1 == TypeInt::ZERO ) return TypeInt::ZERO;
1126   // Shift by zero does nothing
1127   if( t2 == TypeInt::ZERO ) return t1;
1128 
1129   // Either input is BOTTOM ==> the result is BOTTOM
1130   if (t1 == Type::BOTTOM || t2 == Type::BOTTOM)
1131     return TypeInt::INT;
1132 
1133   if (t2 == TypeInt::INT)
1134     return TypeInt::INT;
1135 
1136   const TypeInt *r1 = t1->is_int(); // Handy access
1137   const TypeInt *r2 = t2->is_int(); // Handy access
1138 
1139   // If the shift is a constant, just shift the bounds of the type.
1140   // For example, if the shift is 31, we just propagate sign bits.
1141   if (r2->is_con()) {
1142     uint shift = r2->get_con();
1143     shift &= BitsPerJavaInteger-1;  // semantics of Java shifts
1144     // Shift by a multiple of 32 does nothing:
1145     if (shift == 0)  return t1;
1146     // Calculate reasonably aggressive bounds for the result.
1147     // This is necessary if we are to correctly type things
1148     // like (x<<24>>24) == ((byte)x).
1149     jint lo = (jint)r1->_lo >> (jint)shift;
1150     jint hi = (jint)r1->_hi >> (jint)shift;
1151     assert(lo <= hi, "must have valid bounds");
1152     const TypeInt* ti = TypeInt::make(lo, hi, MAX2(r1->_widen,r2->_widen));
1153 #ifdef ASSERT
1154     // Make sure we get the sign-capture idiom correct.
1155     if (shift == BitsPerJavaInteger-1) {
1156       if (r1->_lo >= 0) assert(ti == TypeInt::ZERO,    ">>31 of + is  0");
1157       if (r1->_hi <  0) assert(ti == TypeInt::MINUS_1, ">>31 of - is -1");
1158     }
1159 #endif
1160     return ti;
1161   }
1162 
1163   if( !r1->is_con() || !r2->is_con() )
1164     return TypeInt::INT;
1165 
1166   // Signed shift right
1167   return TypeInt::make( r1->get_con() >> (r2->get_con()&31) );
1168 }
1169 
1170 //=============================================================================
1171 //------------------------------Identity---------------------------------------
1172 Node* RShiftLNode::Identity(PhaseGVN* phase) {
1173   const TypeInt *ti = phase->type(in(2))->isa_int(); // Shift count is an int.
1174   return (ti && ti->is_con() && (ti->get_con() & (BitsPerJavaLong - 1)) == 0) ? in(1) : this;
1175 }
1176 
1177 //------------------------------Value------------------------------------------
1178 // A RShiftLNode shifts its input2 right by input1 amount.
1179 const Type* RShiftLNode::Value(PhaseGVN* phase) const {
1180   const Type *t1 = phase->type( in(1) );
1181   const Type *t2 = phase->type( in(2) );
1182   // Either input is TOP ==> the result is TOP
1183   if( t1 == Type::TOP ) return Type::TOP;
1184   if( t2 == Type::TOP ) return Type::TOP;
1185 
1186   // Left input is ZERO ==> the result is ZERO.
1187   if( t1 == TypeLong::ZERO ) return TypeLong::ZERO;
1188   // Shift by zero does nothing
1189   if( t2 == TypeInt::ZERO ) return t1;
1190 
1191   // Either input is BOTTOM ==> the result is BOTTOM
1192   if (t1 == Type::BOTTOM || t2 == Type::BOTTOM)
1193     return TypeLong::LONG;
1194 
1195   if (t2 == TypeInt::INT)
1196     return TypeLong::LONG;
1197 
1198   const TypeLong *r1 = t1->is_long(); // Handy access
1199   const TypeInt  *r2 = t2->is_int (); // Handy access
1200 
1201   // If the shift is a constant, just shift the bounds of the type.
1202   // For example, if the shift is 63, we just propagate sign bits.
1203   if (r2->is_con()) {
1204     uint shift = r2->get_con();
1205     shift &= (2*BitsPerJavaInteger)-1;  // semantics of Java shifts
1206     // Shift by a multiple of 64 does nothing:
1207     if (shift == 0)  return t1;
1208     // Calculate reasonably aggressive bounds for the result.
1209     // This is necessary if we are to correctly type things
1210     // like (x<<24>>24) == ((byte)x).
1211     jlong lo = (jlong)r1->_lo >> (jlong)shift;
1212     jlong hi = (jlong)r1->_hi >> (jlong)shift;
1213     assert(lo <= hi, "must have valid bounds");
1214     const TypeLong* tl = TypeLong::make(lo, hi, MAX2(r1->_widen,r2->_widen));
1215     #ifdef ASSERT
1216     // Make sure we get the sign-capture idiom correct.
1217     if (shift == (2*BitsPerJavaInteger)-1) {
1218       if (r1->_lo >= 0) assert(tl == TypeLong::ZERO,    ">>63 of + is 0");
1219       if (r1->_hi < 0)  assert(tl == TypeLong::MINUS_1, ">>63 of - is -1");
1220     }
1221     #endif
1222     return tl;
1223   }
1224 
1225   return TypeLong::LONG;                // Give up
1226 }
1227 
1228 //=============================================================================
1229 //------------------------------Identity---------------------------------------
1230 Node* URShiftINode::Identity(PhaseGVN* phase) {
1231   int count = 0;
1232   if (const_shift_count(phase, this, &count) && (count & (BitsPerJavaInteger - 1)) == 0) {
1233     // Shift by a multiple of 32 does nothing
1234     return in(1);
1235   }
1236 
1237   // Check for "((x << LogBytesPerWord) + (wordSize-1)) >> LogBytesPerWord" which is just "x".
1238   // Happens during new-array length computation.
1239   // Safe if 'x' is in the range [0..(max_int>>LogBytesPerWord)]
1240   Node *add = in(1);
1241   if (add->Opcode() == Op_AddI) {
1242     const TypeInt *t2 = phase->type(add->in(2))->isa_int();
1243     if (t2 && t2->is_con(wordSize - 1) &&
1244         add->in(1)->Opcode() == Op_LShiftI) {
1245       // Check that shift_counts are LogBytesPerWord.
1246       Node          *lshift_count   = add->in(1)->in(2);
1247       const TypeInt *t_lshift_count = phase->type(lshift_count)->isa_int();
1248       if (t_lshift_count && t_lshift_count->is_con(LogBytesPerWord) &&
1249           t_lshift_count == phase->type(in(2))) {
1250         Node          *x   = add->in(1)->in(1);
1251         const TypeInt *t_x = phase->type(x)->isa_int();
1252         if (t_x != NULL && 0 <= t_x->_lo && t_x->_hi <= (max_jint>>LogBytesPerWord)) {
1253           return x;
1254         }
1255       }
1256     }
1257   }
1258 
1259   return (phase->type(in(2))->higher_equal(TypeInt::ZERO)) ? in(1) : this;
1260 }
1261 
1262 //------------------------------Ideal------------------------------------------
1263 Node *URShiftINode::Ideal(PhaseGVN *phase, bool can_reshape) {
1264   int con = maskShiftAmount(phase, this, BitsPerJavaInteger);
1265   if (con == 0) {
1266     return NULL;
1267   }
1268 
1269   // We'll be wanting the right-shift amount as a mask of that many bits
1270   const int mask = right_n_bits(BitsPerJavaInteger - con);
1271 
1272   int in1_op = in(1)->Opcode();
1273 
1274   // Check for ((x>>>a)>>>b) and replace with (x>>>(a+b)) when a+b < 32
1275   if( in1_op == Op_URShiftI ) {
1276     const TypeInt *t12 = phase->type( in(1)->in(2) )->isa_int();
1277     if( t12 && t12->is_con() ) { // Right input is a constant
1278       assert( in(1) != in(1)->in(1), "dead loop in URShiftINode::Ideal" );
1279       const int con2 = t12->get_con() & 31; // Shift count is always masked
1280       const int con3 = con+con2;
1281       if( con3 < 32 )           // Only merge shifts if total is < 32
1282         return new URShiftINode( in(1)->in(1), phase->intcon(con3) );
1283     }
1284   }
1285 
1286   // Check for ((x << z) + Y) >>> z.  Replace with x + con>>>z
1287   // The idiom for rounding to a power of 2 is "(Q+(2^z-1)) >>> z".
1288   // If Q is "X << z" the rounding is useless.  Look for patterns like
1289   // ((X<<Z) + Y) >>> Z  and replace with (X + Y>>>Z) & Z-mask.
1290   Node *add = in(1);
1291   const TypeInt *t2 = phase->type(in(2))->isa_int();
1292   if (in1_op == Op_AddI) {
1293     Node *lshl = add->in(1);
1294     if( lshl->Opcode() == Op_LShiftI &&
1295         phase->type(lshl->in(2)) == t2 ) {
1296       Node *y_z = phase->transform( new URShiftINode(add->in(2),in(2)) );
1297       Node *sum = phase->transform( new AddINode( lshl->in(1), y_z ) );
1298       return new AndINode( sum, phase->intcon(mask) );
1299     }
1300   }
1301 
1302   // Check for (x & mask) >>> z.  Replace with (x >>> z) & (mask >>> z)
1303   // This shortens the mask.  Also, if we are extracting a high byte and
1304   // storing it to a buffer, the mask will be removed completely.
1305   Node *andi = in(1);
1306   if( in1_op == Op_AndI ) {
1307     const TypeInt *t3 = phase->type( andi->in(2) )->isa_int();
1308     if( t3 && t3->is_con() ) { // Right input is a constant
1309       jint mask2 = t3->get_con();
1310       mask2 >>= con;  // *signed* shift downward (high-order zeroes do not help)
1311       Node *newshr = phase->transform( new URShiftINode(andi->in(1), in(2)) );
1312       return new AndINode(newshr, phase->intcon(mask2));
1313       // The negative values are easier to materialize than positive ones.
1314       // A typical case from address arithmetic is ((x & ~15) >> 4).
1315       // It's better to change that to ((x >> 4) & ~0) versus
1316       // ((x >> 4) & 0x0FFFFFFF).  The difference is greatest in LP64.
1317     }
1318   }
1319 
1320   // Check for "(X << z ) >>> z" which simply zero-extends
1321   Node *shl = in(1);
1322   if( in1_op == Op_LShiftI &&
1323       phase->type(shl->in(2)) == t2 )
1324     return new AndINode( shl->in(1), phase->intcon(mask) );
1325 
1326   // Check for (x >> n) >>> 31. Replace with (x >>> 31)
1327   Node *shr = in(1);
1328   if ( in1_op == Op_RShiftI ) {
1329     Node *in11 = shr->in(1);
1330     Node *in12 = shr->in(2);
1331     const TypeInt *t11 = phase->type(in11)->isa_int();
1332     const TypeInt *t12 = phase->type(in12)->isa_int();
1333     if ( t11 && t2 && t2->is_con(31) && t12 && t12->is_con() ) {
1334       return new URShiftINode(in11, phase->intcon(31));
1335     }
1336   }
1337 
1338   return NULL;
1339 }
1340 
1341 //------------------------------Value------------------------------------------
1342 // A URShiftINode shifts its input2 right by input1 amount.
1343 const Type* URShiftINode::Value(PhaseGVN* phase) const {
1344   // (This is a near clone of RShiftINode::Value.)
1345   const Type *t1 = phase->type( in(1) );
1346   const Type *t2 = phase->type( in(2) );
1347   // Either input is TOP ==> the result is TOP
1348   if( t1 == Type::TOP ) return Type::TOP;
1349   if( t2 == Type::TOP ) return Type::TOP;
1350 
1351   // Left input is ZERO ==> the result is ZERO.
1352   if( t1 == TypeInt::ZERO ) return TypeInt::ZERO;
1353   // Shift by zero does nothing
1354   if( t2 == TypeInt::ZERO ) return t1;
1355 
1356   // Either input is BOTTOM ==> the result is BOTTOM
1357   if (t1 == Type::BOTTOM || t2 == Type::BOTTOM)
1358     return TypeInt::INT;
1359 
1360   if (t2 == TypeInt::INT)
1361     return TypeInt::INT;
1362 
1363   const TypeInt *r1 = t1->is_int();     // Handy access
1364   const TypeInt *r2 = t2->is_int();     // Handy access
1365 
1366   if (r2->is_con()) {
1367     uint shift = r2->get_con();
1368     shift &= BitsPerJavaInteger-1;  // semantics of Java shifts
1369     // Shift by a multiple of 32 does nothing:
1370     if (shift == 0)  return t1;
1371     // Calculate reasonably aggressive bounds for the result.
1372     jint lo = (juint)r1->_lo >> (juint)shift;
1373     jint hi = (juint)r1->_hi >> (juint)shift;
1374     if (r1->_hi >= 0 && r1->_lo < 0) {
1375       // If the type has both negative and positive values,
1376       // there are two separate sub-domains to worry about:
1377       // The positive half and the negative half.
1378       jint neg_lo = lo;
1379       jint neg_hi = (juint)-1 >> (juint)shift;
1380       jint pos_lo = (juint) 0 >> (juint)shift;
1381       jint pos_hi = hi;
1382       lo = MIN2(neg_lo, pos_lo);  // == 0
1383       hi = MAX2(neg_hi, pos_hi);  // == -1 >>> shift;
1384     }
1385     assert(lo <= hi, "must have valid bounds");
1386     const TypeInt* ti = TypeInt::make(lo, hi, MAX2(r1->_widen,r2->_widen));
1387     #ifdef ASSERT
1388     // Make sure we get the sign-capture idiom correct.
1389     if (shift == BitsPerJavaInteger-1) {
1390       if (r1->_lo >= 0) assert(ti == TypeInt::ZERO, ">>>31 of + is 0");
1391       if (r1->_hi < 0)  assert(ti == TypeInt::ONE,  ">>>31 of - is +1");
1392     }
1393     #endif
1394     return ti;
1395   }
1396 
1397   //
1398   // Do not support shifted oops in info for GC
1399   //
1400   // else if( t1->base() == Type::InstPtr ) {
1401   //
1402   //   const TypeInstPtr *o = t1->is_instptr();
1403   //   if( t1->singleton() )
1404   //     return TypeInt::make( ((uint32_t)o->const_oop() + o->_offset) >> shift );
1405   // }
1406   // else if( t1->base() == Type::KlassPtr ) {
1407   //   const TypeKlassPtr *o = t1->is_klassptr();
1408   //   if( t1->singleton() )
1409   //     return TypeInt::make( ((uint32_t)o->const_oop() + o->_offset) >> shift );
1410   // }
1411 
1412   return TypeInt::INT;
1413 }
1414 
1415 //=============================================================================
1416 //------------------------------Identity---------------------------------------
1417 Node* URShiftLNode::Identity(PhaseGVN* phase) {
1418   int count = 0;
1419   if (const_shift_count(phase, this, &count) && (count & (BitsPerJavaLong - 1)) == 0) {
1420     // Shift by a multiple of 64 does nothing
1421     return in(1);
1422   }
1423   return this;
1424 }
1425 
1426 //------------------------------Ideal------------------------------------------
1427 Node *URShiftLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1428   int con = maskShiftAmount(phase, this, BitsPerJavaLong);
1429   if (con == 0) {
1430     return NULL;
1431   }
1432 
1433   // We'll be wanting the right-shift amount as a mask of that many bits
1434   const jlong mask = jlong(max_julong >> con);
1435 
1436   // Check for ((x << z) + Y) >>> z.  Replace with x + con>>>z
1437   // The idiom for rounding to a power of 2 is "(Q+(2^z-1)) >>> z".
1438   // If Q is "X << z" the rounding is useless.  Look for patterns like
1439   // ((X<<Z) + Y) >>> Z  and replace with (X + Y>>>Z) & Z-mask.
1440   Node *add = in(1);
1441   const TypeInt *t2 = phase->type(in(2))->isa_int();
1442   if (add->Opcode() == Op_AddL) {
1443     Node *lshl = add->in(1);
1444     if( lshl->Opcode() == Op_LShiftL &&
1445         phase->type(lshl->in(2)) == t2 ) {
1446       Node *y_z = phase->transform( new URShiftLNode(add->in(2),in(2)) );
1447       Node *sum = phase->transform( new AddLNode( lshl->in(1), y_z ) );
1448       return new AndLNode( sum, phase->longcon(mask) );
1449     }
1450   }
1451 
1452   // Check for (x & mask) >>> z.  Replace with (x >>> z) & (mask >>> z)
1453   // This shortens the mask.  Also, if we are extracting a high byte and
1454   // storing it to a buffer, the mask will be removed completely.
1455   Node *andi = in(1);
1456   if( andi->Opcode() == Op_AndL ) {
1457     const TypeLong *t3 = phase->type( andi->in(2) )->isa_long();
1458     if( t3 && t3->is_con() ) { // Right input is a constant
1459       jlong mask2 = t3->get_con();
1460       mask2 >>= con;  // *signed* shift downward (high-order zeroes do not help)
1461       Node *newshr = phase->transform( new URShiftLNode(andi->in(1), in(2)) );
1462       return new AndLNode(newshr, phase->longcon(mask2));
1463     }
1464   }
1465 
1466   // Check for "(X << z ) >>> z" which simply zero-extends
1467   Node *shl = in(1);
1468   if( shl->Opcode() == Op_LShiftL &&
1469       phase->type(shl->in(2)) == t2 )
1470     return new AndLNode( shl->in(1), phase->longcon(mask) );
1471 
1472   // Check for (x >> n) >>> 63. Replace with (x >>> 63)
1473   Node *shr = in(1);
1474   if ( shr->Opcode() == Op_RShiftL ) {
1475     Node *in11 = shr->in(1);
1476     Node *in12 = shr->in(2);
1477     const TypeLong *t11 = phase->type(in11)->isa_long();
1478     const TypeInt *t12 = phase->type(in12)->isa_int();
1479     if ( t11 && t2 && t2->is_con(63) && t12 && t12->is_con() ) {
1480       return new URShiftLNode(in11, phase->intcon(63));
1481     }
1482   }
1483   return NULL;
1484 }
1485 
1486 //------------------------------Value------------------------------------------
1487 // A URShiftINode shifts its input2 right by input1 amount.
1488 const Type* URShiftLNode::Value(PhaseGVN* phase) const {
1489   // (This is a near clone of RShiftLNode::Value.)
1490   const Type *t1 = phase->type( in(1) );
1491   const Type *t2 = phase->type( in(2) );
1492   // Either input is TOP ==> the result is TOP
1493   if( t1 == Type::TOP ) return Type::TOP;
1494   if( t2 == Type::TOP ) return Type::TOP;
1495 
1496   // Left input is ZERO ==> the result is ZERO.
1497   if( t1 == TypeLong::ZERO ) return TypeLong::ZERO;
1498   // Shift by zero does nothing
1499   if( t2 == TypeInt::ZERO ) return t1;
1500 
1501   // Either input is BOTTOM ==> the result is BOTTOM
1502   if (t1 == Type::BOTTOM || t2 == Type::BOTTOM)
1503     return TypeLong::LONG;
1504 
1505   if (t2 == TypeInt::INT)
1506     return TypeLong::LONG;
1507 
1508   const TypeLong *r1 = t1->is_long(); // Handy access
1509   const TypeInt  *r2 = t2->is_int (); // Handy access
1510 
1511   if (r2->is_con()) {
1512     uint shift = r2->get_con();
1513     shift &= BitsPerJavaLong - 1;  // semantics of Java shifts
1514     // Shift by a multiple of 64 does nothing:
1515     if (shift == 0)  return t1;
1516     // Calculate reasonably aggressive bounds for the result.
1517     jlong lo = (julong)r1->_lo >> (juint)shift;
1518     jlong hi = (julong)r1->_hi >> (juint)shift;
1519     if (r1->_hi >= 0 && r1->_lo < 0) {
1520       // If the type has both negative and positive values,
1521       // there are two separate sub-domains to worry about:
1522       // The positive half and the negative half.
1523       jlong neg_lo = lo;
1524       jlong neg_hi = (julong)-1 >> (juint)shift;
1525       jlong pos_lo = (julong) 0 >> (juint)shift;
1526       jlong pos_hi = hi;
1527       //lo = MIN2(neg_lo, pos_lo);  // == 0
1528       lo = neg_lo < pos_lo ? neg_lo : pos_lo;
1529       //hi = MAX2(neg_hi, pos_hi);  // == -1 >>> shift;
1530       hi = neg_hi > pos_hi ? neg_hi : pos_hi;
1531     }
1532     assert(lo <= hi, "must have valid bounds");
1533     const TypeLong* tl = TypeLong::make(lo, hi, MAX2(r1->_widen,r2->_widen));
1534     #ifdef ASSERT
1535     // Make sure we get the sign-capture idiom correct.
1536     if (shift == BitsPerJavaLong - 1) {
1537       if (r1->_lo >= 0) assert(tl == TypeLong::ZERO, ">>>63 of + is 0");
1538       if (r1->_hi < 0)  assert(tl == TypeLong::ONE,  ">>>63 of - is +1");
1539     }
1540     #endif
1541     return tl;
1542   }
1543 
1544   return TypeLong::LONG;                // Give up
1545 }
1546 
1547 //=============================================================================
1548 //------------------------------Value------------------------------------------
1549 const Type* FmaDNode::Value(PhaseGVN* phase) const {
1550   const Type *t1 = phase->type(in(1));
1551   if (t1 == Type::TOP) return Type::TOP;
1552   if (t1->base() != Type::DoubleCon) return Type::DOUBLE;
1553   const Type *t2 = phase->type(in(2));
1554   if (t2 == Type::TOP) return Type::TOP;
1555   if (t2->base() != Type::DoubleCon) return Type::DOUBLE;
1556   const Type *t3 = phase->type(in(3));
1557   if (t3 == Type::TOP) return Type::TOP;
1558   if (t3->base() != Type::DoubleCon) return Type::DOUBLE;
1559 #ifndef __STDC_IEC_559__
1560   return Type::DOUBLE;
1561 #else
1562   double d1 = t1->getd();
1563   double d2 = t2->getd();
1564   double d3 = t3->getd();
1565   return TypeD::make(fma(d1, d2, d3));
1566 #endif
1567 }
1568 
1569 //=============================================================================
1570 //------------------------------Value------------------------------------------
1571 const Type* FmaFNode::Value(PhaseGVN* phase) const {
1572   const Type *t1 = phase->type(in(1));
1573   if (t1 == Type::TOP) return Type::TOP;
1574   if (t1->base() != Type::FloatCon) return Type::FLOAT;
1575   const Type *t2 = phase->type(in(2));
1576   if (t2 == Type::TOP) return Type::TOP;
1577   if (t2->base() != Type::FloatCon) return Type::FLOAT;
1578   const Type *t3 = phase->type(in(3));
1579   if (t3 == Type::TOP) return Type::TOP;
1580   if (t3->base() != Type::FloatCon) return Type::FLOAT;
1581 #ifndef __STDC_IEC_559__
1582   return Type::FLOAT;
1583 #else
1584   float f1 = t1->getf();
1585   float f2 = t2->getf();
1586   float f3 = t3->getf();
1587   return TypeF::make(fma(f1, f2, f3));
1588 #endif
1589 }
1590 
1591 //=============================================================================
1592 //------------------------------hash-------------------------------------------
1593 // Hash function for MulAddS2INode.  Operation is commutative with commutative pairs.
1594 // The hash function must return the same value when edge swapping is performed.
1595 uint MulAddS2INode::hash() const {
1596   return (uintptr_t)in(1) + (uintptr_t)in(2) + (uintptr_t)in(3) + (uintptr_t)in(4) + Opcode();
1597 }
1598 
1599 //------------------------------Rotate Operations ------------------------------
1600 
1601 Node* RotateLeftNode::Identity(PhaseGVN* phase) {
1602   const Type* t1 = phase->type(in(1));
1603   if (t1 == Type::TOP) {
1604     return this;
1605   }
1606   int count = 0;
1607   assert(t1->isa_int() || t1->isa_long(), "Unexpected type");
1608   int mask = (t1->isa_int() ? BitsPerJavaInteger : BitsPerJavaLong) - 1;
1609   if (const_shift_count(phase, this, &count) && (count & mask) == 0) {
1610     // Rotate by a multiple of 32/64 does nothing
1611     return in(1);
1612   }
1613   return this;
1614 }
1615 
1616 const Type* RotateLeftNode::Value(PhaseGVN* phase) const {
1617   const Type* t1 = phase->type(in(1));
1618   const Type* t2 = phase->type(in(2));
1619   // Either input is TOP ==> the result is TOP
1620   if (t1 == Type::TOP || t2 == Type::TOP) {
1621     return Type::TOP;
1622   }
1623 
1624   if (t1->isa_int()) {
1625     const TypeInt* r1 = t1->is_int();
1626     const TypeInt* r2 = t2->is_int();
1627 
1628     // Left input is ZERO ==> the result is ZERO.
1629     if (r1 == TypeInt::ZERO) {
1630       return TypeInt::ZERO;
1631     }
1632     // Rotate by zero does nothing
1633     if (r2 == TypeInt::ZERO) {
1634       return r1;
1635     }
1636     if (r1->is_con() && r2->is_con()) {
1637       juint r1_con = (juint)r1->get_con();
1638       juint shift = (juint)(r2->get_con()) & (juint)(BitsPerJavaInteger - 1); // semantics of Java shifts
1639       return TypeInt::make((r1_con << shift) | (r1_con >> (32 - shift)));
1640     }
1641     return TypeInt::INT;
1642   } else {
1643     assert(t1->isa_long(), "Type must be a long");
1644     const TypeLong* r1 = t1->is_long();
1645     const TypeInt*  r2 = t2->is_int();
1646 
1647     // Left input is ZERO ==> the result is ZERO.
1648     if (r1 == TypeLong::ZERO) {
1649       return TypeLong::ZERO;
1650     }
1651     // Rotate by zero does nothing
1652     if (r2 == TypeInt::ZERO) {
1653       return r1;
1654     }
1655     if (r1->is_con() && r2->is_con()) {
1656       julong r1_con = (julong)r1->get_con();
1657       julong shift = (julong)(r2->get_con()) & (julong)(BitsPerJavaLong - 1); // semantics of Java shifts
1658       return TypeLong::make((r1_con << shift) | (r1_con >> (64 - shift)));
1659     }
1660     return TypeLong::LONG;
1661   }
1662 }
1663 
1664 Node* RotateLeftNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1665   const Type* t1 = phase->type(in(1));
1666   const Type* t2 = phase->type(in(2));
1667   if (t2->isa_int() && t2->is_int()->is_con()) {
1668     if (t1->isa_int()) {
1669       int lshift = t2->is_int()->get_con() & 31;
1670       return new RotateRightNode(in(1), phase->intcon(32 - (lshift & 31)), TypeInt::INT);
1671     } else if (t1 != Type::TOP) {
1672       assert(t1->isa_long(), "Type must be a long");
1673       int lshift = t2->is_int()->get_con() & 63;
1674       return new RotateRightNode(in(1), phase->intcon(64 - (lshift & 63)), TypeLong::LONG);
1675     }
1676   }
1677   return NULL;
1678 }
1679 
1680 Node* RotateRightNode::Identity(PhaseGVN* phase) {
1681   const Type* t1 = phase->type(in(1));
1682   if (t1 == Type::TOP) {
1683     return this;
1684   }
1685   int count = 0;
1686   assert(t1->isa_int() || t1->isa_long(), "Unexpected type");
1687   int mask = (t1->isa_int() ? BitsPerJavaInteger : BitsPerJavaLong) - 1;
1688   if (const_shift_count(phase, this, &count) && (count & mask) == 0) {
1689     // Rotate by a multiple of 32/64 does nothing
1690     return in(1);
1691   }
1692   return this;
1693 }
1694 
1695 const Type* RotateRightNode::Value(PhaseGVN* phase) const {
1696   const Type* t1 = phase->type(in(1));
1697   const Type* t2 = phase->type(in(2));
1698   // Either input is TOP ==> the result is TOP
1699   if (t1 == Type::TOP || t2 == Type::TOP) {
1700     return Type::TOP;
1701   }
1702 
1703   if (t1->isa_int()) {
1704     const TypeInt* r1 = t1->is_int();
1705     const TypeInt* r2 = t2->is_int();
1706 
1707     // Left input is ZERO ==> the result is ZERO.
1708     if (r1 == TypeInt::ZERO) {
1709       return TypeInt::ZERO;
1710     }
1711     // Rotate by zero does nothing
1712     if (r2 == TypeInt::ZERO) {
1713       return r1;
1714     }
1715     if (r1->is_con() && r2->is_con()) {
1716       juint r1_con = (juint)r1->get_con();
1717       juint shift = (juint)(r2->get_con()) & (juint)(BitsPerJavaInteger - 1); // semantics of Java shifts
1718       return TypeInt::make((r1_con >> shift) | (r1_con << (32 - shift)));
1719     }
1720     return TypeInt::INT;
1721   } else {
1722     assert(t1->isa_long(), "Type must be a long");
1723     const TypeLong* r1 = t1->is_long();
1724     const TypeInt*  r2 = t2->is_int();
1725     // Left input is ZERO ==> the result is ZERO.
1726     if (r1 == TypeLong::ZERO) {
1727       return TypeLong::ZERO;
1728     }
1729     // Rotate by zero does nothing
1730     if (r2 == TypeInt::ZERO) {
1731       return r1;
1732     }
1733     if (r1->is_con() && r2->is_con()) {
1734       julong r1_con = (julong)r1->get_con();
1735       julong shift = (julong)(r2->get_con()) & (julong)(BitsPerJavaLong - 1); // semantics of Java shifts
1736       return TypeLong::make((r1_con >> shift) | (r1_con << (64 - shift)));
1737     }
1738     return TypeLong::LONG;
1739   }
1740 }
1741 
1742 // Given an expression (AndX shift mask) or (AndX mask shift),
1743 // determine if the AndX must always produce zero, because the
1744 // the shift (x<<N) is bitwise disjoint from the mask #M.
1745 // The X in AndX must be I or L, depending on bt.
1746 // Specifically, the following cases fold to zero,
1747 // when the shift value N is large enough to zero out
1748 // all the set positions of the and-mask M.
1749 //   (AndI (LShiftI _ #N) #M) => #0
1750 //   (AndL (LShiftL _ #N) #M) => #0
1751 //   (AndL (ConvI2L (LShiftI _ #N)) #M) => #0
1752 // The M and N values must satisfy ((-1 << N) & M) == 0.
1753 // Because the optimization might work for a non-constant
1754 // mask M, we check the AndX for both operand orders.
1755 bool MulNode::AndIL_shift_and_mask_is_always_zero(PhaseGVN* phase, Node* shift, Node* mask, BasicType bt, bool check_reverse) {
1756   if (mask == NULL || shift == NULL) {
1757     return false;
1758   }
1759   shift = shift->uncast();
1760   if (shift == NULL) {
1761     return false;
1762   }
1763   const TypeInteger* mask_t = phase->type(mask)->isa_integer(bt);
1764   const TypeInteger* shift_t = phase->type(shift)->isa_integer(bt);
1765   if (mask_t == NULL || shift_t == NULL) {
1766     return false;
1767   }
1768   BasicType shift_bt = bt;
1769   if (bt == T_LONG && shift->Opcode() == Op_ConvI2L) {
1770     bt = T_INT;
1771     Node* val = shift->in(1);
1772     if (val == NULL) {
1773       return false;
1774     }
1775     val = val->uncast();
1776     if (val == NULL) {
1777       return false;
1778     }
1779     if (val->Opcode() == Op_LShiftI) {
1780       shift_bt = T_INT;
1781       shift = val;
1782     }
1783   }
1784   if (shift->Opcode() != Op_LShift(shift_bt)) {
1785     if (check_reverse &&
1786         (mask->Opcode() == Op_LShift(bt) ||
1787          (bt == T_LONG && mask->Opcode() == Op_ConvI2L))) {
1788       // try it the other way around
1789       return AndIL_shift_and_mask_is_always_zero(phase, mask, shift, bt, false);
1790     }
1791     return false;
1792   }
1793   Node* shift2 = shift->in(2);
1794   if (shift2 == NULL) {
1795     return false;
1796   }
1797   const Type* shift2_t = phase->type(shift2);
1798   if (!shift2_t->isa_int() || !shift2_t->is_int()->is_con()) {
1799     return false;
1800   }
1801 
1802   jint shift_con = shift2_t->is_int()->get_con() & ((shift_bt == T_INT ? BitsPerJavaInteger : BitsPerJavaLong) - 1);
1803   if ((((jlong)1) << shift_con) > mask_t->hi_as_long() && mask_t->lo_as_long() >= 0) {
1804     return true;
1805   }
1806 
1807   return false;
1808 }
1809 
1810 // Given an expression (AndX (AddX v1 (LShiftX v2 #N)) #M)
1811 // determine if the AndX must always produce (AndX v1 #M),
1812 // because the shift (v2<<N) is bitwise disjoint from the mask #M.
1813 // The X in AndX will be I or L, depending on bt.
1814 // Specifically, the following cases fold,
1815 // when the shift value N is large enough to zero out
1816 // all the set positions of the and-mask M.
1817 //   (AndI (AddI v1 (LShiftI _ #N)) #M) => (AndI v1 #M)
1818 //   (AndL (AddI v1 (LShiftL _ #N)) #M) => (AndL v1 #M)
1819 //   (AndL (AddL v1 (ConvI2L (LShiftI _ #N))) #M) => (AndL v1 #M)
1820 // The M and N values must satisfy ((-1 << N) & M) == 0.
1821 // Because the optimization might work for a non-constant
1822 // mask M, and because the AddX operands can come in either
1823 // order, we check for every operand order.
1824 Node* MulNode::AndIL_add_shift_and_mask(PhaseGVN* phase, BasicType bt) {
1825   Node* add = in(1);
1826   Node* mask = in(2);
1827   if (add == NULL || mask == NULL) {
1828     return NULL;
1829   }
1830   int addidx = 0;
1831   if (add->Opcode() == Op_Add(bt)) {
1832     addidx = 1;
1833   } else if (mask->Opcode() == Op_Add(bt)) {
1834     mask = add;
1835     addidx = 2;
1836     add = in(addidx);
1837   }
1838   if (addidx > 0) {
1839     Node* add1 = add->in(1);
1840     Node* add2 = add->in(2);
1841     if (add1 != NULL && add2 != NULL) {
1842       if (AndIL_shift_and_mask_is_always_zero(phase, add1, mask, bt, false)) {
1843         set_req_X(addidx, add2, phase);
1844         return this;
1845       } else if (AndIL_shift_and_mask_is_always_zero(phase, add2, mask, bt, false)) {
1846         set_req_X(addidx, add1, phase);
1847         return this;
1848       }
1849     }
1850   }
1851   return NULL;
1852 }