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