Mon, 25 May 2020 14:24:27 +0800
8244407: JVM crashes after transformation in C2 IdealLoopTree::split_fall_in
Reviewed-by: thartmann, kvn, andrew
Contributed-by: zhouyong44@huawei.com
1 /*
2 * Copyright (c) 1997, 2014, 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 */
25 #include "precompiled.hpp"
26 #include "compiler/compileLog.hpp"
27 #include "memory/allocation.inline.hpp"
28 #include "opto/addnode.hpp"
29 #include "opto/callnode.hpp"
30 #include "opto/cfgnode.hpp"
31 #include "opto/connode.hpp"
32 #include "opto/loopnode.hpp"
33 #include "opto/matcher.hpp"
34 #include "opto/mulnode.hpp"
35 #include "opto/opcodes.hpp"
36 #include "opto/phaseX.hpp"
37 #include "opto/subnode.hpp"
38 #include "runtime/sharedRuntime.hpp"
40 // Portions of code courtesy of Clifford Click
42 // Optimization - Graph Style
44 #include "math.h"
46 //=============================================================================
47 //------------------------------Identity---------------------------------------
48 // If right input is a constant 0, return the left input.
49 Node *SubNode::Identity( PhaseTransform *phase ) {
50 assert(in(1) != this, "Must already have called Value");
51 assert(in(2) != this, "Must already have called Value");
53 // Remove double negation
54 const Type *zero = add_id();
55 if( phase->type( in(1) )->higher_equal( zero ) &&
56 in(2)->Opcode() == Opcode() &&
57 phase->type( in(2)->in(1) )->higher_equal( zero ) ) {
58 return in(2)->in(2);
59 }
61 // Convert "(X+Y) - Y" into X and "(X+Y) - X" into Y
62 if( in(1)->Opcode() == Op_AddI ) {
63 if( phase->eqv(in(1)->in(2),in(2)) )
64 return in(1)->in(1);
65 if (phase->eqv(in(1)->in(1),in(2)))
66 return in(1)->in(2);
68 // Also catch: "(X + Opaque2(Y)) - Y". In this case, 'Y' is a loop-varying
69 // trip counter and X is likely to be loop-invariant (that's how O2 Nodes
70 // are originally used, although the optimizer sometimes jiggers things).
71 // This folding through an O2 removes a loop-exit use of a loop-varying
72 // value and generally lowers register pressure in and around the loop.
73 if( in(1)->in(2)->Opcode() == Op_Opaque2 &&
74 phase->eqv(in(1)->in(2)->in(1),in(2)) )
75 return in(1)->in(1);
76 }
78 return ( phase->type( in(2) )->higher_equal( zero ) ) ? in(1) : this;
79 }
81 //------------------------------Value------------------------------------------
82 // A subtract node differences it's two inputs.
83 const Type* SubNode::Value_common(PhaseTransform *phase) const {
84 const Node* in1 = in(1);
85 const Node* in2 = in(2);
86 // Either input is TOP ==> the result is TOP
87 const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
88 if( t1 == Type::TOP ) return Type::TOP;
89 const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
90 if( t2 == Type::TOP ) return Type::TOP;
92 // Not correct for SubFnode and AddFNode (must check for infinity)
93 // Equal? Subtract is zero
94 if (in1->eqv_uncast(in2)) return add_id();
96 // Either input is BOTTOM ==> the result is the local BOTTOM
97 if( t1 == Type::BOTTOM || t2 == Type::BOTTOM )
98 return bottom_type();
100 return NULL;
101 }
103 const Type* SubNode::Value(PhaseTransform *phase) const {
104 const Type* t = Value_common(phase);
105 if (t != NULL) {
106 return t;
107 }
108 const Type* t1 = phase->type(in(1));
109 const Type* t2 = phase->type(in(2));
110 return sub(t1,t2); // Local flavor of type subtraction
112 }
114 //=============================================================================
116 //------------------------------Helper function--------------------------------
117 static bool ok_to_convert(Node* inc, Node* iv) {
118 // Do not collapse (x+c0)-y if "+" is a loop increment, because the
119 // "-" is loop invariant and collapsing extends the live-range of "x"
120 // to overlap with the "+", forcing another register to be used in
121 // the loop.
122 // This test will be clearer with '&&' (apply DeMorgan's rule)
123 // but I like the early cutouts that happen here.
124 const PhiNode *phi;
125 if( ( !inc->in(1)->is_Phi() ||
126 !(phi=inc->in(1)->as_Phi()) ||
127 phi->is_copy() ||
128 !phi->region()->is_CountedLoop() ||
129 inc != phi->region()->as_CountedLoop()->incr() )
130 &&
131 // Do not collapse (x+c0)-iv if "iv" is a loop induction variable,
132 // because "x" maybe invariant.
133 ( !iv->is_loop_iv() )
134 ) {
135 return true;
136 } else {
137 return false;
138 }
139 }
140 //------------------------------Ideal------------------------------------------
141 Node *SubINode::Ideal(PhaseGVN *phase, bool can_reshape){
142 Node *in1 = in(1);
143 Node *in2 = in(2);
144 uint op1 = in1->Opcode();
145 uint op2 = in2->Opcode();
147 #ifdef ASSERT
148 // Check for dead loop
149 if( phase->eqv( in1, this ) || phase->eqv( in2, this ) ||
150 ( op1 == Op_AddI || op1 == Op_SubI ) &&
151 ( phase->eqv( in1->in(1), this ) || phase->eqv( in1->in(2), this ) ||
152 phase->eqv( in1->in(1), in1 ) || phase->eqv( in1->in(2), in1 ) ) )
153 assert(false, "dead loop in SubINode::Ideal");
154 #endif
156 const Type *t2 = phase->type( in2 );
157 if( t2 == Type::TOP ) return NULL;
158 // Convert "x-c0" into "x+ -c0".
159 if( t2->base() == Type::Int ){ // Might be bottom or top...
160 const TypeInt *i = t2->is_int();
161 if( i->is_con() )
162 return new (phase->C) AddINode(in1, phase->intcon(-i->get_con()));
163 }
165 // Convert "(x+c0) - y" into (x-y) + c0"
166 // Do not collapse (x+c0)-y if "+" is a loop increment or
167 // if "y" is a loop induction variable.
168 if( op1 == Op_AddI && ok_to_convert(in1, in2) ) {
169 const Type *tadd = phase->type( in1->in(2) );
170 if( tadd->singleton() && tadd != Type::TOP ) {
171 Node *sub2 = phase->transform( new (phase->C) SubINode( in1->in(1), in2 ));
172 return new (phase->C) AddINode( sub2, in1->in(2) );
173 }
174 }
177 // Convert "x - (y+c0)" into "(x-y) - c0"
178 // Need the same check as in above optimization but reversed.
179 if (op2 == Op_AddI && ok_to_convert(in2, in1)) {
180 Node* in21 = in2->in(1);
181 Node* in22 = in2->in(2);
182 const TypeInt* tcon = phase->type(in22)->isa_int();
183 if (tcon != NULL && tcon->is_con()) {
184 Node* sub2 = phase->transform( new (phase->C) SubINode(in1, in21) );
185 Node* neg_c0 = phase->intcon(- tcon->get_con());
186 return new (phase->C) AddINode(sub2, neg_c0);
187 }
188 }
190 const Type *t1 = phase->type( in1 );
191 if( t1 == Type::TOP ) return NULL;
193 #ifdef ASSERT
194 // Check for dead loop
195 if( ( op2 == Op_AddI || op2 == Op_SubI ) &&
196 ( phase->eqv( in2->in(1), this ) || phase->eqv( in2->in(2), this ) ||
197 phase->eqv( in2->in(1), in2 ) || phase->eqv( in2->in(2), in2 ) ) )
198 assert(false, "dead loop in SubINode::Ideal");
199 #endif
201 // Convert "x - (x+y)" into "-y"
202 if( op2 == Op_AddI &&
203 phase->eqv( in1, in2->in(1) ) )
204 return new (phase->C) SubINode( phase->intcon(0),in2->in(2));
205 // Convert "(x-y) - x" into "-y"
206 if( op1 == Op_SubI &&
207 phase->eqv( in1->in(1), in2 ) )
208 return new (phase->C) SubINode( phase->intcon(0),in1->in(2));
209 // Convert "x - (y+x)" into "-y"
210 if( op2 == Op_AddI &&
211 phase->eqv( in1, in2->in(2) ) )
212 return new (phase->C) SubINode( phase->intcon(0),in2->in(1));
214 // Convert "0 - (x-y)" into "y-x"
215 if( t1 == TypeInt::ZERO && op2 == Op_SubI )
216 return new (phase->C) SubINode( in2->in(2), in2->in(1) );
218 // Convert "0 - (x+con)" into "-con-x"
219 jint con;
220 if( t1 == TypeInt::ZERO && op2 == Op_AddI &&
221 (con = in2->in(2)->find_int_con(0)) != 0 )
222 return new (phase->C) SubINode( phase->intcon(-con), in2->in(1) );
224 // Convert "(X+A) - (X+B)" into "A - B"
225 if( op1 == Op_AddI && op2 == Op_AddI && in1->in(1) == in2->in(1) )
226 return new (phase->C) SubINode( in1->in(2), in2->in(2) );
228 // Convert "(A+X) - (B+X)" into "A - B"
229 if( op1 == Op_AddI && op2 == Op_AddI && in1->in(2) == in2->in(2) )
230 return new (phase->C) SubINode( in1->in(1), in2->in(1) );
232 // Convert "(A+X) - (X+B)" into "A - B"
233 if( op1 == Op_AddI && op2 == Op_AddI && in1->in(2) == in2->in(1) )
234 return new (phase->C) SubINode( in1->in(1), in2->in(2) );
236 // Convert "(X+A) - (B+X)" into "A - B"
237 if( op1 == Op_AddI && op2 == Op_AddI && in1->in(1) == in2->in(2) )
238 return new (phase->C) SubINode( in1->in(2), in2->in(1) );
240 // Convert "A-(B-C)" into (A+C)-B", since add is commutative and generally
241 // nicer to optimize than subtract.
242 if( op2 == Op_SubI && in2->outcnt() == 1) {
243 Node *add1 = phase->transform( new (phase->C) AddINode( in1, in2->in(2) ) );
244 return new (phase->C) SubINode( add1, in2->in(1) );
245 }
247 return NULL;
248 }
250 //------------------------------sub--------------------------------------------
251 // A subtract node differences it's two inputs.
252 const Type *SubINode::sub( const Type *t1, const Type *t2 ) const {
253 const TypeInt *r0 = t1->is_int(); // Handy access
254 const TypeInt *r1 = t2->is_int();
255 int32 lo = java_subtract(r0->_lo, r1->_hi);
256 int32 hi = java_subtract(r0->_hi, r1->_lo);
258 // We next check for 32-bit overflow.
259 // If that happens, we just assume all integers are possible.
260 if( (((r0->_lo ^ r1->_hi) >= 0) || // lo ends have same signs OR
261 ((r0->_lo ^ lo) >= 0)) && // lo results have same signs AND
262 (((r0->_hi ^ r1->_lo) >= 0) || // hi ends have same signs OR
263 ((r0->_hi ^ hi) >= 0)) ) // hi results have same signs
264 return TypeInt::make(lo,hi,MAX2(r0->_widen,r1->_widen));
265 else // Overflow; assume all integers
266 return TypeInt::INT;
267 }
269 //=============================================================================
270 //------------------------------Ideal------------------------------------------
271 Node *SubLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
272 Node *in1 = in(1);
273 Node *in2 = in(2);
274 uint op1 = in1->Opcode();
275 uint op2 = in2->Opcode();
277 #ifdef ASSERT
278 // Check for dead loop
279 if( phase->eqv( in1, this ) || phase->eqv( in2, this ) ||
280 ( op1 == Op_AddL || op1 == Op_SubL ) &&
281 ( phase->eqv( in1->in(1), this ) || phase->eqv( in1->in(2), this ) ||
282 phase->eqv( in1->in(1), in1 ) || phase->eqv( in1->in(2), in1 ) ) )
283 assert(false, "dead loop in SubLNode::Ideal");
284 #endif
286 if( phase->type( in2 ) == Type::TOP ) return NULL;
287 const TypeLong *i = phase->type( in2 )->isa_long();
288 // Convert "x-c0" into "x+ -c0".
289 if( i && // Might be bottom or top...
290 i->is_con() )
291 return new (phase->C) AddLNode(in1, phase->longcon(-i->get_con()));
293 // Convert "(x+c0) - y" into (x-y) + c0"
294 // Do not collapse (x+c0)-y if "+" is a loop increment or
295 // if "y" is a loop induction variable.
296 if( op1 == Op_AddL && ok_to_convert(in1, in2) ) {
297 Node *in11 = in1->in(1);
298 const Type *tadd = phase->type( in1->in(2) );
299 if( tadd->singleton() && tadd != Type::TOP ) {
300 Node *sub2 = phase->transform( new (phase->C) SubLNode( in11, in2 ));
301 return new (phase->C) AddLNode( sub2, in1->in(2) );
302 }
303 }
305 // Convert "x - (y+c0)" into "(x-y) - c0"
306 // Need the same check as in above optimization but reversed.
307 if (op2 == Op_AddL && ok_to_convert(in2, in1)) {
308 Node* in21 = in2->in(1);
309 Node* in22 = in2->in(2);
310 const TypeLong* tcon = phase->type(in22)->isa_long();
311 if (tcon != NULL && tcon->is_con()) {
312 Node* sub2 = phase->transform( new (phase->C) SubLNode(in1, in21) );
313 Node* neg_c0 = phase->longcon(- tcon->get_con());
314 return new (phase->C) AddLNode(sub2, neg_c0);
315 }
316 }
318 const Type *t1 = phase->type( in1 );
319 if( t1 == Type::TOP ) return NULL;
321 #ifdef ASSERT
322 // Check for dead loop
323 if( ( op2 == Op_AddL || op2 == Op_SubL ) &&
324 ( phase->eqv( in2->in(1), this ) || phase->eqv( in2->in(2), this ) ||
325 phase->eqv( in2->in(1), in2 ) || phase->eqv( in2->in(2), in2 ) ) )
326 assert(false, "dead loop in SubLNode::Ideal");
327 #endif
329 // Convert "x - (x+y)" into "-y"
330 if( op2 == Op_AddL &&
331 phase->eqv( in1, in2->in(1) ) )
332 return new (phase->C) SubLNode( phase->makecon(TypeLong::ZERO), in2->in(2));
333 // Convert "x - (y+x)" into "-y"
334 if( op2 == Op_AddL &&
335 phase->eqv( in1, in2->in(2) ) )
336 return new (phase->C) SubLNode( phase->makecon(TypeLong::ZERO),in2->in(1));
338 // Convert "0 - (x-y)" into "y-x"
339 if( phase->type( in1 ) == TypeLong::ZERO && op2 == Op_SubL )
340 return new (phase->C) SubLNode( in2->in(2), in2->in(1) );
342 // Convert "(X+A) - (X+B)" into "A - B"
343 if( op1 == Op_AddL && op2 == Op_AddL && in1->in(1) == in2->in(1) )
344 return new (phase->C) SubLNode( in1->in(2), in2->in(2) );
346 // Convert "(A+X) - (B+X)" into "A - B"
347 if( op1 == Op_AddL && op2 == Op_AddL && in1->in(2) == in2->in(2) )
348 return new (phase->C) SubLNode( in1->in(1), in2->in(1) );
350 // Convert "A-(B-C)" into (A+C)-B"
351 if( op2 == Op_SubL && in2->outcnt() == 1) {
352 Node *add1 = phase->transform( new (phase->C) AddLNode( in1, in2->in(2) ) );
353 return new (phase->C) SubLNode( add1, in2->in(1) );
354 }
356 return NULL;
357 }
359 //------------------------------sub--------------------------------------------
360 // A subtract node differences it's two inputs.
361 const Type *SubLNode::sub( const Type *t1, const Type *t2 ) const {
362 const TypeLong *r0 = t1->is_long(); // Handy access
363 const TypeLong *r1 = t2->is_long();
364 jlong lo = java_subtract(r0->_lo, r1->_hi);
365 jlong hi = java_subtract(r0->_hi, r1->_lo);
367 // We next check for 32-bit overflow.
368 // If that happens, we just assume all integers are possible.
369 if( (((r0->_lo ^ r1->_hi) >= 0) || // lo ends have same signs OR
370 ((r0->_lo ^ lo) >= 0)) && // lo results have same signs AND
371 (((r0->_hi ^ r1->_lo) >= 0) || // hi ends have same signs OR
372 ((r0->_hi ^ hi) >= 0)) ) // hi results have same signs
373 return TypeLong::make(lo,hi,MAX2(r0->_widen,r1->_widen));
374 else // Overflow; assume all integers
375 return TypeLong::LONG;
376 }
378 //=============================================================================
379 //------------------------------Value------------------------------------------
380 // A subtract node differences its two inputs.
381 const Type *SubFPNode::Value( PhaseTransform *phase ) const {
382 const Node* in1 = in(1);
383 const Node* in2 = in(2);
384 // Either input is TOP ==> the result is TOP
385 const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
386 if( t1 == Type::TOP ) return Type::TOP;
387 const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
388 if( t2 == Type::TOP ) return Type::TOP;
390 // if both operands are infinity of same sign, the result is NaN; do
391 // not replace with zero
392 if( (t1->is_finite() && t2->is_finite()) ) {
393 if( phase->eqv(in1, in2) ) return add_id();
394 }
396 // Either input is BOTTOM ==> the result is the local BOTTOM
397 const Type *bot = bottom_type();
398 if( (t1 == bot) || (t2 == bot) ||
399 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
400 return bot;
402 return sub(t1,t2); // Local flavor of type subtraction
403 }
406 //=============================================================================
407 //------------------------------Ideal------------------------------------------
408 Node *SubFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
409 const Type *t2 = phase->type( in(2) );
410 // Convert "x-c0" into "x+ -c0".
411 if( t2->base() == Type::FloatCon ) { // Might be bottom or top...
412 // return new (phase->C, 3) AddFNode(in(1), phase->makecon( TypeF::make(-t2->getf()) ) );
413 }
415 // Not associative because of boundary conditions (infinity)
416 if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
417 // Convert "x - (x+y)" into "-y"
418 if( in(2)->is_Add() &&
419 phase->eqv(in(1),in(2)->in(1) ) )
420 return new (phase->C) SubFNode( phase->makecon(TypeF::ZERO),in(2)->in(2));
421 }
423 // Cannot replace 0.0-X with -X because a 'fsub' bytecode computes
424 // 0.0-0.0 as +0.0, while a 'fneg' bytecode computes -0.0.
425 //if( phase->type(in(1)) == TypeF::ZERO )
426 //return new (phase->C, 2) NegFNode(in(2));
428 return NULL;
429 }
431 //------------------------------sub--------------------------------------------
432 // A subtract node differences its two inputs.
433 const Type *SubFNode::sub( const Type *t1, const Type *t2 ) const {
434 // no folding if one of operands is infinity or NaN, do not do constant folding
435 if( g_isfinite(t1->getf()) && g_isfinite(t2->getf()) ) {
436 return TypeF::make( t1->getf() - t2->getf() );
437 }
438 else if( g_isnan(t1->getf()) ) {
439 return t1;
440 }
441 else if( g_isnan(t2->getf()) ) {
442 return t2;
443 }
444 else {
445 return Type::FLOAT;
446 }
447 }
449 //=============================================================================
450 //------------------------------Ideal------------------------------------------
451 Node *SubDNode::Ideal(PhaseGVN *phase, bool can_reshape){
452 const Type *t2 = phase->type( in(2) );
453 // Convert "x-c0" into "x+ -c0".
454 if( t2->base() == Type::DoubleCon ) { // Might be bottom or top...
455 // return new (phase->C, 3) AddDNode(in(1), phase->makecon( TypeD::make(-t2->getd()) ) );
456 }
458 // Not associative because of boundary conditions (infinity)
459 if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
460 // Convert "x - (x+y)" into "-y"
461 if( in(2)->is_Add() &&
462 phase->eqv(in(1),in(2)->in(1) ) )
463 return new (phase->C) SubDNode( phase->makecon(TypeD::ZERO),in(2)->in(2));
464 }
466 // Cannot replace 0.0-X with -X because a 'dsub' bytecode computes
467 // 0.0-0.0 as +0.0, while a 'dneg' bytecode computes -0.0.
468 //if( phase->type(in(1)) == TypeD::ZERO )
469 //return new (phase->C, 2) NegDNode(in(2));
471 return NULL;
472 }
474 //------------------------------sub--------------------------------------------
475 // A subtract node differences its two inputs.
476 const Type *SubDNode::sub( const Type *t1, const Type *t2 ) const {
477 // no folding if one of operands is infinity or NaN, do not do constant folding
478 if( g_isfinite(t1->getd()) && g_isfinite(t2->getd()) ) {
479 return TypeD::make( t1->getd() - t2->getd() );
480 }
481 else if( g_isnan(t1->getd()) ) {
482 return t1;
483 }
484 else if( g_isnan(t2->getd()) ) {
485 return t2;
486 }
487 else {
488 return Type::DOUBLE;
489 }
490 }
492 //=============================================================================
493 //------------------------------Idealize---------------------------------------
494 // Unlike SubNodes, compare must still flatten return value to the
495 // range -1, 0, 1.
496 // And optimizations like those for (X + Y) - X fail if overflow happens.
497 Node *CmpNode::Identity( PhaseTransform *phase ) {
498 return this;
499 }
501 //=============================================================================
502 //------------------------------cmp--------------------------------------------
503 // Simplify a CmpI (compare 2 integers) node, based on local information.
504 // If both inputs are constants, compare them.
505 const Type *CmpINode::sub( const Type *t1, const Type *t2 ) const {
506 const TypeInt *r0 = t1->is_int(); // Handy access
507 const TypeInt *r1 = t2->is_int();
509 if( r0->_hi < r1->_lo ) // Range is always low?
510 return TypeInt::CC_LT;
511 else if( r0->_lo > r1->_hi ) // Range is always high?
512 return TypeInt::CC_GT;
514 else if( r0->is_con() && r1->is_con() ) { // comparing constants?
515 assert(r0->get_con() == r1->get_con(), "must be equal");
516 return TypeInt::CC_EQ; // Equal results.
517 } else if( r0->_hi == r1->_lo ) // Range is never high?
518 return TypeInt::CC_LE;
519 else if( r0->_lo == r1->_hi ) // Range is never low?
520 return TypeInt::CC_GE;
521 return TypeInt::CC; // else use worst case results
522 }
524 // Simplify a CmpU (compare 2 integers) node, based on local information.
525 // If both inputs are constants, compare them.
526 const Type *CmpUNode::sub( const Type *t1, const Type *t2 ) const {
527 assert(!t1->isa_ptr(), "obsolete usage of CmpU");
529 // comparing two unsigned ints
530 const TypeInt *r0 = t1->is_int(); // Handy access
531 const TypeInt *r1 = t2->is_int();
533 // Current installed version
534 // Compare ranges for non-overlap
535 juint lo0 = r0->_lo;
536 juint hi0 = r0->_hi;
537 juint lo1 = r1->_lo;
538 juint hi1 = r1->_hi;
540 // If either one has both negative and positive values,
541 // it therefore contains both 0 and -1, and since [0..-1] is the
542 // full unsigned range, the type must act as an unsigned bottom.
543 bool bot0 = ((jint)(lo0 ^ hi0) < 0);
544 bool bot1 = ((jint)(lo1 ^ hi1) < 0);
546 if (bot0 || bot1) {
547 // All unsigned values are LE -1 and GE 0.
548 if (lo0 == 0 && hi0 == 0) {
549 return TypeInt::CC_LE; // 0 <= bot
550 } else if ((jint)lo0 == -1 && (jint)hi0 == -1) {
551 return TypeInt::CC_GE; // -1 >= bot
552 } else if (lo1 == 0 && hi1 == 0) {
553 return TypeInt::CC_GE; // bot >= 0
554 } else if ((jint)lo1 == -1 && (jint)hi1 == -1) {
555 return TypeInt::CC_LE; // bot <= -1
556 }
557 } else {
558 // We can use ranges of the form [lo..hi] if signs are the same.
559 assert(lo0 <= hi0 && lo1 <= hi1, "unsigned ranges are valid");
560 // results are reversed, '-' > '+' for unsigned compare
561 if (hi0 < lo1) {
562 return TypeInt::CC_LT; // smaller
563 } else if (lo0 > hi1) {
564 return TypeInt::CC_GT; // greater
565 } else if (hi0 == lo1 && lo0 == hi1) {
566 return TypeInt::CC_EQ; // Equal results
567 } else if (lo0 >= hi1) {
568 return TypeInt::CC_GE;
569 } else if (hi0 <= lo1) {
570 // Check for special case in Hashtable::get. (See below.)
571 if ((jint)lo0 >= 0 && (jint)lo1 >= 0 && is_index_range_check())
572 return TypeInt::CC_LT;
573 return TypeInt::CC_LE;
574 }
575 }
576 // Check for special case in Hashtable::get - the hash index is
577 // mod'ed to the table size so the following range check is useless.
578 // Check for: (X Mod Y) CmpU Y, where the mod result and Y both have
579 // to be positive.
580 // (This is a gross hack, since the sub method never
581 // looks at the structure of the node in any other case.)
582 if ((jint)lo0 >= 0 && (jint)lo1 >= 0 && is_index_range_check())
583 return TypeInt::CC_LT;
584 return TypeInt::CC; // else use worst case results
585 }
587 const Type* CmpUNode::Value(PhaseTransform *phase) const {
588 const Type* t = SubNode::Value_common(phase);
589 if (t != NULL) {
590 return t;
591 }
592 const Node* in1 = in(1);
593 const Node* in2 = in(2);
594 const Type* t1 = phase->type(in1);
595 const Type* t2 = phase->type(in2);
596 assert(t1->isa_int(), "CmpU has only Int type inputs");
597 if (t2 == TypeInt::INT) { // Compare to bottom?
598 return bottom_type();
599 }
600 uint in1_op = in1->Opcode();
601 if (in1_op == Op_AddI || in1_op == Op_SubI) {
602 // The problem rise when result of AddI(SubI) may overflow
603 // signed integer value. Let say the input type is
604 // [256, maxint] then +128 will create 2 ranges due to
605 // overflow: [minint, minint+127] and [384, maxint].
606 // But C2 type system keep only 1 type range and as result
607 // it use general [minint, maxint] for this case which we
608 // can't optimize.
609 //
610 // Make 2 separate type ranges based on types of AddI(SubI) inputs
611 // and compare results of their compare. If results are the same
612 // CmpU node can be optimized.
613 const Node* in11 = in1->in(1);
614 const Node* in12 = in1->in(2);
615 const Type* t11 = (in11 == in1) ? Type::TOP : phase->type(in11);
616 const Type* t12 = (in12 == in1) ? Type::TOP : phase->type(in12);
617 // Skip cases when input types are top or bottom.
618 if ((t11 != Type::TOP) && (t11 != TypeInt::INT) &&
619 (t12 != Type::TOP) && (t12 != TypeInt::INT)) {
620 const TypeInt *r0 = t11->is_int();
621 const TypeInt *r1 = t12->is_int();
622 jlong lo_r0 = r0->_lo;
623 jlong hi_r0 = r0->_hi;
624 jlong lo_r1 = r1->_lo;
625 jlong hi_r1 = r1->_hi;
626 if (in1_op == Op_SubI) {
627 jlong tmp = hi_r1;
628 hi_r1 = -lo_r1;
629 lo_r1 = -tmp;
630 // Note, for substructing [minint,x] type range
631 // long arithmetic provides correct overflow answer.
632 // The confusion come from the fact that in 32-bit
633 // -minint == minint but in 64-bit -minint == maxint+1.
634 }
635 jlong lo_long = lo_r0 + lo_r1;
636 jlong hi_long = hi_r0 + hi_r1;
637 int lo_tr1 = min_jint;
638 int hi_tr1 = (int)hi_long;
639 int lo_tr2 = (int)lo_long;
640 int hi_tr2 = max_jint;
641 bool underflow = lo_long != (jlong)lo_tr2;
642 bool overflow = hi_long != (jlong)hi_tr1;
643 // Use sub(t1, t2) when there is no overflow (one type range)
644 // or when both overflow and underflow (too complex).
645 if ((underflow != overflow) && (hi_tr1 < lo_tr2)) {
646 // Overflow only on one boundary, compare 2 separate type ranges.
647 int w = MAX2(r0->_widen, r1->_widen); // _widen does not matter here
648 const TypeInt* tr1 = TypeInt::make(lo_tr1, hi_tr1, w);
649 const TypeInt* tr2 = TypeInt::make(lo_tr2, hi_tr2, w);
650 const Type* cmp1 = sub(tr1, t2);
651 const Type* cmp2 = sub(tr2, t2);
652 if (cmp1 == cmp2) {
653 return cmp1; // Hit!
654 }
655 }
656 }
657 }
659 return sub(t1, t2); // Local flavor of type subtraction
660 }
662 bool CmpUNode::is_index_range_check() const {
663 // Check for the "(X ModI Y) CmpU Y" shape
664 return (in(1)->Opcode() == Op_ModI &&
665 in(1)->in(2)->eqv_uncast(in(2)));
666 }
668 //------------------------------Idealize---------------------------------------
669 Node *CmpINode::Ideal( PhaseGVN *phase, bool can_reshape ) {
670 if (phase->type(in(2))->higher_equal(TypeInt::ZERO)) {
671 switch (in(1)->Opcode()) {
672 case Op_CmpL3: // Collapse a CmpL3/CmpI into a CmpL
673 return new (phase->C) CmpLNode(in(1)->in(1),in(1)->in(2));
674 case Op_CmpF3: // Collapse a CmpF3/CmpI into a CmpF
675 return new (phase->C) CmpFNode(in(1)->in(1),in(1)->in(2));
676 case Op_CmpD3: // Collapse a CmpD3/CmpI into a CmpD
677 return new (phase->C) CmpDNode(in(1)->in(1),in(1)->in(2));
678 //case Op_SubI:
679 // If (x - y) cannot overflow, then ((x - y) <?> 0)
680 // can be turned into (x <?> y).
681 // This is handled (with more general cases) by Ideal_sub_algebra.
682 }
683 }
684 return NULL; // No change
685 }
688 //=============================================================================
689 // Simplify a CmpL (compare 2 longs ) node, based on local information.
690 // If both inputs are constants, compare them.
691 const Type *CmpLNode::sub( const Type *t1, const Type *t2 ) const {
692 const TypeLong *r0 = t1->is_long(); // Handy access
693 const TypeLong *r1 = t2->is_long();
695 if( r0->_hi < r1->_lo ) // Range is always low?
696 return TypeInt::CC_LT;
697 else if( r0->_lo > r1->_hi ) // Range is always high?
698 return TypeInt::CC_GT;
700 else if( r0->is_con() && r1->is_con() ) { // comparing constants?
701 assert(r0->get_con() == r1->get_con(), "must be equal");
702 return TypeInt::CC_EQ; // Equal results.
703 } else if( r0->_hi == r1->_lo ) // Range is never high?
704 return TypeInt::CC_LE;
705 else if( r0->_lo == r1->_hi ) // Range is never low?
706 return TypeInt::CC_GE;
707 return TypeInt::CC; // else use worst case results
708 }
711 // Simplify a CmpUL (compare 2 unsigned longs) node, based on local information.
712 // If both inputs are constants, compare them.
713 const Type* CmpULNode::sub(const Type* t1, const Type* t2) const {
714 assert(!t1->isa_ptr(), "obsolete usage of CmpUL");
716 // comparing two unsigned longs
717 const TypeLong* r0 = t1->is_long(); // Handy access
718 const TypeLong* r1 = t2->is_long();
720 // Current installed version
721 // Compare ranges for non-overlap
722 julong lo0 = r0->_lo;
723 julong hi0 = r0->_hi;
724 julong lo1 = r1->_lo;
725 julong hi1 = r1->_hi;
727 // If either one has both negative and positive values,
728 // it therefore contains both 0 and -1, and since [0..-1] is the
729 // full unsigned range, the type must act as an unsigned bottom.
730 bool bot0 = ((jlong)(lo0 ^ hi0) < 0);
731 bool bot1 = ((jlong)(lo1 ^ hi1) < 0);
733 if (bot0 || bot1) {
734 // All unsigned values are LE -1 and GE 0.
735 if (lo0 == 0 && hi0 == 0) {
736 return TypeInt::CC_LE; // 0 <= bot
737 } else if ((jlong)lo0 == -1 && (jlong)hi0 == -1) {
738 return TypeInt::CC_GE; // -1 >= bot
739 } else if (lo1 == 0 && hi1 == 0) {
740 return TypeInt::CC_GE; // bot >= 0
741 } else if ((jlong)lo1 == -1 && (jlong)hi1 == -1) {
742 return TypeInt::CC_LE; // bot <= -1
743 }
744 } else {
745 // We can use ranges of the form [lo..hi] if signs are the same.
746 assert(lo0 <= hi0 && lo1 <= hi1, "unsigned ranges are valid");
747 // results are reversed, '-' > '+' for unsigned compare
748 if (hi0 < lo1) {
749 return TypeInt::CC_LT; // smaller
750 } else if (lo0 > hi1) {
751 return TypeInt::CC_GT; // greater
752 } else if (hi0 == lo1 && lo0 == hi1) {
753 return TypeInt::CC_EQ; // Equal results
754 } else if (lo0 >= hi1) {
755 return TypeInt::CC_GE;
756 } else if (hi0 <= lo1) {
757 return TypeInt::CC_LE;
758 }
759 }
761 return TypeInt::CC; // else use worst case results
762 }
764 //=============================================================================
765 //------------------------------sub--------------------------------------------
766 // Simplify an CmpP (compare 2 pointers) node, based on local information.
767 // If both inputs are constants, compare them.
768 const Type *CmpPNode::sub( const Type *t1, const Type *t2 ) const {
769 const TypePtr *r0 = t1->is_ptr(); // Handy access
770 const TypePtr *r1 = t2->is_ptr();
772 // Undefined inputs makes for an undefined result
773 if( TypePtr::above_centerline(r0->_ptr) ||
774 TypePtr::above_centerline(r1->_ptr) )
775 return Type::TOP;
777 if (r0 == r1 && r0->singleton()) {
778 // Equal pointer constants (klasses, nulls, etc.)
779 return TypeInt::CC_EQ;
780 }
782 // See if it is 2 unrelated classes.
783 const TypeOopPtr* p0 = r0->isa_oopptr();
784 const TypeOopPtr* p1 = r1->isa_oopptr();
785 if (p0 && p1) {
786 Node* in1 = in(1)->uncast();
787 Node* in2 = in(2)->uncast();
788 AllocateNode* alloc1 = AllocateNode::Ideal_allocation(in1, NULL);
789 AllocateNode* alloc2 = AllocateNode::Ideal_allocation(in2, NULL);
790 if (MemNode::detect_ptr_independence(in1, alloc1, in2, alloc2, NULL)) {
791 return TypeInt::CC_GT; // different pointers
792 }
793 ciKlass* klass0 = p0->klass();
794 bool xklass0 = p0->klass_is_exact();
795 ciKlass* klass1 = p1->klass();
796 bool xklass1 = p1->klass_is_exact();
797 int kps = (p0->isa_klassptr()?1:0) + (p1->isa_klassptr()?1:0);
798 if (klass0 && klass1 &&
799 kps != 1 && // both or neither are klass pointers
800 klass0->is_loaded() && !klass0->is_interface() && // do not trust interfaces
801 klass1->is_loaded() && !klass1->is_interface() &&
802 (!klass0->is_obj_array_klass() ||
803 !klass0->as_obj_array_klass()->base_element_klass()->is_interface()) &&
804 (!klass1->is_obj_array_klass() ||
805 !klass1->as_obj_array_klass()->base_element_klass()->is_interface())) {
806 bool unrelated_classes = false;
807 // See if neither subclasses the other, or if the class on top
808 // is precise. In either of these cases, the compare is known
809 // to fail if at least one of the pointers is provably not null.
810 if (klass0->equals(klass1)) { // if types are unequal but klasses are equal
811 // Do nothing; we know nothing for imprecise types
812 } else if (klass0->is_subtype_of(klass1)) {
813 // If klass1's type is PRECISE, then classes are unrelated.
814 unrelated_classes = xklass1;
815 } else if (klass1->is_subtype_of(klass0)) {
816 // If klass0's type is PRECISE, then classes are unrelated.
817 unrelated_classes = xklass0;
818 } else { // Neither subtypes the other
819 unrelated_classes = true;
820 }
821 if (unrelated_classes) {
822 // The oops classes are known to be unrelated. If the joined PTRs of
823 // two oops is not Null and not Bottom, then we are sure that one
824 // of the two oops is non-null, and the comparison will always fail.
825 TypePtr::PTR jp = r0->join_ptr(r1->_ptr);
826 if (jp != TypePtr::Null && jp != TypePtr::BotPTR) {
827 return TypeInt::CC_GT;
828 }
829 }
830 }
831 }
833 // Known constants can be compared exactly
834 // Null can be distinguished from any NotNull pointers
835 // Unknown inputs makes an unknown result
836 if( r0->singleton() ) {
837 intptr_t bits0 = r0->get_con();
838 if( r1->singleton() )
839 return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT;
840 return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC;
841 } else if( r1->singleton() ) {
842 intptr_t bits1 = r1->get_con();
843 return ( r0->_ptr == TypePtr::NotNull && bits1==0 ) ? TypeInt::CC_GT : TypeInt::CC;
844 } else
845 return TypeInt::CC;
846 }
848 static inline Node* isa_java_mirror_load(PhaseGVN* phase, Node* n) {
849 // Return the klass node for
850 // LoadP(AddP(foo:Klass, #java_mirror))
851 // or NULL if not matching.
852 if (n->Opcode() != Op_LoadP) return NULL;
854 const TypeInstPtr* tp = phase->type(n)->isa_instptr();
855 if (!tp || tp->klass() != phase->C->env()->Class_klass()) return NULL;
857 Node* adr = n->in(MemNode::Address);
858 intptr_t off = 0;
859 Node* k = AddPNode::Ideal_base_and_offset(adr, phase, off);
860 if (k == NULL) return NULL;
861 const TypeKlassPtr* tkp = phase->type(k)->isa_klassptr();
862 if (!tkp || off != in_bytes(Klass::java_mirror_offset())) return NULL;
864 // We've found the klass node of a Java mirror load.
865 return k;
866 }
868 static inline Node* isa_const_java_mirror(PhaseGVN* phase, Node* n) {
869 // for ConP(Foo.class) return ConP(Foo.klass)
870 // otherwise return NULL
871 if (!n->is_Con()) return NULL;
873 const TypeInstPtr* tp = phase->type(n)->isa_instptr();
874 if (!tp) return NULL;
876 ciType* mirror_type = tp->java_mirror_type();
877 // TypeInstPtr::java_mirror_type() returns non-NULL for compile-
878 // time Class constants only.
879 if (!mirror_type) return NULL;
881 // x.getClass() == int.class can never be true (for all primitive types)
882 // Return a ConP(NULL) node for this case.
883 if (mirror_type->is_classless()) {
884 return phase->makecon(TypePtr::NULL_PTR);
885 }
887 // return the ConP(Foo.klass)
888 assert(mirror_type->is_klass(), "mirror_type should represent a Klass*");
889 return phase->makecon(TypeKlassPtr::make(mirror_type->as_klass()));
890 }
892 //------------------------------Ideal------------------------------------------
893 // Normalize comparisons between Java mirror loads to compare the klass instead.
894 //
895 // Also check for the case of comparing an unknown klass loaded from the primary
896 // super-type array vs a known klass with no subtypes. This amounts to
897 // checking to see an unknown klass subtypes a known klass with no subtypes;
898 // this only happens on an exact match. We can shorten this test by 1 load.
899 Node *CmpPNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
900 // Normalize comparisons between Java mirrors into comparisons of the low-
901 // level klass, where a dependent load could be shortened.
902 //
903 // The new pattern has a nice effect of matching the same pattern used in the
904 // fast path of instanceof/checkcast/Class.isInstance(), which allows
905 // redundant exact type check be optimized away by GVN.
906 // For example, in
907 // if (x.getClass() == Foo.class) {
908 // Foo foo = (Foo) x;
909 // // ... use a ...
910 // }
911 // a CmpPNode could be shared between if_acmpne and checkcast
912 {
913 Node* k1 = isa_java_mirror_load(phase, in(1));
914 Node* k2 = isa_java_mirror_load(phase, in(2));
915 Node* conk2 = isa_const_java_mirror(phase, in(2));
917 if (k1 && (k2 || conk2)) {
918 Node* lhs = k1;
919 Node* rhs = (k2 != NULL) ? k2 : conk2;
920 this->set_req(1, lhs);
921 this->set_req(2, rhs);
922 return this;
923 }
924 }
926 // Constant pointer on right?
927 const TypeKlassPtr* t2 = phase->type(in(2))->isa_klassptr();
928 if (t2 == NULL || !t2->klass_is_exact())
929 return NULL;
930 // Get the constant klass we are comparing to.
931 ciKlass* superklass = t2->klass();
933 // Now check for LoadKlass on left.
934 Node* ldk1 = in(1);
935 if (ldk1->is_DecodeNKlass()) {
936 ldk1 = ldk1->in(1);
937 if (ldk1->Opcode() != Op_LoadNKlass )
938 return NULL;
939 } else if (ldk1->Opcode() != Op_LoadKlass )
940 return NULL;
941 // Take apart the address of the LoadKlass:
942 Node* adr1 = ldk1->in(MemNode::Address);
943 intptr_t con2 = 0;
944 Node* ldk2 = AddPNode::Ideal_base_and_offset(adr1, phase, con2);
945 if (ldk2 == NULL)
946 return NULL;
947 if (con2 == oopDesc::klass_offset_in_bytes()) {
948 // We are inspecting an object's concrete class.
949 // Short-circuit the check if the query is abstract.
950 if (superklass->is_interface() ||
951 superklass->is_abstract()) {
952 // Make it come out always false:
953 this->set_req(2, phase->makecon(TypePtr::NULL_PTR));
954 return this;
955 }
956 }
958 // Check for a LoadKlass from primary supertype array.
959 // Any nested loadklass from loadklass+con must be from the p.s. array.
960 if (ldk2->is_DecodeNKlass()) {
961 // Keep ldk2 as DecodeN since it could be used in CmpP below.
962 if (ldk2->in(1)->Opcode() != Op_LoadNKlass )
963 return NULL;
964 } else if (ldk2->Opcode() != Op_LoadKlass)
965 return NULL;
967 // Verify that we understand the situation
968 if (con2 != (intptr_t) superklass->super_check_offset())
969 return NULL; // Might be element-klass loading from array klass
971 // If 'superklass' has no subklasses and is not an interface, then we are
972 // assured that the only input which will pass the type check is
973 // 'superklass' itself.
974 //
975 // We could be more liberal here, and allow the optimization on interfaces
976 // which have a single implementor. This would require us to increase the
977 // expressiveness of the add_dependency() mechanism.
978 // %%% Do this after we fix TypeOopPtr: Deps are expressive enough now.
980 // Object arrays must have their base element have no subtypes
981 while (superklass->is_obj_array_klass()) {
982 ciType* elem = superklass->as_obj_array_klass()->element_type();
983 superklass = elem->as_klass();
984 }
985 if (superklass->is_instance_klass()) {
986 ciInstanceKlass* ik = superklass->as_instance_klass();
987 if (ik->has_subklass() || ik->is_interface()) return NULL;
988 // Add a dependency if there is a chance that a subclass will be added later.
989 if (!ik->is_final()) {
990 phase->C->dependencies()->assert_leaf_type(ik);
991 }
992 }
994 // Bypass the dependent load, and compare directly
995 this->set_req(1,ldk2);
997 return this;
998 }
1000 //=============================================================================
1001 //------------------------------sub--------------------------------------------
1002 // Simplify an CmpN (compare 2 pointers) node, based on local information.
1003 // If both inputs are constants, compare them.
1004 const Type *CmpNNode::sub( const Type *t1, const Type *t2 ) const {
1005 const TypePtr *r0 = t1->make_ptr(); // Handy access
1006 const TypePtr *r1 = t2->make_ptr();
1008 // Undefined inputs makes for an undefined result
1009 if ((r0 == NULL) || (r1 == NULL) ||
1010 TypePtr::above_centerline(r0->_ptr) ||
1011 TypePtr::above_centerline(r1->_ptr)) {
1012 return Type::TOP;
1013 }
1014 if (r0 == r1 && r0->singleton()) {
1015 // Equal pointer constants (klasses, nulls, etc.)
1016 return TypeInt::CC_EQ;
1017 }
1019 // See if it is 2 unrelated classes.
1020 const TypeOopPtr* p0 = r0->isa_oopptr();
1021 const TypeOopPtr* p1 = r1->isa_oopptr();
1022 if (p0 && p1) {
1023 ciKlass* klass0 = p0->klass();
1024 bool xklass0 = p0->klass_is_exact();
1025 ciKlass* klass1 = p1->klass();
1026 bool xklass1 = p1->klass_is_exact();
1027 int kps = (p0->isa_klassptr()?1:0) + (p1->isa_klassptr()?1:0);
1028 if (klass0 && klass1 &&
1029 kps != 1 && // both or neither are klass pointers
1030 !klass0->is_interface() && // do not trust interfaces
1031 !klass1->is_interface()) {
1032 bool unrelated_classes = false;
1033 // See if neither subclasses the other, or if the class on top
1034 // is precise. In either of these cases, the compare is known
1035 // to fail if at least one of the pointers is provably not null.
1036 if (klass0->equals(klass1)) { // if types are unequal but klasses are equal
1037 // Do nothing; we know nothing for imprecise types
1038 } else if (klass0->is_subtype_of(klass1)) {
1039 // If klass1's type is PRECISE, then classes are unrelated.
1040 unrelated_classes = xklass1;
1041 } else if (klass1->is_subtype_of(klass0)) {
1042 // If klass0's type is PRECISE, then classes are unrelated.
1043 unrelated_classes = xklass0;
1044 } else { // Neither subtypes the other
1045 unrelated_classes = true;
1046 }
1047 if (unrelated_classes) {
1048 // The oops classes are known to be unrelated. If the joined PTRs of
1049 // two oops is not Null and not Bottom, then we are sure that one
1050 // of the two oops is non-null, and the comparison will always fail.
1051 TypePtr::PTR jp = r0->join_ptr(r1->_ptr);
1052 if (jp != TypePtr::Null && jp != TypePtr::BotPTR) {
1053 return TypeInt::CC_GT;
1054 }
1055 }
1056 }
1057 }
1059 // Known constants can be compared exactly
1060 // Null can be distinguished from any NotNull pointers
1061 // Unknown inputs makes an unknown result
1062 if( r0->singleton() ) {
1063 intptr_t bits0 = r0->get_con();
1064 if( r1->singleton() )
1065 return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT;
1066 return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC;
1067 } else if( r1->singleton() ) {
1068 intptr_t bits1 = r1->get_con();
1069 return ( r0->_ptr == TypePtr::NotNull && bits1==0 ) ? TypeInt::CC_GT : TypeInt::CC;
1070 } else
1071 return TypeInt::CC;
1072 }
1074 //------------------------------Ideal------------------------------------------
1075 Node *CmpNNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
1076 return NULL;
1077 }
1079 //=============================================================================
1080 //------------------------------Value------------------------------------------
1081 // Simplify an CmpF (compare 2 floats ) node, based on local information.
1082 // If both inputs are constants, compare them.
1083 const Type *CmpFNode::Value( PhaseTransform *phase ) const {
1084 const Node* in1 = in(1);
1085 const Node* in2 = in(2);
1086 // Either input is TOP ==> the result is TOP
1087 const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
1088 if( t1 == Type::TOP ) return Type::TOP;
1089 const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
1090 if( t2 == Type::TOP ) return Type::TOP;
1092 // Not constants? Don't know squat - even if they are the same
1093 // value! If they are NaN's they compare to LT instead of EQ.
1094 const TypeF *tf1 = t1->isa_float_constant();
1095 const TypeF *tf2 = t2->isa_float_constant();
1096 if( !tf1 || !tf2 ) return TypeInt::CC;
1098 // This implements the Java bytecode fcmpl, so unordered returns -1.
1099 if( tf1->is_nan() || tf2->is_nan() )
1100 return TypeInt::CC_LT;
1102 if( tf1->_f < tf2->_f ) return TypeInt::CC_LT;
1103 if( tf1->_f > tf2->_f ) return TypeInt::CC_GT;
1104 assert( tf1->_f == tf2->_f, "do not understand FP behavior" );
1105 return TypeInt::CC_EQ;
1106 }
1109 //=============================================================================
1110 //------------------------------Value------------------------------------------
1111 // Simplify an CmpD (compare 2 doubles ) node, based on local information.
1112 // If both inputs are constants, compare them.
1113 const Type *CmpDNode::Value( PhaseTransform *phase ) const {
1114 const Node* in1 = in(1);
1115 const Node* in2 = in(2);
1116 // Either input is TOP ==> the result is TOP
1117 const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
1118 if( t1 == Type::TOP ) return Type::TOP;
1119 const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
1120 if( t2 == Type::TOP ) return Type::TOP;
1122 // Not constants? Don't know squat - even if they are the same
1123 // value! If they are NaN's they compare to LT instead of EQ.
1124 const TypeD *td1 = t1->isa_double_constant();
1125 const TypeD *td2 = t2->isa_double_constant();
1126 if( !td1 || !td2 ) return TypeInt::CC;
1128 // This implements the Java bytecode dcmpl, so unordered returns -1.
1129 if( td1->is_nan() || td2->is_nan() )
1130 return TypeInt::CC_LT;
1132 if( td1->_d < td2->_d ) return TypeInt::CC_LT;
1133 if( td1->_d > td2->_d ) return TypeInt::CC_GT;
1134 assert( td1->_d == td2->_d, "do not understand FP behavior" );
1135 return TypeInt::CC_EQ;
1136 }
1138 //------------------------------Ideal------------------------------------------
1139 Node *CmpDNode::Ideal(PhaseGVN *phase, bool can_reshape){
1140 // Check if we can change this to a CmpF and remove a ConvD2F operation.
1141 // Change (CMPD (F2D (float)) (ConD value))
1142 // To (CMPF (float) (ConF value))
1143 // Valid when 'value' does not lose precision as a float.
1144 // Benefits: eliminates conversion, does not require 24-bit mode
1146 // NaNs prevent commuting operands. This transform works regardless of the
1147 // order of ConD and ConvF2D inputs by preserving the original order.
1148 int idx_f2d = 1; // ConvF2D on left side?
1149 if( in(idx_f2d)->Opcode() != Op_ConvF2D )
1150 idx_f2d = 2; // No, swap to check for reversed args
1151 int idx_con = 3-idx_f2d; // Check for the constant on other input
1153 if( ConvertCmpD2CmpF &&
1154 in(idx_f2d)->Opcode() == Op_ConvF2D &&
1155 in(idx_con)->Opcode() == Op_ConD ) {
1156 const TypeD *t2 = in(idx_con)->bottom_type()->is_double_constant();
1157 double t2_value_as_double = t2->_d;
1158 float t2_value_as_float = (float)t2_value_as_double;
1159 if( t2_value_as_double == (double)t2_value_as_float ) {
1160 // Test value can be represented as a float
1161 // Eliminate the conversion to double and create new comparison
1162 Node *new_in1 = in(idx_f2d)->in(1);
1163 Node *new_in2 = phase->makecon( TypeF::make(t2_value_as_float) );
1164 if( idx_f2d != 1 ) { // Must flip args to match original order
1165 Node *tmp = new_in1;
1166 new_in1 = new_in2;
1167 new_in2 = tmp;
1168 }
1169 CmpFNode *new_cmp = (Opcode() == Op_CmpD3)
1170 ? new (phase->C) CmpF3Node( new_in1, new_in2 )
1171 : new (phase->C) CmpFNode ( new_in1, new_in2 ) ;
1172 return new_cmp; // Changed to CmpFNode
1173 }
1174 // Testing value required the precision of a double
1175 }
1176 return NULL; // No change
1177 }
1180 //=============================================================================
1181 //------------------------------cc2logical-------------------------------------
1182 // Convert a condition code type to a logical type
1183 const Type *BoolTest::cc2logical( const Type *CC ) const {
1184 if( CC == Type::TOP ) return Type::TOP;
1185 if( CC->base() != Type::Int ) return TypeInt::BOOL; // Bottom or worse
1186 const TypeInt *ti = CC->is_int();
1187 if( ti->is_con() ) { // Only 1 kind of condition codes set?
1188 // Match low order 2 bits
1189 int tmp = ((ti->get_con()&3) == (_test&3)) ? 1 : 0;
1190 if( _test & 4 ) tmp = 1-tmp; // Optionally complement result
1191 return TypeInt::make(tmp); // Boolean result
1192 }
1194 if( CC == TypeInt::CC_GE ) {
1195 if( _test == ge ) return TypeInt::ONE;
1196 if( _test == lt ) return TypeInt::ZERO;
1197 }
1198 if( CC == TypeInt::CC_LE ) {
1199 if( _test == le ) return TypeInt::ONE;
1200 if( _test == gt ) return TypeInt::ZERO;
1201 }
1203 return TypeInt::BOOL;
1204 }
1206 //------------------------------dump_spec-------------------------------------
1207 // Print special per-node info
1208 void BoolTest::dump_on(outputStream *st) const {
1209 const char *msg[] = {"eq","gt","of","lt","ne","le","nof","ge"};
1210 st->print("%s", msg[_test]);
1211 }
1213 //=============================================================================
1214 uint BoolNode::hash() const { return (Node::hash() << 3)|(_test._test+1); }
1215 uint BoolNode::size_of() const { return sizeof(BoolNode); }
1217 //------------------------------operator==-------------------------------------
1218 uint BoolNode::cmp( const Node &n ) const {
1219 const BoolNode *b = (const BoolNode *)&n; // Cast up
1220 return (_test._test == b->_test._test);
1221 }
1223 //-------------------------------make_predicate--------------------------------
1224 Node* BoolNode::make_predicate(Node* test_value, PhaseGVN* phase) {
1225 if (test_value->is_Con()) return test_value;
1226 if (test_value->is_Bool()) return test_value;
1227 Compile* C = phase->C;
1228 if (test_value->is_CMove() &&
1229 test_value->in(CMoveNode::Condition)->is_Bool()) {
1230 BoolNode* bol = test_value->in(CMoveNode::Condition)->as_Bool();
1231 const Type* ftype = phase->type(test_value->in(CMoveNode::IfFalse));
1232 const Type* ttype = phase->type(test_value->in(CMoveNode::IfTrue));
1233 if (ftype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ttype)) {
1234 return bol;
1235 } else if (ttype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ftype)) {
1236 return phase->transform( bol->negate(phase) );
1237 }
1238 // Else fall through. The CMove gets in the way of the test.
1239 // It should be the case that make_predicate(bol->as_int_value()) == bol.
1240 }
1241 Node* cmp = new (C) CmpINode(test_value, phase->intcon(0));
1242 cmp = phase->transform(cmp);
1243 Node* bol = new (C) BoolNode(cmp, BoolTest::ne);
1244 return phase->transform(bol);
1245 }
1247 //--------------------------------as_int_value---------------------------------
1248 Node* BoolNode::as_int_value(PhaseGVN* phase) {
1249 // Inverse to make_predicate. The CMove probably boils down to a Conv2B.
1250 Node* cmov = CMoveNode::make(phase->C, NULL, this,
1251 phase->intcon(0), phase->intcon(1),
1252 TypeInt::BOOL);
1253 return phase->transform(cmov);
1254 }
1256 //----------------------------------negate-------------------------------------
1257 BoolNode* BoolNode::negate(PhaseGVN* phase) {
1258 Compile* C = phase->C;
1259 return new (C) BoolNode(in(1), _test.negate());
1260 }
1263 //------------------------------Ideal------------------------------------------
1264 Node *BoolNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1265 // Change "bool tst (cmp con x)" into "bool ~tst (cmp x con)".
1266 // This moves the constant to the right. Helps value-numbering.
1267 Node *cmp = in(1);
1268 if( !cmp->is_Sub() ) return NULL;
1269 int cop = cmp->Opcode();
1270 if( cop == Op_FastLock || cop == Op_FastUnlock) return NULL;
1271 Node *cmp1 = cmp->in(1);
1272 Node *cmp2 = cmp->in(2);
1273 if( !cmp1 ) return NULL;
1275 if (_test._test == BoolTest::overflow || _test._test == BoolTest::no_overflow) {
1276 return NULL;
1277 }
1279 // Constant on left?
1280 Node *con = cmp1;
1281 uint op2 = cmp2->Opcode();
1282 // Move constants to the right of compare's to canonicalize.
1283 // Do not muck with Opaque1 nodes, as this indicates a loop
1284 // guard that cannot change shape.
1285 if( con->is_Con() && !cmp2->is_Con() && op2 != Op_Opaque1 &&
1286 // Because of NaN's, CmpD and CmpF are not commutative
1287 cop != Op_CmpD && cop != Op_CmpF &&
1288 // Protect against swapping inputs to a compare when it is used by a
1289 // counted loop exit, which requires maintaining the loop-limit as in(2)
1290 !is_counted_loop_exit_test() ) {
1291 // Ok, commute the constant to the right of the cmp node.
1292 // Clone the Node, getting a new Node of the same class
1293 cmp = cmp->clone();
1294 // Swap inputs to the clone
1295 cmp->swap_edges(1, 2);
1296 cmp = phase->transform( cmp );
1297 return new (phase->C) BoolNode( cmp, _test.commute() );
1298 }
1300 // Change "bool eq/ne (cmp (xor X 1) 0)" into "bool ne/eq (cmp X 0)".
1301 // The XOR-1 is an idiom used to flip the sense of a bool. We flip the
1302 // test instead.
1303 int cmp1_op = cmp1->Opcode();
1304 const TypeInt* cmp2_type = phase->type(cmp2)->isa_int();
1305 if (cmp2_type == NULL) return NULL;
1306 Node* j_xor = cmp1;
1307 if( cmp2_type == TypeInt::ZERO &&
1308 cmp1_op == Op_XorI &&
1309 j_xor->in(1) != j_xor && // An xor of itself is dead
1310 phase->type( j_xor->in(1) ) == TypeInt::BOOL &&
1311 phase->type( j_xor->in(2) ) == TypeInt::ONE &&
1312 (_test._test == BoolTest::eq ||
1313 _test._test == BoolTest::ne) ) {
1314 Node *ncmp = phase->transform(new (phase->C) CmpINode(j_xor->in(1),cmp2));
1315 return new (phase->C) BoolNode( ncmp, _test.negate() );
1316 }
1318 // Change "bool eq/ne (cmp (Conv2B X) 0)" into "bool eq/ne (cmp X 0)".
1319 // This is a standard idiom for branching on a boolean value.
1320 Node *c2b = cmp1;
1321 if( cmp2_type == TypeInt::ZERO &&
1322 cmp1_op == Op_Conv2B &&
1323 (_test._test == BoolTest::eq ||
1324 _test._test == BoolTest::ne) ) {
1325 Node *ncmp = phase->transform(phase->type(c2b->in(1))->isa_int()
1326 ? (Node*)new (phase->C) CmpINode(c2b->in(1),cmp2)
1327 : (Node*)new (phase->C) CmpPNode(c2b->in(1),phase->makecon(TypePtr::NULL_PTR))
1328 );
1329 return new (phase->C) BoolNode( ncmp, _test._test );
1330 }
1332 // Comparing a SubI against a zero is equal to comparing the SubI
1333 // arguments directly. This only works for eq and ne comparisons
1334 // due to possible integer overflow.
1335 if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1336 (cop == Op_CmpI) &&
1337 (cmp1->Opcode() == Op_SubI) &&
1338 ( cmp2_type == TypeInt::ZERO ) ) {
1339 Node *ncmp = phase->transform( new (phase->C) CmpINode(cmp1->in(1),cmp1->in(2)));
1340 return new (phase->C) BoolNode( ncmp, _test._test );
1341 }
1343 // Change (-A vs 0) into (A vs 0) by commuting the test. Disallow in the
1344 // most general case because negating 0x80000000 does nothing. Needed for
1345 // the CmpF3/SubI/CmpI idiom.
1346 if( cop == Op_CmpI &&
1347 cmp1->Opcode() == Op_SubI &&
1348 cmp2_type == TypeInt::ZERO &&
1349 phase->type( cmp1->in(1) ) == TypeInt::ZERO &&
1350 phase->type( cmp1->in(2) )->higher_equal(TypeInt::SYMINT) ) {
1351 Node *ncmp = phase->transform( new (phase->C) CmpINode(cmp1->in(2),cmp2));
1352 return new (phase->C) BoolNode( ncmp, _test.commute() );
1353 }
1355 // The transformation below is not valid for either signed or unsigned
1356 // comparisons due to wraparound concerns at MAX_VALUE and MIN_VALUE.
1357 // This transformation can be resurrected when we are able to
1358 // make inferences about the range of values being subtracted from
1359 // (or added to) relative to the wraparound point.
1360 //
1361 // // Remove +/-1's if possible.
1362 // // "X <= Y-1" becomes "X < Y"
1363 // // "X+1 <= Y" becomes "X < Y"
1364 // // "X < Y+1" becomes "X <= Y"
1365 // // "X-1 < Y" becomes "X <= Y"
1366 // // Do not this to compares off of the counted-loop-end. These guys are
1367 // // checking the trip counter and they want to use the post-incremented
1368 // // counter. If they use the PRE-incremented counter, then the counter has
1369 // // to be incremented in a private block on a loop backedge.
1370 // if( du && du->cnt(this) && du->out(this)[0]->Opcode() == Op_CountedLoopEnd )
1371 // return NULL;
1372 // #ifndef PRODUCT
1373 // // Do not do this in a wash GVN pass during verification.
1374 // // Gets triggered by too many simple optimizations to be bothered with
1375 // // re-trying it again and again.
1376 // if( !phase->allow_progress() ) return NULL;
1377 // #endif
1378 // // Not valid for unsigned compare because of corner cases in involving zero.
1379 // // For example, replacing "X-1 <u Y" with "X <=u Y" fails to throw an
1380 // // exception in case X is 0 (because 0-1 turns into 4billion unsigned but
1381 // // "0 <=u Y" is always true).
1382 // if( cmp->Opcode() == Op_CmpU ) return NULL;
1383 // int cmp2_op = cmp2->Opcode();
1384 // if( _test._test == BoolTest::le ) {
1385 // if( cmp1_op == Op_AddI &&
1386 // phase->type( cmp1->in(2) ) == TypeInt::ONE )
1387 // return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::lt );
1388 // else if( cmp2_op == Op_AddI &&
1389 // phase->type( cmp2->in(2) ) == TypeInt::MINUS_1 )
1390 // return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::lt );
1391 // } else if( _test._test == BoolTest::lt ) {
1392 // if( cmp1_op == Op_AddI &&
1393 // phase->type( cmp1->in(2) ) == TypeInt::MINUS_1 )
1394 // return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::le );
1395 // else if( cmp2_op == Op_AddI &&
1396 // phase->type( cmp2->in(2) ) == TypeInt::ONE )
1397 // return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::le );
1398 // }
1400 return NULL;
1401 }
1403 //------------------------------Value------------------------------------------
1404 // Simplify a Bool (convert condition codes to boolean (1 or 0)) node,
1405 // based on local information. If the input is constant, do it.
1406 const Type *BoolNode::Value( PhaseTransform *phase ) const {
1407 return _test.cc2logical( phase->type( in(1) ) );
1408 }
1410 //------------------------------dump_spec--------------------------------------
1411 // Dump special per-node info
1412 #ifndef PRODUCT
1413 void BoolNode::dump_spec(outputStream *st) const {
1414 st->print("[");
1415 _test.dump_on(st);
1416 st->print("]");
1417 }
1418 #endif
1420 //------------------------------is_counted_loop_exit_test--------------------------------------
1421 // Returns true if node is used by a counted loop node.
1422 bool BoolNode::is_counted_loop_exit_test() {
1423 for( DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++ ) {
1424 Node* use = fast_out(i);
1425 if (use->is_CountedLoopEnd()) {
1426 return true;
1427 }
1428 }
1429 return false;
1430 }
1432 //=============================================================================
1433 //------------------------------Value------------------------------------------
1434 // Compute sqrt
1435 const Type *SqrtDNode::Value( PhaseTransform *phase ) const {
1436 const Type *t1 = phase->type( in(1) );
1437 if( t1 == Type::TOP ) return Type::TOP;
1438 if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1439 double d = t1->getd();
1440 if( d < 0.0 ) return Type::DOUBLE;
1441 return TypeD::make( sqrt( d ) );
1442 }
1444 //=============================================================================
1445 //------------------------------Value------------------------------------------
1446 // Compute cos
1447 const Type *CosDNode::Value( PhaseTransform *phase ) const {
1448 const Type *t1 = phase->type( in(1) );
1449 if( t1 == Type::TOP ) return Type::TOP;
1450 if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1451 double d = t1->getd();
1452 return TypeD::make( StubRoutines::intrinsic_cos( d ) );
1453 }
1455 //=============================================================================
1456 //------------------------------Value------------------------------------------
1457 // Compute sin
1458 const Type *SinDNode::Value( PhaseTransform *phase ) const {
1459 const Type *t1 = phase->type( in(1) );
1460 if( t1 == Type::TOP ) return Type::TOP;
1461 if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1462 double d = t1->getd();
1463 return TypeD::make( StubRoutines::intrinsic_sin( d ) );
1464 }
1466 //=============================================================================
1467 //------------------------------Value------------------------------------------
1468 // Compute tan
1469 const Type *TanDNode::Value( PhaseTransform *phase ) const {
1470 const Type *t1 = phase->type( in(1) );
1471 if( t1 == Type::TOP ) return Type::TOP;
1472 if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1473 double d = t1->getd();
1474 return TypeD::make( StubRoutines::intrinsic_tan( d ) );
1475 }
1477 //=============================================================================
1478 //------------------------------Value------------------------------------------
1479 // Compute log
1480 const Type *LogDNode::Value( PhaseTransform *phase ) const {
1481 const Type *t1 = phase->type( in(1) );
1482 if( t1 == Type::TOP ) return Type::TOP;
1483 if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1484 double d = t1->getd();
1485 return TypeD::make( StubRoutines::intrinsic_log( d ) );
1486 }
1488 //=============================================================================
1489 //------------------------------Value------------------------------------------
1490 // Compute log10
1491 const Type *Log10DNode::Value( PhaseTransform *phase ) const {
1492 const Type *t1 = phase->type( in(1) );
1493 if( t1 == Type::TOP ) return Type::TOP;
1494 if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1495 double d = t1->getd();
1496 return TypeD::make( StubRoutines::intrinsic_log10( d ) );
1497 }
1499 //=============================================================================
1500 //------------------------------Value------------------------------------------
1501 // Compute exp
1502 const Type *ExpDNode::Value( PhaseTransform *phase ) const {
1503 const Type *t1 = phase->type( in(1) );
1504 if( t1 == Type::TOP ) return Type::TOP;
1505 if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1506 double d = t1->getd();
1507 return TypeD::make( StubRoutines::intrinsic_exp( d ) );
1508 }
1511 //=============================================================================
1512 //------------------------------Value------------------------------------------
1513 // Compute pow
1514 const Type *PowDNode::Value( PhaseTransform *phase ) const {
1515 const Type *t1 = phase->type( in(1) );
1516 if( t1 == Type::TOP ) return Type::TOP;
1517 if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1518 const Type *t2 = phase->type( in(2) );
1519 if( t2 == Type::TOP ) return Type::TOP;
1520 if( t2->base() != Type::DoubleCon ) return Type::DOUBLE;
1521 double d1 = t1->getd();
1522 double d2 = t2->getd();
1523 return TypeD::make( StubRoutines::intrinsic_pow( d1, d2 ) );
1524 }