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(PhaseGVN& gvn, Node* mem, Node* oop) { 39 // Create and initialize a ValueTypeNode by loading all field 40 // values from a heap-allocated version and also save the oop. 41 const TypeValueTypePtr* vtptr = gvn.type(oop)->is_valuetypeptr(); 42 ValueTypeNode* vt = new ValueTypeNode(vtptr->value_type(), oop); 43 vt->load_values(gvn, mem, vt->get_value_klass(), oop); 44 return gvn.transform(vt); 45 } 46 47 Node* ValueTypeNode::make(PhaseGVN& gvn, ciValueKlass* klass, Node* mem, ciInstanceKlass* holder, Node* obj, int field_offset) { 48 // Create and initialize a ValueTypeNode by loading all field 49 // values from a flattened value type field at 'field_offset' in 'obj'. 50 ValueTypeNode* vt = make(gvn, klass)->as_ValueType(); 51 // The value type is flattened into the object without an oop header. Subtract the 52 // offset of the first field to account for the missing header when loading the values. 53 int base_offset = field_offset - klass->get_first_field_offset(); 54 vt->load_values(gvn, mem, holder, obj, base_offset); 55 return gvn.transform(vt); 56 } 57 58 void ValueTypeNode::load_values(PhaseGVN& gvn, Node* mem, ciInstanceKlass* holder, Node* base, int base_offset) { 59 // Initialize the value type by loading its field values from 60 // memory and adding the values as input edges to the node. 61 for (uint i = 0; i < field_count(); ++i) { 62 int offset = base_offset + get_field_offset(i); 63 Node* adr = gvn.transform(new AddPNode(base, base, gvn.longcon(offset))); 64 ciField* field = holder->get_field_by_offset(offset, false); 65 const TypePtr* adr_type = gvn.C->alias_type(field)->adr_type(); 66 Node* value = NULL; 67 ciType* field_type = get_field_type(i); 68 if (field_type->is_valuetype()) { 69 // Recursively load the flattened value type field 70 value = ValueTypeNode::make(gvn, field_type->as_value_klass(), mem, holder, base, offset); 71 } else { 72 value = LoadNode::make(gvn, NULL, mem, adr, adr_type, Type::get_const_type(field_type), field_type->basic_type(), MemNode::unordered); 73 } 74 set_field_value(i, gvn.transform(value)); 75 } 76 } 77 78 void ValueTypeNode::store_to_field(GraphKit* kit, ciInstanceKlass* holder, Node* obj, int field_offset) const { 79 // The value type is embedded into the object without an oop header. Subtract the 80 // offset of the first field to account for the missing header when storing the values. 81 int base_offset = field_offset - get_value_klass()->get_first_field_offset(); 82 store_values(kit, holder, obj, base_offset); 83 } 84 85 void ValueTypeNode::store_values(GraphKit* kit, ciInstanceKlass* holder, Node* base, int base_offset) const { 86 // Write field values to memory 87 for (uint i = 0; i < field_count(); ++i) { 88 int offset = base_offset + get_field_offset(i); 89 Node* adr = kit->basic_plus_adr(base, base, offset); 90 ciField* field = holder->get_field_by_offset(offset, false); 91 const TypePtr* adr_type = kit->C->alias_type(field)->adr_type(); 92 Node* value = get_field_value(i); 93 if (value->is_ValueType()) { 94 // Recursively store the flattened value type field 95 value->isa_ValueType()->store_to_field(kit, holder, base, offset); 96 } else { 97 kit->store_to_memory(kit->control(), adr, value, get_field_type(i)->basic_type(), adr_type, MemNode::unordered); 98 } 99 } 100 } 101 102 Node* ValueTypeNode::store_to_memory(GraphKit* kit) { 103 Node* in_oop = get_oop(); 104 Node* null_ctl = kit->top(); 105 // Check if value type is already allocated 106 Node* not_null_oop = kit->null_check_oop(in_oop, &null_ctl); 107 if (null_ctl->is_top()) { 108 // Value type is allocated 109 return not_null_oop; 110 } 111 // Not able to prove that value type is allocated. 112 // Emit runtime check that may be folded later. 113 const Type* oop_type = kit->gvn().type(in_oop); 114 assert(TypePtr::NULL_PTR->higher_equal(oop_type), "should not be allocated"); 115 116 const TypeValueTypePtr* vtptr_type = TypeValueTypePtr::make(bottom_type()->isa_valuetype(), TypePtr::NotNull); 117 RegionNode* region = new RegionNode(3); 118 PhiNode* oop = new PhiNode(region, vtptr_type); 119 PhiNode* io = new PhiNode(region, Type::ABIO); 120 PhiNode* mem = new PhiNode(region, Type::MEMORY, TypePtr::BOTTOM); 121 122 // Oop is non-NULL, use it 123 region->init_req(1, kit->control()); 124 // Fixme if we cast oop to not null we fail if the control path is not folded 125 // castnode.cpp:69: # assert(ft == Type::TOP) failed: special case #3 126 //oop ->init_req(1, not_null_oop); 127 oop ->init_req(1, in_oop); 128 io ->init_req(1, kit->i_o()); 129 mem ->init_req(1, kit->merged_memory()); 130 131 // Oop is NULL, allocate value type 132 kit->set_control(null_ctl); 133 kit->kill_dead_locals(); 134 Node* klass_node = kit->makecon(TypeKlassPtr::make(get_value_klass())); 135 Node* alloc_oop = kit->new_instance(klass_node); 136 // Write field values to memory 137 store_values(kit, get_value_klass(), alloc_oop); 138 region->init_req(2, kit->control()); 139 oop ->init_req(2, alloc_oop); 140 io ->init_req(2, kit->i_o()); 141 mem ->init_req(2, kit->merged_memory()); 142 143 // Update GraphKit 144 kit->set_control(kit->gvn().transform(region)); 145 kit->set_i_o(kit->gvn().transform(io)); 146 kit->set_all_memory(kit->gvn().transform(mem)); 147 kit->record_for_igvn(region); 148 kit->record_for_igvn(oop); 149 kit->record_for_igvn(io); 150 kit->record_for_igvn(mem); 151 152 // Use cloned ValueTypeNode to propagate oop from now on 153 Node* res_oop = kit->gvn().transform(oop); 154 ValueTypeNode* vt = clone()->as_ValueType(); 155 vt->set_oop(res_oop); 156 kit->replace_in_map(this, kit->gvn().transform(vt)); 157 return res_oop; 158 } 159 160 // Clones the values type to handle control flow merges involving multiple value types. 161 // The input edges are replaced by PhiNodes to represent the merged values. 162 ValueTypeNode* ValueTypeNode::clone_with_phis(PhaseGVN& gvn, Node* region) { 163 ValueTypeNode* vt = clone()->as_ValueType(); 164 165 // Create a PhiNode for merging the oop values 166 const TypeValueTypePtr* vtptr = TypeValueTypePtr::make(vt->bottom_type()->isa_valuetype()); 167 PhiNode* oop = PhiNode::make(region, vt->get_oop(), vtptr); 168 gvn.set_type(oop, vtptr); 169 vt->set_oop(oop); 170 171 // Create a PhiNode each for merging the field values 172 for (uint i = 0; i < vt->field_count(); ++i) { 173 ciType* type = vt->get_field_type(i); 174 Node* value = vt->get_field_value(i); 175 if (type->is_valuetype()) { 176 // Handle flattened value type fields recursively 177 value = value->as_ValueType()->clone_with_phis(gvn, region); 178 } else { 179 const Type* phi_type = Type::get_const_type(type); 180 value = PhiNode::make(region, value, phi_type); 181 gvn.set_type(value, phi_type); 182 } 183 vt->set_field_value(i, value); 184 } 185 gvn.set_type(vt, vt->bottom_type()); 186 return vt; 187 } 188 189 // Merges 'this' with 'other' by updating the input PhiNodes added by 'clone_with_phis' 190 Node* ValueTypeNode::merge_with(GraphKit* kit, const ValueTypeNode* other, int pnum) { 191 // Merge oop inputs 192 PhiNode* phi = get_oop()->as_Phi(); 193 phi->set_req(pnum, other->get_oop()); 194 if (pnum == PhiNode::Input) { 195 // Last merge 196 set_oop(kit->gvn().transform_no_reclaim(phi)); 197 kit->record_for_igvn(phi); 198 } 199 // Merge field values 200 for (uint i = 0; i < field_count(); ++i) { 201 Node* val1 = get_field_value(i); 202 Node* val2 = other->get_field_value(i); 203 if (val1->isa_ValueType()) { 204 val1->as_ValueType()->merge_with(kit, val2->as_ValueType(), pnum); 205 } else { 206 assert(!val2->is_ValueType(), "inconsistent merge values"); 207 val1->set_req(pnum, val2); 208 } 209 if (pnum == PhiNode::Input) { 210 // Last merge 211 set_field_value(i, kit->gvn().transform_no_reclaim(val1)); 212 kit->record_for_igvn(val1); 213 } 214 } 215 if (pnum == PhiNode::Input) { 216 // Last merge for this value type. 217 kit->record_for_igvn(this); 218 return kit->gvn().transform_no_reclaim(this); 219 } 220 return this; 221 } 222 223 Node* ValueTypeNode::get_field_value(uint index) const { 224 assert(index < field_count(), "index out of bounds"); 225 return in(Values + index); 226 } 227 228 // Get the value of the field at the given offset. 229 // If 'recursive' is true, flattened value type fields will be resolved recursively. 230 Node* ValueTypeNode::get_field_value_by_offset(int offset, bool recursive) const { 231 // If the field at 'offset' belongs to a flattened value type field, 'index' refers to the 232 // corresponding ValueTypeNode input and 'sub_offset' is the offset in flattened value type. 233 int index = get_value_klass()->get_field_index_by_offset(offset); 234 int sub_offset = offset - get_field_offset(index); 235 Node* value = get_field_value(index); 236 if (recursive && value->is_ValueType()) { 237 // Flattened value type field 238 ValueTypeNode* vt = value->as_ValueType(); 239 sub_offset += vt->get_value_klass()->get_first_field_offset(); // Add header size 240 return vt->get_field_value_by_offset(sub_offset); 241 } 242 assert(!(recursive && value->is_ValueType()), "should not be a value type"); 243 assert(sub_offset == 0, "offset mismatch"); 244 return value; 245 } 246 247 void ValueTypeNode::set_field_value(uint index, Node* value) { 248 assert(index < field_count(), "index out of bounds"); 249 set_req(Values + index, value); 250 } 251 252 int ValueTypeNode::get_field_offset(uint index) const { 253 assert(index < field_count(), "index out of bounds"); 254 return get_value_klass()->get_field_offset_by_index(index); 255 } 256 257 ciType* ValueTypeNode::get_field_type(uint index) const { 258 assert(index < field_count(), "index out of bounds"); 259 return get_value_klass()->get_field_type_by_index(index); 260 } 261 262 void ValueTypeNode::make_scalar_in_safepoints(Compile* C) { 263 const TypeValueTypePtr* res_type = TypeValueTypePtr::make(bottom_type()->isa_valuetype(), TypePtr::NotNull); 264 ciValueKlass* vk = get_value_klass(); 265 uint nfields = vk->get_field_count(); 266 for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) { 267 Node* u = fast_out(i); 268 if (u->is_SafePoint() && (!u->is_Call() || u->as_Call()->has_debug_use(this))) { 269 Node* in_oop = get_oop(); 270 const Type* oop_type = in_oop->bottom_type(); 271 SafePointNode* sfpt = u->as_SafePoint(); 272 JVMState* jvms = sfpt->jvms(); 273 int start = jvms->debug_start(); 274 int end = jvms->debug_end(); 275 if (oop_type->meet(TypePtr::NULL_PTR) != oop_type) { 276 // Replace safepoint edge by oop 277 int nb = sfpt->replace_edges_in_range(this, in_oop, start, end); 278 --i; imax -= nb; 279 } else { 280 // Replace safepoint edge by SafePointScalarObjectNode and add field values 281 assert(jvms != NULL, "missing JVMS"); 282 uint first_ind = (sfpt->req() - jvms->scloff()); 283 SafePointScalarObjectNode* sobj = new SafePointScalarObjectNode(res_type, 284 #ifdef ASSERT 285 NULL, 286 #endif 287 first_ind, nfields); 288 sobj->init_req(0, C->root()); 289 // Iterate over the value type fields in order of increasing 290 // offset and add the field values to the safepoint. 291 for (uint j = 0; j < nfields; ++j) { 292 int offset = vk->nonstatic_field_at(j)->offset(); 293 Node* value = get_field_value_by_offset(offset, true /* include flattened value type fields */); 294 sfpt->add_req(value); 295 } 296 jvms->set_endoff(sfpt->req()); 297 int nb = sfpt->replace_edges_in_range(this, sobj, start, end); 298 --i; imax -= nb; 299 } 300 } 301 } 302 } 303 304 uint ValueTypeNode::set_arguments_for_java_call(CallJavaNode* call, int base_input, const GraphKit& kit, ciValueKlass* base_vk, int base_offset) { 305 ciValueKlass* vk = get_value_klass(); 306 if (base_vk == NULL) { 307 base_vk = vk; 308 } 309 uint edges = 0; 310 for (uint i = 0; i < field_count(); i++) { 311 ciType* field_type = get_field_type(i); 312 int offset = base_offset + get_field_offset(i) - (base_offset > 0 ? vk->get_first_field_offset() : 0); 313 Node* arg = get_field_value(i); 314 if (field_type->is_valuetype()) { 315 ciValueKlass* embedded_vk = field_type->as_value_klass(); 316 edges += arg->as_ValueType()->set_arguments_for_java_call(call, base_input, kit, base_vk, offset); 317 } else { 318 int j = 0; int extra = 0; 319 for (; j < base_vk->nof_nonstatic_fields(); j++) { 320 ciField* f = base_vk->nonstatic_field_at(j); 321 if (offset == f->offset()) { 322 assert(f->type() == field_type, ""); 323 break; 324 } 325 BasicType bt = f->type()->basic_type(); 326 if (bt == T_LONG || bt == T_DOUBLE) { 327 extra++; 328 } 329 } 330 call->init_req(base_input + j + extra, arg); 331 edges++; 332 BasicType bt = field_type->basic_type(); 333 if (bt == T_LONG || bt == T_DOUBLE) { 334 call->init_req(base_input + j + extra + 1, kit.top()); 335 edges++; 336 } 337 } 338 } 339 return edges; 340 } 341 342 Node* ValueTypeNode::Ideal(PhaseGVN* phase, bool can_reshape) { 343 // No optimizations for now 344 return NULL; 345 } 346 347 #ifndef PRODUCT 348 349 void ValueTypeNode::dump_spec(outputStream* st) const { 350 TypeNode::dump_spec(st); 351 } 352 353 #endif