< prev index next >

src/share/vm/opto/valuetypenode.cpp

Print this page




  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 + 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* f_type = field_type(i);

  68     if (f_type->is_valuetype()) {



  69       // Recursively load the flattened value type field
  70       value = ValueTypeNode::make(gvn, f_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(f_type), f_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 + 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 = 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, 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;




  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_default(PhaseGVN& gvn, ciValueKlass* vk) {
  39   // TODO re-use constant oop of pre-allocated default value type here?
  40   // Create a new ValueTypeNode with default values
  41   ValueTypeNode* vt = ValueTypeNode::make(gvn, vk);
  42   for (uint i = 0; i < vt->field_count(); ++i) {
  43     ciType* field_type = vt->field_type(i);
  44     Node* value = NULL;
  45     if (field_type->is_primitive_type()) {
  46       value = gvn.zerocon(field_type->basic_type());
  47     } else {
  48       value = ValueTypeNode::make_default(gvn, field_type->as_value_klass());
  49     }
  50     vt->set_field_value(i, value);
  51   }
  52   return gvn.transform(vt);
  53 }
  54 
  55 Node* ValueTypeNode::make(PhaseGVN& gvn, Node* mem, Node* oop) {
  56   // Create and initialize a ValueTypeNode by loading all field
  57   // values from a heap-allocated version and also save the oop.
  58   const TypeValueTypePtr* vtptr = gvn.type(oop)->is_valuetypeptr();
  59   ValueTypeNode* vt = new ValueTypeNode(vtptr->value_type(), oop);
  60   vt->load_values(gvn, mem, oop, oop);
  61   return gvn.transform(vt);
  62 }
  63 
  64 Node* ValueTypeNode::make(PhaseGVN& gvn, ciValueKlass* vk, Node* mem, Node* obj, Node* ptr, ciKlass* holder, int field_offset) {
  65   // Create and initialize a ValueTypeNode by loading all field values from
  66   // a flattened value type field at 'field_offset' or from a value type array.
  67   ValueTypeNode* vt = make(gvn, vk);
  68   int base_offset = 0;
  69   if (holder->is_value_array_klass()) {
  70     assert(field_offset == 0, "field offset not supported for arrays");
  71   } else {
  72     // The value type is flattened into the object without an oop header. Subtract the
  73     // offset of the first field to account for the missing header when loading the values.
  74     base_offset = field_offset - vk->first_field_offset();
  75   }
  76   vt->load_values(gvn, mem, obj, ptr, holder, base_offset);
  77   return gvn.transform(vt);
  78 }
  79 
  80 void ValueTypeNode::load_values(PhaseGVN& gvn, Node* mem, Node* base, Node* ptr, ciKlass* holder, int f_offset) {
  81   ciInstanceKlass* lookup;
  82   if (holder) {
  83     // Flattened
  84     if (holder->is_value_array_klass()) {
  85       lookup = value_klass();
  86     } else {
  87       lookup = holder->as_instance_klass();
  88     }
  89   } else {
  90     // Not flattened
  91     assert(f_offset == 0, "must be");
  92     lookup = value_klass();
  93   }
  94   // Initialize the value type by loading its field values from
  95   // memory and adding the values as input edges to the node.
  96   for (uint i = 0; i < field_count(); ++i) {
  97     int offset = f_offset + field_offset(i);
  98     ciField* field = lookup->get_field_by_offset(offset, false);



  99     ciType* f_type = field_type(i);
 100     Node* value = NULL;
 101     if (f_type->is_valuetype()) {
 102       if (holder && holder->is_value_array_klass()) {
 103         offset -= value_klass()->first_field_offset();
 104       }
 105       // Recursively load the flattened value type field
 106       value = ValueTypeNode::make(gvn, f_type->as_value_klass(), mem, base, ptr, lookup, offset);
 107     } else {
 108       const Type* con_type = NULL;
 109       if (base->is_Con()) {
 110         // If the oop to the value type is constant (static final field), we can
 111         // also treat the fields as constants because the value type is immutable.
 112         const TypeOopPtr* oop_ptr = base->bottom_type()->isa_oopptr();
 113         ciObject* constant_oop = oop_ptr->const_oop();
 114         ciConstant constant = constant_oop->as_instance()->field_value(field);
 115         con_type = Type::make_from_constant(constant, /*require_const=*/ true);
 116       }
 117       if (con_type != NULL) {
 118         // Found a constant field value
 119         value = gvn.makecon(con_type);
 120       } else {
 121         // Load field value from memory
 122         const Type* base_type = gvn.type(base);
 123         const TypePtr* adr_type = NULL;
 124         if (base_type->isa_aryptr()) {
 125           adr_type = base_type->is_aryptr()->add_offset(Type::OffsetBot);
 126         } else {
 127           adr_type = gvn.C->alias_type(field)->adr_type();
 128         }
 129         if (holder && holder->is_value_array_klass()) {
 130           offset -= value_klass()->first_field_offset();
 131         }
 132         Node* adr = gvn.transform(new AddPNode(base, ptr, gvn.MakeConX(offset)));
 133         value = LoadNode::make(gvn, NULL, mem, adr, adr_type, Type::get_const_type(f_type), f_type->basic_type(), MemNode::unordered);
 134       }
 135     }
 136     set_field_value(i, gvn.transform(value));
 137   }
 138 }
 139 
 140 void ValueTypeNode::store_to_field(GraphKit* kit, Node* obj, Node* ptr, ciInstanceKlass* instance_type, int field_offset) const {
 141   // The value type is embedded into the object without an oop header. Subtract the
 142   // offset of the first field to account for the missing header when storing the values.
 143   int base_offset = field_offset - value_klass()->first_field_offset();
 144   store_values(kit, obj, ptr, instance_type, base_offset);
 145 }
 146 
 147 void ValueTypeNode::store_values(GraphKit* kit, Node* base, Node* ptr, ciKlass* holder, int holder_offset) const {
 148   ciInstanceKlass* lookup;
 149   if (holder) {
 150     // flattened
 151     if (holder->is_value_array_klass()) {
 152       assert(holder_offset == 0, "must be");
 153       lookup = value_klass();
 154     } else {
 155       lookup = holder->as_instance_klass();
 156     }
 157   } else {
 158     // not flattened
 159     assert(holder_offset == 0, "must be");
 160     lookup = value_klass();
 161   }
 162   // Write field values to memory
 163   for (uint i = 0; i < field_count(); ++i) {
 164     int offset = holder_offset + field_offset(i);



 165     Node* value = field_value(i);
 166     if (value->is_ValueType()) {
 167       // Recursively store the flattened value type field
 168       if (holder && holder->is_value_array_klass()) {
 169         offset -= value_klass()->first_field_offset();
 170       }
 171       value->isa_ValueType()->store_to_field(kit, base, ptr, lookup, offset);
 172     } else {
 173       const Type* base_type = kit->gvn().type(base);
 174       const TypePtr* adr_type = NULL;
 175       if (base_type->isa_aryptr()) {
 176         adr_type = base_type->is_aryptr()->add_offset(Type::OffsetBot);
 177       } else {
 178         ciField* field = lookup->get_field_by_offset(offset, false);
 179         adr_type = kit->gvn().C->alias_type(field)->adr_type();
 180       }
 181       if (holder && holder->is_value_array_klass()) {
 182         offset -= value_klass()->first_field_offset();
 183       }
 184       Node* adr = kit->basic_plus_adr(base, ptr, offset);
 185       kit->store_to_memory(kit->control(), adr, value, field_type(i)->basic_type(), adr_type, MemNode::unordered);
 186     }
 187   }
 188 }
 189 
 190 Node* ValueTypeNode::store_to_memory(GraphKit* kit) {
 191   Node* in_oop = get_oop();
 192   Node* null_ctl = kit->top();
 193   // Check if value type is already allocated
 194   Node* not_null_oop = kit->null_check_oop(in_oop, &null_ctl);
 195   if (null_ctl->is_top()) {
 196     // Value type is allocated
 197     return not_null_oop;
 198   }
 199   // Not able to prove that value type is allocated.
 200   // Emit runtime check that may be folded later.
 201   const Type* oop_type = kit->gvn().type(in_oop);
 202   assert(TypePtr::NULL_PTR->higher_equal(oop_type), "should not be allocated");
 203 
 204   const TypeValueTypePtr* vtptr_type = TypeValueTypePtr::make(bottom_type()->isa_valuetype(), TypePtr::NotNull);
 205   RegionNode* region = new RegionNode(3);
 206   PhiNode* oop = new PhiNode(region, vtptr_type);
 207   PhiNode* io  = new PhiNode(region, Type::ABIO);
 208   PhiNode* mem = new PhiNode(region, Type::MEMORY, TypePtr::BOTTOM);
 209 
 210   // Oop is non-NULL, use it
 211   region->init_req(1, kit->control());
 212   // Fixme if we cast oop to not null we fail if the control path is not folded
 213   // castnode.cpp:69: #  assert(ft == Type::TOP) failed: special case #3
 214   //oop   ->init_req(1, not_null_oop);
 215   oop   ->init_req(1, in_oop);
 216   io    ->init_req(1, kit->i_o());
 217   mem   ->init_req(1, kit->merged_memory());
 218 
 219   // Oop is NULL, allocate value type
 220   kit->set_control(null_ctl);
 221   kit->kill_dead_locals();
 222   Node* klass_node = kit->makecon(TypeKlassPtr::make(value_klass()));
 223   Node* alloc_oop  = kit->new_instance(klass_node);
 224   AllocateNode* alloc = AllocateNode::Ideal_allocation(alloc_oop, &kit->gvn());
 225   // TODO enable/fix this
 226   // alloc->initialization()->set_complete_with_arraycopy();
 227   // Write field values to memory
 228   store_values(kit, alloc_oop, alloc_oop);
 229   region->init_req(2, kit->control());
 230   oop   ->init_req(2, alloc_oop);
 231   io    ->init_req(2, kit->i_o());
 232   mem   ->init_req(2, kit->merged_memory());
 233 
 234   // Update GraphKit
 235   kit->set_control(kit->gvn().transform(region));
 236   kit->set_i_o(kit->gvn().transform(io));
 237   kit->set_all_memory(kit->gvn().transform(mem));
 238   kit->record_for_igvn(region);
 239   kit->record_for_igvn(oop);
 240   kit->record_for_igvn(io);
 241   kit->record_for_igvn(mem);
 242 
 243   // Use cloned ValueTypeNode to propagate oop from now on
 244   Node* res_oop = kit->gvn().transform(oop);
 245   ValueTypeNode* vt = clone()->as_ValueType();
 246   vt->set_oop(res_oop);
 247   kit->replace_in_map(this, kit->gvn().transform(vt));
 248   return res_oop;


< prev index next >