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