1 /*
  2  * Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "precompiled.hpp"
 26 #include "opto/addnode.hpp"
 27 #include "opto/callnode.hpp"
 28 #include "opto/castnode.hpp"
 29 #include "opto/connode.hpp"
 30 #include "opto/matcher.hpp"
 31 #include "opto/phaseX.hpp"
 32 #include "opto/subnode.hpp"
 33 #include "opto/type.hpp"
 34 #include "castnode.hpp"
 35 #include "utilities/checkedCast.hpp"
 36 
 37 //=============================================================================
 38 // If input is already higher or equal to cast type, then this is an identity.
 39 Node* ConstraintCastNode::Identity(PhaseGVN* phase) {
 40   if (_dependency == UnconditionalDependency) {
 41     return this;
 42   }
 43   Node* dom = dominating_cast(phase, phase);
 44   if (dom != nullptr) {
 45     return dom;
 46   }
 47   return higher_equal_types(phase, in(1)) ? in(1) : this;
 48 }
 49 
 50 //------------------------------Value------------------------------------------
 51 // Take 'join' of input and cast-up type
 52 const Type* ConstraintCastNode::Value(PhaseGVN* phase) const {
 53   if (in(0) && phase->type(in(0)) == Type::TOP) return Type::TOP;
 54 
 55   const Type* in_type = phase->type(in(1));
 56   const Type* ft = in_type->filter_speculative(_type);
 57 
 58   // Check if both _type and in_type had a speculative type, but for the just
 59   // computed ft the speculative type was dropped.
 60   if (ft->speculative() == nullptr &&
 61       _type->speculative() != nullptr &&
 62       in_type->speculative() != nullptr) {
 63     // Speculative type may have disagreed between cast and input, and was
 64     // dropped in filtering. Recompute so that ft can take speculative type
 65     // of in_type. If we did not do it now, a subsequent ::Value call would
 66     // do it, and violate idempotence of ::Value.
 67     ft = in_type->filter_speculative(ft);
 68   }
 69 
 70 #ifdef ASSERT
 71   // Previous versions of this function had some special case logic,
 72   // which is no longer necessary.  Make sure of the required effects.
 73   switch (Opcode()) {
 74     case Op_CastII:
 75     {
 76       if (in_type == Type::TOP) {
 77         assert(ft == Type::TOP, "special case #1");
 78       }
 79       const Type* rt = in_type->join_speculative(_type);
 80       if (rt->empty()) {
 81         assert(ft == Type::TOP, "special case #2");
 82       }
 83       break;
 84     }
 85     case Op_CastPP:
 86     if (in_type == TypePtr::NULL_PTR &&
 87         _type->isa_ptr() && _type->is_ptr()->_ptr == TypePtr::NotNull) {
 88       assert(ft == Type::TOP, "special case #3");
 89       break;
 90     }
 91   }
 92 #endif //ASSERT
 93 
 94   return ft;
 95 }
 96 
 97 //------------------------------Ideal------------------------------------------
 98 // Return a node which is more "ideal" than the current node.  Strip out
 99 // control copies
100 Node *ConstraintCastNode::Ideal(PhaseGVN *phase, bool can_reshape) {
101   return (in(0) && remove_dead_region(phase, can_reshape)) ? this : nullptr;
102 }
103 
104 uint ConstraintCastNode::hash() const {
105   return TypeNode::hash() + (int)_dependency + (_extra_types != nullptr ? _extra_types->hash() : 0);
106 }
107 
108 bool ConstraintCastNode::cmp(const Node &n) const {
109   if (!TypeNode::cmp(n)) {
110     return false;
111   }
112   ConstraintCastNode& cast = (ConstraintCastNode&) n;
113   if (cast._dependency != _dependency) {
114     return false;
115   }
116   if (_extra_types == nullptr || cast._extra_types == nullptr) {
117     return _extra_types == cast._extra_types;
118   }
119   return _extra_types->eq(cast._extra_types);
120 }
121 
122 uint ConstraintCastNode::size_of() const {
123   return sizeof(*this);
124 }
125 
126 Node* ConstraintCastNode::make_cast_for_basic_type(Node* c, Node* n, const Type* t, DependencyType dependency, BasicType bt) {
127   switch(bt) {
128   case T_INT:
129     return new CastIINode(c, n, t, dependency);
130   case T_LONG:
131     return new CastLLNode(c, n, t, dependency);
132   default:
133     fatal("Bad basic type %s", type2name(bt));
134   }
135   return nullptr;
136 }
137 
138 TypeNode* ConstraintCastNode::dominating_cast(PhaseGVN* gvn, PhaseTransform* pt) const {
139   if (_dependency == UnconditionalDependency) {
140     return nullptr;
141   }
142   Node* val = in(1);
143   Node* ctl = in(0);
144   int opc = Opcode();
145   if (ctl == nullptr) {
146     return nullptr;
147   }
148   // Range check CastIIs may all end up under a single range check and
149   // in that case only the narrower CastII would be kept by the code
150   // below which would be incorrect.
151   if (is_CastII() && as_CastII()->has_range_check()) {
152     return nullptr;
153   }
154   if (type()->isa_rawptr() && (gvn->type_or_null(val) == nullptr || gvn->type(val)->isa_oopptr())) {
155     return nullptr;
156   }
157   for (DUIterator_Fast imax, i = val->fast_outs(imax); i < imax; i++) {
158     Node* u = val->fast_out(i);
159     if (u != this &&
160         u->outcnt() > 0 &&
161         u->Opcode() == opc &&
162         u->in(0) != nullptr &&
163         higher_equal_types(gvn, u)) {
164       if (pt->is_dominator(u->in(0), ctl)) {
165         return u->as_Type();
166       }
167       if (is_CheckCastPP() && u->in(1)->is_Proj() && u->in(1)->in(0)->is_Allocate() &&
168           u->in(0)->is_Proj() && u->in(0)->in(0)->is_Initialize() &&
169           u->in(1)->in(0)->as_Allocate()->initialization() == u->in(0)->in(0)) {
170         // CheckCastPP following an allocation always dominates all
171         // use of the allocation result
172         return u->as_Type();
173       }
174     }
175   }
176   return nullptr;
177 }
178 
179 bool ConstraintCastNode::higher_equal_types(PhaseGVN* phase, const Node* other) const {
180   const Type* t = phase->type(other);
181   if (!t->higher_equal_speculative(type())) {
182     return false;
183   }
184   if (_extra_types != nullptr) {
185     for (uint i = 0; i < _extra_types->cnt(); ++i) {
186       if (!t->higher_equal_speculative(_extra_types->field_at(i))) {
187         return false;
188       }
189     }
190   }
191   return true;
192 }
193 
194 #ifndef PRODUCT
195 void ConstraintCastNode::dump_spec(outputStream *st) const {
196   TypeNode::dump_spec(st);
197   if (_extra_types != nullptr) {
198     st->print(" extra types: ");
199     _extra_types->dump_on(st);
200   }
201   if (_dependency != RegularDependency) {
202     st->print(" %s dependency", _dependency == StrongDependency ? "strong" : "unconditional");
203   }
204 }
205 #endif
206 
207 const Type* CastIINode::Value(PhaseGVN* phase) const {
208   const Type *res = ConstraintCastNode::Value(phase);
209   if (res == Type::TOP) {
210     return Type::TOP;
211   }
212   assert(res->isa_int(), "res must be int");
213 
214   // Similar to ConvI2LNode::Value() for the same reasons
215   // see if we can remove type assertion after loop opts
216   // But here we have to pay extra attention:
217   // Do not narrow the type of range check dependent CastIINodes to
218   // avoid corruption of the graph if a CastII is replaced by TOP but
219   // the corresponding range check is not removed.
220   if (!_range_check_dependency) {
221     res = widen_type(phase, res, T_INT);
222   }
223 
224   return res;
225 }
226 
227 static Node* find_or_make_integer_cast(PhaseIterGVN* igvn, Node* parent, Node* control, const TypeInteger* type, ConstraintCastNode::DependencyType dependency, BasicType bt) {
228   Node* n = ConstraintCastNode::make_cast_for_basic_type(control, parent, type, dependency, bt);
229   Node* existing = igvn->hash_find_insert(n);
230   if (existing != nullptr) {
231     n->destruct(igvn);
232     return existing;
233   }
234   return igvn->register_new_node_with_optimizer(n);
235 }
236 
237 Node *CastIINode::Ideal(PhaseGVN *phase, bool can_reshape) {
238   Node* progress = ConstraintCastNode::Ideal(phase, can_reshape);
239   if (progress != nullptr) {
240     return progress;
241   }
242   if (can_reshape && !_range_check_dependency && !phase->C->post_loop_opts_phase()) {
243     // makes sure we run ::Value to potentially remove type assertion after loop opts
244     phase->C->record_for_post_loop_opts_igvn(this);
245   }
246   if (!_range_check_dependency) {
247     return optimize_integer_cast(phase, T_INT);
248   }
249   return nullptr;
250 }
251 
252 Node* CastIINode::Identity(PhaseGVN* phase) {
253   Node* progress = ConstraintCastNode::Identity(phase);
254   if (progress != this) {
255     return progress;
256   }
257   if (_range_check_dependency) {
258     if (phase->C->post_loop_opts_phase()) {
259       return this->in(1);
260     } else {
261       phase->C->record_for_post_loop_opts_igvn(this);
262     }
263   }
264   return this;
265 }
266 
267 bool CastIINode::cmp(const Node &n) const {
268   return ConstraintCastNode::cmp(n) && ((CastIINode&)n)._range_check_dependency == _range_check_dependency;
269 }
270 
271 uint CastIINode::size_of() const {
272   return sizeof(*this);
273 }
274 
275 #ifndef PRODUCT
276 void CastIINode::dump_spec(outputStream* st) const {
277   ConstraintCastNode::dump_spec(st);
278   if (_range_check_dependency) {
279     st->print(" range check dependency");
280   }
281 }
282 #endif
283 
284 CastIINode* CastIINode::pin_array_access_node() const {
285   assert(_dependency == RegularDependency, "already pinned");
286   if (has_range_check()) {
287     return new CastIINode(in(0), in(1), bottom_type(), StrongDependency, has_range_check());
288   }
289   return nullptr;
290 }
291 
292 
293 const Type* CastLLNode::Value(PhaseGVN* phase) const {
294   const Type* res = ConstraintCastNode::Value(phase);
295   if (res == Type::TOP) {
296     return Type::TOP;
297   }
298   assert(res->isa_long(), "res must be long");
299 
300   return widen_type(phase, res, T_LONG);
301 }
302 
303 Node* CastLLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
304   Node* progress = ConstraintCastNode::Ideal(phase, can_reshape);
305   if (progress != nullptr) {
306     return progress;
307   }
308   if (!phase->C->post_loop_opts_phase()) {
309     // makes sure we run ::Value to potentially remove type assertion after loop opts
310     phase->C->record_for_post_loop_opts_igvn(this);
311   }
312   // transform (CastLL (ConvI2L ..)) into (ConvI2L (CastII ..)) if the type of the CastLL is narrower than the type of
313   // the ConvI2L.
314   Node* in1 = in(1);
315   if (in1 != nullptr && in1->Opcode() == Op_ConvI2L) {
316     const Type* t = Value(phase);
317     const Type* t_in = phase->type(in1);
318     if (t != Type::TOP && t_in != Type::TOP) {
319       const TypeLong* tl = t->is_long();
320       const TypeLong* t_in_l = t_in->is_long();
321       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");
322       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");
323       if (tl != t_in_l) {
324         const TypeInt* ti = TypeInt::make(checked_cast<jint>(tl->_lo), checked_cast<jint>(tl->_hi), tl->_widen);
325         Node* castii = phase->transform(new CastIINode(in(0), in1->in(1), ti));
326         Node* convi2l = in1->clone();
327         convi2l->set_req(1, castii);
328         return convi2l;
329       }
330     }
331   }
332   return optimize_integer_cast(phase, T_LONG);
333 }
334 
335 //------------------------------Value------------------------------------------
336 // Take 'join' of input and cast-up type, unless working with an Interface
337 const Type* CheckCastPPNode::Value(PhaseGVN* phase) const {
338   if( in(0) && phase->type(in(0)) == Type::TOP ) return Type::TOP;
339 
340   const Type *inn = phase->type(in(1));
341   if( inn == Type::TOP ) return Type::TOP;  // No information yet
342 
343   if (inn->isa_oopptr() && _type->isa_oopptr()) {
344     return ConstraintCastNode::Value(phase);
345   }
346 
347   const TypePtr *in_type = inn->isa_ptr();
348   const TypePtr *my_type = _type->isa_ptr();
349   const Type *result = _type;
350   if (in_type != nullptr && my_type != nullptr) {
351     TypePtr::PTR in_ptr = in_type->ptr();
352     if (in_ptr == TypePtr::Null) {
353       result = in_type;
354     } else if (in_ptr != TypePtr::Constant) {
355       result =  my_type->cast_to_ptr_type(my_type->join_ptr(in_ptr));
356     }
357   }
358 
359   return result;
360 }
361 
362 //=============================================================================
363 //------------------------------Value------------------------------------------
364 const Type* CastX2PNode::Value(PhaseGVN* phase) const {
365   const Type* t = phase->type(in(1));
366   if (t == Type::TOP) return Type::TOP;
367   if (t->base() == Type_X && t->singleton()) {
368     uintptr_t bits = (uintptr_t) t->is_intptr_t()->get_con();
369     if (bits == 0)   return TypePtr::NULL_PTR;
370     return TypeRawPtr::make((address) bits);
371   }
372   return CastX2PNode::bottom_type();
373 }
374 
375 //------------------------------Idealize---------------------------------------
376 static inline bool fits_in_int(const Type* t, bool but_not_min_int = false) {
377   if (t == Type::TOP)  return false;
378   const TypeX* tl = t->is_intptr_t();
379   jint lo = min_jint;
380   jint hi = max_jint;
381   if (but_not_min_int)  ++lo;  // caller wants to negate the value w/o overflow
382   return (tl->_lo >= lo) && (tl->_hi <= hi);
383 }
384 
385 static inline Node* addP_of_X2P(PhaseGVN *phase,
386                                 Node* base,
387                                 Node* dispX,
388                                 bool negate = false) {
389   if (negate) {
390     dispX = phase->transform(new SubXNode(phase->MakeConX(0), dispX));
391   }
392   return new AddPNode(phase->C->top(),
393                       phase->transform(new CastX2PNode(base)),
394                       dispX);
395 }
396 
397 Node *CastX2PNode::Ideal(PhaseGVN *phase, bool can_reshape) {
398   // convert CastX2P(AddX(x, y)) to AddP(CastX2P(x), y) if y fits in an int
399   int op = in(1)->Opcode();
400   Node* x;
401   Node* y;
402   switch (op) {
403     case Op_SubX:
404     x = in(1)->in(1);
405     // Avoid ideal transformations ping-pong between this and AddP for raw pointers.
406     if (phase->find_intptr_t_con(x, -1) == 0)
407     break;
408     y = in(1)->in(2);
409     if (fits_in_int(phase->type(y), true)) {
410       return addP_of_X2P(phase, x, y, true);
411     }
412     break;
413     case Op_AddX:
414     x = in(1)->in(1);
415     y = in(1)->in(2);
416     if (fits_in_int(phase->type(y))) {
417       return addP_of_X2P(phase, x, y);
418     }
419     if (fits_in_int(phase->type(x))) {
420       return addP_of_X2P(phase, y, x);
421     }
422     break;
423   }
424   return nullptr;
425 }
426 
427 //------------------------------Identity---------------------------------------
428 Node* CastX2PNode::Identity(PhaseGVN* phase) {
429   if (in(1)->Opcode() == Op_CastP2X)  return in(1)->in(1);
430   return this;
431 }
432 
433 //=============================================================================
434 //------------------------------Value------------------------------------------
435 const Type* CastP2XNode::Value(PhaseGVN* phase) const {
436   const Type* t = phase->type(in(1));
437   if (t == Type::TOP) return Type::TOP;
438   if (t->base() == Type::RawPtr && t->singleton()) {
439     uintptr_t bits = (uintptr_t) t->is_rawptr()->get_con();
440     return TypeX::make(bits);
441   }
442   return CastP2XNode::bottom_type();
443 }
444 
445 Node *CastP2XNode::Ideal(PhaseGVN *phase, bool can_reshape) {
446   return (in(0) && remove_dead_region(phase, can_reshape)) ? this : nullptr;
447 }
448 
449 //------------------------------Identity---------------------------------------
450 Node* CastP2XNode::Identity(PhaseGVN* phase) {
451   if (in(1)->Opcode() == Op_CastX2P)  return in(1)->in(1);
452   return this;
453 }
454 
455 Node* ConstraintCastNode::make_cast_for_type(Node* c, Node* in, const Type* type, DependencyType dependency,
456                                              const TypeTuple* types) {
457   if (type->isa_int()) {
458     return new CastIINode(c, in, type, dependency, false, types);
459   } else if (type->isa_long()) {
460     return new CastLLNode(c, in, type, dependency, types);
461   } else if (type->isa_float()) {
462     return new CastFFNode(c, in, type, dependency, types);
463   } else if (type->isa_double()) {
464     return new CastDDNode(c, in, type, dependency, types);
465   } else if (type->isa_vect()) {
466     return new CastVVNode(c, in, type, dependency, types);
467   } else if (type->isa_ptr()) {
468     return new CastPPNode(c, in, type, dependency, types);
469   }
470   fatal("unreachable. Invalid cast type.");
471   return nullptr;
472 }
473 
474 Node* ConstraintCastNode::optimize_integer_cast(PhaseGVN* phase, BasicType bt) {
475   PhaseIterGVN *igvn = phase->is_IterGVN();
476   const TypeInteger* this_type = this->type()->is_integer(bt);
477   Node* z = in(1);
478   const TypeInteger* rx = nullptr;
479   const TypeInteger* ry = nullptr;
480   // Similar to ConvI2LNode::Ideal() for the same reasons
481   if (Compile::push_thru_add(phase, z, this_type, rx, ry, bt, bt)) {
482     if (igvn == nullptr) {
483       // Postpone this optimization to iterative GVN, where we can handle deep
484       // AddI chains without an exponential number of recursive Ideal() calls.
485       phase->record_for_igvn(this);
486       return nullptr;
487     }
488     int op = z->Opcode();
489     Node* x = z->in(1);
490     Node* y = z->in(2);
491 
492     Node* cx = find_or_make_integer_cast(igvn, x, in(0), rx, _dependency, bt);
493     Node* cy = find_or_make_integer_cast(igvn, y, in(0), ry, _dependency, bt);
494     if (op == Op_Add(bt)) {
495       return AddNode::make(cx, cy, bt);
496     } else {
497       assert(op == Op_Sub(bt), "");
498       return SubNode::make(cx, cy, bt);
499     }
500     return nullptr;
501   }
502   return nullptr;
503 }
504 
505 const Type* ConstraintCastNode::widen_type(const PhaseGVN* phase, const Type* res, BasicType bt) const {
506   if (!phase->C->post_loop_opts_phase()) {
507     return res;
508   }
509   const TypeInteger* this_type = res->is_integer(bt);
510   const TypeInteger* in_type = phase->type(in(1))->isa_integer(bt);
511   if (in_type != nullptr &&
512       (in_type->lo_as_long() != this_type->lo_as_long() ||
513        in_type->hi_as_long() != this_type->hi_as_long())) {
514     jlong lo1 = this_type->lo_as_long();
515     jlong hi1 = this_type->hi_as_long();
516     int w1 = this_type->_widen;
517     if (lo1 >= 0) {
518       // Keep a range assertion of >=0.
519       lo1 = 0;        hi1 = max_signed_integer(bt);
520     } else if (hi1 < 0) {
521       // Keep a range assertion of <0.
522       lo1 = min_signed_integer(bt); hi1 = -1;
523     } else {
524       lo1 = min_signed_integer(bt); hi1 = max_signed_integer(bt);
525     }
526     return TypeInteger::make(MAX2(in_type->lo_as_long(), lo1),
527                              MIN2(in_type->hi_as_long(), hi1),
528                              MAX2((int)in_type->_widen, w1), bt);
529   }
530   return res;
531 }