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