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