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