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