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