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