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