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