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