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