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 void ValueTypeBaseNode::make(PhaseGVN* gvn, Node* n, ValueTypeBaseNode* vt, ciValueKlass* base_vk, int base_offset, int base_input, bool in) {
 198   assert(base_offset >= 0, "offset in value type always positive");
 199   for (uint i = 0; i < vt->field_count(); i++) {
 200     ciType* field_type = vt->field_type(i);
 201     int offset = base_offset + vt->field_offset(i);
 202     if (field_type->is_valuetype()) {
 203       ciValueKlass* embedded_vk = field_type->as_value_klass();
 204       ValueTypeNode* embedded_vt = ValueTypeNode::make(*gvn, embedded_vk);
 205       ValueTypeBaseNode::make(gvn, n, embedded_vt, base_vk, offset - vt->value_klass()->first_field_offset(), base_input, in);
 206       vt->set_field_value(i, gvn->transform(embedded_vt));
 207     } else {
 208       int j = 0; int extra = 0;
 209       for (; j < base_vk->nof_nonstatic_fields(); j++) {
 210         ciField* f = base_vk->nonstatic_field_at(j);
 211         if (offset == f->offset()) {
 212           assert(f->type() == field_type, "inconsistent field type");
 213           break;
 214         }
 215         BasicType bt = f->type()->basic_type();
 216         if (bt == T_LONG || bt == T_DOUBLE) {
 217           extra++;
 218         }
 219       }
 220       assert(j != base_vk->nof_nonstatic_fields(), "must find");
 221       Node* parm = NULL;
 222       if (n->is_Start()) {
 223         assert(in, "return from start?");
 224         parm = gvn->transform(new ParmNode(n->as_Start(), base_input + j + extra));
 225       } else {
 226         if (in) {
 227           assert(n->is_Call(), "nothing else here");
 228           parm = n->in(base_input + j + extra);
 229         } else {
 230           parm = gvn->transform(new ProjNode(n->as_Call(), base_input + j + extra));
 231         }
 232       }
 233       vt->set_field_value(i, parm);
 234       // Record all these guys for later GVN.
 235       gvn->record_for_igvn(parm);
 236     }
 237   }
 238 }
 239 
 240 void ValueTypeBaseNode::load(PhaseGVN& gvn, Node* mem, Node* base, Node* ptr, ciInstanceKlass* holder, int holder_offset) {
 241   // Initialize the value type by loading its field values from
 242   // memory and adding the values as input edges to the node.
 243   for (uint i = 0; i < field_count(); ++i) {
 244     int offset = holder_offset + field_offset(i);
 245     ciType* ftype = field_type(i);
 246     Node* value = NULL;
 247     if (ftype->is_valuetype()) {
 248       // Recursively load the flattened value type field
 249       value = ValueTypeNode::make(gvn, ftype->as_value_klass(), mem, base, ptr, holder, offset);
 250     } else {
 251       const Type* con_type = NULL;
 252       if (base->is_Con()) {
 253         // If the oop to the value type is constant (static final field), we can
 254         // also treat the fields as constants because the value type is immutable.
 255         const TypeOopPtr* oop_ptr = base->bottom_type()->isa_oopptr();
 256         ciObject* constant_oop = oop_ptr->const_oop();
 257         ciField* field = holder->get_field_by_offset(offset, false);
 258         ciConstant constant = constant_oop->as_instance()->field_value(field);
 259         con_type = Type::make_from_constant(constant, /*require_const=*/ true);
 260       }
 261       if (con_type != NULL) {
 262         // Found a constant field value
 263         value = gvn.makecon(con_type);
 264       } else {
 265         // Load field value from memory
 266         const Type* base_type = gvn.type(base);
 267         const TypePtr* adr_type = NULL;
 268         if (base_type->isa_aryptr()) {
 269           // In the case of a flattened value type array, each field
 270           // has its own slice
 271           adr_type = base_type->is_aryptr()->with_field_offset(offset)->add_offset(Type::OffsetBot);
 272         } else {
 273           ciField* field = holder->get_field_by_offset(offset, false);
 274           adr_type = gvn.C->alias_type(field)->adr_type();
 275         }
 276         Node* adr = gvn.transform(new AddPNode(base, ptr, gvn.MakeConX(offset)));
 277         BasicType bt = type2field[ftype->basic_type()];
 278         value = LoadNode::make(gvn, NULL, mem, adr, adr_type, Type::get_const_type(ftype), bt, MemNode::unordered);
 279       }
 280     }
 281     set_field_value(i, gvn.transform(value));
 282   }
 283 }
 284 
 285 void ValueTypeBaseNode::store_flattened(PhaseGVN* gvn, Node* ctl, MergeMemNode* mem, Node* base, ciValueKlass* holder, int holder_offset) const {
 286   // The value type is embedded into the object without an oop header. Subtract the
 287   // offset of the first field to account for the missing header when storing the values.
 288   holder_offset -= value_klass()->first_field_offset();
 289   store(gvn, ctl, mem, base, holder, holder_offset);
 290 }
 291 
 292 void ValueTypeBaseNode::store(PhaseGVN* gvn, Node* ctl, MergeMemNode* mem, Node* base, ciValueKlass* holder, int holder_offset) const {
 293   if (holder == NULL) {
 294     holder = value_klass();
 295   }
 296   // Write field values to memory
 297   for (uint i = 0; i < field_count(); ++i) {
 298     int offset = holder_offset + field_offset(i);
 299     Node* value = field_value(i);
 300     if (value->is_ValueType()) {
 301       // Recursively store the flattened value type field
 302       value->isa_ValueTypeBase()->store_flattened(gvn, ctl, mem, base, holder, offset);
 303     } else {
 304       const Type* base_type = gvn->type(base);
 305       const TypePtr* adr_type = NULL;
 306       if (base_type->isa_aryptr()) {
 307         // In the case of a flattened value type array, each field has its own slice
 308         adr_type = base_type->is_aryptr()->with_field_offset(offset)->add_offset(Type::OffsetBot);
 309       } else {
 310         ciField* field = holder->get_field_by_offset(offset, false);
 311         adr_type = gvn->C->alias_type(field)->adr_type();
 312       }
 313       Node* adr = gvn->transform(new AddPNode(base, base, gvn->MakeConX(offset)));
 314       BasicType bt = type2field[field_type(i)->basic_type()];
 315       uint alias_idx = gvn->C->get_alias_index(adr_type);
 316       Node* st = StoreNode::make(*gvn, ctl, mem->memory_at(alias_idx), adr, adr_type, value, bt, MemNode::unordered);
 317       mem->set_memory_at(alias_idx, gvn->transform(st));
 318     }
 319   }
 320 }
 321 
 322 // When a call returns multiple values, it has several result
 323 // projections, one per field. Replacing the result of the call by a
 324 // value type node (after late inlining) requires that for each result
 325 // projection, we find the corresponding value type field.
 326 void ValueTypeBaseNode::replace_call_results(Node* call, Compile* C) {
 327   ciValueKlass* vk = value_klass();
 328   for (DUIterator_Fast imax, i = call->fast_outs(imax); i < imax; i++) {
 329     ProjNode *pn = call->fast_out(i)->as_Proj();
 330     uint con = pn->_con;
 331     if (con >= TypeFunc::Parms+1) {
 332       uint field_nb = con - (TypeFunc::Parms+1);
 333       int extra = 0;
 334       for (uint j = 0; j < field_nb - extra; j++) {
 335         ciField* f = vk->nonstatic_field_at(j);
 336         BasicType bt = f->type()->basic_type();
 337         if (bt == T_LONG || bt == T_DOUBLE) {
 338           extra++;
 339         }
 340       }
 341       ciField* f = vk->nonstatic_field_at(field_nb - extra);
 342       Node* field = field_value_by_offset(f->offset(), true);
 343 
 344       C->gvn_replace_by(pn, field);
 345       C->initial_gvn()->hash_delete(pn);
 346       pn->set_req(0, C->top());
 347       --i; --imax;
 348     }
 349   }
 350 }
 351 
 352 ValueTypeNode* ValueTypeNode::make(PhaseGVN& gvn, ciValueKlass* klass) {
 353   // Create a new ValueTypeNode with uninitialized values and NULL oop
 354   const TypeValueType* type = TypeValueType::make(klass);
 355   return new ValueTypeNode(type, gvn.zerocon(T_VALUETYPE));
 356 }
 357 
 358 Node* ValueTypeNode::make_default(PhaseGVN& gvn, ciValueKlass* vk) {
 359   // TODO re-use constant oop of pre-allocated default value type here?
 360   // Create a new ValueTypeNode with default values
 361   ValueTypeNode* vt = ValueTypeNode::make(gvn, vk);
 362   for (uint i = 0; i < vt->field_count(); ++i) {
 363     ciType* field_type = vt->field_type(i);
 364     Node* value = NULL;
 365     if (field_type->is_valuetype()) {
 366       value = ValueTypeNode::make_default(gvn, field_type->as_value_klass());
 367     } else {
 368       value = gvn.zerocon(field_type->basic_type());
 369     }
 370     vt->set_field_value(i, value);
 371   }
 372   return gvn.transform(vt);
 373 }
 374 
 375 Node* ValueTypeNode::make(PhaseGVN& gvn, Node* mem, Node* oop) {
 376   // Create and initialize a ValueTypeNode by loading all field
 377   // values from a heap-allocated version and also save the oop.
 378   const TypeValueType* type = gvn.type(oop)->is_valuetypeptr()->value_type();
 379   ValueTypeNode* vt = new ValueTypeNode(type, oop);
 380   vt->load(gvn, mem, oop, oop, type->value_klass());
 381   assert(vt->is_allocated(&gvn), "value type should be allocated");
 382   assert(oop->is_Con() || oop->is_CheckCastPP() || oop->Opcode() == Op_ValueTypePtr || vt->is_loaded(&gvn, type) == oop, "value type should be loaded");
 383   return gvn.transform(vt);
 384 }
 385 
 386 Node* ValueTypeNode::make(PhaseGVN& gvn, ciValueKlass* vk, Node* mem, Node* obj, Node* ptr, ciInstanceKlass* holder, int holder_offset) {
 387   // Create and initialize a ValueTypeNode by loading all field values from
 388   // a flattened value type field at 'holder_offset' or from a value type array.
 389   ValueTypeNode* vt = make(gvn, vk);
 390   // The value type is flattened into the object without an oop header. Subtract the
 391   // offset of the first field to account for the missing header when loading the values.
 392   holder_offset -= vk->first_field_offset();
 393   vt->load(gvn, mem, obj, ptr, holder, holder_offset);
 394   assert(vt->is_loaded(&gvn, vt->type()->isa_valuetype()) != obj, "holder oop should not be used as flattened value type oop");
 395   return gvn.transform(vt)->as_ValueType();
 396 }
 397 
 398 Node* ValueTypeNode::make(PhaseGVN& gvn, Node* n, ciValueKlass* vk, int base_input, bool in) {
 399   ValueTypeNode* vt = ValueTypeNode::make(gvn, vk);
 400   ValueTypeBaseNode::make(&gvn, n, vt, vk, 0, base_input, in);
 401   return gvn.transform(vt);
 402 }
 403 
 404 Node* ValueTypeNode::is_loaded(PhaseGVN* phase, const TypeValueType* t, Node* base, int holder_offset) {
 405   if (field_count() == 0) {
 406     assert(t->value_klass() == phase->C->env()->___Value_klass(), "unexpected value type klass");
 407     assert(is_allocated(phase), "must be allocated");
 408     return get_oop();
 409   }
 410   for (uint i = 0; i < field_count(); ++i) {
 411     int offset = holder_offset + field_offset(i);
 412     Node* value = field_value(i);
 413     if (value->isa_DecodeN()) {
 414       // Skip DecodeN
 415       value = value->in(1);
 416     }
 417     if (value->isa_Load()) {
 418       // Check if base and offset of field load matches value type layout
 419       intptr_t loffset = 0;
 420       Node* lbase = AddPNode::Ideal_base_and_offset(value->in(MemNode::Address), phase, loffset);
 421       if (lbase == NULL || (lbase != base && base != NULL) || loffset != offset) {
 422         return NULL;
 423       } else if (base == NULL) {
 424         // Set base and check if pointer type matches
 425         base = lbase;
 426         const TypeValueTypePtr* vtptr = phase->type(base)->isa_valuetypeptr();
 427         if (vtptr == NULL || !vtptr->value_type()->eq(t)) {
 428           return NULL;
 429         }
 430       }
 431     } else if (value->isa_ValueType()) {
 432       // Check value type field load recursively
 433       ValueTypeNode* vt = value->as_ValueType();
 434       base = vt->is_loaded(phase, t, base, offset - vt->value_klass()->first_field_offset());
 435       if (base == NULL) {
 436         return NULL;
 437       }
 438     } else {
 439       return NULL;
 440     }
 441   }
 442   return base;
 443 }
 444 
 445 void ValueTypeNode::store_flattened(GraphKit* kit, Node* base, Node* ptr, ciInstanceKlass* holder, int holder_offset) const {
 446   // The value type is embedded into the object without an oop header. Subtract the
 447   // offset of the first field to account for the missing header when storing the values.
 448   holder_offset -= value_klass()->first_field_offset();
 449   store(kit, base, ptr, holder, holder_offset);
 450 }
 451 
 452 void ValueTypeNode::store(GraphKit* kit, Node* base, Node* ptr, ciInstanceKlass* holder, int holder_offset) const {
 453   // Write field values to memory
 454   for (uint i = 0; i < field_count(); ++i) {
 455     int offset = holder_offset + field_offset(i);
 456     Node* value = field_value(i);
 457     if (value->is_ValueType()) {
 458       // Recursively store the flattened value type field
 459       value->isa_ValueType()->store_flattened(kit, base, ptr, holder, offset);
 460     } else {
 461       const Type* base_type = kit->gvn().type(base);
 462       const TypePtr* adr_type = NULL;
 463       if (base_type->isa_aryptr()) {
 464         // In the case of a flattened value type array, each field has its own slice
 465         adr_type = base_type->is_aryptr()->with_field_offset(offset)->add_offset(Type::OffsetBot);
 466       } else {
 467         ciField* field = holder->get_field_by_offset(offset, false);
 468         adr_type = kit->C->alias_type(field)->adr_type();
 469       }
 470       Node* adr = kit->basic_plus_adr(base, ptr, offset);
 471       BasicType bt = type2field[field_type(i)->basic_type()];
 472       if (is_java_primitive(bt)) {
 473         kit->store_to_memory(kit->control(), adr, value, bt, adr_type, MemNode::unordered);
 474       } else {
 475         const TypeOopPtr* ft = TypeOopPtr::make_from_klass(field_type(i)->as_klass());
 476         assert(adr->bottom_type()->is_ptr_to_narrowoop() == UseCompressedOops, "inconsistent");
 477         bool is_array = base_type->isa_aryptr() != NULL;
 478         kit->store_oop(kit->control(), base, adr, adr_type, value, ft, bt, is_array, MemNode::unordered);
 479       }
 480     }
 481   }
 482 }
 483 
 484 Node* ValueTypeNode::allocate(GraphKit* kit) {
 485   Node* in_oop = get_oop();
 486   Node* null_ctl = kit->top();
 487   // Check if value type is already allocated
 488   Node* not_null_oop = kit->null_check_oop(in_oop, &null_ctl);
 489   if (null_ctl->is_top()) {
 490     // Value type is allocated
 491     return not_null_oop;
 492   }
 493   // Not able to prove that value type is allocated.
 494   // Emit runtime check that may be folded later.
 495   assert(!is_allocated(&kit->gvn()), "should not be allocated");
 496   const TypeValueTypePtr* vtptr_type = TypeValueTypePtr::make(bottom_type()->isa_valuetype(), TypePtr::NotNull);
 497   RegionNode* region = new RegionNode(3);
 498   PhiNode* oop = new PhiNode(region, vtptr_type);
 499   PhiNode* io  = new PhiNode(region, Type::ABIO);
 500   PhiNode* mem = new PhiNode(region, Type::MEMORY, TypePtr::BOTTOM);
 501 
 502   // Oop is non-NULL, use it
 503   region->init_req(1, kit->control());
 504   oop   ->init_req(1, not_null_oop);
 505   io    ->init_req(1, kit->i_o());
 506   mem   ->init_req(1, kit->merged_memory());
 507 
 508   // Oop is NULL, allocate value type
 509   kit->set_control(null_ctl);
 510   kit->kill_dead_locals();
 511   ciValueKlass* vk = value_klass();
 512   Node* klass_node = kit->makecon(TypeKlassPtr::make(vk));
 513   Node* alloc_oop  = kit->new_instance(klass_node, NULL, NULL, false, this);
 514   // Write field values to memory
 515   store(kit, alloc_oop, alloc_oop, vk);
 516   region->init_req(2, kit->control());
 517   oop   ->init_req(2, alloc_oop);
 518   io    ->init_req(2, kit->i_o());
 519   mem   ->init_req(2, kit->merged_memory());
 520 
 521   // Update GraphKit
 522   kit->set_control(kit->gvn().transform(region));
 523   kit->set_i_o(kit->gvn().transform(io));
 524   kit->set_all_memory(kit->gvn().transform(mem));
 525   kit->record_for_igvn(region);
 526   kit->record_for_igvn(oop);
 527   kit->record_for_igvn(io);
 528   kit->record_for_igvn(mem);
 529 
 530   // Use cloned ValueTypeNode to propagate oop from now on
 531   Node* res_oop = kit->gvn().transform(oop);
 532   ValueTypeNode* vt = clone()->as_ValueType();
 533   vt->set_oop(res_oop);
 534   kit->replace_in_map(this, kit->gvn().transform(vt));
 535   return res_oop;
 536 }
 537 
 538 bool ValueTypeNode::is_allocated(PhaseGVN* phase) const {
 539   const Type* oop_type = phase->type(get_oop());
 540   return oop_type->meet(TypePtr::NULL_PTR) != oop_type;
 541 }
 542 
 543 void ValueTypeNode::pass_klass(Node* n, uint pos, const GraphKit& kit) {
 544   ciValueKlass* vk = value_klass();
 545   const TypeKlassPtr* tk = TypeKlassPtr::make(vk);
 546   intptr_t bits = tk->get_con();
 547   set_nth_bit(bits, 0);
 548   Node* klass_tagged = kit.MakeConX(bits);
 549   n->init_req(pos, klass_tagged);
 550 }
 551 
 552 uint ValueTypeNode::pass_fields(Node* n, int base_input, const GraphKit& kit, ciValueKlass* base_vk, int base_offset) {
 553   ciValueKlass* vk = value_klass();
 554   if (base_vk == NULL) {
 555     base_vk = vk;
 556   }
 557   uint edges = 0;
 558   for (uint i = 0; i < field_count(); i++) {
 559     ciType* f_type = field_type(i);
 560     int offset = base_offset + field_offset(i) - (base_offset > 0 ? vk->first_field_offset() : 0);
 561     Node* arg = field_value(i);
 562     if (f_type->is_valuetype()) {
 563       ciValueKlass* embedded_vk = f_type->as_value_klass();
 564       edges += arg->as_ValueType()->pass_fields(n, base_input, kit, base_vk, offset);
 565     } else {
 566       int j = 0; int extra = 0;
 567       for (; j < base_vk->nof_nonstatic_fields(); j++) {
 568         ciField* f = base_vk->nonstatic_field_at(j);
 569         if (offset == f->offset()) {
 570           assert(f->type() == f_type, "inconsistent field type");
 571           break;
 572         }
 573         BasicType bt = f->type()->basic_type();
 574         if (bt == T_LONG || bt == T_DOUBLE) {
 575           extra++;
 576         }
 577       }
 578       n->init_req(base_input + j + extra, arg);
 579       edges++;
 580       BasicType bt = f_type->basic_type();
 581       if (bt == T_LONG || bt == T_DOUBLE) {
 582         n->init_req(base_input + j + extra + 1, kit.top());
 583         edges++;
 584       }
 585     }
 586   }
 587   return edges;
 588 }
 589 
 590 Node* ValueTypeNode::Ideal(PhaseGVN* phase, bool can_reshape) {
 591   if (!is_allocated(phase)) {
 592     // Check if this value type is loaded from memory
 593     Node* base = is_loaded(phase, type()->is_valuetype());
 594     if (base != NULL) {
 595       // Save the oop
 596       set_oop(base);
 597       assert(is_allocated(phase), "should now be allocated");
 598       return this;
 599     }
 600   }
 601 
 602   if (can_reshape) {
 603     PhaseIterGVN* igvn = phase->is_IterGVN();
 604     if (is_allocated(igvn)) {
 605       // Value type is heap allocated, search for safepoint uses
 606       for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
 607         Node* out = fast_out(i);
 608         if (out->is_SafePoint()) {
 609           // Let SafePointNode::Ideal() take care of re-wiring the
 610           // safepoint to the oop input instead of the value type node.
 611           igvn->rehash_node_delayed(out);
 612         }
 613       }
 614     }
 615   }
 616   return NULL;
 617 }
 618 
 619 // Search for multiple allocations of this value type
 620 // and try to replace them by dominating allocations.
 621 void ValueTypeNode::remove_redundant_allocations(PhaseIterGVN* igvn, PhaseIdealLoop* phase) {
 622   assert(EliminateAllocations, "allocation elimination should be enabled");
 623   Node_List dead_allocations;
 624   // Search for allocations of this value type
 625   for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
 626     Node* out1 = fast_out(i);
 627     if (out1->is_Allocate() && out1->in(AllocateNode::ValueNode) == this) {
 628       AllocateNode* alloc = out1->as_Allocate();
 629       Node* res_dom = NULL;
 630       if (is_allocated(igvn)) {
 631         // The value type is already allocated but still connected to an AllocateNode.
 632         // This can happen with late inlining when we first allocate a value type argument
 633         // but later decide to inline the call with the callee code also allocating.
 634         res_dom = get_oop();
 635       } else {
 636         // Search for a dominating allocation of the same value type
 637         for (DUIterator_Fast jmax, j = fast_outs(jmax); j < jmax; j++) {
 638           Node* out2 = fast_out(j);
 639           if (alloc != out2 && out2->is_Allocate() && out2->in(AllocateNode::ValueNode) == this &&
 640               phase->is_dominator(out2, alloc)) {
 641             AllocateNode* alloc_dom =  out2->as_Allocate();
 642             assert(alloc->in(AllocateNode::KlassNode) == alloc_dom->in(AllocateNode::KlassNode), "klasses should match");
 643             res_dom = alloc_dom->result_cast();
 644             break;
 645           }
 646         }
 647       }
 648       if (res_dom != NULL) {
 649         // Found a dominating allocation
 650         Node* res = alloc->result_cast();
 651         assert(res != NULL, "value type allocation should not be dead");
 652         // Move users to dominating allocation
 653         igvn->replace_node(res, res_dom);
 654         // The dominated allocation is now dead, remove the
 655         // value type node connection and adjust the iterator.
 656         dead_allocations.push(alloc);
 657         igvn->replace_input_of(alloc, AllocateNode::ValueNode, NULL);
 658         --i; --imax;
 659 #ifdef ASSERT
 660         if (PrintEliminateAllocations) {
 661           tty->print("++++ Eliminated: %d Allocate ", alloc->_idx);
 662           dump_spec(tty);
 663           tty->cr();
 664         }
 665 #endif
 666       }
 667     }
 668   }
 669 
 670   // Remove dead value type allocations by replacing the projection nodes
 671   for (uint i = 0; i < dead_allocations.size(); ++i) {
 672     CallProjections projs;
 673     AllocateNode* alloc = dead_allocations.at(i)->as_Allocate();
 674     alloc->extract_projections(&projs, true);
 675     // Use lazy_replace to avoid corrupting the dominator tree of PhaseIdealLoop
 676     phase->lazy_replace(projs.fallthrough_catchproj, alloc->in(TypeFunc::Control));
 677     phase->lazy_replace(projs.fallthrough_memproj, alloc->in(TypeFunc::Memory));
 678     phase->lazy_replace(projs.catchall_memproj, phase->C->top());
 679     phase->lazy_replace(projs.fallthrough_ioproj, alloc->in(TypeFunc::I_O));
 680     phase->lazy_replace(projs.catchall_ioproj, phase->C->top());
 681     phase->lazy_replace(projs.catchall_catchproj, phase->C->top());
 682     phase->lazy_replace(projs.resproj, phase->C->top());
 683   }
 684 }
 685 
 686 
 687 #ifndef PRODUCT
 688 
 689 void ValueTypeNode::dump_spec(outputStream* st) const {
 690   TypeNode::dump_spec(st);
 691 }
 692 
 693 #endif
 694 
 695 ValueTypePtrNode* ValueTypePtrNode::make(PhaseGVN* gvn, CheckCastPPNode* cast) {
 696   ciValueKlass* vk = cast->type()->is_valuetypeptr()->value_type()->value_klass();
 697   ValueTypePtrNode* vt = new ValueTypePtrNode(vk, gvn->C);
 698   assert(cast->in(1)->is_Proj(), "bad graph shape");
 699   ValueTypeBaseNode::make(gvn, cast->in(1)->in(0), vt, vk, 0, TypeFunc::Parms+1, false);
 700   return vt;
 701 }
 702 
 703 ValueTypePtrNode* ValueTypePtrNode::make(PhaseGVN& gvn, Node* mem, Node* oop) {
 704   // Create and initialize a ValueTypePtrNode by loading all field
 705   // values from a heap-allocated version and also save the oop.
 706   ciValueKlass* vk = gvn.type(oop)->is_valuetypeptr()->value_type()->value_klass();
 707   ValueTypePtrNode* vtptr = new ValueTypePtrNode(vk, gvn.C);
 708   vtptr->set_oop(oop);
 709   vtptr->load(gvn, mem, oop, oop, vk);
 710   return vtptr;
 711 }