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 return AddNode::Ideal(phase, can_reshape);
400 }
401
402
403 Node* AddINode::Ideal(PhaseGVN* phase, bool can_reshape) {
404 Node* in1 = in(1);
405 Node* in2 = in(2);
406 int op1 = in1->Opcode();
407 int op2 = in2->Opcode();
408
409 // Convert (x>>>z)+y into (x+(y<<z))>>>z for small constant z and y.
410 // Helps with array allocation math constant folding
411 // See 4790063:
412 // Unrestricted transformation is unsafe for some runtime values of 'x'
413 // ( x == 0, z == 1, y == -1 ) fails
414 // ( x == -5, z == 1, y == 1 ) fails
415 // Transform works for small z and small negative y when the addition
416 // (x + (y << z)) does not cross zero.
417 // Implement support for negative y and (x >= -(y << z))
418 // Have not observed cases where type information exists to support
419 // positive y and (x <= -(y << z))
420 if (op1 == Op_URShiftI && op2 == Op_ConI &&
421 in1->in(2)->Opcode() == Op_ConI) {
422 jint z = phase->type(in1->in(2))->is_int()->get_con() & 0x1f; // only least significant 5 bits matter
423 jint y = phase->type(in2)->is_int()->get_con();
424
425 if (z < 5 && -5 < y && y < 0) {
426 const Type* t_in11 = phase->type(in1->in(1));
427 if( t_in11 != Type::TOP && (t_in11->is_int()->_lo >= -(y << z))) {
428 Node* a = phase->transform(new AddINode( in1->in(1), phase->intcon(y<<z)));
429 return new URShiftINode(a, in1->in(2));
430 }
431 }
432 }
433
434 return AddNode::IdealIL(phase, can_reshape, T_INT);
435 }
436
437
438 //------------------------------Identity---------------------------------------
439 // Fold (x-y)+y OR y+(x-y) into x
440 Node* AddINode::Identity(PhaseGVN* phase) {
441 if (in(1)->Opcode() == Op_SubI && in(1)->in(2) == in(2)) {
442 return in(1)->in(1);
443 } else if (in(2)->Opcode() == Op_SubI && in(2)->in(2) == in(1)) {
444 return in(2)->in(1);
445 }
446 return AddNode::Identity(phase);
447 }
448
449
450 //------------------------------add_ring---------------------------------------
451 // Supplied function returns the sum of the inputs. Guaranteed never
452 // to be passed a TOP or BOTTOM type, these are filtered out by
453 // pre-check.
454 const Type *AddINode::add_ring( const Type *t0, const Type *t1 ) const {
455 const TypeInt *r0 = t0->is_int(); // Handy access
456 const TypeInt *r1 = t1->is_int();
457 int lo = java_add(r0->_lo, r1->_lo);
458 int hi = java_add(r0->_hi, r1->_hi);
459 if( !(r0->is_con() && r1->is_con()) ) {
460 // Not both constants, compute approximate result
461 if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {
462 lo = min_jint; hi = max_jint; // Underflow on the low side
463 }
464 if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {
465 lo = min_jint; hi = max_jint; // Overflow on the high side
466 }
467 if( lo > hi ) { // Handle overflow
468 lo = min_jint; hi = max_jint;
469 }
470 } else {
471 // both constants, compute precise result using 'lo' and 'hi'
472 // Semantics define overflow and underflow for integer addition
473 // as expected. In particular: 0x80000000 + 0x80000000 --> 0x0
474 }
475 return TypeInt::make( lo, hi, MAX2(r0->_widen,r1->_widen) );
476 }
477
478
479 //=============================================================================
480 //------------------------------Idealize---------------------------------------
481 Node* AddLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
482 return AddNode::IdealIL(phase, can_reshape, T_LONG);
483 }
484
485
486 //------------------------------Identity---------------------------------------
487 // Fold (x-y)+y OR y+(x-y) into x
488 Node* AddLNode::Identity(PhaseGVN* phase) {
489 if (in(1)->Opcode() == Op_SubL && in(1)->in(2) == in(2)) {
490 return in(1)->in(1);
491 } else if (in(2)->Opcode() == Op_SubL && in(2)->in(2) == in(1)) {
492 return in(2)->in(1);
493 }
494 return AddNode::Identity(phase);
495 }
496
497
498 //------------------------------add_ring---------------------------------------
499 // Supplied function returns the sum of the inputs. Guaranteed never
500 // to be passed a TOP or BOTTOM type, these are filtered out by
501 // pre-check.
502 const Type *AddLNode::add_ring( const Type *t0, const Type *t1 ) const {
503 const TypeLong *r0 = t0->is_long(); // Handy access
504 const TypeLong *r1 = t1->is_long();
505 jlong lo = java_add(r0->_lo, r1->_lo);
506 jlong hi = java_add(r0->_hi, r1->_hi);
507 if( !(r0->is_con() && r1->is_con()) ) {
508 // Not both constants, compute approximate result
509 if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {
510 lo =min_jlong; hi = max_jlong; // Underflow on the low side
511 }
512 if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {
513 lo = min_jlong; hi = max_jlong; // Overflow on the high side
514 }
515 if( lo > hi ) { // Handle overflow
516 lo = min_jlong; hi = max_jlong;
517 }
518 } else {
519 // both constants, compute precise result using 'lo' and 'hi'
520 // Semantics define overflow and underflow for integer addition
521 // as expected. In particular: 0x80000000 + 0x80000000 --> 0x0
522 }
523 return TypeLong::make( lo, hi, MAX2(r0->_widen,r1->_widen) );
524 }
525
526
527 //=============================================================================
528 //------------------------------add_of_identity--------------------------------
529 // Check for addition of the identity
530 const Type *AddFNode::add_of_identity( const Type *t1, const Type *t2 ) const {
531 // x ADD 0 should return x unless 'x' is a -zero
532 //
533 // const Type *zero = add_id(); // The additive identity
534 // jfloat f1 = t1->getf();
535 // jfloat f2 = t2->getf();
536 //
537 // if( t1->higher_equal( zero ) ) return t2;
538 // if( t2->higher_equal( zero ) ) return t1;
539
540 return nullptr;
541 }
542
543 //------------------------------add_ring---------------------------------------
544 // Supplied function returns the sum of the inputs.
545 // This also type-checks the inputs for sanity. Guaranteed never to
546 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
547 const Type *AddFNode::add_ring( const Type *t0, const Type *t1 ) const {
548 if (!t0->isa_float_constant() || !t1->isa_float_constant()) {
549 return bottom_type();
550 }
551 return TypeF::make( t0->getf() + t1->getf() );
552 }
553
554 //------------------------------Ideal------------------------------------------
555 Node *AddFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
556 // Floating point additions are not associative because of boundary conditions (infinity)
557 return commute(phase, this) ? this : nullptr;
558 }
559
560 //=============================================================================
561 //------------------------------add_of_identity--------------------------------
562 // Check for addition of the identity
563 const Type* AddHFNode::add_of_identity(const Type* t1, const Type* t2) const {
564 return nullptr;
565 }
566
567 // Supplied function returns the sum of the inputs.
568 // This also type-checks the inputs for sanity. Guaranteed never to
569 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
570 const Type* AddHFNode::add_ring(const Type* t0, const Type* t1) const {
571 if (!t0->isa_half_float_constant() || !t1->isa_half_float_constant()) {
572 return bottom_type();
573 }
574 return TypeH::make(t0->getf() + t1->getf());
575 }
576
577 //=============================================================================
578 //------------------------------add_of_identity--------------------------------
579 // Check for addition of the identity
580 const Type *AddDNode::add_of_identity( const Type *t1, const Type *t2 ) const {
581 // x ADD 0 should return x unless 'x' is a -zero
582 //
583 // const Type *zero = add_id(); // The additive identity
584 // jfloat f1 = t1->getf();
585 // jfloat f2 = t2->getf();
586 //
587 // if( t1->higher_equal( zero ) ) return t2;
588 // if( t2->higher_equal( zero ) ) return t1;
589
590 return nullptr;
591 }
592 //------------------------------add_ring---------------------------------------
593 // Supplied function returns the sum of the inputs.
594 // This also type-checks the inputs for sanity. Guaranteed never to
595 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
596 const Type *AddDNode::add_ring( const Type *t0, const Type *t1 ) const {
597 if (!t0->isa_double_constant() || !t1->isa_double_constant()) {
598 return bottom_type();
599 }
600 return TypeD::make( t0->getd() + t1->getd() );
601 }
602
603 //------------------------------Ideal------------------------------------------
604 Node *AddDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
605 // Floating point additions are not associative because of boundary conditions (infinity)
606 return commute(phase, this) ? this : nullptr;
607 }
608
609
610 //=============================================================================
611 //------------------------------Identity---------------------------------------
612 // If one input is a constant 0, return the other input.
613 Node* AddPNode::Identity(PhaseGVN* phase) {
614 return ( phase->type( in(Offset) )->higher_equal( TypeX_ZERO ) ) ? in(Address) : this;
615 }
616
617 //------------------------------Idealize---------------------------------------
618 Node *AddPNode::Ideal(PhaseGVN *phase, bool can_reshape) {
619 // Bail out if dead inputs
620 if( phase->type( in(Address) ) == Type::TOP ) return nullptr;
621
622 // If the left input is an add of a constant, flatten the expression tree.
623 const Node *n = in(Address);
624 if (n->is_AddP() && n->in(Base) == in(Base)) {
625 const AddPNode *addp = n->as_AddP(); // Left input is an AddP
626 assert( !addp->in(Address)->is_AddP() ||
627 addp->in(Address)->as_AddP() != addp,
628 "dead loop in AddPNode::Ideal" );
629 // Type of left input's right input
630 const Type *t = phase->type( addp->in(Offset) );
631 if( t == Type::TOP ) return nullptr;
632 const TypeX *t12 = t->is_intptr_t();
633 if( t12->is_con() ) { // Left input is an add of a constant?
634 // If the right input is a constant, combine constants
635 const Type *temp_t2 = phase->type( in(Offset) );
636 if( temp_t2 == Type::TOP ) return nullptr;
637 const TypeX *t2 = temp_t2->is_intptr_t();
638 Node* address;
639 Node* offset;
640 if( t2->is_con() ) {
641 // The Add of the flattened expression
642 address = addp->in(Address);
643 offset = phase->MakeConX(t2->get_con() + t12->get_con());
644 } else {
645 // Else move the constant to the right. ((A+con)+B) into ((A+B)+con)
646 address = phase->transform(new AddPNode(in(Base),addp->in(Address),in(Offset)));
647 offset = addp->in(Offset);
648 }
649 set_req_X(Address, address, phase);
650 set_req_X(Offset, offset, phase);
651 return this;
652 }
653 }
654
655 // Raw pointers?
656 if( in(Base)->bottom_type() == Type::TOP ) {
657 // If this is a null+long form (from unsafe accesses), switch to a rawptr.
658 if (phase->type(in(Address)) == TypePtr::NULL_PTR) {
659 Node* offset = in(Offset);
660 return new CastX2PNode(offset);
661 }
662 }
663
664 // If the right is an add of a constant, push the offset down.
665 // Convert: (ptr + (offset+con)) into (ptr+offset)+con.
666 // The idea is to merge array_base+scaled_index groups together,
667 // and only have different constant offsets from the same base.
668 const Node *add = in(Offset);
669 if( add->Opcode() == Op_AddX && add->in(1) != add ) {
670 const Type *t22 = phase->type( add->in(2) );
671 if( t22->singleton() && (t22 != Type::TOP) ) { // Right input is an add of a constant?
672 set_req(Address, phase->transform(new AddPNode(in(Base),in(Address),add->in(1))));
673 set_req_X(Offset, add->in(2), phase); // puts add on igvn worklist if needed
674 return this; // Made progress
675 }
676 }
677
678 return nullptr; // No progress
679 }
680
681 //------------------------------bottom_type------------------------------------
682 // Bottom-type is the pointer-type with unknown offset.
683 const Type *AddPNode::bottom_type() const {
684 if (in(Address) == nullptr) return TypePtr::BOTTOM;
685 const TypePtr *tp = in(Address)->bottom_type()->isa_ptr();
686 if( !tp ) return Type::TOP; // TOP input means TOP output
687 assert( in(Offset)->Opcode() != Op_ConP, "" );
688 const Type *t = in(Offset)->bottom_type();
689 if( t == Type::TOP )
690 return tp->add_offset(Type::OffsetTop);
691 const TypeX *tx = t->is_intptr_t();
692 intptr_t txoffset = Type::OffsetBot;
693 if (tx->is_con()) { // Left input is an add of a constant?
694 txoffset = tx->get_con();
695 }
696 if (tp->isa_aryptr()) {
697 // In the case of a flat inline type array, each field has its
698 // own slice so we need to extract the field being accessed from
699 // the address computation
700 return tp->is_aryptr()->add_field_offset_and_offset(txoffset);
701 }
702 return tp->add_offset(txoffset);
703 }
704
705 //------------------------------Value------------------------------------------
706 const Type* AddPNode::Value(PhaseGVN* phase) const {
707 // Either input is TOP ==> the result is TOP
708 const Type *t1 = phase->type( in(Address) );
709 const Type *t2 = phase->type( in(Offset) );
710 if( t1 == Type::TOP ) return Type::TOP;
711 if( t2 == Type::TOP ) return Type::TOP;
712
713 // Left input is a pointer
714 const TypePtr *p1 = t1->isa_ptr();
715 // Right input is an int
716 const TypeX *p2 = t2->is_intptr_t();
717 // Add 'em
718 intptr_t p2offset = Type::OffsetBot;
719 if (p2->is_con()) { // Left input is an add of a constant?
720 p2offset = p2->get_con();
721 }
722 if (p1->isa_aryptr()) {
723 // In the case of a flat inline type array, each field has its
724 // own slice so we need to extract the field being accessed from
725 // the address computation
726 return p1->is_aryptr()->add_field_offset_and_offset(p2offset);
727 }
728 return p1->add_offset(p2offset);
729 }
730
731 //------------------------Ideal_base_and_offset--------------------------------
732 // Split an oop pointer into a base and offset.
733 // (The offset might be Type::OffsetBot in the case of an array.)
734 // Return the base, or null if failure.
735 Node* AddPNode::Ideal_base_and_offset(Node* ptr, PhaseValues* phase,
736 // second return value:
737 intptr_t& offset) {
738 if (ptr->is_AddP()) {
739 Node* base = ptr->in(AddPNode::Base);
740 Node* addr = ptr->in(AddPNode::Address);
741 Node* offs = ptr->in(AddPNode::Offset);
742 if (base == addr || base->is_top()) {
743 offset = phase->find_intptr_t_con(offs, Type::OffsetBot);
744 if (offset != Type::OffsetBot) {
745 return addr;
746 }
747 }
748 }
749 offset = Type::OffsetBot;
750 return nullptr;
751 }
752
753 //------------------------------unpack_offsets----------------------------------
754 // Collect the AddP offset values into the elements array, giving up
755 // if there are more than length.
756 int AddPNode::unpack_offsets(Node* elements[], int length) const {
757 int count = 0;
758 Node const* addr = this;
759 Node* base = addr->in(AddPNode::Base);
760 while (addr->is_AddP()) {
761 if (addr->in(AddPNode::Base) != base) {
762 // give up
763 return -1;
764 }
765 elements[count++] = addr->in(AddPNode::Offset);
766 if (count == length) {
767 // give up
768 return -1;
769 }
770 addr = addr->in(AddPNode::Address);
771 }
772 if (addr != base) {
773 return -1;
774 }
775 return count;
776 }
777
778 //------------------------------match_edge-------------------------------------
779 // Do we Match on this edge index or not? Do not match base pointer edge
780 uint AddPNode::match_edge(uint idx) const {
781 return idx > Base;
782 }
783
784 //=============================================================================
785 //------------------------------Identity---------------------------------------
786 Node* OrINode::Identity(PhaseGVN* phase) {
787 // x | x => x
788 if (in(1) == in(2)) {
789 return in(1);
790 }
791
792 return AddNode::Identity(phase);
793 }
794
795 // Find shift value for Integer or Long OR.
796 static Node* rotate_shift(PhaseGVN* phase, Node* lshift, Node* rshift, int mask) {
797 // val << norm_con_shift | val >> ({32|64} - norm_con_shift) => rotate_left val, norm_con_shift
798 const TypeInt* lshift_t = phase->type(lshift)->isa_int();
799 const TypeInt* rshift_t = phase->type(rshift)->isa_int();
800 if (lshift_t != nullptr && lshift_t->is_con() &&
801 rshift_t != nullptr && rshift_t->is_con() &&
802 ((lshift_t->get_con() & mask) == ((mask + 1) - (rshift_t->get_con() & mask)))) {
803 return phase->intcon(lshift_t->get_con() & mask);
804 }
805 // val << var_shift | val >> ({0|32|64} - var_shift) => rotate_left val, var_shift
806 if (rshift->Opcode() == Op_SubI && rshift->in(2) == lshift && rshift->in(1)->is_Con()){
807 const TypeInt* shift_t = phase->type(rshift->in(1))->isa_int();
808 if (shift_t != nullptr && shift_t->is_con() &&
809 (shift_t->get_con() == 0 || shift_t->get_con() == (mask + 1))) {
810 return lshift;
811 }
812 }
813 return nullptr;
814 }
815
816 Node* OrINode::Ideal(PhaseGVN* phase, bool can_reshape) {
817 int lopcode = in(1)->Opcode();
818 int ropcode = in(2)->Opcode();
819 if (Matcher::match_rule_supported(Op_RotateLeft) &&
820 lopcode == Op_LShiftI && ropcode == Op_URShiftI && in(1)->in(1) == in(2)->in(1)) {
821 Node* lshift = in(1)->in(2);
822 Node* rshift = in(2)->in(2);
823 Node* shift = rotate_shift(phase, lshift, rshift, 0x1F);
824 if (shift != nullptr) {
825 return new RotateLeftNode(in(1)->in(1), shift, TypeInt::INT);
826 }
827 return nullptr;
828 }
829 if (Matcher::match_rule_supported(Op_RotateRight) &&
830 lopcode == Op_URShiftI && ropcode == Op_LShiftI && in(1)->in(1) == in(2)->in(1)) {
831 Node* rshift = in(1)->in(2);
832 Node* lshift = in(2)->in(2);
833 Node* shift = rotate_shift(phase, rshift, lshift, 0x1F);
834 if (shift != nullptr) {
835 return new RotateRightNode(in(1)->in(1), shift, TypeInt::INT);
836 }
837 }
838
839 // Convert "~a | ~b" into "~(a & b)"
840 if (AddNode::is_not(phase, in(1), T_INT) && AddNode::is_not(phase, in(2), T_INT)) {
841 Node* and_a_b = new AndINode(in(1)->in(1), in(2)->in(1));
842 Node* tn = phase->transform(and_a_b);
843 return AddNode::make_not(phase, tn, T_INT);
844 }
845 return AddNode::Ideal(phase, can_reshape);
846 }
847
848 //------------------------------add_ring---------------------------------------
849 // Supplied function returns the sum of the inputs IN THE CURRENT RING. For
850 // the logical operations the ring's ADD is really a logical OR function.
851 // This also type-checks the inputs for sanity. Guaranteed never to
852 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
853 const Type *OrINode::add_ring( const Type *t0, const Type *t1 ) const {
854 const TypeInt *r0 = t0->is_int(); // Handy access
855 const TypeInt *r1 = t1->is_int();
856
857 // If both args are bool, can figure out better types
858 if ( r0 == TypeInt::BOOL ) {
859 if ( r1 == TypeInt::ONE) {
860 return TypeInt::ONE;
861 } else if ( r1 == TypeInt::BOOL ) {
862 return TypeInt::BOOL;
863 }
864 } else if ( r0 == TypeInt::ONE ) {
865 if ( r1 == TypeInt::BOOL ) {
866 return TypeInt::ONE;
867 }
868 }
869
870 // If either input is all ones, the output is all ones.
871 // x | ~0 == ~0 <==> x | -1 == -1
872 if (r0 == TypeInt::MINUS_1 || r1 == TypeInt::MINUS_1) {
873 return TypeInt::MINUS_1;
874 }
875
876 // If either input is not a constant, just return all integers.
877 if( !r0->is_con() || !r1->is_con() )
878 return TypeInt::INT; // Any integer, but still no symbols.
879
880 // Otherwise just OR them bits.
881 return TypeInt::make( r0->get_con() | r1->get_con() );
882 }
883
884 //=============================================================================
885 //------------------------------Identity---------------------------------------
886 Node* OrLNode::Identity(PhaseGVN* phase) {
887 // x | x => x
888 if (in(1) == in(2)) {
889 return in(1);
890 }
891
892 return AddNode::Identity(phase);
893 }
894
895 Node* OrLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
896 int lopcode = in(1)->Opcode();
897 int ropcode = in(2)->Opcode();
898 if (Matcher::match_rule_supported(Op_RotateLeft) &&
899 lopcode == Op_LShiftL && ropcode == Op_URShiftL && in(1)->in(1) == in(2)->in(1)) {
900 Node* lshift = in(1)->in(2);
901 Node* rshift = in(2)->in(2);
902 Node* shift = rotate_shift(phase, lshift, rshift, 0x3F);
903 if (shift != nullptr) {
904 return new RotateLeftNode(in(1)->in(1), shift, TypeLong::LONG);
905 }
906 return nullptr;
907 }
908 if (Matcher::match_rule_supported(Op_RotateRight) &&
909 lopcode == Op_URShiftL && ropcode == Op_LShiftL && in(1)->in(1) == in(2)->in(1)) {
910 Node* rshift = in(1)->in(2);
911 Node* lshift = in(2)->in(2);
912 Node* shift = rotate_shift(phase, rshift, lshift, 0x3F);
913 if (shift != nullptr) {
914 return new RotateRightNode(in(1)->in(1), shift, TypeLong::LONG);
915 }
916 }
917
918 // Convert "~a | ~b" into "~(a & b)"
919 if (AddNode::is_not(phase, in(1), T_LONG) && AddNode::is_not(phase, in(2), T_LONG)) {
920 Node* and_a_b = new AndLNode(in(1)->in(1), in(2)->in(1));
921 Node* tn = phase->transform(and_a_b);
922 return AddNode::make_not(phase, tn, T_LONG);
923 }
924
925 return AddNode::Ideal(phase, can_reshape);
926 }
927
928 //------------------------------add_ring---------------------------------------
929 const Type *OrLNode::add_ring( const Type *t0, const Type *t1 ) const {
930 const TypeLong *r0 = t0->is_long(); // Handy access
931 const TypeLong *r1 = t1->is_long();
932
933 // If either input is all ones, the output is all ones.
934 // x | ~0 == ~0 <==> x | -1 == -1
935 if (r0 == TypeLong::MINUS_1 || r1 == TypeLong::MINUS_1) {
936 return TypeLong::MINUS_1;
937 }
938
939 // If either input is not a constant, just return all integers.
940 if( !r0->is_con() || !r1->is_con() )
941 return TypeLong::LONG; // Any integer, but still no symbols.
942
943 // Otherwise just OR them bits.
944 return TypeLong::make( r0->get_con() | r1->get_con() );
945 }
946
947 //---------------------------Helper -------------------------------------------
948 /* Decide if the given node is used only in arithmetic expressions(add or sub).
949 */
950 static bool is_used_in_only_arithmetic(Node* n, BasicType bt) {
951 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
952 Node* u = n->fast_out(i);
953 if (u->Opcode() != Op_Add(bt) && u->Opcode() != Op_Sub(bt)) {
954 return false;
955 }
956 }
957 return true;
958 }
959
960 //=============================================================================
961 //------------------------------Idealize---------------------------------------
962 Node* XorINode::Ideal(PhaseGVN* phase, bool can_reshape) {
963 Node* in1 = in(1);
964 Node* in2 = in(2);
965
966 // Convert ~x into -1-x when ~x is used in an arithmetic expression
967 // or x itself is an expression.
968 if (phase->type(in2) == TypeInt::MINUS_1) { // follows LHS^(-1), i.e., ~LHS
969 if (phase->is_IterGVN()) {
970 if (is_used_in_only_arithmetic(this, T_INT)
971 // LHS is arithmetic
972 || (in1->Opcode() == Op_AddI || in1->Opcode() == Op_SubI)) {
973 return new SubINode(in2, in1);
974 }
975 } else {
976 // graph could be incomplete in GVN so we postpone to IGVN
977 phase->record_for_igvn(this);
978 }
979 }
980
981 // Propagate xor through constant cmoves. This pattern can occur after expansion of Conv2B nodes.
982 const TypeInt* in2_type = phase->type(in2)->isa_int();
983 if (in1->Opcode() == Op_CMoveI && in2_type != nullptr && in2_type->is_con()) {
984 int in2_val = in2_type->get_con();
985
986 // Get types of both sides of the CMove
987 const TypeInt* left = phase->type(in1->in(CMoveNode::IfFalse))->isa_int();
988 const TypeInt* right = phase->type(in1->in(CMoveNode::IfTrue))->isa_int();
989
990 // Ensure that both sides are int constants
991 if (left != nullptr && right != nullptr && left->is_con() && right->is_con()) {
992 Node* cond = in1->in(CMoveNode::Condition);
993
994 // Check that the comparison is a bool and that the cmp node type is correct
995 if (cond->is_Bool()) {
996 int cmp_op = cond->in(1)->Opcode();
997
998 if (cmp_op == Op_CmpI || cmp_op == Op_CmpP) {
999 int l_val = left->get_con();
1000 int r_val = right->get_con();
1001
1002 return new CMoveINode(cond, phase->intcon(l_val ^ in2_val), phase->intcon(r_val ^ in2_val), TypeInt::INT);
1003 }
1004 }
1005 }
1006 }
1007
1008 return AddNode::Ideal(phase, can_reshape);
1009 }
1010
1011 const Type* XorINode::Value(PhaseGVN* phase) const {
1012 Node* in1 = in(1);
1013 Node* in2 = in(2);
1014 const Type* t1 = phase->type(in1);
1015 const Type* t2 = phase->type(in2);
1016 if (t1 == Type::TOP || t2 == Type::TOP) {
1017 return Type::TOP;
1018 }
1019 // x ^ x ==> 0
1020 if (in1->eqv_uncast(in2)) {
1021 return add_id();
1022 }
1023 return AddNode::Value(phase);
1024 }
1025
1026 //------------------------------add_ring---------------------------------------
1027 // Supplied function returns the sum of the inputs IN THE CURRENT RING. For
1028 // the logical operations the ring's ADD is really a logical OR function.
1029 // This also type-checks the inputs for sanity. Guaranteed never to
1030 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
1031 const Type *XorINode::add_ring( const Type *t0, const Type *t1 ) const {
1032 const TypeInt *r0 = t0->is_int(); // Handy access
1033 const TypeInt *r1 = t1->is_int();
1034
1035 if (r0->is_con() && r1->is_con()) {
1036 // compute constant result
1037 return TypeInt::make(r0->get_con() ^ r1->get_con());
1038 }
1039
1040 // At least one of the arguments is not constant
1041
1042 if (r0->_lo >= 0 && r1->_lo >= 0) {
1043 // Combine [r0->_lo, r0->_hi] ^ [r0->_lo, r1->_hi] -> [0, upper_bound]
1044 jint upper_bound = xor_upper_bound_for_ranges<jint, juint>(r0->_hi, r1->_hi);
1045 return TypeInt::make(0, upper_bound, MAX2(r0->_widen, r1->_widen));
1046 }
1047
1048 return TypeInt::INT;
1049 }
1050
1051 //=============================================================================
1052 //------------------------------add_ring---------------------------------------
1053 const Type *XorLNode::add_ring( const Type *t0, const Type *t1 ) const {
1054 const TypeLong *r0 = t0->is_long(); // Handy access
1055 const TypeLong *r1 = t1->is_long();
1056
1057 if (r0->is_con() && r1->is_con()) {
1058 // compute constant result
1059 return TypeLong::make(r0->get_con() ^ r1->get_con());
1060 }
1061
1062 // At least one of the arguments is not constant
1063
1064 if (r0->_lo >= 0 && r1->_lo >= 0) {
1065 // Combine [r0->_lo, r0->_hi] ^ [r0->_lo, r1->_hi] -> [0, upper_bound]
1066 julong upper_bound = xor_upper_bound_for_ranges<jlong, julong>(r0->_hi, r1->_hi);
1067 return TypeLong::make(0, upper_bound, MAX2(r0->_widen, r1->_widen));
1068 }
1069
1070 return TypeLong::LONG;
1071 }
1072
1073 Node* XorLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
1074 Node* in1 = in(1);
1075 Node* in2 = in(2);
1076
1077 // Convert ~x into -1-x when ~x is used in an arithmetic expression
1078 // or x itself is an arithmetic expression.
1079 if (phase->type(in2) == TypeLong::MINUS_1) { // follows LHS^(-1), i.e., ~LHS
1080 if (phase->is_IterGVN()) {
1081 if (is_used_in_only_arithmetic(this, T_LONG)
1082 // LHS is arithmetic
1083 || (in1->Opcode() == Op_AddL || in1->Opcode() == Op_SubL)) {
1084 return new SubLNode(in2, in1);
1085 }
1086 } else {
1087 // graph could be incomplete in GVN so we postpone to IGVN
1088 phase->record_for_igvn(this);
1089 }
1090 }
1091 return AddNode::Ideal(phase, can_reshape);
1092 }
1093
1094 const Type* XorLNode::Value(PhaseGVN* phase) const {
1095 Node* in1 = in(1);
1096 Node* in2 = in(2);
1097 const Type* t1 = phase->type(in1);
1098 const Type* t2 = phase->type(in2);
1099 if (t1 == Type::TOP || t2 == Type::TOP) {
1100 return Type::TOP;
1101 }
1102 // x ^ x ==> 0
1103 if (in1->eqv_uncast(in2)) {
1104 return add_id();
1105 }
1106
1107 return AddNode::Value(phase);
1108 }
1109
1110 Node* MaxNode::build_min_max_int(Node* a, Node* b, bool is_max) {
1111 if (is_max) {
1112 return new MaxINode(a, b);
1113 } else {
1114 return new MinINode(a, b);
1115 }
1116 }
1117
1118 Node* MaxNode::build_min_max_long(PhaseGVN* phase, Node* a, Node* b, bool is_max) {
1119 if (is_max) {
1120 return new MaxLNode(phase->C, a, b);
1121 } else {
1122 return new MinLNode(phase->C, a, b);
1123 }
1124 }
1125
1126 Node* MaxNode::build_min_max(Node* a, Node* b, bool is_max, bool is_unsigned, const Type* t, PhaseGVN& gvn) {
1127 bool is_int = gvn.type(a)->isa_int();
1128 assert(is_int || gvn.type(a)->isa_long(), "int or long inputs");
1129 assert(is_int == (gvn.type(b)->isa_int() != nullptr), "inconsistent inputs");
1130 BasicType bt = is_int ? T_INT: T_LONG;
1131 Node* hook = nullptr;
1132 if (gvn.is_IterGVN()) {
1133 // Make sure a and b are not destroyed
1134 hook = new Node(2);
1135 hook->init_req(0, a);
1136 hook->init_req(1, b);
1137 }
1138 Node* res = nullptr;
1139 if (is_int && !is_unsigned) {
1140 res = gvn.transform(build_min_max_int(a, b, is_max));
1141 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");
1142 } else {
1143 Node* cmp = nullptr;
1144 if (is_max) {
1145 cmp = gvn.transform(CmpNode::make(a, b, bt, is_unsigned));
1146 } else {
1147 cmp = gvn.transform(CmpNode::make(b, a, bt, is_unsigned));
1148 }
1149 Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1150 res = gvn.transform(CMoveNode::make(bol, a, b, t));
1151 }
1152 if (hook != nullptr) {
1153 hook->destruct(&gvn);
1154 }
1155 return res;
1156 }
1157
1158 Node* MaxNode::build_min_max_diff_with_zero(Node* a, Node* b, bool is_max, const Type* t, PhaseGVN& gvn) {
1159 bool is_int = gvn.type(a)->isa_int();
1160 assert(is_int || gvn.type(a)->isa_long(), "int or long inputs");
1161 assert(is_int == (gvn.type(b)->isa_int() != nullptr), "inconsistent inputs");
1162 BasicType bt = is_int ? T_INT: T_LONG;
1163 Node* zero = gvn.integercon(0, bt);
1164 Node* hook = nullptr;
1165 if (gvn.is_IterGVN()) {
1166 // Make sure a and b are not destroyed
1167 hook = new Node(2);
1168 hook->init_req(0, a);
1169 hook->init_req(1, b);
1170 }
1171 Node* cmp = nullptr;
1172 if (is_max) {
1173 cmp = gvn.transform(CmpNode::make(a, b, bt, false));
1174 } else {
1175 cmp = gvn.transform(CmpNode::make(b, a, bt, false));
1176 }
1177 Node* sub = gvn.transform(SubNode::make(a, b, bt));
1178 Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1179 Node* res = gvn.transform(CMoveNode::make(bol, sub, zero, t));
1180 if (hook != nullptr) {
1181 hook->destruct(&gvn);
1182 }
1183 return res;
1184 }
1185
1186 // Check if addition of an integer with type 't' and a constant 'c' can overflow.
1187 static bool can_overflow(const TypeInt* t, jint c) {
1188 jint t_lo = t->_lo;
1189 jint t_hi = t->_hi;
1190 return ((c < 0 && (java_add(t_lo, c) > t_lo)) ||
1191 (c > 0 && (java_add(t_hi, c) < t_hi)));
1192 }
1193
1194 // Check if addition of a long with type 't' and a constant 'c' can overflow.
1195 static bool can_overflow(const TypeLong* t, jlong c) {
1196 jlong t_lo = t->_lo;
1197 jlong t_hi = t->_hi;
1198 return ((c < 0 && (java_add(t_lo, c) > t_lo)) ||
1199 (c > 0 && (java_add(t_hi, c) < t_hi)));
1200 }
1201
1202 // Let <x, x_off> = x_operands and <y, y_off> = y_operands.
1203 // If x == y and neither add(x, x_off) nor add(y, y_off) overflow, return
1204 // add(x, op(x_off, y_off)). Otherwise, return nullptr.
1205 Node* MaxNode::extract_add(PhaseGVN* phase, ConstAddOperands x_operands, ConstAddOperands y_operands) {
1206 Node* x = x_operands.first;
1207 Node* y = y_operands.first;
1208 int opcode = Opcode();
1209 assert(opcode == Op_MaxI || opcode == Op_MinI, "Unexpected opcode");
1210 const TypeInt* tx = phase->type(x)->isa_int();
1211 jint x_off = x_operands.second;
1212 jint y_off = y_operands.second;
1213 if (x == y && tx != nullptr &&
1214 !can_overflow(tx, x_off) &&
1215 !can_overflow(tx, y_off)) {
1216 jint c = opcode == Op_MinI ? MIN2(x_off, y_off) : MAX2(x_off, y_off);
1217 return new AddINode(x, phase->intcon(c));
1218 }
1219 return nullptr;
1220 }
1221
1222 // Try to cast n as an integer addition with a constant. Return:
1223 // <x, C>, if n == add(x, C), where 'C' is a non-TOP constant;
1224 // <nullptr, 0>, if n == add(x, C), where 'C' is a TOP constant; or
1225 // <n, 0>, otherwise.
1226 static ConstAddOperands as_add_with_constant(Node* n) {
1227 if (n->Opcode() != Op_AddI) {
1228 return ConstAddOperands(n, 0);
1229 }
1230 Node* x = n->in(1);
1231 Node* c = n->in(2);
1232 if (!c->is_Con()) {
1233 return ConstAddOperands(n, 0);
1234 }
1235 const Type* c_type = c->bottom_type();
1236 if (c_type == Type::TOP) {
1237 return ConstAddOperands(nullptr, 0);
1238 }
1239 return ConstAddOperands(x, c_type->is_int()->get_con());
1240 }
1241
1242 Node* MaxNode::IdealI(PhaseGVN* phase, bool can_reshape) {
1243 int opcode = Opcode();
1244 assert(opcode == Op_MinI || opcode == Op_MaxI, "Unexpected opcode");
1245 // Try to transform the following pattern, in any of its four possible
1246 // permutations induced by op's commutativity:
1247 // op(op(add(inner, inner_off), inner_other), add(outer, outer_off))
1248 // into
1249 // op(add(inner, op(inner_off, outer_off)), inner_other),
1250 // where:
1251 // op is either MinI or MaxI, and
1252 // inner == outer, and
1253 // the additions cannot overflow.
1254 for (uint inner_op_index = 1; inner_op_index <= 2; inner_op_index++) {
1255 if (in(inner_op_index)->Opcode() != opcode) {
1256 continue;
1257 }
1258 Node* outer_add = in(inner_op_index == 1 ? 2 : 1);
1259 ConstAddOperands outer_add_operands = as_add_with_constant(outer_add);
1260 if (outer_add_operands.first == nullptr) {
1261 return nullptr; // outer_add has a TOP input, no need to continue.
1262 }
1263 // One operand is a MinI/MaxI and the other is an integer addition with
1264 // constant. Test the operands of the inner MinI/MaxI.
1265 for (uint inner_add_index = 1; inner_add_index <= 2; inner_add_index++) {
1266 Node* inner_op = in(inner_op_index);
1267 Node* inner_add = inner_op->in(inner_add_index);
1268 ConstAddOperands inner_add_operands = as_add_with_constant(inner_add);
1269 if (inner_add_operands.first == nullptr) {
1270 return nullptr; // inner_add has a TOP input, no need to continue.
1271 }
1272 // Try to extract the inner add.
1273 Node* add_extracted = extract_add(phase, inner_add_operands, outer_add_operands);
1274 if (add_extracted == nullptr) {
1275 continue;
1276 }
1277 Node* add_transformed = phase->transform(add_extracted);
1278 Node* inner_other = inner_op->in(inner_add_index == 1 ? 2 : 1);
1279 return build_min_max_int(add_transformed, inner_other, opcode == Op_MaxI);
1280 }
1281 }
1282 // Try to transform
1283 // op(add(x, x_off), add(y, y_off))
1284 // into
1285 // add(x, op(x_off, y_off)),
1286 // where:
1287 // op is either MinI or MaxI, and
1288 // inner == outer, and
1289 // the additions cannot overflow.
1290 ConstAddOperands xC = as_add_with_constant(in(1));
1291 ConstAddOperands yC = as_add_with_constant(in(2));
1292 if (xC.first == nullptr || yC.first == nullptr) return nullptr;
1293 return extract_add(phase, xC, yC);
1294 }
1295
1296 // Ideal transformations for MaxINode
1297 Node* MaxINode::Ideal(PhaseGVN* phase, bool can_reshape) {
1298 return IdealI(phase, can_reshape);
1299 }
1300
1301 Node* MaxINode::Identity(PhaseGVN* phase) {
1302 const TypeInt* t1 = phase->type(in(1))->is_int();
1303 const TypeInt* t2 = phase->type(in(2))->is_int();
1304
1305 // Can we determine the maximum statically?
1306 if (t1->_lo >= t2->_hi) {
1307 return in(1);
1308 } else if (t2->_lo >= t1->_hi) {
1309 return in(2);
1310 }
1311
1312 return MaxNode::Identity(phase);
1313 }
1314
1315 //=============================================================================
1316 //------------------------------add_ring---------------------------------------
1317 // Supplied function returns the sum of the inputs.
1318 const Type *MaxINode::add_ring( const Type *t0, const Type *t1 ) const {
1319 const TypeInt *r0 = t0->is_int(); // Handy access
1320 const TypeInt *r1 = t1->is_int();
1321
1322 // Otherwise just MAX them bits.
1323 return TypeInt::make( MAX2(r0->_lo,r1->_lo), MAX2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
1324 }
1325
1326 //=============================================================================
1327 //------------------------------Idealize---------------------------------------
1328 // MINs show up in range-check loop limit calculations. Look for
1329 // "MIN2(x+c0,MIN2(y,x+c1))". Pick the smaller constant: "MIN2(x+c0,y)"
1330 Node* MinINode::Ideal(PhaseGVN* phase, bool can_reshape) {
1331 return IdealI(phase, can_reshape);
1332 }
1333
1334 Node* MinINode::Identity(PhaseGVN* phase) {
1335 const TypeInt* t1 = phase->type(in(1))->is_int();
1336 const TypeInt* t2 = phase->type(in(2))->is_int();
1337
1338 // Can we determine the minimum statically?
1339 if (t1->_lo >= t2->_hi) {
1340 return in(2);
1341 } else if (t2->_lo >= t1->_hi) {
1342 return in(1);
1343 }
1344
1345 return MaxNode::Identity(phase);
1346 }
1347
1348 //------------------------------add_ring---------------------------------------
1349 // Supplied function returns the sum of the inputs.
1350 const Type *MinINode::add_ring( const Type *t0, const Type *t1 ) const {
1351 const TypeInt *r0 = t0->is_int(); // Handy access
1352 const TypeInt *r1 = t1->is_int();
1353
1354 // Otherwise just MIN them bits.
1355 return TypeInt::make( MIN2(r0->_lo,r1->_lo), MIN2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
1356 }
1357
1358 // Collapse the "addition with overflow-protection" pattern, and the symmetrical
1359 // "subtraction with underflow-protection" pattern. These are created during the
1360 // unrolling, when we have to adjust the limit by subtracting the stride, but want
1361 // to protect against underflow: MaxL(SubL(limit, stride), min_jint).
1362 // If we have more than one of those in a sequence:
1363 //
1364 // x con2
1365 // | |
1366 // AddL clamp2
1367 // | |
1368 // Max/MinL con1
1369 // | |
1370 // AddL clamp1
1371 // | |
1372 // Max/MinL (n)
1373 //
1374 // We want to collapse it to:
1375 //
1376 // x con1 con2
1377 // | | |
1378 // | AddLNode (new_con)
1379 // | |
1380 // AddLNode clamp1
1381 // | |
1382 // Max/MinL (n)
1383 //
1384 // Note: we assume that SubL was already replaced by an AddL, and that the stride
1385 // has its sign flipped: SubL(limit, stride) -> AddL(limit, -stride).
1386 //
1387 // Proof MaxL collapsed version equivalent to original (MinL version similar):
1388 // is_sub_con ensures that con1, con2 ∈ [min_int, 0[
1389 //
1390 // Original:
1391 // - AddL2 underflow => x + con2 ∈ ]max_long - min_int, max_long], ALWAYS BAILOUT as x + con1 + con2 surely fails can_overflow (*)
1392 // - AddL2 no underflow => x + con2 ∈ [min_long, max_long]
1393 // - MaxL2 clamp => min_int
1394 // - AddL1 underflow: NOT POSSIBLE: cannot underflow since min_int + con1 ∈ [2 * min_int, min_int] always > min_long
1395 // - AddL1 no underflow => min_int + con1 ∈ [2 * min_int, min_int]
1396 // - MaxL1 clamp => min_int (RESULT 1)
1397 // - MaxL1 no clamp: NOT POSSIBLE: min_int + con1 ∈ [2 * min_int, min_int] always <= min_int, so clamp always taken
1398 // - MaxL2 no clamp => x + con2 ∈ [min_int, max_long]
1399 // - AddL1 underflow: NOT POSSIBLE: cannot underflow since x + con2 + con1 ∈ [2 * min_int, max_long] always > min_long
1400 // - AddL1 no underflow => x + con2 + con1 ∈ [2 * min_int, max_long]
1401 // - MaxL1 clamp => min_int (RESULT 2)
1402 // - MaxL1 no clamp => x + con2 + con1 ∈ ]min_int, max_long] (RESULT 3)
1403 //
1404 // Collapsed:
1405 // - AddL2 (cannot underflow) => con2 + con1 ∈ [2 * min_int, 0]
1406 // - AddL1 underflow: NOT POSSIBLE: would have bailed out at can_overflow (*)
1407 // - AddL1 no underflow => x + con2 + con1 ∈ [min_long, max_long]
1408 // - MaxL clamp => min_int (RESULT 1 and RESULT 2)
1409 // - MaxL no clamp => x + con2 + con1 ∈ ]min_int, max_long] (RESULT 3)
1410 //
1411 static Node* fold_subI_no_underflow_pattern(Node* n, PhaseGVN* phase) {
1412 assert(n->Opcode() == Op_MaxL || n->Opcode() == Op_MinL, "sanity");
1413 // Check that the two clamps have the correct values.
1414 jlong clamp = (n->Opcode() == Op_MaxL) ? min_jint : max_jint;
1415 auto is_clamp = [&](Node* c) {
1416 const TypeLong* t = phase->type(c)->isa_long();
1417 return t != nullptr && t->is_con() &&
1418 t->get_con() == clamp;
1419 };
1420 // Check that the constants are negative if MaxL, and positive if MinL.
1421 auto is_sub_con = [&](Node* c) {
1422 const TypeLong* t = phase->type(c)->isa_long();
1423 return t != nullptr && t->is_con() &&
1424 t->get_con() < max_jint && t->get_con() > min_jint &&
1425 (t->get_con() < 0) == (n->Opcode() == Op_MaxL);
1426 };
1427 // Verify the graph level by level:
1428 Node* add1 = n->in(1);
1429 Node* clamp1 = n->in(2);
1430 if (add1->Opcode() == Op_AddL && is_clamp(clamp1)) {
1431 Node* max2 = add1->in(1);
1432 Node* con1 = add1->in(2);
1433 if (max2->Opcode() == n->Opcode() && is_sub_con(con1)) {
1434 Node* add2 = max2->in(1);
1435 Node* clamp2 = max2->in(2);
1436 if (add2->Opcode() == Op_AddL && is_clamp(clamp2)) {
1437 Node* x = add2->in(1);
1438 Node* con2 = add2->in(2);
1439 if (is_sub_con(con2)) {
1440 // Collapsed graph not equivalent if potential over/underflow -> bailing out (*)
1441 if (can_overflow(phase->type(x)->is_long(), con1->get_long() + con2->get_long())) {
1442 return nullptr;
1443 }
1444 Node* new_con = phase->transform(new AddLNode(con1, con2));
1445 Node* new_sub = phase->transform(new AddLNode(x, new_con));
1446 n->set_req_X(1, new_sub, phase);
1447 return n;
1448 }
1449 }
1450 }
1451 }
1452 return nullptr;
1453 }
1454
1455 const Type* MaxLNode::add_ring(const Type* t0, const Type* t1) const {
1456 const TypeLong* r0 = t0->is_long();
1457 const TypeLong* r1 = t1->is_long();
1458
1459 return TypeLong::make(MAX2(r0->_lo, r1->_lo), MAX2(r0->_hi, r1->_hi), MAX2(r0->_widen, r1->_widen));
1460 }
1461
1462 Node* MaxLNode::Identity(PhaseGVN* phase) {
1463 const TypeLong* t1 = phase->type(in(1))->is_long();
1464 const TypeLong* t2 = phase->type(in(2))->is_long();
1465
1466 // Can we determine maximum statically?
1467 if (t1->_lo >= t2->_hi) {
1468 return in(1);
1469 } else if (t2->_lo >= t1->_hi) {
1470 return in(2);
1471 }
1472
1473 return MaxNode::Identity(phase);
1474 }
1475
1476 Node* MaxLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
1477 Node* n = AddNode::Ideal(phase, can_reshape);
1478 if (n != nullptr) {
1479 return n;
1480 }
1481 if (can_reshape) {
1482 return fold_subI_no_underflow_pattern(this, phase);
1483 }
1484 return nullptr;
1485 }
1486
1487 const Type* MinLNode::add_ring(const Type* t0, const Type* t1) const {
1488 const TypeLong* r0 = t0->is_long();
1489 const TypeLong* r1 = t1->is_long();
1490
1491 return TypeLong::make(MIN2(r0->_lo, r1->_lo), MIN2(r0->_hi, r1->_hi), MAX2(r0->_widen, r1->_widen));
1492 }
1493
1494 Node* MinLNode::Identity(PhaseGVN* phase) {
1495 const TypeLong* t1 = phase->type(in(1))->is_long();
1496 const TypeLong* t2 = phase->type(in(2))->is_long();
1497
1498 // Can we determine minimum statically?
1499 if (t1->_lo >= t2->_hi) {
1500 return in(2);
1501 } else if (t2->_lo >= t1->_hi) {
1502 return in(1);
1503 }
1504
1505 return MaxNode::Identity(phase);
1506 }
1507
1508 Node* MinLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
1509 Node* n = AddNode::Ideal(phase, can_reshape);
1510 if (n != nullptr) {
1511 return n;
1512 }
1513 if (can_reshape) {
1514 return fold_subI_no_underflow_pattern(this, phase);
1515 }
1516 return nullptr;
1517 }
1518
1519 int MaxNode::opposite_opcode() const {
1520 if (Opcode() == max_opcode()) {
1521 return min_opcode();
1522 } else {
1523 assert(Opcode() == min_opcode(), "Caller should be either %s or %s, but is %s", NodeClassNames[max_opcode()], NodeClassNames[min_opcode()], NodeClassNames[Opcode()]);
1524 return max_opcode();
1525 }
1526 }
1527
1528 // 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.
1529 // '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.
1530 Node* MaxNode::find_identity_operation(Node* operation, Node* operand) {
1531 if (operation->Opcode() == Opcode() || operation->Opcode() == opposite_opcode()) {
1532 Node* n1 = operation->in(1);
1533 Node* n2 = operation->in(2);
1534
1535 // Given Op(A, Op(B, C)), see if either A == B or A == C is true.
1536 if (n1 == operand || n2 == operand) {
1537 // If the operations are the same return the inner operation, as Max(A, Max(A, B)) == Max(A, B).
1538 if (operation->Opcode() == Opcode()) {
1539 return operation;
1540 }
1541
1542 // If the operations are different return the operand 'A', as Max(A, Min(A, B)) == A if the value isn't floating point.
1543 // With floating point values, the identity doesn't hold if B == NaN.
1544 const Type* type = bottom_type();
1545 if (type->isa_int() || type->isa_long()) {
1546 return operand;
1547 }
1548 }
1549 }
1550
1551 return nullptr;
1552 }
1553
1554 Node* MaxNode::Identity(PhaseGVN* phase) {
1555 if (in(1) == in(2)) {
1556 return in(1);
1557 }
1558
1559 Node* identity_1 = MaxNode::find_identity_operation(in(2), in(1));
1560 if (identity_1 != nullptr) {
1561 return identity_1;
1562 }
1563
1564 Node* identity_2 = MaxNode::find_identity_operation(in(1), in(2));
1565 if (identity_2 != nullptr) {
1566 return identity_2;
1567 }
1568
1569 return AddNode::Identity(phase);
1570 }
1571
1572 //------------------------------add_ring---------------------------------------
1573 const Type* MinHFNode::add_ring(const Type* t0, const Type* t1) const {
1574 const TypeH* r0 = t0->isa_half_float_constant();
1575 const TypeH* r1 = t1->isa_half_float_constant();
1576 if (r0 == nullptr || r1 == nullptr) {
1577 return bottom_type();
1578 }
1579
1580 if (r0->is_nan()) {
1581 return r0;
1582 }
1583 if (r1->is_nan()) {
1584 return r1;
1585 }
1586
1587 float f0 = r0->getf();
1588 float f1 = r1->getf();
1589 if (f0 != 0.0f || f1 != 0.0f) {
1590 return f0 < f1 ? r0 : r1;
1591 }
1592
1593 // As per IEEE 754 specification, floating point comparison consider +ve and -ve
1594 // zeros as equals. Thus, performing signed integral comparison for min value
1595 // detection.
1596 return (jint_cast(f0) < jint_cast(f1)) ? r0 : r1;
1597 }
1598
1599 //------------------------------add_ring---------------------------------------
1600 const Type* MinFNode::add_ring(const Type* t0, const Type* t1 ) const {
1601 const TypeF* r0 = t0->isa_float_constant();
1602 const TypeF* r1 = t1->isa_float_constant();
1603 if (r0 == nullptr || r1 == nullptr) {
1604 return bottom_type();
1605 }
1606
1607 if (r0->is_nan()) {
1608 return r0;
1609 }
1610 if (r1->is_nan()) {
1611 return r1;
1612 }
1613
1614 float f0 = r0->getf();
1615 float f1 = r1->getf();
1616 if (f0 != 0.0f || f1 != 0.0f) {
1617 return f0 < f1 ? r0 : r1;
1618 }
1619
1620 // handle min of 0.0, -0.0 case.
1621 return (jint_cast(f0) < jint_cast(f1)) ? r0 : r1;
1622 }
1623
1624 //------------------------------add_ring---------------------------------------
1625 const Type* MinDNode::add_ring(const Type* t0, const Type* t1) const {
1626 const TypeD* r0 = t0->isa_double_constant();
1627 const TypeD* r1 = t1->isa_double_constant();
1628 if (r0 == nullptr || r1 == nullptr) {
1629 return bottom_type();
1630 }
1631
1632 if (r0->is_nan()) {
1633 return r0;
1634 }
1635 if (r1->is_nan()) {
1636 return r1;
1637 }
1638
1639 double d0 = r0->getd();
1640 double d1 = r1->getd();
1641 if (d0 != 0.0 || d1 != 0.0) {
1642 return d0 < d1 ? r0 : r1;
1643 }
1644
1645 // handle min of 0.0, -0.0 case.
1646 return (jlong_cast(d0) < jlong_cast(d1)) ? r0 : r1;
1647 }
1648
1649 //------------------------------add_ring---------------------------------------
1650 const Type* MaxHFNode::add_ring(const Type* t0, const Type* t1) const {
1651 const TypeH* r0 = t0->isa_half_float_constant();
1652 const TypeH* r1 = t1->isa_half_float_constant();
1653 if (r0 == nullptr || r1 == nullptr) {
1654 return bottom_type();
1655 }
1656
1657 if (r0->is_nan()) {
1658 return r0;
1659 }
1660 if (r1->is_nan()) {
1661 return r1;
1662 }
1663
1664 float f0 = r0->getf();
1665 float f1 = r1->getf();
1666 if (f0 != 0.0f || f1 != 0.0f) {
1667 return f0 > f1 ? r0 : r1;
1668 }
1669
1670 // As per IEEE 754 specification, floating point comparison consider +ve and -ve
1671 // zeros as equals. Thus, performing signed integral comparison for max value
1672 // detection.
1673 return (jint_cast(f0) > jint_cast(f1)) ? r0 : r1;
1674 }
1675
1676
1677 //------------------------------add_ring---------------------------------------
1678 const Type* MaxFNode::add_ring(const Type* t0, const Type* t1) const {
1679 const TypeF* r0 = t0->isa_float_constant();
1680 const TypeF* r1 = t1->isa_float_constant();
1681 if (r0 == nullptr || r1 == nullptr) {
1682 return bottom_type();
1683 }
1684
1685 if (r0->is_nan()) {
1686 return r0;
1687 }
1688 if (r1->is_nan()) {
1689 return r1;
1690 }
1691
1692 float f0 = r0->getf();
1693 float f1 = r1->getf();
1694 if (f0 != 0.0f || f1 != 0.0f) {
1695 return f0 > f1 ? r0 : r1;
1696 }
1697
1698 // handle max of 0.0,-0.0 case.
1699 return (jint_cast(f0) > jint_cast(f1)) ? r0 : r1;
1700 }
1701
1702 //------------------------------add_ring---------------------------------------
1703 const Type* MaxDNode::add_ring(const Type* t0, const Type* t1) const {
1704 const TypeD* r0 = t0->isa_double_constant();
1705 const TypeD* r1 = t1->isa_double_constant();
1706 if (r0 == nullptr || r1 == nullptr) {
1707 return bottom_type();
1708 }
1709
1710 if (r0->is_nan()) {
1711 return r0;
1712 }
1713 if (r1->is_nan()) {
1714 return r1;
1715 }
1716
1717 double d0 = r0->getd();
1718 double d1 = r1->getd();
1719 if (d0 != 0.0 || d1 != 0.0) {
1720 return d0 > d1 ? r0 : r1;
1721 }
1722
1723 // handle max of 0.0, -0.0 case.
1724 return (jlong_cast(d0) > jlong_cast(d1)) ? r0 : r1;
1725 }