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