1 /* 2 * Copyright (c) 2016, 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 "ci/ciValueKlass.hpp" 26 #include "opto/addnode.hpp" 27 #include "opto/graphKit.hpp" 28 #include "opto/rootnode.hpp" 29 #include "opto/valuetypenode.hpp" 30 #include "opto/phaseX.hpp" 31 32 ValueTypeNode* ValueTypeNode::make(PhaseGVN& gvn, ciValueKlass* klass) { 33 // Create a new ValueTypeNode with uninitialized values and NULL oop 34 const TypeValueType* type = TypeValueType::make(klass); 35 return new ValueTypeNode(type, gvn.zerocon(T_VALUETYPE)); 36 } 37 38 Node* ValueTypeNode::make_default(PhaseGVN& gvn, ciValueKlass* vk) { 39 // TODO re-use constant oop of pre-allocated default value type here? 40 // Create a new ValueTypeNode with default values 41 ValueTypeNode* vt = ValueTypeNode::make(gvn, vk); 42 for (uint i = 0; i < vt->field_count(); ++i) { 43 ciType* field_type = vt->field_type(i); 44 Node* value = NULL; 45 if (field_type->is_primitive_type()) { 46 value = gvn.zerocon(field_type->basic_type()); 47 } else { 48 value = ValueTypeNode::make_default(gvn, field_type->as_value_klass()); 49 } 50 vt->set_field_value(i, value); 51 } 52 return gvn.transform(vt); 53 } 54 55 Node* ValueTypeNode::make(PhaseGVN& gvn, Node* mem, Node* oop) { 56 // Create and initialize a ValueTypeNode by loading all field 57 // values from a heap-allocated version and also save the oop. 58 const TypeValueType* type = gvn.type(oop)->is_valuetypeptr()->value_type(); 59 ValueTypeNode* vt = new ValueTypeNode(type, oop); 60 vt->load_values(gvn, mem, oop, oop, type->value_klass()); 61 return gvn.transform(vt); 62 } 63 64 Node* ValueTypeNode::make(PhaseGVN& gvn, ciValueKlass* vk, Node* mem, Node* obj, Node* ptr, ciInstanceKlass* holder, int holder_offset) { 65 // Create and initialize a ValueTypeNode by loading all field values from 66 // a flattened value type field at 'holder_offset' or from a value type array. 67 ValueTypeNode* vt = make(gvn, vk); 68 // The value type is flattened into the object without an oop header. Subtract the 69 // offset of the first field to account for the missing header when loading the values. 70 holder_offset -= vk->first_field_offset(); 71 vt->load_values(gvn, mem, obj, ptr, holder, holder_offset); 72 return gvn.transform(vt); 73 } 74 75 void ValueTypeNode::load_values(PhaseGVN& gvn, Node* mem, Node* base, Node* ptr, ciInstanceKlass* holder, int holder_offset) { 76 // Initialize the value type by loading its field values from 77 // memory and adding the values as input edges to the node. 78 for (uint i = 0; i < field_count(); ++i) { 79 int offset = holder_offset + field_offset(i); 80 ciType* ftype = field_type(i); 81 Node* value = NULL; 82 if (ftype->is_valuetype()) { 83 // Recursively load the flattened value type field 84 value = ValueTypeNode::make(gvn, ftype->as_value_klass(), mem, base, ptr, holder, offset); 85 } else { 86 const Type* con_type = NULL; 87 if (base->is_Con()) { 88 // If the oop to the value type is constant (static final field), we can 89 // also treat the fields as constants because the value type is immutable. 90 const TypeOopPtr* oop_ptr = base->bottom_type()->isa_oopptr(); 91 ciObject* constant_oop = oop_ptr->const_oop(); 92 ciField* field = holder->get_field_by_offset(offset, false); 93 ciConstant constant = constant_oop->as_instance()->field_value(field); 94 con_type = Type::make_from_constant(constant, /*require_const=*/ true); 95 } 96 if (con_type != NULL) { 97 // Found a constant field value 98 value = gvn.makecon(con_type); 99 } else { 100 // Load field value from memory 101 const Type* base_type = gvn.type(base); 102 const TypePtr* adr_type = NULL; 103 if (base_type->isa_aryptr()) { 104 // In the case of a flattened value type array, each field 105 // has its own slice 106 adr_type = base_type->is_aryptr()->with_field_offset(offset)->add_offset(Type::OffsetBot); 107 } else { 108 ciField* field = holder->get_field_by_offset(offset, false); 109 adr_type = gvn.C->alias_type(field)->adr_type(); 110 } 111 Node* adr = gvn.transform(new AddPNode(base, ptr, gvn.MakeConX(offset))); 112 value = LoadNode::make(gvn, NULL, mem, adr, adr_type, Type::get_const_type(ftype), ftype->basic_type(), MemNode::unordered); 113 } 114 } 115 set_field_value(i, gvn.transform(value)); 116 } 117 } 118 119 void ValueTypeNode::store(GraphKit* kit, Node* obj, Node* ptr, ciInstanceKlass* holder, int holder_offset) const { 120 // The value type is embedded into the object without an oop header. Subtract the 121 // offset of the first field to account for the missing header when storing the values. 122 holder_offset -= value_klass()->first_field_offset(); 123 store_values(kit, obj, ptr, holder, holder_offset); 124 } 125 126 void ValueTypeNode::store_values(GraphKit* kit, Node* base, Node* ptr, ciInstanceKlass* holder, int holder_offset) const { 127 // Write field values to memory 128 for (uint i = 0; i < field_count(); ++i) { 129 int offset = holder_offset + field_offset(i); 130 Node* value = field_value(i); 131 if (value->is_ValueType()) { 132 // Recursively store the flattened value type field 133 value->isa_ValueType()->store(kit, base, ptr, value_klass(), offset); 134 } else { 135 const Type* base_type = kit->gvn().type(base); 136 const TypePtr* adr_type = NULL; 137 if (base_type->isa_aryptr()) { 138 // In the case of a flattened value type array, each field has its own slice 139 adr_type = base_type->is_aryptr()->with_field_offset(offset)->add_offset(Type::OffsetBot); 140 } else { 141 ciField* field = holder->get_field_by_offset(offset, false); 142 adr_type = kit->C->alias_type(field)->adr_type(); 143 } 144 Node* adr = kit->basic_plus_adr(base, ptr, offset); 145 kit->store_to_memory(kit->control(), adr, value, field_type(i)->basic_type(), adr_type, MemNode::unordered); 146 } 147 } 148 } 149 150 Node* ValueTypeNode::store_to_memory(GraphKit* kit) { 151 Node* in_oop = get_oop(); 152 Node* null_ctl = kit->top(); 153 // Check if value type is already allocated 154 Node* not_null_oop = kit->null_check_oop(in_oop, &null_ctl); 155 if (null_ctl->is_top()) { 156 // Value type is allocated 157 return not_null_oop; 158 } 159 // Not able to prove that value type is allocated. 160 // Emit runtime check that may be folded later. 161 const Type* oop_type = kit->gvn().type(in_oop); 162 assert(TypePtr::NULL_PTR->higher_equal(oop_type), "should not be allocated"); 163 164 const TypeValueTypePtr* vtptr_type = TypeValueTypePtr::make(bottom_type()->isa_valuetype(), TypePtr::NotNull); 165 RegionNode* region = new RegionNode(3); 166 PhiNode* oop = new PhiNode(region, vtptr_type); 167 PhiNode* io = new PhiNode(region, Type::ABIO); 168 PhiNode* mem = new PhiNode(region, Type::MEMORY, TypePtr::BOTTOM); 169 170 // Oop is non-NULL, use it 171 region->init_req(1, kit->control()); 172 // Fixme if we cast oop to not null we fail if the control path is not folded 173 // castnode.cpp:69: # assert(ft == Type::TOP) failed: special case #3 174 //oop ->init_req(1, not_null_oop); 175 oop ->init_req(1, in_oop); 176 io ->init_req(1, kit->i_o()); 177 mem ->init_req(1, kit->merged_memory()); 178 179 // Oop is NULL, allocate value type 180 kit->set_control(null_ctl); 181 kit->kill_dead_locals(); 182 ciValueKlass* vk = value_klass(); 183 Node* klass_node = kit->makecon(TypeKlassPtr::make(vk)); 184 Node* alloc_oop = kit->new_instance(klass_node); 185 AllocateNode* alloc = AllocateNode::Ideal_allocation(alloc_oop, &kit->gvn()); 186 // TODO enable/fix this 187 // alloc->initialization()->set_complete_with_arraycopy(); 188 // Write field values to memory 189 store_values(kit, alloc_oop, alloc_oop, vk); 190 region->init_req(2, kit->control()); 191 oop ->init_req(2, alloc_oop); 192 io ->init_req(2, kit->i_o()); 193 mem ->init_req(2, kit->merged_memory()); 194 195 // Update GraphKit 196 kit->set_control(kit->gvn().transform(region)); 197 kit->set_i_o(kit->gvn().transform(io)); 198 kit->set_all_memory(kit->gvn().transform(mem)); 199 kit->record_for_igvn(region); 200 kit->record_for_igvn(oop); 201 kit->record_for_igvn(io); 202 kit->record_for_igvn(mem); 203 204 // Use cloned ValueTypeNode to propagate oop from now on 205 Node* res_oop = kit->gvn().transform(oop); 206 ValueTypeNode* vt = clone()->as_ValueType(); 207 vt->set_oop(res_oop); 208 kit->replace_in_map(this, kit->gvn().transform(vt)); 209 return res_oop; 210 } 211 212 // Clones the values type to handle control flow merges involving multiple value types. 213 // The inputs are replaced by PhiNodes to represent the merged values for the given region. 214 ValueTypeNode* ValueTypeNode::clone_with_phis(PhaseGVN& gvn, Node* region) { 215 ValueTypeNode* vt = clone()->as_ValueType(); 216 217 // Create a PhiNode for merging the oop values 218 const TypeValueTypePtr* vtptr = TypeValueTypePtr::make(vt->bottom_type()->isa_valuetype()); 219 PhiNode* oop = PhiNode::make(region, vt->get_oop(), vtptr); 220 gvn.set_type(oop, vtptr); 221 vt->set_oop(oop); 222 223 // Create a PhiNode each for merging the field values 224 for (uint i = 0; i < vt->field_count(); ++i) { 225 ciType* type = vt->field_type(i); 226 Node* value = vt->field_value(i); 227 if (type->is_valuetype()) { 228 // Handle flattened value type fields recursively 229 value = value->as_ValueType()->clone_with_phis(gvn, region); 230 } else { 231 const Type* phi_type = Type::get_const_type(type); 232 value = PhiNode::make(region, value, phi_type); 233 gvn.set_type(value, phi_type); 234 } 235 vt->set_field_value(i, value); 236 } 237 gvn.set_type(vt, vt->bottom_type()); 238 return vt; 239 } 240 241 // Checks if the inputs of the ValueTypeNode were replaced by PhiNodes 242 // for the given region (see ValueTypeNode::clone_with_phis). 243 bool ValueTypeNode::has_phi_inputs(Node* region) { 244 // Check oop input 245 bool result = get_oop()->is_Phi() && get_oop()->as_Phi()->region() == region; 246 #ifdef ASSERT 247 if (result) { 248 // Check all field value inputs for consistency 249 for (uint i = 0; i < field_count(); ++i) { 250 Node* value = field_value(i); 251 if (value->is_ValueType()) { 252 assert(value->as_ValueType()->has_phi_inputs(region), "inconsistent phi inputs"); 253 } else { 254 assert(value->is_Phi() && value->as_Phi()->region() == region, "inconsistent phi inputs"); 255 } 256 } 257 } 258 #endif 259 return result; 260 } 261 262 // Merges 'this' with 'other' by updating the input PhiNodes added by 'clone_with_phis' 263 Node* ValueTypeNode::merge_with(GraphKit* kit, const ValueTypeNode* other, int pnum) { 264 // Merge oop inputs 265 PhiNode* phi = get_oop()->as_Phi(); 266 phi->set_req(pnum, other->get_oop()); 267 if (pnum == PhiNode::Input) { 268 // Last merge 269 set_oop(kit->gvn().transform_no_reclaim(phi)); 270 kit->record_for_igvn(phi); 271 } 272 // Merge field values 273 for (uint i = 0; i < field_count(); ++i) { 274 Node* val1 = field_value(i); 275 Node* val2 = other->field_value(i); 276 if (val1->isa_ValueType()) { 277 val1->as_ValueType()->merge_with(kit, val2->as_ValueType(), pnum); 278 } else { 279 assert(!val2->is_ValueType(), "inconsistent merge values"); 280 val1->set_req(pnum, val2); 281 } 282 if (pnum == PhiNode::Input) { 283 // Last merge 284 set_field_value(i, kit->gvn().transform_no_reclaim(val1)); 285 kit->record_for_igvn(val1); 286 } 287 } 288 if (pnum == PhiNode::Input) { 289 // Last merge for this value type. 290 kit->record_for_igvn(this); 291 return kit->gvn().transform_no_reclaim(this); 292 } 293 return this; 294 } 295 296 Node* ValueTypeNode::field_value(uint index) const { 297 assert(index < field_count(), "index out of bounds"); 298 return in(Values + index); 299 } 300 301 // Get the value of the field at the given offset. 302 // If 'recursive' is true, flattened value type fields will be resolved recursively. 303 Node* ValueTypeNode::field_value_by_offset(int offset, bool recursive) const { 304 // If the field at 'offset' belongs to a flattened value type field, 'index' refers to the 305 // corresponding ValueTypeNode input and 'sub_offset' is the offset in flattened value type. 306 int index = value_klass()->field_index_by_offset(offset); 307 int sub_offset = offset - field_offset(index); 308 Node* value = field_value(index); 309 if (recursive && value->is_ValueType()) { 310 // Flattened value type field 311 ValueTypeNode* vt = value->as_ValueType(); 312 sub_offset += vt->value_klass()->first_field_offset(); // Add header size 313 return vt->field_value_by_offset(sub_offset); 314 } 315 assert(!(recursive && value->is_ValueType()), "should not be a value type"); 316 assert(sub_offset == 0, "offset mismatch"); 317 return value; 318 } 319 320 void ValueTypeNode::set_field_value(uint index, Node* value) { 321 assert(index < field_count(), "index out of bounds"); 322 set_req(Values + index, value); 323 } 324 325 int ValueTypeNode::field_offset(uint index) const { 326 assert(index < field_count(), "index out of bounds"); 327 return value_klass()->field_offset_by_index(index); 328 } 329 330 ciType* ValueTypeNode::field_type(uint index) const { 331 assert(index < field_count(), "index out of bounds"); 332 return value_klass()->field_type_by_index(index); 333 } 334 335 void ValueTypeNode::make_scalar_in_safepoints(Compile* C) { 336 const TypeValueTypePtr* res_type = TypeValueTypePtr::make(bottom_type()->isa_valuetype(), TypePtr::NotNull); 337 ciValueKlass* vk = value_klass(); 338 uint nfields = vk->flattened_field_count(); 339 for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) { 340 Node* u = fast_out(i); 341 if (u->is_SafePoint() && (!u->is_Call() || u->as_Call()->has_debug_use(this))) { 342 Node* in_oop = get_oop(); 343 const Type* oop_type = in_oop->bottom_type(); 344 SafePointNode* sfpt = u->as_SafePoint(); 345 JVMState* jvms = sfpt->jvms(); 346 int start = jvms->debug_start(); 347 int end = jvms->debug_end(); 348 if (oop_type->meet(TypePtr::NULL_PTR) != oop_type) { 349 // Replace safepoint edge by oop 350 int nb = sfpt->replace_edges_in_range(this, in_oop, start, end); 351 --i; imax -= nb; 352 } else { 353 // Replace safepoint edge by SafePointScalarObjectNode and add field values 354 assert(jvms != NULL, "missing JVMS"); 355 uint first_ind = (sfpt->req() - jvms->scloff()); 356 SafePointScalarObjectNode* sobj = new SafePointScalarObjectNode(res_type, 357 #ifdef ASSERT 358 NULL, 359 #endif 360 first_ind, nfields); 361 sobj->init_req(0, C->root()); 362 // Iterate over the value type fields in order of increasing 363 // offset and add the field values to the safepoint. 364 for (uint j = 0; j < nfields; ++j) { 365 int offset = vk->nonstatic_field_at(j)->offset(); 366 Node* value = field_value_by_offset(offset, true /* include flattened value type fields */); 367 sfpt->add_req(value); 368 } 369 jvms->set_endoff(sfpt->req()); 370 int nb = sfpt->replace_edges_in_range(this, sobj, start, end); 371 --i; imax -= nb; 372 } 373 } 374 } 375 } 376 377 uint ValueTypeNode::set_arguments_for_java_call(CallJavaNode* call, int base_input, const GraphKit& kit, ciValueKlass* base_vk, int base_offset) { 378 ciValueKlass* vk = value_klass(); 379 if (base_vk == NULL) { 380 base_vk = vk; 381 } 382 uint edges = 0; 383 for (uint i = 0; i < field_count(); i++) { 384 ciType* f_type = field_type(i); 385 int offset = base_offset + field_offset(i) - (base_offset > 0 ? vk->first_field_offset() : 0); 386 Node* arg = field_value(i); 387 if (f_type->is_valuetype()) { 388 ciValueKlass* embedded_vk = f_type->as_value_klass(); 389 edges += arg->as_ValueType()->set_arguments_for_java_call(call, base_input, kit, base_vk, offset); 390 } else { 391 int j = 0; int extra = 0; 392 for (; j < base_vk->nof_nonstatic_fields(); j++) { 393 ciField* f = base_vk->nonstatic_field_at(j); 394 if (offset == f->offset()) { 395 assert(f->type() == f_type, "inconsistent field type"); 396 break; 397 } 398 BasicType bt = f->type()->basic_type(); 399 if (bt == T_LONG || bt == T_DOUBLE) { 400 extra++; 401 } 402 } 403 call->init_req(base_input + j + extra, arg); 404 edges++; 405 BasicType bt = f_type->basic_type(); 406 if (bt == T_LONG || bt == T_DOUBLE) { 407 call->init_req(base_input + j + extra + 1, kit.top()); 408 edges++; 409 } 410 } 411 } 412 return edges; 413 } 414 415 Node* ValueTypeNode::Ideal(PhaseGVN* phase, bool can_reshape) { 416 // No optimizations for now 417 return NULL; 418 } 419 420 #ifndef PRODUCT 421 422 void ValueTypeNode::dump_spec(outputStream* st) const { 423 TypeNode::dump_spec(st); 424 } 425 426 #endif