/* * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ #include "ci/ciValueKlass.hpp" #include "opto/addnode.hpp" #include "opto/graphKit.hpp" #include "opto/rootnode.hpp" #include "opto/valuetypenode.hpp" #include "opto/phaseX.hpp" ValueTypeNode* ValueTypeNode::make(PhaseGVN& gvn, ciValueKlass* klass) { // Create a new ValueTypeNode with uninitialized values and NULL oop const TypeValueType* type = TypeValueType::make(klass); return new ValueTypeNode(type, gvn.zerocon(T_VALUETYPE)); } Node* ValueTypeNode::make_default(PhaseGVN& gvn, ciValueKlass* vk) { // TODO re-use constant oop of pre-allocated default value type here? // Create a new ValueTypeNode with default values ValueTypeNode* vt = ValueTypeNode::make(gvn, vk); for (uint i = 0; i < vt->field_count(); ++i) { ciType* field_type = vt->field_type(i); Node* value = NULL; if (field_type->is_primitive_type()) { value = gvn.zerocon(field_type->basic_type()); } else { value = ValueTypeNode::make_default(gvn, field_type->as_value_klass()); } vt->set_field_value(i, value); } return gvn.transform(vt); } Node* ValueTypeNode::make(PhaseGVN& gvn, Node* mem, Node* oop) { // Create and initialize a ValueTypeNode by loading all field // values from a heap-allocated version and also save the oop. const TypeValueTypePtr* vtptr = gvn.type(oop)->is_valuetypeptr(); ValueTypeNode* vt = new ValueTypeNode(vtptr->value_type(), oop); vt->load_values(gvn, mem, oop, oop); return gvn.transform(vt); } Node* ValueTypeNode::make(PhaseGVN& gvn, ciValueKlass* vk, Node* mem, Node* obj, Node* ptr, ciKlass* holder, int field_offset) { // Create and initialize a ValueTypeNode by loading all field values from // a flattened value type field at 'field_offset' or from a value type array. ValueTypeNode* vt = make(gvn, vk); int base_offset = 0; if (holder->is_value_array_klass()) { assert(field_offset == 0, "field offset not supported for arrays"); } else { // The value type is flattened into the object without an oop header. Subtract the // offset of the first field to account for the missing header when loading the values. base_offset = field_offset - vk->first_field_offset(); } vt->load_values(gvn, mem, obj, ptr, holder, base_offset); return gvn.transform(vt); } void ValueTypeNode::load_values(PhaseGVN& gvn, Node* mem, Node* base, Node* ptr, ciKlass* holder, int f_offset) { ciInstanceKlass* lookup; if (holder) { // Flattened if (holder->is_value_array_klass()) { lookup = value_klass(); } else { lookup = holder->as_instance_klass(); } } else { // Not flattened assert(f_offset == 0, "must be"); lookup = value_klass(); } // Initialize the value type by loading its field values from // memory and adding the values as input edges to the node. for (uint i = 0; i < field_count(); ++i) { int offset = f_offset + field_offset(i); ciField* field = lookup->get_field_by_offset(offset, false); ciType* f_type = field_type(i); Node* value = NULL; if (f_type->is_valuetype()) { if (holder && holder->is_value_array_klass()) { offset -= value_klass()->first_field_offset(); } // Recursively load the flattened value type field value = ValueTypeNode::make(gvn, f_type->as_value_klass(), mem, base, ptr, lookup, offset); } else { const Type* con_type = NULL; if (base->is_Con()) { // If the oop to the value type is constant (static final field), we can // also treat the fields as constants because the value type is immutable. const TypeOopPtr* oop_ptr = base->bottom_type()->isa_oopptr(); ciObject* constant_oop = oop_ptr->const_oop(); ciConstant constant = constant_oop->as_instance()->field_value(field); con_type = Type::make_from_constant(constant, /*require_const=*/ true); } if (con_type != NULL) { // Found a constant field value value = gvn.makecon(con_type); } else { // Load field value from memory const Type* base_type = gvn.type(base); const TypePtr* adr_type = NULL; if (base_type->isa_aryptr()) { adr_type = base_type->is_aryptr()->add_offset(Type::OffsetBot); } else { adr_type = gvn.C->alias_type(field)->adr_type(); } if (holder && holder->is_value_array_klass()) { offset -= value_klass()->first_field_offset(); } Node* adr = gvn.transform(new AddPNode(base, ptr, gvn.MakeConX(offset))); value = LoadNode::make(gvn, NULL, mem, adr, adr_type, Type::get_const_type(f_type), f_type->basic_type(), MemNode::unordered); } } set_field_value(i, gvn.transform(value)); } } void ValueTypeNode::store_to_field(GraphKit* kit, Node* obj, Node* ptr, ciInstanceKlass* instance_type, int field_offset) const { // The value type is embedded into the object without an oop header. Subtract the // offset of the first field to account for the missing header when storing the values. int base_offset = field_offset - value_klass()->first_field_offset(); store_values(kit, obj, ptr, instance_type, base_offset); } void ValueTypeNode::store_values(GraphKit* kit, Node* base, Node* ptr, ciKlass* holder, int holder_offset) const { ciInstanceKlass* lookup; if (holder) { // flattened if (holder->is_value_array_klass()) { assert(holder_offset == 0, "must be"); lookup = value_klass(); } else { lookup = holder->as_instance_klass(); } } else { // not flattened assert(holder_offset == 0, "must be"); lookup = value_klass(); } // Write field values to memory for (uint i = 0; i < field_count(); ++i) { int offset = holder_offset + field_offset(i); Node* value = field_value(i); if (value->is_ValueType()) { // Recursively store the flattened value type field if (holder && holder->is_value_array_klass()) { offset -= value_klass()->first_field_offset(); } value->isa_ValueType()->store_to_field(kit, base, ptr, lookup, offset); } else { const Type* base_type = kit->gvn().type(base); const TypePtr* adr_type = NULL; if (base_type->isa_aryptr()) { adr_type = base_type->is_aryptr()->add_offset(Type::OffsetBot); } else { ciField* field = lookup->get_field_by_offset(offset, false); adr_type = kit->gvn().C->alias_type(field)->adr_type(); } if (holder && holder->is_value_array_klass()) { offset -= value_klass()->first_field_offset(); } Node* adr = kit->basic_plus_adr(base, ptr, offset); kit->store_to_memory(kit->control(), adr, value, field_type(i)->basic_type(), adr_type, MemNode::unordered); } } } Node* ValueTypeNode::store_to_memory(GraphKit* kit) { Node* in_oop = get_oop(); Node* null_ctl = kit->top(); // Check if value type is already allocated Node* not_null_oop = kit->null_check_oop(in_oop, &null_ctl); if (null_ctl->is_top()) { // Value type is allocated return not_null_oop; } // Not able to prove that value type is allocated. // Emit runtime check that may be folded later. const Type* oop_type = kit->gvn().type(in_oop); assert(TypePtr::NULL_PTR->higher_equal(oop_type), "should not be allocated"); const TypeValueTypePtr* vtptr_type = TypeValueTypePtr::make(bottom_type()->isa_valuetype(), TypePtr::NotNull); RegionNode* region = new RegionNode(3); PhiNode* oop = new PhiNode(region, vtptr_type); PhiNode* io = new PhiNode(region, Type::ABIO); PhiNode* mem = new PhiNode(region, Type::MEMORY, TypePtr::BOTTOM); // Oop is non-NULL, use it region->init_req(1, kit->control()); // Fixme if we cast oop to not null we fail if the control path is not folded // castnode.cpp:69: # assert(ft == Type::TOP) failed: special case #3 //oop ->init_req(1, not_null_oop); oop ->init_req(1, in_oop); io ->init_req(1, kit->i_o()); mem ->init_req(1, kit->merged_memory()); // Oop is NULL, allocate value type kit->set_control(null_ctl); kit->kill_dead_locals(); Node* klass_node = kit->makecon(TypeKlassPtr::make(value_klass())); Node* alloc_oop = kit->new_instance(klass_node); AllocateNode* alloc = AllocateNode::Ideal_allocation(alloc_oop, &kit->gvn()); // TODO enable/fix this // alloc->initialization()->set_complete_with_arraycopy(); // Write field values to memory store_values(kit, alloc_oop, alloc_oop); region->init_req(2, kit->control()); oop ->init_req(2, alloc_oop); io ->init_req(2, kit->i_o()); mem ->init_req(2, kit->merged_memory()); // Update GraphKit kit->set_control(kit->gvn().transform(region)); kit->set_i_o(kit->gvn().transform(io)); kit->set_all_memory(kit->gvn().transform(mem)); kit->record_for_igvn(region); kit->record_for_igvn(oop); kit->record_for_igvn(io); kit->record_for_igvn(mem); // Use cloned ValueTypeNode to propagate oop from now on Node* res_oop = kit->gvn().transform(oop); ValueTypeNode* vt = clone()->as_ValueType(); vt->set_oop(res_oop); kit->replace_in_map(this, kit->gvn().transform(vt)); return res_oop; } // Clones the values type to handle control flow merges involving multiple value types. // The inputs are replaced by PhiNodes to represent the merged values for the given region. ValueTypeNode* ValueTypeNode::clone_with_phis(PhaseGVN& gvn, Node* region) { ValueTypeNode* vt = clone()->as_ValueType(); // Create a PhiNode for merging the oop values const TypeValueTypePtr* vtptr = TypeValueTypePtr::make(vt->bottom_type()->isa_valuetype()); PhiNode* oop = PhiNode::make(region, vt->get_oop(), vtptr); gvn.set_type(oop, vtptr); vt->set_oop(oop); // Create a PhiNode each for merging the field values for (uint i = 0; i < vt->field_count(); ++i) { ciType* type = vt->field_type(i); Node* value = vt->field_value(i); if (type->is_valuetype()) { // Handle flattened value type fields recursively value = value->as_ValueType()->clone_with_phis(gvn, region); } else { const Type* phi_type = Type::get_const_type(type); value = PhiNode::make(region, value, phi_type); gvn.set_type(value, phi_type); } vt->set_field_value(i, value); } gvn.set_type(vt, vt->bottom_type()); return vt; } // Checks if the inputs of the ValueTypeNode were replaced by PhiNodes // for the given region (see ValueTypeNode::clone_with_phis). bool ValueTypeNode::has_phi_inputs(Node* region) { // Check oop input bool result = get_oop()->is_Phi() && get_oop()->as_Phi()->region() == region; #ifdef ASSERT if (result) { // Check all field value inputs for consistency for (uint i = 0; i < field_count(); ++i) { Node* value = field_value(i); if (value->is_ValueType()) { assert(value->as_ValueType()->has_phi_inputs(region), "inconsistent phi inputs"); } else { assert(value->is_Phi() && value->as_Phi()->region() == region, "inconsistent phi inputs"); } } } #endif return result; } // Merges 'this' with 'other' by updating the input PhiNodes added by 'clone_with_phis' Node* ValueTypeNode::merge_with(GraphKit* kit, const ValueTypeNode* other, int pnum) { // Merge oop inputs PhiNode* phi = get_oop()->as_Phi(); phi->set_req(pnum, other->get_oop()); if (pnum == PhiNode::Input) { // Last merge set_oop(kit->gvn().transform_no_reclaim(phi)); kit->record_for_igvn(phi); } // Merge field values for (uint i = 0; i < field_count(); ++i) { Node* val1 = field_value(i); Node* val2 = other->field_value(i); if (val1->isa_ValueType()) { val1->as_ValueType()->merge_with(kit, val2->as_ValueType(), pnum); } else { assert(!val2->is_ValueType(), "inconsistent merge values"); val1->set_req(pnum, val2); } if (pnum == PhiNode::Input) { // Last merge set_field_value(i, kit->gvn().transform_no_reclaim(val1)); kit->record_for_igvn(val1); } } if (pnum == PhiNode::Input) { // Last merge for this value type. kit->record_for_igvn(this); return kit->gvn().transform_no_reclaim(this); } return this; } Node* ValueTypeNode::field_value(uint index) const { assert(index < field_count(), "index out of bounds"); return in(Values + index); } // Get the value of the field at the given offset. // If 'recursive' is true, flattened value type fields will be resolved recursively. Node* ValueTypeNode::field_value_by_offset(int offset, bool recursive) const { // If the field at 'offset' belongs to a flattened value type field, 'index' refers to the // corresponding ValueTypeNode input and 'sub_offset' is the offset in flattened value type. int index = value_klass()->field_index_by_offset(offset); int sub_offset = offset - field_offset(index); Node* value = field_value(index); if (recursive && value->is_ValueType()) { // Flattened value type field ValueTypeNode* vt = value->as_ValueType(); sub_offset += vt->value_klass()->first_field_offset(); // Add header size return vt->field_value_by_offset(sub_offset); } assert(!(recursive && value->is_ValueType()), "should not be a value type"); assert(sub_offset == 0, "offset mismatch"); return value; } void ValueTypeNode::set_field_value(uint index, Node* value) { assert(index < field_count(), "index out of bounds"); set_req(Values + index, value); } int ValueTypeNode::field_offset(uint index) const { assert(index < field_count(), "index out of bounds"); return value_klass()->field_offset_by_index(index); } ciType* ValueTypeNode::field_type(uint index) const { assert(index < field_count(), "index out of bounds"); return value_klass()->field_type_by_index(index); } void ValueTypeNode::make_scalar_in_safepoints(Compile* C) { const TypeValueTypePtr* res_type = TypeValueTypePtr::make(bottom_type()->isa_valuetype(), TypePtr::NotNull); ciValueKlass* vk = value_klass(); uint nfields = vk->flattened_field_count(); for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) { Node* u = fast_out(i); if (u->is_SafePoint() && (!u->is_Call() || u->as_Call()->has_debug_use(this))) { Node* in_oop = get_oop(); const Type* oop_type = in_oop->bottom_type(); SafePointNode* sfpt = u->as_SafePoint(); JVMState* jvms = sfpt->jvms(); int start = jvms->debug_start(); int end = jvms->debug_end(); if (oop_type->meet(TypePtr::NULL_PTR) != oop_type) { // Replace safepoint edge by oop int nb = sfpt->replace_edges_in_range(this, in_oop, start, end); --i; imax -= nb; } else { // Replace safepoint edge by SafePointScalarObjectNode and add field values assert(jvms != NULL, "missing JVMS"); uint first_ind = (sfpt->req() - jvms->scloff()); SafePointScalarObjectNode* sobj = new SafePointScalarObjectNode(res_type, #ifdef ASSERT NULL, #endif first_ind, nfields); sobj->init_req(0, C->root()); // Iterate over the value type fields in order of increasing // offset and add the field values to the safepoint. for (uint j = 0; j < nfields; ++j) { int offset = vk->nonstatic_field_at(j)->offset(); Node* value = field_value_by_offset(offset, true /* include flattened value type fields */); sfpt->add_req(value); } jvms->set_endoff(sfpt->req()); int nb = sfpt->replace_edges_in_range(this, sobj, start, end); --i; imax -= nb; } } } } uint ValueTypeNode::set_arguments_for_java_call(CallJavaNode* call, int base_input, const GraphKit& kit, ciValueKlass* base_vk, int base_offset) { ciValueKlass* vk = value_klass(); if (base_vk == NULL) { base_vk = vk; } uint edges = 0; for (uint i = 0; i < field_count(); i++) { ciType* f_type = field_type(i); int offset = base_offset + field_offset(i) - (base_offset > 0 ? vk->first_field_offset() : 0); Node* arg = field_value(i); if (f_type->is_valuetype()) { ciValueKlass* embedded_vk = f_type->as_value_klass(); edges += arg->as_ValueType()->set_arguments_for_java_call(call, base_input, kit, base_vk, offset); } else { int j = 0; int extra = 0; for (; j < base_vk->nof_nonstatic_fields(); j++) { ciField* f = base_vk->nonstatic_field_at(j); if (offset == f->offset()) { assert(f->type() == f_type, "inconsistent field type"); break; } BasicType bt = f->type()->basic_type(); if (bt == T_LONG || bt == T_DOUBLE) { extra++; } } call->init_req(base_input + j + extra, arg); edges++; BasicType bt = f_type->basic_type(); if (bt == T_LONG || bt == T_DOUBLE) { call->init_req(base_input + j + extra + 1, kit.top()); edges++; } } } return edges; } Node* ValueTypeNode::Ideal(PhaseGVN* phase, bool can_reshape) { // No optimizations for now return NULL; } #ifndef PRODUCT void ValueTypeNode::dump_spec(outputStream* st) const { TypeNode::dump_spec(st); } #endif