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