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