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