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