/* * 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" Node* 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(PhaseGVN& gvn, Node* mem, Node* oop) { // Create and initialize a ValueTypeNode by loading all field // values from memory and also save the oop to the heap allocated version. const TypeValueTypePtr* vtptr = gvn.type(oop)->is_valuetypeptr(); ValueTypeNode* vt = new ValueTypeNode(vtptr->value_type(), oop); for (uint index = 0; index < vt->field_count(); ++index) { int offset = vt->get_field_offset(index); const TypePtr* adr_type = vtptr->add_offset(offset); const Type* field_type = Type::get_const_basic_type(vt->get_field_type(index)); Node* adr = gvn.transform(new AddPNode(oop, oop, gvn.longcon(offset))); Node* ld = LoadNode::make(gvn, NULL, mem, adr, adr_type, field_type, field_type->basic_type(), MemNode::unordered); vt->set_field_value(index, gvn.transform(ld)); } return gvn.transform(vt); } 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(get_value_klass())); Node* alloc_oop = kit->new_instance(klass_node); // Write field values to memory for (uint index = 0; index < field_count(); ++index) { int offset = get_field_offset(index); const TypePtr* adr_type = vtptr_type->add_offset(offset); Node* adr = kit->basic_plus_adr(alloc_oop, alloc_oop, offset); kit->store_to_memory(kit->control(), adr, get_field_value(index), get_field_type(index), adr_type, MemNode::unordered); } 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); 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; } Node* ValueTypeNode::get_field_value(uint index) const { assert(index < field_count(), "index out of bounds"); return in(Values + index); } Node* ValueTypeNode::get_field_value_by_offset(int field_offset) const { int index = get_value_klass()->get_field_index_by_offset(field_offset); return get_field_value(index); } void ValueTypeNode::set_field_value(uint index, Node* value) { assert(index < field_count(), "index out of bounds"); set_req(Values + index, value); } int ValueTypeNode::get_field_offset(uint index) const { assert(index < field_count(), "index out of bounds"); return get_value_klass()->get_field_offset_by_index(index); } BasicType ValueTypeNode::get_field_type(uint index) const { assert(index < field_count(), "index out of bounds"); return get_value_klass()->get_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); uint nfields = 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) { int nb = sfpt->replace_edges_in_range(this, in_oop, start, end); --i; imax -= nb; } else { 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()); // fields must be added to the safepoint in order of increasing offset int min = 0; for (uint j = 0; j < nfields; j++) { int off = INT_MAX; uint next = 0; for (uint k = 0; k < nfields; k++) { int offset = get_field_offset(k); if (offset > min && offset < off) { off = offset; next = k; } } min = get_field_offset(next); sfpt->add_req(in(Values + next)); } jvms->set_endoff(sfpt->req()); int nb = sfpt->replace_edges_in_range(this, sobj, start, end); --i; imax -= nb; } } } } 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); if (!get_oop()->is_top()) { st->print(" #oop"); } } #endif