1 /*
   2  * Copyright 1997-2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 // Portions of code courtesy of Clifford Click
  26 
  27 #include "incls/_precompiled.incl"
  28 #include "incls/_mulnode.cpp.incl"
  29 
  30 
  31 //=============================================================================
  32 //------------------------------hash-------------------------------------------
  33 // Hash function over MulNodes.  Needs to be commutative; i.e., I swap
  34 // (commute) inputs to MulNodes willy-nilly so the hash function must return
  35 // the same value in the presence of edge swapping.
  36 uint MulNode::hash() const {
  37   return (uintptr_t)in(1) + (uintptr_t)in(2) + Opcode();
  38 }
  39 
  40 //------------------------------Identity---------------------------------------
  41 // Multiplying a one preserves the other argument
  42 Node *MulNode::Identity( PhaseTransform *phase ) {
  43   register const Type *one = mul_id();  // The multiplicative identity
  44   if( phase->type( in(1) )->higher_equal( one ) ) return in(2);
  45   if( phase->type( in(2) )->higher_equal( one ) ) return in(1);
  46 
  47   return this;
  48 }
  49 
  50 //------------------------------Ideal------------------------------------------
  51 // We also canonicalize the Node, moving constants to the right input,
  52 // and flatten expressions (so that 1+x+2 becomes x+3).
  53 Node *MulNode::Ideal(PhaseGVN *phase, bool can_reshape) {
  54   const Type *t1 = phase->type( in(1) );
  55   const Type *t2 = phase->type( in(2) );
  56   Node *progress = NULL;        // Progress flag
  57   // We are OK if right is a constant, or right is a load and
  58   // left is a non-constant.
  59   if( !(t2->singleton() ||
  60         (in(2)->is_Load() && !(t1->singleton() || in(1)->is_Load())) ) ) {
  61     if( t1->singleton() ||       // Left input is a constant?
  62         // Otherwise, sort inputs (commutativity) to help value numbering.
  63         (in(1)->_idx > in(2)->_idx) ) {
  64       swap_edges(1, 2);
  65       const Type *t = t1;
  66       t1 = t2;
  67       t2 = t;
  68       progress = this;            // Made progress
  69     }
  70   }
  71 
  72   // If the right input is a constant, and the left input is a product of a
  73   // constant, flatten the expression tree.
  74   uint op = Opcode();
  75   if( t2->singleton() &&        // Right input is a constant?
  76       op != Op_MulF &&          // Float & double cannot reassociate
  77       op != Op_MulD ) {
  78     if( t2 == Type::TOP ) return NULL;
  79     Node *mul1 = in(1);
  80 #ifdef ASSERT
  81     // Check for dead loop
  82     int   op1 = mul1->Opcode();
  83     if( phase->eqv( mul1, this ) || phase->eqv( in(2), this ) ||
  84         ( op1 == mul_opcode() || op1 == add_opcode() ) &&
  85         ( phase->eqv( mul1->in(1), this ) || phase->eqv( mul1->in(2), this ) ||
  86           phase->eqv( mul1->in(1), mul1 ) || phase->eqv( mul1->in(2), mul1 ) ) )
  87       assert(false, "dead loop in MulNode::Ideal");
  88 #endif
  89 
  90     if( mul1->Opcode() == mul_opcode() ) {  // Left input is a multiply?
  91       // Mul of a constant?
  92       const Type *t12 = phase->type( mul1->in(2) );
  93       if( t12->singleton() && t12 != Type::TOP) { // Left input is an add of a constant?
  94         // Compute new constant; check for overflow
  95         const Type *tcon01 = mul1->as_Mul()->mul_ring(t2,t12);
  96         if( tcon01->singleton() ) {
  97           // The Mul of the flattened expression
  98           set_req(1, mul1->in(1));
  99           set_req(2, phase->makecon( tcon01 ));
 100           t2 = tcon01;
 101           progress = this;      // Made progress
 102         }
 103       }
 104     }
 105     // If the right input is a constant, and the left input is an add of a
 106     // constant, flatten the tree: (X+con1)*con0 ==> X*con0 + con1*con0
 107     const Node *add1 = in(1);
 108     if( add1->Opcode() == add_opcode() ) {      // Left input is an add?
 109       // Add of a constant?
 110       const Type *t12 = phase->type( add1->in(2) );
 111       if( t12->singleton() && t12 != Type::TOP ) { // Left input is an add of a constant?
 112         assert( add1->in(1) != add1, "dead loop in MulNode::Ideal" );
 113         // Compute new constant; check for overflow
 114         const Type *tcon01 = mul_ring(t2,t12);
 115         if( tcon01->singleton() ) {
 116 
 117         // Convert (X+con1)*con0 into X*con0
 118           Node *mul = clone();    // mul = ()*con0
 119           mul->set_req(1,add1->in(1));  // mul = X*con0
 120           mul = phase->transform(mul);
 121 
 122           Node *add2 = add1->clone();
 123           add2->set_req(1, mul);        // X*con0 + con0*con1
 124           add2->set_req(2, phase->makecon(tcon01) );
 125           progress = add2;
 126         }
 127       }
 128     } // End of is left input an add
 129   } // End of is right input a Mul
 130 
 131   return progress;
 132 }
 133 
 134 //------------------------------Value-----------------------------------------
 135 const Type *MulNode::Value( PhaseTransform *phase ) const {
 136   const Type *t1 = phase->type( in(1) );
 137   const Type *t2 = phase->type( in(2) );
 138   // Either input is TOP ==> the result is TOP
 139   if( t1 == Type::TOP ) return Type::TOP;
 140   if( t2 == Type::TOP ) return Type::TOP;
 141 
 142   // Either input is ZERO ==> the result is ZERO.
 143   // Not valid for floats or doubles since +0.0 * -0.0 --> +0.0
 144   int op = Opcode();
 145   if( op == Op_MulI || op == Op_AndI || op == Op_MulL || op == Op_AndL ) {
 146     const Type *zero = add_id();        // The multiplicative zero
 147     if( t1->higher_equal( zero ) ) return zero;
 148     if( t2->higher_equal( zero ) ) return zero;
 149   }
 150 
 151   // Either input is BOTTOM ==> the result is the local BOTTOM
 152   if( t1 == Type::BOTTOM || t2 == Type::BOTTOM )
 153     return bottom_type();
 154 
 155 #if defined(IA32)
 156   // Can't trust native compilers to properly fold strict double
 157   // multiplication with round-to-zero on this platform.
 158   if (op == Op_MulD && phase->C->method()->is_strict()) {
 159     return TypeD::DOUBLE;
 160   }
 161 #endif
 162 
 163   return mul_ring(t1,t2);            // Local flavor of type multiplication
 164 }
 165 
 166 
 167 //=============================================================================
 168 //------------------------------Ideal------------------------------------------
 169 // Check for power-of-2 multiply, then try the regular MulNode::Ideal
 170 Node *MulINode::Ideal(PhaseGVN *phase, bool can_reshape) {
 171   // Swap constant to right
 172   jint con;
 173   if ((con = in(1)->find_int_con(0)) != 0) {
 174     swap_edges(1, 2);
 175     // Finish rest of method to use info in 'con'
 176   } else if ((con = in(2)->find_int_con(0)) == 0) {
 177     return MulNode::Ideal(phase, can_reshape);
 178   }
 179 
 180   // Now we have a constant Node on the right and the constant in con
 181   if( con == 0 ) return NULL;   // By zero is handled by Value call
 182   if( con == 1 ) return NULL;   // By one  is handled by Identity call
 183 
 184   // Check for negative constant; if so negate the final result
 185   bool sign_flip = false;
 186   if( con < 0 ) {
 187     con = -con;
 188     sign_flip = true;
 189   }
 190 
 191   // Get low bit; check for being the only bit
 192   Node *res = NULL;
 193   jint bit1 = con & -con;       // Extract low bit
 194   if( bit1 == con ) {           // Found a power of 2?
 195     res = new (phase->C, 3) LShiftINode( in(1), phase->intcon(log2_intptr(bit1)) );
 196   } else {
 197 
 198     // Check for constant with 2 bits set
 199     jint bit2 = con-bit1;
 200     bit2 = bit2 & -bit2;          // Extract 2nd bit
 201     if( bit2 + bit1 == con ) {    // Found all bits in con?
 202       Node *n1 = phase->transform( new (phase->C, 3) LShiftINode( in(1), phase->intcon(log2_intptr(bit1)) ) );
 203       Node *n2 = phase->transform( new (phase->C, 3) LShiftINode( in(1), phase->intcon(log2_intptr(bit2)) ) );
 204       res = new (phase->C, 3) AddINode( n2, n1 );
 205 
 206     } else if (is_power_of_2(con+1)) {
 207       // Sleezy: power-of-2 -1.  Next time be generic.
 208       jint temp = (jint) (con + 1);
 209       Node *n1 = phase->transform( new (phase->C, 3) LShiftINode( in(1), phase->intcon(log2_intptr(temp)) ) );
 210       res = new (phase->C, 3) SubINode( n1, in(1) );
 211     } else {
 212       return MulNode::Ideal(phase, can_reshape);
 213     }
 214   }
 215 
 216   if( sign_flip ) {             // Need to negate result?
 217     res = phase->transform(res);// Transform, before making the zero con
 218     res = new (phase->C, 3) SubINode(phase->intcon(0),res);
 219   }
 220 
 221   return res;                   // Return final result
 222 }
 223 
 224 //------------------------------mul_ring---------------------------------------
 225 // Compute the product type of two integer ranges into this node.
 226 const Type *MulINode::mul_ring(const Type *t0, const Type *t1) const {
 227   const TypeInt *r0 = t0->is_int(); // Handy access
 228   const TypeInt *r1 = t1->is_int();
 229 
 230   // Fetch endpoints of all ranges
 231   int32 lo0 = r0->_lo;
 232   double a = (double)lo0;
 233   int32 hi0 = r0->_hi;
 234   double b = (double)hi0;
 235   int32 lo1 = r1->_lo;
 236   double c = (double)lo1;
 237   int32 hi1 = r1->_hi;
 238   double d = (double)hi1;
 239 
 240   // Compute all endpoints & check for overflow
 241   int32 A = lo0*lo1;
 242   if( (double)A != a*c ) return TypeInt::INT; // Overflow?
 243   int32 B = lo0*hi1;
 244   if( (double)B != a*d ) return TypeInt::INT; // Overflow?
 245   int32 C = hi0*lo1;
 246   if( (double)C != b*c ) return TypeInt::INT; // Overflow?
 247   int32 D = hi0*hi1;
 248   if( (double)D != b*d ) return TypeInt::INT; // Overflow?
 249 
 250   if( A < B ) { lo0 = A; hi0 = B; } // Sort range endpoints
 251   else { lo0 = B; hi0 = A; }
 252   if( C < D ) {
 253     if( C < lo0 ) lo0 = C;
 254     if( D > hi0 ) hi0 = D;
 255   } else {
 256     if( D < lo0 ) lo0 = D;
 257     if( C > hi0 ) hi0 = C;
 258   }
 259   return TypeInt::make(lo0, hi0, MAX2(r0->_widen,r1->_widen));
 260 }
 261 
 262 
 263 //=============================================================================
 264 //------------------------------Ideal------------------------------------------
 265 // Check for power-of-2 multiply, then try the regular MulNode::Ideal
 266 Node *MulLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 267   // Swap constant to right
 268   jlong con;
 269   if ((con = in(1)->find_long_con(0)) != 0) {
 270     swap_edges(1, 2);
 271     // Finish rest of method to use info in 'con'
 272   } else if ((con = in(2)->find_long_con(0)) == 0) {
 273     return MulNode::Ideal(phase, can_reshape);
 274   }
 275 
 276   // Now we have a constant Node on the right and the constant in con
 277   if( con == CONST64(0) ) return NULL;  // By zero is handled by Value call
 278   if( con == CONST64(1) ) return NULL;  // By one  is handled by Identity call
 279 
 280   // Check for negative constant; if so negate the final result
 281   bool sign_flip = false;
 282   if( con < 0 ) {
 283     con = -con;
 284     sign_flip = true;
 285   }
 286 
 287   // Get low bit; check for being the only bit
 288   Node *res = NULL;
 289   jlong bit1 = con & -con;      // Extract low bit
 290   if( bit1 == con ) {           // Found a power of 2?
 291     res = new (phase->C, 3) LShiftLNode( in(1), phase->intcon(log2_long(bit1)) );
 292   } else {
 293 
 294     // Check for constant with 2 bits set
 295     jlong bit2 = con-bit1;
 296     bit2 = bit2 & -bit2;          // Extract 2nd bit
 297     if( bit2 + bit1 == con ) {    // Found all bits in con?
 298       Node *n1 = phase->transform( new (phase->C, 3) LShiftLNode( in(1), phase->intcon(log2_long(bit1)) ) );
 299       Node *n2 = phase->transform( new (phase->C, 3) LShiftLNode( in(1), phase->intcon(log2_long(bit2)) ) );
 300       res = new (phase->C, 3) AddLNode( n2, n1 );
 301 
 302     } else if (is_power_of_2_long(con+1)) {
 303       // Sleezy: power-of-2 -1.  Next time be generic.
 304       jlong temp = (jlong) (con + 1);
 305       Node *n1 = phase->transform( new (phase->C, 3) LShiftLNode( in(1), phase->intcon(log2_long(temp)) ) );
 306       res = new (phase->C, 3) SubLNode( n1, in(1) );
 307     } else {
 308       return MulNode::Ideal(phase, can_reshape);
 309     }
 310   }
 311 
 312   if( sign_flip ) {             // Need to negate result?
 313     res = phase->transform(res);// Transform, before making the zero con
 314     res = new (phase->C, 3) SubLNode(phase->longcon(0),res);
 315   }
 316 
 317   return res;                   // Return final result
 318 }
 319 
 320 //------------------------------mul_ring---------------------------------------
 321 // Compute the product type of two integer ranges into this node.
 322 const Type *MulLNode::mul_ring(const Type *t0, const Type *t1) const {
 323   const TypeLong *r0 = t0->is_long(); // Handy access
 324   const TypeLong *r1 = t1->is_long();
 325 
 326   // Fetch endpoints of all ranges
 327   jlong lo0 = r0->_lo;
 328   double a = (double)lo0;
 329   jlong hi0 = r0->_hi;
 330   double b = (double)hi0;
 331   jlong lo1 = r1->_lo;
 332   double c = (double)lo1;
 333   jlong hi1 = r1->_hi;
 334   double d = (double)hi1;
 335 
 336   // Compute all endpoints & check for overflow
 337   jlong A = lo0*lo1;
 338   if( (double)A != a*c ) return TypeLong::LONG; // Overflow?
 339   jlong B = lo0*hi1;
 340   if( (double)B != a*d ) return TypeLong::LONG; // Overflow?
 341   jlong C = hi0*lo1;
 342   if( (double)C != b*c ) return TypeLong::LONG; // Overflow?
 343   jlong D = hi0*hi1;
 344   if( (double)D != b*d ) return TypeLong::LONG; // Overflow?
 345 
 346   if( A < B ) { lo0 = A; hi0 = B; } // Sort range endpoints
 347   else { lo0 = B; hi0 = A; }
 348   if( C < D ) {
 349     if( C < lo0 ) lo0 = C;
 350     if( D > hi0 ) hi0 = D;
 351   } else {
 352     if( D < lo0 ) lo0 = D;
 353     if( C > hi0 ) hi0 = C;
 354   }
 355   return TypeLong::make(lo0, hi0, MAX2(r0->_widen,r1->_widen));
 356 }
 357 
 358 //=============================================================================
 359 //------------------------------mul_ring---------------------------------------
 360 // Compute the product type of two double ranges into this node.
 361 const Type *MulFNode::mul_ring(const Type *t0, const Type *t1) const {
 362   if( t0 == Type::FLOAT || t1 == Type::FLOAT ) return Type::FLOAT;
 363   return TypeF::make( t0->getf() * t1->getf() );
 364 }
 365 
 366 //=============================================================================
 367 //------------------------------mul_ring---------------------------------------
 368 // Compute the product type of two double ranges into this node.
 369 const Type *MulDNode::mul_ring(const Type *t0, const Type *t1) const {
 370   if( t0 == Type::DOUBLE || t1 == Type::DOUBLE ) return Type::DOUBLE;
 371   // We must be multiplying 2 double constants.
 372   return TypeD::make( t0->getd() * t1->getd() );
 373 }
 374 
 375 //=============================================================================
 376 //------------------------------Value------------------------------------------
 377 const Type *MulHiLNode::Value( PhaseTransform *phase ) const {
 378   // Either input is TOP ==> the result is TOP
 379   const Type *t1 = phase->type( in(1) );
 380   const Type *t2 = phase->type( in(2) );
 381   if( t1 == Type::TOP ) return Type::TOP;
 382   if( t2 == Type::TOP ) return Type::TOP;
 383 
 384   // Either input is BOTTOM ==> the result is the local BOTTOM
 385   const Type *bot = bottom_type();
 386   if( (t1 == bot) || (t2 == bot) ||
 387       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 388     return bot;
 389 
 390   // It is not worth trying to constant fold this stuff!
 391   return TypeLong::LONG;
 392 }
 393 
 394 //=============================================================================
 395 //------------------------------mul_ring---------------------------------------
 396 // Supplied function returns the product of the inputs IN THE CURRENT RING.
 397 // For the logical operations the ring's MUL is really a logical AND function.
 398 // This also type-checks the inputs for sanity.  Guaranteed never to
 399 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 400 const Type *AndINode::mul_ring( const Type *t0, const Type *t1 ) const {
 401   const TypeInt *r0 = t0->is_int(); // Handy access
 402   const TypeInt *r1 = t1->is_int();
 403   int widen = MAX2(r0->_widen,r1->_widen);
 404 
 405   // If either input is a constant, might be able to trim cases
 406   if( !r0->is_con() && !r1->is_con() )
 407     return TypeInt::INT;        // No constants to be had
 408 
 409   // Both constants?  Return bits
 410   if( r0->is_con() && r1->is_con() )
 411     return TypeInt::make( r0->get_con() & r1->get_con() );
 412 
 413   if( r0->is_con() && r0->get_con() > 0 )
 414     return TypeInt::make(0, r0->get_con(), widen);
 415 
 416   if( r1->is_con() && r1->get_con() > 0 )
 417     return TypeInt::make(0, r1->get_con(), widen);
 418 
 419   if( r0 == TypeInt::BOOL || r1 == TypeInt::BOOL ) {
 420     return TypeInt::BOOL;
 421   }
 422 
 423   return TypeInt::INT;          // No constants to be had
 424 }
 425 
 426 //------------------------------Identity---------------------------------------
 427 // Masking off the high bits of an unsigned load is not required
 428 Node *AndINode::Identity( PhaseTransform *phase ) {
 429 
 430   // x & x => x
 431   if (phase->eqv(in(1), in(2))) return in(1);
 432 
 433   Node* in1 = in(1);
 434   uint op = in1->Opcode();
 435   const TypeInt* t2 = phase->type(in(2))->isa_int();
 436   if (t2 && t2->is_con()) {
 437     int con = t2->get_con();
 438     // Masking off high bits which are always zero is useless.
 439     const TypeInt* t1 = phase->type( in(1) )->isa_int();
 440     if (t1 != NULL && t1->_lo >= 0) {
 441       jint t1_support = right_n_bits(1 + log2_intptr(t1->_hi));
 442       if ((t1_support & con) == t1_support)
 443         return in1;
 444     }
 445     // Masking off the high bits of a unsigned-shift-right is not
 446     // needed either.
 447     if (op == Op_URShiftI) {
 448       const TypeInt* t12 = phase->type(in1->in(2))->isa_int();
 449       if (t12 && t12->is_con()) {  // Shift is by a constant
 450         int shift = t12->get_con();
 451         shift &= BitsPerJavaInteger - 1;  // semantics of Java shifts
 452         int mask = max_juint >> shift;
 453         if ((mask & con) == mask)  // If AND is useless, skip it
 454           return in1;
 455       }
 456     }
 457   }
 458   return MulNode::Identity(phase);
 459 }
 460 
 461 //------------------------------Ideal------------------------------------------
 462 Node *AndINode::Ideal(PhaseGVN *phase, bool can_reshape) {
 463   // Special case constant AND mask
 464   const TypeInt *t2 = phase->type( in(2) )->isa_int();
 465   if( !t2 || !t2->is_con() ) return MulNode::Ideal(phase, can_reshape);
 466   const int mask = t2->get_con();
 467   Node *load = in(1);
 468   uint lop = load->Opcode();
 469 
 470   // Masking bits off of a Character?  Hi bits are already zero.
 471   if( lop == Op_LoadUS &&
 472       (mask & 0xFFFF0000) )     // Can we make a smaller mask?
 473     return new (phase->C, 3) AndINode(load,phase->intcon(mask&0xFFFF));
 474 
 475   // Masking bits off of a Short?  Loading a Character does some masking
 476   if (lop == Op_LoadS && (mask & 0xFFFF0000) == 0 ) {
 477     Node *ldus = new (phase->C, 3) LoadUSNode(load->in(MemNode::Control),
 478                                               load->in(MemNode::Memory),
 479                                               load->in(MemNode::Address),
 480                                               load->adr_type());
 481     ldus = phase->transform(ldus);
 482     return new (phase->C, 3) AndINode(ldus, phase->intcon(mask & 0xFFFF));
 483   }
 484 
 485   // Masking sign bits off of a Byte?  Do an unsigned byte load plus
 486   // an and.
 487   if (lop == Op_LoadB && (mask & 0xFFFFFF00) == 0) {
 488     Node* ldub = new (phase->C, 3) LoadUBNode(load->in(MemNode::Control),
 489                                               load->in(MemNode::Memory),
 490                                               load->in(MemNode::Address),
 491                                               load->adr_type());
 492     ldub = phase->transform(ldub);
 493     return new (phase->C, 3) AndINode(ldub, phase->intcon(mask));
 494   }
 495 
 496   // Masking off sign bits?  Dont make them!
 497   if( lop == Op_RShiftI ) {
 498     const TypeInt *t12 = phase->type(load->in(2))->isa_int();
 499     if( t12 && t12->is_con() ) { // Shift is by a constant
 500       int shift = t12->get_con();
 501       shift &= BitsPerJavaInteger-1;  // semantics of Java shifts
 502       const int sign_bits_mask = ~right_n_bits(BitsPerJavaInteger - shift);
 503       // If the AND'ing of the 2 masks has no bits, then only original shifted
 504       // bits survive.  NO sign-extension bits survive the maskings.
 505       if( (sign_bits_mask & mask) == 0 ) {
 506         // Use zero-fill shift instead
 507         Node *zshift = phase->transform(new (phase->C, 3) URShiftINode(load->in(1),load->in(2)));
 508         return new (phase->C, 3) AndINode( zshift, in(2) );
 509       }
 510     }
 511   }
 512 
 513   // Check for 'negate/and-1', a pattern emitted when someone asks for
 514   // 'mod 2'.  Negate leaves the low order bit unchanged (think: complement
 515   // plus 1) and the mask is of the low order bit.  Skip the negate.
 516   if( lop == Op_SubI && mask == 1 && load->in(1) &&
 517       phase->type(load->in(1)) == TypeInt::ZERO )
 518     return new (phase->C, 3) AndINode( load->in(2), in(2) );
 519 
 520   return MulNode::Ideal(phase, can_reshape);
 521 }
 522 
 523 //=============================================================================
 524 //------------------------------mul_ring---------------------------------------
 525 // Supplied function returns the product of the inputs IN THE CURRENT RING.
 526 // For the logical operations the ring's MUL is really a logical AND function.
 527 // This also type-checks the inputs for sanity.  Guaranteed never to
 528 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 529 const Type *AndLNode::mul_ring( const Type *t0, const Type *t1 ) const {
 530   const TypeLong *r0 = t0->is_long(); // Handy access
 531   const TypeLong *r1 = t1->is_long();
 532   int widen = MAX2(r0->_widen,r1->_widen);
 533 
 534   // If either input is a constant, might be able to trim cases
 535   if( !r0->is_con() && !r1->is_con() )
 536     return TypeLong::LONG;      // No constants to be had
 537 
 538   // Both constants?  Return bits
 539   if( r0->is_con() && r1->is_con() )
 540     return TypeLong::make( r0->get_con() & r1->get_con() );
 541 
 542   if( r0->is_con() && r0->get_con() > 0 )
 543     return TypeLong::make(CONST64(0), r0->get_con(), widen);
 544 
 545   if( r1->is_con() && r1->get_con() > 0 )
 546     return TypeLong::make(CONST64(0), r1->get_con(), widen);
 547 
 548   return TypeLong::LONG;        // No constants to be had
 549 }
 550 
 551 //------------------------------Identity---------------------------------------
 552 // Masking off the high bits of an unsigned load is not required
 553 Node *AndLNode::Identity( PhaseTransform *phase ) {
 554 
 555   // x & x => x
 556   if (phase->eqv(in(1), in(2))) return in(1);
 557 
 558   Node *usr = in(1);
 559   const TypeLong *t2 = phase->type( in(2) )->isa_long();
 560   if( t2 && t2->is_con() ) {
 561     jlong con = t2->get_con();
 562     // Masking off high bits which are always zero is useless.
 563     const TypeLong* t1 = phase->type( in(1) )->isa_long();
 564     if (t1 != NULL && t1->_lo >= 0) {
 565       jlong t1_support = ((jlong)1 << (1 + log2_long(t1->_hi))) - 1;
 566       if ((t1_support & con) == t1_support)
 567         return usr;
 568     }
 569     uint lop = usr->Opcode();
 570     // Masking off the high bits of a unsigned-shift-right is not
 571     // needed either.
 572     if( lop == Op_URShiftL ) {
 573       const TypeInt *t12 = phase->type( usr->in(2) )->isa_int();
 574       if( t12 && t12->is_con() ) {  // Shift is by a constant
 575         int shift = t12->get_con();
 576         shift &= BitsPerJavaLong - 1;  // semantics of Java shifts
 577         jlong mask = max_julong >> shift;
 578         if( (mask&con) == mask )  // If AND is useless, skip it
 579           return usr;
 580       }
 581     }
 582   }
 583   return MulNode::Identity(phase);
 584 }
 585 
 586 //------------------------------Ideal------------------------------------------
 587 Node *AndLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 588   // Special case constant AND mask
 589   const TypeLong *t2 = phase->type( in(2) )->isa_long();
 590   if( !t2 || !t2->is_con() ) return MulNode::Ideal(phase, can_reshape);
 591   const jlong mask = t2->get_con();
 592 
 593   Node* in1 = in(1);
 594   uint op = in1->Opcode();
 595 
 596   // Masking sign bits off of an integer?  Do an unsigned integer to
 597   // long load.
 598   // NOTE: This check must be *before* we try to convert the AndLNode
 599   // to an AndINode and commute it with ConvI2LNode because
 600   // 0xFFFFFFFFL masks the whole integer and we get a sign extension,
 601   // which is wrong.
 602   if (op == Op_ConvI2L && in1->in(1)->Opcode() == Op_LoadI && mask == CONST64(0x00000000FFFFFFFF)) {
 603     Node* load = in1->in(1);
 604     return new (phase->C, 3) LoadUI2LNode(load->in(MemNode::Control),
 605                                           load->in(MemNode::Memory),
 606                                           load->in(MemNode::Address),
 607                                           load->adr_type());
 608   }
 609 
 610   // Are we masking a long that was converted from an int with a mask
 611   // that fits in 32-bits?  Commute them and use an AndINode.
 612   if (op == Op_ConvI2L && (mask & CONST64(0xFFFFFFFF00000000)) == 0) {
 613     // If we are doing an UI2L conversion (i.e. the mask is
 614     // 0x00000000FFFFFFFF) we cannot convert the AndL to an AndI
 615     // because the AndI would be optimized away later in Identity.
 616     if (mask != CONST64(0x00000000FFFFFFFF)) {
 617       Node* andi = new (phase->C, 3) AndINode(in1->in(1), phase->intcon(mask));
 618       andi = phase->transform(andi);
 619       return new (phase->C, 2) ConvI2LNode(andi);
 620     }
 621   }
 622 
 623   // Masking off sign bits?  Dont make them!
 624   if (op == Op_RShiftL) {
 625     const TypeInt* t12 = phase->type(in1->in(2))->isa_int();
 626     if( t12 && t12->is_con() ) { // Shift is by a constant
 627       int shift = t12->get_con();
 628       shift &= BitsPerJavaLong - 1;  // semantics of Java shifts
 629       const jlong sign_bits_mask = ~(((jlong)CONST64(1) << (jlong)(BitsPerJavaLong - shift)) -1);
 630       // If the AND'ing of the 2 masks has no bits, then only original shifted
 631       // bits survive.  NO sign-extension bits survive the maskings.
 632       if( (sign_bits_mask & mask) == 0 ) {
 633         // Use zero-fill shift instead
 634         Node *zshift = phase->transform(new (phase->C, 3) URShiftLNode(in1->in(1), in1->in(2)));
 635         return new (phase->C, 3) AndLNode(zshift, in(2));
 636       }
 637     }
 638   }
 639 
 640   return MulNode::Ideal(phase, can_reshape);
 641 }
 642 
 643 //=============================================================================
 644 //------------------------------Identity---------------------------------------
 645 Node *LShiftINode::Identity( PhaseTransform *phase ) {
 646   const TypeInt *ti = phase->type( in(2) )->isa_int();  // shift count is an int
 647   return ( ti && ti->is_con() && ( ti->get_con() & ( BitsPerInt - 1 ) ) == 0 ) ? in(1) : this;
 648 }
 649 
 650 //------------------------------Ideal------------------------------------------
 651 // If the right input is a constant, and the left input is an add of a
 652 // constant, flatten the tree: (X+con1)<<con0 ==> X<<con0 + con1<<con0
 653 Node *LShiftINode::Ideal(PhaseGVN *phase, bool can_reshape) {
 654   const Type *t  = phase->type( in(2) );
 655   if( t == Type::TOP ) return NULL;       // Right input is dead
 656   const TypeInt *t2 = t->isa_int();
 657   if( !t2 || !t2->is_con() ) return NULL; // Right input is a constant
 658   const int con = t2->get_con() & ( BitsPerInt - 1 );  // masked shift count
 659 
 660   if ( con == 0 )  return NULL; // let Identity() handle 0 shift count
 661 
 662   // Left input is an add of a constant?
 663   Node *add1 = in(1);
 664   int add1_op = add1->Opcode();
 665   if( add1_op == Op_AddI ) {    // Left input is an add?
 666     assert( add1 != add1->in(1), "dead loop in LShiftINode::Ideal" );
 667     const TypeInt *t12 = phase->type(add1->in(2))->isa_int();
 668     if( t12 && t12->is_con() ){ // Left input is an add of a con?
 669       // Transform is legal, but check for profit.  Avoid breaking 'i2s'
 670       // and 'i2b' patterns which typically fold into 'StoreC/StoreB'.
 671       if( con < 16 ) {
 672         // Compute X << con0
 673         Node *lsh = phase->transform( new (phase->C, 3) LShiftINode( add1->in(1), in(2) ) );
 674         // Compute X<<con0 + (con1<<con0)
 675         return new (phase->C, 3) AddINode( lsh, phase->intcon(t12->get_con() << con));
 676       }
 677     }
 678   }
 679 
 680   // Check for "(x>>c0)<<c0" which just masks off low bits
 681   if( (add1_op == Op_RShiftI || add1_op == Op_URShiftI ) &&
 682       add1->in(2) == in(2) )
 683     // Convert to "(x & -(1<<c0))"
 684     return new (phase->C, 3) AndINode(add1->in(1),phase->intcon( -(1<<con)));
 685 
 686   // Check for "((x>>c0) & Y)<<c0" which just masks off more low bits
 687   if( add1_op == Op_AndI ) {
 688     Node *add2 = add1->in(1);
 689     int add2_op = add2->Opcode();
 690     if( (add2_op == Op_RShiftI || add2_op == Op_URShiftI ) &&
 691         add2->in(2) == in(2) ) {
 692       // Convert to "(x & (Y<<c0))"
 693       Node *y_sh = phase->transform( new (phase->C, 3) LShiftINode( add1->in(2), in(2) ) );
 694       return new (phase->C, 3) AndINode( add2->in(1), y_sh );
 695     }
 696   }
 697 
 698   // Check for ((x & ((1<<(32-c0))-1)) << c0) which ANDs off high bits
 699   // before shifting them away.
 700   const jint bits_mask = right_n_bits(BitsPerJavaInteger-con);
 701   if( add1_op == Op_AndI &&
 702       phase->type(add1->in(2)) == TypeInt::make( bits_mask ) )
 703     return new (phase->C, 3) LShiftINode( add1->in(1), in(2) );
 704 
 705   return NULL;
 706 }
 707 
 708 //------------------------------Value------------------------------------------
 709 // A LShiftINode shifts its input2 left by input1 amount.
 710 const Type *LShiftINode::Value( PhaseTransform *phase ) const {
 711   const Type *t1 = phase->type( in(1) );
 712   const Type *t2 = phase->type( in(2) );
 713   // Either input is TOP ==> the result is TOP
 714   if( t1 == Type::TOP ) return Type::TOP;
 715   if( t2 == Type::TOP ) return Type::TOP;
 716 
 717   // Left input is ZERO ==> the result is ZERO.
 718   if( t1 == TypeInt::ZERO ) return TypeInt::ZERO;
 719   // Shift by zero does nothing
 720   if( t2 == TypeInt::ZERO ) return t1;
 721 
 722   // Either input is BOTTOM ==> the result is BOTTOM
 723   if( (t1 == TypeInt::INT) || (t2 == TypeInt::INT) ||
 724       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 725     return TypeInt::INT;
 726 
 727   const TypeInt *r1 = t1->is_int(); // Handy access
 728   const TypeInt *r2 = t2->is_int(); // Handy access
 729 
 730   if (!r2->is_con())
 731     return TypeInt::INT;
 732 
 733   uint shift = r2->get_con();
 734   shift &= BitsPerJavaInteger-1;  // semantics of Java shifts
 735   // Shift by a multiple of 32 does nothing:
 736   if (shift == 0)  return t1;
 737 
 738   // If the shift is a constant, shift the bounds of the type,
 739   // unless this could lead to an overflow.
 740   if (!r1->is_con()) {
 741     jint lo = r1->_lo, hi = r1->_hi;
 742     if (((lo << shift) >> shift) == lo &&
 743         ((hi << shift) >> shift) == hi) {
 744       // No overflow.  The range shifts up cleanly.
 745       return TypeInt::make((jint)lo << (jint)shift,
 746                            (jint)hi << (jint)shift,
 747                            MAX2(r1->_widen,r2->_widen));
 748     }
 749     return TypeInt::INT;
 750   }
 751 
 752   return TypeInt::make( (jint)r1->get_con() << (jint)shift );
 753 }
 754 
 755 //=============================================================================
 756 //------------------------------Identity---------------------------------------
 757 Node *LShiftLNode::Identity( PhaseTransform *phase ) {
 758   const TypeInt *ti = phase->type( in(2) )->isa_int(); // shift count is an int
 759   return ( ti && ti->is_con() && ( ti->get_con() & ( BitsPerLong - 1 ) ) == 0 ) ? in(1) : this;
 760 }
 761 
 762 //------------------------------Ideal------------------------------------------
 763 // If the right input is a constant, and the left input is an add of a
 764 // constant, flatten the tree: (X+con1)<<con0 ==> X<<con0 + con1<<con0
 765 Node *LShiftLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 766   const Type *t  = phase->type( in(2) );
 767   if( t == Type::TOP ) return NULL;       // Right input is dead
 768   const TypeInt *t2 = t->isa_int();
 769   if( !t2 || !t2->is_con() ) return NULL; // Right input is a constant
 770   const int con = t2->get_con() & ( BitsPerLong - 1 );  // masked shift count
 771 
 772   if ( con == 0 ) return NULL;  // let Identity() handle 0 shift count
 773 
 774   // Left input is an add of a constant?
 775   Node *add1 = in(1);
 776   int add1_op = add1->Opcode();
 777   if( add1_op == Op_AddL ) {    // Left input is an add?
 778     // Avoid dead data cycles from dead loops
 779     assert( add1 != add1->in(1), "dead loop in LShiftLNode::Ideal" );
 780     const TypeLong *t12 = phase->type(add1->in(2))->isa_long();
 781     if( t12 && t12->is_con() ){ // Left input is an add of a con?
 782       // Compute X << con0
 783       Node *lsh = phase->transform( new (phase->C, 3) LShiftLNode( add1->in(1), in(2) ) );
 784       // Compute X<<con0 + (con1<<con0)
 785       return new (phase->C, 3) AddLNode( lsh, phase->longcon(t12->get_con() << con));
 786     }
 787   }
 788 
 789   // Check for "(x>>c0)<<c0" which just masks off low bits
 790   if( (add1_op == Op_RShiftL || add1_op == Op_URShiftL ) &&
 791       add1->in(2) == in(2) )
 792     // Convert to "(x & -(1<<c0))"
 793     return new (phase->C, 3) AndLNode(add1->in(1),phase->longcon( -(CONST64(1)<<con)));
 794 
 795   // Check for "((x>>c0) & Y)<<c0" which just masks off more low bits
 796   if( add1_op == Op_AndL ) {
 797     Node *add2 = add1->in(1);
 798     int add2_op = add2->Opcode();
 799     if( (add2_op == Op_RShiftL || add2_op == Op_URShiftL ) &&
 800         add2->in(2) == in(2) ) {
 801       // Convert to "(x & (Y<<c0))"
 802       Node *y_sh = phase->transform( new (phase->C, 3) LShiftLNode( add1->in(2), in(2) ) );
 803       return new (phase->C, 3) AndLNode( add2->in(1), y_sh );
 804     }
 805   }
 806 
 807   // Check for ((x & ((CONST64(1)<<(64-c0))-1)) << c0) which ANDs off high bits
 808   // before shifting them away.
 809   const jlong bits_mask = ((jlong)CONST64(1) << (jlong)(BitsPerJavaLong - con)) - CONST64(1);
 810   if( add1_op == Op_AndL &&
 811       phase->type(add1->in(2)) == TypeLong::make( bits_mask ) )
 812     return new (phase->C, 3) LShiftLNode( add1->in(1), in(2) );
 813 
 814   return NULL;
 815 }
 816 
 817 //------------------------------Value------------------------------------------
 818 // A LShiftLNode shifts its input2 left by input1 amount.
 819 const Type *LShiftLNode::Value( PhaseTransform *phase ) const {
 820   const Type *t1 = phase->type( in(1) );
 821   const Type *t2 = phase->type( in(2) );
 822   // Either input is TOP ==> the result is TOP
 823   if( t1 == Type::TOP ) return Type::TOP;
 824   if( t2 == Type::TOP ) return Type::TOP;
 825 
 826   // Left input is ZERO ==> the result is ZERO.
 827   if( t1 == TypeLong::ZERO ) return TypeLong::ZERO;
 828   // Shift by zero does nothing
 829   if( t2 == TypeInt::ZERO ) return t1;
 830 
 831   // Either input is BOTTOM ==> the result is BOTTOM
 832   if( (t1 == TypeLong::LONG) || (t2 == TypeInt::INT) ||
 833       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 834     return TypeLong::LONG;
 835 
 836   const TypeLong *r1 = t1->is_long(); // Handy access
 837   const TypeInt  *r2 = t2->is_int();  // Handy access
 838 
 839   if (!r2->is_con())
 840     return TypeLong::LONG;
 841 
 842   uint shift = r2->get_con();
 843   shift &= BitsPerJavaLong - 1;  // semantics of Java shifts
 844   // Shift by a multiple of 64 does nothing:
 845   if (shift == 0)  return t1;
 846 
 847   // If the shift is a constant, shift the bounds of the type,
 848   // unless this could lead to an overflow.
 849   if (!r1->is_con()) {
 850     jlong lo = r1->_lo, hi = r1->_hi;
 851     if (((lo << shift) >> shift) == lo &&
 852         ((hi << shift) >> shift) == hi) {
 853       // No overflow.  The range shifts up cleanly.
 854       return TypeLong::make((jlong)lo << (jint)shift,
 855                             (jlong)hi << (jint)shift,
 856                             MAX2(r1->_widen,r2->_widen));
 857     }
 858     return TypeLong::LONG;
 859   }
 860 
 861   return TypeLong::make( (jlong)r1->get_con() << (jint)shift );
 862 }
 863 
 864 //=============================================================================
 865 //------------------------------Identity---------------------------------------
 866 Node *RShiftINode::Identity( PhaseTransform *phase ) {
 867   const TypeInt *t2 = phase->type(in(2))->isa_int();
 868   if( !t2 ) return this;
 869   if ( t2->is_con() && ( t2->get_con() & ( BitsPerInt - 1 ) ) == 0 )
 870     return in(1);
 871 
 872   // Check for useless sign-masking
 873   if( in(1)->Opcode() == Op_LShiftI &&
 874       in(1)->req() == 3 &&
 875       in(1)->in(2) == in(2) &&
 876       t2->is_con() ) {
 877     uint shift = t2->get_con();
 878     shift &= BitsPerJavaInteger-1; // semantics of Java shifts
 879     // Compute masks for which this shifting doesn't change
 880     int lo = (-1 << (BitsPerJavaInteger - shift-1)); // FFFF8000
 881     int hi = ~lo;               // 00007FFF
 882     const TypeInt *t11 = phase->type(in(1)->in(1))->isa_int();
 883     if( !t11 ) return this;
 884     // Does actual value fit inside of mask?
 885     if( lo <= t11->_lo && t11->_hi <= hi )
 886       return in(1)->in(1);      // Then shifting is a nop
 887   }
 888 
 889   return this;
 890 }
 891 
 892 //------------------------------Ideal------------------------------------------
 893 Node *RShiftINode::Ideal(PhaseGVN *phase, bool can_reshape) {
 894   // Inputs may be TOP if they are dead.
 895   const TypeInt *t1 = phase->type( in(1) )->isa_int();
 896   if( !t1 ) return NULL;        // Left input is an integer
 897   const TypeInt *t2 = phase->type( in(2) )->isa_int();
 898   if( !t2 || !t2->is_con() ) return NULL; // Right input is a constant
 899   const TypeInt *t3;  // type of in(1).in(2)
 900   int shift = t2->get_con();
 901   shift &= BitsPerJavaInteger-1;  // semantics of Java shifts
 902 
 903   if ( shift == 0 ) return NULL;  // let Identity() handle 0 shift count
 904 
 905   // Check for (x & 0xFF000000) >> 24, whose mask can be made smaller.
 906   // Such expressions arise normally from shift chains like (byte)(x >> 24).
 907   const Node *mask = in(1);
 908   if( mask->Opcode() == Op_AndI &&
 909       (t3 = phase->type(mask->in(2))->isa_int()) &&
 910       t3->is_con() ) {
 911     Node *x = mask->in(1);
 912     jint maskbits = t3->get_con();
 913     // Convert to "(x >> shift) & (mask >> shift)"
 914     Node *shr_nomask = phase->transform( new (phase->C, 3) RShiftINode(mask->in(1), in(2)) );
 915     return new (phase->C, 3) AndINode(shr_nomask, phase->intcon( maskbits >> shift));
 916   }
 917 
 918   // Check for "(short[i] <<16)>>16" which simply sign-extends
 919   const Node *shl = in(1);
 920   if( shl->Opcode() != Op_LShiftI ) return NULL;
 921 
 922   if( shift == 16 &&
 923       (t3 = phase->type(shl->in(2))->isa_int()) &&
 924       t3->is_con(16) ) {
 925     Node *ld = shl->in(1);
 926     if( ld->Opcode() == Op_LoadS ) {
 927       // Sign extension is just useless here.  Return a RShiftI of zero instead
 928       // returning 'ld' directly.  We cannot return an old Node directly as
 929       // that is the job of 'Identity' calls and Identity calls only work on
 930       // direct inputs ('ld' is an extra Node removed from 'this').  The
 931       // combined optimization requires Identity only return direct inputs.
 932       set_req(1, ld);
 933       set_req(2, phase->intcon(0));
 934       return this;
 935     }
 936     else if( ld->Opcode() == Op_LoadUS )
 937       // Replace zero-extension-load with sign-extension-load
 938       return new (phase->C, 3) LoadSNode( ld->in(MemNode::Control),
 939                                 ld->in(MemNode::Memory),
 940                                 ld->in(MemNode::Address),
 941                                 ld->adr_type());
 942   }
 943 
 944   // Check for "(byte[i] <<24)>>24" which simply sign-extends
 945   if( shift == 24 &&
 946       (t3 = phase->type(shl->in(2))->isa_int()) &&
 947       t3->is_con(24) ) {
 948     Node *ld = shl->in(1);
 949     if( ld->Opcode() == Op_LoadB ) {
 950       // Sign extension is just useless here
 951       set_req(1, ld);
 952       set_req(2, phase->intcon(0));
 953       return this;
 954     }
 955   }
 956 
 957   return NULL;
 958 }
 959 
 960 //------------------------------Value------------------------------------------
 961 // A RShiftINode shifts its input2 right by input1 amount.
 962 const Type *RShiftINode::Value( PhaseTransform *phase ) const {
 963   const Type *t1 = phase->type( in(1) );
 964   const Type *t2 = phase->type( in(2) );
 965   // Either input is TOP ==> the result is TOP
 966   if( t1 == Type::TOP ) return Type::TOP;
 967   if( t2 == Type::TOP ) return Type::TOP;
 968 
 969   // Left input is ZERO ==> the result is ZERO.
 970   if( t1 == TypeInt::ZERO ) return TypeInt::ZERO;
 971   // Shift by zero does nothing
 972   if( t2 == TypeInt::ZERO ) return t1;
 973 
 974   // Either input is BOTTOM ==> the result is BOTTOM
 975   if (t1 == Type::BOTTOM || t2 == Type::BOTTOM)
 976     return TypeInt::INT;
 977 
 978   if (t2 == TypeInt::INT)
 979     return TypeInt::INT;
 980 
 981   const TypeInt *r1 = t1->is_int(); // Handy access
 982   const TypeInt *r2 = t2->is_int(); // Handy access
 983 
 984   // If the shift is a constant, just shift the bounds of the type.
 985   // For example, if the shift is 31, we just propagate sign bits.
 986   if (r2->is_con()) {
 987     uint shift = r2->get_con();
 988     shift &= BitsPerJavaInteger-1;  // semantics of Java shifts
 989     // Shift by a multiple of 32 does nothing:
 990     if (shift == 0)  return t1;
 991     // Calculate reasonably aggressive bounds for the result.
 992     // This is necessary if we are to correctly type things
 993     // like (x<<24>>24) == ((byte)x).
 994     jint lo = (jint)r1->_lo >> (jint)shift;
 995     jint hi = (jint)r1->_hi >> (jint)shift;
 996     assert(lo <= hi, "must have valid bounds");
 997     const TypeInt* ti = TypeInt::make(lo, hi, MAX2(r1->_widen,r2->_widen));
 998 #ifdef ASSERT
 999     // Make sure we get the sign-capture idiom correct.
1000     if (shift == BitsPerJavaInteger-1) {
1001       if (r1->_lo >= 0) assert(ti == TypeInt::ZERO,    ">>31 of + is  0");
1002       if (r1->_hi <  0) assert(ti == TypeInt::MINUS_1, ">>31 of - is -1");
1003     }
1004 #endif
1005     return ti;
1006   }
1007 
1008   if( !r1->is_con() || !r2->is_con() )
1009     return TypeInt::INT;
1010 
1011   // Signed shift right
1012   return TypeInt::make( r1->get_con() >> (r2->get_con()&31) );
1013 }
1014 
1015 //=============================================================================
1016 //------------------------------Identity---------------------------------------
1017 Node *RShiftLNode::Identity( PhaseTransform *phase ) {
1018   const TypeInt *ti = phase->type( in(2) )->isa_int(); // shift count is an int
1019   return ( ti && ti->is_con() && ( ti->get_con() & ( BitsPerLong - 1 ) ) == 0 ) ? in(1) : this;
1020 }
1021 
1022 //------------------------------Value------------------------------------------
1023 // A RShiftLNode shifts its input2 right by input1 amount.
1024 const Type *RShiftLNode::Value( PhaseTransform *phase ) const {
1025   const Type *t1 = phase->type( in(1) );
1026   const Type *t2 = phase->type( in(2) );
1027   // Either input is TOP ==> the result is TOP
1028   if( t1 == Type::TOP ) return Type::TOP;
1029   if( t2 == Type::TOP ) return Type::TOP;
1030 
1031   // Left input is ZERO ==> the result is ZERO.
1032   if( t1 == TypeLong::ZERO ) return TypeLong::ZERO;
1033   // Shift by zero does nothing
1034   if( t2 == TypeInt::ZERO ) return t1;
1035 
1036   // Either input is BOTTOM ==> the result is BOTTOM
1037   if (t1 == Type::BOTTOM || t2 == Type::BOTTOM)
1038     return TypeLong::LONG;
1039 
1040   if (t2 == TypeInt::INT)
1041     return TypeLong::LONG;
1042 
1043   const TypeLong *r1 = t1->is_long(); // Handy access
1044   const TypeInt  *r2 = t2->is_int (); // Handy access
1045 
1046   // If the shift is a constant, just shift the bounds of the type.
1047   // For example, if the shift is 63, we just propagate sign bits.
1048   if (r2->is_con()) {
1049     uint shift = r2->get_con();
1050     shift &= (2*BitsPerJavaInteger)-1;  // semantics of Java shifts
1051     // Shift by a multiple of 64 does nothing:
1052     if (shift == 0)  return t1;
1053     // Calculate reasonably aggressive bounds for the result.
1054     // This is necessary if we are to correctly type things
1055     // like (x<<24>>24) == ((byte)x).
1056     jlong lo = (jlong)r1->_lo >> (jlong)shift;
1057     jlong hi = (jlong)r1->_hi >> (jlong)shift;
1058     assert(lo <= hi, "must have valid bounds");
1059     const TypeLong* tl = TypeLong::make(lo, hi, MAX2(r1->_widen,r2->_widen));
1060     #ifdef ASSERT
1061     // Make sure we get the sign-capture idiom correct.
1062     if (shift == (2*BitsPerJavaInteger)-1) {
1063       if (r1->_lo >= 0) assert(tl == TypeLong::ZERO,    ">>63 of + is 0");
1064       if (r1->_hi < 0)  assert(tl == TypeLong::MINUS_1, ">>63 of - is -1");
1065     }
1066     #endif
1067     return tl;
1068   }
1069 
1070   return TypeLong::LONG;                // Give up
1071 }
1072 
1073 //=============================================================================
1074 //------------------------------Identity---------------------------------------
1075 Node *URShiftINode::Identity( PhaseTransform *phase ) {
1076   const TypeInt *ti = phase->type( in(2) )->isa_int();
1077   if ( ti && ti->is_con() && ( ti->get_con() & ( BitsPerInt - 1 ) ) == 0 ) return in(1);
1078 
1079   // Check for "((x << LogBytesPerWord) + (wordSize-1)) >> LogBytesPerWord" which is just "x".
1080   // Happens during new-array length computation.
1081   // Safe if 'x' is in the range [0..(max_int>>LogBytesPerWord)]
1082   Node *add = in(1);
1083   if( add->Opcode() == Op_AddI ) {
1084     const TypeInt *t2  = phase->type(add->in(2))->isa_int();
1085     if( t2 && t2->is_con(wordSize - 1) &&
1086         add->in(1)->Opcode() == Op_LShiftI ) {
1087       // Check that shift_counts are LogBytesPerWord
1088       Node          *lshift_count   = add->in(1)->in(2);
1089       const TypeInt *t_lshift_count = phase->type(lshift_count)->isa_int();
1090       if( t_lshift_count && t_lshift_count->is_con(LogBytesPerWord) &&
1091           t_lshift_count == phase->type(in(2)) ) {
1092         Node          *x   = add->in(1)->in(1);
1093         const TypeInt *t_x = phase->type(x)->isa_int();
1094         if( t_x != NULL && 0 <= t_x->_lo && t_x->_hi <= (max_jint>>LogBytesPerWord) ) {
1095           return x;
1096         }
1097       }
1098     }
1099   }
1100 
1101   return (phase->type(in(2))->higher_equal(TypeInt::ZERO)) ? in(1) : this;
1102 }
1103 
1104 //------------------------------Ideal------------------------------------------
1105 Node *URShiftINode::Ideal(PhaseGVN *phase, bool can_reshape) {
1106   const TypeInt *t2 = phase->type( in(2) )->isa_int();
1107   if( !t2 || !t2->is_con() ) return NULL; // Right input is a constant
1108   const int con = t2->get_con() & 31; // Shift count is always masked
1109   if ( con == 0 ) return NULL;  // let Identity() handle a 0 shift count
1110   // We'll be wanting the right-shift amount as a mask of that many bits
1111   const int mask = right_n_bits(BitsPerJavaInteger - con);
1112 
1113   int in1_op = in(1)->Opcode();
1114 
1115   // Check for ((x>>>a)>>>b) and replace with (x>>>(a+b)) when a+b < 32
1116   if( in1_op == Op_URShiftI ) {
1117     const TypeInt *t12 = phase->type( in(1)->in(2) )->isa_int();
1118     if( t12 && t12->is_con() ) { // Right input is a constant
1119       assert( in(1) != in(1)->in(1), "dead loop in URShiftINode::Ideal" );
1120       const int con2 = t12->get_con() & 31; // Shift count is always masked
1121       const int con3 = con+con2;
1122       if( con3 < 32 )           // Only merge shifts if total is < 32
1123         return new (phase->C, 3) URShiftINode( in(1)->in(1), phase->intcon(con3) );
1124     }
1125   }
1126 
1127   // Check for ((x << z) + Y) >>> z.  Replace with x + con>>>z
1128   // The idiom for rounding to a power of 2 is "(Q+(2^z-1)) >>> z".
1129   // If Q is "X << z" the rounding is useless.  Look for patterns like
1130   // ((X<<Z) + Y) >>> Z  and replace with (X + Y>>>Z) & Z-mask.
1131   Node *add = in(1);
1132   if( in1_op == Op_AddI ) {
1133     Node *lshl = add->in(1);
1134     if( lshl->Opcode() == Op_LShiftI &&
1135         phase->type(lshl->in(2)) == t2 ) {
1136       Node *y_z = phase->transform( new (phase->C, 3) URShiftINode(add->in(2),in(2)) );
1137       Node *sum = phase->transform( new (phase->C, 3) AddINode( lshl->in(1), y_z ) );
1138       return new (phase->C, 3) AndINode( sum, phase->intcon(mask) );
1139     }
1140   }
1141 
1142   // Check for (x & mask) >>> z.  Replace with (x >>> z) & (mask >>> z)
1143   // This shortens the mask.  Also, if we are extracting a high byte and
1144   // storing it to a buffer, the mask will be removed completely.
1145   Node *andi = in(1);
1146   if( in1_op == Op_AndI ) {
1147     const TypeInt *t3 = phase->type( andi->in(2) )->isa_int();
1148     if( t3 && t3->is_con() ) { // Right input is a constant
1149       jint mask2 = t3->get_con();
1150       mask2 >>= con;  // *signed* shift downward (high-order zeroes do not help)
1151       Node *newshr = phase->transform( new (phase->C, 3) URShiftINode(andi->in(1), in(2)) );
1152       return new (phase->C, 3) AndINode(newshr, phase->intcon(mask2));
1153       // The negative values are easier to materialize than positive ones.
1154       // A typical case from address arithmetic is ((x & ~15) >> 4).
1155       // It's better to change that to ((x >> 4) & ~0) versus
1156       // ((x >> 4) & 0x0FFFFFFF).  The difference is greatest in LP64.
1157     }
1158   }
1159 
1160   // Check for "(X << z ) >>> z" which simply zero-extends
1161   Node *shl = in(1);
1162   if( in1_op == Op_LShiftI &&
1163       phase->type(shl->in(2)) == t2 )
1164     return new (phase->C, 3) AndINode( shl->in(1), phase->intcon(mask) );
1165 
1166   return NULL;
1167 }
1168 
1169 //------------------------------Value------------------------------------------
1170 // A URShiftINode shifts its input2 right by input1 amount.
1171 const Type *URShiftINode::Value( PhaseTransform *phase ) const {
1172   // (This is a near clone of RShiftINode::Value.)
1173   const Type *t1 = phase->type( in(1) );
1174   const Type *t2 = phase->type( in(2) );
1175   // Either input is TOP ==> the result is TOP
1176   if( t1 == Type::TOP ) return Type::TOP;
1177   if( t2 == Type::TOP ) return Type::TOP;
1178 
1179   // Left input is ZERO ==> the result is ZERO.
1180   if( t1 == TypeInt::ZERO ) return TypeInt::ZERO;
1181   // Shift by zero does nothing
1182   if( t2 == TypeInt::ZERO ) return t1;
1183 
1184   // Either input is BOTTOM ==> the result is BOTTOM
1185   if (t1 == Type::BOTTOM || t2 == Type::BOTTOM)
1186     return TypeInt::INT;
1187 
1188   if (t2 == TypeInt::INT)
1189     return TypeInt::INT;
1190 
1191   const TypeInt *r1 = t1->is_int();     // Handy access
1192   const TypeInt *r2 = t2->is_int();     // Handy access
1193 
1194   if (r2->is_con()) {
1195     uint shift = r2->get_con();
1196     shift &= BitsPerJavaInteger-1;  // semantics of Java shifts
1197     // Shift by a multiple of 32 does nothing:
1198     if (shift == 0)  return t1;
1199     // Calculate reasonably aggressive bounds for the result.
1200     jint lo = (juint)r1->_lo >> (juint)shift;
1201     jint hi = (juint)r1->_hi >> (juint)shift;
1202     if (r1->_hi >= 0 && r1->_lo < 0) {
1203       // If the type has both negative and positive values,
1204       // there are two separate sub-domains to worry about:
1205       // The positive half and the negative half.
1206       jint neg_lo = lo;
1207       jint neg_hi = (juint)-1 >> (juint)shift;
1208       jint pos_lo = (juint) 0 >> (juint)shift;
1209       jint pos_hi = hi;
1210       lo = MIN2(neg_lo, pos_lo);  // == 0
1211       hi = MAX2(neg_hi, pos_hi);  // == -1 >>> shift;
1212     }
1213     assert(lo <= hi, "must have valid bounds");
1214     const TypeInt* ti = TypeInt::make(lo, hi, MAX2(r1->_widen,r2->_widen));
1215     #ifdef ASSERT
1216     // Make sure we get the sign-capture idiom correct.
1217     if (shift == BitsPerJavaInteger-1) {
1218       if (r1->_lo >= 0) assert(ti == TypeInt::ZERO, ">>>31 of + is 0");
1219       if (r1->_hi < 0)  assert(ti == TypeInt::ONE,  ">>>31 of - is +1");
1220     }
1221     #endif
1222     return ti;
1223   }
1224 
1225   //
1226   // Do not support shifted oops in info for GC
1227   //
1228   // else if( t1->base() == Type::InstPtr ) {
1229   //
1230   //   const TypeInstPtr *o = t1->is_instptr();
1231   //   if( t1->singleton() )
1232   //     return TypeInt::make( ((uint32)o->const_oop() + o->_offset) >> shift );
1233   // }
1234   // else if( t1->base() == Type::KlassPtr ) {
1235   //   const TypeKlassPtr *o = t1->is_klassptr();
1236   //   if( t1->singleton() )
1237   //     return TypeInt::make( ((uint32)o->const_oop() + o->_offset) >> shift );
1238   // }
1239 
1240   return TypeInt::INT;
1241 }
1242 
1243 //=============================================================================
1244 //------------------------------Identity---------------------------------------
1245 Node *URShiftLNode::Identity( PhaseTransform *phase ) {
1246   const TypeInt *ti = phase->type( in(2) )->isa_int(); // shift count is an int
1247   return ( ti && ti->is_con() && ( ti->get_con() & ( BitsPerLong - 1 ) ) == 0 ) ? in(1) : this;
1248 }
1249 
1250 //------------------------------Ideal------------------------------------------
1251 Node *URShiftLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1252   const TypeInt *t2 = phase->type( in(2) )->isa_int();
1253   if( !t2 || !t2->is_con() ) return NULL; // Right input is a constant
1254   const int con = t2->get_con() & ( BitsPerLong - 1 ); // Shift count is always masked
1255   if ( con == 0 ) return NULL;  // let Identity() handle a 0 shift count
1256                               // note: mask computation below does not work for 0 shift count
1257   // We'll be wanting the right-shift amount as a mask of that many bits
1258   const jlong mask = (((jlong)CONST64(1) << (jlong)(BitsPerJavaLong - con)) -1);
1259 
1260   // Check for ((x << z) + Y) >>> z.  Replace with x + con>>>z
1261   // The idiom for rounding to a power of 2 is "(Q+(2^z-1)) >>> z".
1262   // If Q is "X << z" the rounding is useless.  Look for patterns like
1263   // ((X<<Z) + Y) >>> Z  and replace with (X + Y>>>Z) & Z-mask.
1264   Node *add = in(1);
1265   if( add->Opcode() == Op_AddL ) {
1266     Node *lshl = add->in(1);
1267     if( lshl->Opcode() == Op_LShiftL &&
1268         phase->type(lshl->in(2)) == t2 ) {
1269       Node *y_z = phase->transform( new (phase->C, 3) URShiftLNode(add->in(2),in(2)) );
1270       Node *sum = phase->transform( new (phase->C, 3) AddLNode( lshl->in(1), y_z ) );
1271       return new (phase->C, 3) AndLNode( sum, phase->longcon(mask) );
1272     }
1273   }
1274 
1275   // Check for (x & mask) >>> z.  Replace with (x >>> z) & (mask >>> z)
1276   // This shortens the mask.  Also, if we are extracting a high byte and
1277   // storing it to a buffer, the mask will be removed completely.
1278   Node *andi = in(1);
1279   if( andi->Opcode() == Op_AndL ) {
1280     const TypeLong *t3 = phase->type( andi->in(2) )->isa_long();
1281     if( t3 && t3->is_con() ) { // Right input is a constant
1282       jlong mask2 = t3->get_con();
1283       mask2 >>= con;  // *signed* shift downward (high-order zeroes do not help)
1284       Node *newshr = phase->transform( new (phase->C, 3) URShiftLNode(andi->in(1), in(2)) );
1285       return new (phase->C, 3) AndLNode(newshr, phase->longcon(mask2));
1286     }
1287   }
1288 
1289   // Check for "(X << z ) >>> z" which simply zero-extends
1290   Node *shl = in(1);
1291   if( shl->Opcode() == Op_LShiftL &&
1292       phase->type(shl->in(2)) == t2 )
1293     return new (phase->C, 3) AndLNode( shl->in(1), phase->longcon(mask) );
1294 
1295   return NULL;
1296 }
1297 
1298 //------------------------------Value------------------------------------------
1299 // A URShiftINode shifts its input2 right by input1 amount.
1300 const Type *URShiftLNode::Value( PhaseTransform *phase ) const {
1301   // (This is a near clone of RShiftLNode::Value.)
1302   const Type *t1 = phase->type( in(1) );
1303   const Type *t2 = phase->type( in(2) );
1304   // Either input is TOP ==> the result is TOP
1305   if( t1 == Type::TOP ) return Type::TOP;
1306   if( t2 == Type::TOP ) return Type::TOP;
1307 
1308   // Left input is ZERO ==> the result is ZERO.
1309   if( t1 == TypeLong::ZERO ) return TypeLong::ZERO;
1310   // Shift by zero does nothing
1311   if( t2 == TypeInt::ZERO ) return t1;
1312 
1313   // Either input is BOTTOM ==> the result is BOTTOM
1314   if (t1 == Type::BOTTOM || t2 == Type::BOTTOM)
1315     return TypeLong::LONG;
1316 
1317   if (t2 == TypeInt::INT)
1318     return TypeLong::LONG;
1319 
1320   const TypeLong *r1 = t1->is_long(); // Handy access
1321   const TypeInt  *r2 = t2->is_int (); // Handy access
1322 
1323   if (r2->is_con()) {
1324     uint shift = r2->get_con();
1325     shift &= BitsPerJavaLong - 1;  // semantics of Java shifts
1326     // Shift by a multiple of 64 does nothing:
1327     if (shift == 0)  return t1;
1328     // Calculate reasonably aggressive bounds for the result.
1329     jlong lo = (julong)r1->_lo >> (juint)shift;
1330     jlong hi = (julong)r1->_hi >> (juint)shift;
1331     if (r1->_hi >= 0 && r1->_lo < 0) {
1332       // If the type has both negative and positive values,
1333       // there are two separate sub-domains to worry about:
1334       // The positive half and the negative half.
1335       jlong neg_lo = lo;
1336       jlong neg_hi = (julong)-1 >> (juint)shift;
1337       jlong pos_lo = (julong) 0 >> (juint)shift;
1338       jlong pos_hi = hi;
1339       //lo = MIN2(neg_lo, pos_lo);  // == 0
1340       lo = neg_lo < pos_lo ? neg_lo : pos_lo;
1341       //hi = MAX2(neg_hi, pos_hi);  // == -1 >>> shift;
1342       hi = neg_hi > pos_hi ? neg_hi : pos_hi;
1343     }
1344     assert(lo <= hi, "must have valid bounds");
1345     const TypeLong* tl = TypeLong::make(lo, hi, MAX2(r1->_widen,r2->_widen));
1346     #ifdef ASSERT
1347     // Make sure we get the sign-capture idiom correct.
1348     if (shift == BitsPerJavaLong - 1) {
1349       if (r1->_lo >= 0) assert(tl == TypeLong::ZERO, ">>>63 of + is 0");
1350       if (r1->_hi < 0)  assert(tl == TypeLong::ONE,  ">>>63 of - is +1");
1351     }
1352     #endif
1353     return tl;
1354   }
1355 
1356   return TypeLong::LONG;                // Give up
1357 }