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_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;
 249 }
 250 
 251 // Clones the values type to handle control flow merges involving multiple value types.
 252 // The inputs are replaced by PhiNodes to represent the merged values for the given region.
 253 ValueTypeNode* ValueTypeNode::clone_with_phis(PhaseGVN& gvn, Node* region) {
 254   ValueTypeNode* vt = clone()->as_ValueType();
 255 
 256   // Create a PhiNode for merging the oop values
 257   const TypeValueTypePtr* vtptr = TypeValueTypePtr::make(vt->bottom_type()->isa_valuetype());
 258   PhiNode* oop = PhiNode::make(region, vt->get_oop(), vtptr);
 259   gvn.set_type(oop, vtptr);
 260   vt->set_oop(oop);
 261 
 262   // Create a PhiNode each for merging the field values
 263   for (uint i = 0; i < vt->field_count(); ++i) {
 264     ciType* type = vt->field_type(i);
 265     Node*  value = vt->field_value(i);
 266     if (type->is_valuetype()) {
 267       // Handle flattened value type fields recursively
 268       value = value->as_ValueType()->clone_with_phis(gvn, region);
 269     } else {
 270       const Type* phi_type = Type::get_const_type(type);
 271       value = PhiNode::make(region, value, phi_type);
 272       gvn.set_type(value, phi_type);
 273     }
 274     vt->set_field_value(i, value);
 275   }
 276   gvn.set_type(vt, vt->bottom_type());
 277   return vt;
 278 }
 279 
 280 // Checks if the inputs of the ValueTypeNode were replaced by PhiNodes
 281 // for the given region (see ValueTypeNode::clone_with_phis).
 282 bool ValueTypeNode::has_phi_inputs(Node* region) {
 283   // Check oop input
 284   bool result = get_oop()->is_Phi() && get_oop()->as_Phi()->region() == region;
 285 #ifdef ASSERT
 286   if (result) {
 287     // Check all field value inputs for consistency
 288     for (uint i = 0; i < field_count(); ++i) {
 289       Node* value = field_value(i);
 290       if (value->is_ValueType()) {
 291         assert(value->as_ValueType()->has_phi_inputs(region), "inconsistent phi inputs");
 292       } else {
 293         assert(value->is_Phi() && value->as_Phi()->region() == region, "inconsistent phi inputs");
 294       }
 295     }
 296   }
 297 #endif
 298   return result;
 299 }
 300 
 301 // Merges 'this' with 'other' by updating the input PhiNodes added by 'clone_with_phis'
 302 Node* ValueTypeNode::merge_with(GraphKit* kit, const ValueTypeNode* other, int pnum) {
 303   // Merge oop inputs
 304   PhiNode* phi = get_oop()->as_Phi();
 305   phi->set_req(pnum, other->get_oop());
 306   if (pnum == PhiNode::Input) {
 307     // Last merge
 308     set_oop(kit->gvn().transform_no_reclaim(phi));
 309     kit->record_for_igvn(phi);
 310   }
 311   // Merge field values
 312   for (uint i = 0; i < field_count(); ++i) {
 313     Node* val1 =        field_value(i);
 314     Node* val2 = other->field_value(i);
 315     if (val1->isa_ValueType()) {
 316       val1->as_ValueType()->merge_with(kit, val2->as_ValueType(), pnum);
 317     } else {
 318       assert(!val2->is_ValueType(), "inconsistent merge values");
 319       val1->set_req(pnum, val2);
 320     }
 321     if (pnum == PhiNode::Input) {
 322       // Last merge
 323       set_field_value(i, kit->gvn().transform_no_reclaim(val1));
 324       kit->record_for_igvn(val1);
 325     }
 326   }
 327   if (pnum == PhiNode::Input) {
 328     // Last merge for this value type.
 329    kit->record_for_igvn(this);
 330    return kit->gvn().transform_no_reclaim(this);
 331   }
 332   return this;
 333 }
 334 
 335 Node* ValueTypeNode::field_value(uint index) const {
 336   assert(index < field_count(), "index out of bounds");
 337   return in(Values + index);
 338 }
 339 
 340 // Get the value of the field at the given offset.
 341 // If 'recursive' is true, flattened value type fields will be resolved recursively.
 342 Node* ValueTypeNode::field_value_by_offset(int offset, bool recursive) const {
 343   // If the field at 'offset' belongs to a flattened value type field, 'index' refers to the
 344   // corresponding ValueTypeNode input and 'sub_offset' is the offset in flattened value type.
 345   int index = value_klass()->field_index_by_offset(offset);
 346   int sub_offset = offset - field_offset(index);
 347   Node* value = field_value(index);
 348   if (recursive && value->is_ValueType()) {
 349     // Flattened value type field
 350     ValueTypeNode* vt = value->as_ValueType();
 351     sub_offset += vt->value_klass()->first_field_offset(); // Add header size
 352     return vt->field_value_by_offset(sub_offset);
 353   }
 354   assert(!(recursive && value->is_ValueType()), "should not be a value type");
 355   assert(sub_offset == 0, "offset mismatch");
 356   return value;
 357 }
 358 
 359 void ValueTypeNode::set_field_value(uint index, Node* value) {
 360   assert(index < field_count(), "index out of bounds");
 361   set_req(Values + index, value);
 362 }
 363 
 364 int ValueTypeNode::field_offset(uint index) const {
 365   assert(index < field_count(), "index out of bounds");
 366   return value_klass()->field_offset_by_index(index);
 367 }
 368 
 369 ciType* ValueTypeNode::field_type(uint index) const {
 370   assert(index < field_count(), "index out of bounds");
 371   return value_klass()->field_type_by_index(index);
 372 }
 373 
 374 void ValueTypeNode::make_scalar_in_safepoints(Compile* C) {
 375   const TypeValueTypePtr* res_type = TypeValueTypePtr::make(bottom_type()->isa_valuetype(), TypePtr::NotNull);
 376   ciValueKlass* vk = value_klass();
 377   uint nfields = vk->flattened_field_count();
 378   for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
 379     Node* u = fast_out(i);
 380     if (u->is_SafePoint() && (!u->is_Call() || u->as_Call()->has_debug_use(this))) {
 381       Node* in_oop = get_oop();
 382       const Type* oop_type = in_oop->bottom_type();
 383       SafePointNode* sfpt = u->as_SafePoint();
 384       JVMState* jvms = sfpt->jvms();
 385       int start = jvms->debug_start();
 386       int end   = jvms->debug_end();
 387       if (oop_type->meet(TypePtr::NULL_PTR) != oop_type) {
 388         // Replace safepoint edge by oop
 389         int nb = sfpt->replace_edges_in_range(this, in_oop, start, end);
 390         --i; imax -= nb;
 391       } else {
 392         // Replace safepoint edge by SafePointScalarObjectNode and add field values
 393         assert(jvms != NULL, "missing JVMS");
 394         uint first_ind = (sfpt->req() - jvms->scloff());
 395         SafePointScalarObjectNode* sobj = new SafePointScalarObjectNode(res_type,
 396 #ifdef ASSERT
 397                                                                         NULL,
 398 #endif
 399                                                                         first_ind, nfields);
 400         sobj->init_req(0, C->root());
 401         // Iterate over the value type fields in order of increasing
 402         // offset and add the field values to the safepoint.
 403         for (uint j = 0; j < nfields; ++j) {
 404           int offset = vk->nonstatic_field_at(j)->offset();
 405           Node* value = field_value_by_offset(offset, true /* include flattened value type fields */);
 406           sfpt->add_req(value);
 407         }
 408         jvms->set_endoff(sfpt->req());
 409         int nb = sfpt->replace_edges_in_range(this, sobj, start, end);
 410         --i; imax -= nb;
 411       }
 412     }
 413   }
 414 }
 415 
 416 uint ValueTypeNode::set_arguments_for_java_call(CallJavaNode* call, int base_input, const GraphKit& kit, ciValueKlass* base_vk, int base_offset) {
 417   ciValueKlass* vk = value_klass();
 418   if (base_vk == NULL) {
 419     base_vk = vk;
 420   }
 421   uint edges = 0;
 422   for (uint i = 0; i < field_count(); i++) {
 423     ciType* f_type = field_type(i);
 424     int offset = base_offset + field_offset(i) - (base_offset > 0 ? vk->first_field_offset() : 0);
 425     Node* arg = field_value(i);
 426     if (f_type->is_valuetype()) {
 427       ciValueKlass* embedded_vk = f_type->as_value_klass();
 428       edges += arg->as_ValueType()->set_arguments_for_java_call(call, base_input, kit, base_vk, offset);
 429     } else {
 430       int j = 0; int extra = 0;
 431       for (; j < base_vk->nof_nonstatic_fields(); j++) {
 432         ciField* f = base_vk->nonstatic_field_at(j);
 433         if (offset == f->offset()) {
 434           assert(f->type() == f_type, "inconsistent field type");
 435           break;
 436         }
 437         BasicType bt = f->type()->basic_type();
 438         if (bt == T_LONG || bt == T_DOUBLE) {
 439           extra++;
 440         }
 441       }
 442       call->init_req(base_input + j + extra, arg);
 443       edges++;
 444       BasicType bt = f_type->basic_type();
 445       if (bt == T_LONG || bt == T_DOUBLE) {
 446         call->init_req(base_input + j + extra + 1, kit.top());
 447         edges++;
 448       }
 449     }
 450   }
 451   return edges;
 452 }
 453 
 454 Node* ValueTypeNode::Ideal(PhaseGVN* phase, bool can_reshape) {
 455   // No optimizations for now
 456   return NULL;
 457 }
 458 
 459 #ifndef PRODUCT
 460 
 461 void ValueTypeNode::dump_spec(outputStream* st) const {
 462   TypeNode::dump_spec(st);
 463 }
 464 
 465 #endif