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