1 /* 2 * Copyright (c) 1997, 2025, 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 "memory/allocation.inline.hpp" 26 #include "opto/addnode.hpp" 27 #include "opto/connode.hpp" 28 #include "opto/convertnode.hpp" 29 #include "opto/memnode.hpp" 30 #include "opto/mulnode.hpp" 31 #include "opto/phaseX.hpp" 32 #include "opto/subnode.hpp" 33 #include "utilities/powerOfTwo.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 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 Node* in1 = in(1); 62 Node* in2 = in(2); 63 Node* progress = nullptr; // Progress flag 64 65 // This code is used by And nodes too, but some conversions are 66 // only valid for the actual Mul nodes. 67 uint op = Opcode(); 68 bool real_mul = (op == Op_MulI) || (op == Op_MulL) || 69 (op == Op_MulF) || (op == Op_MulD) || 70 (op == Op_MulHF); 71 72 // Convert "(-a)*(-b)" into "a*b". 73 if (real_mul && in1->is_Sub() && in2->is_Sub()) { 74 if (phase->type(in1->in(1))->is_zero_type() && 75 phase->type(in2->in(1))->is_zero_type()) { 76 set_req_X(1, in1->in(2), phase); 77 set_req_X(2, in2->in(2), phase); 78 in1 = in(1); 79 in2 = in(2); 80 progress = this; 81 } 82 } 83 84 // convert "max(a,b) * min(a,b)" into "a*b". 85 if ((in(1)->Opcode() == max_opcode() && in(2)->Opcode() == min_opcode()) 86 || (in(1)->Opcode() == min_opcode() && in(2)->Opcode() == max_opcode())) { 87 Node *in11 = in(1)->in(1); 88 Node *in12 = in(1)->in(2); 89 90 Node *in21 = in(2)->in(1); 91 Node *in22 = in(2)->in(2); 92 93 if ((in11 == in21 && in12 == in22) || 94 (in11 == in22 && in12 == in21)) { 95 set_req_X(1, in11, phase); 96 set_req_X(2, in12, phase); 97 in1 = in(1); 98 in2 = in(2); 99 progress = this; 100 } 101 } 102 103 const Type* t1 = phase->type(in1); 104 const Type* t2 = phase->type(in2); 105 106 // We are OK if right is a constant, or right is a load and 107 // left is a non-constant. 108 if( !(t2->singleton() || 109 (in(2)->is_Load() && !(t1->singleton() || in(1)->is_Load())) ) ) { 110 if( t1->singleton() || // Left input is a constant? 111 // Otherwise, sort inputs (commutativity) to help value numbering. 112 (in(1)->_idx > in(2)->_idx) ) { 113 swap_edges(1, 2); 114 const Type *t = t1; 115 t1 = t2; 116 t2 = t; 117 progress = this; // Made progress 118 } 119 } 120 121 // If the right input is a constant, and the left input is a product of a 122 // constant, flatten the expression tree. 123 if( t2->singleton() && // Right input is a constant? 124 op != Op_MulF && // Float & double cannot reassociate 125 op != Op_MulD && 126 op != Op_MulHF) { 127 if( t2 == Type::TOP ) return nullptr; 128 Node *mul1 = in(1); 129 #ifdef ASSERT 130 // Check for dead loop 131 int op1 = mul1->Opcode(); 132 if ((mul1 == this) || (in(2) == this) || 133 ((op1 == mul_opcode() || op1 == add_opcode()) && 134 ((mul1->in(1) == this) || (mul1->in(2) == this) || 135 (mul1->in(1) == mul1) || (mul1->in(2) == mul1)))) { 136 assert(false, "dead loop in MulNode::Ideal"); 137 } 138 #endif 139 140 if( mul1->Opcode() == mul_opcode() ) { // Left input is a multiply? 141 // Mul of a constant? 142 const Type *t12 = phase->type( mul1->in(2) ); 143 if( t12->singleton() && t12 != Type::TOP) { // Left input is an add of a constant? 144 // Compute new constant; check for overflow 145 const Type *tcon01 = ((MulNode*)mul1)->mul_ring(t2,t12); 146 if( tcon01->singleton() ) { 147 // The Mul of the flattened expression 148 set_req_X(1, mul1->in(1), phase); 149 set_req_X(2, phase->makecon(tcon01), phase); 150 t2 = tcon01; 151 progress = this; // Made progress 152 } 153 } 154 } 155 // If the right input is a constant, and the left input is an add of a 156 // constant, flatten the tree: (X+con1)*con0 ==> X*con0 + con1*con0 157 const Node *add1 = in(1); 158 if( add1->Opcode() == add_opcode() ) { // Left input is an add? 159 // Add of a constant? 160 const Type *t12 = phase->type( add1->in(2) ); 161 if( t12->singleton() && t12 != Type::TOP ) { // Left input is an add of a constant? 162 assert( add1->in(1) != add1, "dead loop in MulNode::Ideal" ); 163 // Compute new constant; check for overflow 164 const Type *tcon01 = mul_ring(t2,t12); 165 if( tcon01->singleton() ) { 166 167 // Convert (X+con1)*con0 into X*con0 168 Node *mul = clone(); // mul = ()*con0 169 mul->set_req(1,add1->in(1)); // mul = X*con0 170 mul = phase->transform(mul); 171 172 Node *add2 = add1->clone(); 173 add2->set_req(1, mul); // X*con0 + con0*con1 174 add2->set_req(2, phase->makecon(tcon01) ); 175 progress = add2; 176 } 177 } 178 } // End of is left input an add 179 } // End of is right input a Mul 180 181 return progress; 182 } 183 184 //------------------------------Value----------------------------------------- 185 const Type* MulNode::Value(PhaseGVN* phase) const { 186 const Type *t1 = phase->type( in(1) ); 187 const Type *t2 = phase->type( in(2) ); 188 // Either input is TOP ==> the result is TOP 189 if( t1 == Type::TOP ) return Type::TOP; 190 if( t2 == Type::TOP ) return Type::TOP; 191 192 // Either input is ZERO ==> the result is ZERO. 193 // Not valid for floats or doubles since +0.0 * -0.0 --> +0.0 194 int op = Opcode(); 195 if( op == Op_MulI || op == Op_AndI || op == Op_MulL || op == Op_AndL ) { 196 const Type *zero = add_id(); // The multiplicative zero 197 if( t1->higher_equal( zero ) ) return zero; 198 if( t2->higher_equal( zero ) ) return zero; 199 } 200 201 // Either input is BOTTOM ==> the result is the local BOTTOM 202 if( t1 == Type::BOTTOM || t2 == Type::BOTTOM ) 203 return bottom_type(); 204 205 #if defined(IA32) 206 // Can't trust native compilers to properly fold strict double 207 // multiplication with round-to-zero on this platform. 208 if (op == Op_MulD) { 209 return TypeD::DOUBLE; 210 } 211 #endif 212 213 return mul_ring(t1,t2); // Local flavor of type multiplication 214 } 215 216 MulNode* MulNode::make(Node* in1, Node* in2, BasicType bt) { 217 switch (bt) { 218 case T_INT: 219 return new MulINode(in1, in2); 220 case T_LONG: 221 return new MulLNode(in1, in2); 222 default: 223 fatal("Not implemented for %s", type2name(bt)); 224 } 225 return nullptr; 226 } 227 228 MulNode* MulNode::make_and(Node* in1, Node* in2, BasicType bt) { 229 switch (bt) { 230 case T_INT: 231 return new AndINode(in1, in2); 232 case T_LONG: 233 return new AndLNode(in1, in2); 234 default: 235 fatal("Not implemented for %s", type2name(bt)); 236 } 237 return nullptr; 238 } 239 240 241 //============================================================================= 242 //------------------------------Ideal------------------------------------------ 243 // Check for power-of-2 multiply, then try the regular MulNode::Ideal 244 Node *MulINode::Ideal(PhaseGVN *phase, bool can_reshape) { 245 const jint con = in(2)->find_int_con(0); 246 if (con == 0) { 247 // If in(2) is not a constant, call Ideal() of the parent class to 248 // try to move constant to the right side. 249 return MulNode::Ideal(phase, can_reshape); 250 } 251 252 // Now we have a constant Node on the right and the constant in con. 253 if (con == 1) { 254 // By one is handled by Identity call 255 return nullptr; 256 } 257 258 // Check for negative constant; if so negate the final result 259 bool sign_flip = false; 260 261 unsigned int abs_con = uabs(con); 262 if (abs_con != (unsigned int)con) { 263 sign_flip = true; 264 } 265 266 // Get low bit; check for being the only bit 267 Node *res = nullptr; 268 unsigned int bit1 = submultiple_power_of_2(abs_con); 269 if (bit1 == abs_con) { // Found a power of 2? 270 res = new LShiftINode(in(1), phase->intcon(log2i_exact(bit1))); 271 } else { 272 // Check for constant with 2 bits set 273 unsigned int bit2 = abs_con - bit1; 274 bit2 = bit2 & (0 - bit2); // Extract 2nd bit 275 if (bit2 + bit1 == abs_con) { // Found all bits in con? 276 Node *n1 = phase->transform(new LShiftINode(in(1), phase->intcon(log2i_exact(bit1)))); 277 Node *n2 = phase->transform(new LShiftINode(in(1), phase->intcon(log2i_exact(bit2)))); 278 res = new AddINode(n2, n1); 279 } else if (is_power_of_2(abs_con + 1)) { 280 // Sleezy: power-of-2 - 1. Next time be generic. 281 unsigned int temp = abs_con + 1; 282 Node *n1 = phase->transform(new LShiftINode(in(1), phase->intcon(log2i_exact(temp)))); 283 res = new SubINode(n1, in(1)); 284 } else { 285 return MulNode::Ideal(phase, can_reshape); 286 } 287 } 288 289 if (sign_flip) { // Need to negate result? 290 res = phase->transform(res);// Transform, before making the zero con 291 res = new SubINode(phase->intcon(0),res); 292 } 293 294 return res; // Return final result 295 } 296 297 // This template class performs type multiplication for MulI/MulLNode. NativeType is either jint or jlong. 298 // In this class, the inputs of the MulNodes are named left and right with types [left_lo,left_hi] and [right_lo,right_hi]. 299 // 300 // In general, the multiplication of two x-bit values could produce a result that consumes up to 2x bits if there is 301 // enough space to hold them all. We can therefore distinguish the following two cases for the product: 302 // - no overflow (i.e. product fits into x bits) 303 // - overflow (i.e. product does not fit into x bits) 304 // 305 // When multiplying the two x-bit inputs 'left' and 'right' with their x-bit types [left_lo,left_hi] and [right_lo,right_hi] 306 // we need to find the minimum and maximum of all possible products to define a new type. To do that, we compute the 307 // cross product of [left_lo,left_hi] and [right_lo,right_hi] in 2x-bit space where no over- or underflow can happen. 308 // The cross product consists of the following four multiplications with 2x-bit results: 309 // (1) left_lo * right_lo 310 // (2) left_lo * right_hi 311 // (3) left_hi * right_lo 312 // (4) left_hi * right_hi 313 // 314 // Let's define the following two functions: 315 // - Lx(i): Returns the lower x bits of the 2x-bit number i. 316 // - Ux(i): Returns the upper x bits of the 2x-bit number i. 317 // 318 // Let's first assume all products are positive where only overflows are possible but no underflows. If there is no 319 // overflow for a product p, then the upper x bits of the 2x-bit result p are all zero: 320 // Ux(p) = 0 321 // Lx(p) = p 322 // 323 // If none of the multiplications (1)-(4) overflow, we can truncate the upper x bits and use the following result type 324 // with x bits: 325 // [result_lo,result_hi] = [MIN(Lx(1),Lx(2),Lx(3),Lx(4)),MAX(Lx(1),Lx(2),Lx(3),Lx(4))] 326 // 327 // If any of these multiplications overflows, we could pessimistically take the bottom type for the x bit result 328 // (i.e. all values in the x-bit space could be possible): 329 // [result_lo,result_hi] = [NativeType_min,NativeType_max] 330 // 331 // However, in case of any overflow, we can do better by analyzing the upper x bits of all multiplications (1)-(4) with 332 // 2x-bit results. The upper x bits tell us something about how many times a multiplication has overflown the lower 333 // x bits. If the upper x bits of (1)-(4) are all equal, then we know that all of these multiplications overflowed 334 // the lower x bits the same number of times: 335 // Ux((1)) = Ux((2)) = Ux((3)) = Ux((4)) 336 // 337 // If all upper x bits are equal, we can conclude: 338 // Lx(MIN((1),(2),(3),(4))) = MIN(Lx(1),Lx(2),Lx(3),Lx(4))) 339 // Lx(MAX((1),(2),(3),(4))) = MAX(Lx(1),Lx(2),Lx(3),Lx(4))) 340 // 341 // Therefore, we can use the same precise x-bit result type as for the no-overflow case: 342 // [result_lo,result_hi] = [(MIN(Lx(1),Lx(2),Lx(3),Lx(4))),MAX(Lx(1),Lx(2),Lx(3),Lx(4)))] 343 // 344 // 345 // Now let's assume that (1)-(4) are signed multiplications where over- and underflow could occur: 346 // Negative numbers are all sign extend with ones. Therefore, if a negative product does not underflow, then the 347 // upper x bits of the 2x-bit result are all set to ones which is minus one in two's complement. If there is an underflow, 348 // the upper x bits are decremented by the number of times an underflow occurred. The smallest possible negative product 349 // is NativeType_min*NativeType_max, where the upper x bits are set to NativeType_min / 2 (b11...0). It is therefore 350 // impossible to underflow the upper x bits. Thus, when having all ones (i.e. minus one) in the upper x bits, we know 351 // that there is no underflow. 352 // 353 // To be able to compare the number of over-/underflows of positive and negative products, respectively, we normalize 354 // the upper x bits of negative 2x-bit products by adding one. This way a product has no over- or underflow if the 355 // normalized upper x bits are zero. Now we can use the same improved type as for strictly positive products because we 356 // can compare the upper x bits in a unified way with N() being the normalization function: 357 // N(Ux((1))) = N(Ux((2))) = N(Ux((3)) = N(Ux((4))) 358 template<typename NativeType> 359 class IntegerTypeMultiplication { 360 361 NativeType _lo_left; 362 NativeType _lo_right; 363 NativeType _hi_left; 364 NativeType _hi_right; 365 short _widen_left; 366 short _widen_right; 367 368 static const Type* overflow_type(); 369 static NativeType multiply_high(NativeType x, NativeType y); 370 const Type* create_type(NativeType lo, NativeType hi) const; 371 372 static NativeType multiply_high_signed_overflow_value(NativeType x, NativeType y) { 373 return normalize_overflow_value(x, y, multiply_high(x, y)); 374 } 375 376 bool cross_product_not_same_overflow_value() const { 377 const NativeType lo_lo_high_product = multiply_high_signed_overflow_value(_lo_left, _lo_right); 378 const NativeType lo_hi_high_product = multiply_high_signed_overflow_value(_lo_left, _hi_right); 379 const NativeType hi_lo_high_product = multiply_high_signed_overflow_value(_hi_left, _lo_right); 380 const NativeType hi_hi_high_product = multiply_high_signed_overflow_value(_hi_left, _hi_right); 381 return lo_lo_high_product != lo_hi_high_product || 382 lo_hi_high_product != hi_lo_high_product || 383 hi_lo_high_product != hi_hi_high_product; 384 } 385 386 bool does_product_overflow(NativeType x, NativeType y) const { 387 return multiply_high_signed_overflow_value(x, y) != 0; 388 } 389 390 static NativeType normalize_overflow_value(const NativeType x, const NativeType y, NativeType result) { 391 return java_multiply(x, y) < 0 ? result + 1 : result; 392 } 393 394 public: 395 template<class IntegerType> 396 IntegerTypeMultiplication(const IntegerType* left, const IntegerType* right) 397 : _lo_left(left->_lo), _lo_right(right->_lo), 398 _hi_left(left->_hi), _hi_right(right->_hi), 399 _widen_left(left->_widen), _widen_right(right->_widen) {} 400 401 // Compute the product type by multiplying the two input type ranges. We take the minimum and maximum of all possible 402 // values (requires 4 multiplications of all possible combinations of the two range boundary values). If any of these 403 // multiplications overflows/underflows, we need to make sure that they all have the same number of overflows/underflows 404 // If that is not the case, we return the bottom type to cover all values due to the inconsistent overflows/underflows). 405 const Type* compute() const { 406 if (cross_product_not_same_overflow_value()) { 407 return overflow_type(); 408 } 409 410 NativeType lo_lo_product = java_multiply(_lo_left, _lo_right); 411 NativeType lo_hi_product = java_multiply(_lo_left, _hi_right); 412 NativeType hi_lo_product = java_multiply(_hi_left, _lo_right); 413 NativeType hi_hi_product = java_multiply(_hi_left, _hi_right); 414 const NativeType min = MIN4(lo_lo_product, lo_hi_product, hi_lo_product, hi_hi_product); 415 const NativeType max = MAX4(lo_lo_product, lo_hi_product, hi_lo_product, hi_hi_product); 416 return create_type(min, max); 417 } 418 419 bool does_overflow() const { 420 return does_product_overflow(_lo_left, _lo_right) || 421 does_product_overflow(_lo_left, _hi_right) || 422 does_product_overflow(_hi_left, _lo_right) || 423 does_product_overflow(_hi_left, _hi_right); 424 } 425 }; 426 427 template <> 428 const Type* IntegerTypeMultiplication<jint>::overflow_type() { 429 return TypeInt::INT; 430 } 431 432 template <> 433 jint IntegerTypeMultiplication<jint>::multiply_high(const jint x, const jint y) { 434 const jlong x_64 = x; 435 const jlong y_64 = y; 436 const jlong product = x_64 * y_64; 437 return (jint)((uint64_t)product >> 32u); 438 } 439 440 template <> 441 const Type* IntegerTypeMultiplication<jint>::create_type(jint lo, jint hi) const { 442 return TypeInt::make(lo, hi, MAX2(_widen_left, _widen_right)); 443 } 444 445 template <> 446 const Type* IntegerTypeMultiplication<jlong>::overflow_type() { 447 return TypeLong::LONG; 448 } 449 450 template <> 451 jlong IntegerTypeMultiplication<jlong>::multiply_high(const jlong x, const jlong y) { 452 return multiply_high_signed(x, y); 453 } 454 455 template <> 456 const Type* IntegerTypeMultiplication<jlong>::create_type(jlong lo, jlong hi) const { 457 return TypeLong::make(lo, hi, MAX2(_widen_left, _widen_right)); 458 } 459 460 // Compute the product type of two integer ranges into this node. 461 const Type* MulINode::mul_ring(const Type* type_left, const Type* type_right) const { 462 const IntegerTypeMultiplication<jint> integer_multiplication(type_left->is_int(), type_right->is_int()); 463 return integer_multiplication.compute(); 464 } 465 466 bool MulINode::does_overflow(const TypeInt* type_left, const TypeInt* type_right) { 467 const IntegerTypeMultiplication<jint> integer_multiplication(type_left, type_right); 468 return integer_multiplication.does_overflow(); 469 } 470 471 // Compute the product type of two long ranges into this node. 472 const Type* MulLNode::mul_ring(const Type* type_left, const Type* type_right) const { 473 const IntegerTypeMultiplication<jlong> integer_multiplication(type_left->is_long(), type_right->is_long()); 474 return integer_multiplication.compute(); 475 } 476 477 //============================================================================= 478 //------------------------------Ideal------------------------------------------ 479 // Check for power-of-2 multiply, then try the regular MulNode::Ideal 480 Node *MulLNode::Ideal(PhaseGVN *phase, bool can_reshape) { 481 const jlong con = in(2)->find_long_con(0); 482 if (con == 0) { 483 // If in(2) is not a constant, call Ideal() of the parent class to 484 // try to move constant to the right side. 485 return MulNode::Ideal(phase, can_reshape); 486 } 487 488 // Now we have a constant Node on the right and the constant in con. 489 if (con == 1) { 490 // By one is handled by Identity call 491 return nullptr; 492 } 493 494 // Check for negative constant; if so negate the final result 495 bool sign_flip = false; 496 julong abs_con = uabs(con); 497 if (abs_con != (julong)con) { 498 sign_flip = true; 499 } 500 501 // Get low bit; check for being the only bit 502 Node *res = nullptr; 503 julong bit1 = submultiple_power_of_2(abs_con); 504 if (bit1 == abs_con) { // Found a power of 2? 505 res = new LShiftLNode(in(1), phase->intcon(log2i_exact(bit1))); 506 } else { 507 508 // Check for constant with 2 bits set 509 julong bit2 = abs_con-bit1; 510 bit2 = bit2 & (0-bit2); // Extract 2nd bit 511 if (bit2 + bit1 == abs_con) { // Found all bits in con? 512 Node *n1 = phase->transform(new LShiftLNode(in(1), phase->intcon(log2i_exact(bit1)))); 513 Node *n2 = phase->transform(new LShiftLNode(in(1), phase->intcon(log2i_exact(bit2)))); 514 res = new AddLNode(n2, n1); 515 516 } else if (is_power_of_2(abs_con+1)) { 517 // Sleezy: power-of-2 -1. Next time be generic. 518 julong temp = abs_con + 1; 519 Node *n1 = phase->transform( new LShiftLNode(in(1), phase->intcon(log2i_exact(temp)))); 520 res = new SubLNode(n1, in(1)); 521 } else { 522 return MulNode::Ideal(phase, can_reshape); 523 } 524 } 525 526 if (sign_flip) { // Need to negate result? 527 res = phase->transform(res);// Transform, before making the zero con 528 res = new SubLNode(phase->longcon(0),res); 529 } 530 531 return res; // Return final result 532 } 533 534 //============================================================================= 535 //------------------------------mul_ring--------------------------------------- 536 // Compute the product type of two double ranges into this node. 537 const Type *MulFNode::mul_ring(const Type *t0, const Type *t1) const { 538 if( t0 == Type::FLOAT || t1 == Type::FLOAT ) return Type::FLOAT; 539 return TypeF::make( t0->getf() * t1->getf() ); 540 } 541 542 //------------------------------Ideal--------------------------------------- 543 // Check to see if we are multiplying by a constant 2 and convert to add, then try the regular MulNode::Ideal 544 Node* MulFNode::Ideal(PhaseGVN* phase, bool can_reshape) { 545 const TypeF *t2 = phase->type(in(2))->isa_float_constant(); 546 547 // x * 2 -> x + x 548 if (t2 != nullptr && t2->getf() == 2) { 549 Node* base = in(1); 550 return new AddFNode(base, base); 551 } 552 return MulNode::Ideal(phase, can_reshape); 553 } 554 555 //============================================================================= 556 //------------------------------Ideal------------------------------------------ 557 // Check to see if we are multiplying by a constant 2 and convert to add, then try the regular MulNode::Ideal 558 Node* MulHFNode::Ideal(PhaseGVN* phase, bool can_reshape) { 559 const TypeH* t2 = phase->type(in(2))->isa_half_float_constant(); 560 561 // x * 2 -> x + x 562 if (t2 != nullptr && t2->getf() == 2) { 563 Node* base = in(1); 564 return new AddHFNode(base, base); 565 } 566 return MulNode::Ideal(phase, can_reshape); 567 } 568 569 // Compute the product type of two half float ranges into this node. 570 const Type* MulHFNode::mul_ring(const Type* t0, const Type* t1) const { 571 if (t0 == Type::HALF_FLOAT || t1 == Type::HALF_FLOAT) { 572 return Type::HALF_FLOAT; 573 } 574 return TypeH::make(t0->getf() * t1->getf()); 575 } 576 577 //============================================================================= 578 //------------------------------mul_ring--------------------------------------- 579 // Compute the product type of two double ranges into this node. 580 const Type *MulDNode::mul_ring(const Type *t0, const Type *t1) const { 581 if( t0 == Type::DOUBLE || t1 == Type::DOUBLE ) return Type::DOUBLE; 582 // We must be multiplying 2 double constants. 583 return TypeD::make( t0->getd() * t1->getd() ); 584 } 585 586 //------------------------------Ideal--------------------------------------- 587 // Check to see if we are multiplying by a constant 2 and convert to add, then try the regular MulNode::Ideal 588 Node* MulDNode::Ideal(PhaseGVN* phase, bool can_reshape) { 589 const TypeD *t2 = phase->type(in(2))->isa_double_constant(); 590 591 // x * 2 -> x + x 592 if (t2 != nullptr && t2->getd() == 2) { 593 Node* base = in(1); 594 return new AddDNode(base, base); 595 } 596 597 return MulNode::Ideal(phase, can_reshape); 598 } 599 600 //============================================================================= 601 //------------------------------Value------------------------------------------ 602 const Type* MulHiLNode::Value(PhaseGVN* phase) const { 603 const Type *t1 = phase->type( in(1) ); 604 const Type *t2 = phase->type( in(2) ); 605 const Type *bot = bottom_type(); 606 return MulHiValue(t1, t2, bot); 607 } 608 609 const Type* UMulHiLNode::Value(PhaseGVN* phase) const { 610 const Type *t1 = phase->type( in(1) ); 611 const Type *t2 = phase->type( in(2) ); 612 const Type *bot = bottom_type(); 613 return MulHiValue(t1, t2, bot); 614 } 615 616 // A common routine used by UMulHiLNode and MulHiLNode 617 const Type* MulHiValue(const Type *t1, const Type *t2, const Type *bot) { 618 // Either input is TOP ==> the result is TOP 619 if( t1 == Type::TOP ) return Type::TOP; 620 if( t2 == Type::TOP ) return Type::TOP; 621 622 // Either input is BOTTOM ==> the result is the local BOTTOM 623 if( (t1 == bot) || (t2 == bot) || 624 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) 625 return bot; 626 627 // It is not worth trying to constant fold this stuff! 628 return TypeLong::LONG; 629 } 630 631 template<typename IntegerType> 632 static const IntegerType* and_value(const IntegerType* r0, const IntegerType* r1) { 633 typedef typename IntegerType::NativeType NativeType; 634 static_assert(std::is_signed<NativeType>::value, "Native type of IntegerType must be signed!"); 635 636 int widen = MAX2(r0->_widen, r1->_widen); 637 638 // If both types are constants, we can calculate a constant result. 639 if (r0->is_con() && r1->is_con()) { 640 return IntegerType::make(r0->get_con() & r1->get_con()); 641 } 642 643 // If both ranges are positive, the result will range from 0 up to the hi value of the smaller range. The minimum 644 // of the two constrains the upper bound because any higher value in the other range will see all zeroes, so it will be masked out. 645 if (r0->_lo >= 0 && r1->_lo >= 0) { 646 return IntegerType::make(0, MIN2(r0->_hi, r1->_hi), widen); 647 } 648 649 // If only one range is positive, the result will range from 0 up to that range's maximum value. 650 // For the operation 'x & C' where C is a positive constant, the result will be in the range [0..C]. With that observation, 651 // we can say that for any integer c such that 0 <= c <= C will also be in the range [0..C]. Therefore, 'x & [c..C]' 652 // where c >= 0 will be in the range [0..C]. 653 if (r0->_lo >= 0) { 654 return IntegerType::make(0, r0->_hi, widen); 655 } 656 657 if (r1->_lo >= 0) { 658 return IntegerType::make(0, r1->_hi, widen); 659 } 660 661 // At this point, all positive ranges will have already been handled, so the only remaining cases will be negative ranges 662 // and constants. 663 664 assert(r0->_lo < 0 && r1->_lo < 0, "positive ranges should already be handled!"); 665 666 // As two's complement means that both numbers will start with leading 1s, the lower bound of both ranges will contain 667 // the common leading 1s of both minimum values. In order to count them with count_leading_zeros, the bits are inverted. 668 NativeType sel_val = ~MIN2(r0->_lo, r1->_lo); 669 670 NativeType min; 671 if (sel_val == 0) { 672 // Since count_leading_zeros is undefined at 0, we short-circuit the condition where both ranges have a minimum of -1. 673 min = -1; 674 } else { 675 // To get the number of bits to shift, we count the leading 0-bits and then subtract one, as the sign bit is already set. 676 int shift_bits = count_leading_zeros(sel_val) - 1; 677 min = std::numeric_limits<NativeType>::min() >> shift_bits; 678 } 679 680 NativeType max; 681 if (r0->_hi < 0 && r1->_hi < 0) { 682 // If both ranges are negative, then the same optimization as both positive ranges will apply, and the smaller hi 683 // value will mask off any bits set by higher values. 684 max = MIN2(r0->_hi, r1->_hi); 685 } else { 686 // In the case of ranges that cross zero, negative values can cause the higher order bits to be set, so the maximum 687 // positive value can be as high as the larger hi value. 688 max = MAX2(r0->_hi, r1->_hi); 689 } 690 691 return IntegerType::make(min, max, widen); 692 } 693 694 //============================================================================= 695 //------------------------------mul_ring--------------------------------------- 696 // Supplied function returns the product of the inputs IN THE CURRENT RING. 697 // For the logical operations the ring's MUL is really a logical AND function. 698 // This also type-checks the inputs for sanity. Guaranteed never to 699 // be passed a TOP or BOTTOM type, these are filtered out by pre-check. 700 const Type *AndINode::mul_ring( const Type *t0, const Type *t1 ) const { 701 const TypeInt* r0 = t0->is_int(); 702 const TypeInt* r1 = t1->is_int(); 703 704 return and_value<TypeInt>(r0, r1); 705 } 706 707 static bool AndIL_is_zero_element_under_mask(const PhaseGVN* phase, const Node* expr, const Node* mask, BasicType bt); 708 709 const Type* AndINode::Value(PhaseGVN* phase) const { 710 if (AndIL_is_zero_element_under_mask(phase, in(1), in(2), T_INT) || 711 AndIL_is_zero_element_under_mask(phase, in(2), in(1), T_INT)) { 712 return TypeInt::ZERO; 713 } 714 715 return MulNode::Value(phase); 716 } 717 718 //------------------------------Identity--------------------------------------- 719 // Masking off the high bits of an unsigned load is not required 720 Node* AndINode::Identity(PhaseGVN* phase) { 721 722 // x & x => x 723 if (in(1) == in(2)) { 724 return in(1); 725 } 726 727 Node* in1 = in(1); 728 uint op = in1->Opcode(); 729 const TypeInt* t2 = phase->type(in(2))->isa_int(); 730 if (t2 && t2->is_con()) { 731 int con = t2->get_con(); 732 // Masking off high bits which are always zero is useless. 733 const TypeInt* t1 = phase->type(in(1))->isa_int(); 734 if (t1 != nullptr && t1->_lo >= 0) { 735 jint t1_support = right_n_bits(1 + log2i_graceful(t1->_hi)); 736 if ((t1_support & con) == t1_support) 737 return in1; 738 } 739 // Masking off the high bits of a unsigned-shift-right is not 740 // needed either. 741 if (op == Op_URShiftI) { 742 const TypeInt* t12 = phase->type(in1->in(2))->isa_int(); 743 if (t12 && t12->is_con()) { // Shift is by a constant 744 int shift = t12->get_con(); 745 shift &= BitsPerJavaInteger - 1; // semantics of Java shifts 746 int mask = max_juint >> shift; 747 if ((mask & con) == mask) // If AND is useless, skip it 748 return in1; 749 } 750 } 751 } 752 return MulNode::Identity(phase); 753 } 754 755 //------------------------------Ideal------------------------------------------ 756 Node *AndINode::Ideal(PhaseGVN *phase, bool can_reshape) { 757 // Simplify (v1 + v2) & mask to v1 & mask or v2 & mask when possible. 758 Node* progress = AndIL_sum_and_mask(phase, T_INT); 759 if (progress != nullptr) { 760 return progress; 761 } 762 763 // Convert "(~a) & (~b)" into "~(a | b)" 764 if (AddNode::is_not(phase, in(1), T_INT) && AddNode::is_not(phase, in(2), T_INT)) { 765 Node* or_a_b = new OrINode(in(1)->in(1), in(2)->in(1)); 766 Node* tn = phase->transform(or_a_b); 767 return AddNode::make_not(phase, tn, T_INT); 768 } 769 770 // Special case constant AND mask 771 const TypeInt *t2 = phase->type( in(2) )->isa_int(); 772 if( !t2 || !t2->is_con() ) return MulNode::Ideal(phase, can_reshape); 773 const int mask = t2->get_con(); 774 Node *load = in(1); 775 uint lop = load->Opcode(); 776 777 // Masking bits off of a Character? Hi bits are already zero. 778 if( lop == Op_LoadUS && 779 (mask & 0xFFFF0000) ) // Can we make a smaller mask? 780 return new AndINode(load,phase->intcon(mask&0xFFFF)); 781 782 // Masking bits off of a Short? Loading a Character does some masking 783 if (can_reshape && 784 load->outcnt() == 1 && load->unique_out() == this) { 785 if (lop == Op_LoadS && (mask & 0xFFFF0000) == 0 ) { 786 Node* ldus = load->as_Load()->convert_to_unsigned_load(*phase); 787 ldus = phase->transform(ldus); 788 return new AndINode(ldus, phase->intcon(mask & 0xFFFF)); 789 } 790 791 // Masking sign bits off of a Byte? Do an unsigned byte load plus 792 // an and. 793 if (lop == Op_LoadB && (mask & 0xFFFFFF00) == 0) { 794 Node* ldub = load->as_Load()->convert_to_unsigned_load(*phase); 795 ldub = phase->transform(ldub); 796 return new AndINode(ldub, phase->intcon(mask)); 797 } 798 } 799 800 // Masking off sign bits? Dont make them! 801 if( lop == Op_RShiftI ) { 802 const TypeInt *t12 = phase->type(load->in(2))->isa_int(); 803 if( t12 && t12->is_con() ) { // Shift is by a constant 804 int shift = t12->get_con(); 805 shift &= BitsPerJavaInteger-1; // semantics of Java shifts 806 const int sign_bits_mask = ~right_n_bits(BitsPerJavaInteger - shift); 807 // If the AND'ing of the 2 masks has no bits, then only original shifted 808 // bits survive. NO sign-extension bits survive the maskings. 809 if( (sign_bits_mask & mask) == 0 ) { 810 // Use zero-fill shift instead 811 Node *zshift = phase->transform(new URShiftINode(load->in(1),load->in(2))); 812 return new AndINode( zshift, in(2) ); 813 } 814 } 815 } 816 817 // Check for 'negate/and-1', a pattern emitted when someone asks for 818 // 'mod 2'. Negate leaves the low order bit unchanged (think: complement 819 // plus 1) and the mask is of the low order bit. Skip the negate. 820 if( lop == Op_SubI && mask == 1 && load->in(1) && 821 phase->type(load->in(1)) == TypeInt::ZERO ) 822 return new AndINode( load->in(2), in(2) ); 823 824 return MulNode::Ideal(phase, can_reshape); 825 } 826 827 //============================================================================= 828 //------------------------------mul_ring--------------------------------------- 829 // Supplied function returns the product of the inputs IN THE CURRENT RING. 830 // For the logical operations the ring's MUL is really a logical AND function. 831 // This also type-checks the inputs for sanity. Guaranteed never to 832 // be passed a TOP or BOTTOM type, these are filtered out by pre-check. 833 const Type *AndLNode::mul_ring( const Type *t0, const Type *t1 ) const { 834 const TypeLong* r0 = t0->is_long(); 835 const TypeLong* r1 = t1->is_long(); 836 837 return and_value<TypeLong>(r0, r1); 838 } 839 840 const Type* AndLNode::Value(PhaseGVN* phase) const { 841 if (AndIL_is_zero_element_under_mask(phase, in(1), in(2), T_LONG) || 842 AndIL_is_zero_element_under_mask(phase, in(2), in(1), T_LONG)) { 843 return TypeLong::ZERO; 844 } 845 846 return MulNode::Value(phase); 847 } 848 849 //------------------------------Identity--------------------------------------- 850 // Masking off the high bits of an unsigned load is not required 851 Node* AndLNode::Identity(PhaseGVN* phase) { 852 853 // x & x => x 854 if (in(1) == in(2)) { 855 return in(1); 856 } 857 858 Node *usr = in(1); 859 const TypeLong *t2 = phase->type( in(2) )->isa_long(); 860 if( t2 && t2->is_con() ) { 861 jlong con = t2->get_con(); 862 // Masking off high bits which are always zero is useless. 863 const TypeLong* t1 = phase->type( in(1) )->isa_long(); 864 if (t1 != nullptr && t1->_lo >= 0) { 865 int bit_count = log2i_graceful(t1->_hi) + 1; 866 jlong t1_support = jlong(max_julong >> (BitsPerJavaLong - bit_count)); 867 if ((t1_support & con) == t1_support) 868 return usr; 869 } 870 uint lop = usr->Opcode(); 871 // Masking off the high bits of a unsigned-shift-right is not 872 // needed either. 873 if( lop == Op_URShiftL ) { 874 const TypeInt *t12 = phase->type( usr->in(2) )->isa_int(); 875 if( t12 && t12->is_con() ) { // Shift is by a constant 876 int shift = t12->get_con(); 877 shift &= BitsPerJavaLong - 1; // semantics of Java shifts 878 jlong mask = max_julong >> shift; 879 if( (mask&con) == mask ) // If AND is useless, skip it 880 return usr; 881 } 882 } 883 } 884 return MulNode::Identity(phase); 885 } 886 887 //------------------------------Ideal------------------------------------------ 888 Node *AndLNode::Ideal(PhaseGVN *phase, bool can_reshape) { 889 // Simplify (v1 + v2) & mask to v1 & mask or v2 & mask when possible. 890 Node* progress = AndIL_sum_and_mask(phase, T_LONG); 891 if (progress != nullptr) { 892 return progress; 893 } 894 895 // Convert "(~a) & (~b)" into "~(a | b)" 896 if (AddNode::is_not(phase, in(1), T_LONG) && AddNode::is_not(phase, in(2), T_LONG)) { 897 Node* or_a_b = new OrLNode(in(1)->in(1), in(2)->in(1)); 898 Node* tn = phase->transform(or_a_b); 899 return AddNode::make_not(phase, tn, T_LONG); 900 } 901 902 // Special case constant AND mask 903 const TypeLong *t2 = phase->type( in(2) )->isa_long(); 904 if( !t2 || !t2->is_con() ) return MulNode::Ideal(phase, can_reshape); 905 const jlong mask = t2->get_con(); 906 907 Node* in1 = in(1); 908 int op = in1->Opcode(); 909 910 // Are we masking a long that was converted from an int with a mask 911 // that fits in 32-bits? Commute them and use an AndINode. Don't 912 // convert masks which would cause a sign extension of the integer 913 // value. This check includes UI2L masks (0x00000000FFFFFFFF) which 914 // would be optimized away later in Identity. 915 if (op == Op_ConvI2L && (mask & UCONST64(0xFFFFFFFF80000000)) == 0) { 916 Node* andi = new AndINode(in1->in(1), phase->intcon(mask)); 917 andi = phase->transform(andi); 918 return new ConvI2LNode(andi); 919 } 920 921 // Masking off sign bits? Dont make them! 922 if (op == Op_RShiftL) { 923 const TypeInt* t12 = phase->type(in1->in(2))->isa_int(); 924 if( t12 && t12->is_con() ) { // Shift is by a constant 925 int shift = t12->get_con(); 926 shift &= BitsPerJavaLong - 1; // semantics of Java shifts 927 const julong sign_bits_mask = ~(((julong)CONST64(1) << (julong)(BitsPerJavaLong - shift)) -1); 928 // If the AND'ing of the 2 masks has no bits, then only original shifted 929 // bits survive. NO sign-extension bits survive the maskings. 930 if( (sign_bits_mask & mask) == 0 ) { 931 // Use zero-fill shift instead 932 Node *zshift = phase->transform(new URShiftLNode(in1->in(1), in1->in(2))); 933 return new AndLNode(zshift, in(2)); 934 } 935 } 936 } 937 938 return MulNode::Ideal(phase, can_reshape); 939 } 940 941 LShiftNode* LShiftNode::make(Node* in1, Node* in2, BasicType bt) { 942 switch (bt) { 943 case T_INT: 944 return new LShiftINode(in1, in2); 945 case T_LONG: 946 return new LShiftLNode(in1, in2); 947 default: 948 fatal("Not implemented for %s", type2name(bt)); 949 } 950 return nullptr; 951 } 952 953 // Returns whether the shift amount is constant. If so, sets count. 954 static bool const_shift_count(PhaseGVN* phase, const Node* shift_node, int* count) { 955 const TypeInt* tcount = phase->type(shift_node->in(2))->isa_int(); 956 if (tcount != nullptr && tcount->is_con()) { 957 *count = tcount->get_con(); 958 return true; 959 } 960 return false; 961 } 962 963 // Returns whether the shift amount is constant. If so, sets real_shift and masked_shift. 964 static bool mask_shift_amount(PhaseGVN* phase, const Node* shift_node, uint nBits, int& real_shift, int& masked_shift) { 965 if (const_shift_count(phase, shift_node, &real_shift)) { 966 masked_shift = real_shift & (nBits - 1); 967 return true; 968 } 969 return false; 970 } 971 972 // Convenience for when we don't care about the real amount 973 static bool mask_shift_amount(PhaseGVN* phase, const Node* shift_node, uint nBits, int& masked_shift) { 974 int real_shift; 975 return mask_shift_amount(phase, shift_node, nBits, real_shift, masked_shift); 976 } 977 978 // Use this in ::Ideal only with shiftNode == this! 979 // Returns the masked shift amount if constant or 0 if not constant. 980 static int mask_and_replace_shift_amount(PhaseGVN* phase, Node* shift_node, uint nBits) { 981 int real_shift; 982 int masked_shift; 983 if (mask_shift_amount(phase, shift_node, nBits, real_shift, masked_shift)) { 984 if (masked_shift == 0) { 985 // Let Identity() handle 0 shift count. 986 return 0; 987 } 988 989 if (real_shift != masked_shift) { 990 PhaseIterGVN* igvn = phase->is_IterGVN(); 991 if (igvn != nullptr) { 992 igvn->_worklist.push(shift_node); 993 } 994 shift_node->set_req(2, phase->intcon(masked_shift)); // Replace shift count with masked value. 995 } 996 return masked_shift; 997 } 998 // Not a shift by a constant. 999 return 0; 1000 } 1001 1002 // Called with 1003 // outer_shift = (_ << rhs_outer) 1004 // We are looking for the pattern: 1005 // outer_shift = ((X << rhs_inner) << rhs_outer) 1006 // where rhs_outer and rhs_inner are constant 1007 // we denote inner_shift the nested expression (X << rhs_inner) 1008 // con_inner = rhs_inner % nbits and con_outer = rhs_outer % nbits 1009 // where nbits is the number of bits of the shifts 1010 // 1011 // There are 2 cases: 1012 // if con_outer + con_inner >= nbits => 0 1013 // if con_outer + con_inner < nbits => X << (con_outer + con_inner) 1014 static Node* collapse_nested_shift_left(PhaseGVN* phase, const Node* outer_shift, int con_outer, BasicType bt) { 1015 assert(bt == T_LONG || bt == T_INT, "Unexpected type"); 1016 const Node* inner_shift = outer_shift->in(1); 1017 if (inner_shift->Opcode() != Op_LShift(bt)) { 1018 return nullptr; 1019 } 1020 1021 int nbits = static_cast<int>(bits_per_java_integer(bt)); 1022 int con_inner; 1023 if (!mask_shift_amount(phase, inner_shift, nbits, con_inner)) { 1024 return nullptr; 1025 } 1026 1027 if (con_inner == 0) { 1028 // We let the Identity() of the inner shift do its job. 1029 return nullptr; 1030 } 1031 1032 if (con_outer + con_inner >= nbits) { 1033 // While it might be tempting to use 1034 // phase->zerocon(bt); 1035 // it would be incorrect: zerocon caches nodes, while Ideal is only allowed 1036 // to return a new node, this or nullptr, but not an old (cached) node. 1037 return ConNode::make(TypeInteger::zero(bt)); 1038 } 1039 1040 // con0 + con1 < nbits ==> actual shift happens now 1041 Node* con0_plus_con1 = phase->intcon(con_outer + con_inner); 1042 return LShiftNode::make(inner_shift->in(1), con0_plus_con1, bt); 1043 } 1044 1045 //------------------------------Identity--------------------------------------- 1046 Node* LShiftINode::Identity(PhaseGVN* phase) { 1047 int count = 0; 1048 if (const_shift_count(phase, this, &count) && (count & (BitsPerJavaInteger - 1)) == 0) { 1049 // Shift by a multiple of 32 does nothing 1050 return in(1); 1051 } 1052 return this; 1053 } 1054 1055 //------------------------------Ideal------------------------------------------ 1056 // If the right input is a constant, and the left input is an add of a 1057 // constant, flatten the tree: (X+con1)<<con0 ==> X<<con0 + con1<<con0 1058 // 1059 // Also collapse nested left-shifts with constant rhs: 1060 // (X << con1) << con2 ==> X << (con1 + con2) 1061 Node *LShiftINode::Ideal(PhaseGVN *phase, bool can_reshape) { 1062 int con = mask_and_replace_shift_amount(phase, this, BitsPerJavaInteger); 1063 if (con == 0) { 1064 return nullptr; 1065 } 1066 1067 // Left input is an add? 1068 Node *add1 = in(1); 1069 int add1_op = add1->Opcode(); 1070 if( add1_op == Op_AddI ) { // Left input is an add? 1071 assert( add1 != add1->in(1), "dead loop in LShiftINode::Ideal" ); 1072 1073 // Transform is legal, but check for profit. Avoid breaking 'i2s' 1074 // and 'i2b' patterns which typically fold into 'StoreC/StoreB'. 1075 if( con < 16 ) { 1076 // Left input is an add of the same number? 1077 if (add1->in(1) == add1->in(2)) { 1078 // Convert "(x + x) << c0" into "x << (c0 + 1)" 1079 // In general, this optimization cannot be applied for c0 == 31 since 1080 // 2x << 31 != x << 32 = x << 0 = x (e.g. x = 1: 2 << 31 = 0 != 1) 1081 return new LShiftINode(add1->in(1), phase->intcon(con + 1)); 1082 } 1083 1084 // Left input is an add of a constant? 1085 const TypeInt *t12 = phase->type(add1->in(2))->isa_int(); 1086 if( t12 && t12->is_con() ){ // Left input is an add of a con? 1087 // Compute X << con0 1088 Node *lsh = phase->transform( new LShiftINode( add1->in(1), in(2) ) ); 1089 // Compute X<<con0 + (con1<<con0) 1090 return new AddINode( lsh, phase->intcon(t12->get_con() << con)); 1091 } 1092 } 1093 } 1094 1095 // Check for "(x >> C1) << C2" 1096 if (add1_op == Op_RShiftI || add1_op == Op_URShiftI) { 1097 int add1Con = 0; 1098 const_shift_count(phase, add1, &add1Con); 1099 1100 // Special case C1 == C2, which just masks off low bits 1101 if (add1Con > 0 && con == add1Con) { 1102 // Convert to "(x & -(1 << C2))" 1103 return new AndINode(add1->in(1), phase->intcon(java_negate(jint(1 << con)))); 1104 } else { 1105 // Wait until the right shift has been sharpened to the correct count 1106 if (add1Con > 0 && add1Con < BitsPerJavaInteger) { 1107 // As loop parsing can produce LShiftI nodes, we should wait until the graph is fully formed 1108 // to apply optimizations, otherwise we can inadvertently stop vectorization opportunities. 1109 if (phase->is_IterGVN()) { 1110 if (con > add1Con) { 1111 // Creates "(x << (C2 - C1)) & -(1 << C2)" 1112 Node* lshift = phase->transform(new LShiftINode(add1->in(1), phase->intcon(con - add1Con))); 1113 return new AndINode(lshift, phase->intcon(java_negate(jint(1 << con)))); 1114 } else { 1115 assert(con < add1Con, "must be (%d < %d)", con, add1Con); 1116 // Creates "(x >> (C1 - C2)) & -(1 << C2)" 1117 1118 // Handle logical and arithmetic shifts 1119 Node* rshift; 1120 if (add1_op == Op_RShiftI) { 1121 rshift = phase->transform(new RShiftINode(add1->in(1), phase->intcon(add1Con - con))); 1122 } else { 1123 rshift = phase->transform(new URShiftINode(add1->in(1), phase->intcon(add1Con - con))); 1124 } 1125 1126 return new AndINode(rshift, phase->intcon(java_negate(jint(1 << con)))); 1127 } 1128 } else { 1129 phase->record_for_igvn(this); 1130 } 1131 } 1132 } 1133 } 1134 1135 // Check for "((x >> C1) & Y) << C2" 1136 if (add1_op == Op_AndI) { 1137 Node *add2 = add1->in(1); 1138 int add2_op = add2->Opcode(); 1139 if (add2_op == Op_RShiftI || add2_op == Op_URShiftI) { 1140 // Special case C1 == C2, which just masks off low bits 1141 if (add2->in(2) == in(2)) { 1142 // Convert to "(x & (Y << C2))" 1143 Node* y_sh = phase->transform(new LShiftINode(add1->in(2), phase->intcon(con))); 1144 return new AndINode(add2->in(1), y_sh); 1145 } 1146 1147 int add2Con = 0; 1148 const_shift_count(phase, add2, &add2Con); 1149 if (add2Con > 0 && add2Con < BitsPerJavaInteger) { 1150 if (phase->is_IterGVN()) { 1151 // Convert to "((x >> C1) << C2) & (Y << C2)" 1152 1153 // Make "(x >> C1) << C2", which will get folded away by the rule above 1154 Node* x_sh = phase->transform(new LShiftINode(add2, phase->intcon(con))); 1155 // Make "Y << C2", which will simplify when Y is a constant 1156 Node* y_sh = phase->transform(new LShiftINode(add1->in(2), phase->intcon(con))); 1157 1158 return new AndINode(x_sh, y_sh); 1159 } else { 1160 phase->record_for_igvn(this); 1161 } 1162 } 1163 } 1164 } 1165 1166 // Check for ((x & ((1<<(32-c0))-1)) << c0) which ANDs off high bits 1167 // before shifting them away. 1168 const jint bits_mask = right_n_bits(BitsPerJavaInteger-con); 1169 if( add1_op == Op_AndI && 1170 phase->type(add1->in(2)) == TypeInt::make( bits_mask ) ) 1171 return new LShiftINode( add1->in(1), in(2) ); 1172 1173 // Performs: 1174 // (X << con1) << con2 ==> X << (con1 + con2) 1175 Node* doubleShift = collapse_nested_shift_left(phase, this, con, T_INT); 1176 if (doubleShift != nullptr) { 1177 return doubleShift; 1178 } 1179 1180 return nullptr; 1181 } 1182 1183 //------------------------------Value------------------------------------------ 1184 // A LShiftINode shifts its input2 left by input1 amount. 1185 const Type* LShiftINode::Value(PhaseGVN* phase) const { 1186 const Type *t1 = phase->type( in(1) ); 1187 const Type *t2 = phase->type( in(2) ); 1188 // Either input is TOP ==> the result is TOP 1189 if( t1 == Type::TOP ) return Type::TOP; 1190 if( t2 == Type::TOP ) return Type::TOP; 1191 1192 // Left input is ZERO ==> the result is ZERO. 1193 if( t1 == TypeInt::ZERO ) return TypeInt::ZERO; 1194 // Shift by zero does nothing 1195 if( t2 == TypeInt::ZERO ) return t1; 1196 1197 // Either input is BOTTOM ==> the result is BOTTOM 1198 if( (t1 == TypeInt::INT) || (t2 == TypeInt::INT) || 1199 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) 1200 return TypeInt::INT; 1201 1202 const TypeInt *r1 = t1->is_int(); // Handy access 1203 const TypeInt *r2 = t2->is_int(); // Handy access 1204 1205 if (!r2->is_con()) 1206 return TypeInt::INT; 1207 1208 uint shift = r2->get_con(); 1209 shift &= BitsPerJavaInteger-1; // semantics of Java shifts 1210 // Shift by a multiple of 32 does nothing: 1211 if (shift == 0) return t1; 1212 1213 // If the shift is a constant, shift the bounds of the type, 1214 // unless this could lead to an overflow. 1215 if (!r1->is_con()) { 1216 jint lo = r1->_lo, hi = r1->_hi; 1217 if (((lo << shift) >> shift) == lo && 1218 ((hi << shift) >> shift) == hi) { 1219 // No overflow. The range shifts up cleanly. 1220 return TypeInt::make((jint)lo << (jint)shift, 1221 (jint)hi << (jint)shift, 1222 MAX2(r1->_widen,r2->_widen)); 1223 } 1224 return TypeInt::INT; 1225 } 1226 1227 return TypeInt::make( (jint)r1->get_con() << (jint)shift ); 1228 } 1229 1230 //============================================================================= 1231 //------------------------------Identity--------------------------------------- 1232 Node* LShiftLNode::Identity(PhaseGVN* phase) { 1233 int count = 0; 1234 if (const_shift_count(phase, this, &count) && (count & (BitsPerJavaLong - 1)) == 0) { 1235 // Shift by a multiple of 64 does nothing 1236 return in(1); 1237 } 1238 return this; 1239 } 1240 1241 //------------------------------Ideal------------------------------------------ 1242 // If the right input is a constant, and the left input is an add of a 1243 // constant, flatten the tree: (X+con1)<<con0 ==> X<<con0 + con1<<con0 1244 // 1245 // Also collapse nested left-shifts with constant rhs: 1246 // (X << con1) << con2 ==> X << (con1 + con2) 1247 Node *LShiftLNode::Ideal(PhaseGVN *phase, bool can_reshape) { 1248 int con = mask_and_replace_shift_amount(phase, this, BitsPerJavaLong); 1249 if (con == 0) { 1250 return nullptr; 1251 } 1252 1253 // Left input is an add? 1254 Node *add1 = in(1); 1255 int add1_op = add1->Opcode(); 1256 if( add1_op == Op_AddL ) { // Left input is an add? 1257 // Avoid dead data cycles from dead loops 1258 assert( add1 != add1->in(1), "dead loop in LShiftLNode::Ideal" ); 1259 1260 // Left input is an add of the same number? 1261 if (con != (BitsPerJavaLong - 1) && add1->in(1) == add1->in(2)) { 1262 // Convert "(x + x) << c0" into "x << (c0 + 1)" 1263 // Can only be applied if c0 != 63 because: 1264 // (x + x) << 63 = 2x << 63, while 1265 // (x + x) << 63 --transform--> x << 64 = x << 0 = x (!= 2x << 63, for example for x = 1) 1266 // According to the Java spec, chapter 15.19, we only consider the six lowest-order bits of the right-hand operand 1267 // (i.e. "right-hand operand" & 0b111111). Therefore, x << 64 is the same as x << 0 (64 = 0b10000000 & 0b0111111 = 0). 1268 return new LShiftLNode(add1->in(1), phase->intcon(con + 1)); 1269 } 1270 1271 // Left input is an add of a constant? 1272 const TypeLong *t12 = phase->type(add1->in(2))->isa_long(); 1273 if( t12 && t12->is_con() ){ // Left input is an add of a con? 1274 // Compute X << con0 1275 Node *lsh = phase->transform( new LShiftLNode( add1->in(1), in(2) ) ); 1276 // Compute X<<con0 + (con1<<con0) 1277 return new AddLNode( lsh, phase->longcon(t12->get_con() << con)); 1278 } 1279 } 1280 1281 // Check for "(x >> C1) << C2" 1282 if (add1_op == Op_RShiftL || add1_op == Op_URShiftL) { 1283 int add1Con = 0; 1284 const_shift_count(phase, add1, &add1Con); 1285 1286 // Special case C1 == C2, which just masks off low bits 1287 if (add1Con > 0 && con == add1Con) { 1288 // Convert to "(x & -(1 << C2))" 1289 return new AndLNode(add1->in(1), phase->longcon(java_negate(jlong(CONST64(1) << con)))); 1290 } else { 1291 // Wait until the right shift has been sharpened to the correct count 1292 if (add1Con > 0 && add1Con < BitsPerJavaLong) { 1293 // As loop parsing can produce LShiftI nodes, we should wait until the graph is fully formed 1294 // to apply optimizations, otherwise we can inadvertently stop vectorization opportunities. 1295 if (phase->is_IterGVN()) { 1296 if (con > add1Con) { 1297 // Creates "(x << (C2 - C1)) & -(1 << C2)" 1298 Node* lshift = phase->transform(new LShiftLNode(add1->in(1), phase->intcon(con - add1Con))); 1299 return new AndLNode(lshift, phase->longcon(java_negate(jlong(CONST64(1) << con)))); 1300 } else { 1301 assert(con < add1Con, "must be (%d < %d)", con, add1Con); 1302 // Creates "(x >> (C1 - C2)) & -(1 << C2)" 1303 1304 // Handle logical and arithmetic shifts 1305 Node* rshift; 1306 if (add1_op == Op_RShiftL) { 1307 rshift = phase->transform(new RShiftLNode(add1->in(1), phase->intcon(add1Con - con))); 1308 } else { 1309 rshift = phase->transform(new URShiftLNode(add1->in(1), phase->intcon(add1Con - con))); 1310 } 1311 1312 return new AndLNode(rshift, phase->longcon(java_negate(jlong(CONST64(1) << con)))); 1313 } 1314 } else { 1315 phase->record_for_igvn(this); 1316 } 1317 } 1318 } 1319 } 1320 1321 // Check for "((x >> C1) & Y) << C2" 1322 if (add1_op == Op_AndL) { 1323 Node* add2 = add1->in(1); 1324 int add2_op = add2->Opcode(); 1325 if (add2_op == Op_RShiftL || add2_op == Op_URShiftL) { 1326 // Special case C1 == C2, which just masks off low bits 1327 if (add2->in(2) == in(2)) { 1328 // Convert to "(x & (Y << C2))" 1329 Node* y_sh = phase->transform(new LShiftLNode(add1->in(2), phase->intcon(con))); 1330 return new AndLNode(add2->in(1), y_sh); 1331 } 1332 1333 int add2Con = 0; 1334 const_shift_count(phase, add2, &add2Con); 1335 if (add2Con > 0 && add2Con < BitsPerJavaLong) { 1336 if (phase->is_IterGVN()) { 1337 // Convert to "((x >> C1) << C2) & (Y << C2)" 1338 1339 // Make "(x >> C1) << C2", which will get folded away by the rule above 1340 Node* x_sh = phase->transform(new LShiftLNode(add2, phase->intcon(con))); 1341 // Make "Y << C2", which will simplify when Y is a constant 1342 Node* y_sh = phase->transform(new LShiftLNode(add1->in(2), phase->intcon(con))); 1343 1344 return new AndLNode(x_sh, y_sh); 1345 } else { 1346 phase->record_for_igvn(this); 1347 } 1348 } 1349 } 1350 } 1351 1352 // Check for ((x & ((CONST64(1)<<(64-c0))-1)) << c0) which ANDs off high bits 1353 // before shifting them away. 1354 const jlong bits_mask = jlong(max_julong >> con); 1355 if( add1_op == Op_AndL && 1356 phase->type(add1->in(2)) == TypeLong::make( bits_mask ) ) 1357 return new LShiftLNode( add1->in(1), in(2) ); 1358 1359 // Performs: 1360 // (X << con1) << con2 ==> X << (con1 + con2) 1361 Node* doubleShift = collapse_nested_shift_left(phase, this, con, T_LONG); 1362 if (doubleShift != nullptr) { 1363 return doubleShift; 1364 } 1365 1366 return nullptr; 1367 } 1368 1369 //------------------------------Value------------------------------------------ 1370 // A LShiftLNode shifts its input2 left by input1 amount. 1371 const Type* LShiftLNode::Value(PhaseGVN* phase) const { 1372 const Type *t1 = phase->type( in(1) ); 1373 const Type *t2 = phase->type( in(2) ); 1374 // Either input is TOP ==> the result is TOP 1375 if( t1 == Type::TOP ) return Type::TOP; 1376 if( t2 == Type::TOP ) return Type::TOP; 1377 1378 // Left input is ZERO ==> the result is ZERO. 1379 if( t1 == TypeLong::ZERO ) return TypeLong::ZERO; 1380 // Shift by zero does nothing 1381 if( t2 == TypeInt::ZERO ) return t1; 1382 1383 // Either input is BOTTOM ==> the result is BOTTOM 1384 if( (t1 == TypeLong::LONG) || (t2 == TypeInt::INT) || 1385 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) 1386 return TypeLong::LONG; 1387 1388 const TypeLong *r1 = t1->is_long(); // Handy access 1389 const TypeInt *r2 = t2->is_int(); // Handy access 1390 1391 if (!r2->is_con()) 1392 return TypeLong::LONG; 1393 1394 uint shift = r2->get_con(); 1395 shift &= BitsPerJavaLong - 1; // semantics of Java shifts 1396 // Shift by a multiple of 64 does nothing: 1397 if (shift == 0) return t1; 1398 1399 // If the shift is a constant, shift the bounds of the type, 1400 // unless this could lead to an overflow. 1401 if (!r1->is_con()) { 1402 jlong lo = r1->_lo, hi = r1->_hi; 1403 if (((lo << shift) >> shift) == lo && 1404 ((hi << shift) >> shift) == hi) { 1405 // No overflow. The range shifts up cleanly. 1406 return TypeLong::make((jlong)lo << (jint)shift, 1407 (jlong)hi << (jint)shift, 1408 MAX2(r1->_widen,r2->_widen)); 1409 } 1410 return TypeLong::LONG; 1411 } 1412 1413 return TypeLong::make( (jlong)r1->get_con() << (jint)shift ); 1414 } 1415 1416 RShiftNode* RShiftNode::make(Node* in1, Node* in2, BasicType bt) { 1417 switch (bt) { 1418 case T_INT: 1419 return new RShiftINode(in1, in2); 1420 case T_LONG: 1421 return new RShiftLNode(in1, in2); 1422 default: 1423 fatal("Not implemented for %s", type2name(bt)); 1424 } 1425 return nullptr; 1426 } 1427 1428 1429 //============================================================================= 1430 //------------------------------Identity--------------------------------------- 1431 Node* RShiftNode::IdentityIL(PhaseGVN* phase, BasicType bt) { 1432 int count = 0; 1433 if (const_shift_count(phase, this, &count)) { 1434 if ((count & (bits_per_java_integer(bt) - 1)) == 0) { 1435 // Shift by a multiple of 32/64 does nothing 1436 return in(1); 1437 } 1438 // Check for useless sign-masking 1439 if (in(1)->Opcode() == Op_LShift(bt) && 1440 in(1)->req() == 3 && 1441 in(1)->in(2) == in(2)) { 1442 count &= bits_per_java_integer(bt) - 1; // semantics of Java shifts 1443 // Compute masks for which this shifting doesn't change 1444 jlong lo = (CONST64(-1) << (bits_per_java_integer(bt) - ((uint)count)-1)); // FFFF8000 1445 jlong hi = ~lo; // 00007FFF 1446 const TypeInteger* t11 = phase->type(in(1)->in(1))->isa_integer(bt); 1447 if (t11 == nullptr) { 1448 return this; 1449 } 1450 // Does actual value fit inside of mask? 1451 if (lo <= t11->lo_as_long() && t11->hi_as_long() <= hi) { 1452 return in(1)->in(1); // Then shifting is a nop 1453 } 1454 } 1455 } 1456 return this; 1457 } 1458 1459 Node* RShiftINode::Identity(PhaseGVN* phase) { 1460 return IdentityIL(phase, T_INT); 1461 } 1462 1463 Node* RShiftNode::IdealIL(PhaseGVN* phase, bool can_reshape, BasicType bt) { 1464 // Inputs may be TOP if they are dead. 1465 const TypeInteger* t1 = phase->type(in(1))->isa_integer(bt); 1466 if (t1 == nullptr) { 1467 return NodeSentinel; // Left input is an integer 1468 } 1469 int shift = mask_and_replace_shift_amount(phase, this, bits_per_java_integer(bt)); 1470 if (shift == 0) { 1471 return NodeSentinel; 1472 } 1473 1474 // Check for (x & 0xFF000000) >> 24, whose mask can be made smaller. 1475 // and convert to (x >> 24) & (0xFF000000 >> 24) = x >> 24 1476 // Such expressions arise normally from shift chains like (byte)(x >> 24). 1477 const Node* and_node = in(1); 1478 if (and_node->Opcode() != Op_And(bt)) { 1479 return nullptr; 1480 } 1481 const TypeInteger* mask_t = phase->type(and_node->in(2))->isa_integer(bt); 1482 if (mask_t != nullptr && mask_t->is_con()) { 1483 jlong maskbits = mask_t->get_con_as_long(bt); 1484 // Convert to "(x >> shift) & (mask >> shift)" 1485 Node* shr_nomask = phase->transform(RShiftNode::make(and_node->in(1), in(2), bt)); 1486 return MulNode::make_and(shr_nomask, phase->integercon(maskbits >> shift, bt), bt); 1487 } 1488 return nullptr; 1489 } 1490 1491 Node* RShiftINode::Ideal(PhaseGVN* phase, bool can_reshape) { 1492 Node* progress = IdealIL(phase, can_reshape, T_INT); 1493 if (progress == NodeSentinel) { 1494 return nullptr; 1495 } 1496 if (progress != nullptr) { 1497 return progress; 1498 } 1499 int shift = mask_and_replace_shift_amount(phase, this, BitsPerJavaInteger); 1500 assert(shift != 0, "handled by IdealIL"); 1501 1502 // Check for "(short[i] <<16)>>16" which simply sign-extends 1503 const Node *shl = in(1); 1504 if (shl->Opcode() != Op_LShiftI) { 1505 return nullptr; 1506 } 1507 1508 const TypeInt* left_shift_t = phase->type(shl->in(2))->isa_int(); 1509 if (left_shift_t == nullptr) { 1510 return nullptr; 1511 } 1512 if (shift == 16 && left_shift_t->is_con(16)) { 1513 Node *ld = shl->in(1); 1514 if (ld->Opcode() == Op_LoadS) { 1515 // Sign extension is just useless here. Return a RShiftI of zero instead 1516 // returning 'ld' directly. We cannot return an old Node directly as 1517 // that is the job of 'Identity' calls and Identity calls only work on 1518 // direct inputs ('ld' is an extra Node removed from 'this'). The 1519 // combined optimization requires Identity only return direct inputs. 1520 set_req_X(1, ld, phase); 1521 set_req_X(2, phase->intcon(0), phase); 1522 return this; 1523 } 1524 else if (can_reshape && 1525 ld->Opcode() == Op_LoadUS && 1526 ld->outcnt() == 1 && ld->unique_out() == shl) 1527 // Replace zero-extension-load with sign-extension-load 1528 return ld->as_Load()->convert_to_signed_load(*phase); 1529 } 1530 1531 // Check for "(byte[i] <<24)>>24" which simply sign-extends 1532 if (shift == 24 && left_shift_t->is_con(24)) { 1533 Node *ld = shl->in(1); 1534 if (ld->Opcode() == Op_LoadB) { 1535 // Sign extension is just useless here 1536 set_req_X(1, ld, phase); 1537 set_req_X(2, phase->intcon(0), phase); 1538 return this; 1539 } 1540 } 1541 1542 return nullptr; 1543 } 1544 1545 const Type* RShiftNode::ValueIL(PhaseGVN* phase, BasicType bt) const { 1546 const Type* t1 = phase->type(in(1)); 1547 const Type* t2 = phase->type(in(2)); 1548 // Either input is TOP ==> the result is TOP 1549 if (t1 == Type::TOP) { 1550 return Type::TOP; 1551 } 1552 if (t2 == Type::TOP) { 1553 return Type::TOP; 1554 } 1555 1556 // Left input is ZERO ==> the result is ZERO. 1557 if (t1 == TypeInteger::zero(bt)) { 1558 return TypeInteger::zero(bt); 1559 } 1560 // Shift by zero does nothing 1561 if (t2 == TypeInt::ZERO) { 1562 return t1; 1563 } 1564 1565 // Either input is BOTTOM ==> the result is BOTTOM 1566 if (t1 == Type::BOTTOM || t2 == Type::BOTTOM) { 1567 return TypeInteger::bottom(bt); 1568 } 1569 1570 const TypeInteger* r1 = t1->isa_integer(bt); 1571 const TypeInt* r2 = t2->isa_int(); 1572 1573 // If the shift is a constant, just shift the bounds of the type. 1574 // For example, if the shift is 31/63, we just propagate sign bits. 1575 if (!r1->is_con() && r2->is_con()) { 1576 uint shift = r2->get_con(); 1577 shift &= bits_per_java_integer(bt) - 1; // semantics of Java shifts 1578 // Shift by a multiple of 32/64 does nothing: 1579 if (shift == 0) { 1580 return t1; 1581 } 1582 // Calculate reasonably aggressive bounds for the result. 1583 // This is necessary if we are to correctly type things 1584 // like (x<<24>>24) == ((byte)x). 1585 jlong lo = r1->lo_as_long() >> (jint)shift; 1586 jlong hi = r1->hi_as_long() >> (jint)shift; 1587 assert(lo <= hi, "must have valid bounds"); 1588 #ifdef ASSERT 1589 if (bt == T_INT) { 1590 jint lo_verify = checked_cast<jint>(r1->lo_as_long()) >> (jint)shift; 1591 jint hi_verify = checked_cast<jint>(r1->hi_as_long()) >> (jint)shift; 1592 assert((checked_cast<jint>(lo) == lo_verify) && (checked_cast<jint>(hi) == hi_verify), "inconsistent"); 1593 } 1594 #endif 1595 const TypeInteger* ti = TypeInteger::make(lo, hi, MAX2(r1->_widen,r2->_widen), bt); 1596 #ifdef ASSERT 1597 // Make sure we get the sign-capture idiom correct. 1598 if (shift == bits_per_java_integer(bt) - 1) { 1599 if (r1->lo_as_long() >= 0) { 1600 assert(ti == TypeInteger::zero(bt), ">>31/63 of + is 0"); 1601 } 1602 if (r1->hi_as_long() < 0) { 1603 assert(ti == TypeInteger::minus_1(bt), ">>31/63 of - is -1"); 1604 } 1605 } 1606 #endif 1607 return ti; 1608 } 1609 1610 if (!r1->is_con() || !r2->is_con()) { 1611 // If the left input is non-negative the result must also be non-negative, regardless of what the right input is. 1612 if (r1->lo_as_long() >= 0) { 1613 return TypeInteger::make(0, r1->hi_as_long(), MAX2(r1->_widen, r2->_widen), bt); 1614 } 1615 1616 // Conversely, if the left input is negative then the result must be negative. 1617 if (r1->hi_as_long() <= -1) { 1618 return TypeInteger::make(r1->lo_as_long(), -1, MAX2(r1->_widen, r2->_widen), bt); 1619 } 1620 1621 return TypeInteger::bottom(bt); 1622 } 1623 1624 // Signed shift right 1625 return TypeInteger::make(r1->get_con_as_long(bt) >> (r2->get_con() & (bits_per_java_integer(bt) - 1)), bt); 1626 } 1627 1628 const Type* RShiftINode::Value(PhaseGVN* phase) const { 1629 return ValueIL(phase, T_INT); 1630 } 1631 1632 //============================================================================= 1633 //------------------------------Identity--------------------------------------- 1634 Node* RShiftLNode::Identity(PhaseGVN* phase) { 1635 return IdentityIL(phase, T_LONG); 1636 } 1637 1638 Node* RShiftLNode::Ideal(PhaseGVN *phase, bool can_reshape) { 1639 Node* progress = IdealIL(phase, can_reshape, T_LONG); 1640 if (progress == NodeSentinel) { 1641 return nullptr; 1642 } 1643 return progress; 1644 } 1645 1646 const Type* RShiftLNode::Value(PhaseGVN* phase) const { 1647 return ValueIL(phase, T_LONG); 1648 } 1649 1650 //============================================================================= 1651 //------------------------------Identity--------------------------------------- 1652 Node* URShiftINode::Identity(PhaseGVN* phase) { 1653 int count = 0; 1654 if (const_shift_count(phase, this, &count) && (count & (BitsPerJavaInteger - 1)) == 0) { 1655 // Shift by a multiple of 32 does nothing 1656 return in(1); 1657 } 1658 1659 // Check for "((x << LogBytesPerWord) + (wordSize-1)) >> LogBytesPerWord" which is just "x". 1660 // Happens during new-array length computation. 1661 // Safe if 'x' is in the range [0..(max_int>>LogBytesPerWord)] 1662 Node *add = in(1); 1663 if (add->Opcode() == Op_AddI) { 1664 const TypeInt *t2 = phase->type(add->in(2))->isa_int(); 1665 if (t2 && t2->is_con(wordSize - 1) && 1666 add->in(1)->Opcode() == Op_LShiftI) { 1667 // Check that shift_counts are LogBytesPerWord. 1668 Node *lshift_count = add->in(1)->in(2); 1669 const TypeInt *t_lshift_count = phase->type(lshift_count)->isa_int(); 1670 if (t_lshift_count && t_lshift_count->is_con(LogBytesPerWord) && 1671 t_lshift_count == phase->type(in(2))) { 1672 Node *x = add->in(1)->in(1); 1673 const TypeInt *t_x = phase->type(x)->isa_int(); 1674 if (t_x != nullptr && 0 <= t_x->_lo && t_x->_hi <= (max_jint>>LogBytesPerWord)) { 1675 return x; 1676 } 1677 } 1678 } 1679 } 1680 1681 return (phase->type(in(2))->higher_equal(TypeInt::ZERO)) ? in(1) : this; 1682 } 1683 1684 //------------------------------Ideal------------------------------------------ 1685 Node *URShiftINode::Ideal(PhaseGVN *phase, bool can_reshape) { 1686 int con = mask_and_replace_shift_amount(phase, this, BitsPerJavaInteger); 1687 if (con == 0) { 1688 return nullptr; 1689 } 1690 1691 // We'll be wanting the right-shift amount as a mask of that many bits 1692 const int mask = right_n_bits(BitsPerJavaInteger - con); 1693 1694 int in1_op = in(1)->Opcode(); 1695 1696 // Check for ((x>>>a)>>>b) and replace with (x>>>(a+b)) when a+b < 32 1697 if( in1_op == Op_URShiftI ) { 1698 const TypeInt *t12 = phase->type( in(1)->in(2) )->isa_int(); 1699 if( t12 && t12->is_con() ) { // Right input is a constant 1700 assert( in(1) != in(1)->in(1), "dead loop in URShiftINode::Ideal" ); 1701 const int con2 = t12->get_con() & 31; // Shift count is always masked 1702 const int con3 = con+con2; 1703 if( con3 < 32 ) // Only merge shifts if total is < 32 1704 return new URShiftINode( in(1)->in(1), phase->intcon(con3) ); 1705 } 1706 } 1707 1708 // Check for ((x << z) + Y) >>> z. Replace with x + con>>>z 1709 // The idiom for rounding to a power of 2 is "(Q+(2^z-1)) >>> z". 1710 // If Q is "X << z" the rounding is useless. Look for patterns like 1711 // ((X<<Z) + Y) >>> Z and replace with (X + Y>>>Z) & Z-mask. 1712 Node *add = in(1); 1713 const TypeInt *t2 = phase->type(in(2))->isa_int(); 1714 if (in1_op == Op_AddI) { 1715 Node *lshl = add->in(1); 1716 if( lshl->Opcode() == Op_LShiftI && 1717 phase->type(lshl->in(2)) == t2 ) { 1718 Node *y_z = phase->transform( new URShiftINode(add->in(2),in(2)) ); 1719 Node *sum = phase->transform( new AddINode( lshl->in(1), y_z ) ); 1720 return new AndINode( sum, phase->intcon(mask) ); 1721 } 1722 } 1723 1724 // Check for (x & mask) >>> z. Replace with (x >>> z) & (mask >>> z) 1725 // This shortens the mask. Also, if we are extracting a high byte and 1726 // storing it to a buffer, the mask will be removed completely. 1727 Node *andi = in(1); 1728 if( in1_op == Op_AndI ) { 1729 const TypeInt *t3 = phase->type( andi->in(2) )->isa_int(); 1730 if( t3 && t3->is_con() ) { // Right input is a constant 1731 jint mask2 = t3->get_con(); 1732 mask2 >>= con; // *signed* shift downward (high-order zeroes do not help) 1733 Node *newshr = phase->transform( new URShiftINode(andi->in(1), in(2)) ); 1734 return new AndINode(newshr, phase->intcon(mask2)); 1735 // The negative values are easier to materialize than positive ones. 1736 // A typical case from address arithmetic is ((x & ~15) >> 4). 1737 // It's better to change that to ((x >> 4) & ~0) versus 1738 // ((x >> 4) & 0x0FFFFFFF). The difference is greatest in LP64. 1739 } 1740 } 1741 1742 // Check for "(X << z ) >>> z" which simply zero-extends 1743 Node *shl = in(1); 1744 if( in1_op == Op_LShiftI && 1745 phase->type(shl->in(2)) == t2 ) 1746 return new AndINode( shl->in(1), phase->intcon(mask) ); 1747 1748 // Check for (x >> n) >>> 31. Replace with (x >>> 31) 1749 Node *shr = in(1); 1750 if ( in1_op == Op_RShiftI ) { 1751 Node *in11 = shr->in(1); 1752 Node *in12 = shr->in(2); 1753 const TypeInt *t11 = phase->type(in11)->isa_int(); 1754 const TypeInt *t12 = phase->type(in12)->isa_int(); 1755 if ( t11 && t2 && t2->is_con(31) && t12 && t12->is_con() ) { 1756 return new URShiftINode(in11, phase->intcon(31)); 1757 } 1758 } 1759 1760 return nullptr; 1761 } 1762 1763 //------------------------------Value------------------------------------------ 1764 // A URShiftINode shifts its input2 right by input1 amount. 1765 const Type* URShiftINode::Value(PhaseGVN* phase) const { 1766 // (This is a near clone of RShiftINode::Value.) 1767 const Type *t1 = phase->type( in(1) ); 1768 const Type *t2 = phase->type( in(2) ); 1769 // Either input is TOP ==> the result is TOP 1770 if( t1 == Type::TOP ) return Type::TOP; 1771 if( t2 == Type::TOP ) return Type::TOP; 1772 1773 // Left input is ZERO ==> the result is ZERO. 1774 if( t1 == TypeInt::ZERO ) return TypeInt::ZERO; 1775 // Shift by zero does nothing 1776 if( t2 == TypeInt::ZERO ) return t1; 1777 1778 // Either input is BOTTOM ==> the result is BOTTOM 1779 if (t1 == Type::BOTTOM || t2 == Type::BOTTOM) 1780 return TypeInt::INT; 1781 1782 if (t2 == TypeInt::INT) 1783 return TypeInt::INT; 1784 1785 const TypeInt *r1 = t1->is_int(); // Handy access 1786 const TypeInt *r2 = t2->is_int(); // Handy access 1787 1788 if (r2->is_con()) { 1789 uint shift = r2->get_con(); 1790 shift &= BitsPerJavaInteger-1; // semantics of Java shifts 1791 // Shift by a multiple of 32 does nothing: 1792 if (shift == 0) return t1; 1793 // Calculate reasonably aggressive bounds for the result. 1794 jint lo = (juint)r1->_lo >> (juint)shift; 1795 jint hi = (juint)r1->_hi >> (juint)shift; 1796 if (r1->_hi >= 0 && r1->_lo < 0) { 1797 // If the type has both negative and positive values, 1798 // there are two separate sub-domains to worry about: 1799 // The positive half and the negative half. 1800 jint neg_lo = lo; 1801 jint neg_hi = (juint)-1 >> (juint)shift; 1802 jint pos_lo = (juint) 0 >> (juint)shift; 1803 jint pos_hi = hi; 1804 lo = MIN2(neg_lo, pos_lo); // == 0 1805 hi = MAX2(neg_hi, pos_hi); // == -1 >>> shift; 1806 } 1807 assert(lo <= hi, "must have valid bounds"); 1808 const TypeInt* ti = TypeInt::make(lo, hi, MAX2(r1->_widen,r2->_widen)); 1809 #ifdef ASSERT 1810 // Make sure we get the sign-capture idiom correct. 1811 if (shift == BitsPerJavaInteger-1) { 1812 if (r1->_lo >= 0) assert(ti == TypeInt::ZERO, ">>>31 of + is 0"); 1813 if (r1->_hi < 0) assert(ti == TypeInt::ONE, ">>>31 of - is +1"); 1814 } 1815 #endif 1816 return ti; 1817 } 1818 1819 // 1820 // Do not support shifted oops in info for GC 1821 // 1822 // else if( t1->base() == Type::InstPtr ) { 1823 // 1824 // const TypeInstPtr *o = t1->is_instptr(); 1825 // if( t1->singleton() ) 1826 // return TypeInt::make( ((uint32_t)o->const_oop() + o->_offset) >> shift ); 1827 // } 1828 // else if( t1->base() == Type::KlassPtr ) { 1829 // const TypeKlassPtr *o = t1->is_klassptr(); 1830 // if( t1->singleton() ) 1831 // return TypeInt::make( ((uint32_t)o->const_oop() + o->_offset) >> shift ); 1832 // } 1833 1834 return TypeInt::INT; 1835 } 1836 1837 //============================================================================= 1838 //------------------------------Identity--------------------------------------- 1839 Node* URShiftLNode::Identity(PhaseGVN* phase) { 1840 int count = 0; 1841 if (const_shift_count(phase, this, &count) && (count & (BitsPerJavaLong - 1)) == 0) { 1842 // Shift by a multiple of 64 does nothing 1843 return in(1); 1844 } 1845 return this; 1846 } 1847 1848 //------------------------------Ideal------------------------------------------ 1849 Node *URShiftLNode::Ideal(PhaseGVN *phase, bool can_reshape) { 1850 int con = mask_and_replace_shift_amount(phase, this, BitsPerJavaLong); 1851 if (con == 0) { 1852 return nullptr; 1853 } 1854 1855 // We'll be wanting the right-shift amount as a mask of that many bits 1856 const jlong mask = jlong(max_julong >> con); 1857 1858 // Check for ((x << z) + Y) >>> z. Replace with x + con>>>z 1859 // The idiom for rounding to a power of 2 is "(Q+(2^z-1)) >>> z". 1860 // If Q is "X << z" the rounding is useless. Look for patterns like 1861 // ((X<<Z) + Y) >>> Z and replace with (X + Y>>>Z) & Z-mask. 1862 Node *add = in(1); 1863 const TypeInt *t2 = phase->type(in(2))->isa_int(); 1864 if (add->Opcode() == Op_AddL) { 1865 Node *lshl = add->in(1); 1866 if( lshl->Opcode() == Op_LShiftL && 1867 phase->type(lshl->in(2)) == t2 ) { 1868 Node *y_z = phase->transform( new URShiftLNode(add->in(2),in(2)) ); 1869 Node *sum = phase->transform( new AddLNode( lshl->in(1), y_z ) ); 1870 return new AndLNode( sum, phase->longcon(mask) ); 1871 } 1872 } 1873 1874 // Check for (x & mask) >>> z. Replace with (x >>> z) & (mask >>> z) 1875 // This shortens the mask. Also, if we are extracting a high byte and 1876 // storing it to a buffer, the mask will be removed completely. 1877 Node *andi = in(1); 1878 if( andi->Opcode() == Op_AndL ) { 1879 const TypeLong *t3 = phase->type( andi->in(2) )->isa_long(); 1880 if( t3 && t3->is_con() ) { // Right input is a constant 1881 jlong mask2 = t3->get_con(); 1882 mask2 >>= con; // *signed* shift downward (high-order zeroes do not help) 1883 Node *newshr = phase->transform( new URShiftLNode(andi->in(1), in(2)) ); 1884 return new AndLNode(newshr, phase->longcon(mask2)); 1885 } 1886 } 1887 1888 // Check for "(X << z ) >>> z" which simply zero-extends 1889 Node *shl = in(1); 1890 if( shl->Opcode() == Op_LShiftL && 1891 phase->type(shl->in(2)) == t2 ) 1892 return new AndLNode( shl->in(1), phase->longcon(mask) ); 1893 1894 // Check for (x >> n) >>> 63. Replace with (x >>> 63) 1895 Node *shr = in(1); 1896 if ( shr->Opcode() == Op_RShiftL ) { 1897 Node *in11 = shr->in(1); 1898 Node *in12 = shr->in(2); 1899 const TypeLong *t11 = phase->type(in11)->isa_long(); 1900 const TypeInt *t12 = phase->type(in12)->isa_int(); 1901 if ( t11 && t2 && t2->is_con(63) && t12 && t12->is_con() ) { 1902 return new URShiftLNode(in11, phase->intcon(63)); 1903 } 1904 } 1905 return nullptr; 1906 } 1907 1908 //------------------------------Value------------------------------------------ 1909 // A URShiftINode shifts its input2 right by input1 amount. 1910 const Type* URShiftLNode::Value(PhaseGVN* phase) const { 1911 // (This is a near clone of RShiftLNode::Value.) 1912 const Type *t1 = phase->type( in(1) ); 1913 const Type *t2 = phase->type( in(2) ); 1914 // Either input is TOP ==> the result is TOP 1915 if( t1 == Type::TOP ) return Type::TOP; 1916 if( t2 == Type::TOP ) return Type::TOP; 1917 1918 // Left input is ZERO ==> the result is ZERO. 1919 if( t1 == TypeLong::ZERO ) return TypeLong::ZERO; 1920 // Shift by zero does nothing 1921 if( t2 == TypeInt::ZERO ) return t1; 1922 1923 // Either input is BOTTOM ==> the result is BOTTOM 1924 if (t1 == Type::BOTTOM || t2 == Type::BOTTOM) 1925 return TypeLong::LONG; 1926 1927 if (t2 == TypeInt::INT) 1928 return TypeLong::LONG; 1929 1930 const TypeLong *r1 = t1->is_long(); // Handy access 1931 const TypeInt *r2 = t2->is_int (); // Handy access 1932 1933 if (r2->is_con()) { 1934 uint shift = r2->get_con(); 1935 shift &= BitsPerJavaLong - 1; // semantics of Java shifts 1936 // Shift by a multiple of 64 does nothing: 1937 if (shift == 0) return t1; 1938 // Calculate reasonably aggressive bounds for the result. 1939 jlong lo = (julong)r1->_lo >> (juint)shift; 1940 jlong hi = (julong)r1->_hi >> (juint)shift; 1941 if (r1->_hi >= 0 && r1->_lo < 0) { 1942 // If the type has both negative and positive values, 1943 // there are two separate sub-domains to worry about: 1944 // The positive half and the negative half. 1945 jlong neg_lo = lo; 1946 jlong neg_hi = (julong)-1 >> (juint)shift; 1947 jlong pos_lo = (julong) 0 >> (juint)shift; 1948 jlong pos_hi = hi; 1949 //lo = MIN2(neg_lo, pos_lo); // == 0 1950 lo = neg_lo < pos_lo ? neg_lo : pos_lo; 1951 //hi = MAX2(neg_hi, pos_hi); // == -1 >>> shift; 1952 hi = neg_hi > pos_hi ? neg_hi : pos_hi; 1953 } 1954 assert(lo <= hi, "must have valid bounds"); 1955 const TypeLong* tl = TypeLong::make(lo, hi, MAX2(r1->_widen,r2->_widen)); 1956 #ifdef ASSERT 1957 // Make sure we get the sign-capture idiom correct. 1958 if (shift == BitsPerJavaLong - 1) { 1959 if (r1->_lo >= 0) assert(tl == TypeLong::ZERO, ">>>63 of + is 0"); 1960 if (r1->_hi < 0) assert(tl == TypeLong::ONE, ">>>63 of - is +1"); 1961 } 1962 #endif 1963 return tl; 1964 } 1965 1966 return TypeLong::LONG; // Give up 1967 } 1968 1969 //============================================================================= 1970 //------------------------------Ideal------------------------------------------ 1971 Node* FmaNode::Ideal(PhaseGVN* phase, bool can_reshape) { 1972 // We canonicalize the node by converting "(-a)*b+c" into "b*(-a)+c" 1973 // This reduces the number of rules in the matcher, as we only need to check 1974 // for negations on the second argument, and not the symmetric case where 1975 // the first argument is negated. 1976 if (in(1)->is_Neg() && !in(2)->is_Neg()) { 1977 swap_edges(1, 2); 1978 return this; 1979 } 1980 return nullptr; 1981 } 1982 1983 //============================================================================= 1984 //------------------------------Value------------------------------------------ 1985 const Type* FmaDNode::Value(PhaseGVN* phase) const { 1986 const Type *t1 = phase->type(in(1)); 1987 if (t1 == Type::TOP) return Type::TOP; 1988 if (t1->base() != Type::DoubleCon) return Type::DOUBLE; 1989 const Type *t2 = phase->type(in(2)); 1990 if (t2 == Type::TOP) return Type::TOP; 1991 if (t2->base() != Type::DoubleCon) return Type::DOUBLE; 1992 const Type *t3 = phase->type(in(3)); 1993 if (t3 == Type::TOP) return Type::TOP; 1994 if (t3->base() != Type::DoubleCon) return Type::DOUBLE; 1995 #ifndef __STDC_IEC_559__ 1996 return Type::DOUBLE; 1997 #else 1998 double d1 = t1->getd(); 1999 double d2 = t2->getd(); 2000 double d3 = t3->getd(); 2001 return TypeD::make(fma(d1, d2, d3)); 2002 #endif 2003 } 2004 2005 //============================================================================= 2006 //------------------------------Value------------------------------------------ 2007 const Type* FmaFNode::Value(PhaseGVN* phase) const { 2008 const Type *t1 = phase->type(in(1)); 2009 if (t1 == Type::TOP) return Type::TOP; 2010 if (t1->base() != Type::FloatCon) return Type::FLOAT; 2011 const Type *t2 = phase->type(in(2)); 2012 if (t2 == Type::TOP) return Type::TOP; 2013 if (t2->base() != Type::FloatCon) return Type::FLOAT; 2014 const Type *t3 = phase->type(in(3)); 2015 if (t3 == Type::TOP) return Type::TOP; 2016 if (t3->base() != Type::FloatCon) return Type::FLOAT; 2017 #ifndef __STDC_IEC_559__ 2018 return Type::FLOAT; 2019 #else 2020 float f1 = t1->getf(); 2021 float f2 = t2->getf(); 2022 float f3 = t3->getf(); 2023 return TypeF::make(fma(f1, f2, f3)); 2024 #endif 2025 } 2026 2027 //============================================================================= 2028 //------------------------------Value------------------------------------------ 2029 const Type* FmaHFNode::Value(PhaseGVN* phase) const { 2030 const Type* t1 = phase->type(in(1)); 2031 if (t1 == Type::TOP) { return Type::TOP; } 2032 if (t1->base() != Type::HalfFloatCon) { return Type::HALF_FLOAT; } 2033 const Type* t2 = phase->type(in(2)); 2034 if (t2 == Type::TOP) { return Type::TOP; } 2035 if (t2->base() != Type::HalfFloatCon) { return Type::HALF_FLOAT; } 2036 const Type* t3 = phase->type(in(3)); 2037 if (t3 == Type::TOP) { return Type::TOP; } 2038 if (t3->base() != Type::HalfFloatCon) { return Type::HALF_FLOAT; } 2039 #ifndef __STDC_IEC_559__ 2040 return Type::HALF_FLOAT; 2041 #else 2042 float f1 = t1->getf(); 2043 float f2 = t2->getf(); 2044 float f3 = t3->getf(); 2045 return TypeH::make(fma(f1, f2, f3)); 2046 #endif 2047 } 2048 2049 //============================================================================= 2050 //------------------------------hash------------------------------------------- 2051 // Hash function for MulAddS2INode. Operation is commutative with commutative pairs. 2052 // The hash function must return the same value when edge swapping is performed. 2053 uint MulAddS2INode::hash() const { 2054 return (uintptr_t)in(1) + (uintptr_t)in(2) + (uintptr_t)in(3) + (uintptr_t)in(4) + Opcode(); 2055 } 2056 2057 //------------------------------Rotate Operations ------------------------------ 2058 2059 Node* RotateLeftNode::Identity(PhaseGVN* phase) { 2060 const Type* t1 = phase->type(in(1)); 2061 if (t1 == Type::TOP) { 2062 return this; 2063 } 2064 int count = 0; 2065 assert(t1->isa_int() || t1->isa_long(), "Unexpected type"); 2066 int mask = (t1->isa_int() ? BitsPerJavaInteger : BitsPerJavaLong) - 1; 2067 if (const_shift_count(phase, this, &count) && (count & mask) == 0) { 2068 // Rotate by a multiple of 32/64 does nothing 2069 return in(1); 2070 } 2071 return this; 2072 } 2073 2074 const Type* RotateLeftNode::Value(PhaseGVN* phase) const { 2075 const Type* t1 = phase->type(in(1)); 2076 const Type* t2 = phase->type(in(2)); 2077 // Either input is TOP ==> the result is TOP 2078 if (t1 == Type::TOP || t2 == Type::TOP) { 2079 return Type::TOP; 2080 } 2081 2082 if (t1->isa_int()) { 2083 const TypeInt* r1 = t1->is_int(); 2084 const TypeInt* r2 = t2->is_int(); 2085 2086 // Left input is ZERO ==> the result is ZERO. 2087 if (r1 == TypeInt::ZERO) { 2088 return TypeInt::ZERO; 2089 } 2090 // Rotate by zero does nothing 2091 if (r2 == TypeInt::ZERO) { 2092 return r1; 2093 } 2094 if (r1->is_con() && r2->is_con()) { 2095 juint r1_con = (juint)r1->get_con(); 2096 juint shift = (juint)(r2->get_con()) & (juint)(BitsPerJavaInteger - 1); // semantics of Java shifts 2097 return TypeInt::make((r1_con << shift) | (r1_con >> (32 - shift))); 2098 } 2099 return TypeInt::INT; 2100 } else { 2101 assert(t1->isa_long(), "Type must be a long"); 2102 const TypeLong* r1 = t1->is_long(); 2103 const TypeInt* r2 = t2->is_int(); 2104 2105 // Left input is ZERO ==> the result is ZERO. 2106 if (r1 == TypeLong::ZERO) { 2107 return TypeLong::ZERO; 2108 } 2109 // Rotate by zero does nothing 2110 if (r2 == TypeInt::ZERO) { 2111 return r1; 2112 } 2113 if (r1->is_con() && r2->is_con()) { 2114 julong r1_con = (julong)r1->get_con(); 2115 julong shift = (julong)(r2->get_con()) & (julong)(BitsPerJavaLong - 1); // semantics of Java shifts 2116 return TypeLong::make((r1_con << shift) | (r1_con >> (64 - shift))); 2117 } 2118 return TypeLong::LONG; 2119 } 2120 } 2121 2122 Node* RotateLeftNode::Ideal(PhaseGVN *phase, bool can_reshape) { 2123 const Type* t1 = phase->type(in(1)); 2124 const Type* t2 = phase->type(in(2)); 2125 if (t2->isa_int() && t2->is_int()->is_con()) { 2126 if (t1->isa_int()) { 2127 int lshift = t2->is_int()->get_con() & 31; 2128 return new RotateRightNode(in(1), phase->intcon(32 - (lshift & 31)), TypeInt::INT); 2129 } else if (t1 != Type::TOP) { 2130 assert(t1->isa_long(), "Type must be a long"); 2131 int lshift = t2->is_int()->get_con() & 63; 2132 return new RotateRightNode(in(1), phase->intcon(64 - (lshift & 63)), TypeLong::LONG); 2133 } 2134 } 2135 return nullptr; 2136 } 2137 2138 Node* RotateRightNode::Identity(PhaseGVN* phase) { 2139 const Type* t1 = phase->type(in(1)); 2140 if (t1 == Type::TOP) { 2141 return this; 2142 } 2143 int count = 0; 2144 assert(t1->isa_int() || t1->isa_long(), "Unexpected type"); 2145 int mask = (t1->isa_int() ? BitsPerJavaInteger : BitsPerJavaLong) - 1; 2146 if (const_shift_count(phase, this, &count) && (count & mask) == 0) { 2147 // Rotate by a multiple of 32/64 does nothing 2148 return in(1); 2149 } 2150 return this; 2151 } 2152 2153 const Type* RotateRightNode::Value(PhaseGVN* phase) const { 2154 const Type* t1 = phase->type(in(1)); 2155 const Type* t2 = phase->type(in(2)); 2156 // Either input is TOP ==> the result is TOP 2157 if (t1 == Type::TOP || t2 == Type::TOP) { 2158 return Type::TOP; 2159 } 2160 2161 if (t1->isa_int()) { 2162 const TypeInt* r1 = t1->is_int(); 2163 const TypeInt* r2 = t2->is_int(); 2164 2165 // Left input is ZERO ==> the result is ZERO. 2166 if (r1 == TypeInt::ZERO) { 2167 return TypeInt::ZERO; 2168 } 2169 // Rotate by zero does nothing 2170 if (r2 == TypeInt::ZERO) { 2171 return r1; 2172 } 2173 if (r1->is_con() && r2->is_con()) { 2174 juint r1_con = (juint)r1->get_con(); 2175 juint shift = (juint)(r2->get_con()) & (juint)(BitsPerJavaInteger - 1); // semantics of Java shifts 2176 return TypeInt::make((r1_con >> shift) | (r1_con << (32 - shift))); 2177 } 2178 return TypeInt::INT; 2179 } else { 2180 assert(t1->isa_long(), "Type must be a long"); 2181 const TypeLong* r1 = t1->is_long(); 2182 const TypeInt* r2 = t2->is_int(); 2183 // Left input is ZERO ==> the result is ZERO. 2184 if (r1 == TypeLong::ZERO) { 2185 return TypeLong::ZERO; 2186 } 2187 // Rotate by zero does nothing 2188 if (r2 == TypeInt::ZERO) { 2189 return r1; 2190 } 2191 if (r1->is_con() && r2->is_con()) { 2192 julong r1_con = (julong)r1->get_con(); 2193 julong shift = (julong)(r2->get_con()) & (julong)(BitsPerJavaLong - 1); // semantics of Java shifts 2194 return TypeLong::make((r1_con >> shift) | (r1_con << (64 - shift))); 2195 } 2196 return TypeLong::LONG; 2197 } 2198 } 2199 2200 //------------------------------ Sum & Mask ------------------------------ 2201 2202 // Returns a lower bound on the number of trailing zeros in expr. 2203 static jint AndIL_min_trailing_zeros(const PhaseGVN* phase, const Node* expr, BasicType bt) { 2204 const TypeInteger* type = phase->type(expr)->isa_integer(bt); 2205 if (type == nullptr) { 2206 return 0; 2207 } 2208 2209 expr = expr->uncast(); 2210 type = phase->type(expr)->isa_integer(bt); 2211 if (type == nullptr) { 2212 return 0; 2213 } 2214 2215 if (type->is_con()) { 2216 jlong con = type->get_con_as_long(bt); 2217 return con == 0L ? (type2aelembytes(bt) * BitsPerByte) : count_trailing_zeros(con); 2218 } 2219 2220 if (expr->Opcode() == Op_ConvI2L) { 2221 expr = expr->in(1)->uncast(); 2222 bt = T_INT; 2223 type = phase->type(expr)->isa_int(); 2224 } 2225 2226 // Pattern: expr = (x << shift) 2227 if (expr->Opcode() == Op_LShift(bt)) { 2228 const TypeInt* shift_t = phase->type(expr->in(2))->isa_int(); 2229 if (shift_t == nullptr || !shift_t->is_con()) { 2230 return 0; 2231 } 2232 // We need to truncate the shift, as it may not have been canonicalized yet. 2233 // T_INT: 0..31 -> shift_mask = 4 * 8 - 1 = 31 2234 // T_LONG: 0..63 -> shift_mask = 8 * 8 - 1 = 63 2235 // (JLS: "Shift Operators") 2236 jint shift_mask = type2aelembytes(bt) * BitsPerByte - 1; 2237 return shift_t->get_con() & shift_mask; 2238 } 2239 2240 return 0; 2241 } 2242 2243 // Checks whether expr is neutral additive element (zero) under mask, 2244 // i.e. whether an expression of the form: 2245 // (AndX (AddX (expr addend) mask) 2246 // (expr + addend) & mask 2247 // is equivalent to 2248 // (AndX addend mask) 2249 // addend & mask 2250 // for any addend. 2251 // (The X in AndX must be I or L, depending on bt). 2252 // 2253 // We check for the sufficient condition when the lowest set bit in expr is higher than 2254 // the highest set bit in mask, i.e.: 2255 // expr: eeeeee0000000000000 2256 // mask: 000000mmmmmmmmmmmmm 2257 // <--w bits---> 2258 // We do not test for other cases. 2259 // 2260 // Correctness: 2261 // Given "expr" with at least "w" trailing zeros, 2262 // let "mod = 2^w", "suffix_mask = mod - 1" 2263 // 2264 // Since "mask" only has bits set where "suffix_mask" does, we have: 2265 // mask = suffix_mask & mask (SUFFIX_MASK) 2266 // 2267 // And since expr only has bits set above w, and suffix_mask only below: 2268 // expr & suffix_mask == 0 (NO_BIT_OVERLAP) 2269 // 2270 // From unsigned modular arithmetic (with unsigned modulo %), and since mod is 2271 // a power of 2, and we are computing in a ring of powers of 2, we know that 2272 // (x + y) % mod = (x % mod + y) % mod 2273 // (x + y) & suffix_mask = (x & suffix_mask + y) & suffix_mask (MOD_ARITH) 2274 // 2275 // We can now prove the equality: 2276 // (expr + addend) & mask 2277 // = (expr + addend) & suffix_mask & mask (SUFFIX_MASK) 2278 // = (expr & suffix_mask + addend) & suffix_mask & mask (MOD_ARITH) 2279 // = (0 + addend) & suffix_mask & mask (NO_BIT_OVERLAP) 2280 // = addend & mask (SUFFIX_MASK) 2281 // 2282 // Hence, an expr with at least w trailing zeros is a neutral additive element under any mask with bit width w. 2283 static bool AndIL_is_zero_element_under_mask(const PhaseGVN* phase, const Node* expr, const Node* mask, BasicType bt) { 2284 // When the mask is negative, it has the most significant bit set. 2285 const TypeInteger* mask_t = phase->type(mask)->isa_integer(bt); 2286 if (mask_t == nullptr || mask_t->lo_as_long() < 0) { 2287 return false; 2288 } 2289 2290 // When the mask is constant zero, we defer to MulNode::Value to eliminate the entire AndX operation. 2291 if (mask_t->hi_as_long() == 0) { 2292 assert(mask_t->lo_as_long() == 0, "checked earlier"); 2293 return false; 2294 } 2295 2296 jint mask_bit_width = BitsPerLong - count_leading_zeros(mask_t->hi_as_long()); 2297 jint expr_trailing_zeros = AndIL_min_trailing_zeros(phase, expr, bt); 2298 return expr_trailing_zeros >= mask_bit_width; 2299 } 2300 2301 // Reduces the pattern: 2302 // (AndX (AddX add1 add2) mask) 2303 // to 2304 // (AndX add1 mask), if add2 is neutral wrt mask (see above), and vice versa. 2305 Node* MulNode::AndIL_sum_and_mask(PhaseGVN* phase, BasicType bt) { 2306 Node* add = in(1); 2307 Node* mask = in(2); 2308 int addidx = 0; 2309 if (add->Opcode() == Op_Add(bt)) { 2310 addidx = 1; 2311 } else if (mask->Opcode() == Op_Add(bt)) { 2312 mask = add; 2313 addidx = 2; 2314 add = in(addidx); 2315 } 2316 if (addidx > 0) { 2317 Node* add1 = add->in(1); 2318 Node* add2 = add->in(2); 2319 if (AndIL_is_zero_element_under_mask(phase, add1, mask, bt)) { 2320 set_req_X(addidx, add2, phase); 2321 return this; 2322 } else if (AndIL_is_zero_element_under_mask(phase, add2, mask, bt)) { 2323 set_req_X(addidx, add1, phase); 2324 return this; 2325 } 2326 } 2327 return nullptr; 2328 }