1 /*
   2  * Copyright (c) 2016, 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 "ci/ciValueKlass.hpp"
  27 #include "opto/addnode.hpp"
  28 #include "opto/graphKit.hpp"
  29 #include "opto/rootnode.hpp"
  30 #include "opto/valuetypenode.hpp"
  31 #include "opto/phaseX.hpp"
  32 
  33 ValueTypeNode* ValueTypeNode::make(PhaseGVN& gvn, ciValueKlass* klass) {
  34   // Create a new ValueTypeNode with uninitialized values and NULL oop
  35   const TypeValueType* type = TypeValueType::make(klass);
  36   return new ValueTypeNode(type, gvn.zerocon(T_VALUETYPE));
  37 }
  38 
  39 Node* ValueTypeNode::make_default(PhaseGVN& gvn, ciValueKlass* vk) {
  40   // TODO re-use constant oop of pre-allocated default value type here?
  41   // Create a new ValueTypeNode with default values
  42   ValueTypeNode* vt = ValueTypeNode::make(gvn, vk);
  43   for (uint i = 0; i < vt->field_count(); ++i) {
  44     ciType* field_type = vt->field_type(i);
  45     Node* value = NULL;
  46     if (field_type->is_valuetype()) {
  47       value = ValueTypeNode::make_default(gvn, field_type->as_value_klass());
  48     } else {
  49       value = gvn.zerocon(field_type->basic_type());
  50     }
  51     vt->set_field_value(i, value);
  52   }
  53   return gvn.transform(vt);
  54 }
  55 
  56 Node* ValueTypeNode::make(PhaseGVN& gvn, Node* mem, Node* oop) {
  57   // Create and initialize a ValueTypeNode by loading all field
  58   // values from a heap-allocated version and also save the oop.
  59   const TypeValueType* type = gvn.type(oop)->is_valuetypeptr()->value_type();
  60   ValueTypeNode* vt = new ValueTypeNode(type, oop);
  61   vt->load(gvn, mem, oop, oop, type->value_klass());
  62   assert(vt->is_allocated(&gvn), "value type should be allocated");
  63   assert(oop->is_Con() || oop->is_CheckCastPP() || vt->is_loaded(&gvn, type) != NULL, "value type should be loaded");
  64   return gvn.transform(vt);
  65 }
  66 
  67 Node* ValueTypeNode::make(PhaseGVN& gvn, ciValueKlass* vk, Node* mem, Node* obj, Node* ptr, ciInstanceKlass* holder, int holder_offset) {
  68   // Create and initialize a ValueTypeNode by loading all field values from
  69   // a flattened value type field at 'holder_offset' or from a value type array.
  70   ValueTypeNode* vt = make(gvn, vk);
  71   // The value type is flattened into the object without an oop header. Subtract the
  72   // offset of the first field to account for the missing header when loading the values.
  73   holder_offset -= vk->first_field_offset();
  74   vt->load(gvn, mem, obj, ptr, holder, holder_offset);
  75   vt = gvn.transform(vt)->as_ValueType();
  76   assert(!vt->is_allocated(&gvn), "value type should not be allocated");
  77   return vt;
  78 }
  79 
  80 void ValueTypeNode::load(PhaseGVN& gvn, Node* mem, Node* base, Node* ptr, ciInstanceKlass* holder, int holder_offset) {
  81   // Initialize the value type by loading its field values from
  82   // memory and adding the values as input edges to the node.
  83   for (uint i = 0; i < field_count(); ++i) {
  84     int offset = holder_offset + field_offset(i);
  85     ciType* ftype = field_type(i);
  86     Node* value = NULL;
  87     if (ftype->is_valuetype()) {
  88       // Recursively load the flattened value type field
  89       value = ValueTypeNode::make(gvn, ftype->as_value_klass(), mem, base, ptr, holder, offset);
  90     } else {
  91       const Type* con_type = NULL;
  92       if (base->is_Con()) {
  93         // If the oop to the value type is constant (static final field), we can
  94         // also treat the fields as constants because the value type is immutable.
  95         const TypeOopPtr* oop_ptr = base->bottom_type()->isa_oopptr();
  96         ciObject* constant_oop = oop_ptr->const_oop();
  97         ciField* field = holder->get_field_by_offset(offset, false);
  98         ciConstant constant = constant_oop->as_instance()->field_value(field);
  99         con_type = Type::make_from_constant(constant, /*require_const=*/ true);
 100       }
 101       if (con_type != NULL) {
 102         // Found a constant field value
 103         value = gvn.makecon(con_type);
 104       } else {
 105         // Load field value from memory
 106         const Type* base_type = gvn.type(base);
 107         const TypePtr* adr_type = NULL;
 108         if (base_type->isa_aryptr()) {
 109           // In the case of a flattened value type array, each field
 110           // has its own slice
 111           adr_type = base_type->is_aryptr()->with_field_offset(offset)->add_offset(Type::OffsetBot);
 112         } else {
 113           ciField* field = holder->get_field_by_offset(offset, false);
 114           adr_type = gvn.C->alias_type(field)->adr_type();
 115         }
 116         Node* adr = gvn.transform(new AddPNode(base, ptr, gvn.MakeConX(offset)));
 117         BasicType bt = type2field[ftype->basic_type()];
 118         value = LoadNode::make(gvn, NULL, mem, adr, adr_type, Type::get_const_type(ftype), bt, MemNode::unordered);
 119       }
 120     }
 121     set_field_value(i, gvn.transform(value));
 122   }
 123 }
 124 
 125 Node* ValueTypeNode::is_loaded(PhaseGVN* phase, const TypeValueType* t, Node* base, int holder_offset) {
 126   for (uint i = 0; i < field_count(); ++i) {
 127     int offset = holder_offset + field_offset(i);
 128     Node* value = field_value(i);
 129     if (value->isa_DecodeN()) {
 130       // Skip DecodeN
 131       value = value->in(1);
 132     }
 133     if (value->isa_Load()) {
 134       AddPNode* load_addr = value->in(MemNode::Address)->as_AddP();
 135       if (base == NULL) {
 136         // Set base and check if pointer type matches
 137         base = load_addr->base_node();
 138         const TypeValueTypePtr* vtptr = phase->type(base)->isa_valuetypeptr();
 139         if (vtptr == NULL || !vtptr->value_type()->eq(t)) {
 140           return NULL;
 141         }
 142       }
 143       // Check if base and offset of field load matches
 144       Node* off = load_addr->in(AddPNode::Offset);
 145       int load_offset = LP64_ONLY(off->get_long()) NOT_LP64(off->get_int());
 146       if (base != load_addr->base_node() || offset != load_offset) {
 147         return NULL;
 148       }
 149     } else if (value->isa_ValueType()) {
 150       // Check value type field load recursively
 151       ValueTypeNode* vt = value->as_ValueType();
 152       base = vt->is_loaded(phase, t, base, offset - vt->value_klass()->first_field_offset());
 153       if (base == NULL) {
 154         return NULL;
 155       }
 156     } else {
 157       return NULL;
 158     }
 159   }
 160   return base;
 161 }
 162 
 163 void ValueTypeNode::store_flattened(GraphKit* kit, Node* base, Node* ptr, ciInstanceKlass* holder, int holder_offset) const {
 164   // The value type is embedded into the object without an oop header. Subtract the
 165   // offset of the first field to account for the missing header when storing the values.
 166   holder_offset -= value_klass()->first_field_offset();
 167   store(kit, base, ptr, holder, holder_offset);
 168 }
 169 
 170 void ValueTypeNode::store(GraphKit* kit, Node* base, Node* ptr, ciInstanceKlass* holder, int holder_offset) const {
 171   // Write field values to memory
 172   for (uint i = 0; i < field_count(); ++i) {
 173     int offset = holder_offset + field_offset(i);
 174     Node* value = field_value(i);
 175     if (value->is_ValueType()) {
 176       // Recursively store the flattened value type field
 177       value->isa_ValueType()->store_flattened(kit, base, ptr, holder, offset);
 178     } else {
 179       const Type* base_type = kit->gvn().type(base);
 180       const TypePtr* adr_type = NULL;
 181       if (base_type->isa_aryptr()) {
 182         // In the case of a flattened value type array, each field has its own slice
 183         adr_type = base_type->is_aryptr()->with_field_offset(offset)->add_offset(Type::OffsetBot);
 184       } else {
 185         ciField* field = holder->get_field_by_offset(offset, false);
 186         adr_type = kit->C->alias_type(field)->adr_type();
 187       }
 188       Node* adr = kit->basic_plus_adr(base, ptr, offset);
 189       BasicType bt = type2field[field_type(i)->basic_type()];
 190       if (is_java_primitive(bt)) {
 191         kit->store_to_memory(kit->control(), adr, value, bt, adr_type, MemNode::unordered);
 192       } else {
 193         const TypeOopPtr* ft = TypeOopPtr::make_from_klass(field_type(i)->as_klass());
 194         assert(adr->bottom_type()->is_ptr_to_narrowoop() == UseCompressedOops, "inconsistent");
 195         bool is_array = base_type->isa_aryptr() != NULL;
 196         kit->store_oop(kit->control(), base, adr, adr_type, value, ft, bt, is_array, MemNode::unordered);
 197       }
 198     }
 199   }
 200 }
 201 
 202 Node* ValueTypeNode::allocate(GraphKit* kit) {
 203   Node* in_oop = get_oop();
 204   Node* null_ctl = kit->top();
 205   // Check if value type is already allocated
 206   Node* not_null_oop = kit->null_check_oop(in_oop, &null_ctl);
 207   if (null_ctl->is_top()) {
 208     // Value type is allocated
 209     return not_null_oop;
 210   }
 211   // Not able to prove that value type is allocated.
 212   // Emit runtime check that may be folded later.
 213   assert(!is_allocated(&kit->gvn()), "should not be allocated");
 214   const TypeValueTypePtr* vtptr_type = TypeValueTypePtr::make(bottom_type()->isa_valuetype(), TypePtr::NotNull);
 215   RegionNode* region = new RegionNode(3);
 216   PhiNode* oop = new PhiNode(region, vtptr_type);
 217   PhiNode* io  = new PhiNode(region, Type::ABIO);
 218   PhiNode* mem = new PhiNode(region, Type::MEMORY, TypePtr::BOTTOM);
 219 
 220   // Oop is non-NULL, use it
 221   region->init_req(1, kit->control());
 222   oop   ->init_req(1, not_null_oop);
 223   io    ->init_req(1, kit->i_o());
 224   mem   ->init_req(1, kit->merged_memory());
 225 
 226   // Oop is NULL, allocate value type
 227   kit->set_control(null_ctl);
 228   kit->kill_dead_locals();
 229   ciValueKlass* vk = value_klass();
 230   Node* klass_node = kit->makecon(TypeKlassPtr::make(vk));
 231   Node* alloc_oop  = kit->new_instance(klass_node, NULL, NULL, false, this);
 232   // Write field values to memory
 233   store(kit, alloc_oop, alloc_oop, vk);
 234   region->init_req(2, kit->control());
 235   oop   ->init_req(2, alloc_oop);
 236   io    ->init_req(2, kit->i_o());
 237   mem   ->init_req(2, kit->merged_memory());
 238 
 239   // Update GraphKit
 240   kit->set_control(kit->gvn().transform(region));
 241   kit->set_i_o(kit->gvn().transform(io));
 242   kit->set_all_memory(kit->gvn().transform(mem));
 243   kit->record_for_igvn(region);
 244   kit->record_for_igvn(oop);
 245   kit->record_for_igvn(io);
 246   kit->record_for_igvn(mem);
 247 
 248   // Use cloned ValueTypeNode to propagate oop from now on
 249   Node* res_oop = kit->gvn().transform(oop);
 250   ValueTypeNode* vt = clone()->as_ValueType();
 251   vt->set_oop(res_oop);
 252   kit->replace_in_map(this, kit->gvn().transform(vt));
 253   return res_oop;
 254 }
 255 
 256 bool ValueTypeNode::is_allocated(PhaseGVN* phase) const {
 257   const Type* oop_type = phase->type(get_oop());
 258   return oop_type->meet(TypePtr::NULL_PTR) != oop_type;
 259 }
 260 
 261 // Clones the values type to handle control flow merges involving multiple value types.
 262 // The inputs are replaced by PhiNodes to represent the merged values for the given region.
 263 ValueTypeNode* ValueTypeNode::clone_with_phis(PhaseGVN* gvn, Node* region) {
 264   assert(!has_phi_inputs(region), "already cloned with phis");
 265   ValueTypeNode* vt = clone()->as_ValueType();
 266 
 267   // Create a PhiNode for merging the oop values
 268   const TypeValueTypePtr* vtptr = TypeValueTypePtr::make(vt->bottom_type()->isa_valuetype());
 269   PhiNode* oop = PhiNode::make(region, vt->get_oop(), vtptr);
 270   gvn->set_type(oop, vtptr);
 271   vt->set_oop(oop);
 272 
 273   // Create a PhiNode each for merging the field values
 274   for (uint i = 0; i < vt->field_count(); ++i) {
 275     ciType* type = vt->field_type(i);
 276     Node*  value = vt->field_value(i);
 277     if (type->is_valuetype()) {
 278       // Handle flattened value type fields recursively
 279       value = value->as_ValueType()->clone_with_phis(gvn, region);
 280     } else {
 281       const Type* phi_type = Type::get_const_type(type);
 282       value = PhiNode::make(region, value, phi_type);
 283       gvn->set_type(value, phi_type);
 284     }
 285     vt->set_field_value(i, value);
 286   }
 287   gvn->set_type(vt, vt->bottom_type());
 288   return vt;
 289 }
 290 
 291 // Checks if the inputs of the ValueTypeNode were replaced by PhiNodes
 292 // for the given region (see ValueTypeNode::clone_with_phis).
 293 bool ValueTypeNode::has_phi_inputs(Node* region) {
 294   // Check oop input
 295   bool result = get_oop()->is_Phi() && get_oop()->as_Phi()->region() == region;
 296 #ifdef ASSERT
 297   if (result) {
 298     // Check all field value inputs for consistency
 299     for (uint i = Oop; i < field_count(); ++i) {
 300       Node* n = in(i);
 301       if (n->is_ValueType()) {
 302         assert(n->as_ValueType()->has_phi_inputs(region), "inconsistent phi inputs");
 303       } else {
 304         assert(n->is_Phi() && n->as_Phi()->region() == region, "inconsistent phi inputs");
 305       }
 306     }
 307   }
 308 #endif
 309   return result;
 310 }
 311 
 312 // Merges 'this' with 'other' by updating the input PhiNodes added by 'clone_with_phis'
 313 ValueTypeNode* ValueTypeNode::merge_with(PhaseGVN* gvn, const ValueTypeNode* other, int pnum, bool transform) {
 314   // Merge oop inputs
 315   PhiNode* phi = get_oop()->as_Phi();
 316   phi->set_req(pnum, other->get_oop());
 317   if (transform) {
 318     set_oop(gvn->transform(phi));
 319     gvn->record_for_igvn(phi);
 320   }
 321   // Merge field values
 322   for (uint i = 0; i < field_count(); ++i) {
 323     Node* val1 =        field_value(i);
 324     Node* val2 = other->field_value(i);
 325     if (val1->isa_ValueType()) {
 326       val1->as_ValueType()->merge_with(gvn, val2->as_ValueType(), pnum, transform);
 327     } else {
 328       assert(val1->is_Phi(), "must be a phi node");
 329       assert(!val2->is_ValueType(), "inconsistent merge values");
 330       val1->set_req(pnum, val2);
 331     }
 332     if (transform) {
 333       set_field_value(i, gvn->transform(val1));
 334       gvn->record_for_igvn(val1);
 335     }
 336   }
 337   return this;
 338 }
 339 
 340 Node* ValueTypeNode::field_value(uint index) const {
 341   assert(index < field_count(), "index out of bounds");
 342   return in(Values + index);
 343 }
 344 
 345 // Get the value of the field at the given offset.
 346 // If 'recursive' is true, flattened value type fields will be resolved recursively.
 347 Node* ValueTypeNode::field_value_by_offset(int offset, bool recursive) const {
 348   // If the field at 'offset' belongs to a flattened value type field, 'index' refers to the
 349   // corresponding ValueTypeNode input and 'sub_offset' is the offset in flattened value type.
 350   int index = value_klass()->field_index_by_offset(offset);
 351   int sub_offset = offset - field_offset(index);
 352   Node* value = field_value(index);
 353   if (recursive && value->is_ValueType()) {
 354     // Flattened value type field
 355     ValueTypeNode* vt = value->as_ValueType();
 356     sub_offset += vt->value_klass()->first_field_offset(); // Add header size
 357     return vt->field_value_by_offset(sub_offset);
 358   }
 359   assert(!(recursive && value->is_ValueType()), "should not be a value type");
 360   assert(sub_offset == 0, "offset mismatch");
 361   return value;
 362 }
 363 
 364 void ValueTypeNode::set_field_value(uint index, Node* value) {
 365   assert(index < field_count(), "index out of bounds");
 366   set_req(Values + index, value);
 367 }
 368 
 369 int ValueTypeNode::field_offset(uint index) const {
 370   assert(index < field_count(), "index out of bounds");
 371   return value_klass()->field_offset_by_index(index);
 372 }
 373 
 374 ciType* ValueTypeNode::field_type(uint index) const {
 375   assert(index < field_count(), "index out of bounds");
 376   return value_klass()->field_type_by_index(index);
 377 }
 378 
 379 void ValueTypeNode::make_scalar_in_safepoints(Compile* C) {
 380   const TypeValueTypePtr* res_type = TypeValueTypePtr::make(bottom_type()->isa_valuetype(), TypePtr::NotNull);
 381   ciValueKlass* vk = value_klass();
 382   uint nfields = vk->flattened_field_count();
 383   for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
 384     Node* u = fast_out(i);
 385     if (u->is_SafePoint() && (!u->is_Call() || u->as_Call()->has_debug_use(this))) {
 386       Node* in_oop = get_oop();
 387       const Type* oop_type = in_oop->bottom_type();
 388       SafePointNode* sfpt = u->as_SafePoint();
 389       JVMState* jvms = sfpt->jvms();
 390       int start = jvms->debug_start();
 391       int end   = jvms->debug_end();
 392       assert(TypePtr::NULL_PTR->higher_equal(oop_type), "already heap allocated value type should be linked directly");
 393       // Replace safepoint edge by SafePointScalarObjectNode and add field values
 394       assert(jvms != NULL, "missing JVMS");
 395       uint first_ind = (sfpt->req() - jvms->scloff());
 396       SafePointScalarObjectNode* sobj = new SafePointScalarObjectNode(res_type,
 397 #ifdef ASSERT
 398                                                                       NULL,
 399 #endif
 400                                                                       first_ind, nfields);
 401       sobj->init_req(0, C->root());
 402       // Iterate over the value type fields in order of increasing
 403       // offset and add the field values to the safepoint.
 404       for (uint j = 0; j < nfields; ++j) {
 405         int offset = vk->nonstatic_field_at(j)->offset();
 406         Node* value = field_value_by_offset(offset, true /* include flattened value type fields */);
 407         sfpt->add_req(value);
 408       }
 409       jvms->set_endoff(sfpt->req());
 410       int nb = sfpt->replace_edges_in_range(this, sobj, start, end);
 411       --i; imax -= nb;
 412     }
 413   }
 414 }
 415 
 416 void ValueTypeNode::pass_klass(Node* n, uint pos, const GraphKit& kit) {
 417   ciValueKlass* vk = value_klass();
 418   const TypeKlassPtr* tk = TypeKlassPtr::make(vk);
 419   Node* arg = kit.makecon(tk);
 420   n->init_req(pos, arg);
 421 }
 422 
 423 uint ValueTypeNode::pass_fields(Node* n, int base_input, const GraphKit& kit, ciValueKlass* base_vk, int base_offset) {
 424   ciValueKlass* vk = value_klass();
 425   if (base_vk == NULL) {
 426     base_vk = vk;
 427   }
 428   uint edges = 0;
 429   for (uint i = 0; i < field_count(); i++) {
 430     ciType* f_type = field_type(i);
 431     int offset = base_offset + field_offset(i) - (base_offset > 0 ? vk->first_field_offset() : 0);
 432     Node* arg = field_value(i);
 433     if (f_type->is_valuetype()) {
 434       ciValueKlass* embedded_vk = f_type->as_value_klass();
 435       edges += arg->as_ValueType()->pass_fields(n, base_input, kit, base_vk, offset);
 436     } else {
 437       int j = 0; int extra = 0;
 438       for (; j < base_vk->nof_nonstatic_fields(); j++) {
 439         ciField* f = base_vk->nonstatic_field_at(j);
 440         if (offset == f->offset()) {
 441           assert(f->type() == f_type, "inconsistent field type");
 442           break;
 443         }
 444         BasicType bt = f->type()->basic_type();
 445         if (bt == T_LONG || bt == T_DOUBLE) {
 446           extra++;
 447         }
 448       }
 449       n->init_req(base_input + j + extra, arg);
 450       edges++;
 451       BasicType bt = f_type->basic_type();
 452       if (bt == T_LONG || bt == T_DOUBLE) {
 453         n->init_req(base_input + j + extra + 1, kit.top());
 454         edges++;
 455       }
 456     }
 457   }
 458   return edges;
 459 }
 460 
 461 Node* ValueTypeNode::Ideal(PhaseGVN* phase, bool can_reshape) {
 462   if (!is_allocated(phase)) {
 463     // Check if this value type is loaded from memory
 464     Node* base = is_loaded(phase, type()->is_valuetype());
 465     if (base != NULL) {
 466       // Save the oop
 467       set_oop(base);
 468       assert(is_allocated(phase), "should now be allocated");
 469     }
 470   }
 471 
 472   if (can_reshape) {
 473     PhaseIterGVN* igvn = phase->is_IterGVN();
 474     if (is_allocated(igvn)) {
 475       // Value type is heap allocated, search for safepoint uses
 476       for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
 477         Node* out = fast_out(i);
 478         if (out->is_SafePoint()) {
 479           // Let SafePointNode::Ideal() take care of re-wiring the
 480           // safepoint to the oop input instead of the value type node.
 481           igvn->rehash_node_delayed(out);
 482         }
 483       }
 484     }
 485   }
 486   return NULL;
 487 }
 488 
 489 // Search for multiple allocations of this value type
 490 // and try to replace them by dominating allocations.
 491 void ValueTypeNode::remove_redundant_allocations(PhaseIterGVN* igvn, PhaseIdealLoop* phase) {
 492   assert(EliminateAllocations, "allocation elimination should be enabled");
 493   Node_List dead_allocations;
 494   // Search for allocations of this value type
 495   for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
 496     Node* out1 = fast_out(i);
 497     if (out1->is_Allocate() && out1->in(AllocateNode::ValueNode) == this) {
 498       AllocateNode* alloc = out1->as_Allocate();
 499       Node* res_dom = NULL;
 500       if (is_allocated(igvn)) {
 501         // The value type is already allocated but still connected to an AllocateNode.
 502         // This can happen with late inlining when we first allocate a value type argument
 503         // but later decide to inline the call with the callee code also allocating.
 504         res_dom = get_oop();
 505       } else {
 506         // Search for a dominating allocation of the same value type
 507         for (DUIterator_Fast jmax, j = fast_outs(jmax); j < jmax; j++) {
 508           Node* out2 = fast_out(j);
 509           if (alloc != out2 && out2->is_Allocate() && out2->in(AllocateNode::ValueNode) == this &&
 510               phase->is_dominator(out2, alloc)) {
 511             AllocateNode* alloc_dom =  out2->as_Allocate();
 512             assert(alloc->in(AllocateNode::KlassNode) == alloc_dom->in(AllocateNode::KlassNode), "klasses should match");
 513             res_dom = alloc_dom->result_cast();
 514             break;
 515           }
 516         }
 517       }
 518       if (res_dom != NULL) {
 519         // Found a dominating allocation
 520         Node* res = alloc->result_cast();
 521         assert(res != NULL, "value type allocation should not be dead");
 522         // Move users to dominating allocation
 523         igvn->replace_node(res, res_dom);
 524         // The dominated allocation is now dead, remove the
 525         // value type node connection and adjust the iterator.
 526         dead_allocations.push(alloc);
 527         igvn->replace_input_of(alloc, AllocateNode::ValueNode, NULL);
 528         --i; --imax;
 529 #ifdef ASSERT
 530         if (PrintEliminateAllocations) {
 531           tty->print("++++ Eliminated: %d Allocate ", alloc->_idx);
 532           dump_spec(tty);
 533           tty->cr();
 534         }
 535 #endif
 536       }
 537     }
 538   }
 539 
 540   // Remove dead value type allocations by replacing the projection nodes
 541   for (uint i = 0; i < dead_allocations.size(); ++i) {
 542     CallProjections projs;
 543     AllocateNode* alloc = dead_allocations.at(i)->as_Allocate();
 544     alloc->extract_projections(&projs, true);
 545     // Use lazy_replace to avoid corrupting the dominator tree of PhaseIdealLoop
 546     phase->lazy_replace(projs.fallthrough_catchproj, alloc->in(TypeFunc::Control));
 547     phase->lazy_replace(projs.fallthrough_memproj, alloc->in(TypeFunc::Memory));
 548     phase->lazy_replace(projs.catchall_memproj, phase->C->top());
 549     phase->lazy_replace(projs.fallthrough_ioproj, alloc->in(TypeFunc::I_O));
 550     phase->lazy_replace(projs.catchall_ioproj, phase->C->top());
 551     phase->lazy_replace(projs.catchall_catchproj, phase->C->top());
 552     phase->lazy_replace(projs.resproj, phase->C->top());
 553   }
 554 }
 555 
 556 // When a call returns multiple values, it has several result
 557 // projections, one per field. Replacing the result of the call by a
 558 // value type node (after late inlining) requires that for each result
 559 // projection, we find the corresponding value type field.
 560 void ValueTypeNode::replace_call_results(Node* call, Compile* C) {
 561   ciValueKlass* vk = value_klass();
 562   for (DUIterator_Fast imax, i = call->fast_outs(imax); i < imax; i++) {
 563     ProjNode *pn = call->fast_out(i)->as_Proj();
 564     uint con = pn->_con;
 565     if (con >= TypeFunc::Parms+1) {
 566       uint field_nb = con - (TypeFunc::Parms+1);
 567       int extra = 0;
 568       for (uint j = 0; j < field_nb - extra; j++) {
 569         ciField* f = vk->nonstatic_field_at(j);
 570         BasicType bt = f->type()->basic_type();
 571         if (bt == T_LONG || bt == T_DOUBLE) {
 572           extra++;
 573         }
 574       }
 575       ciField* f = vk->nonstatic_field_at(field_nb - extra);
 576       Node* field = field_value_by_offset(f->offset(), true);
 577 
 578       C->gvn_replace_by(pn, field);
 579       C->initial_gvn()->hash_delete(pn);
 580       pn->set_req(0, C->top());
 581       --i; --imax;
 582     }
 583   }
 584 }
 585 
 586 
 587 #ifndef PRODUCT
 588 
 589 void ValueTypeNode::dump_spec(outputStream* st) const {
 590   TypeNode::dump_spec(st);
 591 }
 592 
 593 #endif