1 /*
  2  * Copyright (c) 2014, 2025, 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 "castnode.hpp"
 26 #include "opto/addnode.hpp"
 27 #include "opto/callnode.hpp"
 28 #include "opto/castnode.hpp"
 29 #include "opto/cfgnode.hpp"
 30 #include "opto/connode.hpp"
 31 #include "opto/loopnode.hpp"
 32 #include "opto/matcher.hpp"
 33 #include "opto/phaseX.hpp"
 34 #include "opto/subnode.hpp"
 35 #include "opto/type.hpp"
 36 #include "utilities/checkedCast.hpp"
 37 
 38 //=============================================================================
 39 // If input is already higher or equal to cast type, then this is an identity.
 40 Node* ConstraintCastNode::Identity(PhaseGVN* phase) {
 41   if (_dependency == UnconditionalDependency) {
 42     return this;
 43   }
 44   Node* dom = dominating_cast(phase, phase);
 45   if (dom != nullptr) {
 46     return dom;
 47   }
 48   return higher_equal_types(phase, in(1)) ? in(1) : this;
 49 }
 50 
 51 //------------------------------Value------------------------------------------
 52 // Take 'join' of input and cast-up type
 53 const Type* ConstraintCastNode::Value(PhaseGVN* phase) const {
 54   if (in(0) && phase->type(in(0)) == Type::TOP) return Type::TOP;
 55 
 56   const Type* in_type = phase->type(in(1));
 57   const Type* ft = in_type->filter_speculative(_type);
 58 
 59   // Check if both _type and in_type had a speculative type, but for the just
 60   // computed ft the speculative type was dropped.
 61   if (ft->speculative() == nullptr &&
 62       _type->speculative() != nullptr &&
 63       in_type->speculative() != nullptr) {
 64     // Speculative type may have disagreed between cast and input, and was
 65     // dropped in filtering. Recompute so that ft can take speculative type
 66     // of in_type. If we did not do it now, a subsequent ::Value call would
 67     // do it, and violate idempotence of ::Value.
 68     ft = in_type->filter_speculative(ft);
 69   }
 70 
 71 #ifdef ASSERT
 72   // Previous versions of this function had some special case logic,
 73   // which is no longer necessary.  Make sure of the required effects.
 74   switch (Opcode()) {
 75     case Op_CastII:
 76     {
 77       if (in_type == Type::TOP) {
 78         assert(ft == Type::TOP, "special case #1");
 79       }
 80       const Type* rt = in_type->join_speculative(_type);
 81       if (rt->empty()) {
 82         assert(ft == Type::TOP, "special case #2");
 83       }
 84       break;
 85     }
 86     case Op_CastPP:
 87     if (in_type == TypePtr::NULL_PTR &&
 88         _type->isa_ptr() && _type->is_ptr()->_ptr == TypePtr::NotNull) {
 89       assert(ft == Type::TOP, "special case #3");
 90       break;
 91     }
 92   }
 93 #endif //ASSERT
 94 
 95   return ft;
 96 }
 97 
 98 //------------------------------Ideal------------------------------------------
 99 // Return a node which is more "ideal" than the current node.  Strip out
100 // control copies
101 Node* ConstraintCastNode::Ideal(PhaseGVN* phase, bool can_reshape) {
102   if (in(0) != nullptr && remove_dead_region(phase, can_reshape)) {
103     return this;
104   }
105   if (in(1) != nullptr && phase->type(in(1)) != Type::TOP) {
106     return TypeNode::Ideal(phase, can_reshape);
107   }
108   return nullptr;
109 }
110 
111 uint ConstraintCastNode::hash() const {
112   return TypeNode::hash() + (int)_dependency + (_extra_types != nullptr ? _extra_types->hash() : 0);
113 }
114 
115 bool ConstraintCastNode::cmp(const Node &n) const {
116   if (!TypeNode::cmp(n)) {
117     return false;
118   }
119   ConstraintCastNode& cast = (ConstraintCastNode&) n;
120   if (cast._dependency != _dependency) {
121     return false;
122   }
123   if (_extra_types == nullptr || cast._extra_types == nullptr) {
124     return _extra_types == cast._extra_types;
125   }
126   return _extra_types->eq(cast._extra_types);
127 }
128 
129 uint ConstraintCastNode::size_of() const {
130   return sizeof(*this);
131 }
132 
133 Node* ConstraintCastNode::make_cast_for_basic_type(Node* c, Node* n, const Type* t, DependencyType dependency, BasicType bt) {
134   switch(bt) {
135   case T_INT:
136     return new CastIINode(c, n, t, dependency);
137   case T_LONG:
138     return new CastLLNode(c, n, t, dependency);
139   default:
140     fatal("Bad basic type %s", type2name(bt));
141   }
142   return nullptr;
143 }
144 
145 TypeNode* ConstraintCastNode::dominating_cast(PhaseGVN* gvn, PhaseTransform* pt) const {
146   if (_dependency == UnconditionalDependency) {
147     return nullptr;
148   }
149   Node* val = in(1);
150   Node* ctl = in(0);
151   int opc = Opcode();
152   if (ctl == nullptr) {
153     return nullptr;
154   }
155   // Range check CastIIs may all end up under a single range check and
156   // in that case only the narrower CastII would be kept by the code
157   // below which would be incorrect.
158   if (is_CastII() && as_CastII()->has_range_check()) {
159     return nullptr;
160   }
161   if (type()->isa_rawptr() && (gvn->type_or_null(val) == nullptr || gvn->type(val)->isa_oopptr())) {
162     return nullptr;
163   }
164   for (DUIterator_Fast imax, i = val->fast_outs(imax); i < imax; i++) {
165     Node* u = val->fast_out(i);
166     if (u != this &&
167         u->outcnt() > 0 &&
168         u->Opcode() == opc &&
169         u->in(0) != nullptr &&
170         higher_equal_types(gvn, u)) {
171       if (pt->is_dominator(u->in(0), ctl)) {
172         return u->as_Type();
173       }
174       if (is_CheckCastPP() && u->in(1)->is_Proj() && u->in(1)->in(0)->is_Allocate() &&
175           u->in(0)->is_Proj() && u->in(0)->in(0)->is_Initialize() &&
176           u->in(1)->in(0)->as_Allocate()->initialization() == u->in(0)->in(0)) {
177         // CheckCastPP following an allocation always dominates all
178         // use of the allocation result
179         return u->as_Type();
180       }
181     }
182   }
183   return nullptr;
184 }
185 
186 bool ConstraintCastNode::higher_equal_types(PhaseGVN* phase, const Node* other) const {
187   const Type* t = phase->type(other);
188   if (!t->higher_equal_speculative(type())) {
189     return false;
190   }
191   if (_extra_types != nullptr) {
192     for (uint i = 0; i < _extra_types->cnt(); ++i) {
193       if (!t->higher_equal_speculative(_extra_types->field_at(i))) {
194         return false;
195       }
196     }
197   }
198   return true;
199 }
200 
201 #ifndef PRODUCT
202 void ConstraintCastNode::dump_spec(outputStream *st) const {
203   TypeNode::dump_spec(st);
204   if (_extra_types != nullptr) {
205     st->print(" extra types: ");
206     _extra_types->dump_on(st);
207   }
208   if (_dependency != RegularDependency) {
209     st->print(" %s dependency", _dependency == StrongDependency ? "strong" : "unconditional");
210   }
211 }
212 #endif
213 
214 const Type* CastIINode::Value(PhaseGVN* phase) const {
215   const Type *res = ConstraintCastNode::Value(phase);
216   if (res == Type::TOP) {
217     return Type::TOP;
218   }
219   assert(res->isa_int(), "res must be int");
220 
221   // Similar to ConvI2LNode::Value() for the same reasons
222   // see if we can remove type assertion after loop opts
223   res = widen_type(phase, res, T_INT);
224 
225   return res;
226 }
227 
228 Node* ConstraintCastNode::find_or_make_integer_cast(PhaseIterGVN* igvn, Node* parent, const TypeInteger* type) const {
229   Node* n = clone();
230   n->set_req(1, parent);
231   n->as_ConstraintCast()->set_type(type);
232   Node* existing = igvn->hash_find_insert(n);
233   if (existing != nullptr) {
234     n->destruct(igvn);
235     return existing;
236   }
237   return igvn->register_new_node_with_optimizer(n);
238 }
239 
240 Node *CastIINode::Ideal(PhaseGVN *phase, bool can_reshape) {
241   Node* progress = ConstraintCastNode::Ideal(phase, can_reshape);
242   if (progress != nullptr) {
243     return progress;
244   }
245   if (can_reshape && !phase->C->post_loop_opts_phase()) {
246     // makes sure we run ::Value to potentially remove type assertion after loop opts
247     phase->C->record_for_post_loop_opts_igvn(this);
248   }
249   if (!_range_check_dependency || phase->C->post_loop_opts_phase()) {
250     return optimize_integer_cast(phase, T_INT);
251   }
252   phase->C->record_for_post_loop_opts_igvn(this);
253   return nullptr;
254 }
255 
256 Node* CastIINode::Identity(PhaseGVN* phase) {
257   Node* progress = ConstraintCastNode::Identity(phase);
258   if (progress != this) {
259     return progress;
260   }
261   return this;
262 }
263 
264 bool CastIINode::cmp(const Node &n) const {
265   return ConstraintCastNode::cmp(n) && ((CastIINode&)n)._range_check_dependency == _range_check_dependency;
266 }
267 
268 uint CastIINode::size_of() const {
269   return sizeof(*this);
270 }
271 
272 #ifndef PRODUCT
273 void CastIINode::dump_spec(outputStream* st) const {
274   ConstraintCastNode::dump_spec(st);
275   if (_range_check_dependency) {
276     st->print(" range check dependency");
277   }
278 }
279 #endif
280 
281 CastIINode* CastIINode::pin_array_access_node() const {
282   assert(_dependency == RegularDependency, "already pinned");
283   if (has_range_check()) {
284     return new CastIINode(in(0), in(1), bottom_type(), StrongDependency, has_range_check());
285   }
286   return nullptr;
287 }
288 
289 void CastIINode::remove_range_check_cast(Compile* C) {
290   if (has_range_check()) {
291     // Range check CastII nodes feed into an address computation subgraph. Remove them to let that subgraph float freely.
292     // For memory access or integer divisions nodes that depend on the cast, record the dependency on the cast's control
293     // as a precedence edge, so they can't float above the cast in case that cast's narrowed type helped eliminate a
294     // range check or a null divisor check.
295     assert(in(0) != nullptr, "All RangeCheck CastII must have a control dependency");
296     ResourceMark rm;
297     Unique_Node_List wq;
298     wq.push(this);
299     for (uint next = 0; next < wq.size(); ++next) {
300       Node* m = wq.at(next);
301       for (DUIterator_Fast imax, i = m->fast_outs(imax); i < imax; i++) {
302         Node* use = m->fast_out(i);
303         if (use->is_Mem() || use->is_div_or_mod(T_INT) || use->is_div_or_mod(T_LONG)) {
304           use->ensure_control_or_add_prec(in(0));
305         } else if (!use->is_CFG() && !use->is_Phi()) {
306           wq.push(use);
307         }
308       }
309     }
310     subsume_by(in(1), C);
311     if (outcnt() == 0) {
312       disconnect_inputs(C);
313     }
314   }
315 }
316 
317 
318 const Type* CastLLNode::Value(PhaseGVN* phase) const {
319   const Type* res = ConstraintCastNode::Value(phase);
320   if (res == Type::TOP) {
321     return Type::TOP;
322   }
323   assert(res->isa_long(), "res must be long");
324 
325   return widen_type(phase, res, T_LONG);
326 }
327 
328 bool CastLLNode::is_inner_loop_backedge(ProjNode* proj) {
329   if (proj != nullptr) {
330     Node* ctrl_use = proj->unique_ctrl_out_or_null();
331     if (ctrl_use != nullptr && ctrl_use->Opcode() == Op_Loop &&
332         ctrl_use->in(2) == proj &&
333         ctrl_use->as_Loop()->is_loop_nest_inner_loop()) {
334       return true;
335     }
336   }
337   return false;
338 }
339 
340 bool CastLLNode::cmp_used_at_inner_loop_exit_test(CmpNode* cmp) {
341   for (DUIterator_Fast imax, i = cmp->fast_outs(imax); i < imax; i++) {
342     Node* bol = cmp->fast_out(i);
343     if (bol->Opcode() == Op_Bool) {
344       for (DUIterator_Fast jmax, j = bol->fast_outs(jmax); j < jmax; j++) {
345         Node* iff = bol->fast_out(j);
346         if (iff->Opcode() == Op_If) {
347           ProjNode* true_proj = iff->as_If()->proj_out_or_null(true);
348           ProjNode* false_proj = iff->as_If()->proj_out_or_null(false);
349           if (is_inner_loop_backedge(true_proj) || is_inner_loop_backedge(false_proj)) {
350             return true;
351           }
352         }
353       }
354     }
355   }
356   return false;
357 }
358 
359 // Find if this is a cast node added by PhaseIdealLoop::create_loop_nest() to narrow the number of iterations of the
360 // inner loop
361 bool CastLLNode::used_at_inner_loop_exit_test() const {
362   for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
363     Node* convl2i = fast_out(i);
364     if (convl2i->Opcode() == Op_ConvL2I) {
365       for (DUIterator_Fast jmax, j = convl2i->fast_outs(jmax); j < jmax; j++) {
366         Node* cmp_or_sub = convl2i->fast_out(j);
367         if (cmp_or_sub->Opcode() == Op_CmpI) {
368           if (cmp_used_at_inner_loop_exit_test(cmp_or_sub->as_Cmp())) {
369             // (Loop .. .. (IfProj (If (Bool (CmpI (ConvL2I (CastLL )))))))
370             return true;
371           }
372         } else if (cmp_or_sub->Opcode() == Op_SubI && cmp_or_sub->in(1)->find_int_con(-1) == 0) {
373           for (DUIterator_Fast kmax, k = cmp_or_sub->fast_outs(kmax); k < kmax; k++) {
374             Node* cmp = cmp_or_sub->fast_out(k);
375             if (cmp->Opcode() == Op_CmpI) {
376               if (cmp_used_at_inner_loop_exit_test(cmp->as_Cmp())) {
377                 // (Loop .. .. (IfProj (If (Bool (CmpI (SubI 0 (ConvL2I (CastLL ))))))))
378                 return true;
379               }
380             }
381           }
382         }
383       }
384     }
385   }
386   return false;
387 }
388 
389 Node* CastLLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
390   Node* progress = ConstraintCastNode::Ideal(phase, can_reshape);
391   if (progress != nullptr) {
392     return progress;
393   }
394   if (!phase->C->post_loop_opts_phase()) {
395     // makes sure we run ::Value to potentially remove type assertion after loop opts
396     phase->C->record_for_post_loop_opts_igvn(this);
397   }
398   // transform (CastLL (ConvI2L ..)) into (ConvI2L (CastII ..)) if the type of the CastLL is narrower than the type of
399   // the ConvI2L.
400   Node* in1 = in(1);
401   if (in1 != nullptr && in1->Opcode() == Op_ConvI2L) {
402     const Type* t = Value(phase);
403     const Type* t_in = phase->type(in1);
404     if (t != Type::TOP && t_in != Type::TOP) {
405       const TypeLong* tl = t->is_long();
406       const TypeLong* t_in_l = t_in->is_long();
407       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");
408       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");
409       if (tl != t_in_l) {
410         const TypeInt* ti = TypeInt::make(checked_cast<jint>(tl->_lo), checked_cast<jint>(tl->_hi), tl->_widen);
411         Node* castii = phase->transform(new CastIINode(in(0), in1->in(1), ti));
412         Node* convi2l = in1->clone();
413         convi2l->set_req(1, castii);
414         return convi2l;
415       }
416     }
417   }
418   // If it's a cast created by PhaseIdealLoop::short_running_loop(), don't transform it until the counted loop is created
419   // in next loop opts pass
420   if (!can_reshape || !used_at_inner_loop_exit_test()) {
421     return optimize_integer_cast(phase, T_LONG);
422   }
423   return nullptr;
424 }
425 
426 //------------------------------Value------------------------------------------
427 // Take 'join' of input and cast-up type, unless working with an Interface
428 const Type* CheckCastPPNode::Value(PhaseGVN* phase) const {
429   if( in(0) && phase->type(in(0)) == Type::TOP ) return Type::TOP;
430 
431   const Type *inn = phase->type(in(1));
432   if( inn == Type::TOP ) return Type::TOP;  // No information yet
433 
434   if (inn->isa_oopptr() && _type->isa_oopptr()) {
435     return ConstraintCastNode::Value(phase);
436   }
437 
438   const TypePtr *in_type = inn->isa_ptr();
439   const TypePtr *my_type = _type->isa_ptr();
440   const Type *result = _type;
441   if (in_type != nullptr && my_type != nullptr) {
442     TypePtr::PTR in_ptr = in_type->ptr();
443     if (in_ptr == TypePtr::Null) {
444       result = in_type;
445     } else if (in_ptr != TypePtr::Constant) {
446       result =  my_type->cast_to_ptr_type(my_type->join_ptr(in_ptr));
447     }
448   }
449 
450   return result;
451 }
452 
453 //=============================================================================
454 //------------------------------Value------------------------------------------
455 const Type* CastX2PNode::Value(PhaseGVN* phase) const {
456   const Type* t = phase->type(in(1));
457   if (t == Type::TOP) return Type::TOP;
458   if (t->base() == Type_X && t->singleton()) {
459     uintptr_t bits = (uintptr_t) t->is_intptr_t()->get_con();
460     if (bits == 0)   return TypePtr::NULL_PTR;
461     return TypeRawPtr::make((address) bits);
462   }
463   return CastX2PNode::bottom_type();
464 }
465 
466 //------------------------------Idealize---------------------------------------
467 static inline bool fits_in_int(const Type* t, bool but_not_min_int = false) {
468   if (t == Type::TOP)  return false;
469   const TypeX* tl = t->is_intptr_t();
470   jint lo = min_jint;
471   jint hi = max_jint;
472   if (but_not_min_int)  ++lo;  // caller wants to negate the value w/o overflow
473   return (tl->_lo >= lo) && (tl->_hi <= hi);
474 }
475 
476 static inline Node* addP_of_X2P(PhaseGVN *phase,
477                                 Node* base,
478                                 Node* dispX,
479                                 bool negate = false) {
480   if (negate) {
481     dispX = phase->transform(new SubXNode(phase->MakeConX(0), dispX));
482   }
483   return new AddPNode(phase->C->top(),
484                       phase->transform(new CastX2PNode(base)),
485                       dispX);
486 }
487 
488 Node *CastX2PNode::Ideal(PhaseGVN *phase, bool can_reshape) {
489   // convert CastX2P(AddX(x, y)) to AddP(CastX2P(x), y) if y fits in an int
490   int op = in(1)->Opcode();
491   Node* x;
492   Node* y;
493   switch (op) {
494     case Op_SubX:
495     x = in(1)->in(1);
496     // Avoid ideal transformations ping-pong between this and AddP for raw pointers.
497     if (phase->find_intptr_t_con(x, -1) == 0)
498     break;
499     y = in(1)->in(2);
500     if (fits_in_int(phase->type(y), true)) {
501       return addP_of_X2P(phase, x, y, true);
502     }
503     break;
504     case Op_AddX:
505     x = in(1)->in(1);
506     y = in(1)->in(2);
507     if (fits_in_int(phase->type(y))) {
508       return addP_of_X2P(phase, x, y);
509     }
510     if (fits_in_int(phase->type(x))) {
511       return addP_of_X2P(phase, y, x);
512     }
513     break;
514   }
515   return nullptr;
516 }
517 
518 //------------------------------Identity---------------------------------------
519 Node* CastX2PNode::Identity(PhaseGVN* phase) {
520   if (in(1)->Opcode() == Op_CastP2X)  return in(1)->in(1);
521   return this;
522 }
523 
524 //=============================================================================
525 //------------------------------Value------------------------------------------
526 const Type* CastP2XNode::Value(PhaseGVN* phase) const {
527   const Type* t = phase->type(in(1));
528   if (t == Type::TOP) return Type::TOP;
529   if (t->base() == Type::RawPtr && t->singleton()) {
530     uintptr_t bits = (uintptr_t) t->is_rawptr()->get_con();
531     return TypeX::make(bits);
532   }
533   return CastP2XNode::bottom_type();
534 }
535 
536 Node *CastP2XNode::Ideal(PhaseGVN *phase, bool can_reshape) {
537   return (in(0) && remove_dead_region(phase, can_reshape)) ? this : nullptr;
538 }
539 
540 //------------------------------Identity---------------------------------------
541 Node* CastP2XNode::Identity(PhaseGVN* phase) {
542   if (in(1)->Opcode() == Op_CastX2P)  return in(1)->in(1);
543   return this;
544 }
545 
546 Node* ConstraintCastNode::make_cast_for_type(Node* c, Node* in, const Type* type, DependencyType dependency,
547                                              const TypeTuple* types) {
548   if (type->isa_int()) {
549     return new CastIINode(c, in, type, dependency, false, types);
550   } else if (type->isa_long()) {
551     return new CastLLNode(c, in, type, dependency, types);
552   } else if (type->isa_half_float()) {
553     return new CastHHNode(c, in, type, dependency, types);
554   } else if (type->isa_float()) {
555     return new CastFFNode(c, in, type, dependency, types);
556   } else if (type->isa_double()) {
557     return new CastDDNode(c, in, type, dependency, types);
558   } else if (type->isa_vect()) {
559     return new CastVVNode(c, in, type, dependency, types);
560   } else if (type->isa_ptr()) {
561     return new CastPPNode(c, in, type, dependency, types);
562   }
563   fatal("unreachable. Invalid cast type.");
564   return nullptr;
565 }
566 
567 Node* ConstraintCastNode::optimize_integer_cast(PhaseGVN* phase, BasicType bt) {
568   PhaseIterGVN *igvn = phase->is_IterGVN();
569   const TypeInteger* this_type = this->type()->isa_integer(bt);
570   if (this_type == nullptr) {
571     return nullptr;
572   }
573 
574   Node* z = in(1);
575   const TypeInteger* rx = nullptr;
576   const TypeInteger* ry = nullptr;
577   // Similar to ConvI2LNode::Ideal() for the same reasons
578   if (Compile::push_thru_add(phase, z, this_type, rx, ry, bt, bt)) {
579     if (igvn == nullptr) {
580       // Postpone this optimization to iterative GVN, where we can handle deep
581       // AddI chains without an exponential number of recursive Ideal() calls.
582       phase->record_for_igvn(this);
583       return nullptr;
584     }
585     int op = z->Opcode();
586     Node* x = z->in(1);
587     Node* y = z->in(2);
588 
589     Node* cx = find_or_make_integer_cast(igvn, x, rx);
590     Node* cy = find_or_make_integer_cast(igvn, y, ry);
591     if (op == Op_Add(bt)) {
592       return AddNode::make(cx, cy, bt);
593     } else {
594       assert(op == Op_Sub(bt), "");
595       return SubNode::make(cx, cy, bt);
596     }
597     return nullptr;
598   }
599   return nullptr;
600 }
601 
602 const Type* ConstraintCastNode::widen_type(const PhaseGVN* phase, const Type* res, BasicType bt) const {
603   if (!phase->C->post_loop_opts_phase()) {
604     return res;
605   }
606 
607   // At VerifyConstraintCasts == 1, we verify the ConstraintCastNodes that are present during code
608   // emission. This allows us detecting possible mis-scheduling due to these nodes being pinned at
609   // the wrong control nodes.
610   // At VerifyConstraintCasts == 2, we do not perform widening so that we can verify the
611   // correctness of more ConstraintCastNodes. This further helps us detect possible
612   // mis-transformations that may happen due to these nodes being pinned at the wrong control
613   // nodes.
614   if (VerifyConstraintCasts > 1) {
615     return res;
616   }
617 
618   const TypeInteger* this_type = res->is_integer(bt);
619   const TypeInteger* in_type = phase->type(in(1))->isa_integer(bt);
620   if (in_type != nullptr &&
621       (in_type->lo_as_long() != this_type->lo_as_long() ||
622        in_type->hi_as_long() != this_type->hi_as_long())) {
623     jlong lo1 = this_type->lo_as_long();
624     jlong hi1 = this_type->hi_as_long();
625     int w1 = this_type->_widen;
626     if (lo1 >= 0) {
627       // Keep a range assertion of >=0.
628       lo1 = 0;        hi1 = max_signed_integer(bt);
629     } else if (hi1 < 0) {
630       // Keep a range assertion of <0.
631       lo1 = min_signed_integer(bt); hi1 = -1;
632     } else {
633       lo1 = min_signed_integer(bt); hi1 = max_signed_integer(bt);
634     }
635     return TypeInteger::make(MAX2(in_type->lo_as_long(), lo1),
636                              MIN2(in_type->hi_as_long(), hi1),
637                              MAX2((int)in_type->_widen, w1), bt);
638   }
639   return res;
640 }