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