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