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