src/share/vm/opto/mulnode.cpp

Mon, 04 Jan 2010 18:38:08 +0100

author
twisti
date
Mon, 04 Jan 2010 18:38:08 +0100
changeset 1570
e66fd840cb6b
parent 1332
52898b0c43e9
child 1907
c18cbe5936b8
permissions
-rw-r--r--

6893081: method handle & invokedynamic code needs additional cleanup (post 6815692, 6858164)
Summary: During the work for 6829187 we have fixed a number of basic bugs which are logically grouped with 6815692 and 6858164 but which must be reviewed and pushed separately.
Reviewed-by: kvn, never

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

mercurial