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