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