1 /*
2 * Copyright (c) 2014, 2026, 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/cfgnode.hpp"
29 #include "opto/connode.hpp"
30 #include "opto/graphKit.hpp"
31 #include "opto/inlinetypenode.hpp"
32 #include "opto/loopnode.hpp"
33 #include "opto/matcher.hpp"
34 #include "opto/phaseX.hpp"
35 #include "opto/rootnode.hpp"
36 #include "opto/subnode.hpp"
37 #include "opto/type.hpp"
38 #include "utilities/checkedCast.hpp"
39
40 const ConstraintCastNode::DependencyType ConstraintCastNode::DependencyType::FloatingNarrowing(true, true, "floating narrowing dependency"); // not pinned, narrows type
41 const ConstraintCastNode::DependencyType ConstraintCastNode::DependencyType::FloatingNonNarrowing(true, false, "floating non-narrowing dependency"); // not pinned, doesn't narrow type
42 const ConstraintCastNode::DependencyType ConstraintCastNode::DependencyType::NonFloatingNarrowing(false, true, "non-floating narrowing dependency"); // pinned, narrows type
43 const ConstraintCastNode::DependencyType ConstraintCastNode::DependencyType::NonFloatingNonNarrowing(false, false, "non-floating non-narrowing dependency"); // pinned, doesn't narrow type
44
45 //=============================================================================
46 // If input is already higher or equal to cast type, then this is an identity.
47 Node* ConstraintCastNode::Identity(PhaseGVN* phase) {
48 if (!_dependency.narrows_type()) {
49 // If this cast doesn't carry a type dependency (i.e. not used for type narrowing), we cannot optimize it.
50 return this;
51 }
52
53 // This cast node carries a type dependency. We can remove it if:
54 // - Its input has a narrower type
55 // - There's a dominating cast with same input but narrower type
56 Node* dom = dominating_cast(phase, phase);
57 if (dom != nullptr) {
58 return dom;
59 }
60 return higher_equal_types(phase, in(1)) ? in(1) : this;
61 }
62
63 //------------------------------Value------------------------------------------
64 // Take 'join' of input and cast-up type
65 const Type* ConstraintCastNode::Value(PhaseGVN* phase) const {
66 if (in(0) && phase->type(in(0)) == Type::TOP) return Type::TOP;
67
68 const Type* in_type = phase->type(in(1));
69 const Type* ft = in_type->filter_speculative(_type);
70
71 // Check if both _type and in_type had a speculative type, but for the just
72 // computed ft the speculative type was dropped.
73 if (ft->speculative() == nullptr &&
74 _type->speculative() != nullptr &&
75 in_type->speculative() != nullptr) {
76 // Speculative type may have disagreed between cast and input, and was
77 // dropped in filtering. Recompute so that ft can take speculative type
78 // of in_type. If we did not do it now, a subsequent ::Value call would
79 // do it, and violate idempotence of ::Value.
80 ft = in_type->filter_speculative(ft);
81 }
82
83 #ifdef ASSERT
84 // Previous versions of this function had some special case logic,
85 // which is no longer necessary. Make sure of the required effects.
86 switch (Opcode()) {
87 case Op_CastII:
88 {
89 if (in_type == Type::TOP) {
90 assert(ft == Type::TOP, "special case #1");
91 }
92 const Type* rt = in_type->join_speculative(_type);
93 if (rt->empty()) {
94 assert(ft == Type::TOP, "special case #2");
95 }
96 break;
97 }
98 case Op_CastPP:
99 if (in_type == TypePtr::NULL_PTR &&
100 _type->isa_ptr() && _type->is_ptr()->_ptr == TypePtr::NotNull) {
101 assert(ft == Type::TOP, "special case #3");
102 break;
103 }
104 }
105 #endif //ASSERT
106
107 return ft;
108 }
109
110 //------------------------------Ideal------------------------------------------
111 // Return a node which is more "ideal" than the current node. Strip out
112 // control copies
113 Node *ConstraintCastNode::Ideal(PhaseGVN *phase, bool can_reshape) {
114 if (in(0) != nullptr && remove_dead_region(phase, can_reshape)) {
115 return this;
116 }
117
118 // Push cast through InlineTypeNode
119 if (in(1)->is_InlineType()) {
120 return ideal_cast_of_inline_type_node(phase);
121 }
122
123 if (in(1) != nullptr && phase->type(in(1)) != Type::TOP) {
124 return TypeNode::Ideal(phase, can_reshape);
125 }
126
127 return nullptr;
128 }
129
130 uint ConstraintCastNode::hash() const {
131 return TypeNode::hash() + _dependency.hash() + (_extra_types != nullptr ? _extra_types->hash() : 0);
132 }
133
134 bool ConstraintCastNode::cmp(const Node &n) const {
135 if (!TypeNode::cmp(n)) {
136 return false;
137 }
138 ConstraintCastNode& cast = (ConstraintCastNode&) n;
139 if (!cast._dependency.cmp(_dependency)) {
140 return false;
141 }
142 if (_extra_types == nullptr || cast._extra_types == nullptr) {
143 return _extra_types == cast._extra_types;
144 }
145 return _extra_types->eq(cast._extra_types);
146 }
147
148 uint ConstraintCastNode::size_of() const {
149 return sizeof(*this);
150 }
151
152 Node* ConstraintCastNode::make_cast_for_basic_type(Node* c, Node* n, const Type* t, const DependencyType& dependency, BasicType bt) {
153 switch(bt) {
154 case T_INT:
155 return new CastIINode(c, n, t, dependency);
156 case T_LONG:
157 return new CastLLNode(c, n, t, dependency);
158 default:
159 fatal("Bad basic type %s", type2name(bt));
160 }
161 return nullptr;
162 }
163
164 TypeNode* ConstraintCastNode::dominating_cast(PhaseGVN* gvn, PhaseTransform* pt) const {
165 // See discussion at definition of ConstraintCastNode::DependencyType: replacing this cast with a dominating one is
166 // not safe if _dependency.narrows_type() is not true.
167 assert(_dependency.narrows_type(), "cast can't be replaced by dominating one");
168 Node* val = in(1);
169 Node* ctl = in(0);
170 int opc = Opcode();
171 if (ctl == nullptr) {
172 return nullptr;
173 }
174 // Range check CastIIs may all end up under a single range check and
175 // in that case only the narrower CastII would be kept by the code
176 // below which would be incorrect.
177 if (is_CastII() && as_CastII()->has_range_check()) {
178 return nullptr;
179 }
180 if (type()->isa_rawptr() && (gvn->type_or_null(val) == nullptr || gvn->type(val)->isa_oopptr())) {
181 return nullptr;
182 }
183 for (DUIterator_Fast imax, i = val->fast_outs(imax); i < imax; i++) {
184 Node* u = val->fast_out(i);
185 if (u != this &&
186 u->outcnt() > 0 &&
187 u->Opcode() == opc &&
188 u->in(0) != nullptr &&
189 higher_equal_types(gvn, u)) {
190 if (pt->is_dominator(u->in(0), ctl)) {
191 return u->as_Type();
192 }
193 if (is_CheckCastPP() && u->in(1)->is_Proj() && u->in(1)->in(0)->is_Allocate() &&
194 u->in(0)->is_Proj() && u->in(0)->in(0)->is_Initialize() &&
195 u->in(1)->in(0)->as_Allocate()->initialization() == u->in(0)->in(0)) {
196 // CheckCastPP following an allocation always dominates all
197 // use of the allocation result
198 return u->as_Type();
199 }
200 }
201 }
202 return nullptr;
203 }
204
205 bool ConstraintCastNode::higher_equal_types(PhaseGVN* phase, const Node* other) const {
206 const Type* t = phase->type(other);
207 if (!t->higher_equal_speculative(type())) {
208 return false;
209 }
210 if (_extra_types != nullptr) {
211 for (uint i = 0; i < _extra_types->cnt(); ++i) {
212 if (!t->higher_equal_speculative(_extra_types->field_at(i))) {
213 return false;
214 }
215 }
216 }
217 return true;
218 }
219
220 Node* ConstraintCastNode::pin_node_under_control_impl() const {
221 assert(_dependency.is_floating(), "already pinned");
222 return make_cast_for_type(in(0), in(1), bottom_type(), _dependency.with_pinned_dependency(), _extra_types);
223 }
224
225 Node* ConstraintCastNode::ideal_cast_of_inline_type_node(PhaseGVN* phase) {
226 InlineTypeNode* vt = in(1)->as_InlineType();
227 const Type* join = vt->type()->filter(type());
228 if (join == Type::TOP) {
229 // Do not push a dead Cast since its type can be unrelated
230 return nullptr;
231 }
232
233 if (join == vt->type()->remove_speculative()) {
234 // Redundant cast, let Identity handle
235 return nullptr;
236 }
237
238 if (join == TypePtr::NULL_PTR) {
239 // Will collapse to the constant null
240 return nullptr;
241 }
242
243 // The only possible case left is that the cast is a cast to not-null
244 assert(join == vt->type()->filter(TypePtr::NOTNULL), "must be");
245 InlineTypeNode* res = vt->clone()->as_InlineType();
246 res->set_null_marker(*phase);
247
248 // Push the cast to the oop input if possible
249 if (vt->is_allocated(phase)) {
250 Node* new_oop = clone();
251 new_oop->set_req(1, vt->get_oop());
252 res->set_oop(*phase, phase->transform(new_oop));
253 }
254
255 // Push the cast to the null-free inputs of vt
256 for (uint i = 0; i < vt->field_count(); i++) {
257 if (vt->field(i)->is_null_free()) {
258 const ConstraintCastNode::DependencyType& dep = _dependency.is_floating() ? ConstraintCastNode::DependencyType::FloatingNarrowing
259 : ConstraintCastNode::DependencyType::NonFloatingNarrowing;
260 Node* new_fv = new CastPPNode(in(0), vt->field_value(i), TypePtr::NOTNULL, dep);
261 res->set_field_value(i, phase->transform(new_fv));
262 }
263 }
264
265 return res;
266 }
267
268 #ifndef PRODUCT
269 void ConstraintCastNode::dump_spec(outputStream *st) const {
270 TypeNode::dump_spec(st);
271 if (_extra_types != nullptr) {
272 st->print(" extra types: ");
273 _extra_types->dump_on(st);
274 }
275 st->print(" ");
276 _dependency.dump_on(st);
277 }
278 #endif
279
280 CastIINode* CastIINode::make_with(Node* parent, const TypeInteger* type, const DependencyType& dependency) const {
281 return new CastIINode(in(0), parent, type, dependency, _range_check_dependency, _extra_types);
282 }
283
284 CastLLNode* CastLLNode::make_with(Node* parent, const TypeInteger* type, const DependencyType& dependency) const {
285 return new CastLLNode(in(0), parent, type, dependency, _extra_types);
286 }
287
288 Node* ConstraintCastNode::find_or_make_integer_cast(PhaseIterGVN* igvn, Node* parent, const TypeInteger* type, const DependencyType& dependency) const {
289 Node* n = make_with(parent, type, dependency);
290 Node* existing = igvn->hash_find_insert(n);
291 if (existing != nullptr) {
292 n->destruct(igvn);
293 return existing;
294 }
295 return igvn->register_new_node_with_optimizer(n);
296 }
297
298 Node *CastIINode::Ideal(PhaseGVN *phase, bool can_reshape) {
299 Node* progress = ConstraintCastNode::Ideal(phase, can_reshape);
300 if (progress != nullptr) {
301 return progress;
302 }
303 if (!phase->C->post_loop_opts_phase()) {
304 // makes sure we run widen_type() to potentially common type assertions after loop opts
305 phase->C->record_for_post_loop_opts_igvn(this);
306 }
307 if (!_range_check_dependency || phase->C->post_loop_opts_phase()) {
308 return optimize_integer_cast(phase, T_INT);
309 }
310 return nullptr;
311 }
312
313 Node* CastIINode::Identity(PhaseGVN* phase) {
314 Node* progress = ConstraintCastNode::Identity(phase);
315 if (progress != this) {
316 return progress;
317 }
318 return this;
319 }
320
321 bool CastIINode::cmp(const Node &n) const {
322 return ConstraintCastNode::cmp(n) && ((CastIINode&)n)._range_check_dependency == _range_check_dependency;
323 }
324
325 uint CastIINode::size_of() const {
326 return sizeof(*this);
327 }
328
329 #ifndef PRODUCT
330 void CastIINode::dump_spec(outputStream* st) const {
331 ConstraintCastNode::dump_spec(st);
332 if (_range_check_dependency) {
333 st->print(" range check dependency");
334 }
335 }
336 #endif
337
338 CastIINode* CastIINode::pin_node_under_control_impl() const {
339 assert(_dependency.is_floating(), "already pinned");
340 return new CastIINode(in(0), in(1), bottom_type(), _dependency.with_pinned_dependency(), _range_check_dependency, _extra_types);
341 }
342
343 void CastIINode::remove_range_check_cast(Compile* C) {
344 if (has_range_check()) {
345 // Range check CastII nodes feed into an address computation subgraph. Remove them to let that subgraph float freely.
346 // For memory access or integer divisions nodes that depend on the cast, record the dependency on the cast's control
347 // as a precedence edge, so they can't float above the cast in case that cast's narrowed type helped eliminate a
348 // range check or a null divisor check.
349 assert(in(0) != nullptr, "All RangeCheck CastII must have a control dependency");
350 ResourceMark rm;
351 Unique_Node_List wq;
352 wq.push(this);
353 for (uint next = 0; next < wq.size(); ++next) {
354 Node* m = wq.at(next);
355 for (DUIterator_Fast imax, i = m->fast_outs(imax); i < imax; i++) {
356 Node* use = m->fast_out(i);
357 if (use->is_Mem() || use->is_div_or_mod(T_INT) || use->is_div_or_mod(T_LONG)) {
358 use->ensure_control_or_add_prec(in(0));
359 } else if (!use->is_CFG() && !use->is_Phi()) {
360 wq.push(use);
361 }
362 }
363 }
364 subsume_by(in(1), C);
365 if (outcnt() == 0) {
366 disconnect_inputs(C);
367 }
368 }
369 }
370
371 bool CastLLNode::is_inner_loop_backedge(IfProjNode* proj) {
372 if (proj != nullptr) {
373 Node* ctrl_use = proj->unique_ctrl_out_or_null();
374 if (ctrl_use != nullptr && ctrl_use->Opcode() == Op_Loop &&
375 ctrl_use->in(2) == proj &&
376 ctrl_use->as_Loop()->is_loop_nest_inner_loop()) {
377 return true;
378 }
379 }
380 return false;
381 }
382
383 bool CastLLNode::cmp_used_at_inner_loop_exit_test(CmpNode* cmp) {
384 for (DUIterator_Fast imax, i = cmp->fast_outs(imax); i < imax; i++) {
385 Node* bol = cmp->fast_out(i);
386 if (bol->Opcode() == Op_Bool) {
387 for (DUIterator_Fast jmax, j = bol->fast_outs(jmax); j < jmax; j++) {
388 Node* iff = bol->fast_out(j);
389 if (iff->Opcode() == Op_If) {
390 IfTrueNode* true_proj = iff->as_If()->true_proj_or_null();
391 IfFalseNode* false_proj = iff->as_If()->false_proj_or_null();
392 if (is_inner_loop_backedge(true_proj) || is_inner_loop_backedge(false_proj)) {
393 return true;
394 }
395 }
396 }
397 }
398 }
399 return false;
400 }
401
402 // Find if this is a cast node added by PhaseIdealLoop::create_loop_nest() to narrow the number of iterations of the
403 // inner loop
404 bool CastLLNode::used_at_inner_loop_exit_test() const {
405 for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
406 Node* convl2i = fast_out(i);
407 if (convl2i->Opcode() == Op_ConvL2I) {
408 for (DUIterator_Fast jmax, j = convl2i->fast_outs(jmax); j < jmax; j++) {
409 Node* cmp_or_sub = convl2i->fast_out(j);
410 if (cmp_or_sub->Opcode() == Op_CmpI) {
411 if (cmp_used_at_inner_loop_exit_test(cmp_or_sub->as_Cmp())) {
412 // (Loop .. .. (IfProj (If (Bool (CmpI (ConvL2I (CastLL )))))))
413 return true;
414 }
415 } else if (cmp_or_sub->Opcode() == Op_SubI && cmp_or_sub->in(1)->find_int_con(-1) == 0) {
416 for (DUIterator_Fast kmax, k = cmp_or_sub->fast_outs(kmax); k < kmax; k++) {
417 Node* cmp = cmp_or_sub->fast_out(k);
418 if (cmp->Opcode() == Op_CmpI) {
419 if (cmp_used_at_inner_loop_exit_test(cmp->as_Cmp())) {
420 // (Loop .. .. (IfProj (If (Bool (CmpI (SubI 0 (ConvL2I (CastLL ))))))))
421 return true;
422 }
423 }
424 }
425 }
426 }
427 }
428 }
429 return false;
430 }
431
432 Node* CastLLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
433 Node* progress = ConstraintCastNode::Ideal(phase, can_reshape);
434 if (progress != nullptr) {
435 return progress;
436 }
437 if (!phase->C->post_loop_opts_phase()) {
438 // makes sure we run widen_type() to potentially common type assertions after loop opts
439 phase->C->record_for_post_loop_opts_igvn(this);
440 }
441 // transform (CastLL (ConvI2L ..)) into (ConvI2L (CastII ..)) if the type of the CastLL is narrower than the type of
442 // the ConvI2L.
443 Node* in1 = in(1);
444 if (in1 != nullptr && in1->Opcode() == Op_ConvI2L) {
445 const Type* t = Value(phase);
446 const Type* t_in = phase->type(in1);
447 if (t != Type::TOP && t_in != Type::TOP) {
448 const TypeLong* tl = t->is_long();
449 const TypeLong* t_in_l = t_in->is_long();
450 assert(t_in_l->contains(tl), "CastLL type should be narrower than or equal to the type of its input");
451 assert((tl != t_in_l) == t_in_l->strictly_contains(tl), "if type differs then this nodes's type must be narrower");
452 if (tl != t_in_l) {
453 const TypeInt* ti = TypeInt::make(checked_cast<jint>(tl->_lo), checked_cast<jint>(tl->_hi), tl->_widen);
454 Node* castii = phase->transform(new CastIINode(in(0), in1->in(1), ti));
455 Node* convi2l = in1->clone();
456 convi2l->set_req(1, castii);
457 return convi2l;
458 }
459 }
460 }
461 // If it's a cast created by PhaseIdealLoop::short_running_loop(), don't transform it until the counted loop is created
462 // in next loop opts pass
463 if (!can_reshape || !used_at_inner_loop_exit_test()) {
464 return optimize_integer_cast(phase, T_LONG);
465 }
466 return nullptr;
467 }
468
469 //=============================================================================
470 //------------------------------Identity---------------------------------------
471 // If input is already higher or equal to cast type, then this is an identity.
472 Node* CheckCastPPNode::Identity(PhaseGVN* phase) {
473 if (in(1)->is_InlineType() && _type->isa_instptr() && phase->type(in(1))->inline_klass()->is_subtype_of(_type->is_instptr()->instance_klass())) {
474 return in(1);
475 }
476 return ConstraintCastNode::Identity(phase);
477 }
478
479 // CastPPNodes are removed before matching, while alias classes are needed in global code motion.
480 // As a result, it is not valid for a CastPPNode to change the oop such that the derived pointers
481 // lie in different alias classes with and without the node. For example, a CastPPNode c may not
482 // cast an Object to a Bottom[], because later removal of c would affect the alias class of c's
483 // array length field (c + arrayOopDesc::length_offset_in_bytes()).
484 //
485 // This function verifies that a CastPPNode on an oop does not violate the aforementioned property.
486 //
487 // TODO 8382147: Currently, this verification only applies during the construction of a CastPPNode,
488 // we may want to apply the same verification during IGVN transformations, as well as final graph
489 // reshaping.
490 void CastPPNode::verify_type(const Type* in_type, const Type* out_type) {
491 #ifdef ASSERT
492 out_type = out_type->join(in_type);
493 if (in_type->empty() || out_type->empty()) {
494 return;
495 }
496 if (in_type == TypePtr::NULL_PTR || out_type == TypePtr::NULL_PTR) {
497 return;
498 }
499 if (!in_type->isa_oopptr() && !out_type->isa_oopptr()) {
500 return;
501 }
502
503 assert(in_type->isa_oopptr() && out_type->isa_oopptr(), "must be both oops or both non-oops");
504 if (in_type->isa_aryptr() && out_type->isa_aryptr()) {
505 const Type* e1 = in_type->is_aryptr()->elem();
506 const Type* e2 = out_type->is_aryptr()->elem();
507 assert(e1->basic_type() == e2->basic_type(), "must both be arrays of the same primitive type or both be oops arrays");
508 return;
509 }
510
511 assert(in_type->isa_instptr() && out_type->isa_instptr(), "must be both array oops or both non-array oops");
512 assert(in_type->is_instptr()->instance_klass() == out_type->is_instptr()->instance_klass(), "must not cast to a different type");
513 #endif // ASSERT
514 }
515
516 //------------------------------Value------------------------------------------
517 // Take 'join' of input and cast-up type, unless working with an Interface
518 const Type* CheckCastPPNode::Value(PhaseGVN* phase) const {
519 if( in(0) && phase->type(in(0)) == Type::TOP ) return Type::TOP;
520
521 const Type *inn = phase->type(in(1));
522 if( inn == Type::TOP ) return Type::TOP; // No information yet
523
524 if (inn->isa_oopptr() && _type->isa_oopptr()) {
525 return ConstraintCastNode::Value(phase);
526 }
527
528 const TypePtr *in_type = inn->isa_ptr();
529 const TypePtr *my_type = _type->isa_ptr();
530 const Type *result = _type;
531 if (in_type != nullptr && my_type != nullptr) {
532 // TODO 8302672
533 if (!StressReflectiveCode && my_type->isa_aryptr() && in_type->isa_aryptr()) {
534 // Propagate array properties (not flat/null-free)
535 // Don't do this when StressReflectiveCode is enabled because it might lead to
536 // a dying data path while the corresponding flat/null-free check is not folded.
537 my_type = my_type->is_aryptr()->update_properties(in_type->is_aryptr());
538 if (my_type == nullptr) {
539 return Type::TOP; // Inconsistent properties
540 }
541 }
542 TypePtr::PTR in_ptr = in_type->ptr();
543 if (in_ptr == TypePtr::Null) {
544 result = in_type;
545 } else if (in_ptr != TypePtr::Constant) {
546 result = my_type->cast_to_ptr_type(my_type->join_ptr(in_ptr));
547 }
548 }
549
550 return result;
551 }
552
553 Node* CheckCastPPNode::pin_node_under_control_impl() const {
554 assert(_dependency.is_floating(), "already pinned");
555 return new CheckCastPPNode(in(0), in(1), bottom_type(), _dependency.with_pinned_dependency(), _extra_types);
556 }
557
558 //=============================================================================
559 //------------------------------Value------------------------------------------
560 const Type* CastX2PNode::Value(PhaseGVN* phase) const {
561 const Type* t = phase->type(in(1));
562 if (t == Type::TOP) return Type::TOP;
563 if (t->base() == Type_X && t->singleton()) {
564 uintptr_t bits = (uintptr_t) t->is_intptr_t()->get_con();
565 if (bits == 0) return TypePtr::NULL_PTR;
566 return TypeRawPtr::make((address) bits);
567 }
568 return CastX2PNode::bottom_type();
569 }
570
571 //------------------------------Idealize---------------------------------------
572 static inline bool fits_in_int(const Type* t, bool but_not_min_int = false) {
573 if (t == Type::TOP) return false;
574 const TypeX* tl = t->is_intptr_t();
575 jint lo = min_jint;
576 jint hi = max_jint;
577 if (but_not_min_int) ++lo; // caller wants to negate the value w/o overflow
578 return (tl->_lo >= lo) && (tl->_hi <= hi);
579 }
580
581 static inline Node* addP_of_X2P(PhaseGVN *phase,
582 Node* base,
583 Node* dispX,
584 bool negate = false) {
585 if (negate) {
586 dispX = phase->transform(new SubXNode(phase->MakeConX(0), dispX));
587 }
588 return AddPNode::make_off_heap(phase->transform(new CastX2PNode(base)), dispX);
589 }
590
591 Node *CastX2PNode::Ideal(PhaseGVN *phase, bool can_reshape) {
592 // convert CastX2P(AddX(x, y)) to AddP(CastX2P(x), y) if y fits in an int
593 int op = in(1)->Opcode();
594 Node* x;
595 Node* y;
596 switch (op) {
597 case Op_SubX:
598 x = in(1)->in(1);
599 // Avoid ideal transformations ping-pong between this and AddP for raw pointers.
600 if (phase->find_intptr_t_con(x, -1) == 0)
601 break;
602 y = in(1)->in(2);
603 if (fits_in_int(phase->type(y), true)) {
604 return addP_of_X2P(phase, x, y, true);
605 }
606 break;
607 case Op_AddX:
608 x = in(1)->in(1);
609 y = in(1)->in(2);
610 if (fits_in_int(phase->type(y))) {
611 return addP_of_X2P(phase, x, y);
612 }
613 if (fits_in_int(phase->type(x))) {
614 return addP_of_X2P(phase, y, x);
615 }
616 break;
617 }
618 return nullptr;
619 }
620
621 //------------------------------Identity---------------------------------------
622 Node* CastX2PNode::Identity(PhaseGVN* phase) {
623 if (in(1)->Opcode() == Op_CastP2X) return in(1)->in(1);
624 return this;
625 }
626
627 //=============================================================================
628 //------------------------------Value------------------------------------------
629 const Type* CastP2XNode::Value(PhaseGVN* phase) const {
630 const Type* t = phase->type(in(1));
631 if (t == Type::TOP) return Type::TOP;
632 if (t->base() == Type::RawPtr && t->singleton()) {
633 uintptr_t bits = (uintptr_t) t->is_rawptr()->get_con();
634 return TypeX::make(bits);
635 }
636 return CastP2XNode::bottom_type();
637 }
638
639 Node *CastP2XNode::Ideal(PhaseGVN *phase, bool can_reshape) {
640 return (in(0) && remove_dead_region(phase, can_reshape)) ? this : nullptr;
641 }
642
643 //------------------------------Identity---------------------------------------
644 Node* CastP2XNode::Identity(PhaseGVN* phase) {
645 if (in(1)->Opcode() == Op_CastX2P) return in(1)->in(1);
646 return this;
647 }
648
649 Node* ConstraintCastNode::make_cast_for_type(Node* c, Node* in, const Type* type, const DependencyType& dependency,
650 const TypeTuple* types) {
651 if (type->isa_int()) {
652 return new CastIINode(c, in, type, dependency, false, types);
653 } else if (type->isa_long()) {
654 return new CastLLNode(c, in, type, dependency, types);
655 } else if (type->isa_half_float()) {
656 return new CastHHNode(c, in, type, dependency, types);
657 } else if (type->isa_float()) {
658 return new CastFFNode(c, in, type, dependency, types);
659 } else if (type->isa_double()) {
660 return new CastDDNode(c, in, type, dependency, types);
661 } else if (type->isa_vect()) {
662 return new CastVVNode(c, in, type, dependency, types);
663 } else if (type->isa_ptr()) {
664 return new CastPPNode(c, in, type, dependency, types);
665 }
666 fatal("unreachable. Invalid cast type.");
667 return nullptr;
668 }
669
670 Node* ConstraintCastNode::optimize_integer_cast_of_add(PhaseGVN* phase, BasicType bt) {
671 PhaseIterGVN *igvn = phase->is_IterGVN();
672 const TypeInteger* this_type = this->type()->isa_integer(bt);
673 if (this_type == nullptr) {
674 return nullptr;
675 }
676
677 Node* z = in(1);
678 const TypeInteger* rx = nullptr;
679 const TypeInteger* ry = nullptr;
680 // Similar to ConvI2LNode::Ideal() for the same reasons
681 if (Compile::push_thru_add(phase, z, this_type, rx, ry, bt, bt)) {
682 if (igvn == nullptr) {
683 // Postpone this optimization to iterative GVN, where we can handle deep
684 // AddI chains without an exponential number of recursive Ideal() calls.
685 phase->record_for_igvn(this);
686 return nullptr;
687 }
688 int op = z->Opcode();
689 Node* x = z->in(1);
690 Node* y = z->in(2);
691
692 const TypeInteger* tx = phase->type(x)->is_integer(bt);
693 const TypeInteger* ty = phase->type(y)->is_integer(bt);
694
695 // (Cast (Add x y) tz) is transformed into (Add (Cast x rx) (Cast y ry))
696 //
697 // tz = [tzlo, tzhi]
698 // rx = [rxlo, rxhi]
699 // ry = [rylo, ryhi]
700 // with type of x, tx = [txlo, txhi]
701 // with type of y, ty = [tylo, tyhi]
702 //
703 // From Compile::push_thru_add():
704 // rxlo = max(tzlo - tyhi, txlo)
705 // rxhi = min(tzhi - tylo, txhi)
706 // rylo = max(tzlo - txhi, tylo)
707 // ryhi = min(tzhi - txlo, tyhi)
708 //
709 // If x is a constant, then txlo = txhi
710 // rxlo = txlo, rxhi = txhi
711 // The bounds of the type of the Add after transformation then is:
712 // rxlo + rylo >= txlo + tzlo - txhi >= tzlo
713 // rxhi + ryhi <= txhi + tzhi - txlo <= tzhi
714 // The resulting type is not wider than the type of the Cast
715 // before transformation
716 //
717 // If neither x nor y are constant then the type of the resulting
718 // Add can be wider than the type of the type of the Cast before
719 // transformation.
720 // For instance, tx = [0, 10], ty = [0, 10], tz = [0, 10]
721 // then rx = [0, 10], ry = [0, 10]
722 // and rx + ry = [0, 20] which is wider than tz
723 //
724 // Same reasoning applies to (Cast (Sub x y) tz)
725 const DependencyType& dependency = (!tx->is_con() && !ty->is_con()) ? _dependency.with_non_narrowing() : _dependency;
726 Node* cx = find_or_make_integer_cast(igvn, x, rx, dependency);
727 Node* cy = find_or_make_integer_cast(igvn, y, ry, dependency);
728 if (op == Op_Add(bt)) {
729 return AddNode::make(cx, cy, bt);
730 } else {
731 assert(op == Op_Sub(bt), "");
732 return SubNode::make(cx, cy, bt);
733 }
734 return nullptr;
735 }
736 return nullptr;
737 }
738
739 Node* ConstraintCastNode::optimize_integer_cast(PhaseGVN* phase, BasicType bt) {
740 Node* res = optimize_integer_cast_of_add(phase, bt);
741 if (res != nullptr) {
742 return res;
743 }
744 const Type* t = Value(phase);
745 if (t != Type::TOP && phase->C->post_loop_opts_phase()) {
746 const Type* bottom_t = bottom_type();
747 const TypeInteger* wide_t = widen_type(phase, bottom_t, bt);
748 if (wide_t != bottom_t) {
749 // Widening the type of the Cast (to allow some commoning) causes the Cast to change how it can be optimized (if
750 // type of its input is narrower than the Cast's type, we can't remove it to not loose the control dependency).
751 return make_with(in(1), wide_t, _dependency.with_non_narrowing());
752 }
753 }
754 return nullptr;
755 }
756
757 const TypeInteger* ConstraintCastNode::widen_type(const PhaseGVN* phase, const Type* res, BasicType bt) const {
758 const TypeInteger* this_type = res->is_integer(bt);
759 // At VerifyConstraintCasts == 1, we verify the ConstraintCastNodes that are present during code
760 // emission. This allows us detecting possible mis-scheduling due to these nodes being pinned at
761 // the wrong control nodes.
762 // At VerifyConstraintCasts == 2, we do not perform widening so that we can verify the
763 // correctness of more ConstraintCastNodes. This further helps us detect possible
764 // mis-transformations that may happen due to these nodes being pinned at the wrong control
765 // nodes.
766 if (VerifyConstraintCasts > 1) {
767 return this_type;
768 }
769
770 const TypeInteger* in_type = phase->type(in(1))->isa_integer(bt);
771 if (in_type != nullptr &&
772 (in_type->lo_as_long() != this_type->lo_as_long() ||
773 in_type->hi_as_long() != this_type->hi_as_long())) {
774 jlong lo1 = this_type->lo_as_long();
775 jlong hi1 = this_type->hi_as_long();
776 int w1 = this_type->_widen;
777 if (lo1 >= 0) {
778 // Keep a range assertion of >=0.
779 lo1 = 0; hi1 = max_signed_integer(bt);
780 } else if (hi1 < 0) {
781 // Keep a range assertion of <0.
782 lo1 = min_signed_integer(bt); hi1 = -1;
783 } else {
784 lo1 = min_signed_integer(bt); hi1 = max_signed_integer(bt);
785 }
786 return TypeInteger::make(MAX2(in_type->lo_as_long(), lo1),
787 MIN2(in_type->hi_as_long(), hi1),
788 MAX2((int)in_type->_widen, w1), bt);
789 }
790 return this_type;
791 }