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/graphKit.hpp" 31 #include "opto/inlinetypenode.hpp" 32 #include "opto/matcher.hpp" 33 #include "opto/phaseX.hpp" 34 #include "opto/rootnode.hpp" 35 #include "opto/subnode.hpp" 36 #include "opto/type.hpp" 37 #include "castnode.hpp" 38 #include "utilities/checkedCast.hpp" 39 40 //============================================================================= 41 // If input is already higher or equal to cast type, then this is an identity. 42 Node* ConstraintCastNode::Identity(PhaseGVN* phase) { 43 if (_dependency == UnconditionalDependency) { 44 return this; 45 } 46 Node* dom = dominating_cast(phase, phase); 47 if (dom != nullptr) { 48 return dom; 49 } 50 return higher_equal_types(phase, in(1)) ? in(1) : this; 51 } 52 53 //------------------------------Value------------------------------------------ 54 // Take 'join' of input and cast-up type 55 const Type* ConstraintCastNode::Value(PhaseGVN* phase) const { 56 if (in(0) && phase->type(in(0)) == Type::TOP) return Type::TOP; 57 58 const Type* in_type = phase->type(in(1)); 59 const Type* ft = in_type->filter_speculative(_type); 60 61 // Check if both _type and in_type had a speculative type, but for the just 62 // computed ft the speculative type was dropped. 63 if (ft->speculative() == nullptr && 64 _type->speculative() != nullptr && 65 in_type->speculative() != nullptr) { 66 // Speculative type may have disagreed between cast and input, and was 67 // dropped in filtering. Recompute so that ft can take speculative type 68 // of in_type. If we did not do it now, a subsequent ::Value call would 69 // do it, and violate idempotence of ::Value. 70 ft = in_type->filter_speculative(ft); 71 } 72 73 #ifdef ASSERT 74 // Previous versions of this function had some special case logic, 75 // which is no longer necessary. Make sure of the required effects. 76 switch (Opcode()) { 77 case Op_CastII: 78 { 79 if (in_type == Type::TOP) { 80 assert(ft == Type::TOP, "special case #1"); 81 } 82 const Type* rt = in_type->join_speculative(_type); 83 if (rt->empty()) { 84 assert(ft == Type::TOP, "special case #2"); 85 } 86 break; 87 } 88 case Op_CastPP: 89 if (in_type == TypePtr::NULL_PTR && 90 _type->isa_ptr() && _type->is_ptr()->_ptr == TypePtr::NotNull) { 91 assert(ft == Type::TOP, "special case #3"); 92 break; 93 } 94 } 95 #endif //ASSERT 96 97 return ft; 98 } 99 100 //------------------------------Ideal------------------------------------------ 101 // Return a node which is more "ideal" than the current node. Strip out 102 // control copies 103 Node *ConstraintCastNode::Ideal(PhaseGVN *phase, bool can_reshape) { 104 if (in(0) && remove_dead_region(phase, can_reshape)) { 105 return this; 106 } 107 108 // Push cast through InlineTypeNode 109 InlineTypeNode* vt = in(1)->isa_InlineType(); 110 if (vt != nullptr && phase->type(vt)->filter_speculative(_type) != Type::TOP) { 111 // TODO 8325106 Can we avoid cloning? 112 Node* cast = clone(); 113 cast->set_req(1, vt->get_oop()); 114 vt = vt->clone()->as_InlineType(); 115 if (!_type->maybe_null()) { 116 vt->as_InlineType()->set_is_init(*phase); 117 } 118 vt->set_oop(*phase, phase->transform(cast)); 119 return vt; 120 } 121 122 return nullptr; 123 } 124 125 uint ConstraintCastNode::hash() const { 126 return TypeNode::hash() + (int)_dependency + (_extra_types != nullptr ? _extra_types->hash() : 0); 127 } 128 129 bool ConstraintCastNode::cmp(const Node &n) const { 130 if (!TypeNode::cmp(n)) { 131 return false; 132 } 133 ConstraintCastNode& cast = (ConstraintCastNode&) n; 134 if (cast._dependency != _dependency) { 135 return false; 136 } 137 if (_extra_types == nullptr || cast._extra_types == nullptr) { 138 return _extra_types == cast._extra_types; 139 } 140 return _extra_types->eq(cast._extra_types); 141 } 142 143 uint ConstraintCastNode::size_of() const { 144 return sizeof(*this); 145 } 146 147 Node* ConstraintCastNode::make_cast(int opcode, Node* c, Node* n, const Type* t, DependencyType dependency, 148 const TypeTuple* extra_types) { 149 switch(opcode) { 150 case Op_CastII: { 151 Node* cast = new CastIINode(n, t, dependency, false, extra_types); 152 cast->set_req(0, c); 153 return cast; 154 } 155 case Op_CastLL: { 156 Node* cast = new CastLLNode(n, t, dependency, extra_types); 157 cast->set_req(0, c); 158 return cast; 159 } 160 case Op_CastPP: { 161 Node* cast = new CastPPNode(n, t, dependency, extra_types); 162 cast->set_req(0, c); 163 return cast; 164 } 165 case Op_CastFF: { 166 Node* cast = new CastFFNode(n, t, dependency, extra_types); 167 cast->set_req(0, c); 168 return cast; 169 } 170 case Op_CastDD: { 171 Node* cast = new CastDDNode(n, t, dependency, extra_types); 172 cast->set_req(0, c); 173 return cast; 174 } 175 case Op_CastVV: { 176 Node* cast = new CastVVNode(n, t, dependency, extra_types); 177 cast->set_req(0, c); 178 return cast; 179 } 180 case Op_CheckCastPP: return new CheckCastPPNode(c, n, t, dependency, extra_types); 181 default: 182 fatal("Bad opcode %d", opcode); 183 } 184 return nullptr; 185 } 186 187 Node* ConstraintCastNode::make(Node* c, Node *n, const Type *t, DependencyType dependency, BasicType bt) { 188 switch(bt) { 189 case T_INT: { 190 return make_cast(Op_CastII, c, n, t, dependency, nullptr); 191 } 192 case T_LONG: { 193 return make_cast(Op_CastLL, c, n, t, dependency, nullptr); 194 } 195 default: 196 fatal("Bad basic type %s", type2name(bt)); 197 } 198 return nullptr; 199 } 200 201 TypeNode* ConstraintCastNode::dominating_cast(PhaseGVN* gvn, PhaseTransform* pt) const { 202 if (_dependency == UnconditionalDependency) { 203 return nullptr; 204 } 205 Node* val = in(1); 206 Node* ctl = in(0); 207 int opc = Opcode(); 208 if (ctl == nullptr) { 209 return nullptr; 210 } 211 // Range check CastIIs may all end up under a single range check and 212 // in that case only the narrower CastII would be kept by the code 213 // below which would be incorrect. 214 if (is_CastII() && as_CastII()->has_range_check()) { 215 return nullptr; 216 } 217 if (type()->isa_rawptr() && (gvn->type_or_null(val) == nullptr || gvn->type(val)->isa_oopptr())) { 218 return nullptr; 219 } 220 for (DUIterator_Fast imax, i = val->fast_outs(imax); i < imax; i++) { 221 Node* u = val->fast_out(i); 222 if (u != this && 223 u->outcnt() > 0 && 224 u->Opcode() == opc && 225 u->in(0) != nullptr && 226 higher_equal_types(gvn, u)) { 227 if (pt->is_dominator(u->in(0), ctl)) { 228 return u->as_Type(); 229 } 230 if (is_CheckCastPP() && u->in(1)->is_Proj() && u->in(1)->in(0)->is_Allocate() && 231 u->in(0)->is_Proj() && u->in(0)->in(0)->is_Initialize() && 232 u->in(1)->in(0)->as_Allocate()->initialization() == u->in(0)->in(0)) { 233 // CheckCastPP following an allocation always dominates all 234 // use of the allocation result 235 return u->as_Type(); 236 } 237 } 238 } 239 return nullptr; 240 } 241 242 bool ConstraintCastNode::higher_equal_types(PhaseGVN* phase, const Node* other) const { 243 const Type* t = phase->type(other); 244 if (!t->higher_equal_speculative(type())) { 245 return false; 246 } 247 if (_extra_types != nullptr) { 248 for (uint i = 0; i < _extra_types->cnt(); ++i) { 249 if (!t->higher_equal_speculative(_extra_types->field_at(i))) { 250 return false; 251 } 252 } 253 } 254 return true; 255 } 256 257 #ifndef PRODUCT 258 void ConstraintCastNode::dump_spec(outputStream *st) const { 259 TypeNode::dump_spec(st); 260 if (_extra_types != nullptr) { 261 st->print(" extra types: "); 262 _extra_types->dump_on(st); 263 } 264 if (_dependency != RegularDependency) { 265 st->print(" %s dependency", _dependency == StrongDependency ? "strong" : "unconditional"); 266 } 267 } 268 #endif 269 270 const Type* CastIINode::Value(PhaseGVN* phase) const { 271 const Type *res = ConstraintCastNode::Value(phase); 272 if (res == Type::TOP) { 273 return Type::TOP; 274 } 275 assert(res->isa_int(), "res must be int"); 276 277 // Similar to ConvI2LNode::Value() for the same reasons 278 // see if we can remove type assertion after loop opts 279 // But here we have to pay extra attention: 280 // Do not narrow the type of range check dependent CastIINodes to 281 // avoid corruption of the graph if a CastII is replaced by TOP but 282 // the corresponding range check is not removed. 283 if (!_range_check_dependency) { 284 res = widen_type(phase, res, T_INT); 285 } 286 287 return res; 288 } 289 290 static Node* find_or_make_integer_cast(PhaseIterGVN* igvn, Node* parent, Node* control, const TypeInteger* type, ConstraintCastNode::DependencyType dependency, BasicType bt) { 291 Node* n = ConstraintCastNode::make(control, parent, type, dependency, bt); 292 Node* existing = igvn->hash_find_insert(n); 293 if (existing != nullptr) { 294 n->destruct(igvn); 295 return existing; 296 } 297 return igvn->register_new_node_with_optimizer(n); 298 } 299 300 Node *CastIINode::Ideal(PhaseGVN *phase, bool can_reshape) { 301 Node* progress = ConstraintCastNode::Ideal(phase, can_reshape); 302 if (progress != nullptr) { 303 return progress; 304 } 305 if (can_reshape && !_range_check_dependency && !phase->C->post_loop_opts_phase()) { 306 // makes sure we run ::Value to potentially remove type assertion after loop opts 307 phase->C->record_for_post_loop_opts_igvn(this); 308 } 309 if (!_range_check_dependency) { 310 return optimize_integer_cast(phase, T_INT); 311 } 312 return nullptr; 313 } 314 315 Node* CastIINode::Identity(PhaseGVN* phase) { 316 Node* progress = ConstraintCastNode::Identity(phase); 317 if (progress != this) { 318 return progress; 319 } 320 if (_range_check_dependency) { 321 if (phase->C->post_loop_opts_phase()) { 322 return this->in(1); 323 } else { 324 phase->C->record_for_post_loop_opts_igvn(this); 325 } 326 } 327 return this; 328 } 329 330 bool CastIINode::cmp(const Node &n) const { 331 return ConstraintCastNode::cmp(n) && ((CastIINode&)n)._range_check_dependency == _range_check_dependency; 332 } 333 334 uint CastIINode::size_of() const { 335 return sizeof(*this); 336 } 337 338 #ifndef PRODUCT 339 void CastIINode::dump_spec(outputStream* st) const { 340 ConstraintCastNode::dump_spec(st); 341 if (_range_check_dependency) { 342 st->print(" range check dependency"); 343 } 344 } 345 #endif 346 347 const Type* CastLLNode::Value(PhaseGVN* phase) const { 348 const Type* res = ConstraintCastNode::Value(phase); 349 if (res == Type::TOP) { 350 return Type::TOP; 351 } 352 assert(res->isa_long(), "res must be long"); 353 354 return widen_type(phase, res, T_LONG); 355 } 356 357 Node* CastLLNode::Ideal(PhaseGVN* phase, bool can_reshape) { 358 Node* progress = ConstraintCastNode::Ideal(phase, can_reshape); 359 if (progress != nullptr) { 360 return progress; 361 } 362 if (!phase->C->post_loop_opts_phase()) { 363 // makes sure we run ::Value to potentially remove type assertion after loop opts 364 phase->C->record_for_post_loop_opts_igvn(this); 365 } 366 // transform (CastLL (ConvI2L ..)) into (ConvI2L (CastII ..)) if the type of the CastLL is narrower than the type of 367 // the ConvI2L. 368 Node* in1 = in(1); 369 if (in1 != nullptr && in1->Opcode() == Op_ConvI2L) { 370 const Type* t = Value(phase); 371 const Type* t_in = phase->type(in1); 372 if (t != Type::TOP && t_in != Type::TOP) { 373 const TypeLong* tl = t->is_long(); 374 const TypeLong* t_in_l = t_in->is_long(); 375 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"); 376 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"); 377 if (tl != t_in_l) { 378 const TypeInt* ti = TypeInt::make(checked_cast<jint>(tl->_lo), checked_cast<jint>(tl->_hi), tl->_widen); 379 Node* castii = phase->transform(new CastIINode(in(0), in1->in(1), ti)); 380 Node* convi2l = in1->clone(); 381 convi2l->set_req(1, castii); 382 return convi2l; 383 } 384 } 385 } 386 return optimize_integer_cast(phase, T_LONG); 387 } 388 389 //============================================================================= 390 //------------------------------Identity--------------------------------------- 391 // If input is already higher or equal to cast type, then this is an identity. 392 Node* CheckCastPPNode::Identity(PhaseGVN* phase) { 393 if (in(1)->is_InlineType() && _type->isa_instptr() && phase->type(in(1))->inline_klass()->is_subtype_of(_type->is_instptr()->instance_klass())) { 394 return in(1); 395 } 396 return ConstraintCastNode::Identity(phase); 397 } 398 399 //------------------------------Value------------------------------------------ 400 // Take 'join' of input and cast-up type, unless working with an Interface 401 const Type* CheckCastPPNode::Value(PhaseGVN* phase) const { 402 if( in(0) && phase->type(in(0)) == Type::TOP ) return Type::TOP; 403 404 const Type *inn = phase->type(in(1)); 405 if( inn == Type::TOP ) return Type::TOP; // No information yet 406 407 if (inn->isa_oopptr() && _type->isa_oopptr()) { 408 return ConstraintCastNode::Value(phase); 409 } 410 411 const TypePtr *in_type = inn->isa_ptr(); 412 const TypePtr *my_type = _type->isa_ptr(); 413 const Type *result = _type; 414 if (in_type != nullptr && my_type != nullptr) { 415 // TODO 8302672 416 if (!StressReflectiveCode && my_type->isa_aryptr() && in_type->isa_aryptr()) { 417 // Propagate array properties (not flat/null-free) 418 // Don't do this when StressReflectiveCode is enabled because it might lead to 419 // a dying data path while the corresponding flat/null-free check is not folded. 420 my_type = my_type->is_aryptr()->update_properties(in_type->is_aryptr()); 421 if (my_type == nullptr) { 422 return Type::TOP; // Inconsistent properties 423 } 424 } 425 TypePtr::PTR in_ptr = in_type->ptr(); 426 if (in_ptr == TypePtr::Null) { 427 result = in_type; 428 } else if (in_ptr != TypePtr::Constant) { 429 result = my_type->cast_to_ptr_type(my_type->join_ptr(in_ptr)); 430 } 431 } 432 433 return result; 434 } 435 436 //============================================================================= 437 //------------------------------Value------------------------------------------ 438 const Type* CastX2PNode::Value(PhaseGVN* phase) const { 439 const Type* t = phase->type(in(1)); 440 if (t == Type::TOP) return Type::TOP; 441 if (t->base() == Type_X && t->singleton()) { 442 uintptr_t bits = (uintptr_t) t->is_intptr_t()->get_con(); 443 if (bits == 0) return TypePtr::NULL_PTR; 444 return TypeRawPtr::make((address) bits); 445 } 446 return CastX2PNode::bottom_type(); 447 } 448 449 //------------------------------Idealize--------------------------------------- 450 static inline bool fits_in_int(const Type* t, bool but_not_min_int = false) { 451 if (t == Type::TOP) return false; 452 const TypeX* tl = t->is_intptr_t(); 453 jint lo = min_jint; 454 jint hi = max_jint; 455 if (but_not_min_int) ++lo; // caller wants to negate the value w/o overflow 456 return (tl->_lo >= lo) && (tl->_hi <= hi); 457 } 458 459 static inline Node* addP_of_X2P(PhaseGVN *phase, 460 Node* base, 461 Node* dispX, 462 bool negate = false) { 463 if (negate) { 464 dispX = phase->transform(new SubXNode(phase->MakeConX(0), dispX)); 465 } 466 return new AddPNode(phase->C->top(), 467 phase->transform(new CastX2PNode(base)), 468 dispX); 469 } 470 471 Node *CastX2PNode::Ideal(PhaseGVN *phase, bool can_reshape) { 472 // convert CastX2P(AddX(x, y)) to AddP(CastX2P(x), y) if y fits in an int 473 int op = in(1)->Opcode(); 474 Node* x; 475 Node* y; 476 switch (op) { 477 case Op_SubX: 478 x = in(1)->in(1); 479 // Avoid ideal transformations ping-pong between this and AddP for raw pointers. 480 if (phase->find_intptr_t_con(x, -1) == 0) 481 break; 482 y = in(1)->in(2); 483 if (fits_in_int(phase->type(y), true)) { 484 return addP_of_X2P(phase, x, y, true); 485 } 486 break; 487 case Op_AddX: 488 x = in(1)->in(1); 489 y = in(1)->in(2); 490 if (fits_in_int(phase->type(y))) { 491 return addP_of_X2P(phase, x, y); 492 } 493 if (fits_in_int(phase->type(x))) { 494 return addP_of_X2P(phase, y, x); 495 } 496 break; 497 } 498 return nullptr; 499 } 500 501 //------------------------------Identity--------------------------------------- 502 Node* CastX2PNode::Identity(PhaseGVN* phase) { 503 if (in(1)->Opcode() == Op_CastP2X) return in(1)->in(1); 504 return this; 505 } 506 507 //============================================================================= 508 //------------------------------Value------------------------------------------ 509 const Type* CastP2XNode::Value(PhaseGVN* phase) const { 510 const Type* t = phase->type(in(1)); 511 if (t == Type::TOP) return Type::TOP; 512 if (t->base() == Type::RawPtr && t->singleton()) { 513 uintptr_t bits = (uintptr_t) t->is_rawptr()->get_con(); 514 return TypeX::make(bits); 515 } 516 517 if (t->is_zero_type() || !t->maybe_null()) { 518 for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) { 519 Node* u = fast_out(i); 520 if (u->Opcode() == Op_OrL) { 521 for (DUIterator_Fast jmax, j = u->fast_outs(jmax); j < jmax; j++) { 522 Node* cmp = u->fast_out(j); 523 if (cmp->Opcode() == Op_CmpL) { 524 // Give CmpL a chance to get optimized 525 phase->record_for_igvn(cmp); 526 } 527 } 528 } 529 } 530 } 531 532 return CastP2XNode::bottom_type(); 533 } 534 535 Node *CastP2XNode::Ideal(PhaseGVN *phase, bool can_reshape) { 536 return (in(0) && remove_dead_region(phase, can_reshape)) ? this : nullptr; 537 } 538 539 //------------------------------Identity--------------------------------------- 540 Node* CastP2XNode::Identity(PhaseGVN* phase) { 541 if (in(1)->Opcode() == Op_CastX2P) return in(1)->in(1); 542 return this; 543 } 544 545 Node* ConstraintCastNode::make_cast_for_type(Node* c, Node* in, const Type* type, DependencyType dependency, 546 const TypeTuple* types) { 547 Node* cast= nullptr; 548 if (type->isa_int()) { 549 cast = make_cast(Op_CastII, c, in, type, dependency, types); 550 } else if (type->isa_long()) { 551 cast = make_cast(Op_CastLL, c, in, type, dependency, types); 552 } else if (type->isa_float()) { 553 cast = make_cast(Op_CastFF, c, in, type, dependency, types); 554 } else if (type->isa_double()) { 555 cast = make_cast(Op_CastDD, c, in, type, dependency, types); 556 } else if (type->isa_vect()) { 557 cast = make_cast(Op_CastVV, c, in, type, dependency, types); 558 } else if (type->isa_ptr()) { 559 cast = make_cast(Op_CastPP, c, in, type, dependency, types); 560 } 561 return cast; 562 } 563 564 Node* ConstraintCastNode::optimize_integer_cast(PhaseGVN* phase, BasicType bt) { 565 PhaseIterGVN *igvn = phase->is_IterGVN(); 566 const TypeInteger* this_type = this->type()->is_integer(bt); 567 Node* z = in(1); 568 const TypeInteger* rx = nullptr; 569 const TypeInteger* ry = nullptr; 570 // Similar to ConvI2LNode::Ideal() for the same reasons 571 if (Compile::push_thru_add(phase, z, this_type, rx, ry, bt, bt)) { 572 if (igvn == nullptr) { 573 // Postpone this optimization to iterative GVN, where we can handle deep 574 // AddI chains without an exponential number of recursive Ideal() calls. 575 phase->record_for_igvn(this); 576 return nullptr; 577 } 578 int op = z->Opcode(); 579 Node* x = z->in(1); 580 Node* y = z->in(2); 581 582 Node* cx = find_or_make_integer_cast(igvn, x, in(0), rx, _dependency, bt); 583 Node* cy = find_or_make_integer_cast(igvn, y, in(0), ry, _dependency, bt); 584 if (op == Op_Add(bt)) { 585 return AddNode::make(cx, cy, bt); 586 } else { 587 assert(op == Op_Sub(bt), ""); 588 return SubNode::make(cx, cy, bt); 589 } 590 return nullptr; 591 } 592 return nullptr; 593 } 594 595 const Type* ConstraintCastNode::widen_type(const PhaseGVN* phase, const Type* res, BasicType bt) const { 596 if (!phase->C->post_loop_opts_phase()) { 597 return res; 598 } 599 const TypeInteger* this_type = res->is_integer(bt); 600 const TypeInteger* in_type = phase->type(in(1))->isa_integer(bt); 601 if (in_type != nullptr && 602 (in_type->lo_as_long() != this_type->lo_as_long() || 603 in_type->hi_as_long() != this_type->hi_as_long())) { 604 jlong lo1 = this_type->lo_as_long(); 605 jlong hi1 = this_type->hi_as_long(); 606 int w1 = this_type->_widen; 607 if (lo1 >= 0) { 608 // Keep a range assertion of >=0. 609 lo1 = 0; hi1 = max_signed_integer(bt); 610 } else if (hi1 < 0) { 611 // Keep a range assertion of <0. 612 lo1 = min_signed_integer(bt); hi1 = -1; 613 } else { 614 lo1 = min_signed_integer(bt); hi1 = max_signed_integer(bt); 615 } 616 return TypeInteger::make(MAX2(in_type->lo_as_long(), lo1), 617 MIN2(in_type->hi_as_long(), hi1), 618 MAX2((int)in_type->_widen, w1), bt); 619 } 620 return res; 621 }