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