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---------------------------------------
Identity(PhaseGVN * phase)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------------------------------------------
Value(PhaseGVN * phase) const46 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------------------------------------------
Value(PhaseGVN * phase) const68 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.
Ideal(PhaseGVN * phase,bool can_reshape)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.
Identity(PhaseGVN * phase)94 Node* ConvD2FNode::Identity(PhaseGVN* phase) {
95   return (in(1)->Opcode() == Op_ConvF2D) ? in(1)->in(1) : this;
96 }
97 
98 //=============================================================================
99 //------------------------------Value------------------------------------------
Value(PhaseGVN * phase) const100 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
Ideal(PhaseGVN * phase,bool can_reshape)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 NULL;
114 }
115 
116 //------------------------------Identity---------------------------------------
117 // Int's can be converted to doubles with no loss of bits.  Hence
118 // converting an integer to a double and back to an integer is a NOP.
Identity(PhaseGVN * phase)119 Node* ConvD2INode::Identity(PhaseGVN* phase) {
120   return (in(1)->Opcode() == Op_ConvI2D) ? in(1)->in(1) : this;
121 }
122 
123 //=============================================================================
124 //------------------------------Value------------------------------------------
Value(PhaseGVN * phase) const125 const Type* ConvD2LNode::Value(PhaseGVN* phase) const {
126   const Type *t = phase->type( in(1) );
127   if( t == Type::TOP ) return Type::TOP;
128   if( t == Type::DOUBLE ) return TypeLong::LONG;
129   const TypeD *td = t->is_double_constant();
130   return TypeLong::make( SharedRuntime::d2l( td->getd() ) );
131 }
132 
133 //------------------------------Identity---------------------------------------
Identity(PhaseGVN * phase)134 Node* ConvD2LNode::Identity(PhaseGVN* phase) {
135   // Remove ConvD2L->ConvL2D->ConvD2L sequences.
136   if( in(1)       ->Opcode() == Op_ConvL2D &&
137      in(1)->in(1)->Opcode() == Op_ConvD2L )
138   return in(1)->in(1);
139   return this;
140 }
141 
142 //------------------------------Ideal------------------------------------------
143 // If converting to an int type, skip any rounding nodes
Ideal(PhaseGVN * phase,bool can_reshape)144 Node *ConvD2LNode::Ideal(PhaseGVN *phase, bool can_reshape) {
145   if( in(1)->Opcode() == Op_RoundDouble )
146   set_req(1,in(1)->in(1));
147   return NULL;
148 }
149 
150 //=============================================================================
151 //------------------------------Value------------------------------------------
Value(PhaseGVN * phase) const152 const Type* ConvF2DNode::Value(PhaseGVN* phase) const {
153   const Type *t = phase->type( in(1) );
154   if( t == Type::TOP ) return Type::TOP;
155   if( t == Type::FLOAT ) return Type::DOUBLE;
156   const TypeF *tf = t->is_float_constant();
157   return TypeD::make( (double)tf->getf() );
158 }
159 
160 //=============================================================================
161 //------------------------------Value------------------------------------------
Value(PhaseGVN * phase) const162 const Type* ConvF2INode::Value(PhaseGVN* phase) const {
163   const Type *t = phase->type( in(1) );
164   if( t == Type::TOP )       return Type::TOP;
165   if( t == Type::FLOAT ) return TypeInt::INT;
166   const TypeF *tf = t->is_float_constant();
167   return TypeInt::make( SharedRuntime::f2i( tf->getf() ) );
168 }
169 
170 //------------------------------Identity---------------------------------------
Identity(PhaseGVN * phase)171 Node* ConvF2INode::Identity(PhaseGVN* phase) {
172   // Remove ConvF2I->ConvI2F->ConvF2I sequences.
173   if( in(1)       ->Opcode() == Op_ConvI2F &&
174      in(1)->in(1)->Opcode() == Op_ConvF2I )
175   return in(1)->in(1);
176   return this;
177 }
178 
179 //------------------------------Ideal------------------------------------------
180 // If converting to an int type, skip any rounding nodes
Ideal(PhaseGVN * phase,bool can_reshape)181 Node *ConvF2INode::Ideal(PhaseGVN *phase, bool can_reshape) {
182   if( in(1)->Opcode() == Op_RoundFloat )
183   set_req(1,in(1)->in(1));
184   return NULL;
185 }
186 
187 //=============================================================================
188 //------------------------------Value------------------------------------------
Value(PhaseGVN * phase) const189 const Type* ConvF2LNode::Value(PhaseGVN* phase) const {
190   const Type *t = phase->type( in(1) );
191   if( t == Type::TOP )       return Type::TOP;
192   if( t == Type::FLOAT ) return TypeLong::LONG;
193   const TypeF *tf = t->is_float_constant();
194   return TypeLong::make( SharedRuntime::f2l( tf->getf() ) );
195 }
196 
197 //------------------------------Identity---------------------------------------
Identity(PhaseGVN * phase)198 Node* ConvF2LNode::Identity(PhaseGVN* phase) {
199   // Remove ConvF2L->ConvL2F->ConvF2L sequences.
200   if( in(1)       ->Opcode() == Op_ConvL2F &&
201      in(1)->in(1)->Opcode() == Op_ConvF2L )
202   return in(1)->in(1);
203   return this;
204 }
205 
206 //------------------------------Ideal------------------------------------------
207 // If converting to an int type, skip any rounding nodes
Ideal(PhaseGVN * phase,bool can_reshape)208 Node *ConvF2LNode::Ideal(PhaseGVN *phase, bool can_reshape) {
209   if( in(1)->Opcode() == Op_RoundFloat )
210   set_req(1,in(1)->in(1));
211   return NULL;
212 }
213 
214 //=============================================================================
215 //------------------------------Value------------------------------------------
Value(PhaseGVN * phase) const216 const Type* ConvI2DNode::Value(PhaseGVN* phase) const {
217   const Type *t = phase->type( in(1) );
218   if( t == Type::TOP ) return Type::TOP;
219   const TypeInt *ti = t->is_int();
220   if( ti->is_con() ) return TypeD::make( (double)ti->get_con() );
221   return bottom_type();
222 }
223 
224 //=============================================================================
225 //------------------------------Value------------------------------------------
Value(PhaseGVN * phase) const226 const Type* ConvI2FNode::Value(PhaseGVN* phase) const {
227   const Type *t = phase->type( in(1) );
228   if( t == Type::TOP ) return Type::TOP;
229   const TypeInt *ti = t->is_int();
230   if( ti->is_con() ) return TypeF::make( (float)ti->get_con() );
231   return bottom_type();
232 }
233 
234 //------------------------------Identity---------------------------------------
Identity(PhaseGVN * phase)235 Node* ConvI2FNode::Identity(PhaseGVN* phase) {
236   // Remove ConvI2F->ConvF2I->ConvI2F sequences.
237   if( in(1)       ->Opcode() == Op_ConvF2I &&
238      in(1)->in(1)->Opcode() == Op_ConvI2F )
239   return in(1)->in(1);
240   return this;
241 }
242 
243 //=============================================================================
244 //------------------------------Value------------------------------------------
Value(PhaseGVN * phase) const245 const Type* ConvI2LNode::Value(PhaseGVN* phase) const {
246   const Type *t = phase->type( in(1) );
247   if( t == Type::TOP ) return Type::TOP;
248   const TypeInt *ti = t->is_int();
249   const Type* tl = TypeLong::make(ti->_lo, ti->_hi, ti->_widen);
250   // Join my declared type against my incoming type.
251   tl = tl->filter(_type);
252   return tl;
253 }
254 
long_ranges_overlap(jlong lo1,jlong hi1,jlong lo2,jlong hi2)255 static inline bool long_ranges_overlap(jlong lo1, jlong hi1,
256                                        jlong lo2, jlong hi2) {
257   // Two ranges overlap iff one range's low point falls in the other range.
258   return (lo2 <= lo1 && lo1 <= hi2) || (lo1 <= lo2 && lo2 <= hi1);
259 }
260 
261 #ifdef _LP64
262 // If there is an existing ConvI2L node with the given parent and type, return
263 // it. Otherwise, create and return a new one. Both reusing existing ConvI2L
264 // nodes and postponing the idealization of new ones are needed to avoid an
265 // explosion of recursive Ideal() calls when compiling long AddI chains.
find_or_make_convI2L(PhaseIterGVN * igvn,Node * parent,const TypeLong * type)266 static Node* find_or_make_convI2L(PhaseIterGVN* igvn, Node* parent,
267                                   const TypeLong* type) {
268   Node* n = new ConvI2LNode(parent, type);
269   Node* existing = igvn->hash_find_insert(n);
270   if (existing != NULL) {
271     n->destruct(igvn);
272     return existing;
273   }
274   return igvn->register_new_node_with_optimizer(n);
275 }
276 #endif
277 
push_thru_add(PhaseGVN * phase,Node * z,const TypeInteger * tz,const TypeInteger * & rx,const TypeInteger * & ry,BasicType bt)278 bool Compile::push_thru_add(PhaseGVN* phase, Node* z, const TypeInteger* tz, const TypeInteger*& rx, const TypeInteger*& ry,
279                             BasicType bt) {
280   int op = z->Opcode();
281   if (op == Op_AddI || op == Op_SubI) {
282     Node* x = z->in(1);
283     Node* y = z->in(2);
284     assert (x != z && y != z, "dead loop in ConvI2LNode::Ideal");
285     if (phase->type(x) == Type::TOP) {
286       return false;
287     }
288     if (phase->type(y) == Type::TOP) {
289       return false;
290     }
291     const TypeInt*  tx = phase->type(x)->is_int();
292     const TypeInt*  ty = phase->type(y)->is_int();
293 
294     jlong xlo = tx->is_int()->_lo;
295     jlong xhi = tx->is_int()->_hi;
296     jlong ylo = ty->is_int()->_lo;
297     jlong yhi = ty->is_int()->_hi;
298     jlong zlo = tz->lo_as_long();
299     jlong zhi = tz->hi_as_long();
300     jlong vbit = CONST64(1) << BitsPerInt;
301     int widen =  MAX2(tx->_widen, ty->_widen);
302     if (op == Op_SubI) {
303       jlong ylo0 = ylo;
304       ylo = -yhi;
305       yhi = -ylo0;
306     }
307     // See if x+y can cause positive overflow into z+2**32
308     if (long_ranges_overlap(xlo+ylo, xhi+yhi, zlo+vbit, zhi+vbit)) {
309       return false;
310     }
311     // See if x+y can cause negative overflow into z-2**32
312     if (long_ranges_overlap(xlo+ylo, xhi+yhi, zlo-vbit, zhi-vbit)) {
313       return false;
314     }
315     // Now it's always safe to assume x+y does not overflow.
316     // This is true even if some pairs x,y might cause overflow, as long
317     // as that overflow value cannot fall into [zlo,zhi].
318 
319     // Confident that the arithmetic is "as if infinite precision",
320     // we can now use z's range to put constraints on those of x and y.
321     // The "natural" range of x [xlo,xhi] can perhaps be narrowed to a
322     // more "restricted" range by intersecting [xlo,xhi] with the
323     // range obtained by subtracting y's range from the asserted range
324     // of the I2L conversion.  Here's the interval arithmetic algebra:
325     //    x == z-y == [zlo,zhi]-[ylo,yhi] == [zlo,zhi]+[-yhi,-ylo]
326     //    => x in [zlo-yhi, zhi-ylo]
327     //    => x in [zlo-yhi, zhi-ylo] INTERSECT [xlo,xhi]
328     //    => x in [xlo MAX zlo-yhi, xhi MIN zhi-ylo]
329     jlong rxlo = MAX2(xlo, zlo - yhi);
330     jlong rxhi = MIN2(xhi, zhi - ylo);
331     // And similarly, x changing place with y:
332     jlong rylo = MAX2(ylo, zlo - xhi);
333     jlong ryhi = MIN2(yhi, zhi - xlo);
334     if (rxlo > rxhi || rylo > ryhi) {
335       return false;  // x or y is dying; don't mess w/ it
336     }
337     if (op == Op_SubI) {
338       jlong rylo0 = rylo;
339       rylo = -ryhi;
340       ryhi = -rylo0;
341     }
342     assert(rxlo == (int)rxlo && rxhi == (int)rxhi, "x should not overflow");
343     assert(rylo == (int)rylo && ryhi == (int)ryhi, "y should not overflow");
344     rx = TypeInteger::make(rxlo, rxhi, widen, bt);
345     ry = TypeInteger::make(rylo, ryhi, widen, bt);
346     return true;
347   }
348   return false;
349 }
350 
351 
352 //------------------------------Ideal------------------------------------------
Ideal(PhaseGVN * phase,bool can_reshape)353 Node *ConvI2LNode::Ideal(PhaseGVN *phase, bool can_reshape) {
354   PhaseIterGVN *igvn = phase->is_IterGVN();
355   const TypeLong* this_type = this->type()->is_long();
356   Node* this_changed = NULL;
357 
358   if (igvn != NULL) {
359     // Do NOT remove this node's type assertion until no more loop ops can happen.
360     if (phase->C->post_loop_opts_phase()) {
361       const TypeInt* in_type = phase->type(in(1))->isa_int();
362       if (in_type != NULL && this_type != NULL &&
363           (in_type->_lo != this_type->_lo ||
364            in_type->_hi != this_type->_hi)) {
365         // Although this WORSENS the type, it increases GVN opportunities,
366         // because I2L nodes with the same input will common up, regardless
367         // of slightly differing type assertions.  Such slight differences
368         // arise routinely as a result of loop unrolling, so this is a
369         // post-unrolling graph cleanup.  Choose a type which depends only
370         // on my input.  (Exception:  Keep a range assertion of >=0 or <0.)
371         jlong lo1 = this_type->_lo;
372         jlong hi1 = this_type->_hi;
373         int   w1  = this_type->_widen;
374         if (lo1 != (jint)lo1 ||
375             hi1 != (jint)hi1 ||
376             lo1 > hi1) {
377           // Overflow leads to wraparound, wraparound leads to range saturation.
378           lo1 = min_jint; hi1 = max_jint;
379         } else if (lo1 >= 0) {
380           // Keep a range assertion of >=0.
381           lo1 = 0;        hi1 = max_jint;
382         } else if (hi1 < 0) {
383           // Keep a range assertion of <0.
384           lo1 = min_jint; hi1 = -1;
385         } else {
386           lo1 = min_jint; hi1 = max_jint;
387         }
388         const TypeLong* wtype = TypeLong::make(MAX2((jlong)in_type->_lo, lo1),
389                                                MIN2((jlong)in_type->_hi, hi1),
390                                                MAX2((int)in_type->_widen, w1));
391         if (wtype != type()) {
392           set_type(wtype);
393           // Note: this_type still has old type value, for the logic below.
394           this_changed = this;
395         }
396       }
397     } else {
398       phase->C->record_for_post_loop_opts_igvn(this);
399     }
400   }
401 #ifdef _LP64
402   // Convert ConvI2L(AddI(x, y)) to AddL(ConvI2L(x), ConvI2L(y))
403   // but only if x and y have subranges that cannot cause 32-bit overflow,
404   // under the assumption that x+y is in my own subrange this->type().
405 
406   // This assumption is based on a constraint (i.e., type assertion)
407   // established in Parse::array_addressing or perhaps elsewhere.
408   // This constraint has been adjoined to the "natural" type of
409   // the incoming argument in(0).  We know (because of runtime
410   // checks) - that the result value I2L(x+y) is in the joined range.
411   // Hence we can restrict the incoming terms (x, y) to values such
412   // that their sum also lands in that range.
413 
414   // This optimization is useful only on 64-bit systems, where we hope
415   // the addition will end up subsumed in an addressing mode.
416   // It is necessary to do this when optimizing an unrolled array
417   // copy loop such as x[i++] = y[i++].
418 
419   // On 32-bit systems, it's better to perform as much 32-bit math as
420   // possible before the I2L conversion, because 32-bit math is cheaper.
421   // There's no common reason to "leak" a constant offset through the I2L.
422   // Addressing arithmetic will not absorb it as part of a 64-bit AddL.
423 
424   Node* z = in(1);
425   const TypeInteger* rx = NULL;
426   const TypeInteger* ry = NULL;
427   if (Compile::push_thru_add(phase, z, this_type, rx, ry, T_LONG)) {
428     if (igvn == NULL) {
429       // Postpone this optimization to iterative GVN, where we can handle deep
430       // AddI chains without an exponential number of recursive Ideal() calls.
431       phase->record_for_igvn(this);
432       return this_changed;
433     }
434     int op = z->Opcode();
435     Node* x = z->in(1);
436     Node* y = z->in(2);
437 
438     Node* cx = find_or_make_convI2L(igvn, x, rx->is_long());
439     Node* cy = find_or_make_convI2L(igvn, y, ry->is_long());
440     switch (op) {
441       case Op_AddI:  return new AddLNode(cx, cy);
442       case Op_SubI:  return new SubLNode(cx, cy);
443       default:       ShouldNotReachHere();
444     }
445   }
446 #endif //_LP64
447 
448   return this_changed;
449 }
450 
451 //=============================================================================
452 //------------------------------Value------------------------------------------
Value(PhaseGVN * phase) const453 const Type* ConvL2DNode::Value(PhaseGVN* phase) const {
454   const Type *t = phase->type( in(1) );
455   if( t == Type::TOP ) return Type::TOP;
456   const TypeLong *tl = t->is_long();
457   if( tl->is_con() ) return TypeD::make( (double)tl->get_con() );
458   return bottom_type();
459 }
460 
461 //=============================================================================
462 //------------------------------Value------------------------------------------
Value(PhaseGVN * phase) const463 const Type* ConvL2FNode::Value(PhaseGVN* phase) const {
464   const Type *t = phase->type( in(1) );
465   if( t == Type::TOP ) return Type::TOP;
466   const TypeLong *tl = t->is_long();
467   if( tl->is_con() ) return TypeF::make( (float)tl->get_con() );
468   return bottom_type();
469 }
470 
471 //=============================================================================
472 //----------------------------Identity-----------------------------------------
Identity(PhaseGVN * phase)473 Node* ConvL2INode::Identity(PhaseGVN* phase) {
474   // Convert L2I(I2L(x)) => x
475   if (in(1)->Opcode() == Op_ConvI2L)  return in(1)->in(1);
476   return this;
477 }
478 
479 //------------------------------Value------------------------------------------
Value(PhaseGVN * phase) const480 const Type* ConvL2INode::Value(PhaseGVN* phase) const {
481   const Type *t = phase->type( in(1) );
482   if( t == Type::TOP ) return Type::TOP;
483   const TypeLong *tl = t->is_long();
484   const TypeInt* ti = TypeInt::INT;
485   if (tl->is_con()) {
486     // Easy case.
487     ti = TypeInt::make((jint)tl->get_con());
488   } else if (tl->_lo >= min_jint && tl->_hi <= max_jint) {
489     ti = TypeInt::make((jint)tl->_lo, (jint)tl->_hi, tl->_widen);
490   }
491   return ti->filter(_type);
492 }
493 
494 //------------------------------Ideal------------------------------------------
495 // Return a node which is more "ideal" than the current node.
496 // Blow off prior masking to int
Ideal(PhaseGVN * phase,bool can_reshape)497 Node *ConvL2INode::Ideal(PhaseGVN *phase, bool can_reshape) {
498   Node *andl = in(1);
499   uint andl_op = andl->Opcode();
500   if( andl_op == Op_AndL ) {
501     // Blow off prior masking to int
502     if( phase->type(andl->in(2)) == TypeLong::make( 0xFFFFFFFF ) ) {
503       set_req(1,andl->in(1));
504       return this;
505     }
506   }
507 
508   // Swap with a prior add: convL2I(addL(x,y)) ==> addI(convL2I(x),convL2I(y))
509   // This replaces an 'AddL' with an 'AddI'.
510   if( andl_op == Op_AddL ) {
511     // Don't do this for nodes which have more than one user since
512     // we'll end up computing the long add anyway.
513     if (andl->outcnt() > 1) return NULL;
514 
515     Node* x = andl->in(1);
516     Node* y = andl->in(2);
517     assert( x != andl && y != andl, "dead loop in ConvL2INode::Ideal" );
518     if (phase->type(x) == Type::TOP)  return NULL;
519     if (phase->type(y) == Type::TOP)  return NULL;
520     Node *add1 = phase->transform(new ConvL2INode(x));
521     Node *add2 = phase->transform(new ConvL2INode(y));
522     return new AddINode(add1,add2);
523   }
524 
525   // Disable optimization: LoadL->ConvL2I ==> LoadI.
526   // It causes problems (sizes of Load and Store nodes do not match)
527   // in objects initialization code and Escape Analysis.
528   return NULL;
529 }
530 
531 
532 
533 //=============================================================================
534 //------------------------------Identity---------------------------------------
535 // Remove redundant roundings
Identity(PhaseGVN * phase)536 Node* RoundFloatNode::Identity(PhaseGVN* phase) {
537   assert(Matcher::strict_fp_requires_explicit_rounding, "should only generate for Intel");
538   // Do not round constants
539   if (phase->type(in(1))->base() == Type::FloatCon)  return in(1);
540   int op = in(1)->Opcode();
541   // Redundant rounding
542   if( op == Op_RoundFloat ) return in(1);
543   // Already rounded
544   if( op == Op_Parm ) return in(1);
545   if( op == Op_LoadF ) return in(1);
546   return this;
547 }
548 
549 //------------------------------Value------------------------------------------
Value(PhaseGVN * phase) const550 const Type* RoundFloatNode::Value(PhaseGVN* phase) const {
551   return phase->type( in(1) );
552 }
553 
554 //=============================================================================
555 //------------------------------Identity---------------------------------------
556 // Remove redundant roundings.  Incoming arguments are already rounded.
Identity(PhaseGVN * phase)557 Node* RoundDoubleNode::Identity(PhaseGVN* phase) {
558   assert(Matcher::strict_fp_requires_explicit_rounding, "should only generate for Intel");
559   // Do not round constants
560   if (phase->type(in(1))->base() == Type::DoubleCon)  return in(1);
561   int op = in(1)->Opcode();
562   // Redundant rounding
563   if( op == Op_RoundDouble ) return in(1);
564   // Already rounded
565   if( op == Op_Parm ) return in(1);
566   if( op == Op_LoadD ) return in(1);
567   if( op == Op_ConvF2D ) return in(1);
568   if( op == Op_ConvI2D ) return in(1);
569   return this;
570 }
571 
572 //------------------------------Value------------------------------------------
Value(PhaseGVN * phase) const573 const Type* RoundDoubleNode::Value(PhaseGVN* phase) const {
574   return phase->type( in(1) );
575 }
576 
577 //=============================================================================
make(PhaseGVN & gvn,Node * arg,RoundDoubleModeNode::RoundingMode rmode)578 RoundDoubleModeNode* RoundDoubleModeNode::make(PhaseGVN& gvn, Node* arg, RoundDoubleModeNode::RoundingMode rmode) {
579   ConINode* rm = gvn.intcon(rmode);
580   return new RoundDoubleModeNode(arg, (Node *)rm);
581 }
582 
583 //------------------------------Identity---------------------------------------
584 // Remove redundant roundings.
Identity(PhaseGVN * phase)585 Node* RoundDoubleModeNode::Identity(PhaseGVN* phase) {
586   int op = in(1)->Opcode();
587   // Redundant rounding e.g. floor(ceil(n)) -> ceil(n)
588   if(op == Op_RoundDoubleMode) return in(1);
589   return this;
590 }
Value(PhaseGVN * phase) const591 const Type* RoundDoubleModeNode::Value(PhaseGVN* phase) const {
592   return Type::DOUBLE;
593 }
594 //=============================================================================
595