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