1 /* 2 * Copyright (c) 2014, 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 "opto/addnode.hpp" 27 #include "opto/castnode.hpp" 28 #include "opto/convertnode.hpp" 29 #include "opto/inlinetypenode.hpp" 30 #include "opto/matcher.hpp" 31 #include "opto/phaseX.hpp" 32 #include "opto/subnode.hpp" 33 #include "runtime/stubRoutines.hpp" 34 35 //============================================================================= 36 //------------------------------Identity--------------------------------------- 37 Node* Conv2BNode::Identity(PhaseGVN* phase) { 38 const Type *t = phase->type( in(1) ); 39 if( t == Type::TOP ) return in(1); 40 if( t == TypeInt::ZERO ) return in(1); 41 if( t == TypeInt::ONE ) return in(1); 42 if( t == TypeInt::BOOL ) return in(1); 43 return this; 44 } 45 46 //------------------------------Value------------------------------------------ 47 const Type* Conv2BNode::Value(PhaseGVN* phase) const { 48 const Type *t = phase->type( in(1) ); 49 if( t == Type::TOP ) return Type::TOP; 50 if( t == TypeInt::ZERO ) return TypeInt::ZERO; 51 if( t == TypePtr::NULL_PTR ) return TypeInt::ZERO; 52 const TypePtr *tp = t->isa_ptr(); 53 if(tp != nullptr) { 54 if( tp->ptr() == TypePtr::AnyNull ) return Type::TOP; 55 if( tp->ptr() == TypePtr::Constant) return TypeInt::ONE; 56 if (tp->ptr() == TypePtr::NotNull) return TypeInt::ONE; 57 return TypeInt::BOOL; 58 } 59 if (t->base() != Type::Int) return TypeInt::BOOL; 60 const TypeInt *ti = t->is_int(); 61 if( ti->_hi < 0 || ti->_lo > 0 ) return TypeInt::ONE; 62 return TypeInt::BOOL; 63 } 64 65 //------------------------------Ideal------------------------------------------ 66 Node* Conv2BNode::Ideal(PhaseGVN* phase, bool can_reshape) { 67 if (in(1)->is_InlineType()) { 68 // Null checking a scalarized but nullable inline type. Check the IsInit 69 // input instead of the oop input to avoid keeping buffer allocations alive. 70 set_req_X(1, in(1)->as_InlineType()->get_is_init(), phase); 71 return this; 72 } 73 return nullptr; 74 } 75 76 // The conversions operations are all Alpha sorted. Please keep it that way! 77 //============================================================================= 78 //------------------------------Value------------------------------------------ 79 const Type* ConvD2FNode::Value(PhaseGVN* phase) const { 80 const Type *t = phase->type( in(1) ); 81 if( t == Type::TOP ) return Type::TOP; 82 if( t == Type::DOUBLE ) return Type::FLOAT; 83 const TypeD *td = t->is_double_constant(); 84 return TypeF::make( (float)td->getd() ); 85 } 86 87 //------------------------------Ideal------------------------------------------ 88 // If we see pattern ConvF2D SomeDoubleOp ConvD2F, do operation as float. 89 Node *ConvD2FNode::Ideal(PhaseGVN *phase, bool can_reshape) { 90 if ( in(1)->Opcode() == Op_SqrtD ) { 91 Node* sqrtd = in(1); 92 if ( sqrtd->in(1)->Opcode() == Op_ConvF2D ) { 93 if ( Matcher::match_rule_supported(Op_SqrtF) ) { 94 Node* convf2d = sqrtd->in(1); 95 return new SqrtFNode(phase->C, sqrtd->in(0), convf2d->in(1)); 96 } 97 } 98 } 99 return nullptr; 100 } 101 102 //------------------------------Identity--------------------------------------- 103 // Float's can be converted to doubles with no loss of bits. Hence 104 // converting a float to a double and back to a float is a NOP. 105 Node* ConvD2FNode::Identity(PhaseGVN* phase) { 106 return (in(1)->Opcode() == Op_ConvF2D) ? in(1)->in(1) : this; 107 } 108 109 //============================================================================= 110 //------------------------------Value------------------------------------------ 111 const Type* ConvD2INode::Value(PhaseGVN* phase) const { 112 const Type *t = phase->type( in(1) ); 113 if( t == Type::TOP ) return Type::TOP; 114 if( t == Type::DOUBLE ) return TypeInt::INT; 115 const TypeD *td = t->is_double_constant(); 116 return TypeInt::make( SharedRuntime::d2i( td->getd() ) ); 117 } 118 119 //------------------------------Ideal------------------------------------------ 120 // If converting to an int type, skip any rounding nodes 121 Node *ConvD2INode::Ideal(PhaseGVN *phase, bool can_reshape) { 122 if (in(1)->Opcode() == Op_RoundDouble) { 123 set_req(1, in(1)->in(1)); 124 return this; 125 } 126 return nullptr; 127 } 128 129 //------------------------------Identity--------------------------------------- 130 // Int's can be converted to doubles with no loss of bits. Hence 131 // converting an integer to a double and back to an integer is a NOP. 132 Node* ConvD2INode::Identity(PhaseGVN* phase) { 133 return (in(1)->Opcode() == Op_ConvI2D) ? in(1)->in(1) : this; 134 } 135 136 //============================================================================= 137 //------------------------------Value------------------------------------------ 138 const Type* ConvD2LNode::Value(PhaseGVN* phase) const { 139 const Type *t = phase->type( in(1) ); 140 if( t == Type::TOP ) return Type::TOP; 141 if( t == Type::DOUBLE ) return TypeLong::LONG; 142 const TypeD *td = t->is_double_constant(); 143 return TypeLong::make( SharedRuntime::d2l( td->getd() ) ); 144 } 145 146 //------------------------------Identity--------------------------------------- 147 Node* ConvD2LNode::Identity(PhaseGVN* phase) { 148 // Remove ConvD2L->ConvL2D->ConvD2L sequences. 149 if( in(1) ->Opcode() == Op_ConvL2D && 150 in(1)->in(1)->Opcode() == Op_ConvD2L ) 151 return in(1)->in(1); 152 return this; 153 } 154 155 //------------------------------Ideal------------------------------------------ 156 // If converting to an int type, skip any rounding nodes 157 Node *ConvD2LNode::Ideal(PhaseGVN *phase, bool can_reshape) { 158 if (in(1)->Opcode() == Op_RoundDouble) { 159 set_req(1, in(1)->in(1)); 160 return this; 161 } 162 return nullptr; 163 } 164 165 //============================================================================= 166 //------------------------------Value------------------------------------------ 167 const Type* ConvF2DNode::Value(PhaseGVN* phase) const { 168 const Type *t = phase->type( in(1) ); 169 if( t == Type::TOP ) return Type::TOP; 170 if( t == Type::FLOAT ) return Type::DOUBLE; 171 const TypeF *tf = t->is_float_constant(); 172 return TypeD::make( (double)tf->getf() ); 173 } 174 175 //============================================================================= 176 //------------------------------Value------------------------------------------ 177 const Type* ConvF2HFNode::Value(PhaseGVN* phase) const { 178 const Type *t = phase->type( in(1) ); 179 if (t == Type::TOP) return Type::TOP; 180 if (t == Type::FLOAT) return TypeInt::SHORT; 181 if (StubRoutines::f2hf_adr() == nullptr) return bottom_type(); 182 183 const TypeF *tf = t->is_float_constant(); 184 return TypeInt::make( StubRoutines::f2hf(tf->getf()) ); 185 } 186 187 //============================================================================= 188 //------------------------------Value------------------------------------------ 189 const Type* ConvF2INode::Value(PhaseGVN* phase) const { 190 const Type *t = phase->type( in(1) ); 191 if( t == Type::TOP ) return Type::TOP; 192 if( t == Type::FLOAT ) return TypeInt::INT; 193 const TypeF *tf = t->is_float_constant(); 194 return TypeInt::make( SharedRuntime::f2i( tf->getf() ) ); 195 } 196 197 //------------------------------Identity--------------------------------------- 198 Node* ConvF2INode::Identity(PhaseGVN* phase) { 199 // Remove ConvF2I->ConvI2F->ConvF2I sequences. 200 if( in(1) ->Opcode() == Op_ConvI2F && 201 in(1)->in(1)->Opcode() == Op_ConvF2I ) 202 return in(1)->in(1); 203 return this; 204 } 205 206 //------------------------------Ideal------------------------------------------ 207 // If converting to an int type, skip any rounding nodes 208 Node *ConvF2INode::Ideal(PhaseGVN *phase, bool can_reshape) { 209 if (in(1)->Opcode() == Op_RoundFloat) { 210 set_req(1, in(1)->in(1)); 211 return this; 212 } 213 return nullptr; 214 } 215 216 //============================================================================= 217 //------------------------------Value------------------------------------------ 218 const Type* ConvF2LNode::Value(PhaseGVN* phase) const { 219 const Type *t = phase->type( in(1) ); 220 if( t == Type::TOP ) return Type::TOP; 221 if( t == Type::FLOAT ) return TypeLong::LONG; 222 const TypeF *tf = t->is_float_constant(); 223 return TypeLong::make( SharedRuntime::f2l( tf->getf() ) ); 224 } 225 226 //------------------------------Identity--------------------------------------- 227 Node* ConvF2LNode::Identity(PhaseGVN* phase) { 228 // Remove ConvF2L->ConvL2F->ConvF2L sequences. 229 if( in(1) ->Opcode() == Op_ConvL2F && 230 in(1)->in(1)->Opcode() == Op_ConvF2L ) 231 return in(1)->in(1); 232 return this; 233 } 234 235 //------------------------------Ideal------------------------------------------ 236 // If converting to an int type, skip any rounding nodes 237 Node *ConvF2LNode::Ideal(PhaseGVN *phase, bool can_reshape) { 238 if (in(1)->Opcode() == Op_RoundFloat) { 239 set_req(1, in(1)->in(1)); 240 return this; 241 } 242 return nullptr; 243 } 244 245 //============================================================================= 246 //------------------------------Value------------------------------------------ 247 const Type* ConvHF2FNode::Value(PhaseGVN* phase) const { 248 const Type *t = phase->type( in(1) ); 249 if (t == Type::TOP) return Type::TOP; 250 if (t == TypeInt::SHORT) return Type::FLOAT; 251 if (StubRoutines::hf2f_adr() == nullptr) return bottom_type(); 252 253 const TypeInt *ti = t->is_int(); 254 if (ti->is_con()) { 255 return TypeF::make( StubRoutines::hf2f(ti->get_con()) ); 256 } 257 return bottom_type(); 258 } 259 260 //============================================================================= 261 //------------------------------Value------------------------------------------ 262 const Type* ConvI2DNode::Value(PhaseGVN* phase) const { 263 const Type *t = phase->type( in(1) ); 264 if( t == Type::TOP ) return Type::TOP; 265 const TypeInt *ti = t->is_int(); 266 if( ti->is_con() ) return TypeD::make( (double)ti->get_con() ); 267 return bottom_type(); 268 } 269 270 //============================================================================= 271 //------------------------------Value------------------------------------------ 272 const Type* ConvI2FNode::Value(PhaseGVN* phase) const { 273 const Type *t = phase->type( in(1) ); 274 if( t == Type::TOP ) return Type::TOP; 275 const TypeInt *ti = t->is_int(); 276 if( ti->is_con() ) return TypeF::make( (float)ti->get_con() ); 277 return bottom_type(); 278 } 279 280 //------------------------------Identity--------------------------------------- 281 Node* ConvI2FNode::Identity(PhaseGVN* phase) { 282 // Remove ConvI2F->ConvF2I->ConvI2F sequences. 283 if( in(1) ->Opcode() == Op_ConvF2I && 284 in(1)->in(1)->Opcode() == Op_ConvI2F ) 285 return in(1)->in(1); 286 return this; 287 } 288 289 //============================================================================= 290 //------------------------------Value------------------------------------------ 291 const Type* ConvI2LNode::Value(PhaseGVN* phase) const { 292 const Type *t = phase->type( in(1) ); 293 if (t == Type::TOP) { 294 return Type::TOP; 295 } 296 const TypeInt *ti = t->is_int(); 297 const Type* tl = TypeLong::make(ti->_lo, ti->_hi, ti->_widen); 298 // Join my declared type against my incoming type. 299 tl = tl->filter(_type); 300 if (!tl->isa_long()) { 301 return tl; 302 } 303 const TypeLong* this_type = tl->is_long(); 304 // Do NOT remove this node's type assertion until no more loop ops can happen. 305 if (phase->C->post_loop_opts_phase()) { 306 const TypeInt* in_type = phase->type(in(1))->isa_int(); 307 if (in_type != nullptr && 308 (in_type->_lo != this_type->_lo || 309 in_type->_hi != this_type->_hi)) { 310 // Although this WORSENS the type, it increases GVN opportunities, 311 // because I2L nodes with the same input will common up, regardless 312 // of slightly differing type assertions. Such slight differences 313 // arise routinely as a result of loop unrolling, so this is a 314 // post-unrolling graph cleanup. Choose a type which depends only 315 // on my input. (Exception: Keep a range assertion of >=0 or <0.) 316 jlong lo1 = this_type->_lo; 317 jlong hi1 = this_type->_hi; 318 int w1 = this_type->_widen; 319 if (lo1 >= 0) { 320 // Keep a range assertion of >=0. 321 lo1 = 0; hi1 = max_jint; 322 } else if (hi1 < 0) { 323 // Keep a range assertion of <0. 324 lo1 = min_jint; hi1 = -1; 325 } else { 326 lo1 = min_jint; hi1 = max_jint; 327 } 328 return TypeLong::make(MAX2((jlong)in_type->_lo, lo1), 329 MIN2((jlong)in_type->_hi, hi1), 330 MAX2((int)in_type->_widen, w1)); 331 } 332 } 333 return this_type; 334 } 335 336 Node* ConvI2LNode::Identity(PhaseGVN* phase) { 337 // If type is in "int" sub-range, we can 338 // convert I2L(L2I(x)) => x 339 // since the conversions have no effect. 340 if (in(1)->Opcode() == Op_ConvL2I) { 341 Node* x = in(1)->in(1); 342 const TypeLong* t = phase->type(x)->isa_long(); 343 if (t != nullptr && t->_lo >= min_jint && t->_hi <= max_jint) { 344 return x; 345 } 346 } 347 return this; 348 } 349 350 #ifdef ASSERT 351 static inline bool long_ranges_overlap(jlong lo1, jlong hi1, 352 jlong lo2, jlong hi2) { 353 // Two ranges overlap iff one range's low point falls in the other range. 354 return (lo2 <= lo1 && lo1 <= hi2) || (lo1 <= lo2 && lo2 <= hi1); 355 } 356 #endif 357 358 template<class T> static bool subtract_overflows(T x, T y) { 359 T s = java_subtract(x, y); 360 return (x >= 0) && (y < 0) && (s < 0); 361 } 362 363 template<class T> static bool subtract_underflows(T x, T y) { 364 T s = java_subtract(x, y); 365 return (x < 0) && (y > 0) && (s > 0); 366 } 367 368 template<class T> static bool add_overflows(T x, T y) { 369 T s = java_add(x, y); 370 return (x > 0) && (y > 0) && (s < 0); 371 } 372 373 template<class T> static bool add_underflows(T x, T y) { 374 T s = java_add(x, y); 375 return (x < 0) && (y < 0) && (s >= 0); 376 } 377 378 template<class T> static bool ranges_overlap(T xlo, T ylo, T xhi, T yhi, T zlo, T zhi, 379 const Node* n, bool pos) { 380 assert(xlo <= xhi && ylo <= yhi && zlo <= zhi, "should not be empty types"); 381 T x_y_lo; 382 T x_y_hi; 383 bool x_y_lo_overflow; 384 bool x_y_hi_overflow; 385 386 if (n->is_Sub()) { 387 x_y_lo = java_subtract(xlo, yhi); 388 x_y_hi = java_subtract(xhi, ylo); 389 x_y_lo_overflow = pos ? subtract_overflows(xlo, yhi) : subtract_underflows(xlo, yhi); 390 x_y_hi_overflow = pos ? subtract_overflows(xhi, ylo) : subtract_underflows(xhi, ylo); 391 } else { 392 assert(n->is_Add(), "Add or Sub only"); 393 x_y_lo = java_add(xlo, ylo); 394 x_y_hi = java_add(xhi, yhi); 395 x_y_lo_overflow = pos ? add_overflows(xlo, ylo) : add_underflows(xlo, ylo); 396 x_y_hi_overflow = pos ? add_overflows(xhi, yhi) : add_underflows(xhi, yhi); 397 } 398 assert(!pos || !x_y_lo_overflow || x_y_hi_overflow, "x_y_lo_overflow => x_y_hi_overflow"); 399 assert(pos || !x_y_hi_overflow || x_y_lo_overflow, "x_y_hi_overflow => x_y_lo_overflow"); 400 401 // Two ranges overlap iff one range's low point falls in the other range. 402 // nbits = 32 or 64 403 if (pos) { 404 // (zlo + 2**nbits <= x_y_lo && x_y_lo <= zhi ** nbits) 405 if (x_y_lo_overflow) { 406 if (zlo <= x_y_lo && x_y_lo <= zhi) { 407 return true; 408 } 409 } 410 411 // (x_y_lo <= zlo + 2**nbits && zlo + 2**nbits <= x_y_hi) 412 if (x_y_hi_overflow) { 413 if ((!x_y_lo_overflow || x_y_lo <= zlo) && zlo <= x_y_hi) { 414 return true; 415 } 416 } 417 } else { 418 // (zlo - 2**nbits <= x_y_hi && x_y_hi <= zhi - 2**nbits) 419 if (x_y_hi_overflow) { 420 if (zlo <= x_y_hi && x_y_hi <= zhi) { 421 return true; 422 } 423 } 424 425 // (x_y_lo <= zhi - 2**nbits && zhi - 2**nbits <= x_y_hi) 426 if (x_y_lo_overflow) { 427 if (x_y_lo <= zhi && (!x_y_hi_overflow || zhi <= x_y_hi)) { 428 return true; 429 } 430 } 431 } 432 433 return false; 434 } 435 436 static bool ranges_overlap(const TypeInteger* tx, const TypeInteger* ty, const TypeInteger* tz, 437 const Node* n, bool pos, BasicType bt) { 438 jlong xlo = tx->lo_as_long(); 439 jlong xhi = tx->hi_as_long(); 440 jlong ylo = ty->lo_as_long(); 441 jlong yhi = ty->hi_as_long(); 442 jlong zlo = tz->lo_as_long(); 443 jlong zhi = tz->hi_as_long(); 444 445 if (bt == T_INT) { 446 // See if x+y can cause positive overflow into z+2**32 447 // See if x+y can cause negative overflow into z-2**32 448 bool res = ranges_overlap(checked_cast<jint>(xlo), checked_cast<jint>(ylo), 449 checked_cast<jint>(xhi), checked_cast<jint>(yhi), 450 checked_cast<jint>(zlo), checked_cast<jint>(zhi), n, pos); 451 #ifdef ASSERT 452 jlong vbit = CONST64(1) << BitsPerInt; 453 if (n->Opcode() == Op_SubI) { 454 jlong ylo0 = ylo; 455 ylo = -yhi; 456 yhi = -ylo0; 457 } 458 assert(res == long_ranges_overlap(xlo+ylo, xhi+yhi, pos ? zlo+vbit : zlo-vbit, pos ? zhi+vbit : zhi-vbit), "inconsistent result"); 459 #endif 460 return res; 461 } 462 assert(bt == T_LONG, "only int or long"); 463 // See if x+y can cause positive overflow into z+2**64 464 // See if x+y can cause negative overflow into z-2**64 465 return ranges_overlap(xlo, ylo, xhi, yhi, zlo, zhi, n, pos); 466 } 467 468 #ifdef ASSERT 469 static bool compute_updates_ranges_verif(const TypeInteger* tx, const TypeInteger* ty, const TypeInteger* tz, 470 jlong& rxlo, jlong& rxhi, jlong& rylo, jlong& ryhi, 471 const Node* n) { 472 jlong xlo = tx->lo_as_long(); 473 jlong xhi = tx->hi_as_long(); 474 jlong ylo = ty->lo_as_long(); 475 jlong yhi = ty->hi_as_long(); 476 jlong zlo = tz->lo_as_long(); 477 jlong zhi = tz->hi_as_long(); 478 if (n->is_Sub()) { 479 swap(ylo, yhi); 480 ylo = -ylo; 481 yhi = -yhi; 482 } 483 484 rxlo = MAX2(xlo, zlo - yhi); 485 rxhi = MIN2(xhi, zhi - ylo); 486 rylo = MAX2(ylo, zlo - xhi); 487 ryhi = MIN2(yhi, zhi - xlo); 488 if (rxlo > rxhi || rylo > ryhi) { 489 return false; 490 } 491 if (n->is_Sub()) { 492 swap(rylo, ryhi); 493 rylo = -rylo; 494 ryhi = -ryhi; 495 } 496 assert(rxlo == (int) rxlo && rxhi == (int) rxhi, "x should not overflow"); 497 assert(rylo == (int) rylo && ryhi == (int) ryhi, "y should not overflow"); 498 return true; 499 } 500 #endif 501 502 template<class T> static bool compute_updates_ranges(T xlo, T ylo, T xhi, T yhi, T zlo, T zhi, 503 jlong& rxlo, jlong& rxhi, jlong& rylo, jlong& ryhi, 504 const Node* n) { 505 assert(xlo <= xhi && ylo <= yhi && zlo <= zhi, "should not be empty types"); 506 507 // Now it's always safe to assume x+y does not overflow. 508 // This is true even if some pairs x,y might cause overflow, as long 509 // as that overflow value cannot fall into [zlo,zhi]. 510 511 // Confident that the arithmetic is "as if infinite precision", 512 // we can now use n's range to put constraints on those of x and y. 513 // The "natural" range of x [xlo,xhi] can perhaps be narrowed to a 514 // more "restricted" range by intersecting [xlo,xhi] with the 515 // range obtained by subtracting y's range from the asserted range 516 // of the I2L conversion. Here's the interval arithmetic algebra: 517 // x == n-y == [zlo,zhi]-[ylo,yhi] == [zlo,zhi]+[-yhi,-ylo] 518 // => x in [zlo-yhi, zhi-ylo] 519 // => x in [zlo-yhi, zhi-ylo] INTERSECT [xlo,xhi] 520 // => x in [xlo MAX zlo-yhi, xhi MIN zhi-ylo] 521 // And similarly, x changing place with y. 522 if (n->is_Sub()) { 523 if (add_overflows(zlo, ylo) || add_underflows(zhi, yhi) || subtract_underflows(xhi, zlo) || 524 subtract_overflows(xlo, zhi)) { 525 return false; 526 } 527 rxlo = add_underflows(zlo, ylo) ? xlo : MAX2(xlo, java_add(zlo, ylo)); 528 rxhi = add_overflows(zhi, yhi) ? xhi : MIN2(xhi, java_add(zhi, yhi)); 529 ryhi = subtract_overflows(xhi, zlo) ? yhi : MIN2(yhi, java_subtract(xhi, zlo)); 530 rylo = subtract_underflows(xlo, zhi) ? ylo : MAX2(ylo, java_subtract(xlo, zhi)); 531 } else { 532 assert(n->is_Add(), "Add or Sub only"); 533 if (subtract_overflows(zlo, yhi) || subtract_underflows(zhi, ylo) || 534 subtract_overflows(zlo, xhi) || subtract_underflows(zhi, xlo)) { 535 return false; 536 } 537 rxlo = subtract_underflows(zlo, yhi) ? xlo : MAX2(xlo, java_subtract(zlo, yhi)); 538 rxhi = subtract_overflows(zhi, ylo) ? xhi : MIN2(xhi, java_subtract(zhi, ylo)); 539 rylo = subtract_underflows(zlo, xhi) ? ylo : MAX2(ylo, java_subtract(zlo, xhi)); 540 ryhi = subtract_overflows(zhi, xlo) ? yhi : MIN2(yhi, java_subtract(zhi, xlo)); 541 } 542 543 if (rxlo > rxhi || rylo > ryhi) { 544 return false; // x or y is dying; don't mess w/ it 545 } 546 547 return true; 548 } 549 550 static bool compute_updates_ranges(const TypeInteger* tx, const TypeInteger* ty, const TypeInteger* tz, 551 const TypeInteger*& rx, const TypeInteger*& ry, 552 const Node* n, const BasicType in_bt, BasicType out_bt) { 553 554 jlong xlo = tx->lo_as_long(); 555 jlong xhi = tx->hi_as_long(); 556 jlong ylo = ty->lo_as_long(); 557 jlong yhi = ty->hi_as_long(); 558 jlong zlo = tz->lo_as_long(); 559 jlong zhi = tz->hi_as_long(); 560 jlong rxlo, rxhi, rylo, ryhi; 561 562 if (in_bt == T_INT) { 563 #ifdef ASSERT 564 jlong expected_rxlo, expected_rxhi, expected_rylo, expected_ryhi; 565 bool expected = compute_updates_ranges_verif(tx, ty, tz, 566 expected_rxlo, expected_rxhi, 567 expected_rylo, expected_ryhi, n); 568 #endif 569 if (!compute_updates_ranges(checked_cast<jint>(xlo), checked_cast<jint>(ylo), 570 checked_cast<jint>(xhi), checked_cast<jint>(yhi), 571 checked_cast<jint>(zlo), checked_cast<jint>(zhi), 572 rxlo, rxhi, rylo, ryhi, n)) { 573 assert(!expected, "inconsistent"); 574 return false; 575 } 576 assert(expected && rxlo == expected_rxlo && rxhi == expected_rxhi && rylo == expected_rylo && ryhi == expected_ryhi, "inconsistent"); 577 } else { 578 if (!compute_updates_ranges(xlo, ylo, xhi, yhi, zlo, zhi, 579 rxlo, rxhi, rylo, ryhi, n)) { 580 return false; 581 } 582 } 583 584 int widen = MAX2(tx->widen_limit(), ty->widen_limit()); 585 rx = TypeInteger::make(rxlo, rxhi, widen, out_bt); 586 ry = TypeInteger::make(rylo, ryhi, widen, out_bt); 587 return true; 588 } 589 590 #ifdef _LP64 591 // If there is an existing ConvI2L node with the given parent and type, return 592 // it. Otherwise, create and return a new one. Both reusing existing ConvI2L 593 // nodes and postponing the idealization of new ones are needed to avoid an 594 // explosion of recursive Ideal() calls when compiling long AddI chains. 595 static Node* find_or_make_convI2L(PhaseIterGVN* igvn, Node* parent, 596 const TypeLong* type) { 597 Node* n = new ConvI2LNode(parent, type); 598 Node* existing = igvn->hash_find_insert(n); 599 if (existing != nullptr) { 600 n->destruct(igvn); 601 return existing; 602 } 603 return igvn->register_new_node_with_optimizer(n); 604 } 605 #endif 606 607 bool Compile::push_thru_add(PhaseGVN* phase, Node* z, const TypeInteger* tz, const TypeInteger*& rx, const TypeInteger*& ry, 608 BasicType in_bt, BasicType out_bt) { 609 int op = z->Opcode(); 610 if (op == Op_Add(in_bt) || op == Op_Sub(in_bt)) { 611 Node* x = z->in(1); 612 Node* y = z->in(2); 613 assert (x != z && y != z, "dead loop in ConvI2LNode::Ideal"); 614 if (phase->type(x) == Type::TOP) { 615 return false; 616 } 617 if (phase->type(y) == Type::TOP) { 618 return false; 619 } 620 const TypeInteger* tx = phase->type(x)->is_integer(in_bt); 621 const TypeInteger* ty = phase->type(y)->is_integer(in_bt); 622 623 if (ranges_overlap(tx, ty, tz, z, true, in_bt) || 624 ranges_overlap(tx, ty, tz, z, false, in_bt)) { 625 return false; 626 } 627 return compute_updates_ranges(tx, ty, tz, rx, ry, z, in_bt, out_bt); 628 } 629 return false; 630 } 631 632 633 //------------------------------Ideal------------------------------------------ 634 Node *ConvI2LNode::Ideal(PhaseGVN *phase, bool can_reshape) { 635 const TypeLong* this_type = this->type()->is_long(); 636 if (can_reshape && !phase->C->post_loop_opts_phase()) { 637 // makes sure we run ::Value to potentially remove type assertion after loop opts 638 phase->C->record_for_post_loop_opts_igvn(this); 639 } 640 #ifdef _LP64 641 // Convert ConvI2L(AddI(x, y)) to AddL(ConvI2L(x), ConvI2L(y)) 642 // but only if x and y have subranges that cannot cause 32-bit overflow, 643 // under the assumption that x+y is in my own subrange this->type(). 644 645 // This assumption is based on a constraint (i.e., type assertion) 646 // established in Parse::array_addressing or perhaps elsewhere. 647 // This constraint has been adjoined to the "natural" type of 648 // the incoming argument in(0). We know (because of runtime 649 // checks) - that the result value I2L(x+y) is in the joined range. 650 // Hence we can restrict the incoming terms (x, y) to values such 651 // that their sum also lands in that range. 652 653 // This optimization is useful only on 64-bit systems, where we hope 654 // the addition will end up subsumed in an addressing mode. 655 // It is necessary to do this when optimizing an unrolled array 656 // copy loop such as x[i++] = y[i++]. 657 658 // On 32-bit systems, it's better to perform as much 32-bit math as 659 // possible before the I2L conversion, because 32-bit math is cheaper. 660 // There's no common reason to "leak" a constant offset through the I2L. 661 // Addressing arithmetic will not absorb it as part of a 64-bit AddL. 662 PhaseIterGVN* igvn = phase->is_IterGVN(); 663 Node* z = in(1); 664 const TypeInteger* rx = nullptr; 665 const TypeInteger* ry = nullptr; 666 if (Compile::push_thru_add(phase, z, this_type, rx, ry, T_INT, T_LONG)) { 667 if (igvn == nullptr) { 668 // Postpone this optimization to iterative GVN, where we can handle deep 669 // AddI chains without an exponential number of recursive Ideal() calls. 670 phase->record_for_igvn(this); 671 return nullptr; 672 } 673 int op = z->Opcode(); 674 Node* x = z->in(1); 675 Node* y = z->in(2); 676 677 Node* cx = find_or_make_convI2L(igvn, x, rx->is_long()); 678 Node* cy = find_or_make_convI2L(igvn, y, ry->is_long()); 679 switch (op) { 680 case Op_AddI: return new AddLNode(cx, cy); 681 case Op_SubI: return new SubLNode(cx, cy); 682 default: ShouldNotReachHere(); 683 } 684 } 685 #endif //_LP64 686 687 return nullptr; 688 } 689 690 //============================================================================= 691 //------------------------------Value------------------------------------------ 692 const Type* ConvL2DNode::Value(PhaseGVN* phase) const { 693 const Type *t = phase->type( in(1) ); 694 if( t == Type::TOP ) return Type::TOP; 695 const TypeLong *tl = t->is_long(); 696 if( tl->is_con() ) return TypeD::make( (double)tl->get_con() ); 697 return bottom_type(); 698 } 699 700 //============================================================================= 701 //------------------------------Value------------------------------------------ 702 const Type* ConvL2FNode::Value(PhaseGVN* phase) const { 703 const Type *t = phase->type( in(1) ); 704 if( t == Type::TOP ) return Type::TOP; 705 const TypeLong *tl = t->is_long(); 706 if( tl->is_con() ) return TypeF::make( (float)tl->get_con() ); 707 return bottom_type(); 708 } 709 710 //============================================================================= 711 //----------------------------Identity----------------------------------------- 712 Node* ConvL2INode::Identity(PhaseGVN* phase) { 713 // Convert L2I(I2L(x)) => x 714 if (in(1)->Opcode() == Op_ConvI2L) return in(1)->in(1); 715 return this; 716 } 717 718 //------------------------------Value------------------------------------------ 719 const Type* ConvL2INode::Value(PhaseGVN* phase) const { 720 const Type *t = phase->type( in(1) ); 721 if( t == Type::TOP ) return Type::TOP; 722 const TypeLong *tl = t->is_long(); 723 const TypeInt* ti = TypeInt::INT; 724 if (tl->is_con()) { 725 // Easy case. 726 ti = TypeInt::make((jint)tl->get_con()); 727 } else if (tl->_lo >= min_jint && tl->_hi <= max_jint) { 728 ti = TypeInt::make((jint)tl->_lo, (jint)tl->_hi, tl->_widen); 729 } 730 return ti->filter(_type); 731 } 732 733 //------------------------------Ideal------------------------------------------ 734 // Return a node which is more "ideal" than the current node. 735 // Blow off prior masking to int 736 Node *ConvL2INode::Ideal(PhaseGVN *phase, bool can_reshape) { 737 Node *andl = in(1); 738 uint andl_op = andl->Opcode(); 739 if( andl_op == Op_AndL ) { 740 // Blow off prior masking to int 741 if( phase->type(andl->in(2)) == TypeLong::make( 0xFFFFFFFF ) ) { 742 set_req_X(1,andl->in(1), phase); 743 return this; 744 } 745 } 746 747 // Swap with a prior add: convL2I(addL(x,y)) ==> addI(convL2I(x),convL2I(y)) 748 // This replaces an 'AddL' with an 'AddI'. 749 if( andl_op == Op_AddL ) { 750 // Don't do this for nodes which have more than one user since 751 // we'll end up computing the long add anyway. 752 if (andl->outcnt() > 1) return nullptr; 753 754 Node* x = andl->in(1); 755 Node* y = andl->in(2); 756 assert( x != andl && y != andl, "dead loop in ConvL2INode::Ideal" ); 757 if (phase->type(x) == Type::TOP) return nullptr; 758 if (phase->type(y) == Type::TOP) return nullptr; 759 Node *add1 = phase->transform(new ConvL2INode(x)); 760 Node *add2 = phase->transform(new ConvL2INode(y)); 761 return new AddINode(add1,add2); 762 } 763 764 // Disable optimization: LoadL->ConvL2I ==> LoadI. 765 // It causes problems (sizes of Load and Store nodes do not match) 766 // in objects initialization code and Escape Analysis. 767 return nullptr; 768 } 769 770 771 772 //============================================================================= 773 //------------------------------Identity--------------------------------------- 774 // Remove redundant roundings 775 Node* RoundFloatNode::Identity(PhaseGVN* phase) { 776 assert(Matcher::strict_fp_requires_explicit_rounding, "should only generate for Intel"); 777 // Do not round constants 778 if (phase->type(in(1))->base() == Type::FloatCon) return in(1); 779 int op = in(1)->Opcode(); 780 // Redundant rounding 781 if( op == Op_RoundFloat ) return in(1); 782 // Already rounded 783 if( op == Op_Parm ) return in(1); 784 if( op == Op_LoadF ) return in(1); 785 return this; 786 } 787 788 //------------------------------Value------------------------------------------ 789 const Type* RoundFloatNode::Value(PhaseGVN* phase) const { 790 return phase->type( in(1) ); 791 } 792 793 //============================================================================= 794 //------------------------------Identity--------------------------------------- 795 // Remove redundant roundings. Incoming arguments are already rounded. 796 Node* RoundDoubleNode::Identity(PhaseGVN* phase) { 797 assert(Matcher::strict_fp_requires_explicit_rounding, "should only generate for Intel"); 798 // Do not round constants 799 if (phase->type(in(1))->base() == Type::DoubleCon) return in(1); 800 int op = in(1)->Opcode(); 801 // Redundant rounding 802 if( op == Op_RoundDouble ) return in(1); 803 // Already rounded 804 if( op == Op_Parm ) return in(1); 805 if( op == Op_LoadD ) return in(1); 806 if( op == Op_ConvF2D ) return in(1); 807 if( op == Op_ConvI2D ) return in(1); 808 return this; 809 } 810 811 //------------------------------Value------------------------------------------ 812 const Type* RoundDoubleNode::Value(PhaseGVN* phase) const { 813 return phase->type( in(1) ); 814 } 815 816 //============================================================================= 817 RoundDoubleModeNode* RoundDoubleModeNode::make(PhaseGVN& gvn, Node* arg, RoundDoubleModeNode::RoundingMode rmode) { 818 ConINode* rm = gvn.intcon(rmode); 819 return new RoundDoubleModeNode(arg, (Node *)rm); 820 } 821 822 //------------------------------Identity--------------------------------------- 823 // Remove redundant roundings. 824 Node* RoundDoubleModeNode::Identity(PhaseGVN* phase) { 825 int op = in(1)->Opcode(); 826 // Redundant rounding e.g. floor(ceil(n)) -> ceil(n) 827 if(op == Op_RoundDoubleMode) return in(1); 828 return this; 829 } 830 const Type* RoundDoubleModeNode::Value(PhaseGVN* phase) const { 831 return Type::DOUBLE; 832 } 833 //=============================================================================