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