1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "opto/addnode.hpp" 27 #include "opto/callnode.hpp" 28 #include "opto/castnode.hpp" 29 #include "opto/connode.hpp" 30 #include "opto/matcher.hpp" 31 #include "opto/phaseX.hpp" 32 #include "opto/rootnode.hpp" 33 #include "opto/subnode.hpp" 34 #include "opto/type.hpp" 35 #include "opto/valuetypenode.hpp" 36 37 //============================================================================= 38 // If input is already higher or equal to cast type, then this is an identity. 39 Node* ConstraintCastNode::Identity(PhaseGVN* phase) { 40 Node* dom = dominating_cast(phase, phase); 41 if (dom != NULL) { 42 return dom; 43 } 44 if (_carry_dependency) { 45 return this; 46 } 47 return phase->type(in(1))->higher_equal_speculative(_type) ? in(1) : this; 48 } 49 50 //------------------------------Value------------------------------------------ 51 // Take 'join' of input and cast-up type 52 const Type* ConstraintCastNode::Value(PhaseGVN* phase) const { 53 if (in(0) && phase->type(in(0)) == Type::TOP) return Type::TOP; 54 const Type* ft = phase->type(in(1))->filter_speculative(_type); 55 56 #ifdef ASSERT 57 // Previous versions of this function had some special case logic, 58 // which is no longer necessary. Make sure of the required effects. 59 switch (Opcode()) { 60 case Op_CastII: 61 { 62 const Type* t1 = phase->type(in(1)); 63 if( t1 == Type::TOP ) assert(ft == Type::TOP, "special case #1"); 64 const Type* rt = t1->join_speculative(_type); 65 if (rt->empty()) assert(ft == Type::TOP, "special case #2"); 66 break; 67 } 68 case Op_CastPP: 69 if (phase->type(in(1)) == TypePtr::NULL_PTR && 70 _type->isa_ptr() && _type->is_ptr()->_ptr == TypePtr::NotNull) 71 assert(ft == Type::TOP, "special case #3"); 72 break; 73 } 74 #endif //ASSERT 75 76 return ft; 77 } 78 79 //------------------------------Ideal------------------------------------------ 80 // Return a node which is more "ideal" than the current node. Strip out 81 // control copies 82 Node *ConstraintCastNode::Ideal(PhaseGVN *phase, bool can_reshape) { 83 return (in(0) && remove_dead_region(phase, can_reshape)) ? this : NULL; 84 } 85 86 uint ConstraintCastNode::cmp(const Node &n) const { 87 return TypeNode::cmp(n) && ((ConstraintCastNode&)n)._carry_dependency == _carry_dependency; 88 } 89 90 uint ConstraintCastNode::size_of() const { 91 return sizeof(*this); 92 } 93 94 Node* ConstraintCastNode::make_cast(int opcode, Node* c, Node *n, const Type *t, bool carry_dependency) { 95 switch(opcode) { 96 case Op_CastII: { 97 Node* cast = new CastIINode(n, t, carry_dependency); 98 cast->set_req(0, c); 99 return cast; 100 } 101 case Op_CastPP: { 102 Node* cast = new CastPPNode(n, t, carry_dependency); 103 cast->set_req(0, c); 104 return cast; 105 } 106 case Op_CheckCastPP: return new CheckCastPPNode(c, n, t, carry_dependency); 107 default: 108 fatal("Bad opcode %d", opcode); 109 } 110 return NULL; 111 } 112 113 TypeNode* ConstraintCastNode::dominating_cast(PhaseGVN* gvn, PhaseTransform* pt) const { 114 Node* val = in(1); 115 Node* ctl = in(0); 116 int opc = Opcode(); 117 if (ctl == NULL) { 118 return NULL; 119 } 120 // Range check CastIIs may all end up under a single range check and 121 // in that case only the narrower CastII would be kept by the code 122 // below which would be incorrect. 123 if (is_CastII() && as_CastII()->has_range_check()) { 124 return NULL; 125 } 126 if (type()->isa_rawptr() && (gvn->type_or_null(val) == NULL || gvn->type(val)->isa_oopptr())) { 127 return NULL; 128 } 129 for (DUIterator_Fast imax, i = val->fast_outs(imax); i < imax; i++) { 130 Node* u = val->fast_out(i); 131 if (u != this && 132 u->outcnt() > 0 && 133 u->Opcode() == opc && 134 u->in(0) != NULL && 135 u->bottom_type()->higher_equal(type())) { 136 if (pt->is_dominator(u->in(0), ctl)) { 137 return u->as_Type(); 138 } 139 if (is_CheckCastPP() && u->in(1)->is_Proj() && u->in(1)->in(0)->is_Allocate() && 140 u->in(0)->is_Proj() && u->in(0)->in(0)->is_Initialize() && 141 u->in(1)->in(0)->as_Allocate()->initialization() == u->in(0)->in(0)) { 142 // CheckCastPP following an allocation always dominates all 143 // use of the allocation result 144 return u->as_Type(); 145 } 146 } 147 } 148 return NULL; 149 } 150 151 #ifndef PRODUCT 152 void ConstraintCastNode::dump_spec(outputStream *st) const { 153 TypeNode::dump_spec(st); 154 if (_carry_dependency) { 155 st->print(" carry dependency"); 156 } 157 } 158 #endif 159 160 const Type* CastIINode::Value(PhaseGVN* phase) const { 161 const Type *res = ConstraintCastNode::Value(phase); 162 163 // Try to improve the type of the CastII if we recognize a CmpI/If 164 // pattern. 165 if (_carry_dependency) { 166 if (in(0) != NULL && in(0)->in(0) != NULL && in(0)->in(0)->is_If()) { 167 assert(in(0)->is_IfFalse() || in(0)->is_IfTrue(), "should be If proj"); 168 Node* proj = in(0); 169 if (proj->in(0)->in(1)->is_Bool()) { 170 Node* b = proj->in(0)->in(1); 171 if (b->in(1)->Opcode() == Op_CmpI) { 172 Node* cmp = b->in(1); 173 if (cmp->in(1) == in(1) && phase->type(cmp->in(2))->isa_int()) { 174 const TypeInt* in2_t = phase->type(cmp->in(2))->is_int(); 175 const Type* t = TypeInt::INT; 176 BoolTest test = b->as_Bool()->_test; 177 if (proj->is_IfFalse()) { 178 test = test.negate(); 179 } 180 BoolTest::mask m = test._test; 181 jlong lo_long = min_jint; 182 jlong hi_long = max_jint; 183 if (m == BoolTest::le || m == BoolTest::lt) { 184 hi_long = in2_t->_hi; 185 if (m == BoolTest::lt) { 186 hi_long -= 1; 187 } 188 } else if (m == BoolTest::ge || m == BoolTest::gt) { 189 lo_long = in2_t->_lo; 190 if (m == BoolTest::gt) { 191 lo_long += 1; 192 } 193 } else if (m == BoolTest::eq) { 194 lo_long = in2_t->_lo; 195 hi_long = in2_t->_hi; 196 } else if (m == BoolTest::ne) { 197 // can't do any better 198 } else { 199 stringStream ss; 200 test.dump_on(&ss); 201 fatal("unexpected comparison %s", ss.as_string()); 202 } 203 int lo_int = (int)lo_long; 204 int hi_int = (int)hi_long; 205 206 if (lo_long != (jlong)lo_int) { 207 lo_int = min_jint; 208 } 209 if (hi_long != (jlong)hi_int) { 210 hi_int = max_jint; 211 } 212 213 t = TypeInt::make(lo_int, hi_int, Type::WidenMax); 214 215 res = res->filter_speculative(t); 216 217 return res; 218 } 219 } 220 } 221 } 222 } 223 return res; 224 } 225 226 Node *CastIINode::Ideal(PhaseGVN *phase, bool can_reshape) { 227 Node* progress = ConstraintCastNode::Ideal(phase, can_reshape); 228 if (progress != NULL) { 229 return progress; 230 } 231 232 // Similar to ConvI2LNode::Ideal() for the same reasons 233 // Do not narrow the type of range check dependent CastIINodes to 234 // avoid corruption of the graph if a CastII is replaced by TOP but 235 // the corresponding range check is not removed. 236 if (can_reshape && !_range_check_dependency && !phase->C->major_progress()) { 237 const TypeInt* this_type = this->type()->is_int(); 238 const TypeInt* in_type = phase->type(in(1))->isa_int(); 239 if (in_type != NULL && this_type != NULL && 240 (in_type->_lo != this_type->_lo || 241 in_type->_hi != this_type->_hi)) { 242 int lo1 = this_type->_lo; 243 int hi1 = this_type->_hi; 244 int w1 = this_type->_widen; 245 246 if (lo1 >= 0) { 247 // Keep a range assertion of >=0. 248 lo1 = 0; hi1 = max_jint; 249 } else if (hi1 < 0) { 250 // Keep a range assertion of <0. 251 lo1 = min_jint; hi1 = -1; 252 } else { 253 lo1 = min_jint; hi1 = max_jint; 254 } 255 const TypeInt* wtype = TypeInt::make(MAX2(in_type->_lo, lo1), 256 MIN2(in_type->_hi, hi1), 257 MAX2((int)in_type->_widen, w1)); 258 if (wtype != type()) { 259 set_type(wtype); 260 return this; 261 } 262 } 263 } 264 return NULL; 265 } 266 267 uint CastIINode::cmp(const Node &n) const { 268 return ConstraintCastNode::cmp(n) && ((CastIINode&)n)._range_check_dependency == _range_check_dependency; 269 } 270 271 uint CastIINode::size_of() const { 272 return sizeof(*this); 273 } 274 275 #ifndef PRODUCT 276 void CastIINode::dump_spec(outputStream* st) const { 277 ConstraintCastNode::dump_spec(st); 278 if (_range_check_dependency) { 279 st->print(" range check dependency"); 280 } 281 } 282 #endif 283 284 //============================================================================= 285 //------------------------------Identity--------------------------------------- 286 // If input is already higher or equal to cast type, then this is an identity. 287 Node* CheckCastPPNode::Identity(PhaseGVN* phase) { 288 // This is a value type, its input is a phi. That phi is also a 289 // value type of that same type and its inputs are value types of 290 // the same type: push the cast through the phi. 291 if (phase->is_IterGVN() && 292 in(0) == NULL && 293 type()->isa_valuetypeptr() && 294 in(1) != NULL && 295 in(1)->is_Phi()) { 296 PhaseIterGVN* igvn = phase->is_IterGVN(); 297 Node* phi = in(1); 298 const Type* vtptr = type(); 299 for (uint i = 1; i < phi->req(); i++) { 300 if (phi->in(i) != NULL && !phase->type(phi->in(i))->higher_equal(vtptr)) { 301 Node* cast = phase->transform(new CheckCastPPNode(NULL, phi->in(i), vtptr)); 302 igvn->replace_input_of(phi, i, cast); 303 } 304 } 305 return phi; 306 } 307 308 Node* dom = dominating_cast(phase, phase); 309 if (dom != NULL) { 310 return dom; 311 } 312 if (_carry_dependency) { 313 return this; 314 } 315 // Toned down to rescue meeting at a Phi 3 different oops all implementing 316 // the same interface. CompileTheWorld starting at 502, kd12rc1.zip. 317 return (phase->type(in(1)) == phase->type(this)) ? in(1) : this; 318 } 319 320 //------------------------------Value------------------------------------------ 321 // Take 'join' of input and cast-up type, unless working with an Interface 322 const Type* CheckCastPPNode::Value(PhaseGVN* phase) const { 323 if( in(0) && phase->type(in(0)) == Type::TOP ) return Type::TOP; 324 325 const Type *inn = phase->type(in(1)); 326 if( inn == Type::TOP ) return Type::TOP; // No information yet 327 328 const TypePtr *in_type = inn->isa_ptr(); 329 const TypePtr *my_type = _type->isa_ptr(); 330 const Type *result = _type; 331 if( in_type != NULL && my_type != NULL ) { 332 TypePtr::PTR in_ptr = in_type->ptr(); 333 if (in_ptr == TypePtr::Null) { 334 result = in_type; 335 } else if (in_ptr == TypePtr::Constant) { 336 if (my_type->isa_rawptr()) { 337 result = my_type; 338 } else { 339 const TypeOopPtr *jptr = my_type->isa_oopptr(); 340 assert(jptr, ""); 341 result = !in_type->higher_equal(_type) 342 ? my_type->cast_to_ptr_type(TypePtr::NotNull) 343 : in_type; 344 } 345 } else { 346 result = my_type->cast_to_ptr_type( my_type->join_ptr(in_ptr) ); 347 } 348 } 349 350 // This is the code from TypePtr::xmeet() that prevents us from 351 // having 2 ways to represent the same type. We have to replicate it 352 // here because we don't go through meet/join. 353 if (result->remove_speculative() == result->speculative()) { 354 result = result->remove_speculative(); 355 } 356 357 // Same as above: because we don't go through meet/join, remove the 358 // speculative type if we know we won't use it. 359 return result->cleanup_speculative(); 360 361 // JOIN NOT DONE HERE BECAUSE OF INTERFACE ISSUES. 362 // FIX THIS (DO THE JOIN) WHEN UNION TYPES APPEAR! 363 364 // 365 // Remove this code after overnight run indicates no performance 366 // loss from not performing JOIN at CheckCastPPNode 367 // 368 // const TypeInstPtr *in_oop = in->isa_instptr(); 369 // const TypeInstPtr *my_oop = _type->isa_instptr(); 370 // // If either input is an 'interface', return destination type 371 // assert (in_oop == NULL || in_oop->klass() != NULL, ""); 372 // assert (my_oop == NULL || my_oop->klass() != NULL, ""); 373 // if( (in_oop && in_oop->klass()->is_interface()) 374 // ||(my_oop && my_oop->klass()->is_interface()) ) { 375 // TypePtr::PTR in_ptr = in->isa_ptr() ? in->is_ptr()->_ptr : TypePtr::BotPTR; 376 // // Preserve cast away nullness for interfaces 377 // if( in_ptr == TypePtr::NotNull && my_oop && my_oop->_ptr == TypePtr::BotPTR ) { 378 // return my_oop->cast_to_ptr_type(TypePtr::NotNull); 379 // } 380 // return _type; 381 // } 382 // 383 // // Neither the input nor the destination type is an interface, 384 // 385 // // history: JOIN used to cause weird corner case bugs 386 // // return (in == TypeOopPtr::NULL_PTR) ? in : _type; 387 // // JOIN picks up NotNull in common instance-of/check-cast idioms, both oops. 388 // // JOIN does not preserve NotNull in other cases, e.g. RawPtr vs InstPtr 389 // const Type *join = in->join(_type); 390 // // Check if join preserved NotNull'ness for pointers 391 // if( join->isa_ptr() && _type->isa_ptr() ) { 392 // TypePtr::PTR join_ptr = join->is_ptr()->_ptr; 393 // TypePtr::PTR type_ptr = _type->is_ptr()->_ptr; 394 // // If there isn't any NotNull'ness to preserve 395 // // OR if join preserved NotNull'ness then return it 396 // if( type_ptr == TypePtr::BotPTR || type_ptr == TypePtr::Null || 397 // join_ptr == TypePtr::NotNull || join_ptr == TypePtr::Constant ) { 398 // return join; 399 // } 400 // // ELSE return same old type as before 401 // return _type; 402 // } 403 // // Not joining two pointers 404 // return join; 405 } 406 407 Node* CheckCastPPNode::Ideal(PhaseGVN *phase, bool can_reshape) { 408 // This is a value type. Its input is the return of a call: the call 409 // returns a value type and we now know its exact type: build a 410 // ValueTypePtrNode from the call. 411 if (can_reshape && 412 in(0) == NULL && 413 phase->C->can_add_value_type_ptr() && 414 type()->isa_valuetypeptr() && 415 in(1) != NULL && in(1)->is_Proj() && 416 in(1)->in(0) != NULL && in(1)->in(0)->is_CallStaticJava() && 417 in(1)->in(0)->as_CallStaticJava()->method() != NULL && 418 in(1)->as_Proj()->_con == TypeFunc::Parms) { 419 ciValueKlass* vk = type()->is_valuetypeptr()->value_type()->value_klass(); 420 assert(vk != phase->C->env()->___Value_klass(), "why cast to __Value?"); 421 PhaseIterGVN *igvn = phase->is_IterGVN(); 422 423 if (ValueTypeReturnedAsFields && vk->can_be_returned_as_fields()) { 424 igvn->set_delay_transform(true); 425 CallNode* call = in(1)->in(0)->as_Call(); 426 phase->C->remove_macro_node(call); 427 // We now know the return type of the call 428 const TypeTuple *range_sig = TypeTuple::make_range(vk, false); 429 const TypeTuple *range_cc = TypeTuple::make_range(vk, true); 430 assert(range_sig != call->_tf->range_sig() && range_cc != call->_tf->range_cc(), "type should change"); 431 call->_tf = TypeFunc::make(call->_tf->domain_sig(), call->_tf->domain_cc(), 432 range_sig, range_cc); 433 phase->set_type(call, call->Value(phase)); 434 phase->set_type(in(1), in(1)->Value(phase)); 435 436 CallProjections projs; 437 call->extract_projections(&projs, true, true); 438 439 Node* init_ctl = new Node(1); 440 Node* init_mem = new Node(1); 441 Node* init_io = new Node(1); 442 Node* init_ex_ctl = new Node(1); 443 Node* init_ex_mem = new Node(1); 444 Node* init_ex_io = new Node(1); 445 Node* res = new Node(1); 446 447 Node* ctl = init_ctl; 448 Node* mem = init_mem; 449 Node* io = init_io; 450 Node* ex_ctl = init_ex_ctl; 451 Node* ex_mem = init_ex_mem; 452 Node* ex_io = init_ex_io; 453 454 // Either we get a buffered value pointer and we can case use it 455 // or we get a tagged klass pointer and we need to allocate a 456 // value. 457 Node* cast = phase->transform(new CastP2XNode(ctl, res)); 458 Node* masked = phase->transform(new AndXNode(cast, phase->MakeConX(0x1))); 459 Node* cmp = phase->transform(new CmpXNode(masked, phase->MakeConX(0x1))); 460 Node* bol = phase->transform(new BoolNode(cmp, BoolTest::eq)); 461 IfNode* iff = phase->transform(new IfNode(ctl, bol, PROB_MAX, COUNT_UNKNOWN))->as_If(); 462 Node* iftrue = phase->transform(new IfTrueNode(iff)); 463 Node* iffalse = phase->transform(new IfFalseNode(iff)); 464 465 ctl = iftrue; 466 467 Node* ex_r = new RegionNode(3); 468 Node* ex_mem_phi = new PhiNode(ex_r, Type::MEMORY, TypePtr::BOTTOM); 469 Node* ex_io_phi = new PhiNode(ex_r, Type::ABIO); 470 471 ex_r->init_req(2, ex_ctl); 472 ex_mem_phi->init_req(2, ex_mem); 473 ex_io_phi->init_req(2, ex_io); 474 475 // We need an oop pointer in case allocation elimination 476 // fails. Allocate a new instance here. 477 Node* javaoop = ValueTypeBaseNode::allocate(type(), ctl, mem, io, 478 call->in(TypeFunc::FramePtr), 479 ex_ctl, ex_mem, ex_io, 480 call->jvms(), igvn); 481 482 483 484 ex_r->init_req(1, ex_ctl); 485 ex_mem_phi->init_req(1, ex_mem); 486 ex_io_phi->init_req(1, ex_io); 487 488 ex_r = igvn->transform(ex_r); 489 ex_mem_phi = igvn->transform(ex_mem_phi); 490 ex_io_phi = igvn->transform(ex_io_phi); 491 492 // Create the ValueTypePtrNode. This will add extra projections 493 // to the call. 494 ValueTypePtrNode* vtptr = ValueTypePtrNode::make(igvn, this); 495 // Newly allocated value type must be initialized 496 vtptr->store(igvn, ctl, mem->as_MergeMem(), javaoop); 497 vtptr->set_oop(javaoop); 498 499 Node* r = new RegionNode(3); 500 Node* mem_phi = new PhiNode(r, Type::MEMORY, TypePtr::BOTTOM); 501 Node* io_phi = new PhiNode(r, Type::ABIO); 502 Node* res_phi = new PhiNode(r, type()); 503 504 r->init_req(1, ctl); 505 mem_phi->init_req(1, mem); 506 io_phi->init_req(1, io); 507 res_phi->init_req(1, igvn->transform(vtptr)); 508 509 ctl = iffalse; 510 mem = init_mem; 511 io = init_io; 512 513 Node* castnotnull = new CastPPNode(res, TypePtr::NOTNULL); 514 castnotnull->set_req(0, ctl); 515 castnotnull = phase->transform(castnotnull); 516 Node* ccast = clone(); 517 ccast->set_req(0, ctl); 518 ccast->set_req(1, castnotnull); 519 ccast = phase->transform(ccast); 520 521 vtptr = ValueTypePtrNode::make(*phase, mem, ccast); 522 523 r->init_req(2, ctl); 524 mem_phi->init_req(2, mem); 525 io_phi->init_req(2, io); 526 res_phi->init_req(2, igvn->transform(vtptr)); 527 528 r = igvn->transform(r); 529 mem_phi = igvn->transform(mem_phi); 530 io_phi = igvn->transform(io_phi); 531 res_phi = igvn->transform(res_phi); 532 533 igvn->replace_in_uses(projs.fallthrough_catchproj, r); 534 igvn->replace_in_uses(projs.fallthrough_memproj, mem_phi); 535 igvn->replace_in_uses(projs.fallthrough_ioproj, io_phi); 536 igvn->replace_in_uses(projs.resproj, res_phi); 537 igvn->replace_in_uses(projs.catchall_catchproj, ex_r); 538 igvn->replace_in_uses(projs.catchall_memproj, ex_mem_phi); 539 igvn->replace_in_uses(projs.catchall_ioproj, ex_io_phi); 540 541 igvn->set_delay_transform(false); 542 543 igvn->replace_node(init_ctl, projs.fallthrough_catchproj); 544 igvn->replace_node(init_mem, projs.fallthrough_memproj); 545 igvn->replace_node(init_io, projs.fallthrough_ioproj); 546 igvn->replace_node(res, projs.resproj); 547 igvn->replace_node(init_ex_ctl, projs.catchall_catchproj); 548 igvn->replace_node(init_ex_mem, projs.catchall_memproj); 549 igvn->replace_node(init_ex_io, projs.catchall_ioproj); 550 551 return this; 552 } else { 553 CallNode* call = in(1)->in(0)->as_Call(); 554 // We now know the return type of the call 555 const TypeTuple *range = TypeTuple::make_range(vk, false); 556 if (range != call->_tf->range_sig()) { 557 // Build the ValueTypePtrNode by loading the fields. Use call 558 // return as oop edge in the ValueTypePtrNode. 559 call->_tf = TypeFunc::make(call->_tf->domain_sig(), call->_tf->domain_cc(), 560 range, range); 561 phase->set_type(call, call->Value(phase)); 562 phase->set_type(in(1), in(1)->Value(phase)); 563 uint last = phase->C->unique(); 564 CallNode* call = in(1)->in(0)->as_Call(); 565 CallProjections projs; 566 call->extract_projections(&projs, true, true); 567 Node* mem = projs.fallthrough_memproj; 568 Node* vtptr = ValueTypePtrNode::make(*phase, mem, in(1)); 569 570 return vtptr; 571 } 572 } 573 } 574 return NULL; 575 } 576 577 //============================================================================= 578 //------------------------------Value------------------------------------------ 579 const Type* CastX2PNode::Value(PhaseGVN* phase) const { 580 const Type* t = phase->type(in(1)); 581 if (t == Type::TOP) return Type::TOP; 582 if (t->base() == Type_X && t->singleton()) { 583 uintptr_t bits = (uintptr_t) t->is_intptr_t()->get_con(); 584 if (bits == 0) return TypePtr::NULL_PTR; 585 return TypeRawPtr::make((address) bits); 586 } 587 return CastX2PNode::bottom_type(); 588 } 589 590 //------------------------------Idealize--------------------------------------- 591 static inline bool fits_in_int(const Type* t, bool but_not_min_int = false) { 592 if (t == Type::TOP) return false; 593 const TypeX* tl = t->is_intptr_t(); 594 jint lo = min_jint; 595 jint hi = max_jint; 596 if (but_not_min_int) ++lo; // caller wants to negate the value w/o overflow 597 return (tl->_lo >= lo) && (tl->_hi <= hi); 598 } 599 600 static inline Node* addP_of_X2P(PhaseGVN *phase, 601 Node* base, 602 Node* dispX, 603 bool negate = false) { 604 if (negate) { 605 dispX = new SubXNode(phase->MakeConX(0), phase->transform(dispX)); 606 } 607 return new AddPNode(phase->C->top(), 608 phase->transform(new CastX2PNode(base)), 609 phase->transform(dispX)); 610 } 611 612 Node *CastX2PNode::Ideal(PhaseGVN *phase, bool can_reshape) { 613 // convert CastX2P(AddX(x, y)) to AddP(CastX2P(x), y) if y fits in an int 614 int op = in(1)->Opcode(); 615 Node* x; 616 Node* y; 617 switch (op) { 618 case Op_SubX: 619 x = in(1)->in(1); 620 // Avoid ideal transformations ping-pong between this and AddP for raw pointers. 621 if (phase->find_intptr_t_con(x, -1) == 0) 622 break; 623 y = in(1)->in(2); 624 if (fits_in_int(phase->type(y), true)) { 625 return addP_of_X2P(phase, x, y, true); 626 } 627 break; 628 case Op_AddX: 629 x = in(1)->in(1); 630 y = in(1)->in(2); 631 if (fits_in_int(phase->type(y))) { 632 return addP_of_X2P(phase, x, y); 633 } 634 if (fits_in_int(phase->type(x))) { 635 return addP_of_X2P(phase, y, x); 636 } 637 break; 638 } 639 return NULL; 640 } 641 642 //------------------------------Identity--------------------------------------- 643 Node* CastX2PNode::Identity(PhaseGVN* phase) { 644 if (in(1)->Opcode() == Op_CastP2X) return in(1)->in(1); 645 return this; 646 } 647 648 //============================================================================= 649 //------------------------------Value------------------------------------------ 650 const Type* CastP2XNode::Value(PhaseGVN* phase) const { 651 const Type* t = phase->type(in(1)); 652 if (t == Type::TOP) return Type::TOP; 653 if (t->base() == Type::RawPtr && t->singleton()) { 654 uintptr_t bits = (uintptr_t) t->is_rawptr()->get_con(); 655 return TypeX::make(bits); 656 } 657 return CastP2XNode::bottom_type(); 658 } 659 660 Node *CastP2XNode::Ideal(PhaseGVN *phase, bool can_reshape) { 661 return (in(0) && remove_dead_region(phase, can_reshape)) ? this : NULL; 662 } 663 664 //------------------------------Identity--------------------------------------- 665 Node* CastP2XNode::Identity(PhaseGVN* phase) { 666 if (in(1)->Opcode() == Op_CastX2P) return in(1)->in(1); 667 return this; 668 }