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/castnode.hpp"
28 #include "opto/cfgnode.hpp"
29 #include "opto/connode.hpp"
30 #include "opto/machnode.hpp"
31 #include "opto/movenode.hpp"
32 #include "opto/mulnode.hpp"
33 #include "opto/phaseX.hpp"
34 #include "opto/subnode.hpp"
35 #include "opto/utilities/xor.hpp"
36 #include "runtime/stubRoutines.hpp"
37
38 // Portions of code courtesy of Clifford Click
39
40 // Classic Add functionality. This covers all the usual 'add' behaviors for
41 // an algebraic ring. Add-integer, add-float, add-double, and binary-or are
42 // all inherited from this class. The various identity values are supplied
43 // by virtual functions.
44
45
46 //=============================================================================
47 //------------------------------hash-------------------------------------------
48 // Hash function over AddNodes. Needs to be commutative; i.e., I swap
49 // (commute) inputs to AddNodes willy-nilly so the hash function must return
50 // the same value in the presence of edge swapping.
51 uint AddNode::hash() const {
52 return (uintptr_t)in(1) + (uintptr_t)in(2) + Opcode();
53 }
54
55 //------------------------------Identity---------------------------------------
56 // If either input is a constant 0, return the other input.
57 Node* AddNode::Identity(PhaseGVN* phase) {
58 const Type *zero = add_id(); // The additive identity
59 if( phase->type( in(1) )->higher_equal( zero ) ) return in(2);
60 if( phase->type( in(2) )->higher_equal( zero ) ) return in(1);
61 return this;
62 }
63
64 //------------------------------commute----------------------------------------
65 // Commute operands to move loads and constants to the right.
66 static bool commute(PhaseGVN* phase, Node* add) {
67 Node *in1 = add->in(1);
68 Node *in2 = add->in(2);
69
70 // convert "max(a,b) + min(a,b)" into "a+b".
71 if ((in1->Opcode() == add->as_Add()->max_opcode() && in2->Opcode() == add->as_Add()->min_opcode())
72 || (in1->Opcode() == add->as_Add()->min_opcode() && in2->Opcode() == add->as_Add()->max_opcode())) {
73 Node *in11 = in1->in(1);
74 Node *in12 = in1->in(2);
75
76 Node *in21 = in2->in(1);
77 Node *in22 = in2->in(2);
78
79 if ((in11 == in21 && in12 == in22) ||
80 (in11 == in22 && in12 == in21)) {
81 add->set_req_X(1, in11, phase);
82 add->set_req_X(2, in12, phase);
83 return true;
84 }
85 }
86
87 bool con_left = phase->type(in1)->singleton();
88 bool con_right = phase->type(in2)->singleton();
89
90 // Convert "1+x" into "x+1".
91 // Right is a constant; leave it
92 if( con_right ) return false;
93 // Left is a constant; move it right.
94 if( con_left ) {
95 add->swap_edges(1, 2);
96 return true;
97 }
98
99 // Convert "Load+x" into "x+Load".
100 // Now check for loads
101 if (in2->is_Load()) {
102 if (!in1->is_Load()) {
103 // already x+Load to return
104 return false;
105 }
106 // both are loads, so fall through to sort inputs by idx
107 } else if( in1->is_Load() ) {
108 // Left is a Load and Right is not; move it right.
109 add->swap_edges(1, 2);
110 return true;
111 }
112
113 PhiNode *phi;
114 // Check for tight loop increments: Loop-phi of Add of loop-phi
115 if (in1->is_Phi() && (phi = in1->as_Phi()) && phi->region()->is_Loop() && phi->in(2) == add)
116 return false;
117 if (in2->is_Phi() && (phi = in2->as_Phi()) && phi->region()->is_Loop() && phi->in(2) == add) {
118 add->swap_edges(1, 2);
119 return true;
120 }
121
122 // Otherwise, sort inputs (commutativity) to help value numbering.
123 if( in1->_idx > in2->_idx ) {
124 add->swap_edges(1, 2);
125 return true;
126 }
127 return false;
128 }
129
130 //------------------------------Idealize---------------------------------------
131 // If we get here, we assume we are associative!
132 Node *AddNode::Ideal(PhaseGVN *phase, bool can_reshape) {
133 const Type *t1 = phase->type(in(1));
134 const Type *t2 = phase->type(in(2));
135 bool con_left = t1->singleton();
136 bool con_right = t2->singleton();
137
138 // Check for commutative operation desired
139 if (commute(phase, this)) return this;
140
141 AddNode *progress = nullptr; // Progress flag
142
143 // Convert "(x+1)+2" into "x+(1+2)". If the right input is a
144 // constant, and the left input is an add of a constant, flatten the
145 // expression tree.
146 Node *add1 = in(1);
147 Node *add2 = in(2);
148 int add1_op = add1->Opcode();
149 int this_op = Opcode();
150 if (con_right && t2 != Type::TOP && // Right input is a constant?
151 add1_op == this_op) { // Left input is an Add?
152
153 // Type of left _in right input
154 const Type *t12 = phase->type(add1->in(2));
155 if (t12->singleton() && t12 != Type::TOP) { // Left input is an add of a constant?
156 // Check for rare case of closed data cycle which can happen inside
157 // unreachable loops. In these cases the computation is undefined.
158 #ifdef ASSERT
159 Node *add11 = add1->in(1);
160 int add11_op = add11->Opcode();
161 if ((add1 == add1->in(1))
162 || (add11_op == this_op && add11->in(1) == add1)) {
163 assert(false, "dead loop in AddNode::Ideal");
164 }
165 #endif
166 // The Add of the flattened expression
167 Node *x1 = add1->in(1);
168 Node *x2 = phase->makecon(add1->as_Add()->add_ring(t2, t12));
169 set_req_X(2, x2, phase);
170 set_req_X(1, x1, phase);
171 progress = this; // Made progress
172 add1 = in(1);
173 add1_op = add1->Opcode();
174 }
175 }
176
177 // Convert "(x+1)+y" into "(x+y)+1". Push constants down the expression tree.
178 if (add1_op == this_op && !con_right) {
179 Node *a12 = add1->in(2);
180 const Type *t12 = phase->type( a12 );
181 if (t12->singleton() && t12 != Type::TOP && (add1 != add1->in(1)) &&
182 !(add1->in(1)->is_Phi() && (add1->in(1)->as_Phi()->is_tripcount(T_INT) || add1->in(1)->as_Phi()->is_tripcount(T_LONG)))) {
183 assert(add1->in(1) != this, "dead loop in AddNode::Ideal");
184 add2 = add1->clone();
185 add2->set_req(2, in(2));
186 add2 = phase->transform(add2);
187 set_req_X(1, add2, phase);
188 set_req_X(2, a12, phase);
189 progress = this;
190 add2 = a12;
191 }
192 }
193
194 // Convert "x+(y+1)" into "(x+y)+1". Push constants down the expression tree.
195 int add2_op = add2->Opcode();
196 if (add2_op == this_op && !con_left) {
197 Node *a22 = add2->in(2);
198 const Type *t22 = phase->type( a22 );
199 if (t22->singleton() && t22 != Type::TOP && (add2 != add2->in(1)) &&
200 !(add2->in(1)->is_Phi() && (add2->in(1)->as_Phi()->is_tripcount(T_INT) || add2->in(1)->as_Phi()->is_tripcount(T_LONG)))) {
201 assert(add2->in(1) != this, "dead loop in AddNode::Ideal");
202 Node *addx = add2->clone();
203 addx->set_req(1, in(1));
204 addx->set_req(2, add2->in(1));
205 addx = phase->transform(addx);
206 set_req_X(1, addx, phase);
207 set_req_X(2, a22, phase);
208 progress = this;
209 }
210 }
211
212 return progress;
213 }
214
215 //------------------------------Value-----------------------------------------
216 // An add node sums it's two _in. If one input is an RSD, we must mixin
217 // the other input's symbols.
218 const Type* AddNode::Value(PhaseGVN* phase) const {
219 // Either input is TOP ==> the result is TOP
220 const Type* t1 = phase->type(in(1));
221 const Type* t2 = phase->type(in(2));
222 if (t1 == Type::TOP || t2 == Type::TOP) {
223 return Type::TOP;
224 }
225
226 // Check for an addition involving the additive identity
227 const Type* tadd = add_of_identity(t1, t2);
228 if (tadd != nullptr) {
229 return tadd;
230 }
231
232 return add_ring(t1, t2); // Local flavor of type addition
233 }
234
235 //------------------------------add_identity-----------------------------------
236 // Check for addition of the identity
237 const Type *AddNode::add_of_identity( const Type *t1, const Type *t2 ) const {
238 const Type *zero = add_id(); // The additive identity
239 if( t1->higher_equal( zero ) ) return t2;
240 if( t2->higher_equal( zero ) ) return t1;
241
242 return nullptr;
243 }
244
245 AddNode* AddNode::make(Node* in1, Node* in2, BasicType bt) {
246 switch (bt) {
247 case T_INT:
248 return new AddINode(in1, in2);
249 case T_LONG:
250 return new AddLNode(in1, in2);
251 default:
252 fatal("Not implemented for %s", type2name(bt));
253 }
254 return nullptr;
255 }
256
257 bool AddNode::is_not(PhaseGVN* phase, Node* n, BasicType bt) {
258 return n->Opcode() == Op_Xor(bt) && phase->type(n->in(2)) == TypeInteger::minus_1(bt);
259 }
260
261 AddNode* AddNode::make_not(PhaseGVN* phase, Node* n, BasicType bt) {
262 switch (bt) {
263 case T_INT:
264 return new XorINode(n, phase->intcon(-1));
265 case T_LONG:
266 return new XorLNode(n, phase->longcon(-1L));
267 default:
268 fatal("Not implemented for %s", type2name(bt));
269 }
270 return nullptr;
271 }
272
273 //=============================================================================
274 //------------------------------Idealize---------------------------------------
275 Node* AddNode::IdealIL(PhaseGVN* phase, bool can_reshape, BasicType bt) {
276 Node* in1 = in(1);
277 Node* in2 = in(2);
278 int op1 = in1->Opcode();
279 int op2 = in2->Opcode();
280 // Fold (con1-x)+con2 into (con1+con2)-x
281 if (op1 == Op_Add(bt) && op2 == Op_Sub(bt)) {
282 // Swap edges to try optimizations below
283 in1 = in2;
284 in2 = in(1);
285 op1 = op2;
286 op2 = in2->Opcode();
287 }
288 if (op1 == Op_Sub(bt)) {
289 const Type* t_sub1 = phase->type(in1->in(1));
290 const Type* t_2 = phase->type(in2 );
291 if (t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP) {
292 return SubNode::make(phase->makecon(add_ring(t_sub1, t_2)), in1->in(2), bt);
293 }
294 // Convert "(a-b)+(c-d)" into "(a+c)-(b+d)"
295 if (op2 == Op_Sub(bt)) {
296 // Check for dead cycle: d = (a-b)+(c-d)
297 assert( in1->in(2) != this && in2->in(2) != this,
298 "dead loop in AddINode::Ideal" );
299 Node* sub = SubNode::make(nullptr, nullptr, bt);
300 Node* sub_in1;
301 PhaseIterGVN* igvn = phase->is_IterGVN();
302 // During IGVN, if both inputs of the new AddNode are a tree of SubNodes, this same transformation will be applied
303 // to every node of the tree. Calling transform() causes the transformation to be applied recursively, once per
304 // tree node whether some subtrees are identical or not. Pushing to the IGVN worklist instead, causes the transform
305 // to be applied once per unique subtrees (because all uses of a subtree are updated with the result of the
306 // transformation). In case of a large tree, this can make a difference in compilation time.
307 if (igvn != nullptr) {
308 sub_in1 = igvn->register_new_node_with_optimizer(AddNode::make(in1->in(1), in2->in(1), bt));
309 } else {
310 sub_in1 = phase->transform(AddNode::make(in1->in(1), in2->in(1), bt));
311 }
312 Node* sub_in2;
313 if (igvn != nullptr) {
314 sub_in2 = igvn->register_new_node_with_optimizer(AddNode::make(in1->in(2), in2->in(2), bt));
315 } else {
316 sub_in2 = phase->transform(AddNode::make(in1->in(2), in2->in(2), bt));
317 }
318 sub->init_req(1, sub_in1);
319 sub->init_req(2, sub_in2);
320 return sub;
321 }
322 // Convert "(a-b)+(b+c)" into "(a+c)"
323 if (op2 == Op_Add(bt) && in1->in(2) == in2->in(1)) {
324 assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddINode::Ideal/AddLNode::Ideal");
325 return AddNode::make(in1->in(1), in2->in(2), bt);
326 }
327 // Convert "(a-b)+(c+b)" into "(a+c)"
328 if (op2 == Op_Add(bt) && in1->in(2) == in2->in(2)) {
329 assert(in1->in(1) != this && in2->in(1) != this,"dead loop in AddINode::Ideal/AddLNode::Ideal");
330 return AddNode::make(in1->in(1), in2->in(1), bt);
331 }
332 }
333
334 // Convert (con - y) + x into "(x - y) + con"
335 if (op1 == Op_Sub(bt) && in1->in(1)->Opcode() == Op_ConIL(bt)
336 && in1 != in1->in(2) && !(in1->in(2)->is_Phi() && in1->in(2)->as_Phi()->is_tripcount(bt))) {
337 return AddNode::make(phase->transform(SubNode::make(in2, in1->in(2), bt)), in1->in(1), bt);
338 }
339
340 // Convert x + (con - y) into "(x - y) + con"
341 if (op2 == Op_Sub(bt) && in2->in(1)->Opcode() == Op_ConIL(bt)
342 && in2 != in2->in(2) && !(in2->in(2)->is_Phi() && in2->in(2)->as_Phi()->is_tripcount(bt))) {
343 return AddNode::make(phase->transform(SubNode::make(in1, in2->in(2), bt)), in2->in(1), bt);
344 }
345
346 // Associative
347 if (op1 == Op_Mul(bt) && op2 == Op_Mul(bt)) {
348 Node* add_in1 = nullptr;
349 Node* add_in2 = nullptr;
350 Node* mul_in = nullptr;
351
352 if (in1->in(1) == in2->in(1)) {
353 // Convert "a*b+a*c into a*(b+c)
354 add_in1 = in1->in(2);
355 add_in2 = in2->in(2);
356 mul_in = in1->in(1);
357 } else if (in1->in(2) == in2->in(1)) {
358 // Convert a*b+b*c into b*(a+c)
359 add_in1 = in1->in(1);
360 add_in2 = in2->in(2);
361 mul_in = in1->in(2);
362 } else if (in1->in(2) == in2->in(2)) {
363 // Convert a*c+b*c into (a+b)*c
364 add_in1 = in1->in(1);
365 add_in2 = in2->in(1);
366 mul_in = in1->in(2);
367 } else if (in1->in(1) == in2->in(2)) {
368 // Convert a*b+c*a into a*(b+c)
369 add_in1 = in1->in(2);
370 add_in2 = in2->in(1);
371 mul_in = in1->in(1);
372 }
373
374 if (mul_in != nullptr) {
375 Node* add = phase->transform(AddNode::make(add_in1, add_in2, bt));
376 return MulNode::make(mul_in, add, bt);
377 }
378 }
379
380 // Convert (x >>> rshift) + (x << lshift) into RotateRight(x, rshift)
381 if (Matcher::match_rule_supported(Op_RotateRight) &&
382 ((op1 == Op_URShift(bt) && op2 == Op_LShift(bt)) || (op1 == Op_LShift(bt) && op2 == Op_URShift(bt))) &&
383 in1->in(1) != nullptr && in1->in(1) == in2->in(1)) {
384 Node* rshift = op1 == Op_URShift(bt) ? in1->in(2) : in2->in(2);
385 Node* lshift = op1 == Op_URShift(bt) ? in2->in(2) : in1->in(2);
386 if (rshift != nullptr && lshift != nullptr) {
387 const TypeInt* rshift_t = phase->type(rshift)->isa_int();
388 const TypeInt* lshift_t = phase->type(lshift)->isa_int();
389 int bits = bt == T_INT ? 32 : 64;
390 int mask = bt == T_INT ? 0x1F : 0x3F;
391 if (lshift_t != nullptr && lshift_t->is_con() &&
392 rshift_t != nullptr && rshift_t->is_con() &&
393 ((lshift_t->get_con() & mask) == (bits - (rshift_t->get_con() & mask)))) {
394 return new RotateRightNode(in1->in(1), phase->intcon(rshift_t->get_con() & mask), TypeInteger::bottom(bt));
395 }
396 }
397 }
398
399 // Collapse addition of the same terms into multiplications.
400 Node* collapsed = Ideal_collapse_variable_times_con(phase, bt);
401 if (collapsed != nullptr) {
402 return collapsed; // Skip AddNode::Ideal() since it may now be a multiplication node.
403 }
404
405 return AddNode::Ideal(phase, can_reshape);
406 }
407
408 // Try to collapse addition of the same terms into a single multiplication. On success, a new MulNode is returned.
409 // Examples of this conversion includes:
410 // - a + a + ... + a => CON*a
411 // - (a * CON) + a => (CON + 1) * a
412 // - a + (a * CON) => (CON + 1) * a
413 //
414 // We perform such conversions incrementally during IGVN by transforming left most nodes first and work up to the root
415 // of the expression. In other words, we convert, at each iteration:
416 // a + a + a + ... + a
417 // => 2*a + a + ... + a
418 // => 3*a + ... + a
419 // => n*a
420 //
421 // Due to the iterative nature of IGVN, MulNode transformed from first few AddNode terms may be further transformed into
422 // power-of-2 pattern. (e.g., 2 * a => a << 1, 3 * a => (a << 2) + a). We can't guarantee we'll always pick up
423 // transformed power-of-2 patterns when term `a` is complex.
424 //
425 // Note this also converts, for example, original expression `(a*3) + a` into `4*a` and `(a<<2) + a` into `5*a`. A more
426 // generalized pattern `(a*b) + (a*c)` into `a*(b + c)` is handled by AddNode::IdealIL().
427 Node* AddNode::Ideal_collapse_variable_times_con(PhaseGVN* phase, BasicType bt) {
428 // We need to make sure that the current AddNode is not part of a MulNode that has already been optimized to a
429 // power-of-2 addition (e.g., 3 * a => (a << 2) + a). Without this check, GVN would keep trying to optimize the same
430 // node and can't progress. For example, 3 * a => (a << 2) + a => 3 * a => (a << 2) + a => ...
431 if (Multiplication::find_power_of_two_addition_pattern(this, bt).is_valid()) {
432 return nullptr;
433 }
434
435 Node* lhs = in(1);
436 Node* rhs = in(2);
437
438 Multiplication mul = Multiplication::find_collapsible_addition_patterns(lhs, rhs, bt);
439 if (!mul.is_valid_with(rhs)) {
440 // Swap lhs and rhs then try again
441 mul = Multiplication::find_collapsible_addition_patterns(rhs, lhs, bt);
442 if (!mul.is_valid_with(lhs)) {
443 return nullptr;
444 }
445 }
446
447 Node* con;
448 if (bt == T_INT) {
449 con = phase->intcon(java_add(static_cast<jint>(mul.multiplier()), 1));
450 } else {
451 con = phase->longcon(java_add(mul.multiplier(), CONST64(1)));
452 }
453
454 return MulNode::make(con, mul.variable(), bt);
455 }
456
457 // Find a pattern of collapsable additions that can be converted to a multiplication.
458 // When matching the LHS `a * CON`, we match with best efforts by looking for the following patterns:
459 // - (1) Simple addition: LHS = a + a
460 // - (2) Simple lshift: LHS = a << CON
461 // - (3) Simple multiplication: LHS = CON * a
462 // - (4) Power-of-two addition: LHS = (a << CON1) + (a << CON2)
463 AddNode::Multiplication AddNode::Multiplication::find_collapsible_addition_patterns(const Node* a, const Node* pattern, BasicType bt) {
464 // (1) Simple addition pattern (e.g., lhs = a + a)
465 Multiplication mul = find_simple_addition_pattern(a, bt);
466 if (mul.is_valid_with(pattern)) {
467 return mul;
468 }
469
470 // (2) Simple lshift pattern (e.g., lhs = a << CON)
471 mul = find_simple_lshift_pattern(a, bt);
472 if (mul.is_valid_with(pattern)) {
473 return mul;
474 }
475
476 // (3) Simple multiplication pattern (e.g., lhs = CON * a)
477 mul = find_simple_multiplication_pattern(a, bt);
478 if (mul.is_valid_with(pattern)) {
479 return mul;
480 }
481
482 // (4) Power-of-two addition pattern (e.g., lhs = (a << CON1) + (a << CON2))
483 // While multiplications can be potentially optimized to power-of-2 subtractions (e.g., a * 7 => (a << 3) - a),
484 // (x - y) + y => x is already handled by the Identity() methods. So, we don't need to check for that pattern here.
485 mul = find_power_of_two_addition_pattern(a, bt);
486 if (mul.is_valid_with(pattern)) {
487 return mul;
488 }
489
490 // We've tried everything.
491 return make_invalid();
492 }
493
494 // Try to match `n = a + a`. On success, return a struct with `.valid = true`, `variable = a`, and `multiplier = 2`.
495 // The method matches `n` for pattern: a + a.
496 AddNode::Multiplication AddNode::Multiplication::find_simple_addition_pattern(const Node* n, BasicType bt) {
497 if (n->Opcode() == Op_Add(bt) && n->in(1) == n->in(2)) {
498 return Multiplication(n->in(1), 2);
499 }
500
501 return make_invalid();
502 }
503
504 // Try to match `n = a << CON`. On success, return a struct with `.valid = true`, `variable = a`, and
505 // `multiplier = 1 << CON`.
506 // Match `n` for pattern: a << CON.
507 // Note that the power-of-2 multiplication optimization could potentially convert a MulNode to this pattern.
508 AddNode::Multiplication AddNode::Multiplication::find_simple_lshift_pattern(const Node* n, BasicType bt) {
509 // Note that power-of-2 multiplication optimization could potentially convert a MulNode to this pattern
510 if (n->Opcode() == Op_LShift(bt) && n->in(2)->is_Con()) {
511 Node* con = n->in(2);
512 if (!con->is_top()) {
513 return Multiplication(n->in(1), java_shift_left(1, con->get_int(), bt));
514 }
515 }
516
517 return make_invalid();
518 }
519
520 // Try to match `n = CON * a`. On success, return a struct with `.valid = true`, `variable = a`, and `multiplier = CON`.
521 // Match `n` for patterns: CON * a
522 // Note that `CON` will always be the second input node of a Mul node canonicalized by Ideal(). If this is not the case,
523 // `n` has not been processed by iGVN. So we skip the optimization for the current add node and wait for to be added to
524 // the queue again.
525 AddNode::Multiplication AddNode::Multiplication::find_simple_multiplication_pattern(const Node* n, BasicType bt) {
526 if (n->Opcode() == Op_Mul(bt) && n->in(2)->is_Con()) {
527 Node* con = n->in(2);
528 Node* base = n->in(1);
529
530 if (!con->is_top()) {
531 return Multiplication(base, con->get_integer_as_long(bt));
532 }
533 }
534
535 return make_invalid();
536 }
537
538 // Try to match `n = (a << CON1) + (a << CON2)`. On success, return a struct with `.valid = true`, `variable = a`, and
539 // `multiplier = (1 << CON1) + (1 << CON2)`.
540 // Match `n` for patterns:
541 // - (1) (a << CON) + (a << CON)
542 // - (2) (a << CON) + a
543 // - (3) a + (a << CON)
544 // - (4) a + a
545 // Note that one or both of the term of the addition could simply be `a` (i.e., a << 0) as in pattern (4).
546 AddNode::Multiplication AddNode::Multiplication::find_power_of_two_addition_pattern(const Node* n, BasicType bt) {
547 if (n->Opcode() == Op_Add(bt) && n->in(1) != n->in(2)) {
548 const Multiplication lhs = find_simple_lshift_pattern(n->in(1), bt);
549 const Multiplication rhs = find_simple_lshift_pattern(n->in(2), bt);
550
551 // Pattern (1)
552 {
553 const Multiplication res = lhs.add(rhs);
554 if (res.is_valid()) {
555 return res;
556 }
557 }
558
559 // Pattern (2)
560 if (lhs.is_valid_with(n->in(2))) {
561 return Multiplication(lhs.variable(), java_add(lhs.multiplier(), CONST64(1)));
562 }
563
564 // Pattern (3)
565 if (rhs.is_valid_with(n->in(1))) {
566 return Multiplication(rhs.variable(), java_add(rhs.multiplier(), CONST64(1)));
567 }
568
569 // Pattern (4), which is equivalent to a simple addition pattern
570 return find_simple_addition_pattern(n, bt);
571 }
572
573 return make_invalid();
574 }
575
576 Node* AddINode::Ideal(PhaseGVN* phase, bool can_reshape) {
577 Node* in1 = in(1);
578 Node* in2 = in(2);
579 int op1 = in1->Opcode();
580 int op2 = in2->Opcode();
581
582 // Convert (x>>>z)+y into (x+(y<<z))>>>z for small constant z and y.
583 // Helps with array allocation math constant folding
584 // See 4790063:
585 // Unrestricted transformation is unsafe for some runtime values of 'x'
586 // ( x == 0, z == 1, y == -1 ) fails
587 // ( x == -5, z == 1, y == 1 ) fails
588 // Transform works for small z and small negative y when the addition
589 // (x + (y << z)) does not cross zero.
590 // Implement support for negative y and (x >= -(y << z))
591 // Have not observed cases where type information exists to support
592 // positive y and (x <= -(y << z))
593 if (op1 == Op_URShiftI && op2 == Op_ConI &&
594 in1->in(2)->Opcode() == Op_ConI) {
595 jint z = phase->type(in1->in(2))->is_int()->get_con() & 0x1f; // only least significant 5 bits matter
596 jint y = phase->type(in2)->is_int()->get_con();
597
598 if (z < 5 && -5 < y && y < 0) {
599 const Type* t_in11 = phase->type(in1->in(1));
600 if( t_in11 != Type::TOP && (t_in11->is_int()->_lo >= -(y << z))) {
601 Node* a = phase->transform(new AddINode( in1->in(1), phase->intcon(y<<z)));
602 return new URShiftINode(a, in1->in(2));
603 }
604 }
605 }
606
607 return AddNode::IdealIL(phase, can_reshape, T_INT);
608 }
609
610
611 //------------------------------Identity---------------------------------------
612 // Fold (x-y)+y OR y+(x-y) into x
613 Node* AddINode::Identity(PhaseGVN* phase) {
614 if (in(1)->Opcode() == Op_SubI && in(1)->in(2) == in(2)) {
615 return in(1)->in(1);
616 } else if (in(2)->Opcode() == Op_SubI && in(2)->in(2) == in(1)) {
617 return in(2)->in(1);
618 }
619 return AddNode::Identity(phase);
620 }
621
622
623 //------------------------------add_ring---------------------------------------
624 // Supplied function returns the sum of the inputs. Guaranteed never
625 // to be passed a TOP or BOTTOM type, these are filtered out by
626 // pre-check.
627 const Type *AddINode::add_ring( const Type *t0, const Type *t1 ) const {
628 const TypeInt *r0 = t0->is_int(); // Handy access
629 const TypeInt *r1 = t1->is_int();
630 int lo = java_add(r0->_lo, r1->_lo);
631 int hi = java_add(r0->_hi, r1->_hi);
632 if( !(r0->is_con() && r1->is_con()) ) {
633 // Not both constants, compute approximate result
634 if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {
635 lo = min_jint; hi = max_jint; // Underflow on the low side
636 }
637 if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {
638 lo = min_jint; hi = max_jint; // Overflow on the high side
639 }
640 if( lo > hi ) { // Handle overflow
641 lo = min_jint; hi = max_jint;
642 }
643 } else {
644 // both constants, compute precise result using 'lo' and 'hi'
645 // Semantics define overflow and underflow for integer addition
646 // as expected. In particular: 0x80000000 + 0x80000000 --> 0x0
647 }
648 return TypeInt::make( lo, hi, MAX2(r0->_widen,r1->_widen) );
649 }
650
651
652 //=============================================================================
653 //------------------------------Idealize---------------------------------------
654 Node* AddLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
655 return AddNode::IdealIL(phase, can_reshape, T_LONG);
656 }
657
658
659 //------------------------------Identity---------------------------------------
660 // Fold (x-y)+y OR y+(x-y) into x
661 Node* AddLNode::Identity(PhaseGVN* phase) {
662 if (in(1)->Opcode() == Op_SubL && in(1)->in(2) == in(2)) {
663 return in(1)->in(1);
664 } else if (in(2)->Opcode() == Op_SubL && in(2)->in(2) == in(1)) {
665 return in(2)->in(1);
666 }
667 return AddNode::Identity(phase);
668 }
669
670
671 //------------------------------add_ring---------------------------------------
672 // Supplied function returns the sum of the inputs. Guaranteed never
673 // to be passed a TOP or BOTTOM type, these are filtered out by
674 // pre-check.
675 const Type *AddLNode::add_ring( const Type *t0, const Type *t1 ) const {
676 const TypeLong *r0 = t0->is_long(); // Handy access
677 const TypeLong *r1 = t1->is_long();
678 jlong lo = java_add(r0->_lo, r1->_lo);
679 jlong hi = java_add(r0->_hi, r1->_hi);
680 if( !(r0->is_con() && r1->is_con()) ) {
681 // Not both constants, compute approximate result
682 if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {
683 lo =min_jlong; hi = max_jlong; // Underflow on the low side
684 }
685 if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {
686 lo = min_jlong; hi = max_jlong; // Overflow on the high side
687 }
688 if( lo > hi ) { // Handle overflow
689 lo = min_jlong; hi = max_jlong;
690 }
691 } else {
692 // both constants, compute precise result using 'lo' and 'hi'
693 // Semantics define overflow and underflow for integer addition
694 // as expected. In particular: 0x80000000 + 0x80000000 --> 0x0
695 }
696 return TypeLong::make( lo, hi, MAX2(r0->_widen,r1->_widen) );
697 }
698
699
700 //=============================================================================
701 //------------------------------add_of_identity--------------------------------
702 // Check for addition of the identity
703 const Type *AddFNode::add_of_identity( const Type *t1, const Type *t2 ) const {
704 // x ADD 0 should return x unless 'x' is a -zero
705 //
706 // const Type *zero = add_id(); // The additive identity
707 // jfloat f1 = t1->getf();
708 // jfloat f2 = t2->getf();
709 //
710 // if( t1->higher_equal( zero ) ) return t2;
711 // if( t2->higher_equal( zero ) ) return t1;
712
713 return nullptr;
714 }
715
716 //------------------------------add_ring---------------------------------------
717 // Supplied function returns the sum of the inputs.
718 // This also type-checks the inputs for sanity. Guaranteed never to
719 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
720 const Type *AddFNode::add_ring( const Type *t0, const Type *t1 ) const {
721 if (!t0->isa_float_constant() || !t1->isa_float_constant()) {
722 return bottom_type();
723 }
724 return TypeF::make( t0->getf() + t1->getf() );
725 }
726
727 //------------------------------Ideal------------------------------------------
728 Node *AddFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
729 // Floating point additions are not associative because of boundary conditions (infinity)
730 return commute(phase, this) ? this : nullptr;
731 }
732
733 //=============================================================================
734 //------------------------------add_of_identity--------------------------------
735 // Check for addition of the identity
736 const Type* AddHFNode::add_of_identity(const Type* t1, const Type* t2) const {
737 return nullptr;
738 }
739
740 // Supplied function returns the sum of the inputs.
741 // This also type-checks the inputs for sanity. Guaranteed never to
742 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
743 const Type* AddHFNode::add_ring(const Type* t0, const Type* t1) const {
744 if (!t0->isa_half_float_constant() || !t1->isa_half_float_constant()) {
745 return bottom_type();
746 }
747 return TypeH::make(t0->getf() + t1->getf());
748 }
749
750 //=============================================================================
751 //------------------------------add_of_identity--------------------------------
752 // Check for addition of the identity
753 const Type *AddDNode::add_of_identity( const Type *t1, const Type *t2 ) const {
754 // x ADD 0 should return x unless 'x' is a -zero
755 //
756 // const Type *zero = add_id(); // The additive identity
757 // jfloat f1 = t1->getf();
758 // jfloat f2 = t2->getf();
759 //
760 // if( t1->higher_equal( zero ) ) return t2;
761 // if( t2->higher_equal( zero ) ) return t1;
762
763 return nullptr;
764 }
765 //------------------------------add_ring---------------------------------------
766 // Supplied function returns the sum of the inputs.
767 // This also type-checks the inputs for sanity. Guaranteed never to
768 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
769 const Type *AddDNode::add_ring( const Type *t0, const Type *t1 ) const {
770 if (!t0->isa_double_constant() || !t1->isa_double_constant()) {
771 return bottom_type();
772 }
773 return TypeD::make( t0->getd() + t1->getd() );
774 }
775
776 //------------------------------Ideal------------------------------------------
777 Node *AddDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
778 // Floating point additions are not associative because of boundary conditions (infinity)
779 return commute(phase, this) ? this : nullptr;
780 }
781
782
783 //=============================================================================
784 //------------------------------Identity---------------------------------------
785 // If one input is a constant 0, return the other input.
786 Node* AddPNode::Identity(PhaseGVN* phase) {
787 return ( phase->type( in(Offset) )->higher_equal( TypeX_ZERO ) ) ? in(Address) : this;
788 }
789
790 //------------------------------Idealize---------------------------------------
791 Node *AddPNode::Ideal(PhaseGVN *phase, bool can_reshape) {
792 // Bail out if dead inputs
793 if( phase->type( in(Address) ) == Type::TOP ) return nullptr;
794
795 // If the left input is an add of a constant, flatten the expression tree.
796 const Node *n = in(Address);
797 if (n->is_AddP() && n->in(Base) == in(Base)) {
798 const AddPNode *addp = n->as_AddP(); // Left input is an AddP
799 assert( !addp->in(Address)->is_AddP() ||
800 addp->in(Address)->as_AddP() != addp,
801 "dead loop in AddPNode::Ideal" );
802 // Type of left input's right input
803 const Type *t = phase->type( addp->in(Offset) );
804 if( t == Type::TOP ) return nullptr;
805 const TypeX *t12 = t->is_intptr_t();
806 if( t12->is_con() ) { // Left input is an add of a constant?
807 // If the right input is a constant, combine constants
808 const Type *temp_t2 = phase->type( in(Offset) );
809 if( temp_t2 == Type::TOP ) return nullptr;
810 const TypeX *t2 = temp_t2->is_intptr_t();
811 Node* address;
812 Node* offset;
813 if( t2->is_con() ) {
814 // The Add of the flattened expression
815 address = addp->in(Address);
816 offset = phase->MakeConX(t2->get_con() + t12->get_con());
817 } else {
818 // Else move the constant to the right. ((A+con)+B) into ((A+B)+con)
819 address = phase->transform(new AddPNode(in(Base),addp->in(Address),in(Offset)));
820 offset = addp->in(Offset);
821 }
822 set_req_X(Address, address, phase);
823 set_req_X(Offset, offset, phase);
824 return this;
825 }
826 }
827
828 // Raw pointers?
829 if( in(Base)->bottom_type() == Type::TOP ) {
830 // If this is a null+long form (from unsafe accesses), switch to a rawptr.
831 if (phase->type(in(Address)) == TypePtr::NULL_PTR) {
832 Node* offset = in(Offset);
833 return new CastX2PNode(offset);
834 }
835 }
836
837 // If the right is an add of a constant, push the offset down.
838 // Convert: (ptr + (offset+con)) into (ptr+offset)+con.
839 // The idea is to merge array_base+scaled_index groups together,
840 // and only have different constant offsets from the same base.
841 const Node *add = in(Offset);
842 if( add->Opcode() == Op_AddX && add->in(1) != add ) {
843 const Type *t22 = phase->type( add->in(2) );
844 if( t22->singleton() && (t22 != Type::TOP) ) { // Right input is an add of a constant?
845 set_req(Address, phase->transform(new AddPNode(in(Base),in(Address),add->in(1))));
846 set_req_X(Offset, add->in(2), phase); // puts add on igvn worklist if needed
847 return this; // Made progress
848 }
849 }
850
851 return nullptr; // No progress
852 }
853
854 //------------------------------bottom_type------------------------------------
855 // Bottom-type is the pointer-type with unknown offset.
856 const Type *AddPNode::bottom_type() const {
857 if (in(Address) == nullptr) return TypePtr::BOTTOM;
858 const TypePtr *tp = in(Address)->bottom_type()->isa_ptr();
859 if( !tp ) return Type::TOP; // TOP input means TOP output
860 assert( in(Offset)->Opcode() != Op_ConP, "" );
861 const Type *t = in(Offset)->bottom_type();
862 if( t == Type::TOP )
863 return tp->add_offset(Type::OffsetTop);
864 const TypeX *tx = t->is_intptr_t();
865 intptr_t txoffset = Type::OffsetBot;
866 if (tx->is_con()) { // Left input is an add of a constant?
867 txoffset = tx->get_con();
868 }
869 if (tp->isa_aryptr()) {
870 // In the case of a flat inline type array, each field has its
871 // own slice so we need to extract the field being accessed from
872 // the address computation
873 return tp->is_aryptr()->add_field_offset_and_offset(txoffset);
874 }
875 return tp->add_offset(txoffset);
876 }
877
878 //------------------------------Value------------------------------------------
879 const Type* AddPNode::Value(PhaseGVN* phase) const {
880 // Either input is TOP ==> the result is TOP
881 const Type *t1 = phase->type( in(Address) );
882 const Type *t2 = phase->type( in(Offset) );
883 if( t1 == Type::TOP ) return Type::TOP;
884 if( t2 == Type::TOP ) return Type::TOP;
885
886 // Left input is a pointer
887 const TypePtr *p1 = t1->isa_ptr();
888 // Right input is an int
889 const TypeX *p2 = t2->is_intptr_t();
890 // Add 'em
891 intptr_t p2offset = Type::OffsetBot;
892 if (p2->is_con()) { // Left input is an add of a constant?
893 p2offset = p2->get_con();
894 }
895 if (p1->isa_aryptr()) {
896 // In the case of a flat inline type array, each field has its
897 // own slice so we need to extract the field being accessed from
898 // the address computation
899 return p1->is_aryptr()->add_field_offset_and_offset(p2offset);
900 }
901 return p1->add_offset(p2offset);
902 }
903
904 //------------------------Ideal_base_and_offset--------------------------------
905 // Split an oop pointer into a base and offset.
906 // (The offset might be Type::OffsetBot in the case of an array.)
907 // Return the base, or null if failure.
908 Node* AddPNode::Ideal_base_and_offset(Node* ptr, PhaseValues* phase,
909 // second return value:
910 intptr_t& offset) {
911 if (ptr->is_AddP()) {
912 Node* base = ptr->in(AddPNode::Base);
913 Node* addr = ptr->in(AddPNode::Address);
914 Node* offs = ptr->in(AddPNode::Offset);
915 if (base == addr || base->is_top()) {
916 offset = phase->find_intptr_t_con(offs, Type::OffsetBot);
917 if (offset != Type::OffsetBot) {
918 return addr;
919 }
920 }
921 }
922 offset = Type::OffsetBot;
923 return nullptr;
924 }
925
926 //------------------------------unpack_offsets----------------------------------
927 // Collect the AddP offset values into the elements array, giving up
928 // if there are more than length.
929 int AddPNode::unpack_offsets(Node* elements[], int length) const {
930 int count = 0;
931 Node const* addr = this;
932 Node* base = addr->in(AddPNode::Base);
933 while (addr->is_AddP()) {
934 if (addr->in(AddPNode::Base) != base) {
935 // give up
936 return -1;
937 }
938 elements[count++] = addr->in(AddPNode::Offset);
939 if (count == length) {
940 // give up
941 return -1;
942 }
943 addr = addr->in(AddPNode::Address);
944 }
945 if (addr != base) {
946 return -1;
947 }
948 return count;
949 }
950
951 //------------------------------match_edge-------------------------------------
952 // Do we Match on this edge index or not? Do not match base pointer edge
953 uint AddPNode::match_edge(uint idx) const {
954 return idx > Base;
955 }
956
957 //=============================================================================
958 //------------------------------Identity---------------------------------------
959 Node* OrINode::Identity(PhaseGVN* phase) {
960 // x | x => x
961 if (in(1) == in(2)) {
962 return in(1);
963 }
964
965 return AddNode::Identity(phase);
966 }
967
968 // Find shift value for Integer or Long OR.
969 static Node* rotate_shift(PhaseGVN* phase, Node* lshift, Node* rshift, int mask) {
970 // val << norm_con_shift | val >> ({32|64} - norm_con_shift) => rotate_left val, norm_con_shift
971 const TypeInt* lshift_t = phase->type(lshift)->isa_int();
972 const TypeInt* rshift_t = phase->type(rshift)->isa_int();
973 if (lshift_t != nullptr && lshift_t->is_con() &&
974 rshift_t != nullptr && rshift_t->is_con() &&
975 ((lshift_t->get_con() & mask) == ((mask + 1) - (rshift_t->get_con() & mask)))) {
976 return phase->intcon(lshift_t->get_con() & mask);
977 }
978 // val << var_shift | val >> ({0|32|64} - var_shift) => rotate_left val, var_shift
979 if (rshift->Opcode() == Op_SubI && rshift->in(2) == lshift && rshift->in(1)->is_Con()){
980 const TypeInt* shift_t = phase->type(rshift->in(1))->isa_int();
981 if (shift_t != nullptr && shift_t->is_con() &&
982 (shift_t->get_con() == 0 || shift_t->get_con() == (mask + 1))) {
983 return lshift;
984 }
985 }
986 return nullptr;
987 }
988
989 Node* OrINode::Ideal(PhaseGVN* phase, bool can_reshape) {
990 int lopcode = in(1)->Opcode();
991 int ropcode = in(2)->Opcode();
992 if (Matcher::match_rule_supported(Op_RotateLeft) &&
993 lopcode == Op_LShiftI && ropcode == Op_URShiftI && in(1)->in(1) == in(2)->in(1)) {
994 Node* lshift = in(1)->in(2);
995 Node* rshift = in(2)->in(2);
996 Node* shift = rotate_shift(phase, lshift, rshift, 0x1F);
997 if (shift != nullptr) {
998 return new RotateLeftNode(in(1)->in(1), shift, TypeInt::INT);
999 }
1000 return nullptr;
1001 }
1002 if (Matcher::match_rule_supported(Op_RotateRight) &&
1003 lopcode == Op_URShiftI && ropcode == Op_LShiftI && in(1)->in(1) == in(2)->in(1)) {
1004 Node* rshift = in(1)->in(2);
1005 Node* lshift = in(2)->in(2);
1006 Node* shift = rotate_shift(phase, rshift, lshift, 0x1F);
1007 if (shift != nullptr) {
1008 return new RotateRightNode(in(1)->in(1), shift, TypeInt::INT);
1009 }
1010 }
1011
1012 // Convert "~a | ~b" into "~(a & b)"
1013 if (AddNode::is_not(phase, in(1), T_INT) && AddNode::is_not(phase, in(2), T_INT)) {
1014 Node* and_a_b = new AndINode(in(1)->in(1), in(2)->in(1));
1015 Node* tn = phase->transform(and_a_b);
1016 return AddNode::make_not(phase, tn, T_INT);
1017 }
1018 return AddNode::Ideal(phase, can_reshape);
1019 }
1020
1021 //------------------------------add_ring---------------------------------------
1022 // Supplied function returns the sum of the inputs IN THE CURRENT RING. For
1023 // the logical operations the ring's ADD is really a logical OR function.
1024 // This also type-checks the inputs for sanity. Guaranteed never to
1025 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
1026 const Type *OrINode::add_ring( const Type *t0, const Type *t1 ) const {
1027 const TypeInt *r0 = t0->is_int(); // Handy access
1028 const TypeInt *r1 = t1->is_int();
1029
1030 // If both args are bool, can figure out better types
1031 if ( r0 == TypeInt::BOOL ) {
1032 if ( r1 == TypeInt::ONE) {
1033 return TypeInt::ONE;
1034 } else if ( r1 == TypeInt::BOOL ) {
1035 return TypeInt::BOOL;
1036 }
1037 } else if ( r0 == TypeInt::ONE ) {
1038 if ( r1 == TypeInt::BOOL ) {
1039 return TypeInt::ONE;
1040 }
1041 }
1042
1043 // If either input is all ones, the output is all ones.
1044 // x | ~0 == ~0 <==> x | -1 == -1
1045 if (r0 == TypeInt::MINUS_1 || r1 == TypeInt::MINUS_1) {
1046 return TypeInt::MINUS_1;
1047 }
1048
1049 // If either input is not a constant, just return all integers.
1050 if( !r0->is_con() || !r1->is_con() )
1051 return TypeInt::INT; // Any integer, but still no symbols.
1052
1053 // Otherwise just OR them bits.
1054 return TypeInt::make( r0->get_con() | r1->get_con() );
1055 }
1056
1057 //=============================================================================
1058 //------------------------------Identity---------------------------------------
1059 Node* OrLNode::Identity(PhaseGVN* phase) {
1060 // x | x => x
1061 if (in(1) == in(2)) {
1062 return in(1);
1063 }
1064
1065 return AddNode::Identity(phase);
1066 }
1067
1068 Node* OrLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
1069 int lopcode = in(1)->Opcode();
1070 int ropcode = in(2)->Opcode();
1071 if (Matcher::match_rule_supported(Op_RotateLeft) &&
1072 lopcode == Op_LShiftL && ropcode == Op_URShiftL && in(1)->in(1) == in(2)->in(1)) {
1073 Node* lshift = in(1)->in(2);
1074 Node* rshift = in(2)->in(2);
1075 Node* shift = rotate_shift(phase, lshift, rshift, 0x3F);
1076 if (shift != nullptr) {
1077 return new RotateLeftNode(in(1)->in(1), shift, TypeLong::LONG);
1078 }
1079 return nullptr;
1080 }
1081 if (Matcher::match_rule_supported(Op_RotateRight) &&
1082 lopcode == Op_URShiftL && ropcode == Op_LShiftL && in(1)->in(1) == in(2)->in(1)) {
1083 Node* rshift = in(1)->in(2);
1084 Node* lshift = in(2)->in(2);
1085 Node* shift = rotate_shift(phase, rshift, lshift, 0x3F);
1086 if (shift != nullptr) {
1087 return new RotateRightNode(in(1)->in(1), shift, TypeLong::LONG);
1088 }
1089 }
1090
1091 // Convert "~a | ~b" into "~(a & b)"
1092 if (AddNode::is_not(phase, in(1), T_LONG) && AddNode::is_not(phase, in(2), T_LONG)) {
1093 Node* and_a_b = new AndLNode(in(1)->in(1), in(2)->in(1));
1094 Node* tn = phase->transform(and_a_b);
1095 return AddNode::make_not(phase, tn, T_LONG);
1096 }
1097
1098 return AddNode::Ideal(phase, can_reshape);
1099 }
1100
1101 //------------------------------add_ring---------------------------------------
1102 const Type *OrLNode::add_ring( const Type *t0, const Type *t1 ) const {
1103 const TypeLong *r0 = t0->is_long(); // Handy access
1104 const TypeLong *r1 = t1->is_long();
1105
1106 // If either input is all ones, the output is all ones.
1107 // x | ~0 == ~0 <==> x | -1 == -1
1108 if (r0 == TypeLong::MINUS_1 || r1 == TypeLong::MINUS_1) {
1109 return TypeLong::MINUS_1;
1110 }
1111
1112 // If either input is not a constant, just return all integers.
1113 if( !r0->is_con() || !r1->is_con() )
1114 return TypeLong::LONG; // Any integer, but still no symbols.
1115
1116 // Otherwise just OR them bits.
1117 return TypeLong::make( r0->get_con() | r1->get_con() );
1118 }
1119
1120 //---------------------------Helper -------------------------------------------
1121 /* Decide if the given node is used only in arithmetic expressions(add or sub).
1122 */
1123 static bool is_used_in_only_arithmetic(Node* n, BasicType bt) {
1124 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
1125 Node* u = n->fast_out(i);
1126 if (u->Opcode() != Op_Add(bt) && u->Opcode() != Op_Sub(bt)) {
1127 return false;
1128 }
1129 }
1130 return true;
1131 }
1132
1133 //=============================================================================
1134 //------------------------------Idealize---------------------------------------
1135 Node* XorINode::Ideal(PhaseGVN* phase, bool can_reshape) {
1136 Node* in1 = in(1);
1137 Node* in2 = in(2);
1138
1139 // Convert ~x into -1-x when ~x is used in an arithmetic expression
1140 // or x itself is an expression.
1141 if (phase->type(in2) == TypeInt::MINUS_1) { // follows LHS^(-1), i.e., ~LHS
1142 if (phase->is_IterGVN()) {
1143 if (is_used_in_only_arithmetic(this, T_INT)
1144 // LHS is arithmetic
1145 || (in1->Opcode() == Op_AddI || in1->Opcode() == Op_SubI)) {
1146 return new SubINode(in2, in1);
1147 }
1148 } else {
1149 // graph could be incomplete in GVN so we postpone to IGVN
1150 phase->record_for_igvn(this);
1151 }
1152 }
1153
1154 // Propagate xor through constant cmoves. This pattern can occur after expansion of Conv2B nodes.
1155 const TypeInt* in2_type = phase->type(in2)->isa_int();
1156 if (in1->Opcode() == Op_CMoveI && in2_type != nullptr && in2_type->is_con()) {
1157 int in2_val = in2_type->get_con();
1158
1159 // Get types of both sides of the CMove
1160 const TypeInt* left = phase->type(in1->in(CMoveNode::IfFalse))->isa_int();
1161 const TypeInt* right = phase->type(in1->in(CMoveNode::IfTrue))->isa_int();
1162
1163 // Ensure that both sides are int constants
1164 if (left != nullptr && right != nullptr && left->is_con() && right->is_con()) {
1165 Node* cond = in1->in(CMoveNode::Condition);
1166
1167 // Check that the comparison is a bool and that the cmp node type is correct
1168 if (cond->is_Bool()) {
1169 int cmp_op = cond->in(1)->Opcode();
1170
1171 if (cmp_op == Op_CmpI || cmp_op == Op_CmpP) {
1172 int l_val = left->get_con();
1173 int r_val = right->get_con();
1174
1175 return new CMoveINode(cond, phase->intcon(l_val ^ in2_val), phase->intcon(r_val ^ in2_val), TypeInt::INT);
1176 }
1177 }
1178 }
1179 }
1180
1181 return AddNode::Ideal(phase, can_reshape);
1182 }
1183
1184 const Type* XorINode::Value(PhaseGVN* phase) const {
1185 Node* in1 = in(1);
1186 Node* in2 = in(2);
1187 const Type* t1 = phase->type(in1);
1188 const Type* t2 = phase->type(in2);
1189 if (t1 == Type::TOP || t2 == Type::TOP) {
1190 return Type::TOP;
1191 }
1192 // x ^ x ==> 0
1193 if (in1->eqv_uncast(in2)) {
1194 return add_id();
1195 }
1196 return AddNode::Value(phase);
1197 }
1198
1199 //------------------------------add_ring---------------------------------------
1200 // Supplied function returns the sum of the inputs IN THE CURRENT RING. For
1201 // the logical operations the ring's ADD is really a logical OR function.
1202 // This also type-checks the inputs for sanity. Guaranteed never to
1203 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
1204 const Type *XorINode::add_ring( const Type *t0, const Type *t1 ) const {
1205 const TypeInt *r0 = t0->is_int(); // Handy access
1206 const TypeInt *r1 = t1->is_int();
1207
1208 if (r0->is_con() && r1->is_con()) {
1209 // compute constant result
1210 return TypeInt::make(r0->get_con() ^ r1->get_con());
1211 }
1212
1213 // At least one of the arguments is not constant
1214
1215 if (r0->_lo >= 0 && r1->_lo >= 0) {
1216 // Combine [r0->_lo, r0->_hi] ^ [r0->_lo, r1->_hi] -> [0, upper_bound]
1217 jint upper_bound = xor_upper_bound_for_ranges<jint, juint>(r0->_hi, r1->_hi);
1218 return TypeInt::make(0, upper_bound, MAX2(r0->_widen, r1->_widen));
1219 }
1220
1221 return TypeInt::INT;
1222 }
1223
1224 //=============================================================================
1225 //------------------------------add_ring---------------------------------------
1226 const Type *XorLNode::add_ring( const Type *t0, const Type *t1 ) const {
1227 const TypeLong *r0 = t0->is_long(); // Handy access
1228 const TypeLong *r1 = t1->is_long();
1229
1230 if (r0->is_con() && r1->is_con()) {
1231 // compute constant result
1232 return TypeLong::make(r0->get_con() ^ r1->get_con());
1233 }
1234
1235 // At least one of the arguments is not constant
1236
1237 if (r0->_lo >= 0 && r1->_lo >= 0) {
1238 // Combine [r0->_lo, r0->_hi] ^ [r0->_lo, r1->_hi] -> [0, upper_bound]
1239 julong upper_bound = xor_upper_bound_for_ranges<jlong, julong>(r0->_hi, r1->_hi);
1240 return TypeLong::make(0, upper_bound, MAX2(r0->_widen, r1->_widen));
1241 }
1242
1243 return TypeLong::LONG;
1244 }
1245
1246 Node* XorLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
1247 Node* in1 = in(1);
1248 Node* in2 = in(2);
1249
1250 // Convert ~x into -1-x when ~x is used in an arithmetic expression
1251 // or x itself is an arithmetic expression.
1252 if (phase->type(in2) == TypeLong::MINUS_1) { // follows LHS^(-1), i.e., ~LHS
1253 if (phase->is_IterGVN()) {
1254 if (is_used_in_only_arithmetic(this, T_LONG)
1255 // LHS is arithmetic
1256 || (in1->Opcode() == Op_AddL || in1->Opcode() == Op_SubL)) {
1257 return new SubLNode(in2, in1);
1258 }
1259 } else {
1260 // graph could be incomplete in GVN so we postpone to IGVN
1261 phase->record_for_igvn(this);
1262 }
1263 }
1264 return AddNode::Ideal(phase, can_reshape);
1265 }
1266
1267 const Type* XorLNode::Value(PhaseGVN* phase) const {
1268 Node* in1 = in(1);
1269 Node* in2 = in(2);
1270 const Type* t1 = phase->type(in1);
1271 const Type* t2 = phase->type(in2);
1272 if (t1 == Type::TOP || t2 == Type::TOP) {
1273 return Type::TOP;
1274 }
1275 // x ^ x ==> 0
1276 if (in1->eqv_uncast(in2)) {
1277 return add_id();
1278 }
1279
1280 return AddNode::Value(phase);
1281 }
1282
1283 Node* MaxNode::build_min_max_int(Node* a, Node* b, bool is_max) {
1284 if (is_max) {
1285 return new MaxINode(a, b);
1286 } else {
1287 return new MinINode(a, b);
1288 }
1289 }
1290
1291 Node* MaxNode::build_min_max_long(PhaseGVN* phase, Node* a, Node* b, bool is_max) {
1292 if (is_max) {
1293 return new MaxLNode(phase->C, a, b);
1294 } else {
1295 return new MinLNode(phase->C, a, b);
1296 }
1297 }
1298
1299 Node* MaxNode::build_min_max(Node* a, Node* b, bool is_max, bool is_unsigned, const Type* t, PhaseGVN& gvn) {
1300 bool is_int = gvn.type(a)->isa_int();
1301 assert(is_int || gvn.type(a)->isa_long(), "int or long inputs");
1302 assert(is_int == (gvn.type(b)->isa_int() != nullptr), "inconsistent inputs");
1303 BasicType bt = is_int ? T_INT: T_LONG;
1304 Node* hook = nullptr;
1305 if (gvn.is_IterGVN()) {
1306 // Make sure a and b are not destroyed
1307 hook = new Node(2);
1308 hook->init_req(0, a);
1309 hook->init_req(1, b);
1310 }
1311 Node* res = nullptr;
1312 if (is_int && !is_unsigned) {
1313 res = gvn.transform(build_min_max_int(a, b, is_max));
1314 assert(gvn.type(res)->is_int()->_lo >= t->is_int()->_lo && gvn.type(res)->is_int()->_hi <= t->is_int()->_hi, "type doesn't match");
1315 } else {
1316 Node* cmp = nullptr;
1317 if (is_max) {
1318 cmp = gvn.transform(CmpNode::make(a, b, bt, is_unsigned));
1319 } else {
1320 cmp = gvn.transform(CmpNode::make(b, a, bt, is_unsigned));
1321 }
1322 Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1323 res = gvn.transform(CMoveNode::make(bol, a, b, t));
1324 }
1325 if (hook != nullptr) {
1326 hook->destruct(&gvn);
1327 }
1328 return res;
1329 }
1330
1331 Node* MaxNode::build_min_max_diff_with_zero(Node* a, Node* b, bool is_max, const Type* t, PhaseGVN& gvn) {
1332 bool is_int = gvn.type(a)->isa_int();
1333 assert(is_int || gvn.type(a)->isa_long(), "int or long inputs");
1334 assert(is_int == (gvn.type(b)->isa_int() != nullptr), "inconsistent inputs");
1335 BasicType bt = is_int ? T_INT: T_LONG;
1336 Node* zero = gvn.integercon(0, bt);
1337 Node* hook = nullptr;
1338 if (gvn.is_IterGVN()) {
1339 // Make sure a and b are not destroyed
1340 hook = new Node(2);
1341 hook->init_req(0, a);
1342 hook->init_req(1, b);
1343 }
1344 Node* cmp = nullptr;
1345 if (is_max) {
1346 cmp = gvn.transform(CmpNode::make(a, b, bt, false));
1347 } else {
1348 cmp = gvn.transform(CmpNode::make(b, a, bt, false));
1349 }
1350 Node* sub = gvn.transform(SubNode::make(a, b, bt));
1351 Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1352 Node* res = gvn.transform(CMoveNode::make(bol, sub, zero, t));
1353 if (hook != nullptr) {
1354 hook->destruct(&gvn);
1355 }
1356 return res;
1357 }
1358
1359 // Check if addition of an integer with type 't' and a constant 'c' can overflow.
1360 static bool can_overflow(const TypeInt* t, jint c) {
1361 jint t_lo = t->_lo;
1362 jint t_hi = t->_hi;
1363 return ((c < 0 && (java_add(t_lo, c) > t_lo)) ||
1364 (c > 0 && (java_add(t_hi, c) < t_hi)));
1365 }
1366
1367 // Check if addition of a long with type 't' and a constant 'c' can overflow.
1368 static bool can_overflow(const TypeLong* t, jlong c) {
1369 jlong t_lo = t->_lo;
1370 jlong t_hi = t->_hi;
1371 return ((c < 0 && (java_add(t_lo, c) > t_lo)) ||
1372 (c > 0 && (java_add(t_hi, c) < t_hi)));
1373 }
1374
1375 // Let <x, x_off> = x_operands and <y, y_off> = y_operands.
1376 // If x == y and neither add(x, x_off) nor add(y, y_off) overflow, return
1377 // add(x, op(x_off, y_off)). Otherwise, return nullptr.
1378 Node* MaxNode::extract_add(PhaseGVN* phase, ConstAddOperands x_operands, ConstAddOperands y_operands) {
1379 Node* x = x_operands.first;
1380 Node* y = y_operands.first;
1381 int opcode = Opcode();
1382 assert(opcode == Op_MaxI || opcode == Op_MinI, "Unexpected opcode");
1383 const TypeInt* tx = phase->type(x)->isa_int();
1384 jint x_off = x_operands.second;
1385 jint y_off = y_operands.second;
1386 if (x == y && tx != nullptr &&
1387 !can_overflow(tx, x_off) &&
1388 !can_overflow(tx, y_off)) {
1389 jint c = opcode == Op_MinI ? MIN2(x_off, y_off) : MAX2(x_off, y_off);
1390 return new AddINode(x, phase->intcon(c));
1391 }
1392 return nullptr;
1393 }
1394
1395 // Try to cast n as an integer addition with a constant. Return:
1396 // <x, C>, if n == add(x, C), where 'C' is a non-TOP constant;
1397 // <nullptr, 0>, if n == add(x, C), where 'C' is a TOP constant; or
1398 // <n, 0>, otherwise.
1399 static ConstAddOperands as_add_with_constant(Node* n) {
1400 if (n->Opcode() != Op_AddI) {
1401 return ConstAddOperands(n, 0);
1402 }
1403 Node* x = n->in(1);
1404 Node* c = n->in(2);
1405 if (!c->is_Con()) {
1406 return ConstAddOperands(n, 0);
1407 }
1408 const Type* c_type = c->bottom_type();
1409 if (c_type == Type::TOP) {
1410 return ConstAddOperands(nullptr, 0);
1411 }
1412 return ConstAddOperands(x, c_type->is_int()->get_con());
1413 }
1414
1415 Node* MaxNode::IdealI(PhaseGVN* phase, bool can_reshape) {
1416 int opcode = Opcode();
1417 assert(opcode == Op_MinI || opcode == Op_MaxI, "Unexpected opcode");
1418 // Try to transform the following pattern, in any of its four possible
1419 // permutations induced by op's commutativity:
1420 // op(op(add(inner, inner_off), inner_other), add(outer, outer_off))
1421 // into
1422 // op(add(inner, op(inner_off, outer_off)), inner_other),
1423 // where:
1424 // op is either MinI or MaxI, and
1425 // inner == outer, and
1426 // the additions cannot overflow.
1427 for (uint inner_op_index = 1; inner_op_index <= 2; inner_op_index++) {
1428 if (in(inner_op_index)->Opcode() != opcode) {
1429 continue;
1430 }
1431 Node* outer_add = in(inner_op_index == 1 ? 2 : 1);
1432 ConstAddOperands outer_add_operands = as_add_with_constant(outer_add);
1433 if (outer_add_operands.first == nullptr) {
1434 return nullptr; // outer_add has a TOP input, no need to continue.
1435 }
1436 // One operand is a MinI/MaxI and the other is an integer addition with
1437 // constant. Test the operands of the inner MinI/MaxI.
1438 for (uint inner_add_index = 1; inner_add_index <= 2; inner_add_index++) {
1439 Node* inner_op = in(inner_op_index);
1440 Node* inner_add = inner_op->in(inner_add_index);
1441 ConstAddOperands inner_add_operands = as_add_with_constant(inner_add);
1442 if (inner_add_operands.first == nullptr) {
1443 return nullptr; // inner_add has a TOP input, no need to continue.
1444 }
1445 // Try to extract the inner add.
1446 Node* add_extracted = extract_add(phase, inner_add_operands, outer_add_operands);
1447 if (add_extracted == nullptr) {
1448 continue;
1449 }
1450 Node* add_transformed = phase->transform(add_extracted);
1451 Node* inner_other = inner_op->in(inner_add_index == 1 ? 2 : 1);
1452 return build_min_max_int(add_transformed, inner_other, opcode == Op_MaxI);
1453 }
1454 }
1455 // Try to transform
1456 // op(add(x, x_off), add(y, y_off))
1457 // into
1458 // add(x, op(x_off, y_off)),
1459 // where:
1460 // op is either MinI or MaxI, and
1461 // inner == outer, and
1462 // the additions cannot overflow.
1463 ConstAddOperands xC = as_add_with_constant(in(1));
1464 ConstAddOperands yC = as_add_with_constant(in(2));
1465 if (xC.first == nullptr || yC.first == nullptr) return nullptr;
1466 return extract_add(phase, xC, yC);
1467 }
1468
1469 // Ideal transformations for MaxINode
1470 Node* MaxINode::Ideal(PhaseGVN* phase, bool can_reshape) {
1471 return IdealI(phase, can_reshape);
1472 }
1473
1474 Node* MaxINode::Identity(PhaseGVN* phase) {
1475 const TypeInt* t1 = phase->type(in(1))->is_int();
1476 const TypeInt* t2 = phase->type(in(2))->is_int();
1477
1478 // Can we determine the maximum statically?
1479 if (t1->_lo >= t2->_hi) {
1480 return in(1);
1481 } else if (t2->_lo >= t1->_hi) {
1482 return in(2);
1483 }
1484
1485 return MaxNode::Identity(phase);
1486 }
1487
1488 //=============================================================================
1489 //------------------------------add_ring---------------------------------------
1490 // Supplied function returns the sum of the inputs.
1491 const Type *MaxINode::add_ring( const Type *t0, const Type *t1 ) const {
1492 const TypeInt *r0 = t0->is_int(); // Handy access
1493 const TypeInt *r1 = t1->is_int();
1494
1495 // Otherwise just MAX them bits.
1496 return TypeInt::make( MAX2(r0->_lo,r1->_lo), MAX2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
1497 }
1498
1499 //=============================================================================
1500 //------------------------------Idealize---------------------------------------
1501 // MINs show up in range-check loop limit calculations. Look for
1502 // "MIN2(x+c0,MIN2(y,x+c1))". Pick the smaller constant: "MIN2(x+c0,y)"
1503 Node* MinINode::Ideal(PhaseGVN* phase, bool can_reshape) {
1504 return IdealI(phase, can_reshape);
1505 }
1506
1507 Node* MinINode::Identity(PhaseGVN* phase) {
1508 const TypeInt* t1 = phase->type(in(1))->is_int();
1509 const TypeInt* t2 = phase->type(in(2))->is_int();
1510
1511 // Can we determine the minimum statically?
1512 if (t1->_lo >= t2->_hi) {
1513 return in(2);
1514 } else if (t2->_lo >= t1->_hi) {
1515 return in(1);
1516 }
1517
1518 return MaxNode::Identity(phase);
1519 }
1520
1521 //------------------------------add_ring---------------------------------------
1522 // Supplied function returns the sum of the inputs.
1523 const Type *MinINode::add_ring( const Type *t0, const Type *t1 ) const {
1524 const TypeInt *r0 = t0->is_int(); // Handy access
1525 const TypeInt *r1 = t1->is_int();
1526
1527 // Otherwise just MIN them bits.
1528 return TypeInt::make( MIN2(r0->_lo,r1->_lo), MIN2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
1529 }
1530
1531 // Collapse the "addition with overflow-protection" pattern, and the symmetrical
1532 // "subtraction with underflow-protection" pattern. These are created during the
1533 // unrolling, when we have to adjust the limit by subtracting the stride, but want
1534 // to protect against underflow: MaxL(SubL(limit, stride), min_jint).
1535 // If we have more than one of those in a sequence:
1536 //
1537 // x con2
1538 // | |
1539 // AddL clamp2
1540 // | |
1541 // Max/MinL con1
1542 // | |
1543 // AddL clamp1
1544 // | |
1545 // Max/MinL (n)
1546 //
1547 // We want to collapse it to:
1548 //
1549 // x con1 con2
1550 // | | |
1551 // | AddLNode (new_con)
1552 // | |
1553 // AddLNode clamp1
1554 // | |
1555 // Max/MinL (n)
1556 //
1557 // Note: we assume that SubL was already replaced by an AddL, and that the stride
1558 // has its sign flipped: SubL(limit, stride) -> AddL(limit, -stride).
1559 //
1560 // Proof MaxL collapsed version equivalent to original (MinL version similar):
1561 // is_sub_con ensures that con1, con2 ∈ [min_int, 0[
1562 //
1563 // Original:
1564 // - AddL2 underflow => x + con2 ∈ ]max_long - min_int, max_long], ALWAYS BAILOUT as x + con1 + con2 surely fails can_overflow (*)
1565 // - AddL2 no underflow => x + con2 ∈ [min_long, max_long]
1566 // - MaxL2 clamp => min_int
1567 // - AddL1 underflow: NOT POSSIBLE: cannot underflow since min_int + con1 ∈ [2 * min_int, min_int] always > min_long
1568 // - AddL1 no underflow => min_int + con1 ∈ [2 * min_int, min_int]
1569 // - MaxL1 clamp => min_int (RESULT 1)
1570 // - MaxL1 no clamp: NOT POSSIBLE: min_int + con1 ∈ [2 * min_int, min_int] always <= min_int, so clamp always taken
1571 // - MaxL2 no clamp => x + con2 ∈ [min_int, max_long]
1572 // - AddL1 underflow: NOT POSSIBLE: cannot underflow since x + con2 + con1 ∈ [2 * min_int, max_long] always > min_long
1573 // - AddL1 no underflow => x + con2 + con1 ∈ [2 * min_int, max_long]
1574 // - MaxL1 clamp => min_int (RESULT 2)
1575 // - MaxL1 no clamp => x + con2 + con1 ∈ ]min_int, max_long] (RESULT 3)
1576 //
1577 // Collapsed:
1578 // - AddL2 (cannot underflow) => con2 + con1 ∈ [2 * min_int, 0]
1579 // - AddL1 underflow: NOT POSSIBLE: would have bailed out at can_overflow (*)
1580 // - AddL1 no underflow => x + con2 + con1 ∈ [min_long, max_long]
1581 // - MaxL clamp => min_int (RESULT 1 and RESULT 2)
1582 // - MaxL no clamp => x + con2 + con1 ∈ ]min_int, max_long] (RESULT 3)
1583 //
1584 static Node* fold_subI_no_underflow_pattern(Node* n, PhaseGVN* phase) {
1585 assert(n->Opcode() == Op_MaxL || n->Opcode() == Op_MinL, "sanity");
1586 // Check that the two clamps have the correct values.
1587 jlong clamp = (n->Opcode() == Op_MaxL) ? min_jint : max_jint;
1588 auto is_clamp = [&](Node* c) {
1589 const TypeLong* t = phase->type(c)->isa_long();
1590 return t != nullptr && t->is_con() &&
1591 t->get_con() == clamp;
1592 };
1593 // Check that the constants are negative if MaxL, and positive if MinL.
1594 auto is_sub_con = [&](Node* c) {
1595 const TypeLong* t = phase->type(c)->isa_long();
1596 return t != nullptr && t->is_con() &&
1597 t->get_con() < max_jint && t->get_con() > min_jint &&
1598 (t->get_con() < 0) == (n->Opcode() == Op_MaxL);
1599 };
1600 // Verify the graph level by level:
1601 Node* add1 = n->in(1);
1602 Node* clamp1 = n->in(2);
1603 if (add1->Opcode() == Op_AddL && is_clamp(clamp1)) {
1604 Node* max2 = add1->in(1);
1605 Node* con1 = add1->in(2);
1606 if (max2->Opcode() == n->Opcode() && is_sub_con(con1)) {
1607 Node* add2 = max2->in(1);
1608 Node* clamp2 = max2->in(2);
1609 if (add2->Opcode() == Op_AddL && is_clamp(clamp2)) {
1610 Node* x = add2->in(1);
1611 Node* con2 = add2->in(2);
1612 if (is_sub_con(con2)) {
1613 // Collapsed graph not equivalent if potential over/underflow -> bailing out (*)
1614 if (can_overflow(phase->type(x)->is_long(), con1->get_long() + con2->get_long())) {
1615 return nullptr;
1616 }
1617 Node* new_con = phase->transform(new AddLNode(con1, con2));
1618 Node* new_sub = phase->transform(new AddLNode(x, new_con));
1619 n->set_req_X(1, new_sub, phase);
1620 return n;
1621 }
1622 }
1623 }
1624 }
1625 return nullptr;
1626 }
1627
1628 const Type* MaxLNode::add_ring(const Type* t0, const Type* t1) const {
1629 const TypeLong* r0 = t0->is_long();
1630 const TypeLong* r1 = t1->is_long();
1631
1632 return TypeLong::make(MAX2(r0->_lo, r1->_lo), MAX2(r0->_hi, r1->_hi), MAX2(r0->_widen, r1->_widen));
1633 }
1634
1635 Node* MaxLNode::Identity(PhaseGVN* phase) {
1636 const TypeLong* t1 = phase->type(in(1))->is_long();
1637 const TypeLong* t2 = phase->type(in(2))->is_long();
1638
1639 // Can we determine maximum statically?
1640 if (t1->_lo >= t2->_hi) {
1641 return in(1);
1642 } else if (t2->_lo >= t1->_hi) {
1643 return in(2);
1644 }
1645
1646 return MaxNode::Identity(phase);
1647 }
1648
1649 Node* MaxLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
1650 Node* n = AddNode::Ideal(phase, can_reshape);
1651 if (n != nullptr) {
1652 return n;
1653 }
1654 if (can_reshape) {
1655 return fold_subI_no_underflow_pattern(this, phase);
1656 }
1657 return nullptr;
1658 }
1659
1660 const Type* MinLNode::add_ring(const Type* t0, const Type* t1) const {
1661 const TypeLong* r0 = t0->is_long();
1662 const TypeLong* r1 = t1->is_long();
1663
1664 return TypeLong::make(MIN2(r0->_lo, r1->_lo), MIN2(r0->_hi, r1->_hi), MAX2(r0->_widen, r1->_widen));
1665 }
1666
1667 Node* MinLNode::Identity(PhaseGVN* phase) {
1668 const TypeLong* t1 = phase->type(in(1))->is_long();
1669 const TypeLong* t2 = phase->type(in(2))->is_long();
1670
1671 // Can we determine minimum statically?
1672 if (t1->_lo >= t2->_hi) {
1673 return in(2);
1674 } else if (t2->_lo >= t1->_hi) {
1675 return in(1);
1676 }
1677
1678 return MaxNode::Identity(phase);
1679 }
1680
1681 Node* MinLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
1682 Node* n = AddNode::Ideal(phase, can_reshape);
1683 if (n != nullptr) {
1684 return n;
1685 }
1686 if (can_reshape) {
1687 return fold_subI_no_underflow_pattern(this, phase);
1688 }
1689 return nullptr;
1690 }
1691
1692 int MaxNode::opposite_opcode() const {
1693 if (Opcode() == max_opcode()) {
1694 return min_opcode();
1695 } else {
1696 assert(Opcode() == min_opcode(), "Caller should be either %s or %s, but is %s", NodeClassNames[max_opcode()], NodeClassNames[min_opcode()], NodeClassNames[Opcode()]);
1697 return max_opcode();
1698 }
1699 }
1700
1701 // Given a redundant structure such as Max/Min(A, Max/Min(B, C)) where A == B or A == C, return the useful part of the structure.
1702 // 'operation' is the node expected to be the inner 'Max/Min(B, C)', and 'operand' is the node expected to be the 'A' operand of the outer node.
1703 Node* MaxNode::find_identity_operation(Node* operation, Node* operand) {
1704 if (operation->Opcode() == Opcode() || operation->Opcode() == opposite_opcode()) {
1705 Node* n1 = operation->in(1);
1706 Node* n2 = operation->in(2);
1707
1708 // Given Op(A, Op(B, C)), see if either A == B or A == C is true.
1709 if (n1 == operand || n2 == operand) {
1710 // If the operations are the same return the inner operation, as Max(A, Max(A, B)) == Max(A, B).
1711 if (operation->Opcode() == Opcode()) {
1712 return operation;
1713 }
1714
1715 // If the operations are different return the operand 'A', as Max(A, Min(A, B)) == A if the value isn't floating point.
1716 // With floating point values, the identity doesn't hold if B == NaN.
1717 const Type* type = bottom_type();
1718 if (type->isa_int() || type->isa_long()) {
1719 return operand;
1720 }
1721 }
1722 }
1723
1724 return nullptr;
1725 }
1726
1727 Node* MaxNode::Identity(PhaseGVN* phase) {
1728 if (in(1) == in(2)) {
1729 return in(1);
1730 }
1731
1732 Node* identity_1 = MaxNode::find_identity_operation(in(2), in(1));
1733 if (identity_1 != nullptr) {
1734 return identity_1;
1735 }
1736
1737 Node* identity_2 = MaxNode::find_identity_operation(in(1), in(2));
1738 if (identity_2 != nullptr) {
1739 return identity_2;
1740 }
1741
1742 return AddNode::Identity(phase);
1743 }
1744
1745 //------------------------------add_ring---------------------------------------
1746 const Type* MinHFNode::add_ring(const Type* t0, const Type* t1) const {
1747 const TypeH* r0 = t0->isa_half_float_constant();
1748 const TypeH* r1 = t1->isa_half_float_constant();
1749 if (r0 == nullptr || r1 == nullptr) {
1750 return bottom_type();
1751 }
1752
1753 if (r0->is_nan()) {
1754 return r0;
1755 }
1756 if (r1->is_nan()) {
1757 return r1;
1758 }
1759
1760 float f0 = r0->getf();
1761 float f1 = r1->getf();
1762 if (f0 != 0.0f || f1 != 0.0f) {
1763 return f0 < f1 ? r0 : r1;
1764 }
1765
1766 // As per IEEE 754 specification, floating point comparison consider +ve and -ve
1767 // zeros as equals. Thus, performing signed integral comparison for min value
1768 // detection.
1769 return (jint_cast(f0) < jint_cast(f1)) ? r0 : r1;
1770 }
1771
1772 //------------------------------add_ring---------------------------------------
1773 const Type* MinFNode::add_ring(const Type* t0, const Type* t1 ) const {
1774 const TypeF* r0 = t0->isa_float_constant();
1775 const TypeF* r1 = t1->isa_float_constant();
1776 if (r0 == nullptr || r1 == nullptr) {
1777 return bottom_type();
1778 }
1779
1780 if (r0->is_nan()) {
1781 return r0;
1782 }
1783 if (r1->is_nan()) {
1784 return r1;
1785 }
1786
1787 float f0 = r0->getf();
1788 float f1 = r1->getf();
1789 if (f0 != 0.0f || f1 != 0.0f) {
1790 return f0 < f1 ? r0 : r1;
1791 }
1792
1793 // handle min of 0.0, -0.0 case.
1794 return (jint_cast(f0) < jint_cast(f1)) ? r0 : r1;
1795 }
1796
1797 //------------------------------add_ring---------------------------------------
1798 const Type* MinDNode::add_ring(const Type* t0, const Type* t1) const {
1799 const TypeD* r0 = t0->isa_double_constant();
1800 const TypeD* r1 = t1->isa_double_constant();
1801 if (r0 == nullptr || r1 == nullptr) {
1802 return bottom_type();
1803 }
1804
1805 if (r0->is_nan()) {
1806 return r0;
1807 }
1808 if (r1->is_nan()) {
1809 return r1;
1810 }
1811
1812 double d0 = r0->getd();
1813 double d1 = r1->getd();
1814 if (d0 != 0.0 || d1 != 0.0) {
1815 return d0 < d1 ? r0 : r1;
1816 }
1817
1818 // handle min of 0.0, -0.0 case.
1819 return (jlong_cast(d0) < jlong_cast(d1)) ? r0 : r1;
1820 }
1821
1822 //------------------------------add_ring---------------------------------------
1823 const Type* MaxHFNode::add_ring(const Type* t0, const Type* t1) const {
1824 const TypeH* r0 = t0->isa_half_float_constant();
1825 const TypeH* r1 = t1->isa_half_float_constant();
1826 if (r0 == nullptr || r1 == nullptr) {
1827 return bottom_type();
1828 }
1829
1830 if (r0->is_nan()) {
1831 return r0;
1832 }
1833 if (r1->is_nan()) {
1834 return r1;
1835 }
1836
1837 float f0 = r0->getf();
1838 float f1 = r1->getf();
1839 if (f0 != 0.0f || f1 != 0.0f) {
1840 return f0 > f1 ? r0 : r1;
1841 }
1842
1843 // As per IEEE 754 specification, floating point comparison consider +ve and -ve
1844 // zeros as equals. Thus, performing signed integral comparison for max value
1845 // detection.
1846 return (jint_cast(f0) > jint_cast(f1)) ? r0 : r1;
1847 }
1848
1849
1850 //------------------------------add_ring---------------------------------------
1851 const Type* MaxFNode::add_ring(const Type* t0, const Type* t1) const {
1852 const TypeF* r0 = t0->isa_float_constant();
1853 const TypeF* r1 = t1->isa_float_constant();
1854 if (r0 == nullptr || r1 == nullptr) {
1855 return bottom_type();
1856 }
1857
1858 if (r0->is_nan()) {
1859 return r0;
1860 }
1861 if (r1->is_nan()) {
1862 return r1;
1863 }
1864
1865 float f0 = r0->getf();
1866 float f1 = r1->getf();
1867 if (f0 != 0.0f || f1 != 0.0f) {
1868 return f0 > f1 ? r0 : r1;
1869 }
1870
1871 // handle max of 0.0,-0.0 case.
1872 return (jint_cast(f0) > jint_cast(f1)) ? r0 : r1;
1873 }
1874
1875 //------------------------------add_ring---------------------------------------
1876 const Type* MaxDNode::add_ring(const Type* t0, const Type* t1) const {
1877 const TypeD* r0 = t0->isa_double_constant();
1878 const TypeD* r1 = t1->isa_double_constant();
1879 if (r0 == nullptr || r1 == nullptr) {
1880 return bottom_type();
1881 }
1882
1883 if (r0->is_nan()) {
1884 return r0;
1885 }
1886 if (r1->is_nan()) {
1887 return r1;
1888 }
1889
1890 double d0 = r0->getd();
1891 double d1 = r1->getd();
1892 if (d0 != 0.0 || d1 != 0.0) {
1893 return d0 > d1 ? r0 : r1;
1894 }
1895
1896 // handle max of 0.0, -0.0 case.
1897 return (jlong_cast(d0) > jlong_cast(d1)) ? r0 : r1;
1898 }