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