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