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 Node* 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(PhaseGVN& gvn, Node* mem, Node* oop) { 39 // Create and initialize a ValueTypeNode by loading all field 40 // values from memory and also save the oop to the heap allocated version. 41 const TypeValueTypePtr* vtptr = gvn.type(oop)->is_valuetypeptr(); 42 ValueTypeNode* vt = new ValueTypeNode(vtptr->value_type(), oop); 43 for (uint index = 0; index < vt->field_count(); ++index) { 44 int offset = vt->get_field_offset(index); 45 const TypePtr* adr_type = vtptr->add_offset(offset); 46 const Type* field_type = Type::get_const_basic_type(vt->get_field_type(index)); 47 Node* adr = gvn.transform(new AddPNode(oop, oop, gvn.longcon(offset))); 48 Node* ld = LoadNode::make(gvn, NULL, mem, adr, adr_type, field_type, field_type->basic_type(), MemNode::unordered); 49 vt->set_field_value(index, gvn.transform(ld)); 50 } 51 return gvn.transform(vt); 52 } 53 54 Node* ValueTypeNode::store_to_memory(GraphKit* kit) { 55 Node* in_oop = get_oop(); 56 Node* null_ctl = kit->top(); 57 // Check if value type is already allocated 58 Node* not_null_oop = kit->null_check_oop(in_oop, &null_ctl); 59 if (null_ctl->is_top()) { 60 // Value type is allocated 61 return not_null_oop; 62 } 63 // Not able to prove that value type is allocated. 64 // Emit runtime check that may be folded later. 65 const Type* oop_type = kit->gvn().type(in_oop); 66 assert(TypePtr::NULL_PTR->higher_equal(oop_type), "should not be allocated"); 67 68 const TypeValueTypePtr* vtptr_type = TypeValueTypePtr::make(bottom_type()->isa_valuetype(), TypePtr::NotNull); 69 RegionNode* region = new RegionNode(3); 70 PhiNode* oop = new PhiNode(region, vtptr_type); 71 PhiNode* io = new PhiNode(region, Type::ABIO); 72 PhiNode* mem = new PhiNode(region, Type::MEMORY, TypePtr::BOTTOM); 73 74 // Oop is non-NULL, use it 75 region->init_req(1, kit->control()); 76 // Fixme if we cast oop to not null we fail if the control path is not folded 77 // castnode.cpp:69: # assert(ft == Type::TOP) failed: special case #3 78 //oop ->init_req(1, not_null_oop); 79 oop ->init_req(1, in_oop); 80 io ->init_req(1, kit->i_o()); 81 mem ->init_req(1, kit->merged_memory()); 82 83 // Oop is NULL, allocate value type 84 kit->set_control(null_ctl); 85 kit->kill_dead_locals(); 86 Node* klass_node = kit->makecon(TypeKlassPtr::make(get_value_klass())); 87 Node* alloc_oop = kit->new_instance(klass_node); 88 // Write field values to memory 89 for (uint index = 0; index < field_count(); ++index) { 90 int offset = get_field_offset(index); 91 const TypePtr* adr_type = vtptr_type->add_offset(offset); 92 Node* adr = kit->basic_plus_adr(alloc_oop, alloc_oop, offset); 93 kit->store_to_memory(kit->control(), adr, get_field_value(index), get_field_type(index), adr_type, MemNode::unordered); 94 } 95 region->init_req(2, kit->control()); 96 oop ->init_req(2, alloc_oop); 97 io ->init_req(2, kit->i_o()); 98 mem ->init_req(2, kit->merged_memory()); 99 100 // Update GraphKit 101 kit->set_control(kit->gvn().transform(region)); 102 kit->set_i_o(kit->gvn().transform(io)); 103 kit->set_all_memory(kit->gvn().transform(mem)); 104 kit->record_for_igvn(region); 105 kit->record_for_igvn(oop); 106 kit->record_for_igvn(io); 107 kit->record_for_igvn(mem); 108 109 Node* res_oop = kit->gvn().transform(oop); 110 ValueTypeNode* vt = clone()->as_ValueType(); 111 vt->set_oop(res_oop); 112 kit->replace_in_map(this, kit->gvn().transform(vt)); 113 return res_oop; 114 } 115 116 Node* ValueTypeNode::get_field_value(uint index) const { 117 assert(index < field_count(), "index out of bounds"); 118 return in(Values + index); 119 } 120 121 Node* ValueTypeNode::get_field_value_by_offset(int field_offset) const { 122 int index = get_value_klass()->get_field_index_by_offset(field_offset); 123 return get_field_value(index); 124 } 125 126 void ValueTypeNode::set_field_value(uint index, Node* value) { 127 assert(index < field_count(), "index out of bounds"); 128 set_req(Values + index, value); 129 } 130 131 int ValueTypeNode::get_field_offset(uint index) const { 132 assert(index < field_count(), "index out of bounds"); 133 return get_value_klass()->get_field_offset_by_index(index); 134 } 135 136 BasicType ValueTypeNode::get_field_type(uint index) const { 137 assert(index < field_count(), "index out of bounds"); 138 return get_value_klass()->get_field_type_by_index(index); 139 } 140 141 void ValueTypeNode::make_scalar_in_safepoints(Compile* C) { 142 const TypeValueTypePtr* res_type = TypeValueTypePtr::make(bottom_type()->isa_valuetype(), TypePtr::NotNull); 143 uint nfields = field_count(); 144 for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) { 145 Node* u = fast_out(i); 146 if (u->is_SafePoint() && (!u->is_Call() || u->as_Call()->has_debug_use(this))) { 147 Node* in_oop = get_oop(); 148 const Type* oop_type = in_oop->bottom_type(); 149 SafePointNode* sfpt = u->as_SafePoint(); 150 JVMState* jvms = sfpt->jvms(); 151 int start = jvms->debug_start(); 152 int end = jvms->debug_end(); 153 if (oop_type->meet(TypePtr::NULL_PTR) != oop_type) { 154 int nb = sfpt->replace_edges_in_range(this, in_oop, start, end); 155 --i; imax -= nb; 156 } else { 157 assert(jvms != NULL, "missing JVMS"); 158 uint first_ind = (sfpt->req() - jvms->scloff()); 159 SafePointScalarObjectNode* sobj = new SafePointScalarObjectNode(res_type, 160 #ifdef ASSERT 161 NULL, 162 #endif 163 first_ind, nfields); 164 sobj->init_req(0, C->root()); 165 // fields must be added to the safepoint in order of increasing offset 166 int min = 0; 167 for (uint j = 0; j < nfields; j++) { 168 int off = INT_MAX; 169 uint next = 0; 170 for (uint k = 0; k < nfields; k++) { 171 int offset = get_field_offset(k); 172 if (offset > min && offset < off) { 173 off = offset; 174 next = k; 175 } 176 } 177 min = get_field_offset(next); 178 sfpt->add_req(in(Values + next)); 179 } 180 jvms->set_endoff(sfpt->req()); 181 int nb = sfpt->replace_edges_in_range(this, sobj, start, end); 182 --i; imax -= nb; 183 } 184 } 185 } 186 } 187 188 Node* ValueTypeNode::Ideal(PhaseGVN* phase, bool can_reshape) { 189 // No optimizations for now 190 return NULL; 191 } 192 193 #ifndef PRODUCT 194 195 void ValueTypeNode::dump_spec(outputStream* st) const { 196 TypeNode::dump_spec(st); 197 if (!get_oop()->is_top()) { 198 st->print(" #oop"); 199 } 200 } 201 202 #endif