1 /*
  2  * Copyright (c) 2014, 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/callnode.hpp"
 28 #include "opto/castnode.hpp"
 29 #include "opto/connode.hpp"
 30 #include "opto/graphKit.hpp"
 31 #include "opto/inlinetypenode.hpp"
 32 #include "opto/matcher.hpp"
 33 #include "opto/phaseX.hpp"
 34 #include "opto/rootnode.hpp"
 35 #include "opto/subnode.hpp"
 36 #include "opto/type.hpp"
 37 #include "castnode.hpp"
 38 
 39 //=============================================================================
 40 // If input is already higher or equal to cast type, then this is an identity.
 41 Node* ConstraintCastNode::Identity(PhaseGVN* phase) {
 42   Node* dom = dominating_cast(phase, phase);
 43   if (dom != NULL) {
 44     return dom;
 45   }
 46   if (_dependency != RegularDependency) {
 47     return this;
 48   }
 49   return phase->type(in(1))->higher_equal_speculative(_type) ? in(1) : this;
 50 }
 51 
 52 //------------------------------Value------------------------------------------
 53 // Take 'join' of input and cast-up type
 54 const Type* ConstraintCastNode::Value(PhaseGVN* phase) const {
 55   if (in(0) && phase->type(in(0)) == Type::TOP) return Type::TOP;
 56   const Type* ft = phase->type(in(1))->filter_speculative(_type);
 57 
 58 #ifdef ASSERT
 59   // Previous versions of this function had some special case logic,
 60   // which is no longer necessary.  Make sure of the required effects.
 61   switch (Opcode()) {
 62     case Op_CastII:
 63     {
 64       const Type* t1 = phase->type(in(1));
 65       if( t1 == Type::TOP )  assert(ft == Type::TOP, "special case #1");
 66       const Type* rt = t1->join_speculative(_type);
 67       if (rt->empty())       assert(ft == Type::TOP, "special case #2");
 68       break;
 69     }
 70     case Op_CastPP:
 71     if (phase->type(in(1)) == TypePtr::NULL_PTR &&
 72         _type->isa_ptr() && _type->is_ptr()->_ptr == TypePtr::NotNull)
 73     assert(ft == Type::TOP, "special case #3");
 74     break;
 75   }
 76 #endif //ASSERT
 77 
 78   return ft;
 79 }
 80 
 81 //------------------------------Ideal------------------------------------------
 82 // Return a node which is more "ideal" than the current node.  Strip out
 83 // control copies
 84 Node *ConstraintCastNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 85   if (in(0) && remove_dead_region(phase, can_reshape)) {
 86     return this;
 87   }
 88 
 89   // Push cast through InlineTypePtrNode
 90   InlineTypePtrNode* vt = in(1)->isa_InlineTypePtr();
 91   if (vt != NULL && phase->type(vt)->filter_speculative(_type) != Type::TOP) {
 92     Node* cast = clone();
 93     cast->set_req(1, vt->get_oop());
 94     vt = vt->clone()->as_InlineTypePtr();
 95     vt->set_oop(phase->transform(cast));
 96     return vt;
 97   }
 98 
 99   return NULL;
100 }
101 
102 bool ConstraintCastNode::cmp(const Node &n) const {
103   return TypeNode::cmp(n) && ((ConstraintCastNode&)n)._dependency == _dependency;
104 }
105 
106 uint ConstraintCastNode::size_of() const {
107   return sizeof(*this);
108 }
109 
110 Node* ConstraintCastNode::make_cast(int opcode, Node* c, Node *n, const Type *t, DependencyType dependency) {
111   switch(opcode) {
112   case Op_CastII: {
113     Node* cast = new CastIINode(n, t, dependency);
114     cast->set_req(0, c);
115     return cast;
116   }
117   case Op_CastLL: {
118     Node* cast = new CastLLNode(n, t, dependency);
119     cast->set_req(0, c);
120     return cast;
121   }
122   case Op_CastPP: {
123     Node* cast = new CastPPNode(n, t, dependency);
124     cast->set_req(0, c);
125     return cast;
126   }
127   case Op_CastFF: {
128     Node* cast = new CastFFNode(n, t, dependency);
129     cast->set_req(0, c);
130     return cast;
131   }
132   case Op_CastDD: {
133     Node* cast = new CastDDNode(n, t, dependency);
134     cast->set_req(0, c);
135     return cast;
136   }
137   case Op_CastVV: {
138     Node* cast = new CastVVNode(n, t, dependency);
139     cast->set_req(0, c);
140     return cast;
141   }
142   case Op_CheckCastPP: return new CheckCastPPNode(c, n, t, dependency);
143   default:
144     fatal("Bad opcode %d", opcode);
145   }
146   return NULL;
147 }
148 
149 Node* ConstraintCastNode::make(Node* c, Node *n, const Type *t, DependencyType dependency, BasicType bt) {
150   switch(bt) {
151   case T_INT: {
152     return make_cast(Op_CastII, c, n, t, dependency);
153   }
154   case T_LONG: {
155     return make_cast(Op_CastLL, c, n, t, dependency);
156   }
157   default:
158     fatal("Bad basic type %s", type2name(bt));
159   }
160   return NULL;
161 }
162 
163 TypeNode* ConstraintCastNode::dominating_cast(PhaseGVN* gvn, PhaseTransform* pt) const {
164   if (_dependency == UnconditionalDependency) {
165     return NULL;
166   }
167   Node* val = in(1);
168   Node* ctl = in(0);
169   int opc = Opcode();
170   if (ctl == NULL) {
171     return NULL;
172   }
173   // Range check CastIIs may all end up under a single range check and
174   // in that case only the narrower CastII would be kept by the code
175   // below which would be incorrect.
176   if (is_CastII() && as_CastII()->has_range_check()) {
177     return NULL;
178   }
179   if (type()->isa_rawptr() && (gvn->type_or_null(val) == NULL || gvn->type(val)->isa_oopptr())) {
180     return NULL;
181   }
182   for (DUIterator_Fast imax, i = val->fast_outs(imax); i < imax; i++) {
183     Node* u = val->fast_out(i);
184     if (u != this &&
185         u->outcnt() > 0 &&
186         u->Opcode() == opc &&
187         u->in(0) != NULL &&
188         u->bottom_type()->higher_equal(type())) {
189       if (pt->is_dominator(u->in(0), ctl)) {
190         return u->as_Type();
191       }
192       if (is_CheckCastPP() && u->in(1)->is_Proj() && u->in(1)->in(0)->is_Allocate() &&
193           u->in(0)->is_Proj() && u->in(0)->in(0)->is_Initialize() &&
194           u->in(1)->in(0)->as_Allocate()->initialization() == u->in(0)->in(0)) {
195         // CheckCastPP following an allocation always dominates all
196         // use of the allocation result
197         return u->as_Type();
198       }
199     }
200   }
201   return NULL;
202 }
203 
204 #ifndef PRODUCT
205 void ConstraintCastNode::dump_spec(outputStream *st) const {
206   TypeNode::dump_spec(st);
207   if (_dependency != RegularDependency) {
208     st->print(" %s dependency", _dependency == StrongDependency ? "strong" : "unconditional");
209   }
210 }
211 #endif
212 
213 const Type* CastIINode::Value(PhaseGVN* phase) const {
214   const Type *res = ConstraintCastNode::Value(phase);
215   if (res == Type::TOP) {
216     return Type::TOP;
217   }
218   assert(res->isa_int(), "res must be int");
219 
220   // Similar to ConvI2LNode::Value() for the same reasons
221   // see if we can remove type assertion after loop opts
222   // But here we have to pay extra attention:
223   // Do not narrow the type of range check dependent CastIINodes to
224   // avoid corruption of the graph if a CastII is replaced by TOP but
225   // the corresponding range check is not removed.
226   if (!_range_check_dependency) {
227     res = widen_type(phase, res, T_INT);
228   }
229 
230   // Try to improve the type of the CastII if we recognize a CmpI/If
231   // pattern.
232   if (_dependency != RegularDependency) {
233     if (in(0) != NULL && in(0)->in(0) != NULL && in(0)->in(0)->is_If()) {
234       assert(in(0)->is_IfFalse() || in(0)->is_IfTrue(), "should be If proj");
235       Node* proj = in(0);
236       if (proj->in(0)->in(1)->is_Bool()) {
237         Node* b = proj->in(0)->in(1);
238         if (b->in(1)->Opcode() == Op_CmpI) {
239           Node* cmp = b->in(1);
240           if (cmp->in(1) == in(1) && phase->type(cmp->in(2))->isa_int()) {
241             const TypeInt* in2_t = phase->type(cmp->in(2))->is_int();
242             const Type* t = TypeInt::INT;
243             BoolTest test = b->as_Bool()->_test;
244             if (proj->is_IfFalse()) {
245               test = test.negate();
246             }
247             BoolTest::mask m = test._test;
248             jlong lo_long = min_jint;
249             jlong hi_long = max_jint;
250             if (m == BoolTest::le || m == BoolTest::lt) {
251               hi_long = in2_t->_hi;
252               if (m == BoolTest::lt) {
253                 hi_long -= 1;
254               }
255             } else if (m == BoolTest::ge || m == BoolTest::gt) {
256               lo_long = in2_t->_lo;
257               if (m == BoolTest::gt) {
258                 lo_long += 1;
259               }
260             } else if (m == BoolTest::eq) {
261               lo_long = in2_t->_lo;
262               hi_long = in2_t->_hi;
263             } else if (m == BoolTest::ne) {
264               // can't do any better
265             } else {
266               stringStream ss;
267               test.dump_on(&ss);
268               fatal("unexpected comparison %s", ss.freeze());
269             }
270             int lo_int = (int)lo_long;
271             int hi_int = (int)hi_long;
272 
273             if (lo_long != (jlong)lo_int) {
274               lo_int = min_jint;
275             }
276             if (hi_long != (jlong)hi_int) {
277               hi_int = max_jint;
278             }
279 
280             t = TypeInt::make(lo_int, hi_int, Type::WidenMax);
281 
282             res = res->filter_speculative(t);
283             return res;
284           }
285         }
286       }
287     }
288   }
289   return res;
290 }
291 
292 static Node* find_or_make_integer_cast(PhaseIterGVN* igvn, Node* parent, Node* control, const TypeInteger* type, ConstraintCastNode::DependencyType dependency, BasicType bt) {
293   Node* n = ConstraintCastNode::make(control, parent, type, dependency, bt);
294   Node* existing = igvn->hash_find_insert(n);
295   if (existing != NULL) {
296     n->destruct(igvn);
297     return existing;
298   }
299   return igvn->register_new_node_with_optimizer(n);
300 }
301 
302 Node *CastIINode::Ideal(PhaseGVN *phase, bool can_reshape) {
303   Node* progress = ConstraintCastNode::Ideal(phase, can_reshape);
304   if (progress != NULL) {
305     return progress;
306   }
307   if (can_reshape && !_range_check_dependency && !phase->C->post_loop_opts_phase()) {
308     // makes sure we run ::Value to potentially remove type assertion after loop opts
309     phase->C->record_for_post_loop_opts_igvn(this);
310   }
311   if (!_range_check_dependency) {
312     return optimize_integer_cast(phase, T_INT);
313   }
314   return NULL;
315 }
316 
317 Node* CastIINode::Identity(PhaseGVN* phase) {
318   Node* progress = ConstraintCastNode::Identity(phase);
319   if (progress != this) {
320     return progress;
321   }
322   if (_range_check_dependency) {
323     if (phase->C->post_loop_opts_phase()) {
324       return this->in(1);
325     } else {
326       phase->C->record_for_post_loop_opts_igvn(this);
327     }
328   }
329   return this;
330 }
331 
332 bool CastIINode::cmp(const Node &n) const {
333   return ConstraintCastNode::cmp(n) && ((CastIINode&)n)._range_check_dependency == _range_check_dependency;
334 }
335 
336 uint CastIINode::size_of() const {
337   return sizeof(*this);
338 }
339 
340 #ifndef PRODUCT
341 void CastIINode::dump_spec(outputStream* st) const {
342   ConstraintCastNode::dump_spec(st);
343   if (_range_check_dependency) {
344     st->print(" range check dependency");
345   }
346 }
347 #endif
348 
349 const Type* CastLLNode::Value(PhaseGVN* phase) const {
350   const Type* res = ConstraintCastNode::Value(phase);
351   if (res == Type::TOP) {
352     return Type::TOP;
353   }
354   assert(res->isa_long(), "res must be long");
355 
356   return widen_type(phase, res, T_LONG);
357 }
358 
359 Node* CastLLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
360   Node* progress = ConstraintCastNode::Ideal(phase, can_reshape);
361   if (progress != NULL) {
362     return progress;
363   }
364   if (can_reshape && !phase->C->post_loop_opts_phase()) {
365     // makes sure we run ::Value to potentially remove type assertion after loop opts
366     phase->C->record_for_post_loop_opts_igvn(this);
367   }
368   // transform (CastLL (ConvI2L ..)) into (ConvI2L (CastII ..)) if the type of the CastLL is narrower than the type of
369   // the ConvI2L.
370   Node* in1 = in(1);
371   if (in1 != NULL && in1->Opcode() == Op_ConvI2L) {
372     const Type* t = Value(phase);
373     const Type* t_in = phase->type(in1);
374     if (t != Type::TOP && t_in != Type::TOP) {
375       const TypeLong* tl = t->is_long();
376       const TypeLong* t_in_l = t_in->is_long();
377       assert(tl->_lo >= t_in_l->_lo && tl->_hi <= t_in_l->_hi, "CastLL type should be narrower than or equal to the type of its input");
378       assert((tl != t_in_l) == (tl->_lo > t_in_l->_lo || tl->_hi < t_in_l->_hi), "if type differs then this nodes's type must be narrower");
379       if (tl != t_in_l) {
380         const TypeInt* ti = TypeInt::make(checked_cast<jint>(tl->_lo), checked_cast<jint>(tl->_hi), tl->_widen);
381         Node* castii = phase->transform(new CastIINode(in(0), in1->in(1), ti));
382         Node* convi2l = in1->clone();
383         convi2l->set_req(1, castii);
384         return convi2l;
385       }
386     }
387   }
388   return optimize_integer_cast(phase, T_LONG);
389 }
390 
391 //=============================================================================
392 //------------------------------Identity---------------------------------------
393 // If input is already higher or equal to cast type, then this is an identity.
394 Node* CheckCastPPNode::Identity(PhaseGVN* phase) {
395   if (in(1)->is_InlineTypeBase() && _type->isa_instptr() && phase->type(in(1))->inline_klass()->is_subtype_of(_type->is_instptr()->instance_klass())) {
396     return in(1);
397   }
398   Node* dom = dominating_cast(phase, phase);
399   if (dom != NULL) {
400     return dom;
401   }
402   if (_dependency != RegularDependency) {
403     return this;
404   }
405   const Type* t = phase->type(in(1));
406   if (EnableVectorReboxing && in(1)->Opcode() == Op_VectorBox) {
407     if (t->higher_equal_speculative(phase->type(this))) {
408       return in(1);
409     }
410   } else if (t == phase->type(this)) {
411     // Toned down to rescue meeting at a Phi 3 different oops all implementing
412     // the same interface.
413     return in(1);
414   }
415   return this;
416 }
417 
418 //------------------------------Value------------------------------------------
419 // Take 'join' of input and cast-up type, unless working with an Interface
420 const Type* CheckCastPPNode::Value(PhaseGVN* phase) const {
421   if( in(0) && phase->type(in(0)) == Type::TOP ) return Type::TOP;
422 
423   const Type *inn = phase->type(in(1));
424   if( inn == Type::TOP ) return Type::TOP;  // No information yet
425 
426   const TypePtr *in_type   = inn->isa_ptr();
427   const TypePtr *my_type   = _type->isa_ptr();
428   const Type *result = _type;
429   if (in_type != NULL && my_type != NULL) {
430     if (!StressReflectiveCode && my_type->isa_aryptr() && in_type->isa_aryptr()) {
431       // Propagate array properties (not flat/null-free)
432       // Don't do this when StressReflectiveCode is enabled because it might lead to
433       // a dying data path while the corresponding flat/null-free check is not folded.
434       my_type = my_type->is_aryptr()->update_properties(in_type->is_aryptr());
435       if (my_type == NULL) {
436         return Type::TOP; // Inconsistent properties
437       }
438     }
439     TypePtr::PTR in_ptr = in_type->ptr();
440     if (in_ptr == TypePtr::Null) {
441       result = in_type;
442     } else if (in_ptr == TypePtr::Constant) {
443       if (my_type->isa_rawptr()) {
444         result = my_type;
445       } else {
446         const TypeOopPtr *jptr = my_type->isa_oopptr();
447         assert(jptr, "");
448         result = !in_type->higher_equal(_type)
449           ? my_type->cast_to_ptr_type(TypePtr::NotNull)
450           : in_type;
451       }
452     } else {
453       result =  my_type->cast_to_ptr_type( my_type->join_ptr(in_ptr) );
454     }
455   }
456 
457   // This is the code from TypePtr::xmeet() that prevents us from
458   // having 2 ways to represent the same type. We have to replicate it
459   // here because we don't go through meet/join.
460   if (result->remove_speculative() == result->speculative()) {
461     result = result->remove_speculative();
462   }
463 
464   // Same as above: because we don't go through meet/join, remove the
465   // speculative type if we know we won't use it.
466   return result->cleanup_speculative();
467 
468   // JOIN NOT DONE HERE BECAUSE OF INTERFACE ISSUES.
469   // FIX THIS (DO THE JOIN) WHEN UNION TYPES APPEAR!
470 
471   //
472   // Remove this code after overnight run indicates no performance
473   // loss from not performing JOIN at CheckCastPPNode
474   //
475   // const TypeInstPtr *in_oop = in->isa_instptr();
476   // const TypeInstPtr *my_oop = _type->isa_instptr();
477   // // If either input is an 'interface', return destination type
478   // assert (in_oop == NULL || in_oop->klass() != NULL, "");
479   // assert (my_oop == NULL || my_oop->klass() != NULL, "");
480   // if( (in_oop && in_oop->klass()->is_interface())
481   //   ||(my_oop && my_oop->klass()->is_interface()) ) {
482   //   TypePtr::PTR  in_ptr = in->isa_ptr() ? in->is_ptr()->_ptr : TypePtr::BotPTR;
483   //   // Preserve cast away nullness for interfaces
484   //   if( in_ptr == TypePtr::NotNull && my_oop && my_oop->_ptr == TypePtr::BotPTR ) {
485   //     return my_oop->cast_to_ptr_type(TypePtr::NotNull);
486   //   }
487   //   return _type;
488   // }
489   //
490   // // Neither the input nor the destination type is an interface,
491   //
492   // // history: JOIN used to cause weird corner case bugs
493   // //          return (in == TypeOopPtr::NULL_PTR) ? in : _type;
494   // // JOIN picks up NotNull in common instance-of/check-cast idioms, both oops.
495   // // JOIN does not preserve NotNull in other cases, e.g. RawPtr vs InstPtr
496   // const Type *join = in->join(_type);
497   // // Check if join preserved NotNull'ness for pointers
498   // if( join->isa_ptr() && _type->isa_ptr() ) {
499   //   TypePtr::PTR join_ptr = join->is_ptr()->_ptr;
500   //   TypePtr::PTR type_ptr = _type->is_ptr()->_ptr;
501   //   // If there isn't any NotNull'ness to preserve
502   //   // OR if join preserved NotNull'ness then return it
503   //   if( type_ptr == TypePtr::BotPTR  || type_ptr == TypePtr::Null ||
504   //       join_ptr == TypePtr::NotNull || join_ptr == TypePtr::Constant ) {
505   //     return join;
506   //   }
507   //   // ELSE return same old type as before
508   //   return _type;
509   // }
510   // // Not joining two pointers
511   // return join;
512 }
513 
514 //=============================================================================
515 //------------------------------Value------------------------------------------
516 const Type* CastX2PNode::Value(PhaseGVN* phase) const {
517   const Type* t = phase->type(in(1));
518   if (t == Type::TOP) return Type::TOP;
519   if (t->base() == Type_X && t->singleton()) {
520     uintptr_t bits = (uintptr_t) t->is_intptr_t()->get_con();
521     if (bits == 0)   return TypePtr::NULL_PTR;
522     return TypeRawPtr::make((address) bits);
523   }
524   return CastX2PNode::bottom_type();
525 }
526 
527 //------------------------------Idealize---------------------------------------
528 static inline bool fits_in_int(const Type* t, bool but_not_min_int = false) {
529   if (t == Type::TOP)  return false;
530   const TypeX* tl = t->is_intptr_t();
531   jint lo = min_jint;
532   jint hi = max_jint;
533   if (but_not_min_int)  ++lo;  // caller wants to negate the value w/o overflow
534   return (tl->_lo >= lo) && (tl->_hi <= hi);
535 }
536 
537 static inline Node* addP_of_X2P(PhaseGVN *phase,
538                                 Node* base,
539                                 Node* dispX,
540                                 bool negate = false) {
541   if (negate) {
542     dispX = phase->transform(new SubXNode(phase->MakeConX(0), dispX));
543   }
544   return new AddPNode(phase->C->top(),
545                       phase->transform(new CastX2PNode(base)),
546                       dispX);
547 }
548 
549 Node *CastX2PNode::Ideal(PhaseGVN *phase, bool can_reshape) {
550   // convert CastX2P(AddX(x, y)) to AddP(CastX2P(x), y) if y fits in an int
551   int op = in(1)->Opcode();
552   Node* x;
553   Node* y;
554   switch (op) {
555     case Op_SubX:
556     x = in(1)->in(1);
557     // Avoid ideal transformations ping-pong between this and AddP for raw pointers.
558     if (phase->find_intptr_t_con(x, -1) == 0)
559     break;
560     y = in(1)->in(2);
561     if (fits_in_int(phase->type(y), true)) {
562       return addP_of_X2P(phase, x, y, true);
563     }
564     break;
565     case Op_AddX:
566     x = in(1)->in(1);
567     y = in(1)->in(2);
568     if (fits_in_int(phase->type(y))) {
569       return addP_of_X2P(phase, x, y);
570     }
571     if (fits_in_int(phase->type(x))) {
572       return addP_of_X2P(phase, y, x);
573     }
574     break;
575   }
576   return NULL;
577 }
578 
579 //------------------------------Identity---------------------------------------
580 Node* CastX2PNode::Identity(PhaseGVN* phase) {
581   if (in(1)->Opcode() == Op_CastP2X)  return in(1)->in(1);
582   return this;
583 }
584 
585 //=============================================================================
586 //------------------------------Value------------------------------------------
587 const Type* CastP2XNode::Value(PhaseGVN* phase) const {
588   const Type* t = phase->type(in(1));
589   if (t == Type::TOP) return Type::TOP;
590   if (t->base() == Type::RawPtr && t->singleton()) {
591     uintptr_t bits = (uintptr_t) t->is_rawptr()->get_con();
592     return TypeX::make(bits);
593   }
594 
595   if (t->is_zero_type() || !t->maybe_null()) {
596     for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
597       Node* u = fast_out(i);
598       if (u->Opcode() == Op_OrL) {
599         for (DUIterator_Fast jmax, j = u->fast_outs(jmax); j < jmax; j++) {
600           Node* cmp = u->fast_out(j);
601           if (cmp->Opcode() == Op_CmpL) {
602             // Give CmpL a chance to get optimized
603             phase->record_for_igvn(cmp);
604           }
605         }
606       }
607     }
608   }
609 
610   return CastP2XNode::bottom_type();
611 }
612 
613 Node *CastP2XNode::Ideal(PhaseGVN *phase, bool can_reshape) {
614   return (in(0) && remove_dead_region(phase, can_reshape)) ? this : NULL;
615 }
616 
617 //------------------------------Identity---------------------------------------
618 Node* CastP2XNode::Identity(PhaseGVN* phase) {
619   if (in(1)->Opcode() == Op_CastX2P)  return in(1)->in(1);
620   return this;
621 }
622 
623 Node* ConstraintCastNode::make_cast_for_type(Node* c, Node* in, const Type* type, DependencyType dependency) {
624   Node* cast= NULL;
625   if (type->isa_int()) {
626     cast = make_cast(Op_CastII, c, in, type, dependency);
627   } else if (type->isa_long()) {
628     cast = make_cast(Op_CastLL, c, in, type, dependency);
629   } else if (type->isa_float()) {
630     cast = make_cast(Op_CastFF, c, in, type, dependency);
631   } else if (type->isa_double()) {
632     cast = make_cast(Op_CastDD, c, in, type, dependency);
633   } else if (type->isa_vect()) {
634     cast = make_cast(Op_CastVV, c, in, type, dependency);
635   } else if (type->isa_ptr()) {
636     cast = make_cast(Op_CastPP, c, in, type, dependency);
637   }
638   return cast;
639 }
640 
641 Node* ConstraintCastNode::optimize_integer_cast(PhaseGVN* phase, BasicType bt) {
642   PhaseIterGVN *igvn = phase->is_IterGVN();
643   const TypeInteger* this_type = this->type()->is_integer(bt);
644   Node* z = in(1);
645   const TypeInteger* rx = NULL;
646   const TypeInteger* ry = NULL;
647   // Similar to ConvI2LNode::Ideal() for the same reasons
648   if (Compile::push_thru_add(phase, z, this_type, rx, ry, bt, bt)) {
649     if (igvn == NULL) {
650       // Postpone this optimization to iterative GVN, where we can handle deep
651       // AddI chains without an exponential number of recursive Ideal() calls.
652       phase->record_for_igvn(this);
653       return NULL;
654     }
655     int op = z->Opcode();
656     Node* x = z->in(1);
657     Node* y = z->in(2);
658 
659     Node* cx = find_or_make_integer_cast(igvn, x, in(0), rx, _dependency, bt);
660     Node* cy = find_or_make_integer_cast(igvn, y, in(0), ry, _dependency, bt);
661     if (op == Op_Add(bt)) {
662       return AddNode::make(cx, cy, bt);
663     } else {
664       assert(op == Op_Sub(bt), "");
665       return SubNode::make(cx, cy, bt);
666     }
667     return NULL;
668   }
669   return NULL;
670 }
671 
672 const Type* ConstraintCastNode::widen_type(const PhaseGVN* phase, const Type* res, BasicType bt) const {
673   if (!phase->C->post_loop_opts_phase()) {
674     return res;
675   }
676   const TypeInteger* this_type = res->is_integer(bt);
677   const TypeInteger* in_type = phase->type(in(1))->isa_integer(bt);
678   if (in_type != NULL &&
679       (in_type->lo_as_long() != this_type->lo_as_long() ||
680        in_type->hi_as_long() != this_type->hi_as_long())) {
681     jlong lo1 = this_type->lo_as_long();
682     jlong hi1 = this_type->hi_as_long();
683     int w1 = this_type->_widen;
684     if (lo1 >= 0) {
685       // Keep a range assertion of >=0.
686       lo1 = 0;        hi1 = max_signed_integer(bt);
687     } else if (hi1 < 0) {
688       // Keep a range assertion of <0.
689       lo1 = min_signed_integer(bt); hi1 = -1;
690     } else {
691       lo1 = min_signed_integer(bt); hi1 = max_signed_integer(bt);
692     }
693     return TypeInteger::make(MAX2(in_type->lo_as_long(), lo1),
694                              MIN2(in_type->hi_as_long(), hi1),
695                              MAX2((int)in_type->_widen, w1), bt);
696   }
697   return res;
698 }