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