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