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_valuetype()) {
  47       value = ValueTypeNode::make_default(gvn, field_type->as_value_klass());
  48     } else {
  49       value = gvn.zerocon(field_type->basic_type());
  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         BasicType bt = type2field[ftype->basic_type()];
 114         value = LoadNode::make(gvn, NULL, mem, adr, adr_type, Type::get_const_type(ftype), bt, MemNode::unordered);
 115       }
 116     }
 117     set_field_value(i, gvn.transform(value));
 118   }
 119 }
 120 
 121 void ValueTypeNode::store(GraphKit* kit, Node* obj, Node* ptr, ciInstanceKlass* holder, int holder_offset) const {
 122   // The value type is embedded into the object without an oop header. Subtract the
 123   // offset of the first field to account for the missing header when storing the values.
 124   holder_offset -= value_klass()->first_field_offset();
 125   store_values(kit, obj, ptr, holder, holder_offset);
 126 }
 127 
 128 void ValueTypeNode::store_values(GraphKit* kit, Node* base, Node* ptr, ciInstanceKlass* holder, int holder_offset) const {
 129   // Write field values to memory
 130   for (uint i = 0; i < field_count(); ++i) {
 131     int offset = holder_offset + field_offset(i);
 132     Node* value = field_value(i);
 133     if (value->is_ValueType()) {
 134       // Recursively store the flattened value type field
 135       value->isa_ValueType()->store(kit, base, ptr, holder, offset);
 136     } else {
 137       const Type* base_type = kit->gvn().type(base);
 138       const TypePtr* adr_type = NULL;
 139       if (base_type->isa_aryptr()) {
 140         // In the case of a flattened value type array, each field has its own slice
 141         adr_type = base_type->is_aryptr()->with_field_offset(offset)->add_offset(Type::OffsetBot);
 142       } else {
 143         ciField* field = holder->get_field_by_offset(offset, false);
 144         adr_type = kit->C->alias_type(field)->adr_type();
 145       }
 146       Node* adr = kit->basic_plus_adr(base, ptr, offset);
 147       BasicType bt = type2field[field_type(i)->basic_type()];
 148       if (is_java_primitive(bt)) {
 149         kit->store_to_memory(kit->control(), adr, value, bt, adr_type, MemNode::unordered);
 150       } else {
 151         const TypeOopPtr* ft = TypeOopPtr::make_from_klass(field_type(i)->as_klass());
 152         assert(adr->bottom_type()->is_ptr_to_narrowoop() == UseCompressedOops, "inconsistent");
 153         bool is_array = base_type->isa_aryptr() != NULL;
 154         kit->store_oop(kit->control(), base, adr, adr_type, value, ft, bt, is_array, MemNode::unordered);
 155       }
 156 
 157     }
 158   }
 159 }
 160 
 161 Node* ValueTypeNode::store_to_memory(GraphKit* kit) {
 162   Node* in_oop = get_oop();
 163   Node* null_ctl = kit->top();
 164   // Check if value type is already allocated
 165   Node* not_null_oop = kit->null_check_oop(in_oop, &null_ctl);
 166   if (null_ctl->is_top()) {
 167     // Value type is allocated
 168     return not_null_oop;
 169   }
 170   // Not able to prove that value type is allocated.
 171   // Emit runtime check that may be folded later.
 172   const Type* oop_type = kit->gvn().type(in_oop);
 173   assert(TypePtr::NULL_PTR->higher_equal(oop_type), "should not be allocated");
 174 
 175   const TypeValueTypePtr* vtptr_type = TypeValueTypePtr::make(bottom_type()->isa_valuetype(), TypePtr::NotNull);
 176   RegionNode* region = new RegionNode(3);
 177   PhiNode* oop = new PhiNode(region, vtptr_type);
 178   PhiNode* io  = new PhiNode(region, Type::ABIO);
 179   PhiNode* mem = new PhiNode(region, Type::MEMORY, TypePtr::BOTTOM);
 180 
 181   // Oop is non-NULL, use it
 182   region->init_req(1, kit->control());
 183   oop   ->init_req(1, not_null_oop);
 184   io    ->init_req(1, kit->i_o());
 185   mem   ->init_req(1, kit->merged_memory());
 186 
 187   // Oop is NULL, allocate value type
 188   kit->set_control(null_ctl);
 189   kit->kill_dead_locals();
 190   ciValueKlass* vk = value_klass();
 191   Node* klass_node = kit->makecon(TypeKlassPtr::make(vk));
 192   Node* alloc_oop  = kit->new_instance(klass_node);
 193   // Write field values to memory
 194   store_values(kit, alloc_oop, alloc_oop, vk);
 195   region->init_req(2, kit->control());
 196   oop   ->init_req(2, alloc_oop);
 197   io    ->init_req(2, kit->i_o());
 198   mem   ->init_req(2, kit->merged_memory());
 199 
 200   // Update GraphKit
 201   kit->set_control(kit->gvn().transform(region));
 202   kit->set_i_o(kit->gvn().transform(io));
 203   kit->set_all_memory(kit->gvn().transform(mem));
 204   kit->record_for_igvn(region);
 205   kit->record_for_igvn(oop);
 206   kit->record_for_igvn(io);
 207   kit->record_for_igvn(mem);
 208 
 209   // Use cloned ValueTypeNode to propagate oop from now on
 210   Node* res_oop = kit->gvn().transform(oop);
 211   ValueTypeNode* vt = clone()->as_ValueType();
 212   vt->set_oop(res_oop);
 213   kit->replace_in_map(this, kit->gvn().transform(vt));
 214   return res_oop;
 215 }
 216 
 217 // Clones the values type to handle control flow merges involving multiple value types.
 218 // The inputs are replaced by PhiNodes to represent the merged values for the given region.
 219 ValueTypeNode* ValueTypeNode::clone_with_phis(PhaseGVN* gvn, Node* region) {
 220   assert(!has_phi_inputs(region), "already cloned with phis");
 221   ValueTypeNode* vt = clone()->as_ValueType();
 222 
 223   // Create a PhiNode for merging the oop values
 224   const TypeValueTypePtr* vtptr = TypeValueTypePtr::make(vt->bottom_type()->isa_valuetype());
 225   PhiNode* oop = PhiNode::make(region, vt->get_oop(), vtptr);
 226   gvn->set_type(oop, vtptr);
 227   vt->set_oop(oop);
 228 
 229   // Create a PhiNode each for merging the field values
 230   for (uint i = 0; i < vt->field_count(); ++i) {
 231     ciType* type = vt->field_type(i);
 232     Node*  value = vt->field_value(i);
 233     if (type->is_valuetype()) {
 234       // Handle flattened value type fields recursively
 235       value = value->as_ValueType()->clone_with_phis(gvn, region);
 236     } else {
 237       const Type* phi_type = Type::get_const_type(type);
 238       value = PhiNode::make(region, value, phi_type);
 239       gvn->set_type(value, phi_type);
 240     }
 241     vt->set_field_value(i, value);
 242   }
 243   gvn->set_type(vt, vt->bottom_type());
 244   return vt;
 245 }
 246 
 247 // Checks if the inputs of the ValueTypeNode were replaced by PhiNodes
 248 // for the given region (see ValueTypeNode::clone_with_phis).
 249 bool ValueTypeNode::has_phi_inputs(Node* region) {
 250   // Check oop input
 251   bool result = get_oop()->is_Phi() && get_oop()->as_Phi()->region() == region;
 252 #ifdef ASSERT
 253   if (result) {
 254     // Check all field value inputs for consistency
 255     for (uint i = Oop; i < field_count(); ++i) {
 256       Node* n = in(i);
 257       if (n->is_ValueType()) {
 258         assert(n->as_ValueType()->has_phi_inputs(region), "inconsistent phi inputs");
 259       } else {
 260         assert(n->is_Phi() && n->as_Phi()->region() == region, "inconsistent phi inputs");
 261       }
 262     }
 263   }
 264 #endif
 265   return result;
 266 }
 267 
 268 // Merges 'this' with 'other' by updating the input PhiNodes added by 'clone_with_phis'
 269 ValueTypeNode* ValueTypeNode::merge_with(PhaseGVN* gvn, const ValueTypeNode* other, int pnum, bool transform) {
 270   // Merge oop inputs
 271   PhiNode* phi = get_oop()->as_Phi();
 272   phi->set_req(pnum, other->get_oop());
 273   if (transform) {
 274     set_oop(gvn->transform(phi));
 275     gvn->record_for_igvn(phi);
 276   }
 277   // Merge field values
 278   for (uint i = 0; i < field_count(); ++i) {
 279     Node* val1 =        field_value(i);
 280     Node* val2 = other->field_value(i);
 281     if (val1->isa_ValueType()) {
 282       val1->as_ValueType()->merge_with(gvn, val2->as_ValueType(), pnum, transform);
 283     } else {
 284       assert(val1->is_Phi(), "must be a phi node");
 285       assert(!val2->is_ValueType(), "inconsistent merge values");
 286       val1->set_req(pnum, val2);
 287     }
 288     if (transform) {
 289       set_field_value(i, gvn->transform(val1));
 290       gvn->record_for_igvn(val1);
 291     }
 292   }
 293   return this;
 294 }
 295 
 296 Node* ValueTypeNode::field_value(uint index) const {
 297   assert(index < field_count(), "index out of bounds");
 298   return in(Values + index);
 299 }
 300 
 301 // Get the value of the field at the given offset.
 302 // If 'recursive' is true, flattened value type fields will be resolved recursively.
 303 Node* ValueTypeNode::field_value_by_offset(int offset, bool recursive) const {
 304   // If the field at 'offset' belongs to a flattened value type field, 'index' refers to the
 305   // corresponding ValueTypeNode input and 'sub_offset' is the offset in flattened value type.
 306   int index = value_klass()->field_index_by_offset(offset);
 307   int sub_offset = offset - field_offset(index);
 308   Node* value = field_value(index);
 309   if (recursive && value->is_ValueType()) {
 310     // Flattened value type field
 311     ValueTypeNode* vt = value->as_ValueType();
 312     sub_offset += vt->value_klass()->first_field_offset(); // Add header size
 313     return vt->field_value_by_offset(sub_offset);
 314   }
 315   assert(!(recursive && value->is_ValueType()), "should not be a value type");
 316   assert(sub_offset == 0, "offset mismatch");
 317   return value;
 318 }
 319 
 320 void ValueTypeNode::set_field_value(uint index, Node* value) {
 321   assert(index < field_count(), "index out of bounds");
 322   set_req(Values + index, value);
 323 }
 324 
 325 int ValueTypeNode::field_offset(uint index) const {
 326   assert(index < field_count(), "index out of bounds");
 327   return value_klass()->field_offset_by_index(index);
 328 }
 329 
 330 ciType* ValueTypeNode::field_type(uint index) const {
 331   assert(index < field_count(), "index out of bounds");
 332   return value_klass()->field_type_by_index(index);
 333 }
 334 
 335 void ValueTypeNode::make_scalar_in_safepoints(Compile* C) {
 336   const TypeValueTypePtr* res_type = TypeValueTypePtr::make(bottom_type()->isa_valuetype(), TypePtr::NotNull);
 337   ciValueKlass* vk = value_klass();
 338   uint nfields = vk->flattened_field_count();
 339   for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
 340     Node* u = fast_out(i);
 341     if (u->is_SafePoint() && (!u->is_Call() || u->as_Call()->has_debug_use(this))) {
 342       Node* in_oop = get_oop();
 343       const Type* oop_type = in_oop->bottom_type();
 344       SafePointNode* sfpt = u->as_SafePoint();
 345       JVMState* jvms = sfpt->jvms();
 346       int start = jvms->debug_start();
 347       int end   = jvms->debug_end();
 348       assert(TypePtr::NULL_PTR->higher_equal(oop_type), "already heap allocated value type should be linked directly");
 349       // Replace safepoint edge by SafePointScalarObjectNode and add field values
 350       assert(jvms != NULL, "missing JVMS");
 351       uint first_ind = (sfpt->req() - jvms->scloff());
 352       SafePointScalarObjectNode* sobj = new SafePointScalarObjectNode(res_type,
 353 #ifdef ASSERT
 354                                                                       NULL,
 355 #endif
 356                                                                       first_ind, nfields);
 357       sobj->init_req(0, C->root());
 358       // Iterate over the value type fields in order of increasing
 359       // offset and add the field values to the safepoint.
 360       for (uint j = 0; j < nfields; ++j) {
 361         int offset = vk->nonstatic_field_at(j)->offset();
 362         Node* value = field_value_by_offset(offset, true /* include flattened value type fields */);
 363         sfpt->add_req(value);
 364       }
 365       jvms->set_endoff(sfpt->req());
 366       int nb = sfpt->replace_edges_in_range(this, sobj, start, end);
 367       --i; imax -= nb;
 368     }
 369   }
 370 }
 371 
 372 uint ValueTypeNode::set_arguments_for_java_call(CallJavaNode* call, int base_input, const GraphKit& kit, ciValueKlass* base_vk, int base_offset) {
 373   ciValueKlass* vk = value_klass();
 374   if (base_vk == NULL) {
 375     base_vk = vk;
 376   }
 377   uint edges = 0;
 378   for (uint i = 0; i < field_count(); i++) {
 379     ciType* f_type = field_type(i);
 380     int offset = base_offset + field_offset(i) - (base_offset > 0 ? vk->first_field_offset() : 0);
 381     Node* arg = field_value(i);
 382     if (f_type->is_valuetype()) {
 383       ciValueKlass* embedded_vk = f_type->as_value_klass();
 384       edges += arg->as_ValueType()->set_arguments_for_java_call(call, base_input, kit, base_vk, offset);
 385     } else {
 386       int j = 0; int extra = 0;
 387       for (; j < base_vk->nof_nonstatic_fields(); j++) {
 388         ciField* f = base_vk->nonstatic_field_at(j);
 389         if (offset == f->offset()) {
 390           assert(f->type() == f_type, "inconsistent field type");
 391           break;
 392         }
 393         BasicType bt = f->type()->basic_type();
 394         if (bt == T_LONG || bt == T_DOUBLE) {
 395           extra++;
 396         }
 397       }
 398       call->init_req(base_input + j + extra, arg);
 399       edges++;
 400       BasicType bt = f_type->basic_type();
 401       if (bt == T_LONG || bt == T_DOUBLE) {
 402         call->init_req(base_input + j + extra + 1, kit.top());
 403         edges++;
 404       }
 405     }
 406   }
 407   return edges;
 408 }
 409 
 410 Node* ValueTypeNode::Ideal(PhaseGVN* phase, bool can_reshape) {
 411   if (can_reshape) {
 412     PhaseIterGVN* igvn = phase->is_IterGVN();
 413     const Type* oop_type = igvn->type(get_oop());
 414     if (oop_type->meet(TypePtr::NULL_PTR) != oop_type) {
 415       // Value type is heap allocated, search for safepoint uses
 416       for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
 417         Node* out = fast_out(i);
 418         if (out->is_SafePoint()) {
 419           // Let SafePointNode::Ideal() take care of re-wiring the
 420           // safepoint to the oop input instead of the value type node.
 421           igvn->rehash_node_delayed(out);
 422         }
 423       }
 424     }
 425   }
 426 
 427   return NULL;
 428 }
 429 
 430 #ifndef PRODUCT
 431 
 432 void ValueTypeNode::dump_spec(outputStream* st) const {
 433   TypeNode::dump_spec(st);
 434 }
 435 
 436 #endif