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 "opto/addnode.hpp"
 26 #include "opto/callnode.hpp"
 27 #include "opto/castnode.hpp"
 28 #include "opto/connode.hpp"


 29 #include "opto/matcher.hpp"
 30 #include "opto/phaseX.hpp"

 31 #include "opto/subnode.hpp"
 32 #include "opto/type.hpp"
 33 #include "castnode.hpp"
 34 #include "utilities/checkedCast.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   if (_dependency == UnconditionalDependency) {
 40     return this;
 41   }
 42   Node* dom = dominating_cast(phase, phase);
 43   if (dom != nullptr) {
 44     return dom;
 45   }
 46   return higher_equal_types(phase, in(1)) ? 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 
 54   const Type* in_type = phase->type(in(1));
 55   const Type* ft = in_type->filter_speculative(_type);
 56 
 57   // Check if both _type and in_type had a speculative type, but for the just
 58   // computed ft the speculative type was dropped.
 59   if (ft->speculative() == nullptr &&
 60       _type->speculative() != nullptr &&
 61       in_type->speculative() != nullptr) {
 62     // Speculative type may have disagreed between cast and input, and was
 63     // dropped in filtering. Recompute so that ft can take speculative type
 64     // of in_type. If we did not do it now, a subsequent ::Value call would
 65     // do it, and violate idempotence of ::Value.
 66     ft = in_type->filter_speculative(ft);
 67   }
 68 
 69 #ifdef ASSERT
 70   // Previous versions of this function had some special case logic,
 71   // which is no longer necessary.  Make sure of the required effects.
 72   switch (Opcode()) {
 73     case Op_CastII:
 74     {
 75       if (in_type == Type::TOP) {
 76         assert(ft == Type::TOP, "special case #1");
 77       }
 78       const Type* rt = in_type->join_speculative(_type);
 79       if (rt->empty()) {
 80         assert(ft == Type::TOP, "special case #2");
 81       }
 82       break;
 83     }
 84     case Op_CastPP:
 85     if (in_type == TypePtr::NULL_PTR &&
 86         _type->isa_ptr() && _type->is_ptr()->_ptr == TypePtr::NotNull) {
 87       assert(ft == Type::TOP, "special case #3");
 88       break;
 89     }
 90   }
 91 #endif //ASSERT
 92 
 93   return ft;
 94 }
 95 
 96 //------------------------------Ideal------------------------------------------
 97 // Return a node which is more "ideal" than the current node.  Strip out
 98 // control copies
 99 Node *ConstraintCastNode::Ideal(PhaseGVN *phase, bool can_reshape) {
100   return (in(0) && remove_dead_region(phase, can_reshape)) ? this : nullptr;

















101 }
102 
103 uint ConstraintCastNode::hash() const {
104   return TypeNode::hash() + (int)_dependency + (_extra_types != nullptr ? _extra_types->hash() : 0);
105 }
106 
107 bool ConstraintCastNode::cmp(const Node &n) const {
108   if (!TypeNode::cmp(n)) {
109     return false;
110   }
111   ConstraintCastNode& cast = (ConstraintCastNode&) n;
112   if (cast._dependency != _dependency) {
113     return false;
114   }
115   if (_extra_types == nullptr || cast._extra_types == nullptr) {
116     return _extra_types == cast._extra_types;
117   }
118   return _extra_types->eq(cast._extra_types);
119 }
120 
121 uint ConstraintCastNode::size_of() const {
122   return sizeof(*this);
123 }
124 
125 Node* ConstraintCastNode::make_cast_for_basic_type(Node* c, Node* n, const Type* t, DependencyType dependency, BasicType bt) {
126   switch(bt) {
127   case T_INT:
128     return new CastIINode(c, n, t, dependency);
129   case T_LONG:
130     return new CastLLNode(c, n, t, dependency);
131   default:
132     fatal("Bad basic type %s", type2name(bt));
133   }
134   return nullptr;
135 }
136 
137 TypeNode* ConstraintCastNode::dominating_cast(PhaseGVN* gvn, PhaseTransform* pt) const {
138   if (_dependency == UnconditionalDependency) {
139     return nullptr;
140   }
141   Node* val = in(1);
142   Node* ctl = in(0);
143   int opc = Opcode();
144   if (ctl == nullptr) {
145     return nullptr;
146   }
147   // Range check CastIIs may all end up under a single range check and
148   // in that case only the narrower CastII would be kept by the code
149   // below which would be incorrect.
150   if (is_CastII() && as_CastII()->has_range_check()) {
151     return nullptr;
152   }
153   if (type()->isa_rawptr() && (gvn->type_or_null(val) == nullptr || gvn->type(val)->isa_oopptr())) {
154     return nullptr;
155   }
156   for (DUIterator_Fast imax, i = val->fast_outs(imax); i < imax; i++) {
157     Node* u = val->fast_out(i);
158     if (u != this &&
159         u->outcnt() > 0 &&
160         u->Opcode() == opc &&
161         u->in(0) != nullptr &&
162         higher_equal_types(gvn, u)) {
163       if (pt->is_dominator(u->in(0), ctl)) {
164         return u->as_Type();
165       }
166       if (is_CheckCastPP() && u->in(1)->is_Proj() && u->in(1)->in(0)->is_Allocate() &&
167           u->in(0)->is_Proj() && u->in(0)->in(0)->is_Initialize() &&
168           u->in(1)->in(0)->as_Allocate()->initialization() == u->in(0)->in(0)) {
169         // CheckCastPP following an allocation always dominates all
170         // use of the allocation result
171         return u->as_Type();
172       }
173     }
174   }
175   return nullptr;
176 }
177 
178 bool ConstraintCastNode::higher_equal_types(PhaseGVN* phase, const Node* other) const {
179   const Type* t = phase->type(other);
180   if (!t->higher_equal_speculative(type())) {
181     return false;
182   }
183   if (_extra_types != nullptr) {
184     for (uint i = 0; i < _extra_types->cnt(); ++i) {
185       if (!t->higher_equal_speculative(_extra_types->field_at(i))) {
186         return false;
187       }
188     }
189   }
190   return true;
191 }
192 
193 #ifndef PRODUCT
194 void ConstraintCastNode::dump_spec(outputStream *st) const {
195   TypeNode::dump_spec(st);
196   if (_extra_types != nullptr) {
197     st->print(" extra types: ");
198     _extra_types->dump_on(st);
199   }
200   if (_dependency != RegularDependency) {
201     st->print(" %s dependency", _dependency == StrongDependency ? "strong" : "unconditional");
202   }
203 }
204 #endif
205 
206 const Type* CastIINode::Value(PhaseGVN* phase) const {
207   const Type *res = ConstraintCastNode::Value(phase);
208   if (res == Type::TOP) {
209     return Type::TOP;
210   }
211   assert(res->isa_int(), "res must be int");
212 
213   // Similar to ConvI2LNode::Value() for the same reasons
214   // see if we can remove type assertion after loop opts
215   res = widen_type(phase, res, T_INT);
216 
217   return res;
218 }
219 
220 Node* ConstraintCastNode::find_or_make_integer_cast(PhaseIterGVN* igvn, Node* parent, const TypeInteger* type) const {
221   Node* n = clone();
222   n->set_req(1, parent);
223   n->as_ConstraintCast()->set_type(type);
224   Node* existing = igvn->hash_find_insert(n);
225   if (existing != nullptr) {
226     n->destruct(igvn);
227     return existing;
228   }
229   return igvn->register_new_node_with_optimizer(n);
230 }
231 
232 Node *CastIINode::Ideal(PhaseGVN *phase, bool can_reshape) {
233   Node* progress = ConstraintCastNode::Ideal(phase, can_reshape);
234   if (progress != nullptr) {
235     return progress;
236   }
237   if (can_reshape && !phase->C->post_loop_opts_phase()) {
238     // makes sure we run ::Value to potentially remove type assertion after loop opts
239     phase->C->record_for_post_loop_opts_igvn(this);
240   }
241   if (!_range_check_dependency || phase->C->post_loop_opts_phase()) {
242     return optimize_integer_cast(phase, T_INT);
243   }
244   phase->C->record_for_post_loop_opts_igvn(this);
245   return nullptr;
246 }
247 
248 Node* CastIINode::Identity(PhaseGVN* phase) {
249   Node* progress = ConstraintCastNode::Identity(phase);
250   if (progress != this) {
251     return progress;
252   }
253   return this;
254 }
255 
256 bool CastIINode::cmp(const Node &n) const {
257   return ConstraintCastNode::cmp(n) && ((CastIINode&)n)._range_check_dependency == _range_check_dependency;
258 }
259 
260 uint CastIINode::size_of() const {
261   return sizeof(*this);
262 }
263 
264 #ifndef PRODUCT
265 void CastIINode::dump_spec(outputStream* st) const {
266   ConstraintCastNode::dump_spec(st);
267   if (_range_check_dependency) {
268     st->print(" range check dependency");
269   }
270 }
271 #endif
272 
273 CastIINode* CastIINode::pin_array_access_node() const {
274   assert(_dependency == RegularDependency, "already pinned");
275   if (has_range_check()) {
276     return new CastIINode(in(0), in(1), bottom_type(), StrongDependency, has_range_check());
277   }
278   return nullptr;
279 }
280 
281 void CastIINode::remove_range_check_cast(Compile* C) {
282   if (has_range_check()) {
283     // Range check CastII nodes feed into an address computation subgraph. Remove them to let that subgraph float freely.
284     // For memory access or integer divisions nodes that depend on the cast, record the dependency on the cast's control
285     // as a precedence edge, so they can't float above the cast in case that cast's narrowed type helped eliminate a
286     // range check or a null divisor check.
287     assert(in(0) != nullptr, "All RangeCheck CastII must have a control dependency");
288     ResourceMark rm;
289     Unique_Node_List wq;
290     wq.push(this);
291     for (uint next = 0; next < wq.size(); ++next) {
292       Node* m = wq.at(next);
293       for (DUIterator_Fast imax, i = m->fast_outs(imax); i < imax; i++) {
294         Node* use = m->fast_out(i);
295         if (use->is_Mem() || use->is_div_or_mod(T_INT) || use->is_div_or_mod(T_LONG)) {
296           use->ensure_control_or_add_prec(in(0));
297         } else if (!use->is_CFG() && !use->is_Phi()) {
298           wq.push(use);
299         }
300       }
301     }
302     subsume_by(in(1), C);
303     if (outcnt() == 0) {
304       disconnect_inputs(C);
305     }
306   }
307 }
308 
309 
310 const Type* CastLLNode::Value(PhaseGVN* phase) const {
311   const Type* res = ConstraintCastNode::Value(phase);
312   if (res == Type::TOP) {
313     return Type::TOP;
314   }
315   assert(res->isa_long(), "res must be long");
316 
317   return widen_type(phase, res, T_LONG);
318 }
319 
320 Node* CastLLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
321   Node* progress = ConstraintCastNode::Ideal(phase, can_reshape);
322   if (progress != nullptr) {
323     return progress;
324   }
325   if (!phase->C->post_loop_opts_phase()) {
326     // makes sure we run ::Value to potentially remove type assertion after loop opts
327     phase->C->record_for_post_loop_opts_igvn(this);
328   }
329   // transform (CastLL (ConvI2L ..)) into (ConvI2L (CastII ..)) if the type of the CastLL is narrower than the type of
330   // the ConvI2L.
331   Node* in1 = in(1);
332   if (in1 != nullptr && in1->Opcode() == Op_ConvI2L) {
333     const Type* t = Value(phase);
334     const Type* t_in = phase->type(in1);
335     if (t != Type::TOP && t_in != Type::TOP) {
336       const TypeLong* tl = t->is_long();
337       const TypeLong* t_in_l = t_in->is_long();
338       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");
339       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");
340       if (tl != t_in_l) {
341         const TypeInt* ti = TypeInt::make(checked_cast<jint>(tl->_lo), checked_cast<jint>(tl->_hi), tl->_widen);
342         Node* castii = phase->transform(new CastIINode(in(0), in1->in(1), ti));
343         Node* convi2l = in1->clone();
344         convi2l->set_req(1, castii);
345         return convi2l;
346       }
347     }
348   }
349   return optimize_integer_cast(phase, T_LONG);
350 }
351 










