1 /*
2 * Copyright (c) 1997, 2020, 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 "precompiled.hpp"
26 #include "compiler/compileLog.hpp"
27 #include "gc/shared/barrierSet.hpp"
28 #include "gc/shared/c2/barrierSetC2.hpp"
29 #include "memory/allocation.inline.hpp"
30 #include "opto/addnode.hpp"
31 #include "opto/callnode.hpp"
32 #include "opto/cfgnode.hpp"
33 #include "opto/loopnode.hpp"
34 #include "opto/matcher.hpp"
35 #include "opto/movenode.hpp"
36 #include "opto/mulnode.hpp"
37 #include "opto/opcodes.hpp"
38 #include "opto/phaseX.hpp"
39 #include "opto/subnode.hpp"
40 #include "runtime/sharedRuntime.hpp"
41
42 // Portions of code courtesy of Clifford Click
43
44 // Optimization - Graph Style
45
46 #include "math.h"
47
48 //=============================================================================
49 //------------------------------Identity---------------------------------------
50 // If right input is a constant 0, return the left input.
Identity(PhaseGVN * phase)51 Node* SubNode::Identity(PhaseGVN* phase) {
52 assert(in(1) != this, "Must already have called Value");
53 assert(in(2) != this, "Must already have called Value");
54
55 // Remove double negation
56 const Type *zero = add_id();
57 if( phase->type( in(1) )->higher_equal( zero ) &&
58 in(2)->Opcode() == Opcode() &&
59 phase->type( in(2)->in(1) )->higher_equal( zero ) ) {
60 return in(2)->in(2);
61 }
62
63 // Convert "(X+Y) - Y" into X and "(X+Y) - X" into Y
64 if (in(1)->Opcode() == Op_AddI) {
65 if (in(1)->in(2) == in(2)) {
66 return in(1)->in(1);
67 }
68 if (in(1)->in(1) == in(2)) {
69 return in(1)->in(2);
70 }
71
72 // Also catch: "(X + Opaque2(Y)) - Y". In this case, 'Y' is a loop-varying
73 // trip counter and X is likely to be loop-invariant (that's how O2 Nodes
74 // are originally used, although the optimizer sometimes jiggers things).
75 // This folding through an O2 removes a loop-exit use of a loop-varying
76 // value and generally lowers register pressure in and around the loop.
77 if (in(1)->in(2)->Opcode() == Op_Opaque2 && in(1)->in(2)->in(1) == in(2)) {
78 return in(1)->in(1);
79 }
80 }
81
82 return ( phase->type( in(2) )->higher_equal( zero ) ) ? in(1) : this;
83 }
84
85 //------------------------------Value------------------------------------------
86 // A subtract node differences it's two inputs.
Value_common(PhaseTransform * phase) const87 const Type* SubNode::Value_common(PhaseTransform *phase) const {
88 const Node* in1 = in(1);
89 const Node* in2 = in(2);
90 // Either input is TOP ==> the result is TOP
91 const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
92 if( t1 == Type::TOP ) return Type::TOP;
93 const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
94 if( t2 == Type::TOP ) return Type::TOP;
95
96 // Not correct for SubFnode and AddFNode (must check for infinity)
97 // Equal? Subtract is zero
98 if (in1->eqv_uncast(in2)) return add_id();
99
100 // Either input is BOTTOM ==> the result is the local BOTTOM
101 if( t1 == Type::BOTTOM || t2 == Type::BOTTOM )
102 return bottom_type();
103
104 return NULL;
105 }
106
Value(PhaseGVN * phase) const107 const Type* SubNode::Value(PhaseGVN* phase) const {
108 const Type* t = Value_common(phase);
109 if (t != NULL) {
110 return t;
111 }
112 const Type* t1 = phase->type(in(1));
113 const Type* t2 = phase->type(in(2));
114 return sub(t1,t2); // Local flavor of type subtraction
115
116 }
117
make(Node * in1,Node * in2,BasicType bt)118 SubNode* SubNode::make(Node* in1, Node* in2, BasicType bt) {
119 switch (bt) {
120 case T_INT:
121 return new SubINode(in1, in2);
122 case T_LONG:
123 return new SubLNode(in1, in2);
124 default:
125 fatal("Not implemented for %s", type2name(bt));
126 }
127 return NULL;
128 }
129
130 //=============================================================================
131 //------------------------------Helper function--------------------------------
132
is_cloop_increment(Node * inc)133 static bool is_cloop_increment(Node* inc) {
134 precond(inc->Opcode() == Op_AddI || inc->Opcode() == Op_AddL);
135
136 if (!inc->in(1)->is_Phi()) {
137 return false;
138 }
139 const PhiNode* phi = inc->in(1)->as_Phi();
140
141 if (!phi->region()->is_CountedLoop()) {
142 return false;
143 }
144
145 return inc == phi->region()->as_CountedLoop()->incr();
146 }
147
148 // Given the expression '(x + C) - v', or
149 // 'v - (x + C)', we examine nodes '+' and 'v':
150 //
151 // 1. Do not convert if '+' is a counted-loop increment, because the '-' is
152 // loop invariant and converting extends the live-range of 'x' to overlap
153 // with the '+', forcing another register to be used in the loop.
154 //
155 // 2. Do not convert if 'v' is a counted-loop induction variable, because
156 // 'x' might be invariant.
157 //
ok_to_convert(Node * inc,Node * var)158 static bool ok_to_convert(Node* inc, Node* var) {
159 return !(is_cloop_increment(inc) || var->is_cloop_ind_var());
160 }
161
162 //------------------------------Ideal------------------------------------------
Ideal(PhaseGVN * phase,bool can_reshape)163 Node *SubINode::Ideal(PhaseGVN *phase, bool can_reshape){
164 Node *in1 = in(1);
165 Node *in2 = in(2);
166 uint op1 = in1->Opcode();
167 uint op2 = in2->Opcode();
168
169 #ifdef ASSERT
170 // Check for dead loop
171 if ((in1 == this) || (in2 == this) ||
172 ((op1 == Op_AddI || op1 == Op_SubI) &&
173 ((in1->in(1) == this) || (in1->in(2) == this) ||
174 (in1->in(1) == in1) || (in1->in(2) == in1)))) {
175 assert(false, "dead loop in SubINode::Ideal");
176 }
177 #endif
178
179 const Type *t2 = phase->type( in2 );
180 if( t2 == Type::TOP ) return NULL;
181 // Convert "x-c0" into "x+ -c0".
182 if( t2->base() == Type::Int ){ // Might be bottom or top...
183 const TypeInt *i = t2->is_int();
184 if( i->is_con() )
185 return new AddINode(in1, phase->intcon(-i->get_con()));
186 }
187
188 // Convert "(x+c0) - y" into (x-y) + c0"
189 // Do not collapse (x+c0)-y if "+" is a loop increment or
190 // if "y" is a loop induction variable.
191 if( op1 == Op_AddI && ok_to_convert(in1, in2) ) {
192 const Type *tadd = phase->type( in1->in(2) );
193 if( tadd->singleton() && tadd != Type::TOP ) {
194 Node *sub2 = phase->transform( new SubINode( in1->in(1), in2 ));
195 return new AddINode( sub2, in1->in(2) );
196 }
197 }
198
199
200 // Convert "x - (y+c0)" into "(x-y) - c0"
201 // Need the same check as in above optimization but reversed.
202 if (op2 == Op_AddI && ok_to_convert(in2, in1)) {
203 Node* in21 = in2->in(1);
204 Node* in22 = in2->in(2);
205 const TypeInt* tcon = phase->type(in22)->isa_int();
206 if (tcon != NULL && tcon->is_con()) {
207 Node* sub2 = phase->transform( new SubINode(in1, in21) );
208 Node* neg_c0 = phase->intcon(- tcon->get_con());
209 return new AddINode(sub2, neg_c0);
210 }
211 }
212
213 const Type *t1 = phase->type( in1 );
214 if( t1 == Type::TOP ) return NULL;
215
216 #ifdef ASSERT
217 // Check for dead loop
218 if ((op2 == Op_AddI || op2 == Op_SubI) &&
219 ((in2->in(1) == this) || (in2->in(2) == this) ||
220 (in2->in(1) == in2) || (in2->in(2) == in2))) {
221 assert(false, "dead loop in SubINode::Ideal");
222 }
223 #endif
224
225 // Convert "x - (x+y)" into "-y"
226 if (op2 == Op_AddI && in1 == in2->in(1)) {
227 return new SubINode(phase->intcon(0), in2->in(2));
228 }
229 // Convert "(x-y) - x" into "-y"
230 if (op1 == Op_SubI && in1->in(1) == in2) {
231 return new SubINode(phase->intcon(0), in1->in(2));
232 }
233 // Convert "x - (y+x)" into "-y"
234 if (op2 == Op_AddI && in1 == in2->in(2)) {
235 return new SubINode(phase->intcon(0), in2->in(1));
236 }
237
238 // Convert "0 - (x-y)" into "y-x"
239 if( t1 == TypeInt::ZERO && op2 == Op_SubI )
240 return new SubINode( in2->in(2), in2->in(1) );
241
242 // Convert "0 - (x+con)" into "-con-x"
243 jint con;
244 if( t1 == TypeInt::ZERO && op2 == Op_AddI &&
245 (con = in2->in(2)->find_int_con(0)) != 0 )
246 return new SubINode( phase->intcon(-con), in2->in(1) );
247
248 // Convert "(X+A) - (X+B)" into "A - B"
249 if( op1 == Op_AddI && op2 == Op_AddI && in1->in(1) == in2->in(1) )
250 return new SubINode( in1->in(2), in2->in(2) );
251
252 // Convert "(A+X) - (B+X)" into "A - B"
253 if( op1 == Op_AddI && op2 == Op_AddI && in1->in(2) == in2->in(2) )
254 return new SubINode( in1->in(1), in2->in(1) );
255
256 // Convert "(A+X) - (X+B)" into "A - B"
257 if( op1 == Op_AddI && op2 == Op_AddI && in1->in(2) == in2->in(1) )
258 return new SubINode( in1->in(1), in2->in(2) );
259
260 // Convert "(X+A) - (B+X)" into "A - B"
261 if( op1 == Op_AddI && op2 == Op_AddI && in1->in(1) == in2->in(2) )
262 return new SubINode( in1->in(2), in2->in(1) );
263
264 // Convert "A-(B-C)" into (A+C)-B", since add is commutative and generally
265 // nicer to optimize than subtract.
266 if( op2 == Op_SubI && in2->outcnt() == 1) {
267 Node *add1 = phase->transform( new AddINode( in1, in2->in(2) ) );
268 return new SubINode( add1, in2->in(1) );
269 }
270
271 // Convert "0-(A>>31)" into "(A>>>31)"
272 if ( op2 == Op_RShiftI ) {
273 Node *in21 = in2->in(1);
274 Node *in22 = in2->in(2);
275 const TypeInt *zero = phase->type(in1)->isa_int();
276 const TypeInt *t21 = phase->type(in21)->isa_int();
277 const TypeInt *t22 = phase->type(in22)->isa_int();
278 if ( t21 && t22 && zero == TypeInt::ZERO && t22->is_con(31) ) {
279 return new URShiftINode(in21, in22);
280 }
281 }
282
283 return NULL;
284 }
285
286 //------------------------------sub--------------------------------------------
287 // A subtract node differences it's two inputs.
sub(const Type * t1,const Type * t2) const288 const Type *SubINode::sub( const Type *t1, const Type *t2 ) const {
289 const TypeInt *r0 = t1->is_int(); // Handy access
290 const TypeInt *r1 = t2->is_int();
291 int32_t lo = java_subtract(r0->_lo, r1->_hi);
292 int32_t hi = java_subtract(r0->_hi, r1->_lo);
293
294 // We next check for 32-bit overflow.
295 // If that happens, we just assume all integers are possible.
296 if( (((r0->_lo ^ r1->_hi) >= 0) || // lo ends have same signs OR
297 ((r0->_lo ^ lo) >= 0)) && // lo results have same signs AND
298 (((r0->_hi ^ r1->_lo) >= 0) || // hi ends have same signs OR
299 ((r0->_hi ^ hi) >= 0)) ) // hi results have same signs
300 return TypeInt::make(lo,hi,MAX2(r0->_widen,r1->_widen));
301 else // Overflow; assume all integers
302 return TypeInt::INT;
303 }
304
305 //=============================================================================
306 //------------------------------Ideal------------------------------------------
Ideal(PhaseGVN * phase,bool can_reshape)307 Node *SubLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
308 Node *in1 = in(1);
309 Node *in2 = in(2);
310 uint op1 = in1->Opcode();
311 uint op2 = in2->Opcode();
312
313 #ifdef ASSERT
314 // Check for dead loop
315 if ((in1 == this) || (in2 == this) ||
316 ((op1 == Op_AddL || op1 == Op_SubL) &&
317 ((in1->in(1) == this) || (in1->in(2) == this) ||
318 (in1->in(1) == in1) || (in1->in(2) == in1)))) {
319 assert(false, "dead loop in SubLNode::Ideal");
320 }
321 #endif
322
323 if( phase->type( in2 ) == Type::TOP ) return NULL;
324 const TypeLong *i = phase->type( in2 )->isa_long();
325 // Convert "x-c0" into "x+ -c0".
326 if( i && // Might be bottom or top...
327 i->is_con() )
328 return new AddLNode(in1, phase->longcon(-i->get_con()));
329
330 // Convert "(x+c0) - y" into (x-y) + c0"
331 // Do not collapse (x+c0)-y if "+" is a loop increment or
332 // if "y" is a loop induction variable.
333 if( op1 == Op_AddL && ok_to_convert(in1, in2) ) {
334 Node *in11 = in1->in(1);
335 const Type *tadd = phase->type( in1->in(2) );
336 if( tadd->singleton() && tadd != Type::TOP ) {
337 Node *sub2 = phase->transform( new SubLNode( in11, in2 ));
338 return new AddLNode( sub2, in1->in(2) );
339 }
340 }
341
342 // Convert "x - (y+c0)" into "(x-y) - c0"
343 // Need the same check as in above optimization but reversed.
344 if (op2 == Op_AddL && ok_to_convert(in2, in1)) {
345 Node* in21 = in2->in(1);
346 Node* in22 = in2->in(2);
347 const TypeLong* tcon = phase->type(in22)->isa_long();
348 if (tcon != NULL && tcon->is_con()) {
349 Node* sub2 = phase->transform( new SubLNode(in1, in21) );
350 Node* neg_c0 = phase->longcon(- tcon->get_con());
351 return new AddLNode(sub2, neg_c0);
352 }
353 }
354
355 const Type *t1 = phase->type( in1 );
356 if( t1 == Type::TOP ) return NULL;
357
358 #ifdef ASSERT
359 // Check for dead loop
360 if ((op2 == Op_AddL || op2 == Op_SubL) &&
361 ((in2->in(1) == this) || (in2->in(2) == this) ||
362 (in2->in(1) == in2) || (in2->in(2) == in2))) {
363 assert(false, "dead loop in SubLNode::Ideal");
364 }
365 #endif
366
367 // Convert "x - (x+y)" into "-y"
368 if (op2 == Op_AddL && in1 == in2->in(1)) {
369 return new SubLNode(phase->makecon(TypeLong::ZERO), in2->in(2));
370 }
371 // Convert "x - (y+x)" into "-y"
372 if (op2 == Op_AddL && in1 == in2->in(2)) {
373 return new SubLNode(phase->makecon(TypeLong::ZERO), in2->in(1));
374 }
375
376 // Convert "0 - (x-y)" into "y-x"
377 if( phase->type( in1 ) == TypeLong::ZERO && op2 == Op_SubL )
378 return new SubLNode( in2->in(2), in2->in(1) );
379
380 // Convert "(X+A) - (X+B)" into "A - B"
381 if( op1 == Op_AddL && op2 == Op_AddL && in1->in(1) == in2->in(1) )
382 return new SubLNode( in1->in(2), in2->in(2) );
383
384 // Convert "(A+X) - (B+X)" into "A - B"
385 if( op1 == Op_AddL && op2 == Op_AddL && in1->in(2) == in2->in(2) )
386 return new SubLNode( in1->in(1), in2->in(1) );
387
388 // Convert "A-(B-C)" into (A+C)-B"
389 if( op2 == Op_SubL && in2->outcnt() == 1) {
390 Node *add1 = phase->transform( new AddLNode( in1, in2->in(2) ) );
391 return new SubLNode( add1, in2->in(1) );
392 }
393
394 // Convert "0L-(A>>63)" into "(A>>>63)"
395 if ( op2 == Op_RShiftL ) {
396 Node *in21 = in2->in(1);
397 Node *in22 = in2->in(2);
398 const TypeLong *zero = phase->type(in1)->isa_long();
399 const TypeLong *t21 = phase->type(in21)->isa_long();
400 const TypeInt *t22 = phase->type(in22)->isa_int();
401 if ( t21 && t22 && zero == TypeLong::ZERO && t22->is_con(63) ) {
402 return new URShiftLNode(in21, in22);
403 }
404 }
405
406 return NULL;
407 }
408
409 //------------------------------sub--------------------------------------------
410 // A subtract node differences it's two inputs.
sub(const Type * t1,const Type * t2) const411 const Type *SubLNode::sub( const Type *t1, const Type *t2 ) const {
412 const TypeLong *r0 = t1->is_long(); // Handy access
413 const TypeLong *r1 = t2->is_long();
414 jlong lo = java_subtract(r0->_lo, r1->_hi);
415 jlong hi = java_subtract(r0->_hi, r1->_lo);
416
417 // We next check for 32-bit overflow.
418 // If that happens, we just assume all integers are possible.
419 if( (((r0->_lo ^ r1->_hi) >= 0) || // lo ends have same signs OR
420 ((r0->_lo ^ lo) >= 0)) && // lo results have same signs AND
421 (((r0->_hi ^ r1->_lo) >= 0) || // hi ends have same signs OR
422 ((r0->_hi ^ hi) >= 0)) ) // hi results have same signs
423 return TypeLong::make(lo,hi,MAX2(r0->_widen,r1->_widen));
424 else // Overflow; assume all integers
425 return TypeLong::LONG;
426 }
427
428 //=============================================================================
429 //------------------------------Value------------------------------------------
430 // A subtract node differences its two inputs.
Value(PhaseGVN * phase) const431 const Type* SubFPNode::Value(PhaseGVN* phase) const {
432 const Node* in1 = in(1);
433 const Node* in2 = in(2);
434 // Either input is TOP ==> the result is TOP
435 const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
436 if( t1 == Type::TOP ) return Type::TOP;
437 const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
438 if( t2 == Type::TOP ) return Type::TOP;
439
440 // if both operands are infinity of same sign, the result is NaN; do
441 // not replace with zero
442 if (t1->is_finite() && t2->is_finite() && in1 == in2) {
443 return add_id();
444 }
445
446 // Either input is BOTTOM ==> the result is the local BOTTOM
447 const Type *bot = bottom_type();
448 if( (t1 == bot) || (t2 == bot) ||
449 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
450 return bot;
451
452 return sub(t1,t2); // Local flavor of type subtraction
453 }
454
455
456 //=============================================================================
457 //------------------------------Ideal------------------------------------------
Ideal(PhaseGVN * phase,bool can_reshape)458 Node *SubFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
459 const Type *t2 = phase->type( in(2) );
460 // Convert "x-c0" into "x+ -c0".
461 if( t2->base() == Type::FloatCon ) { // Might be bottom or top...
462 // return new (phase->C, 3) AddFNode(in(1), phase->makecon( TypeF::make(-t2->getf()) ) );
463 }
464
465 // Not associative because of boundary conditions (infinity)
466 if (IdealizedNumerics && !phase->C->method()->is_strict() &&
467 in(2)->is_Add() && in(1) == in(2)->in(1)) {
468 // Convert "x - (x+y)" into "-y"
469 return new SubFNode(phase->makecon(TypeF::ZERO), in(2)->in(2));
470 }
471
472 // Cannot replace 0.0-X with -X because a 'fsub' bytecode computes
473 // 0.0-0.0 as +0.0, while a 'fneg' bytecode computes -0.0.
474 //if( phase->type(in(1)) == TypeF::ZERO )
475 //return new (phase->C, 2) NegFNode(in(2));
476
477 return NULL;
478 }
479
480 //------------------------------sub--------------------------------------------
481 // A subtract node differences its two inputs.
sub(const Type * t1,const Type * t2) const482 const Type *SubFNode::sub( const Type *t1, const Type *t2 ) const {
483 // no folding if one of operands is infinity or NaN, do not do constant folding
484 if( g_isfinite(t1->getf()) && g_isfinite(t2->getf()) ) {
485 return TypeF::make( t1->getf() - t2->getf() );
486 }
487 else if( g_isnan(t1->getf()) ) {
488 return t1;
489 }
490 else if( g_isnan(t2->getf()) ) {
491 return t2;
492 }
493 else {
494 return Type::FLOAT;
495 }
496 }
497
498 //=============================================================================
499 //------------------------------Ideal------------------------------------------
Ideal(PhaseGVN * phase,bool can_reshape)500 Node *SubDNode::Ideal(PhaseGVN *phase, bool can_reshape){
501 const Type *t2 = phase->type( in(2) );
502 // Convert "x-c0" into "x+ -c0".
503 if( t2->base() == Type::DoubleCon ) { // Might be bottom or top...
504 // return new (phase->C, 3) AddDNode(in(1), phase->makecon( TypeD::make(-t2->getd()) ) );
505 }
506
507 // Not associative because of boundary conditions (infinity)
508 if (IdealizedNumerics && !phase->C->method()->is_strict() &&
509 in(2)->is_Add() && in(1) == in(2)->in(1)) {
510 // Convert "x - (x+y)" into "-y"
511 return new SubDNode(phase->makecon(TypeD::ZERO), in(2)->in(2));
512 }
513
514 // Cannot replace 0.0-X with -X because a 'dsub' bytecode computes
515 // 0.0-0.0 as +0.0, while a 'dneg' bytecode computes -0.0.
516 //if( phase->type(in(1)) == TypeD::ZERO )
517 //return new (phase->C, 2) NegDNode(in(2));
518
519 return NULL;
520 }
521
522 //------------------------------sub--------------------------------------------
523 // A subtract node differences its two inputs.
sub(const Type * t1,const Type * t2) const524 const Type *SubDNode::sub( const Type *t1, const Type *t2 ) const {
525 // no folding if one of operands is infinity or NaN, do not do constant folding
526 if( g_isfinite(t1->getd()) && g_isfinite(t2->getd()) ) {
527 return TypeD::make( t1->getd() - t2->getd() );
528 }
529 else if( g_isnan(t1->getd()) ) {
530 return t1;
531 }
532 else if( g_isnan(t2->getd()) ) {
533 return t2;
534 }
535 else {
536 return Type::DOUBLE;
537 }
538 }
539
540 //=============================================================================
541 //------------------------------Idealize---------------------------------------
542 // Unlike SubNodes, compare must still flatten return value to the
543 // range -1, 0, 1.
544 // And optimizations like those for (X + Y) - X fail if overflow happens.
Identity(PhaseGVN * phase)545 Node* CmpNode::Identity(PhaseGVN* phase) {
546 return this;
547 }
548
549 #ifndef PRODUCT
550 //----------------------------related------------------------------------------
551 // Related nodes of comparison nodes include all data inputs (until hitting a
552 // control boundary) as well as all outputs until and including control nodes
553 // as well as their projections. In compact mode, data inputs till depth 1 and
554 // all outputs till depth 1 are considered.
related(GrowableArray<Node * > * in_rel,GrowableArray<Node * > * out_rel,bool compact) const555 void CmpNode::related(GrowableArray<Node*> *in_rel, GrowableArray<Node*> *out_rel, bool compact) const {
556 if (compact) {
557 this->collect_nodes(in_rel, 1, false, true);
558 this->collect_nodes(out_rel, -1, false, false);
559 } else {
560 this->collect_nodes_in_all_data(in_rel, false);
561 this->collect_nodes_out_all_ctrl_boundary(out_rel);
562 // Now, find all control nodes in out_rel, and include their projections
563 // and projection targets (if any) in the result.
564 GrowableArray<Node*> proj(Compile::current()->unique());
565 for (GrowableArrayIterator<Node*> it = out_rel->begin(); it != out_rel->end(); ++it) {
566 Node* n = *it;
567 if (n->is_CFG() && !n->is_Proj()) {
568 // Assume projections and projection targets are found at levels 1 and 2.
569 n->collect_nodes(&proj, -2, false, false);
570 for (GrowableArrayIterator<Node*> p = proj.begin(); p != proj.end(); ++p) {
571 out_rel->append_if_missing(*p);
572 }
573 proj.clear();
574 }
575 }
576 }
577 }
578
579 #endif
580
make(Node * in1,Node * in2,BasicType bt,bool unsigned_comp)581 CmpNode *CmpNode::make(Node *in1, Node *in2, BasicType bt, bool unsigned_comp) {
582 switch (bt) {
583 case T_INT:
584 if (unsigned_comp) {
585 return new CmpUNode(in1, in2);
586 }
587 return new CmpINode(in1, in2);
588 case T_LONG:
589 if (unsigned_comp) {
590 return new CmpULNode(in1, in2);
591 }
592 return new CmpLNode(in1, in2);
593 default:
594 fatal("Not implemented for %s", type2name(bt));
595 }
596 return NULL;
597 }
598
599 //=============================================================================
600 //------------------------------cmp--------------------------------------------
601 // Simplify a CmpI (compare 2 integers) node, based on local information.
602 // If both inputs are constants, compare them.
sub(const Type * t1,const Type * t2) const603 const Type *CmpINode::sub( const Type *t1, const Type *t2 ) const {
604 const TypeInt *r0 = t1->is_int(); // Handy access
605 const TypeInt *r1 = t2->is_int();
606
607 if( r0->_hi < r1->_lo ) // Range is always low?
608 return TypeInt::CC_LT;
609 else if( r0->_lo > r1->_hi ) // Range is always high?
610 return TypeInt::CC_GT;
611
612 else if( r0->is_con() && r1->is_con() ) { // comparing constants?
613 assert(r0->get_con() == r1->get_con(), "must be equal");
614 return TypeInt::CC_EQ; // Equal results.
615 } else if( r0->_hi == r1->_lo ) // Range is never high?
616 return TypeInt::CC_LE;
617 else if( r0->_lo == r1->_hi ) // Range is never low?
618 return TypeInt::CC_GE;
619 return TypeInt::CC; // else use worst case results
620 }
621
622 // Simplify a CmpU (compare 2 integers) node, based on local information.
623 // If both inputs are constants, compare them.
sub(const Type * t1,const Type * t2) const624 const Type *CmpUNode::sub( const Type *t1, const Type *t2 ) const {
625 assert(!t1->isa_ptr(), "obsolete usage of CmpU");
626
627 // comparing two unsigned ints
628 const TypeInt *r0 = t1->is_int(); // Handy access
629 const TypeInt *r1 = t2->is_int();
630
631 // Current installed version
632 // Compare ranges for non-overlap
633 juint lo0 = r0->_lo;
634 juint hi0 = r0->_hi;
635 juint lo1 = r1->_lo;
636 juint hi1 = r1->_hi;
637
638 // If either one has both negative and positive values,
639 // it therefore contains both 0 and -1, and since [0..-1] is the
640 // full unsigned range, the type must act as an unsigned bottom.
641 bool bot0 = ((jint)(lo0 ^ hi0) < 0);
642 bool bot1 = ((jint)(lo1 ^ hi1) < 0);
643
644 if (bot0 || bot1) {
645 // All unsigned values are LE -1 and GE 0.
646 if (lo0 == 0 && hi0 == 0) {
647 return TypeInt::CC_LE; // 0 <= bot
648 } else if ((jint)lo0 == -1 && (jint)hi0 == -1) {
649 return TypeInt::CC_GE; // -1 >= bot
650 } else if (lo1 == 0 && hi1 == 0) {
651 return TypeInt::CC_GE; // bot >= 0
652 } else if ((jint)lo1 == -1 && (jint)hi1 == -1) {
653 return TypeInt::CC_LE; // bot <= -1
654 }
655 } else {
656 // We can use ranges of the form [lo..hi] if signs are the same.
657 assert(lo0 <= hi0 && lo1 <= hi1, "unsigned ranges are valid");
658 // results are reversed, '-' > '+' for unsigned compare
659 if (hi0 < lo1) {
660 return TypeInt::CC_LT; // smaller
661 } else if (lo0 > hi1) {
662 return TypeInt::CC_GT; // greater
663 } else if (hi0 == lo1 && lo0 == hi1) {
664 return TypeInt::CC_EQ; // Equal results
665 } else if (lo0 >= hi1) {
666 return TypeInt::CC_GE;
667 } else if (hi0 <= lo1) {
668 // Check for special case in Hashtable::get. (See below.)
669 if ((jint)lo0 >= 0 && (jint)lo1 >= 0 && is_index_range_check())
670 return TypeInt::CC_LT;
671 return TypeInt::CC_LE;
672 }
673 }
674 // Check for special case in Hashtable::get - the hash index is
675 // mod'ed to the table size so the following range check is useless.
676 // Check for: (X Mod Y) CmpU Y, where the mod result and Y both have
677 // to be positive.
678 // (This is a gross hack, since the sub method never
679 // looks at the structure of the node in any other case.)
680 if ((jint)lo0 >= 0 && (jint)lo1 >= 0 && is_index_range_check())
681 return TypeInt::CC_LT;
682 return TypeInt::CC; // else use worst case results
683 }
684
Value(PhaseGVN * phase) const685 const Type* CmpUNode::Value(PhaseGVN* phase) const {
686 const Type* t = SubNode::Value_common(phase);
687 if (t != NULL) {
688 return t;
689 }
690 const Node* in1 = in(1);
691 const Node* in2 = in(2);
692 const Type* t1 = phase->type(in1);
693 const Type* t2 = phase->type(in2);
694 assert(t1->isa_int(), "CmpU has only Int type inputs");
695 if (t2 == TypeInt::INT) { // Compare to bottom?
696 return bottom_type();
697 }
698 uint in1_op = in1->Opcode();
699 if (in1_op == Op_AddI || in1_op == Op_SubI) {
700 // The problem rise when result of AddI(SubI) may overflow
701 // signed integer value. Let say the input type is
702 // [256, maxint] then +128 will create 2 ranges due to
703 // overflow: [minint, minint+127] and [384, maxint].
704 // But C2 type system keep only 1 type range and as result
705 // it use general [minint, maxint] for this case which we
706 // can't optimize.
707 //
708 // Make 2 separate type ranges based on types of AddI(SubI) inputs
709 // and compare results of their compare. If results are the same
710 // CmpU node can be optimized.
711 const Node* in11 = in1->in(1);
712 const Node* in12 = in1->in(2);
713 const Type* t11 = (in11 == in1) ? Type::TOP : phase->type(in11);
714 const Type* t12 = (in12 == in1) ? Type::TOP : phase->type(in12);
715 // Skip cases when input types are top or bottom.
716 if ((t11 != Type::TOP) && (t11 != TypeInt::INT) &&
717 (t12 != Type::TOP) && (t12 != TypeInt::INT)) {
718 const TypeInt *r0 = t11->is_int();
719 const TypeInt *r1 = t12->is_int();
720 jlong lo_r0 = r0->_lo;
721 jlong hi_r0 = r0->_hi;
722 jlong lo_r1 = r1->_lo;
723 jlong hi_r1 = r1->_hi;
724 if (in1_op == Op_SubI) {
725 jlong tmp = hi_r1;
726 hi_r1 = -lo_r1;
727 lo_r1 = -tmp;
728 // Note, for substructing [minint,x] type range
729 // long arithmetic provides correct overflow answer.
730 // The confusion come from the fact that in 32-bit
731 // -minint == minint but in 64-bit -minint == maxint+1.
732 }
733 jlong lo_long = lo_r0 + lo_r1;
734 jlong hi_long = hi_r0 + hi_r1;
735 int lo_tr1 = min_jint;
736 int hi_tr1 = (int)hi_long;
737 int lo_tr2 = (int)lo_long;
738 int hi_tr2 = max_jint;
739 bool underflow = lo_long != (jlong)lo_tr2;
740 bool overflow = hi_long != (jlong)hi_tr1;
741 // Use sub(t1, t2) when there is no overflow (one type range)
742 // or when both overflow and underflow (too complex).
743 if ((underflow != overflow) && (hi_tr1 < lo_tr2)) {
744 // Overflow only on one boundary, compare 2 separate type ranges.
745 int w = MAX2(r0->_widen, r1->_widen); // _widen does not matter here
746 const TypeInt* tr1 = TypeInt::make(lo_tr1, hi_tr1, w);
747 const TypeInt* tr2 = TypeInt::make(lo_tr2, hi_tr2, w);
748 const Type* cmp1 = sub(tr1, t2);
749 const Type* cmp2 = sub(tr2, t2);
750 if (cmp1 == cmp2) {
751 return cmp1; // Hit!
752 }
753 }
754 }
755 }
756
757 return sub(t1, t2); // Local flavor of type subtraction
758 }
759
is_index_range_check() const760 bool CmpUNode::is_index_range_check() const {
761 // Check for the "(X ModI Y) CmpU Y" shape
762 return (in(1)->Opcode() == Op_ModI &&
763 in(1)->in(2)->eqv_uncast(in(2)));
764 }
765
766 //------------------------------Idealize---------------------------------------
Ideal(PhaseGVN * phase,bool can_reshape)767 Node *CmpINode::Ideal( PhaseGVN *phase, bool can_reshape ) {
768 if (phase->type(in(2))->higher_equal(TypeInt::ZERO)) {
769 switch (in(1)->Opcode()) {
770 case Op_CmpL3: // Collapse a CmpL3/CmpI into a CmpL
771 return new CmpLNode(in(1)->in(1),in(1)->in(2));
772 case Op_CmpF3: // Collapse a CmpF3/CmpI into a CmpF
773 return new CmpFNode(in(1)->in(1),in(1)->in(2));
774 case Op_CmpD3: // Collapse a CmpD3/CmpI into a CmpD
775 return new CmpDNode(in(1)->in(1),in(1)->in(2));
776 //case Op_SubI:
777 // If (x - y) cannot overflow, then ((x - y) <?> 0)
778 // can be turned into (x <?> y).
779 // This is handled (with more general cases) by Ideal_sub_algebra.
780 }
781 }
782 return NULL; // No change
783 }
784
Ideal(PhaseGVN * phase,bool can_reshape)785 Node *CmpLNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
786 const TypeLong *t2 = phase->type(in(2))->isa_long();
787 if (Opcode() == Op_CmpL && in(1)->Opcode() == Op_ConvI2L && t2 && t2->is_con()) {
788 const jlong con = t2->get_con();
789 if (con >= min_jint && con <= max_jint) {
790 return new CmpINode(in(1)->in(1), phase->intcon((jint)con));
791 }
792 }
793 return NULL;
794 }
795
796 //=============================================================================
797 // Simplify a CmpL (compare 2 longs ) node, based on local information.
798 // If both inputs are constants, compare them.
sub(const Type * t1,const Type * t2) const799 const Type *CmpLNode::sub( const Type *t1, const Type *t2 ) const {
800 const TypeLong *r0 = t1->is_long(); // Handy access
801 const TypeLong *r1 = t2->is_long();
802
803 if( r0->_hi < r1->_lo ) // Range is always low?
804 return TypeInt::CC_LT;
805 else if( r0->_lo > r1->_hi ) // Range is always high?
806 return TypeInt::CC_GT;
807
808 else if( r0->is_con() && r1->is_con() ) { // comparing constants?
809 assert(r0->get_con() == r1->get_con(), "must be equal");
810 return TypeInt::CC_EQ; // Equal results.
811 } else if( r0->_hi == r1->_lo ) // Range is never high?
812 return TypeInt::CC_LE;
813 else if( r0->_lo == r1->_hi ) // Range is never low?
814 return TypeInt::CC_GE;
815 return TypeInt::CC; // else use worst case results
816 }
817
818
819 // Simplify a CmpUL (compare 2 unsigned longs) node, based on local information.
820 // If both inputs are constants, compare them.
sub(const Type * t1,const Type * t2) const821 const Type* CmpULNode::sub(const Type* t1, const Type* t2) const {
822 assert(!t1->isa_ptr(), "obsolete usage of CmpUL");
823
824 // comparing two unsigned longs
825 const TypeLong* r0 = t1->is_long(); // Handy access
826 const TypeLong* r1 = t2->is_long();
827
828 // Current installed version
829 // Compare ranges for non-overlap
830 julong lo0 = r0->_lo;
831 julong hi0 = r0->_hi;
832 julong lo1 = r1->_lo;
833 julong hi1 = r1->_hi;
834
835 // If either one has both negative and positive values,
836 // it therefore contains both 0 and -1, and since [0..-1] is the
837 // full unsigned range, the type must act as an unsigned bottom.
838 bool bot0 = ((jlong)(lo0 ^ hi0) < 0);
839 bool bot1 = ((jlong)(lo1 ^ hi1) < 0);
840
841 if (bot0 || bot1) {
842 // All unsigned values are LE -1 and GE 0.
843 if (lo0 == 0 && hi0 == 0) {
844 return TypeInt::CC_LE; // 0 <= bot
845 } else if ((jlong)lo0 == -1 && (jlong)hi0 == -1) {
846 return TypeInt::CC_GE; // -1 >= bot
847 } else if (lo1 == 0 && hi1 == 0) {
848 return TypeInt::CC_GE; // bot >= 0
849 } else if ((jlong)lo1 == -1 && (jlong)hi1 == -1) {
850 return TypeInt::CC_LE; // bot <= -1
851 }
852 } else {
853 // We can use ranges of the form [lo..hi] if signs are the same.
854 assert(lo0 <= hi0 && lo1 <= hi1, "unsigned ranges are valid");
855 // results are reversed, '-' > '+' for unsigned compare
856 if (hi0 < lo1) {
857 return TypeInt::CC_LT; // smaller
858 } else if (lo0 > hi1) {
859 return TypeInt::CC_GT; // greater
860 } else if (hi0 == lo1 && lo0 == hi1) {
861 return TypeInt::CC_EQ; // Equal results
862 } else if (lo0 >= hi1) {
863 return TypeInt::CC_GE;
864 } else if (hi0 <= lo1) {
865 return TypeInt::CC_LE;
866 }
867 }
868
869 return TypeInt::CC; // else use worst case results
870 }
871
872 //=============================================================================
873 //------------------------------sub--------------------------------------------
874 // Simplify an CmpP (compare 2 pointers) node, based on local information.
875 // If both inputs are constants, compare them.
sub(const Type * t1,const Type * t2) const876 const Type *CmpPNode::sub( const Type *t1, const Type *t2 ) const {
877 const TypePtr *r0 = t1->is_ptr(); // Handy access
878 const TypePtr *r1 = t2->is_ptr();
879
880 // Undefined inputs makes for an undefined result
881 if( TypePtr::above_centerline(r0->_ptr) ||
882 TypePtr::above_centerline(r1->_ptr) )
883 return Type::TOP;
884
885 if (r0 == r1 && r0->singleton()) {
886 // Equal pointer constants (klasses, nulls, etc.)
887 return TypeInt::CC_EQ;
888 }
889
890 // See if it is 2 unrelated classes.
891 const TypeOopPtr* oop_p0 = r0->isa_oopptr();
892 const TypeOopPtr* oop_p1 = r1->isa_oopptr();
893 bool both_oop_ptr = oop_p0 && oop_p1;
894
895 if (both_oop_ptr) {
896 Node* in1 = in(1)->uncast();
897 Node* in2 = in(2)->uncast();
898 AllocateNode* alloc1 = AllocateNode::Ideal_allocation(in1, NULL);
899 AllocateNode* alloc2 = AllocateNode::Ideal_allocation(in2, NULL);
900 if (MemNode::detect_ptr_independence(in1, alloc1, in2, alloc2, NULL)) {
901 return TypeInt::CC_GT; // different pointers
902 }
903 }
904
905 const TypeKlassPtr* klass_p0 = r0->isa_klassptr();
906 const TypeKlassPtr* klass_p1 = r1->isa_klassptr();
907
908 if (both_oop_ptr || (klass_p0 && klass_p1)) { // both or neither are klass pointers
909 ciKlass* klass0 = NULL;
910 bool xklass0 = false;
911 ciKlass* klass1 = NULL;
912 bool xklass1 = false;
913
914 if (oop_p0) {
915 klass0 = oop_p0->klass();
916 xklass0 = oop_p0->klass_is_exact();
917 } else {
918 assert(klass_p0, "must be non-null if oop_p0 is null");
919 klass0 = klass_p0->klass();
920 xklass0 = klass_p0->klass_is_exact();
921 }
922
923 if (oop_p1) {
924 klass1 = oop_p1->klass();
925 xklass1 = oop_p1->klass_is_exact();
926 } else {
927 assert(klass_p1, "must be non-null if oop_p1 is null");
928 klass1 = klass_p1->klass();
929 xklass1 = klass_p1->klass_is_exact();
930 }
931
932 if (klass0 && klass1 &&
933 klass0->is_loaded() && !klass0->is_interface() && // do not trust interfaces
934 klass1->is_loaded() && !klass1->is_interface() &&
935 (!klass0->is_obj_array_klass() ||
936 !klass0->as_obj_array_klass()->base_element_klass()->is_interface()) &&
937 (!klass1->is_obj_array_klass() ||
938 !klass1->as_obj_array_klass()->base_element_klass()->is_interface())) {
939 bool unrelated_classes = false;
940 // See if neither subclasses the other, or if the class on top
941 // is precise. In either of these cases, the compare is known
942 // to fail if at least one of the pointers is provably not null.
943 if (klass0->equals(klass1)) { // if types are unequal but klasses are equal
944 // Do nothing; we know nothing for imprecise types
945 } else if (klass0->is_subtype_of(klass1)) {
946 // If klass1's type is PRECISE, then classes are unrelated.
947 unrelated_classes = xklass1;
948 } else if (klass1->is_subtype_of(klass0)) {
949 // If klass0's type is PRECISE, then classes are unrelated.
950 unrelated_classes = xklass0;
951 } else { // Neither subtypes the other
952 unrelated_classes = true;
953 }
954 if (unrelated_classes) {
955 // The oops classes are known to be unrelated. If the joined PTRs of
956 // two oops is not Null and not Bottom, then we are sure that one
957 // of the two oops is non-null, and the comparison will always fail.
958 TypePtr::PTR jp = r0->join_ptr(r1->_ptr);
959 if (jp != TypePtr::Null && jp != TypePtr::BotPTR) {
960 return TypeInt::CC_GT;
961 }
962 }
963 }
964 }
965
966 // Known constants can be compared exactly
967 // Null can be distinguished from any NotNull pointers
968 // Unknown inputs makes an unknown result
969 if( r0->singleton() ) {
970 intptr_t bits0 = r0->get_con();
971 if( r1->singleton() )
972 return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT;
973 return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC;
974 } else if( r1->singleton() ) {
975 intptr_t bits1 = r1->get_con();
976 return ( r0->_ptr == TypePtr::NotNull && bits1==0 ) ? TypeInt::CC_GT : TypeInt::CC;
977 } else
978 return TypeInt::CC;
979 }
980
isa_java_mirror_load(PhaseGVN * phase,Node * n)981 static inline Node* isa_java_mirror_load(PhaseGVN* phase, Node* n) {
982 // Return the klass node for (indirect load from OopHandle)
983 // LoadBarrier?(LoadP(LoadP(AddP(foo:Klass, #java_mirror))))
984 // or NULL if not matching.
985 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
986 n = bs->step_over_gc_barrier(n);
987
988 if (n->Opcode() != Op_LoadP) return NULL;
989
990 const TypeInstPtr* tp = phase->type(n)->isa_instptr();
991 if (!tp || tp->klass() != phase->C->env()->Class_klass()) return NULL;
992
993 Node* adr = n->in(MemNode::Address);
994 // First load from OopHandle: ((OopHandle)mirror)->resolve(); may need barrier.
995 if (adr->Opcode() != Op_LoadP || !phase->type(adr)->isa_rawptr()) return NULL;
996 adr = adr->in(MemNode::Address);
997
998 intptr_t off = 0;
999 Node* k = AddPNode::Ideal_base_and_offset(adr, phase, off);
1000 if (k == NULL) return NULL;
1001 const TypeKlassPtr* tkp = phase->type(k)->isa_klassptr();
1002 if (!tkp || off != in_bytes(Klass::java_mirror_offset())) return NULL;
1003
1004 // We've found the klass node of a Java mirror load.
1005 return k;
1006 }
1007
isa_const_java_mirror(PhaseGVN * phase,Node * n)1008 static inline Node* isa_const_java_mirror(PhaseGVN* phase, Node* n) {
1009 // for ConP(Foo.class) return ConP(Foo.klass)
1010 // otherwise return NULL
1011 if (!n->is_Con()) return NULL;
1012
1013 const TypeInstPtr* tp = phase->type(n)->isa_instptr();
1014 if (!tp) return NULL;
1015
1016 ciType* mirror_type = tp->java_mirror_type();
1017 // TypeInstPtr::java_mirror_type() returns non-NULL for compile-
1018 // time Class constants only.
1019 if (!mirror_type) return NULL;
1020
1021 // x.getClass() == int.class can never be true (for all primitive types)
1022 // Return a ConP(NULL) node for this case.
1023 if (mirror_type->is_classless()) {
1024 return phase->makecon(TypePtr::NULL_PTR);
1025 }
1026
1027 // return the ConP(Foo.klass)
1028 assert(mirror_type->is_klass(), "mirror_type should represent a Klass*");
1029 return phase->makecon(TypeKlassPtr::make(mirror_type->as_klass()));
1030 }
1031
1032 //------------------------------Ideal------------------------------------------
1033 // Normalize comparisons between Java mirror loads to compare the klass instead.
1034 //
1035 // Also check for the case of comparing an unknown klass loaded from the primary
1036 // super-type array vs a known klass with no subtypes. This amounts to
1037 // checking to see an unknown klass subtypes a known klass with no subtypes;
1038 // this only happens on an exact match. We can shorten this test by 1 load.
Ideal(PhaseGVN * phase,bool can_reshape)1039 Node *CmpPNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
1040 // Normalize comparisons between Java mirrors into comparisons of the low-
1041 // level klass, where a dependent load could be shortened.
1042 //
1043 // The new pattern has a nice effect of matching the same pattern used in the
1044 // fast path of instanceof/checkcast/Class.isInstance(), which allows
1045 // redundant exact type check be optimized away by GVN.
1046 // For example, in
1047 // if (x.getClass() == Foo.class) {
1048 // Foo foo = (Foo) x;
1049 // // ... use a ...
1050 // }
1051 // a CmpPNode could be shared between if_acmpne and checkcast
1052 {
1053 Node* k1 = isa_java_mirror_load(phase, in(1));
1054 Node* k2 = isa_java_mirror_load(phase, in(2));
1055 Node* conk2 = isa_const_java_mirror(phase, in(2));
1056
1057 if (k1 && (k2 || conk2)) {
1058 Node* lhs = k1;
1059 Node* rhs = (k2 != NULL) ? k2 : conk2;
1060 PhaseIterGVN* igvn = phase->is_IterGVN();
1061 if (igvn != NULL) {
1062 set_req_X(1, lhs, igvn);
1063 set_req_X(2, rhs, igvn);
1064 } else {
1065 set_req(1, lhs);
1066 set_req(2, rhs);
1067 }
1068 return this;
1069 }
1070 }
1071
1072 // Constant pointer on right?
1073 const TypeKlassPtr* t2 = phase->type(in(2))->isa_klassptr();
1074 if (t2 == NULL || !t2->klass_is_exact())
1075 return NULL;
1076 // Get the constant klass we are comparing to.
1077 ciKlass* superklass = t2->klass();
1078
1079 // Now check for LoadKlass on left.
1080 Node* ldk1 = in(1);
1081 if (ldk1->is_DecodeNKlass()) {
1082 ldk1 = ldk1->in(1);
1083 if (ldk1->Opcode() != Op_LoadNKlass )
1084 return NULL;
1085 } else if (ldk1->Opcode() != Op_LoadKlass )
1086 return NULL;
1087 // Take apart the address of the LoadKlass:
1088 Node* adr1 = ldk1->in(MemNode::Address);
1089 intptr_t con2 = 0;
1090 Node* ldk2 = AddPNode::Ideal_base_and_offset(adr1, phase, con2);
1091 if (ldk2 == NULL)
1092 return NULL;
1093 if (con2 == oopDesc::klass_offset_in_bytes()) {
1094 // We are inspecting an object's concrete class.
1095 // Short-circuit the check if the query is abstract.
1096 if (superklass->is_interface() ||
1097 superklass->is_abstract()) {
1098 // Make it come out always false:
1099 this->set_req(2, phase->makecon(TypePtr::NULL_PTR));
1100 return this;
1101 }
1102 }
1103
1104 // Check for a LoadKlass from primary supertype array.
1105 // Any nested loadklass from loadklass+con must be from the p.s. array.
1106 if (ldk2->is_DecodeNKlass()) {
1107 // Keep ldk2 as DecodeN since it could be used in CmpP below.
1108 if (ldk2->in(1)->Opcode() != Op_LoadNKlass )
1109 return NULL;
1110 } else if (ldk2->Opcode() != Op_LoadKlass)
1111 return NULL;
1112
1113 // Verify that we understand the situation
1114 if (con2 != (intptr_t) superklass->super_check_offset())
1115 return NULL; // Might be element-klass loading from array klass
1116
1117 // If 'superklass' has no subklasses and is not an interface, then we are
1118 // assured that the only input which will pass the type check is
1119 // 'superklass' itself.
1120 //
1121 // We could be more liberal here, and allow the optimization on interfaces
1122 // which have a single implementor. This would require us to increase the
1123 // expressiveness of the add_dependency() mechanism.
1124 // %%% Do this after we fix TypeOopPtr: Deps are expressive enough now.
1125
1126 // Object arrays must have their base element have no subtypes
1127 while (superklass->is_obj_array_klass()) {
1128 ciType* elem = superklass->as_obj_array_klass()->element_type();
1129 superklass = elem->as_klass();
1130 }
1131 if (superklass->is_instance_klass()) {
1132 ciInstanceKlass* ik = superklass->as_instance_klass();
1133 if (ik->has_subklass() || ik->is_interface()) return NULL;
1134 // Add a dependency if there is a chance that a subclass will be added later.
1135 if (!ik->is_final()) {
1136 phase->C->dependencies()->assert_leaf_type(ik);
1137 }
1138 }
1139
1140 // Bypass the dependent load, and compare directly
1141 this->set_req(1,ldk2);
1142
1143 return this;
1144 }
1145
1146 //=============================================================================
1147 //------------------------------sub--------------------------------------------
1148 // Simplify an CmpN (compare 2 pointers) node, based on local information.
1149 // If both inputs are constants, compare them.
sub(const Type * t1,const Type * t2) const1150 const Type *CmpNNode::sub( const Type *t1, const Type *t2 ) const {
1151 ShouldNotReachHere();
1152 return bottom_type();
1153 }
1154
1155 //------------------------------Ideal------------------------------------------
Ideal(PhaseGVN * phase,bool can_reshape)1156 Node *CmpNNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
1157 return NULL;
1158 }
1159
1160 //=============================================================================
1161 //------------------------------Value------------------------------------------
1162 // Simplify an CmpF (compare 2 floats ) node, based on local information.
1163 // If both inputs are constants, compare them.
Value(PhaseGVN * phase) const1164 const Type* CmpFNode::Value(PhaseGVN* phase) const {
1165 const Node* in1 = in(1);
1166 const Node* in2 = in(2);
1167 // Either input is TOP ==> the result is TOP
1168 const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
1169 if( t1 == Type::TOP ) return Type::TOP;
1170 const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
1171 if( t2 == Type::TOP ) return Type::TOP;
1172
1173 // Not constants? Don't know squat - even if they are the same
1174 // value! If they are NaN's they compare to LT instead of EQ.
1175 const TypeF *tf1 = t1->isa_float_constant();
1176 const TypeF *tf2 = t2->isa_float_constant();
1177 if( !tf1 || !tf2 ) return TypeInt::CC;
1178
1179 // This implements the Java bytecode fcmpl, so unordered returns -1.
1180 if( tf1->is_nan() || tf2->is_nan() )
1181 return TypeInt::CC_LT;
1182
1183 if( tf1->_f < tf2->_f ) return TypeInt::CC_LT;
1184 if( tf1->_f > tf2->_f ) return TypeInt::CC_GT;
1185 assert( tf1->_f == tf2->_f, "do not understand FP behavior" );
1186 return TypeInt::CC_EQ;
1187 }
1188
1189
1190 //=============================================================================
1191 //------------------------------Value------------------------------------------
1192 // Simplify an CmpD (compare 2 doubles ) node, based on local information.
1193 // If both inputs are constants, compare them.
Value(PhaseGVN * phase) const1194 const Type* CmpDNode::Value(PhaseGVN* phase) const {
1195 const Node* in1 = in(1);
1196 const Node* in2 = in(2);
1197 // Either input is TOP ==> the result is TOP
1198 const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
1199 if( t1 == Type::TOP ) return Type::TOP;
1200 const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
1201 if( t2 == Type::TOP ) return Type::TOP;
1202
1203 // Not constants? Don't know squat - even if they are the same
1204 // value! If they are NaN's they compare to LT instead of EQ.
1205 const TypeD *td1 = t1->isa_double_constant();
1206 const TypeD *td2 = t2->isa_double_constant();
1207 if( !td1 || !td2 ) return TypeInt::CC;
1208
1209 // This implements the Java bytecode dcmpl, so unordered returns -1.
1210 if( td1->is_nan() || td2->is_nan() )
1211 return TypeInt::CC_LT;
1212
1213 if( td1->_d < td2->_d ) return TypeInt::CC_LT;
1214 if( td1->_d > td2->_d ) return TypeInt::CC_GT;
1215 assert( td1->_d == td2->_d, "do not understand FP behavior" );
1216 return TypeInt::CC_EQ;
1217 }
1218
1219 //------------------------------Ideal------------------------------------------
Ideal(PhaseGVN * phase,bool can_reshape)1220 Node *CmpDNode::Ideal(PhaseGVN *phase, bool can_reshape){
1221 // Check if we can change this to a CmpF and remove a ConvD2F operation.
1222 // Change (CMPD (F2D (float)) (ConD value))
1223 // To (CMPF (float) (ConF value))
1224 // Valid when 'value' does not lose precision as a float.
1225 // Benefits: eliminates conversion, does not require 24-bit mode
1226
1227 // NaNs prevent commuting operands. This transform works regardless of the
1228 // order of ConD and ConvF2D inputs by preserving the original order.
1229 int idx_f2d = 1; // ConvF2D on left side?
1230 if( in(idx_f2d)->Opcode() != Op_ConvF2D )
1231 idx_f2d = 2; // No, swap to check for reversed args
1232 int idx_con = 3-idx_f2d; // Check for the constant on other input
1233
1234 if( ConvertCmpD2CmpF &&
1235 in(idx_f2d)->Opcode() == Op_ConvF2D &&
1236 in(idx_con)->Opcode() == Op_ConD ) {
1237 const TypeD *t2 = in(idx_con)->bottom_type()->is_double_constant();
1238 double t2_value_as_double = t2->_d;
1239 float t2_value_as_float = (float)t2_value_as_double;
1240 if( t2_value_as_double == (double)t2_value_as_float ) {
1241 // Test value can be represented as a float
1242 // Eliminate the conversion to double and create new comparison
1243 Node *new_in1 = in(idx_f2d)->in(1);
1244 Node *new_in2 = phase->makecon( TypeF::make(t2_value_as_float) );
1245 if( idx_f2d != 1 ) { // Must flip args to match original order
1246 Node *tmp = new_in1;
1247 new_in1 = new_in2;
1248 new_in2 = tmp;
1249 }
1250 CmpFNode *new_cmp = (Opcode() == Op_CmpD3)
1251 ? new CmpF3Node( new_in1, new_in2 )
1252 : new CmpFNode ( new_in1, new_in2 ) ;
1253 return new_cmp; // Changed to CmpFNode
1254 }
1255 // Testing value required the precision of a double
1256 }
1257 return NULL; // No change
1258 }
1259
1260
1261 //=============================================================================
1262 //------------------------------cc2logical-------------------------------------
1263 // Convert a condition code type to a logical type
cc2logical(const Type * CC) const1264 const Type *BoolTest::cc2logical( const Type *CC ) const {
1265 if( CC == Type::TOP ) return Type::TOP;
1266 if( CC->base() != Type::Int ) return TypeInt::BOOL; // Bottom or worse
1267 const TypeInt *ti = CC->is_int();
1268 if( ti->is_con() ) { // Only 1 kind of condition codes set?
1269 // Match low order 2 bits
1270 int tmp = ((ti->get_con()&3) == (_test&3)) ? 1 : 0;
1271 if( _test & 4 ) tmp = 1-tmp; // Optionally complement result
1272 return TypeInt::make(tmp); // Boolean result
1273 }
1274
1275 if( CC == TypeInt::CC_GE ) {
1276 if( _test == ge ) return TypeInt::ONE;
1277 if( _test == lt ) return TypeInt::ZERO;
1278 }
1279 if( CC == TypeInt::CC_LE ) {
1280 if( _test == le ) return TypeInt::ONE;
1281 if( _test == gt ) return TypeInt::ZERO;
1282 }
1283
1284 return TypeInt::BOOL;
1285 }
1286
1287 //------------------------------dump_spec-------------------------------------
1288 // Print special per-node info
dump_on(outputStream * st) const1289 void BoolTest::dump_on(outputStream *st) const {
1290 const char *msg[] = {"eq","gt","of","lt","ne","le","nof","ge"};
1291 st->print("%s", msg[_test]);
1292 }
1293
1294 // Returns the logical AND of two tests (or 'never' if both tests can never be true).
1295 // For example, a test for 'le' followed by a test for 'lt' is equivalent with 'lt'.
merge(BoolTest other) const1296 BoolTest::mask BoolTest::merge(BoolTest other) const {
1297 const mask res[illegal+1][illegal+1] = {
1298 // eq, gt, of, lt, ne, le, nof, ge, never, illegal
1299 {eq, never, illegal, never, never, eq, illegal, eq, never, illegal}, // eq
1300 {never, gt, illegal, never, gt, never, illegal, gt, never, illegal}, // gt
1301 {illegal, illegal, illegal, illegal, illegal, illegal, illegal, illegal, never, illegal}, // of
1302 {never, never, illegal, lt, lt, lt, illegal, never, never, illegal}, // lt
1303 {never, gt, illegal, lt, ne, lt, illegal, gt, never, illegal}, // ne
1304 {eq, never, illegal, lt, lt, le, illegal, eq, never, illegal}, // le
1305 {illegal, illegal, illegal, illegal, illegal, illegal, illegal, illegal, never, illegal}, // nof
1306 {eq, gt, illegal, never, gt, eq, illegal, ge, never, illegal}, // ge
1307 {never, never, never, never, never, never, never, never, never, illegal}, // never
1308 {illegal, illegal, illegal, illegal, illegal, illegal, illegal, illegal, illegal, illegal}}; // illegal
1309 return res[_test][other._test];
1310 }
1311
1312 //=============================================================================
hash() const1313 uint BoolNode::hash() const { return (Node::hash() << 3)|(_test._test+1); }
size_of() const1314 uint BoolNode::size_of() const { return sizeof(BoolNode); }
1315
1316 //------------------------------operator==-------------------------------------
cmp(const Node & n) const1317 bool BoolNode::cmp( const Node &n ) const {
1318 const BoolNode *b = (const BoolNode *)&n; // Cast up
1319 return (_test._test == b->_test._test);
1320 }
1321
1322 //-------------------------------make_predicate--------------------------------
make_predicate(Node * test_value,PhaseGVN * phase)1323 Node* BoolNode::make_predicate(Node* test_value, PhaseGVN* phase) {
1324 if (test_value->is_Con()) return test_value;
1325 if (test_value->is_Bool()) return test_value;
1326 if (test_value->is_CMove() &&
1327 test_value->in(CMoveNode::Condition)->is_Bool()) {
1328 BoolNode* bol = test_value->in(CMoveNode::Condition)->as_Bool();
1329 const Type* ftype = phase->type(test_value->in(CMoveNode::IfFalse));
1330 const Type* ttype = phase->type(test_value->in(CMoveNode::IfTrue));
1331 if (ftype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ttype)) {
1332 return bol;
1333 } else if (ttype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ftype)) {
1334 return phase->transform( bol->negate(phase) );
1335 }
1336 // Else fall through. The CMove gets in the way of the test.
1337 // It should be the case that make_predicate(bol->as_int_value()) == bol.
1338 }
1339 Node* cmp = new CmpINode(test_value, phase->intcon(0));
1340 cmp = phase->transform(cmp);
1341 Node* bol = new BoolNode(cmp, BoolTest::ne);
1342 return phase->transform(bol);
1343 }
1344
1345 //--------------------------------as_int_value---------------------------------
as_int_value(PhaseGVN * phase)1346 Node* BoolNode::as_int_value(PhaseGVN* phase) {
1347 // Inverse to make_predicate. The CMove probably boils down to a Conv2B.
1348 Node* cmov = CMoveNode::make(NULL, this,
1349 phase->intcon(0), phase->intcon(1),
1350 TypeInt::BOOL);
1351 return phase->transform(cmov);
1352 }
1353
1354 //----------------------------------negate-------------------------------------
negate(PhaseGVN * phase)1355 BoolNode* BoolNode::negate(PhaseGVN* phase) {
1356 return new BoolNode(in(1), _test.negate());
1357 }
1358
1359 // Change "bool eq/ne (cmp (add/sub A B) C)" into false/true if add/sub
1360 // overflows and we can prove that C is not in the two resulting ranges.
1361 // This optimization is similar to the one performed by CmpUNode::Value().
fold_cmpI(PhaseGVN * phase,SubNode * cmp,Node * cmp1,int cmp_op,int cmp1_op,const TypeInt * cmp2_type)1362 Node* BoolNode::fold_cmpI(PhaseGVN* phase, SubNode* cmp, Node* cmp1, int cmp_op,
1363 int cmp1_op, const TypeInt* cmp2_type) {
1364 // Only optimize eq/ne integer comparison of add/sub
1365 if((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1366 (cmp_op == Op_CmpI) && (cmp1_op == Op_AddI || cmp1_op == Op_SubI)) {
1367 // Skip cases were inputs of add/sub are not integers or of bottom type
1368 const TypeInt* r0 = phase->type(cmp1->in(1))->isa_int();
1369 const TypeInt* r1 = phase->type(cmp1->in(2))->isa_int();
1370 if ((r0 != NULL) && (r0 != TypeInt::INT) &&
1371 (r1 != NULL) && (r1 != TypeInt::INT) &&
1372 (cmp2_type != TypeInt::INT)) {
1373 // Compute exact (long) type range of add/sub result
1374 jlong lo_long = r0->_lo;
1375 jlong hi_long = r0->_hi;
1376 if (cmp1_op == Op_AddI) {
1377 lo_long += r1->_lo;
1378 hi_long += r1->_hi;
1379 } else {
1380 lo_long -= r1->_hi;
1381 hi_long -= r1->_lo;
1382 }
1383 // Check for over-/underflow by casting to integer
1384 int lo_int = (int)lo_long;
1385 int hi_int = (int)hi_long;
1386 bool underflow = lo_long != (jlong)lo_int;
1387 bool overflow = hi_long != (jlong)hi_int;
1388 if ((underflow != overflow) && (hi_int < lo_int)) {
1389 // Overflow on one boundary, compute resulting type ranges:
1390 // tr1 [MIN_INT, hi_int] and tr2 [lo_int, MAX_INT]
1391 int w = MAX2(r0->_widen, r1->_widen); // _widen does not matter here
1392 const TypeInt* tr1 = TypeInt::make(min_jint, hi_int, w);
1393 const TypeInt* tr2 = TypeInt::make(lo_int, max_jint, w);
1394 // Compare second input of cmp to both type ranges
1395 const Type* sub_tr1 = cmp->sub(tr1, cmp2_type);
1396 const Type* sub_tr2 = cmp->sub(tr2, cmp2_type);
1397 if (sub_tr1 == TypeInt::CC_LT && sub_tr2 == TypeInt::CC_GT) {
1398 // The result of the add/sub will never equal cmp2. Replace BoolNode
1399 // by false (0) if it tests for equality and by true (1) otherwise.
1400 return ConINode::make((_test._test == BoolTest::eq) ? 0 : 1);
1401 }
1402 }
1403 }
1404 }
1405 return NULL;
1406 }
1407
is_counted_loop_cmp(Node * cmp)1408 static bool is_counted_loop_cmp(Node *cmp) {
1409 Node *n = cmp->in(1)->in(1);
1410 return n != NULL &&
1411 n->is_Phi() &&
1412 n->in(0) != NULL &&
1413 n->in(0)->is_CountedLoop() &&
1414 n->in(0)->as_CountedLoop()->phi() == n;
1415 }
1416
1417 //------------------------------Ideal------------------------------------------
Ideal(PhaseGVN * phase,bool can_reshape)1418 Node *BoolNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1419 // Change "bool tst (cmp con x)" into "bool ~tst (cmp x con)".
1420 // This moves the constant to the right. Helps value-numbering.
1421 Node *cmp = in(1);
1422 if( !cmp->is_Sub() ) return NULL;
1423 int cop = cmp->Opcode();
1424 if( cop == Op_FastLock || cop == Op_FastUnlock || cmp->is_SubTypeCheck()) return NULL;
1425 Node *cmp1 = cmp->in(1);
1426 Node *cmp2 = cmp->in(2);
1427 if( !cmp1 ) return NULL;
1428
1429 if (_test._test == BoolTest::overflow || _test._test == BoolTest::no_overflow) {
1430 return NULL;
1431 }
1432
1433 // Constant on left?
1434 Node *con = cmp1;
1435 uint op2 = cmp2->Opcode();
1436 // Move constants to the right of compare's to canonicalize.
1437 // Do not muck with Opaque1 nodes, as this indicates a loop
1438 // guard that cannot change shape.
1439 if( con->is_Con() && !cmp2->is_Con() && op2 != Op_Opaque1 &&
1440 // Because of NaN's, CmpD and CmpF are not commutative
1441 cop != Op_CmpD && cop != Op_CmpF &&
1442 // Protect against swapping inputs to a compare when it is used by a
1443 // counted loop exit, which requires maintaining the loop-limit as in(2)
1444 !is_counted_loop_exit_test() ) {
1445 // Ok, commute the constant to the right of the cmp node.
1446 // Clone the Node, getting a new Node of the same class
1447 cmp = cmp->clone();
1448 // Swap inputs to the clone
1449 cmp->swap_edges(1, 2);
1450 cmp = phase->transform( cmp );
1451 return new BoolNode( cmp, _test.commute() );
1452 }
1453
1454 // Change "bool eq/ne (cmp (and X 16) 16)" into "bool ne/eq (cmp (and X 16) 0)".
1455 if (cop == Op_CmpI &&
1456 (_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1457 cmp1->Opcode() == Op_AndI && cmp2->Opcode() == Op_ConI &&
1458 cmp1->in(2)->Opcode() == Op_ConI) {
1459 const TypeInt *t12 = phase->type(cmp2)->isa_int();
1460 const TypeInt *t112 = phase->type(cmp1->in(2))->isa_int();
1461 if (t12 && t12->is_con() && t112 && t112->is_con() &&
1462 t12->get_con() == t112->get_con() && is_power_of_2(t12->get_con())) {
1463 Node *ncmp = phase->transform(new CmpINode(cmp1, phase->intcon(0)));
1464 return new BoolNode(ncmp, _test.negate());
1465 }
1466 }
1467
1468 // Same for long type: change "bool eq/ne (cmp (and X 16) 16)" into "bool ne/eq (cmp (and X 16) 0)".
1469 if (cop == Op_CmpL &&
1470 (_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1471 cmp1->Opcode() == Op_AndL && cmp2->Opcode() == Op_ConL &&
1472 cmp1->in(2)->Opcode() == Op_ConL) {
1473 const TypeLong *t12 = phase->type(cmp2)->isa_long();
1474 const TypeLong *t112 = phase->type(cmp1->in(2))->isa_long();
1475 if (t12 && t12->is_con() && t112 && t112->is_con() &&
1476 t12->get_con() == t112->get_con() && is_power_of_2(t12->get_con())) {
1477 Node *ncmp = phase->transform(new CmpLNode(cmp1, phase->longcon(0)));
1478 return new BoolNode(ncmp, _test.negate());
1479 }
1480 }
1481
1482 // Change "bool eq/ne (cmp (xor X 1) 0)" into "bool ne/eq (cmp X 0)".
1483 // The XOR-1 is an idiom used to flip the sense of a bool. We flip the
1484 // test instead.
1485 int cmp1_op = cmp1->Opcode();
1486 const TypeInt* cmp2_type = phase->type(cmp2)->isa_int();
1487 if (cmp2_type == NULL) return NULL;
1488 Node* j_xor = cmp1;
1489 if( cmp2_type == TypeInt::ZERO &&
1490 cmp1_op == Op_XorI &&
1491 j_xor->in(1) != j_xor && // An xor of itself is dead
1492 phase->type( j_xor->in(1) ) == TypeInt::BOOL &&
1493 phase->type( j_xor->in(2) ) == TypeInt::ONE &&
1494 (_test._test == BoolTest::eq ||
1495 _test._test == BoolTest::ne) ) {
1496 Node *ncmp = phase->transform(new CmpINode(j_xor->in(1),cmp2));
1497 return new BoolNode( ncmp, _test.negate() );
1498 }
1499
1500 // Change ((x & m) u<= m) or ((m & x) u<= m) to always true
1501 // Same with ((x & m) u< m+1) and ((m & x) u< m+1)
1502 if (cop == Op_CmpU &&
1503 cmp1_op == Op_AndI) {
1504 Node* bound = NULL;
1505 if (_test._test == BoolTest::le) {
1506 bound = cmp2;
1507 } else if (_test._test == BoolTest::lt &&
1508 cmp2->Opcode() == Op_AddI &&
1509 cmp2->in(2)->find_int_con(0) == 1) {
1510 bound = cmp2->in(1);
1511 }
1512 if (cmp1->in(2) == bound || cmp1->in(1) == bound) {
1513 return ConINode::make(1);
1514 }
1515 }
1516
1517 // Change ((x & (m - 1)) u< m) into (m > 0)
1518 // This is the off-by-one variant of the above
1519 if (cop == Op_CmpU &&
1520 _test._test == BoolTest::lt &&
1521 cmp1_op == Op_AndI) {
1522 Node* l = cmp1->in(1);
1523 Node* r = cmp1->in(2);
1524 for (int repeat = 0; repeat < 2; repeat++) {
1525 bool match = r->Opcode() == Op_AddI && r->in(2)->find_int_con(0) == -1 &&
1526 r->in(1) == cmp2;
1527 if (match) {
1528 // arraylength known to be non-negative, so a (arraylength != 0) is sufficient,
1529 // but to be compatible with the array range check pattern, use (arraylength u> 0)
1530 Node* ncmp = cmp2->Opcode() == Op_LoadRange
1531 ? phase->transform(new CmpUNode(cmp2, phase->intcon(0)))
1532 : phase->transform(new CmpINode(cmp2, phase->intcon(0)));
1533 return new BoolNode(ncmp, BoolTest::gt);
1534 } else {
1535 // commute and try again
1536 l = cmp1->in(2);
1537 r = cmp1->in(1);
1538 }
1539 }
1540 }
1541
1542 // Change x u< 1 or x u<= 0 to x == 0
1543 if (cop == Op_CmpU &&
1544 cmp1_op != Op_LoadRange &&
1545 ((_test._test == BoolTest::lt &&
1546 cmp2->find_int_con(-1) == 1) ||
1547 (_test._test == BoolTest::le &&
1548 cmp2->find_int_con(-1) == 0))) {
1549 Node* ncmp = phase->transform(new CmpINode(cmp1, phase->intcon(0)));
1550 return new BoolNode(ncmp, BoolTest::eq);
1551 }
1552
1553 // Change (arraylength <= 0) or (arraylength == 0)
1554 // into (arraylength u<= 0)
1555 // Also change (arraylength != 0) into (arraylength u> 0)
1556 // The latter version matches the code pattern generated for
1557 // array range checks, which will more likely be optimized later.
1558 if (cop == Op_CmpI &&
1559 cmp1_op == Op_LoadRange &&
1560 cmp2->find_int_con(-1) == 0) {
1561 if (_test._test == BoolTest::le || _test._test == BoolTest::eq) {
1562 Node* ncmp = phase->transform(new CmpUNode(cmp1, cmp2));
1563 return new BoolNode(ncmp, BoolTest::le);
1564 } else if (_test._test == BoolTest::ne) {
1565 Node* ncmp = phase->transform(new CmpUNode(cmp1, cmp2));
1566 return new BoolNode(ncmp, BoolTest::gt);
1567 }
1568 }
1569
1570 // Change "bool eq/ne (cmp (Conv2B X) 0)" into "bool eq/ne (cmp X 0)".
1571 // This is a standard idiom for branching on a boolean value.
1572 Node *c2b = cmp1;
1573 if( cmp2_type == TypeInt::ZERO &&
1574 cmp1_op == Op_Conv2B &&
1575 (_test._test == BoolTest::eq ||
1576 _test._test == BoolTest::ne) ) {
1577 Node *ncmp = phase->transform(phase->type(c2b->in(1))->isa_int()
1578 ? (Node*)new CmpINode(c2b->in(1),cmp2)
1579 : (Node*)new CmpPNode(c2b->in(1),phase->makecon(TypePtr::NULL_PTR))
1580 );
1581 return new BoolNode( ncmp, _test._test );
1582 }
1583
1584 // Comparing a SubI against a zero is equal to comparing the SubI
1585 // arguments directly. This only works for eq and ne comparisons
1586 // due to possible integer overflow.
1587 if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1588 (cop == Op_CmpI) &&
1589 (cmp1_op == Op_SubI) &&
1590 ( cmp2_type == TypeInt::ZERO ) ) {
1591 Node *ncmp = phase->transform( new CmpINode(cmp1->in(1),cmp1->in(2)));
1592 return new BoolNode( ncmp, _test._test );
1593 }
1594
1595 // Same as above but with and AddI of a constant
1596 if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1597 cop == Op_CmpI &&
1598 cmp1_op == Op_AddI &&
1599 cmp1->in(2) != NULL &&
1600 phase->type(cmp1->in(2))->isa_int() &&
1601 phase->type(cmp1->in(2))->is_int()->is_con() &&
1602 cmp2_type == TypeInt::ZERO &&
1603 !is_counted_loop_cmp(cmp) // modifying the exit test of a counted loop messes the counted loop shape
1604 ) {
1605 const TypeInt* cmp1_in2 = phase->type(cmp1->in(2))->is_int();
1606 Node *ncmp = phase->transform( new CmpINode(cmp1->in(1),phase->intcon(-cmp1_in2->_hi)));
1607 return new BoolNode( ncmp, _test._test );
1608 }
1609
1610 // Change "bool eq/ne (cmp (phi (X -X) 0))" into "bool eq/ne (cmp X 0)"
1611 // since zero check of conditional negation of an integer is equal to
1612 // zero check of the integer directly.
1613 if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1614 (cop == Op_CmpI) &&
1615 (cmp2_type == TypeInt::ZERO) &&
1616 (cmp1_op == Op_Phi)) {
1617 // There should be a diamond phi with true path at index 1 or 2
1618 PhiNode *phi = cmp1->as_Phi();
1619 int idx_true = phi->is_diamond_phi();
1620 if (idx_true != 0) {
1621 // True input is in(idx_true) while false input is in(3 - idx_true)
1622 Node *tin = phi->in(idx_true);
1623 Node *fin = phi->in(3 - idx_true);
1624 if ((tin->Opcode() == Op_SubI) &&
1625 (phase->type(tin->in(1)) == TypeInt::ZERO) &&
1626 (tin->in(2) == fin)) {
1627 // Found conditional negation at true path, create a new CmpINode without that
1628 Node *ncmp = phase->transform(new CmpINode(fin, cmp2));
1629 return new BoolNode(ncmp, _test._test);
1630 }
1631 if ((fin->Opcode() == Op_SubI) &&
1632 (phase->type(fin->in(1)) == TypeInt::ZERO) &&
1633 (fin->in(2) == tin)) {
1634 // Found conditional negation at false path, create a new CmpINode without that
1635 Node *ncmp = phase->transform(new CmpINode(tin, cmp2));
1636 return new BoolNode(ncmp, _test._test);
1637 }
1638 }
1639 }
1640
1641 // Change (-A vs 0) into (A vs 0) by commuting the test. Disallow in the
1642 // most general case because negating 0x80000000 does nothing. Needed for
1643 // the CmpF3/SubI/CmpI idiom.
1644 if( cop == Op_CmpI &&
1645 cmp1_op == Op_SubI &&
1646 cmp2_type == TypeInt::ZERO &&
1647 phase->type( cmp1->in(1) ) == TypeInt::ZERO &&
1648 phase->type( cmp1->in(2) )->higher_equal(TypeInt::SYMINT) ) {
1649 Node *ncmp = phase->transform( new CmpINode(cmp1->in(2),cmp2));
1650 return new BoolNode( ncmp, _test.commute() );
1651 }
1652
1653 // Try to optimize signed integer comparison
1654 return fold_cmpI(phase, cmp->as_Sub(), cmp1, cop, cmp1_op, cmp2_type);
1655
1656 // The transformation below is not valid for either signed or unsigned
1657 // comparisons due to wraparound concerns at MAX_VALUE and MIN_VALUE.
1658 // This transformation can be resurrected when we are able to
1659 // make inferences about the range of values being subtracted from
1660 // (or added to) relative to the wraparound point.
1661 //
1662 // // Remove +/-1's if possible.
1663 // // "X <= Y-1" becomes "X < Y"
1664 // // "X+1 <= Y" becomes "X < Y"
1665 // // "X < Y+1" becomes "X <= Y"
1666 // // "X-1 < Y" becomes "X <= Y"
1667 // // Do not this to compares off of the counted-loop-end. These guys are
1668 // // checking the trip counter and they want to use the post-incremented
1669 // // counter. If they use the PRE-incremented counter, then the counter has
1670 // // to be incremented in a private block on a loop backedge.
1671 // if( du && du->cnt(this) && du->out(this)[0]->Opcode() == Op_CountedLoopEnd )
1672 // return NULL;
1673 // #ifndef PRODUCT
1674 // // Do not do this in a wash GVN pass during verification.
1675 // // Gets triggered by too many simple optimizations to be bothered with
1676 // // re-trying it again and again.
1677 // if( !phase->allow_progress() ) return NULL;
1678 // #endif
1679 // // Not valid for unsigned compare because of corner cases in involving zero.
1680 // // For example, replacing "X-1 <u Y" with "X <=u Y" fails to throw an
1681 // // exception in case X is 0 (because 0-1 turns into 4billion unsigned but
1682 // // "0 <=u Y" is always true).
1683 // if( cmp->Opcode() == Op_CmpU ) return NULL;
1684 // int cmp2_op = cmp2->Opcode();
1685 // if( _test._test == BoolTest::le ) {
1686 // if( cmp1_op == Op_AddI &&
1687 // phase->type( cmp1->in(2) ) == TypeInt::ONE )
1688 // return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::lt );
1689 // else if( cmp2_op == Op_AddI &&
1690 // phase->type( cmp2->in(2) ) == TypeInt::MINUS_1 )
1691 // return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::lt );
1692 // } else if( _test._test == BoolTest::lt ) {
1693 // if( cmp1_op == Op_AddI &&
1694 // phase->type( cmp1->in(2) ) == TypeInt::MINUS_1 )
1695 // return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::le );
1696 // else if( cmp2_op == Op_AddI &&
1697 // phase->type( cmp2->in(2) ) == TypeInt::ONE )
1698 // return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::le );
1699 // }
1700 }
1701
1702 //------------------------------Value------------------------------------------
1703 // Simplify a Bool (convert condition codes to boolean (1 or 0)) node,
1704 // based on local information. If the input is constant, do it.
Value(PhaseGVN * phase) const1705 const Type* BoolNode::Value(PhaseGVN* phase) const {
1706 return _test.cc2logical( phase->type( in(1) ) );
1707 }
1708
1709 #ifndef PRODUCT
1710 //------------------------------dump_spec--------------------------------------
1711 // Dump special per-node info
dump_spec(outputStream * st) const1712 void BoolNode::dump_spec(outputStream *st) const {
1713 st->print("[");
1714 _test.dump_on(st);
1715 st->print("]");
1716 }
1717
1718 //-------------------------------related---------------------------------------
1719 // A BoolNode's related nodes are all of its data inputs, and all of its
1720 // outputs until control nodes are hit, which are included. In compact
1721 // representation, inputs till level 3 and immediate outputs are included.
related(GrowableArray<Node * > * in_rel,GrowableArray<Node * > * out_rel,bool compact) const1722 void BoolNode::related(GrowableArray<Node*> *in_rel, GrowableArray<Node*> *out_rel, bool compact) const {
1723 if (compact) {
1724 this->collect_nodes(in_rel, 3, false, true);
1725 this->collect_nodes(out_rel, -1, false, false);
1726 } else {
1727 this->collect_nodes_in_all_data(in_rel, false);
1728 this->collect_nodes_out_all_ctrl_boundary(out_rel);
1729 }
1730 }
1731 #endif
1732
1733 //----------------------is_counted_loop_exit_test------------------------------
1734 // Returns true if node is used by a counted loop node.
is_counted_loop_exit_test()1735 bool BoolNode::is_counted_loop_exit_test() {
1736 for( DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++ ) {
1737 Node* use = fast_out(i);
1738 if (use->is_CountedLoopEnd()) {
1739 return true;
1740 }
1741 }
1742 return false;
1743 }
1744
1745 //=============================================================================
1746 //------------------------------Value------------------------------------------
1747 // Compute sqrt
Value(PhaseGVN * phase) const1748 const Type* SqrtDNode::Value(PhaseGVN* phase) const {
1749 const Type *t1 = phase->type( in(1) );
1750 if( t1 == Type::TOP ) return Type::TOP;
1751 if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1752 double d = t1->getd();
1753 if( d < 0.0 ) return Type::DOUBLE;
1754 return TypeD::make( sqrt( d ) );
1755 }
1756
Value(PhaseGVN * phase) const1757 const Type* SqrtFNode::Value(PhaseGVN* phase) const {
1758 const Type *t1 = phase->type( in(1) );
1759 if( t1 == Type::TOP ) return Type::TOP;
1760 if( t1->base() != Type::FloatCon ) return Type::FLOAT;
1761 float f = t1->getf();
1762 if( f < 0.0f ) return Type::FLOAT;
1763 return TypeF::make( (float)sqrt( (double)f ) );
1764 }
1765