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() && value->isa_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->is_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->is_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 void ValueTypeBaseNode::set_field_value_by_offset(int offset, Node* value) { 168 set_field_value(field_index(offset), value); 169 } 170 171 int ValueTypeBaseNode::field_offset(uint index) const { 172 assert(index < field_count(), "index out of bounds"); 173 return value_klass()->declared_nonstatic_field_at(index)->offset(); 174 } 175 176 uint ValueTypeBaseNode::field_index(int offset) const { 177 uint i = 0; 178 for (; i < field_count() && field_offset(i) != offset; i++) { } 179 assert(i < field_count(), "field not found"); 180 return i; 181 } 182 183 ciType* ValueTypeBaseNode::field_type(uint index) const { 184 assert(index < field_count(), "index out of bounds"); 185 return value_klass()->declared_nonstatic_field_at(index)->type(); 186 } 187 188 bool ValueTypeBaseNode::field_is_flattened(uint index) const { 189 assert(index < field_count(), "index out of bounds"); 190 ciField* field = value_klass()->declared_nonstatic_field_at(index); 191 assert(!field->is_flattened() || field->type()->is_valuetype(), "must be a value type"); 192 return field->is_flattened(); 193 } 194 195 bool ValueTypeBaseNode::field_is_flattenable(uint index) const { 196 assert(index < field_count(), "index out of bounds"); 197 ciField* field = value_klass()->declared_nonstatic_field_at(index); 198 assert(!field->is_flattenable() || field->type()->is_valuetype(), "must be a value type"); 199 return field->is_flattenable(); 200 } 201 202 int ValueTypeBaseNode::make_scalar_in_safepoint(PhaseIterGVN* igvn, Unique_Node_List& worklist, SafePointNode* sfpt) { 203 ciValueKlass* vk = value_klass(); 204 uint nfields = vk->nof_nonstatic_fields(); 205 JVMState* jvms = sfpt->jvms(); 206 int start = jvms->debug_start(); 207 int end = jvms->debug_end(); 208 // Replace safepoint edge by SafePointScalarObjectNode and add field values 209 assert(jvms != NULL, "missing JVMS"); 210 uint first_ind = (sfpt->req() - jvms->scloff()); 211 SafePointScalarObjectNode* sobj = new SafePointScalarObjectNode(value_ptr(), 212 #ifdef ASSERT 213 NULL, 214 #endif 215 first_ind, nfields); 216 sobj->init_req(0, igvn->C->root()); 217 // Iterate over the value type fields in order of increasing 218 // offset and add the field values to the safepoint. 219 for (uint j = 0; j < nfields; ++j) { 220 int offset = vk->nonstatic_field_at(j)->offset(); 221 Node* value = field_value_by_offset(offset, true /* include flattened value type fields */); 222 if (value->is_ValueType()) { 223 // Add value type field to the worklist to process later 224 worklist.push(value); 225 } 226 sfpt->add_req(value); 227 } 228 jvms->set_endoff(sfpt->req()); 229 sobj = igvn->transform(sobj)->as_SafePointScalarObject(); 230 igvn->rehash_node_delayed(sfpt); 231 return sfpt->replace_edges_in_range(this, sobj, start, end); 232 } 233 234 void ValueTypeBaseNode::make_scalar_in_safepoints(PhaseIterGVN* igvn) { 235 // Process all safepoint uses and scalarize value type 236 Unique_Node_List worklist; 237 for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) { 238 SafePointNode* sfpt = fast_out(i)->isa_SafePoint(); 239 if (sfpt != NULL && !sfpt->is_CallLeaf() && (!sfpt->is_Call() || sfpt->as_Call()->has_debug_use(this))) { 240 int nb = 0; 241 if (is_allocated(igvn) && get_oop()->is_Con()) { 242 // Value type is allocated with a constant oop, link it directly 243 nb = sfpt->replace_edges_in_range(this, get_oop(), sfpt->jvms()->debug_start(), sfpt->jvms()->debug_end()); 244 igvn->rehash_node_delayed(sfpt); 245 } else { 246 nb = make_scalar_in_safepoint(igvn, worklist, sfpt); 247 } 248 --i; imax -= nb; 249 } 250 } 251 // Now scalarize non-flattened fields 252 for (uint i = 0; i < worklist.size(); ++i) { 253 Node* vt = worklist.at(i); 254 vt->as_ValueType()->make_scalar_in_safepoints(igvn); 255 } 256 } 257 258 const TypePtr* ValueTypeBaseNode::field_adr_type(Node* base, int offset, ciInstanceKlass* holder, DecoratorSet decorators, PhaseGVN& gvn) const { 259 const TypeAryPtr* ary_type = gvn.type(base)->isa_aryptr(); 260 const TypePtr* adr_type = NULL; 261 bool is_array = ary_type != NULL; 262 if ((decorators & C2_MISMATCHED) != 0) { 263 adr_type = TypeRawPtr::BOTTOM; 264 } else if (is_array) { 265 // In the case of a flattened value type array, each field has its own slice 266 adr_type = ary_type->with_field_offset(offset)->add_offset(Type::OffsetBot); 267 } else { 268 ciField* field = holder->get_field_by_offset(offset, false); 269 assert(field != NULL, "field not found"); 270 adr_type = gvn.C->alias_type(field)->adr_type(); 271 } 272 return adr_type; 273 } 274 275 void ValueTypeBaseNode::load(GraphKit* kit, Node* base, Node* ptr, ciInstanceKlass* holder, int holder_offset, DecoratorSet decorators) { 276 // Initialize the value type by loading its field values from 277 // memory and adding the values as input edges to the node. 278 for (uint i = 0; i < field_count(); ++i) { 279 int offset = holder_offset + field_offset(i); 280 Node* value = NULL; 281 ciType* ft = field_type(i); 282 bool is_flattenable = field_is_flattenable(i); 283 if (field_is_flattened(i)) { 284 // Recursively load the flattened value type field 285 value = ValueTypeNode::make_from_flattened(kit, ft->as_value_klass(), base, ptr, holder, offset, decorators); 286 } else { 287 const TypeOopPtr* oop_ptr = kit->gvn().type(base)->isa_oopptr(); 288 bool is_array = (oop_ptr->isa_aryptr() != NULL); 289 if (base->is_Con() && !is_array) { 290 // If the oop to the value type is constant (static final field), we can 291 // also treat the fields as constants because the value type is immutable. 292 ciObject* constant_oop = oop_ptr->const_oop(); 293 ciField* field = holder->get_field_by_offset(offset, false); 294 assert(field != NULL, "field not found"); 295 ciConstant constant = constant_oop->as_instance()->field_value(field); 296 const Type* con_type = Type::make_from_constant(constant, /*require_const=*/ true); 297 assert(con_type != NULL, "type not found"); 298 value = kit->gvn().transform(kit->makecon(con_type)); 299 if (ft->is_valuetype() && !constant.as_object()->is_null_object()) { 300 // Null-free, treat as flattenable 301 is_flattenable = true; 302 } 303 } else { 304 // Load field value from memory 305 const TypePtr* adr_type = field_adr_type(base, offset, holder, decorators, kit->gvn()); 306 Node* adr = kit->basic_plus_adr(base, ptr, offset); 307 BasicType bt = type2field[ft->basic_type()]; 308 assert(is_java_primitive(bt) || adr->bottom_type()->is_ptr_to_narrowoop() == UseCompressedOops, "inconsistent"); 309 const Type* val_type = Type::get_const_type(ft); 310 if (is_array) { 311 decorators |= IS_ARRAY; 312 } 313 value = kit->access_load_at(base, adr, adr_type, val_type, bt, decorators); 314 } 315 if (is_flattenable) { 316 // Loading a non-flattened but flattenable value type from memory 317 if (ft->as_value_klass()->is_scalarizable()) { 318 value = ValueTypeNode::make_from_oop(kit, value, ft->as_value_klass()); 319 } else { 320 value = kit->null2default(value, ft->as_value_klass()); 321 } 322 } 323 } 324 set_field_value(i, value); 325 } 326 } 327 328 void ValueTypeBaseNode::store_flattened(GraphKit* kit, Node* base, Node* ptr, ciInstanceKlass* holder, int holder_offset, DecoratorSet decorators) const { 329 // The value type is embedded into the object without an oop header. Subtract the 330 // offset of the first field to account for the missing header when storing the values. 331 if (holder == NULL) { 332 holder = value_klass(); 333 } 334 holder_offset -= value_klass()->first_field_offset(); 335 store(kit, base, ptr, holder, holder_offset, decorators); 336 } 337 338 void ValueTypeBaseNode::store(GraphKit* kit, Node* base, Node* ptr, ciInstanceKlass* holder, int holder_offset, DecoratorSet decorators) const { 339 // Write field values to memory 340 for (uint i = 0; i < field_count(); ++i) { 341 int offset = holder_offset + field_offset(i); 342 Node* value = field_value(i); 343 ciType* ft = field_type(i); 344 if (field_is_flattened(i)) { 345 // Recursively store the flattened value type field 346 if (!value->is_ValueType()) { 347 assert(!kit->gvn().type(value)->maybe_null(), "should never be null"); 348 value = ValueTypeNode::make_from_oop(kit, value, ft->as_value_klass()); 349 } 350 value->as_ValueType()->store_flattened(kit, base, ptr, holder, offset, decorators); 351 } else { 352 // Store field value to memory 353 const TypePtr* adr_type = field_adr_type(base, offset, holder, decorators, kit->gvn()); 354 Node* adr = kit->basic_plus_adr(base, ptr, offset); 355 BasicType bt = type2field[ft->basic_type()]; 356 assert(is_java_primitive(bt) || adr->bottom_type()->is_ptr_to_narrowoop() == UseCompressedOops, "inconsistent"); 357 const Type* val_type = Type::get_const_type(ft); 358 const TypeAryPtr* ary_type = kit->gvn().type(base)->isa_aryptr(); 359 if (ary_type != NULL) { 360 decorators |= IS_ARRAY; 361 } 362 kit->access_store_at(base, adr, adr_type, value, val_type, bt, decorators); 363 } 364 } 365 } 366 367 ValueTypeBaseNode* ValueTypeBaseNode::allocate(GraphKit* kit, bool safe_for_replace) { 368 // Check if value type is already allocated 369 Node* null_ctl = kit->top(); 370 Node* not_null_oop = kit->null_check_oop(get_oop(), &null_ctl); 371 if (null_ctl->is_top()) { 372 // Value type is allocated 373 return this; 374 } 375 assert(!is_allocated(&kit->gvn()), "should not be allocated"); 376 RegionNode* region = new RegionNode(3); 377 378 // Oop is non-NULL, use it 379 region->init_req(1, kit->control()); 380 PhiNode* oop = PhiNode::make(region, not_null_oop, value_ptr()); 381 PhiNode* io = PhiNode::make(region, kit->i_o(), Type::ABIO); 382 PhiNode* mem = PhiNode::make(region, kit->merged_memory(), Type::MEMORY, TypePtr::BOTTOM); 383 384 int bci = kit->bci(); 385 bool reexecute = kit->jvms()->should_reexecute(); 386 { 387 // Oop is NULL, allocate and initialize buffer 388 PreserveJVMState pjvms(kit); 389 // Propagate re-execution state and bci 390 kit->set_bci(bci); 391 kit->jvms()->set_bci(bci); 392 kit->jvms()->set_should_reexecute(reexecute); 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, /* deoptimize_on_exception */ true, this); 398 store(kit, alloc_oop, alloc_oop, vk, 0); 399 400 // Do not let stores that initialize this buffer be reordered with a subsequent 401 // store that would make this buffer accessible by other threads. 402 AllocateNode* alloc = AllocateNode::Ideal_allocation(alloc_oop, &kit->gvn()); 403 assert(alloc != NULL, "must have an allocation node"); 404 kit->insert_mem_bar(Op_MemBarStoreStore, alloc->proj_out_or_null(AllocateNode::RawAddress)); 405 406 region->init_req(2, kit->control()); 407 oop ->init_req(2, alloc_oop); 408 io ->init_req(2, kit->i_o()); 409 mem ->init_req(2, kit->merged_memory()); 410 } 411 412 // Update GraphKit 413 kit->set_control(kit->gvn().transform(region)); 414 kit->set_i_o(kit->gvn().transform(io)); 415 kit->set_all_memory(kit->gvn().transform(mem)); 416 kit->record_for_igvn(region); 417 kit->record_for_igvn(oop); 418 kit->record_for_igvn(io); 419 kit->record_for_igvn(mem); 420 421 // Use cloned ValueTypeNode to propagate oop from now on 422 Node* res_oop = kit->gvn().transform(oop); 423 ValueTypeBaseNode* vt = clone()->as_ValueTypeBase(); 424 vt->set_oop(res_oop); 425 vt = kit->gvn().transform(vt)->as_ValueTypeBase(); 426 if (safe_for_replace) { 427 kit->replace_in_map(this, vt); 428 } 429 assert(vt->is_allocated(&kit->gvn()), "must be allocated"); 430 return vt; 431 } 432 433 bool ValueTypeBaseNode::is_allocated(PhaseGVN* phase) const { 434 Node* oop = get_oop(); 435 const Type* oop_type = (phase != NULL) ? phase->type(oop) : oop->bottom_type(); 436 return !oop_type->maybe_null(); 437 } 438 439 // When a call returns multiple values, it has several result 440 // projections, one per field. Replacing the result of the call by a 441 // value type node (after late inlining) requires that for each result 442 // projection, we find the corresponding value type field. 443 void ValueTypeBaseNode::replace_call_results(GraphKit* kit, Node* call, Compile* C) { 444 ciValueKlass* vk = value_klass(); 445 for (DUIterator_Fast imax, i = call->fast_outs(imax); i < imax; i++) { 446 ProjNode* pn = call->fast_out(i)->as_Proj(); 447 uint con = pn->_con; 448 if (con >= TypeFunc::Parms+1) { 449 uint field_nb = con - (TypeFunc::Parms+1); 450 int extra = 0; 451 for (uint j = 0; j < field_nb - extra; j++) { 452 ciField* f = vk->nonstatic_field_at(j); 453 BasicType bt = f->type()->basic_type(); 454 if (bt == T_LONG || bt == T_DOUBLE) { 455 extra++; 456 } 457 } 458 ciField* f = vk->nonstatic_field_at(field_nb - extra); 459 Node* field = field_value_by_offset(f->offset(), true); 460 if (field->is_ValueType()) { 461 assert(field->as_ValueType()->is_allocated(&kit->gvn()), "must be allocated"); 462 field = field->as_ValueType()->get_oop(); 463 } 464 C->gvn_replace_by(pn, field); 465 C->initial_gvn()->hash_delete(pn); 466 pn->set_req(0, C->top()); 467 --i; --imax; 468 } 469 } 470 } 471 472 Node* ValueTypeBaseNode::allocate_fields(GraphKit* kit) { 473 ValueTypeBaseNode* vt = clone()->as_ValueTypeBase(); 474 for (uint i = 0; i < field_count(); i++) { 475 ValueTypeNode* value = field_value(i)->isa_ValueType(); 476 if (field_is_flattened(i)) { 477 // Flattened value type field 478 vt->set_field_value(i, value->allocate_fields(kit)); 479 } else if (value != NULL) { 480 // Non-flattened value type field 481 vt->set_field_value(i, value->allocate(kit)); 482 } 483 } 484 vt = kit->gvn().transform(vt)->as_ValueTypeBase(); 485 kit->replace_in_map(this, vt); 486 return vt; 487 } 488 489 ValueTypeNode* ValueTypeNode::make_uninitialized(PhaseGVN& gvn, ciValueKlass* vk) { 490 // Create a new ValueTypeNode with uninitialized values and NULL oop 491 return new ValueTypeNode(vk, gvn.zerocon(T_VALUETYPE)); 492 } 493 494 Node* ValueTypeNode::default_oop(PhaseGVN& gvn, ciValueKlass* vk) { 495 // Returns the constant oop of the default value type allocation 496 assert(vk->is_initialized(), "must be initialized"); 497 return gvn.makecon(TypeInstPtr::make(vk->default_value_instance())); 498 } 499 500 ValueTypeNode* ValueTypeNode::make_default(PhaseGVN& gvn, ciValueKlass* vk) { 501 // Create a new ValueTypeNode with default values 502 ValueTypeNode* vt = new ValueTypeNode(vk, default_oop(gvn, vk)); 503 for (uint i = 0; i < vt->field_count(); ++i) { 504 ciType* field_type = vt->field_type(i); 505 Node* value = NULL; 506 if (field_type->is_valuetype() && vt->field_is_flattenable(i)) { 507 ciValueKlass* field_klass = field_type->as_value_klass(); 508 if (field_klass->is_scalarizable() || vt->field_is_flattened(i)) { 509 value = ValueTypeNode::make_default(gvn, field_klass); 510 } else { 511 value = default_oop(gvn, field_klass); 512 } 513 } else { 514 value = gvn.zerocon(field_type->basic_type()); 515 } 516 vt->set_field_value(i, value); 517 } 518 vt = gvn.transform(vt)->as_ValueType(); 519 assert(vt->is_default(gvn), "must be the default value type"); 520 return vt; 521 } 522 523 bool ValueTypeNode::is_default(PhaseGVN& gvn) const { 524 for (uint i = 0; i < field_count(); ++i) { 525 Node* value = field_value(i); 526 if (!gvn.type(value)->is_zero_type() && 527 !(value->is_ValueType() && value->as_ValueType()->is_default(gvn)) && 528 !(field_type(i)->is_valuetype() && value == default_oop(gvn, field_type(i)->as_value_klass()))) { 529 return false; 530 } 531 } 532 return true; 533 } 534 535 ValueTypeNode* ValueTypeNode::make_from_oop(GraphKit* kit, Node* oop, ciValueKlass* vk) { 536 PhaseGVN& gvn = kit->gvn(); 537 538 // Create and initialize a ValueTypeNode by loading all field 539 // values from a heap-allocated version and also save the oop. 540 ValueTypeNode* vt = new ValueTypeNode(vk, oop); 541 542 if (oop->isa_ValueTypePtr()) { 543 // Can happen with late inlining 544 ValueTypePtrNode* vtptr = oop->as_ValueTypePtr(); 545 vt->set_oop(vtptr->get_oop()); 546 for (uint i = Oop+1; i < vtptr->req(); ++i) { 547 vt->init_req(i, vtptr->in(i)); 548 } 549 } else if (gvn.type(oop)->maybe_null()) { 550 // Add a null check because the oop may be null 551 Node* null_ctl = kit->top(); 552 Node* not_null_oop = kit->null_check_oop(oop, &null_ctl); 553 if (kit->stopped()) { 554 // Constant null 555 kit->set_control(null_ctl); 556 return make_default(gvn, vk); 557 } 558 vt->set_oop(not_null_oop); 559 vt->load(kit, not_null_oop, not_null_oop, vk, /* holder_offset */ 0); 560 561 if (null_ctl != kit->top()) { 562 // Return default value type if oop is null 563 ValueTypeNode* def = make_default(gvn, vk); 564 Node* region = new RegionNode(3); 565 region->init_req(1, kit->control()); 566 region->init_req(2, null_ctl); 567 568 vt = vt->clone_with_phis(&gvn, region)->as_ValueType(); 569 vt->merge_with(&gvn, def, 2, true); 570 kit->set_control(gvn.transform(region)); 571 } 572 } else { 573 // Oop can never be null 574 Node* init_ctl = kit->control(); 575 vt->load(kit, oop, oop, vk, /* holder_offset */ 0); 576 assert(init_ctl != kit->control() || !gvn.type(oop)->is_valuetypeptr() || oop->is_Con() || oop->Opcode() == Op_ValueTypePtr || 577 AllocateNode::Ideal_allocation(oop, &gvn) != NULL || vt->is_loaded(&gvn) == oop, "value type should be loaded"); 578 } 579 580 assert(vt->is_allocated(&gvn), "value type should be allocated"); 581 return gvn.transform(vt)->as_ValueType(); 582 } 583 584 // GraphKit wrapper for the 'make_from_flattened' method 585 ValueTypeNode* ValueTypeNode::make_from_flattened(GraphKit* kit, ciValueKlass* vk, Node* obj, Node* ptr, ciInstanceKlass* holder, int holder_offset, DecoratorSet decorators) { 586 // Create and initialize a ValueTypeNode by loading all field values from 587 // a flattened value type field at 'holder_offset' or from a value type array. 588 ValueTypeNode* vt = make_uninitialized(kit->gvn(), vk); 589 // The value type is flattened into the object without an oop header. Subtract the 590 // offset of the first field to account for the missing header when loading the values. 591 holder_offset -= vk->first_field_offset(); 592 vt->load(kit, obj, ptr, holder, holder_offset, decorators); 593 assert(vt->is_loaded(&kit->gvn()) != obj, "holder oop should not be used as flattened value type oop"); 594 return kit->gvn().transform(vt)->as_ValueType(); 595 } 596 597 ValueTypeNode* ValueTypeNode::make_from_multi(GraphKit* kit, MultiNode* multi, ExtendedSignature& sig, ciValueKlass* vk, uint& base_input, bool in) { 598 ValueTypeNode* vt = ValueTypeNode::make_uninitialized(kit->gvn(), vk); 599 vt->initialize_fields(kit, multi, sig, base_input, 0, in); 600 return kit->gvn().transform(vt)->as_ValueType(); 601 } 602 603 ValueTypeNode* ValueTypeNode::make_larval(GraphKit* kit, bool allocate) const { 604 ciValueKlass* vk = value_klass(); 605 ValueTypeNode* res = clone()->as_ValueType(); 606 if (allocate) { 607 // Re-execute if buffering triggers deoptimization 608 PreserveReexecuteState preexecs(kit); 609 kit->jvms()->set_should_reexecute(true); 610 Node* klass_node = kit->makecon(TypeKlassPtr::make(vk)); 611 Node* alloc_oop = kit->new_instance(klass_node, NULL, NULL, true); 612 AllocateNode* alloc = AllocateNode::Ideal_allocation(alloc_oop, &kit->gvn()); 613 alloc->_larval = true; 614 615 store(kit, alloc_oop, alloc_oop, vk, 0); 616 res->set_oop(alloc_oop); 617 } 618 res->set_type(TypeValueType::make(vk, true)); 619 res = kit->gvn().transform(res)->as_ValueType(); 620 assert(!allocate || res->is_allocated(&kit->gvn()), "must be allocated"); 621 return res; 622 } 623 624 ValueTypeNode* ValueTypeNode::finish_larval(GraphKit* kit) const { 625 Node* obj = get_oop(); 626 Node* mark_addr = kit->basic_plus_adr(obj, oopDesc::mark_offset_in_bytes()); 627 Node* mark = kit->make_load(NULL, mark_addr, TypeX_X, TypeX_X->basic_type(), MemNode::unordered); 628 mark = kit->gvn().transform(new AndXNode(mark, kit->MakeConX(~markWord::larval_mask_in_place))); 629 kit->store_to_memory(kit->control(), mark_addr, mark, TypeX_X->basic_type(), kit->gvn().type(mark_addr)->is_ptr(), MemNode::unordered); 630 631 // Do not let stores that initialize this buffer be reordered with a subsequent 632 // store that would make this buffer accessible by other threads. 633 AllocateNode* alloc = AllocateNode::Ideal_allocation(obj, &kit->gvn()); 634 assert(alloc != NULL, "must have an allocation node"); 635 kit->insert_mem_bar(Op_MemBarStoreStore, alloc->proj_out_or_null(AllocateNode::RawAddress)); 636 637 ciValueKlass* vk = value_klass(); 638 ValueTypeNode* res = clone()->as_ValueType(); 639 res->set_type(TypeValueType::make(vk, false)); 640 res = kit->gvn().transform(res)->as_ValueType(); 641 return res; 642 } 643 644 Node* ValueTypeNode::is_loaded(PhaseGVN* phase, ciValueKlass* vk, Node* base, int holder_offset) { 645 if (vk == NULL) { 646 vk = value_klass(); 647 } 648 if (field_count() == 0) { 649 assert(is_allocated(phase), "must be allocated"); 650 return get_oop(); 651 } 652 for (uint i = 0; i < field_count(); ++i) { 653 int offset = holder_offset + field_offset(i); 654 Node* value = field_value(i); 655 if (value->is_ValueType()) { 656 ValueTypeNode* vt = value->as_ValueType(); 657 if (field_is_flattened(i)) { 658 // Check value type field load recursively 659 base = vt->is_loaded(phase, vk, base, offset - vt->value_klass()->first_field_offset()); 660 if (base == NULL) { 661 return NULL; 662 } 663 continue; 664 } else { 665 value = vt->get_oop(); 666 if (value->Opcode() == Op_CastPP) { 667 // Skip CastPP 668 value = value->in(1); 669 } 670 } 671 } 672 if (value->isa_DecodeN()) { 673 // Skip DecodeN 674 value = value->in(1); 675 } 676 if (value->isa_Load()) { 677 // Check if base and offset of field load matches value type layout 678 intptr_t loffset = 0; 679 Node* lbase = AddPNode::Ideal_base_and_offset(value->in(MemNode::Address), phase, loffset); 680 if (lbase == NULL || (lbase != base && base != NULL) || loffset != offset) { 681 return NULL; 682 } else if (base == NULL) { 683 // Set base and check if pointer type matches 684 base = lbase; 685 const TypeInstPtr* vtptr = phase->type(base)->isa_instptr(); 686 if (vtptr == NULL || !vtptr->klass()->equals(vk)) { 687 return NULL; 688 } 689 } 690 } else { 691 return NULL; 692 } 693 } 694 return base; 695 } 696 697 Node* ValueTypeNode::tagged_klass(ciValueKlass* vk, PhaseGVN& gvn) { 698 const TypeKlassPtr* tk = TypeKlassPtr::make(vk); 699 intptr_t bits = tk->get_con(); 700 set_nth_bit(bits, 0); 701 return gvn.makecon(TypeRawPtr::make((address)bits)); 702 } 703 704 void ValueTypeNode::pass_fields(GraphKit* kit, Node* n, ExtendedSignature& sig, uint& base_input, int base_offset) { 705 for (uint i = 0; i < field_count(); i++) { 706 int sig_offset = (*sig)._offset; 707 uint idx = field_index(sig_offset - base_offset); 708 Node* arg = field_value(idx); 709 710 if (field_is_flattened(idx)) { 711 // Flattened value type field 712 ValueTypeNode* vt = arg->as_ValueType(); 713 vt->pass_fields(kit, n, sig, base_input, sig_offset - vt->value_klass()->first_field_offset()); 714 } else { 715 if (arg->is_ValueType()) { 716 // Non-flattened value type field 717 ValueTypeNode* vt = arg->as_ValueType(); 718 assert(n->Opcode() != Op_Return || vt->is_allocated(&kit->gvn()), "value type field should be allocated on return"); 719 arg = vt->allocate(kit)->get_oop(); 720 } 721 // Initialize call/return arguments 722 BasicType bt = field_type(i)->basic_type(); 723 n->init_req(base_input++, arg); 724 if (type2size[bt] == 2) { 725 n->init_req(base_input++, kit->top()); 726 } 727 // Skip reserved arguments 728 while (SigEntry::next_is_reserved(sig, bt)) { 729 n->init_req(base_input++, kit->top()); 730 if (type2size[bt] == 2) { 731 n->init_req(base_input++, kit->top()); 732 } 733 } 734 } 735 } 736 } 737 738 void ValueTypeNode::initialize_fields(GraphKit* kit, MultiNode* multi, ExtendedSignature& sig, uint& base_input, int base_offset, bool in) { 739 PhaseGVN& gvn = kit->gvn(); 740 for (uint i = 0; i < field_count(); i++) { 741 int sig_offset = (*sig)._offset; 742 uint idx = field_index(sig_offset - base_offset); 743 ciType* type = field_type(idx); 744 745 Node* parm = NULL; 746 if (field_is_flattened(idx)) { 747 // Flattened value type field 748 ValueTypeNode* vt = ValueTypeNode::make_uninitialized(gvn, type->as_value_klass()); 749 vt->initialize_fields(kit, multi, sig, base_input, sig_offset - type->as_value_klass()->first_field_offset(), in); 750 parm = gvn.transform(vt); 751 } else { 752 if (multi->is_Start()) { 753 assert(in, "return from start?"); 754 parm = gvn.transform(new ParmNode(multi->as_Start(), base_input)); 755 } else if (in) { 756 parm = multi->as_Call()->in(base_input); 757 } else { 758 parm = gvn.transform(new ProjNode(multi->as_Call(), base_input)); 759 } 760 if (field_is_flattenable(idx)) { 761 // Non-flattened but flattenable value type 762 if (type->as_value_klass()->is_scalarizable()) { 763 parm = ValueTypeNode::make_from_oop(kit, parm, type->as_value_klass()); 764 } else { 765 parm = kit->null2default(parm, type->as_value_klass()); 766 } 767 } 768 base_input += type2size[type->basic_type()]; 769 // Skip reserved arguments 770 BasicType bt = type->basic_type(); 771 while (SigEntry::next_is_reserved(sig, bt)) { 772 base_input += type2size[bt]; 773 } 774 } 775 assert(parm != NULL, "should never be null"); 776 set_field_value(idx, parm); 777 gvn.record_for_igvn(parm); 778 } 779 } 780 781 Node* ValueTypeNode::Ideal(PhaseGVN* phase, bool can_reshape) { 782 Node* oop = get_oop(); 783 if (is_default(*phase) && (!oop->is_Con() || phase->type(oop)->is_zero_type())) { 784 // Use the pre-allocated oop for default value types 785 set_oop(default_oop(*phase, value_klass())); 786 return this; 787 } else if (oop->isa_ValueTypePtr()) { 788 // Can happen with late inlining 789 ValueTypePtrNode* vtptr = oop->as_ValueTypePtr(); 790 set_oop(vtptr->get_oop()); 791 for (uint i = Oop+1; i < vtptr->req(); ++i) { 792 set_req(i, vtptr->in(i)); 793 } 794 return this; 795 } 796 797 if (!is_allocated(phase)) { 798 // Save base oop if fields are loaded from memory and the value 799 // type is not buffered (in this case we should not use the oop). 800 Node* base = is_loaded(phase); 801 if (base != NULL) { 802 set_oop(base); 803 assert(is_allocated(phase), "should now be allocated"); 804 return this; 805 } 806 } 807 808 if (can_reshape) { 809 PhaseIterGVN* igvn = phase->is_IterGVN(); 810 811 if (is_default(*phase)) { 812 // Search for users of the default value type 813 for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) { 814 Node* user = fast_out(i); 815 AllocateNode* alloc = user->isa_Allocate(); 816 if (alloc != NULL && alloc->result_cast() != NULL && alloc->in(AllocateNode::ValueNode) == this) { 817 // Found an allocation of the default value type. 818 // If the code in StoreNode::Identity() that removes useless stores was not yet 819 // executed or ReduceFieldZeroing is disabled, there can still be initializing 820 // stores (only zero-type or default value stores, because value types are immutable). 821 Node* res = alloc->result_cast(); 822 for (DUIterator_Fast jmax, j = res->fast_outs(jmax); j < jmax; j++) { 823 AddPNode* addp = res->fast_out(j)->isa_AddP(); 824 if (addp != NULL) { 825 for (DUIterator_Fast kmax, k = addp->fast_outs(kmax); k < kmax; k++) { 826 StoreNode* store = addp->fast_out(k)->isa_Store(); 827 if (store != NULL && store->outcnt() != 0) { 828 // Remove the useless store 829 igvn->replace_in_uses(store, store->in(MemNode::Memory)); 830 } 831 } 832 } 833 } 834 // Replace allocation by pre-allocated oop 835 igvn->replace_node(res, default_oop(*phase, value_klass())); 836 } else if (user->is_ValueType()) { 837 // Add value type user to worklist to give it a chance to get optimized as well 838 igvn->_worklist.push(user); 839 } 840 } 841 } 842 843 if (is_allocated(igvn)) { 844 // Value type is heap allocated, search for safepoint uses 845 for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) { 846 Node* out = fast_out(i); 847 if (out->is_SafePoint()) { 848 // Let SafePointNode::Ideal() take care of re-wiring the 849 // safepoint to the oop input instead of the value type node. 850 igvn->rehash_node_delayed(out); 851 } 852 } 853 } 854 } 855 return NULL; 856 } 857 858 // Search for multiple allocations of this value type 859 // and try to replace them by dominating allocations. 860 // Then unlink the value type node and remove it. 861 void ValueTypeNode::remove_redundant_allocations(PhaseIterGVN* igvn, PhaseIdealLoop* phase) { 862 // Search for allocations of this value type 863 for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) { 864 AllocateNode* alloc = fast_out(i)->isa_Allocate(); 865 if (alloc != NULL && alloc->in(AllocateNode::ValueNode) == this) { 866 assert(!is_default(*igvn), "default value type allocation"); 867 Node* res = alloc->result_cast(); 868 if (res == NULL || !res->is_CheckCastPP()) { 869 break; // No unique CheckCastPP 870 } 871 Node* res_dom = res; 872 if (is_allocated(igvn)) { 873 // The value type is already allocated but still connected to an AllocateNode. 874 // This can happen with late inlining when we first allocate a value type argument 875 // but later decide to inline the call with the callee code also allocating. 876 res_dom = get_oop(); 877 } else { 878 // Search for a dominating allocation of the same value type 879 for (DUIterator_Fast jmax, j = fast_outs(jmax); j < jmax; j++) { 880 AllocateNode* alloc_other = fast_out(j)->isa_Allocate(); 881 if (alloc_other != NULL && alloc_other->in(AllocateNode::ValueNode) == this) { 882 Node* res_other = alloc_other->result_cast(); 883 if (res_other != NULL && res_other->is_CheckCastPP() && res_other != res_dom && 884 phase->is_dominator(res_other->in(0), res_dom->in(0))) { 885 res_dom = res_other; 886 } 887 } 888 } 889 } 890 if (res_dom != res) { 891 // Move users to dominating allocation 892 igvn->replace_node(res, res_dom); 893 // The result of the dominated allocation is now unused and will be 894 // removed later in AllocateNode::Ideal() to not confuse loop opts. 895 igvn->record_for_igvn(alloc); 896 #ifdef ASSERT 897 if (PrintEliminateAllocations) { 898 tty->print("++++ Eliminated: %d Allocate ", alloc->_idx); 899 dump_spec(tty); 900 tty->cr(); 901 } 902 #endif 903 } 904 } 905 } 906 907 // Process users 908 for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) { 909 Node* out = fast_out(i); 910 if (out->is_ValueType()) { 911 // Recursively process value type users 912 out->as_ValueType()->remove_redundant_allocations(igvn, phase); 913 --i; --imax; 914 } else if (out->isa_Allocate() != NULL) { 915 // Unlink AllocateNode 916 assert(out->in(AllocateNode::ValueNode) == this, "should be linked"); 917 igvn->replace_input_of(out, AllocateNode::ValueNode, igvn->C->top()); 918 --i; --imax; 919 } else { 920 #ifdef ASSERT 921 // The value type should not have any other users at this time 922 out->dump(); 923 assert(false, "unexpected user of value type"); 924 #endif 925 } 926 } 927 igvn->remove_dead_node(this); 928 } 929 930 ValueTypePtrNode* ValueTypePtrNode::make_from_value_type(GraphKit* kit, ValueTypeNode* vt) { 931 Node* oop = vt->allocate(kit)->get_oop(); 932 ValueTypePtrNode* vtptr = new ValueTypePtrNode(vt->value_klass(), oop); 933 for (uint i = Oop+1; i < vt->req(); i++) { 934 vtptr->init_req(i, vt->in(i)); 935 } 936 return kit->gvn().transform(vtptr)->as_ValueTypePtr(); 937 } 938 939 ValueTypePtrNode* ValueTypePtrNode::make_from_oop(GraphKit* kit, Node* oop) { 940 // Create and initialize a ValueTypePtrNode by loading all field 941 // values from a heap-allocated version and also save the oop. 942 ciValueKlass* vk = kit->gvn().type(oop)->value_klass(); 943 ValueTypePtrNode* vtptr = new ValueTypePtrNode(vk, oop); 944 vtptr->load(kit, oop, oop, vk); 945 return kit->gvn().transform(vtptr)->as_ValueTypePtr(); 946 }