352 //------------------------------Value------------------------------------------
353 // Take 'join' of input and cast-up type, unless working with an Interface
354 const Type* CheckCastPPNode::Value(PhaseGVN* phase) const {
355   if( in(0) && phase->type(in(0)) == Type::TOP ) return Type::TOP;
356 
357   const Type *inn = phase->type(in(1));
358   if( inn == Type::TOP ) return Type::TOP;  // No information yet
359 
360   if (inn->isa_oopptr() && _type->isa_oopptr()) {
361     return ConstraintCastNode::Value(phase);
362   }
363 
364   const TypePtr *in_type = inn->isa_ptr();
365   const TypePtr *my_type = _type->isa_ptr();
366   const Type *result = _type;
367   if (in_type != nullptr && my_type != nullptr) {










368     TypePtr::PTR in_ptr = in_type->ptr();
369     if (in_ptr == TypePtr::Null) {
370       result = in_type;
371     } else if (in_ptr != TypePtr::Constant) {
372       result =  my_type->cast_to_ptr_type(my_type->join_ptr(in_ptr));
373     }
374   }
375 
376   return result;
377 }
378 
379 //=============================================================================
380 //------------------------------Value------------------------------------------
381 const Type* CastX2PNode::Value(PhaseGVN* phase) const {
382   const Type* t = phase->type(in(1));
383   if (t == Type::TOP) return Type::TOP;
384   if (t->base() == Type_X && t->singleton()) {
385     uintptr_t bits = (uintptr_t) t->is_intptr_t()->get_con();
386     if (bits == 0)   return TypePtr::NULL_PTR;
387     return TypeRawPtr::make((address) bits);
388   }
389   return CastX2PNode::bottom_type();
390 }
391 
392 //------------------------------Idealize---------------------------------------
393 static inline bool fits_in_int(const Type* t, bool but_not_min_int = false) {
394   if (t == Type::TOP)  return false;
395   const TypeX* tl = t->is_intptr_t();
396   jint lo = min_jint;
397   jint hi = max_jint;
398   if (but_not_min_int)  ++lo;  // caller wants to negate the value w/o overflow
399   return (tl->_lo >= lo) && (tl->_hi <= hi);
400 }
401 
402 static inline Node* addP_of_X2P(PhaseGVN *phase,
403                                 Node* base,
404                                 Node* dispX,
405                                 bool negate = false) {
406   if (negate) {
407     dispX = phase->transform(new SubXNode(phase->MakeConX(0), dispX));
408   }
409   return new AddPNode(phase->C->top(),
410                       phase->transform(new CastX2PNode(base)),
411                       dispX);
412 }
413 
414 Node *CastX2PNode::Ideal(PhaseGVN *phase, bool can_reshape) {
415   // convert CastX2P(AddX(x, y)) to AddP(CastX2P(x), y) if y fits in an int
416   int op = in(1)->Opcode();
417   Node* x;
418   Node* y;
419   switch (op) {
420     case Op_SubX:
421     x = in(1)->in(1);
422     // Avoid ideal transformations ping-pong between this and AddP for raw pointers.
423     if (phase->find_intptr_t_con(x, -1) == 0)
424     break;
425     y = in(1)->in(2);
426     if (fits_in_int(phase->type(y), true)) {
427       return addP_of_X2P(phase, x, y, true);
428     }
429     break;
430     case Op_AddX:
431     x = in(1)->in(1);
432     y = in(1)->in(2);
433     if (fits_in_int(phase->type(y))) {
434       return addP_of_X2P(phase, x, y);
435     }
436     if (fits_in_int(phase->type(x))) {
437       return addP_of_X2P(phase, y, x);
438     }
439     break;
440   }
441   return nullptr;
442 }
443 
444 //------------------------------Identity---------------------------------------
445 Node* CastX2PNode::Identity(PhaseGVN* phase) {
446   if (in(1)->Opcode() == Op_CastP2X)  return in(1)->in(1);
447   return this;
448 }
449 
450 //=============================================================================
451 //------------------------------Value------------------------------------------
452 const Type* CastP2XNode::Value(PhaseGVN* phase) const {
453   const Type* t = phase->type(in(1));
454   if (t == Type::TOP) return Type::TOP;
455   if (t->base() == Type::RawPtr && t->singleton()) {
456     uintptr_t bits = (uintptr_t) t->is_rawptr()->get_con();
457     return TypeX::make(bits);
458   }
















459   return CastP2XNode::bottom_type();
460 }
461 
462 Node *CastP2XNode::Ideal(PhaseGVN *phase, bool can_reshape) {
463   return (in(0) && remove_dead_region(phase, can_reshape)) ? this : nullptr;
464 }
465 
466 //------------------------------Identity---------------------------------------
467 Node* CastP2XNode::Identity(PhaseGVN* phase) {
468   if (in(1)->Opcode() == Op_CastX2P)  return in(1)->in(1);
469   return this;
470 }
471 
472 Node* ConstraintCastNode::make_cast_for_type(Node* c, Node* in, const Type* type, DependencyType dependency,
473                                              const TypeTuple* types) {
474   if (type->isa_int()) {
475     return new CastIINode(c, in, type, dependency, false, types);
476   } else if (type->isa_long()) {
477     return new CastLLNode(c, in, type, dependency, types);
478   } else if (type->isa_float()) {
479     return new CastFFNode(c, in, type, dependency, types);
480   } else if (type->isa_double()) {
481     return new CastDDNode(c, in, type, dependency, types);
482   } else if (type->isa_vect()) {
483     return new CastVVNode(c, in, type, dependency, types);
484   } else if (type->isa_ptr()) {
485     return new CastPPNode(c, in, type, dependency, types);
486   }
487   fatal("unreachable. Invalid cast type.");
488   return nullptr;
489 }
490 
491 Node* ConstraintCastNode::optimize_integer_cast(PhaseGVN* phase, BasicType bt) {
492   PhaseIterGVN *igvn = phase->is_IterGVN();
493   const TypeInteger* this_type = this->type()->is_integer(bt);
494   Node* z = in(1);
495   const TypeInteger* rx = nullptr;
496   const TypeInteger* ry = nullptr;
497   // Similar to ConvI2LNode::Ideal() for the same reasons
498   if (Compile::push_thru_add(phase, z, this_type, rx, ry, bt, bt)) {
499     if (igvn == nullptr) {
500       // Postpone this optimization to iterative GVN, where we can handle deep
501       // AddI chains without an exponential number of recursive Ideal() calls.
502       phase->record_for_igvn(this);
503       return nullptr;
504     }
505     int op = z->Opcode();
506     Node* x = z->in(1);
507     Node* y = z->in(2);
508 
509     Node* cx = find_or_make_integer_cast(igvn, x, rx);
510     Node* cy = find_or_make_integer_cast(igvn, y, ry);
511     if (op == Op_Add(bt)) {
512       return AddNode::make(cx, cy, bt);
513     } else {
514       assert(op == Op_Sub(bt), "");
515       return SubNode::make(cx, cy, bt);
516     }
517     return nullptr;
518   }
519   return nullptr;
520 }
521 
522 const Type* ConstraintCastNode::widen_type(const PhaseGVN* phase, const Type* res, BasicType bt) const {
523   if (!phase->C->post_loop_opts_phase()) {
524     return res;
525   }
526   const TypeInteger* this_type = res->is_integer(bt);
527   const TypeInteger* in_type = phase->type(in(1))->isa_integer(bt);
528   if (in_type != nullptr &&
529       (in_type->lo_as_long() != this_type->lo_as_long() ||
530        in_type->hi_as_long() != this_type->hi_as_long())) {
531     jlong lo1 = this_type->lo_as_long();
532     jlong hi1 = this_type->hi_as_long();
533     int w1 = this_type->_widen;
534     if (lo1 >= 0) {
535       // Keep a range assertion of >=0.
536       lo1 = 0;        hi1 = max_signed_integer(bt);
537     } else if (hi1 < 0) {
538       // Keep a range assertion of <0.
539       lo1 = min_signed_integer(bt); hi1 = -1;
540     } else {
541       lo1 = min_signed_integer(bt); hi1 = max_signed_integer(bt);
542     }
543     return TypeInteger::make(MAX2(in_type->lo_as_long(), lo1),
544                              MIN2(in_type->hi_as_long(), hi1),
545                              MAX2((int)in_type->_widen, w1), bt);
546   }
547   return res;
548 }
--- EOF ---