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