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