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