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