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