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 = Oop; i < field_count(); ++i) {
 252       Node* n = in(i);
 253       if (n->is_ValueType()) {
 254         assert(n->as_ValueType()->has_phi_inputs(region), "inconsistent phi inputs");
 255       } else {
 256         assert(n->is_Phi() && n->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 ValueTypeNode* ValueTypeNode::merge_with(PhaseGVN* gvn, const ValueTypeNode* other, int pnum, bool transform) {
 266   // Merge oop inputs
 267   PhiNode* phi = get_oop()->as_Phi();
 268   phi->set_req(pnum, other->get_oop());
 269   if (transform) {
 270     set_oop(gvn->transform(phi));
 271     gvn->record_for_igvn(phi);
 272   }
 273   // Merge field values
 274   for (uint i = 0; i < field_count(); ++i) {
 275     Node* val1 =        field_value(i);
 276     Node* val2 = other->field_value(i);
 277     if (val1->isa_ValueType()) {
 278       val1->as_ValueType()->merge_with(gvn, val2->as_ValueType(), pnum, transform);
 279     } else {
 280       assert(val1->is_Phi(), "must be a phi node");
 281       assert(!val2->is_ValueType(), "inconsistent merge values");
 282       val1->set_req(pnum, val2);
 283     }
 284     if (transform) {
 285       set_field_value(i, gvn->transform(val1));
 286       gvn->record_for_igvn(val1);
 287     }
 288   }
 289   return this;
 290 }
 291 
 292 Node* ValueTypeNode::field_value(uint index) const {
 293   assert(index < field_count(), "index out of bounds");
 294   return in(Values + index);
 295 }
 296 
 297 // Get the value of the field at the given offset.
 298 // If 'recursive' is true, flattened value type fields will be resolved recursively.
 299 Node* ValueTypeNode::field_value_by_offset(int offset, bool recursive) const {
 300   // If the field at 'offset' belongs to a flattened value type field, 'index' refers to the
 301   // corresponding ValueTypeNode input and 'sub_offset' is the offset in flattened value type.
 302   int index = value_klass()->field_index_by_offset(offset);
 303   int sub_offset = offset - field_offset(index);
 304   Node* value = field_value(index);
 305   if (recursive && value->is_ValueType()) {
 306     // Flattened value type field
 307     ValueTypeNode* vt = value->as_ValueType();
 308     sub_offset += vt->value_klass()->first_field_offset(); // Add header size
 309     return vt->field_value_by_offset(sub_offset);
 310   }
 311   assert(!(recursive && value->is_ValueType()), "should not be a value type");
 312   assert(sub_offset == 0, "offset mismatch");
 313   return value;
 314 }
 315 
 316 void ValueTypeNode::set_field_value(uint index, Node* value) {
 317   assert(index < field_count(), "index out of bounds");
 318   set_req(Values + index, value);
 319 }
 320 
 321 int ValueTypeNode::field_offset(uint index) const {
 322   assert(index < field_count(), "index out of bounds");
 323   return value_klass()->field_offset_by_index(index);
 324 }
 325 
 326 ciType* ValueTypeNode::field_type(uint index) const {
 327   assert(index < field_count(), "index out of bounds");
 328   return value_klass()->field_type_by_index(index);
 329 }
 330 
 331 void ValueTypeNode::make_scalar_in_safepoints(Compile* C) {
 332   const TypeValueTypePtr* res_type = TypeValueTypePtr::make(bottom_type()->isa_valuetype(), TypePtr::NotNull);
 333   ciValueKlass* vk = value_klass();
 334   uint nfields = vk->flattened_field_count();
 335   for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
 336     Node* u = fast_out(i);
 337     if (u->is_SafePoint() && (!u->is_Call() || u->as_Call()->has_debug_use(this))) {
 338       Node* in_oop = get_oop();
 339       const Type* oop_type = in_oop->bottom_type();
 340       SafePointNode* sfpt = u->as_SafePoint();
 341       JVMState* jvms = sfpt->jvms();
 342       int start = jvms->debug_start();
 343       int end   = jvms->debug_end();
 344       assert(TypePtr::NULL_PTR->higher_equal(oop_type), "already heap allocated value type should be linked directly");
 345       // Replace safepoint edge by SafePointScalarObjectNode and add field values
 346       assert(jvms != NULL, "missing JVMS");
 347       uint first_ind = (sfpt->req() - jvms->scloff());
 348       SafePointScalarObjectNode* sobj = new SafePointScalarObjectNode(res_type,
 349 #ifdef ASSERT
 350                                                                       NULL,
 351 #endif
 352                                                                       first_ind, nfields);
 353       sobj->init_req(0, C->root());
 354       // Iterate over the value type fields in order of increasing
 355       // offset and add the field values to the safepoint.
 356       for (uint j = 0; j < nfields; ++j) {
 357         int offset = vk->nonstatic_field_at(j)->offset();
 358         Node* value = field_value_by_offset(offset, true /* include flattened value type fields */);
 359         sfpt->add_req(value);
 360       }
 361       jvms->set_endoff(sfpt->req());
 362       int nb = sfpt->replace_edges_in_range(this, sobj, start, end);
 363       --i; imax -= nb;
 364     }
 365   }
 366 }
 367 
 368 uint ValueTypeNode::set_arguments_for_java_call(CallJavaNode* call, int base_input, const GraphKit& kit, ciValueKlass* base_vk, int base_offset) {
 369   ciValueKlass* vk = value_klass();
 370   if (base_vk == NULL) {
 371     base_vk = vk;
 372   }
 373   uint edges = 0;
 374   for (uint i = 0; i < field_count(); i++) {
 375     ciType* f_type = field_type(i);
 376     int offset = base_offset + field_offset(i) - (base_offset > 0 ? vk->first_field_offset() : 0);
 377     Node* arg = field_value(i);
 378     if (f_type->is_valuetype()) {
 379       ciValueKlass* embedded_vk = f_type->as_value_klass();
 380       edges += arg->as_ValueType()->set_arguments_for_java_call(call, base_input, kit, base_vk, offset);
 381     } else {
 382       int j = 0; int extra = 0;
 383       for (; j < base_vk->nof_nonstatic_fields(); j++) {
 384         ciField* f = base_vk->nonstatic_field_at(j);
 385         if (offset == f->offset()) {
 386           assert(f->type() == f_type, "inconsistent field type");
 387           break;
 388         }
 389         BasicType bt = f->type()->basic_type();
 390         if (bt == T_LONG || bt == T_DOUBLE) {
 391           extra++;
 392         }
 393       }
 394       call->init_req(base_input + j + extra, arg);
 395       edges++;
 396       BasicType bt = f_type->basic_type();
 397       if (bt == T_LONG || bt == T_DOUBLE) {
 398         call->init_req(base_input + j + extra + 1, kit.top());
 399         edges++;
 400       }
 401     }
 402   }
 403   return edges;
 404 }
 405 
 406 Node* ValueTypeNode::Ideal(PhaseGVN* phase, bool can_reshape) {
 407   if (can_reshape) {
 408     PhaseIterGVN* igvn = phase->is_IterGVN();
 409     const Type* oop_type = igvn->type(get_oop());
 410     if (oop_type->meet(TypePtr::NULL_PTR) != oop_type) {
 411       // Value type is heap allocated, search for safepoint uses
 412       for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
 413         Node* out = fast_out(i);
 414         if (out->is_SafePoint()) {
 415           // Let SafePointNode::Ideal() take care of re-wiring the
 416           // safepoint to the oop input instead of the value type node.
 417           igvn->rehash_node_delayed(out);
 418         }
 419       }
 420     }
 421   }
 422 
 423   return NULL;
 424 }
 425 
 426 #ifndef PRODUCT
 427 
 428 void ValueTypeNode::dump_spec(outputStream* st) const {
 429   TypeNode::dump_spec(st);
 430 }
 431 
 432 #endif