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