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