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