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