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 "compiler/compileLog.hpp"
26 #include "gc/shared/barrierSet.hpp"
27 #include "gc/shared/c2/barrierSetC2.hpp"
28 #include "memory/allocation.inline.hpp"
29 #include "opto/addnode.hpp"
30 #include "opto/callnode.hpp"
31 #include "opto/cfgnode.hpp"
32 #include "opto/inlinetypenode.hpp"
33 #include "opto/loopnode.hpp"
34 #include "opto/matcher.hpp"
35 #include "opto/movenode.hpp"
36 #include "opto/mulnode.hpp"
37 #include "opto/opaquenode.hpp"
38 #include "opto/opcodes.hpp"
39 #include "opto/phaseX.hpp"
40 #include "opto/subnode.hpp"
41 #include "runtime/sharedRuntime.hpp"
42 #include "utilities/reverse_bits.hpp"
43
44 // Portions of code courtesy of Clifford Click
45
46 // Optimization - Graph Style
47
48 #include "math.h"
49
50 //=============================================================================
51 //------------------------------Identity---------------------------------------
52 // If right input is a constant 0, return the left input.
53 Node* SubNode::Identity(PhaseGVN* phase) {
54 assert(in(1) != this, "Must already have called Value");
55 assert(in(2) != this, "Must already have called Value");
56
57 const Type* zero = add_id();
58
59 // Remove double negation if it is not a floating point number since negation
60 // is not the same as subtraction for floating point numbers
61 // (cf. JLS § 15.15.4). `0-(0-(-0.0))` must be equal to positive 0.0 according to
62 // JLS § 15.8.2, but would result in -0.0 if this folding would be applied.
63 if (phase->type(in(1))->higher_equal(zero) &&
64 in(2)->Opcode() == Opcode() &&
65 phase->type(in(2)->in(1))->higher_equal(zero) &&
66 !phase->type(in(2)->in(2))->is_floatingpoint()) {
67 return in(2)->in(2);
68 }
69
70 // Convert "(X+Y) - Y" into X and "(X+Y) - X" into Y
71 if (in(1)->Opcode() == Op_AddI || in(1)->Opcode() == Op_AddL) {
72 if (in(1)->in(2) == in(2)) {
73 return in(1)->in(1);
74 }
75 if (in(1)->in(1) == in(2)) {
76 return in(1)->in(2);
77 }
78 }
79
80 return ( phase->type( in(2) )->higher_equal( zero ) ) ? in(1) : this;
81 }
82
83 //------------------------------Value------------------------------------------
84 // A subtract node differences it's two inputs.
85 const Type* SubNode::Value_common(PhaseValues* phase) const {
86 const Node* in1 = in(1);
87 const Node* in2 = in(2);
88 // Either input is TOP ==> the result is TOP
89 const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
90 if( t1 == Type::TOP ) return Type::TOP;
91 const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
92 if( t2 == Type::TOP ) return Type::TOP;
93
94 // Not correct for SubFnode and AddFNode (must check for infinity)
95 // Equal? Subtract is zero
96 if (in1->eqv_uncast(in2)) return add_id();
97
98 // Either input is BOTTOM ==> the result is the local BOTTOM
99 if( t1 == Type::BOTTOM || t2 == Type::BOTTOM )
100 return bottom_type();
101
102 return nullptr;
103 }
104
105 const Type* SubNode::Value(PhaseGVN* phase) const {
106 const Type* t = Value_common(phase);
107 if (t != nullptr) {
108 return t;
109 }
110 const Type* t1 = phase->type(in(1));
111 const Type* t2 = phase->type(in(2));
112 return sub(t1,t2); // Local flavor of type subtraction
113
114 }
115
116 SubNode* SubNode::make(Node* in1, Node* in2, BasicType bt) {
117 switch (bt) {
118 case T_INT:
119 return new SubINode(in1, in2);
120 case T_LONG:
121 return new SubLNode(in1, in2);
122 default:
123 fatal("Not implemented for %s", type2name(bt));
124 }
125 return nullptr;
126 }
127
128 //=============================================================================
129 //------------------------------Helper function--------------------------------
130
131 static bool is_cloop_increment(Node* inc) {
132 precond(inc->Opcode() == Op_AddI || inc->Opcode() == Op_AddL);
133
134 if (!inc->in(1)->is_Phi()) {
135 return false;
136 }
137 const PhiNode* phi = inc->in(1)->as_Phi();
138
139 if (!phi->region()->is_CountedLoop()) {
140 return false;
141 }
142
143 return inc == phi->region()->as_CountedLoop()->incr();
144 }
145
146 // Given the expression '(x + C) - v', or
147 // 'v - (x + C)', we examine nodes '+' and 'v':
148 //
149 // 1. Do not convert if '+' is a counted-loop increment, because the '-' is
150 // loop invariant and converting extends the live-range of 'x' to overlap
151 // with the '+', forcing another register to be used in the loop.
152 //
153 // 2. Do not convert if 'v' is a counted-loop induction variable, because
154 // 'x' might be invariant.
155 //
156 static bool ok_to_convert(Node* inc, Node* var) {
157 return !(is_cloop_increment(inc) || var->is_cloop_ind_var());
158 }
159
160 static bool is_cloop_condition(BoolNode* bol) {
161 for (DUIterator_Fast imax, i = bol->fast_outs(imax); i < imax; i++) {
162 Node* out = bol->fast_out(i);
163 if (out->is_BaseCountedLoopEnd()) {
164 return true;
165 }
166 }
167 return false;
168 }
169
170 //------------------------------Ideal------------------------------------------
171 Node *SubINode::Ideal(PhaseGVN *phase, bool can_reshape){
172 Node *in1 = in(1);
173 Node *in2 = in(2);
174 uint op1 = in1->Opcode();
175 uint op2 = in2->Opcode();
176
177 #ifdef ASSERT
178 // Check for dead loop
179 if ((in1 == this) || (in2 == this) ||
180 ((op1 == Op_AddI || op1 == Op_SubI) &&
181 ((in1->in(1) == this) || (in1->in(2) == this) ||
182 (in1->in(1) == in1) || (in1->in(2) == in1)))) {
183 assert(false, "dead loop in SubINode::Ideal");
184 }
185 #endif
186
187 const Type *t2 = phase->type( in2 );
188 if( t2 == Type::TOP ) return nullptr;
189 // Convert "x-c0" into "x+ -c0".
190 if( t2->base() == Type::Int ){ // Might be bottom or top...
191 const TypeInt *i = t2->is_int();
192 if( i->is_con() )
193 return new AddINode(in1, phase->intcon(java_negate(i->get_con())));
194 }
195
196 // Convert "(x+c0) - y" into (x-y) + c0"
197 // Do not collapse (x+c0)-y if "+" is a loop increment or
198 // if "y" is a loop induction variable.
199 if( op1 == Op_AddI && ok_to_convert(in1, in2) ) {
200 const Type *tadd = phase->type( in1->in(2) );
201 if( tadd->singleton() && tadd != Type::TOP ) {
202 Node *sub2 = phase->transform( new SubINode( in1->in(1), in2 ));
203 return new AddINode( sub2, in1->in(2) );
204 }
205 }
206
207 // Convert "x - (y+c0)" into "(x-y) - c0" AND
208 // Convert "c1 - (y+c0)" into "(c1-c0) - y"
209 // Need the same check as in above optimization but reversed.
210 if (op2 == Op_AddI
211 && ok_to_convert(in2, in1)
212 && in2->in(2)->Opcode() == Op_ConI) {
213 jint c0 = phase->type(in2->in(2))->isa_int()->get_con();
214 Node* in21 = in2->in(1);
215 if (in1->Opcode() == Op_ConI) {
216 // Match c1
217 jint c1 = phase->type(in1)->isa_int()->get_con();
218 Node* sub2 = phase->intcon(java_subtract(c1, c0));
219 return new SubINode(sub2, in21);
220 } else {
221 // Match x
222 Node* sub2 = phase->transform(new SubINode(in1, in21));
223 Node* neg_c0 = phase->intcon(java_negate(c0));
224 return new AddINode(sub2, neg_c0);
225 }
226 }
227
228 const Type *t1 = phase->type( in1 );
229 if( t1 == Type::TOP ) return nullptr;
230
231 #ifdef ASSERT
232 // Check for dead loop
233 if ((op2 == Op_AddI || op2 == Op_SubI) &&
234 ((in2->in(1) == this) || (in2->in(2) == this) ||
235 (in2->in(1) == in2) || (in2->in(2) == in2))) {
236 assert(false, "dead loop in SubINode::Ideal");
237 }
238 #endif
239
240 // Convert "x - (x+y)" into "-y"
241 if (op2 == Op_AddI && in1 == in2->in(1)) {
242 return new SubINode(phase->intcon(0), in2->in(2));
243 }
244 // Convert "(x-y) - x" into "-y"
245 if (op1 == Op_SubI && in1->in(1) == in2) {
246 return new SubINode(phase->intcon(0), in1->in(2));
247 }
248 // Convert "x - (y+x)" into "-y"
249 if (op2 == Op_AddI && in1 == in2->in(2)) {
250 return new SubINode(phase->intcon(0), in2->in(1));
251 }
252
253 // Convert "0 - (x-y)" into "y-x", leave the double negation "-(-y)" to SubNode::Identity().
254 if (t1 == TypeInt::ZERO && op2 == Op_SubI && phase->type(in2->in(1)) != TypeInt::ZERO) {
255 return new SubINode(in2->in(2), in2->in(1));
256 }
257
258 // Convert "0 - (x+con)" into "-con-x"
259 jint con;
260 if( t1 == TypeInt::ZERO && op2 == Op_AddI &&
261 (con = in2->in(2)->find_int_con(0)) != 0 )
262 return new SubINode( phase->intcon(-con), in2->in(1) );
263
264 // Convert "(X+A) - (X+B)" into "A - B"
265 if( op1 == Op_AddI && op2 == Op_AddI && in1->in(1) == in2->in(1) )
266 return new SubINode( in1->in(2), in2->in(2) );
267
268 // Convert "(A+X) - (B+X)" into "A - B"
269 if( op1 == Op_AddI && op2 == Op_AddI && in1->in(2) == in2->in(2) )
270 return new SubINode( in1->in(1), in2->in(1) );
271
272 // Convert "(A+X) - (X+B)" into "A - B"
273 if( op1 == Op_AddI && op2 == Op_AddI && in1->in(2) == in2->in(1) )
274 return new SubINode( in1->in(1), in2->in(2) );
275
276 // Convert "(X+A) - (B+X)" into "A - B"
277 if( op1 == Op_AddI && op2 == Op_AddI && in1->in(1) == in2->in(2) )
278 return new SubINode( in1->in(2), in2->in(1) );
279
280 // Convert "A-(B-C)" into (A+C)-B", since add is commutative and generally
281 // nicer to optimize than subtract.
282 if( op2 == Op_SubI && in2->outcnt() == 1) {
283 Node *add1 = phase->transform( new AddINode( in1, in2->in(2) ) );
284 return new SubINode( add1, in2->in(1) );
285 }
286
287 // Associative
288 if (op1 == Op_MulI && op2 == Op_MulI) {
289 Node* sub_in1 = nullptr;
290 Node* sub_in2 = nullptr;
291 Node* mul_in = nullptr;
292
293 if (in1->in(1) == in2->in(1)) {
294 // Convert "a*b-a*c into a*(b-c)
295 sub_in1 = in1->in(2);
296 sub_in2 = in2->in(2);
297 mul_in = in1->in(1);
298 } else if (in1->in(2) == in2->in(1)) {
299 // Convert a*b-b*c into b*(a-c)
300 sub_in1 = in1->in(1);
301 sub_in2 = in2->in(2);
302 mul_in = in1->in(2);
303 } else if (in1->in(2) == in2->in(2)) {
304 // Convert a*c-b*c into (a-b)*c
305 sub_in1 = in1->in(1);
306 sub_in2 = in2->in(1);
307 mul_in = in1->in(2);
308 } else if (in1->in(1) == in2->in(2)) {
309 // Convert a*b-c*a into a*(b-c)
310 sub_in1 = in1->in(2);
311 sub_in2 = in2->in(1);
312 mul_in = in1->in(1);
313 }
314
315 if (mul_in != nullptr) {
316 Node* sub = phase->transform(new SubINode(sub_in1, sub_in2));
317 return new MulINode(mul_in, sub);
318 }
319 }
320
321 // Convert "0-(A>>31)" into "(A>>>31)"
322 if ( op2 == Op_RShiftI ) {
323 Node *in21 = in2->in(1);
324 Node *in22 = in2->in(2);
325 const TypeInt *zero = phase->type(in1)->isa_int();
326 const TypeInt *t21 = phase->type(in21)->isa_int();
327 const TypeInt *t22 = phase->type(in22)->isa_int();
328 if ( t21 && t22 && zero == TypeInt::ZERO && t22->is_con(31) ) {
329 return new URShiftINode(in21, in22);
330 }
331 }
332
333 return nullptr;
334 }
335
336 //------------------------------sub--------------------------------------------
337 // A subtract node differences it's two inputs.
338 const Type *SubINode::sub( const Type *t1, const Type *t2 ) const {
339 const TypeInt *r0 = t1->is_int(); // Handy access
340 const TypeInt *r1 = t2->is_int();
341 int32_t lo = java_subtract(r0->_lo, r1->_hi);
342 int32_t hi = java_subtract(r0->_hi, r1->_lo);
343
344 // We next check for 32-bit overflow.
345 // If that happens, we just assume all integers are possible.
346 if( (((r0->_lo ^ r1->_hi) >= 0) || // lo ends have same signs OR
347 ((r0->_lo ^ lo) >= 0)) && // lo results have same signs AND
348 (((r0->_hi ^ r1->_lo) >= 0) || // hi ends have same signs OR
349 ((r0->_hi ^ hi) >= 0)) ) // hi results have same signs
350 return TypeInt::make(lo,hi,MAX2(r0->_widen,r1->_widen));
351 else // Overflow; assume all integers
352 return TypeInt::INT;
353 }
354
355 //=============================================================================
356 //------------------------------Ideal------------------------------------------
357 Node *SubLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
358 Node *in1 = in(1);
359 Node *in2 = in(2);
360 uint op1 = in1->Opcode();
361 uint op2 = in2->Opcode();
362
363 #ifdef ASSERT
364 // Check for dead loop
365 if ((in1 == this) || (in2 == this) ||
366 ((op1 == Op_AddL || op1 == Op_SubL) &&
367 ((in1->in(1) == this) || (in1->in(2) == this) ||
368 (in1->in(1) == in1) || (in1->in(2) == in1)))) {
369 assert(false, "dead loop in SubLNode::Ideal");
370 }
371 #endif
372
373 if( phase->type( in2 ) == Type::TOP ) return nullptr;
374 const TypeLong *i = phase->type( in2 )->isa_long();
375 // Convert "x-c0" into "x+ -c0".
376 if( i && // Might be bottom or top...
377 i->is_con() )
378 return new AddLNode(in1, phase->longcon(java_negate(i->get_con())));
379
380 // Convert "(x+c0) - y" into (x-y) + c0"
381 // Do not collapse (x+c0)-y if "+" is a loop increment or
382 // if "y" is a loop induction variable.
383 if( op1 == Op_AddL && ok_to_convert(in1, in2) ) {
384 Node *in11 = in1->in(1);
385 const Type *tadd = phase->type( in1->in(2) );
386 if( tadd->singleton() && tadd != Type::TOP ) {
387 Node *sub2 = phase->transform( new SubLNode( in11, in2 ));
388 return new AddLNode( sub2, in1->in(2) );
389 }
390 }
391
392 // Convert "x - (y+c0)" into "(x-y) - c0" AND
393 // Convert "c1 - (y+c0)" into "(c1-c0) - y"
394 // Need the same check as in above optimization but reversed.
395 if (op2 == Op_AddL
396 && ok_to_convert(in2, in1)
397 && in2->in(2)->Opcode() == Op_ConL) {
398 jlong c0 = phase->type(in2->in(2))->isa_long()->get_con();
399 Node* in21 = in2->in(1);
400 if (in1->Opcode() == Op_ConL) {
401 // Match c1
402 jlong c1 = phase->type(in1)->isa_long()->get_con();
403 Node* sub2 = phase->longcon(java_subtract(c1, c0));
404 return new SubLNode(sub2, in21);
405 } else {
406 Node* sub2 = phase->transform(new SubLNode(in1, in21));
407 Node* neg_c0 = phase->longcon(java_negate(c0));
408 return new AddLNode(sub2, neg_c0);
409 }
410 }
411
412 const Type *t1 = phase->type( in1 );
413 if( t1 == Type::TOP ) return nullptr;
414
415 #ifdef ASSERT
416 // Check for dead loop
417 if ((op2 == Op_AddL || op2 == Op_SubL) &&
418 ((in2->in(1) == this) || (in2->in(2) == this) ||
419 (in2->in(1) == in2) || (in2->in(2) == in2))) {
420 assert(false, "dead loop in SubLNode::Ideal");
421 }
422 #endif
423
424 // Convert "x - (x+y)" into "-y"
425 if (op2 == Op_AddL && in1 == in2->in(1)) {
426 return new SubLNode(phase->makecon(TypeLong::ZERO), in2->in(2));
427 }
428 // Convert "(x-y) - x" into "-y"
429 if (op1 == Op_SubL && in1->in(1) == in2) {
430 return new SubLNode(phase->makecon(TypeLong::ZERO), in1->in(2));
431 }
432 // Convert "x - (y+x)" into "-y"
433 if (op2 == Op_AddL && in1 == in2->in(2)) {
434 return new SubLNode(phase->makecon(TypeLong::ZERO), in2->in(1));
435 }
436
437 // Convert "0 - (x-y)" into "y-x", leave the double negation "-(-y)" to SubNode::Identity.
438 if (t1 == TypeLong::ZERO && op2 == Op_SubL && phase->type(in2->in(1)) != TypeLong::ZERO) {
439 return new SubLNode(in2->in(2), in2->in(1));
440 }
441
442 // Convert "(X+A) - (X+B)" into "A - B"
443 if( op1 == Op_AddL && op2 == Op_AddL && in1->in(1) == in2->in(1) )
444 return new SubLNode( in1->in(2), in2->in(2) );
445
446 // Convert "(A+X) - (B+X)" into "A - B"
447 if( op1 == Op_AddL && op2 == Op_AddL && in1->in(2) == in2->in(2) )
448 return new SubLNode( in1->in(1), in2->in(1) );
449
450 // Convert "(A+X) - (X+B)" into "A - B"
451 if( op1 == Op_AddL && op2 == Op_AddL && in1->in(2) == in2->in(1) )
452 return new SubLNode( in1->in(1), in2->in(2) );
453
454 // Convert "(X+A) - (B+X)" into "A - B"
455 if( op1 == Op_AddL && op2 == Op_AddL && in1->in(1) == in2->in(2) )
456 return new SubLNode( in1->in(2), in2->in(1) );
457
458 // Convert "A-(B-C)" into (A+C)-B"
459 if( op2 == Op_SubL && in2->outcnt() == 1) {
460 Node *add1 = phase->transform( new AddLNode( in1, in2->in(2) ) );
461 return new SubLNode( add1, in2->in(1) );
462 }
463
464 // Associative
465 if (op1 == Op_MulL && op2 == Op_MulL) {
466 Node* sub_in1 = nullptr;
467 Node* sub_in2 = nullptr;
468 Node* mul_in = nullptr;
469
470 if (in1->in(1) == in2->in(1)) {
471 // Convert "a*b-a*c into a*(b+c)
472 sub_in1 = in1->in(2);
473 sub_in2 = in2->in(2);
474 mul_in = in1->in(1);
475 } else if (in1->in(2) == in2->in(1)) {
476 // Convert a*b-b*c into b*(a-c)
477 sub_in1 = in1->in(1);
478 sub_in2 = in2->in(2);
479 mul_in = in1->in(2);
480 } else if (in1->in(2) == in2->in(2)) {
481 // Convert a*c-b*c into (a-b)*c
482 sub_in1 = in1->in(1);
483 sub_in2 = in2->in(1);
484 mul_in = in1->in(2);
485 } else if (in1->in(1) == in2->in(2)) {
486 // Convert a*b-c*a into a*(b-c)
487 sub_in1 = in1->in(2);
488 sub_in2 = in2->in(1);
489 mul_in = in1->in(1);
490 }
491
492 if (mul_in != nullptr) {
493 Node* sub = phase->transform(new SubLNode(sub_in1, sub_in2));
494 return new MulLNode(mul_in, sub);
495 }
496 }
497
498 // Convert "0L-(A>>63)" into "(A>>>63)"
499 if ( op2 == Op_RShiftL ) {
500 Node *in21 = in2->in(1);
501 Node *in22 = in2->in(2);
502 const TypeLong *zero = phase->type(in1)->isa_long();
503 const TypeLong *t21 = phase->type(in21)->isa_long();
504 const TypeInt *t22 = phase->type(in22)->isa_int();
505 if ( t21 && t22 && zero == TypeLong::ZERO && t22->is_con(63) ) {
506 return new URShiftLNode(in21, in22);
507 }
508 }
509
510 return nullptr;
511 }
512
513 //------------------------------sub--------------------------------------------
514 // A subtract node differences it's two inputs.
515 const Type *SubLNode::sub( const Type *t1, const Type *t2 ) const {
516 const TypeLong *r0 = t1->is_long(); // Handy access
517 const TypeLong *r1 = t2->is_long();
518 jlong lo = java_subtract(r0->_lo, r1->_hi);
519 jlong hi = java_subtract(r0->_hi, r1->_lo);
520
521 // We next check for 32-bit overflow.
522 // If that happens, we just assume all integers are possible.
523 if( (((r0->_lo ^ r1->_hi) >= 0) || // lo ends have same signs OR
524 ((r0->_lo ^ lo) >= 0)) && // lo results have same signs AND
525 (((r0->_hi ^ r1->_lo) >= 0) || // hi ends have same signs OR
526 ((r0->_hi ^ hi) >= 0)) ) // hi results have same signs
527 return TypeLong::make(lo,hi,MAX2(r0->_widen,r1->_widen));
528 else // Overflow; assume all integers
529 return TypeLong::LONG;
530 }
531
532 //=============================================================================
533 //------------------------------Value------------------------------------------
534 // A subtract node differences its two inputs.
535 const Type* SubFPNode::Value(PhaseGVN* phase) const {
536 const Node* in1 = in(1);
537 const Node* in2 = in(2);
538 // Either input is TOP ==> the result is TOP
539 const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
540 if( t1 == Type::TOP ) return Type::TOP;
541 const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
542 if( t2 == Type::TOP ) return Type::TOP;
543
544 // if both operands are infinity of same sign, the result is NaN; do
545 // not replace with zero
546 if (t1->is_finite() && t2->is_finite() && in1 == in2) {
547 return add_id();
548 }
549
550 // Either input is BOTTOM ==> the result is the local BOTTOM
551 const Type *bot = bottom_type();
552 if( (t1 == bot) || (t2 == bot) ||
553 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
554 return bot;
555
556 return sub(t1,t2); // Local flavor of type subtraction
557 }
558
559
560 //=============================================================================
561 //------------------------------sub--------------------------------------------
562 // A subtract node differences its two inputs.
563 const Type* SubHFNode::sub(const Type* t1, const Type* t2) const {
564 // Half precision floating point subtraction follows the rules of IEEE 754
565 // applicable to other floating point types.
566 if (t1->isa_half_float_constant() != nullptr &&
567 t2->isa_half_float_constant() != nullptr) {
568 return TypeH::make(t1->getf() - t2->getf());
569 } else {
570 return Type::HALF_FLOAT;
571 }
572 }
573
574 //------------------------------Ideal------------------------------------------
575 Node *SubFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
576 const Type *t2 = phase->type( in(2) );
577 // Convert "x-c0" into "x+ -c0".
578 if( t2->base() == Type::FloatCon ) { // Might be bottom or top...
579 // return new (phase->C, 3) AddFNode(in(1), phase->makecon( TypeF::make(-t2->getf()) ) );
580 }
581
582 // Cannot replace 0.0-X with -X because a 'fsub' bytecode computes
583 // 0.0-0.0 as +0.0, while a 'fneg' bytecode computes -0.0.
584 //if( phase->type(in(1)) == TypeF::ZERO )
585 //return new (phase->C, 2) NegFNode(in(2));
586
587 return nullptr;
588 }
589
590 //------------------------------sub--------------------------------------------
591 // A subtract node differences its two inputs.
592 const Type *SubFNode::sub( const Type *t1, const Type *t2 ) const {
593 // no folding if one of operands is infinity or NaN, do not do constant folding
594 if( g_isfinite(t1->getf()) && g_isfinite(t2->getf()) ) {
595 return TypeF::make( t1->getf() - t2->getf() );
596 }
597 else if( g_isnan(t1->getf()) ) {
598 return t1;
599 }
600 else if( g_isnan(t2->getf()) ) {
601 return t2;
602 }
603 else {
604 return Type::FLOAT;
605 }
606 }
607
608 //=============================================================================
609 //------------------------------Ideal------------------------------------------
610 Node *SubDNode::Ideal(PhaseGVN *phase, bool can_reshape){
611 const Type *t2 = phase->type( in(2) );
612 // Convert "x-c0" into "x+ -c0".
613 if( t2->base() == Type::DoubleCon ) { // Might be bottom or top...
614 // return new (phase->C, 3) AddDNode(in(1), phase->makecon( TypeD::make(-t2->getd()) ) );
615 }
616
617 // Cannot replace 0.0-X with -X because a 'dsub' bytecode computes
618 // 0.0-0.0 as +0.0, while a 'dneg' bytecode computes -0.0.
619 //if( phase->type(in(1)) == TypeD::ZERO )
620 //return new (phase->C, 2) NegDNode(in(2));
621
622 return nullptr;
623 }
624
625 //------------------------------sub--------------------------------------------
626 // A subtract node differences its two inputs.
627 const Type *SubDNode::sub( const Type *t1, const Type *t2 ) const {
628 // no folding if one of operands is infinity or NaN, do not do constant folding
629 if( g_isfinite(t1->getd()) && g_isfinite(t2->getd()) ) {
630 return TypeD::make( t1->getd() - t2->getd() );
631 }
632 else if( g_isnan(t1->getd()) ) {
633 return t1;
634 }
635 else if( g_isnan(t2->getd()) ) {
636 return t2;
637 }
638 else {
639 return Type::DOUBLE;
640 }
641 }
642
643 //=============================================================================
644 //------------------------------Idealize---------------------------------------
645 // Unlike SubNodes, compare must still flatten return value to the
646 // range -1, 0, 1.
647 // And optimizations like those for (X + Y) - X fail if overflow happens.
648 Node* CmpNode::Identity(PhaseGVN* phase) {
649 return this;
650 }
651
652 CmpNode *CmpNode::make(Node *in1, Node *in2, BasicType bt, bool unsigned_comp) {
653 switch (bt) {
654 case T_INT:
655 if (unsigned_comp) {
656 return new CmpUNode(in1, in2);
657 }
658 return new CmpINode(in1, in2);
659 case T_LONG:
660 if (unsigned_comp) {
661 return new CmpULNode(in1, in2);
662 }
663 return new CmpLNode(in1, in2);
664 case T_OBJECT:
665 case T_ARRAY:
666 case T_ADDRESS:
667 case T_METADATA:
668 return new CmpPNode(in1, in2);
669 case T_NARROWOOP:
670 case T_NARROWKLASS:
671 return new CmpNNode(in1, in2);
672 default:
673 fatal("Not implemented for %s", type2name(bt));
674 }
675 return nullptr;
676 }
677
678 //=============================================================================
679 //------------------------------cmp--------------------------------------------
680 // Simplify a CmpI (compare 2 integers) node, based on local information.
681 // If both inputs are constants, compare them.
682 const Type *CmpINode::sub( const Type *t1, const Type *t2 ) const {
683 const TypeInt *r0 = t1->is_int(); // Handy access
684 const TypeInt *r1 = t2->is_int();
685
686 if( r0->_hi < r1->_lo ) // Range is always low?
687 return TypeInt::CC_LT;
688 else if( r0->_lo > r1->_hi ) // Range is always high?
689 return TypeInt::CC_GT;
690
691 else if( r0->is_con() && r1->is_con() ) { // comparing constants?
692 assert(r0->get_con() == r1->get_con(), "must be equal");
693 return TypeInt::CC_EQ; // Equal results.
694 } else if( r0->_hi == r1->_lo ) // Range is never high?
695 return TypeInt::CC_LE;
696 else if( r0->_lo == r1->_hi ) // Range is never low?
697 return TypeInt::CC_GE;
698
699 const Type* joined = r0->join(r1);
700 if (joined == Type::TOP) {
701 return TypeInt::CC_NE;
702 }
703 return TypeInt::CC; // else use worst case results
704 }
705
706 const Type* CmpINode::Value(PhaseGVN* phase) const {
707 Node* in1 = in(1);
708 Node* in2 = in(2);
709 // If this test is the zero trip guard for a main or post loop, check whether, with the opaque node removed, the test
710 // would constant fold so the loop is never entered. If so return the type of the test without the opaque node removed:
711 // make the loop unreachable.
712 // The reason for this is that the iv phi captures the bounds of the loop and if the loop becomes unreachable, it can
713 // become top. In that case, the loop must be removed.
714 // This is safe because:
715 // - as optimizations proceed, the range of iterations executed by the main loop narrows. If no iterations remain, then
716 // we're done with optimizations for that loop.
717 // - the post loop is initially not reachable but as long as there's a main loop, the zero trip guard for the post
718 // loop takes a phi that merges the pre and main loop's iv and can't constant fold the zero trip guard. Once, the main
719 // loop is removed, there's no need to preserve the zero trip guard for the post loop anymore.
720 if (in1 != nullptr && in2 != nullptr) {
721 uint input = 0;
722 Node* cmp = nullptr;
723 BoolTest::mask test;
724 if (in1->Opcode() == Op_OpaqueZeroTripGuard && phase->type(in1) != Type::TOP) {
725 cmp = new CmpINode(in1->in(1), in2);
726 test = ((OpaqueZeroTripGuardNode*)in1)->_loop_entered_mask;
727 }
728 if (in2->Opcode() == Op_OpaqueZeroTripGuard && phase->type(in2) != Type::TOP) {
729 assert(cmp == nullptr, "A cmp with 2 OpaqueZeroTripGuard inputs");
730 cmp = new CmpINode(in1, in2->in(1));
731 test = ((OpaqueZeroTripGuardNode*)in2)->_loop_entered_mask;
732 }
733 if (cmp != nullptr) {
734 const Type* cmp_t = cmp->Value(phase);
735 const Type* t = BoolTest(test).cc2logical(cmp_t);
736 cmp->destruct(phase);
737 if (t == TypeInt::ZERO) {
738 return cmp_t;
739 }
740 }
741 }
742
743 return SubNode::Value(phase);
744 }
745
746
747 // Simplify a CmpU (compare 2 integers) node, based on local information.
748 // If both inputs are constants, compare them.
749 const Type *CmpUNode::sub( const Type *t1, const Type *t2 ) const {
750 assert(!t1->isa_ptr(), "obsolete usage of CmpU");
751
752 // comparing two unsigned ints
753 const TypeInt *r0 = t1->is_int(); // Handy access
754 const TypeInt *r1 = t2->is_int();
755
756 // Current installed version
757 // Compare ranges for non-overlap
758 juint lo0 = r0->_lo;
759 juint hi0 = r0->_hi;
760 juint lo1 = r1->_lo;
761 juint hi1 = r1->_hi;
762
763 // If either one has both negative and positive values,
764 // it therefore contains both 0 and -1, and since [0..-1] is the
765 // full unsigned range, the type must act as an unsigned bottom.
766 bool bot0 = ((jint)(lo0 ^ hi0) < 0);
767 bool bot1 = ((jint)(lo1 ^ hi1) < 0);
768
769 if (bot0 || bot1) {
770 // All unsigned values are LE -1 and GE 0.
771 if (lo0 == 0 && hi0 == 0) {
772 return TypeInt::CC_LE; // 0 <= bot
773 } else if ((jint)lo0 == -1 && (jint)hi0 == -1) {
774 return TypeInt::CC_GE; // -1 >= bot
775 } else if (lo1 == 0 && hi1 == 0) {
776 return TypeInt::CC_GE; // bot >= 0
777 } else if ((jint)lo1 == -1 && (jint)hi1 == -1) {
778 return TypeInt::CC_LE; // bot <= -1
779 }
780 } else {
781 // We can use ranges of the form [lo..hi] if signs are the same.
782 assert(lo0 <= hi0 && lo1 <= hi1, "unsigned ranges are valid");
783 // results are reversed, '-' > '+' for unsigned compare
784 if (hi0 < lo1) {
785 return TypeInt::CC_LT; // smaller
786 } else if (lo0 > hi1) {
787 return TypeInt::CC_GT; // greater
788 } else if (hi0 == lo1 && lo0 == hi1) {
789 return TypeInt::CC_EQ; // Equal results
790 } else if (lo0 >= hi1) {
791 return TypeInt::CC_GE;
792 } else if (hi0 <= lo1) {
793 // Check for special case in Hashtable::get. (See below.)
794 if ((jint)lo0 >= 0 && (jint)lo1 >= 0 && is_index_range_check())
795 return TypeInt::CC_LT;
796 return TypeInt::CC_LE;
797 }
798 }
799 // Check for special case in Hashtable::get - the hash index is
800 // mod'ed to the table size so the following range check is useless.
801 // Check for: (X Mod Y) CmpU Y, where the mod result and Y both have
802 // to be positive.
803 // (This is a gross hack, since the sub method never
804 // looks at the structure of the node in any other case.)
805 if ((jint)lo0 >= 0 && (jint)lo1 >= 0 && is_index_range_check())
806 return TypeInt::CC_LT;
807
808 const Type* joined = r0->join(r1);
809 if (joined == Type::TOP) {
810 return TypeInt::CC_NE;
811 }
812
813 return TypeInt::CC; // else use worst case results
814 }
815
816 const Type* CmpUNode::Value(PhaseGVN* phase) const {
817 const Type* t = SubNode::Value_common(phase);
818 if (t != nullptr) {
819 return t;
820 }
821 const Node* in1 = in(1);
822 const Node* in2 = in(2);
823 const Type* t1 = phase->type(in1);
824 const Type* t2 = phase->type(in2);
825 assert(t1->isa_int(), "CmpU has only Int type inputs");
826 if (t2 == TypeInt::INT) { // Compare to bottom?
827 return bottom_type();
828 }
829
830 const Type* t_sub = sub(t1, t2); // compare based on immediate inputs
831
832 uint in1_op = in1->Opcode();
833 if (in1_op == Op_AddI || in1_op == Op_SubI) {
834 // The problem rise when result of AddI(SubI) may overflow
835 // signed integer value. Let say the input type is
836 // [256, maxint] then +128 will create 2 ranges due to
837 // overflow: [minint, minint+127] and [384, maxint].
838 // But C2 type system keep only 1 type range and as result
839 // it use general [minint, maxint] for this case which we
840 // can't optimize.
841 //
842 // Make 2 separate type ranges based on types of AddI(SubI) inputs
843 // and compare results of their compare. If results are the same
844 // CmpU node can be optimized.
845 const Node* in11 = in1->in(1);
846 const Node* in12 = in1->in(2);
847 const Type* t11 = (in11 == in1) ? Type::TOP : phase->type(in11);
848 const Type* t12 = (in12 == in1) ? Type::TOP : phase->type(in12);
849 // Skip cases when input types are top or bottom.
850 if ((t11 != Type::TOP) && (t11 != TypeInt::INT) &&
851 (t12 != Type::TOP) && (t12 != TypeInt::INT)) {
852 const TypeInt *r0 = t11->is_int();
853 const TypeInt *r1 = t12->is_int();
854 jlong lo_r0 = r0->_lo;
855 jlong hi_r0 = r0->_hi;
856 jlong lo_r1 = r1->_lo;
857 jlong hi_r1 = r1->_hi;
858 if (in1_op == Op_SubI) {
859 jlong tmp = hi_r1;
860 hi_r1 = -lo_r1;
861 lo_r1 = -tmp;
862 // Note, for substructing [minint,x] type range
863 // long arithmetic provides correct overflow answer.
864 // The confusion come from the fact that in 32-bit
865 // -minint == minint but in 64-bit -minint == maxint+1.
866 }
867 jlong lo_long = lo_r0 + lo_r1;
868 jlong hi_long = hi_r0 + hi_r1;
869 int lo_tr1 = min_jint;
870 int hi_tr1 = (int)hi_long;
871 int lo_tr2 = (int)lo_long;
872 int hi_tr2 = max_jint;
873 bool underflow = lo_long != (jlong)lo_tr2;
874 bool overflow = hi_long != (jlong)hi_tr1;
875 // Use sub(t1, t2) when there is no overflow (one type range)
876 // or when both overflow and underflow (too complex).
877 if ((underflow != overflow) && (hi_tr1 < lo_tr2)) {
878 // Overflow only on one boundary, compare 2 separate type ranges.
879 int w = MAX2(r0->_widen, r1->_widen); // _widen does not matter here
880 const TypeInt* tr1 = TypeInt::make(lo_tr1, hi_tr1, w);
881 const TypeInt* tr2 = TypeInt::make(lo_tr2, hi_tr2, w);
882 const TypeInt* cmp1 = sub(tr1, t2)->is_int();
883 const TypeInt* cmp2 = sub(tr2, t2)->is_int();
884 // Compute union, so that cmp handles all possible results from the two cases
885 const Type* t_cmp = cmp1->meet(cmp2);
886 // Pick narrowest type, based on overflow computation and on immediate inputs
887 return t_sub->filter(t_cmp);
888 }
889 }
890 }
891
892 return t_sub;
893 }
894
895 bool CmpUNode::is_index_range_check() const {
896 // Check for the "(X ModI Y) CmpU Y" shape
897 return (in(1)->Opcode() == Op_ModI &&
898 in(1)->in(2)->eqv_uncast(in(2)));
899 }
900
901 //------------------------------Idealize---------------------------------------
902 Node *CmpINode::Ideal( PhaseGVN *phase, bool can_reshape ) {
903 if (phase->type(in(2))->higher_equal(TypeInt::ZERO)) {
904 switch (in(1)->Opcode()) {
905 case Op_CmpU3: // Collapse a CmpU3/CmpI into a CmpU
906 return new CmpUNode(in(1)->in(1),in(1)->in(2));
907 case Op_CmpL3: // Collapse a CmpL3/CmpI into a CmpL
908 return new CmpLNode(in(1)->in(1),in(1)->in(2));
909 case Op_CmpUL3: // Collapse a CmpUL3/CmpI into a CmpUL
910 return new CmpULNode(in(1)->in(1),in(1)->in(2));
911 case Op_CmpF3: // Collapse a CmpF3/CmpI into a CmpF
912 return new CmpFNode(in(1)->in(1),in(1)->in(2));
913 case Op_CmpD3: // Collapse a CmpD3/CmpI into a CmpD
914 return new CmpDNode(in(1)->in(1),in(1)->in(2));
915 //case Op_SubI:
916 // If (x - y) cannot overflow, then ((x - y) <?> 0)
917 // can be turned into (x <?> y).
918 // This is handled (with more general cases) by Ideal_sub_algebra.
919 }
920 }
921 return nullptr; // No change
922 }
923
924 //------------------------------Ideal------------------------------------------
925 Node* CmpLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
926 Node* a = nullptr;
927 Node* b = nullptr;
928 if (is_double_null_check(phase, a, b) && (phase->type(a)->is_zero_type() || phase->type(b)->is_zero_type())) {
929 // Degraded to a simple null check, use old acmp
930 return new CmpPNode(a, b);
931 }
932 const TypeLong *t2 = phase->type(in(2))->isa_long();
933 if (Opcode() == Op_CmpL && in(1)->Opcode() == Op_ConvI2L && t2 && t2->is_con()) {
934 const jlong con = t2->get_con();
935 if (con >= min_jint && con <= max_jint) {
936 return new CmpINode(in(1)->in(1), phase->intcon((jint)con));
937 }
938 }
939 return nullptr;
940 }
941
942 // Match double null check emitted by Compile::optimize_acmp()
943 bool CmpLNode::is_double_null_check(PhaseGVN* phase, Node*& a, Node*& b) const {
944 if (in(1)->Opcode() == Op_OrL &&
945 in(1)->in(1)->Opcode() == Op_CastP2X &&
946 in(1)->in(2)->Opcode() == Op_CastP2X &&
947 in(2)->bottom_type()->is_zero_type()) {
948 assert(EnableValhalla, "unexpected double null check");
949 a = in(1)->in(1)->in(1);
950 b = in(1)->in(2)->in(1);
951 return true;
952 }
953 return false;
954 }
955
956 //------------------------------Value------------------------------------------
957 const Type* CmpLNode::Value(PhaseGVN* phase) const {
958 Node* a = nullptr;
959 Node* b = nullptr;
960 if (is_double_null_check(phase, a, b) && (!phase->type(a)->maybe_null() || !phase->type(b)->maybe_null())) {
961 // One operand is never nullptr, emit constant false
962 return TypeInt::CC_GT;
963 }
964 return SubNode::Value(phase);
965 }
966
967 //=============================================================================
968 // Simplify a CmpL (compare 2 longs ) node, based on local information.
969 // If both inputs are constants, compare them.
970 const Type *CmpLNode::sub( const Type *t1, const Type *t2 ) const {
971 const TypeLong *r0 = t1->is_long(); // Handy access
972 const TypeLong *r1 = t2->is_long();
973
974 if( r0->_hi < r1->_lo ) // Range is always low?
975 return TypeInt::CC_LT;
976 else if( r0->_lo > r1->_hi ) // Range is always high?
977 return TypeInt::CC_GT;
978
979 else if( r0->is_con() && r1->is_con() ) { // comparing constants?
980 assert(r0->get_con() == r1->get_con(), "must be equal");
981 return TypeInt::CC_EQ; // Equal results.
982 } else if( r0->_hi == r1->_lo ) // Range is never high?
983 return TypeInt::CC_LE;
984 else if( r0->_lo == r1->_hi ) // Range is never low?
985 return TypeInt::CC_GE;
986
987 const Type* joined = r0->join(r1);
988 if (joined == Type::TOP) {
989 return TypeInt::CC_NE;
990 }
991
992 return TypeInt::CC; // else use worst case results
993 }
994
995
996 // Simplify a CmpUL (compare 2 unsigned longs) node, based on local information.
997 // If both inputs are constants, compare them.
998 const Type* CmpULNode::sub(const Type* t1, const Type* t2) const {
999 assert(!t1->isa_ptr(), "obsolete usage of CmpUL");
1000
1001 // comparing two unsigned longs
1002 const TypeLong* r0 = t1->is_long(); // Handy access
1003 const TypeLong* r1 = t2->is_long();
1004
1005 // Current installed version
1006 // Compare ranges for non-overlap
1007 julong lo0 = r0->_lo;
1008 julong hi0 = r0->_hi;
1009 julong lo1 = r1->_lo;
1010 julong hi1 = r1->_hi;
1011
1012 // If either one has both negative and positive values,
1013 // it therefore contains both 0 and -1, and since [0..-1] is the
1014 // full unsigned range, the type must act as an unsigned bottom.
1015 bool bot0 = ((jlong)(lo0 ^ hi0) < 0);
1016 bool bot1 = ((jlong)(lo1 ^ hi1) < 0);
1017
1018 if (bot0 || bot1) {
1019 // All unsigned values are LE -1 and GE 0.
1020 if (lo0 == 0 && hi0 == 0) {
1021 return TypeInt::CC_LE; // 0 <= bot
1022 } else if ((jlong)lo0 == -1 && (jlong)hi0 == -1) {
1023 return TypeInt::CC_GE; // -1 >= bot
1024 } else if (lo1 == 0 && hi1 == 0) {
1025 return TypeInt::CC_GE; // bot >= 0
1026 } else if ((jlong)lo1 == -1 && (jlong)hi1 == -1) {
1027 return TypeInt::CC_LE; // bot <= -1
1028 }
1029 } else {
1030 // We can use ranges of the form [lo..hi] if signs are the same.
1031 assert(lo0 <= hi0 && lo1 <= hi1, "unsigned ranges are valid");
1032 // results are reversed, '-' > '+' for unsigned compare
1033 if (hi0 < lo1) {
1034 return TypeInt::CC_LT; // smaller
1035 } else if (lo0 > hi1) {
1036 return TypeInt::CC_GT; // greater
1037 } else if (hi0 == lo1 && lo0 == hi1) {
1038 return TypeInt::CC_EQ; // Equal results
1039 } else if (lo0 >= hi1) {
1040 return TypeInt::CC_GE;
1041 } else if (hi0 <= lo1) {
1042 return TypeInt::CC_LE;
1043 }
1044 }
1045
1046 const Type* joined = r0->join(r1);
1047 if (joined == Type::TOP) {
1048 return TypeInt::CC_NE;
1049 }
1050
1051 return TypeInt::CC; // else use worst case results
1052 }
1053
1054 //=============================================================================
1055 //------------------------------sub--------------------------------------------
1056 // Simplify an CmpP (compare 2 pointers) node, based on local information.
1057 // If both inputs are constants, compare them.
1058 const Type *CmpPNode::sub( const Type *t1, const Type *t2 ) const {
1059 const TypePtr *r0 = t1->is_ptr(); // Handy access
1060 const TypePtr *r1 = t2->is_ptr();
1061
1062 // Undefined inputs makes for an undefined result
1063 if( TypePtr::above_centerline(r0->_ptr) ||
1064 TypePtr::above_centerline(r1->_ptr) )
1065 return Type::TOP;
1066
1067 if (r0 == r1 && r0->singleton()) {
1068 // Equal pointer constants (klasses, nulls, etc.)
1069 return TypeInt::CC_EQ;
1070 }
1071
1072 // See if it is 2 unrelated classes.
1073 const TypeOopPtr* p0 = r0->isa_oopptr();
1074 const TypeOopPtr* p1 = r1->isa_oopptr();
1075 const TypeKlassPtr* k0 = r0->isa_klassptr();
1076 const TypeKlassPtr* k1 = r1->isa_klassptr();
1077 if ((p0 && p1) || (k0 && k1)) {
1078 if (p0 && p1) {
1079 Node* in1 = in(1)->uncast();
1080 Node* in2 = in(2)->uncast();
1081 AllocateNode* alloc1 = AllocateNode::Ideal_allocation(in1);
1082 AllocateNode* alloc2 = AllocateNode::Ideal_allocation(in2);
1083 if (MemNode::detect_ptr_independence(in1, alloc1, in2, alloc2, nullptr)) {
1084 return TypeInt::CC_GT; // different pointers
1085 }
1086 }
1087 bool xklass0 = p0 ? p0->klass_is_exact() : k0->klass_is_exact();
1088 bool xklass1 = p1 ? p1->klass_is_exact() : k1->klass_is_exact();
1089 bool unrelated_classes = false;
1090
1091 if ((p0 && p0->is_same_java_type_as(p1)) ||
1092 (k0 && k0->is_same_java_type_as(k1))) {
1093 } else if ((p0 && !p1->maybe_java_subtype_of(p0) && !p0->maybe_java_subtype_of(p1)) ||
1094 (k0 && !k1->maybe_java_subtype_of(k0) && !k0->maybe_java_subtype_of(k1))) {
1095 unrelated_classes = true;
1096 } else if ((p0 && !p1->maybe_java_subtype_of(p0)) ||
1097 (k0 && !k1->maybe_java_subtype_of(k0))) {
1098 unrelated_classes = xklass1;
1099 } else if ((p0 && !p0->maybe_java_subtype_of(p1)) ||
1100 (k0 && !k0->maybe_java_subtype_of(k1))) {
1101 unrelated_classes = xklass0;
1102 }
1103 if (!unrelated_classes) {
1104 // Handle inline type arrays
1105 if ((r0->flat_in_array() && r1->not_flat_in_array()) ||
1106 (r1->flat_in_array() && r0->not_flat_in_array())) {
1107 // One type is in flat arrays but the other type is not. Must be unrelated.
1108 unrelated_classes = true;
1109 } else if ((r0->is_not_flat() && r1->is_flat()) ||
1110 (r1->is_not_flat() && r0->is_flat())) {
1111 // One type is a non-flat array and the other type is a flat array. Must be unrelated.
1112 unrelated_classes = true;
1113 } else if ((r0->is_not_null_free() && r1->is_null_free()) ||
1114 (r1->is_not_null_free() && r0->is_null_free())) {
1115 // One type is a nullable array and the other type is a null-free array. Must be unrelated.
1116 unrelated_classes = true;
1117 }
1118 }
1119 if (unrelated_classes) {
1120 // The oops classes are known to be unrelated. If the joined PTRs of
1121 // two oops is not Null and not Bottom, then we are sure that one
1122 // of the two oops is non-null, and the comparison will always fail.
1123 TypePtr::PTR jp = r0->join_ptr(r1->_ptr);
1124 if (jp != TypePtr::Null && jp != TypePtr::BotPTR) {
1125 return TypeInt::CC_GT;
1126 }
1127 }
1128 }
1129
1130 // Known constants can be compared exactly
1131 // Null can be distinguished from any NotNull pointers
1132 // Unknown inputs makes an unknown result
1133 if( r0->singleton() ) {
1134 intptr_t bits0 = r0->get_con();
1135 if( r1->singleton() )
1136 return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT;
1137 return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC;
1138 } else if( r1->singleton() ) {
1139 intptr_t bits1 = r1->get_con();
1140 return ( r0->_ptr == TypePtr::NotNull && bits1==0 ) ? TypeInt::CC_GT : TypeInt::CC;
1141 } else
1142 return TypeInt::CC;
1143 }
1144
1145 static inline Node* isa_java_mirror_load(PhaseGVN* phase, Node* n, bool& might_be_an_array) {
1146 // Return the klass node for (indirect load from OopHandle)
1147 // LoadBarrier?(LoadP(LoadP(AddP(foo:Klass, #java_mirror))))
1148 // or null if not matching.
1149 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1150 n = bs->step_over_gc_barrier(n);
1151
1152 if (n->Opcode() != Op_LoadP) return nullptr;
1153
1154 const TypeInstPtr* tp = phase->type(n)->isa_instptr();
1155 if (!tp || tp->instance_klass() != phase->C->env()->Class_klass()) return nullptr;
1156
1157 Node* adr = n->in(MemNode::Address);
1158 // First load from OopHandle: ((OopHandle)mirror)->resolve(); may need barrier.
1159 if (adr->Opcode() != Op_LoadP || !phase->type(adr)->isa_rawptr()) return nullptr;
1160 adr = adr->in(MemNode::Address);
1161
1162 intptr_t off = 0;
1163 Node* k = AddPNode::Ideal_base_and_offset(adr, phase, off);
1164 if (k == nullptr) return nullptr;
1165 const TypeKlassPtr* tkp = phase->type(k)->isa_klassptr();
1166 if (!tkp || off != in_bytes(Klass::java_mirror_offset())) return nullptr;
1167 might_be_an_array |= tkp->isa_aryklassptr() || tkp->is_instklassptr()->might_be_an_array();
1168
1169 // We've found the klass node of a Java mirror load.
1170 return k;
1171 }
1172
1173 static inline Node* isa_const_java_mirror(PhaseGVN* phase, Node* n, bool& might_be_an_array) {
1174 // for ConP(Foo.class) return ConP(Foo.klass)
1175 // otherwise return null
1176 if (!n->is_Con()) return nullptr;
1177
1178 const TypeInstPtr* tp = phase->type(n)->isa_instptr();
1179 if (!tp) return nullptr;
1180
1181 ciType* mirror_type = tp->java_mirror_type();
1182 // TypeInstPtr::java_mirror_type() returns non-null for compile-
1183 // time Class constants only.
1184 if (!mirror_type) return nullptr;
1185
1186 // x.getClass() == int.class can never be true (for all primitive types)
1187 // Return a ConP(null) node for this case.
1188 if (mirror_type->is_classless()) {
1189 return phase->makecon(TypePtr::NULL_PTR);
1190 }
1191
1192 // return the ConP(Foo.klass)
1193 ciKlass* mirror_klass = mirror_type->as_klass();
1194
1195 if (mirror_klass->is_array_klass()) {
1196 if (!mirror_klass->can_be_inline_array_klass()) {
1197 // Special case for non-value arrays: They only have one (default) refined class, use it
1198 return phase->makecon(TypeAryKlassPtr::make(mirror_klass, Type::trust_interfaces, true));
1199 }
1200 might_be_an_array |= true;
1201 }
1202
1203 return phase->makecon(TypeKlassPtr::make(mirror_klass, Type::trust_interfaces));
1204 }
1205
1206 //------------------------------Ideal------------------------------------------
1207 // Normalize comparisons between Java mirror loads to compare the klass instead.
1208 //
1209 // Also check for the case of comparing an unknown klass loaded from the primary
1210 // super-type array vs a known klass with no subtypes. This amounts to
1211 // checking to see an unknown klass subtypes a known klass with no subtypes;
1212 // this only happens on an exact match. We can shorten this test by 1 load.
1213 Node* CmpPNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1214 // TODO 8284443 in(1) could be cast?
1215 if (in(1)->is_InlineType() && phase->type(in(2))->is_zero_type()) {
1216 // Null checking a scalarized but nullable inline type. Check the null marker
1217 // input instead of the oop input to avoid keeping buffer allocations alive.
1218 return new CmpINode(in(1)->as_InlineType()->get_null_marker(), phase->intcon(0));
1219 }
1220
1221 // Normalize comparisons between Java mirrors into comparisons of the low-
1222 // level klass, where a dependent load could be shortened.
1223 //
1224 // The new pattern has a nice effect of matching the same pattern used in the
1225 // fast path of instanceof/checkcast/Class.isInstance(), which allows
1226 // redundant exact type check be optimized away by GVN.
1227 // For example, in
1228 // if (x.getClass() == Foo.class) {
1229 // Foo foo = (Foo) x;
1230 // // ... use a ...
1231 // }
1232 // a CmpPNode could be shared between if_acmpne and checkcast
1233 {
1234 bool might_be_an_array1 = false;
1235 bool might_be_an_array2 = false;
1236 Node* k1 = isa_java_mirror_load(phase, in(1), might_be_an_array1);
1237 Node* k2 = isa_java_mirror_load(phase, in(2), might_be_an_array2);
1238 Node* conk2 = isa_const_java_mirror(phase, in(2), might_be_an_array2);
1239 if (might_be_an_array1 && might_be_an_array2) {
1240 // Don't optimize if both sides might be an array because arrays with
1241 // the same Java mirror can have different refined array klasses.
1242 k1 = k2 = nullptr;
1243 }
1244
1245 if (k1 && (k2 || conk2)) {
1246 Node* lhs = k1;
1247 Node* rhs = (k2 != nullptr) ? k2 : conk2;
1248 set_req_X(1, lhs, phase);
1249 set_req_X(2, rhs, phase);
1250 return this;
1251 }
1252 }
1253
1254 // Constant pointer on right?
1255 const TypeKlassPtr* t2 = phase->type(in(2))->isa_klassptr();
1256 if (t2 == nullptr || !t2->klass_is_exact())
1257 return nullptr;
1258 // Get the constant klass we are comparing to.
1259 ciKlass* superklass = t2->exact_klass();
1260
1261 // Now check for LoadKlass on left.
1262 Node* ldk1 = in(1);
1263 if (ldk1->is_DecodeNKlass()) {
1264 ldk1 = ldk1->in(1);
1265 if (ldk1->Opcode() != Op_LoadNKlass )
1266 return nullptr;
1267 } else if (ldk1->Opcode() != Op_LoadKlass )
1268 return nullptr;
1269 // Take apart the address of the LoadKlass:
1270 Node* adr1 = ldk1->in(MemNode::Address);
1271 intptr_t con2 = 0;
1272 Node* ldk2 = AddPNode::Ideal_base_and_offset(adr1, phase, con2);
1273 if (ldk2 == nullptr)
1274 return nullptr;
1275 if (con2 == oopDesc::klass_offset_in_bytes()) {
1276 // We are inspecting an object's concrete class.
1277 // Short-circuit the check if the query is abstract.
1278 if (superklass->is_interface() ||
1279 superklass->is_abstract()) {
1280 // Make it come out always false:
1281 this->set_req(2, phase->makecon(TypePtr::NULL_PTR));
1282 return this;
1283 }
1284 }
1285
1286 // Check for a LoadKlass from primary supertype array.
1287 // Any nested loadklass from loadklass+con must be from the p.s. array.
1288 if (ldk2->is_DecodeNKlass()) {
1289 // Keep ldk2 as DecodeN since it could be used in CmpP below.
1290 if (ldk2->in(1)->Opcode() != Op_LoadNKlass )
1291 return nullptr;
1292 } else if (ldk2->Opcode() != Op_LoadKlass)
1293 return nullptr;
1294
1295 // Verify that we understand the situation
1296 if (con2 != (intptr_t) superklass->super_check_offset())
1297 return nullptr; // Might be element-klass loading from array klass
1298
1299 // If 'superklass' has no subklasses and is not an interface, then we are
1300 // assured that the only input which will pass the type check is
1301 // 'superklass' itself.
1302 //
1303 // We could be more liberal here, and allow the optimization on interfaces
1304 // which have a single implementor. This would require us to increase the
1305 // expressiveness of the add_dependency() mechanism.
1306 // %%% Do this after we fix TypeOopPtr: Deps are expressive enough now.
1307
1308 // Object arrays must have their base element have no subtypes
1309 while (superklass->is_obj_array_klass()) {
1310 ciType* elem = superklass->as_obj_array_klass()->element_type();
1311 superklass = elem->as_klass();
1312 }
1313 if (superklass->is_instance_klass()) {
1314 ciInstanceKlass* ik = superklass->as_instance_klass();
1315 if (ik->has_subklass() || ik->is_interface()) return nullptr;
1316 // Add a dependency if there is a chance that a subclass will be added later.
1317 if (!ik->is_final()) {
1318 phase->C->dependencies()->assert_leaf_type(ik);
1319 }
1320 }
1321
1322 // Do not fold the subtype check to an array klass pointer comparison for
1323 // value class arrays because they can have multiple refined array klasses.
1324 superklass = t2->exact_klass();
1325 assert(!superklass->is_flat_array_klass(), "Unexpected flat array klass");
1326 if (superklass->is_obj_array_klass()) {
1327 if (!superklass->as_array_klass()->is_elem_null_free() &&
1328 superklass->as_array_klass()->element_klass()->is_inlinetype()) {
1329 return nullptr;
1330 } else {
1331 // Special case for non-value arrays: They only have one (default) refined class, use it
1332 set_req_X(2, phase->makecon(t2->is_aryklassptr()->cast_to_refined_array_klass_ptr()), phase);
1333 }
1334 }
1335
1336 // Bypass the dependent load, and compare directly
1337 this->set_req_X(1, ldk2, phase);
1338
1339 return this;
1340 }
1341
1342 //=============================================================================
1343 //------------------------------sub--------------------------------------------
1344 // Simplify an CmpN (compare 2 pointers) node, based on local information.
1345 // If both inputs are constants, compare them.
1346 const Type *CmpNNode::sub( const Type *t1, const Type *t2 ) const {
1347 ShouldNotReachHere();
1348 return bottom_type();
1349 }
1350
1351 //------------------------------Ideal------------------------------------------
1352 Node *CmpNNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
1353 return nullptr;
1354 }
1355
1356 //=============================================================================
1357 //------------------------------Value------------------------------------------
1358 // Simplify an CmpF (compare 2 floats ) node, based on local information.
1359 // If both inputs are constants, compare them.
1360 const Type* CmpFNode::Value(PhaseGVN* phase) const {
1361 const Node* in1 = in(1);
1362 const Node* in2 = in(2);
1363 // Either input is TOP ==> the result is TOP
1364 const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
1365 if( t1 == Type::TOP ) return Type::TOP;
1366 const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
1367 if( t2 == Type::TOP ) return Type::TOP;
1368
1369 // Not constants? Don't know squat - even if they are the same
1370 // value! If they are NaN's they compare to LT instead of EQ.
1371 const TypeF *tf1 = t1->isa_float_constant();
1372 const TypeF *tf2 = t2->isa_float_constant();
1373 if( !tf1 || !tf2 ) return TypeInt::CC;
1374
1375 // This implements the Java bytecode fcmpl, so unordered returns -1.
1376 if( tf1->is_nan() || tf2->is_nan() )
1377 return TypeInt::CC_LT;
1378
1379 if( tf1->_f < tf2->_f ) return TypeInt::CC_LT;
1380 if( tf1->_f > tf2->_f ) return TypeInt::CC_GT;
1381 assert( tf1->_f == tf2->_f, "do not understand FP behavior" );
1382 return TypeInt::CC_EQ;
1383 }
1384
1385
1386 //=============================================================================
1387 //------------------------------Value------------------------------------------
1388 // Simplify an CmpD (compare 2 doubles ) node, based on local information.
1389 // If both inputs are constants, compare them.
1390 const Type* CmpDNode::Value(PhaseGVN* phase) const {
1391 const Node* in1 = in(1);
1392 const Node* in2 = in(2);
1393 // Either input is TOP ==> the result is TOP
1394 const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
1395 if( t1 == Type::TOP ) return Type::TOP;
1396 const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
1397 if( t2 == Type::TOP ) return Type::TOP;
1398
1399 // Not constants? Don't know squat - even if they are the same
1400 // value! If they are NaN's they compare to LT instead of EQ.
1401 const TypeD *td1 = t1->isa_double_constant();
1402 const TypeD *td2 = t2->isa_double_constant();
1403 if( !td1 || !td2 ) return TypeInt::CC;
1404
1405 // This implements the Java bytecode dcmpl, so unordered returns -1.
1406 if( td1->is_nan() || td2->is_nan() )
1407 return TypeInt::CC_LT;
1408
1409 if( td1->_d < td2->_d ) return TypeInt::CC_LT;
1410 if( td1->_d > td2->_d ) return TypeInt::CC_GT;
1411 assert( td1->_d == td2->_d, "do not understand FP behavior" );
1412 return TypeInt::CC_EQ;
1413 }
1414
1415 //------------------------------Ideal------------------------------------------
1416 Node *CmpDNode::Ideal(PhaseGVN *phase, bool can_reshape){
1417 // Check if we can change this to a CmpF and remove a ConvD2F operation.
1418 // Change (CMPD (F2D (float)) (ConD value))
1419 // To (CMPF (float) (ConF value))
1420 // Valid when 'value' does not lose precision as a float.
1421 // Benefits: eliminates conversion, does not require 24-bit mode
1422
1423 // NaNs prevent commuting operands. This transform works regardless of the
1424 // order of ConD and ConvF2D inputs by preserving the original order.
1425 int idx_f2d = 1; // ConvF2D on left side?
1426 if( in(idx_f2d)->Opcode() != Op_ConvF2D )
1427 idx_f2d = 2; // No, swap to check for reversed args
1428 int idx_con = 3-idx_f2d; // Check for the constant on other input
1429
1430 if( ConvertCmpD2CmpF &&
1431 in(idx_f2d)->Opcode() == Op_ConvF2D &&
1432 in(idx_con)->Opcode() == Op_ConD ) {
1433 const TypeD *t2 = in(idx_con)->bottom_type()->is_double_constant();
1434 double t2_value_as_double = t2->_d;
1435 float t2_value_as_float = (float)t2_value_as_double;
1436 if( t2_value_as_double == (double)t2_value_as_float ) {
1437 // Test value can be represented as a float
1438 // Eliminate the conversion to double and create new comparison
1439 Node *new_in1 = in(idx_f2d)->in(1);
1440 Node *new_in2 = phase->makecon( TypeF::make(t2_value_as_float) );
1441 if( idx_f2d != 1 ) { // Must flip args to match original order
1442 Node *tmp = new_in1;
1443 new_in1 = new_in2;
1444 new_in2 = tmp;
1445 }
1446 CmpFNode *new_cmp = (Opcode() == Op_CmpD3)
1447 ? new CmpF3Node( new_in1, new_in2 )
1448 : new CmpFNode ( new_in1, new_in2 ) ;
1449 return new_cmp; // Changed to CmpFNode
1450 }
1451 // Testing value required the precision of a double
1452 }
1453 return nullptr; // No change
1454 }
1455
1456 //=============================================================================
1457 //------------------------------Value------------------------------------------
1458 const Type* FlatArrayCheckNode::Value(PhaseGVN* phase) const {
1459 bool all_not_flat = true;
1460 for (uint i = ArrayOrKlass; i < req(); ++i) {
1461 const Type* t = phase->type(in(i));
1462 if (t == Type::TOP) {
1463 return Type::TOP;
1464 }
1465 if (t->is_ptr()->is_flat()) {
1466 // One of the input arrays is flat, check always passes
1467 return TypeInt::CC_EQ;
1468 } else if (!t->is_ptr()->is_not_flat()) {
1469 // One of the input arrays might be flat
1470 all_not_flat = false;
1471 }
1472 }
1473 if (all_not_flat) {
1474 // None of the input arrays can be flat, check always fails
1475 return TypeInt::CC_GT;
1476 }
1477 return TypeInt::CC;
1478 }
1479
1480 //------------------------------Ideal------------------------------------------
1481 Node* FlatArrayCheckNode::Ideal(PhaseGVN* phase, bool can_reshape) {
1482 bool changed = false;
1483 // Remove inputs that are known to be non-flat
1484 for (uint i = ArrayOrKlass; i < req(); ++i) {
1485 const Type* t = phase->type(in(i));
1486 if (t->isa_ptr() && t->is_ptr()->is_not_flat()) {
1487 del_req(i--);
1488 changed = true;
1489 }
1490 }
1491 return changed ? this : nullptr;
1492 }
1493
1494 //=============================================================================
1495 //------------------------------cc2logical-------------------------------------
1496 // Convert a condition code type to a logical type
1497 const Type *BoolTest::cc2logical( const Type *CC ) const {
1498 if( CC == Type::TOP ) return Type::TOP;
1499 if( CC->base() != Type::Int ) return TypeInt::BOOL; // Bottom or worse
1500 const TypeInt *ti = CC->is_int();
1501 if( ti->is_con() ) { // Only 1 kind of condition codes set?
1502 // Match low order 2 bits
1503 int tmp = ((ti->get_con()&3) == (_test&3)) ? 1 : 0;
1504 if( _test & 4 ) tmp = 1-tmp; // Optionally complement result
1505 return TypeInt::make(tmp); // Boolean result
1506 }
1507
1508 if( CC == TypeInt::CC_GE ) {
1509 if( _test == ge ) return TypeInt::ONE;
1510 if( _test == lt ) return TypeInt::ZERO;
1511 }
1512 if( CC == TypeInt::CC_LE ) {
1513 if( _test == le ) return TypeInt::ONE;
1514 if( _test == gt ) return TypeInt::ZERO;
1515 }
1516 if( CC == TypeInt::CC_NE ) {
1517 if( _test == ne ) return TypeInt::ONE;
1518 if( _test == eq ) return TypeInt::ZERO;
1519 }
1520
1521 return TypeInt::BOOL;
1522 }
1523
1524 BoolTest::mask BoolTest::unsigned_mask(BoolTest::mask btm) {
1525 switch(btm) {
1526 case eq:
1527 case ne:
1528 return btm;
1529 case lt:
1530 case le:
1531 case gt:
1532 case ge:
1533 return mask(btm | unsigned_compare);
1534 default:
1535 ShouldNotReachHere();
1536 }
1537 }
1538
1539 //------------------------------dump_spec-------------------------------------
1540 // Print special per-node info
1541 void BoolTest::dump_on(outputStream *st) const {
1542 const char *msg[] = {"eq","gt","of","lt","ne","le","nof","ge"};
1543 st->print("%s", msg[_test]);
1544 }
1545
1546 // Returns the logical AND of two tests (or 'never' if both tests can never be true).
1547 // For example, a test for 'le' followed by a test for 'lt' is equivalent with 'lt'.
1548 BoolTest::mask BoolTest::merge(BoolTest other) const {
1549 const mask res[illegal+1][illegal+1] = {
1550 // eq, gt, of, lt, ne, le, nof, ge, never, illegal
1551 {eq, never, illegal, never, never, eq, illegal, eq, never, illegal}, // eq
1552 {never, gt, illegal, never, gt, never, illegal, gt, never, illegal}, // gt
1553 {illegal, illegal, illegal, illegal, illegal, illegal, illegal, illegal, never, illegal}, // of
1554 {never, never, illegal, lt, lt, lt, illegal, never, never, illegal}, // lt
1555 {never, gt, illegal, lt, ne, lt, illegal, gt, never, illegal}, // ne
1556 {eq, never, illegal, lt, lt, le, illegal, eq, never, illegal}, // le
1557 {illegal, illegal, illegal, illegal, illegal, illegal, illegal, illegal, never, illegal}, // nof
1558 {eq, gt, illegal, never, gt, eq, illegal, ge, never, illegal}, // ge
1559 {never, never, never, never, never, never, never, never, never, illegal}, // never
1560 {illegal, illegal, illegal, illegal, illegal, illegal, illegal, illegal, illegal, illegal}}; // illegal
1561 return res[_test][other._test];
1562 }
1563
1564 //=============================================================================
1565 uint BoolNode::hash() const { return (Node::hash() << 3)|(_test._test+1); }
1566 uint BoolNode::size_of() const { return sizeof(BoolNode); }
1567
1568 //------------------------------operator==-------------------------------------
1569 bool BoolNode::cmp( const Node &n ) const {
1570 const BoolNode *b = (const BoolNode *)&n; // Cast up
1571 return (_test._test == b->_test._test);
1572 }
1573
1574 //-------------------------------make_predicate--------------------------------
1575 Node* BoolNode::make_predicate(Node* test_value, PhaseGVN* phase) {
1576 if (test_value->is_Con()) return test_value;
1577 if (test_value->is_Bool()) return test_value;
1578 if (test_value->is_CMove() &&
1579 test_value->in(CMoveNode::Condition)->is_Bool()) {
1580 BoolNode* bol = test_value->in(CMoveNode::Condition)->as_Bool();
1581 const Type* ftype = phase->type(test_value->in(CMoveNode::IfFalse));
1582 const Type* ttype = phase->type(test_value->in(CMoveNode::IfTrue));
1583 if (ftype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ttype)) {
1584 return bol;
1585 } else if (ttype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ftype)) {
1586 return phase->transform( bol->negate(phase) );
1587 }
1588 // Else fall through. The CMove gets in the way of the test.
1589 // It should be the case that make_predicate(bol->as_int_value()) == bol.
1590 }
1591 Node* cmp = new CmpINode(test_value, phase->intcon(0));
1592 cmp = phase->transform(cmp);
1593 Node* bol = new BoolNode(cmp, BoolTest::ne);
1594 return phase->transform(bol);
1595 }
1596
1597 //--------------------------------as_int_value---------------------------------
1598 Node* BoolNode::as_int_value(PhaseGVN* phase) {
1599 // Inverse to make_predicate. The CMove probably boils down to a Conv2B.
1600 Node* cmov = CMoveNode::make(this, phase->intcon(0), phase->intcon(1), TypeInt::BOOL);
1601 return phase->transform(cmov);
1602 }
1603
1604 //----------------------------------negate-------------------------------------
1605 BoolNode* BoolNode::negate(PhaseGVN* phase) {
1606 return new BoolNode(in(1), _test.negate());
1607 }
1608
1609 // Change "bool eq/ne (cmp (add/sub A B) C)" into false/true if add/sub
1610 // overflows and we can prove that C is not in the two resulting ranges.
1611 // This optimization is similar to the one performed by CmpUNode::Value().
1612 Node* BoolNode::fold_cmpI(PhaseGVN* phase, SubNode* cmp, Node* cmp1, int cmp_op,
1613 int cmp1_op, const TypeInt* cmp2_type) {
1614 // Only optimize eq/ne integer comparison of add/sub
1615 if((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1616 (cmp_op == Op_CmpI) && (cmp1_op == Op_AddI || cmp1_op == Op_SubI)) {
1617 // Skip cases were inputs of add/sub are not integers or of bottom type
1618 const TypeInt* r0 = phase->type(cmp1->in(1))->isa_int();
1619 const TypeInt* r1 = phase->type(cmp1->in(2))->isa_int();
1620 if ((r0 != nullptr) && (r0 != TypeInt::INT) &&
1621 (r1 != nullptr) && (r1 != TypeInt::INT) &&
1622 (cmp2_type != TypeInt::INT)) {
1623 // Compute exact (long) type range of add/sub result
1624 jlong lo_long = r0->_lo;
1625 jlong hi_long = r0->_hi;
1626 if (cmp1_op == Op_AddI) {
1627 lo_long += r1->_lo;
1628 hi_long += r1->_hi;
1629 } else {
1630 lo_long -= r1->_hi;
1631 hi_long -= r1->_lo;
1632 }
1633 // Check for over-/underflow by casting to integer
1634 int lo_int = (int)lo_long;
1635 int hi_int = (int)hi_long;
1636 bool underflow = lo_long != (jlong)lo_int;
1637 bool overflow = hi_long != (jlong)hi_int;
1638 if ((underflow != overflow) && (hi_int < lo_int)) {
1639 // Overflow on one boundary, compute resulting type ranges:
1640 // tr1 [MIN_INT, hi_int] and tr2 [lo_int, MAX_INT]
1641 int w = MAX2(r0->_widen, r1->_widen); // _widen does not matter here
1642 const TypeInt* tr1 = TypeInt::make(min_jint, hi_int, w);
1643 const TypeInt* tr2 = TypeInt::make(lo_int, max_jint, w);
1644 // Compare second input of cmp to both type ranges
1645 const Type* sub_tr1 = cmp->sub(tr1, cmp2_type);
1646 const Type* sub_tr2 = cmp->sub(tr2, cmp2_type);
1647 if (sub_tr1 == TypeInt::CC_LT && sub_tr2 == TypeInt::CC_GT) {
1648 // The result of the add/sub will never equal cmp2. Replace BoolNode
1649 // by false (0) if it tests for equality and by true (1) otherwise.
1650 return ConINode::make((_test._test == BoolTest::eq) ? 0 : 1);
1651 }
1652 }
1653 }
1654 }
1655 return nullptr;
1656 }
1657
1658 static bool is_counted_loop_cmp(Node *cmp) {
1659 Node *n = cmp->in(1)->in(1);
1660 return n != nullptr &&
1661 n->is_Phi() &&
1662 n->in(0) != nullptr &&
1663 n->in(0)->is_CountedLoop() &&
1664 n->in(0)->as_CountedLoop()->phi() == n;
1665 }
1666
1667 //------------------------------Ideal------------------------------------------
1668 Node *BoolNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1669 // Change "bool tst (cmp con x)" into "bool ~tst (cmp x con)".
1670 // This moves the constant to the right. Helps value-numbering.
1671 Node *cmp = in(1);
1672 if( !cmp->is_Sub() ) return nullptr;
1673 int cop = cmp->Opcode();
1674 if( cop == Op_FastLock || cop == Op_FastUnlock ||
1675 cmp->is_SubTypeCheck() || cop == Op_VectorTest ) {
1676 return nullptr;
1677 }
1678 Node *cmp1 = cmp->in(1);
1679 Node *cmp2 = cmp->in(2);
1680 if( !cmp1 ) return nullptr;
1681
1682 if (_test._test == BoolTest::overflow || _test._test == BoolTest::no_overflow) {
1683 return nullptr;
1684 }
1685
1686 const int cmp1_op = cmp1->Opcode();
1687 const int cmp2_op = cmp2->Opcode();
1688
1689 // Constant on left?
1690 Node *con = cmp1;
1691 // Move constants to the right of compare's to canonicalize.
1692 // Do not muck with Opaque1 nodes, as this indicates a loop
1693 // guard that cannot change shape.
1694 if (con->is_Con() && !cmp2->is_Con() && cmp2_op != Op_OpaqueZeroTripGuard &&
1695 // Because of NaN's, CmpD and CmpF are not commutative
1696 cop != Op_CmpD && cop != Op_CmpF &&
1697 // Protect against swapping inputs to a compare when it is used by a
1698 // counted loop exit, which requires maintaining the loop-limit as in(2)
1699 !is_counted_loop_exit_test() ) {
1700 // Ok, commute the constant to the right of the cmp node.
1701 // Clone the Node, getting a new Node of the same class
1702 cmp = cmp->clone();
1703 // Swap inputs to the clone
1704 cmp->swap_edges(1, 2);
1705 cmp = phase->transform( cmp );
1706 return new BoolNode( cmp, _test.commute() );
1707 }
1708
1709 // Change "bool eq/ne (cmp (cmove (bool tst (cmp2)) 1 0) 0)" into "bool tst/~tst (cmp2)"
1710 if (cop == Op_CmpI &&
1711 (_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1712 cmp1_op == Op_CMoveI && cmp2->find_int_con(1) == 0) {
1713 // 0 should be on the true branch
1714 if (cmp1->in(CMoveNode::Condition)->is_Bool() &&
1715 cmp1->in(CMoveNode::IfTrue)->find_int_con(1) == 0 &&
1716 cmp1->in(CMoveNode::IfFalse)->find_int_con(0) != 0) {
1717 BoolNode* target = cmp1->in(CMoveNode::Condition)->as_Bool();
1718 return new BoolNode(target->in(1),
1719 (_test._test == BoolTest::eq) ? target->_test._test :
1720 target->_test.negate());
1721 }
1722 }
1723
1724 // Change "bool eq/ne (cmp (and X 16) 16)" into "bool ne/eq (cmp (and X 16) 0)".
1725 if (cop == Op_CmpI &&
1726 (_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1727 cmp1_op == Op_AndI && cmp2_op == Op_ConI &&
1728 cmp1->in(2)->Opcode() == Op_ConI) {
1729 const TypeInt *t12 = phase->type(cmp2)->isa_int();
1730 const TypeInt *t112 = phase->type(cmp1->in(2))->isa_int();
1731 if (t12 && t12->is_con() && t112 && t112->is_con() &&
1732 t12->get_con() == t112->get_con() && is_power_of_2(t12->get_con())) {
1733 Node *ncmp = phase->transform(new CmpINode(cmp1, phase->intcon(0)));
1734 return new BoolNode(ncmp, _test.negate());
1735 }
1736 }
1737
1738 // Same for long type: change "bool eq/ne (cmp (and X 16) 16)" into "bool ne/eq (cmp (and X 16) 0)".
1739 if (cop == Op_CmpL &&
1740 (_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1741 cmp1_op == Op_AndL && cmp2_op == Op_ConL &&
1742 cmp1->in(2)->Opcode() == Op_ConL) {
1743 const TypeLong *t12 = phase->type(cmp2)->isa_long();
1744 const TypeLong *t112 = phase->type(cmp1->in(2))->isa_long();
1745 if (t12 && t12->is_con() && t112 && t112->is_con() &&
1746 t12->get_con() == t112->get_con() && is_power_of_2(t12->get_con())) {
1747 Node *ncmp = phase->transform(new CmpLNode(cmp1, phase->longcon(0)));
1748 return new BoolNode(ncmp, _test.negate());
1749 }
1750 }
1751
1752 // Change "cmp (add X min_jint) (add Y min_jint)" into "cmpu X Y"
1753 // and "cmp (add X min_jint) c" into "cmpu X (c + min_jint)"
1754 if (cop == Op_CmpI &&
1755 cmp1_op == Op_AddI &&
1756 phase->type(cmp1->in(2)) == TypeInt::MIN &&
1757 !is_cloop_condition(this)) {
1758 if (cmp2_op == Op_ConI) {
1759 Node* ncmp2 = phase->intcon(java_add(cmp2->get_int(), min_jint));
1760 Node* ncmp = phase->transform(new CmpUNode(cmp1->in(1), ncmp2));
1761 return new BoolNode(ncmp, _test._test);
1762 } else if (cmp2_op == Op_AddI &&
1763 phase->type(cmp2->in(2)) == TypeInt::MIN &&
1764 !is_cloop_condition(this)) {
1765 Node* ncmp = phase->transform(new CmpUNode(cmp1->in(1), cmp2->in(1)));
1766 return new BoolNode(ncmp, _test._test);
1767 }
1768 }
1769
1770 // Change "cmp (add X min_jlong) (add Y min_jlong)" into "cmpu X Y"
1771 // and "cmp (add X min_jlong) c" into "cmpu X (c + min_jlong)"
1772 if (cop == Op_CmpL &&
1773 cmp1_op == Op_AddL &&
1774 phase->type(cmp1->in(2)) == TypeLong::MIN &&
1775 !is_cloop_condition(this)) {
1776 if (cmp2_op == Op_ConL) {
1777 Node* ncmp2 = phase->longcon(java_add(cmp2->get_long(), min_jlong));
1778 Node* ncmp = phase->transform(new CmpULNode(cmp1->in(1), ncmp2));
1779 return new BoolNode(ncmp, _test._test);
1780 } else if (cmp2_op == Op_AddL &&
1781 phase->type(cmp2->in(2)) == TypeLong::MIN &&
1782 !is_cloop_condition(this)) {
1783 Node* ncmp = phase->transform(new CmpULNode(cmp1->in(1), cmp2->in(1)));
1784 return new BoolNode(ncmp, _test._test);
1785 }
1786 }
1787
1788 // Change "bool eq/ne (cmp (xor X 1) 0)" into "bool ne/eq (cmp X 0)".
1789 // The XOR-1 is an idiom used to flip the sense of a bool. We flip the
1790 // test instead.
1791 const TypeInt* cmp2_type = phase->type(cmp2)->isa_int();
1792 if (cmp2_type == nullptr) return nullptr;
1793 Node* j_xor = cmp1;
1794 if( cmp2_type == TypeInt::ZERO &&
1795 cmp1_op == Op_XorI &&
1796 j_xor->in(1) != j_xor && // An xor of itself is dead
1797 phase->type( j_xor->in(1) ) == TypeInt::BOOL &&
1798 phase->type( j_xor->in(2) ) == TypeInt::ONE &&
1799 (_test._test == BoolTest::eq ||
1800 _test._test == BoolTest::ne) ) {
1801 Node *ncmp = phase->transform(new CmpINode(j_xor->in(1),cmp2));
1802 return new BoolNode( ncmp, _test.negate() );
1803 }
1804
1805 // Transform: "((x & (m - 1)) <u m)" or "(((m - 1) & x) <u m)" into "(m >u 0)"
1806 // This is case [CMPU_MASK] which is further described at the method comment of BoolNode::Value_cmpu_and_mask().
1807 if (cop == Op_CmpU && _test._test == BoolTest::lt && cmp1_op == Op_AndI) {
1808 Node* m = cmp2; // RHS: m
1809 for (int add_idx = 1; add_idx <= 2; add_idx++) { // LHS: "(m + (-1)) & x" or "x & (m + (-1))"?
1810 Node* maybe_m_minus_1 = cmp1->in(add_idx);
1811 if (maybe_m_minus_1->Opcode() == Op_AddI &&
1812 maybe_m_minus_1->in(2)->find_int_con(0) == -1 &&
1813 maybe_m_minus_1->in(1) == m) {
1814 Node* m_cmpu_0 = phase->transform(new CmpUNode(m, phase->intcon(0)));
1815 return new BoolNode(m_cmpu_0, BoolTest::gt);
1816 }
1817 }
1818 }
1819
1820 // Change x u< 1 or x u<= 0 to x == 0
1821 // and x u> 0 or u>= 1 to x != 0
1822 if (cop == Op_CmpU &&
1823 cmp1_op != Op_LoadRange &&
1824 (((_test._test == BoolTest::lt || _test._test == BoolTest::ge) &&
1825 cmp2->find_int_con(-1) == 1) ||
1826 ((_test._test == BoolTest::le || _test._test == BoolTest::gt) &&
1827 cmp2->find_int_con(-1) == 0))) {
1828 Node* ncmp = phase->transform(new CmpINode(cmp1, phase->intcon(0)));
1829 return new BoolNode(ncmp, _test.is_less() ? BoolTest::eq : BoolTest::ne);
1830 }
1831
1832 // Change (arraylength <= 0) or (arraylength == 0)
1833 // into (arraylength u<= 0)
1834 // Also change (arraylength != 0) into (arraylength u> 0)
1835 // The latter version matches the code pattern generated for
1836 // array range checks, which will more likely be optimized later.
1837 if (cop == Op_CmpI &&
1838 cmp1_op == Op_LoadRange &&
1839 cmp2->find_int_con(-1) == 0) {
1840 if (_test._test == BoolTest::le || _test._test == BoolTest::eq) {
1841 Node* ncmp = phase->transform(new CmpUNode(cmp1, cmp2));
1842 return new BoolNode(ncmp, BoolTest::le);
1843 } else if (_test._test == BoolTest::ne) {
1844 Node* ncmp = phase->transform(new CmpUNode(cmp1, cmp2));
1845 return new BoolNode(ncmp, BoolTest::gt);
1846 }
1847 }
1848
1849 // Change "bool eq/ne (cmp (Conv2B X) 0)" into "bool eq/ne (cmp X 0)".
1850 // This is a standard idiom for branching on a boolean value.
1851 Node *c2b = cmp1;
1852 if( cmp2_type == TypeInt::ZERO &&
1853 cmp1_op == Op_Conv2B &&
1854 (_test._test == BoolTest::eq ||
1855 _test._test == BoolTest::ne) ) {
1856 Node *ncmp = phase->transform(phase->type(c2b->in(1))->isa_int()
1857 ? (Node*)new CmpINode(c2b->in(1),cmp2)
1858 : (Node*)new CmpPNode(c2b->in(1),phase->makecon(TypePtr::NULL_PTR))
1859 );
1860 return new BoolNode( ncmp, _test._test );
1861 }
1862
1863 // Comparing a SubI against a zero is equal to comparing the SubI
1864 // arguments directly. This only works for eq and ne comparisons
1865 // due to possible integer overflow.
1866 if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1867 (cop == Op_CmpI) &&
1868 (cmp1_op == Op_SubI) &&
1869 ( cmp2_type == TypeInt::ZERO ) ) {
1870 Node *ncmp = phase->transform( new CmpINode(cmp1->in(1),cmp1->in(2)));
1871 return new BoolNode( ncmp, _test._test );
1872 }
1873
1874 // Same as above but with and AddI of a constant
1875 if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1876 cop == Op_CmpI &&
1877 cmp1_op == Op_AddI &&
1878 cmp1->in(2) != nullptr &&
1879 phase->type(cmp1->in(2))->isa_int() &&
1880 phase->type(cmp1->in(2))->is_int()->is_con() &&
1881 cmp2_type == TypeInt::ZERO &&
1882 !is_counted_loop_cmp(cmp) // modifying the exit test of a counted loop messes the counted loop shape
1883 ) {
1884 const TypeInt* cmp1_in2 = phase->type(cmp1->in(2))->is_int();
1885 Node *ncmp = phase->transform( new CmpINode(cmp1->in(1),phase->intcon(-cmp1_in2->_hi)));
1886 return new BoolNode( ncmp, _test._test );
1887 }
1888
1889 // Change "bool eq/ne (cmp (phi (X -X) 0))" into "bool eq/ne (cmp X 0)"
1890 // since zero check of conditional negation of an integer is equal to
1891 // zero check of the integer directly.
1892 if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1893 (cop == Op_CmpI) &&
1894 (cmp2_type == TypeInt::ZERO) &&
1895 (cmp1_op == Op_Phi)) {
1896 // There should be a diamond phi with true path at index 1 or 2
1897 PhiNode *phi = cmp1->as_Phi();
1898 int idx_true = phi->is_diamond_phi();
1899 if (idx_true != 0) {
1900 // True input is in(idx_true) while false input is in(3 - idx_true)
1901 Node *tin = phi->in(idx_true);
1902 Node *fin = phi->in(3 - idx_true);
1903 if ((tin->Opcode() == Op_SubI) &&
1904 (phase->type(tin->in(1)) == TypeInt::ZERO) &&
1905 (tin->in(2) == fin)) {
1906 // Found conditional negation at true path, create a new CmpINode without that
1907 Node *ncmp = phase->transform(new CmpINode(fin, cmp2));
1908 return new BoolNode(ncmp, _test._test);
1909 }
1910 if ((fin->Opcode() == Op_SubI) &&
1911 (phase->type(fin->in(1)) == TypeInt::ZERO) &&
1912 (fin->in(2) == tin)) {
1913 // Found conditional negation at false path, create a new CmpINode without that
1914 Node *ncmp = phase->transform(new CmpINode(tin, cmp2));
1915 return new BoolNode(ncmp, _test._test);
1916 }
1917 }
1918 }
1919
1920 // Change (-A vs 0) into (A vs 0) by commuting the test. Disallow in the
1921 // most general case because negating 0x80000000 does nothing. Needed for
1922 // the CmpF3/SubI/CmpI idiom.
1923 if( cop == Op_CmpI &&
1924 cmp1_op == Op_SubI &&
1925 cmp2_type == TypeInt::ZERO &&
1926 phase->type( cmp1->in(1) ) == TypeInt::ZERO &&
1927 phase->type( cmp1->in(2) )->higher_equal(TypeInt::SYMINT) ) {
1928 Node *ncmp = phase->transform( new CmpINode(cmp1->in(2),cmp2));
1929 return new BoolNode( ncmp, _test.commute() );
1930 }
1931
1932 // Try to optimize signed integer comparison
1933 return fold_cmpI(phase, cmp->as_Sub(), cmp1, cop, cmp1_op, cmp2_type);
1934
1935 // The transformation below is not valid for either signed or unsigned
1936 // comparisons due to wraparound concerns at MAX_VALUE and MIN_VALUE.
1937 // This transformation can be resurrected when we are able to
1938 // make inferences about the range of values being subtracted from
1939 // (or added to) relative to the wraparound point.
1940 //
1941 // // Remove +/-1's if possible.
1942 // // "X <= Y-1" becomes "X < Y"
1943 // // "X+1 <= Y" becomes "X < Y"
1944 // // "X < Y+1" becomes "X <= Y"
1945 // // "X-1 < Y" becomes "X <= Y"
1946 // // Do not this to compares off of the counted-loop-end. These guys are
1947 // // checking the trip counter and they want to use the post-incremented
1948 // // counter. If they use the PRE-incremented counter, then the counter has
1949 // // to be incremented in a private block on a loop backedge.
1950 // if( du && du->cnt(this) && du->out(this)[0]->Opcode() == Op_CountedLoopEnd )
1951 // return nullptr;
1952 // #ifndef PRODUCT
1953 // // Do not do this in a wash GVN pass during verification.
1954 // // Gets triggered by too many simple optimizations to be bothered with
1955 // // re-trying it again and again.
1956 // if( !phase->allow_progress() ) return nullptr;
1957 // #endif
1958 // // Not valid for unsigned compare because of corner cases in involving zero.
1959 // // For example, replacing "X-1 <u Y" with "X <=u Y" fails to throw an
1960 // // exception in case X is 0 (because 0-1 turns into 4billion unsigned but
1961 // // "0 <=u Y" is always true).
1962 // if( cmp->Opcode() == Op_CmpU ) return nullptr;
1963 // int cmp2_op = cmp2->Opcode();
1964 // if( _test._test == BoolTest::le ) {
1965 // if( cmp1_op == Op_AddI &&
1966 // phase->type( cmp1->in(2) ) == TypeInt::ONE )
1967 // return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::lt );
1968 // else if( cmp2_op == Op_AddI &&
1969 // phase->type( cmp2->in(2) ) == TypeInt::MINUS_1 )
1970 // return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::lt );
1971 // } else if( _test._test == BoolTest::lt ) {
1972 // if( cmp1_op == Op_AddI &&
1973 // phase->type( cmp1->in(2) ) == TypeInt::MINUS_1 )
1974 // return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::le );
1975 // else if( cmp2_op == Op_AddI &&
1976 // phase->type( cmp2->in(2) ) == TypeInt::ONE )
1977 // return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::le );
1978 // }
1979 }
1980
1981 // We use the following Lemmas/insights for the following two transformations (1) and (2):
1982 // x & y <=u y, for any x and y (Lemma 1, masking always results in a smaller unsigned number)
1983 // y <u y + 1 is always true if y != -1 (Lemma 2, (uint)(-1 + 1) == (uint)(UINT_MAX + 1) which overflows)
1984 // y <u 0 is always false for any y (Lemma 3, 0 == UINT_MIN and nothing can be smaller than that)
1985 //
1986 // (1a) Always: Change ((x & m) <=u m ) or ((m & x) <=u m ) to always true (true by Lemma 1)
1987 // (1b) If m != -1: Change ((x & m) <u m + 1) or ((m & x) <u m + 1) to always true:
1988 // x & m <=u m is always true // (Lemma 1)
1989 // x & m <=u m <u m + 1 is always true // (Lemma 2: m <u m + 1, if m != -1)
1990 //
1991 // A counter example for (1b), if we allowed m == -1:
1992 // (x & m) <u m + 1
1993 // (x & -1) <u 0
1994 // x <u 0
1995 // which is false for any x (Lemma 3)
1996 //
1997 // (2) Change ((x & (m - 1)) <u m) or (((m - 1) & x) <u m) to (m >u 0)
1998 // This is the off-by-one variant of the above.
1999 //
2000 // We now prove that this replacement is correct. This is the same as proving
2001 // "m >u 0" if and only if "x & (m - 1) <u m", i.e. "m >u 0 <=> x & (m - 1) <u m"
2002 //
2003 // We use (Lemma 1) and (Lemma 3) from above.
2004 //
2005 // Case "x & (m - 1) <u m => m >u 0":
2006 // We prove this by contradiction:
2007 // Assume m <=u 0 which is equivalent to m == 0:
2008 // and thus
2009 // x & (m - 1) <u m = 0 // m == 0
2010 // y <u 0 // y = x & (m - 1)
2011 // by Lemma 3, this is always false, i.e. a contradiction to our assumption.
2012 //
2013 // Case "m >u 0 => x & (m - 1) <u m":
2014 // x & (m - 1) <=u (m - 1) // (Lemma 1)
2015 // x & (m - 1) <=u (m - 1) <u m // Using assumption m >u 0, no underflow of "m - 1"
2016 //
2017 //
2018 // Note that the signed version of "m > 0":
2019 // m > 0 <=> x & (m - 1) <u m
2020 // does not hold:
2021 // Assume m == -1 and x == -1:
2022 // x & (m - 1) <u m
2023 // -1 & -2 <u -1
2024 // -2 <u -1
2025 // UINT_MAX - 1 <u UINT_MAX // Signed to unsigned numbers
2026 // which is true while
2027 // m > 0
2028 // is false which is a contradiction.
2029 //
2030 // (1a) and (1b) is covered by this method since we can directly return a true value as type while (2) is covered
2031 // in BoolNode::Ideal since we create a new non-constant node (see [CMPU_MASK]).
2032 const Type* BoolNode::Value_cmpu_and_mask(PhaseValues* phase) const {
2033 Node* cmp = in(1);
2034 if (cmp != nullptr && cmp->Opcode() == Op_CmpU) {
2035 Node* cmp1 = cmp->in(1);
2036 Node* cmp2 = cmp->in(2);
2037
2038 if (cmp1->Opcode() == Op_AndI) {
2039 Node* m = nullptr;
2040 if (_test._test == BoolTest::le) {
2041 // (1a) "((x & m) <=u m)", cmp2 = m
2042 m = cmp2;
2043 } else if (_test._test == BoolTest::lt && cmp2->Opcode() == Op_AddI && cmp2->in(2)->find_int_con(0) == 1) {
2044 // (1b) "(x & m) <u m + 1" and "(m & x) <u m + 1", cmp2 = m + 1
2045 Node* rhs_m = cmp2->in(1);
2046 const TypeInt* rhs_m_type = phase->type(rhs_m)->isa_int();
2047 if (rhs_m_type != nullptr && (rhs_m_type->_lo > -1 || rhs_m_type->_hi < -1)) {
2048 // Exclude any case where m == -1 is possible.
2049 m = rhs_m;
2050 }
2051 }
2052
2053 if (cmp1->in(2) == m || cmp1->in(1) == m) {
2054 return TypeInt::ONE;
2055 }
2056 }
2057 }
2058
2059 return nullptr;
2060 }
2061
2062 // Simplify a Bool (convert condition codes to boolean (1 or 0)) node,
2063 // based on local information. If the input is constant, do it.
2064 const Type* BoolNode::Value(PhaseGVN* phase) const {
2065 const Type* input_type = phase->type(in(1));
2066 if (input_type == Type::TOP) {
2067 return Type::TOP;
2068 }
2069 const Type* t = Value_cmpu_and_mask(phase);
2070 if (t != nullptr) {
2071 return t;
2072 }
2073
2074 return _test.cc2logical(input_type);
2075 }
2076
2077 #ifndef PRODUCT
2078 //------------------------------dump_spec--------------------------------------
2079 // Dump special per-node info
2080 void BoolNode::dump_spec(outputStream *st) const {
2081 st->print("[");
2082 _test.dump_on(st);
2083 st->print("]");
2084 }
2085 #endif
2086
2087 //----------------------is_counted_loop_exit_test------------------------------
2088 // Returns true if node is used by a counted loop node.
2089 bool BoolNode::is_counted_loop_exit_test() {
2090 for( DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++ ) {
2091 Node* use = fast_out(i);
2092 if (use->is_CountedLoopEnd()) {
2093 return true;
2094 }
2095 }
2096 return false;
2097 }
2098
2099 //=============================================================================
2100 //------------------------------Value------------------------------------------
2101 const Type* AbsNode::Value(PhaseGVN* phase) const {
2102 const Type* t1 = phase->type(in(1));
2103 if (t1 == Type::TOP) return Type::TOP;
2104
2105 switch (t1->base()) {
2106 case Type::Int: {
2107 const TypeInt* ti = t1->is_int();
2108 if (ti->is_con()) {
2109 return TypeInt::make(g_uabs(ti->get_con()));
2110 }
2111 break;
2112 }
2113 case Type::Long: {
2114 const TypeLong* tl = t1->is_long();
2115 if (tl->is_con()) {
2116 return TypeLong::make(g_uabs(tl->get_con()));
2117 }
2118 break;
2119 }
2120 case Type::FloatCon:
2121 return TypeF::make(abs(t1->getf()));
2122 case Type::DoubleCon:
2123 return TypeD::make(abs(t1->getd()));
2124 default:
2125 break;
2126 }
2127
2128 return bottom_type();
2129 }
2130
2131 //------------------------------Identity----------------------------------------
2132 Node* AbsNode::Identity(PhaseGVN* phase) {
2133 Node* in1 = in(1);
2134 // No need to do abs for non-negative values
2135 if (phase->type(in1)->higher_equal(TypeInt::POS) ||
2136 phase->type(in1)->higher_equal(TypeLong::POS)) {
2137 return in1;
2138 }
2139 // Convert "abs(abs(x))" into "abs(x)"
2140 if (in1->Opcode() == Opcode()) {
2141 return in1;
2142 }
2143 return this;
2144 }
2145
2146 //------------------------------Ideal------------------------------------------
2147 Node* AbsNode::Ideal(PhaseGVN* phase, bool can_reshape) {
2148 Node* in1 = in(1);
2149 // Convert "abs(0-x)" into "abs(x)"
2150 if (in1->is_Sub() && phase->type(in1->in(1))->is_zero_type()) {
2151 set_req_X(1, in1->in(2), phase);
2152 return this;
2153 }
2154 return nullptr;
2155 }
2156
2157 //=============================================================================
2158 //------------------------------Value------------------------------------------
2159 // Compute sqrt
2160 const Type* SqrtDNode::Value(PhaseGVN* phase) const {
2161 const Type *t1 = phase->type( in(1) );
2162 if( t1 == Type::TOP ) return Type::TOP;
2163 if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
2164 double d = t1->getd();
2165 if( d < 0.0 ) return Type::DOUBLE;
2166 return TypeD::make( sqrt( d ) );
2167 }
2168
2169 const Type* SqrtFNode::Value(PhaseGVN* phase) const {
2170 const Type *t1 = phase->type( in(1) );
2171 if( t1 == Type::TOP ) return Type::TOP;
2172 if( t1->base() != Type::FloatCon ) return Type::FLOAT;
2173 float f = t1->getf();
2174 if( f < 0.0f ) return Type::FLOAT;
2175 return TypeF::make( (float)sqrt( (double)f ) );
2176 }
2177
2178 const Type* SqrtHFNode::Value(PhaseGVN* phase) const {
2179 const Type* t1 = phase->type(in(1));
2180 if (t1 == Type::TOP) { return Type::TOP; }
2181 if (t1->base() != Type::HalfFloatCon) { return Type::HALF_FLOAT; }
2182 float f = t1->getf();
2183 if (f < 0.0f) return Type::HALF_FLOAT;
2184 return TypeH::make((float)sqrt((double)f));
2185 }
2186
2187 static const Type* reverse_bytes(int opcode, const Type* con) {
2188 switch (opcode) {
2189 // It is valid in bytecode to load any int and pass it to a method that expects a smaller type (i.e., short, char).
2190 // Let's cast the value to match the Java behavior.
2191 case Op_ReverseBytesS: return TypeInt::make(byteswap(static_cast<jshort>(con->is_int()->get_con())));
2192 case Op_ReverseBytesUS: return TypeInt::make(byteswap(static_cast<jchar>(con->is_int()->get_con())));
2193 case Op_ReverseBytesI: return TypeInt::make(byteswap(con->is_int()->get_con()));
2194 case Op_ReverseBytesL: return TypeLong::make(byteswap(con->is_long()->get_con()));
2195 default: ShouldNotReachHere();
2196 }
2197 }
2198
2199 const Type* ReverseBytesNode::Value(PhaseGVN* phase) const {
2200 const Type* type = phase->type(in(1));
2201 if (type == Type::TOP) {
2202 return Type::TOP;
2203 }
2204 if (type->singleton()) {
2205 return reverse_bytes(Opcode(), type);
2206 }
2207 return bottom_type();
2208 }
2209
2210 const Type* ReverseINode::Value(PhaseGVN* phase) const {
2211 const Type *t1 = phase->type( in(1) );
2212 if (t1 == Type::TOP) {
2213 return Type::TOP;
2214 }
2215 const TypeInt* t1int = t1->isa_int();
2216 if (t1int && t1int->is_con()) {
2217 jint res = reverse_bits(t1int->get_con());
2218 return TypeInt::make(res);
2219 }
2220 return bottom_type();
2221 }
2222
2223 const Type* ReverseLNode::Value(PhaseGVN* phase) const {
2224 const Type *t1 = phase->type( in(1) );
2225 if (t1 == Type::TOP) {
2226 return Type::TOP;
2227 }
2228 const TypeLong* t1long = t1->isa_long();
2229 if (t1long && t1long->is_con()) {
2230 jlong res = reverse_bits(t1long->get_con());
2231 return TypeLong::make(res);
2232 }
2233 return bottom_type();
2234 }
2235
2236 Node* ReverseINode::Identity(PhaseGVN* phase) {
2237 if (in(1)->Opcode() == Op_ReverseI) {
2238 return in(1)->in(1);
2239 }
2240 return this;
2241 }
2242
2243 Node* ReverseLNode::Identity(PhaseGVN* phase) {
2244 if (in(1)->Opcode() == Op_ReverseL) {
2245 return in(1)->in(1);
2246 }
2247 return this;
2248 }