43 for (uint i = 0; i < vt->field_count(); ++i) {
44 ciType* field_type = vt->field_type(i);
45 Node* value = NULL;
46 if (field_type->is_valuetype()) {
47 value = ValueTypeNode::make_default(gvn, field_type->as_value_klass());
48 } else {
49 value = gvn.zerocon(field_type->basic_type());
50 }
51 vt->set_field_value(i, value);
52 }
53 return gvn.transform(vt);
54 }
55
56 Node* ValueTypeNode::make(PhaseGVN& gvn, Node* mem, Node* oop) {
57 // Create and initialize a ValueTypeNode by loading all field
58 // values from a heap-allocated version and also save the oop.
59 const TypeValueType* type = gvn.type(oop)->is_valuetypeptr()->value_type();
60 ValueTypeNode* vt = new ValueTypeNode(type, oop);
61 vt->load(gvn, mem, oop, oop, type->value_klass());
62 assert(vt->is_allocated(&gvn), "value type should be allocated");
63 assert(oop->is_Con() || oop->is_CheckCastPP() || vt->is_loaded(&gvn, type) == oop, "value type should be loaded");
64 return gvn.transform(vt);
65 }
66
67 Node* ValueTypeNode::make(PhaseGVN& gvn, ciValueKlass* vk, Node* mem, Node* obj, Node* ptr, ciInstanceKlass* holder, int holder_offset) {
68 // Create and initialize a ValueTypeNode by loading all field values from
69 // a flattened value type field at 'holder_offset' or from a value type array.
70 ValueTypeNode* vt = make(gvn, vk);
71 // The value type is flattened into the object without an oop header. Subtract the
72 // offset of the first field to account for the missing header when loading the values.
73 holder_offset -= vk->first_field_offset();
74 vt->load(gvn, mem, obj, ptr, holder, holder_offset);
75 assert(vt->is_loaded(&gvn, vt->type()->isa_valuetype()) != obj, "holder oop should not be used as flattened value type oop");
76 return gvn.transform(vt)->as_ValueType();
77 }
78
79 void ValueTypeNode::load(PhaseGVN& gvn, Node* mem, Node* base, Node* ptr, ciInstanceKlass* holder, int holder_offset) {
80 // Initialize the value type by loading its field values from
81 // memory and adding the values as input edges to the node.
82 for (uint i = 0; i < field_count(); ++i) {
83 int offset = holder_offset + field_offset(i);
247 kit->record_for_igvn(region);
248 kit->record_for_igvn(oop);
249 kit->record_for_igvn(io);
250 kit->record_for_igvn(mem);
251
252 // Use cloned ValueTypeNode to propagate oop from now on
253 Node* res_oop = kit->gvn().transform(oop);
254 ValueTypeNode* vt = clone()->as_ValueType();
255 vt->set_oop(res_oop);
256 kit->replace_in_map(this, kit->gvn().transform(vt));
257 return res_oop;
258 }
259
260 bool ValueTypeNode::is_allocated(PhaseGVN* phase) const {
261 const Type* oop_type = phase->type(get_oop());
262 return oop_type->meet(TypePtr::NULL_PTR) != oop_type;
263 }
264
265 // Clones the values type to handle control flow merges involving multiple value types.
266 // The inputs are replaced by PhiNodes to represent the merged values for the given region.
267 ValueTypeNode* ValueTypeNode::clone_with_phis(PhaseGVN* gvn, Node* region) {
268 assert(!has_phi_inputs(region), "already cloned with phis");
269 ValueTypeNode* vt = clone()->as_ValueType();
270
271 // Create a PhiNode for merging the oop values
272 const TypeValueTypePtr* vtptr = TypeValueTypePtr::make(vt->bottom_type()->isa_valuetype());
273 PhiNode* oop = PhiNode::make(region, vt->get_oop(), vtptr);
274 gvn->set_type(oop, vtptr);
275 vt->set_oop(oop);
276
277 // Create a PhiNode each for merging the field values
278 for (uint i = 0; i < vt->field_count(); ++i) {
279 ciType* type = vt->field_type(i);
280 Node* value = vt->field_value(i);
281 if (type->is_valuetype()) {
282 // Handle flattened value type fields recursively
283 value = value->as_ValueType()->clone_with_phis(gvn, region);
284 } else {
285 const Type* phi_type = Type::get_const_type(type);
286 value = PhiNode::make(region, value, phi_type);
287 gvn->set_type(value, phi_type);
288 }
289 vt->set_field_value(i, value);
290 }
291 gvn->set_type(vt, vt->bottom_type());
292 return vt;
293 }
294
295 // Checks if the inputs of the ValueTypeNode were replaced by PhiNodes
296 // for the given region (see ValueTypeNode::clone_with_phis).
297 bool ValueTypeNode::has_phi_inputs(Node* region) {
298 // Check oop input
299 bool result = get_oop()->is_Phi() && get_oop()->as_Phi()->region() == region;
300 #ifdef ASSERT
301 if (result) {
302 // Check all field value inputs for consistency
303 for (uint i = Oop; i < field_count(); ++i) {
304 Node* n = in(i);
305 if (n->is_ValueType()) {
306 assert(n->as_ValueType()->has_phi_inputs(region), "inconsistent phi inputs");
307 } else {
308 assert(n->is_Phi() && n->as_Phi()->region() == region, "inconsistent phi inputs");
309 }
310 }
311 }
312 #endif
313 return result;
314 }
315
316 // Merges 'this' with 'other' by updating the input PhiNodes added by 'clone_with_phis'
317 ValueTypeNode* ValueTypeNode::merge_with(PhaseGVN* gvn, const ValueTypeNode* other, int pnum, bool transform) {
318 // Merge oop inputs
319 PhiNode* phi = get_oop()->as_Phi();
320 phi->set_req(pnum, other->get_oop());
321 if (transform) {
322 set_oop(gvn->transform(phi));
323 gvn->record_for_igvn(phi);
324 }
325 // Merge field values
326 for (uint i = 0; i < field_count(); ++i) {
327 Node* val1 = field_value(i);
328 Node* val2 = other->field_value(i);
329 if (val1->isa_ValueType()) {
330 val1->as_ValueType()->merge_with(gvn, val2->as_ValueType(), pnum, transform);
331 } else {
332 assert(val1->is_Phi(), "must be a phi node");
333 assert(!val2->is_ValueType(), "inconsistent merge values");
334 val1->set_req(pnum, val2);
335 }
336 if (transform) {
337 set_field_value(i, gvn->transform(val1));
338 gvn->record_for_igvn(val1);
339 }
340 }
341 return this;
342 }
343
344 Node* ValueTypeNode::field_value(uint index) const {
345 assert(index < field_count(), "index out of bounds");
346 return in(Values + index);
347 }
348
349 // Get the value of the field at the given offset.
350 // If 'recursive' is true, flattened value type fields will be resolved recursively.
351 Node* ValueTypeNode::field_value_by_offset(int offset, bool recursive) const {
352 // If the field at 'offset' belongs to a flattened value type field, 'index' refers to the
353 // corresponding ValueTypeNode input and 'sub_offset' is the offset in flattened value type.
354 int index = value_klass()->field_index_by_offset(offset);
355 int sub_offset = offset - field_offset(index);
356 Node* value = field_value(index);
357 if (recursive && value->is_ValueType()) {
358 // Flattened value type field
359 ValueTypeNode* vt = value->as_ValueType();
360 sub_offset += vt->value_klass()->first_field_offset(); // Add header size
361 return vt->field_value_by_offset(sub_offset);
362 }
363 assert(!(recursive && value->is_ValueType()), "should not be a value type");
364 assert(sub_offset == 0, "offset mismatch");
365 return value;
366 }
367
368 void ValueTypeNode::set_field_value(uint index, Node* value) {
369 assert(index < field_count(), "index out of bounds");
370 set_req(Values + index, value);
371 }
372
373 int ValueTypeNode::field_offset(uint index) const {
374 assert(index < field_count(), "index out of bounds");
375 return value_klass()->field_offset_by_index(index);
376 }
377
378 ciType* ValueTypeNode::field_type(uint index) const {
379 assert(index < field_count(), "index out of bounds");
380 return value_klass()->field_type_by_index(index);
381 }
382
383 void ValueTypeNode::make_scalar_in_safepoints(Compile* C) {
384 const TypeValueTypePtr* res_type = TypeValueTypePtr::make(bottom_type()->isa_valuetype(), TypePtr::NotNull);
385 ciValueKlass* vk = value_klass();
386 uint nfields = vk->flattened_field_count();
387 for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
388 Node* u = fast_out(i);
389 if (u->is_SafePoint() && (!u->is_Call() || u->as_Call()->has_debug_use(this))) {
390 Node* in_oop = get_oop();
391 const Type* oop_type = in_oop->bottom_type();
392 SafePointNode* sfpt = u->as_SafePoint();
393 JVMState* jvms = sfpt->jvms();
394 int start = jvms->debug_start();
395 int end = jvms->debug_end();
396 assert(TypePtr::NULL_PTR->higher_equal(oop_type), "already heap allocated value type should be linked directly");
397 // Replace safepoint edge by SafePointScalarObjectNode and add field values
398 assert(jvms != NULL, "missing JVMS");
399 uint first_ind = (sfpt->req() - jvms->scloff());
400 SafePointScalarObjectNode* sobj = new SafePointScalarObjectNode(res_type,
401 #ifdef ASSERT
402 NULL,
403 #endif
404 first_ind, nfields);
405 sobj->init_req(0, C->root());
406 // Iterate over the value type fields in order of increasing
407 // offset and add the field values to the safepoint.
408 for (uint j = 0; j < nfields; ++j) {
409 int offset = vk->nonstatic_field_at(j)->offset();
410 Node* value = field_value_by_offset(offset, true /* include flattened value type fields */);
411 sfpt->add_req(value);
412 }
413 jvms->set_endoff(sfpt->req());
414 int nb = sfpt->replace_edges_in_range(this, sobj, start, end);
415 --i; imax -= nb;
416 }
417 }
418 }
419
420 void ValueTypeNode::pass_klass(Node* n, uint pos, const GraphKit& kit) {
421 ciValueKlass* vk = value_klass();
422 const TypeKlassPtr* tk = TypeKlassPtr::make(vk);
423 Node* arg = kit.makecon(tk);
424 n->init_req(pos, arg);
425 }
426
427 uint ValueTypeNode::pass_fields(Node* n, int base_input, const GraphKit& kit, ciValueKlass* base_vk, int base_offset) {
428 ciValueKlass* vk = value_klass();
429 if (base_vk == NULL) {
430 base_vk = vk;
431 }
432 uint edges = 0;
433 for (uint i = 0; i < field_count(); i++) {
434 ciType* f_type = field_type(i);
|
43 for (uint i = 0; i < vt->field_count(); ++i) {
44 ciType* field_type = vt->field_type(i);
45 Node* value = NULL;
46 if (field_type->is_valuetype()) {
47 value = ValueTypeNode::make_default(gvn, field_type->as_value_klass());
48 } else {
49 value = gvn.zerocon(field_type->basic_type());
50 }
51 vt->set_field_value(i, value);
52 }
53 return gvn.transform(vt);
54 }
55
56 Node* ValueTypeNode::make(PhaseGVN& gvn, Node* mem, Node* oop) {
57 // Create and initialize a ValueTypeNode by loading all field
58 // values from a heap-allocated version and also save the oop.
59 const TypeValueType* type = gvn.type(oop)->is_valuetypeptr()->value_type();
60 ValueTypeNode* vt = new ValueTypeNode(type, oop);
61 vt->load(gvn, mem, oop, oop, type->value_klass());
62 assert(vt->is_allocated(&gvn), "value type should be allocated");
63 assert(oop->is_Con() || oop->is_CheckCastPP() || oop->Opcode() == Op_ValueTypePtr || vt->is_loaded(&gvn, type) == oop, "value type should be loaded");
64 return gvn.transform(vt);
65 }
66
67 Node* ValueTypeNode::make(PhaseGVN& gvn, ciValueKlass* vk, Node* mem, Node* obj, Node* ptr, ciInstanceKlass* holder, int holder_offset) {
68 // Create and initialize a ValueTypeNode by loading all field values from
69 // a flattened value type field at 'holder_offset' or from a value type array.
70 ValueTypeNode* vt = make(gvn, vk);
71 // The value type is flattened into the object without an oop header. Subtract the
72 // offset of the first field to account for the missing header when loading the values.
73 holder_offset -= vk->first_field_offset();
74 vt->load(gvn, mem, obj, ptr, holder, holder_offset);
75 assert(vt->is_loaded(&gvn, vt->type()->isa_valuetype()) != obj, "holder oop should not be used as flattened value type oop");
76 return gvn.transform(vt)->as_ValueType();
77 }
78
79 void ValueTypeNode::load(PhaseGVN& gvn, Node* mem, Node* base, Node* ptr, ciInstanceKlass* holder, int holder_offset) {
80 // Initialize the value type by loading its field values from
81 // memory and adding the values as input edges to the node.
82 for (uint i = 0; i < field_count(); ++i) {
83 int offset = holder_offset + field_offset(i);
247 kit->record_for_igvn(region);
248 kit->record_for_igvn(oop);
249 kit->record_for_igvn(io);
250 kit->record_for_igvn(mem);
251
252 // Use cloned ValueTypeNode to propagate oop from now on
253 Node* res_oop = kit->gvn().transform(oop);
254 ValueTypeNode* vt = clone()->as_ValueType();
255 vt->set_oop(res_oop);
256 kit->replace_in_map(this, kit->gvn().transform(vt));
257 return res_oop;
258 }
259
260 bool ValueTypeNode::is_allocated(PhaseGVN* phase) const {
261 const Type* oop_type = phase->type(get_oop());
262 return oop_type->meet(TypePtr::NULL_PTR) != oop_type;
263 }
264
265 // Clones the values type to handle control flow merges involving multiple value types.
266 // The inputs are replaced by PhiNodes to represent the merged values for the given region.
267 ValueTypeBaseNode* ValueTypeBaseNode::clone_with_phis(PhaseGVN* gvn, Node* region) {
268 assert(!has_phi_inputs(region), "already cloned with phis");
269 ValueTypeBaseNode* vt = clone()->as_ValueTypeBase();
270
271 // Create a PhiNode for merging the oop values
272 const TypeValueTypePtr* vtptr = value_type_ptr();
273 PhiNode* oop = PhiNode::make(region, vt->get_oop(), vtptr);
274 gvn->set_type(oop, vtptr);
275 vt->set_oop(oop);
276
277 // Create a PhiNode each for merging the field values
278 for (uint i = 0; i < vt->field_count(); ++i) {
279 ciType* type = vt->field_type(i);
280 Node* value = vt->field_value(i);
281 if (type->is_valuetype()) {
282 // Handle flattened value type fields recursively
283 value = value->as_ValueType()->clone_with_phis(gvn, region);
284 } else {
285 const Type* phi_type = Type::get_const_type(type);
286 value = PhiNode::make(region, value, phi_type);
287 gvn->set_type(value, phi_type);
288 }
289 vt->set_field_value(i, value);
290 }
291 gvn->set_type(vt, vt->bottom_type());
292 return vt;
293 }
294
295 // Checks if the inputs of the ValueBaseTypeNode were replaced by PhiNodes
296 // for the given region (see ValueBaseTypeNode::clone_with_phis).
297 bool ValueTypeBaseNode::has_phi_inputs(Node* region) {
298 // Check oop input
299 bool result = get_oop()->is_Phi() && get_oop()->as_Phi()->region() == region;
300 #ifdef ASSERT
301 if (result) {
302 // Check all field value inputs for consistency
303 for (uint i = Oop; i < field_count(); ++i) {
304 Node* n = in(i);
305 if (n->is_ValueTypeBase()) {
306 assert(n->as_ValueTypeBase()->has_phi_inputs(region), "inconsistent phi inputs");
307 } else {
308 assert(n->is_Phi() && n->as_Phi()->region() == region, "inconsistent phi inputs");
309 }
310 }
311 }
312 #endif
313 return result;
314 }
315
316 // Merges 'this' with 'other' by updating the input PhiNodes added by 'clone_with_phis'
317 ValueTypeBaseNode* ValueTypeBaseNode::merge_with(PhaseGVN* gvn, const ValueTypeBaseNode* other, int pnum, bool transform) {
318 // Merge oop inputs
319 PhiNode* phi = get_oop()->as_Phi();
320 phi->set_req(pnum, other->get_oop());
321 if (transform) {
322 set_oop(gvn->transform(phi));
323 gvn->record_for_igvn(phi);
324 }
325 // Merge field values
326 for (uint i = 0; i < field_count(); ++i) {
327 Node* val1 = field_value(i);
328 Node* val2 = other->field_value(i);
329 if (val1->isa_ValueType()) {
330 val1->as_ValueType()->merge_with(gvn, val2->as_ValueType(), pnum, transform);
331 } else {
332 assert(val1->is_Phi(), "must be a phi node");
333 assert(!val2->is_ValueType(), "inconsistent merge values");
334 val1->set_req(pnum, val2);
335 }
336 if (transform) {
337 set_field_value(i, gvn->transform(val1));
338 gvn->record_for_igvn(val1);
339 }
340 }
341 return this;
342 }
343
344 Node* ValueTypeBaseNode::field_value(uint index) const {
345 assert(index < field_count(), "index out of bounds");
346 return in(Values + index);
347 }
348
349 // Get the value of the field at the given offset.
350 // If 'recursive' is true, flattened value type fields will be resolved recursively.
351 Node* ValueTypeBaseNode::field_value_by_offset(int offset, bool recursive) const {
352 // If the field at 'offset' belongs to a flattened value type field, 'index' refers to the
353 // corresponding ValueTypeNode input and 'sub_offset' is the offset in flattened value type.
354 int index = value_klass()->field_index_by_offset(offset);
355 int sub_offset = offset - field_offset(index);
356 Node* value = field_value(index);
357 if (recursive && value->is_ValueType()) {
358 // Flattened value type field
359 ValueTypeNode* vt = value->as_ValueType();
360 sub_offset += vt->value_klass()->first_field_offset(); // Add header size
361 return vt->field_value_by_offset(sub_offset);
362 }
363 assert(!(recursive && value->is_ValueType()), "should not be a value type");
364 assert(sub_offset == 0, "offset mismatch");
365 return value;
366 }
367
368 void ValueTypeBaseNode::set_field_value(uint index, Node* value) {
369 assert(index < field_count(), "index out of bounds");
370 set_req(Values + index, value);
371 }
372
373 int ValueTypeBaseNode::field_offset(uint index) const {
374 assert(index < field_count(), "index out of bounds");
375 return value_klass()->field_offset_by_index(index);
376 }
377
378 ciType* ValueTypeBaseNode::field_type(uint index) const {
379 assert(index < field_count(), "index out of bounds");
380 return value_klass()->field_type_by_index(index);
381 }
382
383 int ValueTypeBaseNode::make_scalar_in_safepoint(SafePointNode* sfpt, Node* root, PhaseGVN* gvn) {
384 ciValueKlass* vk = value_klass();
385 uint nfields = vk->flattened_field_count();
386 JVMState* jvms = sfpt->jvms();
387 int start = jvms->debug_start();
388 int end = jvms->debug_end();
389 // Replace safepoint edge by SafePointScalarObjectNode and add field values
390 assert(jvms != NULL, "missing JVMS");
391 uint first_ind = (sfpt->req() - jvms->scloff());
392 const TypeValueTypePtr* res_type = value_type_ptr();
393 SafePointScalarObjectNode* sobj = new SafePointScalarObjectNode(res_type,
394 #ifdef ASSERT
395 NULL,
396 #endif
397 first_ind, nfields);
398 sobj->init_req(0, root);
399 // Iterate over the value type fields in order of increasing
400 // offset and add the field values to the safepoint.
401 for (uint j = 0; j < nfields; ++j) {
402 int offset = vk->nonstatic_field_at(j)->offset();
403 Node* value = field_value_by_offset(offset, true /* include flattened value type fields */);
404 sfpt->add_req(value);
405 }
406 jvms->set_endoff(sfpt->req());
407 if (gvn != NULL) {
408 sobj = gvn->transform(sobj)->as_SafePointScalarObject();
409 }
410 return sfpt->replace_edges_in_range(this, sobj, start, end);
411 }
412
413 void ValueTypeNode::make_scalar_in_safepoints(Compile* C) {
414 for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
415 Node* u = fast_out(i);
416 if (u->is_SafePoint() && (!u->is_Call() || u->as_Call()->has_debug_use(this))) {
417 SafePointNode* sfpt = u->as_SafePoint();
418 Node* in_oop = get_oop();
419 const Type* oop_type = in_oop->bottom_type();
420 assert(TypePtr::NULL_PTR->higher_equal(oop_type), "already heap allocated value type should be linked directly");
421 int nb = make_scalar_in_safepoint(sfpt, C->root(), NULL);
422 --i; imax -= nb;
423 }
424 }
425 }
426
427 void ValueTypeNode::pass_klass(Node* n, uint pos, const GraphKit& kit) {
428 ciValueKlass* vk = value_klass();
429 const TypeKlassPtr* tk = TypeKlassPtr::make(vk);
430 Node* arg = kit.makecon(tk);
431 n->init_req(pos, arg);
432 }
433
434 uint ValueTypeNode::pass_fields(Node* n, int base_input, const GraphKit& kit, ciValueKlass* base_vk, int base_offset) {
435 ciValueKlass* vk = value_klass();
436 if (base_vk == NULL) {
437 base_vk = vk;
438 }
439 uint edges = 0;
440 for (uint i = 0; i < field_count(); i++) {
441 ciType* f_type = field_type(i);
|