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   if (in(0) != nullptr && remove_dead_region(phase, can_reshape)) {
101     return this;
102   }
103   if (in(1) != nullptr && phase->type(in(1)) != Type::TOP) {
104     return TypeNode::Ideal(phase, can_reshape);
105   }
106   return nullptr;
107 }
108 
109 uint ConstraintCastNode::hash() const {
110   return TypeNode::hash() + (int)_dependency + (_extra_types != nullptr ? _extra_types->hash() : 0);
111 }
112 
113 bool ConstraintCastNode::cmp(const Node &n) const {
114   if (!TypeNode::cmp(n)) {
115     return false;
116   }
117   ConstraintCastNode& cast = (ConstraintCastNode&) n;
118   if (cast._dependency != _dependency) {
119     return false;
120   }
121   if (_extra_types == nullptr || cast._extra_types == nullptr) {
122     return _extra_types == cast._extra_types;
123   }
124   return _extra_types->eq(cast._extra_types);
125 }
126 
127 uint ConstraintCastNode::size_of() const {
128   return sizeof(*this);
129 }
130 
131 Node* ConstraintCastNode::make_cast_for_basic_type(Node* c, Node* n, const Type* t, DependencyType dependency, BasicType bt) {
132   switch(bt) {
133   case T_INT:
134     return new CastIINode(c, n, t, dependency);
135   case T_LONG:
136     return new CastLLNode(c, n, t, dependency);
137   default:
138     fatal("Bad basic type %s", type2name(bt));
139   }
140   return nullptr;
141 }
142 
143 TypeNode* ConstraintCastNode::dominating_cast(PhaseGVN* gvn, PhaseTransform* pt) const {
144   if (_dependency == UnconditionalDependency) {
145     return nullptr;
146   }
147   Node* val = in(1);
148   Node* ctl = in(0);
149   int opc = Opcode();
150   if (ctl == nullptr) {
151     return nullptr;
152   }
153   // Range check CastIIs may all end up under a single range check and
154   // in that case only the narrower CastII would be kept by the code
155   // below which would be incorrect.
156   if (is_CastII() && as_CastII()->has_range_check()) {
157     return nullptr;
158   }
159   if (type()->isa_rawptr() && (gvn->type_or_null(val) == nullptr || gvn->type(val)->isa_oopptr())) {
160     return nullptr;
161   }
162   for (DUIterator_Fast imax, i = val->fast_outs(imax); i < imax; i++) {
163     Node* u = val->fast_out(i);
164     if (u != this &&
165         u->outcnt() > 0 &&
166         u->Opcode() == opc &&
167         u->in(0) != nullptr &&
168         higher_equal_types(gvn, u)) {
169       if (pt->is_dominator(u->in(0), ctl)) {
170         return u->as_Type();
171       }
172       if (is_CheckCastPP() && u->in(1)->is_Proj() && u->in(1)->in(0)->is_Allocate() &&
173           u->in(0)->is_Proj() && u->in(0)->in(0)->is_Initialize() &&
174           u->in(1)->in(0)->as_Allocate()->initialization() == u->in(0)->in(0)) {
175         // CheckCastPP following an allocation always dominates all
176         // use of the allocation result
177         return u->as_Type();
178       }
179     }
180   }
181   return nullptr;
182 }
183 
184 bool ConstraintCastNode::higher_equal_types(PhaseGVN* phase, const Node* other) const {
185   const Type* t = phase->type(other);
186   if (!t->higher_equal_speculative(type())) {
187     return false;
188   }
189   if (_extra_types != nullptr) {
190     for (uint i = 0; i < _extra_types->cnt(); ++i) {
191       if (!t->higher_equal_speculative(_extra_types->field_at(i))) {
192         return false;
193       }
194     }
195   }
196   return true;
197 }
198 
199 #ifndef PRODUCT
200 void ConstraintCastNode::dump_spec(outputStream *st) const {
201   TypeNode::dump_spec(st);
202   if (_extra_types != nullptr) {
203     st->print(" extra types: ");
204     _extra_types->dump_on(st);
205   }
206   if (_dependency != RegularDependency) {
207     st->print(" %s dependency", _dependency == StrongDependency ? "strong" : "unconditional");
208   }
209 }
210 #endif
211 
212 const Type* CastIINode::Value(PhaseGVN* phase) const {
213   const Type *res = ConstraintCastNode::Value(phase);
214   if (res == Type::TOP) {
215     return Type::TOP;
216   }
217   assert(res->isa_int(), "res must be int");
218 
219   // Similar to ConvI2LNode::Value() for the same reasons
220   // see if we can remove type assertion after loop opts
221   res = widen_type(phase, res, T_INT);
222 
223   return res;
224 }
225 
226 Node* ConstraintCastNode::find_or_make_integer_cast(PhaseIterGVN* igvn, Node* parent, const TypeInteger* type) const {
227   Node* n = clone();
228   n->set_req(1, parent);
229   n->as_ConstraintCast()->set_type(type);
230   Node* existing = igvn->hash_find_insert(n);
231   if (existing != nullptr) {
232     n->destruct(igvn);
233     return existing;
234   }
235   return igvn->register_new_node_with_optimizer(n);
236 }
237 
238 Node *CastIINode::Ideal(PhaseGVN *phase, bool can_reshape) {
239   Node* progress = ConstraintCastNode::Ideal(phase, can_reshape);
240   if (progress != nullptr) {
241     return progress;
242   }
243   if (can_reshape && !phase->C->post_loop_opts_phase()) {
244     // makes sure we run ::Value to potentially remove type assertion after loop opts
245     phase->C->record_for_post_loop_opts_igvn(this);
246   }
247   if (!_range_check_dependency || phase->C->post_loop_opts_phase()) {
248     return optimize_integer_cast(phase, T_INT);
249   }
250   phase->C->record_for_post_loop_opts_igvn(this);
251   return nullptr;
252 }
253 
254 Node* CastIINode::Identity(PhaseGVN* phase) {
255   Node* progress = ConstraintCastNode::Identity(phase);
256   if (progress != this) {
257     return progress;
258   }
259   return this;
260 }
261 
262 bool CastIINode::cmp(const Node &n) const {
263   return ConstraintCastNode::cmp(n) && ((CastIINode&)n)._range_check_dependency == _range_check_dependency;
264 }
265 
266 uint CastIINode::size_of() const {
267   return sizeof(*this);
268 }
269 
270 #ifndef PRODUCT
271 void CastIINode::dump_spec(outputStream* st) const {
272   ConstraintCastNode::dump_spec(st);
273   if (_range_check_dependency) {
274     st->print(" range check dependency");
275   }
276 }
277 #endif
278 
279 CastIINode* CastIINode::pin_array_access_node() const {
280   assert(_dependency == RegularDependency, "already pinned");
281   if (has_range_check()) {
282     return new CastIINode(in(0), in(1), bottom_type(), StrongDependency, has_range_check());
283   }
284   return nullptr;
285 }
286 
287 void CastIINode::remove_range_check_cast(Compile* C) {
288   if (has_range_check()) {
289     // Range check CastII nodes feed into an address computation subgraph. Remove them to let that subgraph float freely.
290     // For memory access or integer divisions nodes that depend on the cast, record the dependency on the cast's control
291     // as a precedence edge, so they can't float above the cast in case that cast's narrowed type helped eliminate a
292     // range check or a null divisor check.
293     assert(in(0) != nullptr, "All RangeCheck CastII must have a control dependency");
294     ResourceMark rm;
295     Unique_Node_List wq;
296     wq.push(this);
297     for (uint next = 0; next < wq.size(); ++next) {
298       Node* m = wq.at(next);
299       for (DUIterator_Fast imax, i = m->fast_outs(imax); i < imax; i++) {
300         Node* use = m->fast_out(i);
301         if (use->is_Mem() || use->is_div_or_mod(T_INT) || use->is_div_or_mod(T_LONG)) {
302           use->ensure_control_or_add_prec(in(0));
303         } else if (!use->is_CFG() && !use->is_Phi()) {
304           wq.push(use);
305         }
306       }
307     }
308     subsume_by(in(1), C);
309     if (outcnt() == 0) {
310       disconnect_inputs(C);
311     }
312   }
313 }
314 
315 
316 const Type* CastLLNode::Value(PhaseGVN* phase) const {
317   const Type* res = ConstraintCastNode::Value(phase);
318   if (res == Type::TOP) {
319     return Type::TOP;
320   }
321   assert(res->isa_long(), "res must be long");
322 
323   return widen_type(phase, res, T_LONG);
324 }
325 
326 Node* CastLLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
327   Node* progress = ConstraintCastNode::Ideal(phase, can_reshape);
328   if (progress != nullptr) {
329     return progress;
330   }
331   if (!phase->C->post_loop_opts_phase()) {
332     // makes sure we run ::Value to potentially remove type assertion after loop opts
333     phase->C->record_for_post_loop_opts_igvn(this);
334   }
335   // transform (CastLL (ConvI2L ..)) into (ConvI2L (CastII ..)) if the type of the CastLL is narrower than the type of
336   // the ConvI2L.
337   Node* in1 = in(1);
338   if (in1 != nullptr && in1->Opcode() == Op_ConvI2L) {
339     const Type* t = Value(phase);
340     const Type* t_in = phase->type(in1);
341     if (t != Type::TOP && t_in != Type::TOP) {
342       const TypeLong* tl = t->is_long();
343       const TypeLong* t_in_l = t_in->is_long();
344       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");
345       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");
346       if (tl != t_in_l) {
347         const TypeInt* ti = TypeInt::make(checked_cast<jint>(tl->_lo), checked_cast<jint>(tl->_hi), tl->_widen);
348         Node* castii = phase->transform(new CastIINode(in(0), in1->in(1), ti));
349         Node* convi2l = in1->clone();
350         convi2l->set_req(1, castii);
351         return convi2l;
352       }
353     }
354   }
355   return optimize_integer_cast(phase, T_LONG);
356 }
357 
358 //------------------------------Value------------------------------------------
359 // Take 'join' of input and cast-up type, unless working with an Interface
360 const Type* CheckCastPPNode::Value(PhaseGVN* phase) const {
361   if( in(0) && phase->type(in(0)) == Type::TOP ) return Type::TOP;
362 
363   const Type *inn = phase->type(in(1));
364   if( inn == Type::TOP ) return Type::TOP;  // No information yet
365 
366   if (inn->isa_oopptr() && _type->isa_oopptr()) {
367     return ConstraintCastNode::Value(phase);
368   }
369 
370   const TypePtr *in_type = inn->isa_ptr();
371   const TypePtr *my_type = _type->isa_ptr();
372   const Type *result = _type;
373   if (in_type != nullptr && my_type != nullptr) {
374     TypePtr::PTR in_ptr = in_type->ptr();
375     if (in_ptr == TypePtr::Null) {
376       result = in_type;
377     } else if (in_ptr != TypePtr::Constant) {
378       result =  my_type->cast_to_ptr_type(my_type->join_ptr(in_ptr));
379     }
380   }
381 
382   return result;
383 }
384 
385 //=============================================================================
386 //------------------------------Value------------------------------------------
387 const Type* CastX2PNode::Value(PhaseGVN* phase) const {
388   const Type* t = phase->type(in(1));
389   if (t == Type::TOP) return Type::TOP;
390   if (t->base() == Type_X && t->singleton()) {
391     uintptr_t bits = (uintptr_t) t->is_intptr_t()->get_con();
392     if (bits == 0)   return TypePtr::NULL_PTR;
393     return TypeRawPtr::make((address) bits);
394   }
395   return CastX2PNode::bottom_type();
396 }
397 
398 //------------------------------Idealize---------------------------------------
399 static inline bool fits_in_int(const Type* t, bool but_not_min_int = false) {
400   if (t == Type::TOP)  return false;
401   const TypeX* tl = t->is_intptr_t();
402   jint lo = min_jint;
403   jint hi = max_jint;
404   if (but_not_min_int)  ++lo;  // caller wants to negate the value w/o overflow
405   return (tl->_lo >= lo) && (tl->_hi <= hi);
406 }
407 
408 static inline Node* addP_of_X2P(PhaseGVN *phase,
409                                 Node* base,
410                                 Node* dispX,
411                                 bool negate = false) {
412   if (negate) {
413     dispX = phase->transform(new SubXNode(phase->MakeConX(0), dispX));
414   }
415   return new AddPNode(phase->C->top(),
416                       phase->transform(new CastX2PNode(base)),
417                       dispX);
418 }
419 
420 Node *CastX2PNode::Ideal(PhaseGVN *phase, bool can_reshape) {
421   // convert CastX2P(AddX(x, y)) to AddP(CastX2P(x), y) if y fits in an int
422   int op = in(1)->Opcode();
423   Node* x;
424   Node* y;
425   switch (op) {
426     case Op_SubX:
427     x = in(1)->in(1);
428     // Avoid ideal transformations ping-pong between this and AddP for raw pointers.
429     if (phase->find_intptr_t_con(x, -1) == 0)
430     break;
431     y = in(1)->in(2);
432     if (fits_in_int(phase->type(y), true)) {
433       return addP_of_X2P(phase, x, y, true);
434     }
435     break;
436     case Op_AddX:
437     x = in(1)->in(1);
438     y = in(1)->in(2);
439     if (fits_in_int(phase->type(y))) {
440       return addP_of_X2P(phase, x, y);
441     }
442     if (fits_in_int(phase->type(x))) {
443       return addP_of_X2P(phase, y, x);
444     }
445     break;
446   }
447   return nullptr;
448 }
449 
450 //------------------------------Identity---------------------------------------
451 Node* CastX2PNode::Identity(PhaseGVN* phase) {
452   if (in(1)->Opcode() == Op_CastP2X)  return in(1)->in(1);
453   return this;
454 }
455 
456 //=============================================================================
457 //------------------------------Value------------------------------------------
458 const Type* CastP2XNode::Value(PhaseGVN* phase) const {
459   const Type* t = phase->type(in(1));
460   if (t == Type::TOP) return Type::TOP;
461   if (t->base() == Type::RawPtr && t->singleton()) {
462     uintptr_t bits = (uintptr_t) t->is_rawptr()->get_con();
463     return TypeX::make(bits);
464   }
465   return CastP2XNode::bottom_type();
466 }
467 
468 Node *CastP2XNode::Ideal(PhaseGVN *phase, bool can_reshape) {
469   return (in(0) && remove_dead_region(phase, can_reshape)) ? this : nullptr;
470 }
471 
472 //------------------------------Identity---------------------------------------
473 Node* CastP2XNode::Identity(PhaseGVN* phase) {
474   if (in(1)->Opcode() == Op_CastX2P)  return in(1)->in(1);
475   return this;
476 }
477 
478 Node* ConstraintCastNode::make_cast_for_type(Node* c, Node* in, const Type* type, DependencyType dependency,
479                                              const TypeTuple* types) {
480   if (type->isa_int()) {
481     return new CastIINode(c, in, type, dependency, false, types);
482   } else if (type->isa_long()) {
483     return new CastLLNode(c, in, type, dependency, types);
484   } else if (type->isa_half_float()) {
485     return new CastHHNode(c, in, type, dependency, types);
486   } else if (type->isa_float()) {
487     return new CastFFNode(c, in, type, dependency, types);
488   } else if (type->isa_double()) {
489     return new CastDDNode(c, in, type, dependency, types);
490   } else if (type->isa_vect()) {
491     return new CastVVNode(c, in, type, dependency, types);
492   } else if (type->isa_ptr()) {
493     return new CastPPNode(c, in, type, dependency, types);
494   }
495   fatal("unreachable. Invalid cast type.");
496   return nullptr;
497 }
498 
499 Node* ConstraintCastNode::optimize_integer_cast(PhaseGVN* phase, BasicType bt) {
500   PhaseIterGVN *igvn = phase->is_IterGVN();
501   const TypeInteger* this_type = this->type()->is_integer(bt);
502   Node* z = in(1);
503   const TypeInteger* rx = nullptr;
504   const TypeInteger* ry = nullptr;
505   // Similar to ConvI2LNode::Ideal() for the same reasons
506   if (Compile::push_thru_add(phase, z, this_type, rx, ry, bt, bt)) {
507     if (igvn == nullptr) {
508       // Postpone this optimization to iterative GVN, where we can handle deep
509       // AddI chains without an exponential number of recursive Ideal() calls.
510       phase->record_for_igvn(this);
511       return nullptr;
512     }
513     int op = z->Opcode();
514     Node* x = z->in(1);
515     Node* y = z->in(2);
516 
517     Node* cx = find_or_make_integer_cast(igvn, x, rx);
518     Node* cy = find_or_make_integer_cast(igvn, y, ry);
519     if (op == Op_Add(bt)) {
520       return AddNode::make(cx, cy, bt);
521     } else {
522       assert(op == Op_Sub(bt), "");
523       return SubNode::make(cx, cy, bt);
524     }
525     return nullptr;
526   }
527   return nullptr;
528 }
529 
530 const Type* ConstraintCastNode::widen_type(const PhaseGVN* phase, const Type* res, BasicType bt) const {
531   if (!phase->C->post_loop_opts_phase()) {
532     return res;
533   }
534   const TypeInteger* this_type = res->is_integer(bt);
535   const TypeInteger* in_type = phase->type(in(1))->isa_integer(bt);
536   if (in_type != nullptr &&
537       (in_type->lo_as_long() != this_type->lo_as_long() ||
538        in_type->hi_as_long() != this_type->hi_as_long())) {
539     jlong lo1 = this_type->lo_as_long();
540     jlong hi1 = this_type->hi_as_long();
541     int w1 = this_type->_widen;
542     if (lo1 >= 0) {
543       // Keep a range assertion of >=0.
544       lo1 = 0;        hi1 = max_signed_integer(bt);
545     } else if (hi1 < 0) {
546       // Keep a range assertion of <0.
547       lo1 = min_signed_integer(bt); hi1 = -1;
548     } else {
549       lo1 = min_signed_integer(bt); hi1 = max_signed_integer(bt);
550     }
551     return TypeInteger::make(MAX2(in_type->lo_as_long(), lo1),
552                              MIN2(in_type->hi_as_long(), hi1),
553                              MAX2((int)in_type->_widen, w1), bt);
554   }
555   return res;
556 }