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