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