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 //============================================================================= 954 955 static bool const_shift_count(PhaseGVN* phase, Node* shiftNode, int* count) { 956 const TypeInt* tcount = phase->type(shiftNode->in(2))->isa_int(); 957 if (tcount != nullptr && tcount->is_con()) { 958 *count = tcount->get_con(); 959 return true; 960 } 961 return false; 962 } 963 964 static int maskShiftAmount(PhaseGVN* phase, Node* shiftNode, uint nBits) { 965 int count = 0; 966 if (const_shift_count(phase, shiftNode, &count)) { 967 int maskedShift = count & (nBits - 1); 968 if (maskedShift == 0) { 969 // Let Identity() handle 0 shift count. 970 return 0; 971 } 972 973 if (count != maskedShift) { 974 shiftNode->set_req(2, phase->intcon(maskedShift)); // Replace shift count with masked value. 975 PhaseIterGVN* igvn = phase->is_IterGVN(); 976 if (igvn) { 977 igvn->rehash_node_delayed(shiftNode); 978 } 979 } 980 return maskedShift; 981 } 982 return 0; 983 } 984 985 // Called with 986 // outer_shift = (_ << con0) 987 // We are looking for the pattern: 988 // outer_shift = ((X << con1) << con0) 989 // we denote inner_shift the nested expression (X << con1) 990 // 991 // con0 and con1 are both in [0..nbits), as they are computed by maskShiftAmount. 992 // 993 // There are 2 cases: 994 // if con0 + con1 >= nbits => 0 995 // if con0 + con1 < nbits => X << (con1 + con0) 996 static Node* collapse_nested_shift_left(PhaseGVN* phase, Node* outer_shift, int con0, BasicType bt) { 997 assert(bt == T_LONG || bt == T_INT, "Unexpected type"); 998 int nbits = static_cast<int>(bits_per_java_integer(bt)); 999 Node* inner_shift = outer_shift->in(1); 1000 if (inner_shift->Opcode() != Op_LShift(bt)) { 1001 return nullptr; 1002 } 1003 1004 int con1 = maskShiftAmount(phase, inner_shift, nbits); 1005 if (con1 == 0) { // Either non-const, or actually 0 (up to mask) and then delegated to Identity() 1006 return nullptr; 1007 } 1008 1009 if (con0 + con1 >= nbits) { 1010 // While it might be tempting to use 1011 // phase->zerocon(bt); 1012 // it would be incorrect: zerocon caches nodes, while Ideal is only allowed 1013 // to return a new node, this or nullptr, but not an old (cached) node. 1014 return ConNode::make(TypeInteger::zero(bt)); 1015 } 1016 1017 // con0 + con1 < nbits ==> actual shift happens now 1018 Node* con0_plus_con1 = phase->intcon(con0 + con1); 1019 return LShiftNode::make(inner_shift->in(1), con0_plus_con1, bt); 1020 } 1021 1022 //------------------------------Identity--------------------------------------- 1023 Node* LShiftINode::Identity(PhaseGVN* phase) { 1024 int count = 0; 1025 if (const_shift_count(phase, this, &count) && (count & (BitsPerJavaInteger - 1)) == 0) { 1026 // Shift by a multiple of 32 does nothing 1027 return in(1); 1028 } 1029 return this; 1030 } 1031 1032 //------------------------------Ideal------------------------------------------ 1033 // If the right input is a constant, and the left input is an add of a 1034 // constant, flatten the tree: (X+con1)<<con0 ==> X<<con0 + con1<<con0 1035 // 1036 // Also collapse nested left-shifts with constant rhs: 1037 // (X << con1) << con2 ==> X << (con1 + con2) 1038 Node *LShiftINode::Ideal(PhaseGVN *phase, bool can_reshape) { 1039 int con = maskShiftAmount(phase, this, BitsPerJavaInteger); 1040 if (con == 0) { 1041 return nullptr; 1042 } 1043 1044 // Left input is an add? 1045 Node *add1 = in(1); 1046 int add1_op = add1->Opcode(); 1047 if( add1_op == Op_AddI ) { // Left input is an add? 1048 assert( add1 != add1->in(1), "dead loop in LShiftINode::Ideal" ); 1049 1050 // Transform is legal, but check for profit. Avoid breaking 'i2s' 1051 // and 'i2b' patterns which typically fold into 'StoreC/StoreB'. 1052 if( con < 16 ) { 1053 // Left input is an add of the same number? 1054 if (add1->in(1) == add1->in(2)) { 1055 // Convert "(x + x) << c0" into "x << (c0 + 1)" 1056 // In general, this optimization cannot be applied for c0 == 31 since 1057 // 2x << 31 != x << 32 = x << 0 = x (e.g. x = 1: 2 << 31 = 0 != 1) 1058 return new LShiftINode(add1->in(1), phase->intcon(con + 1)); 1059 } 1060 1061 // Left input is an add of a constant? 1062 const TypeInt *t12 = phase->type(add1->in(2))->isa_int(); 1063 if( t12 && t12->is_con() ){ // Left input is an add of a con? 1064 // Compute X << con0 1065 Node *lsh = phase->transform( new LShiftINode( add1->in(1), in(2) ) ); 1066 // Compute X<<con0 + (con1<<con0) 1067 return new AddINode( lsh, phase->intcon(t12->get_con() << con)); 1068 } 1069 } 1070 } 1071 1072 // Check for "(x >> C1) << C2" 1073 if (add1_op == Op_RShiftI || add1_op == Op_URShiftI) { 1074 int add1Con = 0; 1075 const_shift_count(phase, add1, &add1Con); 1076 1077 // Special case C1 == C2, which just masks off low bits 1078 if (add1Con > 0 && con == add1Con) { 1079 // Convert to "(x & -(1 << C2))" 1080 return new AndINode(add1->in(1), phase->intcon(java_negate(jint(1 << con)))); 1081 } else { 1082 // Wait until the right shift has been sharpened to the correct count 1083 if (add1Con > 0 && add1Con < BitsPerJavaInteger) { 1084 // As loop parsing can produce LShiftI nodes, we should wait until the graph is fully formed 1085 // to apply optimizations, otherwise we can inadvertently stop vectorization opportunities. 1086 if (phase->is_IterGVN()) { 1087 if (con > add1Con) { 1088 // Creates "(x << (C2 - C1)) & -(1 << C2)" 1089 Node* lshift = phase->transform(new LShiftINode(add1->in(1), phase->intcon(con - add1Con))); 1090 return new AndINode(lshift, phase->intcon(java_negate(jint(1 << con)))); 1091 } else { 1092 assert(con < add1Con, "must be (%d < %d)", con, add1Con); 1093 // Creates "(x >> (C1 - C2)) & -(1 << C2)" 1094 1095 // Handle logical and arithmetic shifts 1096 Node* rshift; 1097 if (add1_op == Op_RShiftI) { 1098 rshift = phase->transform(new RShiftINode(add1->in(1), phase->intcon(add1Con - con))); 1099 } else { 1100 rshift = phase->transform(new URShiftINode(add1->in(1), phase->intcon(add1Con - con))); 1101 } 1102 1103 return new AndINode(rshift, phase->intcon(java_negate(jint(1 << con)))); 1104 } 1105 } else { 1106 phase->record_for_igvn(this); 1107 } 1108 } 1109 } 1110 } 1111 1112 // Check for "((x >> C1) & Y) << C2" 1113 if (add1_op == Op_AndI) { 1114 Node *add2 = add1->in(1); 1115 int add2_op = add2->Opcode(); 1116 if (add2_op == Op_RShiftI || add2_op == Op_URShiftI) { 1117 // Special case C1 == C2, which just masks off low bits 1118 if (add2->in(2) == in(2)) { 1119 // Convert to "(x & (Y << C2))" 1120 Node* y_sh = phase->transform(new LShiftINode(add1->in(2), phase->intcon(con))); 1121 return new AndINode(add2->in(1), y_sh); 1122 } 1123 1124 int add2Con = 0; 1125 const_shift_count(phase, add2, &add2Con); 1126 if (add2Con > 0 && add2Con < BitsPerJavaInteger) { 1127 if (phase->is_IterGVN()) { 1128 // Convert to "((x >> C1) << C2) & (Y << C2)" 1129 1130 // Make "(x >> C1) << C2", which will get folded away by the rule above 1131 Node* x_sh = phase->transform(new LShiftINode(add2, phase->intcon(con))); 1132 // Make "Y << C2", which will simplify when Y is a constant 1133 Node* y_sh = phase->transform(new LShiftINode(add1->in(2), phase->intcon(con))); 1134 1135 return new AndINode(x_sh, y_sh); 1136 } else { 1137 phase->record_for_igvn(this); 1138 } 1139 } 1140 } 1141 } 1142 1143 // Check for ((x & ((1<<(32-c0))-1)) << c0) which ANDs off high bits 1144 // before shifting them away. 1145 const jint bits_mask = right_n_bits(BitsPerJavaInteger-con); 1146 if( add1_op == Op_AndI && 1147 phase->type(add1->in(2)) == TypeInt::make( bits_mask ) ) 1148 return new LShiftINode( add1->in(1), in(2) ); 1149 1150 // Performs: 1151 // (X << con1) << con2 ==> X << (con1 + con2) 1152 Node* doubleShift = collapse_nested_shift_left(phase, this, con, T_INT); 1153 if (doubleShift != nullptr) { 1154 return doubleShift; 1155 } 1156 1157 return nullptr; 1158 } 1159 1160 //------------------------------Value------------------------------------------ 1161 // A LShiftINode shifts its input2 left by input1 amount. 1162 const Type* LShiftINode::Value(PhaseGVN* phase) const { 1163 const Type *t1 = phase->type( in(1) ); 1164 const Type *t2 = phase->type( in(2) ); 1165 // Either input is TOP ==> the result is TOP 1166 if( t1 == Type::TOP ) return Type::TOP; 1167 if( t2 == Type::TOP ) return Type::TOP; 1168 1169 // Left input is ZERO ==> the result is ZERO. 1170 if( t1 == TypeInt::ZERO ) return TypeInt::ZERO; 1171 // Shift by zero does nothing 1172 if( t2 == TypeInt::ZERO ) return t1; 1173 1174 // Either input is BOTTOM ==> the result is BOTTOM 1175 if( (t1 == TypeInt::INT) || (t2 == TypeInt::INT) || 1176 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) 1177 return TypeInt::INT; 1178 1179 const TypeInt *r1 = t1->is_int(); // Handy access 1180 const TypeInt *r2 = t2->is_int(); // Handy access 1181 1182 if (!r2->is_con()) 1183 return TypeInt::INT; 1184 1185 uint shift = r2->get_con(); 1186 shift &= BitsPerJavaInteger-1; // semantics of Java shifts 1187 // Shift by a multiple of 32 does nothing: 1188 if (shift == 0) return t1; 1189 1190 // If the shift is a constant, shift the bounds of the type, 1191 // unless this could lead to an overflow. 1192 if (!r1->is_con()) { 1193 jint lo = r1->_lo, hi = r1->_hi; 1194 if (((lo << shift) >> shift) == lo && 1195 ((hi << shift) >> shift) == hi) { 1196 // No overflow. The range shifts up cleanly. 1197 return TypeInt::make((jint)lo << (jint)shift, 1198 (jint)hi << (jint)shift, 1199 MAX2(r1->_widen,r2->_widen)); 1200 } 1201 return TypeInt::INT; 1202 } 1203 1204 return TypeInt::make( (jint)r1->get_con() << (jint)shift ); 1205 } 1206 1207 //============================================================================= 1208 //------------------------------Identity--------------------------------------- 1209 Node* LShiftLNode::Identity(PhaseGVN* phase) { 1210 int count = 0; 1211 if (const_shift_count(phase, this, &count) && (count & (BitsPerJavaLong - 1)) == 0) { 1212 // Shift by a multiple of 64 does nothing 1213 return in(1); 1214 } 1215 return this; 1216 } 1217 1218 //------------------------------Ideal------------------------------------------ 1219 // If the right input is a constant, and the left input is an add of a 1220 // constant, flatten the tree: (X+con1)<<con0 ==> X<<con0 + con1<<con0 1221 // 1222 // Also collapse nested left-shifts with constant rhs: 1223 // (X << con1) << con2 ==> X << (con1 + con2) 1224 Node *LShiftLNode::Ideal(PhaseGVN *phase, bool can_reshape) { 1225 int con = maskShiftAmount(phase, this, BitsPerJavaLong); 1226 if (con == 0) { 1227 return nullptr; 1228 } 1229 1230 // Left input is an add? 1231 Node *add1 = in(1); 1232 int add1_op = add1->Opcode(); 1233 if( add1_op == Op_AddL ) { // Left input is an add? 1234 // Avoid dead data cycles from dead loops 1235 assert( add1 != add1->in(1), "dead loop in LShiftLNode::Ideal" ); 1236 1237 // Left input is an add of the same number? 1238 if (con != (BitsPerJavaLong - 1) && add1->in(1) == add1->in(2)) { 1239 // Convert "(x + x) << c0" into "x << (c0 + 1)" 1240 // Can only be applied if c0 != 63 because: 1241 // (x + x) << 63 = 2x << 63, while 1242 // (x + x) << 63 --transform--> x << 64 = x << 0 = x (!= 2x << 63, for example for x = 1) 1243 // According to the Java spec, chapter 15.19, we only consider the six lowest-order bits of the right-hand operand 1244 // (i.e. "right-hand operand" & 0b111111). Therefore, x << 64 is the same as x << 0 (64 = 0b10000000 & 0b0111111 = 0). 1245 return new LShiftLNode(add1->in(1), phase->intcon(con + 1)); 1246 } 1247 1248 // Left input is an add of a constant? 1249 const TypeLong *t12 = phase->type(add1->in(2))->isa_long(); 1250 if( t12 && t12->is_con() ){ // Left input is an add of a con? 1251 // Compute X << con0 1252 Node *lsh = phase->transform( new LShiftLNode( add1->in(1), in(2) ) ); 1253 // Compute X<<con0 + (con1<<con0) 1254 return new AddLNode( lsh, phase->longcon(t12->get_con() << con)); 1255 } 1256 } 1257 1258 // Check for "(x >> C1) << C2" 1259 if (add1_op == Op_RShiftL || add1_op == Op_URShiftL) { 1260 int add1Con = 0; 1261 const_shift_count(phase, add1, &add1Con); 1262 1263 // Special case C1 == C2, which just masks off low bits 1264 if (add1Con > 0 && con == add1Con) { 1265 // Convert to "(x & -(1 << C2))" 1266 return new AndLNode(add1->in(1), phase->longcon(java_negate(jlong(CONST64(1) << con)))); 1267 } else { 1268 // Wait until the right shift has been sharpened to the correct count 1269 if (add1Con > 0 && add1Con < BitsPerJavaLong) { 1270 // As loop parsing can produce LShiftI nodes, we should wait until the graph is fully formed 1271 // to apply optimizations, otherwise we can inadvertently stop vectorization opportunities. 1272 if (phase->is_IterGVN()) { 1273 if (con > add1Con) { 1274 // Creates "(x << (C2 - C1)) & -(1 << C2)" 1275 Node* lshift = phase->transform(new LShiftLNode(add1->in(1), phase->intcon(con - add1Con))); 1276 return new AndLNode(lshift, phase->longcon(java_negate(jlong(CONST64(1) << con)))); 1277 } else { 1278 assert(con < add1Con, "must be (%d < %d)", con, add1Con); 1279 // Creates "(x >> (C1 - C2)) & -(1 << C2)" 1280 1281 // Handle logical and arithmetic shifts 1282 Node* rshift; 1283 if (add1_op == Op_RShiftL) { 1284 rshift = phase->transform(new RShiftLNode(add1->in(1), phase->intcon(add1Con - con))); 1285 } else { 1286 rshift = phase->transform(new URShiftLNode(add1->in(1), phase->intcon(add1Con - con))); 1287 } 1288 1289 return new AndLNode(rshift, phase->longcon(java_negate(jlong(CONST64(1) << con)))); 1290 } 1291 } else { 1292 phase->record_for_igvn(this); 1293 } 1294 } 1295 } 1296 } 1297 1298 // Check for "((x >> C1) & Y) << C2" 1299 if (add1_op == Op_AndL) { 1300 Node* add2 = add1->in(1); 1301 int add2_op = add2->Opcode(); 1302 if (add2_op == Op_RShiftL || add2_op == Op_URShiftL) { 1303 // Special case C1 == C2, which just masks off low bits 1304 if (add2->in(2) == in(2)) { 1305 // Convert to "(x & (Y << C2))" 1306 Node* y_sh = phase->transform(new LShiftLNode(add1->in(2), phase->intcon(con))); 1307 return new AndLNode(add2->in(1), y_sh); 1308 } 1309 1310 int add2Con = 0; 1311 const_shift_count(phase, add2, &add2Con); 1312 if (add2Con > 0 && add2Con < BitsPerJavaLong) { 1313 if (phase->is_IterGVN()) { 1314 // Convert to "((x >> C1) << C2) & (Y << C2)" 1315 1316 // Make "(x >> C1) << C2", which will get folded away by the rule above 1317 Node* x_sh = phase->transform(new LShiftLNode(add2, phase->intcon(con))); 1318 // Make "Y << C2", which will simplify when Y is a constant 1319 Node* y_sh = phase->transform(new LShiftLNode(add1->in(2), phase->intcon(con))); 1320 1321 return new AndLNode(x_sh, y_sh); 1322 } else { 1323 phase->record_for_igvn(this); 1324 } 1325 } 1326 } 1327 } 1328 1329 // Check for ((x & ((CONST64(1)<<(64-c0))-1)) << c0) which ANDs off high bits 1330 // before shifting them away. 1331 const jlong bits_mask = jlong(max_julong >> con); 1332 if( add1_op == Op_AndL && 1333 phase->type(add1->in(2)) == TypeLong::make( bits_mask ) ) 1334 return new LShiftLNode( add1->in(1), in(2) ); 1335 1336 // Performs: 1337 // (X << con1) << con2 ==> X << (con1 + con2) 1338 Node* doubleShift = collapse_nested_shift_left(phase, this, con, T_LONG); 1339 if (doubleShift != nullptr) { 1340 return doubleShift; 1341 } 1342 1343 return nullptr; 1344 } 1345 1346 //------------------------------Value------------------------------------------ 1347 // A LShiftLNode shifts its input2 left by input1 amount. 1348 const Type* LShiftLNode::Value(PhaseGVN* phase) const { 1349 const Type *t1 = phase->type( in(1) ); 1350 const Type *t2 = phase->type( in(2) ); 1351 // Either input is TOP ==> the result is TOP 1352 if( t1 == Type::TOP ) return Type::TOP; 1353 if( t2 == Type::TOP ) return Type::TOP; 1354 1355 // Left input is ZERO ==> the result is ZERO. 1356 if( t1 == TypeLong::ZERO ) return TypeLong::ZERO; 1357 // Shift by zero does nothing 1358 if( t2 == TypeInt::ZERO ) return t1; 1359 1360 // Either input is BOTTOM ==> the result is BOTTOM 1361 if( (t1 == TypeLong::LONG) || (t2 == TypeInt::INT) || 1362 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) 1363 return TypeLong::LONG; 1364 1365 const TypeLong *r1 = t1->is_long(); // Handy access 1366 const TypeInt *r2 = t2->is_int(); // Handy access 1367 1368 if (!r2->is_con()) 1369 return TypeLong::LONG; 1370 1371 uint shift = r2->get_con(); 1372 shift &= BitsPerJavaLong - 1; // semantics of Java shifts 1373 // Shift by a multiple of 64 does nothing: 1374 if (shift == 0) return t1; 1375 1376 // If the shift is a constant, shift the bounds of the type, 1377 // unless this could lead to an overflow. 1378 if (!r1->is_con()) { 1379 jlong lo = r1->_lo, hi = r1->_hi; 1380 if (((lo << shift) >> shift) == lo && 1381 ((hi << shift) >> shift) == hi) { 1382 // No overflow. The range shifts up cleanly. 1383 return TypeLong::make((jlong)lo << (jint)shift, 1384 (jlong)hi << (jint)shift, 1385 MAX2(r1->_widen,r2->_widen)); 1386 } 1387 return TypeLong::LONG; 1388 } 1389 1390 return TypeLong::make( (jlong)r1->get_con() << (jint)shift ); 1391 } 1392 1393 RShiftNode* RShiftNode::make(Node* in1, Node* in2, BasicType bt) { 1394 switch (bt) { 1395 case T_INT: 1396 return new RShiftINode(in1, in2); 1397 case T_LONG: 1398 return new RShiftLNode(in1, in2); 1399 default: 1400 fatal("Not implemented for %s", type2name(bt)); 1401 } 1402 return nullptr; 1403 } 1404 1405 1406 //============================================================================= 1407 //------------------------------Identity--------------------------------------- 1408 Node* RShiftNode::IdentityIL(PhaseGVN* phase, BasicType bt) { 1409 int count = 0; 1410 if (const_shift_count(phase, this, &count)) { 1411 if ((count & (bits_per_java_integer(bt) - 1)) == 0) { 1412 // Shift by a multiple of 32/64 does nothing 1413 return in(1); 1414 } 1415 // Check for useless sign-masking 1416 if (in(1)->Opcode() == Op_LShift(bt) && 1417 in(1)->req() == 3 && 1418 in(1)->in(2) == in(2)) { 1419 count &= bits_per_java_integer(bt) - 1; // semantics of Java shifts 1420 // Compute masks for which this shifting doesn't change 1421 jlong lo = (CONST64(-1) << (bits_per_java_integer(bt) - ((uint)count)-1)); // FFFF8000 1422 jlong hi = ~lo; // 00007FFF 1423 const TypeInteger* t11 = phase->type(in(1)->in(1))->isa_integer(bt); 1424 if (t11 == nullptr) { 1425 return this; 1426 } 1427 // Does actual value fit inside of mask? 1428 if (lo <= t11->lo_as_long() && t11->hi_as_long() <= hi) { 1429 return in(1)->in(1); // Then shifting is a nop 1430 } 1431 } 1432 } 1433 return this; 1434 } 1435 1436 Node* RShiftINode::Identity(PhaseGVN* phase) { 1437 return IdentityIL(phase, T_INT); 1438 } 1439 1440 Node* RShiftNode::IdealIL(PhaseGVN* phase, bool can_reshape, BasicType bt) { 1441 // Inputs may be TOP if they are dead. 1442 const TypeInteger* t1 = phase->type(in(1))->isa_integer(bt); 1443 if (t1 == nullptr) { 1444 return NodeSentinel; // Left input is an integer 1445 } 1446 int shift = maskShiftAmount(phase, this, bits_per_java_integer(bt)); 1447 if (shift == 0) { 1448 return NodeSentinel; 1449 } 1450 1451 // Check for (x & 0xFF000000) >> 24, whose mask can be made smaller. 1452 // and convert to (x >> 24) & (0xFF000000 >> 24) = x >> 24 1453 // Such expressions arise normally from shift chains like (byte)(x >> 24). 1454 const Node* and_node = in(1); 1455 if (and_node->Opcode() != Op_And(bt)) { 1456 return nullptr; 1457 } 1458 const TypeInteger* mask_t = phase->type(and_node->in(2))->isa_integer(bt); 1459 if (mask_t != nullptr && mask_t->is_con()) { 1460 jlong maskbits = mask_t->get_con_as_long(bt); 1461 // Convert to "(x >> shift) & (mask >> shift)" 1462 Node* shr_nomask = phase->transform(RShiftNode::make(and_node->in(1), in(2), bt)); 1463 return MulNode::make_and(shr_nomask, phase->integercon(maskbits >> shift, bt), bt); 1464 } 1465 return nullptr; 1466 } 1467 1468 Node* RShiftINode::Ideal(PhaseGVN* phase, bool can_reshape) { 1469 Node* progress = IdealIL(phase, can_reshape, T_INT); 1470 if (progress == NodeSentinel) { 1471 return nullptr; 1472 } 1473 if (progress != nullptr) { 1474 return progress; 1475 } 1476 int shift = maskShiftAmount(phase, this, BitsPerJavaInteger); 1477 assert(shift != 0, "handled by IdealIL"); 1478 1479 // Check for "(short[i] <<16)>>16" which simply sign-extends 1480 const Node *shl = in(1); 1481 if (shl->Opcode() != Op_LShiftI) { 1482 return nullptr; 1483 } 1484 1485 const TypeInt* left_shift_t = phase->type(shl->in(2))->isa_int(); 1486 if (left_shift_t == nullptr) { 1487 return nullptr; 1488 } 1489 if (shift == 16 && left_shift_t->is_con(16)) { 1490 Node *ld = shl->in(1); 1491 if (ld->Opcode() == Op_LoadS) { 1492 // Sign extension is just useless here. Return a RShiftI of zero instead 1493 // returning 'ld' directly. We cannot return an old Node directly as 1494 // that is the job of 'Identity' calls and Identity calls only work on 1495 // direct inputs ('ld' is an extra Node removed from 'this'). The 1496 // combined optimization requires Identity only return direct inputs. 1497 set_req_X(1, ld, phase); 1498 set_req_X(2, phase->intcon(0), phase); 1499 return this; 1500 } 1501 else if (can_reshape && 1502 ld->Opcode() == Op_LoadUS && 1503 ld->outcnt() == 1 && ld->unique_out() == shl) 1504 // Replace zero-extension-load with sign-extension-load 1505 return ld->as_Load()->convert_to_signed_load(*phase); 1506 } 1507 1508 // Check for "(byte[i] <<24)>>24" which simply sign-extends 1509 if (shift == 24 && left_shift_t->is_con(24)) { 1510 Node *ld = shl->in(1); 1511 if (ld->Opcode() == Op_LoadB) { 1512 // Sign extension is just useless here 1513 set_req_X(1, ld, phase); 1514 set_req_X(2, phase->intcon(0), phase); 1515 return this; 1516 } 1517 } 1518 1519 return nullptr; 1520 } 1521 1522 const Type* RShiftNode::ValueIL(PhaseGVN* phase, BasicType bt) const { 1523 const Type* t1 = phase->type(in(1)); 1524 const Type* t2 = phase->type(in(2)); 1525 // Either input is TOP ==> the result is TOP 1526 if (t1 == Type::TOP) { 1527 return Type::TOP; 1528 } 1529 if (t2 == Type::TOP) { 1530 return Type::TOP; 1531 } 1532 1533 // Left input is ZERO ==> the result is ZERO. 1534 if (t1 == TypeInteger::zero(bt)) { 1535 return TypeInteger::zero(bt); 1536 } 1537 // Shift by zero does nothing 1538 if (t2 == TypeInt::ZERO) { 1539 return t1; 1540 } 1541 1542 // Either input is BOTTOM ==> the result is BOTTOM 1543 if (t1 == Type::BOTTOM || t2 == Type::BOTTOM) { 1544 return TypeInteger::bottom(bt); 1545 } 1546 1547 const TypeInteger* r1 = t1->isa_integer(bt); 1548 const TypeInt* r2 = t2->isa_int(); 1549 1550 // If the shift is a constant, just shift the bounds of the type. 1551 // For example, if the shift is 31/63, we just propagate sign bits. 1552 if (!r1->is_con() && r2->is_con()) { 1553 uint shift = r2->get_con(); 1554 shift &= bits_per_java_integer(bt) - 1; // semantics of Java shifts 1555 // Shift by a multiple of 32/64 does nothing: 1556 if (shift == 0) { 1557 return t1; 1558 } 1559 // Calculate reasonably aggressive bounds for the result. 1560 // This is necessary if we are to correctly type things 1561 // like (x<<24>>24) == ((byte)x). 1562 jlong lo = r1->lo_as_long() >> (jint)shift; 1563 jlong hi = r1->hi_as_long() >> (jint)shift; 1564 assert(lo <= hi, "must have valid bounds"); 1565 #ifdef ASSERT 1566 if (bt == T_INT) { 1567 jint lo_verify = checked_cast<jint>(r1->lo_as_long()) >> (jint)shift; 1568 jint hi_verify = checked_cast<jint>(r1->hi_as_long()) >> (jint)shift; 1569 assert((checked_cast<jint>(lo) == lo_verify) && (checked_cast<jint>(hi) == hi_verify), "inconsistent"); 1570 } 1571 #endif 1572 const TypeInteger* ti = TypeInteger::make(lo, hi, MAX2(r1->_widen,r2->_widen), bt); 1573 #ifdef ASSERT 1574 // Make sure we get the sign-capture idiom correct. 1575 if (shift == bits_per_java_integer(bt) - 1) { 1576 if (r1->lo_as_long() >= 0) { 1577 assert(ti == TypeInteger::zero(bt), ">>31/63 of + is 0"); 1578 } 1579 if (r1->hi_as_long() < 0) { 1580 assert(ti == TypeInteger::minus_1(bt), ">>31/63 of - is -1"); 1581 } 1582 } 1583 #endif 1584 return ti; 1585 } 1586 1587 if (!r1->is_con() || !r2->is_con()) { 1588 // If the left input is non-negative the result must also be non-negative, regardless of what the right input is. 1589 if (r1->lo_as_long() >= 0) { 1590 return TypeInteger::make(0, r1->hi_as_long(), MAX2(r1->_widen, r2->_widen), bt); 1591 } 1592 1593 // Conversely, if the left input is negative then the result must be negative. 1594 if (r1->hi_as_long() <= -1) { 1595 return TypeInteger::make(r1->lo_as_long(), -1, MAX2(r1->_widen, r2->_widen), bt); 1596 } 1597 1598 return TypeInteger::bottom(bt); 1599 } 1600 1601 // Signed shift right 1602 return TypeInteger::make(r1->get_con_as_long(bt) >> (r2->get_con() & (bits_per_java_integer(bt) - 1)), bt); 1603 } 1604 1605 const Type* RShiftINode::Value(PhaseGVN* phase) const { 1606 return ValueIL(phase, T_INT); 1607 } 1608 1609 //============================================================================= 1610 //------------------------------Identity--------------------------------------- 1611 Node* RShiftLNode::Identity(PhaseGVN* phase) { 1612 return IdentityIL(phase, T_LONG); 1613 } 1614 1615 Node* RShiftLNode::Ideal(PhaseGVN *phase, bool can_reshape) { 1616 Node* progress = IdealIL(phase, can_reshape, T_LONG); 1617 if (progress == NodeSentinel) { 1618 return nullptr; 1619 } 1620 return progress; 1621 } 1622 1623 const Type* RShiftLNode::Value(PhaseGVN* phase) const { 1624 return ValueIL(phase, T_LONG); 1625 } 1626 1627 //============================================================================= 1628 //------------------------------Identity--------------------------------------- 1629 Node* URShiftINode::Identity(PhaseGVN* phase) { 1630 int count = 0; 1631 if (const_shift_count(phase, this, &count) && (count & (BitsPerJavaInteger - 1)) == 0) { 1632 // Shift by a multiple of 32 does nothing 1633 return in(1); 1634 } 1635 1636 // Check for "((x << LogBytesPerWord) + (wordSize-1)) >> LogBytesPerWord" which is just "x". 1637 // Happens during new-array length computation. 1638 // Safe if 'x' is in the range [0..(max_int>>LogBytesPerWord)] 1639 Node *add = in(1); 1640 if (add->Opcode() == Op_AddI) { 1641 const TypeInt *t2 = phase->type(add->in(2))->isa_int(); 1642 if (t2 && t2->is_con(wordSize - 1) && 1643 add->in(1)->Opcode() == Op_LShiftI) { 1644 // Check that shift_counts are LogBytesPerWord. 1645 Node *lshift_count = add->in(1)->in(2); 1646 const TypeInt *t_lshift_count = phase->type(lshift_count)->isa_int(); 1647 if (t_lshift_count && t_lshift_count->is_con(LogBytesPerWord) && 1648 t_lshift_count == phase->type(in(2))) { 1649 Node *x = add->in(1)->in(1); 1650 const TypeInt *t_x = phase->type(x)->isa_int(); 1651 if (t_x != nullptr && 0 <= t_x->_lo && t_x->_hi <= (max_jint>>LogBytesPerWord)) { 1652 return x; 1653 } 1654 } 1655 } 1656 } 1657 1658 return (phase->type(in(2))->higher_equal(TypeInt::ZERO)) ? in(1) : this; 1659 } 1660 1661 //------------------------------Ideal------------------------------------------ 1662 Node *URShiftINode::Ideal(PhaseGVN *phase, bool can_reshape) { 1663 int con = maskShiftAmount(phase, this, BitsPerJavaInteger); 1664 if (con == 0) { 1665 return nullptr; 1666 } 1667 1668 // We'll be wanting the right-shift amount as a mask of that many bits 1669 const int mask = right_n_bits(BitsPerJavaInteger - con); 1670 1671 int in1_op = in(1)->Opcode(); 1672 1673 // Check for ((x>>>a)>>>b) and replace with (x>>>(a+b)) when a+b < 32 1674 if( in1_op == Op_URShiftI ) { 1675 const TypeInt *t12 = phase->type( in(1)->in(2) )->isa_int(); 1676 if( t12 && t12->is_con() ) { // Right input is a constant 1677 assert( in(1) != in(1)->in(1), "dead loop in URShiftINode::Ideal" ); 1678 const int con2 = t12->get_con() & 31; // Shift count is always masked 1679 const int con3 = con+con2; 1680 if( con3 < 32 ) // Only merge shifts if total is < 32 1681 return new URShiftINode( in(1)->in(1), phase->intcon(con3) ); 1682 } 1683 } 1684 1685 // Check for ((x << z) + Y) >>> z. Replace with x + con>>>z 1686 // The idiom for rounding to a power of 2 is "(Q+(2^z-1)) >>> z". 1687 // If Q is "X << z" the rounding is useless. Look for patterns like 1688 // ((X<<Z) + Y) >>> Z and replace with (X + Y>>>Z) & Z-mask. 1689 Node *add = in(1); 1690 const TypeInt *t2 = phase->type(in(2))->isa_int(); 1691 if (in1_op == Op_AddI) { 1692 Node *lshl = add->in(1); 1693 if( lshl->Opcode() == Op_LShiftI && 1694 phase->type(lshl->in(2)) == t2 ) { 1695 Node *y_z = phase->transform( new URShiftINode(add->in(2),in(2)) ); 1696 Node *sum = phase->transform( new AddINode( lshl->in(1), y_z ) ); 1697 return new AndINode( sum, phase->intcon(mask) ); 1698 } 1699 } 1700 1701 // Check for (x & mask) >>> z. Replace with (x >>> z) & (mask >>> z) 1702 // This shortens the mask. Also, if we are extracting a high byte and 1703 // storing it to a buffer, the mask will be removed completely. 1704 Node *andi = in(1); 1705 if( in1_op == Op_AndI ) { 1706 const TypeInt *t3 = phase->type( andi->in(2) )->isa_int(); 1707 if( t3 && t3->is_con() ) { // Right input is a constant 1708 jint mask2 = t3->get_con(); 1709 mask2 >>= con; // *signed* shift downward (high-order zeroes do not help) 1710 Node *newshr = phase->transform( new URShiftINode(andi->in(1), in(2)) ); 1711 return new AndINode(newshr, phase->intcon(mask2)); 1712 // The negative values are easier to materialize than positive ones. 1713 // A typical case from address arithmetic is ((x & ~15) >> 4). 1714 // It's better to change that to ((x >> 4) & ~0) versus 1715 // ((x >> 4) & 0x0FFFFFFF). The difference is greatest in LP64. 1716 } 1717 } 1718 1719 // Check for "(X << z ) >>> z" which simply zero-extends 1720 Node *shl = in(1); 1721 if( in1_op == Op_LShiftI && 1722 phase->type(shl->in(2)) == t2 ) 1723 return new AndINode( shl->in(1), phase->intcon(mask) ); 1724 1725 // Check for (x >> n) >>> 31. Replace with (x >>> 31) 1726 Node *shr = in(1); 1727 if ( in1_op == Op_RShiftI ) { 1728 Node *in11 = shr->in(1); 1729 Node *in12 = shr->in(2); 1730 const TypeInt *t11 = phase->type(in11)->isa_int(); 1731 const TypeInt *t12 = phase->type(in12)->isa_int(); 1732 if ( t11 && t2 && t2->is_con(31) && t12 && t12->is_con() ) { 1733 return new URShiftINode(in11, phase->intcon(31)); 1734 } 1735 } 1736 1737 return nullptr; 1738 } 1739 1740 //------------------------------Value------------------------------------------ 1741 // A URShiftINode shifts its input2 right by input1 amount. 1742 const Type* URShiftINode::Value(PhaseGVN* phase) const { 1743 // (This is a near clone of RShiftINode::Value.) 1744 const Type *t1 = phase->type( in(1) ); 1745 const Type *t2 = phase->type( in(2) ); 1746 // Either input is TOP ==> the result is TOP 1747 if( t1 == Type::TOP ) return Type::TOP; 1748 if( t2 == Type::TOP ) return Type::TOP; 1749 1750 // Left input is ZERO ==> the result is ZERO. 1751 if( t1 == TypeInt::ZERO ) return TypeInt::ZERO; 1752 // Shift by zero does nothing 1753 if( t2 == TypeInt::ZERO ) return t1; 1754 1755 // Either input is BOTTOM ==> the result is BOTTOM 1756 if (t1 == Type::BOTTOM || t2 == Type::BOTTOM) 1757 return TypeInt::INT; 1758 1759 if (t2 == TypeInt::INT) 1760 return TypeInt::INT; 1761 1762 const TypeInt *r1 = t1->is_int(); // Handy access 1763 const TypeInt *r2 = t2->is_int(); // Handy access 1764 1765 if (r2->is_con()) { 1766 uint shift = r2->get_con(); 1767 shift &= BitsPerJavaInteger-1; // semantics of Java shifts 1768 // Shift by a multiple of 32 does nothing: 1769 if (shift == 0) return t1; 1770 // Calculate reasonably aggressive bounds for the result. 1771 jint lo = (juint)r1->_lo >> (juint)shift; 1772 jint hi = (juint)r1->_hi >> (juint)shift; 1773 if (r1->_hi >= 0 && r1->_lo < 0) { 1774 // If the type has both negative and positive values, 1775 // there are two separate sub-domains to worry about: 1776 // The positive half and the negative half. 1777 jint neg_lo = lo; 1778 jint neg_hi = (juint)-1 >> (juint)shift; 1779 jint pos_lo = (juint) 0 >> (juint)shift; 1780 jint pos_hi = hi; 1781 lo = MIN2(neg_lo, pos_lo); // == 0 1782 hi = MAX2(neg_hi, pos_hi); // == -1 >>> shift; 1783 } 1784 assert(lo <= hi, "must have valid bounds"); 1785 const TypeInt* ti = TypeInt::make(lo, hi, MAX2(r1->_widen,r2->_widen)); 1786 #ifdef ASSERT 1787 // Make sure we get the sign-capture idiom correct. 1788 if (shift == BitsPerJavaInteger-1) { 1789 if (r1->_lo >= 0) assert(ti == TypeInt::ZERO, ">>>31 of + is 0"); 1790 if (r1->_hi < 0) assert(ti == TypeInt::ONE, ">>>31 of - is +1"); 1791 } 1792 #endif 1793 return ti; 1794 } 1795 1796 // 1797 // Do not support shifted oops in info for GC 1798 // 1799 // else if( t1->base() == Type::InstPtr ) { 1800 // 1801 // const TypeInstPtr *o = t1->is_instptr(); 1802 // if( t1->singleton() ) 1803 // return TypeInt::make( ((uint32_t)o->const_oop() + o->_offset) >> shift ); 1804 // } 1805 // else if( t1->base() == Type::KlassPtr ) { 1806 // const TypeKlassPtr *o = t1->is_klassptr(); 1807 // if( t1->singleton() ) 1808 // return TypeInt::make( ((uint32_t)o->const_oop() + o->_offset) >> shift ); 1809 // } 1810 1811 return TypeInt::INT; 1812 } 1813 1814 //============================================================================= 1815 //------------------------------Identity--------------------------------------- 1816 Node* URShiftLNode::Identity(PhaseGVN* phase) { 1817 int count = 0; 1818 if (const_shift_count(phase, this, &count) && (count & (BitsPerJavaLong - 1)) == 0) { 1819 // Shift by a multiple of 64 does nothing 1820 return in(1); 1821 } 1822 return this; 1823 } 1824 1825 //------------------------------Ideal------------------------------------------ 1826 Node *URShiftLNode::Ideal(PhaseGVN *phase, bool can_reshape) { 1827 int con = maskShiftAmount(phase, this, BitsPerJavaLong); 1828 if (con == 0) { 1829 return nullptr; 1830 } 1831 1832 // We'll be wanting the right-shift amount as a mask of that many bits 1833 const jlong mask = jlong(max_julong >> con); 1834 1835 // Check for ((x << z) + Y) >>> z. Replace with x + con>>>z 1836 // The idiom for rounding to a power of 2 is "(Q+(2^z-1)) >>> z". 1837 // If Q is "X << z" the rounding is useless. Look for patterns like 1838 // ((X<<Z) + Y) >>> Z and replace with (X + Y>>>Z) & Z-mask. 1839 Node *add = in(1); 1840 const TypeInt *t2 = phase->type(in(2))->isa_int(); 1841 if (add->Opcode() == Op_AddL) { 1842 Node *lshl = add->in(1); 1843 if( lshl->Opcode() == Op_LShiftL && 1844 phase->type(lshl->in(2)) == t2 ) { 1845 Node *y_z = phase->transform( new URShiftLNode(add->in(2),in(2)) ); 1846 Node *sum = phase->transform( new AddLNode( lshl->in(1), y_z ) ); 1847 return new AndLNode( sum, phase->longcon(mask) ); 1848 } 1849 } 1850 1851 // Check for (x & mask) >>> z. Replace with (x >>> z) & (mask >>> z) 1852 // This shortens the mask. Also, if we are extracting a high byte and 1853 // storing it to a buffer, the mask will be removed completely. 1854 Node *andi = in(1); 1855 if( andi->Opcode() == Op_AndL ) { 1856 const TypeLong *t3 = phase->type( andi->in(2) )->isa_long(); 1857 if( t3 && t3->is_con() ) { // Right input is a constant 1858 jlong mask2 = t3->get_con(); 1859 mask2 >>= con; // *signed* shift downward (high-order zeroes do not help) 1860 Node *newshr = phase->transform( new URShiftLNode(andi->in(1), in(2)) ); 1861 return new AndLNode(newshr, phase->longcon(mask2)); 1862 } 1863 } 1864 1865 // Check for "(X << z ) >>> z" which simply zero-extends 1866 Node *shl = in(1); 1867 if( shl->Opcode() == Op_LShiftL && 1868 phase->type(shl->in(2)) == t2 ) 1869 return new AndLNode( shl->in(1), phase->longcon(mask) ); 1870 1871 // Check for (x >> n) >>> 63. Replace with (x >>> 63) 1872 Node *shr = in(1); 1873 if ( shr->Opcode() == Op_RShiftL ) { 1874 Node *in11 = shr->in(1); 1875 Node *in12 = shr->in(2); 1876 const TypeLong *t11 = phase->type(in11)->isa_long(); 1877 const TypeInt *t12 = phase->type(in12)->isa_int(); 1878 if ( t11 && t2 && t2->is_con(63) && t12 && t12->is_con() ) { 1879 return new URShiftLNode(in11, phase->intcon(63)); 1880 } 1881 } 1882 return nullptr; 1883 } 1884 1885 //------------------------------Value------------------------------------------ 1886 // A URShiftINode shifts its input2 right by input1 amount. 1887 const Type* URShiftLNode::Value(PhaseGVN* phase) const { 1888 // (This is a near clone of RShiftLNode::Value.) 1889 const Type *t1 = phase->type( in(1) ); 1890 const Type *t2 = phase->type( in(2) ); 1891 // Either input is TOP ==> the result is TOP 1892 if( t1 == Type::TOP ) return Type::TOP; 1893 if( t2 == Type::TOP ) return Type::TOP; 1894 1895 // Left input is ZERO ==> the result is ZERO. 1896 if( t1 == TypeLong::ZERO ) return TypeLong::ZERO; 1897 // Shift by zero does nothing 1898 if( t2 == TypeInt::ZERO ) return t1; 1899 1900 // Either input is BOTTOM ==> the result is BOTTOM 1901 if (t1 == Type::BOTTOM || t2 == Type::BOTTOM) 1902 return TypeLong::LONG; 1903 1904 if (t2 == TypeInt::INT) 1905 return TypeLong::LONG; 1906 1907 const TypeLong *r1 = t1->is_long(); // Handy access 1908 const TypeInt *r2 = t2->is_int (); // Handy access 1909 1910 if (r2->is_con()) { 1911 uint shift = r2->get_con(); 1912 shift &= BitsPerJavaLong - 1; // semantics of Java shifts 1913 // Shift by a multiple of 64 does nothing: 1914 if (shift == 0) return t1; 1915 // Calculate reasonably aggressive bounds for the result. 1916 jlong lo = (julong)r1->_lo >> (juint)shift; 1917 jlong hi = (julong)r1->_hi >> (juint)shift; 1918 if (r1->_hi >= 0 && r1->_lo < 0) { 1919 // If the type has both negative and positive values, 1920 // there are two separate sub-domains to worry about: 1921 // The positive half and the negative half. 1922 jlong neg_lo = lo; 1923 jlong neg_hi = (julong)-1 >> (juint)shift; 1924 jlong pos_lo = (julong) 0 >> (juint)shift; 1925 jlong pos_hi = hi; 1926 //lo = MIN2(neg_lo, pos_lo); // == 0 1927 lo = neg_lo < pos_lo ? neg_lo : pos_lo; 1928 //hi = MAX2(neg_hi, pos_hi); // == -1 >>> shift; 1929 hi = neg_hi > pos_hi ? neg_hi : pos_hi; 1930 } 1931 assert(lo <= hi, "must have valid bounds"); 1932 const TypeLong* tl = TypeLong::make(lo, hi, MAX2(r1->_widen,r2->_widen)); 1933 #ifdef ASSERT 1934 // Make sure we get the sign-capture idiom correct. 1935 if (shift == BitsPerJavaLong - 1) { 1936 if (r1->_lo >= 0) assert(tl == TypeLong::ZERO, ">>>63 of + is 0"); 1937 if (r1->_hi < 0) assert(tl == TypeLong::ONE, ">>>63 of - is +1"); 1938 } 1939 #endif 1940 return tl; 1941 } 1942 1943 return TypeLong::LONG; // Give up 1944 } 1945 1946 //============================================================================= 1947 //------------------------------Ideal------------------------------------------ 1948 Node* FmaNode::Ideal(PhaseGVN* phase, bool can_reshape) { 1949 // We canonicalize the node by converting "(-a)*b+c" into "b*(-a)+c" 1950 // This reduces the number of rules in the matcher, as we only need to check 1951 // for negations on the second argument, and not the symmetric case where 1952 // the first argument is negated. 1953 if (in(1)->is_Neg() && !in(2)->is_Neg()) { 1954 swap_edges(1, 2); 1955 return this; 1956 } 1957 return nullptr; 1958 } 1959 1960 //============================================================================= 1961 //------------------------------Value------------------------------------------ 1962 const Type* FmaDNode::Value(PhaseGVN* phase) const { 1963 const Type *t1 = phase->type(in(1)); 1964 if (t1 == Type::TOP) return Type::TOP; 1965 if (t1->base() != Type::DoubleCon) return Type::DOUBLE; 1966 const Type *t2 = phase->type(in(2)); 1967 if (t2 == Type::TOP) return Type::TOP; 1968 if (t2->base() != Type::DoubleCon) return Type::DOUBLE; 1969 const Type *t3 = phase->type(in(3)); 1970 if (t3 == Type::TOP) return Type::TOP; 1971 if (t3->base() != Type::DoubleCon) return Type::DOUBLE; 1972 #ifndef __STDC_IEC_559__ 1973 return Type::DOUBLE; 1974 #else 1975 double d1 = t1->getd(); 1976 double d2 = t2->getd(); 1977 double d3 = t3->getd(); 1978 return TypeD::make(fma(d1, d2, d3)); 1979 #endif 1980 } 1981 1982 //============================================================================= 1983 //------------------------------Value------------------------------------------ 1984 const Type* FmaFNode::Value(PhaseGVN* phase) const { 1985 const Type *t1 = phase->type(in(1)); 1986 if (t1 == Type::TOP) return Type::TOP; 1987 if (t1->base() != Type::FloatCon) return Type::FLOAT; 1988 const Type *t2 = phase->type(in(2)); 1989 if (t2 == Type::TOP) return Type::TOP; 1990 if (t2->base() != Type::FloatCon) return Type::FLOAT; 1991 const Type *t3 = phase->type(in(3)); 1992 if (t3 == Type::TOP) return Type::TOP; 1993 if (t3->base() != Type::FloatCon) return Type::FLOAT; 1994 #ifndef __STDC_IEC_559__ 1995 return Type::FLOAT; 1996 #else 1997 float f1 = t1->getf(); 1998 float f2 = t2->getf(); 1999 float f3 = t3->getf(); 2000 return TypeF::make(fma(f1, f2, f3)); 2001 #endif 2002 } 2003 2004 //============================================================================= 2005 //------------------------------Value------------------------------------------ 2006 const Type* FmaHFNode::Value(PhaseGVN* phase) const { 2007 const Type* t1 = phase->type(in(1)); 2008 if (t1 == Type::TOP) { return Type::TOP; } 2009 if (t1->base() != Type::HalfFloatCon) { return Type::HALF_FLOAT; } 2010 const Type* t2 = phase->type(in(2)); 2011 if (t2 == Type::TOP) { return Type::TOP; } 2012 if (t2->base() != Type::HalfFloatCon) { return Type::HALF_FLOAT; } 2013 const Type* t3 = phase->type(in(3)); 2014 if (t3 == Type::TOP) { return Type::TOP; } 2015 if (t3->base() != Type::HalfFloatCon) { return Type::HALF_FLOAT; } 2016 #ifndef __STDC_IEC_559__ 2017 return Type::HALF_FLOAT; 2018 #else 2019 float f1 = t1->getf(); 2020 float f2 = t2->getf(); 2021 float f3 = t3->getf(); 2022 return TypeH::make(fma(f1, f2, f3)); 2023 #endif 2024 } 2025 2026 //============================================================================= 2027 //------------------------------hash------------------------------------------- 2028 // Hash function for MulAddS2INode. Operation is commutative with commutative pairs. 2029 // The hash function must return the same value when edge swapping is performed. 2030 uint MulAddS2INode::hash() const { 2031 return (uintptr_t)in(1) + (uintptr_t)in(2) + (uintptr_t)in(3) + (uintptr_t)in(4) + Opcode(); 2032 } 2033 2034 //------------------------------Rotate Operations ------------------------------ 2035 2036 Node* RotateLeftNode::Identity(PhaseGVN* phase) { 2037 const Type* t1 = phase->type(in(1)); 2038 if (t1 == Type::TOP) { 2039 return this; 2040 } 2041 int count = 0; 2042 assert(t1->isa_int() || t1->isa_long(), "Unexpected type"); 2043 int mask = (t1->isa_int() ? BitsPerJavaInteger : BitsPerJavaLong) - 1; 2044 if (const_shift_count(phase, this, &count) && (count & mask) == 0) { 2045 // Rotate by a multiple of 32/64 does nothing 2046 return in(1); 2047 } 2048 return this; 2049 } 2050 2051 const Type* RotateLeftNode::Value(PhaseGVN* phase) const { 2052 const Type* t1 = phase->type(in(1)); 2053 const Type* t2 = phase->type(in(2)); 2054 // Either input is TOP ==> the result is TOP 2055 if (t1 == Type::TOP || t2 == Type::TOP) { 2056 return Type::TOP; 2057 } 2058 2059 if (t1->isa_int()) { 2060 const TypeInt* r1 = t1->is_int(); 2061 const TypeInt* r2 = t2->is_int(); 2062 2063 // Left input is ZERO ==> the result is ZERO. 2064 if (r1 == TypeInt::ZERO) { 2065 return TypeInt::ZERO; 2066 } 2067 // Rotate by zero does nothing 2068 if (r2 == TypeInt::ZERO) { 2069 return r1; 2070 } 2071 if (r1->is_con() && r2->is_con()) { 2072 juint r1_con = (juint)r1->get_con(); 2073 juint shift = (juint)(r2->get_con()) & (juint)(BitsPerJavaInteger - 1); // semantics of Java shifts 2074 return TypeInt::make((r1_con << shift) | (r1_con >> (32 - shift))); 2075 } 2076 return TypeInt::INT; 2077 } else { 2078 assert(t1->isa_long(), "Type must be a long"); 2079 const TypeLong* r1 = t1->is_long(); 2080 const TypeInt* r2 = t2->is_int(); 2081 2082 // Left input is ZERO ==> the result is ZERO. 2083 if (r1 == TypeLong::ZERO) { 2084 return TypeLong::ZERO; 2085 } 2086 // Rotate by zero does nothing 2087 if (r2 == TypeInt::ZERO) { 2088 return r1; 2089 } 2090 if (r1->is_con() && r2->is_con()) { 2091 julong r1_con = (julong)r1->get_con(); 2092 julong shift = (julong)(r2->get_con()) & (julong)(BitsPerJavaLong - 1); // semantics of Java shifts 2093 return TypeLong::make((r1_con << shift) | (r1_con >> (64 - shift))); 2094 } 2095 return TypeLong::LONG; 2096 } 2097 } 2098 2099 Node* RotateLeftNode::Ideal(PhaseGVN *phase, bool can_reshape) { 2100 const Type* t1 = phase->type(in(1)); 2101 const Type* t2 = phase->type(in(2)); 2102 if (t2->isa_int() && t2->is_int()->is_con()) { 2103 if (t1->isa_int()) { 2104 int lshift = t2->is_int()->get_con() & 31; 2105 return new RotateRightNode(in(1), phase->intcon(32 - (lshift & 31)), TypeInt::INT); 2106 } else if (t1 != Type::TOP) { 2107 assert(t1->isa_long(), "Type must be a long"); 2108 int lshift = t2->is_int()->get_con() & 63; 2109 return new RotateRightNode(in(1), phase->intcon(64 - (lshift & 63)), TypeLong::LONG); 2110 } 2111 } 2112 return nullptr; 2113 } 2114 2115 Node* RotateRightNode::Identity(PhaseGVN* phase) { 2116 const Type* t1 = phase->type(in(1)); 2117 if (t1 == Type::TOP) { 2118 return this; 2119 } 2120 int count = 0; 2121 assert(t1->isa_int() || t1->isa_long(), "Unexpected type"); 2122 int mask = (t1->isa_int() ? BitsPerJavaInteger : BitsPerJavaLong) - 1; 2123 if (const_shift_count(phase, this, &count) && (count & mask) == 0) { 2124 // Rotate by a multiple of 32/64 does nothing 2125 return in(1); 2126 } 2127 return this; 2128 } 2129 2130 const Type* RotateRightNode::Value(PhaseGVN* phase) const { 2131 const Type* t1 = phase->type(in(1)); 2132 const Type* t2 = phase->type(in(2)); 2133 // Either input is TOP ==> the result is TOP 2134 if (t1 == Type::TOP || t2 == Type::TOP) { 2135 return Type::TOP; 2136 } 2137 2138 if (t1->isa_int()) { 2139 const TypeInt* r1 = t1->is_int(); 2140 const TypeInt* r2 = t2->is_int(); 2141 2142 // Left input is ZERO ==> the result is ZERO. 2143 if (r1 == TypeInt::ZERO) { 2144 return TypeInt::ZERO; 2145 } 2146 // Rotate by zero does nothing 2147 if (r2 == TypeInt::ZERO) { 2148 return r1; 2149 } 2150 if (r1->is_con() && r2->is_con()) { 2151 juint r1_con = (juint)r1->get_con(); 2152 juint shift = (juint)(r2->get_con()) & (juint)(BitsPerJavaInteger - 1); // semantics of Java shifts 2153 return TypeInt::make((r1_con >> shift) | (r1_con << (32 - shift))); 2154 } 2155 return TypeInt::INT; 2156 } else { 2157 assert(t1->isa_long(), "Type must be a long"); 2158 const TypeLong* r1 = t1->is_long(); 2159 const TypeInt* r2 = t2->is_int(); 2160 // Left input is ZERO ==> the result is ZERO. 2161 if (r1 == TypeLong::ZERO) { 2162 return TypeLong::ZERO; 2163 } 2164 // Rotate by zero does nothing 2165 if (r2 == TypeInt::ZERO) { 2166 return r1; 2167 } 2168 if (r1->is_con() && r2->is_con()) { 2169 julong r1_con = (julong)r1->get_con(); 2170 julong shift = (julong)(r2->get_con()) & (julong)(BitsPerJavaLong - 1); // semantics of Java shifts 2171 return TypeLong::make((r1_con >> shift) | (r1_con << (64 - shift))); 2172 } 2173 return TypeLong::LONG; 2174 } 2175 } 2176 2177 //------------------------------ Sum & Mask ------------------------------ 2178 2179 // Returns a lower bound on the number of trailing zeros in expr. 2180 static jint AndIL_min_trailing_zeros(const PhaseGVN* phase, const Node* expr, BasicType bt) { 2181 expr = expr->uncast(); 2182 const TypeInteger* type = phase->type(expr)->isa_integer(bt); 2183 if (type == nullptr) { 2184 return 0; 2185 } 2186 2187 if (type->is_con()) { 2188 jlong con = type->get_con_as_long(bt); 2189 return con == 0L ? (type2aelembytes(bt) * BitsPerByte) : count_trailing_zeros(con); 2190 } 2191 2192 if (expr->Opcode() == Op_ConvI2L) { 2193 expr = expr->in(1)->uncast(); 2194 bt = T_INT; 2195 type = phase->type(expr)->isa_int(); 2196 } 2197 2198 // Pattern: expr = (x << shift) 2199 if (expr->Opcode() == Op_LShift(bt)) { 2200 const TypeInt* shift_t = phase->type(expr->in(2))->isa_int(); 2201 if (shift_t == nullptr || !shift_t->is_con()) { 2202 return 0; 2203 } 2204 // We need to truncate the shift, as it may not have been canonicalized yet. 2205 // T_INT: 0..31 -> shift_mask = 4 * 8 - 1 = 31 2206 // T_LONG: 0..63 -> shift_mask = 8 * 8 - 1 = 63 2207 // (JLS: "Shift Operators") 2208 jint shift_mask = type2aelembytes(bt) * BitsPerByte - 1; 2209 return shift_t->get_con() & shift_mask; 2210 } 2211 2212 return 0; 2213 } 2214 2215 // Checks whether expr is neutral additive element (zero) under mask, 2216 // i.e. whether an expression of the form: 2217 // (AndX (AddX (expr addend) mask) 2218 // (expr + addend) & mask 2219 // is equivalent to 2220 // (AndX addend mask) 2221 // addend & mask 2222 // for any addend. 2223 // (The X in AndX must be I or L, depending on bt). 2224 // 2225 // We check for the sufficient condition when the lowest set bit in expr is higher than 2226 // the highest set bit in mask, i.e.: 2227 // expr: eeeeee0000000000000 2228 // mask: 000000mmmmmmmmmmmmm 2229 // <--w bits---> 2230 // We do not test for other cases. 2231 // 2232 // Correctness: 2233 // Given "expr" with at least "w" trailing zeros, 2234 // let "mod = 2^w", "suffix_mask = mod - 1" 2235 // 2236 // Since "mask" only has bits set where "suffix_mask" does, we have: 2237 // mask = suffix_mask & mask (SUFFIX_MASK) 2238 // 2239 // And since expr only has bits set above w, and suffix_mask only below: 2240 // expr & suffix_mask == 0 (NO_BIT_OVERLAP) 2241 // 2242 // From unsigned modular arithmetic (with unsigned modulo %), and since mod is 2243 // a power of 2, and we are computing in a ring of powers of 2, we know that 2244 // (x + y) % mod = (x % mod + y) % mod 2245 // (x + y) & suffix_mask = (x & suffix_mask + y) & suffix_mask (MOD_ARITH) 2246 // 2247 // We can now prove the equality: 2248 // (expr + addend) & mask 2249 // = (expr + addend) & suffix_mask & mask (SUFFIX_MASK) 2250 // = (expr & suffix_mask + addend) & suffix_mask & mask (MOD_ARITH) 2251 // = (0 + addend) & suffix_mask & mask (NO_BIT_OVERLAP) 2252 // = addend & mask (SUFFIX_MASK) 2253 // 2254 // Hence, an expr with at least w trailing zeros is a neutral additive element under any mask with bit width w. 2255 static bool AndIL_is_zero_element_under_mask(const PhaseGVN* phase, const Node* expr, const Node* mask, BasicType bt) { 2256 // When the mask is negative, it has the most significant bit set. 2257 const TypeInteger* mask_t = phase->type(mask)->isa_integer(bt); 2258 if (mask_t == nullptr || mask_t->lo_as_long() < 0) { 2259 return false; 2260 } 2261 2262 // When the mask is constant zero, we defer to MulNode::Value to eliminate the entire AndX operation. 2263 if (mask_t->hi_as_long() == 0) { 2264 assert(mask_t->lo_as_long() == 0, "checked earlier"); 2265 return false; 2266 } 2267 2268 jint mask_bit_width = BitsPerLong - count_leading_zeros(mask_t->hi_as_long()); 2269 jint expr_trailing_zeros = AndIL_min_trailing_zeros(phase, expr, bt); 2270 return expr_trailing_zeros >= mask_bit_width; 2271 } 2272 2273 // Reduces the pattern: 2274 // (AndX (AddX add1 add2) mask) 2275 // to 2276 // (AndX add1 mask), if add2 is neutral wrt mask (see above), and vice versa. 2277 Node* MulNode::AndIL_sum_and_mask(PhaseGVN* phase, BasicType bt) { 2278 Node* add = in(1); 2279 Node* mask = in(2); 2280 int addidx = 0; 2281 if (add->Opcode() == Op_Add(bt)) { 2282 addidx = 1; 2283 } else if (mask->Opcode() == Op_Add(bt)) { 2284 mask = add; 2285 addidx = 2; 2286 add = in(addidx); 2287 } 2288 if (addidx > 0) { 2289 Node* add1 = add->in(1); 2290 Node* add2 = add->in(2); 2291 if (AndIL_is_zero_element_under_mask(phase, add1, mask, bt)) { 2292 set_req_X(addidx, add2, phase); 2293 return this; 2294 } else if (AndIL_is_zero_element_under_mask(phase, add2, mask, bt)) { 2295 set_req_X(addidx, add1, phase); 2296 return this; 2297 } 2298 } 2299 return nullptr; 2300 }