1 /*
   2  * Copyright (c) 2017, 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/castnode.hpp"
  29 #include "opto/graphKit.hpp"
  30 #include "opto/rootnode.hpp"
  31 #include "opto/valuetypenode.hpp"
  32 #include "opto/phaseX.hpp"
  33 
  34 // Clones the values type to handle control flow merges involving multiple value types.
  35 // The inputs are replaced by PhiNodes to represent the merged values for the given region.
  36 ValueTypeBaseNode* ValueTypeBaseNode::clone_with_phis(PhaseGVN* gvn, Node* region) {
  37   assert(!has_phi_inputs(region), "already cloned with phis");
  38   ValueTypeBaseNode* vt = clone()->as_ValueTypeBase();
  39 
  40   // Create a PhiNode for merging the oop values
  41   const Type* phi_type = Type::get_const_type(value_klass());
  42   PhiNode* oop = PhiNode::make(region, vt->get_oop(), phi_type);
  43   gvn->set_type(oop, phi_type);
  44   vt->set_oop(oop);
  45 
  46   // Create a PhiNode each for merging the field values
  47   for (uint i = 0; i < vt->field_count(); ++i) {
  48     ciType* type = vt->field_type(i);
  49     Node*  value = vt->field_value(i);
  50     if (type->is_valuetype()) {
  51       // Handle flattened value type fields recursively
  52       value = value->as_ValueType()->clone_with_phis(gvn, region);
  53     } else {
  54       phi_type = Type::get_const_type(type);
  55       value = PhiNode::make(region, value, phi_type);
  56       gvn->set_type(value, phi_type);
  57     }
  58     vt->set_field_value(i, value);
  59   }
  60   gvn->set_type(vt, vt->bottom_type());
  61   return vt;
  62 }
  63 
  64 // Checks if the inputs of the ValueBaseTypeNode were replaced by PhiNodes
  65 // for the given region (see ValueBaseTypeNode::clone_with_phis).
  66 bool ValueTypeBaseNode::has_phi_inputs(Node* region) {
  67   // Check oop input
  68   bool result = get_oop()->is_Phi() && get_oop()->as_Phi()->region() == region;
  69 #ifdef ASSERT
  70   if (result) {
  71     // Check all field value inputs for consistency
  72     for (uint i = Oop; i < field_count(); ++i) {
  73       Node* n = in(i);
  74       if (n->is_ValueTypeBase()) {
  75         assert(n->as_ValueTypeBase()->has_phi_inputs(region), "inconsistent phi inputs");
  76       } else {
  77         assert(n->is_Phi() && n->as_Phi()->region() == region, "inconsistent phi inputs");
  78       }
  79     }
  80   }
  81 #endif
  82   return result;
  83 }
  84 
  85 // Merges 'this' with 'other' by updating the input PhiNodes added by 'clone_with_phis'
  86 ValueTypeBaseNode* ValueTypeBaseNode::merge_with(PhaseGVN* gvn, const ValueTypeBaseNode* other, int pnum, bool transform) {
  87   // Merge oop inputs
  88   PhiNode* phi = get_oop()->as_Phi();
  89   phi->set_req(pnum, other->get_oop());
  90   if (transform) {
  91     set_oop(gvn->transform(phi));
  92     gvn->record_for_igvn(phi);
  93   }
  94   // Merge field values
  95   for (uint i = 0; i < field_count(); ++i) {
  96     Node* val1 =        field_value(i);
  97     Node* val2 = other->field_value(i);
  98     if (val1->isa_ValueType()) {
  99       val1->as_ValueType()->merge_with(gvn, val2->as_ValueType(), pnum, transform);
 100     } else {
 101       assert(val1->is_Phi(), "must be a phi node");
 102       assert(!val2->is_ValueType(), "inconsistent merge values");
 103       val1->set_req(pnum, val2);
 104     }
 105     if (transform) {
 106       set_field_value(i, gvn->transform(val1));
 107       gvn->record_for_igvn(val1);
 108     }
 109   }
 110   return this;
 111 }
 112 
 113 // Adds a new merge path to a valuetype node with phi inputs
 114 void ValueTypeBaseNode::add_new_path(Node* region) {
 115   assert(has_phi_inputs(region), "must have phi inputs");
 116 
 117   PhiNode* phi = get_oop()->as_Phi();
 118   phi->add_req(NULL);
 119   assert(phi->req() == region->req(), "must be same size as region");
 120 
 121   for (uint i = 0; i < field_count(); ++i) {
 122     Node* val = field_value(i);
 123     if (val->isa_ValueType()) {
 124       val->as_ValueType()->add_new_path(region);
 125     } else {
 126       val->as_Phi()->add_req(NULL);
 127       assert(val->req() == region->req(), "must be same size as region");
 128     }
 129   }
 130 }
 131 
 132 Node* ValueTypeBaseNode::field_value(uint index) const {
 133   assert(index < field_count(), "index out of bounds");
 134   return in(Values + index);
 135 }
 136 
 137 // Get the value of the field at the given offset.
 138 // If 'recursive' is true, flattened value type fields will be resolved recursively.
 139 Node* ValueTypeBaseNode::field_value_by_offset(int offset, bool recursive) const {
 140   // If the field at 'offset' belongs to a flattened value type field, 'index' refers to the
 141   // corresponding ValueTypeNode input and 'sub_offset' is the offset in flattened value type.
 142   int index = value_klass()->field_index_by_offset(offset);
 143   int sub_offset = offset - field_offset(index);
 144   Node* value = field_value(index);
 145   assert(value != NULL, "field value not found");
 146   if (recursive && value->is_ValueType()) {
 147     ValueTypeNode* vt = value->as_ValueType();
 148     if (field_is_flattened(index)) {
 149       // Flattened value type field
 150       sub_offset += vt->value_klass()->first_field_offset(); // Add header size
 151       return vt->field_value_by_offset(sub_offset, recursive);
 152     } else {
 153       assert(sub_offset == 0, "should not have a sub offset");
 154       return vt;
 155     }
 156   }
 157   assert(!(recursive && value->is_ValueType()), "should not be a value type");
 158   assert(sub_offset == 0, "offset mismatch");
 159   return value;
 160 }
 161 
 162 void ValueTypeBaseNode::set_field_value(uint index, Node* value) {
 163   assert(index < field_count(), "index out of bounds");
 164   set_req(Values + index, value);
 165 }
 166 
 167 int ValueTypeBaseNode::field_offset(uint index) const {
 168   assert(index < field_count(), "index out of bounds");
 169   return value_klass()->declared_nonstatic_field_at(index)->offset();
 170 }
 171 
 172 ciType* ValueTypeBaseNode::field_type(uint index) const {
 173   assert(index < field_count(), "index out of bounds");
 174   return value_klass()->declared_nonstatic_field_at(index)->type();
 175 }
 176 
 177 bool ValueTypeBaseNode::field_is_flattened(uint index) const {
 178   assert(index < field_count(), "index out of bounds");
 179   ciField* field = value_klass()->declared_nonstatic_field_at(index);
 180   assert(!field->is_flattened() || field->type()->is_valuetype(), "must be a value type");
 181   return field->is_flattened();
 182 }
 183 
 184 bool ValueTypeBaseNode::field_is_flattenable(uint index) const {
 185   assert(index < field_count(), "index out of bounds");
 186   ciField* field = value_klass()->declared_nonstatic_field_at(index);
 187   assert(!field->is_flattenable() || field->type()->is_valuetype(), "must be a value type");
 188   return field->is_flattenable();
 189 }
 190 
 191 int ValueTypeBaseNode::make_scalar_in_safepoint(Unique_Node_List& worklist, SafePointNode* sfpt, Node* root, PhaseGVN* gvn) {
 192   ciValueKlass* vk = value_klass();
 193   uint nfields = vk->nof_nonstatic_fields();
 194   JVMState* jvms = sfpt->jvms();
 195   int start = jvms->debug_start();
 196   int end   = jvms->debug_end();
 197   // Replace safepoint edge by SafePointScalarObjectNode and add field values
 198   assert(jvms != NULL, "missing JVMS");
 199   uint first_ind = (sfpt->req() - jvms->scloff());
 200   SafePointScalarObjectNode* sobj = new SafePointScalarObjectNode(value_ptr(),
 201 #ifdef ASSERT
 202                                                                   NULL,
 203 #endif
 204                                                                   first_ind, nfields);
 205   sobj->init_req(0, root);
 206   // Iterate over the value type fields in order of increasing
 207   // offset and add the field values to the safepoint.
 208   for (uint j = 0; j < nfields; ++j) {
 209     int offset = vk->nonstatic_field_at(j)->offset();
 210     Node* value = field_value_by_offset(offset, true /* include flattened value type fields */);
 211     if (value->is_ValueType()) {
 212       if (value->as_ValueType()->is_allocated(gvn)) {
 213         value = value->as_ValueType()->get_oop();
 214       } else {
 215         // Add non-flattened value type field to the worklist to process later
 216         worklist.push(value);
 217       }
 218     }
 219     sfpt->add_req(value);
 220   }
 221   jvms->set_endoff(sfpt->req());
 222   if (gvn != NULL) {
 223     sobj = gvn->transform(sobj)->as_SafePointScalarObject();
 224     gvn->igvn_rehash_node_delayed(sfpt);
 225   }
 226   return sfpt->replace_edges_in_range(this, sobj, start, end);
 227 }
 228 
 229 void ValueTypeBaseNode::make_scalar_in_safepoints(Node* root, PhaseGVN* gvn) {
 230   // Process all safepoint uses and scalarize value type
 231   Unique_Node_List worklist;
 232   for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
 233     Node* u = fast_out(i);
 234     if (u->is_SafePoint() && (!u->is_Call() || u->as_Call()->has_debug_use(this))) {
 235       SafePointNode* sfpt = u->as_SafePoint();
 236       Node* in_oop = get_oop();
 237       const Type* oop_type = in_oop->bottom_type();
 238       assert(Opcode() == Op_ValueTypePtr || !isa_ValueType()->is_allocated(gvn), "already heap allocated value types should be linked directly");
 239       int nb = make_scalar_in_safepoint(worklist, sfpt, root, gvn);
 240       --i; imax -= nb;
 241     }
 242   }
 243   // Now scalarize non-flattened fields
 244   for (uint i = 0; i < worklist.size(); ++i) {
 245     Node* vt = worklist.at(i);
 246     vt->as_ValueType()->make_scalar_in_safepoints(root, gvn);
 247   }
 248 }
 249 
 250 void ValueTypeBaseNode::initialize(GraphKit* kit, MultiNode* multi, ciValueKlass* vk, int base_offset, int base_input, bool in) {
 251   assert(base_offset >= 0, "offset in value type must be positive");
 252   PhaseGVN& gvn = kit->gvn();
 253   for (uint i = 0; i < field_count(); i++) {
 254     ciType* ft = field_type(i);
 255     int offset = base_offset + field_offset(i);
 256     if (field_is_flattened(i)) {
 257       // Flattened value type field
 258       ValueTypeNode* vt = ValueTypeNode::make_uninitialized(gvn, ft->as_value_klass());
 259       vt->initialize(kit, multi, vk, offset - value_klass()->first_field_offset(), base_input, in);
 260       set_field_value(i, gvn.transform(vt));
 261     } else {
 262       int j = 0; int extra = 0;
 263       for (; j < vk->nof_nonstatic_fields(); j++) {
 264         ciField* f = vk->nonstatic_field_at(j);
 265         if (offset == f->offset()) {
 266           assert(f->type() == ft, "inconsistent field type");
 267           break;
 268         }
 269         BasicType bt = f->type()->basic_type();
 270         if (bt == T_LONG || bt == T_DOUBLE) {
 271           extra++;
 272         }
 273       }
 274       assert(j != vk->nof_nonstatic_fields(), "must find");
 275       Node* parm = NULL;
 276       if (multi->is_Start()) {
 277         assert(in, "return from start?");
 278         parm = gvn.transform(new ParmNode(multi->as_Start(), base_input + j + extra));
 279       } else {
 280         if (in) {
 281           parm = multi->as_Call()->in(base_input + j + extra);
 282         } else {
 283           parm = gvn.transform(new ProjNode(multi->as_Call(), base_input + j + extra));
 284         }
 285       }
 286       if (ft->is_valuetype()) {
 287         // Non-flattened value type field
 288         assert(!gvn.type(parm)->is_ptr()->maybe_null(), "should never be null");
 289         parm = ValueTypeNode::make_from_oop(kit, parm, ft->as_value_klass());
 290       }
 291       set_field_value(i, parm);
 292       // Record all these guys for later GVN.
 293       gvn.record_for_igvn(parm);
 294     }
 295   }
 296 }
 297 
 298 const TypePtr* ValueTypeBaseNode::field_adr_type(Node* base, int offset, ciInstanceKlass* holder, PhaseGVN& gvn) const {
 299   const TypeAryPtr* ary_type = gvn.type(base)->isa_aryptr();
 300   const TypePtr* adr_type = NULL;
 301   bool is_array = ary_type != NULL;
 302   if (is_array) {
 303     // In the case of a flattened value type array, each field has its own slice
 304     adr_type = ary_type->with_field_offset(offset)->add_offset(Type::OffsetBot);
 305   } else {
 306     ciField* field = holder->get_field_by_offset(offset, false);
 307     assert(field != NULL, "field not found");
 308     adr_type = gvn.C->alias_type(field)->adr_type();
 309   }
 310   return adr_type;
 311 }
 312 
 313 void ValueTypeBaseNode::load(GraphKit* kit, Node* base, Node* ptr, ciInstanceKlass* holder, int holder_offset) {
 314   // Initialize the value type by loading its field values from
 315   // memory and adding the values as input edges to the node.
 316   for (uint i = 0; i < field_count(); ++i) {
 317     int offset = holder_offset + field_offset(i);
 318     Node* value = NULL;
 319     ciType* ft = field_type(i);
 320     if (field_is_flattened(i)) {
 321       // Recursively load the flattened value type field
 322       value = ValueTypeNode::make_from_flattened(kit, ft->as_value_klass(), base, ptr, holder, offset);
 323     } else {
 324       const TypeOopPtr* oop_ptr = kit->gvn().type(base)->isa_oopptr();
 325       bool is_array = (oop_ptr->isa_aryptr() != NULL);
 326       if (base->is_Con() && !is_array) {
 327         // If the oop to the value type is constant (static final field), we can
 328         // also treat the fields as constants because the value type is immutable.
 329         ciObject* constant_oop = oop_ptr->const_oop();
 330         ciField* field = holder->get_field_by_offset(offset, false);
 331         assert(field != NULL, "field not found");
 332         ciConstant constant = constant_oop->as_instance()->field_value(field);
 333         const Type* con_type = Type::make_from_constant(constant, /*require_const=*/ true);
 334         assert(con_type != NULL, "type not found");
 335         value = kit->gvn().transform(kit->makecon(con_type));
 336         if (con_type->is_valuetypeptr()) {
 337           // Constant, non-flattened value type field
 338           value = ValueTypeNode::make_from_oop(kit, value, ft->as_value_klass());
 339         }
 340       } else {
 341         // Load field value from memory
 342         const TypePtr* adr_type = field_adr_type(base, offset, holder, kit->gvn());
 343         Node* adr = kit->basic_plus_adr(base, ptr, offset);
 344         BasicType bt = type2field[ft->basic_type()];
 345         assert(is_java_primitive(bt) || adr->bottom_type()->is_ptr_to_narrowoop() == UseCompressedOops, "inconsistent");
 346         const Type* val_type = Type::get_const_type(ft);
 347         DecoratorSet decorators = IN_HEAP | MO_UNORDERED;
 348         if (is_array) {
 349           decorators |= IN_HEAP_ARRAY;
 350         }
 351         value = kit->access_load_at(base, adr, adr_type, val_type, bt, decorators);
 352         if (bt == T_VALUETYPE) {
 353           // Loading a non-flattened value type from memory
 354           value = ValueTypeNode::make_from_oop(kit, value, ft->as_value_klass(), /* buffer_check */ false, /* null2default */ field_is_flattenable(i));
 355         }
 356       }
 357     }
 358     set_field_value(i, value);
 359   }
 360 }
 361 
 362 void ValueTypeBaseNode::store_flattened(GraphKit* kit, Node* base, Node* ptr, ciInstanceKlass* holder, int holder_offset) const {
 363   // The value type is embedded into the object without an oop header. Subtract the
 364   // offset of the first field to account for the missing header when storing the values.
 365   if (holder == NULL) {
 366     holder = value_klass();
 367   }
 368   holder_offset -= value_klass()->first_field_offset();
 369   store(kit, base, ptr, holder, holder_offset);
 370 }
 371 
 372 void ValueTypeBaseNode::store(GraphKit* kit, Node* base, Node* ptr, ciInstanceKlass* holder, int holder_offset, bool deoptimize_on_exception) const {
 373   // Write field values to memory
 374   for (uint i = 0; i < field_count(); ++i) {
 375     int offset = holder_offset + field_offset(i);
 376     Node* value = field_value(i);
 377     ciType* ft = field_type(i);
 378     if (field_is_flattened(i)) {
 379       // Recursively store the flattened value type field
 380       value->as_ValueType()->store_flattened(kit, base, ptr, holder, offset);
 381     } else {
 382       // Store field value to memory
 383       const TypePtr* adr_type = field_adr_type(base, offset, holder, kit->gvn());
 384       Node* adr = kit->basic_plus_adr(base, ptr, offset);
 385       BasicType bt = type2field[ft->basic_type()];
 386       assert(is_java_primitive(bt) || adr->bottom_type()->is_ptr_to_narrowoop() == UseCompressedOops, "inconsistent");
 387       const Type* val_type = Type::get_const_type(ft);
 388       const TypeAryPtr* ary_type = kit->gvn().type(base)->isa_aryptr();
 389       DecoratorSet decorators = IN_HEAP | MO_UNORDERED;
 390       if (ary_type != NULL) {
 391         decorators |= IN_HEAP_ARRAY;
 392       }
 393       kit->access_store_at(kit->control(), base, adr, adr_type, value, val_type, bt, decorators, deoptimize_on_exception);
 394     }
 395   }
 396 }
 397 
 398 ValueTypeBaseNode* ValueTypeBaseNode::allocate(GraphKit* kit, bool deoptimize_on_exception) {
 399   Node* in_oop = get_oop();
 400   Node* null_ctl = kit->top();
 401   // Check if value type is already allocated
 402   Node* not_null_oop = kit->null_check_oop(in_oop, &null_ctl);
 403   if (null_ctl->is_top()) {
 404     // Value type is allocated
 405     return this;
 406   }
 407   // Not able to prove that value type is allocated.
 408   // Emit runtime check that may be folded later.
 409   assert(!is_allocated(&kit->gvn()), "should not be allocated");
 410   RegionNode* region = new RegionNode(3);
 411   PhiNode* oop = new PhiNode(region, value_ptr());
 412   PhiNode* io  = new PhiNode(region, Type::ABIO);
 413   PhiNode* mem = new PhiNode(region, Type::MEMORY, TypePtr::BOTTOM);
 414 
 415   // Oop is non-NULL, use it
 416   region->init_req(1, kit->control());
 417   oop   ->init_req(1, not_null_oop);
 418   io    ->init_req(1, kit->i_o());
 419   mem   ->init_req(1, kit->merged_memory());
 420 
 421   // Oop is NULL, allocate value type
 422   kit->set_control(null_ctl);
 423   kit->kill_dead_locals();
 424   ciValueKlass* vk = value_klass();
 425   Node* klass_node = kit->makecon(TypeKlassPtr::make(vk));
 426   Node* alloc_oop  = kit->new_instance(klass_node, NULL, NULL, deoptimize_on_exception, this);
 427   // Write field values to memory
 428   store(kit, alloc_oop, alloc_oop, vk, 0, deoptimize_on_exception);
 429   region->init_req(2, kit->control());
 430   oop   ->init_req(2, alloc_oop);
 431   io    ->init_req(2, kit->i_o());
 432   mem   ->init_req(2, kit->merged_memory());
 433 
 434   // Update GraphKit
 435   kit->set_control(kit->gvn().transform(region));
 436   kit->set_i_o(kit->gvn().transform(io));
 437   kit->set_all_memory(kit->gvn().transform(mem));
 438   kit->record_for_igvn(region);
 439   kit->record_for_igvn(oop);
 440   kit->record_for_igvn(io);
 441   kit->record_for_igvn(mem);
 442 
 443   // Use cloned ValueTypeNode to propagate oop from now on
 444   Node* res_oop = kit->gvn().transform(oop);
 445   ValueTypeBaseNode* vt = clone()->as_ValueTypeBase();
 446   vt->set_oop(res_oop);
 447   vt = kit->gvn().transform(vt)->as_ValueTypeBase();
 448   kit->replace_in_map(this, vt);
 449   return vt;
 450 }
 451 
 452 bool ValueTypeBaseNode::is_allocated(PhaseGVN* phase) const {
 453   Node* oop = get_oop();
 454   const Type* oop_type = (phase != NULL) ? phase->type(oop) : oop->bottom_type();
 455   return !oop_type->is_ptr()->maybe_null();
 456 }
 457 
 458 // When a call returns multiple values, it has several result
 459 // projections, one per field. Replacing the result of the call by a
 460 // value type node (after late inlining) requires that for each result
 461 // projection, we find the corresponding value type field.
 462 void ValueTypeBaseNode::replace_call_results(GraphKit* kit, Node* call, Compile* C) {
 463   ciValueKlass* vk = value_klass();
 464   for (DUIterator_Fast imax, i = call->fast_outs(imax); i < imax; i++) {
 465     ProjNode* pn = call->fast_out(i)->as_Proj();
 466     uint con = pn->_con;
 467     if (con >= TypeFunc::Parms+1) {
 468       uint field_nb = con - (TypeFunc::Parms+1);
 469       int extra = 0;
 470       for (uint j = 0; j < field_nb - extra; j++) {
 471         ciField* f = vk->nonstatic_field_at(j);
 472         BasicType bt = f->type()->basic_type();
 473         if (bt == T_LONG || bt == T_DOUBLE) {
 474           extra++;
 475         }
 476       }
 477       ciField* f = vk->nonstatic_field_at(field_nb - extra);
 478       Node* field = field_value_by_offset(f->offset(), true);
 479       if (field->is_ValueType()) {
 480         assert(f->is_flattened(), "should be flattened");
 481         field = field->as_ValueType()->allocate(kit)->get_oop();
 482       }
 483       C->gvn_replace_by(pn, field);
 484       C->initial_gvn()->hash_delete(pn);
 485       pn->set_req(0, C->top());
 486       --i; --imax;
 487     }
 488   }
 489 }
 490 
 491 ValueTypeNode* ValueTypeNode::make_uninitialized(PhaseGVN& gvn, ciValueKlass* klass) {
 492   // Create a new ValueTypeNode with uninitialized values and NULL oop
 493   const TypeValueType* type = TypeValueType::make(klass);
 494   return new ValueTypeNode(type, gvn.zerocon(T_VALUETYPE));
 495 }
 496 
 497 Node* ValueTypeNode::load_default_oop(PhaseGVN& gvn, ciValueKlass* vk) {
 498   // Load the default oop from the java mirror of the given ValueKlass
 499   const TypeInstPtr* tip = TypeInstPtr::make(vk->java_mirror());
 500   Node* base = gvn.makecon(tip);
 501   Node* adr = gvn.transform(new AddPNode(base, base, gvn.MakeConX(vk->default_value_offset())));
 502   const Type* rt = Type::get_const_type(vk)->join_speculative(TypePtr::NOTNULL);
 503   return gvn.transform(LoadNode::make(gvn, NULL, gvn.C->immutable_memory(), adr, tip, rt, T_VALUETYPE, MemNode::unordered));
 504 }
 505 
 506 ValueTypeNode* ValueTypeNode::make_default(PhaseGVN& gvn, ciValueKlass* vk) {
 507   // Create a new ValueTypeNode with default values
 508   Node* oop = load_default_oop(gvn, vk);
 509   const TypeValueType* type = TypeValueType::make(vk);
 510   ValueTypeNode* vt = new ValueTypeNode(type, oop);
 511   for (uint i = 0; i < vt->field_count(); ++i) {
 512     ciType* field_type = vt->field_type(i);
 513     Node* value = NULL;
 514     if (field_type->is_valuetype()) {
 515       value = ValueTypeNode::make_default(gvn, field_type->as_value_klass());
 516     } else {
 517       value = gvn.zerocon(field_type->basic_type());
 518     }
 519     vt->set_field_value(i, value);
 520   }
 521   vt = gvn.transform(vt)->as_ValueType();
 522   assert(vt->is_default(gvn), "must be the default value type");
 523   return vt;
 524 }
 525 
 526 
 527 bool ValueTypeNode::is_default(PhaseGVN& gvn) const {
 528   for (uint i = 0; i < field_count(); ++i) {
 529     Node* value = field_value(i);
 530     if (!gvn.type(value)->is_zero_type() &&
 531         !(value->is_ValueType() && value->as_ValueType()->is_default(gvn))) {
 532       return false;
 533     }
 534   }
 535   return true;
 536 }
 537 
 538 ValueTypeNode* ValueTypeNode::make_from_oop(GraphKit* kit, Node* oop, ciValueKlass* vk, bool buffer_check, bool null2default, int trap_bci) {
 539   PhaseGVN& gvn = kit->gvn();
 540   const TypePtr* oop_type = gvn.type(oop)->is_ptr();
 541   bool null_check = oop_type->maybe_null();
 542 
 543   // Create and initialize a ValueTypeNode by loading all field
 544   // values from a heap-allocated version and also save the oop.
 545   ValueTypeNode* vt = new ValueTypeNode(TypeValueType::make(vk), oop);
 546 
 547   if (null_check) {
 548     // Add a null check because the oop may be null
 549     Node* null_ctl = kit->top();
 550     Node* not_null_oop = kit->null_check_oop(oop, &null_ctl);
 551     if (kit->stopped()) {
 552       // Constant null
 553       if (null2default) {
 554         kit->set_control(null_ctl);
 555         return make_default(gvn, vk);
 556       } else {
 557         kit->uncommon_trap(Deoptimization::Reason_null_check, Deoptimization::Action_none);
 558         return NULL;
 559       }
 560     }
 561     vt->set_oop(not_null_oop);
 562     vt->load(kit, not_null_oop, not_null_oop, vk);
 563 
 564     if (null2default && (null_ctl != kit->top())) {
 565       // Return default value type if oop is null
 566       ValueTypeNode* def = make_default(gvn, vk);
 567       Node* region = new RegionNode(3);
 568       region->init_req(1, kit->control());
 569       region->init_req(2, null_ctl);
 570 
 571       vt = vt->clone_with_phis(&gvn, region)->as_ValueType();
 572       vt->merge_with(&gvn, def, 2, true);
 573       kit->set_control(gvn.transform(region));
 574     } else if (null_ctl != kit->top()) {
 575       // Deoptimize if oop is null
 576       PreserveJVMState pjvms(kit);
 577       kit->set_control(null_ctl);
 578       int bci = kit->bci();
 579       if (trap_bci != -1) {
 580         // Put trap at different bytecode
 581         kit->push(kit->null());
 582         kit->set_bci(trap_bci);
 583       }
 584       kit->replace_in_map(oop, kit->null());
 585       kit->uncommon_trap(Deoptimization::Reason_null_check, Deoptimization::Action_none);
 586       kit->set_bci(bci);
 587     }
 588   } else {
 589     // Oop can never be null
 590     Node* init_ctl = kit->control();
 591     vt->load(kit, oop, oop, vk);
 592     assert(init_ctl != kit->control() || oop->is_Con() || oop->is_CheckCastPP() || oop->Opcode() == Op_ValueTypePtr ||
 593            vt->is_loaded(&gvn) == oop, "value type should be loaded");
 594   }
 595 
 596   if (buffer_check && vk->is_bufferable()) {
 597     // Check if oop is in heap bounds or if it points into the vtBuffer:
 598     // base <= oop < (base + size)  <=>  (oop - base) <U size
 599     // Discard buffer oops to avoid storing them into fields or arrays.
 600     assert(!gvn.type(oop)->isa_narrowoop(), "should not be a narrow oop");
 601     Node* heap_base = gvn.MakeConX((intptr_t)Universe::heap()->base());
 602     Node* heap_size = gvn.MakeConX(Universe::heap()->max_capacity());
 603     Node* sub = gvn.transform(new SubXNode(gvn.transform(new CastP2XNode(NULL, oop)), heap_base));
 604     Node* chk = gvn.transform(new CmpUXNode(sub, heap_size));
 605     Node* tst = gvn.transform(new BoolNode(chk, BoolTest::lt));
 606     IfNode* iff = gvn.transform(new IfNode(kit->control(), tst, PROB_MAX, COUNT_UNKNOWN))->as_If();
 607 
 608     Node* region = new RegionNode(3);
 609     region->init_req(1, gvn.transform(new IfTrueNode(iff)));
 610     region->init_req(2, gvn.transform(new IfFalseNode(iff)));
 611     Node* new_oop = new PhiNode(region, vt->value_ptr());
 612     new_oop->init_req(1, oop);
 613     new_oop->init_req(2, gvn.zerocon(T_VALUETYPE));
 614 
 615     gvn.hash_delete(vt);
 616     vt->set_oop(gvn.transform(new_oop));
 617     kit->set_control(gvn.transform(region));
 618   }
 619 
 620   assert(vt->is_allocated(&gvn), "value type should be allocated");
 621   return gvn.transform(vt)->as_ValueType();
 622 }
 623 
 624 // GraphKit wrapper for the 'make_from_flattened' method
 625 ValueTypeNode* ValueTypeNode::make_from_flattened(GraphKit* kit, ciValueKlass* vk, Node* obj, Node* ptr, ciInstanceKlass* holder, int holder_offset) {
 626   // Create and initialize a ValueTypeNode by loading all field values from
 627   // a flattened value type field at 'holder_offset' or from a value type array.
 628   ValueTypeNode* vt = make_uninitialized(kit->gvn(), vk);
 629   // The value type is flattened into the object without an oop header. Subtract the
 630   // offset of the first field to account for the missing header when loading the values.
 631   holder_offset -= vk->first_field_offset();
 632   vt->load(kit, obj, ptr, holder, holder_offset);
 633   assert(vt->is_loaded(&kit->gvn()) != obj, "holder oop should not be used as flattened value type oop");
 634   return kit->gvn().transform(vt)->as_ValueType();
 635 }
 636 
 637 ValueTypeNode* ValueTypeNode::make_from_multi(GraphKit* kit, MultiNode* multi, ciValueKlass* vk, int base_input, bool in) {
 638   ValueTypeNode* vt = ValueTypeNode::make_uninitialized(kit->gvn(), vk);
 639   vt->initialize(kit, multi, vk, 0, base_input, in);
 640   return kit->gvn().transform(vt)->as_ValueType();
 641 }
 642 
 643 Node* ValueTypeNode::is_loaded(PhaseGVN* phase, ciValueKlass* vk, Node* base, int holder_offset) {
 644   if (vk == NULL) {
 645     vk = value_klass();
 646   }
 647   if (field_count() == 0) {
 648     assert(is_allocated(phase), "must be allocated");
 649     return get_oop();
 650   }
 651   for (uint i = 0; i < field_count(); ++i) {
 652     int offset = holder_offset + field_offset(i);
 653     Node* value = field_value(i);
 654     if (value->isa_ValueType()) {
 655       ValueTypeNode* vt = value->as_ValueType();
 656       if (field_is_flattened(i)) {
 657         // Check value type field load recursively
 658         base = vt->is_loaded(phase, vk, base, offset - vt->value_klass()->first_field_offset());
 659         if (base == NULL) {
 660           return NULL;
 661         }
 662         continue;
 663       } else {
 664         value = vt->get_oop();
 665         if (value->Opcode() == Op_CastPP) {
 666           // Skip CastPP
 667           value = value->in(1);
 668         }
 669       }
 670     }
 671     if (value->isa_DecodeN()) {
 672       // Skip DecodeN
 673       value = value->in(1);
 674     }
 675     if (value->isa_Load()) {
 676       // Check if base and offset of field load matches value type layout
 677       intptr_t loffset = 0;
 678       Node* lbase = AddPNode::Ideal_base_and_offset(value->in(MemNode::Address), phase, loffset);
 679       if (lbase == NULL || (lbase != base && base != NULL) || loffset != offset) {
 680         return NULL;
 681       } else if (base == NULL) {
 682         // Set base and check if pointer type matches
 683         base = lbase;
 684         const TypeInstPtr* vtptr = phase->type(base)->isa_instptr();
 685         if (vtptr == NULL || !vtptr->klass()->equals(vk)) {
 686           return NULL;
 687         }
 688       }
 689     } else {
 690       return NULL;
 691     }
 692   }
 693   return base;
 694 }
 695 
 696 Node* ValueTypeNode::allocate_fields(GraphKit* kit) {
 697   ValueTypeNode* vt = clone()->as_ValueType();
 698   for (uint i = 0; i < field_count(); i++) {
 699      ValueTypeNode* value = field_value(i)->isa_ValueType();
 700      if (field_is_flattened(i)) {
 701        // Flattened value type field
 702        vt->set_field_value(i, value->allocate_fields(kit));
 703      } else if (value != NULL){
 704        // Non-flattened value type field
 705        vt->set_field_value(i, value->allocate(kit));
 706      }
 707   }
 708   vt = kit->gvn().transform(vt)->as_ValueType();
 709   kit->replace_in_map(this, vt);
 710   return vt;
 711 }
 712 
 713 Node* ValueTypeNode::tagged_klass(PhaseGVN& gvn) {
 714   ciValueKlass* vk = value_klass();
 715   const TypeKlassPtr* tk = TypeKlassPtr::make(vk);
 716   intptr_t bits = tk->get_con();
 717   set_nth_bit(bits, 0);
 718   return gvn.makecon(TypeRawPtr::make((address)bits));
 719 }
 720 
 721 void ValueTypeNode::pass_klass(Node* n, uint pos, const GraphKit& kit) {
 722   n->init_req(pos, tagged_klass(kit.gvn()));
 723 }
 724 
 725 uint ValueTypeNode::pass_fields(Node* n, int base_input, GraphKit& kit, bool assert_allocated, ciValueKlass* base_vk, int base_offset) {
 726   ciValueKlass* vk = value_klass();
 727   if (base_vk == NULL) {
 728     base_vk = vk;
 729   }
 730   uint edges = 0;
 731   for (uint i = 0; i < field_count(); i++) {
 732     int offset = base_offset + field_offset(i) - (base_offset > 0 ? vk->first_field_offset() : 0);
 733     Node* arg = field_value(i);
 734     if (field_is_flattened(i)) {
 735        // Flattened value type field
 736        edges += arg->as_ValueType()->pass_fields(n, base_input, kit, assert_allocated, base_vk, offset);
 737     } else {
 738       int j = 0; int extra = 0;
 739       for (; j < base_vk->nof_nonstatic_fields(); j++) {
 740         ciField* field = base_vk->nonstatic_field_at(j);
 741         if (offset == field->offset()) {
 742           assert(field->type() == field_type(i), "inconsistent field type");
 743           break;
 744         }
 745         BasicType bt = field->type()->basic_type();
 746         if (bt == T_LONG || bt == T_DOUBLE) {
 747           extra++;
 748         }
 749       }
 750       if (arg->is_ValueType()) {
 751         // non-flattened value type field
 752         ValueTypeNode* vt = arg->as_ValueType();
 753         assert(!assert_allocated || vt->is_allocated(&kit.gvn()), "value type field should be allocated");
 754         arg = vt->allocate(&kit)->get_oop();
 755       }
 756       n->init_req(base_input + j + extra, arg);
 757       edges++;
 758       BasicType bt = field_type(i)->basic_type();
 759       if (bt == T_LONG || bt == T_DOUBLE) {
 760         n->init_req(base_input + j + extra + 1, kit.top());
 761         edges++;
 762       }
 763     }
 764   }
 765   return edges;
 766 }
 767 
 768 Node* ValueTypeNode::Ideal(PhaseGVN* phase, bool can_reshape) {
 769   Node* oop = get_oop();
 770 
 771   if (is_default(*phase) && !oop->is_Load() &&
 772       !(oop->is_DecodeN() && oop->in(1)->is_Load())) {
 773     // Use the pre-allocated oop for default value types
 774     Node* oop = load_default_oop(*phase, value_klass());
 775     set_oop(oop);
 776     return this;
 777   }
 778 
 779   if (!is_allocated(phase) && !value_klass()->is_bufferable()) {
 780     // Save base oop if fields are loaded from memory and the value
 781     // type is not buffered (in this case we should not use the oop).
 782     Node* base = is_loaded(phase);
 783     if (base != NULL) {
 784       set_oop(base);
 785       assert(is_allocated(phase), "should now be allocated");
 786       return this;
 787     }
 788   }
 789 
 790   if (can_reshape) {
 791     PhaseIterGVN* igvn = phase->is_IterGVN();
 792 
 793     if (is_default(*phase)) {
 794       // Search for users of the default value type
 795       for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
 796         Node* user = fast_out(i);
 797         AllocateNode* alloc = user->isa_Allocate();
 798         if (alloc != NULL && alloc->result_cast() != NULL && alloc->in(AllocateNode::ValueNode) == this) {
 799           // Found an allocation of the default value type.
 800           // If the code in StoreNode::Identity() that removes useless stores was not yet
 801           // executed or ReduceFieldZeroing is disabled, there can still be initializing
 802           // stores (only zero-type stores, because value types are immutable).
 803           Node* res = alloc->result_cast();
 804           for (DUIterator_Fast jmax, j = res->fast_outs(jmax); j < jmax; j++) {
 805             AddPNode* addp = res->fast_out(j)->isa_AddP();
 806             if (addp != NULL) {
 807               for (DUIterator_Fast kmax, k = addp->fast_outs(kmax); k < kmax; k++) {
 808                 StoreNode* store = addp->fast_out(k)->isa_Store();
 809                 if (store != NULL && store->outcnt() != 0) {
 810                   // Remove the useless store
 811                   Node* mem = store->in(MemNode::Memory);
 812                   Node* val = store->in(MemNode::ValueIn);
 813                   assert(igvn->type(val)->is_zero_type(), "must be zero-type store");
 814                   igvn->replace_in_uses(store, mem);
 815                 }
 816               }
 817             }
 818           }
 819           // Replace allocation by pre-allocated oop
 820           Node* oop = load_default_oop(*phase, value_klass());
 821           igvn->replace_node(res, oop);
 822         } else if (user->is_ValueType()) {
 823           // Add value type user to worklist to give it a chance to get optimized as well
 824           igvn->_worklist.push(user);
 825         }
 826       }
 827     }
 828 
 829     if (is_allocated(igvn)) {
 830       // Value type is heap allocated, search for safepoint uses
 831       for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
 832         Node* out = fast_out(i);
 833         if (out->is_SafePoint()) {
 834           // Let SafePointNode::Ideal() take care of re-wiring the
 835           // safepoint to the oop input instead of the value type node.
 836           igvn->rehash_node_delayed(out);
 837         }
 838       }
 839     }
 840   }
 841   return NULL;
 842 }
 843 
 844 // Search for multiple allocations of this value type
 845 // and try to replace them by dominating allocations.
 846 void ValueTypeNode::remove_redundant_allocations(PhaseIterGVN* igvn, PhaseIdealLoop* phase) {
 847   assert(EliminateAllocations, "allocation elimination should be enabled");
 848   // Search for allocations of this value type
 849   for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
 850     AllocateNode* alloc = fast_out(i)->isa_Allocate();
 851     if (alloc != NULL && alloc->result_cast() != NULL && alloc->in(AllocateNode::ValueNode) == this) {
 852       assert(!is_default(*igvn), "default value type allocation");
 853       Node* res_dom = NULL;
 854       if (is_allocated(igvn)) {
 855         // The value type is already allocated but still connected to an AllocateNode.
 856         // This can happen with late inlining when we first allocate a value type argument
 857         // but later decide to inline the call with the callee code also allocating.
 858         res_dom = get_oop();
 859       } else {
 860         // Search for a dominating allocation of the same value type
 861         for (DUIterator_Fast jmax, j = fast_outs(jmax); j < jmax; j++) {
 862           Node* out2 = fast_out(j);
 863           if (alloc != out2 && out2->is_Allocate() && out2->in(AllocateNode::ValueNode) == this &&
 864               phase->is_dominator(out2, alloc)) {
 865             AllocateNode* alloc_dom =  out2->as_Allocate();
 866             assert(alloc->in(AllocateNode::KlassNode) == alloc_dom->in(AllocateNode::KlassNode), "klasses should match");
 867             res_dom = alloc_dom->result_cast();
 868             break;
 869           }
 870         }
 871       }
 872       if (res_dom != NULL) {
 873         // Move users to dominating allocation
 874         Node* res = alloc->result_cast();
 875         igvn->replace_node(res, res_dom);
 876         // The result of the dominated allocation is now unused and will be
 877         // removed later in AllocateNode::Ideal() to not confuse loop opts.
 878         igvn->record_for_igvn(alloc);
 879 #ifdef ASSERT
 880         if (PrintEliminateAllocations) {
 881           tty->print("++++ Eliminated: %d Allocate ", alloc->_idx);
 882           dump_spec(tty);
 883           tty->cr();
 884         }
 885 #endif
 886       }
 887     }
 888   }
 889 
 890   // Process users
 891   for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
 892     Node* out = fast_out(i);
 893     if (out->isa_ValueType() != NULL) {
 894       // Recursively process value type users
 895       out->as_ValueType()->remove_redundant_allocations(igvn, phase);
 896     } else if (out->isa_Allocate() != NULL) {
 897       // Allocate users should be linked
 898       assert(out->in(AllocateNode::ValueNode) == this, "should be linked");
 899     } else {
 900 #ifdef ASSERT
 901       // The value type should not have any other users at this time
 902       out->dump();
 903       assert(false, "unexpected user of value type");
 904 #endif
 905     }
 906   }
 907 }
 908 
 909 ValueTypePtrNode* ValueTypePtrNode::make_from_value_type(PhaseGVN& gvn, ValueTypeNode* vt) {
 910   ValueTypePtrNode* vtptr = new ValueTypePtrNode(vt->value_klass(), vt->get_oop());
 911   for (uint i = Oop+1; i < vt->req(); i++) {
 912     vtptr->init_req(i, vt->in(i));
 913   }
 914   return gvn.transform(vtptr)->as_ValueTypePtr();
 915 }
 916 
 917 ValueTypePtrNode* ValueTypePtrNode::make_from_call(GraphKit* kit, ciValueKlass* vk, CallNode* call) {
 918   ValueTypePtrNode* vtptr = new ValueTypePtrNode(vk, kit->zerocon(T_VALUETYPE));
 919   vtptr->initialize(kit, call, vk);
 920   return vtptr;
 921 }
 922 
 923 ValueTypePtrNode* ValueTypePtrNode::make_from_oop(GraphKit* kit, Node* oop) {
 924   // Create and initialize a ValueTypePtrNode by loading all field
 925   // values from a heap-allocated version and also save the oop.
 926   ciValueKlass* vk = kit->gvn().type(oop)->value_klass();
 927   ValueTypePtrNode* vtptr = new ValueTypePtrNode(vk, oop);
 928   vtptr->load(kit, oop, oop, vk);
 929   return vtptr;
 930 }