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