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