< prev index next >

src/share/vm/opto/valuetypenode.cpp

Print this page




  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


  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 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 Node* ValueTypeNode::Ideal(PhaseGVN* phase, bool can_reshape) {
 305   // No optimizations for now
 306   return NULL;
 307 }
 308 
 309 #ifndef PRODUCT
 310 
 311 void ValueTypeNode::dump_spec(outputStream* st) const {
 312   TypeNode::dump_spec(st);



 313 }
 314 
 315 #endif
< prev index next >